Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/ssl/statem/extensions.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2025 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
#if defined(__TANDEM) && defined(_SPT_MODEL_)
11
#include <spthread.h>
12
#include <spt_extensions.h> /* timeval */
13
#endif
14
15
#include <string.h>
16
#include "internal/nelem.h"
17
#include "internal/cryptlib.h"
18
#include "internal/ssl_unwrap.h"
19
#include "../ssl_local.h"
20
#include "statem_local.h"
21
22
static int final_renegotiate(SSL_CONNECTION *s, unsigned int context, int sent);
23
static int init_server_name(SSL_CONNECTION *s, unsigned int context);
24
static int final_server_name(SSL_CONNECTION *s, unsigned int context, int sent);
25
static int final_ec_pt_formats(SSL_CONNECTION *s, unsigned int context,
26
    int sent);
27
static int init_session_ticket(SSL_CONNECTION *s, unsigned int context);
28
#ifndef OPENSSL_NO_OCSP
29
static int init_status_request(SSL_CONNECTION *s, unsigned int context);
30
#endif
31
#ifndef OPENSSL_NO_NEXTPROTONEG
32
static int init_npn(SSL_CONNECTION *s, unsigned int context);
33
#endif
34
static int init_alpn(SSL_CONNECTION *s, unsigned int context);
35
static int final_alpn(SSL_CONNECTION *s, unsigned int context, int sent);
36
static int init_sig_algs_cert(SSL_CONNECTION *s, unsigned int context);
37
static int init_sig_algs(SSL_CONNECTION *s, unsigned int context);
38
static int init_server_cert_type(SSL_CONNECTION *sc, unsigned int context);
39
static int init_client_cert_type(SSL_CONNECTION *sc, unsigned int context);
40
static int init_certificate_authorities(SSL_CONNECTION *s,
41
    unsigned int context);
42
static EXT_RETURN tls_construct_certificate_authorities(SSL_CONNECTION *s,
43
    WPACKET *pkt,
44
    unsigned int context,
45
    X509 *x,
46
    size_t chainidx);
47
static int tls_parse_certificate_authorities(SSL_CONNECTION *s, PACKET *pkt,
48
    unsigned int context, X509 *x,
49
    size_t chainidx);
50
#ifndef OPENSSL_NO_SRP
51
static int init_srp(SSL_CONNECTION *s, unsigned int context);
52
#endif
53
static int init_ec_point_formats(SSL_CONNECTION *s, unsigned int context);
54
static int init_etm(SSL_CONNECTION *s, unsigned int context);
55
static int init_ems(SSL_CONNECTION *s, unsigned int context);
56
static int final_ems(SSL_CONNECTION *s, unsigned int context, int sent);
57
static int init_psk_kex_modes(SSL_CONNECTION *s, unsigned int context);
58
static int final_key_share(SSL_CONNECTION *s, unsigned int context, int sent);
59
#ifndef OPENSSL_NO_SRTP
60
static int init_srtp(SSL_CONNECTION *s, unsigned int context);
61
#endif
62
static int final_sig_algs(SSL_CONNECTION *s, unsigned int context, int sent);
63
static int final_supported_versions(SSL_CONNECTION *s, unsigned int context,
64
    int sent);
65
static int final_early_data(SSL_CONNECTION *s, unsigned int context, int sent);
66
static int final_maxfragmentlen(SSL_CONNECTION *s, unsigned int context,
67
    int sent);
68
static int init_post_handshake_auth(SSL_CONNECTION *s, unsigned int context);
69
static int final_psk(SSL_CONNECTION *s, unsigned int context, int sent);
70
static int tls_init_compress_certificate(SSL_CONNECTION *sc, unsigned int context);
71
static EXT_RETURN tls_construct_compress_certificate(SSL_CONNECTION *sc, WPACKET *pkt,
72
    unsigned int context,
73
    X509 *x, size_t chainidx);
74
static int tls_parse_compress_certificate(SSL_CONNECTION *sc, PACKET *pkt,
75
    unsigned int context,
76
    X509 *x, size_t chainidx);
77
78
/* Structure to define a built-in extension */
79
typedef struct extensions_definition_st {
80
    /* The defined type for the extension */
81
    unsigned int type;
82
    /*
83
     * The context that this extension applies to, e.g. what messages and
84
     * protocol versions
85
     */
86
    unsigned int context;
87
    /*
88
     * Initialise extension before parsing. Always called for relevant contexts
89
     * even if extension not present
90
     */
91
    int (*init)(SSL_CONNECTION *s, unsigned int context);
92
    /* Parse extension sent from client to server */
93
    int (*parse_ctos)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
94
        X509 *x, size_t chainidx);
95
    /* Parse extension send from server to client */
96
    int (*parse_stoc)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
97
        X509 *x, size_t chainidx);
98
    /* Construct extension sent from server to client */
99
    EXT_RETURN (*construct_stoc)(SSL_CONNECTION *s, WPACKET *pkt,
100
        unsigned int context,
101
        X509 *x, size_t chainidx);
102
    /* Construct extension sent from client to server */
103
    EXT_RETURN (*construct_ctos)(SSL_CONNECTION *s, WPACKET *pkt,
104
        unsigned int context,
105
        X509 *x, size_t chainidx);
106
    /*
107
     * Finalise extension after parsing. Always called where an extensions was
108
     * initialised even if the extension was not present. |sent| is set to 1 if
109
     * the extension was seen, or 0 otherwise.
110
     */
111
    int (*final)(SSL_CONNECTION *s, unsigned int context, int sent);
112
} EXTENSION_DEFINITION;
113
114
/*
115
 * Definitions of all built-in extensions. NOTE: Changes in the number or order
116
 * of these extensions should be mirrored with equivalent changes to the
117
 * indexes ( TLSEXT_IDX_* ) defined in ssl_local.h.
118
 * Extensions should be added to test/ext_internal_test.c as well, as that
119
 * tests the ordering of the extensions.
120
 *
121
 * Each extension has an initialiser, a client and
122
 * server side parser and a finaliser. The initialiser is called (if the
123
 * extension is relevant to the given context) even if we did not see the
124
 * extension in the message that we received. The parser functions are only
125
 * called if we see the extension in the message. The finalisers are always
126
 * called if the initialiser was called.
127
 * There are also server and client side constructor functions which are always
128
 * called during message construction if the extension is relevant for the
129
 * given context.
130
 * The initialisation, parsing, finalisation and construction functions are
131
 * always called in the order defined in this list. Some extensions may depend
132
 * on others having been processed first, so the order of this list is
133
 * significant.
134
 * The extension context is defined by a series of flags which specify which
135
 * messages the extension is relevant to. These flags also specify whether the
136
 * extension is relevant to a particular protocol or protocol version.
137
 *
138
 * NOTE: WebSphere Application Server 7+ cannot handle empty extensions at
139
 * the end, keep these extensions before signature_algorithm.
140
 */
