Search in sources :

Example 1 with EncryptedPrivateKeyInfo

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

the class PKCS12KeyStore method engineGetKey.

public Key engineGetKey(String alias, char[] password) throws NoSuchAlgorithmException, UnrecoverableKeyException {
    String localKeyId = name2id.get(alias);
    if (localKeyId == null) {
        return null;
    }
    EncryptedPrivateKeyInfo epki = privateKeys.get(localKeyId);
    return extractPrivateKey(epki, password);
}
Also used : EncryptedPrivateKeyInfo(com.mindbright.security.pkcs8.EncryptedPrivateKeyInfo) ASN1OctetString(com.mindbright.asn1.ASN1OctetString) ASN1CharString(com.mindbright.asn1.ASN1CharString)

Example 2 with EncryptedPrivateKeyInfo

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

the class PKCS12KeyStore method processSafeContents.

private void processSafeContents(byte[] scBer) throws IOException {
    ByteArrayInputStream ba = new ByteArrayInputStream(scBer);
    SafeContents sc = new SafeContents();
    ASN1DER ber = new ASN1DER();
    ber.decode(ba, sc);
    for (int j = 0; j < sc.getCount(); j++) {
        SafeBag safeBag = sc.getSafeBag(j);
        String friendlyName = getAttribute(safeBag, "1.2.840.113549.1.9.20");
        String localKeyId = getAttribute(safeBag, "1.2.840.113549.1.9.21");
        if (friendlyName != null) {
            if (localKeyId != null) {
                name2id.put(friendlyName, localKeyId);
            }
            if (!aliases.contains(friendlyName)) {
                aliases.addElement(friendlyName);
            }
        } else if (localKeyId != null) {
            name2id.put(localKeyId, localKeyId);
            if (!aliases.contains(localKeyId)) {
                aliases.addElement(localKeyId);
            }
        }
        switch(safeBag.getBagType()) {
            case SafeBag.TYPE_PKCS8_SHROUDED_KEYBAG:
                EncryptedPrivateKeyInfo keyBag = (EncryptedPrivateKeyInfo) safeBag.bagValue.getValue();
                privateKeys.put(localKeyId, keyBag);
                break;
            case SafeBag.TYPE_CERTBAG:
                CertBag cb = (CertBag) safeBag.bagValue.getValue();
                byte[] derCert = ((ASN1OctetString) cb.certValue.getValue()).getRaw();
                if (localKeyId == null) {
                    /*
                     * Trusted certs don't have a localKeyId
                     */
                    localKeyId = friendlyName;
                } else {
                    certificates.put(localKeyId, derCert);
                }
                break;
            default:
                throw new IOException("SafeBag type not supported: " + safeBag.bagId.getString());
        }
    }
}
Also used : ASN1OctetString(com.mindbright.asn1.ASN1OctetString) CertBag(com.mindbright.security.pkcs12.CertBag) ByteArrayInputStream(java.io.ByteArrayInputStream) ASN1DER(com.mindbright.asn1.ASN1DER) EncryptedPrivateKeyInfo(com.mindbright.security.pkcs8.EncryptedPrivateKeyInfo) SafeContents(com.mindbright.security.pkcs12.SafeContents) ASN1OctetString(com.mindbright.asn1.ASN1OctetString) ASN1CharString(com.mindbright.asn1.ASN1CharString) IOException(java.io.IOException) SafeBag(com.mindbright.security.pkcs12.SafeBag)

Example 3 with EncryptedPrivateKeyInfo

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

the class NetscapeKeyStore method engineGetKey.

public Key engineGetKey(String alias, char[] password) throws NoSuchAlgorithmException, UnrecoverableKeyException {
    KeyEntry keyEntry = getKeyEntry(alias);
    if (!passwordCheck(password)) {
        throw new UnrecoverableKeyException("Invalid password");
    }
    if (keyEntry != null) {
        try {
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo();
            ASN1DER ber = new ASN1DER();
            ByteArrayInputStream ba = new ByteArrayInputStream(keyEntry.encryptedKey);
            ber.decode(ba, epki);
            byte[] enc = epki.encryptedData.getRaw();
            byte[] dec = new byte[enc.length];
            do3DESCipher(Cipher.DECRYPT_MODE, password, enc, 0, enc.length, dec, globalSalt(), keyEntry.salt);
            ba = new ByteArrayInputStream(dec);
            return PKCS12KeyStore.extractPrivateKey(dec);
        } catch (IOException e) {
            throw new UnrecoverableKeyException(e.getMessage());
        }
    }
    return null;
}
Also used : UnrecoverableKeyException(java.security.UnrecoverableKeyException) ASN1DER(com.mindbright.asn1.ASN1DER) ByteArrayInputStream(java.io.ByteArrayInputStream) EncryptedPrivateKeyInfo(com.mindbright.security.pkcs8.EncryptedPrivateKeyInfo) IOException(java.io.IOException)

Aggregations

EncryptedPrivateKeyInfo (com.mindbright.security.pkcs8.EncryptedPrivateKeyInfo)3 ASN1CharString (com.mindbright.asn1.ASN1CharString)2 ASN1DER (com.mindbright.asn1.ASN1DER)2 ASN1OctetString (com.mindbright.asn1.ASN1OctetString)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 CertBag (com.mindbright.security.pkcs12.CertBag)1 SafeBag (com.mindbright.security.pkcs12.SafeBag)1 SafeContents (com.mindbright.security.pkcs12.SafeContents)1 UnrecoverableKeyException (java.security.UnrecoverableKeyException)1