Search in sources :

Example 11 with ECParameterSpec

use of org.bouncycastle.jce.spec.ECParameterSpec in project XobotOS by xamarin.

the class ECUtil method generatePublicKeyParameter.

public static AsymmetricKeyParameter generatePublicKeyParameter(PublicKey key) throws InvalidKeyException {
    if (key instanceof ECPublicKey) {
        ECPublicKey k = (ECPublicKey) key;
        ECParameterSpec s = k.getParameters();
        if (s == null) {
            s = ProviderUtil.getEcImplicitlyCa();
            return new ECPublicKeyParameters(((JCEECPublicKey) k).engineGetQ(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
        } else {
            return new ECPublicKeyParameters(k.getQ(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
        }
    } else if (key instanceof java.security.interfaces.ECPublicKey) {
        java.security.interfaces.ECPublicKey pubKey = (java.security.interfaces.ECPublicKey) key;
        ECParameterSpec s = EC5Util.convertSpec(pubKey.getParams(), false);
        return new ECPublicKeyParameters(EC5Util.convertPoint(pubKey.getParams(), pubKey.getW(), false), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
    }
    throw new InvalidKeyException("cannot identify EC public key.");
}
Also used : ECPublicKey(org.bouncycastle.jce.interfaces.ECPublicKey) JCEECPublicKey(org.bouncycastle.jce.provider.JCEECPublicKey) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) InvalidKeyException(java.security.InvalidKeyException) ECPublicKeyParameters(org.bouncycastle.crypto.params.ECPublicKeyParameters)

Example 12 with ECParameterSpec

use of org.bouncycastle.jce.spec.ECParameterSpec in project robovm by robovm.

the class ECUtil method generatePrivateKeyParameter.

public static AsymmetricKeyParameter generatePrivateKeyParameter(PrivateKey key) throws InvalidKeyException {
    if (key instanceof ECPrivateKey) {
        ECPrivateKey k = (ECPrivateKey) key;
        ECParameterSpec s = k.getParameters();
        if (s == null) {
            s = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
        }
        return new ECPrivateKeyParameters(k.getD(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
    } else if (key instanceof java.security.interfaces.ECPrivateKey) {
        java.security.interfaces.ECPrivateKey privKey = (java.security.interfaces.ECPrivateKey) key;
        ECParameterSpec s = EC5Util.convertSpec(privKey.getParams(), false);
        return new ECPrivateKeyParameters(privKey.getS(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
    } else {
        // see if we can build a key from key.getEncoded()
        try {
            byte[] bytes = key.getEncoded();
            if (bytes == null) {
                throw new InvalidKeyException("no encoding for EC private key");
            }
            PrivateKey privateKey = BouncyCastleProvider.getPrivateKey(PrivateKeyInfo.getInstance(bytes));
            if (privateKey instanceof java.security.interfaces.ECPrivateKey) {
                return ECUtil.generatePrivateKeyParameter(privateKey);
            }
        } catch (Exception e) {
            throw new InvalidKeyException("cannot identify EC private key: " + e.toString());
        }
    }
    throw new InvalidKeyException("can't identify EC private key.");
}
Also used : ECPrivateKey(org.bouncycastle.jce.interfaces.ECPrivateKey) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) ECPrivateKey(org.bouncycastle.jce.interfaces.ECPrivateKey) PrivateKey(java.security.PrivateKey) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) InvalidKeyException(java.security.InvalidKeyException) InvalidKeyException(java.security.InvalidKeyException)

Example 13 with ECParameterSpec

use of org.bouncycastle.jce.spec.ECParameterSpec 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.xdi.oxauth.model.exception.SignatureException) ECPoint(org.bouncycastle.math.ec.ECPoint) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec)

Example 14 with ECParameterSpec

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

the class ECDSASigner method validateSignature.

@Override
public boolean validateSignature(String signingInput, String signature) throws SignatureException {
    if (getSignatureAlgorithm() == null) {
        throw new SignatureException("The signature algorithm is null");
    }
    if (ecdsaPublicKey == null) {
        throw new SignatureException("The ECDSA public key is null");
    }
    if (signingInput == null) {
        throw new SignatureException("The signing input is null");
    }
    String algorithm;
    String curve;
    switch(getSignatureAlgorithm()) {
        case ES256:
            algorithm = "SHA256WITHECDSA";
            curve = "P-256";
            break;
        case ES384:
            algorithm = "SHA384WITHECDSA";
            curve = "P-384";
            break;
        case ES512:
            algorithm = "SHA512WITHECDSA";
            curve = "P-521";
            break;
        default:
            throw new SignatureException("Unsupported signature algorithm");
    }
    try {
        byte[] sigBytes = Base64Util.base64urldecode(signature);
        byte[] sigInBytes = signingInput.getBytes(Util.UTF8_STRING_ENCODING);
        ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(curve);
        BigInteger q = ((ECCurve.AbstractFp) ecSpec.getCurve()).getField().getCharacteristic();
        ECFieldElement xFieldElement = new ECFieldElement.Fp(q, ecdsaPublicKey.getX());
        ECFieldElement yFieldElement = new ECFieldElement.Fp(q, ecdsaPublicKey.getY());
        ECPoint pointQ = new ECPoint.Fp(ecSpec.getCurve(), xFieldElement, yFieldElement);
        ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(pointQ, ecSpec);
        KeyFactory keyFactory = KeyFactory.getInstance("ECDSA", "BC");
        PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
        Signature sig = Signature.getInstance(algorithm, "BC");
        sig.initVerify(publicKey);
        sig.update(sigInBytes);
        return sig.verify(sigBytes);
    } catch (InvalidKeySpecException e) {
        throw new SignatureException(e);
    } catch (InvalidKeyException e) {
        throw new SignatureException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new SignatureException(e);
    } catch (NoSuchProviderException e) {
        throw new SignatureException(e);
    } catch (UnsupportedEncodingException e) {
        throw new SignatureException(e);
    } catch (Exception e) {
        throw new SignatureException(e);
    }
}
Also used : ECDSAPublicKey(org.xdi.oxauth.model.crypto.signature.ECDSAPublicKey) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ECPoint(org.bouncycastle.math.ec.ECPoint) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) ECCurve(org.bouncycastle.math.ec.ECCurve) BigInteger(java.math.BigInteger) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) ECFieldElement(org.bouncycastle.math.ec.ECFieldElement)

Example 15 with ECParameterSpec

use of org.bouncycastle.jce.spec.ECParameterSpec in project habot by ghys.

the class Utils method loadPublicKey.

/**
 * Load the public key from a URL-safe base64 encoded string. Takes into
 * account the different encodings, including point compression.
 *
 * @param encodedPublicKey
 */
public static PublicKey loadPublicKey(String encodedPublicKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
    byte[] decodedPublicKey = base64Decode(encodedPublicKey);
    KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM, PROVIDER_NAME);
    ECParameterSpec parameterSpec = ECNamedCurveTable.getParameterSpec(CURVE);
    ECCurve curve = parameterSpec.getCurve();
    ECPoint point = curve.decodePoint(decodedPublicKey);
    ECPublicKeySpec pubSpec = new ECPublicKeySpec(point, parameterSpec);
    return keyFactory.generatePublic(pubSpec);
}
Also used : ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) ECCurve(org.bouncycastle.math.ec.ECCurve) ECPoint(org.bouncycastle.math.ec.ECPoint) KeyFactory(java.security.KeyFactory) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec)

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