Coverage Report

Created: 2026-03-09 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/ssl/statem/extensions_srvr.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <openssl/ocsp.h>
11
#include "../ssl_local.h"
12
#include "statem_local.h"
13
#include "internal/cryptlib.h"
14
#include "internal/ssl_unwrap.h"
15
#ifndef OPENSSL_NO_ECH
16
#include <openssl/rand.h>
17
#include <openssl/trace.h>
18
#endif
19
20
0
#define COOKIE_STATE_FORMAT_VERSION 1
21
22
0
#define MAX_SUPPORTED_GROUPS 128
23
0
#define MAX_KEY_SHARES 16
24
25
/*
26
 * 2 bytes for packet length, 2 bytes for format version, 2 bytes for
27
 * protocol version, 2 bytes for group id, 2 bytes for cipher id, 1 byte for
28
 * key_share present flag, 8 bytes for timestamp, 2 bytes for the hashlen,
29
 * EVP_MAX_MD_SIZE for transcript hash, 1 byte for app cookie length, app cookie
30
 * length bytes, SHA256_DIGEST_LENGTH bytes for the HMAC of the whole thing.
31
 */
32
0
#define MAX_COOKIE_SIZE (2 + 2 + 2 + 2 + 2 + 1 + 8 + 2 + EVP_MAX_MD_SIZE + 1 \
33
0
    + SSL_COOKIE_LENGTH + SHA256_DIGEST_LENGTH)
34
35
/*
36
 * Message header + 2 bytes for protocol version + number of random bytes +
37
 * + 1 byte for legacy session id length + number of bytes in legacy session id
38
 * + 2 bytes for ciphersuite + 1 byte for legacy compression
39
 * + 2 bytes for extension block length + 6 bytes for key_share extension
40
 * + 4 bytes for cookie extension header + the number of bytes in the cookie
41
 */
42
#define MAX_HRR_SIZE (SSL3_HM_HEADER_LENGTH + 2 + SSL3_RANDOM_SIZE + 1 \
43
    + SSL_MAX_SSL_SESSION_ID_LENGTH + 2 + 1 + 2 + 6 + 4                \
44
    + MAX_COOKIE_SIZE)
45
46
/*
47
 * Parse the client's renegotiation binding and abort if it's not right
48
 */
49
int tls_parse_ctos_renegotiate(SSL_CONNECTION *s, PACKET *pkt,
50
    unsigned int context,
51
    X509 *x, size_t chainidx)
52
0
{
53
0
    unsigned int ilen;
54
0
    const unsigned char *data;
55
0
    int ok;
56
57
    /* Parse the length byte */
58
0
    if (!PACKET_get_1(pkt, &ilen)
59
0
        || !PACKET_get_bytes(pkt, &data, ilen)) {
60
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR);
61
0
        return 0;
62
0
    }
63
64
    /* Check that the extension matches */
65
0
    if (ilen != s->s3.previous_client_finished_len) {
66
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH);
67
0
        return 0;
68
0
    }
69
70
0
    ok = memcmp(data, s->s3.previous_client_finished,
71
0
        s->s3.previous_client_finished_len);
72
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
73
0
    if (ok) {
74
0
        if ((data[0] ^ s->s3.previous_client_finished[0]) != 0xFF) {
75
0
            ok = 0;
76
0
        }
77
0
    }
78
0
#endif
79
0
    if (ok) {
80
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH);
81
0
        return 0;
82
0
    }
83
84
0
    s->s3.send_connection_binding = 1;
85
86
0
    return 1;
87
0
}
88
89
/*-
90
 * The servername extension is treated as follows:
91
 *
92
 * - Only the hostname type is supported with a maximum length of 255.
93
 * - The servername is rejected if too long or if it contains zeros,
94
 *   in which case an fatal alert is generated.
95
 * - The servername field is maintained together with the session cache.
96
 * - When a session is resumed, the servername call back invoked in order
97
 *   to allow the application to position itself to the right context.
98
 * - The servername is acknowledged if it is new for a session or when
99
 *   it is identical to a previously used for the same session.
100
 *   Applications can control the behaviour.  They can at any time
101
 *   set a 'desirable' servername for a new SSL object. This can be the
102
 *   case for example with HTTPS when a Host: header field is received and
103
 *   a renegotiation is requested. In this case, a possible servername
104
 *   presented in the new client hello is only acknowledged if it matches
105
 *   the value of the Host: field.
106
 * - Applications must  use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
107
 *   if they provide for changing an explicit servername context for the
108
 *   session, i.e. when the session has been established with a servername
109
 *   extension.
110
 * - On session reconnect, the servername extension may be absent.
111
 */
112
int tls_parse_ctos_server_name(SSL_CONNECTION *s, PACKET *pkt,
113
    unsigned int context, X509 *x, size_t chainidx)
114
0
{
115
0
    unsigned int servname_type;
116
0
    PACKET sni, hostname;
117
118
0
    if (!PACKET_as_length_prefixed_2(pkt, &sni)
119
        /* ServerNameList must be at least 1 byte long. */
120
0
        || PACKET_remaining(&sni) == 0) {
121
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
122
0
        return 0;
123
0
    }
124
125
    /*
126
     * Although the intent was for server_name to be extensible, RFC 4366
127
     * was not clear about it; and so OpenSSL among other implementations,
128
     * always and only allows a 'host_name' name types.
129
     * RFC 6066 corrected the mistake but adding new name types
130
     * is nevertheless no longer feasible, so act as if no other
131
     * SNI types can exist, to simplify parsing.
132
     *
133
     * Also note that the RFC permits only one SNI value per type,
134
     * i.e., we can only have a single hostname.
135
     */
136
0
    if (!PACKET_get_1(&sni, &servname_type)
137
0
        || servname_type != TLSEXT_NAMETYPE_host_name
138
0
        || !PACKET_as_length_prefixed_2(&sni, &hostname)) {
139
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
140
0
        return 0;
141
0
    }
142
143
    /*
144
     * In TLSv1.2 and below the SNI is associated with the session. In TLSv1.3
145
     * we always use the SNI value from the handshake.
146
     */
147
0
    if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {
148
0
        if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) {
149
0
            SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION);
150
0
            return 0;
151
0
        }
152
153
0
        if (PACKET_contains_zero_byte(&hostname)) {
154
0
            SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION);
155
0
            return 0;
156
0
        }
157
158
        /*
159
         * Store the requested SNI in the SSL as temporary storage.
160
         * If we accept it, it will get stored in the SSL_SESSION as well.
161
         */
162
0
        OPENSSL_free(s->ext.hostname);
163
0
        s->ext.hostname = NULL;
164
0
        if (!PACKET_strndup(&hostname, &s->ext.hostname)) {
165
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
166
0
            return 0;
167
0
        }
168
169
0
        s->servername_done = 1;
170
0
    } else {
171
        /*
172
         * In TLSv1.2 and below we should check if the SNI is consistent between
173
         * the initial handshake and the resumption. In TLSv1.3 SNI is not
174
         * associated with the session.
175
         */
176
0
        s->servername_done = (s->session->ext.hostname != NULL)
177
0
            && PACKET_equal(&hostname, s->session->ext.hostname,
178
0
                strlen(s->session->ext.hostname));
179
0
    }
180
181
0
    return 1;
182
0
}
183
184
int tls_parse_ctos_maxfragmentlen(SSL_CONNECTION *s, PACKET *pkt,
185
    unsigned int context,
186
    X509 *x, size_t chainidx)
187
0
{
188
0
    unsigned int value;
189
190
0
    if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
191
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
192
0
        return 0;
193
0
    }
194
195
    /* Received |value| should be a valid max-fragment-length code. */
196
0
    if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
197
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
198
0
            SSL_R_TLS_EXT_INVALID_MAX_FRAGMENT_LENGTH);
199
0
        return 0;
200
0
    }
201
202
    /*
203
     * When doing a full handshake or a renegotiation max_fragment_len_mode will
204
     * be TLSEXT_max_fragment_length_UNSPECIFIED
205
     *
206
     * In case of a resumption max_fragment_len_mode will be one of
207
     *      TLSEXT_max_fragment_length_DISABLED, TLSEXT_max_fragment_length_512,
208
     *      TLSEXT_max_fragment_length_1024, TLSEXT_max_fragment_length_2048.
209
     *      TLSEXT_max_fragment_length_4096
210
     *
211
     * RFC 6066: The negotiated length applies for the duration of the session
212
     * including session resumptions.
213
     *
214
     * So we only set the value in case it is unspecified.
215
     */
216
0
    if (s->session->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_UNSPECIFIED)
217
        /*
218
         * Store it in session, so it'll become binding for us
219
         * and we'll include it in a next Server Hello.
220
         */
221
0
        s->session->ext.max_fragment_len_mode = value;
222
223
0
    return 1;
224
0
}
225
226
#ifndef OPENSSL_NO_SRP
227
int tls_parse_ctos_srp(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
228
    X509 *x, size_t chainidx)
229
0
{
230
0
    PACKET srp_I;
231
232
0
    if (!PACKET_as_length_prefixed_1(pkt, &srp_I)
233
0
        || PACKET_contains_zero_byte(&srp_I)) {
234
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
235
0
        return 0;
236
0
    }
237
238
0
    if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) {
239
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
240
0
        return 0;
241
0
    }
242
243
0
    return 1;
244
0
}
245
#endif
246
247
int tls_parse_ctos_ec_pt_formats(SSL_CONNECTION *s, PACKET *pkt,
248
    unsigned int context,
249
    X509 *x, size_t chainidx)
250
0
{
251
0
    PACKET ec_point_format_list;
252
253
0
    if (!PACKET_as_length_prefixed_1(pkt, &ec_point_format_list)
254
0
        || PACKET_remaining(&ec_point_format_list) == 0) {
255
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
256
0
        return 0;
257
0
    }
258
259
0
    if (!s->hit) {
260
0
        if (!PACKET_memdup(&ec_point_format_list,
261
0
                &s->ext.peer_ecpointformats,
262
0
                &s->ext.peer_ecpointformats_len)) {
263
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
264
0
            return 0;
265
0
        }
266
0
    }
267
268
0
    return 1;
269
0
}
270
271
int tls_parse_ctos_session_ticket(SSL_CONNECTION *s, PACKET *pkt,
272
    unsigned int context,
273
    X509 *x, size_t chainidx)
274
0
{
275
0
    if (s->ext.session_ticket_cb && !s->ext.session_ticket_cb(SSL_CONNECTION_GET_USER_SSL(s), PACKET_data(pkt), (int)PACKET_remaining(pkt), s->ext.session_ticket_cb_arg)) {
276
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
277
0
        return 0;
278
0
    }
279
280
0
    return 1;
281
0
}
282
283
int tls_parse_ctos_sig_algs_cert(SSL_CONNECTION *s, PACKET *pkt,
284
    ossl_unused unsigned int context,
285
    ossl_unused X509 *x,
286
    ossl_unused size_t chainidx)
287
0
{
288
0
    PACKET supported_sig_algs;
289
290
0
    if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
291
0
        || PACKET_remaining(&supported_sig_algs) == 0) {
292
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
293
0
        return 0;
294
0
    }
295
296
    /*
297
     * We use this routine on both clients and servers, and when clients
298
     * get asked for PHA we need to always save the sigalgs regardless
299
     * of whether it was a resumption or not.
300
     */
301
0
    if ((!s->server || (s->server && !s->hit))
302
0
        && !tls1_save_sigalgs(s, &supported_sig_algs, 1)) {
303
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
304
0
        return 0;
305
0
    }
306
307
0
    return 1;
308
0
}
309
310
int tls_parse_ctos_sig_algs(SSL_CONNECTION *s, PACKET *pkt,
311
    unsigned int context, X509 *x, size_t chainidx)
312
0
{
313
0
    PACKET supported_sig_algs;
314
315
0
    if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs)
316
0
        || PACKET_remaining(&supported_sig_algs) == 0) {
317
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
318
0
        return 0;
319
0
    }
320
321
    /*
322
     * We use this routine on both clients and servers, and when clients
323
     * get asked for PHA we need to always save the sigalgs regardless
324
     * of whether it was a resumption or not.
325
     */
326
0
    if ((!s->server || (s->server && !s->hit))
327
0
        && !tls1_save_sigalgs(s, &supported_sig_algs, 0)) {
328
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
329
0
        return 0;
330
0
    }
331
332
0
    return 1;
333
0
}
334
335
#ifndef OPENSSL_NO_OCSP
336
int tls_parse_ctos_status_request(SSL_CONNECTION *s, PACKET *pkt,
337
    unsigned int context,
338
    X509 *x, size_t chainidx)
339
0
{
340
0
    PACKET responder_id_list, exts;
341
342
    /* We ignore this in a resumption handshake */
343
0
    if (s->hit)
344
0
        return 1;
345
346
    /* Not defined if we get one of these in a client Certificate */
347
0
    if (x != NULL)
348
0
        return 1;
349
350
0
    if (!PACKET_get_1(pkt, (unsigned int *)&s->ext.status_type)) {
351
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
352
0
        return 0;
353
0
    }
354
355
0
    if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
356
        /*
357
         * We don't know what to do with any other type so ignore it.
358
         */
359
0
        s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
360
0
        return 1;
361
0
    }
