|
1 | 1 | package com.javatmp.demo.crypto.crl; |
2 | 2 |
|
3 | | -import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
4 | | -import org.bouncycastle.asn1.x500.X500Name; |
| 3 | +import com.javatmp.demo.crypto.certificate.example.X509V1CreateExampleNew; |
5 | 4 | import org.bouncycastle.asn1.x509.BasicConstraints; |
| 5 | +import org.bouncycastle.asn1.x509.Extension; |
6 | 6 | import org.bouncycastle.asn1.x509.KeyUsage; |
7 | | -import org.bouncycastle.asn1.x509.SubjectKeyIdentifier; |
8 | | -import org.bouncycastle.asn1.x509.X509Extensions; |
9 | 7 | import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; |
| 8 | +import org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils; |
10 | 9 | import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder; |
11 | 10 | import org.bouncycastle.jce.provider.BouncyCastleProvider; |
12 | 11 | import org.bouncycastle.operator.ContentSigner; |
13 | | -import org.bouncycastle.operator.OperatorCreationException; |
14 | 12 | import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; |
15 | | -import org.bouncycastle.x509.X509V1CertificateGenerator; |
16 | | -import org.bouncycastle.x509.X509V3CertificateGenerator; |
17 | | -import org.bouncycastle.x509.extension.AuthorityKeyIdentifierStructure; |
18 | 13 |
|
19 | 14 | import javax.security.auth.x500.X500Principal; |
20 | | -import java.io.IOException; |
21 | 15 | import java.math.BigInteger; |
22 | | -import java.security.*; |
23 | | -import java.security.cert.CertificateException; |
| 16 | +import java.security.KeyPair; |
| 17 | +import java.security.PrivateKey; |
| 18 | +import java.security.PublicKey; |
24 | 19 | import java.security.cert.X509Certificate; |
25 | | -import java.util.Calendar; |
26 | 20 | import java.util.Date; |
27 | 21 |
|
28 | 22 | /** |
29 | 23 | * Chapter 7 Utils |
30 | 24 | */ |
31 | | -public class Utils extends com.javatmp.demo.crypto.certificate.Utils |
32 | | -{ |
| 25 | +public class Utils extends com.javatmp.demo.crypto.certificate.Utils { |
33 | 26 | private static final int VALIDITY_PERIOD = 7 * 24 * 60 * 60 * 1000; // one week |
34 | 27 |
|
35 | | - |
36 | 28 | /** |
37 | 29 | * Generate a sample V1 certificate to use as a CA root certificate |
38 | 30 | */ |
39 | 31 | public static X509Certificate generateRootCert(KeyPair pair) |
40 | | - throws Exception |
41 | | - { |
42 | | - X509V1CertificateGenerator certGen = new X509V1CertificateGenerator(); |
43 | | - |
44 | | - certGen.setSerialNumber(BigInteger.valueOf(1)); |
45 | | - certGen.setIssuerDN(new X500Principal("CN=Test CA Certificate")); |
46 | | - certGen.setNotBefore(new Date(System.currentTimeMillis())); |
47 | | - certGen.setNotAfter(new Date(System.currentTimeMillis() + VALIDITY_PERIOD)); |
48 | | - certGen.setSubjectDN(new X500Principal("CN=Test CA Certificate")); |
49 | | - certGen.setPublicKey(pair.getPublic()); |
50 | | - certGen.setSignatureAlgorithm("SHA1WithRSAEncryption"); |
51 | | - |
52 | | - return certGen.generateX509Certificate(pair.getPrivate(), "BC"); |
| 32 | + throws Exception { |
| 33 | + return X509V1CreateExampleNew.generateV1Certificate(pair,"CN=Test CA Certificate", "CN=Test CA Certificate", |
| 34 | + VALIDITY_PERIOD / 24 * 60 * 60 * 1000, "SHA1WithRSAEncryption"); |
53 | 35 | } |
54 | 36 |
|
55 | 37 | /** |
56 | 38 | * Generate a sample V3 certificate to use as an intermediate CA certificate |
57 | 39 | */ |
58 | | - public static X509Certificate generateIntermediateCert(PublicKey intKey, PrivateKey caKey, X509Certificate caCert) |
59 | | - throws Exception |
60 | | - { |
61 | | - X509V3CertificateGenerator certGen = new X509V3CertificateGenerator(); |
62 | | - |
63 | | - certGen.setSerialNumber(BigInteger.valueOf(1)); |
64 | | - certGen.setIssuerDN(caCert.getSubjectX500Principal()); |
65 | | - certGen.setNotBefore(new Date(System.currentTimeMillis())); |
66 | | - certGen.setNotAfter(new Date(System.currentTimeMillis() + VALIDITY_PERIOD)); |
67 | | - certGen.setSubjectDN(new X500Principal("CN=Test Intermediate Certificate")); |
68 | | - certGen.setPublicKey(intKey); |
69 | | - certGen.setSignatureAlgorithm("SHA1WithRSAEncryption"); |
70 | | - |
71 | | - certGen.addExtension(X509Extensions.AuthorityKeyIdentifier, false, new AuthorityKeyIdentifierStructure(caCert)); |
72 | | - certGen.addExtension(X509Extensions.SubjectKeyIdentifier, false, new SubjectKeyIdentifier(intKey.getEncoded())); |
73 | | - certGen.addExtension(X509Extensions.BasicConstraints, true, new BasicConstraints(0)); |
74 | | - certGen.addExtension(X509Extensions.KeyUsage, true, new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyCertSign | KeyUsage.cRLSign)); |
75 | | - |
76 | | - return certGen.generateX509Certificate(caKey, "BC"); |
| 40 | + public static X509Certificate generateIntermediateCert( |
| 41 | + PublicKey intKey, PrivateKey caKey, X509Certificate caCert) |
| 42 | + throws Exception { |
| 43 | + |
| 44 | + JcaX509v3CertificateBuilder certBuilder = |
| 45 | + new JcaX509v3CertificateBuilder( |
| 46 | + caCert.getSubjectX500Principal(), |
| 47 | + BigInteger.valueOf(1), |
| 48 | + new Date(System.currentTimeMillis()), |
| 49 | + new Date(System.currentTimeMillis() + VALIDITY_PERIOD), |
| 50 | + new X500Principal("CN=Test Intermediate Certificate"), |
| 51 | + intKey |
| 52 | + ); |
| 53 | + JcaX509ExtensionUtils utils = new JcaX509ExtensionUtils(); |
| 54 | + certBuilder.addExtension(Extension.authorityKeyIdentifier, |
| 55 | + false, utils.createAuthorityKeyIdentifier(caCert)); |
| 56 | + certBuilder.addExtension(Extension.subjectKeyIdentifier, |
| 57 | + false, utils.createSubjectKeyIdentifier(intKey)); |
| 58 | + certBuilder.addExtension(Extension.basicConstraints, true, new BasicConstraints(0)); |
| 59 | + certBuilder.addExtension(Extension.keyUsage, true, |
| 60 | + new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyCertSign | KeyUsage.cRLSign)); |
| 61 | + |
| 62 | + String issuerSignatureAlgorithm = "SHA256WithRSAEncryption"; // <-- Use appropriate signature algorithm based on your keyPair algorithm. |
| 63 | + ContentSigner issuerContentSigner = |
| 64 | + new JcaContentSignerBuilder(issuerSignatureAlgorithm) |
| 65 | + .build(caKey); |
| 66 | + |
| 67 | + return new JcaX509CertificateConverter() |
| 68 | + .setProvider(BouncyCastleProvider.PROVIDER_NAME) |
| 69 | + .getCertificate(certBuilder.build(issuerContentSigner)); |
77 | 70 | } |
78 | 71 |
|
79 | 72 | /** |
80 | 73 | * Generate a sample V3 certificate to use as an end entity certificate |
81 | 74 | */ |
82 | | - public static X509Certificate generateEndEntityCert(PublicKey entityKey, PrivateKey caKey, X509Certificate caCert) |
83 | | - throws Exception |
84 | | - { |
85 | | - X509V3CertificateGenerator certGen = new X509V3CertificateGenerator(); |
86 | | - |
87 | | - certGen.setSerialNumber(BigInteger.valueOf(1)); |
88 | | - certGen.setIssuerDN(caCert.getSubjectX500Principal()); |
89 | | - certGen.setNotBefore(new Date(System.currentTimeMillis())); |
90 | | - certGen.setNotAfter(new Date(System.currentTimeMillis() + VALIDITY_PERIOD)); |
91 | | - certGen.setSubjectDN(new X500Principal("CN=Test End Certificate")); |
92 | | - certGen.setPublicKey(entityKey); |
93 | | - certGen.setSignatureAlgorithm("SHA1WithRSAEncryption"); |
94 | | - |
95 | | - certGen.addExtension(X509Extensions.AuthorityKeyIdentifier, false, new AuthorityKeyIdentifierStructure(caCert)); |
96 | | - certGen.addExtension(X509Extensions.SubjectKeyIdentifier, false, new SubjectKeyIdentifier(entityKey.getEncoded())); |
97 | | - certGen.addExtension(X509Extensions.BasicConstraints, true, new BasicConstraints(false)); |
98 | | - certGen.addExtension(X509Extensions.KeyUsage, true, new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyEncipherment)); |
99 | | - |
100 | | - return certGen.generateX509Certificate(caKey, "BC"); |
101 | | - } |
102 | | - |
103 | | - public static X509Certificate selfSign(KeyPair keyPair, String subjectDN) throws OperatorCreationException, CertificateException, IOException, CertificateException { |
104 | | - Provider bcProvider = new BouncyCastleProvider(); |
105 | | - Security.addProvider(bcProvider); |
106 | | - |
107 | | - long now = System.currentTimeMillis(); |
108 | | - Date startDate = new Date(now); |
109 | | - |
110 | | - X500Name dnName = new X500Name(subjectDN); |
111 | | - BigInteger certSerialNumber = new BigInteger(Long.toString(now)); // <-- Using the current timestamp as the certificate serial number |
112 | | - |
113 | | - Calendar calendar = Calendar.getInstance(); |
114 | | - calendar.setTime(startDate); |
115 | | - calendar.add(Calendar.YEAR, 1); // <-- 1 Yr validity |
116 | | - |
117 | | - Date endDate = calendar.getTime(); |
118 | | - |
119 | | - String signatureAlgorithm = "SHA256WithRSA"; // <-- Use appropriate signature algorithm based on your keyPair algorithm. |
120 | | - |
121 | | - ContentSigner contentSigner = new JcaContentSignerBuilder(signatureAlgorithm).build(keyPair.getPrivate()); |
122 | | - |
123 | | - JcaX509v3CertificateBuilder certBuilder = new JcaX509v3CertificateBuilder(dnName, certSerialNumber, startDate, endDate, dnName, keyPair.getPublic()); |
124 | | - |
125 | | - // Extensions -------------------------- |
126 | | - |
127 | | - // Basic Constraint |
128 | | - BasicConstraints basicConstraints = new BasicConstraints(true); // <-- true for CA, false for EndEntity |
129 | | - |
130 | | - certBuilder.addExtension(new ASN1ObjectIdentifier("2.5.29.19"), true, basicConstraints); // Basic Constraints is usually marked as critical. |
131 | | - |
132 | | - // ------------------------------------- |
133 | | - |
134 | | - return new JcaX509CertificateConverter().setProvider(bcProvider).getCertificate(certBuilder.build(contentSigner)); |
| 75 | + public static X509Certificate generateEndEntityCert( |
| 76 | + PublicKey entityKey, PrivateKey caKey, X509Certificate caCert) |
| 77 | + throws Exception { |
| 78 | + |
| 79 | + JcaX509v3CertificateBuilder certBuilder = |
| 80 | + new JcaX509v3CertificateBuilder( |
| 81 | + caCert.getSubjectX500Principal(), |
| 82 | + BigInteger.valueOf(1), |
| 83 | + new Date(System.currentTimeMillis()), |
| 84 | + new Date(System.currentTimeMillis() + VALIDITY_PERIOD), |
| 85 | + new X500Principal("CN=Test End Certificate"), |
| 86 | + entityKey |
| 87 | + ); |
| 88 | + JcaX509ExtensionUtils utils = new JcaX509ExtensionUtils(); |
| 89 | + certBuilder.addExtension(Extension.authorityKeyIdentifier, |
| 90 | + false, utils.createAuthorityKeyIdentifier(caCert)); |
| 91 | + certBuilder.addExtension(Extension.subjectKeyIdentifier, |
| 92 | + false, utils.createSubjectKeyIdentifier(entityKey)); |
| 93 | + certBuilder.addExtension(Extension.basicConstraints, true, new BasicConstraints(false)); |
| 94 | + certBuilder.addExtension(Extension.keyUsage, true, |
| 95 | + new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyEncipherment)); |
| 96 | + |
| 97 | + String issuerSignatureAlgorithm = "SHA256WithRSAEncryption"; // <-- Use appropriate signature algorithm based on your keyPair algorithm. |
| 98 | + ContentSigner issuerContentSigner = |
| 99 | + new JcaContentSignerBuilder(issuerSignatureAlgorithm) |
| 100 | + .build(caKey); |
| 101 | + |
| 102 | + return new JcaX509CertificateConverter() |
| 103 | + .setProvider(BouncyCastleProvider.PROVIDER_NAME) |
| 104 | + .getCertificate(certBuilder.build(issuerContentSigner)); |
135 | 105 | } |
136 | 106 | } |
0 commit comments