|
| 1 | +package com.baeldung.bouncycastle; |
| 2 | + |
| 3 | +import org.bouncycastle.asn1.x500.X500Name; |
| 4 | +import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
| 5 | +import org.bouncycastle.asn1.x509.Certificate; |
| 6 | +import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; |
| 7 | +import org.bouncycastle.cert.X509CertificateHolder; |
| 8 | +import org.bouncycastle.cert.X509v3CertificateBuilder; |
| 9 | +import org.bouncycastle.crypto.params.AsymmetricKeyParameter; |
| 10 | +import org.bouncycastle.crypto.util.PrivateKeyFactory; |
| 11 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 12 | +import org.bouncycastle.operator.ContentSigner; |
| 13 | +import org.bouncycastle.operator.DefaultDigestAlgorithmIdentifierFinder; |
| 14 | +import org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder; |
| 15 | +import org.bouncycastle.operator.OperatorCreationException; |
| 16 | +import org.bouncycastle.operator.bc.BcRSAContentSignerBuilder; |
| 17 | +import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; |
| 18 | +import org.bouncycastle.pkcs.PKCS10CertificationRequest; |
| 19 | +import org.bouncycastle.pkcs.PKCS10CertificationRequestBuilder; |
| 20 | +import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder; |
| 21 | + |
| 22 | +import javax.security.auth.x500.X500Principal; |
| 23 | +import java.io.ByteArrayInputStream; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.math.BigInteger; |
| 27 | +import java.security.*; |
| 28 | +import java.security.cert.CertificateException; |
| 29 | +import java.security.cert.CertificateFactory; |
| 30 | +import java.security.cert.X509Certificate; |
| 31 | +import java.util.Date; |
| 32 | + |
| 33 | +public class SignCSRBouncyCastle { |
| 34 | + |
| 35 | + static { |
| 36 | + Security.addProvider(new BouncyCastleProvider()); |
| 37 | + } |
| 38 | + |
| 39 | + public X509Certificate signCSR(PKCS10CertificationRequest inputCSR, PrivateKey caPrivate, KeyPair pair) throws OperatorCreationException, CertificateException, NoSuchProviderException, IOException { |
| 40 | + AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder() |
| 41 | + .find("SHA1withRSA"); |
| 42 | + AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder() |
| 43 | + .find(sigAlgId); |
| 44 | + AsymmetricKeyParameter foo = PrivateKeyFactory.createKey(caPrivate |
| 45 | + .getEncoded()); |
| 46 | + SubjectPublicKeyInfo keyInfo = SubjectPublicKeyInfo.getInstance(pair |
| 47 | + .getPublic().getEncoded()); |
| 48 | + |
| 49 | + X509v3CertificateBuilder myCertificateGenerator = new X509v3CertificateBuilder( |
| 50 | + new X500Name("CN=issuer"), new BigInteger("1"), new Date( |
| 51 | + System.currentTimeMillis()), new Date( |
| 52 | + System.currentTimeMillis() + 30L * 365 * 24 * 60 * 60 |
| 53 | + * 1000), inputCSR.getSubject(), keyInfo); |
| 54 | + |
| 55 | + ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId) |
| 56 | + .build(foo); |
| 57 | + |
| 58 | + X509CertificateHolder holder = myCertificateGenerator.build(sigGen); |
| 59 | + Certificate eeX509CertificateStructure = holder.toASN1Structure(); |
| 60 | + CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC"); |
| 61 | + |
| 62 | + InputStream is1 = new ByteArrayInputStream(eeX509CertificateStructure.getEncoded()); |
| 63 | + X509Certificate theCert = (X509Certificate) cf.generateCertificate(is1); |
| 64 | + is1.close(); |
| 65 | + return theCert; |
| 66 | + } |
| 67 | + |
| 68 | + public static KeyPair generateRSAKeyPair() throws NoSuchAlgorithmException { |
| 69 | + KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); |
| 70 | + keyPairGenerator.initialize(2048); |
| 71 | + return keyPairGenerator.generateKeyPair(); |
| 72 | + } |
| 73 | + |
| 74 | + public static PKCS10CertificationRequest generateCSR(KeyPair pair) throws OperatorCreationException { |
| 75 | + PKCS10CertificationRequestBuilder p10Builder = new JcaPKCS10CertificationRequestBuilder( |
| 76 | + new X500Principal("CN=Requested Test Certificate"), pair.getPublic()); |
| 77 | + JcaContentSignerBuilder csBuilder = new JcaContentSignerBuilder("SHA256withRSA"); |
| 78 | + ContentSigner signer = csBuilder.build(pair.getPrivate()); |
| 79 | + return p10Builder.build(signer); |
| 80 | + } |
| 81 | +} |
0 commit comments