141
#define INVALID_EXTENSION { TLSEXT_TYPE_invalid, 0, NULL, NULL, NULL, NULL, NULL, NULL }
142
static const EXTENSION_DEFINITION ext_defs[] = {
143
    { TLSEXT_TYPE_renegotiate,
144
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
145
            | SSL_EXT_SSL3_ALLOWED | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
146
        NULL, tls_parse_ctos_renegotiate, tls_parse_stoc_renegotiate,
147
        tls_construct_stoc_renegotiate, tls_construct_ctos_renegotiate,
148
        final_renegotiate },
149
    { TLSEXT_TYPE_server_name,
150
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
151
            | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
152
        init_server_name,
153
        tls_parse_ctos_server_name, tls_parse_stoc_server_name,
154
        tls_construct_stoc_server_name, tls_construct_ctos_server_name,
155
        final_server_name },
156
    { TLSEXT_TYPE_max_fragment_length,
157
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
158
            | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
159
        NULL, tls_parse_ctos_maxfragmentlen, tls_parse_stoc_maxfragmentlen,
160
        tls_construct_stoc_maxfragmentlen, tls_construct_ctos_maxfragmentlen,
161
        final_maxfragmentlen },
162
#ifndef OPENSSL_NO_SRP
163
    { TLSEXT_TYPE_srp,
164
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
165
        init_srp, tls_parse_ctos_srp, NULL, NULL, tls_construct_ctos_srp, NULL },
166
#else
167
    INVALID_EXTENSION,
168
#endif
169
    { TLSEXT_TYPE_ec_point_formats,
170
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
171
            | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
172
        init_ec_point_formats, tls_parse_ctos_ec_pt_formats, tls_parse_stoc_ec_pt_formats,
173
        tls_construct_stoc_ec_pt_formats, tls_construct_ctos_ec_pt_formats,
174
        final_ec_pt_formats },
175
    { /*
176
       * "supported_groups" is spread across several specifications.
177
       * It was originally specified as "elliptic_curves" in RFC 4492,
178
       * and broadened to include named FFDH groups by RFC 7919.
179
       * Both RFCs 4492 and 7919 do not include a provision for the server
180
       * to indicate to the client the complete list of groups supported
181
       * by the server, with the server instead just indicating the
182
       * selected group for this connection in the ServerKeyExchange
183
       * message.  TLS 1.3 adds a scheme for the server to indicate
184
       * to the client its list of supported groups in the
185
       * EncryptedExtensions message, but none of the relevant
186
       * specifications permit sending supported_groups in the ServerHello.
187
       * Nonetheless (possibly due to the close proximity to the
188
       * "ec_point_formats" extension, which is allowed in the ServerHello),
189
       * there are several servers that send this extension in the
190
       * ServerHello anyway.  Up to and including the 1.1.0 release,
191
       * we did not check for the presence of nonpermitted extensions,
192
       * so to avoid a regression, we must permit this extension in the
193
       * TLS 1.2 ServerHello as well.
194
       *
195
       * Note that there is no tls_parse_stoc_supported_groups function,
196
       * so we do not perform any additional parsing, validation, or
197
       * processing on the server's group list -- this is just a minimal
198
       * change to preserve compatibility with these misbehaving servers.
199
       */
200
        TLSEXT_TYPE_supported_groups,
201
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
202
            | SSL_EXT_TLS1_2_SERVER_HELLO,
203
        NULL, tls_parse_ctos_supported_groups, NULL,
204
        tls_construct_stoc_supported_groups,
205
        tls_construct_ctos_supported_groups, NULL },
206
    { TLSEXT_TYPE_session_ticket,
207
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
208
            | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
209
        init_session_ticket, tls_parse_ctos_session_ticket,
210
        tls_parse_stoc_session_ticket, tls_construct_stoc_session_ticket,
211
        tls_construct_ctos_session_ticket, NULL },
212
#ifndef OPENSSL_NO_OCSP
213
    { TLSEXT_TYPE_status_request,
214
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
215
            | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
216
        init_status_request, tls_parse_ctos_status_request,
217
        tls_parse_stoc_status_request, tls_construct_stoc_status_request,
218
        tls_construct_ctos_status_request, NULL },
219
#else
220
    INVALID_EXTENSION,
221
#endif
222
#ifndef OPENSSL_NO_NEXTPROTONEG
223
    { TLSEXT_TYPE_next_proto_neg,
224
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
225
            | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
226
        init_npn, tls_parse_ctos_npn, tls_parse_stoc_npn,
227
        tls_construct_stoc_next_proto_neg, tls_construct_ctos_npn, NULL },
228
#else
229
    INVALID_EXTENSION,
230
#endif
231
    { /*
232
       * Must appear in this list after server_name so that finalisation
233
       * happens after server_name callbacks
234
       */
235
        TLSEXT_TYPE_application_layer_protocol_negotiation,
236
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
237
            | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
238
        init_alpn, tls_parse_ctos_alpn, tls_parse_stoc_alpn,
239
        tls_construct_stoc_alpn, tls_construct_ctos_alpn, final_alpn },
240
#ifndef OPENSSL_NO_SRTP
241
    { TLSEXT_TYPE_use_srtp,
242
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
243
            | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS | SSL_EXT_DTLS_ONLY,
244
        init_srtp, tls_parse_ctos_use_srtp, tls_parse_stoc_use_srtp,
245
        tls_construct_stoc_use_srtp, tls_construct_ctos_use_srtp, NULL },
246
#else
247
    INVALID_EXTENSION,
248
#endif
249
    { TLSEXT_TYPE_encrypt_then_mac,
250
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
251
            | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
252
        init_etm, tls_parse_ctos_etm, tls_parse_stoc_etm,
253
        tls_construct_stoc_etm, tls_construct_ctos_etm, NULL },
254
#ifndef OPENSSL_NO_CT
255
    { TLSEXT_TYPE_signed_certificate_timestamp,
256
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
257
            | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
258
        NULL,
259
        /*
260
         * No server side support for this, but can be provided by a custom
261
         * extension. This is an exception to the rule that custom extensions
262
         * cannot override built in ones.
263
         */
264
        NULL, tls_parse_stoc_sct, NULL, tls_construct_ctos_sct, NULL },
265
#else
266
    INVALID_EXTENSION,
267
#endif
268
    { TLSEXT_TYPE_extended_master_secret,
269
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
270
            | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
271
        init_ems, tls_parse_ctos_ems, tls_parse_stoc_ems,
272
        tls_construct_stoc_ems, tls_construct_ctos_ems, final_ems },
273
    { TLSEXT_TYPE_signature_algorithms_cert,
274
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
275
        init_sig_algs_cert, tls_parse_ctos_sig_algs_cert,
276
        tls_parse_ctos_sig_algs_cert,
277
        /* We do not generate signature_algorithms_cert at present. */
278
        NULL, NULL, NULL },
279
    {
280
        TLSEXT_TYPE_post_handshake_auth,
281
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ONLY,
282
        init_post_handshake_auth,
283
        tls_parse_ctos_post_handshake_auth,
284
        NULL,
285
        NULL,
286
        tls_construct_ctos_post_handshake_auth,
287
        NULL,
288
    },
289
    { TLSEXT_TYPE_client_cert_type,
290
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
291
            | SSL_EXT_TLS1_2_SERVER_HELLO,
292
        init_client_cert_type,
293
        tls_parse_ctos_client_cert_type, tls_parse_stoc_client_cert_type,
294
        tls_construct_stoc_client_cert_type, tls_construct_ctos_client_cert_type,
295
        NULL },
296
    { TLSEXT_TYPE_server_cert_type,
297
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
298
            | SSL_EXT_TLS1_2_SERVER_HELLO,
299
        init_server_cert_type,
300
        tls_parse_ctos_server_cert_type, tls_parse_stoc_server_cert_type,
301
        tls_construct_stoc_server_cert_type, tls_construct_ctos_server_cert_type,
302
        NULL },
303
    { TLSEXT_TYPE_signature_algorithms,
304
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
305
        init_sig_algs, tls_parse_ctos_sig_algs,
306
        tls_parse_ctos_sig_algs, tls_construct_ctos_sig_algs,
307
        tls_construct_ctos_sig_algs, final_sig_algs },
308
    { TLSEXT_TYPE_supported_versions,
309
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
310
            | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST | SSL_EXT_TLS_IMPLEMENTATION_ONLY,
311
        NULL,
312
        /* Processed inline as part of version selection */
313
        NULL, tls_parse_stoc_supported_versions,
314
        tls_construct_stoc_supported_versions,
315
        tls_construct_ctos_supported_versions, final_supported_versions },
316
    { TLSEXT_TYPE_psk_kex_modes,
317
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS_IMPLEMENTATION_ONLY
318
            | SSL_EXT_TLS1_3_ONLY,
319
        init_psk_kex_modes, tls_parse_ctos_psk_kex_modes, NULL, NULL,
320
        tls_construct_ctos_psk_kex_modes, NULL },
321
    { /*
322
       * Must be in this list after supported_groups. We need that to have
323
       * been parsed before we do this one.
324
       */
325
        TLSEXT_TYPE_key_share,
326
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
327
            | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST | SSL_EXT_TLS_IMPLEMENTATION_ONLY
328
            | SSL_EXT_TLS1_3_ONLY,
329
        NULL, tls_parse_ctos_key_share, tls_parse_stoc_key_share,
330
        tls_construct_stoc_key_share, tls_construct_ctos_key_share,
331
        final_key_share },
332
    { /* Must be after key_share */
333
        TLSEXT_TYPE_cookie,
334
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
335
            | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
336
        NULL, tls_parse_ctos_cookie, tls_parse_stoc_cookie,
337
        tls_construct_stoc_cookie, tls_construct_ctos_cookie, NULL },
338
    { /*
339
       * Special unsolicited ServerHello extension only used when
340
       * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set. We allow it in a ClientHello but
341
       * ignore it.
342
       */
343
        TLSEXT_TYPE_cryptopro_bug,
344
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
345
            | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
346
        NULL, NULL, NULL, tls_construct_stoc_cryptopro_bug, NULL, NULL },
347
    { TLSEXT_TYPE_compress_certificate,
348
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
349
            | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
350
        tls_init_compress_certificate,
351
        tls_parse_compress_certificate, tls_parse_compress_certificate,
352
        tls_construct_compress_certificate, tls_construct_compress_certificate,
353
        NULL },
354
    { TLSEXT_TYPE_early_data,
355
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
356
            | SSL_EXT_TLS1_3_NEW_SESSION_TICKET | SSL_EXT_TLS1_3_ONLY,
357
        NULL, tls_parse_ctos_early_data, tls_parse_stoc_early_data,
358
        tls_construct_stoc_early_data, tls_construct_ctos_early_data,
359
        final_early_data },
360
    {
361
        TLSEXT_TYPE_certificate_authorities,
362
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
363
            | SSL_EXT_TLS1_3_ONLY,
364
        init_certificate_authorities,
365
        tls_parse_certificate_authorities,
366
        tls_parse_certificate_authorities,
367
        tls_construct_certificate_authorities,
368
        tls_construct_certificate_authorities,
369
        NULL,
370
    },
371
    { /* Must be immediately before pre_shared_key */
372
        TLSEXT_TYPE_padding,
373
        SSL_EXT_CLIENT_HELLO,
374
        NULL,
375
        /* We send this, but don't read it */
376
        NULL, NULL, NULL, tls_construct_ctos_padding, NULL },
377
    { /* Required by the TLSv1.3 spec to always be the last extension */
378
        TLSEXT_TYPE_psk,
379
        SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
380
            | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
381
        NULL, tls_parse_ctos_psk, tls_parse_stoc_psk, tls_construct_stoc_psk,
382
        tls_construct_ctos_psk, final_psk }
383
};
384
385
/* Returns a TLSEXT_TYPE for the given index */
386
unsigned int ossl_get_extension_type(size_t idx)
387
0
{
388
0
    size_t num_exts = OSSL_NELEM(ext_defs);
389
390
0
    if (idx >= num_exts)
391
0
        return TLSEXT_TYPE_out_of_range;
392
393
0
    return ext_defs[idx].type;
394
0
}
395
396
/* Check whether an extension's context matches the current context */
397
static int validate_context(SSL_CONNECTION *s, unsigned int extctx,
398
    unsigned int thisctx)
399
425k
{
400
    /* Check we're allowed to use this extension in this context */
401
425k
    if ((thisctx & extctx) == 0)
402
119
        return 0;
403
404
425k
    if (SSL_CONNECTION_IS_DTLS(s)) {
405
71.0k
        if ((extctx & SSL_EXT_TLS_ONLY) != 0)
406
0
            return 0;
407
354k
    } else if ((extctx & SSL_EXT_DTLS_ONLY) != 0) {
408
17
        return 0;
409
17
    }
410
411
425k
    return 1;
412
425k
}
413
414
int tls_validate_all_contexts(SSL_CONNECTION *s, unsigned int thisctx,
415
    RAW_EXTENSION *exts)
