Coverage Report

Created: 2023-06-08 06:41

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