Coverage Report

Created: 2026-11-16 07:12

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
0
    unsigned long lifetime;
1055
1056
0
    if (s->ext.tick_age_checked)
1057
0
        return s->ext.tick_age_ok;
1058
0
    s->ext.tick_age_ok = 1;
1059
1060
    /*
1061
     * Technically the C standard just says time() returns a time_t and says
1062
     * nothing about the encoding of that type. In practice most
1063
     * implementations follow POSIX which holds it as an integral type in
1064
     * seconds since epoch. We've already made the assumption that we can do
1065
     * this in multiple places in the code, so portability shouldn't be an
1066
     * issue.
1067
     */
1068
0
    t = ossl_time_subtract(ossl_time_now(), s->session->time);
1069
0
    agesec = (uint32_t)ossl_time2seconds(t);
1070
1071
    /*
1072
     * We calculate the age in seconds but the server may work in ms. Due to
1073
     * rounding errors we could overestimate the age by up to 1s. It is
1074
     * better to underestimate it. Otherwise, if the RTT is very short, when
1075
     * the server calculates the age reported by the client it could be
1076
     * bigger than the age calculated on the server - which should never
1077
     * happen.
1078
     */
1079
0
    if (agesec > 0)
1080
0
        agesec--;
1081
1082
    /*
1083
     * Calculate age in ms. We're just doing it to nearest second. Should be
1084
     * good enough.
1085
     */
1086
0
    s->ext.tick_age_ms = agesec * (uint32_t)1000;
1087
1088
    /*
1089
     * RFC 9846 4.7.1: Servers MUST NOT use any value greater than 604800
1090
     * seconds (7 days). The value of zero indicates that the ticket should be
1091
     * discarded immediately. Clients MUST NOT use tickets for longer than 7
1092
     * days after issuance, regardless of the ticket_lifetime, and MAY delete
1093
     * tickets earlier based on local policy. A server MAY treat a ticket as
1094
     * valid for a shorter period of time than what is stated in the
1095
     * ticket_lifetime.
1096
     *
1097
     * The hint is capped when a NewSessionTicket is parsed, but a session
1098
     * restored with d2i_SSL_SESSION() carries whatever value was serialised.
1099
     */
1100
0
    lifetime = s->session->ext.tick_lifetime_hint;
1101
0
    if (lifetime > 604800)
1102
0
        lifetime = 604800;
1103
0
    if (lifetime == 0 || ossl_time_compare(t, ossl_seconds2time(lifetime)) > 0)
1104
0
        s->ext.tick_age_ok = 0;
1105
0
    else if (agesec != 0 && s->ext.tick_age_ms / (uint32_t)1000 != agesec)
1106
0
        s->ext.tick_age_ok = 0;
1107
1108
0
    s->ext.tick_age_checked = 1;
1109
0
    return s->ext.tick_age_ok;
1110
0
}
1111
1112
/*
1113
 * True if a TLS 1.3 ciphersuite carrying the same handshake digest as |cipher|
1114
 * is being offered on this handshake. |cipher| must itself be a TLS 1.3 cipher:
1115
 * the algorithm2 handshake-MAC bits read here mean something else in a TLS 1.2
1116
 * suite, so a non-TLS-1.3 cipher (e.g. from a bogus callback PSK) is never viable.
1117
 */
1118
static int tls13_digest_offered(SSL_CONNECTION *s, const SSL_CIPHER *cipher)
1119
0
{
1120
0
    STACK_OF(SSL_CIPHER) *ciphers;
1121
0
    int i, n, want;
1122
1123
0
    if (cipher == NULL || cipher->min_tls <= TLS1_2_VERSION)
1124
0
        return 0;
1125
0
    want = ssl_cipher_get_handshake_digest_nid(cipher);
1126
0
    if (want == NID_undef)
1127
0
        return 0;
1128
0
    ciphers = ssl_get_ciphers_by_id(s);
1129
0
    n = sk_SSL_CIPHER_num(ciphers);
1130
0
    for (i = 0; i < n; i++) {
1131
0
        const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
1132
1133
        /*
1134
         * SSL_SECOP_CIPHER_SUPPORTED matches the ssl_cipher_list_to_bytes()
1135
         * ciphersuite filter.
1136
         */
1137
0
        if (c->min_tls > TLS1_2_VERSION
1138
0
            && ssl_cipher_get_handshake_digest_nid(c) == want
1139
0
            && !ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
1140
0
            return 1;
1141
0
    }
1142
0
    return 0;
1143
0
}
1144
1145
/*
1146
 * Mirrors the ticket-resumption gating checks in tls_construct_ctos_psk() so
1147
 * that early_data is only advertised when the resumption PSK will actually
1148
 * be sent.
1149
 */
1150
static int tls13_check_resumption_psk(SSL_CONNECTION *s, const EVP_MD *handmd)
1151
0
{
1152
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1153
0
    const EVP_MD *mdres;
1154
0
    const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION : TLS1_3_VERSION;
1155
1156
0
    if (s->session == NULL
1157
0
        || s->session->ssl_version != version1_3
1158
0
        || s->session->ext.ticklen == 0
1159
0
        || s->session->cipher == NULL)
1160
0
        return 0;
1161
1162
0
    mdres = ssl_md(sctx, s->session->cipher->algorithm2);
1163
0
    if (mdres == NULL)
1164
0
        return 0;
1165
0
    if (s->hello_retry_request == SSL_HRR_PENDING && mdres != handmd)
1166
0
        return 0;
1167
    /* An offered TLS 1.3 ciphersuite must carry the ticket's digest. */
1168
0
    if (!tls13_digest_offered(s, s->session->cipher))
1169
0
        return 0;
1170
0
    if (tls13_check_tick_lifetime_hint(s) == 0)
1171
0
        return 0;
1172
1173
0
    return 1;
1174
0
}
1175
1176
/*
1177
 * 0-RTT early data is protected with the PSK's own cipher, and per RFC 9846
1178
 * section 4.3.10 the server accepts it only if it negotiates that exact
1179
 * cipher. So if we are not even offering that cipher on this handshake, 0-RTT
1180
 * cannot be accepted and early data must be suppressed -- the client-side
1181
 * mirror of the server's cipher-commitment check. The PSK is still offered for
1182
 * 1-RTT resumption, which needs only a digest-compatible cipher.
1183
 */
1184
static int tls13_early_cipher_offered(SSL_CONNECTION *s, const SSL_SESSION *sess)
1185
0
{
1186
0
    const SSL_CIPHER *c = sess->cipher;
1187
1188
    /*
1189
     * SSL_SECOP_CIPHER_SUPPORTED matches the ssl_cipher_list_to_bytes()
1190
     * ciphersuite filter.
1191
     */
1192
0
    return c != NULL
1193
0
        && !ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED)
1194
0
        && sk_SSL_CIPHER_find(ssl_get_ciphers_by_id(s), c) >= 0;
1195
0
}
1196
1197
/*
1198
 * 0-RTT is bound to the session's ALPN protocol, and the server accepts
1199
 * it only if it negotiates that protocol. So early data is viable only if that
1200
 * protocol is among the ones we offer -- we cannot know which the server will
1201
 * pick, so being present in our list is enough. A session with no ALPN imposes
1202
 * no constraint; a session with one while we offer none cannot match. ALPN
1203
 * affects 0-RTT only: resumption itself permits an ALPN change after the
1204
 * transition.
1205
 */
1206
static int tls13_early_alpn_offered(SSL_CONNECTION *s, const SSL_SESSION *sess)
1207
0
{
1208
0
    PACKET prots, alpnpkt;
1209
1210
0
    if (sess->ext.alpn_selected == NULL)
1211
0
        return 1;
1212
0
    if (s->ext.alpn == NULL
1213
0
        || !PACKET_buf_init(&prots, s->ext.alpn, s->ext.alpn_len))
1214
0
        return 0;
1215
0
    while (PACKET_get_length_prefixed_1(&prots, &alpnpkt)) {
1216
0
        if (PACKET_equal(&alpnpkt, sess->ext.alpn_selected,
1217
0
                sess->ext.alpn_selected_len))
1218
0
            return 1;
1219
0
    }
1220
0
    return 0;
1221
0
}
1222
1223
EXT_RETURN tls_construct_ctos_early_data(SSL_CONNECTION *s, WPACKET *pkt,
1224
    unsigned int context, X509 *x,
1225
    size_t chainidx)
1226
0
{
1227
0
#ifndef OPENSSL_NO_PSK
1228
0
    char identity[PSK_MAX_IDENTITY_LEN + 1];
1229
0
#endif /* OPENSSL_NO_PSK */
1230
0
    const unsigned char *id = NULL;
1231
0
    size_t idlen = 0;
1232
0
    SSL_SESSION *psksess = NULL;
1233
0
    SSL_SESSION *edsess = NULL;
1234
0
    const EVP_MD *handmd = NULL;
1235
0
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
1236
0
    const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION : TLS1_3_VERSION;
1237
1238
0
    s->ext.tick_age_checked = 0;
1239
1240
0
#ifndef OPENSSL_NO_ECH
1241
    /*
1242
     * If we're attempting ECH and processing the outer CH
1243
     * then we only need to check if the extension is to be
1244
     * sent or not - any other processing (with side effects)
1245
     * happened already for the inner CH.
1246
     */
1247
0
    if (s->ext.ech.es != NULL && s->ext.ech.ch_depth == 0) {
1248
        /*
1249
         * if we called this for inner and did send then
1250
         * the following two things should be set, if so,
1251
         * then send again in the outer CH.
1252
         */
1253
0
        if (s->ext.early_data == SSL_EARLY_DATA_REJECTED
1254
0
            && s->ext.early_data_ok == 1) {
1255
0
            if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
1256
0
                || !WPACKET_start_sub_packet_u16(pkt)
1257
0
                || !WPACKET_close(pkt)) {
1258
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1259
0
                return EXT_RETURN_FAIL;
1260
0
            }
1261
0
            return EXT_RETURN_SENT;
1262
0
        } else {
1263
0
            return EXT_RETURN_NOT_SENT;
1264
0
        }
1265
0
    }
1266
0
#endif
1267
1268
    /*
1269
     * Reset the early-secret-derived marker here, past the ECH outer-CH no-op
1270
     * above, so the outer pass preserves the value the inner pass's binder set
1271
     * (the outer CH derives no early secret of its own).
1272
     */
1273
0
    s->ext.early_secret_derived = 0;
1274
1275
0
    if (s->hello_retry_request == SSL_HRR_PENDING)
1276
0
        handmd = ssl_handshake_md(s);