416
73.2k
{
417
73.2k
    size_t i, num_exts, builtin_num = OSSL_NELEM(ext_defs), offset;
418
73.2k
    RAW_EXTENSION *thisext;
419
73.2k
    unsigned int context;
420
73.2k
    ENDPOINT role = ENDPOINT_BOTH;
421
422
73.2k
    if ((thisctx & SSL_EXT_CLIENT_HELLO) != 0)
423
0
        role = ENDPOINT_SERVER;
424
73.2k
    else if ((thisctx & SSL_EXT_TLS1_2_SERVER_HELLO) != 0)
425
47.3k
        role = ENDPOINT_CLIENT;
426
427
    /* Calculate the number of extensions in the extensions list */
428
73.2k
    num_exts = builtin_num + s->cert->custext.meths_count;
429
430
2.20M
    for (thisext = exts, i = 0; i < num_exts; i++, thisext++) {
431
2.13M
        if (!thisext->present)
432
1.99M
            continue;
433
434
134k
        if (i < builtin_num) {
435
134k
            context = ext_defs[i].context;
436
134k
        } else {
437
0
            custom_ext_method *meth = NULL;
438
439
0
            meth = custom_ext_find(&s->cert->custext, role, thisext->type,
440
0
                &offset);
441
0
            if (!ossl_assert(meth != NULL))
442
0
                return 0;
443
0
            context = meth->context;
444
0
        }
445
446
134k
        if (!validate_context(s, context, thisctx))
447
43
            return 0;
448
134k
    }
449
450
73.1k
    return 1;
451
73.2k
}
452
453
/*
454
 * Verify whether we are allowed to use the extension |type| in the current
455
 * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
456
 * indicate the extension is not allowed. If returning 1 then |*found| is set to
457
 * the definition for the extension we found.
458
 */
459
static int verify_extension(SSL_CONNECTION *s, unsigned int context,
460
    unsigned int type, custom_ext_methods *meths,
461
    RAW_EXTENSION *rawexlist, RAW_EXTENSION **found)
462
397k
{
463
397k
    size_t i;
464
397k
    size_t builtin_num = OSSL_NELEM(ext_defs);
465
397k
    const EXTENSION_DEFINITION *thisext;
466
467
6.66M
    for (i = 0, thisext = ext_defs; i < builtin_num; i++, thisext++) {
468
6.53M
        if (type == thisext->type) {
469
271k
            if (!validate_context(s, thisext->context, context))
470
83
                return 0;
471
472
271k
            *found = &rawexlist[i];
473
271k
            return 1;
474
271k
        }
475
6.53M
    }
476
477
    /* Check the custom extensions */
478
126k
    if (meths != NULL) {
479
126k
        size_t offset = 0;
480
126k
        ENDPOINT role = ENDPOINT_BOTH;
481
126k
        custom_ext_method *meth = NULL;
482
483
126k
        if ((context & SSL_EXT_CLIENT_HELLO) != 0)
484
46.6k
            role = ENDPOINT_SERVER;
485
79.9k
        else if ((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0)
486
56.5k
            role = ENDPOINT_CLIENT;
487
488
126k
        meth = custom_ext_find(meths, role, type, &offset);
489
126k
        if (meth != NULL) {
490
20.1k
            if (!validate_context(s, meth->context, context))
491
10
                return 0;
492
20.1k
            *found = &rawexlist[offset + builtin_num];
493
20.1k
            return 1;
494
20.1k
        }
495
126k
    }
496
497
    /* Unknown extension. We allow it */
498
106k
    *found = NULL;
499
106k
    return 1;
500
126k
}
501
502
/*
503
 * Check whether the context defined for an extension |extctx| means whether
504
 * the extension is relevant for the current context |thisctx| or not. Returns
505
 * 1 if the extension is relevant for this context, and 0 otherwise
506
 */
507
int extension_is_relevant(SSL_CONNECTION *s, unsigned int extctx,
508
    unsigned int thisctx)
509
4.79M
{
510
4.79M
    int is_tls13;
511
512
    /*
513
     * For HRR we haven't selected the version yet but we know it will be
514
     * TLSv1.3
515
     */
516
4.79M
    if ((thisctx & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0)
517
2.78k
        is_tls13 = 1;
518
4.79M
    else
519
4.79M
        is_tls13 = SSL_CONNECTION_IS_TLS13(s);
520
521
4.79M
    if ((SSL_CONNECTION_IS_DTLS(s)
522
1.20M
            && (extctx & SSL_EXT_TLS_IMPLEMENTATION_ONLY) != 0)
523
4.63M
        || (s->version == SSL3_VERSION
524
11
            && (extctx & SSL_EXT_SSL3_ALLOWED) == 0)
525
        /*
526
         * Note that SSL_IS_TLS13() means "TLS 1.3 has been negotiated",
527
         * which is never true when generating the ClientHello.
528
         * However, version negotiation *has* occurred by the time the
529
         * ClientHello extensions are being parsed.
530
         * Be careful to allow TLS 1.3-only extensions when generating
531
         * the ClientHello.
532
         */
533
4.63M
        || (is_tls13 && (extctx & SSL_EXT_TLS1_2_AND_BELOW_ONLY) != 0)
534
4.62M
        || (!is_tls13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0
535
805k
            && (thisctx & SSL_EXT_CLIENT_HELLO) == 0)
536
4.62M
        || (s->server && !is_tls13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0)
537
4.52M
        || (s->hit && (extctx & SSL_EXT_IGNORE_ON_RESUMPTION) != 0))
538
272k
        return 0;
539
4.52M
    return 1;
540
4.79M
}
541
542
/*
543
 * Gather a list of all the extensions from the data in |packet]. |context|
544
 * tells us which message this extension is for. The raw extension data is
545
 * stored in |*res| on success. We don't actually process the content of the
546
 * extensions yet, except to check their types. This function also runs the
547
 * initialiser functions for all known extensions if |init| is nonzero (whether
548
 * we have collected them or not). If successful the caller is responsible for
549
 * freeing the contents of |*res|.
550
 *
551
 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
552
 * more than one extension of the same type in a ClientHello or ServerHello.
553
 * This function returns 1 if all extensions are unique and we have parsed their
554
 * types, and 0 if the extensions contain duplicates, could not be successfully
555
 * found, or an internal error occurred. We only check duplicates for
556
 * extensions that we know about. We ignore others.
557
 */
558
int tls_collect_extensions(SSL_CONNECTION *s, PACKET *packet,
559
    unsigned int context,
560
    RAW_EXTENSION **res, size_t *len, int init)
561
159k
{
562
159k
    PACKET extensions = *packet;
563
159k
    size_t i = 0;
564
159k
    size_t num_exts;
565
159k
    custom_ext_methods *exts = &s->cert->custext;
566
159k
    RAW_EXTENSION *raw_extensions = NULL;
567
159k
    const EXTENSION_DEFINITION *thisexd;
568
569
159k
    *res = NULL;
570
571
    /*
572
     * Initialise server side custom extensions. Client side is done during
573
     * construction of extensions for the ClientHello.
574
     */
575
159k
    if ((context & SSL_EXT_CLIENT_HELLO) != 0)
576
42.6k
        custom_ext_init(&s->cert->custext);
577
578
159k
    num_exts = OSSL_NELEM(ext_defs) + (exts != NULL ? exts->meths_count : 0);
579
159k
    raw_extensions = OPENSSL_zalloc(num_exts * sizeof(*raw_extensions));
580
159k
    if (raw_extensions == NULL) {
581
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
582
0
        return 0;
583
0
    }
584
585
159k
    i = 0;
586
557k
    while (PACKET_remaining(&extensions) > 0) {
587
398k
        unsigned int type, idx;
588
398k
        PACKET extension;
589
398k
        RAW_EXTENSION *thisex;
590
591
398k
        if (!PACKET_get_net_2(&extensions, &type) || !PACKET_get_length_prefixed_2(&extensions, &extension)) {
592
692
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
593
692
            goto err;
594
692
        }
595
        /*
596
         * Verify this extension is allowed. We only check duplicates for
597
         * extensions that we recognise. We also have a special case for the
598
         * PSK extension, which must be the last one in the ClientHello.
599
         */
600
397k
        if (!verify_extension(s, context, type, exts, raw_extensions, &thisex)
601
397k
            || (thisex != NULL && thisex->present == 1)
602
397k
            || (type == TLSEXT_TYPE_psk
603
917
                && (context & SSL_EXT_CLIENT_HELLO) != 0
604
899
                && PACKET_remaining(&extensions) != 0)) {
605
271
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
606
271
            goto err;
607
271
        }
608
397k
        idx = thisex - raw_extensions;
609
        /*-
610
         * Check that we requested this extension (if appropriate). Requests can
611
         * be sent in the ClientHello and CertificateRequest. Unsolicited
612
         * extensions can be sent in the NewSessionTicket. We only do this for
613
         * the built-in extensions. Custom extensions have a different but
614
         * similar check elsewhere.
615
         * Special cases:
616
         * - The HRR cookie extension is unsolicited
617
         * - The renegotiate extension is unsolicited (the client signals
618
         *   support via an SCSV)
619
         * - The signed_certificate_timestamp extension can be provided by a
620
         * custom extension or by the built-in version. We let the extension
621
         * itself handle unsolicited response checks.
622
         */
623
397k
        if (idx < OSSL_NELEM(ext_defs)
624
271k
            && (context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) == 0
625
157k
            && type != TLSEXT_TYPE_cookie
626
157k
            && type != TLSEXT_TYPE_renegotiate
627
110k
            && type != TLSEXT_TYPE_signed_certificate_timestamp
628
110k
            && (s->ext.extflags[idx] & SSL_EXT_FLAG_SENT) == 0
629
367
#ifndef OPENSSL_NO_GOST
630
367
            && !((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
631
362
                && type == TLSEXT_TYPE_cryptopro_bug)
632
397k
#endif
633
397k
        ) {
634
131
            SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION,
635
131
                SSL_R_UNSOLICITED_EXTENSION);
636
131
            goto err;
637
131
        }
638
397k
        if (thisex != NULL) {
639
291k
            thisex->data = extension;
640
291k
            thisex->present = 1;
641
291k
            thisex->type = type;
642
291k
            thisex->received_order = i++;
643
291k
            if (s->ext.debug_cb)
644
0
                s->ext.debug_cb(SSL_CONNECTION_GET_USER_SSL(s), !s->server,
645
0
                    thisex->type, PACKET_data(&thisex->data),
646
0
                    PACKET_remaining(&thisex->data),
647
0
                    s->ext.debug_arg);
648
291k
        }
649
397k
    }
650
651
158k
    if (init) {
652
        /*
653
         * Initialise all known extensions relevant to this context,
654
         * whether we have found them or not
655
         */
656
4.72M
        for (thisexd = ext_defs, i = 0; i < OSSL_NELEM(ext_defs);
657
4.56M
            i++, thisexd++) {
658
4.56M
            if (thisexd->init != NULL && (thisexd->context & context) != 0
659
1.68M
                && extension_is_relevant(s, thisexd->context, context)
660
1.51M
                && !thisexd->init(s, context)) {
661
                /* SSLfatal() already called */
662
0
                goto err;
663
0
            }
664
4.56M
        }
665
158k
    }
666
667
158k
    *res = raw_extensions;
668
158k
    if (len != NULL)
669
42.3k
        *len = num_exts;
670
158k
    return 1;
671
672
1.09k
err:
673
1.09k
    OPENSSL_free(raw_extensions);
674
1.09k
    return 0;
675
158k
}
676
677
/*
678
 * Runs the parser for a given extension with index |idx|. |exts| contains the
679
 * list of all parsed extensions previously collected by
680
 * tls_collect_extensions(). The parser is only run if it is applicable for the
681
 * given |context| and the parser has not already been run. If this is for a
682
 * Certificate message, then we also provide the parser with the relevant
683
 * Certificate |x| and its position in the |chainidx| with 0 being the first
684
 * Certificate. Returns 1 on success or 0 on failure. If an extension is not
685
 * present this counted as success.
686
 */
687
int tls_parse_extension(SSL_CONNECTION *s, TLSEXT_INDEX idx, int context,
688
    RAW_EXTENSION *exts, X509 *x, size_t chainidx)
689
4.52M
{
690
4.52M
    RAW_EXTENSION *currext = &exts[idx];
691
4.52M
    int (*parser)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, X509 *x,
692
4.52M
        size_t chainidx)
693
4.52M
        = NULL;
694
695
    /* Skip if the extension is not present */
696
4.52M
    if (!currext->present)
697
4.21M
        return 1;
698
699
    /* Skip if we've already parsed this extension */
700
314k
    if (currext->parsed)
701
37.8k
        return 1;
702
703
276k
    currext->parsed = 1;
704
705
276k
    if (idx < OSSL_NELEM(ext_defs)) {
706
        /* We are handling a built-in extension */
707
256k
        const EXTENSION_DEFINITION *extdef = &ext_defs[idx];
708
709
        /* Check if extension is defined for our protocol. If not, skip */
710
256k
        if (!extension_is_relevant(s, extdef->context, context))
711
10.3k
            return 1;
712
713
246k
        parser = s->server ? extdef->parse_ctos : extdef->parse_stoc;
714
715
246k
        if (parser != NULL)
716
242k
            return parser(s, &currext->data, context, x, chainidx);
717
718
        /*
719
         * If the parser is NULL we fall through to the custom extension
720
         * processing
721
         */
722
246k
    }
723
724
    /* Parse custom extensions */
725
24.3k
    return custom_ext_parse(s, context, currext->type,
726
24.3k
        PACKET_data(&currext->data),
727
24.3k
        PACKET_remaining(&currext->data),
728
24.3k
        x, chainidx);
729
276k
}
730
731
/*
732
 * Parse all remaining extensions that have not yet been parsed. Also calls the
733
 * finalisation for all extensions at the end if |fin| is nonzero, whether we
734
 * collected them or not. Returns 1 for success or 0 for failure. If we are
735
 * working on a Certificate message then we also pass the Certificate |x| and
736
 * its position in the |chainidx|, with 0 being the first certificate.
737
 */
738
int tls_parse_all_extensions(SSL_CONNECTION *s, int context,
739
    RAW_EXTENSION *exts, X509 *x,
740
    size_t chainidx, int fin)
741
154k
{
742
154k
    size_t i, numexts = OSSL_NELEM(ext_defs);
743
154k
    const EXTENSION_DEFINITION *thisexd;
744
745
    /* Calculate the number of extensions in the extensions list */
746
154k
    numexts += s->cert->custext.meths_count;
747
748
    /* Parse each extension in turn */
749
4.51M
    for (i = 0; i < numexts; i++) {
750
4.37M
        if (!tls_parse_extension(s, i, context, exts, x, chainidx)) {
751
            /* SSLfatal() already called */
752
9.70k
            return 0;
753
9.70k
        }
754
4.37M
    }
755
756
145k
    if (fin) {
757
        /*
758
         * Finalise all known extensions relevant to this context,
759
         * whether we have found them or not
760
         */
761
4.32M
        for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs);
762
4.17M
            i++, thisexd++) {
763
4.17M
            if (thisexd->final != NULL && (thisexd->context & context) != 0
764
788k
                && !thisexd->final(s, context, exts[i].present)) {
765
                /* SSLfatal() already called */
766
455
                return 0;
767
455
            }
768
4.17M
        }
769
145k
    }