362
363
0
    if (!PACKET_get_length_prefixed_2(pkt, &responder_id_list)) {
364
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
365
0
        return 0;
366
0
    }
367
368
    /*
369
     * We remove any OCSP_RESPIDs from a previous handshake
370
     * to prevent unbounded memory growth - CVE-2016-6304
371
     */
372
0
    sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
373
0
    if (PACKET_remaining(&responder_id_list) > 0) {
374
0
        s->ext.ocsp.ids = sk_OCSP_RESPID_new_null();
375
0
        if (s->ext.ocsp.ids == NULL) {
376
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
377
0
            return 0;
378
0
        }
379
0
    } else {
380
0
        s->ext.ocsp.ids = NULL;
381
0
    }
382
383
0
    while (PACKET_remaining(&responder_id_list) > 0) {
384
0
        OCSP_RESPID *id;
385
0
        PACKET responder_id;
386
0
        const unsigned char *id_data;
387
388
0
        if (!PACKET_get_length_prefixed_2(&responder_id_list, &responder_id)
389
0
            || PACKET_remaining(&responder_id) == 0) {
390
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
391
0
            return 0;
392
0
        }
393
394
0
        id_data = PACKET_data(&responder_id);
395
0
        id = d2i_OCSP_RESPID(NULL, &id_data,
396
0
            (int)PACKET_remaining(&responder_id));
397
0
        if (id == NULL) {
398
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
399
0
            return 0;
400
0
        }
401
402
0
        if (id_data != PACKET_end(&responder_id)) {
403
0
            OCSP_RESPID_free(id);
404
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
405
406
0
            return 0;
407
0
        }
408
409
0
        if (!sk_OCSP_RESPID_push(s->ext.ocsp.ids, id)) {
410
0
            OCSP_RESPID_free(id);
411
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
412
413
0
            return 0;
414
0
        }
415
0
    }
416
417
    /* Read in request_extensions */
418
0
    if (!PACKET_as_length_prefixed_2(pkt, &exts)) {
419
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
420
0
        return 0;
421
0
    }
422
423
0
    if (PACKET_remaining(&exts) > 0) {
424
0
        const unsigned char *ext_data = PACKET_data(&exts);
425
426
0
        sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts,
427
0
            X509_EXTENSION_free);
428
0
        s->ext.ocsp.exts = d2i_X509_EXTENSIONS(NULL, &ext_data, (int)PACKET_remaining(&exts));
429
0
        if (s->ext.ocsp.exts == NULL || ext_data != PACKET_end(&exts)) {
430
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
431
0
            return 0;
432
0
        }
433
0
    }
434
435
0
    return 1;
436
0
}
437
#endif
438
439
#ifndef OPENSSL_NO_NEXTPROTONEG
440
int tls_parse_ctos_npn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
441
    X509 *x, size_t chainidx)
442
0
{
443
    /*
444
     * We shouldn't accept this extension on a
445
     * renegotiation.
446
     */
447
0
    if (SSL_IS_FIRST_HANDSHAKE(s))
448
0
        s->s3.npn_seen = 1;
449
450
0
    return 1;
451
0
}
452
#endif
453
454
/*
455
 * Save the ALPN extension in a ClientHello.|pkt| holds the contents of the ALPN
456
 * extension, not including type and length. Returns: 1 on success, 0 on error.
457
 */
458
int tls_parse_ctos_alpn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
459
    X509 *x, size_t chainidx)
460
0
{
461
0
    PACKET protocol_list, save_protocol_list, protocol;
462
463
0
    if (!SSL_IS_FIRST_HANDSHAKE(s))
464
0
        return 1;
465
466
0
    if (!PACKET_as_length_prefixed_2(pkt, &protocol_list)
467
0
        || PACKET_remaining(&protocol_list) < 2) {
468
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
469
0
        return 0;
470
0
    }
471
472
0
    save_protocol_list = protocol_list;
473
0
    do {
474
        /* Protocol names can't be empty. */
475
0
        if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol)
476
0
            || PACKET_remaining(&protocol) == 0) {
477
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
478
0
            return 0;
479
0
        }
480
0
    } while (PACKET_remaining(&protocol_list) != 0);
481
482
0
    OPENSSL_free(s->s3.alpn_proposed);
483
0
    s->s3.alpn_proposed = NULL;
484
0
    s->s3.alpn_proposed_len = 0;
485
0
    if (!PACKET_memdup(&save_protocol_list,
486
0
            &s->s3.alpn_proposed, &s->s3.alpn_proposed_len)) {
487
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
488
0
        return 0;
489
0
    }
490
491
0
    return 1;
492
0
}
493
494
#ifndef OPENSSL_NO_SRTP
495
int tls_parse_ctos_use_srtp(SSL_CONNECTION *s, PACKET *pkt,
496
    unsigned int context, X509 *x, size_t chainidx)
497
0
{
498
0
    STACK_OF(SRTP_PROTECTION_PROFILE) *srvr;
499
0
    unsigned int ct, mki_len, id;
500
0
    int i, srtp_pref;
501
0
    PACKET subpkt;
502
0
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
503
504
    /* Ignore this if we have no SRTP profiles */
505
0
    if (SSL_get_srtp_profiles(ssl) == NULL)
506
0
        return 1;
507
508
    /* Pull off the length of the cipher suite list  and check it is even */
509
0
    if (!PACKET_get_net_2(pkt, &ct) || (ct & 1) != 0
510
0
        || !PACKET_get_sub_packet(pkt, &subpkt, ct)) {
511
0
        SSLfatal(s, SSL_AD_DECODE_ERROR,
512
0
            SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
513
0
        return 0;
514
0
    }
515
516
0
    srvr = SSL_get_srtp_profiles(ssl);
517
0
    s->srtp_profile = NULL;
518
    /* Search all profiles for a match initially */
519
0
    srtp_pref = sk_SRTP_PROTECTION_PROFILE_num(srvr);
520
521
0
    while (PACKET_remaining(&subpkt)) {
522
0
        if (!PACKET_get_net_2(&subpkt, &id)) {
523
0
            SSLfatal(s, SSL_AD_DECODE_ERROR,
524
0
                SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
525
0
            return 0;
526
0
        }
527
528
        /*
529
         * Only look for match in profiles of higher preference than
530
         * current match.
531
         * If no profiles have been have been configured then this
532
         * does nothing.
533
         */
534
0
        for (i = 0; i < srtp_pref; i++) {
535
0
            SRTP_PROTECTION_PROFILE *sprof = sk_SRTP_PROTECTION_PROFILE_value(srvr, i);
536
537
0
            if (sprof->id == id) {
538
0
                s->srtp_profile = sprof;
539
0
                srtp_pref = i;
540
0
                break;
541
0
            }
542
0
        }
543
0
    }
544
545
    /* Now extract the MKI value as a sanity check, but discard it for now */
546
0
    if (!PACKET_get_1(pkt, &mki_len)) {
547
0
        SSLfatal(s, SSL_AD_DECODE_ERROR,
548
0
            SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
549
0
        return 0;
550
0
    }
551
552
0
    if (!PACKET_forward(pkt, mki_len)
553
0
        || PACKET_remaining(pkt)) {
554
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRTP_MKI_VALUE);
555
0
        return 0;
556
0
    }
557
558
0
    return 1;
559
0
}
560
#endif
561
562
int tls_parse_ctos_etm(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
563
    X509 *x, size_t chainidx)
564
0
{
565
0
    if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC))
566
0
        s->ext.use_etm = 1;
567
568
0
    return 1;
569
0
}
570
571
/*
572
 * Process a psk_kex_modes extension received in the ClientHello. |pkt| contains
573
 * the raw PACKET data for the extension. Returns 1 on success or 0 on failure.
574
 */
575
int tls_parse_ctos_psk_kex_modes(SSL_CONNECTION *s, PACKET *pkt,
576
    unsigned int context,
577
    X509 *x, size_t chainidx)
578
0
{
579
0
#ifndef OPENSSL_NO_TLS1_3
580
0
    PACKET psk_kex_modes;
581
0
    unsigned int mode;
582
583
0
    if (!PACKET_as_length_prefixed_1(pkt, &psk_kex_modes)
584
0
        || PACKET_remaining(&psk_kex_modes) == 0) {
585
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
586
0
        return 0;
587
0
    }
588
589
0
    while (PACKET_get_1(&psk_kex_modes, &mode)) {
590
0
        if (mode == TLSEXT_KEX_MODE_KE_DHE)
591
0
            s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE_DHE;
592
0
        else if (mode == TLSEXT_KEX_MODE_KE
593
0
            && (s->options & SSL_OP_ALLOW_NO_DHE_KEX) != 0)
594
0
            s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
595
0
    }
596
597
0
    if (((s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) != 0)
598
0
        && (s->options & SSL_OP_PREFER_NO_DHE_KEX) != 0) {
599
600
        /*
601
         * If NO_DHE is supported and preferred, then we only remember this
602
         * mode. DHE PSK will not be used for sure, because in any case where
603
         * it would be supported (i.e. if a key share is present), NO_DHE would
604
         * be supported as well. As the latter is preferred it would be
605
         * chosen. By removing DHE PSK here, we don't have to deal with the
606
         * SSL_OP_PREFER_NO_DHE_KEX option in any other place.
607
         */
608
0
        s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE;
609
0
    }
610
611
0
#endif
612
613
0
    return 1;
614
0
}
615
616
/*
617
 * Use function tls_parse_ctos_key_share with helper functions extract_keyshares,
618
 * check_overlap and tls_accept_ksgroup to parse the key_share extension(s)
619
 * received in the ClientHello and to select the group used of the key exchange
620
 */
621
622
#ifndef OPENSSL_NO_TLS1_3
623
/*
624
 * Accept a key share group by setting the related variables in s->s3 and
625
 * by generating a pubkey for this group
626
 */
627
static int tls_accept_ksgroup(SSL_CONNECTION *s, uint16_t ksgroup, PACKET *encoded_pubkey)
628
0
{
629
    /* Accept the key share group */
630
0
    s->s3.group_id = ksgroup;
631
0
    s->s3.group_id_candidate = ksgroup;
632
    /* Cache the selected group ID in the SSL_SESSION */
633
0
    s->session->kex_group = ksgroup;
634
0
    if ((s->s3.peer_tmp = ssl_generate_param_group(s, ksgroup)) == NULL) {
635
0
        SSLfatal(s,
636
0
            SSL_AD_INTERNAL_ERROR,
637
0
            SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
638
0
        return 0;
639
0
    }
640
0
    if (tls13_set_encoded_pub_key(s->s3.peer_tmp,
641
0
            PACKET_data(encoded_pubkey),
642
0
            PACKET_remaining(encoded_pubkey))
643
0
        <= 0) {
644
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
645
0
        return 0;
646
0
    }
647
0
    return 1;
648
0
}
649
650
#define GROUPLIST_INCREMENT 32 /* Memory allocation chunk size (nominally 64 Bytes chunks) */
651
652
typedef enum KS_EXTRACTION_RESULT {
653
    EXTRACTION_FAILURE,
654
    EXTRACTION_SUCCESS,
655
    EXTRACTION_SUCCESS_HRR
656
} KS_EXTRACTION_RESULT;
657
658
static KS_EXTRACTION_RESULT extract_keyshares(SSL_CONNECTION *s, PACKET *key_share_list,
659
    const uint16_t *clntgroups, size_t clnt_num_groups,
660
    const uint16_t *srvrgroups, size_t srvr_num_groups,
661
    uint16_t **keyshares_arr, PACKET **encoded_pubkey_arr,
662
    size_t *keyshares_cnt)
663
0
{
664
0
    PACKET encoded_pubkey;
665
0
    size_t key_share_pos = 0;
666
0
    size_t previous_key_share_pos = 0;
667
0
    unsigned int group_id = 0;
668
0
    unsigned int i;
669
670
    /*
671
     * Theoretically there is no limit on the number of keyshares as long as
672
     * they are less than 2^16 bytes in total. It costs us something for each
673
     * keyshare to confirm the groups are valid, so we restrict this to a
674
     * sensible number (MAX_KEY_SHARES == 16). Any keyshares over this limit are
675
     * simply ignored.
676
     */
677
678
    /* Prepare memory to hold the extracted key share groups and related pubkeys */
679
0
    *keyshares_arr = OPENSSL_malloc_array(MAX_KEY_SHARES,
680
0
        sizeof(**keyshares_arr));
681
0
    if (*keyshares_arr == NULL) {
682
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
683
0
        goto failure;
684
0
    }
685
0
    *encoded_pubkey_arr = OPENSSL_malloc_array(MAX_KEY_SHARES,
686
0
        sizeof(**encoded_pubkey_arr));
687
0
    if (*encoded_pubkey_arr == NULL) {
688
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
689
0
        goto failure;
690
0
    }
691
692
    /*
693
     * We limit the number of key shares we are willing to process to
694
     * MAX_KEY_SHARES regardless of whether we include them in keyshares_arr or
695
     * not.
696
     */
697
0
    for (i = 0; PACKET_remaining(key_share_list) > 0 && i < MAX_KEY_SHARES; i++) {
698
        /* Get the group_id for the current share and its encoded_pubkey */
699
0
        if (!PACKET_get_net_2(key_share_list, &group_id)
700
0
            || !PACKET_get_length_prefixed_2(key_share_list, &encoded_pubkey)
701
0
            || PACKET_remaining(&encoded_pubkey) == 0) {
702
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
703
0
            goto failure;
704
0
        }
705
706
        /*
707
         * If we sent an HRR then the key_share sent back MUST be for the group
708
         * we requested, and must be the only key_share sent.
709
         */
710
0
        if (s->s3.group_id != 0
711
0
            && (group_id != s->s3.group_id
712
0
                || PACKET_remaining(key_share_list) != 0)) {
713
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
714
0
            goto failure;
715
0
        }
716
717
        /*
718
         * Check if this share is in supported_groups sent from client
719
         * RFC 8446 also mandates that clients send keyshares in the same
720
         * order as listed in the supported groups extension, but its not
721
         * required that the server check that, and some clients violate this
722
         * so instead of failing the connection when that occurs, log a trace
723
         * message indicating the client discrepancy.
724
         */
725
0
        if (!check_in_list(s, group_id, clntgroups, clnt_num_groups, 0, &key_share_pos)) {
726
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
727
0
            goto failure;
728
0
        }
729
730
0
        if (key_share_pos < previous_key_share_pos)
731
0
            OSSL_TRACE1(TLS, "key share group id %d is out of RFC 8446 order\n", group_id);
732
733
0
        previous_key_share_pos = key_share_pos;
734
735
0
        if (s->s3.group_id != 0) {
736
            /*
737
             * We have sent a HRR, and the key share we got back is
738
             * the one we expected and is the only key share and is
739
             * in the list of supported_groups (checked
740
             * above already), hence we accept this key share group
741
             */
742
0
            if (!tls_accept_ksgroup(s, s->s3.group_id, &encoded_pubkey))
743
0
                goto failure; /* SSLfatal already called */
744
            /* We have selected a key share group via HRR, hence we're done here */
745
0
            return EXTRACTION_SUCCESS_HRR;
746
0
        }
747
748
        /*
749
         * We tolerate but ignore a group id that we don't think is
750
         * suitable for TLSv1.3 or which is not supported by the server
751
         */
752
0
        if (!check_in_list(s, group_id, srvrgroups, srvr_num_groups, 1, NULL)
753
0
            || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)
754
0
            || !tls_valid_group(s, group_id, TLS1_3_VERSION, TLS1_3_VERSION,
755
0
                NULL, NULL)) {
756
            /* Share not suitable or not supported, check next share */
757
0
            continue;
758
0
        }
759
760
        /* Memorize this key share group ID and its encoded point */
761
0
        (*keyshares_arr)[*keyshares_cnt] = group_id;
762
0
        (*encoded_pubkey_arr)[(*keyshares_cnt)++] = encoded_pubkey;
763
0
    }
764
765
0
    return EXTRACTION_SUCCESS;
766
767
0
failure:
768
    /* Fatal error -> free any allocated memory and return 0 */
769
0
    OPENSSL_free(*keyshares_arr);
770
0
    OPENSSL_free(*encoded_pubkey_arr);
771
0
    return EXTRACTION_FAILURE;
772
0
}
773
#endif
774
775
/*
776
 * For each group in the priority list of groups, check if that group is
777
 * also present in the secondary list; if so, select the first overlap and
778
 * assign to selected_group and also set the related index in the candidate group list,
779
 * or set selected_group to 0 if no overlap
780
 */
