author | apetcher |
Thu, 16 Aug 2018 13:16:18 -0400 | |
branch | JDK-8171279-XDH-TLS-branch |
changeset 56855 | ee6aa4c74a4b |
parent 56589 | bafd8be2f970 |
child 56856 | 74041d6dcf9d |
permissions | -rw-r--r-- |
56542 | 1 |
/* |
2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
|
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; |
|
30 |
import java.security.CryptoPrimitive; |
|
31 |
import java.security.GeneralSecurityException; |
|
32 |
import java.text.MessageFormat; |
|
33 |
import java.util.Arrays; |
|
34 |
import java.util.Collections; |
|
35 |
import java.util.EnumSet; |
|
36 |
import java.util.LinkedList; |
|
37 |
import java.util.List; |
|
38 |
import java.util.Locale; |
|
39 |
import java.util.Map; |
|
40 |
import javax.net.ssl.SSLProtocolException; |
|
41 |
import sun.security.ssl.DHKeyExchange.DHECredentials; |
|
42 |
import sun.security.ssl.DHKeyExchange.DHEPossession; |
|
43 |
import sun.security.ssl.ECDHKeyExchange.ECDHECredentials; |
|
44 |
import sun.security.ssl.ECDHKeyExchange.ECDHEPossession; |
|
56589
bafd8be2f970
Initial working XDH support in TLS. I should try to refactor the code a bit.
apetcher
parents:
56542
diff
changeset
|
45 |
import sun.security.ssl.XDHKeyExchange.XDHEPossession; |
bafd8be2f970
Initial working XDH support in TLS. I should try to refactor the code a bit.
apetcher
parents:
56542
diff
changeset
|
46 |
import sun.security.ssl.XDHKeyExchange.XDHECredentials; |
56542 | 47 |
import sun.security.ssl.KeyShareExtension.CHKeyShareSpec; |
48 |
import sun.security.ssl.SSLExtension.ExtensionConsumer; |
|
49 |
import sun.security.ssl.SSLExtension.SSLExtensionSpec; |
|
50 |
import sun.security.ssl.SSLHandshake.HandshakeMessage; |
|
51 |
import sun.security.ssl.SupportedGroupsExtension.NamedGroup; |
|
56855 | 52 |
import sun.security.ssl.SupportedGroupsExtension.NamedGroupFunctions; |
56542 | 53 |
import sun.security.ssl.SupportedGroupsExtension.SupportedGroups; |
54 |
import sun.security.util.HexDumpEncoder; |
|
55 |
||
56 |
/** |
|
57 |
* Pack of the "key_share" extensions. |
|
58 |
*/ |
|
59 |
final class KeyShareExtension { |
|
60 |
static final HandshakeProducer chNetworkProducer = |
|
61 |
new CHKeyShareProducer(); |
|
62 |
static final ExtensionConsumer chOnLoadConcumer = |
|
63 |
new CHKeyShareConsumer(); |
|
64 |
static final SSLStringize chStringize = |
|
65 |
new CHKeyShareStringize(); |
|
66 |
||
67 |
static final HandshakeProducer shNetworkProducer = |
|
68 |
new SHKeyShareProducer(); |
|
69 |
static final ExtensionConsumer shOnLoadConcumer = |
|
70 |
new SHKeyShareConsumer(); |
|
71 |
static final HandshakeAbsence shOnLoadAbsence = |
|
72 |
new SHKeyShareAbsence(); |
|
73 |
static final SSLStringize shStringize = |
|
74 |
new SHKeyShareStringize(); |
|
75 |
||
76 |
static final HandshakeProducer hrrNetworkProducer = |
|
77 |
new HRRKeyShareProducer(); |
|
78 |
static final ExtensionConsumer hrrOnLoadConcumer = |
|
79 |
new HRRKeyShareConsumer(); |
|
80 |
static final HandshakeProducer hrrNetworkReproducer = |
|
81 |
new HRRKeyShareReproducer(); |
|
82 |
static final SSLStringize hrrStringize = |
|
83 |
new HRRKeyShareStringize(); |
|
84 |
||
85 |
/** |
|
86 |
* The key share entry used in "key_share" extensions. |
|
87 |
*/ |
|
88 |
private static final class KeyShareEntry { |
|
89 |
final int namedGroupId; |
|
90 |
final byte[] keyExchange; |
|
91 |
||
92 |
private KeyShareEntry(int namedGroupId, byte[] keyExchange) { |
|
93 |
this.namedGroupId = namedGroupId; |
|
94 |
this.keyExchange = keyExchange; |
|
95 |
} |
|
96 |
||
97 |
private byte[] getEncoded() { |
|
98 |
byte[] buffer = new byte[keyExchange.length + 4]; |
|
99 |
// 2: named group id |
|
100 |
// +2: key exchange length |
|
101 |
ByteBuffer m = ByteBuffer.wrap(buffer); |
|
102 |
try { |
|
103 |
Record.putInt16(m, namedGroupId); |
|
104 |
Record.putBytes16(m, keyExchange); |
|
105 |
} catch (IOException ioe) { |
|
106 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
107 |
SSLLogger.warning( |
|
108 |
"Unlikely IOException", ioe); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
return buffer; |
|
113 |
} |
|
114 |
||
115 |
private int getEncodedSize() { |
|
116 |
return keyExchange.length + 4; // 2: named group id |
|
117 |
// +2: key exchange length |
|
118 |
} |
|
119 |
||
120 |
@Override |
|
121 |
public String toString() { |
|
122 |
MessageFormat messageFormat = new MessageFormat( |
|
123 |
"\n'{'\n" + |
|
124 |
" \"named group\": {0}\n" + |
|
125 |
" \"key_exchange\": '{'\n" + |
|
126 |
"{1}\n" + |
|
127 |
" '}'\n" + |
|
128 |
"'}',", Locale.ENGLISH); |
|
129 |
||
130 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
131 |
Object[] messageFields = { |
|
132 |
NamedGroup.nameOf(namedGroupId), |
|
133 |
Utilities.indent(hexEncoder.encode(keyExchange), " ") |
|
134 |
}; |
|
135 |
||
136 |
return messageFormat.format(messageFields); |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
/** |
|
141 |
* The "key_share" extension in a ClientHello handshake message. |
|
142 |
*/ |
|
143 |
static final class CHKeyShareSpec implements SSLExtensionSpec { |
|
144 |
final List<KeyShareEntry> clientShares; |
|
145 |
||
146 |
private CHKeyShareSpec(List<KeyShareEntry> clientShares) { |
|
147 |
this.clientShares = clientShares; |
|
148 |
} |
|
149 |
||
150 |
private CHKeyShareSpec(ByteBuffer buffer) throws IOException { |
|
151 |
// struct { |
|
152 |
// KeyShareEntry client_shares<0..2^16-1>; |
|
153 |
// } KeyShareClientHello; |
|
154 |
if (buffer.remaining() < 2) { |
|
155 |
throw new SSLProtocolException( |
|
156 |
"Invalid key_share extension: " + |
|
157 |
"insufficient data (length=" + buffer.remaining() + ")"); |
|
158 |
} |
|
159 |
||
160 |
int listLen = Record.getInt16(buffer); |
|
161 |
if (listLen != buffer.remaining()) { |
|
162 |
throw new SSLProtocolException( |
|
163 |
"Invalid key_share extension: " + |
|
164 |
"incorrect list length (length=" + listLen + ")"); |
|
165 |
} |
|
166 |
||
167 |
List<KeyShareEntry> keyShares = new LinkedList<>(); |
|
168 |
while (buffer.hasRemaining()) { |
|
169 |
int namedGroupId = Record.getInt16(buffer); |
|
170 |
byte[] keyExchange = Record.getBytes16(buffer); |
|
171 |
if (keyExchange.length == 0) { |
|
172 |
throw new SSLProtocolException( |
|
173 |
"Invalid key_share extension: empty key_exchange"); |
|
174 |
} |
|
175 |
||
176 |
keyShares.add(new KeyShareEntry(namedGroupId, keyExchange)); |
|
177 |
} |
|
178 |
||
179 |
this.clientShares = Collections.unmodifiableList(keyShares); |
|
180 |
} |
|
181 |
||
182 |
@Override |
|
183 |
public String toString() { |
|
184 |
MessageFormat messageFormat = new MessageFormat( |
|
185 |
"\"client_shares\": '['{0}\n']'", Locale.ENGLISH); |
|
186 |
||
187 |
StringBuilder builder = new StringBuilder(512); |
|
188 |
for (KeyShareEntry entry : clientShares) { |
|
189 |
builder.append(entry.toString()); |
|
190 |
} |
|
191 |
||
192 |
Object[] messageFields = { |
|
193 |
Utilities.indent(builder.toString()) |
|
194 |
}; |
|
195 |
||
196 |
return messageFormat.format(messageFields); |
|
197 |
} |
|
198 |
} |
|
199 |
||
200 |
private static final class CHKeyShareStringize implements SSLStringize { |
|
201 |
@Override |
|
202 |
public String toString(ByteBuffer buffer) { |
|
203 |
try { |
|
204 |
return (new CHKeyShareSpec(buffer)).toString(); |
|
205 |
} catch (IOException ioe) { |
|
206 |
// For debug logging only, so please swallow exceptions. |
|
207 |
return ioe.getMessage(); |
|
208 |
} |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
/** |
|
213 |
* Network data producer of the extension in a ClientHello |
|
214 |
* handshake message. |
|
215 |
*/ |
|
216 |
private static final |
|
217 |
class CHKeyShareProducer implements HandshakeProducer { |
|
218 |
// Prevent instantiation of this class. |
|
219 |
private CHKeyShareProducer() { |
|
220 |
// blank |
|
221 |
} |
|
222 |
||
223 |
@Override |
|
224 |
public byte[] produce(ConnectionContext context, |
|
225 |
HandshakeMessage message) throws IOException { |
|
226 |
// The producing happens in client side only. |
|
227 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
228 |
||
229 |
// Is it a supported and enabled extension? |
|
230 |
if (!chc.sslConfig.isAvailable(SSLExtension.CH_KEY_SHARE)) { |
|
231 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
232 |
SSLLogger.fine( |
|
233 |
"Ignore unavailable key_share extension"); |
|
234 |
} |
|
235 |
return null; |
|
236 |
} |
|
237 |
||
238 |
List<NamedGroup> namedGroups; |
|
239 |
if (chc.serverSelectedNamedGroup != null) { |
|
240 |
// Response to HelloRetryRequest |
|
241 |
namedGroups = Arrays.asList(chc.serverSelectedNamedGroup); |
|
242 |
} else { |
|
243 |
namedGroups = chc.clientRequestedNamedGroups; |
|
244 |
if (namedGroups == null || namedGroups.isEmpty()) { |
|
245 |
// No supported groups. |
|
246 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
247 |
SSLLogger.warning( |
|
248 |
"Ignore key_share extension, no supported groups"); |
|
249 |
} |
|
250 |
return null; |
|
251 |
} |
|
252 |
} |
|
253 |
||
254 |
List<KeyShareEntry> keyShares = new LinkedList<>(); |
|
255 |
for (NamedGroup ng : namedGroups) { |
|
256 |
SSLKeyExchange ke = SSLKeyExchange.valueOf(ng); |
|
257 |
if (ke == null) { |
|
258 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
259 |
SSLLogger.warning( |
|
260 |
"No key exchange for named group " + ng.name); |
|
261 |
} |
|
262 |
continue; |
|
263 |
} |
|
264 |
||
265 |
SSLPossession[] poses = ke.createPossessions(chc); |
|
266 |
for (SSLPossession pos : poses) { |
|
267 |
// update the context |
|
268 |
chc.handshakePossessions.add(pos); |
|
269 |
if (!(pos instanceof ECDHEPossession) && |
|
56589
bafd8be2f970
Initial working XDH support in TLS. I should try to refactor the code a bit.
apetcher
parents:
56542
diff
changeset
|
270 |
!(pos instanceof DHEPossession) && |
bafd8be2f970
Initial working XDH support in TLS. I should try to refactor the code a bit.
apetcher
parents:
56542
diff
changeset
|
271 |
!(pos instanceof XDHEPossession)) { |
56542 | 272 |
// May need more possesion types in the future. |
273 |
continue; |
|
274 |
} |
|
275 |
||
276 |
keyShares.add(new KeyShareEntry(ng.id, pos.encode())); |
|
277 |
} |
|
278 |
||
279 |
// One key share entry only. Too much key share entries makes |
|
280 |
// the ClientHello handshake message really big. |
|
281 |
if (!keyShares.isEmpty()) { |
|
282 |
break; |
|
283 |
} |
|
284 |
} |
|
285 |
||
286 |
int listLen = 0; |
|
287 |
for (KeyShareEntry entry : keyShares) { |
|
288 |
listLen += entry.getEncodedSize(); |
|
289 |
} |
|
290 |
byte[] extData = new byte[listLen + 2]; // 2: list length |
|
291 |
ByteBuffer m = ByteBuffer.wrap(extData); |
|
292 |
Record.putInt16(m, listLen); |
|
293 |
for (KeyShareEntry entry : keyShares) { |
|
294 |
m.put(entry.getEncoded()); |
|
295 |
} |
|
296 |
||
297 |
// update the context |
|
298 |
chc.handshakeExtensions.put(SSLExtension.CH_KEY_SHARE, |
|
299 |
new CHKeyShareSpec(keyShares)); |
|
300 |
||
301 |
return extData; |
|
302 |
} |
|
303 |
} |
|
304 |
||
305 |
/** |
|
306 |
* Network data consumer of the extension in a ClientHello |
|
307 |
* handshake message. |
|
308 |
*/ |
|
309 |
private static final class CHKeyShareConsumer implements ExtensionConsumer { |
|
310 |
// Prevent instantiation of this class. |
|
311 |
private CHKeyShareConsumer() { |
|
312 |
// blank |
|
313 |
} |
|
314 |
||
315 |
@Override |
|
316 |
public void consume(ConnectionContext context, |
|
317 |
HandshakeMessage message, ByteBuffer buffer) throws IOException { |
|
318 |
// The comsuming happens in server side only. |
|
319 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
320 |
||
321 |
if (shc.handshakeExtensions.containsKey(SSLExtension.CH_KEY_SHARE)) { |
|
322 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
323 |
SSLLogger.fine( |
|
324 |
"The key_share extension has been loaded"); |
|
325 |
} |
|
326 |
return; |
|
327 |
} |
|
328 |
||
329 |
// Is it a supported and enabled extension? |
|
330 |
if (!shc.sslConfig.isAvailable(SSLExtension.CH_KEY_SHARE)) { |
|
331 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
332 |
SSLLogger.fine( |
|
333 |
"Ignore unavailable key_share extension"); |
|
334 |
} |
|
335 |
return; // ignore the extension |
|
336 |
} |
|
337 |
||
338 |
// Parse the extension |
|
339 |
CHKeyShareSpec spec; |
|
340 |
try { |
|
341 |
spec = new CHKeyShareSpec(buffer); |
|
342 |
} catch (IOException ioe) { |
|
343 |
shc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, ioe); |
|
344 |
return; // fatal() always throws, make the compiler happy. |
|
345 |
} |
|
346 |
||
347 |
List<SSLCredentials> credentials = new LinkedList<>(); |
|
348 |
for (KeyShareEntry entry : spec.clientShares) { |
|
349 |
NamedGroup ng = NamedGroup.valueOf(entry.namedGroupId); |
|
350 |
if (ng != null && !SupportedGroups.isActivatable( |
|
351 |
shc.sslConfig.algorithmConstraints, ng)) { |
|
352 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
353 |
SSLLogger.fine( |
|
354 |
"Ignore unsupported named group: " + |
|
355 |
NamedGroup.nameOf(entry.namedGroupId)); |
|
356 |
} |
|
357 |
continue; |
|
358 |
} |
|
359 |
||
56855 | 360 |
try { |
361 |
NamedGroupFunctions ngf = ng.getFunctions().orElseThrow( |
|
362 |
GeneralSecurityException::new); |
|
363 |
SSLKeyAgreementCredentials kaCred = |
|
364 |
ngf.decodeCredentials(entry.keyExchange); |
|
365 |
if (kaCred != null) { |
|
366 |
if (!shc.algorithmConstraints.permits( |
|
367 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), |
|
368 |
kaCred.getPublicKey())) { |
|
369 |
SSLLogger.warning( |
|
370 |
"key share entry does not " + |
|
371 |
"comply with algorithm constraints"); |
|
372 |
} else { |
|
373 |
credentials.add(kaCred); |
|
56542 | 374 |
} |
375 |
} |
|
56855 | 376 |
} catch (GeneralSecurityException ex) { |
377 |
SSLLogger.warning( |
|
56589
bafd8be2f970
Initial working XDH support in TLS. I should try to refactor the code a bit.
apetcher
parents:
56542
diff
changeset
|
378 |
"Cannot decode named group: " + |
bafd8be2f970
Initial working XDH support in TLS. I should try to refactor the code a bit.
apetcher
parents:
56542
diff
changeset
|
379 |
NamedGroup.nameOf(entry.namedGroupId)); |
56542 | 380 |
} |
381 |
} |
|
382 |
||
383 |
if (!credentials.isEmpty()) { |
|
384 |
shc.handshakeCredentials.addAll(credentials); |
|
385 |
} else { |
|
386 |
// New handshake credentials are required from the client side. |
|
387 |
shc.handshakeProducers.put( |
|
388 |
SSLHandshake.HELLO_RETRY_REQUEST.id, |
|
389 |
SSLHandshake.HELLO_RETRY_REQUEST); |
|
390 |
} |
|
391 |
||
392 |
// update the context |
|
393 |
shc.handshakeExtensions.put(SSLExtension.CH_KEY_SHARE, spec); |
|
394 |
} |
|
395 |
} |
|
396 |
||
397 |
/** |
|
398 |
* The key share entry used in ServerHello "key_share" extensions. |
|
399 |
*/ |
|
400 |
static final class SHKeyShareSpec implements SSLExtensionSpec { |
|
401 |
final KeyShareEntry serverShare; |
|
402 |
||
403 |
SHKeyShareSpec(KeyShareEntry serverShare) { |
|
404 |
this.serverShare = serverShare; |
|
405 |
} |
|
406 |
||
407 |
private SHKeyShareSpec(ByteBuffer buffer) throws IOException { |
|
408 |
// struct { |
|
409 |
// KeyShareEntry server_share; |
|
410 |
// } KeyShareServerHello; |
|
411 |
if (buffer.remaining() < 5) { // 5: minimal server_share |
|
412 |
throw new SSLProtocolException( |
|
413 |
"Invalid key_share extension: " + |
|
414 |
"insufficient data (length=" + buffer.remaining() + ")"); |
|
415 |
} |
|
416 |
||
417 |
int namedGroupId = Record.getInt16(buffer); |
|
418 |
byte[] keyExchange = Record.getBytes16(buffer); |
|
419 |
||
420 |
if (buffer.hasRemaining()) { |
|
421 |
throw new SSLProtocolException( |
|
422 |
"Invalid key_share extension: unknown extra data"); |
|
423 |
} |
|
424 |
||
425 |
this.serverShare = new KeyShareEntry(namedGroupId, keyExchange); |
|
426 |
} |
|
427 |
||
428 |
@Override |
|
429 |
public String toString() { |
|
430 |
MessageFormat messageFormat = new MessageFormat( |
|
431 |
"\"server_share\": '{'\n" + |
|
432 |
" \"named group\": {0}\n" + |
|
433 |
" \"key_exchange\": '{'\n" + |
|
434 |
"{1}\n" + |
|
435 |
" '}'\n" + |
|
436 |
"'}',", Locale.ENGLISH); |
|
437 |
||
438 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
439 |
Object[] messageFields = { |
|
440 |
NamedGroup.nameOf(serverShare.namedGroupId), |
|
441 |
Utilities.indent( |
|
442 |
hexEncoder.encode(serverShare.keyExchange), " ") |
|
443 |
}; |
|
444 |
||
445 |
return messageFormat.format(messageFields); |
|
446 |
} |
|
447 |
} |
|
448 |
||
449 |
private static final class SHKeyShareStringize implements SSLStringize { |
|
450 |
@Override |
|
451 |
public String toString(ByteBuffer buffer) { |
|
452 |
try { |
|
453 |
return (new SHKeyShareSpec(buffer)).toString(); |
|
454 |
} catch (IOException ioe) { |
|
455 |
// For debug logging only, so please swallow exceptions. |
|
456 |
return ioe.getMessage(); |
|
457 |
} |
|
458 |
} |
|
459 |
} |
|
460 |
||
461 |
/** |
|
462 |
* Network data producer of the extension in a ServerHello |
|
463 |
* handshake message. |
|
464 |
*/ |
|
465 |
private static final class SHKeyShareProducer implements HandshakeProducer { |
|
466 |
// Prevent instantiation of this class. |
|
467 |
private SHKeyShareProducer() { |
|
468 |
// blank |
|
469 |
} |
|
470 |
||
471 |
@Override |
|
472 |
public byte[] produce(ConnectionContext context, |
|
473 |
HandshakeMessage message) throws IOException { |
|
474 |
// The producing happens in client side only. |
|
475 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
476 |
||
477 |
// In response to key_share request only |
|
478 |
CHKeyShareSpec kss = |
|
479 |
(CHKeyShareSpec)shc.handshakeExtensions.get( |
|
480 |
SSLExtension.CH_KEY_SHARE); |
|
481 |
if (kss == null) { |
|
482 |
// Unlikely, no key_share extension requested. |
|
483 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
484 |
SSLLogger.warning( |
|
485 |
"Ignore, no client key_share extension"); |
|
486 |
} |
|
487 |
return null; |
|
488 |
} |
|
489 |
||
490 |
// Is it a supported and enabled extension? |
|
491 |
if (!shc.sslConfig.isAvailable(SSLExtension.SH_KEY_SHARE)) { |
|
492 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
493 |
SSLLogger.warning( |
|
494 |
"Ignore, no available server key_share extension"); |
|
495 |
} |
|
496 |
return null; |
|
497 |
} |
|
498 |
||
499 |
// use requested key share entries |
|
500 |
if ((shc.handshakeCredentials == null) || |
|
501 |
shc.handshakeCredentials.isEmpty()) { |
|
502 |
// Unlikely, HelloRetryRequest should be used ealier. |
|
503 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
504 |
SSLLogger.warning( |
|
505 |
"No available client key share entries"); |
|
506 |
} |
|
507 |
return null; |
|
508 |
} |
|
509 |
||
510 |
KeyShareEntry keyShare = null; |
|
511 |
for (SSLCredentials cd : shc.handshakeCredentials) { |
|
512 |
NamedGroup ng = null; |
|
513 |
if (cd instanceof ECDHECredentials) { |
|
514 |
ng = ((ECDHECredentials)cd).namedGroup; |
|
515 |
} else if (cd instanceof DHECredentials) { |
|
516 |
ng = ((DHECredentials)cd).namedGroup; |
|
56589
bafd8be2f970
Initial working XDH support in TLS. I should try to refactor the code a bit.
apetcher
parents:
56542
diff
changeset
|
517 |
} else if (cd instanceof XDHECredentials) { |
bafd8be2f970
Initial working XDH support in TLS. I should try to refactor the code a bit.
apetcher
parents:
56542
diff
changeset
|
518 |
ng = ((XDHECredentials)cd).namedGroup; |
56542 | 519 |
} |
520 |
||
521 |
if (ng == null) { |
|
522 |
continue; |
|
523 |
} |
|
524 |
||
525 |
SSLKeyExchange ke = SSLKeyExchange.valueOf(ng); |
|
526 |
if (ke == null) { |
|
527 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
528 |
SSLLogger.warning( |
|
529 |
"No key exchange for named group " + ng.name); |
|
530 |
} |
|
531 |
continue; |
|
532 |
} |
|
533 |
||
534 |
SSLPossession[] poses = ke.createPossessions(shc); |
|
535 |
for (SSLPossession pos : poses) { |
|
536 |
if (!(pos instanceof ECDHEPossession) && |
|
56589
bafd8be2f970
Initial working XDH support in TLS. I should try to refactor the code a bit.
apetcher
parents:
56542
diff
changeset
|
537 |
!(pos instanceof DHEPossession) && |
bafd8be2f970
Initial working XDH support in TLS. I should try to refactor the code a bit.
apetcher
parents:
56542
diff
changeset
|
538 |
!(pos instanceof XDHEPossession)) { |
56542 | 539 |
// May need more possesion types in the future. |
540 |
continue; |
|
541 |
} |
|
542 |
||
543 |
// update the context |
|
544 |
shc.handshakeKeyExchange = ke; |
|
545 |
shc.handshakePossessions.add(pos); |
|
546 |
keyShare = new KeyShareEntry(ng.id, pos.encode()); |
|
547 |
break; |
|
548 |
} |
|
549 |
||
550 |
if (keyShare != null) { |
|
551 |
for (Map.Entry<Byte, HandshakeProducer> me : |
|
552 |
ke.getHandshakeProducers(shc)) { |
|
553 |
shc.handshakeProducers.put( |
|
554 |
me.getKey(), me.getValue()); |
|
555 |
} |
|
556 |
||
557 |
// We have got one! Don't forgor to break. |
|
558 |
break; |
|
559 |
} |
|
560 |
} |
|
561 |
||
562 |
if (keyShare == null) { |
|
563 |
// Unlikely, HelloRetryRequest should be used instead ealier. |
|
564 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
565 |
SSLLogger.warning( |
|
566 |
"No available server key_share extension"); |
|
567 |
} |
|
568 |
return null; |
|
569 |
} |
|
570 |
||
571 |
byte[] extData = keyShare.getEncoded(); |
|
572 |
||
573 |
// update the context |
|
574 |
SHKeyShareSpec spec = new SHKeyShareSpec(keyShare); |
|
575 |
shc.handshakeExtensions.put(SSLExtension.SH_KEY_SHARE, spec); |
|
576 |
||
577 |
return extData; |
|
578 |
} |
|
579 |
} |
|
580 |
||
581 |
/** |
|
582 |
* Network data consumer of the extension in a ServerHello |
|
583 |
* handshake message. |
|
584 |
*/ |
|
585 |
private static final class SHKeyShareConsumer implements ExtensionConsumer { |
|
586 |
// Prevent instantiation of this class. |
|
587 |
private SHKeyShareConsumer() { |
|
588 |
// blank |
|
589 |
} |
|
590 |
||
591 |
@Override |
|
592 |
public void consume(ConnectionContext context, |
|
593 |
HandshakeMessage message, ByteBuffer buffer) throws IOException { |
|
594 |
// Happens in client side only. |
|
595 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
596 |
if (chc.clientRequestedNamedGroups == null || |
|
597 |
chc.clientRequestedNamedGroups.isEmpty()) { |
|
598 |
// No supported groups. |
|
599 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
600 |
"Unexpected key_share extension in ServerHello"); |
|
601 |
return; // fatal() always throws, make the compiler happy. |
|
602 |
} |
|
603 |
||
604 |
// Is it a supported and enabled extension? |
|
605 |
if (!chc.sslConfig.isAvailable(SSLExtension.SH_KEY_SHARE)) { |
|
606 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
607 |
"Unsupported key_share extension in ServerHello"); |
|
608 |
return; // fatal() always throws, make the compiler happy. |
|
609 |
} |
|
610 |
||
611 |
// Parse the extension |
|
612 |
SHKeyShareSpec spec; |
|
613 |
try { |
|
614 |
spec = new SHKeyShareSpec(buffer); |
|
615 |
} catch (IOException ioe) { |
|
616 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, ioe); |
|
617 |
return; // fatal() always throws, make the compiler happy. |
|
618 |
} |
|
619 |
||
620 |
KeyShareEntry keyShare = spec.serverShare; |
|
621 |
NamedGroup ng = NamedGroup.valueOf(keyShare.namedGroupId); |
|
622 |
if (ng == null || !SupportedGroups.isActivatable( |
|
623 |
chc.sslConfig.algorithmConstraints, ng)) { |
|
624 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
625 |
"Unsupported named group: " + |
|
626 |
NamedGroup.nameOf(keyShare.namedGroupId)); |
|
627 |
return; // fatal() always throws, make the compiler happy. |
|
628 |
} |
|
629 |
||
630 |
SSLKeyExchange ke = SSLKeyExchange.valueOf(ng); |
|
631 |
if (ke == null) { |
|
632 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
633 |
"No key exchange for named group " + ng.name); |
|
634 |
return; // fatal() always throws, make the compiler happy. |
|
635 |
} |
|
636 |
||
637 |
SSLCredentials credentials = null; |
|
56855 | 638 |
try { |
639 |
NamedGroupFunctions ngf = ng.getFunctions().orElseThrow( |
|
640 |
GeneralSecurityException::new); |
|
641 |
SSLKeyAgreementCredentials kaCred = |
|
642 |
ngf.decodeCredentials(keyShare.keyExchange); |
|
643 |
if (kaCred != null) { |
|
644 |
if (!chc.algorithmConstraints.permits( |
|
645 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), |
|
646 |
kaCred.getPublicKey())) { |
|
647 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
648 |
"key share entry does not " + |
|
649 |
"comply to algorithm constraints"); |
|
650 |
} else { |
|
651 |
credentials = kaCred; |
|
56542 | 652 |
} |
653 |
} |
|
56855 | 654 |
} catch (IOException | GeneralSecurityException ex) { |
56542 | 655 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
56855 | 656 |
"Cannot decode named group: " + |
657 |
NamedGroup.nameOf(keyShare.namedGroupId)); |
|
56542 | 658 |
} |
659 |
||
660 |
if (credentials == null) { |
|
661 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
662 |
"Unsupported named group: " + ng.name); |
|
663 |
} |
|
664 |
||
665 |
// update the context |
|
666 |
chc.handshakeKeyExchange = ke; |
|
667 |
chc.handshakeCredentials.add(credentials); |
|
668 |
chc.handshakeExtensions.put(SSLExtension.SH_KEY_SHARE, spec); |
|
669 |
} |
|
670 |
} |
|
671 |
||
672 |
/** |
|
673 |
* The absence processing if the extension is not present in |
|
674 |
* the ServerHello handshake message. |
|
675 |
*/ |
|
676 |
private static final class SHKeyShareAbsence implements HandshakeAbsence { |
|
677 |
@Override |
|
678 |
public void absent(ConnectionContext context, |
|
679 |
HandshakeMessage message) throws IOException { |
|
680 |
// The producing happens in client side only. |
|
681 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
682 |
||
683 |
// Cannot use the previous requested key shares any more. |
|
684 |
if (SSLLogger.isOn && SSLLogger.isOn("handshake")) { |
|
685 |
SSLLogger.fine( |
|
686 |
"No key_share extension in ServerHello, " + |
|
687 |
"cleanup the key shares if necessary"); |
|
688 |
} |
|
689 |
chc.handshakePossessions.clear(); |
|
690 |
} |
|
691 |
} |
|
692 |
||
693 |
/** |
|
694 |
* The key share entry used in HelloRetryRequest "key_share" extensions. |
|
695 |
*/ |
|
696 |
static final class HRRKeyShareSpec implements SSLExtensionSpec { |
|
697 |
final int selectedGroup; |
|
698 |
||
699 |
HRRKeyShareSpec(NamedGroup serverGroup) { |
|
700 |
this.selectedGroup = serverGroup.id; |
|
701 |
} |
|
702 |
||
703 |
private HRRKeyShareSpec(ByteBuffer buffer) throws IOException { |
|
704 |
// struct { |
|
705 |
// NamedGroup selected_group; |
|
706 |
// } KeyShareHelloRetryRequest; |
|
707 |
if (buffer.remaining() != 2) { |
|
708 |
throw new SSLProtocolException( |
|
709 |
"Invalid key_share extension: " + |
|
710 |
"improper data (length=" + buffer.remaining() + ")"); |
|
711 |
} |
|
712 |
||
713 |
this.selectedGroup = Record.getInt16(buffer); |
|
714 |
} |
|
715 |
||
716 |
@Override |
|
717 |
public String toString() { |
|
718 |
MessageFormat messageFormat = new MessageFormat( |
|
719 |
"\"selected group\": '['{0}']'", Locale.ENGLISH); |
|
720 |
||
721 |
Object[] messageFields = { |
|
722 |
NamedGroup.nameOf(selectedGroup) |
|
723 |
}; |
|
724 |
return messageFormat.format(messageFields); |
|
725 |
} |
|
726 |
} |
|
727 |
||
728 |
private static final class HRRKeyShareStringize implements SSLStringize { |
|
729 |
@Override |
|
730 |
public String toString(ByteBuffer buffer) { |
|
731 |
try { |
|
732 |
return (new HRRKeyShareSpec(buffer)).toString(); |
|
733 |
} catch (IOException ioe) { |
|
734 |
// For debug logging only, so please swallow exceptions. |
|
735 |
return ioe.getMessage(); |
|
736 |
} |
|
737 |
} |
|
738 |
} |
|
739 |
||
740 |
/** |
|
741 |
* Network data producer of the extension in a HelloRetryRequest |
|
742 |
* handshake message. |
|
743 |
*/ |
|
744 |
private static final |
|
745 |
class HRRKeyShareProducer implements HandshakeProducer { |
|
746 |
// Prevent instantiation of this class. |
|
747 |
private HRRKeyShareProducer() { |
|
748 |
// blank |
|
749 |
} |
|
750 |
||
751 |
@Override |
|
752 |
public byte[] produce(ConnectionContext context, |
|
753 |
HandshakeMessage message) throws IOException { |
|
754 |
// The producing happens in server side only. |
|
755 |
ServerHandshakeContext shc = (ServerHandshakeContext) context; |
|
756 |
||
757 |
// Is it a supported and enabled extension? |
|
758 |
if (!shc.sslConfig.isAvailable(SSLExtension.HRR_KEY_SHARE)) { |
|
759 |
shc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
760 |
"Unsupported key_share extension in HelloRetryRequest"); |
|
761 |
return null; // make the compiler happy. |
|
762 |
} |
|
763 |
||
764 |
if (shc.clientRequestedNamedGroups == null || |
|
765 |
shc.clientRequestedNamedGroups.isEmpty()) { |
|
766 |
// No supported groups. |
|
767 |
shc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
768 |
"Unexpected key_share extension in HelloRetryRequest"); |
|
769 |
return null; // make the compiler happy. |
|
770 |
} |
|
771 |
||
772 |
NamedGroup selectedGroup = null; |
|
773 |
for (NamedGroup ng : shc.clientRequestedNamedGroups) { |
|
774 |
if (SupportedGroups.isActivatable( |
|
775 |
shc.sslConfig.algorithmConstraints, ng)) { |
|
776 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
777 |
SSLLogger.fine( |
|
778 |
"HelloRetryRequest selected named group: " + |
|
779 |
ng.name); |
|
780 |
} |
|
781 |
||
782 |
// TODO: is the named group supported by the underlying |
|
783 |
// crypto provider? |
|
784 |
selectedGroup = ng; |
|
785 |
break; |
|
786 |
} |
|
787 |
} |
|
788 |
||
789 |
if (selectedGroup == null) { |
|
790 |
shc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
791 |
new IOException("No common named group")); |
|
792 |
return null; // make the complier happy |
|
793 |
} |
|
794 |
||
795 |
byte[] extdata = new byte[] { |
|
796 |
(byte)((selectedGroup.id >> 8) & 0xFF), |
|
797 |
(byte)(selectedGroup.id & 0xFF) |
|
798 |
}; |
|
799 |
||
800 |
// update the context |
|
801 |
shc.serverSelectedNamedGroup = selectedGroup; |
|
802 |
shc.handshakeExtensions.put(SSLExtension.HRR_KEY_SHARE, |
|
803 |
new HRRKeyShareSpec(selectedGroup)); |
|
804 |
||
805 |
return extdata; |
|
806 |
} |
|
807 |
} |
|
808 |
||
809 |
/** |
|
810 |
* Network data producer of the extension for stateless |
|
811 |
* HelloRetryRequest reconstruction. |
|
812 |
*/ |
|
813 |
private static final |
|
814 |
class HRRKeyShareReproducer implements HandshakeProducer { |
|
815 |
// Prevent instantiation of this class. |
|
816 |
private HRRKeyShareReproducer() { |
|
817 |
// blank |
|
818 |
} |
|
819 |
||
820 |
@Override |
|
821 |
public byte[] produce(ConnectionContext context, |
|
822 |
HandshakeMessage message) throws IOException { |
|
823 |
// The producing happens in server side only. |
|
824 |
ServerHandshakeContext shc = (ServerHandshakeContext) context; |
|
825 |
||
826 |
// Is it a supported and enabled extension? |
|
827 |
if (!shc.sslConfig.isAvailable(SSLExtension.HRR_KEY_SHARE)) { |
|
828 |
shc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
829 |
"Unsupported key_share extension in HelloRetryRequest"); |
|
830 |
return null; // make the compiler happy. |
|
831 |
} |
|
832 |
||
833 |
CHKeyShareSpec spec = (CHKeyShareSpec)shc.handshakeExtensions.get( |
|
834 |
SSLExtension.CH_KEY_SHARE); |
|
835 |
if (spec != null && spec.clientShares != null && |
|
836 |
spec.clientShares.size() == 1) { |
|
837 |
int namedGroupId = spec.clientShares.get(0).namedGroupId; |
|
838 |
||
839 |
byte[] extdata = new byte[] { |
|
840 |
(byte)((namedGroupId >> 8) & 0xFF), |
|
841 |
(byte)(namedGroupId & 0xFF) |
|
842 |
}; |
|
843 |
||
844 |
return extdata; |
|
845 |
} |
|
846 |
||
847 |
return null; |
|
848 |
} |
|
849 |
} |
|
850 |
||
851 |
/** |
|
852 |
* Network data consumer of the extension in a HelloRetryRequest |
|
853 |
* handshake message. |
|
854 |
*/ |
|
855 |
private static final |
|
856 |
class HRRKeyShareConsumer implements ExtensionConsumer { |
|
857 |
// Prevent instantiation of this class. |
|
858 |
private HRRKeyShareConsumer() { |
|
859 |
// blank |
|
860 |
} |
|
861 |
||
862 |
@Override |
|
863 |
public void consume(ConnectionContext context, |
|
864 |
HandshakeMessage message, ByteBuffer buffer) throws IOException { |
|
865 |
// The producing happens in client side only. |
|
866 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
867 |
||
868 |
// Is it a supported and enabled extension? |
|
869 |
if (!chc.sslConfig.isAvailable(SSLExtension.HRR_KEY_SHARE)) { |
|
870 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
871 |
"Unsupported key_share extension in HelloRetryRequest"); |
|
872 |
return; // make the compiler happy. |
|
873 |
} |
|
874 |
||
875 |
if (chc.clientRequestedNamedGroups == null || |
|
876 |
chc.clientRequestedNamedGroups.isEmpty()) { |
|
877 |
// No supported groups. |
|
878 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
879 |
"Unexpected key_share extension in HelloRetryRequest"); |
|
880 |
return; // make the compiler happy. |
|
881 |
} |
|
882 |
||
883 |
// Parse the extension |
|
884 |
HRRKeyShareSpec spec; |
|
885 |
try { |
|
886 |
spec = new HRRKeyShareSpec(buffer); |
|
887 |
} catch (IOException ioe) { |
|
888 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, ioe); |
|
889 |
return; // fatal() always throws, make the compiler happy. |
|
890 |
} |
|
891 |
||
892 |
NamedGroup serverGroup = NamedGroup.valueOf(spec.selectedGroup); |
|
893 |
if (serverGroup == null) { |
|
894 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
895 |
"Unsupported HelloRetryRequest selected group: " + |
|
896 |
NamedGroup.nameOf(spec.selectedGroup)); |
|
897 |
return; // fatal() always throws, make the compiler happy. |
|
898 |
} |
|
899 |
||
900 |
if (!chc.clientRequestedNamedGroups.contains(serverGroup)) { |
|
901 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
902 |
"Unexpected HelloRetryRequest selected group: " + |
|
903 |
serverGroup.name); |
|
904 |
return; // fatal() always throws, make the compiler happy. |
|
905 |
} |
|
906 |
||
907 |
// TODO: the selected group does not correspond to a group which |
|
908 |
// was provided in the "key_share" extension in the original |
|
909 |
// ClientHello. |
|
910 |
||
911 |
// update the context |
|
912 |
||
913 |
// When sending the new ClientHello, the client MUST replace the |
|
914 |
// original "key_share" extension with one containing only a new |
|
915 |
// KeyShareEntry for the group indicated in the selected_group |
|
916 |
// field of the triggering HelloRetryRequest. |
|
917 |
// |
|
918 |
chc.serverSelectedNamedGroup = serverGroup; |
|
919 |
chc.handshakeExtensions.put(SSLExtension.HRR_KEY_SHARE, spec); |
|
920 |
} |
|
921 |
} |
|
922 |
} |