Search in sources :

Example 1 with PKCS12SafeBagBuilder

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

the class PKCS12CertReaderWriter method createKeySafeBagBuilder.

private static PKCS12SafeBagBuilder createKeySafeBagBuilder(String alias, KeyPair key, char[] passwordChars) throws GeneralSecurityException {
    PKCS12SafeBagBuilder safeBagBuilder = new JcaPKCS12SafeBagBuilder(key.getPrivate(), PKCS12_ENCRYPTOR_BUILDER.build(passwordChars));
    safeBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString(alias));
    JcaX509ExtensionUtils extensionUtils = new JcaX509ExtensionUtils();
    SubjectKeyIdentifier subjectKeyIdentifier = extensionUtils.createSubjectKeyIdentifier(key.getPublic());
    safeBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_localKeyId, subjectKeyIdentifier);
    return safeBagBuilder;
}
Also used : JcaX509ExtensionUtils(org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils) DERBMPString(org.bouncycastle.asn1.DERBMPString) PKCS12SafeBagBuilder(org.bouncycastle.pkcs.PKCS12SafeBagBuilder) JcaPKCS12SafeBagBuilder(org.bouncycastle.pkcs.jcajce.JcaPKCS12SafeBagBuilder) SubjectKeyIdentifier(org.bouncycastle.asn1.x509.SubjectKeyIdentifier) JcaPKCS12SafeBagBuilder(org.bouncycastle.pkcs.jcajce.JcaPKCS12SafeBagBuilder)

Example 2 with PKCS12SafeBagBuilder

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

the class PKCS12CertReaderWriter method writeEncryptedBinary.

@Override
public void writeEncryptedBinary(IOResource<OutputStream> out, CertObjectStore certObjects, PasswordCallback newPassword) throws IOException {
    char[] passwordChars = newPassword.queryPassword(out.resource());
    if (passwordChars == null) {
        throw new PasswordRequiredException(out.resource());
    }
    try {
        List<PKCS12SafeBagBuilder> safeBagBuilders = new ArrayList<>(certObjects.size());
        for (CertObjectStore.Entry certObject : certObjects) {
            switch(certObject.type()) {
                case CRT:
                    safeBagBuilders.add(createCRTSafeBagBuilder(certObject.alias(), certObject.getCRT(), safeBagBuilders.isEmpty()));
                    break;
                case KEY:
                    safeBagBuilders.add(createKeySafeBagBuilder(certObject.alias(), certObject.getKey(), passwordChars));
                    break;
                case CSR:
                    break;
                case CRL:
                    break;
            }
        }
        PKCS12PfxPduBuilder pkcs12Builder = new PKCS12PfxPduBuilder();
        for (PKCS12SafeBagBuilder safeBagBuilder : safeBagBuilders) {
            pkcs12Builder.addData(safeBagBuilder.build());
        }
        PKCS12PfxPdu pkcs12 = pkcs12Builder.build(new BcPKCS12MacCalculatorBuilder(), passwordChars);
        out.io().write(pkcs12.getEncoded());
    } catch (GeneralSecurityException | PKCSException e) {
        throw new CertProviderException(e);
    }
}
Also used : PKCS12SafeBagBuilder(org.bouncycastle.pkcs.PKCS12SafeBagBuilder) JcaPKCS12SafeBagBuilder(org.bouncycastle.pkcs.jcajce.JcaPKCS12SafeBagBuilder) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) PasswordRequiredException(de.carne.certmgr.certs.PasswordRequiredException) PKCSException(org.bouncycastle.pkcs.PKCSException) CertProviderException(de.carne.certmgr.certs.CertProviderException) CertObjectStore(de.carne.certmgr.certs.CertObjectStore) BcPKCS12MacCalculatorBuilder(org.bouncycastle.pkcs.bc.BcPKCS12MacCalculatorBuilder) PKCS12PfxPdu(org.bouncycastle.pkcs.PKCS12PfxPdu) PKCS12PfxPduBuilder(org.bouncycastle.pkcs.PKCS12PfxPduBuilder)

Example 3 with PKCS12SafeBagBuilder

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

the class PKCS12CertReaderWriter method writeBinary.