1277
1278
0
    if (s->psk_use_session_cb != NULL
1279
0
        && (!s->psk_use_session_cb(ussl, handmd, &id, &idlen, &psksess)
1280
0
            || (psksess != NULL
1281
0
                && (psksess->ssl_version != version1_3
1282
0
                    || psksess->master_key_length == 0)))) {
1283
0
        SSL_SESSION_free(psksess);
1284
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
1285
0
        return EXT_RETURN_FAIL;
1286
0
    }
1287
1288
0
#ifndef OPENSSL_NO_PSK
1289
0
    if (psksess == NULL && s->psk_client_callback != NULL) {
1290
0
        unsigned char psk[PSK_MAX_PSK_LEN];
1291
0
        size_t psklen = 0;
1292
1293
0
        memset(identity, 0, sizeof(identity));
1294
0
        psklen = s->psk_client_callback(ussl, NULL,
1295
0
            identity, sizeof(identity) - 1,
1296
0
            psk, sizeof(psk));
1297
1298
0
        if (psklen > PSK_MAX_PSK_LEN) {
1299
0
            OPENSSL_cleanse(psk, sizeof(psk));
1300
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1301
0
            return EXT_RETURN_FAIL;
1302
0
        }
1303
0
        if (psklen > 0) {
1304
0
            const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
1305
0
            const SSL_CIPHER *cipher;
1306
1307
0
            idlen = strlen(identity);
1308
0
            if (idlen > PSK_MAX_IDENTITY_LEN) {
1309
0
                OPENSSL_cleanse(psk, sizeof(psk));
1310
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1311
0
                return EXT_RETURN_FAIL;
1312
0
            }
1313
0
            id = (unsigned char *)identity;
1314
1315
            /*
1316
             * We found a PSK using an old style callback. We don't know
1317
             * the digest so we default to SHA256 as per the (D)TLSv1.3 spec
1318
             */
1319
0
            cipher = SSL_CIPHER_find(SSL_CONNECTION_GET_SSL(s),
1320
0
                tls13_aes128gcmsha256_id);
1321
0
            if (cipher == NULL) {
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
1327
0
            psksess = SSL_SESSION_new();
1328
1329
0
            if (psksess == NULL
1330
0
                || !SSL_SESSION_set1_master_key(psksess, psk, psklen)
1331
0
                || !SSL_SESSION_set_cipher(psksess, cipher)
1332
0
                || !SSL_SESSION_set_protocol_version(psksess, version1_3)) {
1333
0
                SSL_SESSION_free(psksess);
1334
0
                OPENSSL_cleanse(psk, sizeof(psk));
1335
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1336
0
                return EXT_RETURN_FAIL;
1337
0
            }
1338
0
            OPENSSL_cleanse(psk, psklen);
1339
0
        }
1340
0
    }
1341
0
#endif /* OPENSSL_NO_PSK */
1342
1343
    /*
1344
     * If no offered ciphersuite carries the callback PSK's digest -- or its
1345
     * cipher isn't a TLS 1.3 cipher -- it can never be used, so drop it here and
1346
     * proceed as if the callback had returned no PSK. Every later s->psksession
1347
     * check then handles it for free.
1348
     */
1349
0
    if (psksess != NULL && !tls13_digest_offered(s, psksess->cipher)) {
1350
0
        SSL_SESSION_free(psksess);
1351
0
        psksess = NULL;
1352
0
    }
1353
1354
0
    SSL_SESSION_free(s->psksession);
1355
0
    s->psksession = psksess;
1356
0
    if (psksess != NULL) {
1357
0
        OPENSSL_free(s->psksession_id);
1358
0
        s->psksession_id = OPENSSL_memdup(id, idlen);
1359
0
        if (s->psksession_id == NULL) {
1360
0
            s->psksession_id_len = 0;
1361
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1362
0
            return EXT_RETURN_FAIL;
1363
0
        }
1364
0
        s->psksession_id_len = idlen;
1365
0
    }
1366
1367
    /*
1368
     * Suppress early_data unless a PSK is available and will be sent.
1369
     *
1370
     * RFC 9846 4.3.10: When a PSK is used and early data is allowed for that
1371
     * PSK, the client can send Application Data in its first flight of
1372
     * messages. If the client opts to do so, it MUST supply both the
1373
     * "pre_shared_key" and "early_data" extensions.
1374
     *
1375
     * The PSK used to encrypt the early data MUST be the first PSK listed in
1376
     * the client's "pre_shared_key" extension.
1377
     */
1378
    /*
1379
     * The first PSK identity we offer is the only one that can key 0-RTT: the
1380
     * resumption session when we are offering it, else the external psksession.
1381
     * Offer early_data only when that first PSK is itself 0-RTT-capable; never
1382
     * key it off a PSK offered later.
1383
     */
1384
    /*
1385
     * Freeze the resumption-ticket offer decision here, at the first consumer,
1386
     * so tls_construct_ctos_psk() offers the very same identity 0 and its binder
1387
     * derives s->early_secret for whatever session early_data is keyed on. See
1388
     * s->ext.psk_resumption_offered.
1389
     */
1390
0
    s->ext.psk_resumption_offered = tls13_check_resumption_psk(s, handmd);
1391
0
    edsess = s->ext.psk_resumption_offered ? s->session : psksess;
1392
0
    if (s->early_data_state != SSL_EARLY_DATA_CONNECTING
1393
0
        || edsess == NULL
1394
0
        || edsess->ext.max_early_data == 0
1395
0
        || !tls13_early_cipher_offered(s, edsess)
1396
0
        || !tls13_early_alpn_offered(s, edsess)) {
1397
0
        s->max_early_data = 0;
1398
0
        if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) {
1399
0
            s->ext.early_data_suppressed = 1;
1400
            /*
1401
             * We report REJECTED rather than NOT_SENT, so that suppressing
1402
             * 0-RTT locally looks to the application just like a server that
1403
             * declined it.
1404
             *
1405
             * The early exporter is a separate question: no early secret is
1406
             * derived on this path, so s->ext.early_exporter_ready stays clear
1407
             * and SSL_export_keying_material_early() remains unavailable.
1408
             */
1409
0
            s->ext.early_data = SSL_EARLY_DATA_REJECTED;
1410
0
        }
1411
0
        s->early_data_state = SSL_EARLY_DATA_NONE;
1412
0
        return EXT_RETURN_NOT_SENT;
1413
0
    }
1414
0
    s->max_early_data = edsess->ext.max_early_data;
1415
    /*
1416
     * Record the first-offered PSK so the binder, the early-key derivation, the
1417
     * early exporter, the byte-budget lookup and the post-ServerHello fixup all
1418
     * key off it rather than guessing the source from
1419
     * s->session->ext.max_early_data. Held (up-ref'd) so it stays valid across
1420
     * the swap that later folds a selected psksession into s->session.
1421
     */
1422
0
    SSL_SESSION_free(s->ext.early_data_session);
1423
0
    s->ext.early_data_session = edsess;
1424
0
    if (!SSL_SESSION_up_ref(edsess)) {
1425
0
        s->ext.early_data_session = NULL;
1426
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1427
0
        return EXT_RETURN_FAIL;
1428
0
    }
1429
1430
0
    if (edsess->ext.hostname != NULL) {
1431
0
        if (s->ext.hostname == NULL
1432
0
            || (s->ext.hostname != NULL
1433
0
                && strcmp(s->ext.hostname, edsess->ext.hostname) != 0)) {
1434
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1435
0
                SSL_R_INCONSISTENT_EARLY_DATA_SNI);
1436
0
            return EXT_RETURN_FAIL;
1437
0
        }
1438
0
    }
1439
1440
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
1441
0
        || !WPACKET_start_sub_packet_u16(pkt)
1442
0
        || !WPACKET_close(pkt)) {
1443
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1444
0
        return EXT_RETURN_FAIL;
1445
0
    }
1446
1447
    /*
1448
     * We set this to rejected here. Later, if the server acknowledges the
1449
     * extension, we set it to accepted.
1450
     */
1451
0
    s->ext.early_data = SSL_EARLY_DATA_REJECTED;
1452
0
    s->ext.early_data_ok = 1;
1453
1454
0
    return EXT_RETURN_SENT;
1455
0
}
1456
1457
/*
1458
 * Construct the pre_shared_key extension
1459
 */
1460
EXT_RETURN tls_construct_ctos_psk(SSL_CONNECTION *s, WPACKET *pkt,
1461
    unsigned int context,
1462
    X509 *x, size_t chainidx)
