Example usage for org.bouncycastle.pkcs.jcajce JcePKCS12MacCalculatorBuilder JcePKCS12MacCalculatorBuilder

List of usage examples for org.bouncycastle.pkcs.jcajce JcePKCS12MacCalculatorBuilder JcePKCS12MacCalculatorBuilder

Introduction

In this page you can find the example usage for org.bouncycastle.pkcs.jcajce JcePKCS12MacCalculatorBuilder JcePKCS12MacCalculatorBuilder.

Prototype

public JcePKCS12MacCalculatorBuilder(ASN1ObjectIdentifier hashAlgorithm) 

Source Link

Usage

From source file:com.vvote.thirdparty.ximix.util.BLSKeyStore.java

License:Apache License

/**
 * Return the key store object as a PKCS#12 byte array.
 *
 * @param password the password to use to encrypt the key data.
 * @return an array of bytes representing the encoding.
 * @throws IOException on a conversion to ASN.1 encoding error.
 * @throws GeneralSecurityException if there is an issue encrypting the key data.
 *///w  w w . j  a  v a 2s.  c om
public synchronized byte[] getEncoded(char[] password) throws IOException, GeneralSecurityException {
    KeyFactory fact = KeyFactory.getInstance("ECDSA", "BC");

    EllipticCurve curve = new EllipticCurve(
            new ECFieldFp(
                    new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839")), // q
            new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a
            new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b

    ECParameterSpec spec = new ECParameterSpec(curve,
            ECPointUtil.decodePoint(curve,
                    Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G
            new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307"), // n
            1); // h

    // TODO: neeed an EC key for the node
    ECPrivateKeySpec priKeySpec = new ECPrivateKeySpec(
            new BigInteger("876300101507107567501066130761671078357010671067781776716671676178726717"), // d
            spec);

    try {
        OutputEncryptor encOut = new JcePKCSPBEOutputEncryptorBuilder(NISTObjectIdentifiers.id_aes256_CBC)
                .setProvider("BC").build(password);

        JcaX509ExtensionUtils extUtils = new JcaX509ExtensionUtils();
        PKCS12PfxPduBuilder builder = new PKCS12PfxPduBuilder();

        for (String keyID : sharedPrivateKeyMap.keySet()) {
            PrivateKey sigKey = fact.generatePrivate(priKeySpec);
            SubjectPublicKeyInfo pubKey = this.fetchPublicKey(keyID);

            PKCS12SafeBagBuilder eeCertBagBuilder = new PKCS12SafeBagBuilder(
                    createCertificate(keyID, sequenceNoMap.get(keyID), sigKey));

            eeCertBagBuilder.addBagAttribute(PKCS12SafeBag.friendlyNameAttribute, new DERBMPString(keyID));

            SubjectKeyIdentifier pubKeyId = extUtils.createSubjectKeyIdentifier(pubKey);

            eeCertBagBuilder.addBagAttribute(PKCS12SafeBag.localKeyIdAttribute, pubKeyId);

            PKCS12SafeBagBuilder keyBagBuilder = new PKCS12SafeBagBuilder(PrivateKeyInfoFactory
                    .createPrivateKeyInfo(sharedPrivateKeyMap.get(keyID), paramsMap.get(keyID)), encOut);

            keyBagBuilder.addBagAttribute(PKCS12SafeBag.friendlyNameAttribute, new DERBMPString(keyID));
            keyBagBuilder.addBagAttribute(PKCS12SafeBag.localKeyIdAttribute, pubKeyId);

            builder.addEncryptedData(
                    new JcePKCSPBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHAAnd128BitRC2_CBC)
                            .setProvider("BC").build(password),
                    new PKCS12SafeBag[] { eeCertBagBuilder.build() });

            builder.addData(keyBagBuilder.build());
        }

        PKCS12PfxPdu pfx = builder.build(new JcePKCS12MacCalculatorBuilder(NISTObjectIdentifiers.id_sha256),
                password);

        return pfx.getEncoded(ASN1Encoding.DL);
    } catch (PKCSException e) {
        throw new GeneralSecurityException("Unable to create key store: " + e.getMessage(), e);
    } catch (OperatorCreationException e) {
        throw new GeneralSecurityException("Unable to create operator: " + e.getMessage(), e);
    }
}

From source file:org.cryptoworkshop.ximix.node.crypto.key.BLSKeyManager.java

License:Apache License

public synchronized byte[] getEncoded(char[] password) throws IOException, GeneralSecurityException {
    try {/*from   w  w  w. j  ava 2  s .c  om*/
        OutputEncryptor encOut = new JcePKCSPBEOutputEncryptorBuilder(NISTObjectIdentifiers.id_aes256_CBC)
                .setProvider("BC").build(password);

        JcaX509ExtensionUtils extUtils = new JcaX509ExtensionUtils();
        PKCS12PfxPduBuilder builder = new PKCS12PfxPduBuilder();

        for (String keyID : sharedPrivateKeyMap.getIDs()) {
            SubjectPublicKeyInfo pubKey = this.fetchPublicKey(keyID);

            // TODO: perhaps add CA cert and trust anchor to key store if available
            PKCS12SafeBagBuilder eeCertBagBuilder = new PKCS12SafeBagBuilder(
                    createCertificate(keyID, sharedPrivateKeyMap.getShare(keyID).getSequenceNo(),
                            (PrivateKey) nodeContext.getNodeCAStore().getKey("nodeCA", new char[0])));

            eeCertBagBuilder.addBagAttribute(PKCS12SafeBag.friendlyNameAttribute, new DERBMPString(keyID));

            SubjectKeyIdentifier pubKeyId = extUtils.createSubjectKeyIdentifier(pubKey);

            eeCertBagBuilder.addBagAttribute(PKCS12SafeBag.localKeyIdAttribute, pubKeyId);

            PKCS12SafeBagBuilder keyBagBuilder = new PKCS12SafeBagBuilder(PrivateKeyInfoFactory
                    .createPrivateKeyInfo(sharedPrivateKeyMap.getShare(keyID).getValue(), paramsMap.get(keyID)),
                    encOut);

            keyBagBuilder.addBagAttribute(PKCS12SafeBag.friendlyNameAttribute, new DERBMPString(keyID));
            keyBagBuilder.addBagAttribute(PKCS12SafeBag.localKeyIdAttribute, pubKeyId);

            builder.addEncryptedData(
                    new JcePKCSPBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHAAnd128BitRC2_CBC)
                            .setProvider("BC").build(password),
                    new PKCS12SafeBag[] { eeCertBagBuilder.build() });

            builder.addData(keyBagBuilder.build());
        }

        PKCS12PfxPdu pfx = builder.build(new JcePKCS12MacCalculatorBuilder(NISTObjectIdentifiers.id_sha256),
                password);

        return pfx.getEncoded(ASN1Encoding.DL);
    } catch (PKCSException e) {
        throw new GeneralSecurityException("Unable to create key store: " + e.getMessage(), e);
    } catch (OperatorCreationException e) {
        throw new GeneralSecurityException("Unable to create operator: " + e.getMessage(), e);
    }
}

From source file:org.cryptoworkshop.ximix.node.crypto.key.ECKeyManager.java

License:Apache License

public synchronized byte[] getEncoded(char[] password) throws IOException, GeneralSecurityException {
    KeyFactory fact = KeyFactory.getInstance("ECDSA", "BC");

    try {//from  w  ww.ja va2s. co m
        OutputEncryptor encOut = new JcePKCSPBEOutputEncryptorBuilder(NISTObjectIdentifiers.id_aes256_CBC)
                .setProvider("BC").build(password);

        JcaX509ExtensionUtils extUtils = new JcaX509ExtensionUtils();
        PKCS12PfxPduBuilder builder = new PKCS12PfxPduBuilder();

        for (String keyID : sharedPrivateKeyMap.getIDs()) {
            ECDomainParameters domainParams = paramsMap.get(keyID);
            PrivateKey privKey = fact
                    .generatePrivate(new PKCS8EncodedKeySpec(PrivateKeyInfoFactory
                            .createPrivateKeyInfo(new ECPrivateKeyParameters(
                                    sharedPrivateKeyMap.getShare(keyID).getValue(), domainParams))
                            .getEncoded()));
            SubjectPublicKeyInfo pubKey = this.fetchPublicKey(keyID);

            // TODO: perhaps add CA cert and trust anchor to key store if available
            PKCS12SafeBagBuilder eeCertBagBuilder = new PKCS12SafeBagBuilder(
                    createCertificate(keyID, sharedPrivateKeyMap.getShare(keyID).getSequenceNo(),
                            (PrivateKey) nodeContext.getNodeCAStore().getKey("nodeCA", new char[0])));

            eeCertBagBuilder.addBagAttribute(PKCS12SafeBag.friendlyNameAttribute, new DERBMPString(keyID));

            SubjectKeyIdentifier pubKeyId = extUtils.createSubjectKeyIdentifier(pubKey);

            eeCertBagBuilder.addBagAttribute(PKCS12SafeBag.localKeyIdAttribute, pubKeyId);

            PKCS12SafeBagBuilder keyBagBuilder = new JcaPKCS12SafeBagBuilder(privKey, encOut);

            keyBagBuilder.addBagAttribute(PKCS12SafeBag.friendlyNameAttribute, new DERBMPString(keyID));
            keyBagBuilder.addBagAttribute(PKCS12SafeBag.localKeyIdAttribute, pubKeyId);

            builder.addEncryptedData(
                    new JcePKCSPBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHAAnd128BitRC2_CBC)
                            .setProvider("BC").build(password),
                    new PKCS12SafeBag[] { eeCertBagBuilder.build() });

            builder.addData(keyBagBuilder.build());
        }

        PKCS12PfxPdu pfx = builder.build(new JcePKCS12MacCalculatorBuilder(NISTObjectIdentifiers.id_sha256),
                password);

        return pfx.getEncoded(ASN1Encoding.DL);
    } catch (PKCSException e) {
        throw new GeneralSecurityException("Unable to create key store: " + e.getMessage(), e);
    } catch (OperatorCreationException e) {
        throw new GeneralSecurityException("Unable to create operator: " + e.getMessage(), e);
    }
}