Search in sources :

Example 11 with ECPublicKeySpec

use of org.bouncycastle.jce.spec.ECPublicKeySpec in project oxAuth by GluuFederation.

the class SHA256withECDSASignatureVerification method decodePublicKey.

@Override
public PublicKey decodePublicKey(byte[] encodedPublicKey) throws SignatureException {
    X9ECParameters curve = SECNamedCurves.getByName("secp256r1");
    ECPoint point = curve.getCurve().decodePoint(encodedPublicKey);
    try {
        return KeyFactory.getInstance("ECDSA").generatePublic(new ECPublicKeySpec(point, new ECParameterSpec(curve.getCurve(), curve.getG(), curve.getN(), curve.getH())));
    } catch (GeneralSecurityException ex) {
        throw new SignatureException(ex);
    }
}
Also used : X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) GeneralSecurityException(java.security.GeneralSecurityException) SignatureException(org.gluu.oxauth.model.exception.SignatureException) ECPoint(org.bouncycastle.math.ec.ECPoint) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec)

Example 12 with ECPublicKeySpec

use of org.bouncycastle.jce.spec.ECPublicKeySpec 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 13 with ECPublicKeySpec

use of org.bouncycastle.jce.spec.ECPublicKeySpec 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

ECPublicKeySpec (org.bouncycastle.jce.spec.ECPublicKeySpec)13 ECParameterSpec (org.bouncycastle.jce.spec.ECParameterSpec)12 ECPoint (org.bouncycastle.math.ec.ECPoint)9 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 BigInteger (java.math.BigInteger)5 KeyFactory (java.security.KeyFactory)5 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)3 BCECPublicKey (org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey)3 ECCurve (org.bouncycastle.math.ec.ECCurve)3 GeneralSecurityException (java.security.GeneralSecurityException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 NoSuchProviderException (java.security.NoSuchProviderException)2 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)2 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)2 KeySpec (java.security.spec.KeySpec)2 SubjectPublicKeyInfo (org.bouncycastle.asn1.x509.SubjectPublicKeyInfo)2 ByteString (com.google.protobuf.ByteString)1