Search in sources :

Example 11 with EncryptedPrivateKeyInfo

use of com.github.zhenwei.core.asn1.pkcs.EncryptedPrivateKeyInfo in project jruby-openssl by jruby.

the class PEMInputOutput method readPrivateKey.

/**
 * c: PEM_read_PrivateKey + PEM_read_bio_PrivateKey
 * CAUTION: KeyPair#getPublic() may be null.
 */
public static KeyPair readPrivateKey(final Reader in, char[] passwd) throws PasswordRequiredException, IOException {
    final String BEG_STRING_ECPRIVATEKEY = BEF_G + PEM_STRING_ECPRIVATEKEY;
    final String BEG_STRING_PKCS8INF = BEF_G + PEM_STRING_PKCS8INF;
    final String BEG_STRING_PKCS8 = BEF_G + PEM_STRING_PKCS8;
    final BufferedReader reader = makeBuffered(in);
    String line;
    while ((line = reader.readLine()) != null) {
        if (line.indexOf(BEG_STRING_RSA) != -1) {
            try {
                return readKeyPair(reader, passwd, "RSA", BEF_E + PEM_STRING_RSA);
            } catch (Exception e) {
                throw mapReadException("problem creating RSA private key: ", e);
            }
        } else if (line.indexOf(BEG_STRING_DSA) != -1) {
            try {
                return readKeyPair(reader, passwd, "DSA", BEF_E + PEM_STRING_DSA);
            } catch (Exception e) {
                throw mapReadException("problem creating DSA private key: ", e);
            }
        } else if (line.indexOf(BEG_STRING_ECPRIVATEKEY) != -1) {
            try {
                return readKeyPair(reader, passwd, "ECDSA", BEF_E + PEM_STRING_ECPRIVATEKEY);
            } catch (Exception e) {
                throw mapReadException("problem creating DSA private key: ", e);
            }
        } else if (line.indexOf(BEG_STRING_PKCS8INF) != -1) {
            try {
                byte[] bytes = readBase64Bytes(reader, 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 mapReadException("problem creating private key: ", e);
            }
        } else if (line.indexOf(BEG_STRING_PKCS8) != -1) {
            try {
                byte[] bytes = readBase64Bytes(reader, 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, passwd);
                } else {
                    privKey = derivePrivateKeyPBES1(eIn, algId, passwd);
                }
                return new KeyPair(null, privKey);
            } catch (Exception e) {
                throw mapReadException("problem creating private key: ", e);
            }
        }
    }
    return null;
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) BufferedReader(java.io.BufferedReader) EncryptedPrivateKeyInfo(org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) DERUTF8String(org.bouncycastle.asn1.DERUTF8String) DEROctetString(org.bouncycastle.asn1.DEROctetString) ASN1Object(org.bouncycastle.asn1.ASN1Object) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) CertificateEncodingException(java.security.cert.CertificateEncodingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) CMSException(org.bouncycastle.cms.CMSException) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) IOException(java.io.IOException) CRLException(java.security.cert.CRLException) CertificateException(java.security.cert.CertificateException) EncryptedPrivateKeyInfo(org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 12 with EncryptedPrivateKeyInfo

use of com.github.zhenwei.core.asn1.pkcs.EncryptedPrivateKeyInfo in project LinLong-Java by zhenwei1108.

the class PKCS8Generator method generate.

private PemObject generate(PrivateKeyInfo key, OutputEncryptor encryptor) throws PemGenerationException {
    try {
        byte[] keyData = key.getEncoded();
        if (encryptor == null) {
            return new PemObject("PRIVATE KEY", keyData);
        }
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        OutputStream cOut = encryptor.getOutputStream(bOut);
        cOut.write(key.getEncoded());
        cOut.close();
        EncryptedPrivateKeyInfo info = new EncryptedPrivateKeyInfo(encryptor.getAlgorithmIdentifier(), bOut.toByteArray());
        return new PemObject("ENCRYPTED PRIVATE KEY", info.getEncoded());
    } catch (IOException e) {
        throw new PemGenerationException("unable to process encoded key data: " + e.getMessage(), e);
    }
}
Also used : PemObject(com.github.zhenwei.core.util.io.pem.PemObject) PemGenerationException(com.github.zhenwei.core.util.io.pem.PemGenerationException) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EncryptedPrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.EncryptedPrivateKeyInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 13 with EncryptedPrivateKeyInfo

use of com.github.zhenwei.core.asn1.pkcs.EncryptedPrivateKeyInfo in project LinLong-Java by zhenwei1108.

the class EncryptedPrivateKeyInfo method toASN1Primitive.

/**
 * Produce an object suitable for an ASN1OutputStream.
 * <pre>
 * EncryptedPrivateKeyInfo ::= SEQUENCE {
 *      encryptionAlgorithm AlgorithmIdentifier {{KeyEncryptionAlgorithms}},
 *      encryptedData EncryptedData
 * }
 *
 * EncryptedData ::= OCTET STRING
 *
 * KeyEncryptionAlgorithms ALGORITHM-IDENTIFIER ::= {
 *          ... -- For local profiles
 * }
 * </pre>
 */
public ASN1Primitive toASN1Primitive() {
    ASN1EncodableVector v = new ASN1EncodableVector(2);
    v.add(algId);
    v.add(data);
    return new DERSequence(v);
}
Also used : DERSequence(com.github.zhenwei.core.asn1.DERSequence) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector)

Aggregations

IOException (java.io.IOException)8 EncryptedPrivateKeyInfo (com.github.zhenwei.core.asn1.pkcs.EncryptedPrivateKeyInfo)5 GeneralSecurityException (java.security.GeneralSecurityException)5 InvalidKeyException (java.security.InvalidKeyException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 PrivateKey (java.security.PrivateKey)4 CertificateEncodingException (java.security.cert.CertificateEncodingException)4 CertificateException (java.security.cert.CertificateException)4 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)4 EncryptedPrivateKeyInfo (org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo)4 ObjectData (com.github.zhenwei.core.asn1.bc.ObjectData)3 KeyFactory (java.security.KeyFactory)3 KeyStoreException (java.security.KeyStoreException)3 NoSuchProviderException (java.security.NoSuchProviderException)3 UnrecoverableKeyException (java.security.UnrecoverableKeyException)3 ParseException (java.text.ParseException)3 BadPaddingException (javax.crypto.BadPaddingException)3 Cipher (javax.crypto.Cipher)3 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)3 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)3