Skip to content

Commit

Permalink
playframework#1298: Unified the private key fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
tazmaniax committed Mar 10, 2019
1 parent 411537d commit 17859b8
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions framework/src/play/server/ssl/SslHttpServerContextFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,20 @@ public PEMKeyManager() {

try (PEMParser keyReader = new PEMParser(new FileReader(Play.getFile(keyFile)))) {
final Object object = keyReader.readObject();


PrivateKeyInfo privateKeyInfo = null;
if (object instanceof PrivateKeyInfo) {
key = BouncyCastleProvider.getPrivateKey((PrivateKeyInfo)object);
} else if (object instanceof PEMEncryptedKeyPair || object instanceof PEMKeyPair) {
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
final KeyPair keyPair;
if (object instanceof PEMEncryptedKeyPair) {
PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder()
.build(Play.configuration.getProperty("certificate.password", "secret").toCharArray());
keyPair = converter.getKeyPair(((PEMEncryptedKeyPair) object).decryptKeyPair(decProv));
} else {
keyPair = converter.getKeyPair((PEMKeyPair) object);
}
key = keyPair.getPrivate();
privateKeyInfo = (PrivateKeyInfo)object;
} else if (object instanceof PEMKeyPair) {
privateKeyInfo = ((PEMKeyPair)object).getPrivateKeyInfo();
} else if (object instanceof PEMEncryptedKeyPair) {
PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder()
.build(Play.configuration.getProperty("certificate.password", "secret").toCharArray());
privateKeyInfo = ((PEMEncryptedKeyPair) object).decryptKeyPair(decProv).getPrivateKeyInfo();
} else {
throw new UnsupportedOperationException("Unsupported key type '" + object.getClass() + "'");
throw new UnsupportedOperationException("Unsupported PEM content '" + object.getClass() + "'");
}
key = BouncyCastleProvider.getPrivateKey(privateKeyInfo);

final File hostCertFile = Play.getFile(p.getProperty("certificate.file", "conf/host.cert"));
final Collection collection = new CertificateFactory().engineGenerateCertificates(new FileInputStream(hostCertFile));
Expand Down

0 comments on commit 17859b8

Please sign in to comment.