770
771
144k
    return 1;
772
145k
}
773
774
int should_add_extension(SSL_CONNECTION *s, unsigned int extctx,
775
    unsigned int thisctx, int max_version)
776
4.47M
{
777
    /* Skip if not relevant for our context */
778
4.47M
    if ((extctx & thisctx) == 0)
779
575k
        return 0;
780
781
    /* Check if this extension is defined for our protocol. If not, skip */
782
3.90M
    if (!extension_is_relevant(s, extctx, thisctx)
783
3.74M
        || ((extctx & SSL_EXT_TLS1_3_ONLY) != 0
784
865k
            && (thisctx & SSL_EXT_CLIENT_HELLO) != 0
785
855k
            && (SSL_CONNECTION_IS_DTLS(s) || max_version < TLS1_3_VERSION)))
786
245k
        return 0;
787
788
3.65M
    return 1;
789
3.90M
}
790
791
/*
792
 * Construct all the extensions relevant to the current |context| and write
793
 * them to |pkt|. If this is an extension for a Certificate in a Certificate
794
 * message, then |x| will be set to the Certificate we are handling, and
795
 * |chainidx| will indicate the position in the chainidx we are processing (with
796
 * 0 being the first in the chain). Returns 1 on success or 0 on failure. On a
797
 * failure construction stops at the first extension to fail to construct.
798
 */
799
int tls_construct_extensions(SSL_CONNECTION *s, WPACKET *pkt,
800
    unsigned int context,
801
    X509 *x, size_t chainidx)
802
143k
{
803
143k
    size_t i;
804
143k
    int min_version, max_version = 0, reason;
805
143k
    const EXTENSION_DEFINITION *thisexd;
806
143k
    int for_comp = (context & SSL_EXT_TLS1_3_CERTIFICATE_COMPRESSION) != 0;
807
808
143k
    if (!WPACKET_start_sub_packet_u16(pkt)
809
        /*
810
         * If extensions are of zero length then we don't even add the
811
         * extensions length bytes to a ClientHello/ServerHello
812
         * (for non-TLSv1.3).
813
         */
814
143k
        || ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0
815
135k
            && !WPACKET_set_flags(pkt,
816
135k
                WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
817
0
        if (!for_comp)
818
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
819
0
        return 0;
820
0
    }
821
822
143k
    if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
823
111k
        reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
824
111k
        if (reason != 0) {
825
0
            if (!for_comp)
826
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
827
0
            return 0;
828
0
        }
829
111k
    }
830
831
    /* Add custom extensions first */
832
143k
    if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
833
        /* On the server side with initialise during ClientHello parsing */
834
111k
        custom_ext_init(&s->cert->custext);
835
111k
    }
836
143k
    if (!custom_ext_add(s, context, pkt, x, chainidx, max_version)) {
837
        /* SSLfatal() already called */
838
0
        return 0;
839
0
    }
840
841
4.30M
    for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
842
4.15M
        EXT_RETURN (*construct)(SSL_CONNECTION *s, WPACKET *pkt,
843
4.15M
            unsigned int context,
844
4.15M
            X509 *x, size_t chainidx);
845
4.15M
        EXT_RETURN ret;
846
847
        /* Skip if not relevant for our context */
848
4.15M
        if (!should_add_extension(s, thisexd->context, context, max_version))
849
760k
            continue;
850
851
3.39M
        construct = s->server ? thisexd->construct_stoc
852
3.39M
                              : thisexd->construct_ctos;
853
854
3.39M
        if (construct == NULL)
855
248k
            continue;
856
857
3.14M
        ret = construct(s, pkt, context, x, chainidx);
858
3.14M
        if (ret == EXT_RETURN_FAIL) {
859
            /* SSLfatal() already called */
860
34
            return 0;
861
34
        }
862
3.14M
        if (ret == EXT_RETURN_SENT
863
1.12M
            && (context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) != 0)
864
1.09M
            s->ext.extflags[i] |= SSL_EXT_FLAG_SENT;
865
3.14M
    }
866
867
143k
    if (!WPACKET_close(pkt)) {
868
0
        if (!for_comp)
869
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
870
0
        return 0;
871
0
    }
872
873
143k
    return 1;
874
143k
}
875
876
/*
877
 * Built in extension finalisation and initialisation functions. All initialise
878
 * or finalise the associated extension type for the given |context|. For
879
 * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0
880
 * otherwise. These functions return 1 on success or 0 on failure.
881
 */
882
883
static int final_renegotiate(SSL_CONNECTION *s, unsigned int context, int sent)
884
79.6k
{
885
79.6k
    if (!s->server) {
886
        /*
887
         * Check if we can connect to a server that doesn't support safe
888
         * renegotiation
889
         */
890
46.5k
        if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
891
46.5k
            && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
892
46.5k
            && !sent) {
893
133
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
894
133
                SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
895
133
            return 0;
896
133
        }
897
898
46.4k
        return 1;
899
46.5k
    }
