public X509Certificate generateCertificate ( String subjectDN , String issuerDN , KeyPair pair , PrivateKey privkey , int days , String signingAlgorithm ) throws CertificateException { if ( privkey == null ) { privkey = pair . getPrivate ( ) ; } X500Name issuer ; if ( issuerDN == null ) { issuer = new X500Name ( RFC4519Style . INSTANCE , subjectDN ) ; } else { issuer = new X500Name ( RFC4519Style . INSTANCE , issuerDN ) ; } long now = System . currentTimeMillis ( ) ; BigInteger serial = BigInteger . probablePrime ( 64 , new SecureRandom ( Primitives . writeLong ( now ) ) ) ; X500Name subject = new X500Name ( RFC4519Style . INSTANCE , subjectDN ) ; PublicKey publicKey = pair . getPublic ( ) ; byte [ ] encoded = publicKey . getEncoded ( ) ; SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfo . getInstance ( encoded ) ; X509v3CertificateBuilder builder = new X509v3CertificateBuilder ( issuer , serial , new Date ( now - 86400000l ) , new Date ( now + days * 86400000l ) , subject , subjectPublicKeyInfo ) ; X509CertificateHolder holder = builder . build ( createSigner ( privkey , signingAlgorithm ) ) ; return new JcaX509CertificateConverter ( ) . getCertificate ( holder ) ; }
Create a signed X . 509 Certificate