Coverage Report

Created: 2026-10-16 07:31

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