Example usage for org.bouncycastle.asn1.pkcs EncryptedPrivateKeyInfo getEncryptionAlgorithm

List of usage examples for org.bouncycastle.asn1.pkcs EncryptedPrivateKeyInfo getEncryptionAlgorithm

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs EncryptedPrivateKeyInfo getEncryptionAlgorithm.

Prototype

public AlgorithmIdentifier getEncryptionAlgorithm() 

Source Link

Usage

From source file:edu.vt.middleware.crypt.io.PrivateKeyCredentialReader.java

License:Open Source License

/**
 * Decrypts a DER-encoded private key in PKCS#8 format.
 *
 * @param  encrypted  Bytes of DER-encoded encrypted private key.
 * @param  password  Password to decrypt private key.
 *
 * @return  ASN.1 encoded bytes of decrypted key.
 *
 * @throws  CryptException  On key decryption errors.
 *///  w ww  .j  a v a  2 s . com
private byte[] decryptPKCS8Key(final byte[] encrypted, final char[] password) throws CryptException {
    final EncryptionScheme scheme;
    try {
        final EncryptedPrivateKeyInfo ki = EncryptedPrivateKeyInfo
                .getInstance(ASN1Object.fromByteArray(encrypted));
        final AlgorithmIdentifier alg = ki.getEncryptionAlgorithm();
        if (PKCSObjectIdentifiers.id_PBES2.equals(alg.getObjectId())) {
            // PBES2 has following parameters:
            // {
            // {id-PBKDF2, {salt, iterationCount, keyLength (optional)}}
            // {encryptionAlgorithmOid, iv}
            // }
            final DERSequence pbeSeq = (DERSequence) alg.getParameters();
            final PBKDF2Parameters kdfParms = PBKDF2Parameters.decode((DERSequence) pbeSeq.getObjectAt(0));
            final PBES2CipherGenerator cipherGen = new PBES2CipherGenerator(
                    (DERSequence) pbeSeq.getObjectAt(1));
            if (kdfParms.getLength() == 0) {
                kdfParms.setLength(cipherGen.getKeySize() / 8);
            }
            scheme = new PBES2EncryptionScheme(cipherGen.generate(), kdfParms);
        } else {
            // Use PBES1 encryption scheme to decrypt key
            scheme = new PBES1EncryptionScheme(PBES1Algorithm.fromOid(alg.getObjectId().getId()),
                    PBEParameter.decode((DERSequence) alg.getParameters()));
        }
        return scheme.decrypt(password, ki.getEncryptedData());
    } catch (Exception e) {
        throw new CryptException("Failed decrypting PKCS#8 private key", e);
    }
}

From source file:org.cryptacular.asn.PKCS8PrivateKeyDecoder.java

License:Open Source License

@Override
protected byte[] decryptKey(final byte[] encrypted, final char[] password) {
    final EncryptionScheme scheme;
    final EncryptedPrivateKeyInfo ki = EncryptedPrivateKeyInfo.getInstance(tryConvertPem(encrypted));
    final AlgorithmIdentifier alg = ki.getEncryptionAlgorithm();
    if (PKCSObjectIdentifiers.id_PBES2.equals(alg.getAlgorithm())) {
        scheme = new PBES2EncryptionScheme(PBES2Parameters.getInstance(alg.getParameters()), password);
    } else {/*from www  .  j a  v a  2s.c o  m*/
        scheme = new PBES1EncryptionScheme(PBES1Algorithm.fromOid(alg.getAlgorithm().getId()),
                PBEParameter.getInstance(alg.getParameters()), password);
    }
    return scheme.decrypt(ki.getEncryptedData());
}

From source file:org.jruby.ext.openssl.x509store.BouncyCastleASN1FormatHandler.java

License:LGPL

/**
 * c: PEM_read_PrivateKey + PEM_read_bio_PrivateKey
 * CAUTION: KeyPair#getPublic() may be null.
 *//* w ww. j ava2  s. c o  m*/
