Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/ssl/statem/extensions_clnt.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 "internal/cryptlib.h"
13
#include "internal/ssl_unwrap.h"
14
#include "internal/tlsgroups.h"
15
#include "statem_local.h"
16
#ifndef OPENSSL_NO_ECH
17
#include <openssl/rand.h>
18
#include "internal/ech_helpers.h"
19
#endif
20
21
/* Used in the negotiate_dhe function */
22
typedef enum {
23
    ffdhe_check,
24
    ecdhe_check,
25
    ptfmt_check
26
} dhe_check_t;
27
28
EXT_RETURN tls_construct_ctos_renegotiate(SSL_CONNECTION *s, WPACKET *pkt,
29
    unsigned int context, X509 *x,
30
    size_t chainidx)
31
17.9k
{
32
17.9k
    if (!s->renegotiate) {
33
        /* If not renegotiating, send an empty RI extension to indicate support */
34
35
#if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION
36
#error Internal DTLS version error
37
#endif
38
39
17.6k
        if (!SSL_CONNECTION_IS_DTLS(s)
40
14.4k
            && (s->min_proto_version >= TLS1_3_VERSION
41
5.95k
                || (ssl_security(s, SSL_SECOP_VERSION, 0, TLS1_VERSION, NULL)
42
14.4k
                    && s->min_proto_version <= TLS1_VERSION))) {
43
            /*
44
             * For TLS <= 1.0 SCSV is used instead, and for TLS 1.3 this
45
             * extension isn't used at all.
46
             */
47
14.4k
            return EXT_RETURN_NOT_SENT;
48
14.4k
        }
49
50
3.24k
#ifndef OPENSSL_NO_ECH
51
3.24k
        ECH_SAME_EXT(s, context, pkt)
52
3.24k
#endif
53
54
3.24k
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
55
3.24k
            || !WPACKET_start_sub_packet_u16(pkt)
56
3.24k
            || !WPACKET_put_bytes_u8(pkt, 0)
57
3.24k
            || !WPACKET_close(pkt)) {
58
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
59
0
            return EXT_RETURN_FAIL;
60
0
        }
61
62
3.24k
        return EXT_RETURN_SENT;
63
3.24k
    }
64
65
232
#ifndef OPENSSL_NO_ECH
66
232
    ECH_SAME_EXT(s, context, pkt)
67
232
#endif
68
69
    /* Add a complete RI extension if renegotiating */
70
232
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
71
232
        || !WPACKET_start_sub_packet_u16(pkt)
72
232
        || !WPACKET_sub_memcpy_u8(pkt, s->s3.previous_client_finished,
73
232
            s->s3.previous_client_finished_len)
74
232
        || !WPACKET_close(pkt)) {
75
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
76
0
        return EXT_RETURN_FAIL;
77
0
    }
78
79
232
    return EXT_RETURN_SENT;
80
232
}
81
82
EXT_RETURN tls_construct_ctos_server_name(SSL_CONNECTION *s, WPACKET *pkt,
83
    unsigned int context, X509 *x,
84
    size_t chainidx)
85
60.8k
{
86
60.8k
    char *chosen = s->ext.hostname;
87
60.8k
#ifndef OPENSSL_NO_ECH
88
60.8k
    OSSL_HPKE_SUITE suite;
89
60.8k
    OSSL_ECHSTORE_ENTRY *ee = NULL;
90
91
60.8k
    if (s->ext.ech.es != NULL) {
92
0
        if (ossl_ech_pick_matching_cfg(s, &ee, &suite) != 1) {
93
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
94
0
            return EXT_RETURN_NOT_SENT;
95
0
        }
96
        /* Don't send outer SNI if external API says so */
97
0
        if (s->ext.ech.ch_depth == 0 && s->ext.ech.no_outer == 1)
98
0
            return EXT_RETURN_NOT_SENT;
99
0
        if (s->ext.ech.ch_depth == 1) /* inner */
100
0
            chosen = s->ext.hostname;
101
0
        if (s->ext.ech.ch_depth == 0) { /* outer */
102
0
            if (s->ext.ech.outer_hostname != NULL) /* prefer API */
103
0
                chosen = s->ext.ech.outer_hostname;
104
0
            else /* use name from ECHConfig */
105
0
                chosen = ee->public_name;
106
0
        }
107
0
    }
108
60.8k
#endif
109
60.8k
    if (chosen == NULL)
110
0
        return EXT_RETURN_NOT_SENT;
111
    /* Add TLS extension servername to the Client Hello message */
112
60.8k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
113
        /* Sub-packet for server_name extension */
114
60.8k
        || !WPACKET_start_sub_packet_u16(pkt)
115
        /* Sub-packet for servername list (always 1 hostname)*/
116
60.8k
        || !WPACKET_start_sub_packet_u16(pkt)
117
60.8k
        || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)
118
60.8k
        || !WPACKET_sub_memcpy_u16(pkt, chosen, strlen(chosen))
119
60.8k
        || !WPACKET_close(pkt)
120
60.8k
        || !WPACKET_close(pkt)) {
121
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
122
0
        return EXT_RETURN_FAIL;
123
0
    }
124
60.8k
    return EXT_RETURN_SENT;
125
60.8k
}
126
127
/* Push a Max Fragment Len extension into ClientHello */
128
EXT_RETURN tls_construct_ctos_maxfragmentlen(SSL_CONNECTION *s, WPACKET *pkt,
129
    unsigned int context, X509 *x,
130
    size_t chainidx)
131
60.8k
{
132
60.8k
    if (s->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_DISABLED)
133
60.8k
        return EXT_RETURN_NOT_SENT;
134
0
#ifndef OPENSSL_NO_ECH
135
0
    ECH_SAME_EXT(s, context, pkt)
136
0
#endif
137
138
    /* Add Max Fragment Length extension if client enabled it. */
139
    /*-
140
     * 4 bytes for this extension type and extension length
141
     * 1 byte for the Max Fragment Length code value.
142
     */
143
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)
144
        /* Sub-packet for Max Fragment Length extension (1 byte) */
145
0
        || !WPACKET_start_sub_packet_u16(pkt)
146
0
        || !WPACKET_put_bytes_u8(pkt, s->ext.max_fragment_len_mode)
147
0
        || !WPACKET_close(pkt)) {
148
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
149
0
        return EXT_RETURN_FAIL;
150
0
    }
151
152
0
    return EXT_RETURN_SENT;
153
0
}
154
155
#ifndef OPENSSL_NO_SRP
156
EXT_RETURN tls_construct_ctos_srp(SSL_CONNECTION *s, WPACKET *pkt,
157
    unsigned int context,
158
    X509 *x, size_t chainidx)
159
60.8k
{
160
    /* Add SRP username if there is one */
161
60.8k
    if (s->srp_ctx.login == NULL)
162
60.8k
        return EXT_RETURN_NOT_SENT;
163
0
#ifndef OPENSSL_NO_ECH
164
0
    ECH_SAME_EXT(s, context, pkt)
165
0
#endif
166
167
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)
168
        /* Sub-packet for SRP extension */
169
0
        || !WPACKET_start_sub_packet_u16(pkt)
170
0
        || !WPACKET_start_sub_packet_u8(pkt)
171
        /* login must not be zero...internal error if so */
172
0
        || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
173
0
        || !WPACKET_memcpy(pkt, s->srp_ctx.login,
174
0
            strlen(s->srp_ctx.login))
175
0
        || !WPACKET_close(pkt)
176
0
        || !WPACKET_close(pkt)) {
177
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
178
0
        return EXT_RETURN_FAIL;
179
0
    }
180
181
0
    return EXT_RETURN_SENT;
182
0
}
183
#endif
184
185
/*
186
 * With (D)TLS < 1.3 the only negotiated supported key exchange groups are
187
 * FFDHE (RFC7919) and ECDHE/ECX (RFC8422 + legacy).  With (D)TLS 1.3, we add
188
 * KEMs, and the supported groups are no longer cipher-dependent.
189
 *
190
 * This function serves two purposes:
191
 *
192
 * - To determine whether to send the supported point formats extension.
193
 *   This is no longer applicable with (D)TLS >= 1.3.
194
 * - To determine whether to send the supported groups extension.
195
 *
196
 * In the former case, we only care about whether both ECC ciphers and EC/ECX
197
 * supported groups are configured, and the (D)TLS min version is at most 1.2.
198
 *
199
 * In the latter case, we also admit DHE ciphers with FFDHE groups, or any TLS
200
 * 1.3 cipher, since the extension is effectively mandatory for (D)TLS 1.3,
201
 * with the sole exception of psk-ke resumption, provided the client is sure
202
 * that the server will not want elect a full handshake. The check type then
203
 * indicates whether ECDHE or FFDHE negotiation should be performed.
204
 */
205
static int negotiate_dhe(SSL_CONNECTION *s, dhe_check_t check_type,
206
    int min_version, int max_version)
207
53.7k
{
208
53.7k
    int i, end, ret = 0;
209
53.7k
    STACK_OF(SSL_CIPHER) *cipher_stack = NULL;
210
53.7k
    const uint16_t *pgroups = NULL;
211
53.7k
    size_t num_groups, j;
212
53.7k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
213
53.7k
    int dtls = SSL_CONNECTION_IS_DTLS(s);
214
215
    /* See if we support any EC or FFDHE ciphersuites */
216
53.7k
    cipher_stack = SSL_get1_supported_ciphers(ssl);
217
53.7k
    end = sk_SSL_CIPHER_num(cipher_stack);
218
104k
    for (i = 0; i < end; i++) {
219
95.5k
        const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
220
95.5k
        unsigned long alg_k = c->algorithm_mkey;
221
95.5k
        unsigned long alg_a = c->algorithm_auth;
222
223
95.5k
        int is_ffdhe_ciphersuite = (alg_k & (SSL_kDHE | SSL_kDHEPSK));
224
95.5k
        int is_ec_ciphersuite = ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
225
75.7k
            || (alg_a & SSL_aECDSA));
226
95.5k
        int is_tls13 = (dtls ? DTLS_VERSION_GT(c->min_dtls, DTLS1_2_VERSION)
227
95.5k
                             : (c->min_tls > TLS1_2_VERSION));
228
229
95.5k
        if ((check_type == ffdhe_check && (is_ffdhe_ciphersuite || is_tls13))
230
77.6k
            || (check_type == ecdhe_check && (is_ec_ciphersuite || is_tls13))
231
59.7k
            || (check_type == ptfmt_check && is_ec_ciphersuite)) {
232
45.2k
            ret = 1;
233
45.2k
            break;
234
45.2k
        }
235
95.5k
    }
236
53.7k
    sk_SSL_CIPHER_free(cipher_stack);
237
53.7k
    if (ret == 0)
238
8.49k
        return 0;
239
240
    /* Check we have at least one EC or FFDHE supported group */
241
45.2k
    tls1_get_supported_groups(s, &pgroups, &num_groups);
242
295k
    for (j = 0; j < num_groups; j++) {
243
291k
        uint16_t ctmp = pgroups[j];
244
291k
        const TLS_GROUP_INFO *ginfo = NULL;
245
246
291k
        if (!tls_valid_group(s, ctmp, min_version, max_version, NULL, &ginfo))
247
41.2k
            continue;
248
249
250k
        if (check_type == ffdhe_check && is_ffdhe_group(ginfo->group_id)
250
14.6k
            && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED))
251
14.6k
            return 1;
252
253
235k
        if (check_type != ffdhe_check && is_ecdhe_group(ginfo->group_id)
254
27.3k
            && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED))
255
27.3k
            return 1;
256
235k
    }
257
3.24k
    return 0;
258
45.2k
}
259
260
EXT_RETURN tls_construct_ctos_ec_pt_formats(SSL_CONNECTION *s, WPACKET *pkt,
261
    unsigned int context, X509 *x,
262
    size_t chainidx)
263
17.9k
{
264
17.9k
    const unsigned char *pformats;
265
17.9k
    size_t num_formats;
266
17.9k
    int reason, min_version, max_version;
267
268
17.9k
    reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
269
17.9k
    if (reason != 0) {
270
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
271
0
        return EXT_RETURN_FAIL;
272
0
    }
273
17.9k
    if (!negotiate_dhe(s, ptfmt_check, min_version, max_version))
274
8.49k
        return EXT_RETURN_NOT_SENT;
275
276
9.42k
    tls1_get_formatlist(s, &pformats, &num_formats);
277
9.42k
    if (num_formats == 0)
278
0
        return EXT_RETURN_NOT_SENT;
279
9.42k
#ifndef OPENSSL_NO_ECH
280
9.42k
    ECH_SAME_EXT(s, context, pkt)
281
9.42k
#endif
282
283
    /* Add TLS extension ECPointFormats to the ClientHello message */
284
9.42k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
285
        /* Sub-packet for formats extension */
286
9.42k
        || !WPACKET_start_sub_packet_u16(pkt)
287
9.42k
        || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)
288
9.42k
        || !WPACKET_close(pkt)) {
289
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
290
0
        return EXT_RETURN_FAIL;
291
0
    }
292
293
9.42k
    return EXT_RETURN_SENT;
294
9.42k
}
295
296
EXT_RETURN tls_construct_ctos_supported_groups(SSL_CONNECTION *s, WPACKET *pkt,
297
    unsigned int context, X509 *x,
298
    size_t chainidx)
299
17.9k
{
300
17.9k
    const uint16_t *pgroups = NULL;
301
17.9k
    size_t num_groups = 0, i, tls13added = 0, added = 0;
302
17.9k
    int min_version, max_version, reason;
303
17.9k
    int dtls = SSL_CONNECTION_IS_DTLS(s);
304
17.9k
    int use_ecdhe, use_ffdhe;
305
306
17.9k
    reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
307
17.9k
    if (reason != 0) {
308
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
309
0
        return EXT_RETURN_FAIL;
310
0
    }
311
312
    /*
313
     * If we don't support suitable groups, don't send the extension
314
     */
315
17.9k
    use_ecdhe = negotiate_dhe(s, ecdhe_check, min_version, max_version);
316
17.9k
    use_ffdhe = negotiate_dhe(s, ffdhe_check, min_version, max_version);
317
17.9k
    if (!use_ecdhe && !use_ffdhe
318
1
        && (dtls ? DTLS_VERSION_LE(max_version, DTLS1_2_VERSION)
319
1
                 : (max_version <= TLS1_2_VERSION)))
320
1
        return EXT_RETURN_NOT_SENT;
321
17.9k
#ifndef OPENSSL_NO_ECH
322
17.9k
    ECH_SAME_EXT(s, context, pkt)
323
17.9k
#endif
324
325
    /*
326
     * Add TLS extension supported_groups to the ClientHello message
327
     */
328
17.9k
    tls1_get_supported_groups(s, &pgroups, &num_groups);
329
330
17.9k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
331
        /* Sub-packet for supported_groups extension */
332
17.9k
        || !WPACKET_start_sub_packet_u16(pkt)
333
17.9k
        || !WPACKET_start_sub_packet_u16(pkt)
334
17.9k
        || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)) {
335
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
336
0
        return EXT_RETURN_FAIL;
337
0
    }