1463
0
{
1464
0
#if !(defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_DTLS1_3))
1465
0
    uint32_t agems = 0;
1466
0
    size_t binderoffset, msglen, resbinder_end_offset = 0, pskbinder_end_offset = 0;
1467
0
    int reshashsize = 0, pskhashsize = 0;
1468
0
    unsigned char *resbinder = NULL, *pskbinder = NULL, *msgstart = NULL;
1469
0
    const EVP_MD *handmd = NULL, *mdres = NULL, *mdpsk = NULL;
1470
0
    int dores = 0;
1471
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1472
0
    const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION : TLS1_3_VERSION;
1473
1474
0
    s->ext.tick_identity = 0;
1475
1476
    /*
1477
     * If this is an incompatible or new session then we have nothing to resume
1478
     * so don't add this extension.
1479
     */
1480
0
    if (s->session->ssl_version != version1_3
1481
0
        || (s->session->ext.ticklen == 0 && s->psksession == NULL))
1482
0
        return EXT_RETURN_NOT_SENT;
1483
1484
    /*
1485
     * After a HelloRetryRequest, do not introduce a pre_shared_key extension
1486
     * that the first ClientHello (CH1) did not carry. RFC 9846 4.2.2 lets CH2
1487
     * update or drop PSKs, but not add one -- let alone add the whole
1488
     * extension. This could otherwise happen if a psk_use_session callback
1489
     * only yields a digest-compatible PSK once the retry has settled the
1490
     * ciphersuite.  While a misbehaving callback could still introduce a
1491
     * novel PSK only after HRR, that's an application bug, expected rare and
1492
     * likely harmless if it occurs.
1493
     */
1494
0
    if (s->hello_retry_request == SSL_HRR_PENDING
1495
0
        && (s->ext.extflags[TLSEXT_IDX_psk] & SSL_EXT_FLAG_SENT) == 0)
1496
0
        return EXT_RETURN_NOT_SENT;
1497
1498
0
    if (s->hello_retry_request == SSL_HRR_PENDING)
1499
0
        handmd = ssl_handshake_md(s);
1500
1501
0
    if (s->session->ext.ticklen != 0) {
1502
        /* Get the digest associated with the ciphersuite in the session */
1503
0
        if (s->session->cipher == NULL) {
1504
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1505
0
            return EXT_RETURN_FAIL;
1506
0
        }
1507
0
        mdres = ssl_md(sctx, s->session->cipher->algorithm2);
1508
0
        if (mdres == NULL) {
1509
            /*
1510
             * Don't recognize this cipher so we can't use the session.
1511
             * Ignore it
1512
             */
1513
0
            goto dopsksess;
1514
0
        }
1515
1516
0
        if (s->hello_retry_request == SSL_HRR_PENDING && mdres != handmd) {
1517
            /*
1518
             * Selected ciphersuite hash does not match the hash for the session
1519
             * so we can't use it.
1520
             */
1521
0
            goto dopsksess;
1522
0
        }
1523
1524
0
#ifndef OPENSSL_NO_ECH
1525
        /*
1526
         * When doing ECH, we get here twice (for inner then outer). The
1527
         * 2nd time (for outer) we can skip some checks as we know how
1528
         * those went last time.
1529
         */
1530
0
        if (s->ext.ech.es != NULL && s->ext.ech.ch_depth == 0) {
1531
0
            s->ext.tick_identity = s->ext.ech.tick_identity;
1532
0
            dores = (s->ext.tick_identity > 0);
1533
0
            goto dopsksess;
1534
0
        }
1535
0
#endif
1536
1537
        /*
1538
         * Consume the offer decision frozen by tls_construct_ctos_early_data()
1539
         * rather than re-running the SECOP-dependent tls13_digest_offered() a
1540
         * second time: an answer that flipped between the two calls would drop
1541
         * the ticket after early_data was already committed, leaving
1542
         * s->early_secret underived and 0-RTT keyed off a zero secret.
1543
         */
1544
0
        if (!s->ext.psk_resumption_offered)
1545
0
            goto dopsksess;
1546
0
        if (tls13_check_tick_lifetime_hint(s) == 0)
1547
0
            goto dopsksess;
1548
        /* tls13_check_tick_lifetime_hint() updates the tick_age_ms value. */
1549
0
        agems = s->ext.tick_age_ms;
1550
1551
        /*
1552
         * Obfuscate the age. Overflow here is fine, this addition is supposed
1553
         * to be mod 2^32.
1554
         */
1555
0
        agems += s->session->ext.tick_age_add;
1556
1557
0
        reshashsize = EVP_MD_get_size(mdres);
1558
0
        if (reshashsize <= 0)
1559
0
            goto dopsksess;
1560
0
        s->ext.tick_identity++;
1561
0
#ifndef OPENSSL_NO_ECH
1562
        /* stash this for re-use in outer CH */
1563
0
        if (s->ext.ech.es != NULL && s->ext.ech.ch_depth == 1)
1564
0
            s->ext.ech.tick_identity = s->ext.tick_identity;
1565
0
#endif
1566
0
        dores = 1;
1567
0
    }
1568
1569
0
dopsksess:
1570
0
    if (!dores && s->psksession == NULL)
1571
0
        return EXT_RETURN_NOT_SENT;
1572
1573
0
    if (s->psksession != NULL) {
1574
0
        mdpsk = ssl_md(sctx, s->psksession->cipher->algorithm2);
1575
0
        if (mdpsk == NULL) {
1576
            /*
1577
             * Don't recognize this cipher so we can't use the session.
1578
             * If this happens it's an application bug.
1579
             */
1580
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
1581
0
            return EXT_RETURN_FAIL;
1582
0
        }
1583
1584
0
        if (s->hello_retry_request == SSL_HRR_PENDING && mdpsk != handmd) {
1585
            /*
1586
             * Selected ciphersuite hash does not match the hash for the PSK
1587
             * session. This is an application bug.
1588
             */
1589
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
1590
0
            return EXT_RETURN_FAIL;
1591
0
        }
1592
1593
0
        pskhashsize = EVP_MD_get_size(mdpsk);
1594
0
        if (pskhashsize <= 0) {
1595
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
1596
0
            return EXT_RETURN_FAIL;
1597
0
        }
1598
0
    }
1599
1600
    /* Create the extension, but skip over the binder for now */
1601
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
1602
0
        || !WPACKET_start_sub_packet_u16(pkt)
1603
0
        || !WPACKET_start_sub_packet_u16(pkt)) {
1604
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1605
0
        return EXT_RETURN_FAIL;
1606
0
    }
1607
1608
0
#ifndef OPENSSL_NO_ECH
1609
    /*
1610
     * For ECH if we're processing the outer CH and the inner CH
1611
     * has a PSK, then we want to send a GREASE PSK in the outer.
1612
     * We'll do that by just replacing the ticket value itself
1613
     * with random values of the same length.
1614
     */
1615
0
    if (s->ext.ech.es != NULL && s->ext.ech.ch_depth == 0) {
1616
0
        unsigned char *rndbuf = NULL, *rndbufp = NULL;
1617
0
        size_t totalrndsize = 0;
1618
1619
0
        totalrndsize = s->session->ext.ticklen
1620
0
            + sizeof(agems)
1621
0
            + s->psksession_id_len
1622
0
            + reshashsize
1623
0
            + pskhashsize;
1624
0
        rndbuf = OPENSSL_malloc(totalrndsize);
1625
0
        if (rndbuf == NULL) {
1626
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1627
0
            return EXT_RETURN_FAIL;
1628
0
        }
1629
        /* for outer CH allocate a similar sized random value */
1630
0
        if (RAND_bytes_ex(sctx->libctx, rndbuf, totalrndsize, 0) <= 0) {
1631
0
            OPENSSL_free(rndbuf);
1632
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1633
0
            return EXT_RETURN_FAIL;
1634
0
        }
1635
        /* set agems from random buffer */
1636
0
        rndbufp = rndbuf;
1637
0
        memcpy(&agems, rndbufp, sizeof(agems));
1638
0
        rndbufp += sizeof(agems);
1639
0
        if (dores != 0) {
1640
0
            if (!WPACKET_sub_memcpy_u16(pkt, rndbufp,
1641
0
                    s->session->ext.ticklen)
1642
0
                || !WPACKET_put_bytes_u32(pkt, agems)) {
1643
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1644
0
                OPENSSL_free(rndbuf);
1645
0
                return EXT_RETURN_FAIL;
1646
0
            }
1647
0
            rndbufp += s->session->ext.ticklen;
1648
0
        }
1649
0
        if (s->psksession != NULL) {
1650
0
            if (!WPACKET_sub_memcpy_u16(pkt, rndbufp, s->psksession_id_len)
1651
0
                || !WPACKET_put_bytes_u32(pkt, 0)) {
1652
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1653
0
                OPENSSL_free(rndbuf);
1654
0
                return EXT_RETURN_FAIL;
1655
0
            }
1656
0
            rndbufp += s->psksession_id_len;
1657
0
        }
1658
0
        if (!WPACKET_close(pkt)
1659
0
            || !WPACKET_start_sub_packet_u16(pkt)
1660
0
            || (dores == 1
1661
0
                && !WPACKET_sub_memcpy_u8(pkt, rndbufp, reshashsize))
1662
0
            || (s->psksession != NULL
1663
0
                && !WPACKET_sub_memcpy_u8(pkt, rndbufp, pskhashsize))
1664
0
            || !WPACKET_close(pkt)
1665
0
            || !WPACKET_close(pkt)) {
1666
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1667
0
            OPENSSL_free(rndbuf);
1668
0
            return EXT_RETURN_FAIL;
1669
0
        }
1670
0
        OPENSSL_free(rndbuf);
1671
0
        return EXT_RETURN_SENT;
1672
0
    }
1673
0
#endif /* OPENSSL_NO_ECH */
1674
0
    if (dores) {
1675
0
        if (!WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick,
1676
0
                s->session->ext.ticklen)
1677
0
            || !WPACKET_put_bytes_u32(pkt, agems)) {
1678
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1679
0
            return EXT_RETURN_FAIL;
1680
0
        }
1681
0
    }
1682
1683
0
    if (s->psksession != NULL) {
1684
0
        if (!WPACKET_sub_memcpy_u16(pkt, s->psksession_id,
1685
0
                s->psksession_id_len)
1686
0
            || !WPACKET_put_bytes_u32(pkt, 0)) {
1687
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1688
0
            return EXT_RETURN_FAIL;
1689
0
        }
1690
0
        s->ext.tick_identity++;
1691
0
    }
1692
1693
0
    if (!WPACKET_close(pkt)
1694
0
        || !WPACKET_get_total_written(pkt, &binderoffset)
1695
0
        || !WPACKET_start_sub_packet_u16(pkt)
1696
0
        || (dores
1697
0
            && (!WPACKET_sub_allocate_bytes_u8(pkt, reshashsize, NULL)
1698
0
                || !WPACKET_get_total_written(pkt, &resbinder_end_offset)))
1699
0
        || (s->psksession != NULL
1700
0
            && (!WPACKET_sub_allocate_bytes_u8(pkt, pskhashsize, NULL)
1701
0
                || !WPACKET_get_total_written(pkt, &pskbinder_end_offset)))
1702
0
        || !WPACKET_close(pkt)
1703
0
        || !WPACKET_close(pkt)
1704
0
        || !WPACKET_get_total_written(pkt, &msglen)
1705
        /*
1706
         * We need to fill in all the sub-packet lengths now so we can
1707
         * calculate the HMAC of the message up to the binders
1708
         */
1709
0
        || !WPACKET_fill_lengths(pkt)) {
1710
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1711
0
        return EXT_RETURN_FAIL;
1712
0
    }
1713
1714
0
    msgstart = WPACKET_get_curr(pkt) - msglen;
1715
    /* Allocating either binder may relocate the packet's backing buffer. */
1716
0
    if (dores)
1717
0
        resbinder = msgstart + resbinder_end_offset - reshashsize;
1718
0
    if (s->psksession != NULL)
1719
0
        pskbinder = msgstart + pskbinder_end_offset - pskhashsize;
1720
1721
0
    if (dores && tls_psk_do_binder(s, mdres, msgstart, binderoffset, NULL, resbinder, s->session, 1, 0) != 1) {
1722
        /* SSLfatal() already called */
1723
0
        return EXT_RETURN_FAIL;
1724
0
    }
1725
1726
0
    if (s->psksession != NULL
1727
0
        && tls_psk_do_binder(s, mdpsk, msgstart, binderoffset, NULL,
1728
0
               pskbinder, s->psksession, 1, 1)
1729
0
            != 1) {
1730
        /* SSLfatal() already called */
1731
0
        return EXT_RETURN_FAIL;
1732
0
    }
1733
1734
0
    return EXT_RETURN_SENT;
1735
#else
1736
    return EXT_RETURN_NOT_SENT;
1737
#endif
1738
0
}
1739
1740
EXT_RETURN tls_construct_ctos_post_handshake_auth(SSL_CONNECTION *s, WPACKET *pkt,
1741
    ossl_unused unsigned int context,
1742
    ossl_unused X509 *x,
1743
    ossl_unused size_t chainidx)
