Coverage Report

Created: 2025-08-28 07:07

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