Example usage for org.bouncycastle.jce.provider BouncyCastleProvider getPrivateKey

List of usage examples for org.bouncycastle.jce.provider BouncyCastleProvider getPrivateKey

Introduction

In this page you can find the example usage for org.bouncycastle.jce.provider BouncyCastleProvider getPrivateKey.

Prototype

public static PrivateKey getPrivateKey(PrivateKeyInfo privateKeyInfo) throws IOException 

Source Link

Usage

From source file:ca.trustpoint.m2m.ecqv.EcqvProvider.java

License:Apache License

/**
 * Reconstruct the private key from the reconstruction data
 *
 * @param identifyingInfo the identity portion of the implicit certificate
 * @param reconstructionPoint the reconstruction point for the implicit certificate
 * @param privateKeyReconstructionData the private key reconstruction data associated with the
 *        implicit certificate//from   w  w  w .  j a  va  2  s  . c om
 * @param ephemeralPrivateKey the requesters ephemeral private key
 *
 * @return the private key associated with the implicit certificate
 *
 * @throws IOException when there are errors with, or malformed provided data
 */
public PrivateKey reconstructPrivateKey(byte[] identifyingInfo, byte[] reconstructionPoint,
        byte[] privateKeyReconstructionData, PrivateKey ephemeralPrivateKey) throws IOException {
    // curve point order
    BigInteger n = curveParameters.getN();

    // calculate H(Certu)
    for (byte b : identifyingInfo) {
        digest.update(b);
    }

    for (byte b : reconstructionPoint) {
        digest.update(b);
    }

    // compute the integer e from H(Certu)
    BigInteger e = calculateE(n, digest.digest()).mod(n);

    // compute the private Key dU = r + e*kU (mod n)
    BigInteger r = octetStringToInteger(privateKeyReconstructionData);

    // Check that the 'r' is less than 'n'
    if (n.compareTo(r) != 1) {
        throw new IOException("Octet String value is larger than modulus");
    }

    // Private key dU.
    BigInteger dU = ((BCECPrivateKey) ephemeralPrivateKey).getD();
    dU = e.multiply(dU);
    dU = r.add(dU);
    dU = dU.mod(n);

    return BouncyCastleProvider
            .getPrivateKey(new PrivateKeyInfo(algorithmId, new ASN1Integer(dU.toByteArray())));
}

From source file:com.brienwheeler.apps.tomcat.TomcatBean.java

License:Open Source License

private RSAPrivateKey readKeyFile() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    String parse[] = readPEMFile(sslKeyFile, KEY_PATTERN, 2);
    if (parse == null)
        throw new IllegalArgumentException("invalid key file contents");

    if (parse[0].length() == 0) { // BEGIN PRIVATE KEY
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return (RSAPrivateKey) keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.decode(parse[1])));
    }//from   w  w  w. j  a va2s.  c  o  m

    if (parse[0].contains("RSA")) { // BEGIN RSA PRIVATE KEY
        Security.addProvider(new BouncyCastleProvider());

        PEMParser pemParser = new PEMParser(new FileReader(sslKeyFile));
        Object parsedObject = pemParser.readObject();
        if (!(parsedObject instanceof PEMKeyPair))
            throw new IllegalArgumentException("invalid key file contents");

        PEMKeyPair keyPair = (PEMKeyPair) parsedObject;
        RSAPrivateKey privateKey = (RSAPrivateKey) BouncyCastleProvider
                .getPrivateKey(keyPair.getPrivateKeyInfo());
        if (privateKey == null)
            throw new IllegalArgumentException("invalid key file contents");
        return privateKey;
    }

    throw new IllegalArgumentException("invalid key file contents");
}