Search in sources :

Example 26 with JcaPEMKeyConverter

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;
}
Also used : KeyPair(java.security.KeyPair) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) PEMEncryptedKeyPair(org.bouncycastle.openssl.PEMEncryptedKeyPair) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) PEMDecryptorProvider(org.bouncycastle.openssl.PEMDecryptorProvider) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter) JcePEMDecryptorProviderBuilder(org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder) PEMEncryptedKeyPair(org.bouncycastle.openssl.PEMEncryptedKeyPair) PEMParser(org.bouncycastle.openssl.PEMParser) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair)

Example 27 with JcaPEMKeyConverter

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);
    }
}
Also used : PEMParser(org.bouncycastle.openssl.PEMParser) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter)

Example 28 with JcaPEMKeyConverter

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);
    }
}
Also used : PEMParser(org.bouncycastle.openssl.PEMParser) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) StringReader(java.io.StringReader) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter) PemObject(org.bouncycastle.util.io.pem.PemObject) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo)

Example 29 with JcaPEMKeyConverter

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);
}
Also used : PEMEncryptedKeyPair(org.bouncycastle.openssl.PEMEncryptedKeyPair) InputStreamReader(java.io.InputStreamReader) PEMParser(org.bouncycastle.openssl.PEMParser) PEMDecryptorProvider(org.bouncycastle.openssl.PEMDecryptorProvider) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) JcePEMDecryptorProviderBuilder(org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder)

Example 30 with JcaPEMKeyConverter

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();
        }
    }
}
Also used : PEMParser(org.bouncycastle.openssl.PEMParser) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) BeforeClass(org.junit.BeforeClass)

Aggregations

JcaPEMKeyConverter (org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter)55 PEMParser (org.bouncycastle.openssl.PEMParser)48 PEMKeyPair (org.bouncycastle.openssl.PEMKeyPair)31 PrivateKeyInfo (org.bouncycastle.asn1.pkcs.PrivateKeyInfo)26 IOException (java.io.IOException)20 InputStreamReader (java.io.InputStreamReader)19 PrivateKey (java.security.PrivateKey)19 Reader (java.io.Reader)15 JcePEMDecryptorProviderBuilder (org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder)14 PKCS8EncryptedPrivateKeyInfo (org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo)14 StringReader (java.io.StringReader)13 PEMEncryptedKeyPair (org.bouncycastle.openssl.PEMEncryptedKeyPair)13 InputStream (java.io.InputStream)12 InputDecryptorProvider (org.bouncycastle.operator.InputDecryptorProvider)12 PEMDecryptorProvider (org.bouncycastle.openssl.PEMDecryptorProvider)11 JceOpenSSLPKCS8DecryptorProviderBuilder (org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder)11 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)10 KeyPair (java.security.KeyPair)9 PemObject (org.bouncycastle.util.io.pem.PemObject)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6