Coverage Report

Created: 2026-09-12 06:55

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