author | xuelei |
Wed, 03 Apr 2019 16:23:22 -0700 | |
changeset 54417 | f87041131515 |
parent 53734 | cb1642ccc732 |
child 55353 | 946f7f2d321c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
2 |
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.ssl; |
|
27 |
||
28 |
import java.io.IOException; |
|
50768 | 29 |
import java.nio.ByteBuffer; |
30 |
import java.security.AlgorithmConstraints; |
|
31 |
import java.security.CryptoPrimitive; |
|
32 |
import java.security.GeneralSecurityException; |
|
33 |
import java.security.KeyFactory; |
|
2 | 34 |
import java.security.PublicKey; |
35 |
import java.security.interfaces.ECPublicKey; |
|
50768 | 36 |
import java.security.spec.ECParameterSpec; |
37 |
import java.security.spec.ECPoint; |
|
38 |
import java.security.spec.ECPublicKeySpec; |
|
39 |
import java.text.MessageFormat; |
|
40 |
import java.util.EnumSet; |
|
41 |
import java.util.Locale; |
|
42 |
import javax.crypto.SecretKey; |
|
43 |
import javax.net.ssl.SSLHandshakeException; |
|
44 |
import sun.security.ssl.ECDHKeyExchange.ECDHECredentials; |
|
45 |
import sun.security.ssl.ECDHKeyExchange.ECDHEPossession; |
|
46 |
import sun.security.ssl.SSLHandshake.HandshakeMessage; |
|
47 |
import sun.security.ssl.SupportedGroupsExtension.NamedGroup; |
|
48 |
import sun.security.ssl.X509Authentication.X509Credentials; |
|
49 |
import sun.security.ssl.X509Authentication.X509Possession; |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
50 |
import sun.security.util.ECUtil; |
50768 | 51 |
import sun.security.util.HexDumpEncoder; |
2 | 52 |
|
53 |
/** |
|
50768 | 54 |
* Pack of the "ClientKeyExchange" handshake message. |
2 | 55 |
*/ |
50768 | 56 |
final class ECDHClientKeyExchange { |
57 |
static final SSLConsumer ecdhHandshakeConsumer = |
|
58 |
new ECDHClientKeyExchangeConsumer(); |
|
59 |
static final HandshakeProducer ecdhHandshakeProducer = |
|
60 |
new ECDHClientKeyExchangeProducer(); |
|
61 |
||
62 |
static final SSLConsumer ecdheHandshakeConsumer = |
|
63 |
new ECDHEClientKeyExchangeConsumer(); |
|
64 |
static final HandshakeProducer ecdheHandshakeProducer = |
|
65 |
new ECDHEClientKeyExchangeProducer(); |
|
66 |
||
67 |
/** |
|
68 |
* The ECDH/ECDHE ClientKeyExchange handshake message. |
|
69 |
*/ |
|
70 |
private static final |
|
71 |
class ECDHClientKeyExchangeMessage extends HandshakeMessage { |
|
72 |
private final byte[] encodedPoint; |
|
73 |
||
74 |
ECDHClientKeyExchangeMessage(HandshakeContext handshakeContext, |
|
75 |
ECPublicKey publicKey) { |
|
76 |
super(handshakeContext); |
|
77 |
||
78 |
ECPoint point = publicKey.getW(); |
|
79 |
ECParameterSpec params = publicKey.getParams(); |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
80 |
encodedPoint = ECUtil.encodePoint(point, params.getCurve()); |
50768 | 81 |
} |
82 |
||
83 |
ECDHClientKeyExchangeMessage(HandshakeContext handshakeContext, |
|
84 |
ByteBuffer m) throws IOException { |
|
85 |
super(handshakeContext); |
|
86 |
if (m.remaining() != 0) { // explicit PublicValueEncoding |
|
87 |
this.encodedPoint = Record.getBytes8(m); |
|
88 |
} else { |
|
89 |
this.encodedPoint = new byte[0]; |
|
90 |
} |
|
91 |
} |
|
92 |
||
93 |
// Check constraints of the specified EC public key. |
|
94 |
static void checkConstraints(AlgorithmConstraints constraints, |
|
95 |
ECPublicKey publicKey, |
|
96 |
byte[] encodedPoint) throws SSLHandshakeException { |
|
97 |
||
98 |
try { |
|
99 |
ECParameterSpec params = publicKey.getParams(); |
|
100 |
ECPoint point = |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
101 |
ECUtil.decodePoint(encodedPoint, params.getCurve()); |
50768 | 102 |
ECPublicKeySpec spec = new ECPublicKeySpec(point, params); |
103 |
||
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
104 |
KeyFactory kf = KeyFactory.getInstance("EC"); |
50768 | 105 |
ECPublicKey peerPublicKey = |
106 |
(ECPublicKey)kf.generatePublic(spec); |
|
2 | 107 |
|
50768 | 108 |
// check constraints of ECPublicKey |
109 |
if (!constraints.permits( |
|
110 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), |
|
111 |
peerPublicKey)) { |
|
112 |
throw new SSLHandshakeException( |
|
113 |
"ECPublicKey does not comply to algorithm constraints"); |
|
114 |
} |
|
115 |
} catch (GeneralSecurityException | java.io.IOException e) { |
|
116 |
throw (SSLHandshakeException) new SSLHandshakeException( |
|
117 |
"Could not generate ECPublicKey").initCause(e); |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
@Override |
|
122 |
public SSLHandshake handshakeType() { |
|
123 |
return SSLHandshake.CLIENT_KEY_EXCHANGE; |
|
124 |
} |
|
125 |
||
126 |
@Override |
|
127 |
public int messageLength() { |
|
128 |
if (encodedPoint == null || encodedPoint.length == 0) { |
|
129 |
return 0; |
|
130 |
} else { |
|
131 |
return 1 + encodedPoint.length; |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
@Override |
|
136 |
public void send(HandshakeOutStream hos) throws IOException { |
|
137 |
if (encodedPoint != null && encodedPoint.length != 0) { |
|
138 |
hos.putBytes8(encodedPoint); |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
@Override |
|
143 |
public String toString() { |
|
144 |
MessageFormat messageFormat = new MessageFormat( |
|
145 |
"\"ECDH ClientKeyExchange\": '{'\n" + |
|
146 |
" \"ecdh public\": '{'\n" + |
|
147 |
"{0}\n" + |
|
148 |
" '}',\n" + |
|
149 |
"'}'", |
|
150 |
Locale.ENGLISH); |
|
151 |
if (encodedPoint == null || encodedPoint.length == 0) { |
|
152 |
Object[] messageFields = { |
|
153 |
" <implicit>" |
|
154 |
}; |
|
155 |
return messageFormat.format(messageFields); |
|
156 |
} else { |
|
157 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
158 |
Object[] messageFields = { |
|
159 |
Utilities.indent( |
|
160 |
hexEncoder.encodeBuffer(encodedPoint), " "), |
|
161 |
}; |
|
162 |
return messageFormat.format(messageFields); |
|
163 |
} |
|
164 |
} |
|
2 | 165 |
} |
166 |
||
50768 | 167 |
/** |
168 |
* The ECDH "ClientKeyExchange" handshake message producer. |
|
169 |
*/ |
|
170 |
private static final |
|
171 |
class ECDHClientKeyExchangeProducer implements HandshakeProducer { |
|
172 |
// Prevent instantiation of this class. |
|
173 |
private ECDHClientKeyExchangeProducer() { |
|
174 |
// blank |
|
175 |
} |
|
176 |
||
177 |
@Override |
|
178 |
public byte[] produce(ConnectionContext context, |
|
179 |
HandshakeMessage message) throws IOException { |
|
180 |
// The producing happens in client side only. |
|
181 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
182 |
||
183 |
X509Credentials x509Credentials = null; |
|
184 |
for (SSLCredentials credential : chc.handshakeCredentials) { |
|
185 |
if (credential instanceof X509Credentials) { |
|
186 |
x509Credentials = (X509Credentials)credential; |
|
187 |
break; |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
if (x509Credentials == null) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
192 |
throw chc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 193 |
"No server certificate for ECDH client key exchange"); |
194 |
} |
|
195 |
||
196 |
PublicKey publicKey = x509Credentials.popPublicKey; |
|
197 |
if (!publicKey.getAlgorithm().equals("EC")) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
198 |
throw chc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
50768 | 199 |
"Not EC server certificate for ECDH client key exchange"); |
200 |
} |
|
201 |
||
202 |
ECParameterSpec params = ((ECPublicKey)publicKey).getParams(); |
|
203 |
NamedGroup namedGroup = NamedGroup.valueOf(params); |
|
204 |
if (namedGroup == null) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
205 |
throw chc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
50768 | 206 |
"Unsupported EC server cert for ECDH client key exchange"); |
207 |
} |
|
2 | 208 |
|
50768 | 209 |
ECDHEPossession ecdhePossession = new ECDHEPossession( |
210 |
namedGroup, chc.sslContext.getSecureRandom()); |
|
211 |
chc.handshakePossessions.add(ecdhePossession); |
|
212 |
ECDHClientKeyExchangeMessage cke = |
|
213 |
new ECDHClientKeyExchangeMessage( |
|
214 |
chc, ecdhePossession.publicKey); |
|
215 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
216 |
SSLLogger.fine( |
|
217 |
"Produced ECDH ClientKeyExchange handshake message", cke); |
|
218 |
} |
|
219 |
||
220 |
// Output the handshake message. |
|
221 |
cke.write(chc.handshakeOutput); |
|
222 |
chc.handshakeOutput.flush(); |
|
223 |
||
224 |
// update the states |
|
225 |
SSLKeyExchange ke = SSLKeyExchange.valueOf( |
|
226 |
chc.negotiatedCipherSuite.keyExchange, |
|
227 |
chc.negotiatedProtocol); |
|
228 |
if (ke == null) { |
|
229 |
// unlikely |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
230 |
throw chc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 231 |
"Not supported key exchange type"); |
232 |
} else { |
|
233 |
SSLKeyDerivation masterKD = ke.createKeyDerivation(chc); |
|
234 |
SecretKey masterSecret = |
|
235 |
masterKD.deriveKey("MasterSecret", null); |
|
236 |
chc.handshakeSession.setMasterSecret(masterSecret); |
|
237 |
||
238 |
SSLTrafficKeyDerivation kd = |
|
239 |
SSLTrafficKeyDerivation.valueOf(chc.negotiatedProtocol); |
|
240 |
if (kd == null) { |
|
241 |
// unlikely |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
242 |
throw chc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 243 |
"Not supported key derivation: " + |
244 |
chc.negotiatedProtocol); |
|
245 |
} else { |
|
246 |
chc.handshakeKeyDerivation = |
|
247 |
kd.createKeyDerivation(chc, masterSecret); |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
// The handshake message has been delivered. |
|
252 |
return null; |
|
253 |
} |
|
2 | 254 |
} |
255 |
||
50768 | 256 |
/** |
257 |
* The ECDH "ClientKeyExchange" handshake message consumer. |
|
258 |
*/ |
|
259 |
private static final |
|
260 |
class ECDHClientKeyExchangeConsumer implements SSLConsumer { |
|
261 |
// Prevent instantiation of this class. |
|
262 |
private ECDHClientKeyExchangeConsumer() { |
|
263 |
// blank |
|
264 |
} |
|
265 |
||
266 |
@Override |
|
267 |
public void consume(ConnectionContext context, |
|
268 |
ByteBuffer message) throws IOException { |
|
269 |
// The consuming happens in server side only. |
|
270 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
271 |
||
272 |
X509Possession x509Possession = null; |
|
273 |
for (SSLPossession possession : shc.handshakePossessions) { |
|
274 |
if (possession instanceof X509Possession) { |
|
275 |
x509Possession = (X509Possession)possession; |
|
276 |
break; |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
if (x509Possession == null) { |
|
281 |
// unlikely, have been checked during cipher suite negotiation. |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
282 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 283 |
"No expected EC server cert for ECDH client key exchange"); |
284 |
} |
|
285 |
||
54417
f87041131515
8217610: TLSv1.3 fail with ClassException when EC keys are stored in PKCS11
xuelei
parents:
53734
diff
changeset
|
286 |
ECParameterSpec params = x509Possession.getECParameterSpec(); |
f87041131515
8217610: TLSv1.3 fail with ClassException when EC keys are stored in PKCS11
xuelei
parents:
53734
diff
changeset
|
287 |
if (params == null) { |
50768 | 288 |
// unlikely, have been checked during cipher suite negotiation. |
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
289 |
throw shc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
50768 | 290 |
"Not EC server cert for ECDH client key exchange"); |
291 |
} |
|
292 |
||
293 |
NamedGroup namedGroup = NamedGroup.valueOf(params); |
|
294 |
if (namedGroup == null) { |
|
295 |
// unlikely, have been checked during cipher suite negotiation. |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
296 |
throw shc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
50768 | 297 |
"Unsupported EC server cert for ECDH client key exchange"); |
298 |
} |
|
2 | 299 |
|
50768 | 300 |
SSLKeyExchange ke = SSLKeyExchange.valueOf( |
301 |
shc.negotiatedCipherSuite.keyExchange, |
|
302 |
shc.negotiatedProtocol); |
|
303 |
if (ke == null) { |
|
304 |
// unlikely |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
305 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 306 |
"Not supported key exchange type"); |
307 |
} |
|
308 |
||
309 |
// parse the handshake message |
|
310 |
ECDHClientKeyExchangeMessage cke = |
|
311 |
new ECDHClientKeyExchangeMessage(shc, message); |
|
312 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
313 |
SSLLogger.fine( |
|
314 |
"Consuming ECDH ClientKeyExchange handshake message", cke); |
|
315 |
} |
|
316 |
||
317 |
// create the credentials |
|
318 |
try { |
|
319 |
ECPoint point = |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
320 |
ECUtil.decodePoint(cke.encodedPoint, params.getCurve()); |
50768 | 321 |
ECPublicKeySpec spec = new ECPublicKeySpec(point, params); |
322 |
||
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
323 |
KeyFactory kf = KeyFactory.getInstance("EC"); |
50768 | 324 |
ECPublicKey peerPublicKey = |
325 |
(ECPublicKey)kf.generatePublic(spec); |
|
326 |
||
327 |
// check constraints of peer ECPublicKey |
|
328 |
if (!shc.algorithmConstraints.permits( |
|
329 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), |
|
330 |
peerPublicKey)) { |
|
331 |
throw new SSLHandshakeException( |
|
332 |
"ECPublicKey does not comply to algorithm constraints"); |
|
333 |
} |
|
334 |
||
335 |
shc.handshakeCredentials.add(new ECDHECredentials( |
|
336 |
peerPublicKey, namedGroup)); |
|
337 |
} catch (GeneralSecurityException | java.io.IOException e) { |
|
338 |
throw (SSLHandshakeException)(new SSLHandshakeException( |
|
339 |
"Could not generate ECPublicKey").initCause(e)); |
|
340 |
} |
|
341 |
||
342 |
// update the states |
|
343 |
SSLKeyDerivation masterKD = ke.createKeyDerivation(shc); |
|
344 |
SecretKey masterSecret = |
|
345 |
masterKD.deriveKey("MasterSecret", null); |
|
346 |
shc.handshakeSession.setMasterSecret(masterSecret); |
|
347 |
||
348 |
SSLTrafficKeyDerivation kd = |
|
349 |
SSLTrafficKeyDerivation.valueOf(shc.negotiatedProtocol); |
|
350 |
if (kd == null) { |
|
351 |
// unlikely |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
352 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 353 |
"Not supported key derivation: " + shc.negotiatedProtocol); |
354 |
} else { |
|
355 |
shc.handshakeKeyDerivation = |
|
356 |
kd.createKeyDerivation(shc, masterSecret); |
|
357 |
} |
|
358 |
} |
|
2 | 359 |
} |
360 |
||
50768 | 361 |
/** |
362 |
* The ECDHE "ClientKeyExchange" handshake message producer. |
|
363 |
*/ |
|
364 |
private static final |
|
365 |
class ECDHEClientKeyExchangeProducer implements HandshakeProducer { |
|
366 |
// Prevent instantiation of this class. |
|
367 |
private ECDHEClientKeyExchangeProducer() { |
|
368 |
// blank |
|
369 |
} |
|
370 |
||
371 |
@Override |
|
372 |
public byte[] produce(ConnectionContext context, |
|
373 |
HandshakeMessage message) throws IOException { |
|
374 |
// The producing happens in client side only. |
|
375 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
376 |
||
377 |
ECDHECredentials ecdheCredentials = null; |
|
378 |
for (SSLCredentials cd : chc.handshakeCredentials) { |
|
379 |
if (cd instanceof ECDHECredentials) { |
|
380 |
ecdheCredentials = (ECDHECredentials)cd; |
|
381 |
break; |
|
382 |
} |
|
383 |
} |
|
384 |
||
385 |
if (ecdheCredentials == null) { |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
386 |
throw chc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 387 |
"No ECDHE credentials negotiated for client key exchange"); |
388 |
} |
|
389 |
||
390 |
ECDHEPossession ecdhePossession = new ECDHEPossession( |
|
391 |
ecdheCredentials, chc.sslContext.getSecureRandom()); |
|
392 |
chc.handshakePossessions.add(ecdhePossession); |
|
393 |
ECDHClientKeyExchangeMessage cke = |
|
394 |
new ECDHClientKeyExchangeMessage( |
|
395 |
chc, ecdhePossession.publicKey); |
|
396 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
397 |
SSLLogger.fine( |
|
398 |
"Produced ECDHE ClientKeyExchange handshake message", cke); |
|
399 |
} |
|
400 |
||
401 |
// Output the handshake message. |
|
402 |
cke.write(chc.handshakeOutput); |
|
403 |
chc.handshakeOutput.flush(); |
|
404 |
||
405 |
// update the states |
|
406 |
SSLKeyExchange ke = SSLKeyExchange.valueOf( |
|
407 |
chc.negotiatedCipherSuite.keyExchange, |
|
408 |
chc.negotiatedProtocol); |
|
409 |
if (ke == null) { |
|
410 |
// unlikely |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
411 |
throw chc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 412 |
"Not supported key exchange type"); |
413 |
} else { |
|
414 |
SSLKeyDerivation masterKD = ke.createKeyDerivation(chc); |
|
415 |
SecretKey masterSecret = |
|
416 |
masterKD.deriveKey("MasterSecret", null); |
|
417 |
chc.handshakeSession.setMasterSecret(masterSecret); |
|
418 |
||
419 |
SSLTrafficKeyDerivation kd = |
|
420 |
SSLTrafficKeyDerivation.valueOf(chc.negotiatedProtocol); |
|
421 |
if (kd == null) { |
|
422 |
// unlikely |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
423 |
throw chc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 424 |
"Not supported key derivation: " + |
425 |
chc.negotiatedProtocol); |
|
426 |
} else { |
|
427 |
chc.handshakeKeyDerivation = |
|
428 |
kd.createKeyDerivation(chc, masterSecret); |
|
429 |
} |
|
430 |
} |
|
431 |
||
432 |
// The handshake message has been delivered. |
|
433 |
return null; |
|
434 |
} |
|
2 | 435 |
} |
436 |
||
50768 | 437 |
/** |
438 |
* The ECDHE "ClientKeyExchange" handshake message consumer. |
|
439 |
*/ |
|
440 |
private static final |
|
441 |
class ECDHEClientKeyExchangeConsumer implements SSLConsumer { |
|
442 |
// Prevent instantiation of this class. |
|
443 |
private ECDHEClientKeyExchangeConsumer() { |
|
444 |
// blank |
|
445 |
} |
|
446 |
||
447 |
@Override |
|
448 |
public void consume(ConnectionContext context, |
|
449 |
ByteBuffer message) throws IOException { |
|
450 |
// The consuming happens in server side only. |
|
451 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
452 |
||
453 |
ECDHEPossession ecdhePossession = null; |
|
454 |
for (SSLPossession possession : shc.handshakePossessions) { |
|
455 |
if (possession instanceof ECDHEPossession) { |
|
456 |
ecdhePossession = (ECDHEPossession)possession; |
|
457 |
break; |
|
458 |
} |
|
459 |
} |
|
460 |
if (ecdhePossession == null) { |
|
461 |
// unlikely |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
462 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 463 |
"No expected ECDHE possessions for client key exchange"); |
464 |
} |
|
465 |
||
466 |
ECParameterSpec params = ecdhePossession.publicKey.getParams(); |
|
467 |
NamedGroup namedGroup = NamedGroup.valueOf(params); |
|
468 |
if (namedGroup == null) { |
|
469 |
// unlikely, have been checked during cipher suite negotiation. |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
470 |
throw shc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
50768 | 471 |
"Unsupported EC server cert for ECDHE client key exchange"); |
472 |
} |
|
473 |
||
474 |
SSLKeyExchange ke = SSLKeyExchange.valueOf( |
|
475 |
shc.negotiatedCipherSuite.keyExchange, |
|
476 |
shc.negotiatedProtocol); |
|
477 |
if (ke == null) { |
|
478 |
// unlikely |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
479 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 480 |
"Not supported key exchange type"); |
481 |
} |
|
2 | 482 |
|
50768 | 483 |
// parse the handshake message |
484 |
ECDHClientKeyExchangeMessage cke = |
|
485 |
new ECDHClientKeyExchangeMessage(shc, message); |
|
486 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
487 |
SSLLogger.fine( |
|
488 |
"Consuming ECDHE ClientKeyExchange handshake message", cke); |
|
489 |
} |
|
490 |
||
491 |
// create the credentials |
|
492 |
try { |
|
493 |
ECPoint point = |
|
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
494 |
ECUtil.decodePoint(cke.encodedPoint, params.getCurve()); |
50768 | 495 |
ECPublicKeySpec spec = new ECPublicKeySpec(point, params); |
496 |
||
53734
cb1642ccc732
8217835: Remove the experimental SunJSSE FIPS compliant mode
xuelei
parents:
53064
diff
changeset
|
497 |
KeyFactory kf = KeyFactory.getInstance("EC"); |
50768 | 498 |
ECPublicKey peerPublicKey = |
499 |
(ECPublicKey)kf.generatePublic(spec); |
|
2 | 500 |
|
50768 | 501 |
// check constraints of peer ECPublicKey |
502 |
if (!shc.algorithmConstraints.permits( |
|
503 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), |
|
504 |
peerPublicKey)) { |
|
505 |
throw new SSLHandshakeException( |
|
506 |
"ECPublicKey does not comply to algorithm constraints"); |
|
507 |
} |
|
508 |
||
509 |
shc.handshakeCredentials.add(new ECDHECredentials( |
|
510 |
peerPublicKey, namedGroup)); |
|
511 |
} catch (GeneralSecurityException | java.io.IOException e) { |
|
512 |
throw (SSLHandshakeException)(new SSLHandshakeException( |
|
513 |
"Could not generate ECPublicKey").initCause(e)); |
|
514 |
} |
|
515 |
||
516 |
// update the states |
|
517 |
SSLKeyDerivation masterKD = ke.createKeyDerivation(shc); |
|
518 |
SecretKey masterSecret = |
|
519 |
masterKD.deriveKey("MasterSecret", null); |
|
520 |
shc.handshakeSession.setMasterSecret(masterSecret); |
|
521 |
||
522 |
SSLTrafficKeyDerivation kd = |
|
523 |
SSLTrafficKeyDerivation.valueOf(shc.negotiatedProtocol); |
|
524 |
if (kd == null) { |
|
525 |
// unlikely |
|
53064
103ed9569fc8
8215443: The use of TransportContext.fatal() leads to bad coding style
xuelei
parents:
50768
diff
changeset
|
526 |
throw shc.conContext.fatal(Alert.INTERNAL_ERROR, |
50768 | 527 |
"Not supported key derivation: " + shc.negotiatedProtocol); |
528 |
} else { |
|
529 |
shc.handshakeKeyDerivation = |
|
530 |
kd.createKeyDerivation(shc, masterSecret); |
|
531 |
} |
|
2 | 532 |
} |
533 |
} |
|
534 |
} |