1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.pkitesting;
17
18 import io.netty.util.NetUtil;
19 import org.bouncycastle.asn1.ASN1Encodable;
20 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
21 import org.bouncycastle.asn1.ASN1Primitive;
22 import org.bouncycastle.asn1.DERSequence;
23 import org.bouncycastle.asn1.DERTaggedObject;
24 import org.bouncycastle.asn1.x509.GeneralName;
25
26 import java.io.IOException;
27 import java.io.UncheckedIOException;
28 import java.net.URI;
29 import java.net.URISyntaxException;
30 import javax.security.auth.x500.X500Principal;
31
32
33
34
35
36 final class GeneralNameUtils {
37 private GeneralNameUtils() {
38 }
39
40 static GeneralName otherName(String oid, byte[] value) {
41 try {
42 DERSequence wrappedValue = new DERSequence(new ASN1Encodable[]{
43 new ASN1ObjectIdentifier(oid),
44 new DERTaggedObject(true, 0, ASN1Primitive.fromByteArray(value))
45 });
46 return new GeneralName(GeneralName.otherName, wrappedValue);
47 } catch (IOException e) {
48 throw new UncheckedIOException(e);
49 }
50 }
51
52 static GeneralName rfc822Name(String emailAddress) {
53 return new GeneralName(GeneralName.rfc822Name, emailAddress);
54 }
55
56 static GeneralName dnsName(String dnsName) {
57 URI uri = URI.create("ip://" + dnsName);
58 String host = uri.getHost();
59 return new GeneralName(GeneralName.dNSName, host);
60 }
61
62 static GeneralName directoryName(String x500Name) {
63 return directoryName(new X500Principal(x500Name));
64 }
65
66 static GeneralName directoryName(X500Principal name) {
67 try {
68 return new GeneralName(GeneralName.directoryName, ASN1Primitive.fromByteArray(name.getEncoded()));
69 } catch (IOException e) {
70 throw new UncheckedIOException(e);
71 }
72 }
73
74 static GeneralName uriName(String uri) throws URISyntaxException {
75 return uriName(new URI(uri));
76 }
77
78 static GeneralName uriName(URI uri) {
79 return new GeneralName(GeneralName.uniformResourceIdentifier, uri.toASCIIString());
80 }
81
82 static GeneralName ipAddress(String ipAddress) {
83 if (!NetUtil.isValidIpV4Address(ipAddress) && !NetUtil.isValidIpV6Address(ipAddress)) {
84 throw new IllegalArgumentException("Not a valid IP address: " + ipAddress);
85 }
86 return new GeneralName(GeneralName.iPAddress, ipAddress);
87 }
88
89 static GeneralName registeredId(String oid) {
90 return new GeneralName(GeneralName.registeredID, oid);
91 }
92 }