|
| 1 | +package com.retailsvc.http.internal; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.nio.file.Files; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.security.GeneralSecurityException; |
| 8 | +import java.security.KeyFactory; |
| 9 | +import java.security.KeyStore; |
| 10 | +import java.security.PrivateKey; |
| 11 | +import java.security.cert.Certificate; |
| 12 | +import java.security.cert.CertificateFactory; |
| 13 | +import java.security.spec.InvalidKeySpecException; |
| 14 | +import java.security.spec.PKCS8EncodedKeySpec; |
| 15 | +import java.util.Base64; |
| 16 | +import java.util.Collection; |
| 17 | +import javax.net.ssl.KeyManagerFactory; |
| 18 | +import javax.net.ssl.SSLContext; |
| 19 | + |
| 20 | +/** Loads a {@link SSLContext} from a PEM certificate chain and PEM PKCS#8 private key. */ |
| 21 | +public final class PemSslContext { |
| 22 | + |
| 23 | + private PemSslContext() {} |
| 24 | + |
| 25 | + public static SSLContext load(Path certChainPem, Path privateKeyPem) { |
| 26 | + Certificate[] chain = readCertificateChain(certChainPem); |
| 27 | + PrivateKey key = readPrivateKey(privateKeyPem); |
| 28 | + return buildSslContext(chain, key); |
| 29 | + } |
| 30 | + |
| 31 | + private static Certificate[] readCertificateChain(Path path) { |
| 32 | + byte[] bytes; |
| 33 | + try { |
| 34 | + bytes = Files.readAllBytes(path); |
| 35 | + } catch (IOException e) { |
| 36 | + throw new IllegalStateException("Cannot read TLS certificate chain: " + path, e); |
| 37 | + } |
| 38 | + try { |
| 39 | + CertificateFactory factory = CertificateFactory.getInstance("X.509"); |
| 40 | + Collection<? extends Certificate> certs = |
| 41 | + factory.generateCertificates(new ByteArrayInputStream(bytes)); |
| 42 | + return certs.toArray(new Certificate[0]); |
| 43 | + } catch (GeneralSecurityException e) { |
| 44 | + throw new IllegalStateException("Failed to parse TLS certificate chain from " + path, e); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private static PrivateKey readPrivateKey(Path path) { |
| 49 | + String pem; |
| 50 | + try { |
| 51 | + pem = Files.readString(path); |
| 52 | + } catch (IOException e) { |
| 53 | + throw new IllegalStateException("Cannot read TLS private key: " + path, e); |
| 54 | + } |
| 55 | + byte[] der; |
| 56 | + try { |
| 57 | + String base64 = |
| 58 | + pem.replace("-----BEGIN PRIVATE KEY-----", "") |
| 59 | + .replace("-----END PRIVATE KEY-----", "") |
| 60 | + .replaceAll("\\s+", ""); |
| 61 | + der = Base64.getDecoder().decode(base64); |
| 62 | + } catch (IllegalArgumentException e) { |
| 63 | + throw new IllegalStateException("Failed to parse TLS private key from " + path, e); |
| 64 | + } |
| 65 | + PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(der); |
| 66 | + try { |
| 67 | + return KeyFactory.getInstance("RSA").generatePrivate(spec); |
| 68 | + } catch (InvalidKeySpecException rsaFail) { |
| 69 | + throw new IllegalStateException("Failed to parse TLS private key from " + path, rsaFail); |
| 70 | + } catch (GeneralSecurityException e) { |
| 71 | + throw new IllegalStateException("Failed to parse TLS private key from " + path, e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private static SSLContext buildSslContext(Certificate[] chain, PrivateKey key) { |
| 76 | + try { |
| 77 | + KeyStore ks = KeyStore.getInstance("PKCS12"); |
| 78 | + ks.load(null, null); |
| 79 | + ks.setKeyEntry("server", key, new char[0], chain); |
| 80 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
| 81 | + kmf.init(ks, new char[0]); |
| 82 | + SSLContext ctx = SSLContext.getInstance("TLS"); |
| 83 | + ctx.init(kmf.getKeyManagers(), null, null); |
| 84 | + return ctx; |
| 85 | + } catch (GeneralSecurityException | IOException e) { |
| 86 | + throw new IllegalStateException("TLS certificate and private key do not match", e); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments