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);
}
}
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");
}
}
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());
}
}
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();
}
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);
}
Aggregations