use of org.bouncycastle.openssl.PEMKeyPair in project incubator-pulsar by apache.
the class MessageCrypto method loadPrivateKey.
private PrivateKey loadPrivateKey(byte[] keyBytes) throws Exception {
Reader keyReader = new StringReader(new String(keyBytes));
PrivateKey privateKey = null;
try (PEMParser pemReader = new PEMParser(keyReader)) {
X9ECParameters ecParam = null;
Object pemObj = pemReader.readObject();
if (pemObj instanceof ASN1ObjectIdentifier) {
// make sure this is EC Parameter we're handling. In which case
// we'll store it and read the next object which should be our
// EC Private Key
ASN1ObjectIdentifier ecOID = (ASN1ObjectIdentifier) pemObj;
ecParam = ECNamedCurveTable.getByOID(ecOID);
if (ecParam == null) {
throw new PEMException("Unable to find EC Parameter for the given curve oid: " + ecOID.getId());
}
pemObj = pemReader.readObject();
} else if (pemObj instanceof X9ECParameters) {
ecParam = (X9ECParameters) pemObj;
pemObj = pemReader.readObject();
}
if (pemObj instanceof PEMKeyPair) {
PrivateKeyInfo pKeyInfo = ((PEMKeyPair) pemObj).getPrivateKeyInfo();
JcaPEMKeyConverter pemConverter = new JcaPEMKeyConverter();
privateKey = pemConverter.getPrivateKey(pKeyInfo);
}
if (ecParam != null && ECDSA.equals(privateKey.getAlgorithm())) {
ECParameterSpec ecSpec = new ECParameterSpec(ecParam.getCurve(), ecParam.getG(), ecParam.getN(), ecParam.getH(), ecParam.getSeed());
KeyFactory keyFactory = KeyFactory.getInstance(ECDSA, BouncyCastleProvider.PROVIDER_NAME);
ECPrivateKeySpec keySpec = new ECPrivateKeySpec(((BCECPrivateKey) privateKey).getS(), ecSpec);
privateKey = (PrivateKey) keyFactory.generatePrivate(keySpec);
}
} catch (IOException e) {
throw new Exception(e);
}
return privateKey;
}
use of org.bouncycastle.openssl.PEMKeyPair in project candlepin by candlepin.
the class CertificateReaderForTesting method readPrivateKey.
@Override
protected PrivateKey readPrivateKey(PrivateKeyReader reader) throws FileNotFoundException {
InputStream keyStream = this.getClass().getClassLoader().getResourceAsStream("test-ca.key");
KeyPair keyPair = null;
try (PEMParser parser = new PEMParser(new InputStreamReader(keyStream))) {
keyPair = new JcaPEMKeyConverter().setProvider(BC_PROVIDER).getKeyPair((PEMKeyPair) parser.readObject());
} catch (IOException e) {
Assert.fail("Could not load private key");
}
return keyPair.getPrivate();
}
use of org.bouncycastle.openssl.PEMKeyPair in project candlepin by candlepin.
the class PrivateKeyReaderTest method testReadPKCS1.
@Test
public void testReadPKCS1() throws Exception {
String keyFile = "keys/pkcs1-unencrypted.pem";
try (InputStream keyStream = cl.getResourceAsStream(keyFile);
Reader expectedReader = new InputStreamReader(cl.getResourceAsStream(keyFile))) {
PrivateKey actualKey = new PrivateKeyReader().read(keyStream, null);
PEMKeyPair expected = (PEMKeyPair) new PEMParser(expectedReader).readObject();
PrivateKey expectedKey = new JcaPEMKeyConverter().setProvider(BC_PROVIDER).getKeyPair(expected).getPrivate();
assertEquals(actualKey, expectedKey);
}
}
use of org.bouncycastle.openssl.PEMKeyPair in project certmgr by hdecarne.
the class PEMCertReaderWriter method readObjectsString.
/**
* Read all available certificate objects from a PEM encoded {@link Reader} resource.
*
* @param in The reader resource to read from.
* @param password The callback to use for querying passwords (if needed).
* @return The read certificate objects, or {@code null} if the input is not recognized.
* @throws IOException if an I/O error occurs while reading.
*/
@Nullable
public static CertObjectStore readObjectsString(IOResource<Reader> in, PasswordCallback password) throws IOException {
LOG.debug("Trying to read PEM objects from: ''{0}''...", in);
CertObjectStore certObjects = null;
try (PEMParser parser = new PEMParser(in.io())) {
Object pemObject;
try {
pemObject = parser.readObject();
} catch (IOException e) {
LOG.info(e, "No PEM objects recognized in: ''{0}''", in);
pemObject = null;
}
while (pemObject != null) {
if (certObjects == null) {
certObjects = new CertObjectStore();
}
LOG.info("Decoding PEM object of type {0}", pemObject.getClass().getName());
if (pemObject instanceof X509CertificateHolder) {
certObjects.addCRT(convertCRT((X509CertificateHolder) pemObject));
} else if (pemObject instanceof PEMKeyPair) {
certObjects.addKey(convertKey((PEMKeyPair) pemObject));
} else if (pemObject instanceof PEMEncryptedKeyPair) {
certObjects.addKey(convertKey((PEMEncryptedKeyPair) pemObject, in.resource(), password));
} else if (pemObject instanceof PKCS10CertificationRequest) {
certObjects.addCSR(convertCSR((PKCS10CertificationRequest) pemObject));
} else if (pemObject instanceof X509CRLHolder) {
certObjects.addCRL(convertCRL((X509CRLHolder) pemObject));
} else {
LOG.warning("Ignoring unrecognized PEM object of type {0}", pemObject.getClass().getName());
}
pemObject = parser.readObject();
}
}
return certObjects;
}
use of org.bouncycastle.openssl.PEMKeyPair in project certmgr by hdecarne.
the class PEMCertReaderWriter method convertKey.
private static KeyPair convertKey(PEMEncryptedKeyPair pemObject, String resource, PasswordCallback password) throws IOException {
PEMKeyPair pemKeyPair = null;
Throwable passwordException = null;
while (pemKeyPair == null) {
char[] passwordChars = (passwordException == null ? password.queryPassword(resource) : password.requeryPassword(resource, passwordException));
if (passwordChars == null) {
throw new PasswordRequiredException(resource, passwordException);
}
PEMDecryptorProvider pemDecryptorProvider = PEM_DECRYPTOR_PROVIDER_BUILDER.build(passwordChars);
try {
pemKeyPair = pemObject.decryptKeyPair(pemDecryptorProvider);
} catch (EncryptionException e) {
passwordException = e;
}
}
return convertKey(pemKeyPair);
}
Aggregations