Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/ssl/statem/extensions_clnt.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2024 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 "statem_local.h"
14
15
EXT_RETURN tls_construct_ctos_renegotiate(SSL_CONNECTION *s, WPACKET *pkt,
16
    unsigned int context, X509 *x,
17
    size_t chainidx)
18
88.1k
{
19
88.1k
    if (!s->renegotiate) {
20
        /* If not renegotiating, send an empty RI extension to indicate support */
21
22
#if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION
23
#error Internal DTLS version error
24
#endif
25
26
87.1k
        if (!SSL_CONNECTION_IS_DTLS(s)
27
66.4k
            && (s->min_proto_version >= TLS1_3_VERSION
28
26.6k
                || (ssl_security(s, SSL_SECOP_VERSION, 0, TLS1_VERSION, NULL)
29
66.4k
                    && s->min_proto_version <= TLS1_VERSION))) {
30
            /*
31
             * For TLS <= 1.0 SCSV is used instead, and for TLS 1.3 this
32
             * extension isn't used at all.
33
             */
34
66.4k
            return EXT_RETURN_NOT_SENT;
35
66.4k
        }
36
37
20.7k
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
38
20.7k
            || !WPACKET_start_sub_packet_u16(pkt)
39
20.7k
            || !WPACKET_put_bytes_u8(pkt, 0)
40
20.7k
            || !WPACKET_close(pkt)) {
41
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
42
0
            return EXT_RETURN_FAIL;
43
0
        }
44
45
20.7k
        return EXT_RETURN_SENT;
46
20.7k
    }
47
48
    /* Add a complete RI extension if renegotiating */
49
1.01k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
50
1.01k
        || !WPACKET_start_sub_packet_u16(pkt)
51
1.01k
        || !WPACKET_sub_memcpy_u8(pkt, s->s3.previous_client_finished,
52
1.01k
            s->s3.previous_client_finished_len)
53
1.01k
        || !WPACKET_close(pkt)) {
54
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
55
0
        return EXT_RETURN_FAIL;
56
0
    }
57
58
1.01k
    return EXT_RETURN_SENT;
59
1.01k
}
60
61
EXT_RETURN tls_construct_ctos_server_name(SSL_CONNECTION *s, WPACKET *pkt,
62
    unsigned int context, X509 *x,
63
    size_t chainidx)
64
117k
{
65
117k
    if (s->ext.hostname == NULL)
66
0
        return EXT_RETURN_NOT_SENT;
67
68
    /* Add TLS extension servername to the Client Hello message */
69
117k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
70
        /* Sub-packet for server_name extension */
71
117k
        || !WPACKET_start_sub_packet_u16(pkt)
72
        /* Sub-packet for servername list (always 1 hostname)*/
73
117k
        || !WPACKET_start_sub_packet_u16(pkt)
74
117k
        || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)
75
117k
        || !WPACKET_sub_memcpy_u16(pkt, s->ext.hostname,
76
117k
            strlen(s->ext.hostname))
77
117k
        || !WPACKET_close(pkt)
78
117k
        || !WPACKET_close(pkt)) {
79
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
80
0
        return EXT_RETURN_FAIL;
81
0
    }
82
83
117k
    return EXT_RETURN_SENT;
84
117k
}
85
86
/* Push a Max Fragment Len extension into ClientHello */
87
EXT_RETURN tls_construct_ctos_maxfragmentlen(SSL_CONNECTION *s, WPACKET *pkt,
88
    unsigned int context, X509 *x,
89
    size_t chainidx)
90
117k
{
91
117k
    if (s->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_DISABLED)
92
117k
        return EXT_RETURN_NOT_SENT;
93
94
    /* Add Max Fragment Length extension if client enabled it. */
95
    /*-
96
     * 4 bytes for this extension type and extension length
97
     * 1 byte for the Max Fragment Length code value.
98
     */
99
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)
100
        /* Sub-packet for Max Fragment Length extension (1 byte) */
101
0
        || !WPACKET_start_sub_packet_u16(pkt)
102
0
        || !WPACKET_put_bytes_u8(pkt, s->ext.max_fragment_len_mode)
103
0
        || !WPACKET_close(pkt)) {
104
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
105
0
        return EXT_RETURN_FAIL;
106
0
    }
107
108
0
    return EXT_RETURN_SENT;
109
0
}
110
111
#ifndef OPENSSL_NO_SRP
112
EXT_RETURN tls_construct_ctos_srp(SSL_CONNECTION *s, WPACKET *pkt,
113
    unsigned int context,
114
    X509 *x, size_t chainidx)
115
117k
{
116
    /* Add SRP username if there is one */
117
117k
    if (s->srp_ctx.login == NULL)
118
117k
        return EXT_RETURN_NOT_SENT;
119
120
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)
121
        /* Sub-packet for SRP extension */
122
0
        || !WPACKET_start_sub_packet_u16(pkt)
123
0
        || !WPACKET_start_sub_packet_u8(pkt)
124
        /* login must not be zero...internal error if so */
125
0
        || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
126
0
        || !WPACKET_memcpy(pkt, s->srp_ctx.login,
127
0
            strlen(s->srp_ctx.login))
128
0
        || !WPACKET_close(pkt)
129
0
        || !WPACKET_close(pkt)) {
130
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
131
0
        return EXT_RETURN_FAIL;
132
0
    }
133
134
0
    return EXT_RETURN_SENT;
135
0
}
136
#endif
137
138
static int use_ecc(SSL_CONNECTION *s, int min_version, int max_version)
139
193k
{
140
193k
    int i, end, ret = 0;
141
193k
    unsigned long alg_k, alg_a;
142
193k
    STACK_OF(SSL_CIPHER) *cipher_stack = NULL;
143
193k
    const uint16_t *pgroups = NULL;
144
193k
    size_t num_groups, j;
145
193k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
146
147
    /* See if we support any ECC ciphersuites */
148
193k
    if (s->version == SSL3_VERSION)
149
0
        return 0;
150
151
193k
    cipher_stack = SSL_get1_supported_ciphers(ssl);
152
193k
    end = sk_SSL_CIPHER_num(cipher_stack);
153
193k
    for (i = 0; i < end; i++) {
154
193k
        const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
155
156
193k
        alg_k = c->algorithm_mkey;
157
193k
        alg_a = c->algorithm_auth;
158
193k
        if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
159
147k
            || (alg_a & SSL_aECDSA)
160
193k
            || c->min_tls >= TLS1_3_VERSION) {
161
193k
            ret = 1;
162
193k
            break;
163
193k
        }
164
193k
    }
165
193k
    sk_SSL_CIPHER_free(cipher_stack);
166
193k
    if (!ret)
167
0
        return 0;
168
169
    /* Check we have at least one EC supported group */
170
193k
    tls1_get_supported_groups(s, &pgroups, &num_groups);
171
284k
    for (j = 0; j < num_groups; j++) {
172
284k
        uint16_t ctmp = pgroups[j];
173
174
284k
        if (tls_valid_group(s, ctmp, min_version, max_version, 1, NULL)
175
193k
            && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED))
176
193k
            return 1;
177
284k
    }
178
179
266
    return 0;
180
193k
}
181
182
EXT_RETURN tls_construct_ctos_ec_pt_formats(SSL_CONNECTION *s, WPACKET *pkt,
183
    unsigned int context, X509 *x,
184
    size_t chainidx)
185
117k
{
186
117k
    const unsigned char *pformats;
187
117k
    size_t num_formats;
188
117k
    int reason, min_version, max_version;
189
190
117k
    reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
191
117k
    if (reason != 0) {
192
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
193
0
        return EXT_RETURN_FAIL;
194
0
    }
195
117k
    if (!use_ecc(s, min_version, max_version))
196
231
        return EXT_RETURN_NOT_SENT;
197
198
    /* Add TLS extension ECPointFormats to the ClientHello message */
199
117k
    tls1_get_formatlist(s, &pformats, &num_formats);
200
201
117k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
202
        /* Sub-packet for formats extension */
203
117k
        || !WPACKET_start_sub_packet_u16(pkt)
204
117k
        || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)
205
117k
        || !WPACKET_close(pkt)) {
206
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
207
0
        return EXT_RETURN_FAIL;
208
0
    }
209
210
117k
    return EXT_RETURN_SENT;
211
117k
}
212
213
EXT_RETURN tls_construct_ctos_supported_groups(SSL_CONNECTION *s, WPACKET *pkt,
214
    unsigned int context, X509 *x,
215
    size_t chainidx)
216
117k
{
217
117k
    const uint16_t *pgroups = NULL;
218
117k
    size_t num_groups = 0, i, tls13added = 0, added = 0;
219
117k
    int min_version, max_version, reason;
220
221
117k
    reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
222
117k
    if (reason != 0) {
223
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
224
0
        return EXT_RETURN_FAIL;
225
0
    }
226
227
    /*
228
     * We only support EC groups in TLSv1.2 or below, and in DTLS. Therefore
229
     * if we don't have EC support then we don't send this extension.
230
     */
231
117k
    if (!use_ecc(s, min_version, max_version)
232
231
        && (SSL_CONNECTION_IS_DTLS(s) || max_version < TLS1_3_VERSION))
233
231
        return EXT_RETURN_NOT_SENT;
234
235
    /*
236
     * Add TLS extension supported_groups to the ClientHello message
237
     */
238
117k
    tls1_get_supported_groups(s, &pgroups, &num_groups);
239
240
117k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
241
        /* Sub-packet for supported_groups extension */
242
117k
        || !WPACKET_start_sub_packet_u16(pkt)
243
117k
        || !WPACKET_start_sub_packet_u16(pkt)
244
117k
        || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)) {
245
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
246
0
        return EXT_RETURN_FAIL;
247
0
    }
248
    /* Copy group ID if supported */
249
1.16M
    for (i = 0; i < num_groups; i++) {
250
1.04M
        uint16_t ctmp = pgroups[i];
251
1.04M
        int okfortls13;
252
253
1.04M
        if (tls_valid_group(s, ctmp, min_version, max_version, 0, &okfortls13)
254
941k
            && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) {
255
941k
            if (!WPACKET_put_bytes_u16(pkt, ctmp)) {
256
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
257
0
                return EXT_RETURN_FAIL;
258
0
            }
259
941k
            if (okfortls13 && max_version == TLS1_3_VERSION)
260
805k
                tls13added++;
261
941k
            added++;
262
941k
        }
263
1.04M
    }
264
117k
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
265
0
        if (added == 0)
266
0
            SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS,
267
0
                "No groups enabled for max supported SSL/TLS version");
268
0
        else
269
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
270
0
        return EXT_RETURN_FAIL;
271
0
    }