781
#ifndef OPENSSL_NO_TLS1_3
782
static void check_overlap(SSL_CONNECTION *s,
783
    const uint16_t *prio_groups, size_t prio_num_groups,
784
    const uint16_t *candidate_groups, size_t candidate_num_groups,
785
    int *prio_group_idx, int *candidate_group_idx,
786
    uint16_t *selected_group)
787
0
{
788
0
    uint16_t current_group;
789
0
    size_t group_idx = prio_num_groups;
790
0
    size_t new_group_idx = 0;
791
792
0
    *candidate_group_idx = 0;
793
0
    *prio_group_idx = 0;
794
0
    *selected_group = 0;
795
796
0
    for (current_group = 0; current_group < candidate_num_groups; current_group++) {
797
0
        if (!check_in_list(s, candidate_groups[current_group], prio_groups,
798
0
                prio_num_groups, 1, &new_group_idx)
799
0
            || !tls_group_allowed(s, candidate_groups[current_group],
800
0
                SSL_SECOP_CURVE_SUPPORTED)
801
0
            || !tls_valid_group(s, candidate_groups[current_group], TLS1_3_VERSION,
802
0
                TLS1_3_VERSION, NULL, NULL))
803
            /* No overlap or group not suitable, check next group */
804
0
            continue;
805
806
        /*
807
         * is the found new_group_idx earlier in the priority list than
808
         * initial or last group_idx?
809
         */
810
0
        if (new_group_idx < group_idx) {
811
0
            group_idx = new_group_idx;
812
0
            *candidate_group_idx = current_group;
813
0
            *prio_group_idx = (int)group_idx;
814
0
            *selected_group = prio_groups[group_idx];
815
0
        }
816
0
    }
817
0
}
818
#endif
819
820
int tls_parse_ctos_key_share(SSL_CONNECTION *s, PACKET *pkt,
821
    unsigned int context, X509 *x, size_t chainidx)
822
0
{
823
0
#ifndef OPENSSL_NO_TLS1_3
824
0
    PACKET key_share_list;
825
0
    const uint16_t *clntgroups, *srvrgroups;
826
0
    const size_t *srvrtuples;
827
0
    uint16_t *first_group_in_tuple;
828
0
    size_t clnt_num_groups, srvr_num_groups, srvr_num_tuples;
829
0
    PACKET *encoded_pubkey_arr = NULL;
830
0
    uint16_t *keyshares_arr = NULL;
831
0
    size_t keyshares_cnt = 0;
832
    /* We conservatively assume that we did not find a suitable group */
833
0
    uint16_t group_id_candidate = 0;
834
0
    KS_EXTRACTION_RESULT ks_extraction_result;
835
0
    size_t current_tuple;
836
0
    int ret = 0;
837
838
0
    s->s3.group_id_candidate = 0;
839
0
    if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0)
840
0
        return 1;
841
842
    /* Sanity check */
843
0
    if (s->s3.peer_tmp != NULL) {
844
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
845
0
        return 0;
846
0
    }
847
848
0
    if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) {
849
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
850
0
        return 0;
851
0
    }
852
853
    /* Get list of server supported groups and the group tuples */
854
0
    tls1_get_supported_groups(s, &srvrgroups, &srvr_num_groups);
855
0
    tls1_get_group_tuples(s, &srvrtuples, &srvr_num_tuples);
856
    /* Get the clients list of supported groups. */
857
0
    tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups);
858
859
0
    if (clnt_num_groups == 0) {
860
        /*
861
         * This can only happen if the supported_groups extension was not sent,
862
         * because we verify that the length is non-zero when we process that
863
         * extension.
864
         */
865
0
        SSLfatal(s, SSL_AD_MISSING_EXTENSION,
866
0
            SSL_R_MISSING_SUPPORTED_GROUPS_EXTENSION);
867
0
        return 0;
868
0
    }
869
870
0
    if (s->s3.group_id != 0 && PACKET_remaining(&key_share_list) == 0) {
871
        /*
872
         * If we set a group_id already, then we must have sent an HRR
873
         * requesting a new key_share. If we haven't got one then that is an
874
         * error
875
         */
876
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
877
0
        return 0;
878
0
    }
879
880
    /* We parse the key share extension and memorize the entries (after some checks) */
881
0
    ks_extraction_result = extract_keyshares(s,
882
0
        &key_share_list,
883
0
        clntgroups, clnt_num_groups,
884
0
        srvrgroups, srvr_num_groups,
885
0
        &keyshares_arr, &encoded_pubkey_arr,
886
0
        &keyshares_cnt);
887
888
0
    if (ks_extraction_result == EXTRACTION_FAILURE) /* Fatal error during tests */
889
0
        return 0; /* Memory already freed and SSLfatal already called */
890
0
    if (ks_extraction_result == EXTRACTION_SUCCESS_HRR) /* Successful HRR */
891
0
        goto end;
892
893
    /*
894
     * We now have the following lists available to make a decision for
895
     * which group the server should use for key exchange :
896
     * From client: clntgroups[clnt_num_groups],
897
     *              keyshares_arr[keyshares_cnt], encoded_pubkey_arr[keyshares_cnt]
898
     * From server: srvrgroups[srvr_num_groups], srvrtuples[srvr_num_tuples]
899
     *
900
     * Group selection algorithm:
901
     *    For all tuples do:
902
     *      key share group(s) overlapping with current tuple?
903
     *         --> Yes: accept group_id for SH
904
     *        --> No: is any of the client supported_groups overlapping with current tuple?
905
     *            --> Yes: memorize group_id for HRR, break
906
     *             --> No: continue to check next tuple
907
     *
908
     * Remark: Selection priority different for client- or server-preference
909
     */
910
0
    first_group_in_tuple = (uint16_t *)srvrgroups;
911
0
    for (current_tuple = 0; current_tuple < srvr_num_tuples; current_tuple++) {
912
0
        size_t number_of_groups_in_tuple = srvrtuples[current_tuple];
913
0
        int prio_group_idx = 0, candidate_group_idx = 0;
914
915
        /* Server or client preference ? */
916
0
        if (s->options & SSL_OP_SERVER_PREFERENCE) {
917
            /* Server preference */
918
            /* Is there overlap with a key share group?  */
919
0
            check_overlap(s,
920
0
                first_group_in_tuple, number_of_groups_in_tuple,
921
0
                keyshares_arr, keyshares_cnt,
922
0
                &prio_group_idx, &candidate_group_idx,
923
0
                &group_id_candidate);
924
0
            if (group_id_candidate > 0) { /* Overlap found -> accept the key share group */
925
0
                if (!tls_accept_ksgroup(s, group_id_candidate,
926
0
                        &encoded_pubkey_arr[candidate_group_idx]))
927
0
                    goto err; /* SSLfatal already called */
928
                /* We have all info for a SH, hence we're done here */
929
0
                goto end;
930
0
            } else {
931
                /*
932
                 * There's no overlap with a key share, but is there at least a client
933
                 * supported_group overlapping with the current tuple?
934
                 */
935
0
                check_overlap(s,
936
0
                    first_group_in_tuple, number_of_groups_in_tuple,
937
0
                    clntgroups, clnt_num_groups,
938
0
                    &prio_group_idx, &candidate_group_idx,
939
0
                    &group_id_candidate);
940
0
                if (group_id_candidate > 0) {
941
                    /*
942
                     * We did not have a key share overlap, but at least the supported
943
                     * groups overlap hence we can stop searching
944
                     * (and report group_id_candidate 'upward' for HRR)
945
                     */
946
0
                    s->s3.group_id_candidate = group_id_candidate;
947
0
                    goto end;
948
0
                } else {
949
                    /*
950
                     * Neither key share nor supported_groups overlap current
951
                     * tuple, hence we try the next tuple
952
                     */
953
0
                    first_group_in_tuple = &first_group_in_tuple[number_of_groups_in_tuple];
954
0
                    continue;
955
0
                }
956
0
            }
957
958
0
        } else { /* We have client preference */
959
0
            check_overlap(s,
960
0
                keyshares_arr, keyshares_cnt,
961
0
                first_group_in_tuple, number_of_groups_in_tuple,
962
0
                &prio_group_idx, &candidate_group_idx,
963
0
                &group_id_candidate);
964
0
            if (group_id_candidate > 0) {
965
0
                if (!tls_accept_ksgroup(s, group_id_candidate, &encoded_pubkey_arr[prio_group_idx]))
966
0
                    goto err;
967
0
                goto end;
968
0
            } else {
969
0
                check_overlap(s,
970
0
                    clntgroups, clnt_num_groups,
971
0
                    first_group_in_tuple, number_of_groups_in_tuple,
972
0
                    &prio_group_idx, &candidate_group_idx,
973
0
                    &group_id_candidate);
974
0
                if (group_id_candidate > 0) {
975
0
                    s->s3.group_id_candidate = group_id_candidate;
976
0
                    goto end;
977
0
                } else {
978
0
                    first_group_in_tuple = &first_group_in_tuple[number_of_groups_in_tuple];
979
0
                    continue;
980
0
                }
981
0
            }
982
0
        }
983
0
    }
984
985
0
end:
986
0
    ret = 1;
987
988
0
err:
989
0
    OPENSSL_free(keyshares_arr);
990
0
    OPENSSL_free(encoded_pubkey_arr);
991
0
    return ret;
992
993
0
#endif
994
995
0
    return 1;
996
0
}
997
998
int tls_parse_ctos_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
999
    X509 *x, size_t chainidx)
