Skip to content

Commit

Permalink
Merge pull request Azure#34 from tiffanyachen/dev
Browse files Browse the repository at this point in the history
Fixing up javadoc errors as well as modifying exception throwing
  • Loading branch information
schaabs authored Mar 26, 2018
2 parents 7412f4b + 344828d commit eb41400
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.math.BigInteger;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyFactory;
import java.security.KeyPair;
Expand Down Expand Up @@ -223,54 +224,58 @@ public static EcKey fromJsonWebKey(JsonWebKey jwk, boolean includePrivateParamet
* @param includePrivateParameters true if the EC key pair should include the private key. False otherwise.
* @param provider the Java Security Provider
* @return EcKey
* @throws NoSuchAlgorithmException
* @throws InvalidAlgorithmParameterException
* @throws InvalidKeySpecException
* @throws NoSuchProviderException
*/
public static EcKey fromJsonWebKey(JsonWebKey jwk, boolean includePrivateParameters, Provider provider) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchProviderException {
if (jwk.kid() != null) {
return new EcKey(jwk.kid(), jwk.toEC(includePrivateParameters, provider));
} else {
throw new IllegalArgumentException("Json Web Key should have a kid");
public static EcKey fromJsonWebKey(JsonWebKey jwk, boolean includePrivateParameters, Provider provider) {
try {
if (jwk.kid() != null) {
return new EcKey(jwk.kid(), jwk.toEC(includePrivateParameters, provider));
} else {
throw new IllegalArgumentException("Json Web Key should have a kid");
}
} catch (GeneralSecurityException e) {
throw new IllegalStateException(e);
}
}

/**
* Converts EcKey to JSON web key.
* @return
*/
public JsonWebKey toJsonWebKey() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
public JsonWebKey toJsonWebKey() {
return JsonWebKey.fromEC(_keyPair, _provider);
}

// Matches the curve of the keyPair to supported curves.
private JsonWebKeyCurveName getCurveFromKeyPair(KeyPair keyPair) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
ECPublicKey key = (ECPublicKey) keyPair.getPublic();
ECParameterSpec spec = key.getParams();
EllipticCurve crv = spec.getCurve();

List<JsonWebKeyCurveName> curveList = Arrays.asList(JsonWebKeyCurveName.P_256, JsonWebKeyCurveName.P_384, JsonWebKeyCurveName.P_521, JsonWebKeyCurveName.SECP256K1);

for (JsonWebKeyCurveName curve : curveList) {
ECGenParameterSpec gps = new ECGenParameterSpec(CURVE_TO_SPEC_NAME.get(curve));
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", _provider);
kpg.initialize(gps);
private JsonWebKeyCurveName getCurveFromKeyPair(KeyPair keyPair) {
try {
ECPublicKey key = (ECPublicKey) keyPair.getPublic();
ECParameterSpec spec = key.getParams();
EllipticCurve crv = spec.getCurve();

// Generate dummy keypair to get parameter spec.
KeyPair apair = kpg.generateKeyPair();
ECPublicKey apub = (ECPublicKey) apair.getPublic();
ECParameterSpec aspec = apub.getParams();
EllipticCurve acurve = aspec.getCurve();
List<JsonWebKeyCurveName> curveList = Arrays.asList(JsonWebKeyCurveName.P_256, JsonWebKeyCurveName.P_384, JsonWebKeyCurveName.P_521, JsonWebKeyCurveName.SECP256K1);

//Matches the parameter spec
if (acurve.equals(crv)) {
return curve;
for (JsonWebKeyCurveName curve : curveList) {
ECGenParameterSpec gps = new ECGenParameterSpec(CURVE_TO_SPEC_NAME.get(curve));
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", _provider);
kpg.initialize(gps);

// Generate dummy keypair to get parameter spec.
KeyPair apair = kpg.generateKeyPair();
ECPublicKey apub = (ECPublicKey) apair.getPublic();
ECParameterSpec aspec = apub.getParams();
EllipticCurve acurve = aspec.getCurve();

//Matches the parameter spec
if (acurve.equals(crv)) {
return curve;
}
}

//Did not find a supported curve.
throw new IllegalArgumentException ("Curve not supported.");
} catch (GeneralSecurityException e) {
throw new IllegalStateException(e);
}

//Did not find a supported curve.
throw new IllegalArgumentException ("Curve not supported.");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,18 +575,25 @@ private PrivateKey getRSAPrivateKey(Provider provider) {
}


private static PublicKey getECPublicKey(ECPoint ecPoint, ECParameterSpec curveSpec, Provider provider) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchProviderException {
private static PublicKey getECPublicKey(ECPoint ecPoint, ECParameterSpec curveSpec, Provider provider) {
// Create public key spec with given point
ECPublicKeySpec pubSpec = new ECPublicKeySpec(ecPoint, curveSpec);
KeyFactory kf = provider != null ? KeyFactory.getInstance("EC", provider) : KeyFactory.getInstance("EC", "SunEC");
return (ECPublicKey) kf.generatePublic(pubSpec);

try {
ECPublicKeySpec pubSpec = new ECPublicKeySpec(ecPoint, curveSpec);
KeyFactory kf = provider != null ? KeyFactory.getInstance("EC", provider) : KeyFactory.getInstance("EC", "SunEC");
return (ECPublicKey) kf.generatePublic(pubSpec);
} catch (GeneralSecurityException e) {
throw new IllegalStateException(e);
}
}

private static PrivateKey getECPrivateKey(byte[] d, ECParameterSpec curveSpec, Provider provider) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
ECPrivateKeySpec priSpec = new ECPrivateKeySpec(new BigInteger(1, d), curveSpec);
KeyFactory kf = provider != null ? KeyFactory.getInstance("EC", provider) : KeyFactory.getInstance("EC", "SunEC");
return (ECPrivateKey) kf.generatePrivate(priSpec);
private static PrivateKey getECPrivateKey(byte[] d, ECParameterSpec curveSpec, Provider provider) {
try {
ECPrivateKeySpec priSpec = new ECPrivateKeySpec(new BigInteger(1, d), curveSpec);
KeyFactory kf = provider != null ? KeyFactory.getInstance("EC", provider) : KeyFactory.getInstance("EC", "SunEC");
return (ECPrivateKey) kf.generatePrivate(priSpec);
} catch (GeneralSecurityException e) {
throw new IllegalStateException(e);
}
}

/**
Expand Down Expand Up @@ -699,30 +706,28 @@ public KeyPair toRSA(boolean includePrivateParameters, Provider provider) {
/**
* Converts JSON web key to EC key pair and include the private key if set to true.
* @return EC key pair
* @throws NoSuchProviderException
*/
public KeyPair toEC() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchProviderException {
public KeyPair toEC() {
return toEC(false, null);
}

/**
* Converts JSON web key to EC key pair and include the private key if set to true.
* @param includePrivateParameters true if the EC key pair should include the private key. False otherwise.
* @return EC key pair
* @throws NoSuchProviderException
*/
public KeyPair toEC(boolean includePrivateParameters) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchProviderException {
public KeyPair toEC(boolean includePrivateParameters) {
return toEC(includePrivateParameters, null);
}

/**
* Converts JSON web key to EC key pair and include the private key if set to true.
* @param provider the Java security provider.
* @param includePrivateParameters true if the EC key pair should include the private key. False otherwise.
* @param provider Java security provider
* @return EC key pair
* @throws NoSuchProviderException
*/
public KeyPair toEC(boolean includePrivateParameters, Provider provider) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException, NoSuchProviderException {
public KeyPair toEC(boolean includePrivateParameters, Provider provider) {

if (provider == null) {
//Our default provider for this class
Expand All @@ -733,39 +738,42 @@ public KeyPair toEC(boolean includePrivateParameters, Provider provider) throws
throw new IllegalArgumentException("Not an EC key.");
}

KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", provider);

ECGenParameterSpec gps = new ECGenParameterSpec(CURVE_TO_SPEC_NAME.get(crv));
kpg.initialize(gps);

// Generate dummy keypair to get parameter spec.
KeyPair apair = kpg.generateKeyPair();
ECPublicKey apub = (ECPublicKey) apair.getPublic();
ECParameterSpec aspec = apub.getParams();

ECPoint ecPoint = new ECPoint(new BigInteger(1, x), new BigInteger(1, y));

KeyPair realKeyPair;

if (includePrivateParameters) {
realKeyPair = new KeyPair(getECPublicKey(ecPoint, aspec, provider), getECPrivateKey(d, aspec, provider));
} else {
realKeyPair = new KeyPair(getECPublicKey(ecPoint, aspec, provider), null);
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", provider);

ECGenParameterSpec gps = new ECGenParameterSpec(CURVE_TO_SPEC_NAME.get(crv));
kpg.initialize(gps);


// Generate dummy keypair to get parameter spec.
KeyPair apair = kpg.generateKeyPair();
ECPublicKey apub = (ECPublicKey) apair.getPublic();
ECParameterSpec aspec = apub.getParams();

ECPoint ecPoint = new ECPoint(new BigInteger(1, x), new BigInteger(1, y));

KeyPair realKeyPair;

if (includePrivateParameters) {
realKeyPair = new KeyPair(getECPublicKey(ecPoint, aspec, provider), getECPrivateKey(d, aspec, provider));
} else {
realKeyPair = new KeyPair(getECPublicKey(ecPoint, aspec, provider), null);
}

return realKeyPair;
} catch (GeneralSecurityException e) {
throw new IllegalStateException(e);
}

return realKeyPair;
}

/**
* Converts EC key pair to JSON web key.
* @param keyPair EC key pair
* @provider Java security provider
* @param provider Java security provider
* @return the JSON web key, converted from EC key pair.
* @throws InvalidAlgorithmParameterException
* @throws NoSuchAlgorithmException
*/
public static JsonWebKey fromEC(KeyPair keyPair, Provider provider) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {

public static JsonWebKey fromEC(KeyPair keyPair, Provider provider) {
ECPublicKey apub = (ECPublicKey) keyPair.getPublic();
ECPoint point = apub.getW();
ECPrivateKey apriv = (ECPrivateKey) keyPair.getPrivate();
Expand All @@ -789,32 +797,37 @@ public static JsonWebKey fromEC(KeyPair keyPair, Provider provider) throws NoSuc
}

// Matches the curve of the keyPair to supported curves.
private static JsonWebKeyCurveName getCurveFromKeyPair(KeyPair keyPair, Provider provider) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
ECPublicKey key = (ECPublicKey) keyPair.getPublic();
ECParameterSpec spec = key.getParams();
EllipticCurve crv = spec.getCurve();

List<JsonWebKeyCurveName> curveList = Arrays.asList(JsonWebKeyCurveName.P_256, JsonWebKeyCurveName.P_384, JsonWebKeyCurveName.P_521, JsonWebKeyCurveName.SECP256K1);
private static JsonWebKeyCurveName getCurveFromKeyPair(KeyPair keyPair, Provider provider) {

for (JsonWebKeyCurveName curve : curveList) {
ECGenParameterSpec gps = new ECGenParameterSpec(CURVE_TO_SPEC_NAME.get(curve));
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", provider);
kpg.initialize(gps);
try {
ECPublicKey key = (ECPublicKey) keyPair.getPublic();
ECParameterSpec spec = key.getParams();
EllipticCurve crv = spec.getCurve();

// Generate dummy keypair to get parameter spec.
KeyPair apair = kpg.generateKeyPair();
ECPublicKey apub = (ECPublicKey) apair.getPublic();
ECParameterSpec aspec = apub.getParams();
EllipticCurve acurve = aspec.getCurve();
List<JsonWebKeyCurveName> curveList = Arrays.asList(JsonWebKeyCurveName.P_256, JsonWebKeyCurveName.P_384, JsonWebKeyCurveName.P_521, JsonWebKeyCurveName.SECP256K1);

//Matches the parameter spec
if (acurve.equals(crv)) {
return curve;
for (JsonWebKeyCurveName curve : curveList) {
ECGenParameterSpec gps = new ECGenParameterSpec(CURVE_TO_SPEC_NAME.get(curve));
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", provider);
kpg.initialize(gps);

// Generate dummy keypair to get parameter spec.
KeyPair apair = kpg.generateKeyPair();
ECPublicKey apub = (ECPublicKey) apair.getPublic();
ECParameterSpec aspec = apub.getParams();
EllipticCurve acurve = aspec.getCurve();

//Matches the parameter spec
if (acurve.equals(crv)) {
return curve;
}
}

//Did not find a supported curve.
throw new NoSuchAlgorithmException("Curve not supported.");
} catch (GeneralSecurityException e) {
throw new IllegalStateException(e);
}

//Did not find a supported curve.
throw new NoSuchAlgorithmException("Curve not supported.");
}

/**
Expand Down
Loading

0 comments on commit eb41400

Please sign in to comment.