View Javadoc
1   /*
2    * Copyright 2024 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * Encodes a limited set of GeneralName types, no decoding is supported.
34   * See ITU-T X.509 (10/2019) Section 9.3.2.1, or RFC 5280 Section 4.2.1.6.
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  }