1744
0
{
1745
0
#if !(defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_DTLS1_3))
1746
0
    if (!s->pha_enabled)
1747
0
        return EXT_RETURN_NOT_SENT;
1748
0
#ifndef OPENSSL_NO_ECH
1749
0
    ECH_SAME_EXT(s, context, pkt)
1750
0
#endif
1751
1752
    /* construct extension - 0 length, no contents */
1753
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_post_handshake_auth)
1754
0
        || !WPACKET_start_sub_packet_u16(pkt)
1755
0
        || !WPACKET_close(pkt)) {
1756
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1757
0
        return EXT_RETURN_FAIL;
1758
0
    }
1759
1760
0
    s->post_handshake_auth = SSL_PHA_EXT_SENT;
1761
1762
0
    return EXT_RETURN_SENT;
1763
#else
1764
    return EXT_RETURN_NOT_SENT;
1765
#endif
1766
0
}
1767
1768
/*
1769
 * Parse the server's renegotiation binding and abort if it's not right
1770
 */
1771
int tls_parse_stoc_renegotiate(SSL_CONNECTION *s, PACKET *pkt,
1772
    unsigned int context,
1773
    X509 *x, size_t chainidx)
1774
0
{
1775
0
    size_t expected_len = s->s3.previous_client_finished_len
1776
0
        + s->s3.previous_server_finished_len;
1777
0
    size_t ilen;
1778
0
    const unsigned char *data;
1779
1780
    /* Check for logic errors */
1781
0
    if (!ossl_assert(expected_len == 0
1782
0
            || s->s3.previous_client_finished_len != 0)
1783
0
        || !ossl_assert(expected_len == 0
1784
0
            || s->s3.previous_server_finished_len != 0)) {
1785
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1786
0
        return 0;
1787
0
    }
1788
1789
    /* Parse the length byte */
1790
0
    if (!PACKET_get_1_len(pkt, &ilen)) {
1791
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);
1792
0
        return 0;
1793
0
    }
1794
1795
    /* Consistency check */
1796
0
    if (PACKET_remaining(pkt) != ilen) {
1797
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);
1798
0
        return 0;
1799
0
    }
1800
1801
    /* Check that the extension matches */
1802
0
    if (ilen != expected_len) {
1803
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);
1804
0
        return 0;
1805
0
    }
1806
1807
0
    if (!PACKET_get_bytes(pkt, &data, s->s3.previous_client_finished_len)
1808
0
        || memcmp(data, s->s3.previous_client_finished,
1809
0
               s->s3.previous_client_finished_len)
1810
0
            != 0) {
1811
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);
1812
0
        return 0;
1813
0
    }
1814
1815
0
    if (!PACKET_get_bytes(pkt, &data, s->s3.previous_server_finished_len)
1816
0
        || memcmp(data, s->s3.previous_server_finished,
1817
0
               s->s3.previous_server_finished_len)
1818
0
            != 0) {
1819
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);
1820
0
        return 0;
1821
0
    }
1822
0
    s->s3.send_connection_binding = 1;
1823
1824
0
    return 1;
1825
0
}
1826
1827
/* Parse the server's max fragment len extension packet */
1828
int tls_parse_stoc_maxfragmentlen(SSL_CONNECTION *s, PACKET *pkt,
1829
    unsigned int context,
1830
    X509 *x, size_t chainidx)
1831
0
{
1832
0
    unsigned int value;
1833
1834
0
    if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
1835
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1836
0
        return 0;
1837
0
    }
1838
1839
    /* |value| should contains a valid max-fragment-length code. */
1840
0
    if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
1841
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1842
0
            SSL_R_TLS_EXT_INVALID_MAX_FRAGMENT_LENGTH);
1843
0
        return 0;
1844
0
    }
1845
1846
    /* Must be the same value as client-configured one who was sent to server */
1847
    /*-
1848
     * RFC 6066: if a client receives a maximum fragment length negotiation
1849
     * response that differs from the length it requested, ...
1850
     * It must abort with SSL_AD_ILLEGAL_PARAMETER alert
1851
     */
1852
0
    if (value != s->ext.max_fragment_len_mode) {
1853
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1854
0
            SSL_R_TLS_EXT_INVALID_MAX_FRAGMENT_LENGTH);
1855
0
        return 0;
1856
0
    }
1857
1858
    /*
1859
     * Maximum Fragment Length Negotiation succeeded.
1860
     * The negotiated Maximum Fragment Length is binding now.
1861
     */
1862
0
    s->session->ext.max_fragment_len_mode = value;
1863
1864
0
    return 1;
1865
0
}
1866
1867
int tls_parse_stoc_server_name(SSL_CONNECTION *s, PACKET *pkt,
1868
    unsigned int context,
1869
    X509 *x, size_t chainidx)
1870
0
{
1871
0
    char *eff_sni = s->ext.hostname;
1872
1873
0
#ifndef OPENSSL_NO_ECH
1874
    /* if we tried ECH and failed, the outer is what's expected */
1875
0
    if (s->ext.ech.es != NULL && s->ext.ech.success == 0)
1876
0
        eff_sni = s->ext.ech.outer_hostname;
1877
0
#endif
1878
0
    if (eff_sni == NULL) {
1879
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1880
0
        return 0;
1881
0
    }
1882
0
    if (PACKET_remaining(pkt) > 0) {
1883
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1884
0
        return 0;
1885
0
    }
1886
0
    if (!s->hit) {
1887
0
        if (s->session->ext.hostname != NULL) {
1888
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1889
0
            return 0;
1890
0
        }
1891
0
        s->session->ext.hostname = OPENSSL_strdup(eff_sni);
1892
0
        if (s->session->ext.hostname == NULL) {
1893
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1894
0
            return 0;
1895
0
        }
1896
0
    }
1897
0
    return 1;
1898
0
}
1899
1900
int tls_parse_stoc_session_ticket(SSL_CONNECTION *s, PACKET *pkt,
1901
    unsigned int context,
1902
    X509 *x, size_t chainidx)
1903
0
{
1904
0
    SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);
1905
1906
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)) {
1907
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION);
1908
0
        return 0;
1909
0
    }
1910
1911
0
    if (!tls_use_ticket(s)) {
1912
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1913
0
        return 0;
1914
0
    }
1915
0
    if (PACKET_remaining(pkt) > 0) {
1916
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1917
0
        return 0;
1918
0
    }
1919
1920
0
    s->ext.ticket_expected = 1;
1921
1922
0
    return 1;
1923
0
}
1924
1925
#ifndef OPENSSL_NO_OCSP
1926
int tls_parse_stoc_status_request(SSL_CONNECTION *s, PACKET *pkt,
1927
    unsigned int context,
1928
    X509 *x, size_t chainidx)
1929
0
{
1930
0
    if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
1931
        /* We ignore this if the server sends a CertificateRequest */
1932
0
        return 1;
1933
0
    }
1934
1935
    /*
1936
     * MUST only be sent if we've requested a status
1937
     * request message. In (D)TLS <= 1.2 it must also be empty.
1938
     */
1939
0
    if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
1940
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1941
0
        return 0;
1942
0
    }
1943
0
    if (!SSL_CONNECTION_IS_VERSION13(s) && PACKET_remaining(pkt) > 0) {
1944
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1945
0
        return 0;
1946
0
    }
1947
1948
0
    if (SSL_CONNECTION_IS_VERSION13(s)) {
1949
        /* SSLfatal() already called */
1950
0
        return tls_process_cert_status_body(s, chainidx, pkt);
1951
0
    }
1952
1953
    /* Set flag to expect CertificateStatus message */
1954
0
    s->ext.status_expected = 1;
1955
1956
0
    return 1;
1957
0
}
1958
#endif
1959
1960
#ifndef OPENSSL_NO_CT
1961
int tls_parse_stoc_sct(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1962
    X509 *x, size_t chainidx)
1963
0
{
1964
0
    if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
1965
        /* We ignore this if the server sends it in a CertificateRequest */
1966
0
        return 1;
1967
0
    }
1968
1969
    /*
1970
     * Only take it if we asked for it - i.e if there is no CT validation
1971
     * callback set, then a custom extension MAY be processing it, so we
1972
     * need to let control continue to flow to that.
1973
     */
1974
0
    if (s->ct_validation_callback != NULL) {
1975
0
        size_t size = PACKET_remaining(pkt);
1976
1977
        /* Simply copy it off for later processing */
1978
0
        OPENSSL_free(s->ext.scts);
1979
0
        s->ext.scts = NULL;
1980
1981
0
        s->ext.scts_len = (uint16_t)size;
1982
0
        if (size > 0) {
1983
0
            s->ext.scts = OPENSSL_malloc(size);
1984
0
            if (s->ext.scts == NULL) {
1985
0
                s->ext.scts_len = 0;
1986
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
1987
0
                return 0;
1988
0
            }
1989
0
            if (!PACKET_copy_bytes(pkt, s->ext.scts, size)) {
1990
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1991
0
                return 0;
1992
0
            }
1993
0
        }
1994
0
    } else {
1995
0
        ENDPOINT role = (context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
1996
0
            ? ENDPOINT_CLIENT
1997
0
            : ENDPOINT_BOTH;
1998
1999
        /*
2000
         * If we didn't ask for it then there must be a custom extension,
2001
         * otherwise this is unsolicited.
2002
         */
2003
0
        if (custom_ext_find(&s->cert->custext, role,
2004
0
                TLSEXT_TYPE_signed_certificate_timestamp,
2005
0
                NULL)
2006
0
            == NULL) {
2007
0
            SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
2008
0
            return 0;
2009
0
        }
2010
2011
0
        if (!custom_ext_parse(s, context,
2012
0
                TLSEXT_TYPE_signed_certificate_timestamp,
2013
0
                PACKET_data(pkt), PACKET_remaining(pkt),
2014
0
                x, chainidx)) {
2015
            /* SSLfatal already called */
2016
0
            return 0;
2017
0
        }
2018
0
    }
2019
2020
0
    return 1;
2021
0
}
2022
#endif
2023
2024
#ifndef OPENSSL_NO_NEXTPROTONEG
2025
/*
2026
 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
2027
 * elements of zero length are allowed and the set of elements must exactly
2028
 * fill the length of the block. Returns 1 on success or 0 on failure.
2029
 */
2030
static int ssl_next_proto_validate(SSL_CONNECTION *s, PACKET *pkt)
2031
0
{
2032
0
    PACKET tmp_protocol;
2033
2034
0
    while (PACKET_remaining(pkt)) {
2035
0
        if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
2036
0
            || PACKET_remaining(&tmp_protocol) == 0) {
2037
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2038
0
            return 0;
2039
0
        }
2040
0
    }
2041
2042
0
    return 1;
