Coverage Report

Created: 2026-07-16 07:11

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