]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/statem/extensions_srvr.c
Enable brainpool curves for TLS1.3
[thirdparty/openssl.git] / ssl / statem / extensions_srvr.c
1 /*
2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <openssl/ocsp.h>
11 #include "../ssl_local.h"
12 #include "statem_local.h"
13 #include "internal/cryptlib.h"
14
15 #define COOKIE_STATE_FORMAT_VERSION 0
16
17 /*
18 * 2 bytes for packet length, 2 bytes for format version, 2 bytes for
19 * protocol version, 2 bytes for group id, 2 bytes for cipher id, 1 byte for
20 * key_share present flag, 4 bytes for timestamp, 2 bytes for the hashlen,
21 * EVP_MAX_MD_SIZE for transcript hash, 1 byte for app cookie length, app cookie
22 * length bytes, SHA256_DIGEST_LENGTH bytes for the HMAC of the whole thing.
23 */
24 #define MAX_COOKIE_SIZE (2 + 2 + 2 + 2 + 2 + 1 + 4 + 2 + EVP_MAX_MD_SIZE + 1 \
25 + SSL_COOKIE_LENGTH + SHA256_DIGEST_LENGTH)
26
27 /*
28 * Message header + 2 bytes for protocol version + number of random bytes +
29 * + 1 byte for legacy session id length + number of bytes in legacy session id
30 * + 2 bytes for ciphersuite + 1 byte for legacy compression
31 * + 2 bytes for extension block length + 6 bytes for key_share extension
32 * + 4 bytes for cookie extension header + the number of bytes in the cookie
33 */
34 #define MAX_HRR_SIZE (SSL3_HM_HEADER_LENGTH + 2 + SSL3_RANDOM_SIZE + 1 \
35 + SSL_MAX_SSL_SESSION_ID_LENGTH + 2 + 1 + 2 + 6 + 4 \
36 + MAX_COOKIE_SIZE)
37
38 /*
39 * Parse the client's renegotiation binding and abort if it's not right
40 */
41 int tls_parse_ctos_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
42 X509 *x, size_t chainidx)
43 {
44 unsigned int ilen;
45 const unsigned char *data;
46
47 /* Parse the length byte */
48 if (!PACKET_get_1(pkt, &ilen)
49 || !PACKET_get_bytes(pkt, &data, ilen)) {
50 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);
51 return 0;
52 }
53
54 /* Check that the extension matches */
55 if (ilen != s->s3.previous_client_finished_len) {
56 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH);
57 return 0;
58 }
59
60 if (memcmp(data, s->s3.previous_client_finished,
61 s->s3.previous_client_finished_len)) {
62 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH);
63 return 0;
64 }
65
66 s->s3.send_connection_binding = 1;
67
68 return 1;
69 }
70
71 /*-
72 * The servername extension is treated as follows:
73 *
74 * - Only the hostname type is supported with a maximum length of 255.
75 * - The servername is rejected if too long or if it contains zeros,
76 * in which case an fatal alert is generated.
77 * - The servername field is maintained together with the session cache.
78 * - When a session is resumed, the servername call back invoked in order
79 * to allow the application to position itself to the right context.
80 * - The servername is acknowledged if it is new for a session or when
81 * it is identical to a previously used for the same session.
82 * Applications can control the behaviour. They can at any time
83 * set a 'desirable' servername for a new SSL object. This can be the
84 * case for example with HTTPS when a Host: header field is received and
85 * a renegotiation is requested. In this case, a possible servername
86 * presented in the new client hello is only acknowledged if it matches
87 * the value of the Host: field.
88 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
89 * if they provide for changing an explicit servername context for the
90 * session, i.e. when the session has been established with a servername
91 * extension.
92 * - On session reconnect, the servername extension may be absent.
93 */
94 int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, unsigned int context,
95 X509 *x, size_t chainidx)
96 {
97 unsigned int servname_type;
98 PACKET sni, hostname;
99
100 if (!PACKET_as_length_prefixed_2(pkt, &sni)
101 /* ServerNameList must be at least 1 byte long. */
102 || PACKET_remaining(&sni) == 0) {
103 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
104 return 0;
105 }
106
107 /*
108 * Although the intent was for server_name to be extensible, RFC 4366
109 * was not clear about it; and so OpenSSL among other implementations,
110 * always and only allows a 'host_name' name types.
111 * RFC 6066 corrected the mistake but adding new name types
112 * is nevertheless no longer feasible, so act as if no other
113 * SNI types can exist, to simplify parsing.
114 *
115 * Also note that the RFC permits only one SNI value per type,
116 * i.e., we can only have a single hostname.
117 */
118 if (!PACKET_get_1(&sni, &servname_type)
119 || servname_type != TLSEXT_NAMETYPE_host_name
120 || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
121 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
122 return 0;
123 }
124
125 /*
126 * In TLSv1.2 and below the SNI is associated with the session. In TLSv1.3
127 * we always use the SNI value from the handshake.
128 */
129 if (!s->hit || SSL_IS_TLS13(s)) {
130 if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
131 SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION);
132 return 0;
133 }
134
135 if (PACKET_contains_zero_byte(&hostname)) {
136 SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION);
137 return 0;
138 }
139
140 /*
141 * Store the requested SNI in the SSL as temporary storage.
142 * If we accept it, it will get stored in the SSL_SESSION as well.
143 */
144 OPENSSL_free(s->ext.hostname);
145 s->ext.hostname = NULL;
146 if (!PACKET_strndup(&hostname, &s->ext.hostname)) {
147 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
148 return 0;
149 }
150
151 s->servername_done = 1;
152 } else {
153 /*
154 * In TLSv1.2 and below we should check if the SNI is consistent between
155 * the initial handshake and the resumption. In TLSv1.3 SNI is not
156 * associated with the session.
157 */
158 s->servername_done = (s->session->ext.hostname != NULL)
159 && PACKET_equal(&hostname, s->session->ext.hostname,
160 strlen(s->session->ext.hostname));
161 }
162
163 return 1;
164 }
165
166 int tls_parse_ctos_maxfragmentlen(SSL *s, PACKET *pkt, unsigned int context,
167 X509 *x, size_t chainidx)
168 {
169 unsigned int value;
170
171 if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
172 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
173 return 0;
174 }
175
176 /* Received |value| should be a valid max-fragment-length code. */
177 if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
178 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
179 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
180 return 0;
181 }
182
183 /*
184 * RFC 6066: The negotiated length applies for the duration of the session
185 * including session resumptions.
186 * We should receive the same code as in resumed session !
187 */
188 if (s->hit && s->session->ext.max_fragment_len_mode != value) {
189 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
190 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
191 return 0;
192 }
193
194 /*
195 * Store it in session, so it'll become binding for us
196 * and we'll include it in a next Server Hello.
197 */
198 s->session->ext.max_fragment_len_mode = value;
199 return 1;
200 }
201
202 #ifndef OPENSSL_NO_SRP
203 int tls_parse_ctos_srp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
204 size_t chainidx)
205 {
206 PACKET srp_I;
207
208 if (!PACKET_as_length_prefixed_1(pkt, &srp_I)
209 || PACKET_contains_zero_byte(&srp_I)) {
210 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
211 return 0;
212 }
213
214 if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
215 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
216 return 0;
217 }
218
219 return 1;
220 }
221 #endif
222
223 int tls_parse_ctos_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
224 X509 *x, size_t chainidx)
225 {
226 PACKET ec_point_format_list;
227
228 if (!PACKET_as_length_prefixed_1(pkt, &ec_point_format_list)
229 || PACKET_remaining(&ec_point_format_list) == 0) {
230 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
231 return 0;
232 }
233
234 if (!s->hit) {
235 if (!PACKET_memdup(&ec_point_format_list,
236 &s->ext.peer_ecpointformats,
237 &s->ext.peer_ecpointformats_len)) {
238 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
239 return 0;
240 }
241 }
242
243 return 1;
244 }
245
246 int tls_parse_ctos_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
247 X509 *x, size_t chainidx)
248 {
249 if (s->ext.session_ticket_cb &&
250 !s->ext.session_ticket_cb(s, PACKET_data(pkt),
251 PACKET_remaining(pkt),
252 s->ext.session_ticket_cb_arg)) {
253 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
254 return 0;
255 }
256
257 return 1;
258 }
259
260 int tls_parse_ctos_sig_algs_cert(SSL *s, PACKET *pkt,
261 ossl_unused unsigned int context,
262 ossl_unused X509 *x,
263 ossl_unused size_t chainidx)
264 {
265 PACKET supported_sig_algs;
266
267 if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
268 || PACKET_remaining(&supported_sig_algs) == 0) {
269 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
270 return 0;
271 }
272
273 if (!s->hit && !tls1_save_sigalgs(s, &supported_sig_algs, 1)) {
274 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
275 return 0;
276 }
277
278 return 1;
279 }
280
281 int tls_parse_ctos_sig_algs(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
282 size_t chainidx)
283 {
284 PACKET supported_sig_algs;
285
286 if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
287 || PACKET_remaining(&supported_sig_algs) == 0) {
288 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
289 return 0;
290 }
291
292 if (!s->hit && !tls1_save_sigalgs(s, &supported_sig_algs, 0)) {
293 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
294 return 0;
295 }
296
297 return 1;
298 }
299
300 #ifndef OPENSSL_NO_OCSP
301 int tls_parse_ctos_status_request(SSL *s, PACKET *pkt, unsigned int context,
302 X509 *x, size_t chainidx)
303 {
304 PACKET responder_id_list, exts;
305
306 /* We ignore this in a resumption handshake */
307 if (s->hit)
308 return 1;
309
310 /* Not defined if we get one of these in a client Certificate */
311 if (x != NULL)
312 return 1;
313
314 if (!PACKET_get_1(pkt, (unsigned int *)&s->ext.status_type)) {
315 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
316 return 0;
317 }
318
319 if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
320 /*
321 * We don't know what to do with any other type so ignore it.
322 */
323 s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
324 return 1;
325 }
326
327 if (!PACKET_get_length_prefixed_2 (pkt, &responder_id_list)) {
328 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
329 return 0;
330 }
331
332 /*
333 * We remove any OCSP_RESPIDs from a previous handshake
334 * to prevent unbounded memory growth - CVE-2016-6304
335 */
336 sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
337 if (PACKET_remaining(&responder_id_list) > 0) {
338 s->ext.ocsp.ids = sk_OCSP_RESPID_new_null();
339 if (s->ext.ocsp.ids == NULL) {
340 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
341 return 0;
342 }
343 } else {
344 s->ext.ocsp.ids = NULL;
345 }
346
347 while (PACKET_remaining(&responder_id_list) > 0) {
348 OCSP_RESPID *id;
349 PACKET responder_id;
350 const unsigned char *id_data;
351
352 if (!PACKET_get_length_prefixed_2(&responder_id_list, &responder_id)
353 || PACKET_remaining(&responder_id) == 0) {
354 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
355 return 0;
356 }
357
358 id_data = PACKET_data(&responder_id);
359 id = d2i_OCSP_RESPID(NULL, &id_data,
360 (int)PACKET_remaining(&responder_id));
361 if (id == NULL) {
362 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
363 return 0;
364 }
365
366 if (id_data != PACKET_end(&responder_id)) {
367 OCSP_RESPID_free(id);
368 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
369
370 return 0;
371 }
372
373 if (!sk_OCSP_RESPID_push(s->ext.ocsp.ids, id)) {
374 OCSP_RESPID_free(id);
375 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
376
377 return 0;
378 }
379 }
380
381 /* Read in request_extensions */
382 if (!PACKET_as_length_prefixed_2(pkt, &exts)) {
383 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
384 return 0;
385 }
386
387 if (PACKET_remaining(&exts) > 0) {
388 const unsigned char *ext_data = PACKET_data(&exts);
389
390 sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts,
391 X509_EXTENSION_free);
392 s->ext.ocsp.exts =
393 d2i_X509_EXTENSIONS(NULL, &ext_data, (int)PACKET_remaining(&exts));
394 if (s->ext.ocsp.exts == NULL || ext_data != PACKET_end(&exts)) {
395 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
396 return 0;
397 }
398 }
399
400 return 1;
401 }
402 #endif
403
404 #ifndef OPENSSL_NO_NEXTPROTONEG
405 int tls_parse_ctos_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
406 size_t chainidx)
407 {
408 /*
409 * We shouldn't accept this extension on a
410 * renegotiation.
411 */
412 if (SSL_IS_FIRST_HANDSHAKE(s))
413 s->s3.npn_seen = 1;
414
415 return 1;
416 }
417 #endif
418
419 /*
420 * Save the ALPN extension in a ClientHello.|pkt| holds the contents of the ALPN
421 * extension, not including type and length. Returns: 1 on success, 0 on error.
422 */
423 int tls_parse_ctos_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
424 size_t chainidx)
425 {
426 PACKET protocol_list, save_protocol_list, protocol;
427
428 if (!SSL_IS_FIRST_HANDSHAKE(s))
429 return 1;
430
431 if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
432 || PACKET_remaining(&protocol_list) < 2) {
433 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
434 return 0;
435 }
436
437 save_protocol_list = protocol_list;
438 do {
439 /* Protocol names can't be empty. */
440 if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
441 || PACKET_remaining(&protocol) == 0) {
442 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
443 return 0;
444 }
445 } while (PACKET_remaining(&protocol_list) != 0);
446
447 OPENSSL_free(s->s3.alpn_proposed);
448 s->s3.alpn_proposed = NULL;
449 s->s3.alpn_proposed_len = 0;
450 if (!PACKET_memdup(&save_protocol_list,
451 &s->s3.alpn_proposed, &s->s3.alpn_proposed_len)) {
452 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
453 return 0;
454 }
455
456 return 1;
457 }
458
459 #ifndef OPENSSL_NO_SRTP
460 int tls_parse_ctos_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
461 size_t chainidx)
462 {
463 STACK_OF(SRTP_PROTECTION_PROFILE) *srvr;
464 unsigned int ct, mki_len, id;
465 int i, srtp_pref;
466 PACKET subpkt;
467
468 /* Ignore this if we have no SRTP profiles */
469 if (SSL_get_srtp_profiles(s) == NULL)
470 return 1;
471
472 /* Pull off the length of the cipher suite list and check it is even */
473 if (!PACKET_get_net_2(pkt, &ct) || (ct & 1) != 0
474 || !PACKET_get_sub_packet(pkt, &subpkt, ct)) {
475 SSLfatal(s, SSL_AD_DECODE_ERROR,
476 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
477 return 0;
478 }
479
480 srvr = SSL_get_srtp_profiles(s);
481 s->srtp_profile = NULL;
482 /* Search all profiles for a match initially */
483 srtp_pref = sk_SRTP_PROTECTION_PROFILE_num(srvr);
484
485 while (PACKET_remaining(&subpkt)) {
486 if (!PACKET_get_net_2(&subpkt, &id)) {
487 SSLfatal(s, SSL_AD_DECODE_ERROR,
488 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
489 return 0;
490 }
491
492 /*
493 * Only look for match in profiles of higher preference than
494 * current match.
495 * If no profiles have been have been configured then this
496 * does nothing.
497 */
498 for (i = 0; i < srtp_pref; i++) {
499 SRTP_PROTECTION_PROFILE *sprof =
500 sk_SRTP_PROTECTION_PROFILE_value(srvr, i);
501
502 if (sprof->id == id) {
503 s->srtp_profile = sprof;
504 srtp_pref = i;
505 break;
506 }
507 }
508 }
509
510 /* Now extract the MKI value as a sanity check, but discard it for now */
511 if (!PACKET_get_1(pkt, &mki_len)) {
512 SSLfatal(s, SSL_AD_DECODE_ERROR,
513 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
514 return 0;
515 }
516
517 if (!PACKET_forward(pkt, mki_len)
518 || PACKET_remaining(pkt)) {
519 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRTP_MKI_VALUE);
520 return 0;
521 }
522
523 return 1;
524 }
525 #endif
526
527 int tls_parse_ctos_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
528 size_t chainidx)
529 {
530 if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))
531 s->ext.use_etm = 1;
532
533 return 1;
534 }
535
536 /*
537 * Process a psk_kex_modes extension received in the ClientHello. |pkt| contains
538 * the raw PACKET data for the extension. Returns 1 on success or 0 on failure.
539 */
540 int tls_parse_ctos_psk_kex_modes(SSL *s, PACKET *pkt, unsigned int context,
541 X509 *x, size_t chainidx)
542 {
543 #ifndef OPENSSL_NO_TLS1_3
544 PACKET psk_kex_modes;
545 unsigned int mode;
546
547 if (!PACKET_as_length_prefixed_1(pkt, &psk_kex_modes)
548 || PACKET_remaining(&psk_kex_modes) == 0) {
549 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
550 return 0;
551 }
552
553 while (PACKET_get_1(&psk_kex_modes, &mode)) {
554 if (mode == TLSEXT_KEX_MODE_KE_DHE)
555 s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE_DHE;
556 else if (mode == TLSEXT_KEX_MODE_KE
557 && (s->options & SSL_OP_ALLOW_NO_DHE_KEX) != 0)
558 s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
559 }
560 #endif
561
562 return 1;
563 }
564
565 /*
566 * Process a key_share extension received in the ClientHello. |pkt| contains
567 * the raw PACKET data for the extension. Returns 1 on success or 0 on failure.
568 */
569 int tls_parse_ctos_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
570 size_t chainidx)
571 {
572 #ifndef OPENSSL_NO_TLS1_3
573 unsigned int group_id;
574 PACKET key_share_list, encoded_pt;
575 const uint16_t *clntgroups, *srvrgroups;
576 size_t clnt_num_groups, srvr_num_groups;
577 int found = 0;
578
579 if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0)
580 return 1;
581
582 /* Sanity check */
583 if (s->s3.peer_tmp != NULL) {
584 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
585 return 0;
586 }
587
588 if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) {
589 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
590 return 0;
591 }
592
593 /* Get our list of supported groups */
594 tls1_get_supported_groups(s, &srvrgroups, &srvr_num_groups);
595 /* Get the clients list of supported groups. */
596 tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups);
597 if (clnt_num_groups == 0) {
598 /*
599 * This can only happen if the supported_groups extension was not sent,
600 * because we verify that the length is non-zero when we process that
601 * extension.
602 */
603 SSLfatal(s, SSL_AD_MISSING_EXTENSION,
604 SSL_R_MISSING_SUPPORTED_GROUPS_EXTENSION);
605 return 0;
606 }
607
608 if (s->s3.group_id != 0 && PACKET_remaining(&key_share_list) == 0) {
609 /*
610 * If we set a group_id already, then we must have sent an HRR
611 * requesting a new key_share. If we haven't got one then that is an
612 * error
613 */
614 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
615 return 0;
616 }
617
618 while (PACKET_remaining(&key_share_list) > 0) {
619 if (!PACKET_get_net_2(&key_share_list, &group_id)
620 || !PACKET_get_length_prefixed_2(&key_share_list, &encoded_pt)
621 || PACKET_remaining(&encoded_pt) == 0) {
622 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
623 return 0;
624 }
625
626 /*
627 * If we already found a suitable key_share we loop through the
628 * rest to verify the structure, but don't process them.
629 */
630 if (found)
631 continue;
632
633 /*
634 * If we sent an HRR then the key_share sent back MUST be for the group
635 * we requested, and must be the only key_share sent.
636 */
637 if (s->s3.group_id != 0
638 && (ssl_group_id_tls13_to_internal(group_id) != s->s3.group_id
639 || PACKET_remaining(&key_share_list) != 0)) {
640 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
641 return 0;
642 }
643
644 /* Check if this share is in supported_groups sent from client */
645 if (!check_in_list(s, group_id, clntgroups, clnt_num_groups, 0)) {
646 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
647 return 0;
648 }
649
650 /* Check if this share is for a group we can use */
651 if (!check_in_list(s, group_id, srvrgroups, srvr_num_groups, 1)) {
652 /* Share not suitable */
653 continue;
654 }
655
656 s->s3.group_id = group_id;
657 /* Cache the selected group ID in the SSL_SESSION */
658 s->session->kex_group = group_id;
659
660 group_id = ssl_group_id_tls13_to_internal(group_id);
661
662 if ((s->s3.peer_tmp = ssl_generate_param_group(s, group_id)) == NULL) {
663 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
664 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
665 return 0;
666 }
667
668 if (EVP_PKEY_set1_encoded_public_key(s->s3.peer_tmp,
669 PACKET_data(&encoded_pt),
670 PACKET_remaining(&encoded_pt)) <= 0) {
671 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT);
672 return 0;
673 }
674
675 found = 1;
676 }
677 #endif
678
679 return 1;
680 }
681
682 int tls_parse_ctos_cookie(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
683 size_t chainidx)
684 {
685 #ifndef OPENSSL_NO_TLS1_3
686 unsigned int format, version, key_share, group_id;
687 EVP_MD_CTX *hctx;
688 EVP_PKEY *pkey;
689 PACKET cookie, raw, chhash, appcookie;
690 WPACKET hrrpkt;
691 const unsigned char *data, *mdin, *ciphdata;
692 unsigned char hmac[SHA256_DIGEST_LENGTH];
693 unsigned char hrr[MAX_HRR_SIZE];
694 size_t rawlen, hmaclen, hrrlen, ciphlen;
695 unsigned long tm, now;
696
697 /* Ignore any cookie if we're not set up to verify it */
698 if (s->ctx->verify_stateless_cookie_cb == NULL
699 || (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
700 return 1;
701
702 if (!PACKET_as_length_prefixed_2(pkt, &cookie)) {
703 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
704 return 0;
705 }
706
707 raw = cookie;
708 data = PACKET_data(&raw);
709 rawlen = PACKET_remaining(&raw);
710 if (rawlen < SHA256_DIGEST_LENGTH
711 || !PACKET_forward(&raw, rawlen - SHA256_DIGEST_LENGTH)) {
712 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
713 return 0;
714 }
715 mdin = PACKET_data(&raw);
716
717 /* Verify the HMAC of the cookie */
718 hctx = EVP_MD_CTX_create();
719 pkey = EVP_PKEY_new_raw_private_key_ex(s->ctx->libctx, "HMAC",
720 s->ctx->propq,
721 s->session_ctx->ext.cookie_hmac_key,
722 sizeof(s->session_ctx->ext.cookie_hmac_key));
723 if (hctx == NULL || pkey == NULL) {
724 EVP_MD_CTX_free(hctx);
725 EVP_PKEY_free(pkey);
726 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
727 return 0;
728 }
729
730 hmaclen = SHA256_DIGEST_LENGTH;
731 if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", s->ctx->libctx,
732 s->ctx->propq, pkey, NULL) <= 0
733 || EVP_DigestSign(hctx, hmac, &hmaclen, data,
734 rawlen - SHA256_DIGEST_LENGTH) <= 0
735 || hmaclen != SHA256_DIGEST_LENGTH) {
736 EVP_MD_CTX_free(hctx);
737 EVP_PKEY_free(pkey);
738 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
739 return 0;
740 }
741
742 EVP_MD_CTX_free(hctx);
743 EVP_PKEY_free(pkey);
744
745 if (CRYPTO_memcmp(hmac, mdin, SHA256_DIGEST_LENGTH) != 0) {
746 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COOKIE_MISMATCH);
747 return 0;
748 }
749
750 if (!PACKET_get_net_2(&cookie, &format)) {
751 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
752 return 0;
753 }
754 /* Check the cookie format is something we recognise. Ignore it if not */
755 if (format != COOKIE_STATE_FORMAT_VERSION)
756 return 1;
757
758 /*
759 * The rest of these checks really shouldn't fail since we have verified the
760 * HMAC above.
761 */
762
763 /* Check the version number is sane */
764 if (!PACKET_get_net_2(&cookie, &version)) {
765 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
766 return 0;
767 }
768 if (version != TLS1_3_VERSION) {
769 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
770 SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
771 return 0;
772 }
773
774 if (!PACKET_get_net_2(&cookie, &group_id)) {
775 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
776 return 0;
777 }
778
779 ciphdata = PACKET_data(&cookie);
780 if (!PACKET_forward(&cookie, 2)) {
781 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
782 return 0;
783 }
784 if (group_id != s->s3.group_id
785 || s->s3.tmp.new_cipher
786 != ssl_get_cipher_by_char(s, ciphdata, 0)) {
787 /*
788 * We chose a different cipher or group id this time around to what is
789 * in the cookie. Something must have changed.
790 */
791 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER);
792 return 0;
793 }
794
795 if (!PACKET_get_1(&cookie, &key_share)
796 || !PACKET_get_net_4(&cookie, &tm)
797 || !PACKET_get_length_prefixed_2(&cookie, &chhash)
798 || !PACKET_get_length_prefixed_1(&cookie, &appcookie)
799 || PACKET_remaining(&cookie) != SHA256_DIGEST_LENGTH) {
800 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
801 return 0;
802 }
803
804 /* We tolerate a cookie age of up to 10 minutes (= 60 * 10 seconds) */
805 now = (unsigned long)time(NULL);
806 if (tm > now || (now - tm) > 600) {
807 /* Cookie is stale. Ignore it */
808 return 1;
809 }
810
811 /* Verify the app cookie */
812 if (s->ctx->verify_stateless_cookie_cb(s, PACKET_data(&appcookie),
813 PACKET_remaining(&appcookie)) == 0) {
814 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COOKIE_MISMATCH);
815 return 0;
816 }
817
818 /*
819 * Reconstruct the HRR that we would have sent in response to the original
820 * ClientHello so we can add it to the transcript hash.
821 * Note: This won't work with custom HRR extensions
822 */
823 if (!WPACKET_init_static_len(&hrrpkt, hrr, sizeof(hrr), 0)) {
824 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
825 return 0;
826 }
827 if (!WPACKET_put_bytes_u8(&hrrpkt, SSL3_MT_SERVER_HELLO)
828 || !WPACKET_start_sub_packet_u24(&hrrpkt)
829 || !WPACKET_put_bytes_u16(&hrrpkt, TLS1_2_VERSION)
830 || !WPACKET_memcpy(&hrrpkt, hrrrandom, SSL3_RANDOM_SIZE)
831 || !WPACKET_sub_memcpy_u8(&hrrpkt, s->tmp_session_id,
832 s->tmp_session_id_len)
833 || !s->method->put_cipher_by_char(s->s3.tmp.new_cipher, &hrrpkt,
834 &ciphlen)
835 || !WPACKET_put_bytes_u8(&hrrpkt, 0)
836 || !WPACKET_start_sub_packet_u16(&hrrpkt)) {
837 WPACKET_cleanup(&hrrpkt);
838 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
839 return 0;
840 }
841 if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_supported_versions)
842 || !WPACKET_start_sub_packet_u16(&hrrpkt)
843 || !WPACKET_put_bytes_u16(&hrrpkt, s->version)
844 || !WPACKET_close(&hrrpkt)) {
845 WPACKET_cleanup(&hrrpkt);
846 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
847 return 0;
848 }
849 if (key_share) {
850 if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_key_share)
851 || !WPACKET_start_sub_packet_u16(&hrrpkt)
852 || !WPACKET_put_bytes_u16(&hrrpkt, s->s3.group_id)
853 || !WPACKET_close(&hrrpkt)) {
854 WPACKET_cleanup(&hrrpkt);
855 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
856 return 0;
857 }
858 }
859 if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_cookie)
860 || !WPACKET_start_sub_packet_u16(&hrrpkt)
861 || !WPACKET_sub_memcpy_u16(&hrrpkt, data, rawlen)
862 || !WPACKET_close(&hrrpkt) /* cookie extension */
863 || !WPACKET_close(&hrrpkt) /* extension block */
864 || !WPACKET_close(&hrrpkt) /* message */
865 || !WPACKET_get_total_written(&hrrpkt, &hrrlen)
866 || !WPACKET_finish(&hrrpkt)) {
867 WPACKET_cleanup(&hrrpkt);
868 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
869 return 0;
870 }
871
872 /* Reconstruct the transcript hash */
873 if (!create_synthetic_message_hash(s, PACKET_data(&chhash),
874 PACKET_remaining(&chhash), hrr,
875 hrrlen)) {
876 /* SSLfatal() already called */
877 return 0;
878 }
879
880 /* Act as if this ClientHello came after a HelloRetryRequest */
881 s->hello_retry_request = 1;
882
883 s->ext.cookieok = 1;
884 #endif
885
886 return 1;
887 }
888
889 int tls_parse_ctos_supported_groups(SSL *s, PACKET *pkt, unsigned int context,
890 X509 *x, size_t chainidx)
891 {
892 PACKET supported_groups_list;
893
894 /* Each group is 2 bytes and we must have at least 1. */
895 if (!PACKET_as_length_prefixed_2(pkt, &supported_groups_list)
896 || PACKET_remaining(&supported_groups_list) == 0
897 || (PACKET_remaining(&supported_groups_list) % 2) != 0) {
898 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
899 return 0;
900 }
901
902 if (!s->hit || SSL_IS_TLS13(s)) {
903 OPENSSL_free(s->ext.peer_supportedgroups);
904 s->ext.peer_supportedgroups = NULL;
905 s->ext.peer_supportedgroups_len = 0;
906 if (!tls1_save_u16(&supported_groups_list,
907 &s->ext.peer_supportedgroups,
908 &s->ext.peer_supportedgroups_len)) {
909 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
910 return 0;
911 }
912 }
913
914 return 1;
915 }
916
917 int tls_parse_ctos_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
918 size_t chainidx)
919 {
920 /* The extension must always be empty */
921 if (PACKET_remaining(pkt) != 0) {
922 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
923 return 0;
924 }
925
926 if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)
927 return 1;
928
929 s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS;
930
931 return 1;
932 }
933
934
935 int tls_parse_ctos_early_data(SSL *s, PACKET *pkt, unsigned int context,
936 X509 *x, size_t chainidx)
937 {
938 if (PACKET_remaining(pkt) != 0) {
939 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
940 return 0;
941 }
942
943 if (s->hello_retry_request != SSL_HRR_NONE) {
944 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
945 return 0;
946 }
947
948 return 1;
949 }
950
951 static SSL_TICKET_STATUS tls_get_stateful_ticket(SSL *s, PACKET *tick,
952 SSL_SESSION **sess)
953 {
954 SSL_SESSION *tmpsess = NULL;
955
956 s->ext.ticket_expected = 1;
957
958 switch (PACKET_remaining(tick)) {
959 case 0:
960 return SSL_TICKET_EMPTY;
961
962 case SSL_MAX_SSL_SESSION_ID_LENGTH:
963 break;
964
965 default:
966 return SSL_TICKET_NO_DECRYPT;
967 }
968
969 tmpsess = lookup_sess_in_cache(s, PACKET_data(tick),
970 SSL_MAX_SSL_SESSION_ID_LENGTH);
971
972 if (tmpsess == NULL)
973 return SSL_TICKET_NO_DECRYPT;
974
975 *sess = tmpsess;
976 return SSL_TICKET_SUCCESS;
977 }
978
979 int tls_parse_ctos_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
980 size_t chainidx)
981 {
982 PACKET identities, binders, binder;
983 size_t binderoffset, hashsize;
984 SSL_SESSION *sess = NULL;
985 unsigned int id, i, ext = 0;
986 const EVP_MD *md = NULL;
987
988 /*
989 * If we have no PSK kex mode that we recognise then we can't resume so
990 * ignore this extension
991 */
992 if ((s->ext.psk_kex_mode
993 & (TLSEXT_KEX_MODE_FLAG_KE | TLSEXT_KEX_MODE_FLAG_KE_DHE)) == 0)
994 return 1;
995
996 if (!PACKET_get_length_prefixed_2(pkt, &identities)) {
997 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
998 return 0;
999 }
1000
1001 s->ext.ticket_expected = 0;
1002 for (id = 0; PACKET_remaining(&identities) != 0; id++) {
1003 PACKET identity;
1004 unsigned long ticket_agel;
1005 size_t idlen;
1006
1007 if (!PACKET_get_length_prefixed_2(&identities, &identity)
1008 || !PACKET_get_net_4(&identities, &ticket_agel)) {
1009 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1010 return 0;
1011 }
1012
1013 idlen = PACKET_remaining(&identity);
1014 if (s->psk_find_session_cb != NULL
1015 && !s->psk_find_session_cb(s, PACKET_data(&identity), idlen,
1016 &sess)) {
1017 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_EXTENSION);
1018 return 0;
1019 }
1020
1021 #ifndef OPENSSL_NO_PSK
1022 if (sess == NULL
1023 && s->psk_server_callback != NULL
1024 && idlen <= PSK_MAX_IDENTITY_LEN) {
1025 char *pskid = NULL;
1026 unsigned char pskdata[PSK_MAX_PSK_LEN];
1027 unsigned int pskdatalen;
1028
1029 if (!PACKET_strndup(&identity, &pskid)) {
1030 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1031 return 0;
1032 }
1033 pskdatalen = s->psk_server_callback(s, pskid, pskdata,
1034 sizeof(pskdata));
1035 OPENSSL_free(pskid);
1036 if (pskdatalen > PSK_MAX_PSK_LEN) {
1037 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1038 return 0;
1039 } else if (pskdatalen > 0) {
1040 const SSL_CIPHER *cipher;
1041 const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
1042
1043 /*
1044 * We found a PSK using an old style callback. We don't know
1045 * the digest so we default to SHA256 as per the TLSv1.3 spec
1046 */
1047 cipher = SSL_CIPHER_find(s, tls13_aes128gcmsha256_id);
1048 if (cipher == NULL) {
1049 OPENSSL_cleanse(pskdata, pskdatalen);
1050 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1051 return 0;
1052 }
1053
1054 sess = SSL_SESSION_new();
1055 if (sess == NULL
1056 || !SSL_SESSION_set1_master_key(sess, pskdata,
1057 pskdatalen)
1058 || !SSL_SESSION_set_cipher(sess, cipher)
1059 || !SSL_SESSION_set_protocol_version(sess,
1060 TLS1_3_VERSION)) {
1061 OPENSSL_cleanse(pskdata, pskdatalen);
1062 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1063 goto err;
1064 }
1065 OPENSSL_cleanse(pskdata, pskdatalen);
1066 }
1067 }
1068 #endif /* OPENSSL_NO_PSK */
1069
1070 if (sess != NULL) {
1071 /* We found a PSK */
1072 SSL_SESSION *sesstmp = ssl_session_dup(sess, 0);
1073
1074 if (sesstmp == NULL) {
1075 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1076 return 0;
1077 }
1078 SSL_SESSION_free(sess);
1079 sess = sesstmp;
1080
1081 /*
1082 * We've just been told to use this session for this context so
1083 * make sure the sid_ctx matches up.
1084 */
1085 memcpy(sess->sid_ctx, s->sid_ctx, s->sid_ctx_length);
1086 sess->sid_ctx_length = s->sid_ctx_length;
1087 ext = 1;
1088 if (id == 0)
1089 s->ext.early_data_ok = 1;
1090 s->ext.ticket_expected = 1;
1091 } else {
1092 uint32_t ticket_age = 0, now, agesec, agems;
1093 int ret;
1094
1095 /*
1096 * If we are using anti-replay protection then we behave as if
1097 * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
1098 * is no point in using full stateless tickets.
1099 */
1100 if ((s->options & SSL_OP_NO_TICKET) != 0
1101 || (s->max_early_data > 0
1102 && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))
1103 ret = tls_get_stateful_ticket(s, &identity, &sess);
1104 else
1105 ret = tls_decrypt_ticket(s, PACKET_data(&identity),
1106 PACKET_remaining(&identity), NULL, 0,
1107 &sess);
1108
1109 if (ret == SSL_TICKET_EMPTY) {
1110 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1111 return 0;
1112 }
1113
1114 if (ret == SSL_TICKET_FATAL_ERR_MALLOC
1115 || ret == SSL_TICKET_FATAL_ERR_OTHER) {
1116 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1117 return 0;
1118 }
1119 if (ret == SSL_TICKET_NONE || ret == SSL_TICKET_NO_DECRYPT)
1120 continue;
1121
1122 /* Check for replay */
1123 if (s->max_early_data > 0
1124 && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0
1125 && !SSL_CTX_remove_session(s->session_ctx, sess)) {
1126 SSL_SESSION_free(sess);
1127 sess = NULL;
1128 continue;
1129 }
1130
1131 ticket_age = (uint32_t)ticket_agel;
1132 now = (uint32_t)time(NULL);
1133 agesec = now - (uint32_t)sess->time;
1134 agems = agesec * (uint32_t)1000;
1135 ticket_age -= sess->ext.tick_age_add;
1136
1137 /*
1138 * For simplicity we do our age calculations in seconds. If the
1139 * client does it in ms then it could appear that their ticket age
1140 * is longer than ours (our ticket age calculation should always be
1141 * slightly longer than the client's due to the network latency).
1142 * Therefore we add 1000ms to our age calculation to adjust for
1143 * rounding errors.
1144 */
1145 if (id == 0
1146 && sess->timeout >= (long)agesec
1147 && agems / (uint32_t)1000 == agesec
1148 && ticket_age <= agems + 1000
1149 && ticket_age + TICKET_AGE_ALLOWANCE >= agems + 1000) {
1150 /*
1151 * Ticket age is within tolerance and not expired. We allow it
1152 * for early data
1153 */
1154 s->ext.early_data_ok = 1;
1155 }
1156 }
1157
1158 md = ssl_md(s->ctx, sess->cipher->algorithm2);
1159 if (!EVP_MD_is_a(md,
1160 EVP_MD_get0_name(ssl_md(s->ctx,
1161 s->s3.tmp.new_cipher->algorithm2)))) {
1162 /* The ciphersuite is not compatible with this session. */
1163 SSL_SESSION_free(sess);
1164 sess = NULL;
1165 s->ext.early_data_ok = 0;
1166 s->ext.ticket_expected = 0;
1167 continue;
1168 }
1169 break;
1170 }
1171
1172 if (sess == NULL)
1173 return 1;
1174
1175 binderoffset = PACKET_data(pkt) - (const unsigned char *)s->init_buf->data;
1176 hashsize = EVP_MD_get_size(md);
1177
1178 if (!PACKET_get_length_prefixed_2(pkt, &binders)) {
1179 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1180 goto err;
1181 }
1182
1183 for (i = 0; i <= id; i++) {
1184 if (!PACKET_get_length_prefixed_1(&binders, &binder)) {
1185 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1186 goto err;
1187 }
1188 }
1189
1190 if (PACKET_remaining(&binder) != hashsize) {
1191 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1192 goto err;
1193 }
1194 if (tls_psk_do_binder(s, md, (const unsigned char *)s->init_buf->data,
1195 binderoffset, PACKET_data(&binder), NULL, sess, 0,
1196 ext) != 1) {
1197 /* SSLfatal() already called */
1198 goto err;
1199 }
1200
1201 s->ext.tick_identity = id;
1202
1203 SSL_SESSION_free(s->session);
1204 s->session = sess;
1205 return 1;
1206 err:
1207 SSL_SESSION_free(sess);
1208 return 0;
1209 }
1210
1211 int tls_parse_ctos_post_handshake_auth(SSL *s, PACKET *pkt,
1212 ossl_unused unsigned int context,
1213 ossl_unused X509 *x,
1214 ossl_unused size_t chainidx)
1215 {
1216 if (PACKET_remaining(pkt) != 0) {
1217 SSLfatal(s, SSL_AD_DECODE_ERROR,
1218 SSL_R_POST_HANDSHAKE_AUTH_ENCODING_ERR);
1219 return 0;
1220 }
1221
1222 s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
1223
1224 return 1;
1225 }
1226
1227 /*
1228 * Add the server's renegotiation binding
1229 */
1230 EXT_RETURN tls_construct_stoc_renegotiate(SSL *s, WPACKET *pkt,
1231 unsigned int context, X509 *x,
1232 size_t chainidx)
1233 {
1234 if (!s->s3.send_connection_binding)
1235 return EXT_RETURN_NOT_SENT;
1236
1237 /* Still add this even if SSL_OP_NO_RENEGOTIATION is set */
1238 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
1239 || !WPACKET_start_sub_packet_u16(pkt)
1240 || !WPACKET_start_sub_packet_u8(pkt)
1241 || !WPACKET_memcpy(pkt, s->s3.previous_client_finished,
1242 s->s3.previous_client_finished_len)
1243 || !WPACKET_memcpy(pkt, s->s3.previous_server_finished,
1244 s->s3.previous_server_finished_len)
1245 || !WPACKET_close(pkt)
1246 || !WPACKET_close(pkt)) {
1247 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1248 return EXT_RETURN_FAIL;
1249 }
1250
1251 return EXT_RETURN_SENT;
1252 }
1253
1254 EXT_RETURN tls_construct_stoc_server_name(SSL *s, WPACKET *pkt,
1255 unsigned int context, X509 *x,
1256 size_t chainidx)
1257 {
1258 if (s->servername_done != 1)
1259 return EXT_RETURN_NOT_SENT;
1260
1261 /*
1262 * Prior to TLSv1.3 we ignore any SNI in the current handshake if resuming.
1263 * We just use the servername from the initial handshake.
1264 */
1265 if (s->hit && !SSL_IS_TLS13(s))
1266 return EXT_RETURN_NOT_SENT;
1267
1268 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
1269 || !WPACKET_put_bytes_u16(pkt, 0)) {
1270 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1271 return EXT_RETURN_FAIL;
1272 }
1273
1274 return EXT_RETURN_SENT;
1275 }
1276
1277 /* Add/include the server's max fragment len extension into ServerHello */
1278 EXT_RETURN tls_construct_stoc_maxfragmentlen(SSL *s, WPACKET *pkt,
1279 unsigned int context, X509 *x,
1280 size_t chainidx)
1281 {
1282 if (!USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
1283 return EXT_RETURN_NOT_SENT;
1284
1285 /*-
1286 * 4 bytes for this extension type and extension length
1287 * 1 byte for the Max Fragment Length code value.
1288 */
1289 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)
1290 || !WPACKET_start_sub_packet_u16(pkt)
1291 || !WPACKET_put_bytes_u8(pkt, s->session->ext.max_fragment_len_mode)
1292 || !WPACKET_close(pkt)) {
1293 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1294 return EXT_RETURN_FAIL;
1295 }
1296
1297 return EXT_RETURN_SENT;
1298 }
1299
1300 EXT_RETURN tls_construct_stoc_ec_pt_formats(SSL *s, WPACKET *pkt,
1301 unsigned int context, X509 *x,
1302 size_t chainidx)
1303 {
1304 unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
1305 unsigned long alg_a = s->s3.tmp.new_cipher->algorithm_auth;
1306 int using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))
1307 && (s->ext.peer_ecpointformats != NULL);
1308 const unsigned char *plist;
1309 size_t plistlen;
1310
1311 if (!using_ecc)
1312 return EXT_RETURN_NOT_SENT;
1313
1314 tls1_get_formatlist(s, &plist, &plistlen);
1315 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
1316 || !WPACKET_start_sub_packet_u16(pkt)
1317 || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)
1318 || !WPACKET_close(pkt)) {
1319 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1320 return EXT_RETURN_FAIL;
1321 }
1322
1323 return EXT_RETURN_SENT;
1324 }
1325
1326 EXT_RETURN tls_construct_stoc_supported_groups(SSL *s, WPACKET *pkt,
1327 unsigned int context, X509 *x,
1328 size_t chainidx)
1329 {
1330 const uint16_t *groups;
1331 size_t numgroups, i, first = 1;
1332 int version;
1333
1334 /* s->s3.group_id is non zero if we accepted a key_share */
1335 if (s->s3.group_id == 0)
1336 return EXT_RETURN_NOT_SENT;
1337
1338 /* Get our list of supported groups */
1339 tls1_get_supported_groups(s, &groups, &numgroups);
1340 if (numgroups == 0) {
1341 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1342 return EXT_RETURN_FAIL;
1343 }
1344
1345 /* Copy group ID if supported */
1346 version = SSL_version(s);
1347 for (i = 0; i < numgroups; i++) {
1348 uint16_t group = groups[i];
1349
1350 if (tls_valid_group(s, group, version, version, 0, NULL)
1351 && tls_group_allowed(s, group, SSL_SECOP_CURVE_SUPPORTED)) {
1352 if (first) {
1353 /*
1354 * Check if the client is already using our preferred group. If
1355 * so we don't need to add this extension
1356 */
1357 if (s->s3.group_id == group)
1358 return EXT_RETURN_NOT_SENT;
1359
1360 /* Add extension header */
1361 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
1362 /* Sub-packet for supported_groups extension */
1363 || !WPACKET_start_sub_packet_u16(pkt)
1364 || !WPACKET_start_sub_packet_u16(pkt)) {
1365 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1366 return EXT_RETURN_FAIL;
1367 }
1368
1369 first = 0;
1370 }
1371 if (!WPACKET_put_bytes_u16(pkt, group)) {
1372 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1373 return EXT_RETURN_FAIL;
1374 }
1375 }
1376 }
1377
1378 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
1379 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1380 return EXT_RETURN_FAIL;
1381 }
1382
1383 return EXT_RETURN_SENT;
1384 }
1385
1386 EXT_RETURN tls_construct_stoc_session_ticket(SSL *s, WPACKET *pkt,
1387 unsigned int context, X509 *x,
1388 size_t chainidx)
1389 {
1390 if (!s->ext.ticket_expected || !tls_use_ticket(s)) {
1391 s->ext.ticket_expected = 0;
1392 return EXT_RETURN_NOT_SENT;
1393 }
1394
1395 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
1396 || !WPACKET_put_bytes_u16(pkt, 0)) {
1397 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1398 return EXT_RETURN_FAIL;
1399 }
1400
1401 return EXT_RETURN_SENT;
1402 }
1403
1404 #ifndef OPENSSL_NO_OCSP
1405 EXT_RETURN tls_construct_stoc_status_request(SSL *s, WPACKET *pkt,
1406 unsigned int context, X509 *x,
1407 size_t chainidx)
1408 {
1409 /* We don't currently support this extension inside a CertificateRequest */
1410 if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST)
1411 return EXT_RETURN_NOT_SENT;
1412
1413 if (!s->ext.status_expected)
1414 return EXT_RETURN_NOT_SENT;
1415
1416 if (SSL_IS_TLS13(s) && chainidx != 0)
1417 return EXT_RETURN_NOT_SENT;
1418
1419 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
1420 || !WPACKET_start_sub_packet_u16(pkt)) {
1421 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1422 return EXT_RETURN_FAIL;
1423 }
1424
1425 /*
1426 * In TLSv1.3 we include the certificate status itself. In <= TLSv1.2 we
1427 * send back an empty extension, with the certificate status appearing as a
1428 * separate message
1429 */
1430 if (SSL_IS_TLS13(s) && !tls_construct_cert_status_body(s, pkt)) {
1431 /* SSLfatal() already called */
1432 return EXT_RETURN_FAIL;
1433 }
1434 if (!WPACKET_close(pkt)) {
1435 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1436 return EXT_RETURN_FAIL;
1437 }
1438
1439 return EXT_RETURN_SENT;
1440 }
1441 #endif
1442
1443 #ifndef OPENSSL_NO_NEXTPROTONEG
1444 EXT_RETURN tls_construct_stoc_next_proto_neg(SSL *s, WPACKET *pkt,
1445 unsigned int context, X509 *x,
1446 size_t chainidx)
1447 {
1448 const unsigned char *npa;
1449 unsigned int npalen;
1450 int ret;
1451 int npn_seen = s->s3.npn_seen;
1452
1453 s->s3.npn_seen = 0;
1454 if (!npn_seen || s->ctx->ext.npn_advertised_cb == NULL)
1455 return EXT_RETURN_NOT_SENT;
1456
1457 ret = s->ctx->ext.npn_advertised_cb(s, &npa, &npalen,
1458 s->ctx->ext.npn_advertised_cb_arg);
1459 if (ret == SSL_TLSEXT_ERR_OK) {
1460 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
1461 || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {
1462 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1463 return EXT_RETURN_FAIL;
1464 }
1465 s->s3.npn_seen = 1;
1466 }
1467
1468 return EXT_RETURN_SENT;
1469 }
1470 #endif
1471
1472 EXT_RETURN tls_construct_stoc_alpn(SSL *s, WPACKET *pkt, unsigned int context,
1473 X509 *x, size_t chainidx)
1474 {
1475 if (s->s3.alpn_selected == NULL)
1476 return EXT_RETURN_NOT_SENT;
1477
1478 if (!WPACKET_put_bytes_u16(pkt,
1479 TLSEXT_TYPE_application_layer_protocol_negotiation)
1480 || !WPACKET_start_sub_packet_u16(pkt)
1481 || !WPACKET_start_sub_packet_u16(pkt)
1482 || !WPACKET_sub_memcpy_u8(pkt, s->s3.alpn_selected,
1483 s->s3.alpn_selected_len)
1484 || !WPACKET_close(pkt)
1485 || !WPACKET_close(pkt)) {
1486 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1487 return EXT_RETURN_FAIL;
1488 }
1489
1490 return EXT_RETURN_SENT;
1491 }
1492
1493 #ifndef OPENSSL_NO_SRTP
1494 EXT_RETURN tls_construct_stoc_use_srtp(SSL *s, WPACKET *pkt,
1495 unsigned int context, X509 *x,
1496 size_t chainidx)
1497 {
1498 if (s->srtp_profile == NULL)
1499 return EXT_RETURN_NOT_SENT;
1500
1501 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
1502 || !WPACKET_start_sub_packet_u16(pkt)
1503 || !WPACKET_put_bytes_u16(pkt, 2)
1504 || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)
1505 || !WPACKET_put_bytes_u8(pkt, 0)
1506 || !WPACKET_close(pkt)) {
1507 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1508 return EXT_RETURN_FAIL;
1509 }
1510
1511 return EXT_RETURN_SENT;
1512 }
1513 #endif
1514
1515 EXT_RETURN tls_construct_stoc_etm(SSL *s, WPACKET *pkt, unsigned int context,
1516 X509 *x, size_t chainidx)
1517 {
1518 if (!s->ext.use_etm)
1519 return EXT_RETURN_NOT_SENT;
1520
1521 /*
1522 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1523 * for other cases too.
1524 */
1525 if (s->s3.tmp.new_cipher->algorithm_mac == SSL_AEAD
1526 || s->s3.tmp.new_cipher->algorithm_enc == SSL_RC4
1527 || s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1528 || s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12
1529 || s->s3.tmp.new_cipher->algorithm_enc == SSL_MAGMA
1530 || s->s3.tmp.new_cipher->algorithm_enc == SSL_KUZNYECHIK) {
1531 s->ext.use_etm = 0;
1532 return EXT_RETURN_NOT_SENT;
1533 }
1534
1535 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
1536 || !WPACKET_put_bytes_u16(pkt, 0)) {
1537 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1538 return EXT_RETURN_FAIL;
1539 }
1540
1541 return EXT_RETURN_SENT;
1542 }
1543
1544 EXT_RETURN tls_construct_stoc_ems(SSL *s, WPACKET *pkt, unsigned int context,
1545 X509 *x, size_t chainidx)
1546 {
1547 if ((s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0)
1548 return EXT_RETURN_NOT_SENT;
1549
1550 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
1551 || !WPACKET_put_bytes_u16(pkt, 0)) {
1552 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1553 return EXT_RETURN_FAIL;
1554 }
1555
1556 return EXT_RETURN_SENT;
1557 }
1558
1559 EXT_RETURN tls_construct_stoc_supported_versions(SSL *s, WPACKET *pkt,
1560 unsigned int context, X509 *x,
1561 size_t chainidx)
1562 {
1563 if (!ossl_assert(SSL_IS_TLS13(s))) {
1564 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1565 return EXT_RETURN_FAIL;
1566 }
1567
1568 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
1569 || !WPACKET_start_sub_packet_u16(pkt)
1570 || !WPACKET_put_bytes_u16(pkt, s->version)
1571 || !WPACKET_close(pkt)) {
1572 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1573 return EXT_RETURN_FAIL;
1574 }
1575
1576 return EXT_RETURN_SENT;
1577 }
1578
1579 EXT_RETURN tls_construct_stoc_key_share(SSL *s, WPACKET *pkt,
1580 unsigned int context, X509 *x,
1581 size_t chainidx)
1582 {
1583 #ifndef OPENSSL_NO_TLS1_3
1584 unsigned char *encodedPoint;
1585 size_t encoded_pt_len = 0;
1586 EVP_PKEY *ckey = s->s3.peer_tmp, *skey = NULL;
1587 const TLS_GROUP_INFO *ginf = NULL;
1588
1589 if (s->hello_retry_request == SSL_HRR_PENDING) {
1590 if (ckey != NULL) {
1591 /* Original key_share was acceptable so don't ask for another one */
1592 return EXT_RETURN_NOT_SENT;
1593 }
1594 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
1595 || !WPACKET_start_sub_packet_u16(pkt)
1596 || !WPACKET_put_bytes_u16(pkt, ssl_group_id_internal_to_tls13(
1597 s->s3.group_id))
1598 || !WPACKET_close(pkt)) {
1599 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1600 return EXT_RETURN_FAIL;
1601 }
1602
1603 return EXT_RETURN_SENT;
1604 }
1605
1606 if (ckey == NULL) {
1607 /* No key_share received from client - must be resuming */
1608 if (!s->hit || !tls13_generate_handshake_secret(s, NULL, 0)) {
1609 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1610 return EXT_RETURN_FAIL;
1611 }
1612 return EXT_RETURN_NOT_SENT;
1613 }
1614 if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0) {
1615 /*
1616 * PSK ('hit') and explicitly not doing DHE (if the client sent the
1617 * DHE option we always take it); don't send key share.
1618 */
1619 return EXT_RETURN_NOT_SENT;
1620 }
1621
1622 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
1623 || !WPACKET_start_sub_packet_u16(pkt)
1624 || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)) {
1625 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1626 return EXT_RETURN_FAIL;
1627 }
1628
1629 if ((ginf = tls1_group_id_lookup(s->ctx, s->s3.group_id)) == NULL) {
1630 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1631 return EXT_RETURN_FAIL;
1632 }
1633
1634 if (!ginf->is_kem) {
1635 /* Regular KEX */
1636 skey = ssl_generate_pkey(s, ckey);
1637 if (skey == NULL) {
1638 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
1639 return EXT_RETURN_FAIL;
1640 }
1641
1642 /* Generate encoding of server key */
1643 encoded_pt_len = EVP_PKEY_get1_encoded_public_key(skey, &encodedPoint);
1644 if (encoded_pt_len == 0) {
1645 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
1646 EVP_PKEY_free(skey);
1647 return EXT_RETURN_FAIL;
1648 }
1649
1650 if (!WPACKET_sub_memcpy_u16(pkt, encodedPoint, encoded_pt_len)
1651 || !WPACKET_close(pkt)) {
1652 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1653 EVP_PKEY_free(skey);
1654 OPENSSL_free(encodedPoint);
1655 return EXT_RETURN_FAIL;
1656 }
1657 OPENSSL_free(encodedPoint);
1658
1659 /*
1660 * This causes the crypto state to be updated based on the derived keys
1661 */
1662 s->s3.tmp.pkey = skey;
1663 if (ssl_derive(s, skey, ckey, 1) == 0) {
1664 /* SSLfatal() already called */
1665 return EXT_RETURN_FAIL;
1666 }
1667 } else {
1668 /* KEM mode */
1669 unsigned char *ct = NULL;
1670 size_t ctlen = 0;
1671
1672 /*
1673 * This does not update the crypto state.
1674 *
1675 * The generated pms is stored in `s->s3.tmp.pms` to be later used via
1676 * ssl_gensecret().
1677 */
1678 if (ssl_encapsulate(s, ckey, &ct, &ctlen, 0) == 0) {
1679 /* SSLfatal() already called */
1680 return EXT_RETURN_FAIL;
1681 }
1682
1683 if (ctlen == 0) {
1684 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1685 OPENSSL_free(ct);
1686 return EXT_RETURN_FAIL;
1687 }
1688
1689 if (!WPACKET_sub_memcpy_u16(pkt, ct, ctlen)
1690 || !WPACKET_close(pkt)) {
1691 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1692 OPENSSL_free(ct);
1693 return EXT_RETURN_FAIL;
1694 }
1695 OPENSSL_free(ct);
1696
1697 /*
1698 * This causes the crypto state to be updated based on the generated pms
1699 */
1700 if (ssl_gensecret(s, s->s3.tmp.pms, s->s3.tmp.pmslen) == 0) {
1701 /* SSLfatal() already called */
1702 return EXT_RETURN_FAIL;
1703 }
1704 }
1705 s->s3.did_kex = 1;
1706 return EXT_RETURN_SENT;
1707 #else
1708 return EXT_RETURN_FAIL;
1709 #endif
1710 }
1711
1712 EXT_RETURN tls_construct_stoc_cookie(SSL *s, WPACKET *pkt, unsigned int context,
1713 X509 *x, size_t chainidx)
1714 {
1715 #ifndef OPENSSL_NO_TLS1_3
1716 unsigned char *hashval1, *hashval2, *appcookie1, *appcookie2, *cookie;
1717 unsigned char *hmac, *hmac2;
1718 size_t startlen, ciphlen, totcookielen, hashlen, hmaclen, appcookielen;
1719 EVP_MD_CTX *hctx;
1720 EVP_PKEY *pkey;
1721 int ret = EXT_RETURN_FAIL;
1722
1723 if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
1724 return EXT_RETURN_NOT_SENT;
1725
1726 if (s->ctx->gen_stateless_cookie_cb == NULL) {
1727 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_COOKIE_CALLBACK_SET);
1728 return EXT_RETURN_FAIL;
1729 }
1730
1731 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
1732 || !WPACKET_start_sub_packet_u16(pkt)
1733 || !WPACKET_start_sub_packet_u16(pkt)
1734 || !WPACKET_get_total_written(pkt, &startlen)
1735 || !WPACKET_reserve_bytes(pkt, MAX_COOKIE_SIZE, &cookie)
1736 || !WPACKET_put_bytes_u16(pkt, COOKIE_STATE_FORMAT_VERSION)
1737 || !WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION)
1738 || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)
1739 || !s->method->put_cipher_by_char(s->s3.tmp.new_cipher, pkt,
1740 &ciphlen)
1741 /* Is there a key_share extension present in this HRR? */
1742 || !WPACKET_put_bytes_u8(pkt, s->s3.peer_tmp == NULL)
1743 || !WPACKET_put_bytes_u32(pkt, (unsigned int)time(NULL))
1744 || !WPACKET_start_sub_packet_u16(pkt)
1745 || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &hashval1)) {
1746 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1747 return EXT_RETURN_FAIL;
1748 }
1749
1750 /*
1751 * Get the hash of the initial ClientHello. ssl_handshake_hash() operates
1752 * on raw buffers, so we first reserve sufficient bytes (above) and then
1753 * subsequently allocate them (below)
1754 */
1755 if (!ssl3_digest_cached_records(s, 0)
1756 || !ssl_handshake_hash(s, hashval1, EVP_MAX_MD_SIZE, &hashlen)) {
1757 /* SSLfatal() already called */
1758 return EXT_RETURN_FAIL;
1759 }
1760
1761 if (!WPACKET_allocate_bytes(pkt, hashlen, &hashval2)
1762 || !ossl_assert(hashval1 == hashval2)
1763 || !WPACKET_close(pkt)
1764 || !WPACKET_start_sub_packet_u8(pkt)
1765 || !WPACKET_reserve_bytes(pkt, SSL_COOKIE_LENGTH, &appcookie1)) {
1766 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1767 return EXT_RETURN_FAIL;
1768 }
1769
1770 /* Generate the application cookie */
1771 if (s->ctx->gen_stateless_cookie_cb(s, appcookie1, &appcookielen) == 0) {
1772 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
1773 return EXT_RETURN_FAIL;
1774 }
1775
1776 if (!WPACKET_allocate_bytes(pkt, appcookielen, &appcookie2)
1777 || !ossl_assert(appcookie1 == appcookie2)
1778 || !WPACKET_close(pkt)
1779 || !WPACKET_get_total_written(pkt, &totcookielen)
1780 || !WPACKET_reserve_bytes(pkt, SHA256_DIGEST_LENGTH, &hmac)) {
1781 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1782 return EXT_RETURN_FAIL;
1783 }
1784 hmaclen = SHA256_DIGEST_LENGTH;
1785
1786 totcookielen -= startlen;
1787 if (!ossl_assert(totcookielen <= MAX_COOKIE_SIZE - SHA256_DIGEST_LENGTH)) {
1788 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1789 return EXT_RETURN_FAIL;
1790 }
1791
1792 /* HMAC the cookie */
1793 hctx = EVP_MD_CTX_create();
1794 pkey = EVP_PKEY_new_raw_private_key_ex(s->ctx->libctx, "HMAC",
1795 s->ctx->propq,
1796 s->session_ctx->ext.cookie_hmac_key,
1797 sizeof(s->session_ctx->ext.cookie_hmac_key));
1798 if (hctx == NULL || pkey == NULL) {
1799 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
1800 goto err;
1801 }
1802
1803 if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", s->ctx->libctx,
1804 s->ctx->propq, pkey, NULL) <= 0
1805 || EVP_DigestSign(hctx, hmac, &hmaclen, cookie,
1806 totcookielen) <= 0) {
1807 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1808 goto err;
1809 }
1810
1811 if (!ossl_assert(totcookielen + hmaclen <= MAX_COOKIE_SIZE)) {
1812 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1813 goto err;
1814 }
1815
1816 if (!WPACKET_allocate_bytes(pkt, hmaclen, &hmac2)
1817 || !ossl_assert(hmac == hmac2)
1818 || !ossl_assert(cookie == hmac - totcookielen)
1819 || !WPACKET_close(pkt)
1820 || !WPACKET_close(pkt)) {
1821 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1822 goto err;
1823 }
1824
1825 ret = EXT_RETURN_SENT;
1826
1827 err:
1828 EVP_MD_CTX_free(hctx);
1829 EVP_PKEY_free(pkey);
1830 return ret;
1831 #else
1832 return EXT_RETURN_FAIL;
1833 #endif
1834 }
1835
1836 EXT_RETURN tls_construct_stoc_cryptopro_bug(SSL *s, WPACKET *pkt,
1837 unsigned int context, X509 *x,
1838 size_t chainidx)
1839 {
1840 const unsigned char cryptopro_ext[36] = {
1841 0xfd, 0xe8, /* 65000 */
1842 0x00, 0x20, /* 32 bytes length */
1843 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
1844 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
1845 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
1846 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
1847 };
1848
1849 if (((s->s3.tmp.new_cipher->id & 0xFFFF) != 0x80
1850 && (s->s3.tmp.new_cipher->id & 0xFFFF) != 0x81)
1851 || (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG) == 0)
1852 return EXT_RETURN_NOT_SENT;
1853
1854 if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {
1855 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1856 return EXT_RETURN_FAIL;
1857 }
1858
1859 return EXT_RETURN_SENT;
1860 }
1861
1862 EXT_RETURN tls_construct_stoc_early_data(SSL *s, WPACKET *pkt,
1863 unsigned int context, X509 *x,
1864 size_t chainidx)
1865 {
1866 if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
1867 if (s->max_early_data == 0)
1868 return EXT_RETURN_NOT_SENT;
1869
1870 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
1871 || !WPACKET_start_sub_packet_u16(pkt)
1872 || !WPACKET_put_bytes_u32(pkt, s->max_early_data)
1873 || !WPACKET_close(pkt)) {
1874 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1875 return EXT_RETURN_FAIL;
1876 }
1877
1878 return EXT_RETURN_SENT;
1879 }
1880
1881 if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
1882 return EXT_RETURN_NOT_SENT;
1883
1884 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
1885 || !WPACKET_start_sub_packet_u16(pkt)
1886 || !WPACKET_close(pkt)) {
1887 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1888 return EXT_RETURN_FAIL;
1889 }
1890
1891 return EXT_RETURN_SENT;
1892 }
1893
1894 EXT_RETURN tls_construct_stoc_psk(SSL *s, WPACKET *pkt, unsigned int context,
1895 X509 *x, size_t chainidx)
1896 {
1897 if (!s->hit)
1898 return EXT_RETURN_NOT_SENT;
1899
1900 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
1901 || !WPACKET_start_sub_packet_u16(pkt)
1902 || !WPACKET_put_bytes_u16(pkt, s->ext.tick_identity)
1903 || !WPACKET_close(pkt)) {
1904 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1905 return EXT_RETURN_FAIL;
1906 }
1907
1908 return EXT_RETURN_SENT;
1909 }