2043
0
}
2044
2045
int tls_parse_stoc_npn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
2046
    X509 *x, size_t chainidx)
2047
0
{
2048
0
    unsigned char *selected;
2049
0
    unsigned char selected_len;
2050
0
    PACKET tmppkt;
2051
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2052
2053
    /* Check if we are in a renegotiation. If so ignore this extension */
2054
0
    if (!SSL_IS_FIRST_HANDSHAKE(s))
2055
0
        return 1;
2056
2057
    /* We must have requested it. */
2058
0
    if (sctx->ext.npn_select_cb == NULL) {
2059
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
2060
0
        return 0;
2061
0
    }
2062
2063
    /* The data must be valid */
2064
0
    tmppkt = *pkt;
2065
0
    if (!ssl_next_proto_validate(s, &tmppkt)) {
2066
        /* SSLfatal() already called */
2067
0
        return 0;
2068
0
    }
2069
0
    if (sctx->ext.npn_select_cb(SSL_CONNECTION_GET_USER_SSL(s),
2070
0
            &selected, &selected_len,
2071
0
            PACKET_data(pkt), (unsigned int)PACKET_remaining(pkt),
2072
0
            sctx->ext.npn_select_cb_arg)
2073
0
            != SSL_TLSEXT_ERR_OK
2074
0
        || selected_len == 0) {
2075
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION);
2076
0
        return 0;
2077
0
    }
2078
2079
    /*
2080
     * Could be non-NULL if server has sent multiple NPN extensions in
2081
     * a single Serverhello
2082
     */
2083
0
    OPENSSL_free(s->ext.npn);
2084
0
    s->ext.npn = OPENSSL_malloc(selected_len);
2085
0
    if (s->ext.npn == NULL) {
2086
0
        s->ext.npn_len = 0;
2087
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2088
0
        return 0;
2089
0
    }
2090
2091
0
    memcpy(s->ext.npn, selected, selected_len);
2092
0
    s->ext.npn_len = selected_len;
2093
0
    s->s3.npn_seen = 1;
2094
2095
0
    return 1;
2096
0
}
2097
#endif
2098
2099
int tls_parse_stoc_alpn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
2100
    X509 *x, size_t chainidx)
2101
0
{
2102
0
    size_t len;
2103
0
    PACKET confpkt, protpkt;
2104
0
    int valid = 0;
2105
0
    size_t eff_alpn_len = 0;
2106
0
    unsigned char *eff_alpn = NULL;
2107
2108
    /* We must have requested it. */
2109
0
    if (!s->s3.alpn_sent) {
2110
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
2111
0
        return 0;
2112
0
    }
2113
    /*-
2114
     * The extension data consists of:
2115
     *   uint16 list_length
2116
     *   uint8 proto_length;
2117
     *   uint8 proto[proto_length];
2118
     */
2119
0
    if (!PACKET_get_net_2_len(pkt, &len)
2120
0
        || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len)
2121
0
        || PACKET_remaining(pkt) != len) {
2122
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2123
0
        return 0;
2124
0
    }
2125
2126
0
    eff_alpn = s->ext.alpn;
2127
0
    eff_alpn_len = s->ext.alpn_len;
2128
0
#ifndef OPENSSL_NO_ECH
2129
    /*
2130
     * If we tried ECH and it failed, then we need to compare vs. the
2131
     * outer alpn, if one was set.
2132
     */
2133
0
    if (s->ext.ech.es != NULL && s->ext.ech.success == 0
2134
0
        && s->ext.ech.alpn_outer != NULL) {
2135
0
        eff_alpn = s->ext.ech.alpn_outer;
2136
0
        eff_alpn_len = s->ext.ech.alpn_outer_len;
2137
0
    }
2138
0
#endif
2139
    /* It must be a protocol that we sent */
2140
0
    if (!PACKET_buf_init(&confpkt, eff_alpn, eff_alpn_len)) {
2141
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2142
0
        return 0;
2143
0
    }
2144
0
    while (PACKET_get_length_prefixed_1(&confpkt, &protpkt)) {
2145
0
        if (PACKET_remaining(&protpkt) != len)
2146
0
            continue;
2147
0
        if (memcmp(PACKET_data(pkt), PACKET_data(&protpkt), len) == 0) {
2148
            /* Valid protocol found */
2149
0
            valid = 1;
2150
0
            break;
2151
0
        }
2152
0
    }
2153
2154
0
    if (!valid) {
2155
        /* The protocol sent from the server does not match one we advertised */
2156
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2157
0
        return 0;
2158
0
    }
2159
2160
0
    OPENSSL_free(s->s3.alpn_selected);
2161
0
    s->s3.alpn_selected = OPENSSL_malloc(len);
2162
0
    if (s->s3.alpn_selected == NULL) {
2163
0
        s->s3.alpn_selected_len = 0;
2164
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2165
0
        return 0;
2166
0
    }
2167
0
    if (!PACKET_copy_bytes(pkt, s->s3.alpn_selected, len)) {
2168
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2169
0
        return 0;
2170
0
    }
2171
0
    s->s3.alpn_selected_len = len;
2172
2173
0
    if (s->session->ext.alpn_selected == NULL
2174
0
        || s->session->ext.alpn_selected_len != len
2175
0
        || memcmp(s->session->ext.alpn_selected, s->s3.alpn_selected, len)
2176
0
            != 0) {
2177
        /* ALPN not consistent with the old session so cannot use early_data */
2178
0
        s->ext.early_data_ok = 0;
2179
0
    }
2180
0
    if (!s->hit) {
2181
        /*
2182
         * This is a new session and so alpn_selected should have been
2183
         * initialised to NULL. We should update it with the selected ALPN.
2184
         */
2185
0
        if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
2186
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2187
0
            return 0;
2188
0
        }
2189
0
        s->session->ext.alpn_selected = OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len);
2190
0
        if (s->session->ext.alpn_selected == NULL) {
2191
0
            s->session->ext.alpn_selected_len = 0;
2192
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2193
0
            return 0;
2194
0
        }
2195
0
        s->session->ext.alpn_selected_len = s->s3.alpn_selected_len;
2196
0
    }
2197
2198
0
    return 1;
2199
0
}
2200
2201
#ifndef OPENSSL_NO_SRTP
2202
int tls_parse_stoc_use_srtp(SSL_CONNECTION *s, PACKET *pkt,
2203
    unsigned int context, X509 *x, size_t chainidx)
2204
0
{
2205
0
    unsigned int id, ct, mki;
2206
0
    int i;
2207
0
    STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
2208
0
    SRTP_PROTECTION_PROFILE *prof;
2209
2210
0
    if (!PACKET_get_net_2(pkt, &ct) || ct != 2
2211
0
        || !PACKET_get_net_2(pkt, &id)
2212
0
        || !PACKET_get_1(pkt, &mki)
2213
0
        || PACKET_remaining(pkt) != 0) {
2214
0
        SSLfatal(s, SSL_AD_DECODE_ERROR,
2215
0
            SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
2216
0
        return 0;
2217
0
    }
2218
2219
0
    if (mki != 0) {
2220
        /* Must be no MKI, since we never offer one */
2221
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRTP_MKI_VALUE);
2222
0
        return 0;
2223
0
    }
2224
2225
    /* Throw an error if the server gave us an unsolicited extension */
2226
0
    clnt = SSL_get_srtp_profiles(SSL_CONNECTION_GET_SSL(s));
2227
0
    if (clnt == NULL) {
2228
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_SRTP_PROFILES);
2229
0
        return 0;
2230
0
    }
2231
2232
    /*
2233
     * Check to see if the server gave us something we support (and
2234
     * presumably offered)
2235
     */
2236
0
    for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
2237
0
        prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
2238
2239
0
        if (prof->id == id) {
2240
0
            s->srtp_profile = prof;
2241
0
            return 1;
2242
0
        }
2243
0
    }
2244
2245
0
    SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
2246
0
        SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
2247
0
    return 0;
2248
0
}
2249
#endif
2250
2251
int tls_parse_stoc_etm(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
2252
    X509 *x, size_t chainidx)
2253
0
{
2254
    /* Ignore if inappropriate ciphersuite */
2255
0
    if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
2256
0
        && s->s3.tmp.new_cipher->algorithm_mac != SSL_AEAD
2257
0
        && s->s3.tmp.new_cipher->algorithm_enc != SSL_RC4
2258
0
        && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT
2259
0
        && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT12
2260
0
        && s->s3.tmp.new_cipher->algorithm_enc != SSL_MAGMA
2261
0
        && s->s3.tmp.new_cipher->algorithm_enc != SSL_KUZNYECHIK)
2262
0
        s->ext.use_etm = 1;
2263
2264
0
    return 1;
2265
0
}
2266
2267
int tls_parse_stoc_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
2268
    X509 *x, size_t chainidx)
2269
0
{
2270
0
    if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)
2271
0
        return 1;
2272
0
    s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS;
2273
0
    if (!s->hit)
2274
0
        s->session->flags |= SSL_SESS_FLAG_EXTMS;
2275
2276
0
    return 1;
2277
0
}
2278
2279
int tls_parse_stoc_supported_versions(SSL_CONNECTION *s, PACKET *pkt,
2280
    unsigned int context,
2281
    X509 *x, size_t chainidx)
2282
0
{
2283
0
    unsigned int version;
2284
0
    const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION : TLS1_3_VERSION;
2285
2286
0
    if (!PACKET_get_net_2(pkt, &version)
2287
0
        || PACKET_remaining(pkt) != 0) {
2288
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2289
0
        return 0;
2290
0
    }
2291
2292
    /*
2293
     * The only protocol version we support which is valid in this extension in
2294
     * a ServerHello is (D)TLSv1.3 therefore we shouldn't be getting anything else.
2295
     */
2296
0
    if ((int)version != version1_3) {
2297
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
2298
0
            SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
2299
0
        return 0;
2300
0
    }
2301
2302
    /* We ignore this extension for HRRs except to sanity check it */
2303
0
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)
2304
0
        return 1;
2305
2306
    /* We just set it here. We validate it in ssl_choose_client_version */
2307
0
    s->version = version;
2308
0
    if (!ssl_set_record_protocol_version(s, version)) {
2309
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2310
0
        return 0;
2311
0
    }
2312
2313
0
    return 1;
2314
0
}
2315
2316
int tls_parse_stoc_key_share(SSL_CONNECTION *s, PACKET *pkt,
2317
    unsigned int context, X509 *x,
2318
    size_t chainidx)