1000
0
{
1001
0
#ifndef OPENSSL_NO_TLS1_3
1002
0
    unsigned int format, version, key_share, group_id;
1003
0
    EVP_MD_CTX *hctx;
1004
0
    EVP_PKEY *pkey;
1005
0
    PACKET cookie, raw, chhash, appcookie;
1006
0
    WPACKET hrrpkt;
1007
0
    const unsigned char *data, *mdin, *ciphdata;
1008
0
    unsigned char hmac[SHA256_DIGEST_LENGTH];
1009
0
    unsigned char hrr[MAX_HRR_SIZE];
1010
0
    size_t rawlen, hmaclen, hrrlen, ciphlen;
1011
0
    uint64_t tm, now;
1012
0
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1013
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1014
1015
    /* Ignore any cookie if we're not set up to verify it */
1016
0
    if (sctx->verify_stateless_cookie_cb == NULL
1017
0
        || (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
1018
0
        return 1;
1019
1020
0
    if (!PACKET_as_length_prefixed_2(pkt, &cookie)) {
1021
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1022
0
        return 0;
1023
0
    }
1024
1025
0
    raw = cookie;
1026
0
    data = PACKET_data(&raw);
1027
0
    rawlen = PACKET_remaining(&raw);
1028
0
    if (rawlen < SHA256_DIGEST_LENGTH
1029
0
        || !PACKET_forward(&raw, rawlen - SHA256_DIGEST_LENGTH)) {
1030
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1031
0
        return 0;
1032
0
    }
1033
0
    mdin = PACKET_data(&raw);
1034
1035
    /* Verify the HMAC of the cookie */
1036
0
    hctx = EVP_MD_CTX_create();
1037
0
    pkey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",
1038
0
        sctx->propq,
1039
0
        s->session_ctx->ext.cookie_hmac_key,
1040
0
        sizeof(s->session_ctx->ext.cookie_hmac_key));
1041
0
    if (hctx == NULL || pkey == NULL) {
1042
0
        EVP_MD_CTX_free(hctx);
1043
0
        EVP_PKEY_free(pkey);
1044
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
1045
0
        return 0;
1046
0
    }
1047
1048
0
    hmaclen = SHA256_DIGEST_LENGTH;
1049
0
    if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", sctx->libctx,
1050
0
            sctx->propq, pkey, NULL)
1051
0
            <= 0
1052
0
        || EVP_DigestSign(hctx, hmac, &hmaclen, data,
1053
0
               rawlen - SHA256_DIGEST_LENGTH)
1054
0
            <= 0
1055
0
        || hmaclen != SHA256_DIGEST_LENGTH) {
1056
0
        EVP_MD_CTX_free(hctx);
1057
0
        EVP_PKEY_free(pkey);
1058
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1059
0
        return 0;
1060
0
    }
1061
1062
0
    EVP_MD_CTX_free(hctx);
1063
0
    EVP_PKEY_free(pkey);
1064
1065
0
    if (CRYPTO_memcmp(hmac, mdin, SHA256_DIGEST_LENGTH) != 0) {
1066
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COOKIE_MISMATCH);
1067
0
        return 0;
1068
0
    }
1069
1070
0
    if (!PACKET_get_net_2(&cookie, &format)) {
1071
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1072
0
        return 0;
1073
0
    }
1074
    /* Check the cookie format is something we recognise. Ignore it if not */
1075
0
    if (format != COOKIE_STATE_FORMAT_VERSION)
1076
0
        return 1;
1077
1078
    /*
1079
     * The rest of these checks really shouldn't fail since we have verified the
1080
     * HMAC above.
1081
     */
1082
1083
    /* Check the version number is sane */
1084
0
    if (!PACKET_get_net_2(&cookie, &version)) {
1085
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1086
0
        return 0;
1087
0
    }
1088
0
    if (version != TLS1_3_VERSION) {
1089
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1090
0
            SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
1091
0
        return 0;
1092
0
    }
1093
1094
0
    if (!PACKET_get_net_2(&cookie, &group_id)) {
1095
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1096
0
        return 0;
1097
0
    }
1098
1099
0
    ciphdata = PACKET_data(&cookie);
1100
0
    if (!PACKET_forward(&cookie, 2)) {
1101
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1102
0
        return 0;
1103
0
    }
1104
0
    if (group_id != s->s3.group_id
1105
0
        || s->s3.tmp.new_cipher
1106
0
            != ssl_get_cipher_by_char(s, ciphdata, 0)) {
1107
        /*
1108
         * We chose a different cipher or group id this time around to what is
1109
         * in the cookie. Something must have changed.
1110
         */
1111
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER);
1112
0
        return 0;
1113
0
    }
1114
1115
0
    if (!PACKET_get_1(&cookie, &key_share)
1116
0
        || !PACKET_get_net_8(&cookie, &tm)
1117
0
        || !PACKET_get_length_prefixed_2(&cookie, &chhash)
1118
0
        || !PACKET_get_length_prefixed_1(&cookie, &appcookie)
1119
0
        || PACKET_remaining(&cookie) != SHA256_DIGEST_LENGTH) {
1120
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1121
0
        return 0;
1122
0
    }
1123
1124
    /* We tolerate a cookie age of up to 10 minutes (= 60 * 10 seconds) */
1125
0
    now = time(NULL);
1126
0
    if (tm > now || (now - tm) > 600) {
1127
        /* Cookie is stale. Ignore it */
1128
0
        return 1;
1129
0
    }
1130
1131
    /* Verify the app cookie */
1132
0
    if (sctx->verify_stateless_cookie_cb(SSL_CONNECTION_GET_USER_SSL(s),
1133
0
            PACKET_data(&appcookie),
1134
0
            PACKET_remaining(&appcookie))
1135
0
        == 0) {
1136
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COOKIE_MISMATCH);
1137
0
        return 0;
1138
0
    }
1139
1140
    /*
1141
     * Reconstruct the HRR that we would have sent in response to the original
1142
     * ClientHello so we can add it to the transcript hash.
1143
     * Note: This won't work with custom HRR extensions
1144
     */
1145
0
    if (!WPACKET_init_static_len(&hrrpkt, hrr, sizeof(hrr), 0)) {
1146
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1147
0
        return 0;
1148
0
    }
1149
0
    if (!WPACKET_put_bytes_u8(&hrrpkt, SSL3_MT_SERVER_HELLO)
1150
0
        || !WPACKET_start_sub_packet_u24(&hrrpkt)
1151
0
        || !WPACKET_put_bytes_u16(&hrrpkt, TLS1_2_VERSION)
1152
0
        || !WPACKET_memcpy(&hrrpkt, hrrrandom, SSL3_RANDOM_SIZE)
1153
0
        || !WPACKET_sub_memcpy_u8(&hrrpkt, s->tmp_session_id,
1154
0
            s->tmp_session_id_len)
1155
0
        || !ssl->method->put_cipher_by_char(s->s3.tmp.new_cipher, &hrrpkt,
1156
0
            &ciphlen)
1157
0
        || !WPACKET_put_bytes_u8(&hrrpkt, 0)
1158
0
        || !WPACKET_start_sub_packet_u16(&hrrpkt)) {
1159
0
        WPACKET_cleanup(&hrrpkt);
1160
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1161
0
        return 0;
1162
0
    }
1163
0
    if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_supported_versions)
1164
0
        || !WPACKET_start_sub_packet_u16(&hrrpkt)
1165
0
        || !WPACKET_put_bytes_u16(&hrrpkt, s->version)
1166
0
        || !WPACKET_close(&hrrpkt)) {
1167
0
        WPACKET_cleanup(&hrrpkt);
1168
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1169
0
        return 0;
1170
0
    }
1171
0
    if (key_share) {
1172
0
        if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_key_share)
1173
0
            || !WPACKET_start_sub_packet_u16(&hrrpkt)
1174
0
            || !WPACKET_put_bytes_u16(&hrrpkt, s->s3.group_id)
1175
0
            || !WPACKET_close(&hrrpkt)) {
1176
0
            WPACKET_cleanup(&hrrpkt);
1177
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1178
0
            return 0;
1179
0
        }
1180
0
    }
1181
0
    if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_cookie)
1182
0
        || !WPACKET_start_sub_packet_u16(&hrrpkt)
1183
0
        || !WPACKET_sub_memcpy_u16(&hrrpkt, data, rawlen)
1184
0
        || !WPACKET_close(&hrrpkt) /* cookie extension */
1185
0
        || !WPACKET_close(&hrrpkt) /* extension block */
1186
0
        || !WPACKET_close(&hrrpkt) /* message */
1187
0
        || !WPACKET_get_total_written(&hrrpkt, &hrrlen)
1188
0
        || !WPACKET_finish(&hrrpkt)) {
1189
0
        WPACKET_cleanup(&hrrpkt);
1190
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1191
0
        return 0;
1192
0
    }
1193
1194
    /* Reconstruct the transcript hash */
1195
0
    if (!create_synthetic_message_hash(s, PACKET_data(&chhash),
1196
0
            PACKET_remaining(&chhash), hrr,
1197
0
            hrrlen)) {
1198
        /* SSLfatal() already called */
1199
0
        return 0;
1200
0
    }
1201
1202
    /* Act as if this ClientHello came after a HelloRetryRequest */
1203
0
    s->hello_retry_request = SSL_HRR_PENDING;
1204
1205
0
    s->ext.cookieok = 1;
1206
0
#endif
1207
1208
0
    return 1;
1209
0
}
1210
1211
int tls_parse_ctos_supported_groups(SSL_CONNECTION *s, PACKET *pkt,
1212
    unsigned int context,
1213
    X509 *x, size_t chainidx)
1214
0
{
1215
0
    PACKET supported_groups_list;
1216
1217
    /* Each group is 2 bytes and we must have at least 1. */
1218
0
    if (!PACKET_as_length_prefixed_2(pkt, &supported_groups_list)
1219
0
        || PACKET_remaining(&supported_groups_list) == 0
1220
0
        || (PACKET_remaining(&supported_groups_list) % 2) != 0) {
1221
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1222
0
        return 0;
1223
0
    }
1224
1225
0
    if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) {
1226
0
        OPENSSL_free(s->ext.peer_supportedgroups);
1227
0
        s->ext.peer_supportedgroups = NULL;
1228
0
        s->ext.peer_supportedgroups_len = 0;
1229
        /*
1230
         * We only pay attention to the first 128 supported groups and ignore
1231
         * any beyond that limit. Theoretically this could cause problems if
1232
         * the client also uses one of these groups (say in a key share extension)
1233
         * - but why would any valid client be sending such a huge supported
1234
         * groups list?
1235
         */
1236
0
        if (!tls1_save_u16(&supported_groups_list,
1237
0
                &s->ext.peer_supportedgroups,
1238
0
                &s->ext.peer_supportedgroups_len, MAX_SUPPORTED_GROUPS)) {
1239
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1240
0
            return 0;
1241
0
        }
1242
0
    }
1243
1244
0
    return 1;
1245
0
}
1246
1247
int tls_parse_ctos_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1248
    X509 *x, size_t chainidx)
1249
0
{
1250
    /* The extension must always be empty */
1251
0
    if (PACKET_remaining(pkt) != 0) {
1252
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1253
0
        return 0;
1254
0
    }
1255
1256
0
    if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET)
1257
0
        return 1;
1258
1259
0
    s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS;
1260
1261
0
    return 1;
1262
0
}
1263
1264
int tls_parse_ctos_early_data(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1265
    X509 *x, size_t chainidx)
1266
0
{
1267
0
    if (PACKET_remaining(pkt) != 0) {
1268
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1269
0
        return 0;
1270
0
    }
1271
1272
0
    if (s->hello_retry_request != SSL_HRR_NONE) {
1273
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
1274
0
        return 0;
1275
0
    }
1276
1277
0
    return 1;
1278
0
}
1279
1280
static SSL_TICKET_STATUS tls_get_stateful_ticket(SSL_CONNECTION *s, PACKET *tick,
1281
    SSL_SESSION **sess)
1282
0
{
1283
0
    SSL_SESSION *tmpsess = NULL;
1284
1285
0
    s->ext.ticket_expected = 1;
1286
1287
0
    switch (PACKET_remaining(tick)) {
1288
0
    case 0:
1289
0
        return SSL_TICKET_EMPTY;
1290
1291
0
    case SSL_MAX_SSL_SESSION_ID_LENGTH:
1292
0
        break;
1293
1294
0
    default:
1295
0
        return SSL_TICKET_NO_DECRYPT;
1296
0
    }
1297
1298
0
    tmpsess = lookup_sess_in_cache(s, PACKET_data(tick),
1299
0
        SSL_MAX_SSL_SESSION_ID_LENGTH);
1300
1301
0
    if (tmpsess == NULL)
1302
0
        return SSL_TICKET_NO_DECRYPT;
1303
1304
0
    *sess = tmpsess;
1305
0
    return SSL_TICKET_SUCCESS;
1306
0
}
1307
1308
int tls_parse_ctos_psk(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
1309
    X509 *x, size_t chainidx)