900
901
    /* Need RI if renegotiating */
902
33.0k
    if (s->renegotiate
903
0
        && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
904
0
        && !sent) {
905
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
906
0
            SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
907
0
        return 0;
908
0
    }
909
910
33.0k
    return 1;
911
33.0k
}
912
913
static ossl_inline void ssl_tsan_decr(const SSL_CTX *ctx,
914
    TSAN_QUALIFIER int *stat)
915
0
{
916
0
    if (ssl_tsan_lock(ctx)) {
917
0
        tsan_decr(stat);
918
0
        ssl_tsan_unlock(ctx);
919
0
    }
920
0
}
921
922
static int init_server_name(SSL_CONNECTION *s, unsigned int context)
923
138k
{
924
138k
    if (s->server) {
925
42.3k
        s->servername_done = 0;
926
927
42.3k
        OPENSSL_free(s->ext.hostname);
928
42.3k
        s->ext.hostname = NULL;
929
42.3k
    }
930
931
138k
    return 1;
932
138k
}
933
934
static int final_server_name(SSL_CONNECTION *s, unsigned int context, int sent)
935
100k
{
936
100k
    int ret = SSL_TLSEXT_ERR_NOACK;
937
100k
    int altmp = SSL_AD_UNRECOGNIZED_NAME;
938
100k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
939
100k
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
940
100k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
941
100k
    int was_ticket = (SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0;
942
943
100k
    if (!ossl_assert(sctx != NULL) || !ossl_assert(s->session_ctx != NULL)) {
944
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
945
0
        return 0;
946
0
    }
947
948
100k
    if (sctx->ext.servername_cb != NULL)
949
0
        ret = sctx->ext.servername_cb(ussl, &altmp,
950
0
            sctx->ext.servername_arg);
951
100k
    else if (s->session_ctx->ext.servername_cb != NULL)
952
0
        ret = s->session_ctx->ext.servername_cb(ussl, &altmp,
953
0
            s->session_ctx->ext.servername_arg);
954
955
    /*
956
     * For servers, propagate the SNI hostname from the temporary
957
     * storage in the SSL to the persistent SSL_SESSION, now that we
958
     * know we accepted it.
959
     * Clients make this copy when parsing the server's response to
960
     * the extension, which is when they find out that the negotiation
961
     * was successful.
962
     */
963
100k
    if (s->server) {
964
33.0k
        if (sent && ret == SSL_TLSEXT_ERR_OK && !s->hit) {
965
            /* Only store the hostname in the session if we accepted it. */
966
0
            OPENSSL_free(s->session->ext.hostname);
967
0
            s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
968
0
            if (s->session->ext.hostname == NULL && s->ext.hostname != NULL) {
969
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
970
0
            }
971
0
        }
972
33.0k
    }
973
974
    /*
975
     * If we switched contexts (whether here or in the client_hello callback),
976
     * move the sess_accept increment from the session_ctx to the new
977
     * context, to avoid the confusing situation of having sess_accept_good
978
     * exceed sess_accept (zero) for the new context.
979
     */
980
100k
    if (SSL_IS_FIRST_HANDSHAKE(s) && sctx != s->session_ctx
981
0
        && s->hello_retry_request == SSL_HRR_NONE) {
982
0
        ssl_tsan_counter(sctx, &sctx->stats.sess_accept);
983
0
        ssl_tsan_decr(s->session_ctx, &s->session_ctx->stats.sess_accept);
984
0
    }
985
986
    /*
987
     * If we're expecting to send a ticket, and tickets were previously enabled,
988
     * and now tickets are disabled, then turn off expected ticket.
989
     * Also, if this is not a resumption, create a new session ID
990
     */
991
100k
    if (ret == SSL_TLSEXT_ERR_OK && s->ext.ticket_expected
992
0
        && was_ticket && (SSL_get_options(ssl) & SSL_OP_NO_TICKET) != 0) {
993
0
        s->ext.ticket_expected = 0;
994
0
        if (!s->hit) {
995
0
            SSL_SESSION *ss = SSL_get_session(ssl);
996
997
0
            if (ss != NULL) {
998
0
                OPENSSL_free(ss->ext.tick);
999
0
                ss->ext.tick = NULL;
1000
0
                ss->ext.ticklen = 0;
1001
0
                ss->ext.tick_lifetime_hint = 0;
1002
0
                ss->ext.tick_age_add = 0;
1003
0
                if (!ssl_generate_session_id(s, ss)) {
1004
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1005
0
                    return 0;
1006
0
                }
1007
0
            } else {
1008
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1009
0
                return 0;
1010
0
            }
1011
0
        }
1012
0
    }
1013
1014
100k
    switch (ret) {
1015
0
    case SSL_TLSEXT_ERR_ALERT_FATAL:
1016
0
        SSLfatal(s, altmp, SSL_R_CALLBACK_FAILED);
1017
0
        return 0;
1018
1019
0
    case SSL_TLSEXT_ERR_ALERT_WARNING:
1020
        /* TLSv1.3 doesn't have warning alerts so we suppress this */
1021
0
        if (!SSL_CONNECTION_IS_TLS13(s))
1022
0
            ssl3_send_alert(s, SSL3_AL_WARNING, altmp);
1023
0
        s->servername_done = 0;
1024
0
        return 1;
1025
1026
100k
    case SSL_TLSEXT_ERR_NOACK:
1027
100k
        s->servername_done = 0;
1028
100k
        return 1;
1029
1030
0
    default:
1031
0
        return 1;
1032
100k
    }
1033
100k
}
1034
1035
static int final_ec_pt_formats(SSL_CONNECTION *s, unsigned int context,
1036
    int sent)
1037
79.5k
{
1038
79.5k
    unsigned long alg_k, alg_a;
1039
1040
79.5k
    if (s->server)
1041
33.0k
        return 1;
1042
1043
46.4k
    alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
1044
46.4k
    alg_a = s->s3.tmp.new_cipher->algorithm_auth;
1045
1046
    /*
1047
     * If we are client and using an elliptic curve cryptography cipher
1048
     * suite, then if server returns an EC point formats lists extension it
1049
     * must contain uncompressed.
1050
     */
1051
46.4k
    if (s->ext.ecpointformats != NULL
1052
0
        && s->ext.ecpointformats_len > 0
1053
0
        && s->ext.peer_ecpointformats != NULL
1054
0
        && s->ext.peer_ecpointformats_len > 0
1055
0
        && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
1056
        /* we are using an ECC cipher */
1057
0
        size_t i;
1058
0
        unsigned char *list = s->ext.peer_ecpointformats;
1059
1060
0
        for (i = 0; i < s->ext.peer_ecpointformats_len; i++) {
1061
0
            if (*list++ == TLSEXT_ECPOINTFORMAT_uncompressed)
1062
0
                break;
1063
0
        }
1064
0
        if (i == s->ext.peer_ecpointformats_len) {
1065
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1066
0
                SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
1067
0
            return 0;
1068
0
        }
1069
0
    }
1070
1071
46.4k
    return 1;
1072
46.4k
}
1073
1074
static int init_session_ticket(SSL_CONNECTION *s, unsigned int context)
1075
116k
{
1076
116k
    if (!s->server)
1077
74.3k
        s->ext.ticket_expected = 0;
1078
1079
116k
    return 1;
1080
116k
}
1081
1082
#ifndef OPENSSL_NO_OCSP
1083
static int init_status_request(SSL_CONNECTION *s, unsigned int context)
1084
133k
{
1085
133k
    if (s->server) {
1086
42.3k
        s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
1087
91.6k
    } else {
1088
        /*
1089
         * Ensure we get sensible values passed to tlsext_status_cb in the event
1090
         * that we don't receive a status message
1091
         */
1092
91.6k
        OPENSSL_free(s->ext.ocsp.resp);
1093
91.6k
        s->ext.ocsp.resp = NULL;
1094
91.6k
        s->ext.ocsp.resp_len = 0;
1095
91.6k
    }
1096
1097
133k
    return 1;
1098
133k
}
1099
#endif
1100
1101
#ifndef OPENSSL_NO_NEXTPROTONEG
1102
static int init_npn(SSL_CONNECTION *s, unsigned int context)
1103
116k
{
1104
116k
    s->s3.npn_seen = 0;
1105
1106
116k
    return 1;
1107
116k
}
1108
#endif
1109
1110
static int init_alpn(SSL_CONNECTION *s, unsigned int context)
1111
138k
{
1112
138k
    OPENSSL_free(s->s3.alpn_selected);
1113
138k
    s->s3.alpn_selected = NULL;
1114
138k
    s->s3.alpn_selected_len = 0;
1115
138k
    if (s->server) {
1116
42.3k
        OPENSSL_free(s->s3.alpn_proposed);
1117
42.3k
        s->s3.alpn_proposed = NULL;
1118
42.3k
        s->s3.alpn_proposed_len = 0;
1119
42.3k
    }
1120
138k
    return 1;
1121
138k
}
1122
1123
static int final_alpn(SSL_CONNECTION *s, unsigned int context, int sent)
1124
100k
{
1125
100k
    if (!s->server && !sent && s->session->ext.alpn_selected != NULL)
1126
0
        s->ext.early_data_ok = 0;
1127
1128
100k
    if (!s->server || !SSL_CONNECTION_IS_TLS13(s))
1129
95.8k
        return 1;
1130
1131
    /*
1132
     * Call alpn_select callback if needed.  Has to be done after SNI and
1133
     * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
1134
     * we also have to do this before we decide whether to accept early_data.
1135
     * In TLSv1.3 we've already negotiated our cipher so we do this call now.
1136
     * For < TLSv1.3 we defer it until after cipher negotiation.
1137
     *
1138
     * On failure SSLfatal() already called.
1139
     */
1140
4.15k
    return tls_handle_alpn(s);
1141
100k
}
1142
1143
static int init_sig_algs(SSL_CONNECTION *s, unsigned int context)
1144
42.4k
{
1145
    /* Clear any signature algorithms extension received */
1146
42.4k
    OPENSSL_free(s->s3.tmp.peer_sigalgs);
1147
42.4k
    s->s3.tmp.peer_sigalgs = NULL;
1148
42.4k
    s->s3.tmp.peer_sigalgslen = 0;
1149
1150
42.4k
    return 1;
1151
42.4k
}
1152
1153
static int init_sig_algs_cert(SSL_CONNECTION *s,
1154
    ossl_unused unsigned int context)
1155
42.4k
{
1156
    /* Clear any signature algorithms extension received */
1157
42.4k
    OPENSSL_free(s->s3.tmp.peer_cert_sigalgs);
1158
42.4k
    s->s3.tmp.peer_cert_sigalgs = NULL;
1159
42.4k
    s->s3.tmp.peer_cert_sigalgslen = 0;
1160
1161
42.4k
    return 1;
1162
42.4k
}
1163
1164
#ifndef OPENSSL_NO_SRP
1165
static int init_srp(SSL_CONNECTION *s, unsigned int context)
1166
41.9k
{
1167
41.9k
    OPENSSL_free(s->srp_ctx.login);
1168
41.9k
    s->srp_ctx.login = NULL;
1169
1170
41.9k
    return 1;
1171
41.9k
}
1172
#endif
1173
1174
static int init_ec_point_formats(SSL_CONNECTION *s, unsigned int context)
1175
116k
{
1176
116k
    OPENSSL_free(s->ext.peer_ecpointformats);
1177
116k
    s->ext.peer_ecpointformats = NULL;
1178
116k
    s->ext.peer_ecpointformats_len = 0;
1179
1180
116k
    return 1;
1181
116k
}
1182
1183
static int init_etm(SSL_CONNECTION *s, unsigned int context)
1184
116k
{
1185
116k
    s->ext.use_etm = 0;
1186
1187
116k
    return 1;
1188
116k
}
1189
1190
static int init_ems(SSL_CONNECTION *s, unsigned int context)
1191
116k
{
1192
116k
    if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1193
63
        s->s3.flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
1194
63
        s->s3.flags |= TLS1_FLAGS_REQUIRED_EXTMS;
1195
63
    }
1196
1197
116k
    return 1;
1198
116k
}
1199
1200
static int final_ems(SSL_CONNECTION *s, unsigned int context, int sent)
1201
79.5k
{
1202
    /*
1203
     * Check extended master secret extension is not dropped on
1204
     * renegotiation.
1205
     */
1206
79.5k
    if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)
