Search in sources :

Example 11 with PFX

use of com.mindbright.security.pkcs12.PFX in project SpringRemote by HaleyWang.

the class PKCS12KeyStore method engineLoad.

public void engineLoad(InputStream stream, char[] password) throws IOException, NoSuchAlgorithmException, CertificateException {
    try {
        ASN1DER ber = new ASN1DER();
        PFX pfx = new PFX();
        ber.decode(stream, pfx);
        if (password == null) {
            password = new char[0];
        }
        checkMac(pfx, password);
        AuthenticatedSafe authSafe = new AuthenticatedSafe();
        ASN1OctetString data = pfx.getDataContent();
        ByteArrayInputStream ba = new ByteArrayInputStream(data.getRaw());
        ber.decode(ba, authSafe);
        for (int i = 0; i < authSafe.getCount(); i++) {
            ContentInfo ci = authSafe.getContentInfo(i);
            String cit = ci.contentType.getString();
            if (cit.equals("1.2.840.113549.1.7.1")) {
                data = (ASN1OctetString) ci.content.getValue();
                processSafeContents(data.getRaw());
            } else if (cit.equals("1.2.840.113549.1.7.6")) {
                EncryptedData ed = (EncryptedData) ci.content.getValue();
                String alg = ed.encryptedContentInfo.contentEncryptionAlgorithm.algorithmName();
                byte[] enc = ed.encryptedContentInfo.encryptedContent.getRaw();
                PKCS12PbeParams params = (PKCS12PbeParams) ed.encryptedContentInfo.contentEncryptionAlgorithm.parameters.getValue();
                byte[] salt = params.salt.getRaw();
                int iterations = params.iterations.getValue().intValue();
                byte[] dec = new byte[enc.length];
                doCipher(Cipher.DECRYPT_MODE, password, enc, enc.length, dec, salt, iterations, alg);
                processSafeContents(dec);
            } else {
                throw new IOException("ContentInfo type not supported: " + cit);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : ASN1OctetString(com.mindbright.asn1.ASN1OctetString) PFX(com.mindbright.security.pkcs12.PFX) ASN1DER(com.mindbright.asn1.ASN1DER) ByteArrayInputStream(java.io.ByteArrayInputStream) ContentInfo(com.mindbright.security.pkcs7.ContentInfo) PKCS12PbeParams(com.mindbright.security.pkcs12.PKCS12PbeParams) AuthenticatedSafe(com.mindbright.security.pkcs12.AuthenticatedSafe) ASN1OctetString(com.mindbright.asn1.ASN1OctetString) ASN1CharString(com.mindbright.asn1.ASN1CharString) EncryptedData(com.mindbright.security.pkcs7.EncryptedData) IOException(java.io.IOException)

Aggregations

PFX (org.mozilla.jss.pkcs12.PFX)6 PFX (codec.pkcs12.PFX)4 IOException (java.io.IOException)4 SEQUENCE (org.mozilla.jss.asn1.SEQUENCE)4 ASN1Exception (codec.asn1.ASN1Exception)3 BufferedInputStream (java.io.BufferedInputStream)3 FileOutputStream (java.io.FileOutputStream)3 BMPString (org.mozilla.jss.asn1.BMPString)3 AuthenticatedSafes (org.mozilla.jss.pkcs12.AuthenticatedSafes)3 AMPassword (com.sun.identity.security.keystore.AMPassword)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2 GeneralSecurityException (java.security.GeneralSecurityException)2 MessageDigest (java.security.MessageDigest)2 Certificate (java.security.cert.Certificate)2 CertificateException (java.security.cert.CertificateException)2 SecretKey (javax.crypto.SecretKey)2 InvalidNameException (javax.naming.InvalidNameException)2 IFileStore (org.eclipse.core.filesystem.IFileStore)2