272
273
117k
    if (tls13added == 0 && max_version == TLS1_3_VERSION) {
274
0
        SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_GROUPS,
275
0
            "No groups enabled for max supported SSL/TLS version");
276
0
        return EXT_RETURN_FAIL;
277
0
    }
278
279
117k
    return EXT_RETURN_SENT;
280
117k
}
281
282
EXT_RETURN tls_construct_ctos_session_ticket(SSL_CONNECTION *s, WPACKET *pkt,
283
    unsigned int context, X509 *x,
284
    size_t chainidx)
285
117k
{
286
117k
    size_t ticklen;
287
288
117k
    if (!tls_use_ticket(s))
289
0
        return EXT_RETURN_NOT_SENT;
290
291
117k
    if (!s->new_session && s->session != NULL
292
117k
        && s->session->ext.tick != NULL
293
81
        && s->session->ssl_version != TLS1_3_VERSION) {
294
81
        ticklen = s->session->ext.ticklen;
295
117k
    } else if (s->session && s->ext.session_ticket != NULL
296
0
        && s->ext.session_ticket->data != NULL) {
297
0
        ticklen = s->ext.session_ticket->length;
298
0
        s->session->ext.tick = OPENSSL_malloc(ticklen);
299
0
        if (s->session->ext.tick == NULL) {
300
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
301
0
            return EXT_RETURN_FAIL;
302
0
        }
303
0
        memcpy(s->session->ext.tick,
304
0
            s->ext.session_ticket->data, ticklen);
305
0
        s->session->ext.ticklen = ticklen;
306
117k
    } else {
307
117k
        ticklen = 0;
308
117k
    }
309
310
117k
    if (ticklen == 0 && s->ext.session_ticket != NULL && s->ext.session_ticket->data == NULL)
311
0
        return EXT_RETURN_NOT_SENT;
312
313
117k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
314
117k
        || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, ticklen)) {
315
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
316
0
        return EXT_RETURN_FAIL;
317
0
    }
318
319
117k
    return EXT_RETURN_SENT;
320
117k
}
321
322
EXT_RETURN tls_construct_ctos_sig_algs(SSL_CONNECTION *s, WPACKET *pkt,
323
    unsigned int context, X509 *x,
324
    size_t chainidx)
325
52.6k
{
326
52.6k
    size_t salglen;
327
52.6k
    const uint16_t *salg;
328
329
52.6k
    if (!SSL_CLIENT_USE_SIGALGS(s))
330
310
        return EXT_RETURN_NOT_SENT;
331
332
52.3k
    salglen = tls12_get_psigalgs(s, 1, &salg);
333
52.3k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)
334
        /* Sub-packet for sig-algs extension */
335
52.3k
        || !WPACKET_start_sub_packet_u16(pkt)
336
        /* Sub-packet for the actual list */
337
52.3k
        || !WPACKET_start_sub_packet_u16(pkt)
338
52.3k
        || !tls12_copy_sigalgs(s, pkt, salg, salglen)
339
52.3k
        || !WPACKET_close(pkt)
340
52.3k
        || !WPACKET_close(pkt)) {
341
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
342
0
        return EXT_RETURN_FAIL;
343
0
    }
344
345
52.3k
    return EXT_RETURN_SENT;
346
52.3k
}
347
348
#ifndef OPENSSL_NO_OCSP
349
EXT_RETURN tls_construct_ctos_status_request(SSL_CONNECTION *s, WPACKET *pkt,
350
    unsigned int context, X509 *x,
351
    size_t chainidx)
352
117k
{
353
117k
    int i;
354
355
    /* This extension isn't defined for client Certificates */
356
117k
    if (x != NULL)
357
0
        return EXT_RETURN_NOT_SENT;
358
359
117k
    if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp)
360
117k
        return EXT_RETURN_NOT_SENT;
361
362
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
363
        /* Sub-packet for status request extension */
364
0
        || !WPACKET_start_sub_packet_u16(pkt)
365
0
        || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)
366
        /* Sub-packet for the ids */
367
0
        || !WPACKET_start_sub_packet_u16(pkt)) {
368
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
369
0
        return EXT_RETURN_FAIL;
370
0
    }
371
0
    for (i = 0; i < sk_OCSP_RESPID_num(s->ext.ocsp.ids); i++) {
372
0
        unsigned char *idbytes;
373
0
        OCSP_RESPID *id = sk_OCSP_RESPID_value(s->ext.ocsp.ids, i);
374
0
        int idlen = i2d_OCSP_RESPID(id, NULL);
375
376
0
        if (idlen <= 0
377
            /* Sub-packet for an individual id */
378
0
            || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)
379
0
            || i2d_OCSP_RESPID(id, &idbytes) != idlen) {
380
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
381
0
            return EXT_RETURN_FAIL;
382
0
        }
383
0
    }
384
0
    if (!WPACKET_close(pkt)
385
0
        || !WPACKET_start_sub_packet_u16(pkt)) {
386
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
387
0
        return EXT_RETURN_FAIL;
388
0
    }
389
0
    if (s->ext.ocsp.exts) {
390
0
        unsigned char *extbytes;
391
0
        int extlen = i2d_X509_EXTENSIONS(s->ext.ocsp.exts, NULL);
392
393
0
        if (extlen < 0) {
394
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
395
0
            return EXT_RETURN_FAIL;
396
0
        }
397
0
        if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)
398
0
            || i2d_X509_EXTENSIONS(s->ext.ocsp.exts, &extbytes)
399
0
                != extlen) {
400
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
401
0
            return EXT_RETURN_FAIL;
402
0
        }
403
0
    }
404
0
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
405
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
406
0
        return EXT_RETURN_FAIL;
407
0
    }
408
409
0
    return EXT_RETURN_SENT;
410
0
}
411
#endif
412
413
#ifndef OPENSSL_NO_NEXTPROTONEG
414
EXT_RETURN tls_construct_ctos_npn(SSL_CONNECTION *s, WPACKET *pkt,
415
    unsigned int context,
416
    X509 *x, size_t chainidx)
417
117k
{
418
117k
    if (SSL_CONNECTION_GET_CTX(s)->ext.npn_select_cb == NULL
419
0
        || !SSL_IS_FIRST_HANDSHAKE(s))
420
117k
        return EXT_RETURN_NOT_SENT;
421
422
    /*
423
     * The client advertises an empty extension to indicate its support
424
     * for Next Protocol Negotiation
425
     */
426
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
427
0
        || !WPACKET_put_bytes_u16(pkt, 0)) {
428
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
429
0
        return EXT_RETURN_FAIL;
430
0
    }
431
432
0
    return EXT_RETURN_SENT;
433
0
}
434
#endif
435
436
EXT_RETURN tls_construct_ctos_alpn(SSL_CONNECTION *s, WPACKET *pkt,
437
    unsigned int context,
438
    X509 *x, size_t chainidx)
439
117k
{
440
117k
    s->s3.alpn_sent = 0;
441
442
117k
    if (s->ext.alpn == NULL || !SSL_IS_FIRST_HANDSHAKE(s))
443
67.3k
        return EXT_RETURN_NOT_SENT;
444
445
50.4k
    if (!WPACKET_put_bytes_u16(pkt,
446
50.4k
            TLSEXT_TYPE_application_layer_protocol_negotiation)
447
        /* Sub-packet ALPN extension */
448
50.4k
        || !WPACKET_start_sub_packet_u16(pkt)
449
50.4k
        || !WPACKET_sub_memcpy_u16(pkt, s->ext.alpn, s->ext.alpn_len)
450
50.4k
        || !WPACKET_close(pkt)) {
451
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
452
0
        return EXT_RETURN_FAIL;
453
0
    }
454
50.4k
    s->s3.alpn_sent = 1;
455
456
50.4k
    return EXT_RETURN_SENT;
457
50.4k
}
458
459
#ifndef OPENSSL_NO_SRTP
460
EXT_RETURN tls_construct_ctos_use_srtp(SSL_CONNECTION *s, WPACKET *pkt,
461
    unsigned int context, X509 *x,
462
    size_t chainidx)
463
117k
{
464
117k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
465
117k
    STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(ssl);
466
117k
    int i, end;
467
468
117k
    if (clnt == NULL)
469
117k
        return EXT_RETURN_NOT_SENT;
470
471
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
472
        /* Sub-packet for SRTP extension */
473
0
        || !WPACKET_start_sub_packet_u16(pkt)
474
        /* Sub-packet for the protection profile list */
475
0
        || !WPACKET_start_sub_packet_u16(pkt)) {
476
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
477
0
        return EXT_RETURN_FAIL;
478
0
    }
479
480
0
    end = sk_SRTP_PROTECTION_PROFILE_num(clnt);
481
0
    for (i = 0; i < end; i++) {
482
0
        const SRTP_PROTECTION_PROFILE *prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
483
484
0
        if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {
485
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
486
0
            return EXT_RETURN_FAIL;
487
0
        }
488
0
    }
489
0
    if (!WPACKET_close(pkt)
490
        /* Add an empty use_mki value */
491
0
        || !WPACKET_put_bytes_u8(pkt, 0)
492
0
        || !WPACKET_close(pkt)) {
493
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
494
0
        return EXT_RETURN_FAIL;
495
0
    }
496
497
0
    return EXT_RETURN_SENT;
498
0
}
499
#endif
500
501
EXT_RETURN tls_construct_ctos_etm(SSL_CONNECTION *s, WPACKET *pkt,
502
    unsigned int context,
503
    X509 *x, size_t chainidx)
504
117k
{
505
117k
    if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
506
0
        return EXT_RETURN_NOT_SENT;
507
508
117k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
509
117k
        || !WPACKET_put_bytes_u16(pkt, 0)) {
510
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
511
0
        return EXT_RETURN_FAIL;
512
0
    }
513
514
117k
    return EXT_RETURN_SENT;
515
117k
}
516
517
#ifndef OPENSSL_NO_CT
518
EXT_RETURN tls_construct_ctos_sct(SSL_CONNECTION *s, WPACKET *pkt,
519
    unsigned int context,
520
    X509 *x, size_t chainidx)
521
117k
{
522
117k
    if (s->ct_validation_callback == NULL)
523
117k
        return EXT_RETURN_NOT_SENT;
524
525
    /* Not defined for client Certificates */
526
0
    if (x != NULL)
527
0
        return EXT_RETURN_NOT_SENT;
528
529
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)
530
0
        || !WPACKET_put_bytes_u16(pkt, 0)) {
531
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
532
0
        return EXT_RETURN_FAIL;
533
0
    }
534
535
0
    return EXT_RETURN_SENT;
536
0
}
537
#endif
538
539
EXT_RETURN tls_construct_ctos_ems(SSL_CONNECTION *s, WPACKET *pkt,
540
    unsigned int context,
541
    X509 *x, size_t chainidx)
