Coverage Report

Created: 2026-04-22 06:14

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