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.math.BigInteger; |
|
30 |
import java.nio.ByteBuffer; |
|
31 |
import java.security.CryptoPrimitive; |
|
32 |
import java.security.GeneralSecurityException; |
|
33 |
import java.security.InvalidAlgorithmParameterException; |
|
34 |
import java.security.InvalidKeyException; |
|
35 |
import java.security.Key; |
|
36 |
import java.security.KeyFactory; |
|
37 |
import java.security.NoSuchAlgorithmException; |
|
38 |
import java.security.PrivateKey; |
|
39 |
import java.security.PublicKey; |
|
40 |
import java.security.Signature; |
|
41 |
import java.security.SignatureException; |
|
42 |
import java.text.MessageFormat; |
|
43 |
import java.util.EnumSet; |
|
44 |
import java.util.Locale; |
|
45 |
import javax.crypto.interfaces.DHPublicKey; |
|
46 |
import javax.crypto.spec.DHParameterSpec; |
|
47 |
import javax.crypto.spec.DHPublicKeySpec; |
|
48 |
import sun.security.ssl.DHKeyExchange.DHECredentials; |
|
49 |
import sun.security.ssl.DHKeyExchange.DHEPossession; |
|
50 |
import sun.security.ssl.SSLHandshake.HandshakeMessage; |
|
51 |
import sun.security.ssl.X509Authentication.X509Credentials; |
|
52 |
import sun.security.ssl.X509Authentication.X509Possession; |
|
53 |
import sun.security.util.HexDumpEncoder; |
|
54 |
import sun.security.util.KeyUtil; |
|
55 |
||
56 |
/** |
|
57 |
* Pack of the ServerKeyExchange handshake message. |
|
58 |
*/ |
|
59 |
final class DHServerKeyExchange { |
|
60 |
static final SSLConsumer dhHandshakeConsumer = |
|
61 |
new DHServerKeyExchangeConsumer(); |
|
62 |
static final HandshakeProducer dhHandshakeProducer = |
|
63 |
new DHServerKeyExchangeProducer(); |
|
64 |
||
65 |
/** |
|
66 |
* The DiffieHellman ServerKeyExchange handshake message. |
|
67 |
*/ |
|
68 |
private static final |
|
69 |
class DHServerKeyExchangeMessage extends HandshakeMessage { |
|
70 |
// public key encapsulated in this message |
|
71 |
private final byte[] p; // 1 to 2^16 - 1 bytes |
|
72 |
private final byte[] g; // 1 to 2^16 - 1 bytes |
|
73 |
private final byte[] y; // 1 to 2^16 - 1 bytes |
|
74 |
||
75 |
// the signature algorithm used by this ServerKeyExchange message |
|
76 |
private final boolean useExplicitSigAlgorithm; |
|
77 |
private final SignatureScheme signatureScheme; |
|
78 |
||
79 |
// signature bytes, or null if anonymous |
|
80 |
private final byte[] paramsSignature; |
|
81 |
||
82 |
DHServerKeyExchangeMessage( |
|
83 |
HandshakeContext handshakeContext) throws IOException { |
|
84 |
super(handshakeContext); |
|
85 |
||
86 |
// This happens in server side only. |
|
87 |
ServerHandshakeContext shc = |
|
88 |
(ServerHandshakeContext)handshakeContext; |
|
89 |
||
90 |
DHEPossession dhePossession = null; |
|
91 |
X509Possession x509Possession = null; |
|
92 |
for (SSLPossession possession : shc.handshakePossessions) { |
|
93 |
if (possession instanceof DHEPossession) { |
|
94 |
dhePossession = (DHEPossession)possession; |
|
95 |
if (x509Possession != null) { |
|
96 |
break; |
|
97 |
} |
|
98 |
} else if (possession instanceof X509Possession) { |
|
99 |
x509Possession = (X509Possession)possession; |
|
100 |
if (dhePossession != null) { |
|
101 |
break; |
|
102 |
} |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
if (dhePossession == null) { |
|
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 DHE credentials negotiated for server key exchange"); |
110 |
} |
|
111 |
DHPublicKey publicKey = dhePossession.publicKey; |
|
112 |
DHParameterSpec params = publicKey.getParams(); |
|
113 |
this.p = Utilities.toByteArray(params.getP()); |
|
114 |
this.g = Utilities.toByteArray(params.getG()); |
|
115 |
this.y = Utilities.toByteArray(publicKey.getY()); |
|
116 |
||
117 |
if (x509Possession == null) { |
|
118 |
// anonymous, no authentication, no signature |
|
119 |
paramsSignature = null; |
|
120 |
signatureScheme = null; |
|
121 |
useExplicitSigAlgorithm = false; |
|
122 |
} else { |
|
123 |
useExplicitSigAlgorithm = |
|
124 |
shc.negotiatedProtocol.useTLS12PlusSpec(); |
|
125 |
Signature signer = null; |
|
126 |
if (useExplicitSigAlgorithm) { |
|
127 |
signatureScheme = SignatureScheme.getPreferableAlgorithm( |
|
57718
a93b7b28f644
8226374: Restrict TLS signature schemes and named groups
xuelei
parents:
55353
diff
changeset
|
128 |
shc.algorithmConstraints, |
50768 | 129 |
shc.peerRequestedSignatureSchemes, |
54417
f87041131515
8217610: TLSv1.3 fail with ClassException when EC keys are stored in PKCS11
xuelei
parents:
53734
diff
changeset
|
130 |
x509Possession, |
50768 | 131 |
shc.negotiatedProtocol); |
132 |
if (signatureScheme == null) { |
|
133 |
// Unlikely, the credentials generator should have |
|
134 |
// selected the preferable signature algorithm properly. |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
135 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 136 |
"No preferred signature algorithm"); |
137 |
} |
|
138 |
try { |
|
139 |
signer = signatureScheme.getSignature( |
|
140 |
x509Possession.popPrivateKey); |
|
141 |
} catch (NoSuchAlgorithmException | InvalidKeyException | |
|
142 |
InvalidAlgorithmParameterException nsae) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
143 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 144 |
"Unsupported signature algorithm: " + |
145 |
signatureScheme.name, nsae); |
|
146 |
} |
|
147 |
} else { |
|
148 |
signatureScheme = null; |
|
149 |
try { |
|
150 |
signer = getSignature( |
|
151 |
x509Possession.popPrivateKey.getAlgorithm(), |
|
152 |
x509Possession.popPrivateKey); |
|
153 |
} catch (NoSuchAlgorithmException | InvalidKeyException e) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
154 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 155 |
"Unsupported signature algorithm: " + |
156 |
x509Possession.popPrivateKey.getAlgorithm(), e); |
|
157 |
} |
|
158 |
} |
|
159 |
||
160 |
byte[] signature = null; |
|
161 |
try { |
|
162 |
updateSignature(signer, shc.clientHelloRandom.randomBytes, |
|
163 |
shc.serverHelloRandom.randomBytes); |
|
164 |
signature = signer.sign(); |
|
165 |
} catch (SignatureException ex) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
166 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 167 |
"Failed to sign dhe parameters: " + |
168 |
x509Possession.popPrivateKey.getAlgorithm(), ex); |
|
169 |
} |
|
170 |
paramsSignature = signature; |
|
171 |
} |
|
172 |
} |
|
173 |
||
174 |
DHServerKeyExchangeMessage(HandshakeContext handshakeContext, |
|
175 |
ByteBuffer m) throws IOException { |
|
176 |
super(handshakeContext); |
|
177 |
||
178 |
// This happens in client side only. |
|
179 |
ClientHandshakeContext chc = |
|
180 |
(ClientHandshakeContext)handshakeContext; |
|
181 |
||
182 |
this.p = Record.getBytes16(m); |
|
183 |
this.g = Record.getBytes16(m); |
|
184 |
this.y = Record.getBytes16(m); |
|
185 |
||
186 |
try { |
|
187 |
KeyUtil.validate(new DHPublicKeySpec( |
|
188 |
new BigInteger(1, y), |
|
189 |
new BigInteger(1, p), |
|
190 |
new BigInteger(1, p))); |
|
191 |
} catch (InvalidKeyException ike) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
192 |
throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 193 |
"Invalid DH ServerKeyExchange: invalid parameters", ike); |
194 |
} |
|
195 |
||
196 |
X509Credentials x509Credentials = null; |
|
197 |
for (SSLCredentials cd : chc.handshakeCredentials) { |
|
198 |
if (cd instanceof X509Credentials) { |
|
199 |
x509Credentials = (X509Credentials)cd; |
|
200 |
break; |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
if (x509Credentials == null) { |
|
205 |
// anonymous, no authentication, no signature |
|
206 |
if (m.hasRemaining()) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
207 |
throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 208 |
"Invalid DH ServerKeyExchange: unknown extra data"); |
209 |
} |
|
210 |
||
211 |
this.signatureScheme = null; |
|
212 |
this.paramsSignature = null; |
|
213 |
this.useExplicitSigAlgorithm = false; |
|
214 |
||
215 |
return; |
|
216 |
} |
|
217 |
||
218 |
this.useExplicitSigAlgorithm = |
|
219 |
chc.negotiatedProtocol.useTLS12PlusSpec(); |
|
220 |
if (useExplicitSigAlgorithm) { |
|
221 |
int ssid = Record.getInt16(m); |
|
222 |
signatureScheme = SignatureScheme.valueOf(ssid); |
|
223 |
if (signatureScheme == null) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
224 |
throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 225 |
"Invalid signature algorithm (" + ssid + |
226 |
") used in DH ServerKeyExchange handshake message"); |
|
227 |
} |
|
228 |
||
229 |
if (!chc.localSupportedSignAlgs.contains(signatureScheme)) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
230 |
throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 231 |
"Unsupported signature algorithm (" + |
232 |
signatureScheme.name + |
|
233 |
") used in DH ServerKeyExchange handshake message"); |
|
234 |
} |
|
235 |
} else { |
|
236 |
this.signatureScheme = null; |
|
237 |
} |
|
238 |
||
239 |
// read and verify the signature |
|
240 |
this.paramsSignature = Record.getBytes16(m); |
|
241 |
Signature signer; |
|
242 |
if (useExplicitSigAlgorithm) { |
|
243 |
try { |
|
244 |
signer = signatureScheme.getSignature( |
|
245 |
x509Credentials.popPublicKey); |
|
246 |
} catch (NoSuchAlgorithmException | InvalidKeyException | |
|
247 |
InvalidAlgorithmParameterException nsae) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
248 |
throw chc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 249 |
"Unsupported signature algorithm: " + |
250 |
signatureScheme.name, nsae); |
|
251 |
} |
|
252 |
} else { |
|
253 |
try { |
|
254 |
signer = getSignature( |
|
255 |
x509Credentials.popPublicKey.getAlgorithm(), |
|
256 |
x509Credentials.popPublicKey); |
|
257 |
} catch (NoSuchAlgorithmException | InvalidKeyException e) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
258 |
throw chc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 259 |
"Unsupported signature algorithm: " + |
260 |
x509Credentials.popPublicKey.getAlgorithm(), e); |
|
261 |
} |
|
262 |
} |
|
263 |
||
264 |
try { |
|
265 |
updateSignature(signer, |
|
266 |
chc.clientHelloRandom.randomBytes, |
|
267 |
chc.serverHelloRandom.randomBytes); |
|
268 |
||
269 |
if (!signer.verify(paramsSignature)) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
270 |
throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 271 |
"Invalid signature on DH ServerKeyExchange message"); |
272 |
} |
|
273 |
} catch (SignatureException ex) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
274 |
throw chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
50768 | 275 |
"Cannot verify DH ServerKeyExchange signature", ex); |
276 |
} |
|
277 |
} |
|
278 |
||
279 |
@Override |
|
280 |
public SSLHandshake handshakeType() { |
|
281 |
return SSLHandshake.SERVER_KEY_EXCHANGE; |
|
282 |
} |
|
283 |
||
284 |
@Override |
|
285 |
public int messageLength() { |
|
286 |
int sigLen = 0; |
|
287 |
if (paramsSignature != null) { |
|
288 |
sigLen = 2 + paramsSignature.length; |
|
289 |
if (useExplicitSigAlgorithm) { |
|
290 |
sigLen += SignatureScheme.sizeInRecord(); |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
return 6 + p.length + g.length + y.length + sigLen; |
|
295 |
// 6: overhead for p, g, y values |
|
296 |
} |
|
297 |
||
298 |
@Override |
|
299 |
public void send(HandshakeOutStream hos) throws IOException { |
|
300 |
hos.putBytes16(p); |
|
301 |
hos.putBytes16(g); |
|
302 |
hos.putBytes16(y); |
|
303 |
||
304 |
if (paramsSignature != null) { |
|
305 |
if (useExplicitSigAlgorithm) { |
|
306 |
hos.putInt16(signatureScheme.id); |
|
307 |
} |
|
308 |
||
309 |
hos.putBytes16(paramsSignature); |
|
310 |
} |
|
311 |
} |
|
312 |
||
313 |
@Override |
|
314 |
public String toString() { |
|
315 |
if (paramsSignature == null) { // anonymous |
|
316 |
MessageFormat messageFormat = new MessageFormat( |
|
317 |
"\"DH ServerKeyExchange\": '{'\n" + |
|
318 |
" \"parameters\": '{'\n" + |
|
319 |
" \"dh_p\": '{'\n" + |
|
320 |
"{0}\n" + |
|
321 |
" '}',\n" + |
|
322 |
" \"dh_g\": '{'\n" + |
|
323 |
"{1}\n" + |
|
324 |
" '}',\n" + |
|
325 |
" \"dh_Ys\": '{'\n" + |
|
326 |
"{2}\n" + |
|
327 |
" '}',\n" + |
|
328 |
" '}'\n" + |
|
329 |
"'}'", |
|
330 |
Locale.ENGLISH); |
|
331 |
||
332 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
333 |
Object[] messageFields = { |
|
334 |
Utilities.indent( |
|
335 |
hexEncoder.encodeBuffer(p), " "), |
|
336 |
Utilities.indent( |
|
337 |
hexEncoder.encodeBuffer(g), " "), |
|
338 |
Utilities.indent( |
|
339 |
hexEncoder.encodeBuffer(y), " "), |
|
340 |
}; |
|
341 |
||
342 |
return messageFormat.format(messageFields); |
|
343 |
} |
|
344 |
||
345 |
if (useExplicitSigAlgorithm) { |
|
346 |
MessageFormat messageFormat = new MessageFormat( |
|
347 |
"\"DH ServerKeyExchange\": '{'\n" + |
|
348 |
" \"parameters\": '{'\n" + |
|
349 |
" \"dh_p\": '{'\n" + |
|
350 |
"{0}\n" + |
|
351 |
" '}',\n" + |
|
352 |
" \"dh_g\": '{'\n" + |
|
353 |
"{1}\n" + |
|
354 |
" '}',\n" + |
|
355 |
" \"dh_Ys\": '{'\n" + |
|
356 |
"{2}\n" + |
|
357 |
" '}',\n" + |
|
358 |
" '}',\n" + |
|
359 |
" \"digital signature\": '{'\n" + |
|
360 |
" \"signature algorithm\": \"{3}\"\n" + |
|
361 |
" \"signature\": '{'\n" + |
|
362 |
"{4}\n" + |
|
363 |
" '}',\n" + |
|
364 |
" '}'\n" + |
|
365 |
"'}'", |
|
366 |
Locale.ENGLISH); |
|
367 |
||
368 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
369 |
Object[] messageFields = { |
|
370 |
Utilities.indent( |
|
371 |
hexEncoder.encodeBuffer(p), " "), |
|
372 |
Utilities.indent( |
|
373 |
hexEncoder.encodeBuffer(g), " "), |
|
374 |
Utilities.indent( |
|
375 |
hexEncoder.encodeBuffer(y), " "), |
|
376 |
signatureScheme.name, |
|
377 |
Utilities.indent( |
|
378 |
hexEncoder.encodeBuffer(paramsSignature), " ") |
|
379 |
}; |
|
380 |
||
381 |
return messageFormat.format(messageFields); |
|
382 |
} else { |
|
383 |
MessageFormat messageFormat = new MessageFormat( |
|
384 |
"\"DH ServerKeyExchange\": '{'\n" + |
|
385 |
" \"parameters\": '{'\n" + |
|
386 |
" \"dh_p\": '{'\n" + |
|
387 |
"{0}\n" + |
|
388 |
" '}',\n" + |
|
389 |
" \"dh_g\": '{'\n" + |
|
390 |
"{1}\n" + |
|
391 |
" '}',\n" + |
|
392 |
" \"dh_Ys\": '{'\n" + |
|
393 |
"{2}\n" + |
|
394 |
" '}',\n" + |
|
395 |
" '}',\n" + |
|
396 |
" \"signature\": '{'\n" + |
|
397 |
"{3}\n" + |
|
398 |
" '}'\n" + |
|
399 |
"'}'", |
|
400 |
Locale.ENGLISH); |
|
401 |
||
402 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
403 |
Object[] messageFields = { |
|
404 |
Utilities.indent( |
|
405 |
hexEncoder.encodeBuffer(p), " "), |
|
406 |
Utilities.indent( |
|
407 |
hexEncoder.encodeBuffer(g), " "), |
|
408 |
Utilities.indent( |
|
409 |
hexEncoder.encodeBuffer(y), " "), |
|
410 |
Utilities.indent( |
|
411 |
hexEncoder.encodeBuffer(paramsSignature), " ") |
|
412 |
}; |
|
413 |
||
414 |
return messageFormat.format(messageFields); |
|
415 |
} |
|
416 |
} |
|
417 |
||
418 |
private static Signature getSignature(String keyAlgorithm, |
|
419 |
Key key) throws NoSuchAlgorithmException, InvalidKeyException { |
|
420 |
Signature signer = null; |
|
421 |
switch (keyAlgorithm) { |
|
422 |
case "DSA": |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
423 |
signer = Signature.getInstance(JsseJce.SIGNATURE_DSA); |
50768 | 424 |
break; |
425 |
case "RSA": |
|
426 |
signer = RSASignature.getInstance(); |
|
427 |
break; |
|
428 |
default: |
|
429 |
throw new NoSuchAlgorithmException( |
|
430 |
"neither an RSA or a DSA key : " + keyAlgorithm); |
|
431 |
} |
|
432 |
||
433 |
if (signer != null) { |
|
434 |
if (key instanceof PublicKey) { |
|
435 |
signer.initVerify((PublicKey)(key)); |
|
436 |
} else { |
|
437 |
signer.initSign((PrivateKey)key); |
|
438 |
} |
|
439 |
} |
|
440 |
||
441 |
return signer; |
|
442 |
} |
|
443 |
||
444 |
/* |
|
445 |
* Update sig with nonces and Diffie-Hellman public key. |
|
446 |
*/ |
|
447 |
private void updateSignature(Signature sig, byte[] clntNonce, |
|
448 |
byte[] svrNonce) throws SignatureException { |
|
449 |
int tmp; |
|
450 |
||
451 |
sig.update(clntNonce); |
|
452 |
sig.update(svrNonce); |
|
453 |
||
454 |
sig.update((byte)(p.length >> 8)); |
|
455 |
sig.update((byte)(p.length & 0x0ff)); |
|
456 |
sig.update(p); |
|
457 |
||
458 |
sig.update((byte)(g.length >> 8)); |
|
459 |
sig.update((byte)(g.length & 0x0ff)); |
|
460 |
sig.update(g); |
|
461 |
||
462 |
sig.update((byte)(y.length >> 8)); |
|
463 |
sig.update((byte)(y.length & 0x0ff)); |
|
464 |
sig.update(y); |
|
465 |
} |
|
466 |
} |
|
467 |
||
468 |
/** |
|
469 |
* The DiffieHellman "ServerKeyExchange" handshake message producer. |
|
470 |
*/ |
|
471 |
static final class DHServerKeyExchangeProducer |
|
472 |
implements HandshakeProducer { |
|
473 |
// Prevent instantiation of this class. |
|
474 |
private DHServerKeyExchangeProducer() { |
|
475 |
// blank |
|
476 |
} |
|
477 |
||
478 |
@Override |
|
479 |
public byte[] produce(ConnectionContext context, |
|
480 |
HandshakeMessage message) throws IOException { |
|
481 |
// The producing happens in server side only. |
|
482 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
483 |
DHServerKeyExchangeMessage skem = |
|
484 |
new DHServerKeyExchangeMessage(shc); |
|
485 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
486 |
SSLLogger.fine( |
|
487 |
"Produced DH ServerKeyExchange handshake message", skem); |
|
488 |
} |
|
489 |
||
490 |
// Output the handshake message. |
|
491 |
skem.write(shc.handshakeOutput); |
|
492 |
shc.handshakeOutput.flush(); |
|
493 |
||
494 |
// The handshake message has been delivered. |
|
495 |
return null; |
|
496 |
} |
|
497 |
} |
|
498 |
||
499 |
/** |
|
500 |
* The DiffieHellman "ServerKeyExchange" handshake message consumer. |
|
501 |
*/ |
|
502 |
static final class DHServerKeyExchangeConsumer implements SSLConsumer { |
|
503 |
// Prevent instantiation of this class. |
|
504 |
private DHServerKeyExchangeConsumer() { |
|
505 |
// blank |
|
506 |
} |
|
507 |
||
508 |
@Override |
|
509 |
public void consume(ConnectionContext context, |
|
510 |
ByteBuffer message) throws IOException { |
|
511 |
// The consuming happens in client side only. |
|
512 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
513 |
||
514 |
DHServerKeyExchangeMessage skem = |
|
515 |
new DHServerKeyExchangeMessage(chc, message); |
|
516 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
517 |
SSLLogger.fine( |
|
518 |
"Consuming DH ServerKeyExchange handshake message", skem); |
|
519 |
} |
|
520 |
||
521 |
// |
|
522 |
// validate |
|
523 |
// |
|
524 |
// check constraints of EC PublicKey |
|
525 |
DHPublicKey publicKey; |
|
526 |
try { |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
527 |
KeyFactory kf = KeyFactory.getInstance("DiffieHellman"); |
50768 | 528 |
DHPublicKeySpec spec = new DHPublicKeySpec( |
529 |
new BigInteger(1, skem.y), |
|
530 |
new BigInteger(1, skem.p), |
|
531 |
new BigInteger(1, skem.g)); |
|
532 |
publicKey = (DHPublicKey)kf.generatePublic(spec); |
|
533 |
} catch (GeneralSecurityException gse) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
534 |
throw chc.conContext.fatal(Alert.INSUFFICIENT_SECURITY, |
50768 | 535 |
"Could not generate DHPublicKey", gse); |
536 |
} |
|
537 |
||
538 |
if (!chc.algorithmConstraints.permits( |
|
539 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), publicKey)) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
540 |
throw chc.conContext.fatal(Alert.INSUFFICIENT_SECURITY, |
50768 | 541 |
"DH ServerKeyExchange does not comply to " + |
542 |
"algorithm constraints"); |
|
543 |
} |
|
544 |
||
545 |
// |
|
546 |
// update |
|
547 |
// |
|
548 |
NamedGroup namedGroup = NamedGroup.valueOf(publicKey.getParams()); |
|
549 |
chc.handshakeCredentials.add( |
|
550 |
new DHECredentials(publicKey, namedGroup)); |
|
551 |
||
552 |
// |
|
553 |
// produce |
|
554 |
// |
|
555 |
// Need no new handshake message producers here. |
|
556 |
} |
|
557 |
} |
|
558 |
} |
|
559 |