Search in sources :

Example 1 with PKCS12SafeBagFactory

use of org.bouncycastle.pkcs.PKCS12SafeBagFactory in project certmgr by hdecarne.

the class PKCS12CertReaderWriter method getSafeBagFactory.

private static PKCS12SafeBagFactory getSafeBagFactory(ContentInfo contentInfo, String resource, PasswordCallback password) throws IOException {
    PKCS12SafeBagFactory safeBagFactory = null;
    PKCSException decryptException = null;
    while (safeBagFactory == null) {
        try {
            safeBagFactory = new PKCS12SafeBagFactory(contentInfo, buildInputDecryptorProvider(resource, password, decryptException));
        } catch (PKCSException e) {
            decryptException = e;
        }
    }
    return safeBagFactory;
}
Also used : PKCS12SafeBagFactory(org.bouncycastle.pkcs.PKCS12SafeBagFactory) PKCSException(org.bouncycastle.pkcs.PKCSException)

Example 2 with PKCS12SafeBagFactory

use of org.bouncycastle.pkcs.PKCS12SafeBagFactory in project certmgr by hdecarne.

the class PKCS12CertReaderWriter method readBinary.

@Override
@Nullable
public CertObjectStore readBinary(IOResource<InputStream> in, PasswordCallback password) throws IOException {
    LOG.debug("Trying to read PKCS#12 objects from: ''{0}''...", in);
    CertObjectStore certObjects = null;
    PKCS12PfxPdu pkcs12 = readPKCS12(in);
    if (pkcs12 != null) {
        certObjects = new CertObjectStore();
        for (ContentInfo contentInfo : pkcs12.getContentInfos()) {
            ASN1ObjectIdentifier contentType = contentInfo.getContentType();
            PKCS12SafeBagFactory safeBagFactory;
            if (contentType.equals(PKCSObjectIdentifiers.encryptedData)) {
                safeBagFactory = getSafeBagFactory(contentInfo, in.resource(), password);
            } else {
                safeBagFactory = getSafeBagFactory(contentInfo);
            }
            for (PKCS12SafeBag safeBag : safeBagFactory.getSafeBags()) {
                Object safeBagValue = safeBag.getBagValue();
                if (safeBagValue instanceof X509CertificateHolder) {
                    certObjects.addCRT(convertCRT((X509CertificateHolder) safeBagValue));
                } else if (safeBagValue instanceof PKCS8EncryptedPrivateKeyInfo) {
                    PrivateKey privateKey = convertPrivateKey((PKCS8EncryptedPrivateKeyInfo) safeBagValue, in.resource(), password);
                    try {
                        certObjects.addKey(KeyHelper.rebuildKeyPair(privateKey));
                    } catch (IOException e) {
                        LOG.warning(e, "Unable to rebuild key pair for private key of type ''{1}''", privateKey.getClass().getName());
                    }
                } else if (safeBagValue instanceof PrivateKeyInfo) {
                    PrivateKey privateKey = convertPrivateKey((PrivateKeyInfo) safeBagValue);
                    try {
                        certObjects.addKey(KeyHelper.rebuildKeyPair(privateKey));
                    } catch (IOException e) {
                        LOG.warning(e, "Unable to rebuild key pair for private key of type ''{1}''", privateKey.getClass().getName());
                    }
                } else {
                    LOG.warning(CertIOI18N.STR_PKCS12_UNKNOWN_OBJECT, safeBagValue.getClass().getName());
                }
            }
        }
    }
    return certObjects;
}
Also used : PrivateKey(java.security.PrivateKey) PKCS12SafeBagFactory(org.bouncycastle.pkcs.PKCS12SafeBagFactory) ContentInfo(org.bouncycastle.asn1.pkcs.ContentInfo) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) CertObjectStore(de.carne.certmgr.certs.CertObjectStore) PKCS8EncryptedPrivateKeyInfo(org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo) IOException(java.io.IOException) PKCS12SafeBag(org.bouncycastle.pkcs.PKCS12SafeBag) PKCS12PfxPdu(org.bouncycastle.pkcs.PKCS12PfxPdu) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) PKCS8EncryptedPrivateKeyInfo(org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo) Nullable(de.carne.check.Nullable)

Aggregations

PKCS12SafeBagFactory (org.bouncycastle.pkcs.PKCS12SafeBagFactory)2 CertObjectStore (de.carne.certmgr.certs.CertObjectStore)1 Nullable (de.carne.check.Nullable)1 IOException (java.io.IOException)1 PrivateKey (java.security.PrivateKey)1 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)1 ContentInfo (org.bouncycastle.asn1.pkcs.ContentInfo)1 PrivateKeyInfo (org.bouncycastle.asn1.pkcs.PrivateKeyInfo)1 X509CertificateHolder (org.bouncycastle.cert.X509CertificateHolder)1 PKCS12PfxPdu (org.bouncycastle.pkcs.PKCS12PfxPdu)1 PKCS12SafeBag (org.bouncycastle.pkcs.PKCS12SafeBag)1 PKCS8EncryptedPrivateKeyInfo (org.bouncycastle.pkcs.PKCS8EncryptedPrivateKeyInfo)1 PKCSException (org.bouncycastle.pkcs.PKCSException)1