Search in sources :

Example 36 with PEMKeyPair

use of org.bouncycastle.openssl.PEMKeyPair in project gitblit by gitblit.

the class FileKeyPairProvider method doLoadKey.

protected KeyPair doLoadKey(String file) {
    try {
        PEMParser r = new PEMParser(new InputStreamReader(new FileInputStream(file)));
        try {
            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);
                return (KeyPair) o;
            } else if (o instanceof KeyPair) {
                return (KeyPair) o;
            }
        } finally {
            r.close();
        }
    } catch (Exception e) {
        log.warn("Unable to read key " + file, e);
    }
    return null;
}
Also used : PEMEncryptedKeyPair(org.bouncycastle.openssl.PEMEncryptedKeyPair) KeyPair(java.security.KeyPair) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) PEMEncryptedKeyPair(org.bouncycastle.openssl.PEMEncryptedKeyPair) PEMParser(org.bouncycastle.openssl.PEMParser) InputStreamReader(java.io.InputStreamReader) PEMDecryptorProvider(org.bouncycastle.openssl.PEMDecryptorProvider) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) JcePEMDecryptorProviderBuilder(org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder) FileInputStream(java.io.FileInputStream) NoSuchElementException(java.util.NoSuchElementException)

Example 37 with PEMKeyPair

use of org.bouncycastle.openssl.PEMKeyPair in project graylog2-server by Graylog2.

the class KeyUtil method privateKeyFromFile.

/**
 * Obtain a private key from a PKS8 PEM file, which is optionally password-protected.
 * @param password password to decrypt the file - it may be null or empty in case of an unencrypted file
 * @param keyFile the key file
 * @return the corresponding private key
 */
public static PrivateKey privateKeyFromFile(String password, File keyFile) throws IOException, PKCSException, OperatorCreationException {
    PrivateKey privateKey;
    JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
    // Be sure to specify charset for reader - don't use plain FileReader
    Object object;
    try (InputStream inputStream = Files.newInputStream(keyFile.toPath());
        InputStreamReader fileReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
        PEMParser pemParser = new PEMParser(fileReader)) {
        object = pemParser.readObject();
    }
    if (object instanceof PKCS8EncryptedPrivateKeyInfo) {
        PKCS8EncryptedPrivateKeyInfo pInfo = (PKCS8EncryptedPrivateKeyInfo) object;
        JceOpenSSLPKCS8DecryptorProviderBuilder providerBuilder = new JceOpenSSLPKCS8DecryptorProviderBuilder();
        InputDecryptorProvider provider = providerBuilder.build(Strings.nullToEmpty(password).toCharArray());
        PrivateKeyInfo info = pInfo.decryptPrivateKeyInfo(provider);
        privateKey = converter.getPrivateKey(info);
    } else if (object instanceof PrivateKeyInfo) {
        privateKey = converter.getPrivateKey((PrivateKeyInfo) object);
    } else if (object instanceof PEMKeyPair) {
        privateKey = converter.getPrivateKey(((PEMKeyPair) object).getPrivateKeyInfo());
    } else {
        throw new PKCSException("Encountered unexpected object type: " + object.getClass().getName());
    }
    return privateKey;
}
Also used : PemPrivateKey(io.netty.handler.ssl.PemPrivateKey) PrivateKey(java.security.PrivateKey) InputStreamReader(java.io.InputStreamReader) PEMParser(org.bouncycastle.openssl.PEMParser) InputDecryptorProvider(org.bouncycastle.operator.InputDecryptorProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JcaPEMKeyConverter(org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter) PemObject(org.bouncycastle.util.io.pem.PemObject) JceOpenSSLPKCS8DecryptorProviderBuilder(org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) PKCS8EncryptedPrivateKeyInfo(org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo) PKCSException(org.bouncycastle.pkcs.PKCSException) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) PKCS8EncryptedPrivateKeyInfo(org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo) EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo)

Aggregations

PEMKeyPair (org.bouncycastle.openssl.PEMKeyPair)37 PEMParser (org.bouncycastle.openssl.PEMParser)35 JcaPEMKeyConverter (org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter)28 PrivateKeyInfo (org.bouncycastle.asn1.pkcs.PrivateKeyInfo)17 IOException (java.io.IOException)13 InputStreamReader (java.io.InputStreamReader)13 PrivateKey (java.security.PrivateKey)12 PEMEncryptedKeyPair (org.bouncycastle.openssl.PEMEncryptedKeyPair)12 StringReader (java.io.StringReader)11 PEMDecryptorProvider (org.bouncycastle.openssl.PEMDecryptorProvider)11 JcePEMDecryptorProviderBuilder (org.bouncycastle.openssl.jcajce.JcePEMDecryptorProviderBuilder)11 InputStream (java.io.InputStream)10 KeyPair (java.security.KeyPair)8 Reader (java.io.Reader)7 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)7 PKCS8EncryptedPrivateKeyInfo (org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo)7 JceOpenSSLPKCS8DecryptorProviderBuilder (org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8DecryptorProviderBuilder)6 InputDecryptorProvider (org.bouncycastle.operator.InputDecryptorProvider)6 PemObject (org.bouncycastle.util.io.pem.PemObject)6 KeyFactory (java.security.KeyFactory)5