Skip to content

Commit

Permalink
Handle use of explicitly specified EC curve parameters (issue #446)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikramer committed Oct 8, 2023
1 parent 0770f7a commit 8dd9210
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions kse/src/org/kse/crypto/ecc/EccUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.nist.NISTNamedCurves;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.sec.SECNamedCurves;
Expand Down Expand Up @@ -76,7 +77,7 @@ private EccUtil() {
public static String getNamedCurve(Key key) {

if (!(key instanceof ECKey)) {
throw new InvalidParameterException("Not a EC private key.");
throw new InvalidParameterException("Not a EC key.");
}

ECKey ecKey = (ECKey) key;
Expand All @@ -87,7 +88,7 @@ public static String getNamedCurve(Key key) {
}

if (key instanceof PublicKey) {
return getNamedCurve(key);
return getNamedCurve((PublicKey) key);
}

return "";
Expand All @@ -108,21 +109,36 @@ public static String getNamedCurve(PublicKey publicKey) {

SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(publicKey.getEncoded());
ASN1Encodable parameters = subjectPublicKeyInfo.getAlgorithm().getParameters();
ASN1ObjectIdentifier curveId = ASN1ObjectIdentifier.getInstance(parameters);

String curveName = NISTNamedCurves.getName(curveId);
if (curveName == null) {
curveName = X962NamedCurves.getName(curveId);
}
if (curveName == null) {
curveName = SECNamedCurves.getName(curveId);
}
if (curveName == null) {
curveName = TeleTrusTNamedCurves.getName(curveId);
}
/*
* ECParameters ::= CHOICE {
* namedCurve OBJECT IDENTIFIER
* -- implicitCurve NULL
* -- specifiedCurve SpecifiedECDomain
* }
*/
if (parameters instanceof ASN1ObjectIdentifier) {
ASN1ObjectIdentifier curveId = ASN1ObjectIdentifier.getInstance(parameters);

if (curveName != null) {
return curveName;
String curveName = NISTNamedCurves.getName(curveId);
if (curveName == null) {
curveName = X962NamedCurves.getName(curveId);
}
if (curveName == null) {
curveName = SECNamedCurves.getName(curveId);
}
if (curveName == null) {
curveName = TeleTrusTNamedCurves.getName(curveId);
}

if (curveName != null) {
return curveName;
}
} else if (parameters instanceof ASN1Sequence) {
// RFC 5480: "specifiedCurve, which is of type SpecifiedECDomain type (defined
// in [X9.62]), allows all of the elliptic curve domain parameters
// to be explicitly specified. This choice MUST NOT be used."
return "explicitly specified curve";
}

return "";
Expand Down

0 comments on commit 8dd9210

Please sign in to comment.