338
    /* Copy group ID if supported */
339
215k
    for (i = 0; i < num_groups; i++) {
340
197k
        const TLS_GROUP_INFO *ginfo = NULL;
341
197k
        uint16_t ctmp = pgroups[i];
342
197k
        int okfortls13;
343
344
197k
        if (!tls_valid_group(s, ctmp, min_version, max_version, &okfortls13,
345
197k
                &ginfo)
346
176k
            || (!use_ecdhe && is_ecdhe_group(ginfo->group_id))
347
176k
            || (!use_ffdhe && is_ffdhe_group(ginfo->group_id))
348
            /* Note: SSL_SECOP_CURVE_SUPPORTED covers all key exchange groups */
349
176k
            || !tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED))
350
20.3k
            continue;
351
352
176k
        if (!WPACKET_put_bytes_u16(pkt, ctmp)) {
353
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
354
0
            return EXT_RETURN_FAIL;
355
0
        }
356
176k
        if (okfortls13 && max_version == TLS1_3_VERSION)
357
158k
            tls13added++;
358
176k
        added++;
359
176k
    }
360
17.9k
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
361
0
        if (added == 0)
362
0
            SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS,
363
0
                "No groups enabled for max supported SSL/TLS version");
364
0
        else
365
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
366
0
        return EXT_RETURN_FAIL;
367
0
    }
368
369
17.9k
    if (tls13added == 0 && max_version == TLS1_3_VERSION) {
370
0
        SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS,
371
0
            "No groups enabled for max supported SSL/TLS version");
372
0
        return EXT_RETURN_FAIL;
373
0
    }
374
375
17.9k
    return EXT_RETURN_SENT;
376
17.9k
}
377
378
EXT_RETURN tls_construct_ctos_session_ticket(SSL_CONNECTION *s, WPACKET *pkt,
379
    unsigned int context, X509 *x,
380
    size_t chainidx)
381
17.9k
{
382
17.9k
    size_t ticklen;
383
384
17.9k
    if (!tls_use_ticket(s))
385
0
        return EXT_RETURN_NOT_SENT;
386
17.9k
#ifndef OPENSSL_NO_ECH
387
17.9k
    ECH_SAME_EXT(s, context, pkt)
388
17.9k
#endif
389
390
17.9k
    if (!s->new_session && s->session != NULL
391
17.9k
        && s->session->ext.tick != NULL
392
79
        && s->session->ssl_version != TLS1_3_VERSION) {
393
79
        ticklen = s->session->ext.ticklen;
394
17.8k
    } else if (s->session && s->ext.session_ticket != NULL
395
0
        && s->ext.session_ticket->data != NULL) {
396
0
        ticklen = s->ext.session_ticket->length;
397
0
        s->session->ext.tick = OPENSSL_malloc(ticklen);
398
0
        if (s->session->ext.tick == NULL) {
399
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
400
0
            return EXT_RETURN_FAIL;
401
0
        }
402
0
        memcpy(s->session->ext.tick,
403
0
            s->ext.session_ticket->data, ticklen);
404
0
        s->session->ext.ticklen = ticklen;
405
17.8k
    } else {
406
17.8k
        ticklen = 0;
407
17.8k
    }
408
409
17.9k
    if (ticklen == 0 && s->ext.session_ticket != NULL && s->ext.session_ticket->data == NULL)
410
0
        return EXT_RETURN_NOT_SENT;
411
412
17.9k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
413
17.9k
        || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, ticklen)) {
414
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
415
0
        return EXT_RETURN_FAIL;
416
0
    }
417
418
17.9k
    return EXT_RETURN_SENT;
419
17.9k
}
420
421
EXT_RETURN tls_construct_ctos_sig_algs(SSL_CONNECTION *s, WPACKET *pkt,
422
    unsigned int context, X509 *x,
423
    size_t chainidx)
424
17.9k
{
425
17.9k
    size_t salglen;
426
17.9k
    const uint16_t *salg;
427
428
    /*
429
     * This used both in the initial hello and as part of renegotiation,
430
     * in the latter case, the client version may be already set and may
431
     * be lower than that initially offered in `client_version`.
432
     */
433
17.9k
    if (!SSL_CONNECTION_IS_DTLS(s)) {
434
14.6k
        if (s->client_version < TLS1_2_VERSION
435
14.6k
            || (s->ssl.method->version != TLS_ANY_VERSION
436
232
                && s->version < TLS1_2_VERSION))
437
221
            return EXT_RETURN_NOT_SENT;
438
14.6k
    } else {
439
3.24k
        if (DTLS_VERSION_LT(s->client_version, DTLS1_2_VERSION)
440
3.23k
            || (s->ssl.method->version != DTLS_ANY_VERSION
441
10
                && DTLS_VERSION_LT(s->version, DTLS1_2_VERSION)))
442
14
            return EXT_RETURN_NOT_SENT;
443
3.24k
    }
444
445
17.6k
#ifndef OPENSSL_NO_ECH
446
17.6k
    ECH_SAME_EXT(s, context, pkt)
447
17.6k
#endif
448
449
17.6k
    salglen = tls12_get_psigalgs(s, 1, &salg);
450
17.6k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)
451
        /* Sub-packet for sig-algs extension */
452
17.6k
        || !WPACKET_start_sub_packet_u16(pkt)
453
        /* Sub-packet for the actual list */
454
17.6k
        || !WPACKET_start_sub_packet_u16(pkt)
455
17.6k
        || !tls12_copy_sigalgs(s, pkt, salg, salglen)
456
17.6k
        || !WPACKET_close(pkt)
457
17.6k
        || !WPACKET_close(pkt)) {
458
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
459
0
        return EXT_RETURN_FAIL;
460
0
    }
461
462
17.6k
    return EXT_RETURN_SENT;
463
17.6k
}
464
465
#ifndef OPENSSL_NO_OCSP
466
EXT_RETURN tls_construct_ctos_status_request(SSL_CONNECTION *s, WPACKET *pkt,
467
    unsigned int context, X509 *x,
468
    size_t chainidx)
469
60.8k
{
470
60.8k
    int i;
471
472
    /* This extension isn't defined for client Certificates */
473
60.8k
    if (x != NULL)
474
0
        return EXT_RETURN_NOT_SENT;
475
476
60.8k
    if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp)
477
60.8k
        return EXT_RETURN_NOT_SENT;
478
0
#ifndef OPENSSL_NO_ECH
479
0
    ECH_SAME_EXT(s, context, pkt)
480
0
#endif
481
482
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
483
        /* Sub-packet for status request extension */
484
0
        || !WPACKET_start_sub_packet_u16(pkt)
485
0
        || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)
486
        /* Sub-packet for the ids */
487
0
        || !WPACKET_start_sub_packet_u16(pkt)) {
488
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
489
0
        return EXT_RETURN_FAIL;
490
0
    }
491
0
    for (i = 0; i < sk_OCSP_RESPID_num(s->ext.ocsp.ids); i++) {
492
0
        unsigned char *idbytes;
493
0
        OCSP_RESPID *id = sk_OCSP_RESPID_value(s->ext.ocsp.ids, i);
494
0
        int idlen = i2d_OCSP_RESPID(id, NULL);
495
496
0
        if (idlen <= 0
497
            /* Sub-packet for an individual id */
498
0
            || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)
499
0
            || i2d_OCSP_RESPID(id, &idbytes) != idlen) {
500
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
501
0
            return EXT_RETURN_FAIL;
502
0
        }
503
0
    }
504
0
    if (!WPACKET_close(pkt)
505
0
        || !WPACKET_start_sub_packet_u16(pkt)) {
506
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
507
0
        return EXT_RETURN_FAIL;
508
0
    }
509
0
    if (s->ext.ocsp.exts) {
510
0
        unsigned char *extbytes;
511
0
        int extlen = i2d_X509_EXTENSIONS(s->ext.ocsp.exts, NULL);
512
513
0
        if (extlen < 0) {
514
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
515
0
            return EXT_RETURN_FAIL;
516
0
        }
517
0
        if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)
518
0
            || i2d_X509_EXTENSIONS(s->ext.ocsp.exts, &extbytes)
519
0
                != extlen) {
520
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
521
0
            return EXT_RETURN_FAIL;
522
0
        }
523
0
    }
524
0
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
525
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
526
0
        return EXT_RETURN_FAIL;
527
0
    }
528
529
0
    return EXT_RETURN_SENT;
530
0
}
531
#endif
532
533
#ifndef OPENSSL_NO_NEXTPROTONEG
534
EXT_RETURN tls_construct_ctos_npn(SSL_CONNECTION *s, WPACKET *pkt,
535
    unsigned int context,
536
    X509 *x, size_t chainidx)
537
60.8k
{
538
60.8k
    if (SSL_CONNECTION_GET_CTX(s)->ext.npn_select_cb == NULL
539
0
        || !SSL_IS_FIRST_HANDSHAKE(s))
540
60.8k
        return EXT_RETURN_NOT_SENT;
541
0
#ifndef OPENSSL_NO_ECH
542
0
    ECH_SAME_EXT(s, context, pkt)
543
0
#endif
544
545
    /*
546
     * The client advertises an empty extension to indicate its support
547
     * for Next Protocol Negotiation
548
     */
549
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
550
0
        || !WPACKET_put_bytes_u16(pkt, 0)) {
551
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
552
0
        return EXT_RETURN_FAIL;
553
0
    }
554
555
0
    return EXT_RETURN_SENT;
556
0
}
557
#endif
558
559
EXT_RETURN tls_construct_ctos_alpn(SSL_CONNECTION *s, WPACKET *pkt,
560
    unsigned int context,
561
    X509 *x, size_t chainidx)
562
60.8k
{
563
60.8k
    unsigned char *aval = s->ext.alpn;
564
60.8k
    size_t alen = s->ext.alpn_len;
565
566
60.8k
    s->s3.alpn_sent = 0;
567
60.8k
    if (!SSL_IS_FIRST_HANDSHAKE(s))
568
597
        return EXT_RETURN_NOT_SENT;
569
60.2k
#ifndef OPENSSL_NO_ECH
570
    /*
571
     * If we have different alpn and alpn_outer values, then we set
572
     * the appropriate one for inner and outer.
573
     * If no alpn is set (for inner or outer), we don't send any.
574
     * If only an inner is set then we send the same in both.
575
     * Logic above is on the basis that alpn's aren't that sensitive,
576
     * usually, so special action is needed to do better.
577
     * We also don't support a way to send alpn only in the inner.
578
     * If you don't want the inner value in the outer, you have to
579
     * pick what to send in the outer and send that.
580
     */
581
60.2k
    if (s->ext.ech.ch_depth == 1 && s->ext.alpn == NULL) /* inner */
582
0
        return EXT_RETURN_NOT_SENT;
583
60.2k
    if (s->ext.ech.ch_depth == 0 && s->ext.alpn == NULL
584
35.6k
        && s->ext.ech.alpn_outer == NULL) /* outer */
585
35.6k
        return EXT_RETURN_NOT_SENT;
586
24.5k
    if (s->ext.ech.ch_depth == 0 && s->ext.ech.alpn_outer != NULL) {
587
0
        aval = s->ext.ech.alpn_outer;
588
0
        alen = s->ext.ech.alpn_outer_len;
589
0
    }
590
24.5k
#endif
591
24.5k
    if (aval == NULL)
592
0
        return EXT_RETURN_NOT_SENT;
593
24.5k
    if (!WPACKET_put_bytes_u16(pkt,
594
24.5k
            TLSEXT_TYPE_application_layer_protocol_negotiation)
595
        /* Sub-packet ALPN extension */
596
24.5k
        || !WPACKET_start_sub_packet_u16(pkt)
597
24.5k
        || !WPACKET_sub_memcpy_u16(pkt, aval, alen)
598
24.5k
        || !WPACKET_close(pkt)) {
599
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
600
0
        return EXT_RETURN_FAIL;
601
0
    }
602
24.5k
    s->s3.alpn_sent = 1;
603
24.5k
    return EXT_RETURN_SENT;
604
24.5k
}
605
606
#ifndef OPENSSL_NO_SRTP
607
EXT_RETURN tls_construct_ctos_use_srtp(SSL_CONNECTION *s, WPACKET *pkt,
608
    unsigned int context, X509 *x,
609
    size_t chainidx)
610
60.8k
{
611
60.8k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
612
60.8k
    STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(ssl);
613
60.8k
    int i, end;
614
615
60.8k
    if (clnt == NULL)
616
60.8k
        return EXT_RETURN_NOT_SENT;
617
0
#ifndef OPENSSL_NO_ECH
618
0
    ECH_SAME_EXT(s, context, pkt)
619
0
#endif
620
621
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
622
        /* Sub-packet for SRTP extension */
623
0
        || !WPACKET_start_sub_packet_u16(pkt)
624
        /* Sub-packet for the protection profile list */
625
0
        || !WPACKET_start_sub_packet_u16(pkt)) {
626
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
627
0
        return EXT_RETURN_FAIL;
628
0
    }
629
630
0
    end = sk_SRTP_PROTECTION_PROFILE_num(clnt);
631
0
    for (i = 0; i < end; i++) {
632
0
        const SRTP_PROTECTION_PROFILE *prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
633
634
0
        if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {
635
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
636
0
            return EXT_RETURN_FAIL;
637
0
        }
638
0
    }
639
0
    if (!WPACKET_close(pkt)
640
        /* Add an empty use_mki value */
641
0
        || !WPACKET_put_bytes_u8(pkt, 0)
642
0
        || !WPACKET_close(pkt)) {
643
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
644
0
        return EXT_RETURN_FAIL;
645
0
    }
646
647
0
    return EXT_RETURN_SENT;
648
0
}
649
#endif
650
651
EXT_RETURN tls_construct_ctos_etm(SSL_CONNECTION *s, WPACKET *pkt,
652
    unsigned int context,
653
    X509 *x, size_t chainidx)
654
60.8k
{
655
60.8k
    if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
656
0
        return EXT_RETURN_NOT_SENT;
657
60.8k
#ifndef OPENSSL_NO_ECH
658
60.8k
    ECH_SAME_EXT(s, context, pkt)
659
60.8k
#endif
660
661
60.8k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
662
60.8k
        || !WPACKET_put_bytes_u16(pkt, 0)) {
663
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
664
0
        return EXT_RETURN_FAIL;
665
0
    }
666
667
60.8k
    return EXT_RETURN_SENT;
668
60.8k
}
669
670
#ifndef OPENSSL_NO_CT
671
EXT_RETURN tls_construct_ctos_sct(SSL_CONNECTION *s, WPACKET *pkt,
672
    unsigned int context,
673
    X509 *x, size_t chainidx)
674
60.8k
{
675
60.8k
    if (s->ct_validation_callback == NULL)
676
60.8k
        return EXT_RETURN_NOT_SENT;
677
678
    /* Not defined for client Certificates */
679
0
    if (x != NULL)
680
0
        return EXT_RETURN_NOT_SENT;
681
0
#ifndef OPENSSL_NO_ECH
682
0
    ECH_SAME_EXT(s, context, pkt)
683
0
#endif
684
685
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)
686
0
        || !WPACKET_put_bytes_u16(pkt, 0)) {
687
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
688
0
        return EXT_RETURN_FAIL;
689
0
    }