542
117k
{
543
117k
    if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)
544
0
        return EXT_RETURN_NOT_SENT;
545
546
117k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
547
117k
        || !WPACKET_put_bytes_u16(pkt, 0)) {
548
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
549
0
        return EXT_RETURN_FAIL;
550
0
    }
551
552
117k
    return EXT_RETURN_SENT;
553
117k
}
554
555
EXT_RETURN tls_construct_ctos_supported_versions(SSL_CONNECTION *s, WPACKET *pkt,
556
    unsigned int context, X509 *x,
557
    size_t chainidx)
558
91.6k
{
559
91.6k
    int currv, min_version, max_version, reason;
560
561
91.6k
    reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
562
91.6k
    if (reason != 0) {
563
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
564
0
        return EXT_RETURN_FAIL;
565
0
    }
566
567
    /*
568
     * Don't include this if we can't negotiate TLSv1.3. We can do a straight
569
     * comparison here because we will never be called in DTLS.
570
     */
571
91.6k
    if (max_version < TLS1_3_VERSION)
572
1.23k
        return EXT_RETURN_NOT_SENT;
573
574
90.4k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
575
90.4k
        || !WPACKET_start_sub_packet_u16(pkt)
576
90.4k
        || !WPACKET_start_sub_packet_u8(pkt)) {
577
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
578
0
        return EXT_RETURN_FAIL;
579
0
    }
580
581
300k
    for (currv = max_version; currv >= min_version; currv--) {
582
210k
        if (!WPACKET_put_bytes_u16(pkt, currv)) {
583
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
584
0
            return EXT_RETURN_FAIL;
585
0
        }
586
210k
    }
587
90.4k
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
588
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
589
0
        return EXT_RETURN_FAIL;
590
0
    }
591
592
90.4k
    return EXT_RETURN_SENT;
593
90.4k
}
594
595
/*
596
 * Construct a psk_kex_modes extension.
597
 */
598
EXT_RETURN tls_construct_ctos_psk_kex_modes(SSL_CONNECTION *s, WPACKET *pkt,
599
    unsigned int context, X509 *x,
600
    size_t chainidx)
601
90.4k
{
602
90.4k
#ifndef OPENSSL_NO_TLS1_3
603
90.4k
    int nodhe = s->options & SSL_OP_ALLOW_NO_DHE_KEX;
604
605
90.4k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk_kex_modes)
606
90.4k
        || !WPACKET_start_sub_packet_u16(pkt)
607
90.4k
        || !WPACKET_start_sub_packet_u8(pkt)
608
90.4k
        || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE_DHE)
609
90.4k
        || (nodhe && !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE))
610
90.4k
        || !WPACKET_close(pkt)
611
90.4k
        || !WPACKET_close(pkt)) {
612
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
613
0
        return EXT_RETURN_FAIL;
614
0
    }
615
616
90.4k
    s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE_DHE;
617
90.4k
    if (nodhe)
618
0
        s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
619
90.4k
#endif
620
621
90.4k
    return EXT_RETURN_SENT;
622
90.4k
}
623
624
#ifndef OPENSSL_NO_TLS1_3
625
static int add_key_share(SSL_CONNECTION *s, WPACKET *pkt, unsigned int curve_id)
626
40.9k
{
627
40.9k
    unsigned char *encoded_point = NULL;
628
40.9k
    EVP_PKEY *key_share_key = NULL;
629
40.9k
    size_t encodedlen;
630
631
40.9k
    if (s->s3.tmp.pkey != NULL) {
632
9
        if (!ossl_assert(s->hello_retry_request == SSL_HRR_PENDING)) {
633
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
634
0
            return 0;
635
0
        }
636
        /*
637
         * Could happen if we got an HRR that wasn't requesting a new key_share
638
         */
639
9
        key_share_key = s->s3.tmp.pkey;
640
40.9k
    } else {
641
40.9k
        key_share_key = ssl_generate_pkey_group(s, curve_id);
642
40.9k
        if (key_share_key == NULL) {
643
            /* SSLfatal() already called */
644
0
            return 0;
645
0
        }
646
40.9k
    }
647
648
    /* Encode the public key. */
649
40.9k
    encodedlen = EVP_PKEY_get1_encoded_public_key(key_share_key,
650
40.9k
        &encoded_point);
651
40.9k
    if (encodedlen == 0) {
652
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
653
0
        goto err;
654
0
    }
655
656
    /* Create KeyShareEntry */
657
40.9k
    if (!WPACKET_put_bytes_u16(pkt, curve_id)
658
40.9k
        || !WPACKET_sub_memcpy_u16(pkt, encoded_point, encodedlen)) {
659
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
660
0
        goto err;
661
0
    }
662
663
    /*
664
     * When changing to send more than one key_share we're
665
     * going to need to be able to save more than one EVP_PKEY. For now
666
     * we reuse the existing tmp.pkey
667
     */
668
40.9k
    s->s3.tmp.pkey = key_share_key;
669
40.9k
    s->s3.group_id = curve_id;
670
40.9k
    OPENSSL_free(encoded_point);
671
672
40.9k
    return 1;
673
0
err:
674
0
    if (s->s3.tmp.pkey == NULL)
675
0
        EVP_PKEY_free(key_share_key);
676
0
    OPENSSL_free(encoded_point);
677
0
    return 0;
678
40.9k
}
679
#endif
680
681
EXT_RETURN tls_construct_ctos_key_share(SSL_CONNECTION *s, WPACKET *pkt,
682
    unsigned int context, X509 *x,
683
    size_t chainidx)
684
40.9k
{
685
40.9k
#ifndef OPENSSL_NO_TLS1_3
686
40.9k
    size_t i, num_groups = 0;
687
40.9k
    const uint16_t *pgroups = NULL;
688
40.9k
    uint16_t curve_id = 0;
689
690
    /* key_share extension */
691
40.9k
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
692
        /* Extension data sub-packet */
693
40.9k
        || !WPACKET_start_sub_packet_u16(pkt)
694
        /* KeyShare list sub-packet */
695
40.9k
        || !WPACKET_start_sub_packet_u16(pkt)) {
696
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
697
0
        return EXT_RETURN_FAIL;
698
0
    }
699
700
40.9k
    tls1_get_supported_groups(s, &pgroups, &num_groups);
701
702
    /*
703
     * Make the number of key_shares sent configurable. For
704
     * now, we just send one
705
     */
706
40.9k
    if (s->s3.group_id != 0) {
707
245
        curve_id = s->s3.group_id;
708
40.6k
    } else {
709
40.6k
        for (i = 0; i < num_groups; i++) {
710
40.6k
            if (!tls_group_allowed(s, pgroups[i], SSL_SECOP_CURVE_SUPPORTED))
711
0
                continue;
712
713
40.6k
            if (!tls_valid_group(s, pgroups[i], TLS1_3_VERSION, TLS1_3_VERSION,
714
40.6k
                    0, NULL))
715
0
                continue;
716
717
40.6k
            curve_id = pgroups[i];
718
40.6k
            break;
719
40.6k
        }
720
40.6k
    }
721
722
40.9k
    if (curve_id == 0) {
723
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE);
724
0
        return EXT_RETURN_FAIL;
725
0
    }
726
727
40.9k
    if (!add_key_share(s, pkt, curve_id)) {
728
        /* SSLfatal() already called */
729
0
        return EXT_RETURN_FAIL;
730
0
    }
731
732
40.9k
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
733
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
734
0
        return EXT_RETURN_FAIL;
735
0
    }
736
40.9k
    return EXT_RETURN_SENT;
737
#else
738
    return EXT_RETURN_NOT_SENT;
739
#endif
740
40.9k
}
741
742
EXT_RETURN tls_construct_ctos_cookie(SSL_CONNECTION *s, WPACKET *pkt,
743
    unsigned int context,
744
    X509 *x, size_t chainidx)
745
90.4k
{
746
90.4k
    EXT_RETURN ret = EXT_RETURN_FAIL;
747
748
    /* Should only be set if we've had an HRR */
749
90.4k
    if (s->ext.tls13_cookie_len == 0)
750
90.3k
        return EXT_RETURN_NOT_SENT;
751
752
31
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
753
        /* Extension data sub-packet */
754
31
        || !WPACKET_start_sub_packet_u16(pkt)
755
31
        || !WPACKET_sub_memcpy_u16(pkt, s->ext.tls13_cookie,
756
31
            s->ext.tls13_cookie_len)
757
31
        || !WPACKET_close(pkt)) {
758
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
759
0
        goto end;
760
0
    }
761
762
31
    ret = EXT_RETURN_SENT;
763
31
end:
764
31
    OPENSSL_free(s->ext.tls13_cookie);
765
31
    s->ext.tls13_cookie = NULL;
766
31
    s->ext.tls13_cookie_len = 0;
767
768
31
    return ret;
769
31
}
770
771
EXT_RETURN tls_construct_ctos_early_data(SSL_CONNECTION *s, WPACKET *pkt,
772
    unsigned int context, X509 *x,
773
    size_t chainidx)
774
90.4k
{
775
90.4k
#ifndef OPENSSL_NO_PSK
776
90.4k
    char identity[PSK_MAX_IDENTITY_LEN + 1];
777
90.4k
#endif /* OPENSSL_NO_PSK */
778
90.4k
    const unsigned char *id = NULL;
779
90.4k
    size_t idlen = 0;
780
90.4k
    SSL_SESSION *psksess = NULL;
781
90.4k
    SSL_SESSION *edsess = NULL;
782
90.4k
    const EVP_MD *handmd = NULL;
783
90.4k
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
784
785
90.4k
    if (s->hello_retry_request == SSL_HRR_PENDING)
786
446
        handmd = ssl_handshake_md(s);
787
788
90.4k
    if (s->psk_use_session_cb != NULL
789
0
        && (!s->psk_use_session_cb(ussl, handmd, &id, &idlen, &psksess)
790
0
            || (psksess != NULL
791
0
                && psksess->ssl_version != TLS1_3_VERSION))) {
792
0
        SSL_SESSION_free(psksess);
793
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
794
0
        return EXT_RETURN_FAIL;
795
0
    }
796
797
90.4k
#ifndef OPENSSL_NO_PSK
798
90.4k
    if (psksess == NULL && s->psk_client_callback != NULL) {
799
0
        unsigned char psk[PSK_MAX_PSK_LEN];
800
0
        size_t psklen = 0;
801
802
0
        memset(identity, 0, sizeof(identity));
803
0
        psklen = s->psk_client_callback(ussl, NULL,
804
0
            identity, sizeof(identity) - 1,
805
0
            psk, sizeof(psk));
806
807
0
        if (psklen > PSK_MAX_PSK_LEN) {
808
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR);
809
0
            return EXT_RETURN_FAIL;
810
0
        } else if (psklen > 0) {
811
0
            const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
812
0
            const SSL_CIPHER *cipher;
813
814
0
            idlen = strlen(identity);
815
0
            if (idlen > PSK_MAX_IDENTITY_LEN) {
816
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
817
0
                return EXT_RETURN_FAIL;
818
0
            }
819
0
            id = (unsigned char *)identity;
820
821
            /*
822
             * We found a PSK using an old style callback. We don't know
823
             * the digest so we default to SHA256 as per the TLSv1.3 spec
824
             */
825
0
            cipher = SSL_CIPHER_find(SSL_CONNECTION_GET_SSL(s),
826
0
                tls13_aes128gcmsha256_id);
827
0
            if (cipher == NULL) {
828
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
829
0
                return EXT_RETURN_FAIL;
830
0
            }
831
832
0
            psksess = SSL_SESSION_new();
833
0
            if (psksess == NULL
834
0
                || !SSL_SESSION_set1_master_key(psksess, psk, psklen)
835
0
                || !SSL_SESSION_set_cipher(psksess, cipher)
836
0
                || !SSL_SESSION_set_protocol_version(psksess, TLS1_3_VERSION)) {
837
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
838
0
                OPENSSL_cleanse(psk, psklen);
839
0
                return EXT_RETURN_FAIL;
840
0
            }
841
0
            OPENSSL_cleanse(psk, psklen);
842
0
        }
843
0
    }
