Search in sources :

Example 21 with ECParameterSpec

use of org.bouncycastle.jce.spec.ECParameterSpec in project jruby-openssl by jruby.

the class PKCS10Request method generatePublicKey.

public PublicKey generatePublicKey() throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
    AsymmetricKeyParameter keyParams = PublicKeyFactory.createKey(publicKeyInfo);
    final KeySpec keySpec;
    final KeyFactory keyFactory;
    if (keyParams instanceof RSAKeyParameters) {
        RSAKeyParameters rsa = (RSAKeyParameters) keyParams;
        keySpec = new RSAPublicKeySpec(rsa.getModulus(), rsa.getExponent());
        keyFactory = SecurityHelper.getKeyFactory("RSA");
        return keyFactory.generatePublic(keySpec);
    } else if (keyParams instanceof DSAPublicKeyParameters) {
        DSAPublicKeyParameters dsa = (DSAPublicKeyParameters) keyParams;
        DSAParameters params = dsa.getParameters();
        keySpec = new DSAPublicKeySpec(dsa.getY(), params.getP(), params.getQ(), params.getG());
        keyFactory = SecurityHelper.getKeyFactory("DSA");
        return keyFactory.generatePublic(keySpec);
    } else if (keyParams instanceof ECPublicKeyParameters) {
        ECPublicKeyParameters ec = (ECPublicKeyParameters) keyParams;
        ECDomainParameters ecParams = ec.getParameters();
        ECParameterSpec params = new ECParameterSpec(ecParams.getCurve(), ecParams.getG(), ecParams.getN(), ecParams.getH(), ecParams.getSeed());
        // NOTE: likely to fail if non BC factory picked up :
        keySpec = new ECPublicKeySpec(ec.getQ(), params);
        keyFactory = SecurityHelper.getKeyFactory("EC");
        return keyFactory.generatePublic(keySpec);
    } else {
        throw new IllegalStateException("could not generate public key for request, params type: " + keyParams);
    }
}
Also used : DSAPublicKeyParameters(org.bouncycastle.crypto.params.DSAPublicKeyParameters) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECPublicKeyParameters(org.bouncycastle.crypto.params.ECPublicKeyParameters) RSAKeyParameters(org.bouncycastle.crypto.params.RSAKeyParameters) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) AsymmetricKeyParameter(org.bouncycastle.crypto.params.AsymmetricKeyParameter) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) DSAParameters(org.bouncycastle.crypto.params.DSAParameters) PublicKeyFactory(org.bouncycastle.crypto.util.PublicKeyFactory) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 22 with ECParameterSpec

use of org.bouncycastle.jce.spec.ECParameterSpec in project karaf by apache.

the class KeyPairLoader method convertPrivateToPublicKey.

private static PublicKey convertPrivateToPublicKey(PrivateKey privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
    if (privateKey instanceof RSAPrivateCrtKey) {
        KeySpec keySpec = new RSAPublicKeySpec(((RSAPrivateCrtKey) privateKey).getModulus(), ((RSAPrivateCrtKey) privateKey).getPublicExponent());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(keySpec);
    } else if (privateKey instanceof ECPrivateKey) {
        ECPrivateKey ecPrivateKey = (ECPrivateKey) privateKey;
        // Derive the public point by multiplying the generator by the private value
        ECParameterSpec paramSpec = EC5Util.convertSpec(ecPrivateKey.getParams());
        ECPoint q = paramSpec.getG().multiply(ecPrivateKey.getS());
        KeySpec keySpec = new ECPublicKeySpec(q, paramSpec);
        KeyFactory keyFactory = KeyFactory.getInstance("EC");
        return keyFactory.generatePublic(keySpec);
    } else if (privateKey instanceof DSAPrivateKey) {
        DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey;
        BigInteger q = dsaPrivateKey.getParams().getQ();
        BigInteger p = dsaPrivateKey.getParams().getP();
        KeySpec keySpec = new DSAPublicKeySpec(q.modPow(dsaPrivateKey.getX(), p), p, q, dsaPrivateKey.getParams().getG());
        KeyFactory keyFactory = KeyFactory.getInstance("DSA");
        return keyFactory.generatePublic(keySpec);
    } else {
        LOGGER.warn("Unable to convert private key to public key. Only RSA, DSA + ECDSA supported");
        return null;
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECPoint(org.bouncycastle.math.ec.ECPoint) KeyFactory(java.security.KeyFactory) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Aggregations

ECParameterSpec (org.bouncycastle.jce.spec.ECParameterSpec)22 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)12 ECPublicKeySpec (org.bouncycastle.jce.spec.ECPublicKeySpec)12 ECPoint (org.bouncycastle.math.ec.ECPoint)10 InvalidKeyException (java.security.InvalidKeyException)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 KeyFactory (java.security.KeyFactory)6 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)6 BigInteger (java.math.BigInteger)5 ECDomainParameters (org.bouncycastle.crypto.params.ECDomainParameters)5 ECPrivateKeySpec (org.bouncycastle.jce.spec.ECPrivateKeySpec)5 BCECPublicKey (org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 NoSuchProviderException (java.security.NoSuchProviderException)3 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)3 ECCurve (org.bouncycastle.math.ec.ECCurve)3 PEMException (org.bouncycastle.openssl.PEMException)3 PEMParser (org.bouncycastle.openssl.PEMParser)3 JcaPEMKeyConverter (org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter)3 ByteString (com.google.protobuf.ByteString)2