690
691
0
    return EXT_RETURN_SENT;
692
0
}
693
#endif
694
695
EXT_RETURN tls_construct_ctos_ems(SSL_CONNECTION *s, WPACKET *pkt,
696
    unsigned int context,
697
    X509 *x, size_t chainidx)
698
60.8k
{
699
60.8k
    if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)
700
0
        return EXT_RETURN_NOT_SENT;
701
60.8k
#ifndef OPENSSL_NO_ECH
702
60.8k
    ECH_SAME_EXT(s, context, pkt)
703
60.8k
#endif
704
705
60.8k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
706
60.8k
        || !WPACKET_put_bytes_u16(pkt, 0)) {
707
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
708
0
        return EXT_RETURN_FAIL;
709
0
    }
710
711
60.8k
    return EXT_RETURN_SENT;
712
60.8k
}
713
714
EXT_RETURN tls_construct_ctos_supported_versions(SSL_CONNECTION *s, WPACKET *pkt,
715
    unsigned int context, X509 *x,
716
    size_t chainidx)
717
14.6k
{
718
14.6k
    int currv, min_version, max_version, reason;
719
720
14.6k
    reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
721
14.6k
    if (reason != 0) {
722
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
723
0
        return EXT_RETURN_FAIL;
724
0
    }
725
726
    /*
727
     * Don't include this if we can't negotiate TLSv1.3. We can do a straight
728
     * comparison here because we will never be called in DTLS.
729
     */
730
14.6k
    if (max_version < TLS1_3_VERSION)
731
232
        return EXT_RETURN_NOT_SENT;
732
14.4k
#ifndef OPENSSL_NO_ECH
733
14.4k
    ECH_SAME_EXT(s, context, pkt)
734
14.4k
#endif
735
736
14.4k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
737
14.4k
        || !WPACKET_start_sub_packet_u16(pkt)
738
14.4k
        || !WPACKET_start_sub_packet_u8(pkt)) {
739
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
740
0
        return EXT_RETURN_FAIL;
741
0
    }
742
743
46.7k
    for (currv = max_version; currv >= min_version; currv--) {
744
32.3k
        if (!WPACKET_put_bytes_u16(pkt, currv)) {
745
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
746
0
            return EXT_RETURN_FAIL;
747
0
        }
748
32.3k
    }
749
14.4k
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
750
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
751
0
        return EXT_RETURN_FAIL;
752
0
    }
753
754
14.4k
    return EXT_RETURN_SENT;
755
14.4k
}
756
757
/*
758
 * Construct a psk_kex_modes extension.
759
 */
760
EXT_RETURN tls_construct_ctos_psk_kex_modes(SSL_CONNECTION *s, WPACKET *pkt,
761
    unsigned int context, X509 *x,
762
    size_t chainidx)
763
56.4k
{
764
56.4k
#ifndef OPENSSL_NO_TLS1_3
765
56.4k
    int nodhe = s->options & SSL_OP_ALLOW_NO_DHE_KEX;
766
767
56.4k
#ifndef OPENSSL_NO_ECH
768
56.4k
    ECH_SAME_EXT(s, context, pkt)
769
56.4k
#endif
770
771
56.4k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk_kex_modes)
772
56.4k
        || !WPACKET_start_sub_packet_u16(pkt)
773
56.4k
        || !WPACKET_start_sub_packet_u8(pkt)
774
56.4k
        || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE_DHE)
775
56.4k
        || (nodhe && !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE))
776
56.4k
        || !WPACKET_close(pkt)
777
56.4k
        || !WPACKET_close(pkt)) {
778
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
779
0
        return EXT_RETURN_FAIL;
780
0
    }
781
782
56.4k
    s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE_DHE;
783
56.4k
    if (nodhe)
784
0
        s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
785
56.4k
#endif
786
787
56.4k
    return EXT_RETURN_SENT;
788
56.4k
}
789
790
#ifndef OPENSSL_NO_TLS1_3
791
static int add_key_share(SSL_CONNECTION *s, WPACKET *pkt, unsigned int group_id, size_t loop_num)
792
91.4k
{
793
91.4k
    unsigned char *encoded_pubkey = NULL;
794
91.4k
    EVP_PKEY *key_share_key = NULL;
795
91.4k
    size_t encodedlen;
796
797
91.4k
    if (loop_num < s->s3.tmp.num_ks_pkey) {
798
28
        if (!ossl_assert(s->hello_retry_request == SSL_HRR_PENDING)
799
28
            || !ossl_assert(s->s3.tmp.ks_pkey[loop_num] != NULL)) {
800
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
801
0
            return 0;
802
0
        }
803
        /*
804
         * Could happen if we got an HRR that wasn't requesting a new key_share
805
         */
806
28
        key_share_key = s->s3.tmp.ks_pkey[loop_num];
807
91.4k
    } else {
808
91.4k
        key_share_key = ssl_generate_pkey_group(s, group_id);
809
91.4k
        if (key_share_key == NULL) {
810
            /* SSLfatal() already called */
811
0
            return 0;
812
0
        }
813
91.4k
    }
814
815
    /* Encode the public key. */
816
91.4k
    encodedlen = EVP_PKEY_get1_encoded_public_key(key_share_key,
817
91.4k
        &encoded_pubkey);
818
91.4k
    if (encodedlen == 0) {
819
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
820
0
        goto err;
821
0
    }
822
823
    /* Create KeyShareEntry */
824
91.4k
    if (!WPACKET_put_bytes_u16(pkt, group_id)
825
91.4k
        || !WPACKET_sub_memcpy_u16(pkt, encoded_pubkey, encodedlen)) {
826
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
827
0
        goto err;
828
0
    }
829
830
    /* For backward compatibility, we use the first valid group to add a key share */
831
91.4k
    if (loop_num == 0) {
832
45.8k
        s->s3.tmp.pkey = key_share_key;
833
45.8k
        s->s3.group_id = group_id;
834
45.8k
    }
835
    /* We ensure in t1_lib.c that the loop number does not exceed OPENSSL_CLIENT_MAX_KEY_SHARES */
836
91.4k
    s->s3.tmp.ks_pkey[loop_num] = key_share_key;
837
91.4k
    s->s3.tmp.ks_group_id[loop_num] = group_id;
838
91.4k
    if (loop_num >= s->s3.tmp.num_ks_pkey)
839
91.4k
        s->s3.tmp.num_ks_pkey++;
840
841
91.4k
    OPENSSL_free(encoded_pubkey);
842
91.4k
    return 1;
843
0
err:
844
0
    if (key_share_key != s->s3.tmp.ks_pkey[loop_num])
845
0
        EVP_PKEY_free(key_share_key);
846
0
    OPENSSL_free(encoded_pubkey);
847
0
    return 0;
848
91.4k
}
849
#endif
850
851
EXT_RETURN tls_construct_ctos_key_share(SSL_CONNECTION *s, WPACKET *pkt,
852
    unsigned int context, X509 *x,
853
    size_t chainidx)
854
14.4k
{
855
14.4k
#ifndef OPENSSL_NO_TLS1_3
856
14.4k
    size_t i, num_groups = 0;
857
14.4k
    const uint16_t *pgroups = NULL;
858
14.4k
    uint16_t group_id = 0;
859
14.4k
    int add_only_one = 0;
860
14.4k
    size_t valid_keyshare = 0;
861
862
14.4k
#ifndef OPENSSL_NO_ECH
863
14.4k
    ECH_SAME_EXT(s, context, pkt)
864
14.4k
#endif
865
866
    /* key_share extension */
867
14.4k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
868
        /* Extension data sub-packet */
869
14.4k
        || !WPACKET_start_sub_packet_u16(pkt)
870
        /* KeyShare list sub-packet */
871
14.4k
        || !WPACKET_start_sub_packet_u16(pkt)) {
872
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
873
0
        return EXT_RETURN_FAIL;
874
0
    }
875
876
14.4k
    tls1_get_requested_keyshare_groups(s, &pgroups, &num_groups);
877
14.4k
    if (num_groups == 1 && pgroups[0] == 0) { /* Indication that no * prefix was used */
878
0
        tls1_get_supported_groups(s, &pgroups, &num_groups);
879
0
        add_only_one = 1;
880
0
    }
881
882
    /* If neither the default nor the keyshares have any entry --> fatal */
883
14.4k
    if (num_groups == 0) {
884
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE);
885
0
        return EXT_RETURN_FAIL;
886
0
    }
887
888
    /* Add key shares */
889
890
14.4k
    if (s->s3.group_id != 0 && s->s3.tmp.pkey == NULL) {
891
        /* new, single key share */
892
58
        group_id = s->s3.group_id;
893
58
        s->s3.tmp.num_ks_pkey = 0;
894
58
        if (!add_key_share(s, pkt, group_id, 0)) {
895
            /* SSLfatal() already called */
896
0
            return EXT_RETURN_FAIL;
897
0
        }
898
58
        valid_keyshare++;
899
14.3k
    } else {
900
14.3k
        if (s->ext.supportedgroups == NULL) /* use default */
901
0
            add_only_one = 1;
902
903
43.1k
        for (i = 0; i < num_groups; i++) {
904
28.7k
            if (!tls_group_allowed(s, pgroups[i], SSL_SECOP_CURVE_SUPPORTED))
905
0
                continue;
906
28.7k
            if (!tls_valid_group(s, pgroups[i], TLS1_3_VERSION, TLS1_3_VERSION,
907
28.7k
                    NULL, NULL))
908
0
                continue;
909
910
28.7k
            group_id = pgroups[i];
911
912
28.7k
            if (group_id == 0) {
913
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE);
914
0
                return EXT_RETURN_FAIL;
915
0
            }
916
28.7k
            if (!add_key_share(s, pkt, group_id, valid_keyshare)) {
917
                /* SSLfatal() already called */
918
0
                return EXT_RETURN_FAIL;
919
0
            }
920
28.7k
            valid_keyshare++;
921
28.7k
            if (add_only_one)
922
0
                break;
923
28.7k
        }
924
14.3k
    }
925
926
14.4k
    if (valid_keyshare == 0) {
927
        /* No key shares were allowed */
928
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE);
929
0
        return EXT_RETURN_FAIL;
930
0
    }
931
932
14.4k
#ifndef OPENSSL_NO_ECH
933
    /* stash inner key shares */
934
14.4k
    if (s->ext.ech.ch_depth == 1 && ossl_ech_stash_keyshares(s) != 1) {
935
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
936
0
        return EXT_RETURN_FAIL;
937
0
    }
938
14.4k
#endif
939
940
14.4k
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
941
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
942
0
        return EXT_RETURN_FAIL;
943
0
    }
944
14.4k
    return EXT_RETURN_SENT;
945
#else
946
    return EXT_RETURN_NOT_SENT;
947
#endif
948
14.4k
}
949
950
EXT_RETURN tls_construct_ctos_cookie(SSL_CONNECTION *s, WPACKET *pkt,
951
    unsigned int context,
952
    X509 *x, size_t chainidx)
953
56.4k
{
954
56.4k
    EXT_RETURN ret = EXT_RETURN_FAIL;
955
956
    /* Should only be set if we've had an HRR */
957
56.4k
    if (s->ext.tls13_cookie_len == 0)
958
56.1k
        return EXT_RETURN_NOT_SENT;
959
298
#ifndef OPENSSL_NO_ECH
960
298
    ECH_SAME_EXT(s, context, pkt)
961
298
#endif
962
963
298
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
964
        /* Extension data sub-packet */
965
298
        || !WPACKET_start_sub_packet_u16(pkt)
966
298
        || !WPACKET_sub_memcpy_u16(pkt, s->ext.tls13_cookie,
967
298
            s->ext.tls13_cookie_len)
968
298
        || !WPACKET_close(pkt)) {
969
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
970
0
        goto end;
971
0
    }
972
973
298
    ret = EXT_RETURN_SENT;
974
298
end:
975
298
    OPENSSL_free(s->ext.tls13_cookie);
976
298
    s->ext.tls13_cookie = NULL;
977
298
    s->ext.tls13_cookie_len = 0;
978
979
298
    return ret;
980
298
}
981
982
EXT_RETURN tls_construct_ctos_early_data(SSL_CONNECTION *s, WPACKET *pkt,
983
    unsigned int context, X509 *x,
984
    size_t chainidx)
985
14.4k
{
986
14.4k
#ifndef OPENSSL_NO_PSK
987
14.4k
    char identity[PSK_MAX_IDENTITY_LEN + 1];
988
14.4k
#endif /* OPENSSL_NO_PSK */
989
14.4k
    const unsigned char *id = NULL;
990
14.4k
    size_t idlen = 0;
991
14.4k
    SSL_SESSION *psksess = NULL;
992
14.4k
    SSL_SESSION *edsess = NULL;
993
14.4k
    const EVP_MD *handmd = NULL;
994
14.4k
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
995
996
14.4k
#ifndef OPENSSL_NO_ECH
997
    /*
998
     * If we're attempting ECH and processing the outer CH
999
     * then we only need to check if the extension is to be
1000
     * sent or not - any other processing (with side effects)
1001
     * happened already for the inner CH.
1002
     */
1003
14.4k
    if (s->ext.ech.es != NULL && s->ext.ech.ch_depth == 0) {
1004
        /*
1005
         * if we called this for inner and did send then
1006
         * the following two things should be set, if so,
1007
         * then send again in the outer CH.
1008
         */
1009
0
        if (s->ext.early_data == SSL_EARLY_DATA_REJECTED
1010
0
            && s->ext.early_data_ok == 1) {
1011
0
            if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
1012
0
                || !WPACKET_start_sub_packet_u16(pkt)
1013
0
                || !WPACKET_close(pkt)) {
1014
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1015
0
                return EXT_RETURN_FAIL;
1016
0
            }
1017
0
            return EXT_RETURN_SENT;
1018
0
        } else {
1019
0
            return EXT_RETURN_NOT_SENT;
1020
0
        }
1021
0
    }
1022
14.4k
#endif
1023
14.4k
    if (s->hello_retry_request == SSL_HRR_PENDING)
1024
65
        handmd = ssl_handshake_md(s);
1025
1026
14.4k
    if (s->psk_use_session_cb != NULL
1027
0
        && (!s->psk_use_session_cb(ussl, handmd, &id, &idlen, &psksess)
1028
0
            || (psksess != NULL
1029
0
                && psksess->ssl_version != TLS1_3_VERSION))) {
1030
0
        SSL_SESSION_free(psksess);
1031
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
1032
0
        return EXT_RETURN_FAIL;
1033
0
    }