844
90.4k
#endif /* OPENSSL_NO_PSK */
845
846
90.4k
    SSL_SESSION_free(s->psksession);
847
90.4k
    s->psksession = psksess;
848
90.4k
    if (psksess != NULL) {
849
0
        OPENSSL_free(s->psksession_id);
850
0
        s->psksession_id = OPENSSL_memdup(id, idlen);
851
0
        if (s->psksession_id == NULL) {
852
0
            s->psksession_id_len = 0;
853
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
854
0
            return EXT_RETURN_FAIL;
855
0
        }
856
0
        s->psksession_id_len = idlen;
857
0
    }
858
859
90.4k
    if (s->early_data_state != SSL_EARLY_DATA_CONNECTING
860
0
        || (s->session->ext.max_early_data == 0
861
90.4k
            && (psksess == NULL || psksess->ext.max_early_data == 0))) {
862
90.4k
        s->max_early_data = 0;
863
90.4k
        return EXT_RETURN_NOT_SENT;
864
90.4k
    }
865
0
    edsess = s->session->ext.max_early_data != 0 ? s->session : psksess;
866
0
    s->max_early_data = edsess->ext.max_early_data;
867
868
0
    if (edsess->ext.hostname != NULL) {
869
0
        if (s->ext.hostname == NULL
870
0
            || (s->ext.hostname != NULL
871
0
                && strcmp(s->ext.hostname, edsess->ext.hostname) != 0)) {
872
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
873
0
                SSL_R_INCONSISTENT_EARLY_DATA_SNI);
874
0
            return EXT_RETURN_FAIL;
875
0
        }
876
0
    }
877
878
0
    if ((s->ext.alpn == NULL && edsess->ext.alpn_selected != NULL)) {
879
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_INCONSISTENT_EARLY_DATA_ALPN);
880
0
        return EXT_RETURN_FAIL;
881
0
    }
882
883
    /*
884
     * Verify that we are offering an ALPN protocol consistent with the early
885
     * data.
886
     */
887
0
    if (edsess->ext.alpn_selected != NULL) {
888
0
        PACKET prots, alpnpkt;
889
0
        int found = 0;
890
891
0
        if (!PACKET_buf_init(&prots, s->ext.alpn, s->ext.alpn_len)) {
892
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
893
0
            return EXT_RETURN_FAIL;
894
0
        }
895
0
        while (PACKET_get_length_prefixed_1(&prots, &alpnpkt)) {
896
0
            if (PACKET_equal(&alpnpkt, edsess->ext.alpn_selected,
897
0
                    edsess->ext.alpn_selected_len)) {
898
0
                found = 1;
899
0
                break;
900
0
            }
901
0
        }
902
0
        if (!found) {
903
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
904
0
                SSL_R_INCONSISTENT_EARLY_DATA_ALPN);
905
0
            return EXT_RETURN_FAIL;
906
0
        }
907
0
    }
908
909
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
910
0
        || !WPACKET_start_sub_packet_u16(pkt)
911
0
        || !WPACKET_close(pkt)) {
912
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
913
0
        return EXT_RETURN_FAIL;
914
0
    }
915
916
    /*
917
     * We set this to rejected here. Later, if the server acknowledges the
918
     * extension, we set it to accepted.
919
     */
920
0
    s->ext.early_data = SSL_EARLY_DATA_REJECTED;
921
0
    s->ext.early_data_ok = 1;
922
923
0
    return EXT_RETURN_SENT;
924
0
}
925
926
0
#define F5_WORKAROUND_MIN_MSG_LEN 0xff
927
0
#define F5_WORKAROUND_MAX_MSG_LEN 0x200
928
929
/*
930
 * PSK pre binder overhead =
931
 *  2 bytes for TLSEXT_TYPE_psk
932
 *  2 bytes for extension length
933
 *  2 bytes for identities list length
934
 *  2 bytes for identity length
935
 *  4 bytes for obfuscated_ticket_age
936
 *  2 bytes for binder list length
937
 *  1 byte for binder length
938
 * The above excludes the number of bytes for the identity itself and the
939
 * subsequent binder bytes
940
 */
941
0
#define PSK_PRE_BINDER_OVERHEAD (2 + 2 + 2 + 2 + 4 + 2 + 1)
942
943
EXT_RETURN tls_construct_ctos_padding(SSL_CONNECTION *s, WPACKET *pkt,
944
    unsigned int context, X509 *x,
945
    size_t chainidx)
946
88.1k
{
947
88.1k
    unsigned char *padbytes;
948
88.1k
    size_t hlen;
949
950
88.1k
    if ((s->options & SSL_OP_TLSEXT_PADDING) == 0)
951
88.1k
        return EXT_RETURN_NOT_SENT;
952
953
    /*
954
     * Add padding to workaround bugs in F5 terminators. See RFC7685.
955
     * This code calculates the length of all extensions added so far but
956
     * excludes the PSK extension (because that MUST be written last). Therefore
957
     * this extension MUST always appear second to last.
958
     */
959
0
    if (!WPACKET_get_total_written(pkt, &hlen)) {
960
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
961
0
        return EXT_RETURN_FAIL;
962
0
    }
963
964
    /*
965
     * If we're going to send a PSK then that will be written out after this
966
     * extension, so we need to calculate how long it is going to be.
967
     */
968
0
    if (s->session->ssl_version == TLS1_3_VERSION
969
0
        && s->session->ext.ticklen != 0
970
0
        && s->session->cipher != NULL) {
971
0
        const EVP_MD *md = ssl_md(SSL_CONNECTION_GET_CTX(s),
972
0
            s->session->cipher->algorithm2);
973
974
0
        if (md != NULL) {
975
            /*
976
             * Add the fixed PSK overhead, the identity length and the binder
977
             * length.
978
             */
979
0
            int md_size = EVP_MD_get_size(md);
980
981
0
            if (md_size <= 0)
982
0
                return EXT_RETURN_FAIL;
983
0
            hlen += PSK_PRE_BINDER_OVERHEAD + s->session->ext.ticklen
984
0
                + md_size;
985
0
        }
986
0
    }
987
988
0
    if (hlen > F5_WORKAROUND_MIN_MSG_LEN && hlen < F5_WORKAROUND_MAX_MSG_LEN) {
989
        /* Calculate the amount of padding we need to add */
990
0
        hlen = F5_WORKAROUND_MAX_MSG_LEN - hlen;
991
992
        /*
993
         * Take off the size of extension header itself (2 bytes for type and
994
         * 2 bytes for length bytes), but ensure that the extension is at least
995
         * 1 byte long so as not to have an empty extension last (WebSphere 7.x,
996
         * 8.x are intolerant of that condition)
997
         */
998
0
        if (hlen > 4)
999
0
            hlen -= 4;
1000
0
        else
1001
0
            hlen = 1;
1002
1003
0
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)
1004
0
            || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {
1005
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1006
0
            return EXT_RETURN_FAIL;
1007
0
        }
1008
0
        memset(padbytes, 0, hlen);
1009
0
    }
1010
1011
0
    return EXT_RETURN_SENT;
1012
0
}
1013
1014
/*
1015
 * Construct the pre_shared_key extension
1016
 */
1017
EXT_RETURN tls_construct_ctos_psk(SSL_CONNECTION *s, WPACKET *pkt,
1018
    unsigned int context,
1019
    X509 *x, size_t chainidx)
