author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 57718 | a93b7b28f644 |
permissions | -rw-r--r-- |
50768 | 1 |
/* |
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. |
50768 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.security.ssl; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.nio.ByteBuffer; |
|
55353 | 30 |
import java.security.GeneralSecurityException; |
50768 | 31 |
import java.security.InvalidAlgorithmParameterException; |
32 |
import java.security.InvalidKeyException; |
|
33 |
import java.security.Key; |
|
34 |
import java.security.NoSuchAlgorithmException; |
|
35 |
import java.security.PrivateKey; |
|
36 |
import java.security.PublicKey; |
|
37 |
import java.security.Signature; |
|
38 |
import java.security.SignatureException; |
|
39 |
import java.text.MessageFormat; |
|
40 |
import java.util.Locale; |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55353
diff
changeset
|
41 |
import sun.security.ssl.NamedGroup.NamedGroupSpec; |
50768 | 42 |
import sun.security.ssl.SSLHandshake.HandshakeMessage; |
43 |
import sun.security.ssl.SupportedGroupsExtension.SupportedGroups; |
|
44 |
import sun.security.ssl.X509Authentication.X509Credentials; |
|
45 |
import sun.security.ssl.X509Authentication.X509Possession; |
|
46 |
import sun.security.util.HexDumpEncoder; |
|
47 |
||
48 |
/** |
|
49 |
* Pack of the ServerKeyExchange handshake message. |
|
50 |
*/ |
|
51 |
final class ECDHServerKeyExchange { |
|
52 |
static final SSLConsumer ecdheHandshakeConsumer = |
|
53 |
new ECDHServerKeyExchangeConsumer(); |
|
54 |
static final HandshakeProducer ecdheHandshakeProducer = |
|
55 |
new ECDHServerKeyExchangeProducer(); |
|
56 |
||
57 |
/** |
|
58 |
* The ECDH ServerKeyExchange handshake message. |
|
59 |
*/ |
|
60 |
private static final |
|
61 |
class ECDHServerKeyExchangeMessage extends HandshakeMessage { |
|
62 |
private static final byte CURVE_NAMED_CURVE = (byte)0x03; |
|
63 |
||
64 |
// id of the named curve |
|
65 |
private final NamedGroup namedGroup; |
|
66 |
||
67 |
// encoded public point |
|
68 |
private final byte[] publicPoint; |
|
69 |
||
70 |
// signature bytes, or null if anonymous |
|
71 |
private final byte[] paramsSignature; |
|
72 |
||
73 |
private final boolean useExplicitSigAlgorithm; |
|
74 |
||
75 |
// the signature algorithm used by this ServerKeyExchange message |
|
76 |
private final SignatureScheme signatureScheme; |
|
77 |
||
55353 | 78 |
// the parsed credential object |
79 |
private SSLCredentials sslCredentials; |
|
80 |
||
50768 | 81 |
ECDHServerKeyExchangeMessage( |
82 |
HandshakeContext handshakeContext) throws IOException { |
|
83 |
super(handshakeContext); |
|
84 |
||
85 |
// This happens in server side only. |
|
86 |
ServerHandshakeContext shc = |
|
87 |
(ServerHandshakeContext)handshakeContext; |
|
88 |
||
55353 | 89 |
// Find the Possessions needed |
90 |
NamedGroupPossession namedGroupPossession = null; |
|
50768 | 91 |
X509Possession x509Possession = null; |
92 |
for (SSLPossession possession : shc.handshakePossessions) { |
|
55353 | 93 |
if (possession instanceof NamedGroupPossession) { |
94 |
namedGroupPossession = (NamedGroupPossession)possession; |
|
50768 | 95 |
if (x509Possession != null) { |
96 |
break; |
|
97 |
} |
|
98 |
} else if (possession instanceof X509Possession) { |
|
99 |
x509Possession = (X509Possession)possession; |
|
55353 | 100 |
if (namedGroupPossession != null) { |
50768 | 101 |
break; |
102 |
} |
|
103 |
} |
|
104 |
} |
|
105 |
||
55353 | 106 |
if (namedGroupPossession == null) { |
50768 | 107 |
// unlikely |
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
108 |
throw shc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
50768 | 109 |
"No ECDHE credentials negotiated for server key exchange"); |
110 |
} |
|
111 |
||
55353 | 112 |
// Find the NamedGroup used for the ephemeral keys. |
113 |
namedGroup = namedGroupPossession.getNamedGroup(); |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55353
diff
changeset
|
114 |
if ((namedGroup == null) || (!namedGroup.isAvailable)) { |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55353
diff
changeset
|
115 |
// unlikely |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55353
diff
changeset
|
116 |
throw shc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55353
diff
changeset
|
117 |
"Missing or improper named group: " + namedGroup); |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55353
diff
changeset
|
118 |
} |
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55353
diff
changeset
|
119 |
|
55353 | 120 |
publicPoint = namedGroup.encodePossessionPublicKey( |
121 |
namedGroupPossession); |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55353
diff
changeset
|
122 |
if (publicPoint == null) { |
50768 | 123 |
// unlikely |
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
124 |
throw shc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55353
diff
changeset
|
125 |
"Missing public point for named group: " + namedGroup); |
50768 | 126 |
} |
127 |
||
128 |
if (x509Possession == null) { |
|
129 |
// anonymous, no authentication, no signature |
|
130 |
paramsSignature = null; |
|
131 |
signatureScheme = null; |
|
132 |
useExplicitSigAlgorithm = false; |
|
133 |
} else { |
|
134 |
useExplicitSigAlgorithm = |
|
135 |
shc.negotiatedProtocol.useTLS12PlusSpec(); |
|
136 |
Signature signer = null; |
|
137 |
if (useExplicitSigAlgorithm) { |
|
138 |
signatureScheme = SignatureScheme.getPreferableAlgorithm( |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55353
diff
changeset
|
139 |
shc.algorithmConstraints, |
50768 | 140 |
shc.peerRequestedSignatureSchemes, |
54417
f87041131515
8217610: TLSv1.3 fail with ClassException when EC keys are stored in PKCS11
xuelei
parents:
53734
diff
changeset
|
141 |
x509Possession, |
50768 | 142 |
shc.negotiatedProtocol); |
143 |
if (signatureScheme == null) { |
|
144 |
// Unlikely, the credentials generator should have |
|
145 |
// selected the preferable signature algorithm properly. |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
146 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 147 |
"No preferred signature algorithm for " + |
148 |
x509Possession.popPrivateKey.getAlgorithm() + |
|
149 |
" key"); |
|
150 |
} |
|
151 |
try { |
|
152 |
signer = signatureScheme.getSignature( |
|
153 |
x509Possession.popPrivateKey); |
|
154 |
} catch (NoSuchAlgorithmException | InvalidKeyException | |
|
155 |
InvalidAlgorithmParameterException nsae) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
156 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 157 |
"Unsupported signature algorithm: " + |
158 |
signatureScheme.name, nsae); |
|
159 |
} |
|
160 |
} else { |
|
161 |
signatureScheme = null; |
|
162 |
try { |
|
163 |
signer = getSignature( |
|
164 |
x509Possession.popPrivateKey.getAlgorithm(), |
|
165 |
x509Possession.popPrivateKey); |
|
166 |
} catch (NoSuchAlgorithmException | InvalidKeyException e) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
167 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 168 |
"Unsupported signature algorithm: " + |
169 |
x509Possession.popPrivateKey.getAlgorithm(), e); |
|
170 |
} |
|
171 |
} |
|
172 |
||
173 |
byte[] signature = null; |
|
174 |
try { |
|
175 |
updateSignature(signer, shc.clientHelloRandom.randomBytes, |
|
176 |
shc.serverHelloRandom.randomBytes, |
|
177 |
namedGroup.id, publicPoint); |
|
178 |
signature = signer.sign(); |
|
179 |
} catch (SignatureException ex) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
180 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 181 |
"Failed to sign ecdhe parameters: " + |
182 |
x509Possession.popPrivateKey.getAlgorithm(), ex); |
|
183 |
} |
|
184 |
paramsSignature = signature; |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
ECDHServerKeyExchangeMessage(HandshakeContext handshakeContext, |
|
189 |
ByteBuffer m) throws IOException { |
|
190 |
super(handshakeContext); |
|
191 |
||
192 |
// This happens in client side only. |
|
193 |
ClientHandshakeContext chc = |
|
194 |
(ClientHandshakeContext)handshakeContext; |
|
195 |
||
196 |
byte curveType = (byte)Record.getInt8(m); |
|
197 |
if (curveType != CURVE_NAMED_CURVE) { |
|
198 |
// Unlikely as only the named curves should be negotiated. |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
199 |
throw chc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
50768 | 200 |
"Unsupported ECCurveType: " + curveType); |
201 |
} |
|
202 |
||
203 |
int namedGroupId = Record.getInt16(m); |
|
204 |
this.namedGroup = NamedGroup.valueOf(namedGroupId); |
|
205 |
if (namedGroup == null) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
206 |
throw chc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
50768 | 207 |
"Unknown named group ID: " + namedGroupId); |
208 |
} |
|
209 |
||
210 |
if (!SupportedGroups.isSupported(namedGroup)) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
211 |
throw chc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
50768 | 212 |
"Unsupported named group: " + namedGroup); |
213 |
} |
|
214 |
||
215 |
publicPoint = Record.getBytes8(m); |
|
216 |
if (publicPoint.length == 0) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
217 |
throw chc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
55353 | 218 |
"Insufficient Point data: " + namedGroup); |
50768 | 219 |
} |
220 |
||
221 |
try { |
|
55353 | 222 |
sslCredentials = namedGroup.decodeCredentials( |
223 |
publicPoint, handshakeContext.algorithmConstraints, |
|
224 |
s -> chc.conContext.fatal(Alert.INSUFFICIENT_SECURITY, |
|
225 |
"ServerKeyExchange " + namedGroup + ": " + (s))); |
|
226 |
} catch (GeneralSecurityException ex) { |
|
227 |
throw chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
228 |
"Cannot decode named group: " + |
|
229 |
NamedGroup.nameOf(namedGroupId)); |
|
50768 | 230 |
} |
231 |
||
232 |
X509Credentials x509Credentials = null; |
|
233 |
for (SSLCredentials cd : chc.handshakeCredentials) { |
|
234 |
if (cd instanceof X509Credentials) { |
|
235 |
x509Credentials = (X509Credentials)cd; |
|
236 |
break; |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
if (x509Credentials == null) { |
|
241 |
// anonymous, no authentication, no signature |
|
242 |
if (m.hasRemaining()) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
243 |
throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 244 |
"Invalid DH ServerKeyExchange: unknown extra data"); |
245 |
} |
|
246 |
this.signatureScheme = null; |
|
247 |
this.paramsSignature = null; |
|
248 |
this.useExplicitSigAlgorithm = false; |
|
249 |
||
250 |
return; |
|
251 |
} |
|
252 |
||
253 |
this.useExplicitSigAlgorithm = |
|
254 |
chc.negotiatedProtocol.useTLS12PlusSpec(); |
|
255 |
if (useExplicitSigAlgorithm) { |
|
256 |
int ssid = Record.getInt16(m); |
|
257 |
signatureScheme = SignatureScheme.valueOf(ssid); |
|
258 |
if (signatureScheme == null) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
259 |
throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 260 |
"Invalid signature algorithm (" + ssid + |
261 |
") used in ECDH ServerKeyExchange handshake message"); |
|
262 |
} |
|
263 |
||
264 |
if (!chc.localSupportedSignAlgs.contains(signatureScheme)) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
265 |
throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 266 |
"Unsupported signature algorithm (" + |
267 |
signatureScheme.name + |
|
268 |
") used in ECDH ServerKeyExchange handshake message"); |
|
269 |
} |
|
270 |
} else { |
|
271 |
signatureScheme = null; |
|
272 |
} |
|
273 |
||
274 |
// read and verify the signature |
|
275 |
paramsSignature = Record.getBytes16(m); |
|
276 |
Signature signer; |
|
277 |
if (useExplicitSigAlgorithm) { |
|
278 |
try { |
|
279 |
signer = signatureScheme.getSignature( |
|
280 |
x509Credentials.popPublicKey); |
|
281 |
} catch (NoSuchAlgorithmException | InvalidKeyException | |
|
282 |
InvalidAlgorithmParameterException nsae) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
283 |
throw chc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 284 |
"Unsupported signature algorithm: " + |
285 |
signatureScheme.name, nsae); |
|
286 |
} |
|
287 |
} else { |
|
288 |
try { |
|
289 |
signer = getSignature( |
|
290 |
x509Credentials.popPublicKey.getAlgorithm(), |
|
291 |
x509Credentials.popPublicKey); |
|
292 |
} catch (NoSuchAlgorithmException | InvalidKeyException e) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
293 |
throw chc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 294 |
"Unsupported signature algorithm: " + |
295 |
x509Credentials.popPublicKey.getAlgorithm(), e); |
|
296 |
} |
|
297 |
} |
|
298 |
||
299 |
try { |
|
300 |
updateSignature(signer, |
|
301 |
chc.clientHelloRandom.randomBytes, |
|
302 |
chc.serverHelloRandom.randomBytes, |
|
303 |
namedGroup.id, publicPoint); |
|
304 |
||
305 |
if (!signer.verify(paramsSignature)) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
306 |
throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 307 |
"Invalid ECDH ServerKeyExchange signature"); |
308 |
} |
|
309 |
} catch (SignatureException ex) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
310 |
throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 311 |
"Cannot verify ECDH ServerKeyExchange signature", ex); |
312 |
} |
|
313 |
} |
|
314 |
||
315 |
@Override |
|
316 |
public SSLHandshake handshakeType() { |
|
317 |
return SSLHandshake.SERVER_KEY_EXCHANGE; |
|
318 |
} |
|
319 |
||
320 |
@Override |
|
321 |
public int messageLength() { |
|
322 |
int sigLen = 0; |
|
323 |
if (paramsSignature != null) { |
|
324 |
sigLen = 2 + paramsSignature.length; |
|
325 |
if (useExplicitSigAlgorithm) { |
|
326 |
sigLen += SignatureScheme.sizeInRecord(); |
|
327 |
} |
|
328 |
} |
|
329 |
||
330 |
return 4 + publicPoint.length + sigLen; |
|
331 |
} |
|
332 |
||
333 |
@Override |
|
334 |
public void send(HandshakeOutStream hos) throws IOException { |
|
335 |
hos.putInt8(CURVE_NAMED_CURVE); |
|
336 |
hos.putInt16(namedGroup.id); |
|
337 |
hos.putBytes8(publicPoint); |
|
338 |
if (paramsSignature != null) { |
|
339 |
if (useExplicitSigAlgorithm) { |
|
340 |
hos.putInt16(signatureScheme.id); |
|
341 |
} |
|
342 |
||
343 |
hos.putBytes16(paramsSignature); |
|
344 |
} |
|
345 |
} |
|
346 |
||
347 |
@Override |
|
348 |
public String toString() { |
|
349 |
if (useExplicitSigAlgorithm) { |
|
350 |
MessageFormat messageFormat = new MessageFormat( |
|
351 |
"\"ECDH ServerKeyExchange\": '{'\n" + |
|
352 |
" \"parameters\": '{'\n" + |
|
353 |
" \"named group\": \"{0}\"\n" + |
|
354 |
" \"ecdh public\": '{'\n" + |
|
355 |
"{1}\n" + |
|
356 |
" '}',\n" + |
|
357 |
" '}',\n" + |
|
358 |
" \"digital signature\": '{'\n" + |
|
359 |
" \"signature algorithm\": \"{2}\"\n" + |
|
360 |
" \"signature\": '{'\n" + |
|
361 |
"{3}\n" + |
|
362 |
" '}',\n" + |
|
363 |
" '}'\n" + |
|
364 |
"'}'", |
|
365 |
Locale.ENGLISH); |
|
366 |
||
367 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
368 |
Object[] messageFields = { |
|
369 |
namedGroup.name, |
|
370 |
Utilities.indent( |
|
371 |
hexEncoder.encodeBuffer(publicPoint), " "), |
|
372 |
signatureScheme.name, |
|
373 |
Utilities.indent( |
|
374 |
hexEncoder.encodeBuffer(paramsSignature), " ") |
|
375 |
}; |
|
376 |
return messageFormat.format(messageFields); |
|
377 |
} else if (paramsSignature != null) { |
|
378 |
MessageFormat messageFormat = new MessageFormat( |
|
379 |
"\"ECDH ServerKeyExchange\": '{'\n" + |
|
380 |
" \"parameters\": '{'\n" + |
|
381 |
" \"named group\": \"{0}\"\n" + |
|
382 |
" \"ecdh public\": '{'\n" + |
|
383 |
"{1}\n" + |
|
384 |
" '}',\n" + |
|
385 |
" '}',\n" + |
|
386 |
" \"signature\": '{'\n" + |
|
387 |
"{2}\n" + |
|
388 |
" '}'\n" + |
|
389 |
"'}'", |
|
390 |
Locale.ENGLISH); |
|
391 |
||
392 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
393 |
Object[] messageFields = { |
|
394 |
namedGroup.name, |
|
395 |
Utilities.indent( |
|
396 |
hexEncoder.encodeBuffer(publicPoint), " "), |
|
397 |
Utilities.indent( |
|
398 |
hexEncoder.encodeBuffer(paramsSignature), " ") |
|
399 |
}; |
|
400 |
||
401 |
return messageFormat.format(messageFields); |
|
402 |
} else { // anonymous |
|
403 |
MessageFormat messageFormat = new MessageFormat( |
|
404 |
"\"ECDH ServerKeyExchange\": '{'\n" + |
|
405 |
" \"parameters\": '{'\n" + |
|
406 |
" \"named group\": \"{0}\"\n" + |
|
407 |
" \"ecdh public\": '{'\n" + |
|
408 |
"{1}\n" + |
|
409 |
" '}',\n" + |
|
410 |
" '}'\n" + |
|
411 |
"'}'", |
|
412 |
Locale.ENGLISH); |
|
413 |
||
414 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
415 |
Object[] messageFields = { |
|
416 |
namedGroup.name, |
|
417 |
Utilities.indent( |
|
418 |
hexEncoder.encodeBuffer(publicPoint), " "), |
|
419 |
}; |
|
420 |
||
421 |
return messageFormat.format(messageFields); |
|
422 |
} |
|
423 |
} |
|
424 |
||
425 |
private static Signature getSignature(String keyAlgorithm, |
|
426 |
Key key) throws NoSuchAlgorithmException, InvalidKeyException { |
|
427 |
Signature signer = null; |
|
428 |
switch (keyAlgorithm) { |
|
429 |
case "EC": |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
430 |
signer = Signature.getInstance(JsseJce.SIGNATURE_ECDSA); |
50768 | 431 |
break; |
432 |
case "RSA": |
|
433 |
signer = RSASignature.getInstance(); |
|
434 |
break; |
|
435 |
default: |
|
436 |
throw new NoSuchAlgorithmException( |
|
437 |
"neither an RSA or a EC key : " + keyAlgorithm); |
|
438 |
} |
|
439 |
||
440 |
if (signer != null) { |
|
441 |
if (key instanceof PublicKey) { |
|
442 |
signer.initVerify((PublicKey)(key)); |
|
443 |
} else { |
|
444 |
signer.initSign((PrivateKey)key); |
|
445 |
} |
|
446 |
} |
|
447 |
||
448 |
return signer; |
|
449 |
} |
|
450 |
||
451 |
private static void updateSignature(Signature sig, |
|
452 |
byte[] clntNonce, byte[] svrNonce, int namedGroupId, |
|
453 |
byte[] publicPoint) throws SignatureException { |
|
454 |
sig.update(clntNonce); |
|
455 |
sig.update(svrNonce); |
|
456 |
||
457 |
sig.update(CURVE_NAMED_CURVE); |
|
458 |
sig.update((byte)((namedGroupId >> 8) & 0xFF)); |
|
459 |
sig.update((byte)(namedGroupId & 0xFF)); |
|
460 |
sig.update((byte)publicPoint.length); |
|
461 |
sig.update(publicPoint); |
|
462 |
} |
|
463 |
} |
|
464 |
||
465 |
/** |
|
466 |
* The ECDH "ServerKeyExchange" handshake message producer. |
|
467 |
*/ |
|
468 |
private static final |
|
469 |
class ECDHServerKeyExchangeProducer implements HandshakeProducer { |
|
470 |
// Prevent instantiation of this class. |
|
471 |
private ECDHServerKeyExchangeProducer() { |
|
472 |
// blank |
|
473 |
} |
|
474 |
||
475 |
@Override |
|
476 |
public byte[] produce(ConnectionContext context, |
|
477 |
HandshakeMessage message) throws IOException { |
|
478 |
// The producing happens in server side only. |
|
479 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
480 |
ECDHServerKeyExchangeMessage skem = |
|
481 |
new ECDHServerKeyExchangeMessage(shc); |
|
482 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
483 |
SSLLogger.fine( |
|
484 |
"Produced ECDH ServerKeyExchange handshake message", skem); |
|
485 |
} |
|
486 |
||
487 |
// Output the handshake message. |
|
488 |
skem.write(shc.handshakeOutput); |
|
489 |
shc.handshakeOutput.flush(); |
|
490 |
||
491 |
// The handshake message has been delivered. |
|
492 |
return null; |
|
493 |
} |
|
494 |
} |
|
495 |
||
496 |
/** |
|
497 |
* The ECDH "ServerKeyExchange" handshake message consumer. |
|
498 |
*/ |
|
499 |
private static final |
|
500 |
class ECDHServerKeyExchangeConsumer implements SSLConsumer { |
|
501 |
// Prevent instantiation of this class. |
|
502 |
private ECDHServerKeyExchangeConsumer() { |
|
503 |
// blank |
|
504 |
} |
|
505 |
||
506 |
@Override |
|
507 |
public void consume(ConnectionContext context, |
|
508 |
ByteBuffer message) throws IOException { |
|
509 |
// The consuming happens in client side only. |
|
510 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
511 |
||
55353 | 512 |
// AlgorithmConstraints are checked during decoding |
50768 | 513 |
ECDHServerKeyExchangeMessage skem = |
514 |
new ECDHServerKeyExchangeMessage(chc, message); |
|
515 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
516 |
SSLLogger.fine( |
|
517 |
"Consuming ECDH ServerKeyExchange handshake message", skem); |
|
518 |
} |
|
519 |
||
520 |
// |
|
521 |
// update |
|
522 |
// |
|
55353 | 523 |
chc.handshakeCredentials.add(skem.sslCredentials); |
50768 | 524 |
|
525 |
// |
|
526 |
// produce |
|
527 |
// |
|
528 |
// Need no new handshake message producers here. |
|
529 |
} |
|
530 |
} |
|
531 |
} |
|
532 |