Search in sources :

Example 6 with BCECPrivateKey

use of org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey in project athenz by yahoo.

the class Crypto method extractPublicKey.

public static PublicKey extractPublicKey(PrivateKey privateKey) throws CryptoException {
    // we only support RSA and ECDSA private keys
    PublicKey publicKey;
    switch(privateKey.getAlgorithm()) {
        case RSA:
            try {
                KeyFactory kf = KeyFactory.getInstance(getRSAAlgo(), getKeyFactoryProvider());
                RSAPrivateCrtKey rsaCrtKey = (RSAPrivateCrtKey) privateKey;
                RSAPublicKeySpec keySpec = new RSAPublicKeySpec(rsaCrtKey.getModulus(), rsaCrtKey.getPublicExponent());
                publicKey = kf.generatePublic(keySpec);
            } catch (NoSuchProviderException ex) {
                LOG.error("extractPublicKey: RSA - Caught NoSuchProviderException exception: {}", ex.getMessage());
                throw new CryptoException(ex);
            } catch (NoSuchAlgorithmException ex) {
                LOG.error("extractPublicKey: RSA - Caught NoSuchAlgorithmException exception: {}", ex.getMessage());
                throw new CryptoException(ex);
            // /CLOVER:OFF
            } catch (InvalidKeySpecException ex) {
                LOG.error("extractPublicKey: RSA - Caught InvalidKeySpecException exception: {}", ex.getMessage());
                throw new CryptoException(ex);
            }
            // /CLOVER:ON
            break;
        case ECDSA:
            try {
                KeyFactory kf = KeyFactory.getInstance(getECDSAAlgo(), getKeyFactoryProvider());
                BCECPrivateKey ecPrivKey = (BCECPrivateKey) privateKey;
                ECMultiplier ecMultiplier = new FixedPointCombMultiplier();
                ECParameterSpec ecParamSpec = ecPrivKey.getParameters();
                ECPoint ecPointQ = ecMultiplier.multiply(ecParamSpec.getG(), ecPrivKey.getD());
                ECPublicKeySpec keySpec = new ECPublicKeySpec(ecPointQ, ecParamSpec);
                publicKey = kf.generatePublic(keySpec);
            } catch (NoSuchProviderException ex) {
                LOG.error("extractPublicKey: ECDSA - Caught NoSuchProviderException exception: {}", ex.getMessage());
                throw new CryptoException(ex);
            } catch (NoSuchAlgorithmException ex) {
                LOG.error("extractPublicKey: ECDSA - Caught NoSuchAlgorithmException exception: {}", ex.getMessage());
                throw new CryptoException(ex);
            // /CLOVER:OFF
            } catch (InvalidKeySpecException ex) {
                LOG.error("extractPublicKey: ECDSA - Caught InvalidKeySpecException exception: {}", ex.getMessage());
                throw new CryptoException(ex);
            }
            // /CLOVER:ON
            break;
        default:
            String msg = "Unsupported Key Algorithm: " + privateKey.getAlgorithm();
            LOG.error("extractPublicKey: {}", msg);
            throw new CryptoException(msg);
    }
    return publicKey;
}
Also used : RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) ECMultiplier(org.bouncycastle.math.ec.ECMultiplier) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECPoint(org.bouncycastle.math.ec.ECPoint) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) BCECPrivateKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey) FixedPointCombMultiplier(org.bouncycastle.math.ec.FixedPointCombMultiplier) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Aggregations

BCECPrivateKey (org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)6 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)3 BCECPublicKey (org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey)3 ECParameterSpec (org.bouncycastle.jce.spec.ECParameterSpec)3 BigInteger (java.math.BigInteger)2 PrivateKeyInfo (org.bouncycastle.asn1.pkcs.PrivateKeyInfo)2 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)2 ECPrivateKeySpec (org.bouncycastle.jce.spec.ECPrivateKeySpec)2 PEMException (org.bouncycastle.openssl.PEMException)2 PEMKeyPair (org.bouncycastle.openssl.PEMKeyPair)2 PEMParser (org.bouncycastle.openssl.PEMParser)2 JcaPEMKeyConverter (org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter)2 ByteString (com.google.protobuf.ByteString)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidKeyException (java.security.InvalidKeyException)1 KeyFactory (java.security.KeyFactory)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1