@Override
public KeyPair readPrivateKey(Reader in, char[] password) throws IOException {
    BufferedReader _in = makeBuffered(in);
    String line;
    while ((line = _in.readLine()) != null) {
        if (line.indexOf(BEF_G + PEM_STRING_RSA) != -1) {
            try {
                return readKeyPair(_in, password, "RSA", BEF_E + PEM_STRING_RSA);
            } catch (Exception e) {
                throw new IOException("problem creating RSA private key: " + e.toString());
            }
        } else if (line.indexOf(BEF_G + PEM_STRING_DSA) != -1) {
            try {
                return readKeyPair(_in, password, "DSA", BEF_E + PEM_STRING_DSA);
            } catch (Exception e) {
                throw new IOException("problem creating DSA private key: " + e.toString());
            }
        } else if (line.indexOf(BEF_G + PEM_STRING_ECPRIVATEKEY) != -1) {
            throw new IOException("EC private key not supported");
        } else if (line.indexOf(BEF_G + PEM_STRING_PKCS8INF) != -1) {
            try {
                byte[] bytes = readBytes(_in, BEF_E + PEM_STRING_PKCS8INF);
                ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
                ASN1InputStream aIn = new ASN1InputStream(bIn);
                PrivateKeyInfo info = new PrivateKeyInfo((ASN1Sequence) aIn.readObject());
                String type = getPrivateKeyTypeFromObjectId(info.getAlgorithmId().getObjectId());
                return readPrivateKeySequence(info.getPrivateKey().getDEREncoded(), type);
            } catch (Exception e) {
                throw new IOException("problem creating private key: " + e.toString());
            }
        } else if (line.indexOf(BEF_G + PEM_STRING_PKCS8) != -1) {
            try {
                byte[] bytes = readBytes(_in, BEF_E + PEM_STRING_PKCS8);
                ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
                ASN1InputStream aIn = new ASN1InputStream(bIn);
                org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo eIn = new org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo(
                        (ASN1Sequence) aIn.readObject());
                AlgorithmIdentifier algId = eIn.getEncryptionAlgorithm();
                String algorithm = ASN1Registry.o2a(algId.getObjectId());
                algorithm = (algorithm.split("-"))[0];
                PKCS12PBEParams pbeParams = new PKCS12PBEParams((ASN1Sequence) algId.getParameters());
                SecretKeyFactory fact = OpenSSLReal.getSecretKeyFactoryBC(algorithm); // need to use BC for PKCS12PBEParams.
                PBEKeySpec pbeSpec = new PBEKeySpec(password);
                SecretKey key = fact.generateSecret(pbeSpec);
                PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(),
                        pbeParams.getIterations().intValue());
                Cipher cipher = OpenSSLReal.getCipherBC(algorithm); // need to use BC for PBEParameterSpec.
                cipher.init(Cipher.UNWRAP_MODE, key, defParams);
                // wrappedKeyAlgorithm is unknown ("")
                PrivateKey privKey = (PrivateKey) cipher.unwrap(eIn.getEncryptedData(), "", Cipher.PRIVATE_KEY);
                return new KeyPair(null, privKey);
            } catch (Exception e) {
                throw new IOException("problem creating private key: " + e.toString());
            }
        }
    }
    return null;
}

From source file:org.jruby.ext.openssl.x509store.PEMInputOutput.java

License:LGPL

/**
 * c: PEM_read_PrivateKey + PEM_read_bio_PrivateKey
 * CAUTION: KeyPair#getPublic() may be null.
 *///  w  ww .  j  a va 2 s  .c  om
public static KeyPair readPrivateKey(Reader in, char[] password) throws IOException {
    BufferedReader _in = makeBuffered(in);
    String line;
    while ((line = _in.readLine()) != null) {
        if (line.indexOf(BEF_G + PEM_STRING_RSA) != -1) {
            try {
                return readKeyPair(_in, password, "RSA", BEF_E + PEM_STRING_RSA);
            } catch (Exception e) {
                throw new IOException("problem creating RSA private key: " + e.toString());
            }
        } else if (line.indexOf(BEF_G + PEM_STRING_DSA) != -1) {
            try {
                return readKeyPair(_in, password, "DSA", BEF_E + PEM_STRING_DSA);
            } catch (Exception e) {
                throw new IOException("problem creating DSA private key: " + e.toString());
            }
        } else if (line.indexOf(BEF_G + PEM_STRING_ECPRIVATEKEY) != -1) {
            throw new IOException("EC private key not supported");
        } else if (line.indexOf(BEF_G + PEM_STRING_PKCS8INF) != -1) {
            try {
                byte[] bytes = readBytes(_in, BEF_E + PEM_STRING_PKCS8INF);
                PrivateKeyInfo info = PrivateKeyInfo.getInstance(bytes);
                String type = getPrivateKeyTypeFromObjectId(info.getPrivateKeyAlgorithm().getAlgorithm());
                return org.jruby.ext.openssl.impl.PKey.readPrivateKey(
                        ((ASN1Object) info.parsePrivateKey()).getEncoded(ASN1Encoding.DER), type);
            } catch (Exception e) {
                throw new IOException("problem creating private key: " + e.toString());
            }
        } else if (line.indexOf(BEF_G + PEM_STRING_PKCS8) != -1) {
            try {
                byte[] bytes = readBytes(_in, BEF_E + PEM_STRING_PKCS8);
                EncryptedPrivateKeyInfo eIn = EncryptedPrivateKeyInfo.getInstance(bytes);
                AlgorithmIdentifier algId = eIn.getEncryptionAlgorithm();
                PrivateKey privKey;
                if (algId.getAlgorithm().toString().equals("1.2.840.113549.1.5.13")) { // PBES2
                    privKey = derivePrivateKeyPBES2(eIn, algId, password);
                } else {
                    privKey = derivePrivateKeyPBES1(eIn, algId, password);
                }
                return new KeyPair(null, privKey);
            } catch (Exception e) {
                throw new IOException("problem creating private key: " + e.toString());
            }
        }
    }
    return null;
}

From source file:org.xwiki.crypto.password.internal.DefaultPrivateKeyPasswordBasedEncryptor.java

License:Open Source License

@Override
public PrivateKeyParameters decrypt(byte[] password, byte[] encoded)
        throws GeneralSecurityException, IOException {
    EncryptedPrivateKeyInfo encKeyInfo = EncryptedPrivateKeyInfo.getInstance(encoded);
    return decrypt(password, encKeyInfo.getEncryptionAlgorithm(), encKeyInfo.getEncryptedData());
}