Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/ssl/statem/extensions_srvr.c

raw
1 /*
2 * Copyright 2016-2026 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 #include "internal/ssl_unwrap.h"
15
16 #define COOKIE_STATE_FORMAT_VERSION 1
17
18 /*
19 * 2 bytes for packet length, 2 bytes for format version, 2 bytes for
20 * protocol version, 2 bytes for group id, 2 bytes for cipher id, 1 byte for
21 * key_share present flag, 8 bytes for timestamp, 2 bytes for the hashlen,
22 * EVP_MAX_MD_SIZE for transcript hash, 1 byte for app cookie length, app cookie
23 * length bytes, SHA256_DIGEST_LENGTH bytes for the HMAC of the whole thing.
24 */
25 #define MAX_COOKIE_SIZE (2 + 2 + 2 + 2 + 2 + 1 + 8 + 2 + EVP_MAX_MD_SIZE + 1 \
26 + SSL_COOKIE_LENGTH + SHA256_DIGEST_LENGTH)
27
28 /*
29 * Message header + 2 bytes for protocol version + number of random bytes +
30 * + 1 byte for legacy session id length + number of bytes in legacy session id
31 * + 2 bytes for ciphersuite + 1 byte for legacy compression
32 * + 2 bytes for extension block length + 6 bytes for key_share extension
33 * + 4 bytes for cookie extension header + the number of bytes in the cookie
34 */
35 #define MAX_HRR_SIZE (SSL3_HM_HEADER_LENGTH + 2 + SSL3_RANDOM_SIZE + 1 \
36 + SSL_MAX_SSL_SESSION_ID_LENGTH + 2 + 1 + 2 + 6 + 4 \
37 + MAX_COOKIE_SIZE)
38
39 /*
40 * Parse the client's renegotiation binding and abort if it's not right
41 */
42 int tls_parse_ctos_renegotiate(SSL_CONNECTION *s, PACKET *pkt,
43 unsigned int context,
44 X509 *x, size_t chainidx)
45 {
46 unsigned int ilen;
47 const unsigned char *data;
48 int ok;
49
50 /* Parse the length byte */
51 if (!PACKET_get_1(pkt, &ilen)
52 || !PACKET_get_bytes(pkt, &data, ilen)) {
53 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);
54 return 0;
55 }
56
57 /* Check that the extension matches */
58 if (ilen != s->s3.previous_client_finished_len) {
59 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH);
60 return 0;
61 }
62
63 ok = memcmp(data, s->s3.previous_client_finished,
64 s->s3.previous_client_finished_len);
65 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
66 if (ok) {
67 if ((data[0] ^ s->s3.previous_client_finished[0]) != 0xFF) {
68 ok = 0;
69 }
70 }
71 #endif
72 if (ok) {
73 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH);
74 return 0;
75 }
76
77 s->s3.send_connection_binding = 1;
78
79 return 1;
80 }
81
82 /*-
83 * The servername extension is treated as follows:
84 *
85 * - Only the hostname type is supported with a maximum length of 255.
86 * - The servername is rejected if too long or if it contains zeros,
87 * in which case an fatal alert is generated.
88 * - The servername field is maintained together with the session cache.
89 * - When a session is resumed, the servername call back invoked in order
90 * to allow the application to position itself to the right context.
91 * - The servername is acknowledged if it is new for a session or when
92 * it is identical to a previously used for the same session.
93 * Applications can control the behaviour. They can at any time
94 * set a 'desirable' servername for a new SSL object. This can be the
95 * case for example with HTTPS when a Host: header field is received and
96 * a renegotiation is requested. In this case, a possible servername
97 * presented in the new client hello is only acknowledged if it matches
98 * the value of the Host: field.
99 * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
100 * if they provide for changing an explicit servername context for the
101 * session, i.e. when the session has been established with a servername
102 * extension.
103 * - On session reconnect, the servername extension may be absent.
104 */
105 int tls_parse_ctos_server_name(SSL_CONNECTION *s, PACKET *pkt,
106 unsigned int context, X509 *x, size_t chainidx)
107 {
108 unsigned int servname_type;
109 PACKET sni, hostname;
110
111 if (!PACKET_as_length_prefixed_2(pkt, &sni)
112 /* ServerNameList must be at least 1 byte long. */
113 || PACKET_remaining(&sni) == 0) {
114 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
115 return 0;
116 }
117
118 /*
119 * Although the intent was for server_name to be extensible, RFC 4366
120 * was not clear about it; and so OpenSSL among other implementations,
121 * always and only allows a 'host_name' name types.
122 * RFC 6066 corrected the mistake but adding new name types
123 * is nevertheless no longer feasible, so act as if no other
124 * SNI types can exist, to simplify parsing.
125 *
126 * Also note that the RFC permits only one SNI value per type,
127 * i.e., we can only have a single hostname.
128 */
129 if (!PACKET_get_1(&sni, &servname_type)
130 || servname_type != TLSEXT_NAMETYPE_host_name
131 || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
132 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
133 return 0;
134 }
135
136 /*
137 * In TLSv1.2 and below the SNI is associated with the session. In TLSv1.3
138 * we always use the SNI value from the handshake.
139 */
140 if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {
141 if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
142 SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION);
143 return 0;
144 }
145
146 if (PACKET_contains_zero_byte(&hostname)) {
147 SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION);
148 return 0;
149 }
150
151 /*
152 * Store the requested SNI in the SSL as temporary storage.
153 * If we accept it, it will get stored in the SSL_SESSION as well.
154 */
155 OPENSSL_free(s->ext.hostname);
156 s->ext.hostname = NULL;
157 if (!PACKET_strndup(&hostname, &s->ext.hostname)) {
158 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
159 return 0;
160 }
161
162 s->servername_done = 1;
163 } else {
164 /*
165 * In TLSv1.2 and below we should check if the SNI is consistent between
166 * the initial handshake and the resumption. In TLSv1.3 SNI is not
167 * associated with the session.
168 */
169 s->servername_done = (s->session->ext.hostname != NULL)
170 && PACKET_equal(&hostname, s->session->ext.hostname,
171 strlen(s->session->ext.hostname));
172 }
173
174 return 1;
175 }
176
177 int tls_parse_ctos_maxfragmentlen(SSL_CONNECTION *s, PACKET *pkt,
178 unsigned int context,
179 X509 *x, size_t chainidx)
180 {
181 unsigned int value;
182
183 if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
184 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
185 return 0;
186 }
187
188 /* Received |value| should be a valid max-fragment-length code. */
189 if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
190 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
191 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
192 return 0;
193 }
194
195 /*
196 * When doing a full handshake or a renegotiation max_fragment_len_mode will
197 * be TLSEXT_max_fragment_length_UNSPECIFIED
198 *
199 * In case of a resumption max_fragment_len_mode will be one of
200 * TLSEXT_max_fragment_length_DISABLED, TLSEXT_max_fragment_length_512,
201 * TLSEXT_max_fragment_length_1024, TLSEXT_max_fragment_length_2048.
202 * TLSEXT_max_fragment_length_4096
203 *
204 * RFC 6066: The negotiated length applies for the duration of the session
205 * including session resumptions.
206 *
207 * So we only set the value in case it is unspecified.
208 */
209 if (s->session->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_UNSPECIFIED)
210 /*
211 * Store it in session, so it'll become binding for us
212 * and we'll include it in a next Server Hello.
213 */
214 s->session->ext.max_fragment_len_mode = value;
215
216 return 1;
217 }
218
219 #ifndef OPENSSL_NO_SRP
220 int tls_parse_ctos_srp(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
221 X509 *x, size_t chainidx)
222 {
223 PACKET srp_I;
224
225 if (!PACKET_as_length_prefixed_1(pkt, &srp_I)
226 || PACKET_contains_zero_byte(&srp_I)) {
227 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
228 return 0;
229 }
230
231 if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
232 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
233 return 0;
234 }
235
236 return 1;
237 }
238 #endif
239
240 int tls_parse_ctos_ec_pt_formats(SSL_CONNECTION *s, PACKET *pkt,
241 unsigned int context,
242 X509 *x, size_t chainidx)
243 {
244 PACKET ec_point_format_list;
245
246 if (!PACKET_as_length_prefixed_1(pkt, &ec_point_format_list)
247 || PACKET_remaining(&ec_point_format_list) == 0) {
248 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
249 return 0;
250 }
251
252 if (!s->hit) {
253 if (!PACKET_memdup(&ec_point_format_list,
254 &s->ext.peer_ecpointformats,
255 &s->ext.peer_ecpointformats_len)) {
256 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
257 return 0;
258 }
259 }
260
261 return 1;
262 }
263
264 int tls_parse_ctos_session_ticket(SSL_CONNECTION *s, PACKET *pkt,
265 unsigned int context,
266 X509 *x, size_t chainidx)
267 {
268 if (s->ext.session_ticket_cb && !s->ext.session_ticket_cb(SSL_CONNECTION_GET_USER_SSL(s), PACKET_data(pkt), (int)PACKET_remaining(pkt), s->ext.session_ticket_cb_arg)) {
269 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
270 return 0;
271 }
272
273 return 1;
274 }
275
276 int tls_parse_ctos_sig_algs_cert(SSL_CONNECTION *s, PACKET *pkt,
277 ossl_unused unsigned int context,
278 ossl_unused X509 *x,
279 ossl_unused size_t chainidx)
280 {
281 PACKET supported_sig_algs;
282
283 if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
284 || PACKET_remaining(&supported_sig_algs) == 0) {
285 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
286 return 0;
287 }
288
289 /*
290 * We use this routine on both clients and servers, and when clients
291 * get asked for PHA we need to always save the sigalgs regardless
292 * of whether it was a resumption or not.
293 */
294 if ((!s->server || (s->server && !s->hit))
295 && !tls1_save_sigalgs(s, &supported_sig_algs, 1)) {
296 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
297 return 0;
298 }
299
300 return 1;
301 }
302
303 int tls_parse_ctos_sig_algs(SSL_CONNECTION *s, PACKET *pkt,
304 unsigned int context, X509 *x, size_t chainidx)
305 {
306 PACKET supported_sig_algs;
307
308 if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
309 || PACKET_remaining(&supported_sig_algs) == 0) {
310 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
311 return 0;
312 }
313
314 /*
315 * We use this routine on both clients and servers, and when clients
316 * get asked for PHA we need to always save the sigalgs regardless
317 * of whether it was a resumption or not.
318 */
319 if ((!s->server || (s->server && !s->hit))
320 && !tls1_save_sigalgs(s, &supported_sig_algs, 0)) {
321 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
322 return 0;
323 }
324
325 return 1;
326 }
327
328 #ifndef OPENSSL_NO_OCSP
329 int tls_parse_ctos_status_request(SSL_CONNECTION *s, PACKET *pkt,
330 unsigned int context,
331 X509 *x, size_t chainidx)
332 {
333 PACKET responder_id_list, exts;
334
335 /* We ignore this in a resumption handshake */
336 if (s->hit)
337 return 1;
338
339 /* Not defined if we get one of these in a client Certificate */
340 if (x != NULL)
341 return 1;
342
343 if (!PACKET_get_1(pkt, (unsigned int *)&s->ext.status_type)) {
344 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
345 return 0;
346 }
347
348 if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
349 /*
350 * We don't know what to do with any other type so ignore it.
351 */
352 s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
353 return 1;
354 }
355
356 if (!PACKET_get_length_prefixed_2(pkt, &responder_id_list)) {
357 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
358 return 0;
359 }
360
361 /*
362 * We remove any OCSP_RESPIDs from a previous handshake
363 * to prevent unbounded memory growth - CVE-2016-6304
364 */
365 sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
366 if (PACKET_remaining(&responder_id_list) > 0) {
367 s->ext.ocsp.ids = sk_OCSP_RESPID_new_null();
368 if (s->ext.ocsp.ids == NULL) {
369 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
370 return 0;
371 }
372 } else {
373 s->ext.ocsp.ids = NULL;
374 }
375
376 while (PACKET_remaining(&responder_id_list) > 0) {
377 OCSP_RESPID *id;
378 PACKET responder_id;
379 const unsigned char *id_data;
380
381 if (!PACKET_get_length_prefixed_2(&responder_id_list, &responder_id)
382 || PACKET_remaining(&responder_id) == 0) {
383 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
384 return 0;
385 }
386
387 id_data = PACKET_data(&responder_id);
388 id = d2i_OCSP_RESPID(NULL, &id_data,
389 (int)PACKET_remaining(&responder_id));
390 if (id == NULL) {
391 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
392 return 0;
393 }
394
395 if (id_data != PACKET_end(&responder_id)) {
396 OCSP_RESPID_free(id);
397 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
398
399 return 0;
400 }
401
402 if (!sk_OCSP_RESPID_push(s->ext.ocsp.ids, id)) {
403 OCSP_RESPID_free(id);
404 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
405
406 return 0;
407 }
408 }
409
410 /* Read in request_extensions */
411 if (!PACKET_as_length_prefixed_2(pkt, &exts)) {
412 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
413 return 0;
414 }
415
416 if (PACKET_remaining(&exts) > 0) {
417 const unsigned char *ext_data = PACKET_data(&exts);
418
419 sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts,
420 X509_EXTENSION_free);
421 s->ext.ocsp.exts = d2i_X509_EXTENSIONS(NULL, &ext_data, (int)PACKET_remaining(&exts));
422 if (s->ext.ocsp.exts == NULL || ext_data != PACKET_end(&exts)) {
423 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
424 return 0;
425 }
426 }
427
428 return 1;
429 }
430 #endif
431
432 #ifndef OPENSSL_NO_NEXTPROTONEG
433 int tls_parse_ctos_npn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
434 X509 *x, size_t chainidx)
435 {
436 /*
437 * We shouldn't accept this extension on a
438 * renegotiation.
439 */
440 if (SSL_IS_FIRST_HANDSHAKE(s))
441 s->s3.npn_seen = 1;
442
443 return 1;
444 }
445 #endif
446
447 /*
448 * Save the ALPN extension in a ClientHello.|pkt| holds the contents of the ALPN
449 * extension, not including type and length. Returns: 1 on success, 0 on error.
450 */
451 int tls_parse_ctos_alpn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
452 X509 *x, size_t chainidx)
453 {
454 PACKET protocol_list, save_protocol_list, protocol;
455
456 if (!SSL_IS_FIRST_HANDSHAKE(s))
457 return 1;
458
459 if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
460 || PACKET_remaining(&protocol_list) < 2) {
461 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
462 return 0;
463 }
464
465 save_protocol_list = protocol_list;
466 do {
467 /* Protocol names can't be empty. */
468 if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
469 || PACKET_remaining(&protocol) == 0) {
470 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
471 return 0;
472 }
473 } while (PACKET_remaining(&protocol_list) != 0);
474
475 OPENSSL_free(s->s3.alpn_proposed);
476 s->s3.alpn_proposed = NULL;
477 s->s3.alpn_proposed_len = 0;
478 if (!PACKET_memdup(&save_protocol_list,
479 &s->s3.alpn_proposed, &s->s3.alpn_proposed_len)) {
480 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
481 return 0;
482 }
483
484 return 1;
485 }
486
487 #ifndef OPENSSL_NO_SRTP
488 int tls_parse_ctos_use_srtp(SSL_CONNECTION *s, PACKET *pkt,
489 unsigned int context, X509 *x, size_t chainidx)
490 {
491 STACK_OF(SRTP_PROTECTION_PROFILE) *srvr;
492 unsigned int ct, mki_len, id;
493 int i, srtp_pref;
494 PACKET subpkt;
495 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
496
497 /* Ignore this if we have no SRTP profiles */
498 if (SSL_get_srtp_profiles(ssl) == NULL)
499 return 1;
500
501 /* Pull off the length of the cipher suite list and check it is even */
502 if (!PACKET_get_net_2(pkt, &ct) || (ct & 1) != 0
503 || !PACKET_get_sub_packet(pkt, &subpkt, ct)) {
504 SSLfatal(s, SSL_AD_DECODE_ERROR,
505 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
506 return 0;
507 }
508
509 srvr = SSL_get_srtp_profiles(ssl);
510 s->srtp_profile = NULL;
511 /* Search all profiles for a match initially */
512 srtp_pref = sk_SRTP_PROTECTION_PROFILE_num(srvr);
513
514 while (PACKET_remaining(&subpkt)) {
515 if (!PACKET_get_net_2(&subpkt, &id)) {
516 SSLfatal(s, SSL_AD_DECODE_ERROR,
517 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
518 return 0;
519 }
520
521 /*
522 * Only look for match in profiles of higher preference than
523 * current match.
524 * If no profiles have been have been configured then this
525 * does nothing.
526 */
527 for (i = 0; i < srtp_pref; i++) {
528 SRTP_PROTECTION_PROFILE *sprof = sk_SRTP_PROTECTION_PROFILE_value(srvr, i);
529
530 if (sprof->id == id) {
531 s->srtp_profile = sprof;
532 srtp_pref = i;
533 break;
534 }
535 }
536 }
537
538 /* Now extract the MKI value as a sanity check, but discard it for now */
539 if (!PACKET_get_1(pkt, &mki_len)) {
540 SSLfatal(s, SSL_AD_DECODE_ERROR,
541 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
542 return 0;
543 }
544
545 if (!PACKET_forward(pkt, mki_len)
546 || PACKET_remaining(pkt)) {
547 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRTP_MKI_VALUE);
548 return 0;
549 }
550
551 return 1;
552 }
553 #endif
554
555 int tls_parse_ctos_etm(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
556 X509 *x, size_t chainidx)
557 {
558 if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))
559 s->ext.use_etm = 1;
560
561 return 1;
562 }
563
564 /*
565 * Process a psk_kex_modes extension received in the ClientHello. |pkt| contains
566 * the raw PACKET data for the extension. Returns 1 on success or 0 on failure.
567 */
568 int tls_parse_ctos_psk_kex_modes(SSL_CONNECTION *s, PACKET *pkt,
569 unsigned int context,
570 X509 *x, size_t chainidx)
571 {
572 #ifndef OPENSSL_NO_TLS1_3
573 PACKET psk_kex_modes;
574 unsigned int mode;
575
576 if (!PACKET_as_length_prefixed_1(pkt, &psk_kex_modes)
577 || PACKET_remaining(&psk_kex_modes) == 0) {
578 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
579 return 0;
580 }
581
582 while (PACKET_get_1(&psk_kex_modes, &mode)) {
583 if (mode == TLSEXT_KEX_MODE_KE_DHE)
584 s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE_DHE;
585 else if (mode == TLSEXT_KEX_MODE_KE
586 && (s->options & SSL_OP_ALLOW_NO_DHE_KEX) != 0)
587 s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
588 }
589
590 if (((s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) != 0)
591 && (s->options & SSL_OP_PREFER_NO_DHE_KEX) != 0) {
592
593 /*
594 * If NO_DHE is supported and preferred, then we only remember this
595 * mode. DHE PSK will not be used for sure, because in any case where
596 * it would be supported (i.e. if a key share is present), NO_DHE would
597 * be supported as well. As the latter is preferred it would be
598 * chosen. By removing DHE PSK here, we don't have to deal with the
599 * SSL_OP_PREFER_NO_DHE_KEX option in any other place.
600 */
601 s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE;
602 }
603
604 #endif
605
606 return 1;
607 }
608
609 /*
610 * Use function tls_parse_ctos_key_share with helper functions extract_keyshares,
611 * check_overlap and tls_accept_ksgroup to parse the key_share extension(s)
612 * received in the ClientHello and to select the group used of the key exchange
613 */
614
615 #ifndef OPENSSL_NO_TLS1_3
616 /*
617 * Accept a key share group by setting the related variables in s->s3 and
618 * by generating a pubkey for this group
619 */
620 static int tls_accept_ksgroup(SSL_CONNECTION *s, uint16_t ksgroup, PACKET *encoded_pubkey)
621 {
622 /* Accept the key share group */
623 s->s3.group_id = ksgroup;
624 s->s3.group_id_candidate = ksgroup;
625 /* Cache the selected group ID in the SSL_SESSION */
626 s->session->kex_group = ksgroup;
627 if ((s->s3.peer_tmp = ssl_generate_param_group(s, ksgroup)) == NULL) {
628 SSLfatal(s,
629 SSL_AD_INTERNAL_ERROR,
630 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
631 return 0;
632 }
633 if (tls13_set_encoded_pub_key(s->s3.peer_tmp,
634 PACKET_data(encoded_pubkey),
635 PACKET_remaining(encoded_pubkey))
636 <= 0) {
637 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT);
638 return 0;
639 }
640 return 1;
641 }
642
643 #define GROUPLIST_INCREMENT 32 /* Memory allocation chunk size (nominally 64 Bytes chunks) */
644
645 typedef enum KS_EXTRACTION_RESULT {
646 EXTRACTION_FAILURE,
647 EXTRACTION_SUCCESS,
648 EXTRACTION_SUCCESS_HRR
649 } KS_EXTRACTION_RESULT;
650
651 static KS_EXTRACTION_RESULT extract_keyshares(SSL_CONNECTION *s, PACKET *key_share_list,
652 const uint16_t *clntgroups, size_t clnt_num_groups,
653 const uint16_t *srvrgroups, size_t srvr_num_groups,
654 uint16_t **keyshares_arr, PACKET **encoded_pubkey_arr,
655 size_t *keyshares_cnt, size_t *keyshares_max)
656 {
657 PACKET encoded_pubkey;
658 size_t key_share_pos = 0;
659 size_t previous_key_share_pos = 0;
660 unsigned int group_id = 0;
661
662 /* Prepare memory to hold the extracted key share groups and related pubkeys */
663 *keyshares_arr = OPENSSL_malloc_array(*keyshares_max,
664 sizeof(**keyshares_arr));
665 if (*keyshares_arr == NULL) {
666 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
667 goto failure;
668 }
669 *encoded_pubkey_arr = OPENSSL_malloc_array(*keyshares_max,
670 sizeof(**encoded_pubkey_arr));
671 if (*encoded_pubkey_arr == NULL) {
672 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
673 goto failure;
674 }
675
676 while (PACKET_remaining(key_share_list) > 0) {
677 /* Get the group_id for the current share and its encoded_pubkey */
678 if (!PACKET_get_net_2(key_share_list, &group_id)
679 || !PACKET_get_length_prefixed_2(key_share_list, &encoded_pubkey)
680 || PACKET_remaining(&encoded_pubkey) == 0) {
681 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
682 goto failure;
683 }
684
685 /*
686 * If we sent an HRR then the key_share sent back MUST be for the group
687 * we requested, and must be the only key_share sent.
688 */
689 if (s->s3.group_id != 0
690 && (group_id != s->s3.group_id
691 || PACKET_remaining(key_share_list) != 0)) {
692 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
693 goto failure;
694 }
695
696 /*
697 * Check if this share is in supported_groups sent from client
698 * RFC 8446 also mandates that clients send keyshares in the same
699 * order as listed in the supported groups extension, but its not
700 * required that the server check that, and some clients violate this
701 * so instead of failing the connection when that occurs, log a trace
702 * message indicating the client discrepancy.
703 */
704 if (!check_in_list(s, group_id, clntgroups, clnt_num_groups, 0, &key_share_pos)) {
705 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
706 goto failure;
707 }
708
709 if (key_share_pos < previous_key_share_pos)
710 OSSL_TRACE1(TLS, "key share group id %d is out of RFC 8446 order\n", group_id);
711
712 previous_key_share_pos = key_share_pos;
713
714 if (s->s3.group_id != 0) {
715 /*
716 * We have sent a HRR, and the key share we got back is
717 * the one we expected and is the only key share and is
718 * in the list of supported_groups (checked
719 * above already), hence we accept this key share group
720 */
721 if (!tls_accept_ksgroup(s, s->s3.group_id, &encoded_pubkey))
722 goto failure; /* SSLfatal already called */
723 /* We have selected a key share group via HRR, hence we're done here */
724 return EXTRACTION_SUCCESS_HRR;
725 }
726
727 /*
728 * We tolerate but ignore a group id that we don't think is
729 * suitable for TLSv1.3 or which is not supported by the server
730 */
731 if (!check_in_list(s, group_id, srvrgroups, srvr_num_groups, 1, NULL)
732 || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)
733 || !tls_valid_group(s, group_id, TLS1_3_VERSION, TLS1_3_VERSION,
734 0, NULL)) {
735 /* Share not suitable or not supported, check next share */
736 continue;
737 }
738
739 /* Memorize this key share group ID and its encoded point */
740 (*keyshares_arr)[*keyshares_cnt] = group_id;
741 (*encoded_pubkey_arr)[(*keyshares_cnt)++] = encoded_pubkey;
742
743 /*
744 * Memory management (remark: While limiting the client to only allow
745 * a maximum of OPENSSL_CLIENT_MAX_KEY_SHARES to be sent, the server can
746 * handle any number of key shares)
747 */
748 if (*keyshares_cnt == *keyshares_max) {
749 PACKET *tmp_pkt;
750 uint16_t *tmp = OPENSSL_realloc_array(*keyshares_arr,
751 *keyshares_max + GROUPLIST_INCREMENT,
752 sizeof(**keyshares_arr));
753
754 if (tmp == NULL) {
755 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
756 goto failure;
757 }
758
759 *keyshares_arr = tmp;
760 tmp_pkt = OPENSSL_realloc_array(*encoded_pubkey_arr,
761 *keyshares_max + GROUPLIST_INCREMENT,
762 sizeof(**encoded_pubkey_arr));
763 if (tmp_pkt == NULL) {
764 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
765 goto failure;
766 }
767
768 *encoded_pubkey_arr = tmp_pkt;
769 *keyshares_max += GROUPLIST_INCREMENT;
770 }
771 }
772
773 return EXTRACTION_SUCCESS;
774
775 failure:
776 /* Fatal error -> free any allocated memory and return 0 */
777 OPENSSL_free(*keyshares_arr);
778 OPENSSL_free(*encoded_pubkey_arr);
779 return EXTRACTION_FAILURE;
780 }
781 #endif
782
783 /*
784 * For each group in the priority list of groups, check if that group is
785 * also present in the secondary list; if so, select the first overlap and
786 * assign to selected_group and also set the related index in the candidate group list,
787 * or set selected_group to 0 if no overlap
788 */
789 #ifndef OPENSSL_NO_TLS1_3
790 static void check_overlap(SSL_CONNECTION *s,
791 const uint16_t *prio_groups, size_t prio_num_groups,
792 const uint16_t *candidate_groups, size_t candidate_num_groups,
793 int *prio_group_idx, int *candidate_group_idx,
794 uint16_t *selected_group)
795 {
796 uint16_t current_group;
797 size_t group_idx = prio_num_groups;
798 size_t new_group_idx = 0;
799
800 *candidate_group_idx = 0;
801 *prio_group_idx = 0;
802 *selected_group = 0;
803
804 for (current_group = 0; current_group < candidate_num_groups; current_group++) {
805 if (!check_in_list(s, candidate_groups[current_group], prio_groups,
806 prio_num_groups, 1, &new_group_idx)
807 || !tls_group_allowed(s, candidate_groups[current_group],
808 SSL_SECOP_CURVE_SUPPORTED)
809 || !tls_valid_group(s, candidate_groups[current_group], TLS1_3_VERSION,
810 TLS1_3_VERSION, 0, NULL))
811 /* No overlap or group not suitable, check next group */
812 continue;
813
814 /*
815 * is the found new_group_idx earlier in the priority list than
816 * initial or last group_idx?
817 */
818 if (new_group_idx < group_idx) {
819 group_idx = new_group_idx;
820 *candidate_group_idx = current_group;
821 *prio_group_idx = (int)group_idx;
822 *selected_group = prio_groups[group_idx];
823 }
824 }
825 }
826 #endif
827
828 int tls_parse_ctos_key_share(SSL_CONNECTION *s, PACKET *pkt,
829 unsigned int context, X509 *x, size_t chainidx)
830 {
831 #ifndef OPENSSL_NO_TLS1_3
832 PACKET key_share_list;
833 const uint16_t *clntgroups, *srvrgroups;
834 const size_t *srvrtuples;
835 uint16_t *first_group_in_tuple;
836 size_t clnt_num_groups, srvr_num_groups, srvr_num_tuples;
837 PACKET *encoded_pubkey_arr = NULL;
838 uint16_t *keyshares_arr = NULL;
839 size_t keyshares_cnt = 0;
840 size_t keyshares_max = GROUPLIST_INCREMENT;
841 /* We conservatively assume that we did not find a suitable group */
842 uint16_t group_id_candidate = 0;
843 KS_EXTRACTION_RESULT ks_extraction_result;
844 size_t current_tuple;
845 int ret = 0;
846
847 s->s3.group_id_candidate = 0;
848 if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0)
849 return 1;
850
851 /* Sanity check */
852 if (s->s3.peer_tmp != NULL) {
853 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
854 return 0;
855 }
856
857 if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) {
858 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
859 return 0;
860 }
861
862 /* Get list of server supported groups and the group tuples */
863 tls1_get_supported_groups(s, &srvrgroups, &srvr_num_groups);
864 tls1_get_group_tuples(s, &srvrtuples, &srvr_num_tuples);
865 /* Get the clients list of supported groups. */
866 tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups);
867
868 if (clnt_num_groups == 0) {
869 /*
870 * This can only happen if the supported_groups extension was not sent,
871 * because we verify that the length is non-zero when we process that
872 * extension.
873 */
874 SSLfatal(s, SSL_AD_MISSING_EXTENSION,
875 SSL_R_MISSING_SUPPORTED_GROUPS_EXTENSION);
876 return 0;
877 }
878
879 if (s->s3.group_id != 0 && PACKET_remaining(&key_share_list) == 0) {
880 /*
881 * If we set a group_id already, then we must have sent an HRR
882 * requesting a new key_share. If we haven't got one then that is an
883 * error
884 */
885 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
886 return 0;
887 }
888
889 /* We parse the key share extension and memorize the entries (after some checks) */
890 ks_extraction_result = extract_keyshares(s,
891 &key_share_list,
892 clntgroups, clnt_num_groups,
893 srvrgroups, srvr_num_groups,
894 &keyshares_arr, &encoded_pubkey_arr,
895 &keyshares_cnt, &keyshares_max);
896
897 if (ks_extraction_result == EXTRACTION_FAILURE) /* Fatal error during tests */
898 return 0; /* Memory already freed and SSLfatal already called */
899 if (ks_extraction_result == EXTRACTION_SUCCESS_HRR) /* Successful HRR */
900 goto end;
901
902 /*
903 * We now have the following lists available to make a decision for
904 * which group the server should use for key exchange :
905 * From client: clntgroups[clnt_num_groups],
906 * keyshares_arr[keyshares_cnt], encoded_pubkey_arr[keyshares_cnt]
907 * From server: srvrgroups[srvr_num_groups], srvrtuples[srvr_num_tuples]
908 *
909 * Group selection algorithm:
910 * For all tuples do:
911 * key share group(s) overlapping with current tuple?
912 * --> Yes: accept group_id for SH
913 * --> No: is any of the client supported_groups overlapping with current tuple?
914 * --> Yes: memorize group_id for HRR, break
915 * --> No: continue to check next tuple
916 *
917 * Remark: Selection priority different for client- or server-preference
918 */
919 first_group_in_tuple = (uint16_t *)srvrgroups;
920 for (current_tuple = 0; current_tuple < srvr_num_tuples; current_tuple++) {
921 size_t number_of_groups_in_tuple = srvrtuples[current_tuple];
922 int prio_group_idx = 0, candidate_group_idx = 0;
923
924 /* Server or client preference ? */
925 if (s->options & SSL_OP_SERVER_PREFERENCE) {
926 /* Server preference */
927 /* Is there overlap with a key share group? */
928 check_overlap(s,
929 first_group_in_tuple, number_of_groups_in_tuple,
930 keyshares_arr, keyshares_cnt,
931 &prio_group_idx, &candidate_group_idx,
932 &group_id_candidate);
933 if (group_id_candidate > 0) { /* Overlap found -> accept the key share group */
934 if (!tls_accept_ksgroup(s, group_id_candidate,
935 &encoded_pubkey_arr[candidate_group_idx]))
936 goto err; /* SSLfatal already called */
937 /* We have all info for a SH, hence we're done here */
938 goto end;
939 } else {
940 /*
941 * There's no overlap with a key share, but is there at least a client
942 * supported_group overlapping with the current tuple?
943 */
944 check_overlap(s,
945 first_group_in_tuple, number_of_groups_in_tuple,
946 clntgroups, clnt_num_groups,
947 &prio_group_idx, &candidate_group_idx,
948 &group_id_candidate);
949 if (group_id_candidate > 0) {
950 /*
951 * We did not have a key share overlap, but at least the supported
952 * groups overlap hence we can stop searching
953 * (and report group_id_candidate 'upward' for HRR)
954 */
955 s->s3.group_id_candidate = group_id_candidate;
956 goto end;
957 } else {
958 /*
959 * Neither key share nor supported_groups overlap current
960 * tuple, hence we try the next tuple
961 */
962 first_group_in_tuple = &first_group_in_tuple[number_of_groups_in_tuple];
963 continue;
964 }
965 }
966
967 } else { /* We have client preference */
968 check_overlap(s,
969 keyshares_arr, keyshares_cnt,
970 first_group_in_tuple, number_of_groups_in_tuple,
971 &prio_group_idx, &candidate_group_idx,
972 &group_id_candidate);
973 if (group_id_candidate > 0) {
974 if (!tls_accept_ksgroup(s, group_id_candidate, &encoded_pubkey_arr[prio_group_idx]))
975 goto err;
976 goto end;
977 } else {
978 check_overlap(s,
979 clntgroups, clnt_num_groups,
980 first_group_in_tuple, number_of_groups_in_tuple,
981 &prio_group_idx, &candidate_group_idx,
982 &group_id_candidate);
983 if (group_id_candidate > 0) {
984 s->s3.group_id_candidate = group_id_candidate;
985 goto end;
986 } else {
987 first_group_in_tuple = &first_group_in_tuple[number_of_groups_in_tuple];
988 continue;
989 }
990 }
991 }
992 }
993
994 end:
995 ret = 1;
996
997 err:
998 OPENSSL_free(keyshares_arr);
999 OPENSSL_free(encoded_pubkey_arr);
1000 return ret;
1001
1002 #endif
1003
1004 return 1;
1005 }
1006
1007 int tls_parse_ctos_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1008 X509 *x, size_t chainidx)
1009 {
1010 #ifndef OPENSSL_NO_TLS1_3
1011 unsigned int format, version, key_share, group_id;
1012 EVP_MD_CTX *hctx;
1013 EVP_PKEY *pkey;
1014 PACKET cookie, raw, chhash, appcookie;
1015 WPACKET hrrpkt;
1016 const unsigned char *data, *mdin, *ciphdata;
1017 unsigned char hmac[SHA256_DIGEST_LENGTH];
1018 unsigned char hrr[MAX_HRR_SIZE];
1019 size_t rawlen, hmaclen, hrrlen, ciphlen;
1020 uint64_t tm, now;
1021 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1022 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1023
1024 /* Ignore any cookie if we're not set up to verify it */
1025 if (sctx->verify_stateless_cookie_cb == NULL
1026 || (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
1027 return 1;
1028
1029 if (!PACKET_as_length_prefixed_2(pkt, &cookie)) {
1030 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1031 return 0;
1032 }
1033
1034 raw = cookie;
1035 data = PACKET_data(&raw);
1036 rawlen = PACKET_remaining(&raw);
1037 if (rawlen < SHA256_DIGEST_LENGTH
1038 || !PACKET_forward(&raw, rawlen - SHA256_DIGEST_LENGTH)) {
1039 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1040 return 0;
1041 }
1042 mdin = PACKET_data(&raw);
1043
1044 /* Verify the HMAC of the cookie */
1045 hctx = EVP_MD_CTX_create();
1046 pkey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",
1047 sctx->propq,
1048 s->session_ctx->ext.cookie_hmac_key,
1049 sizeof(s->session_ctx->ext.cookie_hmac_key));
1050 if (hctx == NULL || pkey == NULL) {
1051 EVP_MD_CTX_free(hctx);
1052 EVP_PKEY_free(pkey);
1053 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
1054 return 0;
1055 }
1056
1057 hmaclen = SHA256_DIGEST_LENGTH;
1058 if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", sctx->libctx,
1059 sctx->propq, pkey, NULL)
1060 <= 0
1061 || EVP_DigestSign(hctx, hmac, &hmaclen, data,
1062 rawlen - SHA256_DIGEST_LENGTH)
1063 <= 0
1064 || hmaclen != SHA256_DIGEST_LENGTH) {
1065 EVP_MD_CTX_free(hctx);
1066 EVP_PKEY_free(pkey);
1067 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1068 return 0;
1069 }
1070
1071 EVP_MD_CTX_free(hctx);
1072 EVP_PKEY_free(pkey);
1073
1074 if (CRYPTO_memcmp(hmac, mdin, SHA256_DIGEST_LENGTH) != 0) {
1075 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COOKIE_MISMATCH);
1076 return 0;
1077 }
1078
1079 if (!PACKET_get_net_2(&cookie, &format)) {
1080 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1081 return 0;
1082 }
1083 /* Check the cookie format is something we recognise. Ignore it if not */
1084 if (format != COOKIE_STATE_FORMAT_VERSION)
1085 return 1;
1086
1087 /*
1088 * The rest of these checks really shouldn't fail since we have verified the
1089 * HMAC above.
1090 */
1091
1092 /* Check the version number is sane */
1093 if (!PACKET_get_net_2(&cookie, &version)) {
1094 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1095 return 0;
1096 }
1097 if (version != TLS1_3_VERSION) {
1098 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1099 SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
1100 return 0;
1101 }
1102
1103 if (!PACKET_get_net_2(&cookie, &group_id)) {
1104 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1105 return 0;
1106 }
1107
1108 ciphdata = PACKET_data(&cookie);
1109 if (!PACKET_forward(&cookie, 2)) {
1110 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1111 return 0;
1112 }
1113 if (group_id != s->s3.group_id
1114 || s->s3.tmp.new_cipher
1115 != ssl_get_cipher_by_char(s, ciphdata, 0)) {
1116 /*
1117 * We chose a different cipher or group id this time around to what is
1118 * in the cookie. Something must have changed.
1119 */
1120 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER);
1121 return 0;
1122 }
1123
1124 if (!PACKET_get_1(&cookie, &key_share)
1125 || !PACKET_get_net_8(&cookie, &tm)
1126 || !PACKET_get_length_prefixed_2(&cookie, &chhash)
1127 || !PACKET_get_length_prefixed_1(&cookie, &appcookie)
1128 || PACKET_remaining(&cookie) != SHA256_DIGEST_LENGTH) {
1129 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1130 return 0;
1131 }
1132
1133 /* We tolerate a cookie age of up to 10 minutes (= 60 * 10 seconds) */
1134 now = time(NULL);
1135 if (tm > now || (now - tm) > 600) {
1136 /* Cookie is stale. Ignore it */
1137 return 1;
1138 }
1139
1140 /* Verify the app cookie */
1141 if (sctx->verify_stateless_cookie_cb(SSL_CONNECTION_GET_USER_SSL(s),
1142 PACKET_data(&appcookie),
1143 PACKET_remaining(&appcookie))
1144 == 0) {
1145 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COOKIE_MISMATCH);
1146 return 0;
1147 }
1148
1149 /*
1150 * Reconstruct the HRR that we would have sent in response to the original
1151 * ClientHello so we can add it to the transcript hash.
1152 * Note: This won't work with custom HRR extensions
1153 */
1154 if (!WPACKET_init_static_len(&hrrpkt, hrr, sizeof(hrr), 0)) {
1155 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1156 return 0;
1157 }
1158 if (!WPACKET_put_bytes_u8(&hrrpkt, SSL3_MT_SERVER_HELLO)
1159 || !WPACKET_start_sub_packet_u24(&hrrpkt)
1160 || !WPACKET_put_bytes_u16(&hrrpkt, TLS1_2_VERSION)
1161 || !WPACKET_memcpy(&hrrpkt, hrrrandom, SSL3_RANDOM_SIZE)
1162 || !WPACKET_sub_memcpy_u8(&hrrpkt, s->tmp_session_id,
1163 s->tmp_session_id_len)
1164 || !ssl->method->put_cipher_by_char(s->s3.tmp.new_cipher, &hrrpkt,
1165 &ciphlen)
1166 || !WPACKET_put_bytes_u8(&hrrpkt, 0)
1167 || !WPACKET_start_sub_packet_u16(&hrrpkt)) {
1168 WPACKET_cleanup(&hrrpkt);
1169 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1170 return 0;
1171 }
1172 if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_supported_versions)
1173 || !WPACKET_start_sub_packet_u16(&hrrpkt)
1174 || !WPACKET_put_bytes_u16(&hrrpkt, s->version)
1175 || !WPACKET_close(&hrrpkt)) {
1176 WPACKET_cleanup(&hrrpkt);
1177 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1178 return 0;
1179 }
1180 if (key_share) {
1181 if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_key_share)
1182 || !WPACKET_start_sub_packet_u16(&hrrpkt)
1183 || !WPACKET_put_bytes_u16(&hrrpkt, s->s3.group_id)
1184 || !WPACKET_close(&hrrpkt)) {
1185 WPACKET_cleanup(&hrrpkt);
1186 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1187 return 0;
1188 }
1189 }
1190 if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_cookie)
1191 || !WPACKET_start_sub_packet_u16(&hrrpkt)
1192 || !WPACKET_sub_memcpy_u16(&hrrpkt, data, rawlen)
1193 || !WPACKET_close(&hrrpkt) /* cookie extension */
1194 || !WPACKET_close(&hrrpkt) /* extension block */
1195 || !WPACKET_close(&hrrpkt) /* message */
1196 || !WPACKET_get_total_written(&hrrpkt, &hrrlen)
1197 || !WPACKET_finish(&hrrpkt)) {
1198 WPACKET_cleanup(&hrrpkt);
1199 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1200 return 0;
1201 }
1202
1203 /* Reconstruct the transcript hash */
1204 if (!create_synthetic_message_hash(s, PACKET_data(&chhash),
1205 PACKET_remaining(&chhash), hrr,
1206 hrrlen)) {
1207 /* SSLfatal() already called */
1208 return 0;
1209 }
1210
1211 /* Act as if this ClientHello came after a HelloRetryRequest */
1212 s->hello_retry_request = SSL_HRR_PENDING;
1213
1214 s->ext.cookieok = 1;
1215 #endif
1216
1217 return 1;
1218 }
1219
1220 int tls_parse_ctos_supported_groups(SSL_CONNECTION *s, PACKET *pkt,
1221 unsigned int context,
1222 X509 *x, size_t chainidx)
1223 {
1224 PACKET supported_groups_list;
1225
1226 /* Each group is 2 bytes and we must have at least 1. */
1227 if (!PACKET_as_length_prefixed_2(pkt, &supported_groups_list)
1228 || PACKET_remaining(&supported_groups_list) == 0
1229 || (PACKET_remaining(&supported_groups_list) % 2) != 0) {
1230 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1231 return 0;
1232 }
1233
1234 if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {
1235 OPENSSL_free(s->ext.peer_supportedgroups);
1236 s->ext.peer_supportedgroups = NULL;
1237 s->ext.peer_supportedgroups_len = 0;
1238 if (!tls1_save_u16(&supported_groups_list,
1239 &s->ext.peer_supportedgroups,
1240 &s->ext.peer_supportedgroups_len)) {
1241 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1242 return 0;
1243 }
1244 }
1245
1246 return 1;
1247 }
1248
1249 int tls_parse_ctos_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1250 X509 *x, size_t chainidx)
1251 {
1252 /* The extension must always be empty */
1253 if (PACKET_remaining(pkt) != 0) {
1254 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1255 return 0;
1256 }
1257
1258 if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)
1259 return 1;
1260
1261 s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS;
1262
1263 return 1;
1264 }
1265
1266 int tls_parse_ctos_early_data(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1267 X509 *x, size_t chainidx)
1268 {
1269 if (PACKET_remaining(pkt) != 0) {
1270 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1271 return 0;
1272 }
1273
1274 if (s->hello_retry_request != SSL_HRR_NONE) {
1275 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
1276 return 0;
1277 }
1278
1279 return 1;
1280 }
1281
1282 static SSL_TICKET_STATUS tls_get_stateful_ticket(SSL_CONNECTION *s, PACKET *tick,
1283 SSL_SESSION **sess)
1284 {
1285 SSL_SESSION *tmpsess = NULL;
1286
1287 s->ext.ticket_expected = 1;
1288
1289 switch (PACKET_remaining(tick)) {
1290 case 0:
1291 return SSL_TICKET_EMPTY;
1292
1293 case SSL_MAX_SSL_SESSION_ID_LENGTH:
1294 break;
1295
1296 default:
1297 return SSL_TICKET_NO_DECRYPT;
1298 }
1299
1300 tmpsess = lookup_sess_in_cache(s, PACKET_data(tick),
1301 SSL_MAX_SSL_SESSION_ID_LENGTH);
1302
1303 if (tmpsess == NULL)
1304 return SSL_TICKET_NO_DECRYPT;
1305
1306 *sess = tmpsess;
1307 return SSL_TICKET_SUCCESS;
1308 }
1309
1310 int tls_parse_ctos_psk(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1311 X509 *x, size_t chainidx)
1312 {
1313 PACKET identities, binders, binder;
1314 size_t binderoffset;
1315 int hashsize;
1316 SSL_SESSION *sess = NULL;
1317 unsigned int id, i, ext = 0;
1318 const EVP_MD *md = NULL;
1319 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1320 SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
1321
1322 /*
1323 * If we have no PSK kex mode that we recognise then we can't resume so
1324 * ignore this extension
1325 */
1326 if ((s->ext.psk_kex_mode
1327 & (TLSEXT_KEX_MODE_FLAG_KE | TLSEXT_KEX_MODE_FLAG_KE_DHE))
1328 == 0)
1329 return 1;
1330
1331 if (!PACKET_get_length_prefixed_2(pkt, &identities)) {
1332 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1333 return 0;
1334 }
1335
1336 s->ext.ticket_expected = 0;
1337 for (id = 0; PACKET_remaining(&identities) != 0; id++) {
1338 PACKET identity;
1339 unsigned long ticket_agel;
1340 size_t idlen;
1341
1342 if (!PACKET_get_length_prefixed_2(&identities, &identity)
1343 || !PACKET_get_net_4(&identities, &ticket_agel)) {
1344 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1345 return 0;
1346 }
1347
1348 idlen = PACKET_remaining(&identity);
1349 if (idlen == 0) {
1350 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1351 return 0;
1352 }
1353 if (s->psk_find_session_cb != NULL
1354 && !s->psk_find_session_cb(ussl, PACKET_data(&identity), idlen,
1355 &sess)) {
1356 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_EXTENSION);
1357 return 0;
1358 }
1359
1360 #ifndef OPENSSL_NO_PSK
1361 if (sess == NULL
1362 && s->psk_server_callback != NULL
1363 && idlen <= PSK_MAX_IDENTITY_LEN) {
1364 char *pskid = NULL;
1365 unsigned char pskdata[PSK_MAX_PSK_LEN];
1366 unsigned int pskdatalen;
1367
1368 if (!PACKET_strndup(&identity, &pskid)) {
1369 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1370 return 0;
1371 }
1372 pskdatalen = s->psk_server_callback(ussl, pskid, pskdata,
1373 sizeof(pskdata));
1374 OPENSSL_free(pskid);
1375 if (pskdatalen > PSK_MAX_PSK_LEN) {
1376 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1377 return 0;
1378 } else if (pskdatalen > 0) {
1379 const SSL_CIPHER *cipher;
1380 const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
1381
1382 /*
1383 * We found a PSK using an old style callback. We don't know
1384 * the digest so we default to SHA256 as per the TLSv1.3 spec
1385 */
1386 cipher = SSL_CIPHER_find(SSL_CONNECTION_GET_SSL(s),
1387 tls13_aes128gcmsha256_id);
1388 if (cipher == NULL) {
1389 OPENSSL_cleanse(pskdata, pskdatalen);
1390 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1391 return 0;
1392 }
1393
1394 sess = SSL_SESSION_new();
1395 if (sess == NULL
1396 || !SSL_SESSION_set1_master_key(sess, pskdata,
1397 pskdatalen)
1398 || !SSL_SESSION_set_cipher(sess, cipher)
1399 || !SSL_SESSION_set_protocol_version(sess,
1400 TLS1_3_VERSION)) {
1401 OPENSSL_cleanse(pskdata, pskdatalen);
1402 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1403 goto err;
1404 }
1405 OPENSSL_cleanse(pskdata, pskdatalen);
1406 }
1407 }
1408 #endif /* OPENSSL_NO_PSK */
1409
1410 if (sess != NULL) {
1411 /* We found a PSK */
1412 SSL_SESSION *sesstmp = ssl_session_dup(sess, 0);
1413
1414 if (sesstmp == NULL) {
1415 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1416 goto err;
1417 }
1418 SSL_SESSION_free(sess);
1419 sess = sesstmp;
1420
1421 /*
1422 * We've just been told to use this session for this context so
1423 * make sure the sid_ctx matches up.
1424 */
1425 memcpy(sess->sid_ctx, s->sid_ctx, s->sid_ctx_length);
1426 sess->sid_ctx_length = s->sid_ctx_length;
1427 ext = 1;
1428 if (id == 0)
1429 s->ext.early_data_ok = 1;
1430 s->ext.ticket_expected = 1;
1431 } else {
1432 OSSL_TIME t, age, expire;
1433 int ret;
1434
1435 /*
1436 * If we are using anti-replay protection then we behave as if
1437 * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
1438 * is no point in using full stateless tickets.
1439 */
1440 if ((s->options & SSL_OP_NO_TICKET) != 0
1441 || (s->max_early_data > 0
1442 && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))
1443 ret = tls_get_stateful_ticket(s, &identity, &sess);
1444 else
1445 ret = tls_decrypt_ticket(s, PACKET_data(&identity),
1446 PACKET_remaining(&identity), NULL, 0,
1447 &sess);
1448
1449 if (ret == SSL_TICKET_EMPTY) {
1450 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1451 goto err;
1452 }
1453
1454 if (ret == SSL_TICKET_FATAL_ERR_MALLOC
1455 || ret == SSL_TICKET_FATAL_ERR_OTHER) {
1456 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1457 goto err;
1458 }
1459 if (ret == SSL_TICKET_NONE || ret == SSL_TICKET_NO_DECRYPT)
1460 continue;
1461
1462 /* Check for replay */
1463 if (s->max_early_data > 0
1464 && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0
1465 && !SSL_CTX_remove_session(s->session_ctx, sess)) {
1466 SSL_SESSION_free(sess);
1467 sess = NULL;
1468 continue;
1469 }
1470
1471 age = ossl_time_subtract(ossl_ms2time(ticket_agel),
1472 ossl_ms2time(sess->ext.tick_age_add));
1473 t = ossl_time_subtract(ossl_time_now(), sess->time);
1474
1475 /*
1476 * Although internally we use OSS_TIME which has ns granularity,
1477 * when SSL_SESSION structures are serialised/deserialised we use
1478 * second granularity for the sess->time field. Therefore it could
1479 * appear that the client's ticket age is longer than ours (our
1480 * ticket age calculation should always be slightly longer than the
1481 * client's due to the network latency). Therefore we add 1000ms to
1482 * our age calculation to adjust for rounding errors.
1483 */
1484 expire = ossl_time_add(t, ossl_ms2time(1000));
1485
1486 if (id == 0
1487 && ossl_time_compare(sess->timeout, t) >= 0
1488 && ossl_time_compare(age, expire) <= 0
1489 && ossl_time_compare(ossl_time_add(age, TICKET_AGE_ALLOWANCE),
1490 expire)
1491 >= 0) {
1492 /*
1493 * Ticket age is within tolerance and not expired. We allow it
1494 * for early data
1495 */
1496 s->ext.early_data_ok = 1;
1497 }
1498 }
1499
1500 md = ssl_md(sctx, sess->cipher->algorithm2);
1501 if (md == NULL) {
1502 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1503 goto err;
1504 }
1505 if (!EVP_MD_is_a(md,
1506 EVP_MD_get0_name(ssl_md(sctx,
1507 s->s3.tmp.new_cipher->algorithm2)))) {
1508 /* The ciphersuite is not compatible with this session. */
1509 SSL_SESSION_free(sess);
1510 sess = NULL;
1511 s->ext.early_data_ok = 0;
1512 /*
1513 * We fall back to a full handshake. The new session ticket will be
1514 * issued to the client with the newly negotiated ciphersuite,
1515 * allowing successful resumption on future connections.
1516 */
1517 s->ext.ticket_expected = 1;
1518 continue;
1519 }
1520 break;
1521 }
1522
1523 if (sess == NULL)
1524 return 1;
1525
1526 binderoffset = PACKET_data(pkt) - (const unsigned char *)s->init_buf->data;
1527 hashsize = EVP_MD_get_size(md);
1528 if (hashsize <= 0)
1529 goto err;
1530
1531 if (!PACKET_get_length_prefixed_2(pkt, &binders)) {
1532 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1533 goto err;
1534 }
1535
1536 for (i = 0; i <= id; i++) {
1537 if (!PACKET_get_length_prefixed_1(&binders, &binder)) {
1538 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1539 goto err;
1540 }
1541 }
1542
1543 if (PACKET_remaining(&binder) != (size_t)hashsize) {
1544 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1545 goto err;
1546 }
1547 if (tls_psk_do_binder(s, md, (const unsigned char *)s->init_buf->data,
1548 binderoffset, PACKET_data(&binder), NULL, sess, 0,
1549 ext)
1550 != 1) {
1551 /* SSLfatal() already called */
1552 goto err;
1553 }
1554
1555 s->ext.tick_identity = id;
1556
1557 SSL_SESSION_free(s->session);
1558 s->session = sess;
1559 return 1;
1560 err:
1561 SSL_SESSION_free(sess);
1562 return 0;
1563 }
1564
1565 int tls_parse_ctos_post_handshake_auth(SSL_CONNECTION *s, PACKET *pkt,
1566 ossl_unused unsigned int context,
1567 ossl_unused X509 *x,
1568 ossl_unused size_t chainidx)
1569 {
1570 if (PACKET_remaining(pkt) != 0) {
1571 SSLfatal(s, SSL_AD_DECODE_ERROR,
1572 SSL_R_POST_HANDSHAKE_AUTH_ENCODING_ERR);
1573 return 0;
1574 }
1575
1576 s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
1577
1578 return 1;
1579 }
1580
1581 /*
1582 * Add the server's renegotiation binding
1583 */
1584 EXT_RETURN tls_construct_stoc_renegotiate(SSL_CONNECTION *s, WPACKET *pkt,
1585 unsigned int context, X509 *x,
1586 size_t chainidx)
1587 {
1588 if (!s->s3.send_connection_binding)
1589 return EXT_RETURN_NOT_SENT;
1590
1591 /* Still add this even if SSL_OP_NO_RENEGOTIATION is set */
1592 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
1593 || !WPACKET_start_sub_packet_u16(pkt)
1594 || !WPACKET_start_sub_packet_u8(pkt)
1595 || !WPACKET_memcpy(pkt, s->s3.previous_client_finished,
1596 s->s3.previous_client_finished_len)
1597 || !WPACKET_memcpy(pkt, s->s3.previous_server_finished,
1598 s->s3.previous_server_finished_len)
1599 || !WPACKET_close(pkt)
1600 || !WPACKET_close(pkt)) {
1601 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1602 return EXT_RETURN_FAIL;
1603 }
1604
1605 return EXT_RETURN_SENT;
1606 }
1607
1608 EXT_RETURN tls_construct_stoc_server_name(SSL_CONNECTION *s, WPACKET *pkt,
1609 unsigned int context, X509 *x,
1610 size_t chainidx)
1611 {
1612 if (s->servername_done != 1)
1613 return EXT_RETURN_NOT_SENT;
1614
1615 /*
1616 * Prior to TLSv1.3 we ignore any SNI in the current handshake if resuming.
1617 * We just use the servername from the initial handshake.
1618 */
1619 if (s->hit && !SSL_CONNECTION_IS_TLS13(s))
1620 return EXT_RETURN_NOT_SENT;
1621
1622 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
1623 || !WPACKET_put_bytes_u16(pkt, 0)) {
1624 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1625 return EXT_RETURN_FAIL;
1626 }
1627
1628 return EXT_RETURN_SENT;
1629 }
1630
1631 /* Add/include the server's max fragment len extension into ServerHello */
1632 EXT_RETURN tls_construct_stoc_maxfragmentlen(SSL_CONNECTION *s, WPACKET *pkt,
1633 unsigned int context, X509 *x,
1634 size_t chainidx)
1635 {
1636 if (!USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
1637 return EXT_RETURN_NOT_SENT;
1638
1639 /*-
1640 * 4 bytes for this extension type and extension length
1641 * 1 byte for the Max Fragment Length code value.
1642 */
1643 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)
1644 || !WPACKET_start_sub_packet_u16(pkt)
1645 || !WPACKET_put_bytes_u8(pkt, s->session->ext.max_fragment_len_mode)
1646 || !WPACKET_close(pkt)) {
1647 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1648 return EXT_RETURN_FAIL;
1649 }
1650
1651 return EXT_RETURN_SENT;
1652 }
1653
1654 EXT_RETURN tls_construct_stoc_ec_pt_formats(SSL_CONNECTION *s, WPACKET *pkt,
1655 unsigned int context, X509 *x,
1656 size_t chainidx)
1657 {
1658 unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
1659 unsigned long alg_a = s->s3.tmp.new_cipher->algorithm_auth;
1660 int using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))
1661 && (s->ext.peer_ecpointformats != NULL);
1662 const unsigned char *plist;
1663 size_t plistlen;
1664
1665 if (!using_ecc)
1666 return EXT_RETURN_NOT_SENT;
1667
1668 tls1_get_formatlist(s, &plist, &plistlen);
1669 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
1670 || !WPACKET_start_sub_packet_u16(pkt)
1671 || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)
1672 || !WPACKET_close(pkt)) {
1673 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1674 return EXT_RETURN_FAIL;
1675 }
1676
1677 return EXT_RETURN_SENT;
1678 }
1679
1680 EXT_RETURN tls_construct_stoc_supported_groups(SSL_CONNECTION *s, WPACKET *pkt,
1681 unsigned int context, X509 *x,
1682 size_t chainidx)
1683 {
1684 const uint16_t *groups;
1685 size_t numgroups, i, first = 1;
1686 int version;
1687
1688 /* s->s3.group_id is non zero if we accepted a key_share */
1689 if (s->s3.group_id == 0)
1690 return EXT_RETURN_NOT_SENT;
1691
1692 /* Get our list of supported groups */
1693 tls1_get_supported_groups(s, &groups, &numgroups);
1694 if (numgroups == 0) {
1695 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1696 return EXT_RETURN_FAIL;
1697 }
1698
1699 /* Copy group ID if supported */
1700 version = SSL_version(SSL_CONNECTION_GET_SSL(s));
1701 for (i = 0; i < numgroups; i++) {
1702 uint16_t group = groups[i];
1703
1704 if (tls_valid_group(s, group, version, version, 0, NULL)
1705 && tls_group_allowed(s, group, SSL_SECOP_CURVE_SUPPORTED)) {
1706 if (first) {
1707 /*
1708 * Check if the client is already using our preferred group. If
1709 * so we don't need to add this extension
1710 */
1711 if (s->s3.group_id == group)
1712 return EXT_RETURN_NOT_SENT;
1713
1714 /* Add extension header */
1715 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
1716 /* Sub-packet for supported_groups extension */
1717 || !WPACKET_start_sub_packet_u16(pkt)
1718 || !WPACKET_start_sub_packet_u16(pkt)) {
1719 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1720 return EXT_RETURN_FAIL;
1721 }
1722
1723 first = 0;
1724 }
1725 if (!WPACKET_put_bytes_u16(pkt, group)) {
1726 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1727 return EXT_RETURN_FAIL;
1728 }
1729 }
1730 }
1731
1732 if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
1733 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1734 return EXT_RETURN_FAIL;
1735 }
1736
1737 return EXT_RETURN_SENT;
1738 }
1739
1740 EXT_RETURN tls_construct_stoc_session_ticket(SSL_CONNECTION *s, WPACKET *pkt,
1741 unsigned int context, X509 *x,
1742 size_t chainidx)
1743 {
1744 if (!s->ext.ticket_expected || !tls_use_ticket(s)) {
1745 s->ext.ticket_expected = 0;
1746 return EXT_RETURN_NOT_SENT;
1747 }
1748
1749 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
1750 || !WPACKET_put_bytes_u16(pkt, 0)) {
1751 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1752 return EXT_RETURN_FAIL;
1753 }
1754
1755 return EXT_RETURN_SENT;
1756 }
1757
1758 #ifndef OPENSSL_NO_OCSP
1759 EXT_RETURN tls_construct_stoc_status_request(SSL_CONNECTION *s, WPACKET *pkt,
1760 unsigned int context, X509 *x,
1761 size_t chainidx)
1762 {
1763 OCSP_RESPONSE *resp;
1764
1765 /* We don't currently support this extension inside a CertificateRequest */
1766 if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST)
1767 return EXT_RETURN_NOT_SENT;
1768
1769 if (!s->ext.status_expected)
1770 return EXT_RETURN_NOT_SENT;
1771
1772 /* Try to retrieve OCSP response for the actual certificate */
1773 resp = ossl_get_ocsp_response(s, (int)chainidx);
1774
1775 /* If no OCSP response was found the extension is not sent */
1776 if (resp == NULL)
1777 return EXT_RETURN_NOT_SENT;
1778
1779 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
1780 || !WPACKET_start_sub_packet_u16(pkt)) {
1781 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1782 return EXT_RETURN_FAIL;
1783 }
1784
1785 /*
1786 * In TLSv1.3 we include the certificate status itself. In <= TLSv1.2 we
1787 * send back an empty extension, with the certificate status appearing as a
1788 * separate message
1789 */
1790 if (SSL_CONNECTION_IS_TLS13(s)
1791 && !tls_construct_cert_status_body(s, resp, pkt)) {
1792 /* SSLfatal() already called */
1793 return EXT_RETURN_FAIL;
1794 }
1795 if (!WPACKET_close(pkt)) {
1796 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1797 return EXT_RETURN_FAIL;
1798 }
1799
1800 return EXT_RETURN_SENT;
1801 }
1802 #endif
1803
1804 #ifndef OPENSSL_NO_NEXTPROTONEG
1805 EXT_RETURN tls_construct_stoc_next_proto_neg(SSL_CONNECTION *s, WPACKET *pkt,
1806 unsigned int context, X509 *x,
1807 size_t chainidx)
1808 {
1809 const unsigned char *npa;
1810 unsigned int npalen;
1811 int ret;
1812 int npn_seen = s->s3.npn_seen;
1813 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1814
1815 s->s3.npn_seen = 0;
1816 if (!npn_seen || sctx->ext.npn_advertised_cb == NULL)
1817 return EXT_RETURN_NOT_SENT;
1818
1819 ret = sctx->ext.npn_advertised_cb(SSL_CONNECTION_GET_USER_SSL(s), &npa,
1820 &npalen, sctx->ext.npn_advertised_cb_arg);
1821 if (ret == SSL_TLSEXT_ERR_OK) {
1822 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
1823 || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {
1824 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1825 return EXT_RETURN_FAIL;
1826 }
1827 s->s3.npn_seen = 1;
1828 return EXT_RETURN_SENT;
1829 }
1830
1831 return EXT_RETURN_NOT_SENT;
1832 }
1833 #endif
1834
1835 EXT_RETURN tls_construct_stoc_alpn(SSL_CONNECTION *s, WPACKET *pkt, unsigned int context,
1836 X509 *x, size_t chainidx)
1837 {
1838 if (s->s3.alpn_selected == NULL)
1839 return EXT_RETURN_NOT_SENT;
1840
1841 if (!WPACKET_put_bytes_u16(pkt,
1842 TLSEXT_TYPE_application_layer_protocol_negotiation)
1843 || !WPACKET_start_sub_packet_u16(pkt)
1844 || !WPACKET_start_sub_packet_u16(pkt)
1845 || !WPACKET_sub_memcpy_u8(pkt, s->s3.alpn_selected,
1846 s->s3.alpn_selected_len)
1847 || !WPACKET_close(pkt)
1848 || !WPACKET_close(pkt)) {
1849 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1850 return EXT_RETURN_FAIL;
1851 }
1852
1853 return EXT_RETURN_SENT;
1854 }
1855
1856 #ifndef OPENSSL_NO_SRTP
1857 EXT_RETURN tls_construct_stoc_use_srtp(SSL_CONNECTION *s, WPACKET *pkt,
1858 unsigned int context, X509 *x,
1859 size_t chainidx)
1860 {
1861 if (s->srtp_profile == NULL)
1862 return EXT_RETURN_NOT_SENT;
1863
1864 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
1865 || !WPACKET_start_sub_packet_u16(pkt)
1866 || !WPACKET_put_bytes_u16(pkt, 2)
1867 || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)
1868 || !WPACKET_put_bytes_u8(pkt, 0)
1869 || !WPACKET_close(pkt)) {
1870 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1871 return EXT_RETURN_FAIL;
1872 }
1873
1874 return EXT_RETURN_SENT;
1875 }
1876 #endif
1877
1878 EXT_RETURN tls_construct_stoc_etm(SSL_CONNECTION *s, WPACKET *pkt,
1879 unsigned int context,
1880 X509 *x, size_t chainidx)
1881 {
1882 if (!s->ext.use_etm)
1883 return EXT_RETURN_NOT_SENT;
1884
1885 /*
1886 * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1887 * for other cases too.
1888 */
1889 if (s->s3.tmp.new_cipher->algorithm_mac == SSL_AEAD
1890 || s->s3.tmp.new_cipher->algorithm_enc == SSL_RC4
1891 || s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1892 || s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12
1893 || s->s3.tmp.new_cipher->algorithm_enc == SSL_MAGMA
1894 || s->s3.tmp.new_cipher->algorithm_enc == SSL_KUZNYECHIK) {
1895 s->ext.use_etm = 0;
1896 return EXT_RETURN_NOT_SENT;
1897 }
1898
1899 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
1900 || !WPACKET_put_bytes_u16(pkt, 0)) {
1901 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1902 return EXT_RETURN_FAIL;
1903 }
1904
1905 return EXT_RETURN_SENT;
1906 }
1907
1908 EXT_RETURN tls_construct_stoc_ems(SSL_CONNECTION *s, WPACKET *pkt,
1909 unsigned int context,
1910 X509 *x, size_t chainidx)
1911 {
1912 if ((s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0)
1913 return EXT_RETURN_NOT_SENT;
1914
1915 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
1916 || !WPACKET_put_bytes_u16(pkt, 0)) {
1917 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1918 return EXT_RETURN_FAIL;
1919 }
1920
1921 return EXT_RETURN_SENT;
1922 }
1923
1924 EXT_RETURN tls_construct_stoc_supported_versions(SSL_CONNECTION *s, WPACKET *pkt,
1925 unsigned int context, X509 *x,
1926 size_t chainidx)
1927 {
1928 if (!ossl_assert(SSL_CONNECTION_IS_TLS13(s))) {
1929 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1930 return EXT_RETURN_FAIL;
1931 }
1932
1933 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
1934 || !WPACKET_start_sub_packet_u16(pkt)
1935 || !WPACKET_put_bytes_u16(pkt, s->version)
1936 || !WPACKET_close(pkt)) {
1937 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1938 return EXT_RETURN_FAIL;
1939 }
1940
1941 return EXT_RETURN_SENT;
1942 }
1943
1944 EXT_RETURN tls_construct_stoc_key_share(SSL_CONNECTION *s, WPACKET *pkt,
1945 unsigned int context, X509 *x,
1946 size_t chainidx)
1947 {
1948 #ifndef OPENSSL_NO_TLS1_3
1949 unsigned char *encoded_pubkey;
1950 size_t encoded_pubkey_len = 0;
1951 EVP_PKEY *ckey = s->s3.peer_tmp, *skey = NULL;
1952 const TLS_GROUP_INFO *ginf = NULL;
1953
1954 if (s->hello_retry_request == SSL_HRR_PENDING) {
1955 if (ckey != NULL) {
1956 /* Original key_share was acceptable so don't ask for another one */
1957 return EXT_RETURN_NOT_SENT;
1958 }
1959 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
1960 || !WPACKET_start_sub_packet_u16(pkt)
1961 || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)
1962 || !WPACKET_close(pkt)) {
1963 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1964 return EXT_RETURN_FAIL;
1965 }
1966
1967 return EXT_RETURN_SENT;
1968 }
1969
1970 if (ckey == NULL) {
1971 /* No key_share received from client - must be resuming */
1972 if (!s->hit || !tls13_generate_handshake_secret(s, NULL, 0)) {
1973 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1974 return EXT_RETURN_FAIL;
1975 }
1976 return EXT_RETURN_NOT_SENT;
1977 }
1978
1979 if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0) {
1980 /*
1981 * PSK ('hit') and explicitly not doing DHE. If the client sent the
1982 * DHE option, we take it by default, except if non-DHE would be
1983 * preferred by config, but this case would have been handled in
1984 * tls_parse_ctos_psk_kex_modes().
1985 */
1986 return EXT_RETURN_NOT_SENT;
1987 }
1988
1989 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
1990 || !WPACKET_start_sub_packet_u16(pkt)
1991 || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)) {
1992 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1993 return EXT_RETURN_FAIL;
1994 }
1995
1996 if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),
1997 s->s3.group_id))
1998 == NULL) {
1999 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2000 return EXT_RETURN_FAIL;
2001 }
2002
2003 if (!ginf->is_kem) {
2004 /* Regular KEX */
2005 skey = ssl_generate_pkey(s, ckey);
2006 if (skey == NULL) {
2007 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
2008 return EXT_RETURN_FAIL;
2009 }
2010
2011 /* Generate encoding of server key */
2012 encoded_pubkey_len = EVP_PKEY_get1_encoded_public_key(skey, &encoded_pubkey);
2013 if (encoded_pubkey_len == 0) {
2014 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
2015 EVP_PKEY_free(skey);
2016 return EXT_RETURN_FAIL;
2017 }
2018
2019 if (!WPACKET_sub_memcpy_u16(pkt, encoded_pubkey, encoded_pubkey_len)
2020 || !WPACKET_close(pkt)) {
2021 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2022 EVP_PKEY_free(skey);
2023 OPENSSL_free(encoded_pubkey);
2024 return EXT_RETURN_FAIL;
2025 }
2026 OPENSSL_free(encoded_pubkey);
2027
2028 /*
2029 * This causes the crypto state to be updated based on the derived keys
2030 */
2031 s->s3.tmp.pkey = skey;
2032 if (ssl_derive(s, skey, ckey, 1) == 0) {
2033 /* SSLfatal() already called */
2034 return EXT_RETURN_FAIL;
2035 }
2036 } else {
2037 /* KEM mode */
2038 unsigned char *ct = NULL;
2039 size_t ctlen = 0;
2040
2041 /*
2042 * This does not update the crypto state.
2043 *
2044 * The generated pms is stored in `s->s3.tmp.pms` to be later used via
2045 * ssl_gensecret().
2046 */
2047 if (ssl_encapsulate(s, ckey, &ct, &ctlen, 0) == 0) {
2048 /* SSLfatal() already called */
2049 return EXT_RETURN_FAIL;
2050 }
2051
2052 if (ctlen == 0) {
2053 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2054 OPENSSL_free(ct);
2055 return EXT_RETURN_FAIL;
2056 }
2057
2058 if (!WPACKET_sub_memcpy_u16(pkt, ct, ctlen)
2059 || !WPACKET_close(pkt)) {
2060 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2061 OPENSSL_free(ct);
2062 return EXT_RETURN_FAIL;
2063 }
2064 OPENSSL_free(ct);
2065
2066 /*
2067 * This causes the crypto state to be updated based on the generated pms
2068 */
2069 if (ssl_gensecret(s, s->s3.tmp.pms, s->s3.tmp.pmslen) == 0) {
2070 /* SSLfatal() already called */
2071 return EXT_RETURN_FAIL;
2072 }
2073 }
2074 s->s3.did_kex = 1;
2075 return EXT_RETURN_SENT;
2076 #else
2077 return EXT_RETURN_FAIL;
2078 #endif
2079 }
2080
2081 EXT_RETURN tls_construct_stoc_cookie(SSL_CONNECTION *s, WPACKET *pkt,
2082 unsigned int context,
2083 X509 *x, size_t chainidx)
2084 {
2085 #ifndef OPENSSL_NO_TLS1_3
2086 unsigned char *hashval1, *hashval2, *appcookie1, *appcookie2, *cookie;
2087 unsigned char *hmac, *hmac2;
2088 size_t startlen, ciphlen, totcookielen, hashlen, hmaclen, appcookielen;
2089 EVP_MD_CTX *hctx;
2090 EVP_PKEY *pkey;
2091 int ret = EXT_RETURN_FAIL;
2092 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2093 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
2094 SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
2095
2096 if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
2097 return EXT_RETURN_NOT_SENT;
2098
2099 if (sctx->gen_stateless_cookie_cb == NULL) {
2100 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_COOKIE_CALLBACK_SET);
2101 return EXT_RETURN_FAIL;
2102 }
2103
2104 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
2105 || !WPACKET_start_sub_packet_u16(pkt)
2106 || !WPACKET_start_sub_packet_u16(pkt)
2107 || !WPACKET_get_total_written(pkt, &startlen)
2108 || !WPACKET_reserve_bytes(pkt, MAX_COOKIE_SIZE, &cookie)
2109 || !WPACKET_put_bytes_u16(pkt, COOKIE_STATE_FORMAT_VERSION)
2110 || !WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION)
2111 || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)
2112 || !ssl->method->put_cipher_by_char(s->s3.tmp.new_cipher, pkt,
2113 &ciphlen)
2114 /* Is there a key_share extension present in this HRR? */
2115 || !WPACKET_put_bytes_u8(pkt, s->s3.peer_tmp == NULL)
2116 || !WPACKET_put_bytes_u64(pkt, time(NULL))
2117 || !WPACKET_start_sub_packet_u16(pkt)
2118 || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &hashval1)) {
2119 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2120 return EXT_RETURN_FAIL;
2121 }
2122
2123 /*
2124 * Get the hash of the initial ClientHello. ssl_handshake_hash() operates
2125 * on raw buffers, so we first reserve sufficient bytes (above) and then
2126 * subsequently allocate them (below)
2127 */
2128 if (!ssl3_digest_cached_records(s, 0)
2129 || !ssl_handshake_hash(s, hashval1, EVP_MAX_MD_SIZE, &hashlen)) {
2130 /* SSLfatal() already called */
2131 return EXT_RETURN_FAIL;
2132 }
2133
2134 if (!WPACKET_allocate_bytes(pkt, hashlen, &hashval2)
2135 || !ossl_assert(hashval1 == hashval2)
2136 || !WPACKET_close(pkt)
2137 || !WPACKET_start_sub_packet_u8(pkt)
2138 || !WPACKET_reserve_bytes(pkt, SSL_COOKIE_LENGTH, &appcookie1)) {
2139 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2140 return EXT_RETURN_FAIL;
2141 }
2142
2143 /* Generate the application cookie */
2144 if (sctx->gen_stateless_cookie_cb(ussl, appcookie1,
2145 &appcookielen)
2146 == 0) {
2147 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
2148 return EXT_RETURN_FAIL;
2149 }
2150
2151 if (!WPACKET_allocate_bytes(pkt, appcookielen, &appcookie2)
2152 || !ossl_assert(appcookie1 == appcookie2)
2153 || !WPACKET_close(pkt)
2154 || !WPACKET_get_total_written(pkt, &totcookielen)
2155 || !WPACKET_reserve_bytes(pkt, SHA256_DIGEST_LENGTH, &hmac)) {
2156 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2157 return EXT_RETURN_FAIL;
2158 }
2159 hmaclen = SHA256_DIGEST_LENGTH;
2160
2161 totcookielen -= startlen;
2162 if (!ossl_assert(totcookielen <= MAX_COOKIE_SIZE - SHA256_DIGEST_LENGTH)) {
2163 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2164 return EXT_RETURN_FAIL;
2165 }
2166
2167 /* HMAC the cookie */
2168 hctx = EVP_MD_CTX_create();
2169 pkey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",
2170 sctx->propq,
2171 s->session_ctx->ext.cookie_hmac_key,
2172 sizeof(s->session_ctx->ext.cookie_hmac_key));
2173 if (hctx == NULL || pkey == NULL) {
2174 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2175 goto err;
2176 }
2177
2178 if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", sctx->libctx,
2179 sctx->propq, pkey, NULL)
2180 <= 0
2181 || EVP_DigestSign(hctx, hmac, &hmaclen, cookie,
2182 totcookielen)
2183 <= 0) {
2184 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2185 goto err;
2186 }
2187
2188 if (!ossl_assert(totcookielen + hmaclen <= MAX_COOKIE_SIZE)) {
2189 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2190 goto err;
2191 }
2192
2193 if (!WPACKET_allocate_bytes(pkt, hmaclen, &hmac2)
2194 || !ossl_assert(hmac == hmac2)
2195 || !ossl_assert(cookie == hmac - totcookielen)
2196 || !WPACKET_close(pkt)
2197 || !WPACKET_close(pkt)) {
2198 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2199 goto err;
2200 }
2201
2202 ret = EXT_RETURN_SENT;
2203
2204 err:
2205 EVP_MD_CTX_free(hctx);
2206 EVP_PKEY_free(pkey);
2207 return ret;
2208 #else
2209 return EXT_RETURN_FAIL;
2210 #endif
2211 }
2212
2213 EXT_RETURN tls_construct_stoc_cryptopro_bug(SSL_CONNECTION *s, WPACKET *pkt,
2214 unsigned int context, X509 *x,
2215 size_t chainidx)
2216 {
2217 const unsigned char cryptopro_ext[36] = {
2218 0xfd, 0xe8, /* 65000 */
2219 0x00, 0x20, /* 32 bytes length */
2220 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
2221 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
2222 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
2223 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
2224 };
2225
2226 if (((s->s3.tmp.new_cipher->id & 0xFFFF) != 0x80
2227 && (s->s3.tmp.new_cipher->id & 0xFFFF) != 0x81)
2228 || (SSL_get_options(SSL_CONNECTION_GET_SSL(s))
2229 & SSL_OP_CRYPTOPRO_TLSEXT_BUG)
2230 == 0)
2231 return EXT_RETURN_NOT_SENT;
2232
2233 if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {
2234 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2235 return EXT_RETURN_FAIL;
2236 }
2237
2238 return EXT_RETURN_SENT;
2239 }
2240
2241 EXT_RETURN tls_construct_stoc_early_data(SSL_CONNECTION *s, WPACKET *pkt,
2242 unsigned int context, X509 *x,
2243 size_t chainidx)
2244 {
2245 if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
2246 if (s->max_early_data == 0)
2247 return EXT_RETURN_NOT_SENT;
2248
2249 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
2250 || !WPACKET_start_sub_packet_u16(pkt)
2251 || !WPACKET_put_bytes_u32(pkt, s->max_early_data)
2252 || !WPACKET_close(pkt)) {
2253 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2254 return EXT_RETURN_FAIL;
2255 }
2256
2257 return EXT_RETURN_SENT;
2258 }
2259
2260 if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
2261 return EXT_RETURN_NOT_SENT;
2262
2263 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
2264 || !WPACKET_start_sub_packet_u16(pkt)
2265 || !WPACKET_close(pkt)) {
2266 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2267 return EXT_RETURN_FAIL;
2268 }
2269
2270 return EXT_RETURN_SENT;
2271 }
2272
2273 EXT_RETURN tls_construct_stoc_psk(SSL_CONNECTION *s, WPACKET *pkt,
2274 unsigned int context,
2275 X509 *x, size_t chainidx)
2276 {
2277 if (!s->hit)
2278 return EXT_RETURN_NOT_SENT;
2279
2280 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
2281 || !WPACKET_start_sub_packet_u16(pkt)
2282 || !WPACKET_put_bytes_u16(pkt, s->ext.tick_identity)
2283 || !WPACKET_close(pkt)) {
2284 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2285 return EXT_RETURN_FAIL;
2286 }
2287
2288 return EXT_RETURN_SENT;
2289 }
2290
2291 EXT_RETURN tls_construct_stoc_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2292 unsigned int context,
2293 X509 *x, size_t chainidx)
2294 {
2295 if (sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_ERROR
2296 && (send_certificate_request(sc)
2297 || sc->post_handshake_auth == SSL_PHA_EXT_RECEIVED)) {
2298 /* Did not receive an acceptable cert type - and doing client auth */
2299 SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION);
2300 return EXT_RETURN_FAIL;
2301 }
2302
2303 if (sc->ext.client_cert_type == TLSEXT_cert_type_x509) {
2304 sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2305 return EXT_RETURN_NOT_SENT;
2306 }
2307
2308 /*
2309 * Note: only supposed to send this if we are going to do a cert request,
2310 * but TLSv1.3 could do a PHA request if the client supports it
2311 */
2312 if ((!send_certificate_request(sc) && sc->post_handshake_auth != SSL_PHA_EXT_RECEIVED)
2313 || sc->ext.client_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD
2314 || sc->client_cert_type == NULL) {
2315 /* if we don't send it, reset to TLSEXT_cert_type_x509 */
2316 sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2317 sc->ext.client_cert_type = TLSEXT_cert_type_x509;
2318 return EXT_RETURN_NOT_SENT;
2319 }
2320
2321 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type)
2322 || !WPACKET_start_sub_packet_u16(pkt)
2323 || !WPACKET_put_bytes_u8(pkt, sc->ext.client_cert_type)
2324 || !WPACKET_close(pkt)) {
2325 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2326 return EXT_RETURN_FAIL;
2327 }
2328 return EXT_RETURN_SENT;
2329 }
2330
2331 /* One of |pref|, |other| is configured and the values are sanitized */
2332 static int reconcile_cert_type(const unsigned char *pref, size_t pref_len,
2333 const unsigned char *other, size_t other_len,
2334 uint8_t *chosen_cert_type)
2335 {
2336 size_t i;
2337
2338 for (i = 0; i < pref_len; i++) {
2339 if (memchr(other, pref[i], other_len) != NULL) {
2340 *chosen_cert_type = pref[i];
2341 return OSSL_CERT_TYPE_CTOS_GOOD;
2342 }
2343 }
2344 return OSSL_CERT_TYPE_CTOS_ERROR;
2345 }
2346
2347 int tls_parse_ctos_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2348 unsigned int context,
2349 X509 *x, size_t chainidx)
2350 {
2351 PACKET supported_cert_types;
2352 const unsigned char *data;
2353 size_t len;
2354
2355 /* Ignore the extension */
2356 if (sc->client_cert_type == NULL) {
2357 sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2358 sc->ext.client_cert_type = TLSEXT_cert_type_x509;
2359 return 1;
2360 }
2361
2362 if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) {
2363 sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;
2364 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2365 return 0;
2366 }
2367 if ((len = PACKET_remaining(&supported_cert_types)) == 0) {
2368 sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;
2369 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2370 return 0;
2371 }
2372 if (!PACKET_get_bytes(&supported_cert_types, &data, len)) {
2373 sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;
2374 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2375 return 0;
2376 }
2377 /* client_cert_type: client (peer) has priority */
2378 sc->ext.client_cert_type_ctos = reconcile_cert_type(data, len,
2379 sc->client_cert_type, sc->client_cert_type_len,
2380 &sc->ext.client_cert_type);
2381
2382 /* Ignore the error until sending - so we can check cert auth*/
2383 return 1;
2384 }
2385
2386 EXT_RETURN tls_construct_stoc_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2387 unsigned int context,
2388 X509 *x, size_t chainidx)
2389 {
2390 if (sc->ext.server_cert_type == TLSEXT_cert_type_x509) {
2391 sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2392 return EXT_RETURN_NOT_SENT;
2393 }
2394 if (sc->ext.server_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD
2395 || sc->server_cert_type == NULL) {
2396 /* if we don't send it, reset to TLSEXT_cert_type_x509 */
2397 sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2398 sc->ext.server_cert_type = TLSEXT_cert_type_x509;
2399 return EXT_RETURN_NOT_SENT;
2400 }
2401
2402 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type)
2403 || !WPACKET_start_sub_packet_u16(pkt)
2404 || !WPACKET_put_bytes_u8(pkt, sc->ext.server_cert_type)
2405 || !WPACKET_close(pkt)) {
2406 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2407 return EXT_RETURN_FAIL;
2408 }
2409 return EXT_RETURN_SENT;
2410 }
2411
2412 int tls_parse_ctos_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2413 unsigned int context,
2414 X509 *x, size_t chainidx)
2415 {
2416 PACKET supported_cert_types;
2417 const unsigned char *data;
2418 size_t len;
2419
2420 /* Ignore the extension */
2421 if (sc->server_cert_type == NULL) {
2422 sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2423 sc->ext.server_cert_type = TLSEXT_cert_type_x509;
2424 return 1;
2425 }
2426
2427 if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) {
2428 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2429 return 0;
2430 }
2431
2432 if ((len = PACKET_remaining(&supported_cert_types)) == 0) {
2433 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2434 return 0;
2435 }
2436 if (!PACKET_get_bytes(&supported_cert_types, &data, len)) {
2437 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2438 return 0;
2439 }
2440 /* server_cert_type: server (this) has priority */
2441 sc->ext.server_cert_type_ctos = reconcile_cert_type(sc->server_cert_type, sc->server_cert_type_len,
2442 data, len,
2443 &sc->ext.server_cert_type);
2444 if (sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)
2445 return 1;
2446
2447 /* Did not receive an acceptable cert type */
2448 SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION);
2449 return 0;
2450 }