2319
0
{
2320
0
#if !(defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_DTLS1_3))
2321
0
    unsigned int group_id;
2322
0
    PACKET encoded_pt;
2323
0
    EVP_PKEY *ckey = s->s3.tmp.pkey, *skey = NULL;
2324
0
    const TLS_GROUP_INFO *ginf = NULL;
2325
0
    uint16_t valid_ks_id = 0;
2326
0
    size_t i;
2327
2328
    /* Sanity check */
2329
0
    if (ckey == NULL || s->s3.peer_tmp != NULL) {
2330
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2331
0
        return 0;
2332
0
    }
2333
2334
    /* Which group ID does the server want -> group_id */
2335
0
    if (!PACKET_get_net_2(pkt, &group_id)) {
2336
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2337
0
        return 0;
2338
0
    }
2339
2340
    /* RFC 8701: reject GREASE values selected by the server */
2341
0
    if (ossl_is_grease_value(group_id)) {
2342
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
2343
0
        return 0;
2344
0
    }
2345
2346
0
    if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) {
2347
0
        const uint16_t *pgroups = NULL;
2348
0
        size_t num_groups;
2349
0
        const int version1_3 = SSL_CONNECTION_IS_DTLS(s) ? DTLS1_3_VERSION
2350
0
                                                         : TLS1_3_VERSION;
2351
2352
0
        if (PACKET_remaining(pkt) != 0) {
2353
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2354
0
            return 0;
2355
0
        }
2356
2357
        /*
2358
         * It is an error if the HelloRetryRequest wants a key_share that we
2359
         * already sent in the first ClientHello
2360
         */
2361
0
        for (i = 0; i < s->s3.tmp.num_ks_pkey; i++) {
2362
0
            if (s->s3.tmp.ks_group_id[i] == group_id) {
2363
0
                SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
2364
0
                return 0;
2365
0
            }
2366
0
        }
2367
2368
        /* Validate the selected group is one we support */
2369
0
        tls1_get_supported_groups(s, &pgroups, &num_groups);
2370
0
        for (i = 0; i < num_groups; i++) {
2371
0
            if (group_id == pgroups[i])
2372
0
                break;
2373
0
        }
2374
2375
0
        if (i >= num_groups
2376
0
            || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)
2377
0
            || !tls_valid_group(s, group_id, version1_3, version1_3, NULL, NULL)) {
2378
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
2379
0
            return 0;
2380
0
        }
2381
2382
        /* Memorize which groupID the server wants */
2383
0
        s->s3.group_id = group_id;
2384
2385
        /* The initial keyshares are obsolete now, hence free memory */
2386
0
        for (i = 0; i < s->s3.tmp.num_ks_pkey; i++) {
2387
0
            if (s->s3.tmp.ks_pkey[i] != NULL) {
2388
0
                EVP_PKEY_free(s->s3.tmp.ks_pkey[i]);
2389
0
                s->s3.tmp.ks_pkey[i] = NULL;
2390
0
            }
2391
0
        }
2392
0
        s->s3.tmp.num_ks_pkey = 0;
2393
0
        s->s3.tmp.pkey = NULL;
2394
2395
0
        return 1;
2396
0
    }
2397
2398
    /*
2399
     * check that the group requested by the server is one we've
2400
     * sent a key share for, and if so: memorize which one
2401
     */
2402
0
    for (i = 0; i < s->s3.tmp.num_ks_pkey; i++) {
2403
0
        if (s->s3.tmp.ks_group_id[i] == group_id) {
2404
0
            valid_ks_id = group_id;
2405
0
            ckey = s->s3.tmp.ks_pkey[i];
2406
0
            s->s3.group_id = group_id;
2407
0
            s->s3.tmp.pkey = ckey;
2408
0
            break;
2409
0
        }
2410
0
    }
2411
0
    if (valid_ks_id == 0) {
2412
        /*
2413
         * This isn't for the group that we sent in the original
2414
         * key_share!
2415
         */
2416
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
2417
0
        return 0;
2418
0
    }
2419
    /* Retain this group in the SSL_SESSION */
2420
0
    if (!s->hit) {
2421
0
        s->session->kex_group = group_id;
2422
0
    } else if (group_id != s->session->kex_group) {
2423
        /*
2424
         * If this is a resumption but changed what group was used, we need
2425
         * to record the new group in the session, but the session is not
2426
         * a new session and could be in use by other threads.  So, make
2427
         * a copy of the session to record the new information so that it's
2428
         * useful for any sessions resumed from tickets issued on this
2429
         * connection.
2430
         */
2431
0
        SSL_SESSION *new_sess;
2432
2433
0
        if ((new_sess = ssl_session_dup(s->session, 0)) == NULL) {
2434
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
2435
0
            return 0;
2436
0
        }
2437
0
        SSL_SESSION_free(s->session);
2438
0
        s->session = new_sess;
2439
0
        s->session->kex_group = group_id;
2440
0
    }
2441
2442
0
    if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),
2443
0
             group_id))
2444
0
        == NULL) {
2445
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
2446
0
        return 0;
2447
0
    }
2448
2449
0
    if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt)
2450
0
        || PACKET_remaining(&encoded_pt) == 0) {
2451
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2452
0
        return 0;
2453
0
    }
2454
2455
0
    if (!ginf->is_kem) {
2456
        /* Regular KEX */
2457
0
        skey = EVP_PKEY_new();
2458
0
        if (skey == NULL || EVP_PKEY_copy_parameters(skey, ckey) <= 0) {
2459
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
2460
0
            EVP_PKEY_free(skey);
2461
0
            return 0;
2462
0
        }
2463
2464
0
        if (tls13_set_encoded_pub_key(skey, PACKET_data(&encoded_pt),
2465
0
                PACKET_remaining(&encoded_pt))
2466
0
            <= 0) {
2467
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
2468
0
            EVP_PKEY_free(skey);
2469
0
            return 0;
2470
0
        }
2471
2472
0
        if (ssl_derive(s, ckey, skey, 1) == 0) {
2473
            /* SSLfatal() already called */
2474
0
            EVP_PKEY_free(skey);
2475
0
            return 0;
2476
0
        }
2477
0
        s->s3.peer_tmp = skey;
2478
0
    } else {
2479
        /* KEM Mode */
2480
0
        const unsigned char *ct = PACKET_data(&encoded_pt);
2481
0
        size_t ctlen = PACKET_remaining(&encoded_pt);
2482
2483
0
        if (ssl_decapsulate(s, ckey, ct, ctlen, 1) == 0) {
2484
            /* SSLfatal() already called */
2485
0
            return 0;
2486
0
        }
2487
0
    }
2488
0
    s->s3.did_kex = 1;
2489
0
#endif
2490
2491
0
    return 1;
2492
0
}
2493
2494
int tls_parse_stoc_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
2495
    X509 *x, size_t chainidx)
2496
0
{
2497
0
    PACKET cookie;
2498
2499
0
    if (!PACKET_as_length_prefixed_2(pkt, &cookie)
2500
0
        || PACKET_remaining(&cookie) == 0
2501
0
        || !PACKET_memdup(&cookie, &s->ext.tls13_cookie,
2502
0
            &s->ext.tls13_cookie_len)) {
2503
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2504
0
        return 0;
2505
0
    }
2506
2507
0
    return 1;
2508
0
}
2509
2510
int tls_parse_stoc_early_data(SSL_CONNECTION *s, PACKET *pkt,
2511
    unsigned int context,
2512
    X509 *x, size_t chainidx)
2513
0
{
2514
0
    if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
2515
0
        unsigned long max_early_data;
2516
2517
0
        if (!PACKET_get_net_4(pkt, &max_early_data)
2518
0
            || PACKET_remaining(pkt) != 0) {
2519
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_MAX_EARLY_DATA);
2520
0
            return 0;
2521
0
        }
2522
2523
0
        s->session->ext.max_early_data = max_early_data;
2524
2525
0
        if (SSL_IS_QUIC_HANDSHAKE(s) && max_early_data != 0xffffffff) {
2526
            /*
2527
             * QUIC allows missing max_early_data, or a max_early_data value
2528
             * of 0xffffffff. Missing max_early_data is stored in the session
2529
             * as 0. This is indistinguishable in OpenSSL from a present
2530
             * max_early_data value that was 0. In order that later checks for
2531
             * invalid max_early_data correctly treat as an error the case where
2532
             * max_early_data is present and it is 0, we store any invalid
2533
             * value in the same (non-zero) way. Otherwise we would have to
2534
             * introduce a new flag just for this.
2535
             */
2536
0
            s->session->ext.max_early_data = 1;
2537
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INVALID_MAX_EARLY_DATA);
2538
0
            return 0;
2539
0
        }
2540
2541
0
        return 1;
2542
0
    }
2543
2544
0
    if (PACKET_remaining(pkt) != 0) {
2545
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2546
0
        return 0;
2547
0
    }
2548
2549
0
    if (!s->ext.early_data_ok
2550
0
        || !s->hit) {
2551
        /*
2552
         * If we get here then we didn't send early data, or we didn't resume
2553
         * using the first identity, or the SNI/ALPN is not consistent so the
2554
         * server should not be accepting it.
2555
         */
2556
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
2557
0
        return 0;
2558
0
    }
2559
2560
0
    s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
2561
2562
0
    return 1;
2563
0
}
2564
2565
int tls_parse_stoc_psk(SSL_CONNECTION *s, PACKET *pkt,
2566
    unsigned int context, X509 *x,
2567
    size_t chainidx)
2568
0
{
2569
0
#if !(defined(OPENSSL_NO_TLS1_3) && defined(OPENSSL_NO_DTLS1_3))
2570
0
    SSL_SESSION *sesstmp;
2571
0
    unsigned int identity;
2572
2573
0
    if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) {
2574
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2575
0
        return 0;
2576
0
    }
2577
2578
0
    if (identity >= (unsigned int)s->ext.tick_identity) {
2579
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_PSK_IDENTITY);
2580
0
        return 0;
2581
0
    }
2582
2583
    /*
2584
     * Session resumption tickets are always sent before PSK tickets. If the
2585
     * ticket index is 0 then it must be for a session resumption ticket if we
2586
     * sent two tickets, or if we didn't send a PSK ticket.
2587
     */
2588
0
    if (identity == 0 && (s->psksession == NULL || s->ext.tick_identity == 2)) {
2589
0
        s->hit = 1;
2590
0
        SSL_SESSION_free(s->psksession);
2591
0
        s->psksession = NULL;
2592
0
        return 1;
2593
0
    }
2594
2595
0
    if (s->psksession == NULL) {
2596
        /* Should never happen */
2597
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2598
0
        return 0;
2599
0
    }
2600
2601
    /*
2602
     * If we used the external PSK for sending early_data then s->early_secret
2603
     * is already set up, so don't overwrite it. Otherwise we copy the
2604
     * early_secret across that we generated earlier.
2605
     */
