Coverage Report

Created: 2025-10-28 06:56

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