1034
1035
14.4k
#ifndef OPENSSL_NO_PSK
1036
14.4k
    if (psksess == NULL && s->psk_client_callback != NULL) {
1037
0
        unsigned char psk[PSK_MAX_PSK_LEN];
1038
0
        size_t psklen = 0;
1039
1040
0
        memset(identity, 0, sizeof(identity));
1041
0
        psklen = s->psk_client_callback(ussl, NULL,
1042
0
            identity, sizeof(identity) - 1,
1043
0
            psk, sizeof(psk));
1044
1045
0
        if (psklen > PSK_MAX_PSK_LEN) {
1046
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR);
1047
0
            return EXT_RETURN_FAIL;
1048
0
        } else if (psklen > 0) {
1049
0
            const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
1050
0
            const SSL_CIPHER *cipher;
1051
1052
0
            idlen = strlen(identity);
1053
0
            if (idlen > PSK_MAX_IDENTITY_LEN) {
1054
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1055
0
                return EXT_RETURN_FAIL;
1056
0
            }
1057
0
            id = (unsigned char *)identity;
1058
1059
            /*
1060
             * We found a PSK using an old style callback. We don't know
1061
             * the digest so we default to SHA256 as per the TLSv1.3 spec
1062
             */
1063
0
            cipher = SSL_CIPHER_find(SSL_CONNECTION_GET_SSL(s),
1064
0
                tls13_aes128gcmsha256_id);
1065
0
            if (cipher == NULL) {
1066
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1067
0
                return EXT_RETURN_FAIL;
1068
0
            }
1069
1070
0
            psksess = SSL_SESSION_new();
1071
0
            if (psksess == NULL
1072
0
                || !SSL_SESSION_set1_master_key(psksess, psk, psklen)
1073
0
                || !SSL_SESSION_set_cipher(psksess, cipher)
1074
0
                || !SSL_SESSION_set_protocol_version(psksess, TLS1_3_VERSION)) {
1075
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1076
0
                OPENSSL_cleanse(psk, psklen);
1077
0
                return EXT_RETURN_FAIL;
1078
0
            }
1079
0
            OPENSSL_cleanse(psk, psklen);
1080
0
        }
1081
0
    }
1082
14.4k
#endif /* OPENSSL_NO_PSK */
1083
1084
14.4k
    SSL_SESSION_free(s->psksession);
1085
14.4k
    s->psksession = psksess;
1086
14.4k
    if (psksess != NULL) {
1087
0
        OPENSSL_free(s->psksession_id);
1088
0
        s->psksession_id = OPENSSL_memdup(id, idlen);
1089
0
        if (s->psksession_id == NULL) {
1090
0
            s->psksession_id_len = 0;
1091
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1092
0
            return EXT_RETURN_FAIL;
1093
0
        }
1094
0
        s->psksession_id_len = idlen;
1095
0
    }
1096
1097
14.4k
    if (s->early_data_state != SSL_EARLY_DATA_CONNECTING
1098
0
        || (s->session->ext.max_early_data == 0
1099
14.4k
            && (psksess == NULL || psksess->ext.max_early_data == 0))) {
1100
14.4k
        s->max_early_data = 0;
1101
14.4k
        return EXT_RETURN_NOT_SENT;
1102
14.4k
    }
1103
0
    edsess = s->session->ext.max_early_data != 0 ? s->session : psksess;
1104
0
    s->max_early_data = edsess->ext.max_early_data;
1105
1106
0
    if (edsess->ext.hostname != NULL) {
1107
0
        if (s->ext.hostname == NULL
1108
0
            || (s->ext.hostname != NULL
1109
0
                && strcmp(s->ext.hostname, edsess->ext.hostname) != 0)) {
1110
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1111
0
                SSL_R_INCONSISTENT_EARLY_DATA_SNI);
1112
0
            return EXT_RETURN_FAIL;
1113
0
        }
1114
0
    }
1115
1116
0
    if ((s->ext.alpn == NULL && edsess->ext.alpn_selected != NULL)) {
1117
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_INCONSISTENT_EARLY_DATA_ALPN);
1118
0
        return EXT_RETURN_FAIL;
1119
0
    }
1120
1121
    /*
1122
     * Verify that we are offering an ALPN protocol consistent with the early
1123
     * data.
1124
     */
1125
0
    if (edsess->ext.alpn_selected != NULL) {
1126
0
        PACKET prots, alpnpkt;
1127
0
        int found = 0;
1128
1129
0
        if (!PACKET_buf_init(&prots, s->ext.alpn, s->ext.alpn_len)) {
1130
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1131
0
            return EXT_RETURN_FAIL;
1132
0
        }
1133
0
        while (PACKET_get_length_prefixed_1(&prots, &alpnpkt)) {
1134
0
            if (PACKET_equal(&alpnpkt, edsess->ext.alpn_selected,
1135
0
                    edsess->ext.alpn_selected_len)) {
1136
0
                found = 1;
1137
0
                break;
1138
0
            }
1139
0
        }
1140
0
        if (!found) {
1141
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1142
0
                SSL_R_INCONSISTENT_EARLY_DATA_ALPN);
1143
0
            return EXT_RETURN_FAIL;
1144
0
        }
1145
0
    }
1146
1147
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
1148
0
        || !WPACKET_start_sub_packet_u16(pkt)
1149
0
        || !WPACKET_close(pkt)) {
1150
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1151
0
        return EXT_RETURN_FAIL;
1152
0
    }
1153
1154
    /*
1155
     * We set this to rejected here. Later, if the server acknowledges the
1156
     * extension, we set it to accepted.
1157
     */
1158
0
    s->ext.early_data = SSL_EARLY_DATA_REJECTED;
1159
0
    s->ext.early_data_ok = 1;
1160
1161
0
    return EXT_RETURN_SENT;
1162
0
}
1163
1164
0
#define F5_WORKAROUND_MIN_MSG_LEN 0xff
1165
0
#define F5_WORKAROUND_MAX_MSG_LEN 0x200
1166
1167
/*
1168
 * PSK pre binder overhead =
1169
 *  2 bytes for TLSEXT_TYPE_psk
1170
 *  2 bytes for extension length
1171
 *  2 bytes for identities list length
1172
 *  2 bytes for identity length
1173
 *  4 bytes for obfuscated_ticket_age
1174
 *  2 bytes for binder list length
1175
 *  1 byte for binder length
1176
 * The above excludes the number of bytes for the identity itself and the
1177
 * subsequent binder bytes
1178
 */
1179
0
#define PSK_PRE_BINDER_OVERHEAD (2 + 2 + 2 + 2 + 4 + 2 + 1)
1180
1181
EXT_RETURN tls_construct_ctos_padding(SSL_CONNECTION *s, WPACKET *pkt,
1182
    unsigned int context, X509 *x,
1183
    size_t chainidx)
1184
17.9k
{
1185
17.9k
    unsigned char *padbytes;
1186
17.9k
    size_t hlen;
1187
1188
17.9k
    if ((s->options & SSL_OP_TLSEXT_PADDING) == 0)
1189
17.9k
        return EXT_RETURN_NOT_SENT;
1190
0
#ifndef OPENSSL_NO_ECH
1191
0
    ECH_SAME_EXT(s, context, pkt);
1192
0
#endif
1193
1194
    /*
1195
     * Add padding to workaround bugs in F5 terminators. See RFC7685.
1196
     * This code calculates the length of all extensions added so far but
1197
     * excludes the PSK extension (because that MUST be written last). Therefore
1198
     * this extension MUST always appear second to last.
1199
     */
1200
0
    if (!WPACKET_get_total_written(pkt, &hlen)) {
1201
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1202
0
        return EXT_RETURN_FAIL;
1203
0
    }
1204
1205
    /*
1206
     * If we're going to send a PSK then that will be written out after this
1207
     * extension, so we need to calculate how long it is going to be.
1208
     */
1209
0
    if (s->session->ssl_version == TLS1_3_VERSION
1210
0
        && s->session->ext.ticklen != 0
1211
0
        && s->session->cipher != NULL) {
1212
0
        const EVP_MD *md = ssl_md(SSL_CONNECTION_GET_CTX(s),
1213
0
            s->session->cipher->algorithm2);
1214
1215
0
        if (md != NULL) {
1216
            /*
1217
             * Add the fixed PSK overhead, the identity length and the binder
1218
             * length.
1219
             */
1220
0
            int md_size = EVP_MD_get_size(md);
1221
1222
0
            if (md_size <= 0)
1223
0
                return EXT_RETURN_FAIL;
1224
0
            hlen += PSK_PRE_BINDER_OVERHEAD + s->session->ext.ticklen
1225
0
                + md_size;
1226
0
        }
1227
0
    }
1228
1229
0
    if (hlen > F5_WORKAROUND_MIN_MSG_LEN && hlen < F5_WORKAROUND_MAX_MSG_LEN) {
1230
        /* Calculate the amount of padding we need to add */
1231
0
        hlen = F5_WORKAROUND_MAX_MSG_LEN - hlen;
1232
1233
        /*
1234
         * Take off the size of extension header itself (2 bytes for type and
1235
         * 2 bytes for length bytes), but ensure that the extension is at least
1236
         * 1 byte long so as not to have an empty extension last (WebSphere 7.x,
1237
         * 8.x are intolerant of that condition)
1238
         */
1239
0
        if (hlen > 4)
1240
0
            hlen -= 4;
1241
0
        else
1242
0
            hlen = 1;
1243
1244
0
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)
1245
0
            || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {
1246
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1247
0
            return EXT_RETURN_FAIL;
1248
0
        }
1249
0
        memset(padbytes, 0, hlen);
1250
0
    }
1251
1252
0
    return EXT_RETURN_SENT;
1253
0
}
1254
1255
/*
1256
 * Construct the pre_shared_key extension
1257
 */
1258
EXT_RETURN tls_construct_ctos_psk(SSL_CONNECTION *s, WPACKET *pkt,
1259
    unsigned int context,
1260
    X509 *x, size_t chainidx)
1261
14.4k
{
1262
14.4k
#ifndef OPENSSL_NO_TLS1_3
1263
14.4k
    uint32_t agesec, agems = 0;
1264
14.4k
    size_t binderoffset, msglen, resbinder_end_offset = 0, pskbinder_end_offset = 0;
1265
14.4k
    int reshashsize = 0, pskhashsize = 0;
1266
14.4k
    unsigned char *resbinder = NULL, *pskbinder = NULL, *msgstart = NULL;
1267
14.4k
    const EVP_MD *handmd = NULL, *mdres = NULL, *mdpsk = NULL;
1268
14.4k
    int dores = 0;
1269
14.4k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1270
14.4k
    OSSL_TIME t;
1271
1272
14.4k
    s->ext.tick_identity = 0;
1273
1274
    /*
1275
     * Note: At this stage of the code we only support adding a single
1276
     * resumption PSK. If we add support for multiple PSKs then the length
1277
     * calculations in the padding extension will need to be adjusted.
1278
     */
1279
1280
    /*
1281
     * If this is an incompatible or new session then we have nothing to resume
1282
     * so don't add this extension.
1283
     */
1284
14.4k
    if (s->session->ssl_version != TLS1_3_VERSION
1285
14.4k
        || (s->session->ext.ticklen == 0 && s->psksession == NULL))
1286
14.4k
        return EXT_RETURN_NOT_SENT;
1287
1288
0
    if (s->hello_retry_request == SSL_HRR_PENDING)
1289
0
        handmd = ssl_handshake_md(s);
1290
1291
0
    if (s->session->ext.ticklen != 0) {
1292
        /* Get the digest associated with the ciphersuite in the session */
1293
0
        if (s->session->cipher == NULL) {
1294
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1295
0
            return EXT_RETURN_FAIL;
1296
0
        }
1297
0
        mdres = ssl_md(sctx, s->session->cipher->algorithm2);
1298
0
        if (mdres == NULL) {
1299
            /*
1300
             * Don't recognize this cipher so we can't use the session.
1301
             * Ignore it
1302
             */
1303
0
            goto dopsksess;
1304
0
        }
1305
1306
0
        if (s->hello_retry_request == SSL_HRR_PENDING && mdres != handmd) {
1307
            /*
1308
             * Selected ciphersuite hash does not match the hash for the session
1309
             * so we can't use it.
1310
             */
1311
0
            goto dopsksess;
1312
0
        }
1313
1314
0
#ifndef OPENSSL_NO_ECH
1315
        /*
1316
         * When doing ECH, we get here twice (for inner then outer). The
1317
         * 2nd time (for outer) we can skip some checks as we know how
1318
         * those went last time.
1319
         */
1320
0
        if (s->ext.ech.es != NULL && s->ext.ech.ch_depth == 0) {
1321
0
            s->ext.tick_identity = s->ext.ech.tick_identity;
1322
0
            dores = (s->ext.tick_identity > 0);
1323
0
            goto dopsksess;
1324
0
        }
1325
0
#endif
1326
1327
        /*
1328
         * Technically the C standard just says time() returns a time_t and says
1329
         * nothing about the encoding of that type. In practice most
1330
         * implementations follow POSIX which holds it as an integral type in
1331
         * seconds since epoch. We've already made the assumption that we can do
1332
         * this in multiple places in the code, so portability shouldn't be an
1333
         * issue.
1334
         */
1335
0
        t = ossl_time_subtract(ossl_time_now(), s->session->time);
1336
0
        agesec = (uint32_t)ossl_time2seconds(t);
1337
1338
        /*
1339
         * We calculate the age in seconds but the server may work in ms. Due to
1340
         * rounding errors we could overestimate the age by up to 1s. It is
1341
         * better to underestimate it. Otherwise, if the RTT is very short, when
1342
         * the server calculates the age reported by the client it could be
1343
         * bigger than the age calculated on the server - which should never
1344
         * happen.
1345
         */
1346
0
        if (agesec > 0)
1347
0
            agesec--;
1348
1349
0
        if (s->session->ext.tick_lifetime_hint < agesec) {
1350
            /* Ticket is too old. Ignore it. */
1351
0
            goto dopsksess;
1352
0
        }
1353
1354
        /*
1355
         * Calculate age in ms. We're just doing it to nearest second. Should be
1356
         * good enough.
1357
         */
1358
0
        agems = agesec * (uint32_t)1000;
1359
1360
0
        if (agesec != 0 && agems / (uint32_t)1000 != agesec) {
1361
            /*
1362
             * Overflow. Shouldn't happen unless this is a *really* old session.
1363
             * If so we just ignore it.
1364
             */
1365
0
            goto dopsksess;
1366
0
        }
1367
1368
        /*
1369
         * Obfuscate the age. Overflow here is fine, this addition is supposed
1370
         * to be mod 2^32.
1371
         */
1372
0
        agems += s->session->ext.tick_age_add;
1373
1374
0
        reshashsize = EVP_MD_get_size(mdres);
1375
0
        if (reshashsize <= 0)
1376
0
            goto dopsksess;
1377
0
        s->ext.tick_identity++;
1378
0
#ifndef OPENSSL_NO_ECH
1379
        /* stash this for re-use in outer CH */
1380
0
        if (s->ext.ech.es != NULL && s->ext.ech.ch_depth == 1)
1381
0
            s->ext.ech.tick_identity = s->ext.tick_identity;
1382
0
#endif
1383
0
        dores = 1;
1384
0
    }
1385
1386
0
dopsksess:
1387
0
    if (!dores && s->psksession == NULL)
1388
0
        return EXT_RETURN_NOT_SENT;
1389
1390
0
    if (s->psksession != NULL) {
1391
0
        mdpsk = ssl_md(sctx, s->psksession->cipher->algorithm2);
1392
0
        if (mdpsk == NULL) {
1393
            /*
1394
             * Don't recognize this cipher so we can't use the session.
1395
             * If this happens it's an application bug.
1396
             */
1397
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
1398
0
            return EXT_RETURN_FAIL;
1399
0
        }
1400
1401
0
        if (s->hello_retry_request == SSL_HRR_PENDING && mdpsk != handmd) {
1402
            /*
1403
             * Selected ciphersuite hash does not match the hash for the PSK
1404
             * session. This is an application bug.
1405
             */
1406
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
1407
0
            return EXT_RETURN_FAIL;
1408
0
        }
1409
1410
0
        pskhashsize = EVP_MD_get_size(mdpsk);
1411
0
        if (pskhashsize <= 0) {
1412
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
1413
0
            return EXT_RETURN_FAIL;
1414
0
        }
1415
0
    }
1416
1417
    /* Create the extension, but skip over the binder for now */
1418
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
1419
0
        || !WPACKET_start_sub_packet_u16(pkt)
1420
0
        || !WPACKET_start_sub_packet_u16(pkt)) {
1421
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1422
0
        return EXT_RETURN_FAIL;
1423
0
    }