2606
0
    if ((s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
2607
0
            && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
2608
0
        || s->ext.early_data_session != s->psksession)
2609
0
        memcpy(s->early_secret, s->psksession->early_secret, EVP_MAX_MD_SIZE);
2610
2611
    /*
2612
     * The psk_use_session_cb()/psk_client_callback() may reuse
2613
     * the session across connections we can't mutate it directly.
2614
     */
2615
0
    if ((sesstmp = ssl_session_dup(s->psksession, 0)) == NULL) {
2616
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2617
0
        return 0;
2618
0
    }
2619
0
    SSL_SESSION_free(s->psksession);
2620
0
    s->psksession = sesstmp;
2621
2622
    /*
2623
     * s->psksession (now our private copy) was built by the callback, not via
2624
     * ssl_get_new_session(), so it was never stamped with our own sid_ctx. Do
2625
     * so now, to avoid rejection of the PSK session in tls_process_server_hello().
2626
     */
2627
0
    memcpy(s->psksession->sid_ctx, s->sid_ctx, s->sid_ctx_length);
2628
0
    s->psksession->sid_ctx_length = s->sid_ctx_length;
2629
2630
0
    SSL_SESSION_free(s->session);
2631
0
    s->session = s->psksession;
2632
0
    s->psksession = NULL;
2633
0
    s->hit = 1;
2634
    /* Early data is only allowed if we used the first ticket */
2635
0
    if (identity != 0)
2636
0
        s->ext.early_data_ok = 0;
2637
0
#endif
2638
2639
0
    return 1;
2640
0
}
2641
2642
EXT_RETURN tls_construct_ctos_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2643
    unsigned int context,
2644
    X509 *x, size_t chainidx)
2645
0
{
2646
0
    sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2647
0
    if (sc->client_cert_type == NULL)
2648
0
        return EXT_RETURN_NOT_SENT;
2649
0
#ifndef OPENSSL_NO_ECH
2650
0
    ECH_SAME_EXT(sc, context, pkt)
2651
0
#endif
2652
2653
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type)
2654
0
        || !WPACKET_start_sub_packet_u16(pkt)
2655
0
        || !WPACKET_sub_memcpy_u8(pkt, sc->client_cert_type, sc->client_cert_type_len)
2656
0
        || !WPACKET_close(pkt)) {
2657
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2658
0
        return EXT_RETURN_FAIL;
2659
0
    }
2660
0
    sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD;
2661
0
    return EXT_RETURN_SENT;
2662
0
}
2663
2664
int tls_parse_stoc_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2665
    unsigned int context,
2666
    X509 *x, size_t chainidx)
2667
0
{
2668
0
    unsigned int type;
2669
2670
0
    if (PACKET_remaining(pkt) != 1) {
2671
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2672
0
        return 0;
2673
0
    }
2674
0
    if (!PACKET_get_1(pkt, &type)) {
2675
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2676
0
        return 0;
2677
0
    }
2678
    /* We did not send/ask for this */
2679
0
    if (!ossl_assert(sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) {
2680
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2681
0
        return 0;
2682
0
    }
2683
    /* We don't have this enabled */
2684
0
    if (sc->client_cert_type == NULL) {
2685
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2686
0
        return 0;
2687
0
    }
2688
    /* Given back a value we didn't configure */
2689
0
    if (memchr(sc->client_cert_type, type, sc->client_cert_type_len) == NULL) {
2690
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE);
2691
0
        return 0;
2692
0
    }
2693
0
    sc->ext.client_cert_type = type;
2694
0
    return 1;
2695
0
}
2696
2697
EXT_RETURN tls_construct_ctos_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2698
    unsigned int context,
2699
    X509 *x, size_t chainidx)
2700
0
{
2701
0
    sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2702
0
    if (sc->server_cert_type == NULL)
2703
0
        return EXT_RETURN_NOT_SENT;
2704
0
#ifndef OPENSSL_NO_ECH
2705
0
    ECH_SAME_EXT(sc, context, pkt)
2706
0
#endif
2707
2708
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type)
2709
0
        || !WPACKET_start_sub_packet_u16(pkt)
2710
0
        || !WPACKET_sub_memcpy_u8(pkt, sc->server_cert_type, sc->server_cert_type_len)
2711
0
        || !WPACKET_close(pkt)) {
2712
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2713
0
        return EXT_RETURN_FAIL;
2714
0
    }
2715
0
    sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD;
2716
0
    return EXT_RETURN_SENT;
2717
0
}
2718
2719
int tls_parse_stoc_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2720
    unsigned int context,
2721
    X509 *x, size_t chainidx)
2722
0
{
2723
0
    unsigned int type;
2724
2725
0
    if (PACKET_remaining(pkt) != 1) {
2726
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2727
0
        return 0;
2728
0
    }
2729
0
    if (!PACKET_get_1(pkt, &type)) {
2730
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2731
0
        return 0;
2732
0
    }
2733
    /* We did not send/ask for this */
2734
0
    if (!ossl_assert(sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) {
2735
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2736
0
        return 0;
2737
0
    }
2738
    /* We don't have this enabled */
2739
0
    if (sc->server_cert_type == NULL) {
2740
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2741
0
        return 0;
2742
0
    }
2743
    /* Given back a value we didn't configure */
2744
0
    if (memchr(sc->server_cert_type, type, sc->server_cert_type_len) == NULL) {
2745
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE);
2746
0
        return 0;
2747
0
    }
2748
0
    sc->ext.server_cert_type = type;
2749
0
    return 1;
2750
0
}
2751
2752
#ifndef OPENSSL_NO_ECH
2753
EXT_RETURN tls_construct_ctos_ech(SSL_CONNECTION *s, WPACKET *pkt,
2754
    unsigned int context, X509 *x,
2755
    size_t chainidx)
2756
0
{
2757
0
    int rv = 0, hpke_mode = OSSL_HPKE_MODE_BASE;
2758
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2759
0
    OSSL_ECHSTORE_ENTRY *ee = NULL;
2760
0
    OSSL_HPKE_SUITE hpke_suite = OSSL_HPKE_SUITE_DEFAULT;
2761
0
    unsigned char config_id_to_use = 0x00, info[OSSL_ECH_MAX_INFO_LEN];
2762
0
    unsigned char *encoded = NULL, *mypub = NULL;
2763
0
    size_t cipherlen = 0, aad_len = 0, lenclen = 0, mypub_len = 0;
2764
0
    size_t info_len = OSSL_ECH_MAX_INFO_LEN, clear_len = 0, encoded_len = 0;
2765
    /* whether or not we've been asked to GREASE, one way or another */
2766
0
    int grease_opt_set = (s->ext.ech.attempted != 1
2767
0
        && ((s->ext.ech.grease == OSSL_ECH_IS_GREASE)
2768
0
            || ((s->options & SSL_OP_ECH_GREASE) != 0)));
2769
2770
    /* if we're not doing real ECH and not GREASEing then exit */
2771
0
    if (s->ext.ech.attempted_type != TLSEXT_TYPE_ech && grease_opt_set == 0)
2772
0
        return EXT_RETURN_NOT_SENT;
2773
    /* send grease if not really attempting ECH */
2774
0
    if (grease_opt_set == 1) {
2775
0
        if (s->hello_retry_request == SSL_HRR_PENDING
2776
0
            && s->ext.ech.sent != NULL) {
2777
            /* re-tx already sent GREASEy ECH */
2778
0
            if (WPACKET_memcpy(pkt, s->ext.ech.sent,
2779
0
                    s->ext.ech.sent_len)
2780
0
                != 1) {
2781
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2782
0
                return EXT_RETURN_FAIL;
2783
0
            }
2784
0
            return EXT_RETURN_SENT;
2785
0
        }
2786
        /* if nobody set a type, use the default */
2787
0
        if (s->ext.ech.attempted_type == OSSL_ECH_type_unknown)
2788
0
            s->ext.ech.attempted_type = TLSEXT_TYPE_ech;
2789
0
        if (ossl_ech_send_grease(s, pkt) != 1) {
2790
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2791
0
            return EXT_RETURN_FAIL;
2792
0
        }
2793
0
        return EXT_RETURN_SENT;
2794
0
    }
2795
2796
    /* For the inner CH - we simply include one of these saying "inner" */
2797
0
    if (s->ext.ech.ch_depth == 1) {
2798
0
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ech)
2799
0
            || !WPACKET_start_sub_packet_u16(pkt)
2800
0
            || !WPACKET_put_bytes_u8(pkt, OSSL_ECH_INNER_CH_TYPE)
2801
0
            || !WPACKET_close(pkt)) {
2802
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2803
0
            return EXT_RETURN_FAIL;
2804
0
        }
2805
0
        return EXT_RETURN_SENT;
2806
0
    }
2807
2808
    /*
2809
     * If not GREASEing we prepare sending the outer value - after the
2810
     * entire thing has been constructed, putting in zeros for now where
2811
     * we'd otherwise include ECH ciphertext, we later encode and encrypt.
2812
     * We need to do it that way as we need the rest of the outer CH to
2813
     * be known and used as AAD input before we do encryption.
2814
     */
2815
0
    if (s->ext.ech.ch_depth != 0)
2816
0
        return EXT_RETURN_NOT_SENT;
2817
    /* Make ClientHelloInner and EncodedClientHelloInner as per spec. */
2818
0
    if (ossl_ech_encode_inner(s, &encoded, &encoded_len) != 1) {
2819
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2820
0
        goto err;
2821
0
    }
2822
0
    s->ext.ech.encoded_inner = encoded;
2823
0
    s->ext.ech.encoded_inner_len = encoded_len;
2824
0
#ifdef OSSL_ECH_SUPERVERBOSE
2825
0
    ossl_ech_pbuf("encoded inner CH", encoded, encoded_len);
2826
0
#endif
2827
0
    rv = ossl_ech_pick_matching_cfg(s, &ee, &hpke_suite);
2828
0
    if (rv != 1 || ee == NULL) {
2829
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2830
0
        goto err;
2831
0
    }
2832
0
    s->ext.ech.attempted_type = ee->version;
2833
0
    OSSL_TRACE_BEGIN(TLS)
2834
0
    {
2835
0
        BIO_printf(trc_out, "EAAE: selected: version: %4x, config %2x\n",
2836
0
            ee->version, ee->config_id);
2837
0
    }
2838
0
    OSSL_TRACE_END(TLS);
2839
0
    config_id_to_use = ee->config_id; /* if requested, use a random config_id instead */
