Example usage for org.bouncycastle.asn1.pkcs PKCS12PBEParams getIterations

List of usage examples for org.bouncycastle.asn1.pkcs PKCS12PBEParams getIterations

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs PKCS12PBEParams getIterations.

Prototype

public BigInteger getIterations() 

Source Link

Usage

From source file:org.jruby.ext.openssl.x509store.BouncyCastleASN1FormatHandler.java

License:LGPL

/**
 * c: PEM_read_PrivateKey + PEM_read_bio_PrivateKey
 * CAUTION: KeyPair#getPublic() may be null.
 *//*from  www .  jav a2  s .  c  o  m*/
@Override
public KeyPair readPrivateKey(Reader in, char[] password) throws IOException {
    BufferedReader _in = makeBuffered(in);
    String line;
    while ((line = _in.readLine()) != null) {
        if (line.indexOf(BEF_G + PEM_STRING_RSA) != -1) {
            try {
                return readKeyPair(_in, password, "RSA", BEF_E + PEM_STRING_RSA);
            } catch (Exception e) {
                throw new IOException("problem creating RSA private key: " + e.toString());
            }
        } else if (line.indexOf(BEF_G + PEM_STRING_DSA) != -1) {
            try {
                return readKeyPair(_in, password, "DSA", BEF_E + PEM_STRING_DSA);
            } catch (Exception e) {
                throw new IOException("problem creating DSA private key: " + e.toString());
            }
        } else if (line.indexOf(BEF_G + PEM_STRING_ECPRIVATEKEY) != -1) {
            throw new IOException("EC private key not supported");
        } else if (line.indexOf(BEF_G + PEM_STRING_PKCS8INF) != -1) {
            try {
                byte[] bytes = readBytes(_in, BEF_E + PEM_STRING_PKCS8INF);
                ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
                ASN1InputStream aIn = new ASN1InputStream(bIn);
                PrivateKeyInfo info = new PrivateKeyInfo((ASN1Sequence) aIn.readObject());
                String type = getPrivateKeyTypeFromObjectId(info.getAlgorithmId().getObjectId());
                return readPrivateKeySequence(info.getPrivateKey().getDEREncoded(), type);
            } catch (Exception e) {
                throw new IOException("problem creating private key: " + e.toString());
            }
        } else if (line.indexOf(BEF_G + PEM_STRING_PKCS8) != -1) {
            try {
                byte[] bytes = readBytes(_in, BEF_E + PEM_STRING_PKCS8);
                ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
                ASN1InputStream aIn = new ASN1InputStream(bIn);
                org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo eIn = new org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo(
                        (ASN1Sequence) aIn.readObject());
                AlgorithmIdentifier algId = eIn.getEncryptionAlgorithm();
                String algorithm = ASN1Registry.o2a(algId.getObjectId());
                algorithm = (algorithm.split("-"))[0];
                PKCS12PBEParams pbeParams = new PKCS12PBEParams((ASN1Sequence) algId.getParameters());
                SecretKeyFactory fact = OpenSSLReal.getSecretKeyFactoryBC(algorithm); // need to use BC for PKCS12PBEParams.
                PBEKeySpec pbeSpec = new PBEKeySpec(password);
                SecretKey key = fact.generateSecret(pbeSpec);
                PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(),
                        pbeParams.getIterations().intValue());
                Cipher cipher = OpenSSLReal.getCipherBC(algorithm); // need to use BC for PBEParameterSpec.
                cipher.init(Cipher.UNWRAP_MODE, key, defParams);
                // wrappedKeyAlgorithm is unknown ("")
                PrivateKey privKey = (PrivateKey) cipher.unwrap(eIn.getEncryptedData(), "", Cipher.PRIVATE_KEY);
                return new KeyPair(null, privKey);
            } catch (Exception e) {
                throw new IOException("problem creating private key: " + e.toString());
            }
        }
    }
    return null;
}

From source file:org.jruby.ext.openssl.x509store.PEMInputOutput.java

License:LGPL

private static PrivateKey derivePrivateKeyPBES1(EncryptedPrivateKeyInfo eIn, AlgorithmIdentifier algId,
        char[] password) throws GeneralSecurityException, IOException {
    // From BC's PEMReader
    PKCS12PBEParams pkcs12Params = PKCS12PBEParams.getInstance(algId.getParameters());
    PBEKeySpec pbeSpec = new PBEKeySpec(password);
    PBEParameterSpec pbeParams = new PBEParameterSpec(pkcs12Params.getIV(),
            pkcs12Params.getIterations().intValue());

    //String algorithm = algId.getAlgorithm().getId();
    String algorithm = ASN1Registry.o2a(algId.getAlgorithm());
    algorithm = (algorithm.split("-"))[0];

    SecretKeyFactory secKeyFact = SecretKeyFactory.getInstance(algorithm);

    Cipher cipher = Cipher.getInstance(algorithm);

    cipher.init(Cipher.DECRYPT_MODE, secKeyFact.generateSecret(pbeSpec), pbeParams);

    PrivateKeyInfo pInfo = PrivateKeyInfo
            .getInstance(ASN1Primitive.fromByteArray(cipher.doFinal(eIn.getEncryptedData())));
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pInfo.getEncoded());

    String keyFactAlg = ASN1Registry.o2a(pInfo.getPrivateKeyAlgorithm().getAlgorithm());

    // TODO: Can we just set it to RSA as in derivePrivateKeyPBES2?
    KeyFactory keyFact;//  w w  w  . j ava 2  s.co  m
    if (keyFactAlg.startsWith("dsa")) {
        keyFact = KeyFactory.getInstance("DSA");
    } else {
        keyFact = KeyFactory.getInstance("RSA");
    }

    return keyFact.generatePrivate(keySpec);
}

From source file:org.mailster.core.crypto.MailsterKeyStoreFactory.java

License:Open Source License

public boolean checkCryptoPermission() throws NoSuchAlgorithmException, NoSuchProviderException,
        NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException {
    LOG.debug("Cryptography permission check");

    try {/*from w ww . j  a  v  a2  s .co m*/
        byte[] iv = new byte[20];
        CertificateUtilities.RANDOM.nextBytes(iv);
        PKCS12PBEParams pbeParams = new PKCS12PBEParams(iv, 1024);
        String algorithm = "1.2.840.113549.1.12.1.3";
        SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm, "BC");
        PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(),
                pbeParams.getIterations().intValue());

        Cipher cipher = Cipher.getInstance(algorithm, "BC");
        PBEKeySpec pbeSpec = new PBEKeySpec("testwelcome".toCharArray());
        cipher.init(Cipher.WRAP_MODE, keyFact.generateSecret(pbeSpec), defParams);

        return true;
    } catch (InvalidKeyException ex) {
        cryptoPermissionDenied = true;
        setErrorMessage(Messages.getString("MailsterKeyStoreFactory.error.vm.crypto.restrictions"));
        return false;
    }
}