1207
66.2k
        && (s->s3.flags & TLS1_FLAGS_REQUIRED_EXTMS)) {
1208
3
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_EXTMS);
1209
3
        return 0;
1210
3
    }
1211
79.5k
    if (!s->server && s->hit) {
1212
        /*
1213
         * Check extended master secret extension is consistent with
1214
         * original session.
1215
         */
1216
0
        if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) != !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
1217
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_EXTMS);
1218
0
            return 0;
1219
0
        }
1220
0
    }
1221
1222
79.5k
    return 1;
1223
79.5k
}
1224
1225
static int init_certificate_authorities(SSL_CONNECTION *s, unsigned int context)
1226
540
{
1227
540
    sk_X509_NAME_pop_free(s->s3.tmp.peer_ca_names, X509_NAME_free);
1228
540
    s->s3.tmp.peer_ca_names = NULL;
1229
540
    return 1;
1230
540
}
1231
1232
static EXT_RETURN tls_construct_certificate_authorities(SSL_CONNECTION *s,
1233
    WPACKET *pkt,
1234
    unsigned int context,
1235
    X509 *x,
1236
    size_t chainidx)
1237
90.4k
{
1238
90.4k
    const STACK_OF(X509_NAME) *ca_sk = get_ca_names(s);
1239
1240
90.4k
    if (ca_sk == NULL || sk_X509_NAME_num(ca_sk) == 0)
1241
90.4k
        return EXT_RETURN_NOT_SENT;
1242
1243
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_certificate_authorities)
1244
0
        || !WPACKET_start_sub_packet_u16(pkt)) {
1245
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1246
0
        return EXT_RETURN_FAIL;
1247
0
    }
1248
1249
0
    if (!construct_ca_names(s, ca_sk, pkt)) {
1250
        /* SSLfatal() already called */
1251
0
        return EXT_RETURN_FAIL;
1252
0
    }
1253
1254
0
    if (!WPACKET_close(pkt)) {
1255
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1256
0
        return EXT_RETURN_FAIL;
1257
0
    }
1258
1259
0
    return EXT_RETURN_SENT;
1260
0
}
1261
1262
static int tls_parse_certificate_authorities(SSL_CONNECTION *s, PACKET *pkt,
1263
    unsigned int context, X509 *x,
1264
    size_t chainidx)
1265
251
{
1266
251
    if (!parse_ca_names(s, pkt))
1267
225
        return 0;
1268
26
    if (PACKET_remaining(pkt) != 0) {
1269
14
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1270
14
        return 0;
1271
14
    }
1272
12
    return 1;
1273
26
}
1274
1275
#ifndef OPENSSL_NO_SRTP
1276
static int init_srtp(SSL_CONNECTION *s, unsigned int context)
1277
138k
{
1278
138k
    if (s->server)
1279
42.3k
        s->srtp_profile = NULL;
1280
1281
138k
    return 1;
1282
138k
}
1283
#endif
1284
1285
static int final_sig_algs(SSL_CONNECTION *s, unsigned int context, int sent)
1286
33.1k
{
1287
33.1k
    if (!sent && SSL_CONNECTION_IS_TLS13(s) && !s->hit) {
1288
82
        SSLfatal(s, TLS13_AD_MISSING_EXTENSION,
1289
82
            SSL_R_MISSING_SIGALGS_EXTENSION);
1290
82
        return 0;
1291
82
    }
1292
1293
33.0k
    return 1;
1294
33.1k
}
1295
1296
static int final_supported_versions(SSL_CONNECTION *s, unsigned int context,
1297
    int sent)
1298
44.1k
{
1299
44.1k
    if (!sent && context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) {
1300
41
        SSLfatal(s, TLS13_AD_MISSING_EXTENSION,
1301
41
            SSL_R_MISSING_SUPPORTED_VERSIONS_EXTENSION);
1302
41
        return 0;
1303
41
    }
1304
1305
44.1k
    return 1;
1306
44.1k
}
1307
1308
static int final_key_share(SSL_CONNECTION *s, unsigned int context, int sent)
1309
32.6k
{
1310
32.6k
#if !defined(OPENSSL_NO_TLS1_3)
1311
32.6k
    if (!SSL_CONNECTION_IS_TLS13(s))
1312
15.7k
        return 1;
1313
1314
    /* Nothing to do for key_share in an HRR */
1315
16.8k
    if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0)
1316
0
        return 1;
1317
1318
    /*
1319
     * If
1320
     *     we are a client
1321
     *     AND
1322
     *     we have no key_share
1323
     *     AND
1324
     *     (we are not resuming
1325
     *      OR the kex_mode doesn't allow non key_share resumes)
1326
     * THEN
1327
     *     fail;
1328
     */
1329
16.8k
    if (!s->server
1330
14.6k
        && !sent) {
1331
17
        if ((s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0) {
1332
17
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_SUITABLE_KEY_SHARE);
1333
17
            return 0;
1334
17
        }
1335
0
        if (!s->hit) {
1336
0
            SSLfatal(s, SSL_AD_MISSING_EXTENSION, SSL_R_NO_SUITABLE_KEY_SHARE);
1337
0
            return 0;
1338
0
        }
1339
0
    }
1340
    /*
1341
     * IF
1342
     *     we are a server
1343
     * THEN
1344
     *     IF
1345
     *         we have a suitable key_share
1346
     *     THEN
1347
     *         IF
1348
     *             we are stateless AND we have no cookie
1349
     *         THEN
1350
     *             send a HelloRetryRequest
1351
     *     ELSE
1352
     *         IF
1353
     *             we didn't already send a HelloRetryRequest
1354
     *             AND
1355
     *             the client sent a key_share extension
1356
     *             AND
1357
     *             (we are not resuming
1358
     *              OR the kex_mode allows key_share resumes)
1359
     *             AND
1360
     *             a shared group exists
1361
     *         THEN
1362
     *             send a HelloRetryRequest
1363
     *         ELSE IF
1364
     *             we are not resuming
1365
     *             OR
1366
     *             the kex_mode doesn't allow non key_share resumes
1367
     *         THEN
1368
     *             fail
1369
     *         ELSE IF
1370
     *             we are stateless AND we have no cookie
1371
     *         THEN
1372
     *             send a HelloRetryRequest
1373
     */
1374
16.8k
    if (s->server) {
1375
2.14k
        if (s->s3.peer_tmp != NULL) {
1376
            /* We have a suitable key_share */
1377
1.65k
            if ((s->s3.flags & TLS1_FLAGS_STATELESS) != 0
1378
0
                && !s->ext.cookieok) {
1379
0
                if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) {
1380
                    /*
1381
                     * If we are stateless then we wouldn't know about any
1382
                     * previously sent HRR - so how can this be anything other
1383
                     * than 0?
1384
                     */
1385
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1386
0
                    return 0;
1387
0
                }
1388
0
                s->hello_retry_request = SSL_HRR_PENDING;
1389
0
                return 1;
1390
0
            }
1391
1.65k
        } else {
1392
            /* No suitable key_share */
1393
487
            if (s->hello_retry_request == SSL_HRR_NONE && sent
1394
419
                && (!s->hit
1395
419
                    || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) != 0)) {
1396
1397
                /* Did we detect group overlap in tls_parse_ctos_key_share ? */
1398
419
                if (s->s3.group_id_candidate != 0) {
1399
                    /* A shared group exists so send a HelloRetryRequest */
1400
415
                    s->s3.group_id = s->s3.group_id_candidate;
1401
415
                    s->hello_retry_request = SSL_HRR_PENDING;
1402
415
                    return 1;
1403
415
                }
1404
419
            }