2840
0
    if ((s->options & SSL_OP_ECH_IGNORE_CID) != 0) {
2841
0
        int max_iters = 1000, i = 0;
2842
2843
        /* rejection sample to get a different but random config_id */
2844
0
        while (config_id_to_use == ee->config_id) {
2845
0
#ifdef OSSL_ECH_SUPERVERBOSE
2846
0
            if (i > 0) {
2847
0
                OSSL_TRACE_BEGIN(TLS)
2848
0
                {
2849
0
                    BIO_printf(trc_out, "EAAE: rejected random-config %02x\n",
2850
0
                        config_id_to_use);
2851
0
                }
2852
0
                OSSL_TRACE_END(TLS);
2853
0
            }
2854
0
#endif
2855
0
            if (RAND_bytes_ex(sctx->libctx, &config_id_to_use, 1, 0) <= 0) {
2856
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2857
0
                return 0;
2858
0
            }
2859
0
            if (i++ >= max_iters) {
2860
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2861
0
                return 0;
2862
0
            }
2863
0
        }
2864
0
#ifdef OSSL_ECH_SUPERVERBOSE
2865
0
        ossl_ech_pbuf("EAAE: random config_id", &config_id_to_use, 1);
2866
0
#endif
2867
0
    }
2868
0
    s->ext.ech.attempted_cid = config_id_to_use;
2869
0
#ifdef OSSL_ECH_SUPERVERBOSE
2870
0
    ossl_ech_pbuf("EAAE: peer pub", ee->pub, ee->pub_len);
2871
0
    ossl_ech_pbuf("EAAE: clear", encoded, encoded_len);
2872
0
    ossl_ech_pbuf("EAAE: ECHConfig", ee->encoded, ee->encoded_len);
2873
0
#endif
2874
    /*
2875
     * The AAD is the full outer client hello but with the correct number of
2876
     * zeros for where the ECH ciphertext octets will later be placed. So we
2877
     * add the ECH extension to the |pkt| but with zeros for ciphertext, that
2878
     * forms up the AAD, then after we've encrypted, we'll splice in the actual
2879
     * ciphertext.
2880
     * Watch out for the "4" offsets that remove the type and 3-octet length
2881
     * from the encoded CH as per the spec.
2882
     */
2883
0
    clear_len = ossl_ech_calc_padding(s, ee, encoded_len);
2884
0
    if (clear_len == 0) {
2885
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2886
0
        goto err;
2887
0
    }
2888
0
    lenclen = OSSL_HPKE_get_public_encap_size(hpke_suite);
2889
0
    if (s->ext.ech.hpke_ctx == NULL) { /* 1st CH */
2890
0
        if (ossl_ech_make_enc_info(ee->encoded, ee->encoded_len,
2891
0
                info, &info_len)
2892
0
            != 1) {
2893
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2894
0
            goto err;
2895
0
        }
2896
0
#ifdef OSSL_ECH_SUPERVERBOSE
2897
0
        ossl_ech_pbuf("EAAE info", info, info_len);
2898
0
#endif
2899
0
        s->ext.ech.hpke_ctx = OSSL_HPKE_CTX_new(hpke_mode, hpke_suite,
2900
0
            OSSL_HPKE_ROLE_SENDER,
2901
0
            sctx->libctx, sctx->propq);
2902
0
        if (s->ext.ech.hpke_ctx == NULL) {
2903
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2904
0
            goto err;
2905
0
        }
2906
0
        mypub = OPENSSL_malloc(lenclen);
2907
0
        if (mypub == NULL) {
2908
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2909
0
            goto err;
2910
0
        }
2911
0
        mypub_len = lenclen;
2912
0
        rv = OSSL_HPKE_encap(s->ext.ech.hpke_ctx, mypub, &mypub_len,
2913
0
            ee->pub, ee->pub_len, info, info_len);
2914
0
        if (rv != 1) {
2915
0
            OPENSSL_free(mypub);
2916
0
            mypub = NULL;
2917
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2918
0
            goto err;
2919
0
        }
2920
0
        s->ext.ech.pub = mypub;
2921
0
        s->ext.ech.pub_len = mypub_len;
2922
0
    } else { /* HRR - retrieve public */
2923
0
        mypub = s->ext.ech.pub;
2924
0
        mypub_len = s->ext.ech.pub_len;
2925
0
        if (mypub == NULL || mypub_len == 0) {
2926
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2927
0
            goto err;
2928
0
        }
2929
0
    }
2930
0
#ifdef OSSL_ECH_SUPERVERBOSE
2931
0
    ossl_ech_pbuf("EAAE: mypub", mypub, mypub_len);
2932
0
    WPACKET_get_total_written(pkt, &aad_len); /* use aad_len for tracing */
2933
0
    ossl_ech_pbuf("EAAE pkt b4", WPACKET_get_curr(pkt) - aad_len, aad_len);
2934
0
#endif
2935
0
    cipherlen = OSSL_HPKE_get_ciphertext_size(hpke_suite, clear_len);
2936
0
    if (cipherlen <= clear_len || cipherlen > OSSL_ECH_MAX_PAYLOAD_LEN) {
2937
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2938
0
        goto err;
2939
0
    }
2940
0
    s->ext.ech.clearlen = clear_len;
2941
0
    s->ext.ech.cipherlen = cipherlen;
2942
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ech)
2943
0
        || !WPACKET_start_sub_packet_u16(pkt)
2944
0
        || !WPACKET_put_bytes_u8(pkt, OSSL_ECH_OUTER_CH_TYPE)
2945
0
        || !WPACKET_put_bytes_u16(pkt, hpke_suite.kdf_id)
2946
0
        || !WPACKET_put_bytes_u16(pkt, hpke_suite.aead_id)
2947
0
        || !WPACKET_put_bytes_u8(pkt, config_id_to_use)
2948
0
        || (s->hello_retry_request == SSL_HRR_PENDING
2949
0
            && !WPACKET_put_bytes_u16(pkt, 0x00)) /* no pub */
2950
0
        || (s->hello_retry_request != SSL_HRR_PENDING
2951
0
            && !WPACKET_sub_memcpy_u16(pkt, mypub, mypub_len))
2952
0
        || !WPACKET_start_sub_packet_u16(pkt)
2953
0
        || !WPACKET_get_total_written(pkt, &s->ext.ech.cipher_offset)
2954
0
        || !WPACKET_memset(pkt, 0, cipherlen)
2955
0
        || !WPACKET_close(pkt)
2956
0
        || !WPACKET_close(pkt)) {
2957
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2958
0
        goto err;
2959
0
    }
2960
    /* don't count the type + 3-octet length */
2961
0
    s->ext.ech.cipher_offset -= 4;
2962
0
    return EXT_RETURN_SENT;
2963
0
err:
2964
0
    return EXT_RETURN_FAIL;
2965
0
}
2966
2967
/* if the server thinks we GREASE'd then we may get an ECHConfigList */
2968
int tls_parse_stoc_ech(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
2969
    X509 *x, size_t chainidx)
2970
0
{
2971
0
    size_t rlen = 0;
2972
0
    const unsigned char *rval = NULL;
2973
0
    unsigned char *srval = NULL;
2974
0
    PACKET rcfgs_pkt;
2975
2976
    /*
2977
     * An HRR will have an ECH extension with the 8-octet confirmation value.
2978
     * Store it away for when we check it later
2979
     */
2980
0
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) {
2981
0
        if (PACKET_remaining(pkt) != OSSL_ECH_SIGNAL_LEN) {
2982
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2983
0
            return 0;
2984
0
        }
2985
0
        s->ext.ech.hrrsignal_p = (unsigned char *)PACKET_data(pkt);
2986
0
        memcpy(s->ext.ech.hrrsignal, s->ext.ech.hrrsignal_p,
2987
0
            OSSL_ECH_SIGNAL_LEN);
2988
0
        return 1;
2989
0
    }
2990
    /* otherwise we expect retry-configs */
2991
0
    if (!PACKET_get_length_prefixed_2(pkt, &rcfgs_pkt)) {
2992
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2993
0
        return 0;
2994
0
    }
2995
0
    rval = PACKET_data(&rcfgs_pkt);
2996
0
    rlen = (unsigned int)PACKET_remaining(&rcfgs_pkt);
2997
0
    OPENSSL_free(s->ext.ech.returned);
2998
0
    s->ext.ech.returned = NULL;
2999
0
    srval = OPENSSL_malloc(rlen + 2);
3000
0
    if (srval == NULL) {
3001
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3002
0
        return 0;
3003
0
    }
3004
0
    srval[0] = (rlen >> 8) & 0xff;
3005
0
    srval[1] = rlen & 0xff;
3006
0
    memcpy(srval + 2, rval, rlen);
3007
0
    s->ext.ech.returned = srval;
3008
0
    s->ext.ech.returned_len = rlen + 2;
3009
0
    return 1;
3010
0
}
3011
#endif /* END_OPENSSL_NO_ECH */
3012
3013
/*
3014
 * RFC 8701 GREASE extension constructors.  Each writes an empty extension
3015
 * whose type is a GREASE value (0x?A?A pattern).
3016
 */
3017
EXT_RETURN tls_construct_ctos_grease1(SSL_CONNECTION *s, WPACKET *pkt,
3018
    unsigned int context, X509 *x,
3019
    size_t chainidx)
3020
0
{
3021
0
    uint16_t grease_type;
3022
3023
0
    if (!(s->options & SSL_OP_GREASE) || s->server)
3024
0
        return EXT_RETURN_NOT_SENT;
3025
3026
0
    grease_type = ossl_grease_value(s, OSSL_GREASE_EXT1);
3027
3028
0
    if (!WPACKET_put_bytes_u16(pkt, grease_type)
3029
0
        || !WPACKET_put_bytes_u16(pkt, 0)) {
3030
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3031
0
        return EXT_RETURN_FAIL;
3032
0
    }
3033
3034
0
    return EXT_RETURN_SENT;
3035
0
}
3036
3037
EXT_RETURN tls_construct_ctos_grease2(SSL_CONNECTION *s, WPACKET *pkt,
3038
    unsigned int context, X509 *x,
3039
    size_t chainidx)
3040
0
{
3041
0
    uint16_t grease_type;
3042
3043
0
    if (!(s->options & SSL_OP_GREASE) || s->server)
3044
0
        return EXT_RETURN_NOT_SENT;
3045
3046
0
    grease_type = ossl_grease_value(s, OSSL_GREASE_EXT2);
3047
3048
    /*
3049
     * RFC 8701 recommends "varying length and contents" for GREASE
3050
     * extensions.  Extension 1 is empty; extension 2 carries one zero byte
3051
     * so that servers are tested against both empty and non-empty unknown
3052
     * extensions.  This mirrors the BoringSSL behaviour.
3053
     */
3054
0
    if (!WPACKET_put_bytes_u16(pkt, grease_type)
3055
0
        || !WPACKET_start_sub_packet_u16(pkt)
3056
0
        || !WPACKET_put_bytes_u8(pkt, 0)
3057
0
        || !WPACKET_close(pkt)) {
3058
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3059
0
        return EXT_RETURN_FAIL;
3060
0
    }
3061
3062
0
    return EXT_RETURN_SENT;
3063
0
}