Search in sources :

Example 1 with BCECPublicKey

use of org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey in project oxAuth by GluuFederation.

the class Certificate method getEcdsaPublicKey.

public ECDSAPublicKey getEcdsaPublicKey() {
    ECDSAPublicKey ecdsaPublicKey = null;
    if (x509Certificate != null && x509Certificate.getPublicKey() instanceof BCECPublicKey) {
        BCECPublicKey publicKey = (BCECPublicKey) x509Certificate.getPublicKey();
        ecdsaPublicKey = new ECDSAPublicKey(signatureAlgorithm, publicKey.getQ().getX().toBigInteger(), publicKey.getQ().getY().toBigInteger());
    }
    return ecdsaPublicKey;
}
Also used : BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) ECDSAPublicKey(org.xdi.oxauth.model.crypto.signature.ECDSAPublicKey)

Example 2 with BCECPublicKey

use of org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey 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 3 with BCECPublicKey

use of org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey in project xipki by xipki.

the class P12KeyGenerator method genECKeypair.

// CHECKSTYLE:SKIP
private KeyPairWithSubjectPublicKeyInfo genECKeypair(String curveNameOrOid, SecureRandom random) throws Exception {
    ASN1ObjectIdentifier curveOid = AlgorithmUtil.getCurveOidForCurveNameOrOid(curveNameOrOid);
    if (curveOid == null) {
        throw new IllegalArgumentException("invalid curveNameOrOid '" + curveNameOrOid + "'");
    }
    KeyPair kp = KeyUtil.generateECKeypair(curveOid, random);
    AlgorithmIdentifier algId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, curveOid);
    BCECPublicKey pub = (BCECPublicKey) kp.getPublic();
    byte[] keyData = pub.getQ().getEncoded(false);
    SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo(algId, keyData);
    return new KeyPairWithSubjectPublicKeyInfo(kp, subjectPublicKeyInfo);
}
Also used : KeyPair(java.security.KeyPair) BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) SubjectPublicKeyInfo(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) AlgorithmIdentifier(org.bouncycastle.asn1.x509.AlgorithmIdentifier)

Example 4 with BCECPublicKey

use of org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey in project oxAuth by GluuFederation.

the class Certificate method getPublicKey.

public PublicKey getPublicKey() {
    PublicKey publicKey = null;
    if (x509Certificate != null && x509Certificate.getPublicKey() instanceof BCRSAPublicKey) {
        BCRSAPublicKey jcersaPublicKey = (BCRSAPublicKey) x509Certificate.getPublicKey();
        publicKey = new RSAPublicKey(jcersaPublicKey.getModulus(), jcersaPublicKey.getPublicExponent());
    } else if (x509Certificate != null && x509Certificate.getPublicKey() instanceof BCECPublicKey) {
        BCECPublicKey jceecPublicKey = (BCECPublicKey) x509Certificate.getPublicKey();
        publicKey = new ECDSAPublicKey(signatureAlgorithm, jceecPublicKey.getQ().getXCoord().toBigInteger(), jceecPublicKey.getQ().getYCoord().toBigInteger());
    }
    return publicKey;
}
Also used : RSAPublicKey(org.gluu.oxauth.model.crypto.signature.RSAPublicKey) BCRSAPublicKey(org.bouncycastle.jcajce.provider.asymmetric.rsa.BCRSAPublicKey) BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) RSAPublicKey(org.gluu.oxauth.model.crypto.signature.RSAPublicKey) BCRSAPublicKey(org.bouncycastle.jcajce.provider.asymmetric.rsa.BCRSAPublicKey) BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) ECDSAPublicKey(org.gluu.oxauth.model.crypto.signature.ECDSAPublicKey) BCRSAPublicKey(org.bouncycastle.jcajce.provider.asymmetric.rsa.BCRSAPublicKey) ECDSAPublicKey(org.gluu.oxauth.model.crypto.signature.ECDSAPublicKey)

Example 5 with BCECPublicKey

use of org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey in project web3sdk by FISCO-BCOS.

the class ECKeyPair method create.

/**
 * create ECKeyPair from KeyPair
 *
 * @param keyPair
 * @return
 */
public static ECKeyPair create(KeyPair keyPair) {
    BCECPrivateKey privateKey = (BCECPrivateKey) keyPair.getPrivate();
    BCECPublicKey publicKey = (BCECPublicKey) keyPair.getPublic();
    BigInteger privateKeyValue = privateKey.getD();
    // Ethereum does not use encoded public keys like bitcoin - see
    // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details
    // Additionally, as the first bit is a constant prefix (0x04) we ignore this value
    byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);
    BigInteger publicKeyValue = new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));
    ECKeyPair ecKeyPair = new ECKeyPair(privateKeyValue, publicKeyValue);
    return ecKeyPair;
}
Also used : BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) BigInteger(java.math.BigInteger) BCECPrivateKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)

Aggregations

BCECPublicKey (org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey)11 BigInteger (java.math.BigInteger)3 SubjectPublicKeyInfo (org.bouncycastle.asn1.x509.SubjectPublicKeyInfo)3 ECParameterSpec (org.bouncycastle.jce.spec.ECParameterSpec)3 InvalidKeyException (java.security.InvalidKeyException)2 PublicKey (java.security.PublicKey)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)2 X9ECParameters (org.bouncycastle.asn1.x9.X9ECParameters)2 ECDomainParameters (org.bouncycastle.crypto.params.ECDomainParameters)2 ECPublicKeyParameters (org.bouncycastle.crypto.params.ECPublicKeyParameters)2 BCECPrivateKey (org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)2 BCRSAPublicKey (org.bouncycastle.jcajce.provider.asymmetric.rsa.BCRSAPublicKey)2 ECPublicKeySpec (org.bouncycastle.jce.spec.ECPublicKeySpec)2 PEMException (org.bouncycastle.openssl.PEMException)2 PEMParser (org.bouncycastle.openssl.PEMParser)2 JcaPEMKeyConverter (org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter)2 ECDSAPublicKey (org.gluu.oxauth.model.crypto.signature.ECDSAPublicKey)2 ECDSAPublicKey (org.xdi.oxauth.model.crypto.signature.ECDSAPublicKey)2 ByteString (com.google.protobuf.ByteString)1