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