1424
1425
0
#ifndef OPENSSL_NO_ECH
1426
    /*
1427
     * For ECH if we're processing the outer CH and the inner CH
1428
     * has a PSK, then we want to send a GREASE PSK in the outer.
1429
     * We'll do that by just replacing the ticket value itself
1430
     * with random values of the same length.
1431
     */
1432
0
    if (s->ext.ech.es != NULL && s->ext.ech.ch_depth == 0) {
1433
0
        unsigned char *rndbuf = NULL, *rndbufp = NULL;
1434
0
        size_t totalrndsize = 0;
1435
1436
0
        totalrndsize = s->session->ext.ticklen
1437
0
            + sizeof(agems)
1438
0
            + s->psksession_id_len
1439
0
            + reshashsize
1440
0
            + pskhashsize;
1441
0
        rndbuf = OPENSSL_malloc(totalrndsize);
1442
0
        if (rndbuf == NULL) {
1443
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1444
0
            return EXT_RETURN_FAIL;
1445
0
        }
1446
        /* for outer CH allocate a similar sized random value */
1447
0
        if (RAND_bytes_ex(sctx->libctx, rndbuf, totalrndsize, 0) <= 0) {
1448
0
            OPENSSL_free(rndbuf);
1449
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1450
0
            return EXT_RETURN_FAIL;
1451
0
        }
1452
        /* set agems from random buffer */
1453
0
        rndbufp = rndbuf;
1454
0
        memcpy(&agems, rndbufp, sizeof(agems));
1455
0
        rndbufp += sizeof(agems);
1456
0
        if (dores != 0) {
1457
0
            if (!WPACKET_sub_memcpy_u16(pkt, rndbufp,
1458
0
                    s->session->ext.ticklen)
1459
0
                || !WPACKET_put_bytes_u32(pkt, agems)) {
1460
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1461
0
                OPENSSL_free(rndbuf);
1462
0
                return EXT_RETURN_FAIL;
1463
0
            }
1464
0
            rndbufp += s->session->ext.ticklen;
1465
0
        }
1466
0
        if (s->psksession != NULL) {
1467
0
            if (!WPACKET_sub_memcpy_u16(pkt, rndbufp, s->psksession_id_len)
1468
0
                || !WPACKET_put_bytes_u32(pkt, 0)) {
1469
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1470
0
                OPENSSL_free(rndbuf);
1471
0
                return EXT_RETURN_FAIL;
1472
0
            }
1473
0
            rndbufp += s->psksession_id_len;
1474
0
        }
1475
0
        if (!WPACKET_close(pkt)
1476
0
            || !WPACKET_start_sub_packet_u16(pkt)
1477
0
            || (dores == 1
1478
0
                && !WPACKET_sub_memcpy_u8(pkt, rndbufp, reshashsize))
1479
0
            || (s->psksession != NULL
1480
0
                && !WPACKET_sub_memcpy_u8(pkt, rndbufp, pskhashsize))
1481
0
            || !WPACKET_close(pkt)
1482
0
            || !WPACKET_close(pkt)) {
1483
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1484
0
            OPENSSL_free(rndbuf);
1485
0
            return EXT_RETURN_FAIL;
1486
0
        }
1487
0
        OPENSSL_free(rndbuf);
1488
0
        return EXT_RETURN_SENT;
1489
0
    }
1490
0
#endif /* OPENSSL_NO_ECH */
1491
0
    if (dores) {
1492
0
        if (!WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick,
1493
0
                s->session->ext.ticklen)
1494
0
            || !WPACKET_put_bytes_u32(pkt, agems)) {
1495
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1496
0
            return EXT_RETURN_FAIL;
1497
0
        }
1498
0
    }
1499
1500
0
    if (s->psksession != NULL) {
1501
0
        if (!WPACKET_sub_memcpy_u16(pkt, s->psksession_id,
1502
0
                s->psksession_id_len)
1503
0
            || !WPACKET_put_bytes_u32(pkt, 0)) {
1504
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1505
0
            return EXT_RETURN_FAIL;
1506
0
        }
1507
0
        s->ext.tick_identity++;
1508
0
    }
1509
1510
0
    if (!WPACKET_close(pkt)
1511
0
        || !WPACKET_get_total_written(pkt, &binderoffset)
1512
0
        || !WPACKET_start_sub_packet_u16(pkt)
1513
0
        || (dores
1514
0
            && (!WPACKET_sub_allocate_bytes_u8(pkt, reshashsize, NULL)
1515
0
                || !WPACKET_get_total_written(pkt, &resbinder_end_offset)))
1516
0
        || (s->psksession != NULL
1517
0
            && (!WPACKET_sub_allocate_bytes_u8(pkt, pskhashsize, NULL)
1518
0
                || !WPACKET_get_total_written(pkt, &pskbinder_end_offset)))
1519
0
        || !WPACKET_close(pkt)
1520
0
        || !WPACKET_close(pkt)
1521
0
        || !WPACKET_get_total_written(pkt, &msglen)
1522
        /*
1523
         * We need to fill in all the sub-packet lengths now so we can
1524
         * calculate the HMAC of the message up to the binders
1525
         */
1526
0
        || !WPACKET_fill_lengths(pkt)) {
1527
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1528
0
        return EXT_RETURN_FAIL;
1529
0
    }
1530
1531
0
    msgstart = WPACKET_get_curr(pkt) - msglen;
1532
    /* Allocating either binder may relocate the packet's backing buffer. */
1533
0
    if (dores)
1534
0
        resbinder = msgstart + resbinder_end_offset - reshashsize;
1535
0
    if (s->psksession != NULL)
1536
0
        pskbinder = msgstart + pskbinder_end_offset - pskhashsize;
1537
1538
0
    if (dores
1539
0
        && tls_psk_do_binder(s, mdres, msgstart, binderoffset, NULL,
1540
0
               resbinder, s->session, 1, 0)
1541
0
            != 1) {
1542
        /* SSLfatal() already called */
1543
0
        return EXT_RETURN_FAIL;
1544
0
    }
1545
1546
0
    if (s->psksession != NULL
1547
0
        && tls_psk_do_binder(s, mdpsk, msgstart, binderoffset, NULL,
1548
0
               pskbinder, s->psksession, 1, 1)
1549
0
            != 1) {
1550
        /* SSLfatal() already called */
1551
0
        return EXT_RETURN_FAIL;
1552
0
    }
1553
1554
0
    return EXT_RETURN_SENT;
1555
#else
1556
    return EXT_RETURN_NOT_SENT;
1557
#endif
1558
0
}
1559
1560
EXT_RETURN tls_construct_ctos_post_handshake_auth(SSL_CONNECTION *s, WPACKET *pkt,
1561
    ossl_unused unsigned int context,
1562
    ossl_unused X509 *x,
1563
    ossl_unused size_t chainidx)
1564
56.4k
{
1565
56.4k
#ifndef OPENSSL_NO_TLS1_3
1566
56.4k
    if (!s->pha_enabled)
1567
56.4k
        return EXT_RETURN_NOT_SENT;
1568
0
#ifndef OPENSSL_NO_ECH
1569
0
    ECH_SAME_EXT(s, context, pkt)
1570
0
#endif
1571
1572
    /* construct extension - 0 length, no contents */
1573
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_post_handshake_auth)
1574
0
        || !WPACKET_start_sub_packet_u16(pkt)
1575
0
        || !WPACKET_close(pkt)) {
1576
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1577
0
        return EXT_RETURN_FAIL;
1578
0
    }
1579
1580
0
    s->post_handshake_auth = SSL_PHA_EXT_SENT;
1581
1582
0
    return EXT_RETURN_SENT;
1583
#else
1584
    return EXT_RETURN_NOT_SENT;
1585
#endif
1586
0
}
1587
1588
/*
1589
 * Parse the server's renegotiation binding and abort if it's not right
1590
 */
1591
int tls_parse_stoc_renegotiate(SSL_CONNECTION *s, PACKET *pkt,
1592
    unsigned int context,
1593
    X509 *x, size_t chainidx)
1594
51.9k
{
1595
51.9k
    size_t expected_len = s->s3.previous_client_finished_len
1596
51.9k
        + s->s3.previous_server_finished_len;
1597
51.9k
    size_t ilen;
1598
51.9k
    const unsigned char *data;
1599
1600
    /* Check for logic errors */
1601
51.9k
    if (!ossl_assert(expected_len == 0
1602
51.9k
            || s->s3.previous_client_finished_len != 0)
1603
51.9k
        || !ossl_assert(expected_len == 0
1604
51.9k
            || s->s3.previous_server_finished_len != 0)) {
1605
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1606
0
        return 0;
1607
0
    }
1608
1609
    /* Parse the length byte */
1610
51.9k
    if (!PACKET_get_1_len(pkt, &ilen)) {
1611
13
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);
1612
13
        return 0;
1613
13
    }
1614
1615
    /* Consistency check */
1616
51.9k
    if (PACKET_remaining(pkt) != ilen) {
1617
84
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);
1618
84
        return 0;
1619
84
    }
1620
1621
    /* Check that the extension matches */
1622
51.8k
    if (ilen != expected_len) {
1623
27
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);
1624
27
        return 0;
1625
27
    }
1626
1627
51.8k
    if (!PACKET_get_bytes(pkt, &data, s->s3.previous_client_finished_len)
1628
51.8k
        || memcmp(data, s->s3.previous_client_finished,
1629
51.8k
               s->s3.previous_client_finished_len)
1630
51.8k
            != 0) {
1631
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);
1632
0
        return 0;
1633
0
    }
1634
1635
51.8k
    if (!PACKET_get_bytes(pkt, &data, s->s3.previous_server_finished_len)
1636
51.8k
        || memcmp(data, s->s3.previous_server_finished,
1637
51.8k
               s->s3.previous_server_finished_len)
1638
51.8k
            != 0) {
1639
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);
1640
0
        return 0;
1641
0
    }
1642
51.8k
    s->s3.send_connection_binding = 1;
1643
1644
51.8k
    return 1;
1645
51.8k
}
1646
1647
/* Parse the server's max fragment len extension packet */
1648
int tls_parse_stoc_maxfragmentlen(SSL_CONNECTION *s, PACKET *pkt,
1649
    unsigned int context,
1650
    X509 *x, size_t chainidx)
1651
0
{
1652
0
    unsigned int value;
1653
1654
0
    if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
1655
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1656
0
        return 0;
1657
0
    }
1658
1659
    /* |value| should contains a valid max-fragment-length code. */
1660
0
    if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
1661
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1662
0
            SSL_R_TLS_EXT_INVALID_MAX_FRAGMENT_LENGTH);
1663
0
        return 0;
1664
0
    }
1665
1666
    /* Must be the same value as client-configured one who was sent to server */
1667
    /*-
1668
     * RFC 6066: if a client receives a maximum fragment length negotiation
1669
     * response that differs from the length it requested, ...
1670
     * It must abort with SSL_AD_ILLEGAL_PARAMETER alert
1671
     */
1672
0
    if (value != s->ext.max_fragment_len_mode) {
1673
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1674
0
            SSL_R_TLS_EXT_INVALID_MAX_FRAGMENT_LENGTH);
1675
0
        return 0;
1676
0
    }
1677
1678
    /*
1679
     * Maximum Fragment Length Negotiation succeeded.
1680
     * The negotiated Maximum Fragment Length is binding now.
1681
     */
1682
0
    s->session->ext.max_fragment_len_mode = value;
1683
1684
0
    return 1;
1685
0
}
1686
1687
int tls_parse_stoc_server_name(SSL_CONNECTION *s, PACKET *pkt,
1688
    unsigned int context,
1689
    X509 *x, size_t chainidx)
1690
6.55k
{
1691
6.55k
    char *eff_sni = s->ext.hostname;
1692
1693
6.55k
#ifndef OPENSSL_NO_ECH
1694
    /* if we tried ECH and failed, the outer is what's expected */
1695
6.55k
    if (s->ext.ech.es != NULL && s->ext.ech.success == 0)
1696
0
        eff_sni = s->ext.ech.outer_hostname;
1697
6.55k
#endif
1698
6.55k
    if (eff_sni == NULL) {
1699
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1700
0
        return 0;
1701
0
    }
1702
6.55k
    if (PACKET_remaining(pkt) > 0) {
1703
10
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1704
10
        return 0;
1705
10
    }
1706
6.54k
    if (!s->hit) {
1707
6.54k
        if (s->session->ext.hostname != NULL) {
1708
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1709
0
            return 0;
1710
0
        }
1711
6.54k
        s->session->ext.hostname = OPENSSL_strdup(eff_sni);
1712
6.54k
        if (s->session->ext.hostname == NULL) {
1713
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1714
0
            return 0;
1715
0
        }
1716
6.54k
    }
1717
6.54k
    return 1;
1718
6.54k
}
1719
1720
int tls_parse_stoc_ec_pt_formats(SSL_CONNECTION *s, PACKET *pkt,
1721
    unsigned int context,
1722
    X509 *x, size_t chainidx)
1723
1.98k
{
1724
1.98k
    size_t ecpointformats_len;
1725
1.98k
    PACKET ecptformatlist;
1726
1727
1.98k
    if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) {
1728
68
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1729
68
        return 0;
1730
68
    }