1405
72
            if (!s->hit
1406
72
                || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0) {
1407
                /* Nothing left we can do - just fail */
1408
72
                SSLfatal(s, sent ? SSL_AD_HANDSHAKE_FAILURE : SSL_AD_MISSING_EXTENSION,
1409
72
                    SSL_R_NO_SUITABLE_KEY_SHARE);
1410
72
                return 0;
1411
72
            }
1412
1413
0
            if ((s->s3.flags & TLS1_FLAGS_STATELESS) != 0
1414
0
                && !s->ext.cookieok) {
1415
0
                if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) {
1416
                    /*
1417
                     * If we are stateless then we wouldn't know about any
1418
                     * previously sent HRR - so how can this be anything other
1419
                     * than 0?
1420
                     */
1421
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1422
0
                    return 0;
1423
0
                }
1424
0
                s->hello_retry_request = SSL_HRR_PENDING;
1425
0
                return 1;
1426
0
            }
1427
0
        }
1428
1429
        /*
1430
         * We have a key_share so don't send any more HelloRetryRequest
1431
         * messages
1432
         */
1433
1.65k
        if (s->hello_retry_request == SSL_HRR_PENDING)
1434
92
            s->hello_retry_request = SSL_HRR_COMPLETE;
1435
14.6k
    } else {
1436
        /*
1437
         * For a client side resumption with no key_share we need to generate
1438
         * the handshake secret (otherwise this is done during key_share
1439
         * processing).
1440
         */
1441
14.6k
        if (!sent && !tls13_generate_handshake_secret(s, NULL, 0)) {
1442
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1443
0
            return 0;
1444
0
        }
1445
14.6k
    }
1446
16.3k
#endif /* !defined(OPENSSL_NO_TLS1_3) */
1447
16.3k
    return 1;
1448
16.8k
}
1449
1450
static int init_psk_kex_modes(SSL_CONNECTION *s, unsigned int context)
1451
416
{
1452
416
    s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_NONE;
1453
416
    return 1;
1454
416
}
1455
1456
int tls_psk_do_binder(SSL_CONNECTION *s, const EVP_MD *md,
1457
    const unsigned char *msgstart,
1458
    size_t binderoffset, const unsigned char *binderin,
1459
    unsigned char *binderout, SSL_SESSION *sess, int sign,
1460
    int external)
1461
35
{
1462
35
    EVP_PKEY *mackey = NULL;
1463
35
    EVP_MD_CTX *mctx = NULL;
1464
35
    unsigned char hash[EVP_MAX_MD_SIZE], binderkey[EVP_MAX_MD_SIZE];
1465
35
    unsigned char finishedkey[EVP_MAX_MD_SIZE], tmpbinder[EVP_MAX_MD_SIZE];
1466
35
    unsigned char *early_secret;
1467
    /* ASCII: "res binder", in hex for EBCDIC compatibility */
1468
35
    static const unsigned char resumption_label[] = "\x72\x65\x73\x20\x62\x69\x6E\x64\x65\x72";
1469
    /* ASCII: "ext binder", in hex for EBCDIC compatibility */
1470
35
    static const unsigned char external_label[] = "\x65\x78\x74\x20\x62\x69\x6E\x64\x65\x72";
1471
35
    const unsigned char *label;
1472
35
    size_t bindersize, labelsize, hashsize;
1473
35
    int hashsizei = EVP_MD_get_size(md);
1474
35
    int ret = -1;
1475
35
    int usepskfored = 0;
1476
35
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1477
1478
    /* Ensure cast to size_t is safe */
1479
35
    if (!ossl_assert(hashsizei > 0)) {
1480
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1481
0
        goto err;
1482
0
    }
1483
35
    hashsize = (size_t)hashsizei;
1484
1485
35
    if (external
1486
0
        && s->early_data_state == SSL_EARLY_DATA_CONNECTING
1487
0
        && s->session->ext.max_early_data == 0
1488
0
        && sess->ext.max_early_data > 0)
1489
0
        usepskfored = 1;
1490
1491
35
    if (external) {
1492
0
        label = external_label;
1493
0
        labelsize = sizeof(external_label) - 1;
1494
35
    } else {
1495
35
        label = resumption_label;
1496
35
        labelsize = sizeof(resumption_label) - 1;
1497
35
    }
1498
1499
    /*
1500
     * Generate the early_secret. On the server side we've selected a PSK to
1501
     * resume with (internal or external) so we always do this. On the client
1502
     * side we do this for a non-external (i.e. resumption) PSK or external PSK
1503
     * that will be used for early_data so that it is in place for sending early
1504
     * data. For client side external PSK not being used for early_data we
1505
     * generate it but store it away for later use.
1506
     */
1507
35
    if (s->server || !external || usepskfored)
1508
35
        early_secret = (unsigned char *)s->early_secret;
1509
0
    else
1510
0
        early_secret = (unsigned char *)sess->early_secret;
1511
1512
35
    if (!tls13_generate_secret(s, md, NULL, sess->master_key,
1513
35
            sess->master_key_length, early_secret)) {
1514
        /* SSLfatal() already called */
1515
0
        goto err;
1516
0
    }
1517
1518
    /*
1519
     * Create the handshake hash for the binder key...the messages so far are
1520
     * empty!
1521
     */
1522
35
    mctx = EVP_MD_CTX_new();
1523
35
    if (mctx == NULL
1524
35
        || EVP_DigestInit_ex(mctx, md, NULL) <= 0
1525
35
        || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
1526
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1527
0
        goto err;
1528
0
    }
1529
1530
    /* Generate the binder key */
1531
35
    if (!tls13_hkdf_expand(s, md, early_secret, label, labelsize, hash,
1532
35
            hashsize, binderkey, hashsize, 1)) {
1533
        /* SSLfatal() already called */
1534
0
        goto err;
1535
0
    }
1536
1537
    /* Generate the finished key */
1538
35
    if (!tls13_derive_finishedkey(s, md, binderkey, finishedkey, hashsize)) {
1539
        /* SSLfatal() already called */
1540
0
        goto err;
1541
0
    }
1542
1543
35
    if (EVP_DigestInit_ex(mctx, md, NULL) <= 0) {
1544
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1545
0
        goto err;
1546
0
    }
1547
1548
    /*
1549
     * Get a hash of the ClientHello up to the start of the binders. If we are
1550
     * following a HelloRetryRequest then this includes the hash of the first
1551
     * ClientHello and the HelloRetryRequest itself.
1552
     */
1553
35
    if (s->hello_retry_request == SSL_HRR_PENDING) {
1554
0
        size_t hdatalen;
1555
0
        long hdatalen_l;
1556
0
        void *hdata;
1557
1558
0
        hdatalen = hdatalen_l = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
1559
0
        if (hdatalen_l <= 0) {
1560
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH);
1561
0
            goto err;
1562
0
        }
1563
1564
        /*
1565
         * For servers the handshake buffer data will include the second
1566
         * ClientHello - which we don't want - so we need to take that bit off.
1567
         */
1568
0
        if (s->server) {
1569
0
            PACKET hashprefix, msg;
1570
1571
            /* Find how many bytes are left after the first two messages */
1572
0
            if (!PACKET_buf_init(&hashprefix, hdata, hdatalen)
1573
0
                || !PACKET_forward(&hashprefix, 1)
1574
0
                || !PACKET_get_length_prefixed_3(&hashprefix, &msg)
1575
0
                || !PACKET_forward(&hashprefix, 1)
1576
0
                || !PACKET_get_length_prefixed_3(&hashprefix, &msg)) {
1577
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1578
0
                goto err;
1579
0
            }
1580
0
            hdatalen -= PACKET_remaining(&hashprefix);
1581
0
        }
1582
1583
0
        if (EVP_DigestUpdate(mctx, hdata, hdatalen) <= 0) {
1584
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1585
0
            goto err;
1586
0
        }
1587
0
    }
1588
1589
35
    if (EVP_DigestUpdate(mctx, msgstart, binderoffset) <= 0
1590
35
        || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
1591
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1592
0
        goto err;
1593
0
    }
1594
1595
35
    mackey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",
1596
35
        sctx->propq, finishedkey,
1597
35
        hashsize);
1598
35
    if (mackey == NULL) {
1599
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1600
0
        goto err;
1601
0
    }
1602
1603
35
    if (!sign)
1604
35
        binderout = tmpbinder;
1605
1606
35
    bindersize = hashsize;
1607
35
    if (EVP_DigestSignInit_ex(mctx, NULL, EVP_MD_get0_name(md), sctx->libctx,
1608
35
            sctx->propq, mackey, NULL)
1609
35
            <= 0
1610
35
        || EVP_DigestSignUpdate(mctx, hash, hashsize) <= 0
1611
35
        || EVP_DigestSignFinal(mctx, binderout, &bindersize) <= 0
1612
35
        || bindersize != hashsize) {
1613
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1614
0
        goto err;
1615
0
    }
1616
1617
35
    if (sign) {
1618
0
        ret = 1;
1619
35
    } else {
1620
        /* HMAC keys can't do EVP_DigestVerify* - use CRYPTO_memcmp instead */
1621
35
        ret = (CRYPTO_memcmp(binderin, binderout, hashsize) == 0);
1622
35
        if (!ret)
1623
35
            SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BINDER_DOES_NOT_VERIFY);
1624
35
    }
1625
1626
35
err:
1627
35
    OPENSSL_cleanse(binderkey, sizeof(binderkey));
1628
35
    OPENSSL_cleanse(finishedkey, sizeof(finishedkey));
1629
35
    EVP_PKEY_free(mackey);
1630
35
    EVP_MD_CTX_free(mctx);
1631
1632
35
    return ret;
