use of org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter in project camel by apache.
the class ResourceHelperKeyPairProvider method loadKeys.
@Override
public Iterable<KeyPair> loadKeys() {
if (!SecurityUtils.isBouncyCastleRegistered()) {
throw new IllegalStateException("BouncyCastle must be registered as a JCE provider");
}
final List<KeyPair> keys = new ArrayList<KeyPair>(this.resources.length);
for (String resource : resources) {
PEMParser r = null;
InputStreamReader isr = null;
InputStream is = null;
try {
is = ResourceHelper.resolveMandatoryResourceAsInputStream(classResolver, resource);
isr = new InputStreamReader(is);
r = new PEMParser(isr);
Object o = r.readObject();
JcaPEMKeyConverter pemConverter = new JcaPEMKeyConverter();
pemConverter.setProvider("BC");
if (passwordFinder != null && o instanceof PEMEncryptedKeyPair) {
JcePEMDecryptorProviderBuilder decryptorBuilder = new JcePEMDecryptorProviderBuilder();
PEMDecryptorProvider pemDecryptor = decryptorBuilder.build(passwordFinder.getPassword());
o = pemConverter.getKeyPair(((PEMEncryptedKeyPair) o).decryptKeyPair(pemDecryptor));
}
if (o instanceof PEMKeyPair) {
o = pemConverter.getKeyPair((PEMKeyPair) o);
keys.add((KeyPair) o);
} else if (o instanceof KeyPair) {
keys.add((KeyPair) o);
}
} catch (Exception e) {
log.warn("Unable to read key", e);
} finally {
IoUtils.closeQuietly(r, is, isr);
}
}
return keys;
}
use of org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter in project nifi by apache.
the class TlsHelperTest method loadKeyPair.
public static KeyPair loadKeyPair(Reader reader) throws IOException {
try (PEMParser pemParser = new PEMParser(reader)) {
Object object = pemParser.readObject();
assertEquals(PEMKeyPair.class, object.getClass());
return new JcaPEMKeyConverter().getKeyPair((PEMKeyPair) object);
}
}
use of org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter in project vespa by vespa-engine.
the class KeyUtils method fromPemEncodedPrivateKey.
public static PrivateKey fromPemEncodedPrivateKey(String pem) {
try (PEMParser parser = new PEMParser(new StringReader(pem))) {
Object pemObject = parser.readObject();
if (pemObject instanceof PrivateKeyInfo) {
PrivateKeyInfo keyInfo = (PrivateKeyInfo) pemObject;
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyInfo.getEncoded());
return KeyFactory.getInstance(KeyAlgorithm.RSA.getAlgorithmName()).generatePrivate(keySpec);
} else if (pemObject instanceof PEMKeyPair) {
PEMKeyPair pemKeypair = (PEMKeyPair) pemObject;
PrivateKeyInfo keyInfo = pemKeypair.getPrivateKeyInfo();
JcaPEMKeyConverter pemConverter = new JcaPEMKeyConverter();
return pemConverter.getPrivateKey(keyInfo);
}
throw new IllegalArgumentException("Unexpected type of PEM type: " + pemObject);
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
use of org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter in project groovity by disney.
the class KeyPairValueHandler method doLoad.
@Override
protected Object doLoad(InputStream stream, String contentType, @SuppressWarnings("rawtypes") Class valueClass, @SuppressWarnings("rawtypes") Map config) throws Exception {
// look here http://stackoverflow.com/questions/15656644/get-keypair-from-pem-key-with-bouncycastle
PEMKeyPair keyPair;
Reader reader = new InputStreamReader(stream, getCharset(contentType));
PEMParser pemReader = new PEMParser(reader);
try {
Object o = pemReader.readObject();
if (o == null) {
return null;
}
if (o instanceof PEMEncryptedKeyPair) {
if (config != null && config.containsKey(PASSWORD)) {
String password = config.get(PASSWORD).toString();
PEMDecryptorProvider decryptionProv = new JcePEMDecryptorProviderBuilder().build(password.toCharArray());
keyPair = ((PEMEncryptedKeyPair) o).decryptKeyPair(decryptionProv);
} else {
throw new RuntimeException("Keypair requires password but none provided");
}
} else {
keyPair = ((PEMKeyPair) o);
}
} finally {
pemReader.close();
}
return new JcaPEMKeyConverter().getKeyPair(keyPair);
}
use of org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter in project candlepin by candlepin.
the class DefaultEntitlementCertServiceAdapterTest method keyPair.
@BeforeClass
public static void keyPair() throws Exception {
ClassLoader cl = DefaultEntitlementCertServiceAdapterTest.class.getClassLoader();
InputStream keyStream = cl.getResourceAsStream("test.key");
PEMParser reader = null;
keyPair = null;
try {
reader = new PEMParser(new InputStreamReader(keyStream));
keyPair = new JcaPEMKeyConverter().setProvider(BC_PROVIDER).getKeyPair((PEMKeyPair) reader.readObject());
} finally {
if (reader != null) {
reader.close();
}
}
}
Aggregations