Coverage Report

Created: 2023-06-08 06:41

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