1310
0
{
1311
0
    PACKET identities, binders, binder;
1312
0
    size_t binderoffset;
1313
0
    int hashsize;
1314
0
    SSL_SESSION *sess = NULL;
1315
0
    unsigned int id, i, ext = 0;
1316
0
    const EVP_MD *md = NULL;
1317
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1318
0
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
1319
1320
    /*
1321
     * If we have no PSK kex mode that we recognise then we can't resume so
1322
     * ignore this extension
1323
     */
1324
0
    if ((s->ext.psk_kex_mode
1325
0
            & (TLSEXT_KEX_MODE_FLAG_KE | TLSEXT_KEX_MODE_FLAG_KE_DHE))
1326
0
        == 0)
1327
0
        return 1;
1328
1329
0
    if (!PACKET_get_length_prefixed_2(pkt, &identities)) {
1330
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1331
0
        return 0;
1332
0
    }
1333
1334
0
    s->ext.ticket_expected = 0;
1335
0
    for (id = 0; PACKET_remaining(&identities) != 0; id++) {
1336
0
        PACKET identity;
1337
0
        unsigned long ticket_agel;
1338
0
        size_t idlen;
1339
1340
0
        if (!PACKET_get_length_prefixed_2(&identities, &identity)
1341
0
            || !PACKET_get_net_4(&identities, &ticket_agel)) {
1342
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1343
0
            return 0;
1344
0
        }
1345
1346
0
        idlen = PACKET_remaining(&identity);
1347
0
        if (s->psk_find_session_cb != NULL
1348
0
            && !s->psk_find_session_cb(ussl, PACKET_data(&identity), idlen,
1349
0
                &sess)) {
1350
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_EXTENSION);
1351
0
            return 0;
1352
0
        }
1353
1354
0
#ifndef OPENSSL_NO_PSK
1355
0
        if (sess == NULL
1356
0
            && s->psk_server_callback != NULL
1357
0
            && idlen <= PSK_MAX_IDENTITY_LEN) {
1358
0
            char *pskid = NULL;
1359
0
            unsigned char pskdata[PSK_MAX_PSK_LEN];
1360
0
            unsigned int pskdatalen;
1361
1362
0
            if (!PACKET_strndup(&identity, &pskid)) {
1363
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1364
0
                return 0;
1365
0
            }
1366
0
            pskdatalen = s->psk_server_callback(ussl, pskid, pskdata,
1367
0
                sizeof(pskdata));
1368
0
            OPENSSL_free(pskid);
1369
0
            if (pskdatalen > PSK_MAX_PSK_LEN) {
1370
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1371
0
                return 0;
1372
0
            } else if (pskdatalen > 0) {
1373
0
                const SSL_CIPHER *cipher;
1374
0
                const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
1375
1376
                /*
1377
                 * We found a PSK using an old style callback. We don't know
1378
                 * the digest so we default to SHA256 as per the TLSv1.3 spec
1379
                 */
1380
0
                cipher = SSL_CIPHER_find(SSL_CONNECTION_GET_SSL(s),
1381
0
                    tls13_aes128gcmsha256_id);
1382
0
                if (cipher == NULL) {
1383
0
                    OPENSSL_cleanse(pskdata, pskdatalen);
1384
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1385
0
                    return 0;
1386
0
                }
1387
1388
0
                sess = SSL_SESSION_new();
1389
0
                if (sess == NULL
1390
0
                    || !SSL_SESSION_set1_master_key(sess, pskdata,
1391
0
                        pskdatalen)
1392
0
                    || !SSL_SESSION_set_cipher(sess, cipher)
1393
0
                    || !SSL_SESSION_set_protocol_version(sess,
1394
0
                        TLS1_3_VERSION)) {
1395
0
                    OPENSSL_cleanse(pskdata, pskdatalen);
1396
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1397
0
                    goto err;
1398
0
                }
1399
0
                OPENSSL_cleanse(pskdata, pskdatalen);
1400
0
            }
1401
0
        }
1402
0
#endif /* OPENSSL_NO_PSK */
1403
1404
0
        if (sess != NULL) {
1405
            /* We found a PSK */
1406
0
            SSL_SESSION *sesstmp = ssl_session_dup(sess, 0);
1407
1408
0
            if (sesstmp == NULL) {
1409
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1410
0
                goto err;
1411
0
            }
1412
0
            SSL_SESSION_free(sess);
1413
0
            sess = sesstmp;
1414
1415
            /*
1416
             * We've just been told to use this session for this context so
1417
             * make sure the sid_ctx matches up.
1418
             */
1419
0
            memcpy(sess->sid_ctx, s->sid_ctx, s->sid_ctx_length);
1420
0
            sess->sid_ctx_length = s->sid_ctx_length;
1421
0
            ext = 1;
1422
0
            if (id == 0)
1423
0
                s->ext.early_data_ok = 1;
1424
0
            s->ext.ticket_expected = 1;
1425
0
        } else {
1426
0
            OSSL_TIME t, age, expire;
1427
0
            int ret;
1428
1429
            /*
1430
             * If we are using anti-replay protection then we behave as if
1431
             * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
1432
             * is no point in using full stateless tickets.
1433
             */
1434
0
            if ((s->options & SSL_OP_NO_TICKET) != 0
1435
0
                || (s->max_early_data > 0
1436
0
                    && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))
1437
0
                ret = tls_get_stateful_ticket(s, &identity, &sess);
1438
0
            else
1439
0
                ret = tls_decrypt_ticket(s, PACKET_data(&identity),
1440
0
                    PACKET_remaining(&identity), NULL, 0,
1441
0
                    &sess);
1442
1443
0
            if (ret == SSL_TICKET_EMPTY) {
1444
0
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1445
0
                return 0;
1446
0
            }
1447
1448
0
            if (ret == SSL_TICKET_FATAL_ERR_MALLOC
1449
0
                || ret == SSL_TICKET_FATAL_ERR_OTHER) {
1450
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1451
0
                return 0;
1452
0
            }
1453
0
            if (ret == SSL_TICKET_NONE || ret == SSL_TICKET_NO_DECRYPT)
1454
0
                continue;
1455
1456
            /* Check for replay */
1457
0
            if (s->max_early_data > 0
1458
0
                && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0
1459
0
                && !SSL_CTX_remove_session(s->session_ctx, sess)) {
1460
0
                SSL_SESSION_free(sess);
1461
0
                sess = NULL;
1462
0
                continue;
1463
0
            }
1464
1465
0
            age = ossl_time_subtract(ossl_ms2time(ticket_agel),
1466
0
                ossl_ms2time(sess->ext.tick_age_add));
1467
0
            t = ossl_time_subtract(ossl_time_now(), sess->time);
1468
1469
            /*
1470
             * Although internally we use OSS_TIME which has ns granularity,
1471
             * when SSL_SESSION structures are serialised/deserialised we use
1472
             * second granularity for the sess->time field. Therefore it could
1473
             * appear that the client's ticket age is longer than ours (our
1474
             * ticket age calculation should always be slightly longer than the
1475
             * client's due to the network latency). Therefore we add 1000ms to
1476
             * our age calculation to adjust for rounding errors.
1477
             */
1478
0
            expire = ossl_time_add(t, ossl_ms2time(1000));
1479
1480
0
            if (id == 0
1481
0
                && ossl_time_compare(sess->timeout, t) >= 0
1482
0
                && ossl_time_compare(age, expire) <= 0
1483
0
                && ossl_time_compare(ossl_time_add(age, TICKET_AGE_ALLOWANCE),
1484
0
                       expire)
1485
0
                    >= 0) {
1486
                /*
1487
                 * Ticket age is within tolerance and not expired. We allow it
1488
                 * for early data
1489
                 */
1490
0
                s->ext.early_data_ok = 1;
1491
0
            }
1492
0
        }
1493
1494
0
        md = ssl_md(sctx, sess->cipher->algorithm2);
1495
0
        if (md == NULL) {
1496
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1497
0
            goto err;
1498
0
        }
1499
0
        if (!EVP_MD_is_a(md,
1500
0
                EVP_MD_get0_name(ssl_md(sctx,
1501
0
                    s->s3.tmp.new_cipher->algorithm2)))) {
1502
            /* The ciphersuite is not compatible with this session. */
1503
0
            SSL_SESSION_free(sess);
1504
0
            sess = NULL;
1505
0
            s->ext.early_data_ok = 0;
1506
0
            s->ext.ticket_expected = 0;
1507
0
            continue;
1508
0
        }
1509
0
        break;
1510
0
    }
1511
1512
0
    if (sess == NULL)
1513
0
        return 1;
1514
1515
0
    binderoffset = PACKET_data(pkt) - PACKET_msg_start(pkt);
1516
0
    hashsize = EVP_MD_get_size(md);
1517
0
    if (hashsize <= 0)
1518
0
        goto err;
1519
1520
0
    if (!PACKET_get_length_prefixed_2(pkt, &binders)) {
1521
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1522
0
        goto err;
1523
0
    }
1524
1525
0
    for (i = 0; i <= id; i++) {
1526
0
        if (!PACKET_get_length_prefixed_1(&binders, &binder)) {
1527
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1528
0
            goto err;
1529
0
        }
1530
0
    }
1531
1532
0
    if (PACKET_remaining(&binder) != (size_t)hashsize) {
1533
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1534
0
        goto err;
1535
0
    }
1536
0
    if (tls_psk_do_binder(s, md, PACKET_msg_start(pkt), binderoffset,
1537
0
            PACKET_data(&binder), NULL, sess, 0, ext)
1538
0
        != 1) {
1539
        /* SSLfatal() already called */
1540
0
        goto err;
1541
0
    }
1542
1543
0
    s->ext.tick_identity = id;
1544
1545
0
    SSL_SESSION_free(s->session);
1546
0
    s->session = sess;
1547
0
    return 1;
1548
0
err:
1549
0
    SSL_SESSION_free(sess);
1550
0
    return 0;
1551
0
}
1552
1553
int tls_parse_ctos_post_handshake_auth(SSL_CONNECTION *s, PACKET *pkt,
1554
    ossl_unused unsigned int context,
1555
    ossl_unused X509 *x,
1556
    ossl_unused size_t chainidx)
1557
0
{
1558
0
    if (PACKET_remaining(pkt) != 0) {
1559
0
        SSLfatal(s, SSL_AD_DECODE_ERROR,
1560
0
            SSL_R_POST_HANDSHAKE_AUTH_ENCODING_ERR);
1561
0
        return 0;
1562
0
    }
1563
1564
0
    s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
1565
1566
0
    return 1;
1567
0
}
1568
1569
/*
1570
 * Add the server's renegotiation binding
1571
 */
1572
EXT_RETURN tls_construct_stoc_renegotiate(SSL_CONNECTION *s, WPACKET *pkt,
1573
    unsigned int context, X509 *x,
1574
    size_t chainidx)
1575
0
{
1576
0
    if (!s->s3.send_connection_binding)
1577
0
        return EXT_RETURN_NOT_SENT;
1578
1579
    /* Still add this even if SSL_OP_NO_RENEGOTIATION is set */
1580
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
1581
0
        || !WPACKET_start_sub_packet_u16(pkt)
1582
0
        || !WPACKET_start_sub_packet_u8(pkt)
1583
0
        || !WPACKET_memcpy(pkt, s->s3.previous_client_finished,
1584
0
            s->s3.previous_client_finished_len)
1585
0
        || !WPACKET_memcpy(pkt, s->s3.previous_server_finished,
1586
0
            s->s3.previous_server_finished_len)
1587
0
        || !WPACKET_close(pkt)
1588
0
        || !WPACKET_close(pkt)) {
1589
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1590
0
        return EXT_RETURN_FAIL;
1591
0
    }
1592
1593
0
    return EXT_RETURN_SENT;
1594
0
}
1595
1596
EXT_RETURN tls_construct_stoc_server_name(SSL_CONNECTION *s, WPACKET *pkt,
1597
    unsigned int context, X509 *x,
1598
    size_t chainidx)
1599
0
{
1600
0
    if (s->servername_done != 1)
1601
0
        return EXT_RETURN_NOT_SENT;
1602
1603
    /*
1604
     * Prior to TLSv1.3 we ignore any SNI in the current handshake if resuming.
1605
     * We just use the servername from the initial handshake.
1606
     */
1607
0
    if (s->hit && !SSL_CONNECTION_IS_TLS13(s))
1608
0
        return EXT_RETURN_NOT_SENT;
1609
1610
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
1611
0
        || !WPACKET_put_bytes_u16(pkt, 0)) {
1612
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1613
0
        return EXT_RETURN_FAIL;
1614
0
    }
1615
1616
0
    return EXT_RETURN_SENT;
1617
0
}
1618
1619
/* Add/include the server's max fragment len extension into ServerHello */
1620
EXT_RETURN tls_construct_stoc_maxfragmentlen(SSL_CONNECTION *s, WPACKET *pkt,
1621
    unsigned int context, X509 *x,
1622
    size_t chainidx)
1623
0
{
1624
0
    if (!USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
1625
0
        return EXT_RETURN_NOT_SENT;
1626
1627
    /*-
1628
     * 4 bytes for this extension type and extension length
1629
     * 1 byte for the Max Fragment Length code value.
1630
     */
1631
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)
1632
0
        || !WPACKET_start_sub_packet_u16(pkt)
1633
0
        || !WPACKET_put_bytes_u8(pkt, s->session->ext.max_fragment_len_mode)
1634
0
        || !WPACKET_close(pkt)) {
1635
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1636
0
        return EXT_RETURN_FAIL;
1637
0
    }
1638
1639
0
    return EXT_RETURN_SENT;
1640
0
}
1641
1642
EXT_RETURN tls_construct_stoc_ec_pt_formats(SSL_CONNECTION *s, WPACKET *pkt,
1643
    unsigned int context, X509 *x,
1644
    size_t chainidx)
1645
0
{
1646
0
    unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
1647
0
    unsigned long alg_a = s->s3.tmp.new_cipher->algorithm_auth;
1648
0
    int using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))
1649
0
        && (s->ext.peer_ecpointformats != NULL);
1650
0
    const unsigned char *plist;
