Skip to content

Commit b623764

Browse files
author
Zainab Fatmi
committed
Deal with non-supported ECKey subclasses
Signed-off-by: Zainab Fatmi <[email protected]>
1 parent a61bdbd commit b623764

File tree

1 file changed

+47
-27
lines changed

1 file changed

+47
-27
lines changed

closed/src/jdk.crypto.ec/share/classes/sun/security/ec/NativeECDHKeyAgreement.java

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.security.ProviderException;
4141
import java.security.PublicKey;
4242
import java.security.SecureRandom;
43+
import java.security.interfaces.ECKey;
4344
import java.security.spec.AlgorithmParameterSpec;
4445
import java.security.spec.ECGenParameterSpec;
4546
import java.security.spec.ECParameterSpec;
@@ -100,34 +101,43 @@ protected void engineInit(Key key, SecureRandom random)
100101
("Key must be an instance of PrivateKey");
101102
}
102103
/* attempt to translate the key if it is not an ECKey */
103-
this.privateKey = (ECPrivateKeyImpl) ECKeyFactory.toECKey(key);
104-
this.publicKey = null;
104+
ECKey ecKey = ECKeyFactory.toECKey(key);
105+
if (ecKey instanceof ECPrivateKeyImpl keyImpl) {
106+
this.privateKey = keyImpl;
107+
this.publicKey = null;
105108

106-
ECParameterSpec params = this.privateKey.getParams();
107-
if (params instanceof NamedCurve) {
108-
this.curve = ((NamedCurve) params).getNameAndAliases()[0];
109-
} else {
110-
/* use the OID */
111-
try {
112-
AlgorithmParameters algParams = AlgorithmParameters.getInstance("EC");
113-
algParams.init(this.privateKey.getParams());
114-
this.curve = algParams.getParameterSpec(ECGenParameterSpec.class).getName();
115-
} catch (InvalidParameterSpecException | NoSuchAlgorithmException e) {
116-
/* should not happen */
117-
throw new InternalError(e);
109+
ECParameterSpec params = this.privateKey.getParams();
110+
if (params instanceof NamedCurve nc) {
111+
this.curve = nc.getNameAndAliases()[0];
112+
} else {
113+
/* use the OID */
114+
try {
115+
AlgorithmParameters algParams = AlgorithmParameters.getInstance("EC");
116+
algParams.init(params);
117+
this.curve = algParams.getParameterSpec(ECGenParameterSpec.class).getName();
118+
} catch (InvalidParameterSpecException | NoSuchAlgorithmException e) {
119+
/* should not happen */
120+
throw new InternalError(e);
121+
}
118122
}
119-
}
120123

121-
if ((!nativeGF2m) && this.privateKey.isECFieldF2m()) {
122-
/* only print the first time a curve is used */
123-
if ((curveSupported.putIfAbsent("EC2m", Boolean.FALSE) == null) && (nativeCryptTrace != null)) {
124-
System.err.println("EC2m is not supported by OpenSSL, using Java crypto implementation.");
124+
if ((!nativeGF2m) && this.privateKey.isECFieldF2m()) {
125+
/* only print the first time a curve is used */
126+
if ((curveSupported.putIfAbsent("EC2m", Boolean.FALSE) == null) && (nativeCryptTrace != null)) {
127+
System.err.println("EC2m is not supported by OpenSSL, using Java crypto implementation.");
128+
}
129+
this.initializeJavaImplementation(key, random);
130+
} else if (Boolean.FALSE.equals(curveSupported.get(this.curve))) {
131+
this.initializeJavaImplementation(key, random);
132+
} else {
133+
this.javaImplementation = null;
125134
}
126-
this.initializeJavaImplementation(key, random);
127-
} else if (Boolean.FALSE.equals(curveSupported.get(this.curve))) {
128-
this.initializeJavaImplementation(key, random);
129135
} else {
130-
this.javaImplementation = null;
136+
if ((curveSupported.putIfAbsent("ECKeyImpl", Boolean.FALSE) == null) && (nativeCryptTrace != null)) {
137+
System.err.println("Only ECPrivateKeyImpl and ECPublicKeyImpl are supported by the native implementation,"
138+
+ " using Java crypto implementation.");
139+
}
140+
this.initializeJavaImplementation(key, random);
131141
}
132142
}
133143

@@ -162,12 +172,22 @@ protected Key engineDoPhase(Key key, boolean lastPhase)
162172
("Key must be an instance of PublicKey");
163173
}
164174
/* attempt to translate the key if it is not an ECKey */
165-
this.publicKey = (ECPublicKeyImpl) ECKeyFactory.toECKey(key);
175+
ECKey ecKey = ECKeyFactory.toECKey(key);
176+
if (ecKey instanceof ECPublicKeyImpl keyImpl) {
177+
this.publicKey = keyImpl;
166178

167-
int keyLenBits = this.publicKey.getParams().getCurve().getField().getFieldSize();
168-
this.secretLen = (keyLenBits + 7) >> 3;
179+
int keyLenBits = this.publicKey.getParams().getCurve().getField().getFieldSize();
180+
this.secretLen = (keyLenBits + 7) >> 3;
169181

170-
return null;
182+
return null;
183+
} else {
184+
if ((curveSupported.putIfAbsent("ECKeyImpl", Boolean.FALSE) == null) && (nativeCryptTrace != null)) {
185+
System.err.println("Only ECPrivateKeyImpl and ECPublicKeyImpl are supported by the native implementation,"
186+
+ " using Java crypto implementation.");
187+
}
188+
this.initializeJavaImplementation(this.privateKey, null);
189+
return this.javaImplementation.engineDoPhase(key, lastPhase);
190+
}
171191
}
172192

173193
@Override

0 commit comments

Comments
 (0)