|
40 | 40 | import java.security.ProviderException; |
41 | 41 | import java.security.PublicKey; |
42 | 42 | import java.security.SecureRandom; |
| 43 | +import java.security.interfaces.ECKey; |
43 | 44 | import java.security.spec.AlgorithmParameterSpec; |
44 | 45 | import java.security.spec.ECGenParameterSpec; |
45 | 46 | import java.security.spec.ECParameterSpec; |
@@ -100,34 +101,43 @@ protected void engineInit(Key key, SecureRandom random) |
100 | 101 | ("Key must be an instance of PrivateKey"); |
101 | 102 | } |
102 | 103 | /* 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; |
105 | 108 |
|
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 | + } |
118 | 122 | } |
119 | | - } |
120 | 123 |
|
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; |
125 | 134 | } |
126 | | - this.initializeJavaImplementation(key, random); |
127 | | - } else if (Boolean.FALSE.equals(curveSupported.get(this.curve))) { |
128 | | - this.initializeJavaImplementation(key, random); |
129 | 135 | } 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); |
131 | 141 | } |
132 | 142 | } |
133 | 143 |
|
@@ -162,12 +172,22 @@ protected Key engineDoPhase(Key key, boolean lastPhase) |
162 | 172 | ("Key must be an instance of PublicKey"); |
163 | 173 | } |
164 | 174 | /* 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; |
166 | 178 |
|
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; |
169 | 181 |
|
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 | + } |
171 | 191 | } |
172 | 192 |
|
173 | 193 | @Override |
|
0 commit comments