1010
1111import org .bouncycastle .asn1 .ASN1ObjectIdentifier ;
1212import org .bouncycastle .asn1 .x500 .X500Name ;
13+ import org .bouncycastle .asn1 .x509 .AlgorithmIdentifier ;
1314import org .bouncycastle .asn1 .x509 .SubjectPublicKeyInfo ;
1415import org .bouncycastle .asn1 .x509 .Time ;
1516import org .bouncycastle .cert .X509v3CertificateBuilder ;
17+ import org .bouncycastle .asn1 .misc .MiscObjectIdentifiers ;
18+ import org .bouncycastle .jcajce .CompositePublicKey ;
1619
1720/**
1821 * JCA helper class to allow JCA objects to be used in the construction of a Version 3 certificate.
@@ -23,11 +26,11 @@ public class JcaX509v3CertificateBuilder
2326 /**
2427 * Initialise the builder using a PublicKey.
2528 *
26- * @param issuer X500Name representing the issuer of this certificate.
27- * @param serial the serial number for the certificate.
29+ * @param issuer X500Name representing the issuer of this certificate.
30+ * @param serial the serial number for the certificate.
2831 * @param notBefore date before which the certificate is not valid.
29- * @param notAfter date after which the certificate is not valid.
30- * @param subject X500Name representing the subject of this certificate.
32+ * @param notAfter date after which the certificate is not valid.
33+ * @param subject X500Name representing the subject of this certificate.
3134 * @param publicKey the public key to be associated with the certificate.
3235 */
3336 public JcaX509v3CertificateBuilder (X500Name issuer , BigInteger serial , Date notBefore , Date notAfter , X500Name subject , SubjectPublicKeyInfo publicKey )
@@ -38,58 +41,58 @@ public JcaX509v3CertificateBuilder(X500Name issuer, BigInteger serial, Date notB
3841 /**
3942 * Initialise the builder using a PublicKey.
4043 *
41- * @param issuer X500Name representing the issuer of this certificate.
42- * @param serial the serial number for the certificate.
44+ * @param issuer X500Name representing the issuer of this certificate.
45+ * @param serial the serial number for the certificate.
4346 * @param notBefore date before which the certificate is not valid.
44- * @param notAfter date after which the certificate is not valid.
45- * @param subject X500Name representing the subject of this certificate.
47+ * @param notAfter date after which the certificate is not valid.
48+ * @param subject X500Name representing the subject of this certificate.
4649 * @param publicKey the public key to be associated with the certificate.
4750 */
4851 public JcaX509v3CertificateBuilder (X500Name issuer , BigInteger serial , Date notBefore , Date notAfter , X500Name subject , PublicKey publicKey )
4952 {
50- super (issuer , serial , notBefore , notAfter , subject , SubjectPublicKeyInfo . getInstance (publicKey . getEncoded () ));
53+ super (issuer , serial , notBefore , notAfter , subject , getSubjectPublicKeyInfo (publicKey ));
5154 }
5255
5356 /**
5457 * Initialise the builder using a PublicKey.
5558 *
56- * @param issuer X500Name representing the issuer of this certificate.
57- * @param serial the serial number for the certificate.
59+ * @param issuer X500Name representing the issuer of this certificate.
60+ * @param serial the serial number for the certificate.
5861 * @param notBefore Time before which the certificate is not valid.
59- * @param notAfter Time after which the certificate is not valid.
60- * @param subject X500Name representing the subject of this certificate.
62+ * @param notAfter Time after which the certificate is not valid.
63+ * @param subject X500Name representing the subject of this certificate.
6164 * @param publicKey the public key to be associated with the certificate.
6265 */
6366 public JcaX509v3CertificateBuilder (X500Name issuer , BigInteger serial , Time notBefore , Time notAfter , X500Name subject , PublicKey publicKey )
6467 {
65- super (issuer , serial , notBefore , notAfter , subject , SubjectPublicKeyInfo . getInstance (publicKey . getEncoded () ));
68+ super (issuer , serial , notBefore , notAfter , subject , getSubjectPublicKeyInfo (publicKey ));
6669 }
6770
6871 /**
6972 * Initialise the builder using X500Principal objects and a PublicKey.
7073 *
71- * @param issuer principal representing the issuer of this certificate.
72- * @param serial the serial number for the certificate.
74+ * @param issuer principal representing the issuer of this certificate.
75+ * @param serial the serial number for the certificate.
7376 * @param notBefore date before which the certificate is not valid.
74- * @param notAfter date after which the certificate is not valid.
75- * @param subject principal representing the subject of this certificate.
77+ * @param notAfter date after which the certificate is not valid.
78+ * @param subject principal representing the subject of this certificate.
7679 * @param publicKey the public key to be associated with the certificate.
7780 */
7881 public JcaX509v3CertificateBuilder (X500Principal issuer , BigInteger serial , Date notBefore , Date notAfter , X500Principal subject , PublicKey publicKey )
7982 {
80- super (X500Name .getInstance (issuer .getEncoded ()), serial , notBefore , notAfter , X500Name .getInstance (subject .getEncoded ()), SubjectPublicKeyInfo . getInstance (publicKey . getEncoded () ));
83+ super (X500Name .getInstance (issuer .getEncoded ()), serial , notBefore , notAfter , X500Name .getInstance (subject .getEncoded ()), getSubjectPublicKeyInfo (publicKey ));
8184 }
8285
8386 /**
8487 * Initialise the builder using the subject from the passed in issuerCert as the issuer, as well as
8588 * passing through and converting the other objects provided.
8689 *
8790 * @param issuerCert certificate who's subject is the issuer of the certificate we are building.
88- * @param serial the serial number for the certificate.
89- * @param notBefore date before which the certificate is not valid.
90- * @param notAfter date after which the certificate is not valid.
91- * @param subject principal representing the subject of this certificate.
92- * @param publicKey the public key to be associated with the certificate.
91+ * @param serial the serial number for the certificate.
92+ * @param notBefore date before which the certificate is not valid.
93+ * @param notAfter date after which the certificate is not valid.
94+ * @param subject principal representing the subject of this certificate.
95+ * @param publicKey the public key to be associated with the certificate.
9396 */
9497 public JcaX509v3CertificateBuilder (X509Certificate issuerCert , BigInteger serial , Date notBefore , Date notAfter , X500Principal subject , PublicKey publicKey )
9598 {
@@ -101,11 +104,11 @@ public JcaX509v3CertificateBuilder(X509Certificate issuerCert, BigInteger serial
101104 * passing through and converting the other objects provided.
102105 *
103106 * @param issuerCert certificate who's subject is the issuer of the certificate we are building.
104- * @param serial the serial number for the certificate.
105- * @param notBefore date before which the certificate is not valid.
106- * @param notAfter date after which the certificate is not valid.
107- * @param subject principal representing the subject of this certificate.
108- * @param publicKey the public key to be associated with the certificate.
107+ * @param serial the serial number for the certificate.
108+ * @param notBefore date before which the certificate is not valid.
109+ * @param notAfter date after which the certificate is not valid.
110+ * @param subject principal representing the subject of this certificate.
111+ * @param publicKey the public key to be associated with the certificate.
109112 */
110113 public JcaX509v3CertificateBuilder (X509Certificate issuerCert , BigInteger serial , Date notBefore , Date notAfter , X500Name subject , PublicKey publicKey )
111114 {
@@ -120,15 +123,15 @@ public JcaX509v3CertificateBuilder(X509Certificate issuerCert, BigInteger serial
120123 public JcaX509v3CertificateBuilder (X509Certificate template )
121124 throws CertificateEncodingException
122125 {
123- super (new JcaX509CertificateHolder (template ));
126+ super (new JcaX509CertificateHolder (template ));
124127 }
125128
126129 /**
127130 * Add a given extension field for the standard extensions tag (tag 3)
128131 * copying the extension value from another certificate.
129132 *
130- * @param oid the type of the extension to be copied.
131- * @param critical true if the extension is to be marked critical, false otherwise.
133+ * @param oid the type of the extension to be copied.
134+ * @param critical true if the extension is to be marked critical, false otherwise.
132135 * @param certificate the source of the extension to be copied.
133136 * @return the builder instance.
134137 */
@@ -142,4 +145,16 @@ public JcaX509v3CertificateBuilder copyAndAddExtension(
142145
143146 return this ;
144147 }
148+
149+ private static SubjectPublicKeyInfo getSubjectPublicKeyInfo (PublicKey publicKey )
150+ {
151+ if (publicKey instanceof CompositePublicKey && ((CompositePublicKey )publicKey ).getAlgorithmIdentifier ().on (MiscObjectIdentifiers .id_MLDSA_COMPSIG ))
152+ {
153+ return new SubjectPublicKeyInfo (new AlgorithmIdentifier (((CompositePublicKey )publicKey ).getAlgorithmIdentifier ()), publicKey .getEncoded ());
154+ }
155+ else
156+ {
157+ return SubjectPublicKeyInfo .getInstance (publicKey .getEncoded ());
158+ }
159+ }
145160}
0 commit comments