1731
1.91k
    if (!s->hit) {
1732
1.91k
        ecpointformats_len = PACKET_remaining(&ecptformatlist);
1733
1.91k
        if (ecpointformats_len == 0) {
1734
9
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
1735
9
            return 0;
1736
9
        }
1737
1738
1.90k
        s->ext.peer_ecpointformats_len = 0;
1739
1.90k
        OPENSSL_free(s->ext.peer_ecpointformats);
1740
1.90k
        s->ext.peer_ecpointformats = OPENSSL_malloc(ecpointformats_len);
1741
1.90k
        if (s->ext.peer_ecpointformats == NULL) {
1742
0
            s->ext.peer_ecpointformats_len = 0;
1743
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1744
0
            return 0;
1745
0
        }
1746
1747
1.90k
        s->ext.peer_ecpointformats_len = ecpointformats_len;
1748
1749
1.90k
        if (!PACKET_copy_bytes(&ecptformatlist,
1750
1.90k
                s->ext.peer_ecpointformats,
1751
1.90k
                ecpointformats_len)) {
1752
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1753
0
            return 0;
1754
0
        }
1755
1.90k
    }
1756
1757
1.90k
    return 1;
1758
1.91k
}
1759
1760
int tls_parse_stoc_session_ticket(SSL_CONNECTION *s, PACKET *pkt,
1761
    unsigned int context,
1762
    X509 *x, size_t chainidx)
1763
10.4k
{
1764
10.4k
    SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);
1765
1766
10.4k
    if (s->ext.session_ticket_cb != NULL && !s->ext.session_ticket_cb(ssl, PACKET_data(pkt), (int)PACKET_remaining(pkt), s->ext.session_ticket_cb_arg)) {
1767
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION);
1768
0
        return 0;
1769
0
    }
1770
1771
10.4k
    if (!tls_use_ticket(s)) {
1772
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1773
0
        return 0;
1774
0
    }
1775
10.4k
    if (PACKET_remaining(pkt) > 0) {
1776
16
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1777
16
        return 0;
1778
16
    }
1779
1780
10.4k
    s->ext.ticket_expected = 1;
1781
1782
10.4k
    return 1;
1783
10.4k
}
1784
1785
#ifndef OPENSSL_NO_OCSP
1786
int tls_parse_stoc_status_request(SSL_CONNECTION *s, PACKET *pkt,
1787
    unsigned int context,
1788
    X509 *x, size_t chainidx)
1789
2
{
1790
2
    if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
1791
        /* We ignore this if the server sends a CertificateRequest */
1792
2
        return 1;
1793
2
    }
1794
1795
    /*
1796
     * MUST only be sent if we've requested a status
1797
     * request message. In TLS <= 1.2 it must also be empty.
1798
     */
1799
0
    if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
1800
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1801
0
        return 0;
1802
0
    }
1803
0
    if (!SSL_CONNECTION_IS_TLS13(s) && PACKET_remaining(pkt) > 0) {
1804
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1805
0
        return 0;
1806
0
    }
1807
1808
0
    if (SSL_CONNECTION_IS_TLS13(s)) {
1809
        /* SSLfatal() already called */
1810
0
        return tls_process_cert_status_body(s, chainidx, pkt);
1811
0
    }
1812
1813
    /* Set flag to expect CertificateStatus message */
1814
0
    s->ext.status_expected = 1;
1815
1816
0
    return 1;
1817
0
}
1818
#endif
1819
1820
#ifndef OPENSSL_NO_CT
1821
int tls_parse_stoc_sct(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1822
    X509 *x, size_t chainidx)
1823
31
{
1824
31
    if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
1825
        /* We ignore this if the server sends it in a CertificateRequest */
1826
6
        return 1;
1827
6
    }
1828
1829
    /*
1830
     * Only take it if we asked for it - i.e if there is no CT validation
1831
     * callback set, then a custom extension MAY be processing it, so we
1832
     * need to let control continue to flow to that.
1833
     */
1834
25
    if (s->ct_validation_callback != NULL) {
1835
0
        size_t size = PACKET_remaining(pkt);
1836
1837
        /* Simply copy it off for later processing */
1838
0
        OPENSSL_free(s->ext.scts);
1839
0
        s->ext.scts = NULL;
1840
1841
0
        s->ext.scts_len = (uint16_t)size;
1842
0
        if (size > 0) {
1843
0
            s->ext.scts = OPENSSL_malloc(size);
1844
0
            if (s->ext.scts == NULL) {
1845
0
                s->ext.scts_len = 0;
1846
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
1847
0
                return 0;
1848
0
            }
1849
0
            if (!PACKET_copy_bytes(pkt, s->ext.scts, size)) {
1850
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1851
0
                return 0;
1852
0
            }
1853
0
        }
1854
25
    } else {
1855
25
        ENDPOINT role = (context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
1856
25
            ? ENDPOINT_CLIENT
1857
25
            : ENDPOINT_BOTH;
1858
1859
        /*
1860
         * If we didn't ask for it then there must be a custom extension,
1861
         * otherwise this is unsolicited.
1862
         */
1863
25
        if (custom_ext_find(&s->cert->custext, role,
1864
25
                TLSEXT_TYPE_signed_certificate_timestamp,
1865
25
                NULL)
1866
25
            == NULL) {
1867
25
            SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1868
25
            return 0;
1869
25
        }
1870
1871
0
        if (!custom_ext_parse(s, context,
1872
0
                TLSEXT_TYPE_signed_certificate_timestamp,
1873
0
                PACKET_data(pkt), PACKET_remaining(pkt),
1874
0
                x, chainidx)) {
1875
            /* SSLfatal already called */
1876
0
            return 0;
1877
0
        }
1878
0
    }
1879
1880
0
    return 1;
1881
25
}
1882
#endif
1883
1884
#ifndef OPENSSL_NO_NEXTPROTONEG
1885
/*
1886
 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1887
 * elements of zero length are allowed and the set of elements must exactly
1888
 * fill the length of the block. Returns 1 on success or 0 on failure.
1889
 */
1890
static int ssl_next_proto_validate(SSL_CONNECTION *s, PACKET *pkt)
1891
0
{
1892
0
    PACKET tmp_protocol;
1893
1894
0
    while (PACKET_remaining(pkt)) {
1895
0
        if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
1896
0
            || PACKET_remaining(&tmp_protocol) == 0) {
1897
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1898
0
            return 0;
1899
0
        }
1900
0
    }
1901
1902
0
    return 1;
1903
0
}
1904
1905
int tls_parse_stoc_npn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1906
    X509 *x, size_t chainidx)
1907
0
{
1908
0
    unsigned char *selected;
1909
0
    unsigned char selected_len;
1910
0
    PACKET tmppkt;
1911
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1912
1913
    /* Check if we are in a renegotiation. If so ignore this extension */
1914
0
    if (!SSL_IS_FIRST_HANDSHAKE(s))
1915
0
        return 1;
1916
1917
    /* We must have requested it. */
1918
0
    if (sctx->ext.npn_select_cb == NULL) {
1919
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1920
0
        return 0;
1921
0
    }
1922
1923
    /* The data must be valid */
1924
0
    tmppkt = *pkt;
1925
0
    if (!ssl_next_proto_validate(s, &tmppkt)) {
1926
        /* SSLfatal() already called */
1927
0
        return 0;
1928
0
    }
1929
0
    if (sctx->ext.npn_select_cb(SSL_CONNECTION_GET_USER_SSL(s),
1930
0
            &selected, &selected_len,
1931
0
            PACKET_data(pkt), (unsigned int)PACKET_remaining(pkt),
1932
0
            sctx->ext.npn_select_cb_arg)
1933
0
            != SSL_TLSEXT_ERR_OK
1934
0
        || selected_len == 0) {
1935
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION);
1936
0
        return 0;
1937
0
    }
1938
1939
    /*
1940
     * Could be non-NULL if server has sent multiple NPN extensions in
1941
     * a single Serverhello
1942
     */
1943
0
    OPENSSL_free(s->ext.npn);
1944
0
    s->ext.npn = OPENSSL_malloc(selected_len);
1945
0
    if (s->ext.npn == NULL) {
1946
0
        s->ext.npn_len = 0;
1947
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1948
0
        return 0;
1949
0
    }
1950
1951
0
    memcpy(s->ext.npn, selected, selected_len);
1952
0
    s->ext.npn_len = selected_len;
1953
0
    s->s3.npn_seen = 1;
1954
1955
0
    return 1;
1956
0
}
1957
#endif
1958
1959
int tls_parse_stoc_alpn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1960
    X509 *x, size_t chainidx)
1961
21.7k
{
1962
21.7k
    size_t len;
1963
21.7k
    PACKET confpkt, protpkt;
1964
21.7k
    int valid = 0;
1965
1966
    /* We must have requested it. */
1967
21.7k
    if (!s->s3.alpn_sent) {
1968
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1969
0
        return 0;
1970
0
    }
1971
    /*-
1972
     * The extension data consists of:
1973
     *   uint16 list_length
1974
     *   uint8 proto_length;
1975
     *   uint8 proto[proto_length];
1976
     */
1977
21.7k
    if (!PACKET_get_net_2_len(pkt, &len)
1978
21.7k
        || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len)
1979
21.7k
        || PACKET_remaining(pkt) != len) {
1980
65
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1981
65
        return 0;
1982
65
    }
1983
1984
    /* It must be a protocol that we sent */
1985
21.6k
    if (!PACKET_buf_init(&confpkt, s->ext.alpn, s->ext.alpn_len)) {
1986
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1987
0
        return 0;
1988
0
    }
1989
21.7k
    while (PACKET_get_length_prefixed_1(&confpkt, &protpkt)) {
1990
21.6k
        if (PACKET_remaining(&protpkt) != len)
1991
6
            continue;
1992
21.6k
        if (memcmp(PACKET_data(pkt), PACKET_data(&protpkt), len) == 0) {
1993
            /* Valid protocol found */
1994
21.6k
            valid = 1;
1995
21.6k
            break;
1996
21.6k
        }
1997
21.6k
    }
1998
1999
21.6k
    if (!valid) {
2000
        /* The protocol sent from the server does not match one we advertised */
2001
75
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2002
75
        return 0;
2003
75
    }
2004
2005
21.6k
    OPENSSL_free(s->s3.alpn_selected);
2006
21.6k
    s->s3.alpn_selected = OPENSSL_malloc(len);
2007
21.6k
    if (s->s3.alpn_selected == NULL) {
2008
0
        s->s3.alpn_selected_len = 0;
2009
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2010
0
        return 0;
2011
0
    }
2012
21.6k
    if (!PACKET_copy_bytes(pkt, s->s3.alpn_selected, len)) {
2013
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2014
0
        return 0;
2015
0
    }
2016
21.6k
    s->s3.alpn_selected_len = len;
2017
2018
21.6k
    if (s->session->ext.alpn_selected == NULL
2019
0
        || s->session->ext.alpn_selected_len != len
2020
0
        || memcmp(s->session->ext.alpn_selected, s->s3.alpn_selected, len)
2021
21.6k
            != 0) {
2022
        /* ALPN not consistent with the old session so cannot use early_data */
2023
21.6k
        s->ext.early_data_ok = 0;
2024
21.6k
    }
2025
21.6k
    if (!s->hit) {
2026
        /*
2027
         * This is a new session and so alpn_selected should have been
2028
         * initialised to NULL. We should update it with the selected ALPN.
2029
         */
2030
21.6k
        if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
2031
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2032
0
            return 0;
2033
0
        }
2034
21.6k
        s->session->ext.alpn_selected = OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len);
2035
21.6k
        if (s->session->ext.alpn_selected == NULL) {
2036
0
            s->session->ext.alpn_selected_len = 0;
2037
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2038
0
            return 0;
2039
0
        }
2040
21.6k
        s->session->ext.alpn_selected_len = s->s3.alpn_selected_len;
2041
21.6k
    }
2042
2043
21.6k
    return 1;
2044
21.6k
}
2045
2046
#ifndef OPENSSL_NO_SRTP
2047
int tls_parse_stoc_use_srtp(SSL_CONNECTION *s, PACKET *pkt,
2048
    unsigned int context, X509 *x, size_t chainidx)
2049
0
{
2050
0
    unsigned int id, ct, mki;
2051
0
    int i;
2052
0
    STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
2053
0
    SRTP_PROTECTION_PROFILE *prof;
2054
2055
0
    if (!PACKET_get_net_2(pkt, &ct) || ct != 2
2056
0
        || !PACKET_get_net_2(pkt, &id)
2057
0
        || !PACKET_get_1(pkt, &mki)
2058
0
        || PACKET_remaining(pkt) != 0) {
2059
0
        SSLfatal(s, SSL_AD_DECODE_ERROR,
2060
0
            SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
2061
0
        return 0;
2062
0
    }
2063
2064
0
    if (mki != 0) {
2065
        /* Must be no MKI, since we never offer one */
2066
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRTP_MKI_VALUE);
2067
0
        return 0;
2068
0
    }
2069
2070
    /* Throw an error if the server gave us an unsolicited extension */
2071
0
    clnt = SSL_get_srtp_profiles(SSL_CONNECTION_GET_SSL(s));
2072
0
    if (clnt == NULL) {
2073
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_SRTP_PROFILES);
2074
0
        return 0;
2075
0
    }
2076
2077
    /*
2078
     * Check to see if the server gave us something we support (and
2079
     * presumably offered)
2080
     */
2081
0
    for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
2082
0
        prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
2083
2084
0
        if (prof->id == id) {
2085
0
            s->srtp_profile = prof;
2086
0
            return 1;
2087
0
        }
2088
0
    }
2089
2090
0
    SSLfatal(s, SSL_AD_DECODE_ERROR,
2091
0
        SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
2092
0
    return 0;
2093
0
}
2094
#endif
2095
2096
int tls_parse_stoc_etm(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
2097
    X509 *x, size_t chainidx)
2098
3.87k
{
2099
    /* Ignore if inappropriate ciphersuite */
2100
3.87k
    if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
2101
3.87k
        && s->s3.tmp.new_cipher->algorithm_mac != SSL_AEAD
2102
2.56k
        && s->s3.tmp.new_cipher->algorithm_enc != SSL_RC4
2103
2.56k
        && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT
2104
2.56k
        && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT12
2105
2.56k
        && s->s3.tmp.new_cipher->algorithm_enc != SSL_MAGMA
2106
2.56k
        && s->s3.tmp.new_cipher->algorithm_enc != SSL_KUZNYECHIK)
2107
2.56k
        s->ext.use_etm = 1;
2108
2109
3.87k
    return 1;
2110
3.87k
}
2111
2112
int tls_parse_stoc_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
2113
    X509 *x, size_t chainidx)
2114
7.89k
{
2115
7.89k
    if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)
2116
0
        return 1;
2117
7.89k
    s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2118
7.89k
    if (!s->hit)
2119
7.88k
        s->session->flags |= SSL_SESS_FLAG_EXTMS;
2120
2121
7.89k
    return 1;
2122
7.89k
}
2123
2124
int tls_parse_stoc_supported_versions(SSL_CONNECTION *s, PACKET *pkt,
2125
    unsigned int context,
2126
    X509 *x, size_t chainidx)