1651
0
    size_t plistlen;
1652
1653
0
    if (!using_ecc)
1654
0
        return EXT_RETURN_NOT_SENT;
1655
1656
0
    tls1_get_formatlist(s, &plist, &plistlen);
1657
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
1658
0
        || !WPACKET_start_sub_packet_u16(pkt)
1659
0
        || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen)
1660
0
        || !WPACKET_close(pkt)) {
1661
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1662
0
        return EXT_RETURN_FAIL;
1663
0
    }
1664
1665
0
    return EXT_RETURN_SENT;
1666
0
}
1667
1668
EXT_RETURN tls_construct_stoc_supported_groups(SSL_CONNECTION *s, WPACKET *pkt,
1669
    unsigned int context, X509 *x,
1670
    size_t chainidx)
1671
0
{
1672
0
    const uint16_t *groups;
1673
0
    size_t numgroups, i, first = 1;
1674
0
    int version;
1675
1676
    /* s->s3.group_id is non zero if we accepted a key_share */
1677
0
    if (s->s3.group_id == 0)
1678
0
        return EXT_RETURN_NOT_SENT;
1679
1680
    /* Get our list of supported groups */
1681
0
    tls1_get_supported_groups(s, &groups, &numgroups);
1682
0
    if (numgroups == 0) {
1683
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1684
0
        return EXT_RETURN_FAIL;
1685
0
    }
1686
1687
    /* Copy group ID if supported */
1688
0
    version = SSL_version(SSL_CONNECTION_GET_SSL(s));
1689
0
    for (i = 0; i < numgroups; i++) {
1690
0
        uint16_t group = groups[i];
1691
1692
0
        if (tls_valid_group(s, group, version, version, NULL, NULL)
1693
0
            && tls_group_allowed(s, group, SSL_SECOP_CURVE_SUPPORTED)) {
1694
0
            if (first) {
1695
                /*
1696
                 * Check if the client is already using our preferred group. If
1697
                 * so we don't need to add this extension
1698
                 */
1699
0
                if (s->s3.group_id == group)
1700
0
                    return EXT_RETURN_NOT_SENT;
1701
1702
                /* Add extension header */
1703
0
                if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
1704
                    /* Sub-packet for supported_groups extension */
1705
0
                    || !WPACKET_start_sub_packet_u16(pkt)
1706
0
                    || !WPACKET_start_sub_packet_u16(pkt)) {
1707
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1708
0
                    return EXT_RETURN_FAIL;
1709
0
                }
1710
1711
0
                first = 0;
1712
0
            }
1713
0
            if (!WPACKET_put_bytes_u16(pkt, group)) {
1714
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1715
0
                return EXT_RETURN_FAIL;
1716
0
            }
1717
0
        }
1718
0
    }
1719
1720
0
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
1721
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1722
0
        return EXT_RETURN_FAIL;
1723
0
    }
1724
1725
0
    return EXT_RETURN_SENT;
1726
0
}
1727
1728
EXT_RETURN tls_construct_stoc_session_ticket(SSL_CONNECTION *s, WPACKET *pkt,
1729
    unsigned int context, X509 *x,
1730
    size_t chainidx)
1731
0
{
1732
0
    if (!s->ext.ticket_expected || !tls_use_ticket(s)) {
1733
0
        s->ext.ticket_expected = 0;
1734
0
        return EXT_RETURN_NOT_SENT;
1735
0
    }
1736
1737
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
1738
0
        || !WPACKET_put_bytes_u16(pkt, 0)) {
1739
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1740
0
        return EXT_RETURN_FAIL;
1741
0
    }
1742
1743
0
    return EXT_RETURN_SENT;
1744
0
}
1745
1746
#ifndef OPENSSL_NO_OCSP
1747
EXT_RETURN tls_construct_stoc_status_request(SSL_CONNECTION *s, WPACKET *pkt,
1748
    unsigned int context, X509 *x,
1749
    size_t chainidx)
1750
0
{
1751
0
    OCSP_RESPONSE *resp;
1752
1753
    /* We don't currently support this extension inside a CertificateRequest */
1754
0
    if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST)
1755
0
        return EXT_RETURN_NOT_SENT;
1756
1757
0
    if (!s->ext.status_expected)
1758
0
        return EXT_RETURN_NOT_SENT;
1759
1760
    /* Try to retrieve OCSP response for the actual certificate */
1761
0
    resp = ossl_get_ocsp_response(s, (int)chainidx);
1762
1763
    /* If no OCSP response was found the extension is not sent */
1764
0
    if (resp == NULL)
1765
0
        return EXT_RETURN_NOT_SENT;
1766
1767
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
1768
0
        || !WPACKET_start_sub_packet_u16(pkt)) {
1769
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1770
0
        return EXT_RETURN_FAIL;
1771
0
    }
1772
1773
    /*
1774
     * In TLSv1.3 we include the certificate status itself. In <= TLSv1.2 we
1775
     * send back an empty extension, with the certificate status appearing as a
1776
     * separate message
1777
     */
1778
0
    if (SSL_CONNECTION_IS_TLS13(s)
1779
0
        && !tls_construct_cert_status_body(s, resp, pkt)) {
1780
        /* SSLfatal() already called */
1781
0
        return EXT_RETURN_FAIL;
1782
0
    }
1783
0
    if (!WPACKET_close(pkt)) {
1784
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1785
0
        return EXT_RETURN_FAIL;
1786
0
    }
1787
1788
0
    return EXT_RETURN_SENT;
1789
0
}
1790
#endif
1791
1792
#ifndef OPENSSL_NO_NEXTPROTONEG
1793
EXT_RETURN tls_construct_stoc_next_proto_neg(SSL_CONNECTION *s, WPACKET *pkt,
1794
    unsigned int context, X509 *x,
1795
    size_t chainidx)
1796
0
{
1797
0
    const unsigned char *npa;
1798
0
    unsigned int npalen;
1799
0
    int ret;
1800
0
    int npn_seen = s->s3.npn_seen;
1801
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1802
1803
0
    s->s3.npn_seen = 0;
1804
0
    if (!npn_seen || sctx->ext.npn_advertised_cb == NULL)
1805
0
        return EXT_RETURN_NOT_SENT;
1806
1807
0
    ret = sctx->ext.npn_advertised_cb(SSL_CONNECTION_GET_USER_SSL(s), &npa,
1808
0
        &npalen, sctx->ext.npn_advertised_cb_arg);
1809
0
    if (ret == SSL_TLSEXT_ERR_OK) {
1810
0
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
1811
0
            || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) {
1812
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1813
0
            return EXT_RETURN_FAIL;
1814
0
        }
1815
0
        s->s3.npn_seen = 1;
1816
0
        return EXT_RETURN_SENT;
1817
0
    }
1818
1819
0
    return EXT_RETURN_NOT_SENT;
1820
0
}
1821
#endif
1822
1823
EXT_RETURN tls_construct_stoc_alpn(SSL_CONNECTION *s, WPACKET *pkt, unsigned int context,
1824
    X509 *x, size_t chainidx)
1825
0
{
1826
0
    if (s->s3.alpn_selected == NULL)
1827
0
        return EXT_RETURN_NOT_SENT;
1828
1829
0
    if (!WPACKET_put_bytes_u16(pkt,
1830
0
            TLSEXT_TYPE_application_layer_protocol_negotiation)
1831
0
        || !WPACKET_start_sub_packet_u16(pkt)
1832
0
        || !WPACKET_start_sub_packet_u16(pkt)
1833
0
        || !WPACKET_sub_memcpy_u8(pkt, s->s3.alpn_selected,
1834
0
            s->s3.alpn_selected_len)
1835
0
        || !WPACKET_close(pkt)
1836
0
        || !WPACKET_close(pkt)) {
1837
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1838
0
        return EXT_RETURN_FAIL;
1839
0
    }
1840
1841
0
    return EXT_RETURN_SENT;
1842
0
}
1843
1844
#ifndef OPENSSL_NO_SRTP
1845
EXT_RETURN tls_construct_stoc_use_srtp(SSL_CONNECTION *s, WPACKET *pkt,
1846
    unsigned int context, X509 *x,
1847
    size_t chainidx)
1848
0
{
1849
0
    if (s->srtp_profile == NULL)
1850
0
        return EXT_RETURN_NOT_SENT;
1851
1852
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
1853
0
        || !WPACKET_start_sub_packet_u16(pkt)
1854
0
        || !WPACKET_put_bytes_u16(pkt, 2)
1855
0
        || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id)
1856
0
        || !WPACKET_put_bytes_u8(pkt, 0)
1857
0
        || !WPACKET_close(pkt)) {
1858
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1859
0
        return EXT_RETURN_FAIL;
1860
0
    }
1861
1862
0
    return EXT_RETURN_SENT;
1863
0
}
1864
#endif
1865
1866
EXT_RETURN tls_construct_stoc_etm(SSL_CONNECTION *s, WPACKET *pkt,
1867
    unsigned int context,
1868
    X509 *x, size_t chainidx)
1869
0
{
1870
0
    if (!s->ext.use_etm)
1871
0
        return EXT_RETURN_NOT_SENT;
1872
1873
    /*
1874
     * Don't use encrypt_then_mac if AEAD or RC4 might want to disable
1875
     * for other cases too.
1876
     */
1877
0
    if (s->s3.tmp.new_cipher->algorithm_mac == SSL_AEAD
1878
0
        || s->s3.tmp.new_cipher->algorithm_enc == SSL_RC4
1879
0
        || s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT
1880
0
        || s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12
1881
0
        || s->s3.tmp.new_cipher->algorithm_enc == SSL_MAGMA
1882
0
        || s->s3.tmp.new_cipher->algorithm_enc == SSL_KUZNYECHIK) {
1883
0
        s->ext.use_etm = 0;
1884
0
        return EXT_RETURN_NOT_SENT;
1885
0
    }
1886
1887
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
1888
0
        || !WPACKET_put_bytes_u16(pkt, 0)) {
1889
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1890
0
        return EXT_RETURN_FAIL;
1891
0
    }
1892
1893
0
    return EXT_RETURN_SENT;
1894
0
}
1895
1896
EXT_RETURN tls_construct_stoc_ems(SSL_CONNECTION *s, WPACKET *pkt,
1897
    unsigned int context,
1898
    X509 *x, size_t chainidx)
1899
0
{
1900
0
    if ((s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0)
1901
0
        return EXT_RETURN_NOT_SENT;
1902
1903
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
1904
0
        || !WPACKET_put_bytes_u16(pkt, 0)) {
1905
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1906
0
        return EXT_RETURN_FAIL;
1907
0
    }
1908
1909
0
    return EXT_RETURN_SENT;
1910
0
}
1911
1912
EXT_RETURN tls_construct_stoc_supported_versions(SSL_CONNECTION *s, WPACKET *pkt,
1913
    unsigned int context, X509 *x,
1914
    size_t chainidx)
1915
0
{
1916
0
    if (!ossl_assert(SSL_CONNECTION_IS_TLS13(s))) {
1917
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1918
0
        return EXT_RETURN_FAIL;
1919
0
    }
1920
1921
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
1922
0
        || !WPACKET_start_sub_packet_u16(pkt)
1923
0
        || !WPACKET_put_bytes_u16(pkt, s->version)
1924
0
        || !WPACKET_close(pkt)) {
1925
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1926
0
        return EXT_RETURN_FAIL;
1927
0
    }
1928
1929
0
    return EXT_RETURN_SENT;
1930
0
}
1931
1932
EXT_RETURN tls_construct_stoc_key_share(SSL_CONNECTION *s, WPACKET *pkt,
1933
    unsigned int context, X509 *x,
1934
    size_t chainidx)
1935
0
{
1936
0
#ifndef OPENSSL_NO_TLS1_3
1937
0
    unsigned char *encoded_pubkey;
1938
0
    size_t encoded_pubkey_len = 0;
1939
0
    EVP_PKEY *ckey = s->s3.peer_tmp, *skey = NULL;
1940
0
    const TLS_GROUP_INFO *ginf = NULL;
1941
1942
0
    if (s->hello_retry_request == SSL_HRR_PENDING) {
1943
0
        if (ckey != NULL) {
1944
            /* Original key_share was acceptable so don't ask for another one */
1945
0
            return EXT_RETURN_NOT_SENT;
1946
0
        }
1947
0
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
1948
0
            || !WPACKET_start_sub_packet_u16(pkt)
1949
0
            || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)
1950
0
            || !WPACKET_close(pkt)) {
1951
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1952
0
            return EXT_RETURN_FAIL;
1953
0
        }
1954
1955
0
        return EXT_RETURN_SENT;
1956
0
    }
1957
1958
0
    if (ckey == NULL) {
1959
        /* No key_share received from client - must be resuming */
1960
0
        if (!s->hit || !tls13_generate_handshake_secret(s, NULL, 0)) {
1961
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1962
0
            return EXT_RETURN_FAIL;
1963
0
        }
1964
0
        return EXT_RETURN_NOT_SENT;
1965
0
    }
1966
1967
0
    if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0) {
1968
        /*
1969
         * PSK ('hit') and explicitly not doing DHE. If the client sent the
1970
         * DHE option, we take it by default, except if non-DHE would be
1971
         * preferred by config, but this case would have been handled in
1972
         * tls_parse_ctos_psk_kex_modes().
1973
         */
1974
0
        return EXT_RETURN_NOT_SENT;
1975
0
    }
