99import java .io .StringReader ;
1010import java .net .InetAddress ;
1111import java .security .InvalidAlgorithmParameterException ;
12- import java .security .InvalidKeyException ;
1312import java .security .KeyPair ;
1413import java .security .KeyPairGenerator ;
1514import java .security .NoSuchAlgorithmException ;
2524import java .util .Collection ;
2625import java .util .List ;
2726import java .util .Objects ;
28- import java .util .Vector ;
2927import javax .security .auth .x500 .X500Principal ;
3028import org .bouncycastle .asn1 .ASN1ObjectIdentifier ;
3129import org .bouncycastle .asn1 .DEROctetString ;
32- import org .bouncycastle .asn1 .DERSet ;
33- import org .bouncycastle .asn1 .pkcs .Attribute ;
3430import org .bouncycastle .asn1 .pkcs .PKCSObjectIdentifiers ;
3531import org .bouncycastle .asn1 .x500 .X500NameBuilder ;
3632import org .bouncycastle .asn1 .x500 .style .BCStyle ;
33+ import org .bouncycastle .asn1 .x509 .Extension ;
34+ import org .bouncycastle .asn1 .x509 .ExtensionsGenerator ;
3735import org .bouncycastle .asn1 .x509 .GeneralName ;
3836import org .bouncycastle .asn1 .x509 .GeneralNames ;
39- import org .bouncycastle .asn1 .x509 .X509Extension ;
40- import org .bouncycastle .asn1 .x509 .X509Extensions ;
41- import org .bouncycastle .jce .PKCS10CertificationRequest ;
37+ import org .bouncycastle .openssl .jcajce .JcaPEMKeyConverter ;
38+ import org .bouncycastle .operator .AlgorithmNameFinder ;
39+ import org .bouncycastle .operator .DefaultAlgorithmNameFinder ;
40+ import org .bouncycastle .pkcs .PKCS10CertificationRequest ;
41+ import org .bouncycastle .operator .ContentSigner ;
42+ import org .bouncycastle .operator .jcajce .JcaContentSignerBuilder ;
43+ import org .bouncycastle .pkcs .PKCS10CertificationRequestBuilder ;
44+ import org .bouncycastle .pkcs .jcajce .JcaPKCS10CertificationRequestBuilder ;
4245import org .bouncycastle .util .io .pem .PemReader ;
4346import com .google .common .annotations .VisibleForTesting ;
4447import com .venafi .vcert .sdk .SignatureAlgorithm ;
@@ -111,7 +114,12 @@ public void generatePrivateKey() throws VCertException {
111114
112115 public void generateCSR () throws VCertException {
113116 try {
114- List <GeneralName > sans = new ArrayList <GeneralName >();
117+ List <GeneralName > sans = new ArrayList <>();
118+
119+ PKCS10CertificationRequestBuilder requestBuilder =
120+ new JcaPKCS10CertificationRequestBuilder (subject .toX500Principal (), keyPair .getPublic ());
121+ JcaContentSignerBuilder signerBuilder = new JcaContentSignerBuilder (signatureAlgorithm .standardName ());
122+ ContentSigner signer = signerBuilder .build (keyPair .getPrivate ());
115123
116124 for (String san : dnsNames ) {
117125 sans .add (new GeneralName (GeneralName .dNSName , san ));
@@ -122,21 +130,14 @@ public void generateCSR() throws VCertException {
122130 for (String san : emailAddresses ) {
123131 sans .add (new GeneralName (GeneralName .rfc822Name , san ));
124132 }
133+ if (!sans .isEmpty ()){
134+ GeneralNames names = new GeneralNames (sans .toArray (new GeneralName []{}));
125135
126- GeneralNames names = new GeneralNames (sans .toArray (new GeneralName [] {}));
127- Vector oids = new Vector ();
128- Vector values = new Vector ();
129-
130- oids .add (X509Extensions .SubjectAlternativeName );
131- values .add (new X509Extension (false , new DEROctetString (names )));
132-
133- X509Extensions extensions = new X509Extensions (oids , values );
134- Attribute attribute =
135- new Attribute (PKCSObjectIdentifiers .pkcs_9_at_extensionRequest , new DERSet (extensions ));
136-
137- PKCS10CertificationRequest certificationRequest = new PKCS10CertificationRequest (
138- signatureAlgorithm .standardName (), subject .toX500Principal (), keyPair .getPublic (),
139- new DERSet (attribute ), keyPair .getPrivate ());
136+ ExtensionsGenerator extGen = new ExtensionsGenerator ();
137+ extGen .addExtension (Extension .subjectAlternativeName , false , names );
138+ requestBuilder .addAttribute (PKCSObjectIdentifiers .pkcs_9_at_extensionRequest , extGen .generate ());
139+ }
140+ PKCS10CertificationRequest certificationRequest = requestBuilder .build (signer );
140141
141142 ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
142143 outputStream .write ("-----BEGIN CERTIFICATE REQUEST-----" .getBytes ());
@@ -274,7 +275,7 @@ public boolean checkCertificate(Certificate certificate) throws VCertException {
274275 || certCurve != csrCurve //
275276 && (!certCurve .getA ().equals (csrCurve .getA ()) //
276277 || !certCurve .getB ().equals (csrCurve .getB ()) //
277- || certField .getFieldSize () != csrField .getFieldSize ()))) {
278+ || certField .getFieldSize () != csrField .getFieldSize ()))) {
278279 throw new VCertException ("unmatched parameters for elliptic keys" );
279280 }
280281 break ;
@@ -287,25 +288,28 @@ public boolean checkCertificate(Certificate certificate) throws VCertException {
287288 PKCS10CertificationRequest csr =
288289 new PKCS10CertificationRequest (pemReader .readPemObject ().getContent ());
289290 pemReader .close ();
291+ AlgorithmNameFinder nameFinder = new DefaultAlgorithmNameFinder ();
292+ JcaPEMKeyConverter converter = new JcaPEMKeyConverter ();
290293
291294 PublicKeyAlgorithm csrPublicKeyAlgorithm =
292- PublicKeyAlgorithm .valueOf (csr .getPublicKey ("BC" ).getAlgorithm ());
295+ PublicKeyAlgorithm .valueOf (String .valueOf (nameFinder .getAlgorithmName (csr .getSubjectPublicKeyInfo ().getAlgorithm ())));
296+
293297 if (publicKeyAlgorithm != csrPublicKeyAlgorithm ) {
294298 throw new VCertException (
295- format ("unmatched key type: %s, %s" , publicKeyAlgorithm , csrPublicKeyAlgorithm ));
299+ format ("unmatched key type: %s, %s" , publicKeyAlgorithm , csrPublicKeyAlgorithm ));
296300 }
297301
298302 switch (csrPublicKeyAlgorithm ) {
299303 case RSA :
300304 RSAPublicKey certPublicKey = (RSAPublicKey ) certificate .getPublicKey ();
301- RSAPublicKey reqPublicKey = (RSAPublicKey ) csr .getPublicKey ();
305+ RSAPublicKey reqPublicKey = (RSAPublicKey ) converter .getPublicKey (csr . getSubjectPublicKeyInfo () );
302306 if (certPublicKey .getModulus ().compareTo (reqPublicKey .getModulus ()) != 0 ) {
303307 throw new VCertException ("unmatched key modules" );
304308 }
305309 break ;
306310 case ECDSA :
307311 ECPublicKey certEcPublicKey = (ECPublicKey ) certificate .getPublicKey ();
308- ECPublicKey reqEcPublicKey = (ECPublicKey ) csr .getPublicKey ();
312+ ECPublicKey reqEcPublicKey = (ECPublicKey ) converter .getPublicKey (csr . getSubjectPublicKeyInfo () );
309313
310314 // https://stackoverflow.com/questions/24121801/how-to-verify-if-the-private-key-matches-with-the-certificate
311315 java .security .spec .ECParameterSpec certSpec = certEcPublicKey .getParams (),
@@ -326,8 +330,7 @@ public boolean checkCertificate(Certificate certificate) throws VCertException {
326330 }
327331 break ;
328332 }
329- } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeyException
330- | IOException e ) {
333+ } catch (IOException e ) {
331334 throw new VCertException (format ("bad csr: %s" , e .getMessage ()), e );
332335 }
333336 }
0 commit comments