Coverage Report

Created: 2026-03-09 06:55

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