1976
1977
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
1978
0
        || !WPACKET_start_sub_packet_u16(pkt)
1979
0
        || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)) {
1980
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1981
0
        return EXT_RETURN_FAIL;
1982
0
    }
1983
1984
0
    if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s),
1985
0
             s->s3.group_id))
1986
0
        == NULL) {
1987
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1988
0
        return EXT_RETURN_FAIL;
1989
0
    }
1990
1991
0
    if (!ginf->is_kem) {
1992
        /* Regular KEX */
1993
0
        skey = ssl_generate_pkey(s, ckey);
1994
0
        if (skey == NULL) {
1995
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
1996
0
            return EXT_RETURN_FAIL;
1997
0
        }
1998
1999
        /* Generate encoding of server key */
2000
0
        encoded_pubkey_len = EVP_PKEY_get1_encoded_public_key(skey, &encoded_pubkey);
2001
0
        if (encoded_pubkey_len == 0) {
2002
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
2003
0
            EVP_PKEY_free(skey);
2004
0
            return EXT_RETURN_FAIL;
2005
0
        }
2006
2007
0
        if (!WPACKET_sub_memcpy_u16(pkt, encoded_pubkey, encoded_pubkey_len)
2008
0
            || !WPACKET_close(pkt)) {
2009
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2010
0
            EVP_PKEY_free(skey);
2011
0
            OPENSSL_free(encoded_pubkey);
2012
0
            return EXT_RETURN_FAIL;
2013
0
        }
2014
0
        OPENSSL_free(encoded_pubkey);
2015
2016
        /*
2017
         * This causes the crypto state to be updated based on the derived keys
2018
         */
2019
0
        if (ssl_derive(s, skey, ckey, 1) == 0) {
2020
            /* SSLfatal() already called */
2021
0
            EVP_PKEY_free(skey);
2022
0
            return EXT_RETURN_FAIL;
2023
0
        }
2024
0
        s->s3.tmp.pkey = skey;
2025
0
    } else {
2026
        /* KEM mode */
2027
0
        unsigned char *ct = NULL;
2028
0
        size_t ctlen = 0;
2029
2030
        /*
2031
         * This does not update the crypto state.
2032
         *
2033
         * The generated pms is stored in `s->s3.tmp.pms` to be later used via
2034
         * ssl_gensecret().
2035
         */
2036
0
        if (ssl_encapsulate(s, ckey, &ct, &ctlen, 0) == 0) {
2037
            /* SSLfatal() already called */
2038
0
            return EXT_RETURN_FAIL;
2039
0
        }
2040
2041
0
        if (ctlen == 0) {
2042
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2043
0
            OPENSSL_free(ct);
2044
0
            return EXT_RETURN_FAIL;
2045
0
        }
2046
2047
0
        if (!WPACKET_sub_memcpy_u16(pkt, ct, ctlen)
2048
0
            || !WPACKET_close(pkt)) {
2049
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2050
0
            OPENSSL_free(ct);
2051
0
            return EXT_RETURN_FAIL;
2052
0
        }
2053
0
        OPENSSL_free(ct);
2054
2055
        /*
2056
         * This causes the crypto state to be updated based on the generated pms
2057
         */
2058
0
        if (ssl_gensecret(s, s->s3.tmp.pms, s->s3.tmp.pmslen) == 0) {
2059
            /* SSLfatal() already called */
2060
0
            return EXT_RETURN_FAIL;
2061
0
        }
2062
0
    }
2063
0
    s->s3.did_kex = 1;
2064
0
    return EXT_RETURN_SENT;
2065
#else
2066
    return EXT_RETURN_FAIL;
2067
#endif
2068
0
}
2069
2070
EXT_RETURN tls_construct_stoc_cookie(SSL_CONNECTION *s, WPACKET *pkt,
2071
    unsigned int context,
2072
    X509 *x, size_t chainidx)
2073
0
{
2074
0
#ifndef OPENSSL_NO_TLS1_3
2075
0
    unsigned char *hashval1, *hashval2, *appcookie1, *appcookie2, *cookie;
2076
0
    unsigned char *hmac, *hmac2;
2077
0
    size_t startlen, ciphlen, totcookielen, hashlen, hmaclen, appcookielen;
2078
0
    EVP_MD_CTX *hctx;
2079
0
    EVP_PKEY *pkey;
2080
0
    int ret = EXT_RETURN_FAIL;
2081
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2082
0
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
2083
0
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
2084
2085
0
    if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
2086
0
        return EXT_RETURN_NOT_SENT;
2087
2088
0
    if (sctx->gen_stateless_cookie_cb == NULL) {
2089
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_COOKIE_CALLBACK_SET);
2090
0
        return EXT_RETURN_FAIL;
2091
0
    }
2092
2093
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
2094
0
        || !WPACKET_start_sub_packet_u16(pkt)
2095
0
        || !WPACKET_start_sub_packet_u16(pkt)
2096
0
        || !WPACKET_get_total_written(pkt, &startlen)
2097
0
        || !WPACKET_reserve_bytes(pkt, MAX_COOKIE_SIZE, &cookie)
2098
0
        || !WPACKET_put_bytes_u16(pkt, COOKIE_STATE_FORMAT_VERSION)
2099
0
        || !WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION)
2100
0
        || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)
2101
0
        || !ssl->method->put_cipher_by_char(s->s3.tmp.new_cipher, pkt,
2102
0
            &ciphlen)
2103
        /* Is there a key_share extension present in this HRR? */
2104
0
        || !WPACKET_put_bytes_u8(pkt, s->s3.peer_tmp == NULL)
2105
0
        || !WPACKET_put_bytes_u64(pkt, time(NULL))
2106
0
        || !WPACKET_start_sub_packet_u16(pkt)
2107
0
        || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &hashval1)) {
2108
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2109
0
        return EXT_RETURN_FAIL;
2110
0
    }
2111
2112
    /*
2113
     * Get the hash of the initial ClientHello. ssl_handshake_hash() operates
2114
     * on raw buffers, so we first reserve sufficient bytes (above) and then
2115
     * subsequently allocate them (below)
2116
     */
2117
0
    if (!ssl3_digest_cached_records(s, 0)
2118
0
        || !ssl_handshake_hash(s, hashval1, EVP_MAX_MD_SIZE, &hashlen)) {
2119
        /* SSLfatal() already called */
2120
0
        return EXT_RETURN_FAIL;
2121
0
    }
2122
2123
0
    if (!WPACKET_allocate_bytes(pkt, hashlen, &hashval2)
2124
0
        || !ossl_assert(hashval1 == hashval2)
2125
0
        || !WPACKET_close(pkt)
2126
0
        || !WPACKET_start_sub_packet_u8(pkt)
2127
0
        || !WPACKET_reserve_bytes(pkt, SSL_COOKIE_LENGTH, &appcookie1)) {
2128
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2129
0
        return EXT_RETURN_FAIL;
2130
0
    }
2131
2132
    /* Generate the application cookie */
2133
0
    if (sctx->gen_stateless_cookie_cb(ussl, appcookie1,
2134
0
            &appcookielen)
2135
0
        == 0) {
2136
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
2137
0
        return EXT_RETURN_FAIL;
2138
0
    }
2139
2140
0
    if (!WPACKET_allocate_bytes(pkt, appcookielen, &appcookie2)
2141
0
        || !ossl_assert(appcookie1 == appcookie2)
2142
0
        || !WPACKET_close(pkt)
2143
0
        || !WPACKET_get_total_written(pkt, &totcookielen)
2144
0
        || !WPACKET_reserve_bytes(pkt, SHA256_DIGEST_LENGTH, &hmac)) {
2145
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2146
0
        return EXT_RETURN_FAIL;
2147
0
    }
2148
0
    hmaclen = SHA256_DIGEST_LENGTH;
2149
2150
0
    totcookielen -= startlen;
2151
0
    if (!ossl_assert(totcookielen <= MAX_COOKIE_SIZE - SHA256_DIGEST_LENGTH)) {
2152
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2153
0
        return EXT_RETURN_FAIL;
2154
0
    }
2155
2156
    /* HMAC the cookie */
2157
0
    hctx = EVP_MD_CTX_create();
2158
0
    pkey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",
2159
0
        sctx->propq,
2160
0
        s->session_ctx->ext.cookie_hmac_key,
2161
0
        sizeof(s->session_ctx->ext.cookie_hmac_key));
2162
0
    if (hctx == NULL || pkey == NULL) {
2163
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2164
0
        goto err;
2165
0
    }
2166
2167
0
    if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", sctx->libctx,
2168
0
            sctx->propq, pkey, NULL)
2169
0
            <= 0
2170
0
        || EVP_DigestSign(hctx, hmac, &hmaclen, cookie,
2171
0
               totcookielen)
2172
0
            <= 0) {
2173
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2174
0
        goto err;
2175
0
    }
2176
2177
0
    if (!ossl_assert(totcookielen + hmaclen <= MAX_COOKIE_SIZE)) {
2178
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2179
0
        goto err;
2180
0
    }
2181
2182
0
    if (!WPACKET_allocate_bytes(pkt, hmaclen, &hmac2)
2183
0
        || !ossl_assert(hmac == hmac2)
2184
0
        || !ossl_assert(cookie == hmac - totcookielen)
2185
0
        || !WPACKET_close(pkt)
2186
0
        || !WPACKET_close(pkt)) {
2187
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2188
0
        goto err;
2189
0
    }
2190
2191
0
    ret = EXT_RETURN_SENT;
2192
2193
0
err:
2194
0
    EVP_MD_CTX_free(hctx);
2195
0
    EVP_PKEY_free(pkey);
2196
0
    return ret;
2197
#else
2198
    return EXT_RETURN_FAIL;
2199
#endif
2200
0
}
2201
2202
EXT_RETURN tls_construct_stoc_cryptopro_bug(SSL_CONNECTION *s, WPACKET *pkt,
2203
    unsigned int context, X509 *x,
2204
    size_t chainidx)
2205
0
{
2206
0
    const unsigned char cryptopro_ext[36] = {
2207
0
        0xfd, 0xe8, /* 65000 */
2208
0
        0x00, 0x20, /* 32 bytes length */
2209
0
        0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
2210
0
        0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
2211
0
        0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
2212
0
        0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
2213
0
    };
2214
2215
0
    if (((s->s3.tmp.new_cipher->id & 0xFFFF) != 0x80
2216
0
            && (s->s3.tmp.new_cipher->id & 0xFFFF) != 0x81)
2217
0
        || (SSL_get_options(SSL_CONNECTION_GET_SSL(s))
2218
0
               & SSL_OP_CRYPTOPRO_TLSEXT_BUG)
2219
0
            == 0)
2220
0
        return EXT_RETURN_NOT_SENT;
2221
2222
0
    if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) {
2223
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2224
0
        return EXT_RETURN_FAIL;
2225
0
    }
2226
2227
0
    return EXT_RETURN_SENT;
2228
0
}
2229
2230
EXT_RETURN tls_construct_stoc_early_data(SSL_CONNECTION *s, WPACKET *pkt,
2231
    unsigned int context, X509 *x,
2232
    size_t chainidx)
2233
0
{
2234
0
    if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
2235
0
        if (s->max_early_data == 0)
2236
0
            return EXT_RETURN_NOT_SENT;
2237
2238
0
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
2239
0
            || !WPACKET_start_sub_packet_u16(pkt)
2240
0
            || !WPACKET_put_bytes_u32(pkt, s->max_early_data)
2241
0
            || !WPACKET_close(pkt)) {
2242
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2243
0
            return EXT_RETURN_FAIL;
2244
0
        }
2245
2246
0
        return EXT_RETURN_SENT;
2247
0
    }
2248
2249
0
    if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
2250
0
        return EXT_RETURN_NOT_SENT;
2251
2252
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
2253
0
        || !WPACKET_start_sub_packet_u16(pkt)
2254
0
        || !WPACKET_close(pkt)) {
2255
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2256
0
        return EXT_RETURN_FAIL;
2257
0
    }
2258
2259
0
    return EXT_RETURN_SENT;
2260
0
}
2261
2262
EXT_RETURN tls_construct_stoc_psk(SSL_CONNECTION *s, WPACKET *pkt,
2263
    unsigned int context,
2264
    X509 *x, size_t chainidx)
2265
0
{
2266
0
    if (!s->hit)
2267
0
        return EXT_RETURN_NOT_SENT;
2268
2269
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
2270
0
        || !WPACKET_start_sub_packet_u16(pkt)
2271
0
        || !WPACKET_put_bytes_u16(pkt, s->ext.tick_identity)
2272
0
        || !WPACKET_close(pkt)) {
2273
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2274
0
        return EXT_RETURN_FAIL;
2275
0
    }
2276
2277
0
    return EXT_RETURN_SENT;
2278
0
}
2279
2280
EXT_RETURN tls_construct_stoc_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2281
    unsigned int context,
2282
    X509 *x, size_t chainidx)
2283
0
{
2284
0
    if (sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_ERROR
2285
0
        && (send_certificate_request(sc)
2286
0
            || sc->post_handshake_auth == SSL_PHA_EXT_RECEIVED)) {
2287
        /* Did not receive an acceptable cert type - and doing client auth */
2288
0
        SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION);
2289
0
        return EXT_RETURN_FAIL;
2290
0
    }
