Example usage for org.bouncycastle.pkcs PKCS8EncryptedPrivateKeyInfo PKCS8EncryptedPrivateKeyInfo

List of usage examples for org.bouncycastle.pkcs PKCS8EncryptedPrivateKeyInfo PKCS8EncryptedPrivateKeyInfo

Introduction

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

Prototype

public PKCS8EncryptedPrivateKeyInfo(byte[] encryptedPrivateKeyInfo) throws IOException 

Source Link

Usage

From source file:org.albertschmitt.crypto.RSAService.java

License:Open Source License

public RSAPrivateKey readPrivateKeyDER(InputStream instream, char[] password)
        throws IOException, OperatorCreationException, PKCSException {
    RSAPrivateKey key;//from   w w w.  jav a2 s.c  o m

    byte[] data = ByteUtil.readFileBytes(instream);

    PKCS8EncryptedPrivateKeyInfo pair = new PKCS8EncryptedPrivateKeyInfo(data);
    JceOpenSSLPKCS8DecryptorProviderBuilder jce = new JceOpenSSLPKCS8DecryptorProviderBuilder();
    InputDecryptorProvider decProv = jce.build(password);
    PrivateKeyInfo pki = pair.decryptPrivateKeyInfo(decProv);

    key = new RSAPrivateKey();
    key.setKey(pki);

    return key;
}

From source file:org.soulwing.credo.service.crypto.bc.BcPKCS8PrivateKeyDecoder.java

License:Apache License

/**
 * {@inheritDoc}//  w ww .  j a v a 2s  .  c om
 */
@Override
public PrivateKeyWrapper decode(String encoded) {
    PemObjectWrapper object = objectFactory.newPemObject(encoded);
    try {
        return new BcPrivateKeyWrapper(new PKCS8EncryptedPrivateKeyInfo(object.getContent()),
                objectBuilderFactory);
    } catch (IOException ex) {
        throw new IllegalArgumentException("invalid PKCS8 private key", ex);
    }
}

From source file:org.xipki.commons.security.pkcs11.emulator.EmulatorP11Slot.java

License:Open Source License

@Override
protected P11SlotRefreshResult doRefresh() throws P11TokenException {
    P11SlotRefreshResult ret = new P11SlotRefreshResult();
    for (long mech : supportedMechs) {
        ret.addMechanism(mech);// w ww.  j a v a  2 s .  c  o m
    }

    // Certificates

    File[] certInfoFiles = certDir.listFiles(INFO_FILENAME_FILTER);
    if (certInfoFiles != null) {
        for (File infoFile : certInfoFiles) {
            byte[] id = getKeyIdFromInfoFilename(infoFile.getName());
            Properties props = loadProperties(infoFile);
            String label = props.getProperty(PROP_LABEL);
            P11ObjectIdentifier objId = new P11ObjectIdentifier(id, label);
            try {
                X509Cert cert = readCertificate(id);
                ret.addCertificate(objId, cert);
            } catch (CertificateException | IOException ex) {
                LOG.warn("could not parse certificate " + objId);
            }
        }
    }

    // Private / Public keys
    File[] privKeyInfoFiles = privKeyDir.listFiles(INFO_FILENAME_FILTER);

    if (privKeyInfoFiles == null || privKeyInfoFiles.length == 0) {
        return ret;
    }

    for (File privKeyInfoFile : privKeyInfoFiles) {
        byte[] id = getKeyIdFromInfoFilename(privKeyInfoFile.getName());
        String hexId = Hex.toHexString(id);

        try {
            Properties props = loadProperties(privKeyInfoFile);
            String label = props.getProperty(PROP_LABEL);

            P11ObjectIdentifier p11ObjId = new P11ObjectIdentifier(id, label);
            X509Cert cert = ret.getCertForId(id);
            java.security.PublicKey publicKey = (cert == null) ? readPublicKey(id)
                    : cert.getCert().getPublicKey();

            if (publicKey == null) {
                LOG.warn("Neither public key nor certificate is associated with private key {}", p11ObjId);
                continue;
            }

            byte[] encodedValue = IoUtil.read(new File(privKeyDir, hexId + VALUE_FILE_SUFFIX));

            PKCS8EncryptedPrivateKeyInfo epki = new PKCS8EncryptedPrivateKeyInfo(encodedValue);
            PrivateKey privateKey = privateKeyCryptor.decrypt(epki);

            X509Certificate[] certs = (cert == null) ? null : new X509Certificate[] { cert.getCert() };

            EmulatorP11Identity identity = new EmulatorP11Identity(this,
                    new P11EntityIdentifier(slotId, p11ObjId), privateKey, publicKey, certs, maxSessions,
                    random);
            LOG.info("added PKCS#11 key {}", p11ObjId);
            ret.addIdentity(identity);
        } catch (InvalidKeyException ex) {
            LogUtil.warn(LOG, ex, "InvalidKeyException while initializing key with key-id " + hexId);
            continue;
        } catch (Throwable th) {
            LogUtil.warn(LOG, th, "unexpected exception while initializing key with key-id " + hexId);
            continue;
        }
    }

    return ret;
}