Coverage Report

Created: 2025-12-31 06:58

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