2291
2292
0
    if (sc->ext.client_cert_type == TLSEXT_cert_type_x509) {
2293
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2294
0
        return EXT_RETURN_NOT_SENT;
2295
0
    }
2296
2297
    /*
2298
     * Note: only supposed to send this if we are going to do a cert request,
2299
     * but TLSv1.3 could do a PHA request if the client supports it
2300
     */
2301
0
    if ((!send_certificate_request(sc) && sc->post_handshake_auth != SSL_PHA_EXT_RECEIVED)
2302
0
        || sc->ext.client_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD
2303
0
        || sc->client_cert_type == NULL) {
2304
        /* if we don't send it, reset to TLSEXT_cert_type_x509 */
2305
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2306
0
        sc->ext.client_cert_type = TLSEXT_cert_type_x509;
2307
0
        return EXT_RETURN_NOT_SENT;
2308
0
    }
2309
2310
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type)
2311
0
        || !WPACKET_start_sub_packet_u16(pkt)
2312
0
        || !WPACKET_put_bytes_u8(pkt, sc->ext.client_cert_type)
2313
0
        || !WPACKET_close(pkt)) {
2314
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2315
0
        return EXT_RETURN_FAIL;
2316
0
    }
2317
0
    return EXT_RETURN_SENT;
2318
0
}
2319
2320
/* One of |pref|, |other| is configured and the values are sanitized */
2321
static int reconcile_cert_type(const unsigned char *pref, size_t pref_len,
2322
    const unsigned char *other, size_t other_len,
2323
    uint8_t *chosen_cert_type)
2324
0
{
2325
0
    size_t i;
2326
2327
0
    for (i = 0; i < pref_len; i++) {
2328
0
        if (memchr(other, pref[i], other_len) != NULL) {
2329
0
            *chosen_cert_type = pref[i];
2330
0
            return OSSL_CERT_TYPE_CTOS_GOOD;
2331
0
        }
2332
0
    }
2333
0
    return OSSL_CERT_TYPE_CTOS_ERROR;
2334
0
}
2335
2336
int tls_parse_ctos_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2337
    unsigned int context,
2338
    X509 *x, size_t chainidx)
2339
0
{
2340
0
    PACKET supported_cert_types;
2341
0
    const unsigned char *data;
2342
0
    size_t len;
2343
2344
    /* Ignore the extension */
2345
0
    if (sc->client_cert_type == NULL) {
2346
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2347
0
        sc->ext.client_cert_type = TLSEXT_cert_type_x509;
2348
0
        return 1;
2349
0
    }
2350
2351
0
    if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) {
2352
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;
2353
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2354
0
        return 0;
2355
0
    }
2356
0
    if ((len = PACKET_remaining(&supported_cert_types)) == 0) {
2357
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;
2358
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2359
0
        return 0;
2360
0
    }
2361
0
    if (!PACKET_get_bytes(&supported_cert_types, &data, len)) {
2362
0
        sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR;
2363
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2364
0
        return 0;
2365
0
    }
2366
    /* client_cert_type: client (peer) has priority */
2367
0
    sc->ext.client_cert_type_ctos = reconcile_cert_type(data, len,
2368
0
        sc->client_cert_type, sc->client_cert_type_len,
2369
0
        &sc->ext.client_cert_type);
2370
2371
    /* Ignore the error until sending - so we can check cert auth*/
2372
0
    return 1;
2373
0
}
2374
2375
EXT_RETURN tls_construct_stoc_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt,
2376
    unsigned int context,
2377
    X509 *x, size_t chainidx)
2378
0
{
2379
0
    if (sc->ext.server_cert_type == TLSEXT_cert_type_x509) {
2380
0
        sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2381
0
        return EXT_RETURN_NOT_SENT;
2382
0
    }
2383
0
    if (sc->ext.server_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD
2384
0
        || sc->server_cert_type == NULL) {
2385
        /* if we don't send it, reset to TLSEXT_cert_type_x509 */
2386
0
        sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2387
0
        sc->ext.server_cert_type = TLSEXT_cert_type_x509;
2388
0
        return EXT_RETURN_NOT_SENT;
2389
0
    }
2390
2391
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type)
2392
0
        || !WPACKET_start_sub_packet_u16(pkt)
2393
0
        || !WPACKET_put_bytes_u8(pkt, sc->ext.server_cert_type)
2394
0
        || !WPACKET_close(pkt)) {
2395
0
        SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2396
0
        return EXT_RETURN_FAIL;
2397
0
    }
2398
0
    return EXT_RETURN_SENT;
2399
0
}
2400
2401
int tls_parse_ctos_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt,
2402
    unsigned int context,
2403
    X509 *x, size_t chainidx)
2404
0
{
2405
0
    PACKET supported_cert_types;
2406
0
    const unsigned char *data;
2407
0
    size_t len;
2408
2409
    /* Ignore the extension */
2410
0
    if (sc->server_cert_type == NULL) {
2411
0
        sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE;
2412
0
        sc->ext.server_cert_type = TLSEXT_cert_type_x509;
2413
0
        return 1;
2414
0
    }
2415
2416
0
    if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) {
2417
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2418
0
        return 0;
2419
0
    }
2420
2421
0
    if ((len = PACKET_remaining(&supported_cert_types)) == 0) {
2422
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2423
0
        return 0;
2424
0
    }
2425
0
    if (!PACKET_get_bytes(&supported_cert_types, &data, len)) {
2426
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2427
0
        return 0;
2428
0
    }
2429
    /* server_cert_type: server (this) has priority */
2430
0
    sc->ext.server_cert_type_ctos = reconcile_cert_type(sc->server_cert_type, sc->server_cert_type_len,
2431
0
        data, len,
2432
0
        &sc->ext.server_cert_type);
2433
0
    if (sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD)
2434
0
        return 1;
2435
2436
    /* Did not receive an acceptable cert type */
2437
0
    SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION);
2438
0
    return 0;
2439
0
}
2440
2441
#ifndef OPENSSL_NO_ECH
2442
/*
2443
 * ECH handling for edge cases (GREASE/inner) and errors.
2444
 * return 1 for good, 0 otherwise
2445
 *
2446
 * Real ECH handling (i.e. decryption) happens before, via
2447
 * ech_early_decrypt(), but if that failed (e.g. decryption
2448
 * failed, which may be down to GREASE) then we end up here,
2449
 * processing the ECH from the outer CH.
2450
 * Otherwise, we only expect to see an inner ECH with a fixed
2451
 * value here.
2452
 */
2453
int tls_parse_ctos_ech(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
2454
    X509 *x, size_t chainidx)
2455
0
{
2456
0
    unsigned int echtype = 0;
2457
2458
0
    if (s->ext.ech.grease == OSSL_ECH_IS_GREASE) {
2459
        /* GREASE is fine */
2460
0
        return 1;
2461
0
    }
2462
0
    if (s->ext.ech.es == NULL) {
2463
        /* If not configured for ECH then we ignore it */
2464
0
        return 1;
2465
0
    }
2466
0
    if (s->ext.ech.attempted_type != TLSEXT_TYPE_ech) {
2467
        /* if/when new versions of ECH are added we'll update here */
2468
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2469
0
        return 0;
2470
0
    }
2471
    /*
2472
     * we only allow "inner" which is one octet, valued 0x01
2473
     * and only if we decrypted ok or are a backend
2474
     */
2475
0
    if (PACKET_get_1(pkt, &echtype) != 1
2476
0
        || echtype != OSSL_ECH_INNER_CH_TYPE
2477
0
        || PACKET_remaining(pkt) != 0) {
2478
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2479
0
        return 0;
2480
0
    }
2481
0
    if (s->ext.ech.success != 1 && s->ext.ech.backend != 1) {
2482
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
2483
0
        return 0;
2484
0
    }
2485
    /* yay - we're ok with this */
2486
0
    OSSL_TRACE_BEGIN(TLS)
2487
0
    {
2488
0
        BIO_printf(trc_out, "ECH seen in inner as expected.\n");
2489
0
    }
2490
0
    OSSL_TRACE_END(TLS);
2491
0
    return 1;
2492
0
}
2493
2494
/*
2495
 * Answer an ECH, as needed
2496
 * return 1 for good, 0 otherwise
2497
 *
2498
 * Return most-recent ECH config for retry, as needed.
2499
 * If doing HRR we include the confirmation value, but
2500
 * for now, we'll just add the zeros - the real octets
2501
 * will be added later via ech_calc_ech_confirm() which
2502
 * is called when constructing the server hello.
2503
 */
2504
EXT_RETURN tls_construct_stoc_ech(SSL_CONNECTION *s, WPACKET *pkt,
2505
    unsigned int context, X509 *x,
2506
    size_t chainidx)
2507
0
{
2508
0
    unsigned char *rcfgs = NULL;
2509
0
    size_t rcfgslen = 0;
2510
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2511
2512
0
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2513
0
        && (s->ext.ech.success == 1 || s->ext.ech.backend == 1)
2514
0
        && s->ext.ech.attempted_type == TLSEXT_TYPE_ech) {
2515
0
        unsigned char eightzeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
2516
2517
0
        if (!WPACKET_put_bytes_u16(pkt, s->ext.ech.attempted_type)
2518
0
            || !WPACKET_sub_memcpy_u16(pkt, eightzeros, 8)) {
2519
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2520
0
            return 0;
2521
0
        }
2522
0
        OSSL_TRACE_BEGIN(TLS)
2523
0
        {
2524
0
            BIO_printf(trc_out, "set 8 zeros for ECH accept confirm in HRR\n");
2525
0
        }
2526
0
        OSSL_TRACE_END(TLS);
2527
0
        return EXT_RETURN_SENT;
2528
0
    }
2529
    /* GREASE or error => random confirmation in HRR case */
2530
0
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2531
0
        && s->ext.ech.attempted_type == TLSEXT_TYPE_ech
2532
0
        && s->ext.ech.attempted == 1) {
2533
0
        unsigned char randomconf[8];
2534
2535
0
        if (RAND_bytes_ex(sctx->libctx, randomconf, 8,
2536
0
                RAND_DRBG_STRENGTH)
2537
0
            <= 0) {
2538
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2539
0
            return 0;
2540
0
        }
2541
0
        if (!WPACKET_put_bytes_u16(pkt, s->ext.ech.attempted_type)
2542
0
            || !WPACKET_sub_memcpy_u16(pkt, randomconf, 8)) {
2543
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2544
0
            return 0;
2545
0
        }
2546
0
        OSSL_TRACE_BEGIN(TLS)
2547
0
        {
2548
0
            BIO_printf(trc_out, "set random for ECH acccpt confirm in HRR\n");
2549
0
        }
2550
0
        OSSL_TRACE_END(TLS);
2551
0
        return EXT_RETURN_SENT;
2552
0
    }
2553
    /* in other HRR circumstances: don't set */
2554
0
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)
2555
0
        return EXT_RETURN_NOT_SENT;
2556
    /* If in some weird state we ignore and send nothing */
2557
0
    if (s->ext.ech.grease != OSSL_ECH_IS_GREASE
2558
0
        || s->ext.ech.attempted_type != TLSEXT_TYPE_ech)
2559
0
        return EXT_RETURN_NOT_SENT;
2560
    /*
2561
     * If the client GREASEd, or we think it did, return the
2562
     * most-recently loaded ECHConfigList, as the value of the
2563
     * extension. Most-recently loaded can be anywhere in the
2564
     * list, depending on changing or non-changing file names.
2565
     */
2566
0
    if (s->ext.ech.es == NULL) {
2567
0
        OSSL_TRACE_BEGIN(TLS)
2568
0
        {
2569
0
            BIO_printf(trc_out, "ECH - not sending ECHConfigList to client "
2570
0
                                "even though they GREASE'd as I've no loaded configs\n");
2571
0
        }
2572
0
        OSSL_TRACE_END(TLS);
2573
0
        return EXT_RETURN_NOT_SENT;
2574
0
    }
2575
0
    if (ossl_ech_get_retry_configs(s, &rcfgs, &rcfgslen) != 1) {
2576
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2577
0
        return 0;
2578
0
    }
2579
0
    if (rcfgslen == 0) {
2580
0
        OSSL_TRACE_BEGIN(TLS)
2581
0
        {
2582
0
            BIO_printf(trc_out, "ECH - not sending ECHConfigList to client "
2583
0
                                "even though they GREASE'd and I have configs but "
2584
0
                                "I've no configs set to be returned\n");
2585
0
        }
2586
0
        OSSL_TRACE_END(TLS);
2587
0
        OPENSSL_free(rcfgs);
2588
0
        return EXT_RETURN_NOT_SENT;
2589
0
    }
2590
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ech)
2591
0
        || !WPACKET_start_sub_packet_u16(pkt)
2592
0
        || !WPACKET_sub_memcpy_u16(pkt, rcfgs, rcfgslen)
2593
0
        || !WPACKET_close(pkt)) {
2594
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2595
0
        OPENSSL_free(rcfgs);
2596
0
        return 0;
2597
0
    }
2598
0
    OPENSSL_free(rcfgs);
2599
0
    return EXT_RETURN_SENT;
2600
0
}
2601
#endif /* END OPENSSL_NO_ECH */