View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.generator.sigstore.internal;
20  
21  import java.io.IOException;
22  import java.nio.charset.StandardCharsets;
23  import java.security.cert.X509Certificate;
24  
25  import org.bouncycastle.asn1.ASN1Primitive;
26  import org.bouncycastle.asn1.ASN1Sequence;
27  import org.bouncycastle.asn1.ASN1String;
28  import org.bouncycastle.asn1.DEROctetString;
29  
30  /**
31   * Helper to decode Fulcio OID data, see <a
32   * href="https://github.com/sigstore/fulcio/blob/main/docs/oid-info.md">Sigstore OID
33   * information</a>.
34   */
35  public class FulcioOidHelper {
36      private static final String SIGSTORE_OID_ROOT = "1.3.6.1.4.1.57264";
37      private static final String FULCIO_OID_ROOT = SIGSTORE_OID_ROOT + ".1";
38  
39      @Deprecated
40      private static final String FULCIO_ISSUER_OID = FULCIO_OID_ROOT + ".1";
41  
42      private static final String FULCIO_ISSUER_V2_OID = FULCIO_OID_ROOT + ".8";
43  
44      public static String getIssuer(X509Certificate cert) {
45          String issuerV2 = getIssuerV2(cert);
46          if (issuerV2 == null) {
47              return getIssuerV1(cert);
48          }
49          return issuerV2;
50      }
51  
52      @Deprecated
53      public static String getIssuerV1(X509Certificate cert) {
54          return getExtensionValue(cert, FULCIO_ISSUER_OID, true);
55      }
56  
57      public static String getIssuerV2(X509Certificate cert) {
58          return getExtensionValue(cert, FULCIO_ISSUER_V2_OID, false);
59      }
60  
61      /* Extracts the octets from an extension value and converts to utf-8 directly, it does NOT
62       * account for any ASN1 encoded value. If the extension value is an ASN1 object (like an
63       * ASN1 encoded string), you need to write a new extraction helper. */
64      private static String getExtensionValue(X509Certificate cert, String oid, boolean rawUtf8) {
65          byte[] extensionValue = cert.getExtensionValue(oid);
66  
67          if (extensionValue == null) {
68              return null;
69          }
70          try {
71              ASN1Primitive derObject = ASN1Sequence.fromByteArray(cert.getExtensionValue(oid));
72              if (derObject instanceof DEROctetString) {
73                  DEROctetString derOctetString = (DEROctetString) derObject;
74                  if (rawUtf8) {
75                      // this is unusual, but the octet is a raw utf8 string in fulcio land (no prefix of type)
76                      // and not an ASN1 object.
77                      return new String(derOctetString.getOctets(), StandardCharsets.UTF_8);
78                  }
79  
80                  derObject = ASN1Sequence.fromByteArray(derOctetString.getOctets());
81                  if (derObject instanceof ASN1String) {
82                      ASN1String s = (ASN1String) derObject;
83                      return s.getString();
84                  }
85              }
86              throw new RuntimeException(
87                      "Could not parse extension " + oid + " in certificate because it was not an octet string");
88          } catch (IOException ioe) {
89              throw new RuntimeException("Could not parse extension " + oid + " in certificate", ioe);
90          }
91      }
92  }