1020
66.4k
{
1021
66.4k
#ifndef OPENSSL_NO_TLS1_3
1022
66.4k
    uint32_t agesec, agems = 0;
1023
66.4k
    size_t binderoffset, msglen;
1024
66.4k
    int reshashsize = 0, pskhashsize = 0;
1025
66.4k
    unsigned char *resbinder = NULL, *pskbinder = NULL, *msgstart = NULL;
1026
66.4k
    const EVP_MD *handmd = NULL, *mdres = NULL, *mdpsk = NULL;
1027
66.4k
    int dores = 0;
1028
66.4k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1029
66.4k
    OSSL_TIME t;
1030
1031
66.4k
    s->ext.tick_identity = 0;
1032
1033
    /*
1034
     * Note: At this stage of the code we only support adding a single
1035
     * resumption PSK. If we add support for multiple PSKs then the length
1036
     * calculations in the padding extension will need to be adjusted.
1037
     */
1038
1039
    /*
1040
     * If this is an incompatible or new session then we have nothing to resume
1041
     * so don't add this extension.
1042
     */
1043
66.4k
    if (s->session->ssl_version != TLS1_3_VERSION
1044
66.4k
        || (s->session->ext.ticklen == 0 && s->psksession == NULL))
1045
66.4k
        return EXT_RETURN_NOT_SENT;
1046
1047
0
    if (s->hello_retry_request == SSL_HRR_PENDING)
1048
0
        handmd = ssl_handshake_md(s);
1049
1050
0
    if (s->session->ext.ticklen != 0) {
1051
        /* Get the digest associated with the ciphersuite in the session */
1052
0
        if (s->session->cipher == NULL) {
1053
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1054
0
            return EXT_RETURN_FAIL;
1055
0
        }
1056
0
        mdres = ssl_md(sctx, s->session->cipher->algorithm2);
1057
0
        if (mdres == NULL) {
1058
            /*
1059
             * Don't recognize this cipher so we can't use the session.
1060
             * Ignore it
1061
             */
1062
0
            goto dopsksess;
1063
0
        }
1064
1065
0
        if (s->hello_retry_request == SSL_HRR_PENDING && mdres != handmd) {
1066
            /*
1067
             * Selected ciphersuite hash does not match the hash for the session
1068
             * so we can't use it.
1069
             */
1070
0
            goto dopsksess;
1071
0
        }
1072
1073
        /*
1074
         * Technically the C standard just says time() returns a time_t and says
1075
         * nothing about the encoding of that type. In practice most
1076
         * implementations follow POSIX which holds it as an integral type in
1077
         * seconds since epoch. We've already made the assumption that we can do
1078
         * this in multiple places in the code, so portability shouldn't be an
1079
         * issue.
1080
         */
1081
0
        t = ossl_time_subtract(ossl_time_now(), s->session->time);
1082
0
        agesec = (uint32_t)ossl_time2seconds(t);
1083
        /*
1084
         * We calculate the age in seconds but the server may work in ms. Due to
1085
         * rounding errors we could overestimate the age by up to 1s. It is
1086
         * better to underestimate it. Otherwise, if the RTT is very short, when
1087
         * the server calculates the age reported by the client it could be
1088
         * bigger than the age calculated on the server - which should never
1089
         * happen.
1090
         */
1091
0
        if (agesec > 0)
1092
0
            agesec--;
1093
1094
0
        if (s->session->ext.tick_lifetime_hint < agesec) {
1095
            /* Ticket is too old. Ignore it. */
1096
0
            goto dopsksess;
1097
0
        }
1098
1099
        /*
1100
         * Calculate age in ms. We're just doing it to nearest second. Should be
1101
         * good enough.
1102
         */
1103
0
        agems = agesec * (uint32_t)1000;
1104
1105
0
        if (agesec != 0 && agems / (uint32_t)1000 != agesec) {
1106
            /*
1107
             * Overflow. Shouldn't happen unless this is a *really* old session.
1108
             * If so we just ignore it.
1109
             */
1110
0
            goto dopsksess;
1111
0
        }
1112
1113
        /*
1114
         * Obfuscate the age. Overflow here is fine, this addition is supposed
1115
         * to be mod 2^32.
1116
         */
1117
0
        agems += s->session->ext.tick_age_add;
1118
1119
0
        reshashsize = EVP_MD_get_size(mdres);
1120
0
        if (reshashsize <= 0)
1121
0
            goto dopsksess;
1122
0
        s->ext.tick_identity++;
1123
0
        dores = 1;
1124
0
    }
1125
1126
0
dopsksess:
1127
0
    if (!dores && s->psksession == NULL)
1128
0
        return EXT_RETURN_NOT_SENT;
1129
1130
0
    if (s->psksession != NULL) {
1131
0
        mdpsk = ssl_md(sctx, s->psksession->cipher->algorithm2);
1132
0
        if (mdpsk == NULL) {
1133
            /*
1134
             * Don't recognize this cipher so we can't use the session.
1135
             * If this happens it's an application bug.
1136
             */
1137
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
1138
0
            return EXT_RETURN_FAIL;
1139
0
        }
1140
1141
0
        if (s->hello_retry_request == SSL_HRR_PENDING && mdpsk != handmd) {
1142
            /*
1143
             * Selected ciphersuite hash does not match the hash for the PSK
1144
             * session. This is an application bug.
1145
             */
1146
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
1147
0
            return EXT_RETURN_FAIL;
1148
0
        }
1149
1150
0
        pskhashsize = EVP_MD_get_size(mdpsk);
1151
0
        if (pskhashsize <= 0) {
1152
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
1153
0
            return EXT_RETURN_FAIL;
1154
0
        }
1155
0
    }
1156
1157
    /* Create the extension, but skip over the binder for now */
1158
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
1159
0
        || !WPACKET_start_sub_packet_u16(pkt)
1160
0
        || !WPACKET_start_sub_packet_u16(pkt)) {
1161
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1162
0
        return EXT_RETURN_FAIL;
1163
0
    }
1164
1165
0
    if (dores) {
1166
0
        if (!WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick,
1167
0
                s->session->ext.ticklen)
1168
0
            || !WPACKET_put_bytes_u32(pkt, agems)) {
1169
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1170
0
            return EXT_RETURN_FAIL;
1171
0
        }
1172
0
    }
1173
1174
0
    if (s->psksession != NULL) {
1175
0
        if (!WPACKET_sub_memcpy_u16(pkt, s->psksession_id,
1176
0
                s->psksession_id_len)
1177
0
            || !WPACKET_put_bytes_u32(pkt, 0)) {
1178
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1179
0
            return EXT_RETURN_FAIL;
1180
0
        }
1181
0
        s->ext.tick_identity++;
1182
0
    }
1183
1184
0
    if (!WPACKET_close(pkt)
1185
0
        || !WPACKET_get_total_written(pkt, &binderoffset)
1186
0
        || !WPACKET_start_sub_packet_u16(pkt)
1187
0
        || (dores
1188
0
            && !WPACKET_sub_allocate_bytes_u8(pkt, reshashsize, &resbinder))
1189
0
        || (s->psksession != NULL
1190
0
            && !WPACKET_sub_allocate_bytes_u8(pkt, pskhashsize, &pskbinder))
1191
0
        || !WPACKET_close(pkt)
1192
0
        || !WPACKET_close(pkt)
1193
0
        || !WPACKET_get_total_written(pkt, &msglen)
1194
        /*
1195
         * We need to fill in all the sub-packet lengths now so we can
1196
         * calculate the HMAC of the message up to the binders
1197
         */
1198
0
        || !WPACKET_fill_lengths(pkt)) {
1199
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1200
0
        return EXT_RETURN_FAIL;
1201
0
    }
1202
1203
0
    msgstart = WPACKET_get_curr(pkt) - msglen;
1204
1205
0
    if (dores
1206
0
        && tls_psk_do_binder(s, mdres, msgstart, binderoffset, NULL,
1207
0
               resbinder, s->session, 1, 0)
1208
0
            != 1) {
1209
        /* SSLfatal() already called */
1210
0
        return EXT_RETURN_FAIL;
1211
0
    }
1212
1213
0
    if (s->psksession != NULL
1214
0
        && tls_psk_do_binder(s, mdpsk, msgstart, binderoffset, NULL,
1215
0
               pskbinder, s->psksession, 1, 1)
1216
0
            != 1) {
1217
        /* SSLfatal() already called */
1218
0
        return EXT_RETURN_FAIL;
1219
0
    }
1220
1221
0
    return EXT_RETURN_SENT;
1222
#else
1223
    return EXT_RETURN_NOT_SENT;
1224
#endif
1225
0
}
1226
1227
EXT_RETURN tls_construct_ctos_post_handshake_auth(SSL_CONNECTION *s, WPACKET *pkt,
1228
    ossl_unused unsigned int context,
1229
    ossl_unused X509 *x,
1230
    ossl_unused size_t chainidx)
1231
90.4k
{
1232
90.4k
#ifndef OPENSSL_NO_TLS1_3
1233
90.4k
    if (!s->pha_enabled)
1234
90.4k
        return EXT_RETURN_NOT_SENT;
1235
1236
    /* construct extension - 0 length, no contents */
1237
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_post_handshake_auth)
1238
0
        || !WPACKET_start_sub_packet_u16(pkt)
1239
0
        || !WPACKET_close(pkt)) {
1240
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1241
0
        return EXT_RETURN_FAIL;
1242
0
    }
1243
1244
0
    s->post_handshake_auth = SSL_PHA_EXT_SENT;
1245
1246
0
    return EXT_RETURN_SENT;
1247
#else
1248
    return EXT_RETURN_NOT_SENT;
1249
#endif
1250
0
}
1251
1252
/*
1253
 * Parse the server's renegotiation binding and abort if it's not right
1254
 */
1255
int tls_parse_stoc_renegotiate(SSL_CONNECTION *s, PACKET *pkt,
1256
    unsigned int context,
1257
    X509 *x, size_t chainidx)
1258
46.6k
{
1259
46.6k
    size_t expected_len = s->s3.previous_client_finished_len
1260
46.6k
        + s->s3.previous_server_finished_len;
1261
46.6k
    size_t ilen;
1262
46.6k
    const unsigned char *data;
1263
1264
    /* Check for logic errors */
1265
46.6k
    if (!ossl_assert(expected_len == 0
1266
46.6k
            || s->s3.previous_client_finished_len != 0)
1267
46.6k
        || !ossl_assert(expected_len == 0
1268
46.6k
            || s->s3.previous_server_finished_len != 0)) {
1269
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1270
0
        return 0;
1271
0
    }
1272
1273
    /* Parse the length byte */
1274
46.6k
    if (!PACKET_get_1_len(pkt, &ilen)) {
1275
12
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);
1276
12
        return 0;
1277
12
    }
1278
1279
    /* Consistency check */
1280
46.6k
    if (PACKET_remaining(pkt) != ilen) {
1281
86
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);
1282
86
        return 0;
1283
86
    }
1284
1285
    /* Check that the extension matches */
1286
46.5k
    if (ilen != expected_len) {
1287
32
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);
1288
32
        return 0;
1289
32
    }
1290
1291
46.5k
    if (!PACKET_get_bytes(pkt, &data, s->s3.previous_client_finished_len)
1292
46.5k
        || memcmp(data, s->s3.previous_client_finished,
1293
46.5k
               s->s3.previous_client_finished_len)
1294
46.5k
            != 0) {
1295
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);
1296
0
        return 0;
1297
0
    }
1298
1299
46.5k
    if (!PACKET_get_bytes(pkt, &data, s->s3.previous_server_finished_len)
1300
46.5k
        || memcmp(data, s->s3.previous_server_finished,
1301
46.5k
               s->s3.previous_server_finished_len)
1302
46.5k
            != 0) {
1303
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH);
1304
0
        return 0;
1305
0
    }
1306
46.5k
    s->s3.send_connection_binding = 1;
1307
1308
46.5k
    return 1;
1309
46.5k
}
1310
1311
/* Parse the server's max fragment len extension packet */
1312
int tls_parse_stoc_maxfragmentlen(SSL_CONNECTION *s, PACKET *pkt,
1313
    unsigned int context,
1314
    X509 *x, size_t chainidx)
1315
0
{
1316
0
    unsigned int value;
1317
1318
0
    if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
1319
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1320
0
        return 0;
1321
0
    }
1322
1323
    /* |value| should contains a valid max-fragment-length code. */
1324
0
    if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
1325
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1326
0
            SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
1327
0
        return 0;
1328
0
    }
1329
1330
    /* Must be the same value as client-configured one who was sent to server */
1331
    /*-
1332
     * RFC 6066: if a client receives a maximum fragment length negotiation
1333
     * response that differs from the length it requested, ...
1334
     * It must abort with SSL_AD_ILLEGAL_PARAMETER alert
1335
     */
