3838import org .apache .commons .codec .binary .Base64 ;
3939import org .apache .commons .io .FileUtils ;
4040import org .apache .commons .lang3 .StringUtils ;
41- import org .bouncycastle .asn1 .ASN1EncodableVector ;
42- import org .bouncycastle .asn1 .DERSet ;
4341import org .bouncycastle .asn1 .pkcs .PKCSObjectIdentifiers ;
44- import org .bouncycastle .asn1 .x509 .Attribute ;
4542import org .bouncycastle .asn1 .x509 .BasicConstraints ;
4643import org .bouncycastle .asn1 .x509 .ExtendedKeyUsage ;
44+ import org .bouncycastle .asn1 .x509 .Extension ;
45+ import org .bouncycastle .asn1 .x509 .ExtensionsGenerator ;
4746import org .bouncycastle .asn1 .x509 .GeneralName ;
4847import org .bouncycastle .asn1 .x509 .GeneralNames ;
4948import org .bouncycastle .asn1 .x509 .KeyPurposeId ;
5049import org .bouncycastle .asn1 .x509 .KeyUsage ;
51- import org .bouncycastle .asn1 .x509 .X509Extensions ;
52- import org .bouncycastle .asn1 .x509 .X509ExtensionsGenerator ;
5350import org .bouncycastle .crypto .prng .VMPCRandomGenerator ;
54- import org .bouncycastle .jce .PKCS10CertificationRequest ;
5551import org .bouncycastle .jce .X509Principal ;
52+ import org .bouncycastle .operator .ContentSigner ;
53+ import org .bouncycastle .operator .jcajce .JcaContentSignerBuilder ;
54+ import org .bouncycastle .pkcs .PKCS10CertificationRequestBuilder ;
55+ import org .bouncycastle .pkcs .jcajce .JcaPKCS10CertificationRequestBuilder ;
5656import org .bouncycastle .x509 .X509V3CertificateGenerator ;
5757import org .nhindirect .common .crypto .MutableKeyStoreProtectionManager ;
5858import org .nhindirect .common .crypto .WrappableKeyProtectionManager ;
@@ -610,7 +610,7 @@ public void createCSR(String[] args)
610610 // create the CSR
611611
612612 // create the extensions that we want
613- final X509ExtensionsGenerator extsGen = new X509ExtensionsGenerator ();
613+ final ExtensionsGenerator extsGen = new ExtensionsGenerator ();
614614
615615 // Key Usage
616616 int usage ;
@@ -621,32 +621,23 @@ else if (keyUsage.compareToIgnoreCase("DigitalSignature") == 0)
621621 else
622622 usage = KeyUsage .keyEncipherment | KeyUsage .digitalSignature ;
623623
624- extsGen .addExtension (X509Extensions . KeyUsage , true , new KeyUsage (usage ));
624+ extsGen .addExtension (Extension . keyUsage , true , new KeyUsage (usage ));
625625
626626 // Subject Alt Name
627627 int nameType = subjectAltName .contains ("@" ) ? GeneralName .rfc822Name : GeneralName .dNSName ;
628628 final GeneralNames altName = new GeneralNames (new GeneralName (nameType , subjectAltName ));
629- extsGen .addExtension (X509Extensions . SubjectAlternativeName , false , altName );
629+ extsGen .addExtension (Extension . subjectAlternativeName , false , altName );
630630
631631 // Extended Key Usage
632- final Vector <KeyPurposeId > purposes = new Vector <KeyPurposeId >();
633- purposes .add (KeyPurposeId .id_kp_emailProtection );
634- extsGen .addExtension (X509Extensions .ExtendedKeyUsage , false , new ExtendedKeyUsage (purposes ));
632+ ExtendedKeyUsage eku = new ExtendedKeyUsage (KeyPurposeId .id_kp_emailProtection );
633+
634+
635+ extsGen .addExtension (Extension .extendedKeyUsage , false , eku );
635636
636637 // Basic constraint
637638 final BasicConstraints bc = new BasicConstraints (false );
638- extsGen .addExtension (X509Extensions . BasicConstraints , true , bc );
639+ extsGen .addExtension (Extension . basicConstraints , true , bc );
639640
640- // create the extension requests
641- final X509Extensions exts = extsGen .generate ();
642-
643- final ASN1EncodableVector attributes = new ASN1EncodableVector ();
644- final Attribute attribute = new Attribute (PKCSObjectIdentifiers .pkcs_9_at_extensionRequest ,
645- new DERSet (exts .toASN1Primitive ()));
646-
647- attributes .add (attribute );
648-
649- final DERSet requestedAttributes = new DERSet (attributes );
650641
651642 // create the DN
652643 final StringBuilder dnBuilder = new StringBuilder ("CN=" ).append (commonName );
@@ -656,16 +647,20 @@ else if (keyUsage.compareToIgnoreCase("DigitalSignature") == 0)
656647
657648 final X500Principal subjectPrin = new X500Principal (dnBuilder .toString ());
658649
659- final X509Principal xName = new X509Principal (true , subjectPrin .getName ());
650+ //final X509Principal xName = new X509Principal(true, subjectPrin.getName());
651+ PKCS10CertificationRequestBuilder builder = new JcaPKCS10CertificationRequestBuilder (subjectPrin , storedCert .getPublicKey ());
652+ builder .setAttribute (PKCSObjectIdentifiers .pkcs_9_at_extensionRequest , extsGen .generate ());
653+
654+ JcaContentSignerBuilder csBuilder = new JcaContentSignerBuilder ("SHA256withRSA" );
655+ ContentSigner signer = csBuilder .build (privKey );
660656
661657 // create the CSR
662- final PKCS10CertificationRequest request = new PKCS10CertificationRequest ("SHA256WITHRSA" , xName , storedCert .getPublicKey (),
663- requestedAttributes , privKey , ks .getProvider ().getName ());
658+
664659
665- final byte [] encodedCSR = request .getEncoded ();
660+ final byte [] encodedCSR = builder . build ( signer ) .getEncoded ();
666661
667662 final String csrString = "-----BEGIN CERTIFICATE REQUEST-----\r \n " + Base64 .encodeBase64String (encodedCSR )
668- + "-----END CERTIFICATE REQUEST-----" ;
663+ + "\r \n -----END CERTIFICATE REQUEST-----" ;
669664
670665 final File csrFile = new File (alias + "-CSR.pem" );
671666 FileUtils .writeStringToFile (csrFile , csrString );
0 commit comments