Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/main/java/com/wolfssl/provider/jce/WolfCryptECKeyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,19 @@ private PrivateKey generatePrivateFromECSpec(ECPrivateKeySpec keySpec)
"ECParameterSpec cannot be null");
}

/* Validate ECParameterSpec is supported by wolfCrypt,
* throws WolfCryptException if invalid */
WolfCryptECParameterSpec.validateParameters(keySpec.getParams());
/* Validate ECParameterSpec is supported by wolfCrypt.
* validateParameters throws IllegalArgumentException (unchecked)
* when the curve is not recognized; wrap it as
* InvalidKeySpecException so callers see a
* GeneralSecurityException. */
try {
WolfCryptECParameterSpec.validateParameters(
keySpec.getParams());
} catch (IllegalArgumentException e) {
throw new InvalidKeySpecException(
"Unsupported curve parameters: " +
e.getMessage(), e);
}

/* Get curve name from ECParameterSpec */
try {
Expand Down Expand Up @@ -560,9 +570,19 @@ private PublicKey generatePublicFromECSpec(ECPublicKeySpec keySpec)
"ECParameterSpec cannot be null");
}

/* Validate ECParameterSpec is supported by wolfCrypt,
* throws WolfCryptException if invalid */
WolfCryptECParameterSpec.validateParameters(keySpec.getParams());
/* Validate ECParameterSpec is supported by wolfCrypt.
* validateParameters throws IllegalArgumentException (unchecked)
* when the curve is not recognized; wrap it as
* InvalidKeySpecException so callers see a
* GeneralSecurityException. */
try {
WolfCryptECParameterSpec.validateParameters(
keySpec.getParams());
} catch (IllegalArgumentException e) {
throw new InvalidKeySpecException(
"Unsupported curve parameters: " +
e.getMessage(), e);
}

/* Get curve name from ECParameterSpec */
try {
Expand Down
64 changes: 61 additions & 3 deletions src/main/java/com/wolfssl/provider/jce/WolfCryptKeyAgreement.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ enum EngineState {
private Ecc ecPrivate = null;

private int primeLen = 0;
private byte[] dhParamP = null;
private byte[] dhParamG = null;
private int curveSize = 0;
private String curveName = null;

Expand Down Expand Up @@ -128,12 +130,31 @@ protected Key engineDoPhase(Key key, boolean lastPhase)
"Key must be of type DHPublicKey");
}

pubKey = ((DHPublicKey)key).getY().toByteArray();
DHPublicKey dhPubKey = (DHPublicKey)key;

pubKey = dhPubKey.getY().toByteArray();
if (pubKey == null) {
throw new InvalidKeyException(
"Failed to get DH public key from Key object");
}

if (this.dhParamP == null || this.dhParamG == null) {
throw new InvalidKeyException(
"DH private key not initialized with parameters");
}

BigInteger privP = new BigInteger(this.dhParamP);
BigInteger privG = new BigInteger(this.dhParamG);
BigInteger pubPBig = dhPubKey.getParams().getP();
BigInteger pubGBig = dhPubKey.getParams().getG();

if (!privP.equals(pubPBig) || !privG.equals(pubGBig)) {
throw new InvalidKeyException(
"DH public key parameters do not match " +
"private key parameters, cannot generate " +
"shared secret");
}

/* Validate peer public key (SP 800-56A) before use */
try {
this.dh.checkPublicKey(pubKey);
Expand Down Expand Up @@ -162,10 +183,35 @@ protected Key engineDoPhase(Key key, boolean lastPhase)
* the curve before use. */
try {
this.ecPublic.publicKeyDecode(pubKey);
} catch (WolfCryptException e) {
throw new InvalidKeyException(
"EC public key could not be decoded", e);
}

try {
this.ecPublic.checkKey();
} catch (WolfCryptException e) {
throw new InvalidKeyException(
"ECC public key failed validation", e);
"EC public key point is not on the curve", e);
}

/* Verify the public key curve matches the private key curve.
* A curve mismatch (e.g. secp224r1 public against secp256r1
* private) is caught here so the caller sees
* InvalidKeyException
* rather than a WolfCryptException from makeSharedSecret. */
try {
int pubCurveId = this.ecPublic.getCurveId();
int privCurveId = this.ecPrivate.getCurveId();
if (pubCurveId != privCurveId) {
throw new InvalidKeyException(
"EC public key curve does not match private key " +
"curve (public curveId=" + pubCurveId +
", private curveId=" + privCurveId + ")");
}
} catch (WolfCryptException e) {
throw new InvalidKeyException(
"Failed to validate EC key curve compatibility", e);
}

break;
Expand Down Expand Up @@ -326,7 +372,13 @@ protected int engineGenerateSecret(byte[] sharedSecret, int offset)

case WC_ECDH:

tmp = this.ecPrivate.makeSharedSecret(this.ecPublic);
try {
tmp = this.ecPrivate.makeSharedSecret(this.ecPublic);
} catch (WolfCryptException e) {
throw new IllegalStateException(
"Native ECDH shared secret generation failed: " +
e.getMessage(), e);
}
if (tmp == null) {
throw new RuntimeException("Error when creating " +
"ECDH shared secret");
Expand Down Expand Up @@ -477,6 +529,9 @@ private void wcInitDHParams(Key key, AlgorithmParameterSpec params)

this.dh.setParams(paramP, paramG);

this.dhParamP = paramP;
this.dhParamG = paramG;

primeLen = paramP.length;

/* prime may have leading zero */
Expand Down Expand Up @@ -505,6 +560,9 @@ private void wcInitDHParams(Key key, AlgorithmParameterSpec params)

this.dh.setParams(paramP, paramG);

this.dhParamP = paramP;
this.dhParamG = paramG;

primeLen = paramP.length;

/* prime may have leading zero */
Expand Down
Loading