1336
0
    if (value != s->ext.max_fragment_len_mode) {
1337
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1338
0
            SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
1339
0
        return 0;
1340
0
    }
1341
1342
    /*
1343
     * Maximum Fragment Length Negotiation succeeded.
1344
     * The negotiated Maximum Fragment Length is binding now.
1345
     */
1346
0
    s->session->ext.max_fragment_len_mode = value;
1347
1348
0
    return 1;
1349
0
}
1350
1351
int tls_parse_stoc_server_name(SSL_CONNECTION *s, PACKET *pkt,
1352
    unsigned int context,
1353
    X509 *x, size_t chainidx)
1354
6.04k
{
1355
6.04k
    if (s->ext.hostname == NULL) {
1356
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1357
0
        return 0;
1358
0
    }
1359
1360
6.04k
    if (PACKET_remaining(pkt) > 0) {
1361
27
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1362
27
        return 0;
1363
27
    }
1364
1365
6.01k
    if (!s->hit) {
1366
6.01k
        if (s->session->ext.hostname != NULL) {
1367
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1368
0
            return 0;
1369
0
        }
1370
6.01k
        s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
1371
6.01k
        if (s->session->ext.hostname == NULL) {
1372
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1373
0
            return 0;
1374
0
        }
1375
6.01k
    }
1376
1377
6.01k
    return 1;
1378
6.01k
}
1379
1380
int tls_parse_stoc_ec_pt_formats(SSL_CONNECTION *s, PACKET *pkt,
1381
    unsigned int context,
1382
    X509 *x, size_t chainidx)
1383
3.36k
{
1384
3.36k
    size_t ecpointformats_len;
1385
3.36k
    PACKET ecptformatlist;
1386
1387
3.36k
    if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) {
1388
107
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1389
107
        return 0;
1390
107
    }
1391
3.26k
    if (!s->hit) {
1392
3.26k
        ecpointformats_len = PACKET_remaining(&ecptformatlist);
1393
3.26k
        if (ecpointformats_len == 0) {
1394
13
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
1395
13
            return 0;
1396
13
        }
1397
1398
3.24k
        s->ext.peer_ecpointformats_len = 0;
1399
3.24k
        OPENSSL_free(s->ext.peer_ecpointformats);
1400
3.24k
        s->ext.peer_ecpointformats = OPENSSL_malloc(ecpointformats_len);
1401
3.24k
        if (s->ext.peer_ecpointformats == NULL) {
1402
0
            s->ext.peer_ecpointformats_len = 0;
1403
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1404
0
            return 0;
1405
0
        }
1406
1407
3.24k
        s->ext.peer_ecpointformats_len = ecpointformats_len;
1408
1409
3.24k
        if (!PACKET_copy_bytes(&ecptformatlist,
1410
3.24k
                s->ext.peer_ecpointformats,
1411
3.24k
                ecpointformats_len)) {
1412
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1413
0
            return 0;
1414
0
        }
1415
3.24k
    }
1416
1417
3.24k
    return 1;
1418
3.26k
}
1419
1420
int tls_parse_stoc_session_ticket(SSL_CONNECTION *s, PACKET *pkt,
1421
    unsigned int context,
1422
    X509 *x, size_t chainidx)
1423
11.6k
{
1424
11.6k
    SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s);
1425
1426
11.6k
    if (s->ext.session_ticket_cb != NULL && !s->ext.session_ticket_cb(ssl, PACKET_data(pkt), PACKET_remaining(pkt), s->ext.session_ticket_cb_arg)) {
1427
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION);
1428
0
        return 0;
1429
0
    }
1430
1431
11.6k
    if (!tls_use_ticket(s)) {
1432
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1433
0
        return 0;
1434
0
    }
1435
11.6k
    if (PACKET_remaining(pkt) > 0) {
1436
13
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1437
13
        return 0;
1438
13
    }
1439
1440
11.5k
    s->ext.ticket_expected = 1;
1441
1442
11.5k
    return 1;
1443
11.6k
}
1444
1445
#ifndef OPENSSL_NO_OCSP
1446
int tls_parse_stoc_status_request(SSL_CONNECTION *s, PACKET *pkt,
1447
    unsigned int context,
1448
    X509 *x, size_t chainidx)
1449
3
{
1450
3
    if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
1451
        /* We ignore this if the server sends a CertificateRequest */
1452
3
        return 1;
1453
3
    }
1454
1455
    /*
1456
     * MUST only be sent if we've requested a status
1457
     * request message. In TLS <= 1.2 it must also be empty.
1458
     */
1459
0
    if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
1460
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1461
0
        return 0;
1462
0
    }
1463
0
    if (!SSL_CONNECTION_IS_TLS13(s) && PACKET_remaining(pkt) > 0) {
1464
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1465
0
        return 0;
1466
0
    }
1467
1468
0
    if (SSL_CONNECTION_IS_TLS13(s)) {
1469
        /* We only know how to handle this if it's for the first Certificate in
1470
         * the chain. We ignore any other responses.
1471
         */
1472
0
        if (chainidx != 0)
1473
0
            return 1;
1474
1475
        /* SSLfatal() already called */
1476
0
        return tls_process_cert_status_body(s, pkt);
1477
0
    }
1478
1479
    /* Set flag to expect CertificateStatus message */
1480
0
    s->ext.status_expected = 1;
1481
1482
0
    return 1;
1483
0
}
1484
#endif
1485
1486
#ifndef OPENSSL_NO_CT
1487
int tls_parse_stoc_sct(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1488
    X509 *x, size_t chainidx)
1489
26
{
1490
26
    if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
1491
        /* We ignore this if the server sends it in a CertificateRequest */
1492
5
        return 1;
1493
5
    }
1494
1495
    /*
1496
     * Only take it if we asked for it - i.e if there is no CT validation
1497
     * callback set, then a custom extension MAY be processing it, so we
1498
     * need to let control continue to flow to that.
1499
     */
1500
21
    if (s->ct_validation_callback != NULL) {
1501
0
        size_t size = PACKET_remaining(pkt);
1502
1503
        /* Simply copy it off for later processing */
1504
0
        OPENSSL_free(s->ext.scts);
1505
0
        s->ext.scts = NULL;
1506
1507
0
        s->ext.scts_len = (uint16_t)size;
1508
0
        if (size > 0) {
1509
0
            s->ext.scts = OPENSSL_malloc(size);
1510
0
            if (s->ext.scts == NULL) {
1511
0
                s->ext.scts_len = 0;
1512
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
1513
0
                return 0;
1514
0
            }
1515
0
            if (!PACKET_copy_bytes(pkt, s->ext.scts, size)) {
1516
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1517
0
                return 0;
1518
0
            }
1519
0
        }
1520
21
    } else {
1521
21
        ENDPOINT role = (context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
1522
21
            ? ENDPOINT_CLIENT
1523
21
            : ENDPOINT_BOTH;
1524
1525
        /*
1526
         * If we didn't ask for it then there must be a custom extension,
1527
         * otherwise this is unsolicited.
1528
         */
1529
21
        if (custom_ext_find(&s->cert->custext, role,
1530
21
                TLSEXT_TYPE_signed_certificate_timestamp,
1531
21
                NULL)
1532
21
            == NULL) {
1533
21
            SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1534
21
            return 0;
1535
21
        }
1536
1537
0
        if (!custom_ext_parse(s, context,
1538
0
                TLSEXT_TYPE_signed_certificate_timestamp,
1539
0
                PACKET_data(pkt), PACKET_remaining(pkt),
1540
0
                x, chainidx)) {
1541
            /* SSLfatal already called */
1542
0
            return 0;
1543
0
        }
1544
0
    }
1545
1546
0
    return 1;
1547
21
}
1548
#endif
1549
1550
#ifndef OPENSSL_NO_NEXTPROTONEG
1551
/*
1552
 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1553
 * elements of zero length are allowed and the set of elements must exactly
1554
 * fill the length of the block. Returns 1 on success or 0 on failure.
1555
 */
1556
static int ssl_next_proto_validate(SSL_CONNECTION *s, PACKET *pkt)
1557
0
{
1558
0
    PACKET tmp_protocol;
1559
1560
0
    while (PACKET_remaining(pkt)) {
1561
0
        if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
1562
0
            || PACKET_remaining(&tmp_protocol) == 0) {
1563
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1564
0
            return 0;
1565
0
        }
1566
0
    }
1567
1568
0
    return 1;
1569
0
}
1570
1571
int tls_parse_stoc_npn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1572
    X509 *x, size_t chainidx)
1573
0
{
1574
0
    unsigned char *selected;
1575
0
    unsigned char selected_len;
1576
0
    PACKET tmppkt;
1577
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1578
1579
    /* Check if we are in a renegotiation. If so ignore this extension */
1580
0
    if (!SSL_IS_FIRST_HANDSHAKE(s))
1581
0
        return 1;
1582
1583
    /* We must have requested it. */
1584
0
    if (sctx->ext.npn_select_cb == NULL) {
1585
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1586
0
        return 0;
1587
0
    }
1588
1589
    /* The data must be valid */
1590
0
    tmppkt = *pkt;
1591
0
    if (!ssl_next_proto_validate(s, &tmppkt)) {
1592
        /* SSLfatal() already called */
1593
0
        return 0;
1594
0
    }
1595
0
    if (sctx->ext.npn_select_cb(SSL_CONNECTION_GET_USER_SSL(s),
1596
0
            &selected, &selected_len,
1597
0
            PACKET_data(pkt), PACKET_remaining(pkt),
1598
0
            sctx->ext.npn_select_cb_arg)
1599
0
            != SSL_TLSEXT_ERR_OK
1600
0
        || selected_len == 0) {
1601
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION);
1602
0
        return 0;
1603
0
    }
1604
1605
    /*
1606
     * Could be non-NULL if server has sent multiple NPN extensions in
1607
     * a single Serverhello
1608
     */
1609
0
    OPENSSL_free(s->ext.npn);
1610
0
    s->ext.npn = OPENSSL_malloc(selected_len);
1611
0
    if (s->ext.npn == NULL) {
1612
0
        s->ext.npn_len = 0;
1613
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1614
0
        return 0;
1615
0
    }
1616
1617
0
    memcpy(s->ext.npn, selected, selected_len);
1618
0
    s->ext.npn_len = selected_len;
1619
0
    s->s3.npn_seen = 1;
1620
1621
0
    return 1;
1622
0
}
1623
#endif
1624
1625
int tls_parse_stoc_alpn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1626
    X509 *x, size_t chainidx)