@Override
public void writeBinary(IOResource<OutputStream> out, CertObjectStore certObjects) throws IOException, UnsupportedOperationException {
    try {
        List<PKCS12SafeBagBuilder> safeBagBuilders = new ArrayList<>(certObjects.size());
        for (CertObjectStore.Entry certObject : certObjects) {
            switch(certObject.type()) {
                case CRT:
                    safeBagBuilders.add(createCRTSafeBagBuilder(certObject.alias(), certObject.getCRT(), safeBagBuilders.isEmpty()));
                    break;
                case KEY:
                    safeBagBuilders.add(createKeySafeBagBuilder(certObject.alias(), certObject.getKey()));
                    break;
                case CSR:
                    break;
                case CRL:
                    break;
            }
        }
        PKCS12PfxPduBuilder pkcs12Builder = new PKCS12PfxPduBuilder();
        for (PKCS12SafeBagBuilder safeBagBuilder : safeBagBuilders) {
            pkcs12Builder.addData(safeBagBuilder.build());
        }
        PKCS12PfxPdu pkcs12 = pkcs12Builder.build(null, null);
        out.io().write(pkcs12.getEncoded());
    } catch (GeneralSecurityException | PKCSException e) {
        throw new CertProviderException(e);
    }
}
Also used : PKCS12SafeBagBuilder(org.bouncycastle.pkcs.PKCS12SafeBagBuilder) JcaPKCS12SafeBagBuilder(org.bouncycastle.pkcs.jcajce.JcaPKCS12SafeBagBuilder) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) CertObjectStore(de.carne.certmgr.certs.CertObjectStore) PKCS12PfxPdu(org.bouncycastle.pkcs.PKCS12PfxPdu) PKCSException(org.bouncycastle.pkcs.PKCSException) CertProviderException(de.carne.certmgr.certs.CertProviderException) PKCS12PfxPduBuilder(org.bouncycastle.pkcs.PKCS12PfxPduBuilder)

Example 4 with PKCS12SafeBagBuilder

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

the class PKCS12CertReaderWriter method createKeySafeBagBuilder.

private static PKCS12SafeBagBuilder createKeySafeBagBuilder(String alias, KeyPair key) throws GeneralSecurityException {
    PKCS12SafeBagBuilder safeBagBuilder = new JcaPKCS12SafeBagBuilder(key.getPrivate());
    safeBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString(alias));
    JcaX509ExtensionUtils extensionUtils = new JcaX509ExtensionUtils();
    SubjectKeyIdentifier subjectKeyIdentifier = extensionUtils.createSubjectKeyIdentifier(key.getPublic());
    safeBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_localKeyId, subjectKeyIdentifier);
    return safeBagBuilder;
}
Also used : JcaX509ExtensionUtils(org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils) DERBMPString(org.bouncycastle.asn1.DERBMPString) PKCS12SafeBagBuilder(org.bouncycastle.pkcs.PKCS12SafeBagBuilder) JcaPKCS12SafeBagBuilder(org.bouncycastle.pkcs.jcajce.JcaPKCS12SafeBagBuilder) SubjectKeyIdentifier(org.bouncycastle.asn1.x509.SubjectKeyIdentifier) JcaPKCS12SafeBagBuilder(org.bouncycastle.pkcs.jcajce.JcaPKCS12SafeBagBuilder)

Example 5 with PKCS12SafeBagBuilder

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

the class PKCS12CertReaderWriter method createCRTSafeBagBuilder.

private static PKCS12SafeBagBuilder createCRTSafeBagBuilder(String alias, X509Certificate crt, boolean addKeyId) throws IOException, GeneralSecurityException {
    PKCS12SafeBagBuilder safeBagBuilder = new JcaPKCS12SafeBagBuilder(crt);
    safeBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString(alias));
    if (addKeyId) {
        JcaX509ExtensionUtils extensionUtils = new JcaX509ExtensionUtils();
        SubjectKeyIdentifier subjectKeyIdentifier = extensionUtils.createSubjectKeyIdentifier(crt.getPublicKey());
        safeBagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_localKeyId, subjectKeyIdentifier);
    }
    return safeBagBuilder;
}
Also used : JcaX509ExtensionUtils(org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils) DERBMPString(org.bouncycastle.asn1.DERBMPString) PKCS12SafeBagBuilder(org.bouncycastle.pkcs.PKCS12SafeBagBuilder) JcaPKCS12SafeBagBuilder(org.bouncycastle.pkcs.jcajce.JcaPKCS12SafeBagBuilder) SubjectKeyIdentifier(org.bouncycastle.asn1.x509.SubjectKeyIdentifier) JcaPKCS12SafeBagBuilder(org.bouncycastle.pkcs.jcajce.JcaPKCS12SafeBagBuilder)

Aggregations

PKCS12SafeBagBuilder (org.bouncycastle.pkcs.PKCS12SafeBagBuilder)5 JcaPKCS12SafeBagBuilder (org.bouncycastle.pkcs.jcajce.JcaPKCS12SafeBagBuilder)5 DERBMPString (org.bouncycastle.asn1.DERBMPString)3 SubjectKeyIdentifier (org.bouncycastle.asn1.x509.SubjectKeyIdentifier)3 JcaX509ExtensionUtils (org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils)3 CertObjectStore (de.carne.certmgr.certs.CertObjectStore)2 CertProviderException (de.carne.certmgr.certs.CertProviderException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 ArrayList (java.util.ArrayList)2 PKCS12PfxPdu (org.bouncycastle.pkcs.PKCS12PfxPdu)2 PKCS12PfxPduBuilder (org.bouncycastle.pkcs.PKCS12PfxPduBuilder)2 PKCSException (org.bouncycastle.pkcs.PKCSException)2 PasswordRequiredException (de.carne.certmgr.certs.PasswordRequiredException)1 BcPKCS12MacCalculatorBuilder (org.bouncycastle.pkcs.bc.BcPKCS12MacCalculatorBuilder)1