Coverage Report

Created: 2024-07-27 06:39

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