2127
19.6k
{
2128
19.6k
    unsigned int version;
2129
2130
19.6k
    if (!PACKET_get_net_2(pkt, &version)
2131
19.5k
        || PACKET_remaining(pkt) != 0) {
2132
66
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2133
66
        return 0;
2134
66
    }
2135
2136
    /*
2137
     * The only protocol version we support which is valid in this extension in
2138
     * a ServerHello is TLSv1.3 therefore we shouldn't be getting anything else.
2139
     */
2140
19.5k
    if (version != TLS1_3_VERSION) {
2141
127
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
2142
127
            SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
2143
127
        return 0;
2144
127
    }
2145
2146
    /* We ignore this extension for HRRs except to sanity check it */
2147
19.4k
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)
2148
371
        return 1;
2149
2150
    /* We just set it here. We validate it in ssl_choose_client_version */
2151
19.0k
    s->version = version;
2152
19.0k
    if (!ssl_set_record_protocol_version(s, version)) {
2153
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2154
0
        return 0;
2155
0
    }
2156
2157
19.0k
    return 1;
2158
19.0k
}
2159
2160
int tls_parse_stoc_key_share(SSL_CONNECTION *s, PACKET *pkt,
2161
    unsigned int context, X509 *x,
2162
    size_t chainidx)
2163
13.7k
{
2164
13.7k
#ifndef OPENSSL_NO_TLS1_3
2165
13.7k
    unsigned int group_id;
2166
13.7k
    PACKET encoded_pt;
2167
13.7k
    EVP_PKEY *ckey = s->s3.tmp.pkey, *skey = NULL;
2168
13.7k
    const TLS_GROUP_INFO *ginf = NULL;
2169
13.7k
    uint16_t valid_ks_id = 0;
2170
13.7k
    size_t i;
2171
2172
    /* Sanity check */
2173
13.7k
    if (ckey == NULL || s->s3.peer_tmp != NULL) {
2174
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2175
0
        return 0;
2176
0
    }
2177
2178
    /* Which group ID does the server want -> group_id */
2179
13.7k
    if (!PACKET_get_net_2(pkt, &group_id)) {
2180
6
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2181
6
        return 0;
2182
6
    }
2183
2184
13.7k
    if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) {
2185
294
        const uint16_t *pgroups = NULL;
2186
294
        size_t num_groups;
2187
2188
294
        if (PACKET_remaining(pkt) != 0) {
2189
6
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2190
6
            return 0;
2191
6
        }
2192
2193
        /*
2194
         * It is an error if the HelloRetryRequest wants a key_share that we
2195
         * already sent in the first ClientHello
2196
         */
2197
858
        for (i = 0; i < s->s3.tmp.num_ks_pkey; i++) {
2198
576
            if (s->s3.tmp.ks_group_id[i] == group_id) {
2199
6
                SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
2200
6
                return 0;
2201
6
            }
2202
576
        }
2203
2204
        /* Validate the selected group is one we support */
2205
282
        tls1_get_supported_groups(s, &pgroups, &num_groups);
2206
1.75k
        for (i = 0; i < num_groups; i++) {
2207
1.67k
            if (group_id == pgroups[i])
2208
207
                break;
2209
1.67k
        }
2210
282
        if (i >= num_groups
2211
207
            || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)
2212
207
            || !tls_valid_group(s, group_id, TLS1_3_VERSION, TLS1_3_VERSION,
2213
207
                NULL, NULL)) {
2214
75
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
2215
75
            return 0;
2216
75
        }
2217
2218
        /* Memorize which groupID the server wants */
2219
207
        s->s3.group_id = group_id;
2220
2221
        /* The initial keyshares are obsolete now, hence free memory */
2222
621
        for (i = 0; i < s->s3.tmp.num_ks_pkey; i++) {
2223
414
            if (s->s3.tmp.ks_pkey[i] != NULL) {
2224
414
                EVP_PKEY_free(s->s3.tmp.ks_pkey[i]);
2225
414
                s->s3.tmp.ks_pkey[i] = NULL;
2226
414
            }
2227
414
        }
2228
207
        s->s3.tmp.num_ks_pkey = 0;
2229
207
        s->s3.tmp.pkey = NULL;
2230
2231
207
        return 1;
2232
282
    }
2233
2234
    /*
2235
     * check that the group requested by the server is one we've
2236
     * sent a key share for, and if so: memorize which one
2237
     */
2238
26.9k
    for (i = 0; i < s->s3.tmp.num_ks_pkey; i++) {
2239
26.8k
        if (s->s3.tmp.ks_group_id[i] == group_id) {
2240
13.3k
            valid_ks_id = group_id;
2241
13.3k
            ckey = s->s3.tmp.ks_pkey[i];
2242
13.3k
            s->s3.group_id = group_id;
2243
13.3k
            s->s3.tmp.pkey = ckey;
2244
13.3k
            break;
2245
13.3k
        }
2246
26.8k
    }
2247
13.4k
    if (valid_ks_id == 0) {
2248
        /*
2249
         * This isn't for the group that we sent in the original
2250
         * key_share!
2251
         */
2252
69
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
2253
69
        return 0;
2254
69
    }
2255
    /* Retain this group in the SSL_SESSION */
2256
13.3k
    if (!s->hit) {
2257
13.3k
        s->session->kex_group = group_id;
2258
13.3k
    } else if (group_id != s->session->kex_group) {
2259
        /*
2260
         * If this is a resumption but changed what group was used, we need
2261
         * to record the new group in the session, but the session is not
2262
         * a new session and could be in use by other threads.  So, make
2263
         * a copy of the session to record the new information so that it's
2264
         * useful for any sessions resumed from tickets issued on this
2265
         * connection.
2266
         */
2267
0
        SSL_SESSION *new_sess;
2268
2269
0
        if ((new_sess = ssl_session_dup(s->session, 0)) == NULL) {
2270
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
2271
0
            return 0;
2272
0
        }
2273
0
        SSL_SESSION_free(s->session);
2274
0
        s->session = new_sess;
2275
0
        s->session->kex_group = group_id;
2276
0
    }
2277
2278
13.3k
    if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),
2279
13.3k
             group_id))
2280
13.3k
        == NULL) {
2281
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
2282
0
        return 0;
2283
0
    }
2284
2285
13.3k
    if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt)
2286
13.3k
        || PACKET_remaining(&encoded_pt) == 0) {
2287
71
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2288
71
        return 0;
2289
71
    }
2290
2291
13.3k
    if (!ginf->is_kem) {
2292
        /* Regular KEX */
2293
13.3k
        skey = EVP_PKEY_new();
2294
13.3k
        if (skey == NULL || EVP_PKEY_copy_parameters(skey, ckey) <= 0) {
2295
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
2296
0
            EVP_PKEY_free(skey);
2297
0
            return 0;
2298
0
        }
2299
2300
13.3k
        if (tls13_set_encoded_pub_key(skey, PACKET_data(&encoded_pt),
2301
13.3k
                PACKET_remaining(&encoded_pt))
2302
13.3k
            <= 0) {
2303
33
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
2304
33
            EVP_PKEY_free(skey);
2305
33
            return 0;
2306
33
        }
2307
2308
13.2k
        if (ssl_derive(s, ckey, skey, 1) == 0) {
2309
            /* SSLfatal() already called */
2310
9
            EVP_PKEY_free(skey);
2311
9
            return 0;
2312
9
        }
2313
13.2k
        s->s3.peer_tmp = skey;
2314
13.2k
    } else {
2315
        /* KEM Mode */
2316
2
        const unsigned char *ct = PACKET_data(&encoded_pt);
2317
2
        size_t ctlen = PACKET_remaining(&encoded_pt);
2318
2319
2
        if (ssl_decapsulate(s, ckey, ct, ctlen, 1) == 0) {
2320
            /* SSLfatal() already called */
2321
2
            return 0;
2322
2
        }
2323
2
    }
2324
13.2k
    s->s3.did_kex = 1;
2325
13.2k
#endif
2326
2327
13.2k
    return 1;
2328
13.3k
}
2329
2330
int tls_parse_stoc_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
2331
    X509 *x, size_t chainidx)
2332
80
{
2333
80
    PACKET cookie;
2334
2335
80
    if (!PACKET_as_length_prefixed_2(pkt, &cookie)
2336
30
        || !PACKET_memdup(&cookie, &s->ext.tls13_cookie,
2337
50
            &s->ext.tls13_cookie_len)) {
2338
50
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2339
50
        return 0;
2340
50
    }
2341
2342
30
    return 1;
2343
80
}
2344
2345
int tls_parse_stoc_early_data(SSL_CONNECTION *s, PACKET *pkt,
2346
    unsigned int context,
2347
    X509 *x, size_t chainidx)
2348
0
{
2349
0
    if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
2350
0
        unsigned long max_early_data;
2351
2352
0
        if (!PACKET_get_net_4(pkt, &max_early_data)
2353
0
            || PACKET_remaining(pkt) != 0) {
2354
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_MAX_EARLY_DATA);
2355
0
            return 0;
2356
0
        }
2357
2358
0
        s->session->ext.max_early_data = max_early_data;
2359
2360
0
        if (SSL_IS_QUIC_HANDSHAKE(s) && max_early_data != 0xffffffff) {
2361
            /*
2362
             * QUIC allows missing max_early_data, or a max_early_data value
2363
             * of 0xffffffff. Missing max_early_data is stored in the session
2364
             * as 0. This is indistinguishable in OpenSSL from a present
2365
             * max_early_data value that was 0. In order that later checks for
2366
             * invalid max_early_data correctly treat as an error the case where
2367
             * max_early_data is present and it is 0, we store any invalid
2368
             * value in the same (non-zero) way. Otherwise we would have to
2369
             * introduce a new flag just for this.
2370
             */
2371
0
            s->session->ext.max_early_data = 1;
2372
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INVALID_MAX_EARLY_DATA);
2373
0
            return 0;
2374
0
        }
2375
2376
0
        return 1;
2377
0
    }
2378
2379
0
    if (PACKET_remaining(pkt) != 0) {
2380
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2381
0
        return 0;
2382
0
    }
2383
2384
0
    if (!s->ext.early_data_ok
2385
0
        || !s->hit) {
2386
        /*
2387
         * If we get here then we didn't send early data, or we didn't resume
2388
         * using the first identity, or the SNI/ALPN is not consistent so the
2389
         * server should not be accepting it.
2390
         */
2391
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
2392
0
        return 0;
2393
0
    }
2394
2395
0
    s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
2396
2397
0
    return 1;
2398
0
}
2399
2400
int tls_parse_stoc_psk(SSL_CONNECTION *s, PACKET *pkt,
2401
    unsigned int context, X509 *x,
2402
    size_t chainidx)
2403
0
{
2404
0
#ifndef OPENSSL_NO_TLS1_3
2405
0
    unsigned int identity;
2406
2407
0
    if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) {
2408
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2409
0
        return 0;
2410
0
    }
2411
2412
0
    if (identity >= (unsigned int)s->ext.tick_identity) {
2413
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_PSK_IDENTITY);
2414
0
        return 0;
2415
0
    }
2416
2417
    /*
2418
     * Session resumption tickets are always sent before PSK tickets. If the
2419
     * ticket index is 0 then it must be for a session resumption ticket if we
2420
     * sent two tickets, or if we didn't send a PSK ticket.
2421
     */
2422
0
    if (identity == 0 && (s->psksession == NULL || s->ext.tick_identity == 2)) {
2423
0
        s->hit = 1;
2424
0
        SSL_SESSION_free(s->psksession);
2425
0
        s->psksession = NULL;
2426
0
        return 1;
2427
0
    }
2428
2429
0
    if (s->psksession == NULL) {
2430
        /* Should never happen */
2431
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2432
0
        return 0;
2433
0
    }
2434
2435
    /*
2436
     * If we used the external PSK for sending early_data then s->early_secret
2437
     * is already set up, so don't overwrite it. Otherwise we copy the
2438
     * early_secret across that we generated earlier.
2439
     */
2440
0
    if ((s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
2441
0
            && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
2442
0
        || s->session->ext.max_early_data > 0
2443
0
        || s->psksession->ext.max_early_data == 0)
2444
0
        memcpy(s->early_secret, s->psksession->early_secret, EVP_MAX_MD_SIZE);
2445
2446
0
    SSL_SESSION_free(s->session);
2447
0
    s->session = s->psksession;
2448
0
    s->psksession = NULL;
2449
0
    s->hit = 1;
2450
    /* Early data is only allowed if we used the first ticket */
2451
0
    if (identity != 0)
2452
0
        s->ext.early_data_ok = 0;
2453
0
#endif
2454
2455
0
    return 1;
2456
0
}
2457
2458
EXT_RETURN tls_construct_ctos_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2459
    unsigned int context,
2460
    X509 *x, size_t chainidx)
2461
60.8k
{
2462
60.8k
    sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2463
60.8k
    if (sc->client_cert_type == NULL)
2464
60.8k
        return EXT_RETURN_NOT_SENT;
2465
0
#ifndef OPENSSL_NO_ECH
2466
0
    ECH_SAME_EXT(sc, context, pkt)
2467
0
#endif
2468
2469
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type)
2470
0
        || !WPACKET_start_sub_packet_u16(pkt)
2471
0
        || !WPACKET_sub_memcpy_u8(pkt, sc->client_cert_type, sc->client_cert_type_len)
2472
0
        || !WPACKET_close(pkt)) {
2473
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2474
0
        return EXT_RETURN_FAIL;
2475
0
    }
2476
0
    sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD;
2477
0
    return EXT_RETURN_SENT;
2478
0
}
2479
2480
int tls_parse_stoc_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2481
    unsigned int context,
2482
    X509 *x, size_t chainidx)
2483
0
{
2484
0
    unsigned int type;
2485
2486
0
    if (PACKET_remaining(pkt) != 1) {
2487
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2488
0
        return 0;
2489
0
    }
2490
0
    if (!PACKET_get_1(pkt, &type)) {
2491
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2492
0
        return 0;
2493
0
    }
2494
    /* We did not send/ask for this */
2495
0
    if (!ossl_assert(sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) {
2496
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2497
0
        return 0;
2498
0
    }
2499
    /* We don't have this enabled */
2500
0
    if (sc->client_cert_type == NULL) {
2501
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2502
0
        return 0;
2503
0
    }
2504
    /* Given back a value we didn't configure */
2505
0
    if (memchr(sc->client_cert_type, type, sc->client_cert_type_len) == NULL) {
2506
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE);
2507
0
        return 0;
2508
0
    }
2509
0
    sc->ext.client_cert_type = type;
2510
0
    return 1;
2511
0
}
2512
2513
EXT_RETURN tls_construct_ctos_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2514
    unsigned int context,
2515
    X509 *x, size_t chainidx)
2516
60.8k
{
2517
60.8k
    sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2518
60.8k
    if (sc->server_cert_type == NULL)
2519
60.8k
        return EXT_RETURN_NOT_SENT;
2520
0
#ifndef OPENSSL_NO_ECH
2521
0
    ECH_SAME_EXT(sc, context, pkt)
2522
0
#endif
2523
2524
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type)
2525
0
        || !WPACKET_start_sub_packet_u16(pkt)
2526
0
        || !WPACKET_sub_memcpy_u8(pkt, sc->server_cert_type, sc->server_cert_type_len)
2527
0
        || !WPACKET_close(pkt)) {
2528
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2529
0
        return EXT_RETURN_FAIL;
2530
0
    }
