1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
32
33
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
62
63
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
76
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 }