|
| 1 | +/* |
| 2 | + * Copyright 2020-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package sample; |
| 17 | + |
| 18 | +import java.io.FileOutputStream; |
| 19 | +import java.io.StringWriter; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.nio.file.Paths; |
| 23 | +import java.security.KeyPair; |
| 24 | +import java.security.KeyStore; |
| 25 | +import java.security.cert.Certificate; |
| 26 | +import java.security.cert.X509Certificate; |
| 27 | + |
| 28 | +import org.bouncycastle.openssl.jcajce.JcaPEMWriter; |
| 29 | + |
| 30 | +import org.springframework.boot.CommandLineRunner; |
| 31 | +import org.springframework.boot.SpringApplication; |
| 32 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 33 | + |
| 34 | +import static sample.BouncyCastleUtils.BC_PROVIDER; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Joe Grandja |
| 38 | + * @since 1.3 |
| 39 | + */ |
| 40 | +@SpringBootApplication |
| 41 | +public class X509CertificateGeneratorApplication implements CommandLineRunner { |
| 42 | + |
| 43 | + public static void main(String[] args) { |
| 44 | + SpringApplication.run(X509CertificateGeneratorApplication.class, args); |
| 45 | + } |
| 46 | + @Override |
| 47 | + public void run(String... args) throws Exception { |
| 48 | + String baseDistinguishedName = "OU=Spring Samples, O=Spring, C=US"; |
| 49 | + |
| 50 | + // Generate the Root certificate (Trust Anchor or most-trusted CA) and keystore file |
| 51 | + String commonName = "spring-samples-trusted-ca"; |
| 52 | + String rootCommonName = commonName; |
| 53 | + String distinguishedName = "CN=" + commonName + ", " + baseDistinguishedName; |
| 54 | + KeyPair rootKeyPair = BouncyCastleUtils.generateRSAKeyPair(); |
| 55 | + X509Certificate rootCertificate = BouncyCastleUtils.createTrustAnchorCertificate(rootKeyPair, distinguishedName); |
| 56 | + writeCertificatePEMEncoded(rootCertificate, "./samples/x509-certificate-generator/generated/" + commonName + ".pem"); |
| 57 | + createKeystoreFile(rootKeyPair, new Certificate[] {rootCertificate}, commonName, |
| 58 | + null, "./samples/x509-certificate-generator/generated/" + commonName + "-keystore.p12"); |
| 59 | + TrustedCertificateHolder[] rootTrustedCertificate = { new TrustedCertificateHolder(rootCertificate, rootCommonName) }; |
| 60 | + |
| 61 | + // Generate the CA (intermediary) certificate and keystore file |
| 62 | + commonName = "spring-samples-ca"; |
| 63 | + String caCommonName = commonName; |
| 64 | + distinguishedName = "CN=" + commonName + ", " + baseDistinguishedName; |
| 65 | + KeyPair caKeyPair = BouncyCastleUtils.generateRSAKeyPair(); |
| 66 | + X509Certificate caCertificate = BouncyCastleUtils.createCACertificate( |
| 67 | + rootCertificate, rootKeyPair.getPrivate(), caKeyPair.getPublic(), distinguishedName); |
| 68 | + writeCertificatePEMEncoded(caCertificate, "./samples/x509-certificate-generator/generated/" + commonName + ".pem"); |
| 69 | + createKeystoreFile(caKeyPair, new Certificate[] {caCertificate, rootCertificate}, commonName, |
| 70 | + rootTrustedCertificate, "./samples/x509-certificate-generator/generated/" + commonName + "-keystore.p12"); |
| 71 | + TrustedCertificateHolder[] caTrustedCertificate = { new TrustedCertificateHolder(caCertificate, caCommonName) }; |
| 72 | + |
| 73 | + // Generate the certificate and keystore file for the demo-client sample |
| 74 | + commonName = "demo-client-sample"; |
| 75 | + distinguishedName = "CN=" + commonName + ", " + baseDistinguishedName; |
| 76 | + KeyPair demoClientKeyPair = BouncyCastleUtils.generateRSAKeyPair(); |
| 77 | + X509Certificate demoClientCertificate = BouncyCastleUtils.createEndEntityCertificate( |
| 78 | + caCertificate, caKeyPair.getPrivate(), demoClientKeyPair.getPublic(), distinguishedName); |
| 79 | + demoClientCertificate.verify(caCertificate.getPublicKey(), BC_PROVIDER); |
| 80 | + createKeystoreFile(demoClientKeyPair, new Certificate[] {demoClientCertificate, caCertificate, rootCertificate}, commonName, |
| 81 | + caTrustedCertificate, "./samples/demo-client/src/main/resources/keystore.p12"); |
| 82 | + |
| 83 | + // Generate the certificate and keystore file for the messages-resource sample |
| 84 | + commonName = "messages-resource-sample"; |
| 85 | + distinguishedName = "CN=" + commonName + ", " + baseDistinguishedName; |
| 86 | + KeyPair messagesResourceKeyPair = BouncyCastleUtils.generateRSAKeyPair(); |
| 87 | + X509Certificate messagesResourceCertificate = BouncyCastleUtils.createEndEntityCertificate( |
| 88 | + caCertificate, caKeyPair.getPrivate(), messagesResourceKeyPair.getPublic(), distinguishedName); |
| 89 | + messagesResourceCertificate.verify(caCertificate.getPublicKey(), BC_PROVIDER); |
| 90 | + createKeystoreFile(messagesResourceKeyPair, new Certificate[] {messagesResourceCertificate, caCertificate, rootCertificate}, commonName, |
| 91 | + caTrustedCertificate, "./samples/messages-resource/src/main/resources/keystore.p12"); |
| 92 | + |
| 93 | + // Generate the certificate and keystore file for the demo-authorizationserver sample |
| 94 | + commonName = "demo-authorizationserver-sample"; |
| 95 | + distinguishedName = "CN=" + commonName + ", " + baseDistinguishedName; |
| 96 | + KeyPair demoAuthorizationServerKeyPair = BouncyCastleUtils.generateRSAKeyPair(); |
| 97 | + X509Certificate demoAuthorizationServerCertificate = BouncyCastleUtils.createEndEntityCertificate( |
| 98 | + caCertificate, caKeyPair.getPrivate(), demoAuthorizationServerKeyPair.getPublic(), distinguishedName); |
| 99 | + demoAuthorizationServerCertificate.verify(caCertificate.getPublicKey(), BC_PROVIDER); |
| 100 | + createKeystoreFile(demoAuthorizationServerKeyPair, new Certificate[] {demoAuthorizationServerCertificate, caCertificate, rootCertificate}, commonName, |
| 101 | + caTrustedCertificate, "./samples/demo-authorizationserver/src/main/resources/keystore.p12"); |
| 102 | + } |
| 103 | + |
| 104 | + private static void createKeystoreFile(KeyPair keyPair, Certificate[] certificateChain, String alias, |
| 105 | + TrustedCertificateHolder[] trustedCertificates, String fileName) throws Exception { |
| 106 | + |
| 107 | + KeyStore keyStore = KeyStore.getInstance("PKCS12", BC_PROVIDER); |
| 108 | + keyStore.load(null, null); |
| 109 | + keyStore.setKeyEntry(alias, keyPair.getPrivate(), "password".toCharArray(), certificateChain); |
| 110 | + if (trustedCertificates != null && trustedCertificates.length > 0) { |
| 111 | + for (TrustedCertificateHolder trustedCertificate : trustedCertificates) { |
| 112 | + keyStore.setCertificateEntry(trustedCertificate.alias, trustedCertificate.certificate); |
| 113 | + } |
| 114 | + } |
| 115 | + Path path = Paths.get(fileName); |
| 116 | + Path parent = path.getParent(); |
| 117 | + if (parent != null && Files.notExists(parent)) { |
| 118 | + Files.createDirectories(parent); |
| 119 | + } |
| 120 | + FileOutputStream fos = new FileOutputStream(fileName); |
| 121 | + keyStore.store(fos, "password".toCharArray()); |
| 122 | + } |
| 123 | + |
| 124 | + private static void writeCertificatePEMEncoded(Certificate certificate, String fileName) throws Exception { |
| 125 | + StringWriter sw = new StringWriter(); |
| 126 | + try (JcaPEMWriter jpw = new JcaPEMWriter(sw)) { |
| 127 | + jpw.writeObject(certificate); |
| 128 | + } |
| 129 | + String pem = sw.toString(); |
| 130 | + Path path = Paths.get(fileName); |
| 131 | + Path parent = path.getParent(); |
| 132 | + if (parent != null && Files.notExists(parent)) { |
| 133 | + Files.createDirectories(parent); |
| 134 | + } |
| 135 | + Files.write(path, pem.getBytes()); |
| 136 | + } |
| 137 | + |
| 138 | + private record TrustedCertificateHolder(Certificate certificate, String alias) { |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments