Search in sources :

Example 41 with ECPoint

use of java.security.spec.ECPoint in project j2objc by google.

the class ECPrivateKeySpecTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    ECPoint ecpoint = new ECPoint(BigInteger.valueOf(1), BigInteger.valueOf(1));
    EllipticCurve curve = new EllipticCurve(new ECFieldF2m(2), BigInteger.valueOf(1), BigInteger.valueOf(1));
    s = BigInteger.valueOf(1);
    ecparams = new ECParameterSpec(curve, ecpoint, BigInteger.valueOf(1), 1);
    ecpks = new ECPrivateKeySpec(s, ecparams);
}
Also used : ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) ECFieldF2m(java.security.spec.ECFieldF2m) ECPoint(java.security.spec.ECPoint)

Example 42 with ECPoint

use of java.security.spec.ECPoint in project dhis2-core by dhis2.

the class KeyGeneratorUtils method generateEcKey.

static KeyPair generateEcKey() {
    EllipticCurve ellipticCurve = new EllipticCurve(new ECFieldFp(new BigInteger("115792089210356248762697446949407573530086143415290314195533631308867097853951")), new BigInteger("115792089210356248762697446949407573530086143415290314195533631308867097853948"), new BigInteger("41058363725152142129326129780047268409114441015993725554835256314039467401291"));
    ECPoint ecPoint = new ECPoint(new BigInteger("48439561293906451759052585252797914202762949526041747995844080717082404635286"), new BigInteger("36134250956749795798585127919587881956611106672985015071877198253568414405109"));
    ECParameterSpec ecParameterSpec = new ECParameterSpec(ellipticCurve, ecPoint, new BigInteger("115792089210356248762697446949407573529996955224135760342422259061068512044369"), 1);
    KeyPair keyPair;
    try {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
        keyPairGenerator.initialize(ecParameterSpec);
        keyPair = keyPairGenerator.generateKeyPair();
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }
    return keyPair;
}
Also used : KeyPair(java.security.KeyPair) ECFieldFp(java.security.spec.ECFieldFp) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) KeyPairGenerator(java.security.KeyPairGenerator) ECPoint(java.security.spec.ECPoint)

Example 43 with ECPoint

use of java.security.spec.ECPoint in project Payara by payara.

the class JwtPublicKeyStore method createPublicKeyFromJWKS.

private PublicKey createPublicKeyFromJWKS(String jwksValue, String keyID) throws Exception {
    JsonObject jwks = JwtKeyStoreUtils.parseJwks(jwksValue);
    JsonArray keys = jwks.getJsonArray("keys");
    JsonObject jwk = keys != null ? JwtKeyStoreUtils.findJwk(keys, keyID) : jwks;
    // Check if an RSA or ECDSA key needs to be created
    String kty = jwk.getString("kty");
    if (kty == null) {
        throw new DeploymentException("Could not determine key type - kty field not present");
    }
    if (kty.equals("RSA")) {
        // the public exponent
        byte[] exponentBytes = Base64.getUrlDecoder().decode(jwk.getString("e"));
        BigInteger exponent = new BigInteger(1, exponentBytes);
        // the modulus
        byte[] modulusBytes = Base64.getUrlDecoder().decode(jwk.getString("n"));
        BigInteger modulus = new BigInteger(1, modulusBytes);
        RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, exponent);
        return KeyFactory.getInstance(RSA_ALGORITHM).generatePublic(publicKeySpec);
    } else if (kty.equals("EC")) {
        // Get x and y to create EC point
        byte[] xBytes = Base64.getUrlDecoder().decode(jwk.getString("x"));
        BigInteger x = new BigInteger(1, xBytes);
        byte[] yBytes = Base64.getUrlDecoder().decode(jwk.getString("y"));
        BigInteger y = new BigInteger(1, yBytes);
        ECPoint ecPoint = new ECPoint(x, y);
        // Get params
        AlgorithmParameters parameters = AlgorithmParameters.getInstance(EC_ALGORITHM);
        String crv = jwk.getString("crv");
        if (!crv.equals("P-256")) {
            throw new DeploymentException("Could not get EC key from JWKS: crv does not equal P-256");
        }
        parameters.init(new ECGenParameterSpec("secp256r1"));
        ECPublicKeySpec publicKeySpec = new ECPublicKeySpec(ecPoint, parameters.getParameterSpec(ECParameterSpec.class));
        return KeyFactory.getInstance(EC_ALGORITHM).generatePublic(publicKeySpec);
    } else {
        throw new DeploymentException("Could not determine key type - JWKS kty field does not equal RSA or EC");
    }
}
Also used : JsonArray(javax.json.JsonArray) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) JsonObject(javax.json.JsonObject) BigInteger(java.math.BigInteger) DeploymentException(javax.enterprise.inject.spi.DeploymentException) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) AlgorithmParameters(java.security.AlgorithmParameters)

Example 44 with ECPoint

use of java.security.spec.ECPoint in project athenz by yahoo.

the class DataStore method getJWK.

