use of org.bouncycastle.openssl.PEMKeyPair in project vespa by vespa-engine.
the class PemKeyStore method loadPrivateKey.
private void loadPrivateKey(PEMParser parser) {
try {
Object object = parser.readObject();
PrivateKeyInfo privateKeyInfo;
if (object instanceof PEMKeyPair) {
// Legacy PKCS1
privateKeyInfo = ((PEMKeyPair) object).getPrivateKeyInfo();
} else if (object instanceof PrivateKeyInfo) {
// PKCS8
privateKeyInfo = (PrivateKeyInfo) object;
} else {
throw new UnsupportedOperationException("Expected " + PrivateKeyInfo.class + " or " + PEMKeyPair.class + ", got " + object.getClass());
}
Object nextObject = parser.readObject();
if (nextObject != null) {
throw new UnsupportedOperationException("Expected a single private key, but found a second element " + nextObject.getClass());
}
setPrivateKey(privateKeyInfo);
} catch (Exception e) {
throw throwUnchecked(e);
}
}
use of org.bouncycastle.openssl.PEMKeyPair in project cloudbreak by hortonworks.
the class PkiUtil method generateSignature.
public static String generateSignature(String privateKeyPem, byte[] data) {
RSAKeyParameters rsaKeyParameters = CACHE.get(privateKeyPem);
if (rsaKeyParameters == null) {
try (PEMParser pEMParser = new PEMParser(new StringReader(clarifyPemKey(privateKeyPem)))) {
PEMKeyPair pemKeyPair = (PEMKeyPair) pEMParser.readObject();
KeyFactory factory = KeyFactory.getInstance("RSA");
KeySpec publicKeySpec = new X509EncodedKeySpec(pemKeyPair.getPublicKeyInfo().getEncoded());
PublicKey publicKey = factory.generatePublic(publicKeySpec);
KeySpec privateKeySpec = new PKCS8EncodedKeySpec(pemKeyPair.getPrivateKeyInfo().getEncoded());
PrivateKey privateKey = factory.generatePrivate(privateKeySpec);
KeyPair kp = new KeyPair(publicKey, privateKey);
RSAPrivateKeySpec privKeySpec = factory.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class);
rsaKeyParameters = new RSAKeyParameters(true, privKeySpec.getModulus(), privKeySpec.getPrivateExponent());
CACHE.put(privateKeyPem, rsaKeyParameters);
} catch (NoSuchAlgorithmException | IOException | InvalidKeySpecException e) {
throw new SecurityException(e);
}
}
Signer signer = new PSSSigner(new RSAEngine(), new SHA256Digest(), SALT_LENGTH);
signer.init(true, rsaKeyParameters);
signer.update(data, 0, data.length);
try {
byte[] signature = signer.generateSignature();
return BaseEncoding.base64().encode(signature);
} catch (CryptoException e) {
throw new SecurityException(e);
}
}
use of org.bouncycastle.openssl.PEMKeyPair in project neo4j by neo4j.
the class PkiUtils method loadPrivateKey.
public static PrivateKey loadPrivateKey(Path privateKeyFile, String passPhrase) throws IOException {
if (passPhrase == null) {
passPhrase = "";
}
try (PEMParser r = new PEMParser(Files.newBufferedReader(privateKeyFile))) {
Object pemObject = r.readObject();
final JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(PROVIDER);
if (// -----BEGIN RSA/DSA/EC PRIVATE KEY----- Proc-Type: 4,ENCRYPTED
pemObject instanceof PEMEncryptedKeyPair) {
final PEMEncryptedKeyPair ckp = (PEMEncryptedKeyPair) pemObject;
final PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(passPhrase.toCharArray());
return converter.getKeyPair(ckp.decryptKeyPair(decProv)).getPrivate();
} else if (// -----BEGIN ENCRYPTED PRIVATE KEY-----
pemObject instanceof PKCS8EncryptedPrivateKeyInfo) {
try {
final PKCS8EncryptedPrivateKeyInfo encryptedInfo = (PKCS8EncryptedPrivateKeyInfo) pemObject;
final InputDecryptorProvider provider = new JceOpenSSLPKCS8DecryptorProviderBuilder().build(passPhrase.toCharArray());
final PrivateKeyInfo privateKeyInfo = encryptedInfo.decryptPrivateKeyInfo(provider);
return converter.getPrivateKey(privateKeyInfo);
} catch (PKCSException | OperatorCreationException e) {
throw new IOException("Unable to decrypt private key.", e);
}
} else if (// -----BEGIN PRIVATE KEY-----
pemObject instanceof PrivateKeyInfo) {
return converter.getPrivateKey((PrivateKeyInfo) pemObject);
} else if (// -----BEGIN RSA/DSA/EC PRIVATE KEY-----
pemObject instanceof PEMKeyPair) {
return converter.getKeyPair((PEMKeyPair) pemObject).getPrivate();
} else {
throw new IOException("Unrecognized private key format.");
}
}
}
use of org.bouncycastle.openssl.PEMKeyPair in project gocd by gocd.
the class GoAgentServerClientBuilder method getPrivateKey.
private PrivateKey getPrivateKey() throws IOException {
PrivateKey privateKey;
try (PEMParser reader = new PEMParser(new FileReader(this.sslPrivateKey, StandardCharsets.UTF_8))) {
Object pemObject = reader.readObject();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(new BouncyCastleProvider());
if (pemObject instanceof PEMEncryptedKeyPair) {
PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(passphrase());
KeyPair keyPair = converter.getKeyPair(((PEMEncryptedKeyPair) pemObject).decryptKeyPair(decProv));
privateKey = keyPair.getPrivate();
} else if (pemObject instanceof PEMKeyPair) {
KeyPair keyPair = converter.getKeyPair((PEMKeyPair) pemObject);
privateKey = keyPair.getPrivate();
} else if (pemObject instanceof PrivateKeyInfo) {
PrivateKeyInfo privateKeyInfo = (PrivateKeyInfo) pemObject;
privateKey = converter.getPrivateKey(privateKeyInfo);
} else {
throw new RuntimeException("Unable to parse key of type " + pemObject.getClass());
}
return privateKey;
}
}
use of org.bouncycastle.openssl.PEMKeyPair in project cas by apereo.
the class PrivateKeyFactoryBean method readPemPrivateKey.
private PrivateKey readPemPrivateKey() {
LOGGER.trace("Attempting to read as PEM [{}]", this.location);
try (val in = new InputStreamReader(this.location.getInputStream(), StandardCharsets.UTF_8);
val br = new BufferedReader(in);
val pp = new PEMParser(br)) {
val object = pp.readObject();
if (object instanceof PrivateKeyInfo) {
return new JcaPEMKeyConverter().getPrivateKey((PrivateKeyInfo) object);
}
if (object instanceof PEMKeyPair) {
val pemKeyPair = (PEMKeyPair) object;
val kp = new JcaPEMKeyConverter().getKeyPair(pemKeyPair);
return kp.getPrivate();
}
} catch (final Exception e) {
LOGGER.debug("Unable to read key", e);
}
return null;
}
Aggregations