Search in sources :

Example 1 with ECParameterSpec

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

the class ECSigner method sign.

@Deprecated
@Override
public String sign(String signingInput) throws Exception {
    if (Strings.isNullOrEmpty(signingInput)) {
        throw new Exception("Invalid signing input");
    }
    try {
        ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(getSignatureAlgorithm().getCurve().getName());
        ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(ecdsaPrivateKey.getD(), ecSpec);
        KeyFactory keyFactory = KeyFactory.getInstance("ECDSA", "BC");
        PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
        Signature signature = Signature.getInstance(getSignatureAlgorithm().getAlgorithm(), "BC");
        signature.initSign(privateKey);
        signature.update(signingInput.getBytes(Util.UTF8_STRING_ENCODING));
        return Base64Util.base64urlencode(signature.sign());
    } catch (NoSuchAlgorithmException e) {
        throw new Exception("There was a problem in EC signing", e);
    } catch (UnsupportedEncodingException e) {
        throw new Exception("There was a problem in EC signing", e);
    } catch (SignatureException e) {
        throw new Exception("There was a problem in EC signing", e);
    } catch (NoSuchProviderException e) {
        throw new Exception("There was a problem in EC signing", e);
    } catch (InvalidKeyException e) {
        throw new Exception("There was a problem in EC signing", e);
    } catch (InvalidKeySpecException e) {
        throw new Exception("There was a problem in EC signing", e);
    }
}
Also used : ECPrivateKeySpec(org.bouncycastle.jce.spec.ECPrivateKeySpec) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with ECParameterSpec

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

the class ECSigner method verifySignature.

@Deprecated
@Override
public boolean verifySignature(String signingInput, String signature) throws Exception {
    if (Strings.isNullOrEmpty(signingInput)) {
        return false;
    }
    if (Strings.isNullOrEmpty(signature)) {
        return false;
    }
    try {
        byte[] sigBytes = Base64Util.base64urldecode(signature);
        byte[] sigInBytes = signingInput.getBytes(Util.UTF8_STRING_ENCODING);
        ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(getSignatureAlgorithm().getCurve().getName());
        BigInteger q = ((ECCurve.Fp) ecSpec.getCurve()).getQ();
        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(getSignatureAlgorithm().getAlgorithm(), "BC");
        sig.initVerify(publicKey);
        sig.update(sigInBytes);
        return sig.verify(sigBytes);
    } catch (NoSuchAlgorithmException e) {
        throw new Exception("There was a problem in EC verifier", e);
    } catch (UnsupportedEncodingException e) {
        throw new Exception("There was a problem in EC verifier", e);
    } catch (SignatureException e) {
        throw new Exception("There was a problem in EC verifier", e);
    } catch (NoSuchProviderException e) {
        throw new Exception("There was a problem in EC verifier", e);
    } catch (InvalidKeyException e) {
        throw new Exception("There was a problem in EC verifier", e);
    } catch (InvalidKeySpecException e) {
        throw new Exception("There was a problem in EC verifier", e);
    }
}
Also used : 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) BigInteger(java.math.BigInteger) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) ECFieldElement(org.bouncycastle.math.ec.ECFieldElement)

Example 3 with ECParameterSpec

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

the class ECDSASigner method generateSignature.

@Override
public String generateSignature(String signingInput) throws SignatureException {
    if (getSignatureAlgorithm() == null) {
        throw new SignatureException("The signature algorithm is null");
    }
    if (ecdsaPrivateKey == null) {
        throw new SignatureException("The ECDSA private key is null");
    }
    if (signingInput == null) {
        throw new SignatureException("The signing input is null");
    }
    try {
        ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(getSignatureAlgorithm().getCurve().getName());
        ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(ecdsaPrivateKey.getD(), ecSpec);
        KeyFactory keyFactory = KeyFactory.getInstance("ECDSA", "BC");
        PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
        Signature signature = Signature.getInstance(getSignatureAlgorithm().getAlgorithm(), "BC");
        signature.initSign(privateKey);
        signature.update(signingInput.getBytes(Util.UTF8_STRING_ENCODING));
        return Base64Util.base64urlencode(signature.sign());
    } 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 : ECPrivateKeySpec(org.bouncycastle.jce.spec.ECPrivateKeySpec) ECDSAPrivateKey(org.xdi.oxauth.model.crypto.signature.ECDSAPrivateKey) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with ECParameterSpec

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

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 = BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
            return new ECPublicKeyParameters(((BCECPublicKey) 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()));
    } 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 public key");
            }
            PublicKey publicKey = BouncyCastleProvider.getPublicKey(SubjectPublicKeyInfo.getInstance(bytes));
            if (publicKey instanceof java.security.interfaces.ECPublicKey) {
                return ECUtil.generatePublicKeyParameter(publicKey);
            }
        } catch (Exception e) {
            throw new InvalidKeyException("cannot identify EC public key: " + e.toString());
        }
    }
    throw new InvalidKeyException("cannot identify EC public key.");
}
Also used : ECPublicKey(org.bouncycastle.jce.interfaces.ECPublicKey) BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) ECPublicKey(org.bouncycastle.jce.interfaces.ECPublicKey) PublicKey(java.security.PublicKey) BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) InvalidKeyException(java.security.InvalidKeyException) ECPublicKeyParameters(org.bouncycastle.crypto.params.ECPublicKeyParameters) InvalidKeyException(java.security.InvalidKeyException)

Example 5 with ECParameterSpec

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

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 = ProviderUtil.getEcImplicitlyCa();
        }
        return new ECPrivateKeyParameters(k.getD(), new ECDomainParameters(s.getCurve(), s.getG(), s.getN(), s.getH(), s.getSeed()));
    }
    throw new InvalidKeyException("can't identify EC private key.");
}
Also used : ECPrivateKey(org.bouncycastle.jce.interfaces.ECPrivateKey) ECPrivateKeyParameters(org.bouncycastle.crypto.params.ECPrivateKeyParameters) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

ECParameterSpec (org.bouncycastle.jce.spec.ECParameterSpec)21 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)12 ECPublicKeySpec (org.bouncycastle.jce.spec.ECPublicKeySpec)11 ECPoint (org.bouncycastle.math.ec.ECPoint)9 InvalidKeyException (java.security.InvalidKeyException)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)6 KeyFactory (java.security.KeyFactory)5 ECDomainParameters (org.bouncycastle.crypto.params.ECDomainParameters)5 ECPrivateKeySpec (org.bouncycastle.jce.spec.ECPrivateKeySpec)5 BigInteger (java.math.BigInteger)4 BCECPublicKey (org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 NoSuchProviderException (java.security.NoSuchProviderException)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 IOException (java.io.IOException)2