JWK getJWK(final String pemKey, final String keyId, boolean rfc) {
    PublicKey publicKey;
    try {
        publicKey = Crypto.loadPublicKey(Crypto.ybase64DecodeString(pemKey));
    } catch (Exception ex) {
        LOGGER.error("Invalid public key: {}", ex.getMessage());
        return null;
    }
    JWK jwk = null;
    final Base64.Encoder encoder = Base64.getUrlEncoder().withoutPadding();
    switch(publicKey.getAlgorithm()) {
        case ZTSConsts.RSA:
            jwk = new JWK();
            jwk.setKid(keyId);
            jwk.setUse("sig");
            jwk.setKty("RSA");
            jwk.setAlg("RS256");
            final RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
            jwk.setN(new String(encoder.encode(Crypto.toIntegerBytes(rsaPublicKey.getModulus(), rfc))));
            jwk.setE(new String(encoder.encode(Crypto.toIntegerBytes(rsaPublicKey.getPublicExponent(), rfc))));
            break;
        case ZTSConsts.ECDSA:
            jwk = new JWK();
            jwk.setKid(keyId);
            jwk.setUse("sig");
            jwk.setKty("EC");
            jwk.setAlg("ES256");
            final ECPublicKey ecPublicKey = (ECPublicKey) publicKey;
            final ECPoint ecPoint = ecPublicKey.getW();
            jwk.setX(new String(encoder.encode(Crypto.toIntegerBytes(ecPoint.getAffineX(), rfc))));
            jwk.setY(new String(encoder.encode(Crypto.toIntegerBytes(ecPoint.getAffineY(), rfc))));
            jwk.setCrv(getCurveName(EC5Util.convertSpec(ecPublicKey.getParams()), rfc));
            break;
    }
    return jwk;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) ECPoint(java.security.spec.ECPoint) ResourceException(com.yahoo.athenz.zts.ResourceException) IOException(java.io.IOException)

Example 45 with ECPoint

use of java.security.spec.ECPoint in project karaf by apache.

the class PublickeyLoginModule method equals.

public static boolean equals(PublicKey key, String storedKey) throws FailedLoginException {
    try {
        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(storedKey)));
        String identifier = readString(dis);
        if (key instanceof DSAPublicKey) {
            if (!"ssh-dss".equals(identifier)) {
                return false;
            }
            BigInteger p = readBigInteger(dis);
            BigInteger q = readBigInteger(dis);
            BigInteger g = readBigInteger(dis);
            BigInteger y = readBigInteger(dis);
            KeyFactory keyFactory = KeyFactory.getInstance("DSA");
            KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g);
            PublicKey generatedPublicKey = keyFactory.generatePublic(publicKeySpec);
            return key.equals(generatedPublicKey);
        } else if (key instanceof RSAKey) {
            if (!"ssh-rsa".equals(identifier)) {
                return false;
            }
            BigInteger exponent = readBigInteger(dis);
            BigInteger modulus = readBigInteger(dis);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            KeySpec publicKeySpec = new RSAPublicKeySpec(modulus, exponent);
            PublicKey generatedPublicKey = keyFactory.generatePublic(publicKeySpec);
            return key.equals(generatedPublicKey);
        } else if (key instanceof ECPublicKey) {
            String ecIdentifier = readString(dis);
            if (!identifier.equals("ecdsa-sha2-" + ecIdentifier) || !nistSecMap.containsKey(ecIdentifier)) {
                return false;
            }
            // Overall size of the x + y coordinates. We only support uncompressed points here, so
            // to read x + y we ignore the "04" byte using (size - 1) / 2
            int size = dis.readInt();
            byte[] bytes = new byte[(size - 1) / 2];
            dis.skipBytes(1);
            dis.read(bytes, 0, bytes.length);
            BigInteger x = new BigInteger(bytes);
            dis.read(bytes, 0, bytes.length);
            BigInteger y = new BigInteger(bytes);
            KeyFactory keyFactory = KeyFactory.getInstance("EC");
            AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
            parameters.init(new ECGenParameterSpec(nistSecMap.get(ecIdentifier)));
            ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
            ECPoint pubPoint = new ECPoint(x, y);
            KeySpec keySpec = new ECPublicKeySpec(pubPoint, ecParameters);
            PublicKey generatedPublicKey = keyFactory.generatePublic(keySpec);
            return key.equals(generatedPublicKey);
        } else {
            throw new FailedLoginException("Unsupported key type " + key.getClass().toString());
        }
    } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException | InvalidParameterSpecException e) {
        throw new FailedLoginException("Unable to check public key");
    }
}
Also used : RSAKey(java.security.interfaces.RSAKey) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) KeyFactory(java.security.KeyFactory) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ECPoint(java.security.spec.ECPoint) ECPoint(java.security.spec.ECPoint) DSAPublicKey(java.security.interfaces.DSAPublicKey) FailedLoginException(javax.security.auth.login.FailedLoginException) ECPublicKey(java.security.interfaces.ECPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

ECPoint (java.security.spec.ECPoint)111 ECParameterSpec (java.security.spec.ECParameterSpec)56 BigInteger (java.math.BigInteger)54 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)36 ECPublicKey (java.security.interfaces.ECPublicKey)31 EllipticCurve (java.security.spec.EllipticCurve)31 KeyPair (java.security.KeyPair)20 ECPrivateKey (java.security.interfaces.ECPrivateKey)20 PublicKey (java.security.PublicKey)17 ECFieldFp (java.security.spec.ECFieldFp)17 ECGenParameterSpec (java.security.spec.ECGenParameterSpec)16 Test (org.junit.Test)16 KeyFactory (java.security.KeyFactory)15 KeyPairGenerator (java.security.KeyPairGenerator)14 AlgorithmParameters (java.security.AlgorithmParameters)13 GeneralSecurityException (java.security.GeneralSecurityException)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 KeySpec (java.security.spec.KeySpec)11 IOException (java.io.IOException)10 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)9