Coverage Report

Created: 2025-06-13 06:58

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