Coverage Report

Created: 2026-04-08 06:20

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