2531
0
    sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD;
2532
0
    return EXT_RETURN_SENT;
2533
0
}
2534
2535
int tls_parse_stoc_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2536
    unsigned int context,
2537
    X509 *x, size_t chainidx)
2538
0
{
2539
0
    unsigned int type;
2540
2541
0
    if (PACKET_remaining(pkt) != 1) {
2542
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2543
0
        return 0;
2544
0
    }
2545
0
    if (!PACKET_get_1(pkt, &type)) {
2546
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2547
0
        return 0;
2548
0
    }
2549
    /* We did not send/ask for this */
2550
0
    if (!ossl_assert(sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) {
2551
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2552
0
        return 0;
2553
0
    }
2554
    /* We don't have this enabled */
2555
0
    if (sc->server_cert_type == NULL) {
2556
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2557
0
        return 0;
2558
0
    }
2559
    /* Given back a value we didn't configure */
2560
0
    if (memchr(sc->server_cert_type, type, sc->server_cert_type_len) == NULL) {
2561
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE);
2562
0
        return 0;
2563
0
    }
2564
0
    sc->ext.server_cert_type = type;
2565
0
    return 1;
2566
0
}
2567
2568
#ifndef OPENSSL_NO_ECH
2569
EXT_RETURN tls_construct_ctos_ech(SSL_CONNECTION *s, WPACKET *pkt,
2570
    unsigned int context, X509 *x,
2571
    size_t chainidx)
2572
56.4k
{
2573
56.4k
    int rv = 0, hpke_mode = OSSL_HPKE_MODE_BASE;
2574
56.4k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2575
56.4k
    OSSL_ECHSTORE_ENTRY *ee = NULL;
2576
56.4k
    OSSL_HPKE_SUITE hpke_suite = OSSL_HPKE_SUITE_DEFAULT;
2577
56.4k
    unsigned char config_id_to_use = 0x00, info[OSSL_ECH_MAX_INFO_LEN];
2578
56.4k
    unsigned char *encoded = NULL, *mypub = NULL;
2579
56.4k
    size_t cipherlen = 0, aad_len = 0, lenclen = 0, mypub_len = 0;
2580
56.4k
    size_t info_len = OSSL_ECH_MAX_INFO_LEN, clear_len = 0, encoded_len = 0;
2581
    /* whether or not we've been asked to GREASE, one way or another */
2582
56.4k
    int grease_opt_set = (s->ext.ech.attempted != 1
2583
56.4k
        && ((s->ext.ech.grease == OSSL_ECH_IS_GREASE)
2584
56.4k
            || ((s->options & SSL_OP_ECH_GREASE) != 0)));
2585
2586
    /* if we're not doing real ECH and not GREASEing then exit */
2587
56.4k
    if (s->ext.ech.attempted_type != TLSEXT_TYPE_ech && grease_opt_set == 0)
2588
56.4k
        return EXT_RETURN_NOT_SENT;
2589
    /* send grease if not really attempting ECH */
2590
0
    if (grease_opt_set == 1) {
2591
0
        if (s->hello_retry_request == SSL_HRR_PENDING
2592
0
            && s->ext.ech.sent != NULL) {
2593
            /* re-tx already sent GREASEy ECH */
2594
0
            if (WPACKET_memcpy(pkt, s->ext.ech.sent,
2595
0
                    s->ext.ech.sent_len)
2596
0
                != 1) {
2597
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2598
0
                return EXT_RETURN_FAIL;
2599
0
            }
2600
0
            return EXT_RETURN_SENT;
2601
0
        }
2602
        /* if nobody set a type, use the default */
2603
0
        if (s->ext.ech.attempted_type == OSSL_ECH_type_unknown)
2604
0
            s->ext.ech.attempted_type = TLSEXT_TYPE_ech;
2605
0
        if (ossl_ech_send_grease(s, pkt) != 1) {
2606
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2607
0
            return EXT_RETURN_FAIL;
2608
0
        }
2609
0
        return EXT_RETURN_SENT;
2610
0
    }
2611
2612
    /* For the inner CH - we simply include one of these saying "inner" */
2613
0
    if (s->ext.ech.ch_depth == 1) {
2614
0
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ech)
2615
0
            || !WPACKET_start_sub_packet_u16(pkt)
2616
0
            || !WPACKET_put_bytes_u8(pkt, OSSL_ECH_INNER_CH_TYPE)
2617
0
            || !WPACKET_close(pkt)) {
2618
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2619
0
            return EXT_RETURN_FAIL;
2620
0
        }
2621
0
        return EXT_RETURN_SENT;
2622
0
    }
2623
2624
    /*
2625
     * If not GREASEing we prepare sending the outer value - after the
2626
     * entire thing has been constructed, putting in zeros for now where
2627
     * we'd otherwise include ECH ciphertext, we later encode and encrypt.
2628
     * We need to do it that way as we need the rest of the outer CH to
2629
     * be known and used as AAD input before we do encryption.
2630
     */
2631
0
    if (s->ext.ech.ch_depth != 0)
2632
0
        return EXT_RETURN_NOT_SENT;
2633
    /* Make ClientHelloInner and EncodedClientHelloInner as per spec. */
2634
0
    if (ossl_ech_encode_inner(s, &encoded, &encoded_len) != 1) {
2635
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2636
0
        goto err;
2637
0
    }
2638
0
    s->ext.ech.encoded_inner = encoded;
2639
0
    s->ext.ech.encoded_inner_len = encoded_len;
2640
0
#ifdef OSSL_ECH_SUPERVERBOSE
2641
0
    ossl_ech_pbuf("encoded inner CH", encoded, encoded_len);
2642
0
#endif
2643
0
    rv = ossl_ech_pick_matching_cfg(s, &ee, &hpke_suite);
2644
0
    if (rv != 1 || ee == NULL) {
2645
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2646
0
        goto err;
2647
0
    }
2648
0
    s->ext.ech.attempted_type = ee->version;
2649
0
    OSSL_TRACE_BEGIN(TLS)
2650
0
    {
2651
0
        BIO_printf(trc_out, "EAAE: selected: version: %4x, config %2x\n",
2652
0
            ee->version, ee->config_id);
2653
0
    }
2654
0
    OSSL_TRACE_END(TLS);
2655
0
    config_id_to_use = ee->config_id; /* if requested, use a random config_id instead */
2656
0
    if ((s->options & SSL_OP_ECH_IGNORE_CID) != 0) {
2657
0
        int max_iters = 1000, i = 0;
2658
2659
        /* rejection sample to get a different but random config_id */
2660
0
        while (config_id_to_use == ee->config_id) {
2661
0
#ifdef OSSL_ECH_SUPERVERBOSE
2662
0
            if (i > 0) {
2663
0
                OSSL_TRACE_BEGIN(TLS)
2664
0
                {
2665
0
                    BIO_printf(trc_out, "EAAE: rejected random-config %02x\n",
2666
0
                        config_id_to_use);
2667
0
                }
2668
0
                OSSL_TRACE_END(TLS);
2669
0
            }
2670
0
#endif
2671
0
            if (RAND_bytes_ex(sctx->libctx, &config_id_to_use, 1, 0) <= 0) {
2672
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2673
0
                return 0;
2674
0
            }
2675
0
            if (i++ >= max_iters) {
2676
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2677
0
                return 0;
2678
0
            }
2679
0
        }
2680
0
#ifdef OSSL_ECH_SUPERVERBOSE
2681
0
        ossl_ech_pbuf("EAAE: random config_id", &config_id_to_use, 1);
2682
0
#endif
2683
0
    }
2684
0
    s->ext.ech.attempted_cid = config_id_to_use;
2685
0
#ifdef OSSL_ECH_SUPERVERBOSE
2686
0
    ossl_ech_pbuf("EAAE: peer pub", ee->pub, ee->pub_len);
2687
0
    ossl_ech_pbuf("EAAE: clear", encoded, encoded_len);
2688
0
    ossl_ech_pbuf("EAAE: ECHConfig", ee->encoded, ee->encoded_len);
2689
0
#endif
2690
    /*
2691
     * The AAD is the full outer client hello but with the correct number of
2692
     * zeros for where the ECH ciphertext octets will later be placed. So we
2693
     * add the ECH extension to the |pkt| but with zeros for ciphertext, that
2694
     * forms up the AAD, then after we've encrypted, we'll splice in the actual
2695
     * ciphertext.
2696
     * Watch out for the "4" offsets that remove the type and 3-octet length
2697
     * from the encoded CH as per the spec.
2698
     */
2699
0
    clear_len = ossl_ech_calc_padding(s, ee, encoded_len);
2700
0
    if (clear_len == 0) {
2701
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2702
0
        goto err;
2703
0
    }
2704
0
    lenclen = OSSL_HPKE_get_public_encap_size(hpke_suite);
2705
0
    if (s->ext.ech.hpke_ctx == NULL) { /* 1st CH */
2706
0
        if (ossl_ech_make_enc_info(ee->encoded, ee->encoded_len,
2707
0
                info, &info_len)
2708
0
            != 1) {
2709
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2710
0
            goto err;
2711
0
        }
2712
0
#ifdef OSSL_ECH_SUPERVERBOSE
2713
0
        ossl_ech_pbuf("EAAE info", info, info_len);
2714
0
#endif
2715
0
        s->ext.ech.hpke_ctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite,
2716
0
            OSSL_HPKE_ROLE_SENDER,
2717
0
            sctx->libctx, sctx->propq);
2718
0
        if (s->ext.ech.hpke_ctx == NULL) {
2719
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2720
0
            goto err;
2721
0
        }
2722
0
        mypub = OPENSSL_malloc(lenclen);
2723
0
        if (mypub == NULL) {
2724
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2725
0
            goto err;
2726
0
        }
2727
0
        mypub_len = lenclen;
2728
0
        rv = OSSL_HPKE_encap(s->ext.ech.hpke_ctx, mypub, &mypub_len,
2729
0
            ee->pub, ee->pub_len, info, info_len);
2730
0
        if (rv != 1) {
2731
0
            OPENSSL_free(mypub);
2732
0
            mypub = NULL;
2733
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2734
0
            goto err;
2735
0
        }
2736
0
        s->ext.ech.pub = mypub;
2737
0
        s->ext.ech.pub_len = mypub_len;
2738
0
    } else { /* HRR - retrieve public */
2739
0
        mypub = s->ext.ech.pub;
2740
0
        mypub_len = s->ext.ech.pub_len;
2741
0
        if (mypub == NULL || mypub_len == 0) {
2742
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2743
0
            goto err;
2744
0
        }
2745
0
    }
2746
0
#ifdef OSSL_ECH_SUPERVERBOSE
2747
0
    ossl_ech_pbuf("EAAE: mypub", mypub, mypub_len);
2748
0
    WPACKET_get_total_written(pkt, &aad_len); /* use aad_len for tracing */
2749
0
    ossl_ech_pbuf("EAAE pkt b4", WPACKET_get_curr(pkt) - aad_len, aad_len);
2750
0
#endif
2751
0
    cipherlen = OSSL_HPKE_get_ciphertext_size(hpke_suite, clear_len);
2752
0
    if (cipherlen <= clear_len || cipherlen > OSSL_ECH_MAX_PAYLOAD_LEN) {
2753
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2754
0
        goto err;
2755
0
    }
2756
0
    s->ext.ech.clearlen = clear_len;
2757
0
    s->ext.ech.cipherlen = cipherlen;
2758
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ech)
2759
0
        || !WPACKET_start_sub_packet_u16(pkt)
2760
0
        || !WPACKET_put_bytes_u8(pkt, OSSL_ECH_OUTER_CH_TYPE)
2761
0
        || !WPACKET_put_bytes_u16(pkt, hpke_suite.kdf_id)
2762
0
        || !WPACKET_put_bytes_u16(pkt, hpke_suite.aead_id)
2763
0
        || !WPACKET_put_bytes_u8(pkt, config_id_to_use)
2764
0
        || (s->hello_retry_request == SSL_HRR_PENDING
2765
0
            && !WPACKET_put_bytes_u16(pkt, 0x00)) /* no pub */
2766
0
        || (s->hello_retry_request != SSL_HRR_PENDING
2767
0
            && !WPACKET_sub_memcpy_u16(pkt, mypub, mypub_len))
2768
0
        || !WPACKET_start_sub_packet_u16(pkt)
2769
0
        || !WPACKET_get_total_written(pkt, &s->ext.ech.cipher_offset)
2770
0
        || !WPACKET_memset(pkt, 0, cipherlen)
2771
0
        || !WPACKET_close(pkt)
2772
0
        || !WPACKET_close(pkt)) {
2773
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2774
0
        goto err;
2775
0
    }
2776
    /* don't count the type + 3-octet length */
2777
0
    s->ext.ech.cipher_offset -= 4;
2778
0
    return EXT_RETURN_SENT;
2779
0
err:
2780
0
    return EXT_RETURN_FAIL;
2781
0
}
2782
2783
/* if the server thinks we GREASE'd then we may get an ECHConfigList */
2784
int tls_parse_stoc_ech(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
2785
    X509 *x, size_t chainidx)
2786
0
{
2787
0
    size_t rlen = 0;
2788
0
    const unsigned char *rval = NULL;
2789
0
    unsigned char *srval = NULL;
2790
0
    PACKET rcfgs_pkt;
2791
2792
    /*
2793
     * An HRR will have an ECH extension with the 8-octet confirmation value.
2794
     * Store it away for when we check it later
2795
     */
2796
0
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) {
2797
0
        if (PACKET_remaining(pkt) != OSSL_ECH_SIGNAL_LEN) {
2798
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2799
0
            return 0;
2800
0
        }
2801
0
        s->ext.ech.hrrsignal_p = (unsigned char *)PACKET_data(pkt);
2802
0
        memcpy(s->ext.ech.hrrsignal, s->ext.ech.hrrsignal_p,
2803
0
            OSSL_ECH_SIGNAL_LEN);
2804
0
        return 1;
2805
0
    }
2806
    /* otherwise we expect retry-configs */
2807
0
    if (!PACKET_get_length_prefixed_2(pkt, &rcfgs_pkt)) {
2808
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2809
0
        return 0;
2810
0
    }
2811
0
    rval = PACKET_data(&rcfgs_pkt);
2812
0
    rlen = (unsigned int)PACKET_remaining(&rcfgs_pkt);
2813
0
    OPENSSL_free(s->ext.ech.returned);
2814
0
    s->ext.ech.returned = NULL;
2815
0
    srval = OPENSSL_malloc(rlen + 2);
2816
0
    if (srval == NULL) {
2817
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2818
0
        return 0;
2819
0
    }
2820
0
    srval[0] = (rlen >> 8) & 0xff;
2821
0
    srval[1] = rlen & 0xff;
2822
0
    memcpy(srval + 2, rval, rlen);
2823
0
    s->ext.ech.returned = srval;
2824
0
    s->ext.ech.returned_len = rlen + 2;
2825
0
    return 1;
2826
0
}
2827
#endif /* END_OPENSSL_NO_ECH */