Search in sources :

Example 1 with PrivateKeyInfo

use of com.mindbright.security.pkcs8.PrivateKeyInfo in project SpringRemote by HaleyWang.

the class PKCS12KeyStore method extractPrivateKey.

/*
     * !!! TODO generalize handling and move to pkcs8 or pkcs1
     */
public static PrivateKey extractPrivateKey(byte[] berPrivateKeyInfo) throws UnrecoverableKeyException {
    ASN1DER ber = new ASN1DER();
    ByteArrayInputStream ba = new ByteArrayInputStream(berPrivateKeyInfo);
    PrivateKeyInfo pki = new PrivateKeyInfo();
    try {
        ber.decode(ba, pki);
        boolean isrsakey = true;
        try {
            String alg = pki.privateKeyAlgorithm.algorithmName().toLowerCase();
            if (alg.indexOf("dsa") >= 0)
                isrsakey = false;
        } catch (Throwable t) {
        }
        ba = new ByteArrayInputStream(pki.privateKey.getRaw());
        if (isrsakey) {
            com.mindbright.security.pkcs1.RSAPrivateKey rsa = new com.mindbright.security.pkcs1.RSAPrivateKey();
            ber.decode(ba, rsa);
            BigInteger n, e, d, p, q, pe, qe, u;
            n = rsa.modulus.getValue();
            e = rsa.publicExponent.getValue();
            d = rsa.privateExponent.getValue();
            p = rsa.prime1.getValue();
            q = rsa.prime2.getValue();
            pe = rsa.exponent1.getValue();
            qe = rsa.exponent2.getValue();
            u = rsa.coefficient.getValue();
            RSAPrivateCrtKeySpec prvSpec = new RSAPrivateCrtKeySpec(n, e, d, p, q, pe, qe, u);
            KeyFactory keyFact = KeyFactory.getInstance("RSA");
            return keyFact.generatePrivate(prvSpec);
        }
        BigInteger x = null;
        try {
            // Normally, we should have just one ASN.1 integer here...
            ASN1Integer dsax = new ASN1Integer();
            ber.decode(ba, dsax);
            x = dsax.getValue();
        } catch (Throwable t) {
        }
        if (x == null) {
            // ... but Mozilla returns SEQUENCE { y? INTEGER, x INTEGER }
            DSAyx dsayx = new DSAyx();
            ber.decode(new ByteArrayInputStream(pki.privateKey.getRaw()), dsayx);
            x = dsayx.x.getValue();
        }
        com.mindbright.security.pkcs1.DSAParams params = (com.mindbright.security.pkcs1.DSAParams) pki.privateKeyAlgorithm.parameters.getValue();
        DSAPrivateKeySpec prvSpec = new DSAPrivateKeySpec(x, params.p.getValue(), params.q.getValue(), params.g.getValue());
        KeyFactory keyFact = KeyFactory.getInstance("DSA");
        return keyFact.generatePrivate(prvSpec);
    } catch (Exception e) {
        throw new UnrecoverableKeyException(e.getMessage());
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) ASN1DER(com.mindbright.asn1.ASN1DER) ASN1OctetString(com.mindbright.asn1.ASN1OctetString) ASN1CharString(com.mindbright.asn1.ASN1CharString) ASN1Integer(com.mindbright.asn1.ASN1Integer) KeyStoreException(java.security.KeyStoreException) GeneralSecurityException(java.security.GeneralSecurityException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) UnrecoverableKeyException(java.security.UnrecoverableKeyException) ByteArrayInputStream(java.io.ByteArrayInputStream) BigInteger(java.math.BigInteger) PrivateKeyInfo(com.mindbright.security.pkcs8.PrivateKeyInfo) EncryptedPrivateKeyInfo(com.mindbright.security.pkcs8.EncryptedPrivateKeyInfo) KeyFactory(java.security.KeyFactory)

Aggregations

ASN1CharString (com.mindbright.asn1.ASN1CharString)1 ASN1DER (com.mindbright.asn1.ASN1DER)1 ASN1Integer (com.mindbright.asn1.ASN1Integer)1 ASN1OctetString (com.mindbright.asn1.ASN1OctetString)1 EncryptedPrivateKeyInfo (com.mindbright.security.pkcs8.EncryptedPrivateKeyInfo)1 PrivateKeyInfo (com.mindbright.security.pkcs8.PrivateKeyInfo)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 GeneralSecurityException (java.security.GeneralSecurityException)1 InvalidKeyException (java.security.InvalidKeyException)1 KeyFactory (java.security.KeyFactory)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 UnrecoverableKeyException (java.security.UnrecoverableKeyException)1 CertificateException (java.security.cert.CertificateException)1 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)1 RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)1