1627
20.7k
{
1628
20.7k
    size_t len;
1629
20.7k
    PACKET confpkt, protpkt;
1630
20.7k
    int valid = 0;
1631
1632
    /* We must have requested it. */
1633
20.7k
    if (!s->s3.alpn_sent) {
1634
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
1635
0
        return 0;
1636
0
    }
1637
    /*-
1638
     * The extension data consists of:
1639
     *   uint16 list_length
1640
     *   uint8 proto_length;
1641
     *   uint8 proto[proto_length];
1642
     */
1643
20.7k
    if (!PACKET_get_net_2_len(pkt, &len)
1644
20.7k
        || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len)
1645
20.7k
        || PACKET_remaining(pkt) != len) {
1646
31
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1647
31
        return 0;
1648
31
    }
1649
1650
    /* It must be a protocol that we sent */
1651
20.7k
    if (!PACKET_buf_init(&confpkt, s->ext.alpn, s->ext.alpn_len)) {
1652
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1653
0
        return 0;
1654
0
    }
1655
20.7k
    while (PACKET_get_length_prefixed_1(&confpkt, &protpkt)) {
1656
20.7k
        if (PACKET_remaining(&protpkt) != len)
1657
5
            continue;
1658
20.7k
        if (memcmp(PACKET_data(pkt), PACKET_data(&protpkt), len) == 0) {
1659
            /* Valid protocol found */
1660
20.6k
            valid = 1;
1661
20.6k
            break;
1662
20.6k
        }
1663
20.7k
    }
1664
1665
20.7k
    if (!valid) {
1666
        /* The protocol sent from the server does not match one we advertised */
1667
51
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1668
51
        return 0;
1669
51
    }
1670
1671
20.6k
    OPENSSL_free(s->s3.alpn_selected);
1672
20.6k
    s->s3.alpn_selected = OPENSSL_malloc(len);
1673
20.6k
    if (s->s3.alpn_selected == NULL) {
1674
0
        s->s3.alpn_selected_len = 0;
1675
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1676
0
        return 0;
1677
0
    }
1678
20.6k
    if (!PACKET_copy_bytes(pkt, s->s3.alpn_selected, len)) {
1679
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1680
0
        return 0;
1681
0
    }
1682
20.6k
    s->s3.alpn_selected_len = len;
1683
1684
20.6k
    if (s->session->ext.alpn_selected == NULL
1685
0
        || s->session->ext.alpn_selected_len != len
1686
0
        || memcmp(s->session->ext.alpn_selected, s->s3.alpn_selected, len)
1687
20.6k
            != 0) {
1688
        /* ALPN not consistent with the old session so cannot use early_data */
1689
20.6k
        s->ext.early_data_ok = 0;
1690
20.6k
    }
1691
20.6k
    if (!s->hit) {
1692
        /*
1693
         * This is a new session and so alpn_selected should have been
1694
         * initialised to NULL. We should update it with the selected ALPN.
1695
         */
1696
20.6k
        if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
1697
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1698
0
            return 0;
1699
0
        }
1700
20.6k
        s->session->ext.alpn_selected = OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len);
1701
20.6k
        if (s->session->ext.alpn_selected == NULL) {
1702
0
            s->session->ext.alpn_selected_len = 0;
1703
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1704
0
            return 0;
1705
0
        }
1706
20.6k
        s->session->ext.alpn_selected_len = s->s3.alpn_selected_len;
1707
20.6k
    }
1708
1709
20.6k
    return 1;
1710
20.6k
}
1711
1712
#ifndef OPENSSL_NO_SRTP
1713
int tls_parse_stoc_use_srtp(SSL_CONNECTION *s, PACKET *pkt,
1714
    unsigned int context, X509 *x, size_t chainidx)
1715
0
{
1716
0
    unsigned int id, ct, mki;
1717
0
    int i;
1718
0
    STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
1719
0
    SRTP_PROTECTION_PROFILE *prof;
1720
1721
0
    if (!PACKET_get_net_2(pkt, &ct) || ct != 2
1722
0
        || !PACKET_get_net_2(pkt, &id)
1723
0
        || !PACKET_get_1(pkt, &mki)
1724
0
        || PACKET_remaining(pkt) != 0) {
1725
0
        SSLfatal(s, SSL_AD_DECODE_ERROR,
1726
0
            SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1727
0
        return 0;
1728
0
    }
1729
1730
0
    if (mki != 0) {
1731
        /* Must be no MKI, since we never offer one */
1732
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRTP_MKI_VALUE);
1733
0
        return 0;
1734
0
    }
1735
1736
    /* Throw an error if the server gave us an unsolicited extension */
1737
0
    clnt = SSL_get_srtp_profiles(SSL_CONNECTION_GET_SSL(s));
1738
0
    if (clnt == NULL) {
1739
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_SRTP_PROFILES);
1740
0
        return 0;
1741
0
    }
1742
1743
    /*
1744
     * Check to see if the server gave us something we support (and
1745
     * presumably offered)
1746
     */
1747
0
    for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
1748
0
        prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
1749
1750
0
        if (prof->id == id) {
1751
0
            s->srtp_profile = prof;
1752
0
            return 1;
1753
0
        }
1754
0
    }
1755
1756
0
    SSLfatal(s, SSL_AD_DECODE_ERROR,
1757
0
        SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1758
0
    return 0;
1759
0
}
1760
#endif
1761
1762
int tls_parse_stoc_etm(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1763
    X509 *x, size_t chainidx)
1764
4.80k
{
1765
    /* Ignore if inappropriate ciphersuite */
1766
4.80k
    if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
1767
4.80k
        && s->s3.tmp.new_cipher->algorithm_mac != SSL_AEAD
1768
3.09k
        && s->s3.tmp.new_cipher->algorithm_enc != SSL_RC4
1769
3.09k
        && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT
1770
3.09k
        && s->s3.tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT12
1771
3.09k
        && s->s3.tmp.new_cipher->algorithm_enc != SSL_MAGMA
1772
3.09k
        && s->s3.tmp.new_cipher->algorithm_enc != SSL_KUZNYECHIK)
1773
3.09k
        s->ext.use_etm = 1;
1774
1775
4.80k
    return 1;
1776
4.80k
}
1777
1778
int tls_parse_stoc_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1779
    X509 *x, size_t chainidx)
1780
8.75k
{
1781
8.75k
    if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)
1782
0
        return 1;
1783
8.75k
    s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS;
1784
8.75k
    if (!s->hit)
1785
8.74k
        s->session->flags |= SSL_SESS_FLAG_EXTMS;
1786
1787
8.75k
    return 1;
1788
8.75k
}
1789
1790
int tls_parse_stoc_supported_versions(SSL_CONNECTION *s, PACKET *pkt,
1791
    unsigned int context,
1792
    X509 *x, size_t chainidx)
1793
26.4k
{
1794
26.4k
    unsigned int version;
1795
1796
26.4k
    if (!PACKET_get_net_2(pkt, &version)
1797
26.3k
        || PACKET_remaining(pkt) != 0) {
1798
94
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1799
94
        return 0;
1800
94
    }
1801
1802
    /*
1803
     * The only protocol version we support which is valid in this extension in
1804
     * a ServerHello is TLSv1.3 therefore we shouldn't be getting anything else.
1805
     */
1806
26.3k
    if (version != TLS1_3_VERSION) {
1807
164
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1808
164
            SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
1809
164
        return 0;
1810
164
    }
1811
1812
    /* We ignore this extension for HRRs except to sanity check it */
1813
26.1k
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)
1814
437
        return 1;
1815
1816
    /* We just set it here. We validate it in ssl_choose_client_version */
1817
25.7k
    s->version = version;
1818
25.7k
    if (!ssl_set_record_protocol_version(s, version)) {
1819
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1820
0
        return 0;
1821
0
    }
1822
1823
25.7k
    return 1;
1824
25.7k
}
1825
1826
int tls_parse_stoc_key_share(SSL_CONNECTION *s, PACKET *pkt,
1827
    unsigned int context, X509 *x,
1828
    size_t chainidx)
1829
11.1k
{
1830
11.1k
#ifndef OPENSSL_NO_TLS1_3
1831
11.1k
    unsigned int group_id;
1832
11.1k
    PACKET encoded_pt;
1833
11.1k
    EVP_PKEY *ckey = s->s3.tmp.pkey, *skey = NULL;
1834
11.1k
    const TLS_GROUP_INFO *ginf = NULL;
1835
1836
    /* Sanity check */
1837
11.1k
    if (ckey == NULL || s->s3.peer_tmp != NULL) {
1838
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1839
0
        return 0;
1840
0
    }
1841
1842
11.1k
    if (!PACKET_get_net_2(pkt, &group_id)) {
1843
5
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1844
5
        return 0;
1845
5
    }
1846
1847
11.1k
    if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) {
1848
327
        const uint16_t *pgroups = NULL;
1849
327
        size_t i, num_groups;
1850
1851
327
        if (PACKET_remaining(pkt) != 0) {
1852
5
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1853
5
            return 0;
1854
5
        }
1855
1856
        /*
1857
         * It is an error if the HelloRetryRequest wants a key_share that we
1858
         * already sent in the first ClientHello
1859
         */
1860
322
        if (group_id == s->s3.group_id) {
1861
5
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
1862
5
            return 0;
1863
5
        }
1864
1865
        /* Validate the selected group is one we support */
1866
317
        tls1_get_supported_groups(s, &pgroups, &num_groups);
1867
1.88k
        for (i = 0; i < num_groups; i++) {
1868
1.80k
            if (group_id == pgroups[i])
1869
243
                break;
1870
1.80k
        }
1871
317
        if (i >= num_groups
1872
243
            || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)
1873
243
            || !tls_valid_group(s, group_id, TLS1_3_VERSION, TLS1_3_VERSION,
1874
243
                0, NULL)) {
1875
74
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
1876
74
            return 0;
1877
74
        }
1878
1879
243
        s->s3.group_id = group_id;
1880
243
        EVP_PKEY_free(s->s3.tmp.pkey);
1881
243
        s->s3.tmp.pkey = NULL;
1882
243
        return 1;
1883
317
    }
1884
1885
10.8k
    if (group_id != s->s3.group_id) {
1886
        /*
1887
         * This isn't for the group that we sent in the original
1888
         * key_share!
1889
         */
1890
93
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
1891
93
        return 0;
1892
93
    }
1893
    /* Retain this group in the SSL_SESSION */
1894
10.7k
    if (!s->hit) {
1895
10.7k
        s->session->kex_group = group_id;
1896
10.7k
    } else if (group_id != s->session->kex_group) {
1897
        /*
1898
         * If this is a resumption but changed what group was used, we need
1899
         * to record the new group in the session, but the session is not
1900
         * a new session and could be in use by other threads.  So, make
1901
         * a copy of the session to record the new information so that it's
1902
         * useful for any sessions resumed from tickets issued on this
1903
         * connection.
1904
         */
1905
0
        SSL_SESSION *new_sess;
1906
1907
0
        if ((new_sess = ssl_session_dup(s->session, 0)) == NULL) {
1908
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
1909
0
            return 0;
1910
0
        }
1911
0
        SSL_SESSION_free(s->session);
1912
0
        s->session = new_sess;
1913
0
        s->session->kex_group = group_id;
1914
0
    }