1633
35
}
1634
1635
static int final_early_data(SSL_CONNECTION *s, unsigned int context, int sent)
1636
55.4k
{
1637
55.4k
    if (!sent)
1638
52.5k
        return 1;
1639
1640
2.89k
    if (!s->server) {
1641
0
        if (context == SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
1642
0
            && sent
1643
0
            && !s->ext.early_data_ok) {
1644
            /*
1645
             * If we get here then the server accepted our early_data but we
1646
             * later realised that it shouldn't have done (e.g. inconsistent
1647
             * ALPN)
1648
             */
1649
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EARLY_DATA);
1650
0
            return 0;
1651
0
        }
1652
1653
0
        return 1;
1654
0
    }
1655
1656
2.89k
    if (s->max_early_data == 0
1657
0
        || !s->hit
1658
0
        || s->early_data_state != SSL_EARLY_DATA_ACCEPTING
1659
0
        || !s->ext.early_data_ok
1660
0
        || s->hello_retry_request != SSL_HRR_NONE
1661
0
        || (s->allow_early_data_cb != NULL
1662
0
            && !s->allow_early_data_cb(SSL_CONNECTION_GET_USER_SSL(s),
1663
2.89k
                s->allow_early_data_cb_data))) {
1664
2.89k
        s->ext.early_data = SSL_EARLY_DATA_REJECTED;
1665
2.89k
    } else {
1666
0
        s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
1667
1668
0
        if (!tls13_change_cipher_state(s,
1669
0
                SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_SERVER_READ)) {
1670
            /* SSLfatal() already called */
1671
0
            return 0;
1672
0
        }
1673
0
    }
1674
1675
2.89k
    return 1;
1676
2.89k
}
1677
1678
static int final_maxfragmentlen(SSL_CONNECTION *s, unsigned int context,
1679
    int sent)
1680
91.4k
{
1681
91.4k
    if (s->session == NULL)
1682
0
        return 1;
1683
1684
    /* MaxFragmentLength defaults to disabled */
1685
91.4k
    if (s->session->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_UNSPECIFIED)
1686
90.2k
        s->session->ext.max_fragment_len_mode = TLSEXT_max_fragment_length_DISABLED;
1687
1688
91.4k
    if (USE_MAX_FRAGMENT_LENGTH_EXT(s->session)) {
1689
1.03k
        s->rlayer.rrlmethod->set_max_frag_len(s->rlayer.rrl,
1690
1.03k
            GET_MAX_FRAGMENT_LENGTH(s->session));
1691
1.03k
        s->rlayer.wrlmethod->set_max_frag_len(s->rlayer.wrl,
1692
1.03k
            ssl_get_max_send_fragment(s));
1693
1.03k
    }
1694
1695
91.4k
    return 1;
1696
91.4k
}
1697
1698
static int init_post_handshake_auth(SSL_CONNECTION *s,
1699
    ossl_unused unsigned int context)
1700
416
{
1701
416
    s->post_handshake_auth = SSL_PHA_NONE;
1702
1703
416
    return 1;
1704
416
}
1705
1706
/*
1707
 * If clients offer "pre_shared_key" without a "psk_key_exchange_modes"
1708
 * extension, servers MUST abort the handshake.
1709
 */
1710
static int final_psk(SSL_CONNECTION *s, unsigned int context, int sent)
1711
58.1k
{
1712
58.1k
    if (s->server && sent && s->clienthello != NULL
1713
79
        && !s->clienthello->pre_proc_exts[TLSEXT_IDX_psk_kex_modes].present) {
1714
16
        SSLfatal(s, TLS13_AD_MISSING_EXTENSION,
1715
16
            SSL_R_MISSING_PSK_KEX_MODES_EXTENSION);
1716
16
        return 0;
1717
16
    }
1718
1719
58.1k
    return 1;
1720
58.1k
}
1721
1722
static int tls_init_compress_certificate(SSL_CONNECTION *sc, unsigned int context)
1723
457
{
1724
457
    memset(sc->ext.compress_certificate_from_peer, 0,
1725
457
        sizeof(sc->ext.compress_certificate_from_peer));
1726
457
    return 1;
1727
457
}
1728
1729
/* The order these are put into the packet imply a preference order: [brotli, zlib, zstd] */
1730
static EXT_RETURN tls_construct_compress_certificate(SSL_CONNECTION *sc, WPACKET *pkt,
1731
    unsigned int context,
1732
    X509 *x, size_t chainidx)
1733
83.6k
{
1734
#ifndef OPENSSL_NO_COMP_ALG
1735
    int i;
1736
1737
    if (!ossl_comp_has_alg(0))
1738
        return EXT_RETURN_NOT_SENT;
1739
1740
    /* Server: Don't attempt to compress a non-X509 (i.e. an RPK) */
1741
    if (sc->server && sc->ext.server_cert_type != TLSEXT_cert_type_x509) {
1742
        sc->cert_comp_prefs[0] = TLSEXT_comp_cert_none;
1743
        return EXT_RETURN_NOT_SENT;
1744
    }
1745
1746
    /* Client: If we sent a client cert-type extension, don't indicate compression */
1747
    if (!sc->server && sc->ext.client_cert_type_ctos) {
1748
        sc->cert_comp_prefs[0] = TLSEXT_comp_cert_none;
1749
        return EXT_RETURN_NOT_SENT;
1750
    }
1751
1752
    /* Do not indicate we support receiving compressed certificates */
1753
    if ((sc->options & SSL_OP_NO_RX_CERTIFICATE_COMPRESSION) != 0)
1754
        return EXT_RETURN_NOT_SENT;
1755
1756
    if (sc->cert_comp_prefs[0] == TLSEXT_comp_cert_none)
1757
        return EXT_RETURN_NOT_SENT;
1758
1759
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_compress_certificate)
1760
        || !WPACKET_start_sub_packet_u16(pkt)
1761
        || !WPACKET_start_sub_packet_u8(pkt))
1762
        goto err;
1763
1764
    for (i = 0; sc->cert_comp_prefs[i] != TLSEXT_comp_cert_none; i++) {
1765
        if (!WPACKET_put_bytes_u16(pkt, sc->cert_comp_prefs[i]))
1766
            goto err;
1767
    }
1768
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt))
1769
        goto err;
1770
1771
    sc->ext.compress_certificate_sent = 1;
1772
    return EXT_RETURN_SENT;
1773
err:
1774
    SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1775
    return EXT_RETURN_FAIL;
1776
#else
1777
83.6k
    return EXT_RETURN_NOT_SENT;
1778
83.6k
#endif
1779
83.6k
}
1780
1781
#ifndef OPENSSL_NO_COMP_ALG
1782
static int tls_comp_in_pref(SSL_CONNECTION *sc, int alg)
1783
{
1784
    int i;
1785
1786
    /* ossl_comp_has_alg() considers 0 as "any" */
1787
    if (alg == 0)
1788
        return 0;
1789
    /* Make sure algorithm is enabled */
1790
    if (!ossl_comp_has_alg(alg))
1791
        return 0;
1792
    /* If no preferences are set, it's ok */
1793
    if (sc->cert_comp_prefs[0] == TLSEXT_comp_cert_none)
1794
        return 1;
1795
    /* Find the algorithm */
1796
    for (i = 0; i < TLSEXT_comp_cert_limit; i++)
1797
        if (sc->cert_comp_prefs[i] == alg)
1798
            return 1;
1799
    return 0;
1800
}
1801
#endif
1802
1803
int tls_parse_compress_certificate(SSL_CONNECTION *sc, PACKET *pkt, unsigned int context,
1804
    X509 *x, size_t chainidx)
1805
77
{
1806
#ifndef OPENSSL_NO_COMP_ALG
1807
    PACKET supported_comp_algs;
1808
    unsigned int comp;
1809
    int already_set[TLSEXT_comp_cert_limit];
1810
    int j = 0;
1811
1812
    /* If no algorithms are available, ignore the extension */
1813
    if (!ossl_comp_has_alg(0))
1814
        return 1;
1815
1816
    /* Don't attempt to compress a non-X509 (i.e. an RPK) */
1817
    if (sc->server && sc->ext.server_cert_type != TLSEXT_cert_type_x509)
1818
        return 1;
1819
    if (!sc->server && sc->ext.client_cert_type != TLSEXT_cert_type_x509)
1820
        return 1;
1821
1822
    /* Ignore the extension and don't send compressed certificates */
1823
    if ((sc->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0)
1824
        return 1;
1825
1826
    if (!PACKET_as_length_prefixed_1(pkt, &supported_comp_algs)
1827
        || PACKET_remaining(&supported_comp_algs) == 0) {
1828
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1829
        return 0;
1830
    }
1831
1832
    memset(already_set, 0, sizeof(already_set));
1833
    /*
1834
     * The preference array has real values, so take a look at each
1835
     * value coming in, and make sure it's in our preference list
1836
     * The array is 0 (i.e. "none") terminated
1837
     * The preference list only contains supported algorithms
1838
     */
1839
    while (PACKET_get_net_2(&supported_comp_algs, &comp)) {
1840
        if (tls_comp_in_pref(sc, comp) && !already_set[comp]) {
1841
            sc->ext.compress_certificate_from_peer[j++] = comp;
1842
            already_set[comp] = 1;
1843
        }
1844
    }
1845
    if (PACKET_remaining(&supported_comp_algs) != 0) {
1846
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1847
        return 0;
1848
    }
1849
#endif
1850
77
    return 1;
1851
77
}
1852
1853
static int init_server_cert_type(SSL_CONNECTION *sc, unsigned int context)
1854
128k
{
1855
    /* Only reset when parsing client hello */
1856
128k
    if (sc->server) {
1857
38.1k
        sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
1858
38.1k
        sc->ext.server_cert_type = TLSEXT_cert_type_x509;
1859
38.1k
    }
1860
128k
    return 1;
1861
128k
}
1862
1863
static int init_client_cert_type(SSL_CONNECTION *sc, unsigned int context)
1864
128k
{
1865
    /* Only reset when parsing client hello */
1866
128k
    if (sc->server) {
1867
38.1k
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
1868
38.1k
        sc->ext.client_cert_type = TLSEXT_cert_type_x509;
1869
38.1k
    }
1870
128k
    return 1;
1871
128k
}