Search in sources :

Example 1 with ECPublicKey

use of java.security.interfaces.ECPublicKey in project jjwt by jwtk.

the class EllipticCurveSignatureValidator method isValid.

@Override
public boolean isValid(byte[] data, byte[] signature) {
    Signature sig = createSignatureInstance();
    PublicKey publicKey = (PublicKey) key;
    try {
        int expectedSize = getSignatureByteArrayLength(alg);
        /**
             *
             * If the expected size is not valid for JOSE, fall back to ASN.1 DER signature.
             * This fallback is for backwards compatibility ONLY (to support tokens generated by previous versions of jjwt)
             * and backwards compatibility will possibly be removed in a future version of this library.
             *
             * **/
        byte[] derSignature = expectedSize != signature.length && signature[0] == 0x30 ? signature : EllipticCurveProvider.transcodeSignatureToDER(signature);
        return doVerify(sig, publicKey, data, derSignature);
    } catch (Exception e) {
        String msg = "Unable to verify Elliptic Curve signature using configured ECPublicKey. " + e.getMessage();
        throw new SignatureException(msg, e);
    }
}
Also used : PublicKey(java.security.PublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) Signature(java.security.Signature) SignatureException(io.jsonwebtoken.SignatureException) SignatureException(io.jsonwebtoken.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 2 with ECPublicKey

use of java.security.interfaces.ECPublicKey in project robovm by robovm.

the class OpenSSLSignature method engineInitVerify.

@Override
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
    // If we had an existing context, destroy it first.
    destroyContextIfExists();
    if (publicKey instanceof OpenSSLKeyHolder) {
        OpenSSLKey pkey = ((OpenSSLKeyHolder) publicKey).getOpenSSLKey();
        checkEngineType(pkey);
        key = pkey;
    } else if (publicKey instanceof RSAPublicKey) {
        if (engineType != EngineType.RSA) {
            throw new InvalidKeyException("Signature not initialized as RSA");
        }
        RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
        key = OpenSSLRSAPublicKey.getInstance(rsaPublicKey);
    } else if (publicKey instanceof DSAPublicKey) {
        if (engineType != EngineType.DSA) {
            throw new InvalidKeyException("Signature not initialized as DSA");
        }
        DSAPublicKey dsaPublicKey = (DSAPublicKey) publicKey;
        key = OpenSSLDSAPublicKey.getInstance(dsaPublicKey);
    } else if (publicKey instanceof ECPublicKey) {
        if (engineType != EngineType.EC) {
            throw new InvalidKeyException("Signature not initialized as EC");
        }
        ECPublicKey ecPublicKey = (ECPublicKey) publicKey;
        key = OpenSSLECPublicKey.getInstance(ecPublicKey);
    } else {
        throw new InvalidKeyException("Need DSA or RSA or EC public key");
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) InvalidKeyException(java.security.InvalidKeyException) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 3 with ECPublicKey

use of java.security.interfaces.ECPublicKey in project robovm by robovm.

the class OpenSSLECKeyFactory method engineTranslateKey.

@Override
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
    if (key == null) {
        throw new InvalidKeyException("key == null");
    }
    if ((key instanceof OpenSSLECPublicKey) || (key instanceof OpenSSLECPrivateKey)) {
        return key;
    } else if (key instanceof ECPublicKey) {
        ECPublicKey ecKey = (ECPublicKey) key;
        ECPoint w = ecKey.getW();
        ECParameterSpec params = ecKey.getParams();
        try {
            return engineGeneratePublic(new ECPublicKeySpec(w, params));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if (key instanceof ECPrivateKey) {
        ECPrivateKey ecKey = (ECPrivateKey) key;
        BigInteger s = ecKey.getS();
        ECParameterSpec params = ecKey.getParams();
        try {
            return engineGeneratePrivate(new ECPrivateKeySpec(s, params));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if ((key instanceof PrivateKey) && ("PKCS#8".equals(key.getFormat()))) {
        byte[] encoded = key.getEncoded();
        if (encoded == null) {
            throw new InvalidKeyException("Key does not support encoding");
        }
        try {
            return engineGeneratePrivate(new PKCS8EncodedKeySpec(encoded));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else if ((key instanceof PublicKey) && ("X.509".equals(key.getFormat()))) {
        byte[] encoded = key.getEncoded();
        if (encoded == null) {
            throw new InvalidKeyException("Key does not support encoding");
        }
        try {
            return engineGeneratePublic(new X509EncodedKeySpec(encoded));
        } catch (InvalidKeySpecException e) {
            throw new InvalidKeyException(e);
        }
    } else {
        throw new InvalidKeyException("Key must be EC public or private key; was " + key.getClass().getName());
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) ECPrivateKeySpec(java.security.spec.ECPrivateKeySpec) PrivateKey(java.security.PrivateKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) PublicKey(java.security.PublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) InvalidKeyException(java.security.InvalidKeyException) ECPoint(java.security.spec.ECPoint) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) ECPublicKey(java.security.interfaces.ECPublicKey) ECParameterSpec(java.security.spec.ECParameterSpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) BigInteger(java.math.BigInteger) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 4 with ECPublicKey

use of java.security.interfaces.ECPublicKey in project robovm by robovm.

the class OpenSSLECPublicKey method equals.

@Override
public boolean equals(Object o) {
    if (o == this) {
        return true;
    }
    if (o instanceof OpenSSLECPrivateKey) {
        OpenSSLECPrivateKey other = (OpenSSLECPrivateKey) o;
        return key.equals(other.key);
    }
    if (!(o instanceof ECPublicKey)) {
        return false;
    }
    final ECPublicKey other = (ECPublicKey) o;
    if (!getPublicKey().equals(other.getW())) {
        return false;
    }
    final ECParameterSpec spec = getParams();
    final ECParameterSpec otherSpec = other.getParams();
    return spec.getCurve().equals(otherSpec.getCurve()) && spec.getGenerator().equals(otherSpec.getGenerator()) && spec.getOrder().equals(otherSpec.getOrder()) && spec.getCofactor() == otherSpec.getCofactor();
}
Also used : ECPublicKey(java.security.interfaces.ECPublicKey) ECParameterSpec(java.security.spec.ECParameterSpec)

Example 5 with ECPublicKey

use of java.security.interfaces.ECPublicKey in project robovm by robovm.

the class NativeCryptoTest method test_OpenSSLKey_toJava.

public void test_OpenSSLKey_toJava() throws Exception {
    OpenSSLKey key1;
    BigInteger e = BigInteger.valueOf(65537);
    key1 = new OpenSSLKey(NativeCrypto.RSA_generate_key_ex(1024, e.toByteArray()));
    assertTrue(key1.getPublicKey() instanceof RSAPublicKey);
    key1 = new OpenSSLKey(NativeCrypto.DSA_generate_key(1024, null, null, null, null));
    assertTrue(key1.getPublicKey() instanceof DSAPublicKey);
    long group1 = NULL;
    try {
        group1 = NativeCrypto.EC_GROUP_new_by_curve_name("prime256v1");
        assertTrue(group1 != NULL);
        key1 = new OpenSSLKey(NativeCrypto.EC_KEY_generate_key(group1));
    } finally {
        if (group1 != NULL) {
            NativeCrypto.EC_GROUP_clear_free(group1);
        }
    }
    assertTrue(key1.getPublicKey() instanceof ECPublicKey);
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) BigInteger(java.math.BigInteger) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Aggregations

ECPublicKey (java.security.interfaces.ECPublicKey)35 RSAPublicKey (java.security.interfaces.RSAPublicKey)14 PublicKey (java.security.PublicKey)13 KeyFactory (java.security.KeyFactory)11 KeyPairGenerator (java.security.KeyPairGenerator)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 ECPrivateKey (java.security.interfaces.ECPrivateKey)8 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)8 PrivateKey (java.security.PrivateKey)7 X509Certificate (java.security.cert.X509Certificate)7 ECParameterSpec (java.security.spec.ECParameterSpec)7 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 NonNull (android.annotation.NonNull)5 ExportResult (android.security.keymaster.ExportResult)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 KeyPair (java.security.KeyPair)5 NoSuchProviderException (java.security.NoSuchProviderException)5 ProviderException (java.security.ProviderException)5 Certificate (java.security.cert.Certificate)5 CertificateFactory (java.security.cert.CertificateFactory)5