1915
1916
10.7k
    if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),
1917
10.7k
             group_id))
1918
10.7k
        == NULL) {
1919
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
1920
0
        return 0;
1921
0
    }
1922
1923
10.7k
    if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt)
1924
10.6k
        || PACKET_remaining(&encoded_pt) == 0) {
1925
90
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1926
90
        return 0;
1927
90
    }
1928
1929
10.6k
    if (!ginf->is_kem) {
1930
        /* Regular KEX */
1931
10.6k
        skey = EVP_PKEY_new();
1932
10.6k
        if (skey == NULL || EVP_PKEY_copy_parameters(skey, ckey) <= 0) {
1933
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
1934
0
            EVP_PKEY_free(skey);
1935
0
            return 0;
1936
0
        }
1937
1938
10.6k
        if (tls13_set_encoded_pub_key(skey, PACKET_data(&encoded_pt),
1939
10.6k
                PACKET_remaining(&encoded_pt))
1940
10.6k
            <= 0) {
1941
29
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT);
1942
29
            EVP_PKEY_free(skey);
1943
29
            return 0;
1944
29
        }
1945
1946
10.6k
        if (ssl_derive(s, ckey, skey, 1) == 0) {
1947
            /* SSLfatal() already called */
1948
9
            EVP_PKEY_free(skey);
1949
9
            return 0;
1950
9
        }
1951
10.5k
        s->s3.peer_tmp = skey;
1952
10.5k
    } else {
1953
        /* KEM Mode */
1954
0
        const unsigned char *ct = PACKET_data(&encoded_pt);
1955
0
        size_t ctlen = PACKET_remaining(&encoded_pt);
1956
1957
0
        if (ssl_decapsulate(s, ckey, ct, ctlen, 1) == 0) {
1958
            /* SSLfatal() already called */
1959
0
            return 0;
1960
0
        }
1961
0
    }
1962
10.5k
    s->s3.did_kex = 1;
1963
10.5k
#endif
1964
1965
10.5k
    return 1;
1966
10.6k
}
1967
1968
int tls_parse_stoc_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1969
    X509 *x, size_t chainidx)
1970
127
{
1971
127
    PACKET cookie;
1972
1973
127
    if (!PACKET_as_length_prefixed_2(pkt, &cookie)
1974
47
        || !PACKET_memdup(&cookie, &s->ext.tls13_cookie,
1975
80
            &s->ext.tls13_cookie_len)) {
1976
80
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1977
80
        return 0;
1978
80
    }
1979
1980
47
    return 1;
1981
127
}
1982
1983
int tls_parse_stoc_early_data(SSL_CONNECTION *s, PACKET *pkt,
1984
    unsigned int context,
1985
    X509 *x, size_t chainidx)
1986
0
{
1987
0
    if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
1988
0
        unsigned long max_early_data;
1989
1990
0
        if (!PACKET_get_net_4(pkt, &max_early_data)
1991
0
            || PACKET_remaining(pkt) != 0) {
1992
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_MAX_EARLY_DATA);
1993
0
            return 0;
1994
0
        }
1995
1996
0
        s->session->ext.max_early_data = max_early_data;
1997
1998
0
        if (SSL_IS_QUIC_HANDSHAKE(s) && max_early_data != 0xffffffff) {
1999
            /*
2000
             * QUIC allows missing max_early_data, or a max_early_data value
2001
             * of 0xffffffff. Missing max_early_data is stored in the session
2002
             * as 0. This is indistinguishable in OpenSSL from a present
2003
             * max_early_data value that was 0. In order that later checks for
2004
             * invalid max_early_data correctly treat as an error the case where
2005
             * max_early_data is present and it is 0, we store any invalid
2006
             * value in the same (non-zero) way. Otherwise we would have to
2007
             * introduce a new flag just for this.
2008
             */
2009
0
            s->session->ext.max_early_data = 1;
2010
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INVALID_MAX_EARLY_DATA);
2011
0
            return 0;
2012
0
        }
2013
2014
0
        return 1;
2015
0
    }
2016
2017
0
    if (PACKET_remaining(pkt) != 0) {
2018
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2019
0
        return 0;
2020
0
    }
2021
2022
0
    if (!s->ext.early_data_ok
2023
0
        || !s->hit) {
2024
        /*
2025
         * If we get here then we didn't send early data, or we didn't resume
2026
         * using the first identity, or the SNI/ALPN is not consistent so the
2027
         * server should not be accepting it.
2028
         */
2029
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
2030
0
        return 0;
2031
0
    }
2032
2033
0
    s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
2034
2035
0
    return 1;
2036
0
}
2037
2038
int tls_parse_stoc_psk(SSL_CONNECTION *s, PACKET *pkt,
2039
    unsigned int context, X509 *x,
2040
    size_t chainidx)
2041
0
{
2042
0
#ifndef OPENSSL_NO_TLS1_3
2043
0
    unsigned int identity;
2044
2045
0
    if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) {
2046
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2047
0
        return 0;
2048
0
    }
2049
2050
0
    if (identity >= (unsigned int)s->ext.tick_identity) {
2051
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_PSK_IDENTITY);
2052
0
        return 0;
2053
0
    }
2054
2055
    /*
2056
     * Session resumption tickets are always sent before PSK tickets. If the
2057
     * ticket index is 0 then it must be for a session resumption ticket if we
2058
     * sent two tickets, or if we didn't send a PSK ticket.
2059
     */
2060
0
    if (identity == 0 && (s->psksession == NULL || s->ext.tick_identity == 2)) {
2061
0
        s->hit = 1;
2062
0
        SSL_SESSION_free(s->psksession);
2063
0
        s->psksession = NULL;
2064
0
        return 1;
2065
0
    }
2066
2067
0
    if (s->psksession == NULL) {
2068
        /* Should never happen */
2069
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2070
0
        return 0;
2071
0
    }
2072
2073
    /*
2074
     * If we used the external PSK for sending early_data then s->early_secret
2075
     * is already set up, so don't overwrite it. Otherwise we copy the
2076
     * early_secret across that we generated earlier.
2077
     */
2078
0
    if ((s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
2079
0
            && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
2080
0
        || s->session->ext.max_early_data > 0
2081
0
        || s->psksession->ext.max_early_data == 0)
2082
0
        memcpy(s->early_secret, s->psksession->early_secret, EVP_MAX_MD_SIZE);
2083
2084
0
    SSL_SESSION_free(s->session);
2085
0
    s->session = s->psksession;
2086
0
    s->psksession = NULL;
2087
0
    s->hit = 1;
2088
    /* Early data is only allowed if we used the first ticket */
2089
0
    if (identity != 0)
2090
0
        s->ext.early_data_ok = 0;
2091
0
#endif
2092
2093
0
    return 1;
2094
0
}
2095
2096
EXT_RETURN tls_construct_ctos_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2097
    unsigned int context,
2098
    X509 *x, size_t chainidx)
2099
111k
{
2100
111k
    sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2101
111k
    if (sc->client_cert_type == NULL)
2102
111k
        return EXT_RETURN_NOT_SENT;
2103
2104
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type)
2105
0
        || !WPACKET_start_sub_packet_u16(pkt)
2106
0
        || !WPACKET_sub_memcpy_u8(pkt, sc->client_cert_type, sc->client_cert_type_len)
2107
0
        || !WPACKET_close(pkt)) {
2108
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2109
0
        return EXT_RETURN_FAIL;
2110
0
    }
2111
0
    sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD;
2112
0
    return EXT_RETURN_SENT;
2113
0
}
2114
2115
int tls_parse_stoc_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2116
    unsigned int context,
2117
    X509 *x, size_t chainidx)
2118
0
{
2119
0
    unsigned int type;
2120
2121
0
    if (PACKET_remaining(pkt) != 1) {
2122
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2123
0
        return 0;
2124
0
    }
2125
0
    if (!PACKET_get_1(pkt, &type)) {
2126
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2127
0
        return 0;
2128
0
    }
2129
    /* We did not send/ask for this */
2130
0
    if (!ossl_assert(sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) {
2131
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2132
0
        return 0;
2133
0
    }
2134
    /* We don't have this enabled */
2135
0
    if (sc->client_cert_type == NULL) {
2136
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2137
0
        return 0;
2138
0
    }
2139
    /* Given back a value we didn't configure */
2140
0
    if (memchr(sc->client_cert_type, type, sc->client_cert_type_len) == NULL) {
2141
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE);
2142
0
        return 0;
2143
0
    }
2144
0
    sc->ext.client_cert_type = type;
2145
0
    return 1;
2146
0
}
2147
2148
EXT_RETURN tls_construct_ctos_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2149
    unsigned int context,
2150
    X509 *x, size_t chainidx)
2151
111k
{
2152
111k
    sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2153
111k
    if (sc->server_cert_type == NULL)
2154
111k
        return EXT_RETURN_NOT_SENT;
2155
2156
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type)
2157
0
        || !WPACKET_start_sub_packet_u16(pkt)
2158
0
        || !WPACKET_sub_memcpy_u8(pkt, sc->server_cert_type, sc->server_cert_type_len)
2159
0
        || !WPACKET_close(pkt)) {
2160
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2161
0
        return EXT_RETURN_FAIL;
2162
0
    }
2163
0
    sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_GOOD;
2164
0
    return EXT_RETURN_SENT;
2165
0
}
2166
2167
int tls_parse_stoc_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2168
    unsigned int context,
2169
    X509 *x, size_t chainidx)
2170
0
{
2171
0
    unsigned int type;
2172
2173
0
    if (PACKET_remaining(pkt) != 1) {
2174
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2175
0
        return 0;
2176
0
    }
2177
0
    if (!PACKET_get_1(pkt, &type)) {
2178
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2179
0
        return 0;
2180
0
    }
2181
    /* We did not send/ask for this */
2182
0
    if (!ossl_assert(sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)) {
2183
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2184
0
        return 0;
2185
0
    }
2186
    /* We don't have this enabled */
2187
0
    if (sc->server_cert_type == NULL) {
2188
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2189
0
        return 0;
2190
0
    }
2191
    /* Given back a value we didn't configure */
2192
0
    if (memchr(sc->server_cert_type, type, sc->server_cert_type_len) == NULL) {
2193
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_VALUE);
2194
0
        return 0;
2195
0
    }
2196
0
    sc->ext.server_cert_type = type;
2197
0
    return 1;
2198
0
}