Coverage Report

Created: 2025-12-31 06:58

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