Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/ssl/statem/extensions_clnt.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <openssl/ocsp.h>
11
#include "../ssl_local.h"
12
#include "internal/cryptlib.h"
13
#include "statem_local.h"
14
15
EXT_RETURN tls_construct_ctos_renegotiate(SSL *s, WPACKET *pkt,
16
                                          unsigned int context, X509 *x,
17
                                          size_t chainidx)
18
0
{
19
    /* Add RI if renegotiating */
20
0
    if (!s->renegotiate)
21
0
        return EXT_RETURN_NOT_SENT;
22
23
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate)
24
0
            || !WPACKET_start_sub_packet_u16(pkt)
25
0
            || !WPACKET_sub_memcpy_u8(pkt, s->s3->previous_client_finished,
26
0
                               s->s3->previous_client_finished_len)
27
0
            || !WPACKET_close(pkt)) {
28
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_RENEGOTIATE,
29
0
                 ERR_R_INTERNAL_ERROR);
30
0
        return EXT_RETURN_FAIL;
31
0
    }
32
33
0
    return EXT_RETURN_SENT;
34
0
}
35
36
EXT_RETURN tls_construct_ctos_server_name(SSL *s, WPACKET *pkt,
37
                                          unsigned int context, X509 *x,
38
                                          size_t chainidx)
39
0
{
40
0
    if (s->ext.hostname == NULL)
41
0
        return EXT_RETURN_NOT_SENT;
42
43
    /* Add TLS extension servername to the Client Hello message */
44
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name)
45
               /* Sub-packet for server_name extension */
46
0
            || !WPACKET_start_sub_packet_u16(pkt)
47
               /* Sub-packet for servername list (always 1 hostname)*/
48
0
            || !WPACKET_start_sub_packet_u16(pkt)
49
0
            || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name)
50
0
            || !WPACKET_sub_memcpy_u16(pkt, s->ext.hostname,
51
0
                                       strlen(s->ext.hostname))
52
0
            || !WPACKET_close(pkt)
53
0
            || !WPACKET_close(pkt)) {
54
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_SERVER_NAME,
55
0
                 ERR_R_INTERNAL_ERROR);
56
0
        return EXT_RETURN_FAIL;
57
0
    }
58
59
0
    return EXT_RETURN_SENT;
60
0
}
61
62
/* Push a Max Fragment Len extension into ClientHello */
63
EXT_RETURN tls_construct_ctos_maxfragmentlen(SSL *s, WPACKET *pkt,
64
                                             unsigned int context, X509 *x,
65
                                             size_t chainidx)
66
0
{
67
0
    if (s->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_DISABLED)
68
0
        return EXT_RETURN_NOT_SENT;
69
70
    /* Add Max Fragment Length extension if client enabled it. */
71
    /*-
72
     * 4 bytes for this extension type and extension length
73
     * 1 byte for the Max Fragment Length code value.
74
     */
75
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length)
76
            /* Sub-packet for Max Fragment Length extension (1 byte) */
77
0
            || !WPACKET_start_sub_packet_u16(pkt)
78
0
            || !WPACKET_put_bytes_u8(pkt, s->ext.max_fragment_len_mode)
79
0
            || !WPACKET_close(pkt)) {
80
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
81
0
                 SSL_F_TLS_CONSTRUCT_CTOS_MAXFRAGMENTLEN, ERR_R_INTERNAL_ERROR);
82
0
        return EXT_RETURN_FAIL;
83
0
    }
84
85
0
    return EXT_RETURN_SENT;
86
0
}
87
88
#ifndef OPENSSL_NO_SRP
89
EXT_RETURN tls_construct_ctos_srp(SSL *s, WPACKET *pkt, unsigned int context,
90
                                  X509 *x, size_t chainidx)
91
0
{
92
    /* Add SRP username if there is one */
93
0
    if (s->srp_ctx.login == NULL)
94
0
        return EXT_RETURN_NOT_SENT;
95
96
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp)
97
               /* Sub-packet for SRP extension */
98
0
            || !WPACKET_start_sub_packet_u16(pkt)
99
0
            || !WPACKET_start_sub_packet_u8(pkt)
100
               /* login must not be zero...internal error if so */
101
0
            || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
102
0
            || !WPACKET_memcpy(pkt, s->srp_ctx.login,
103
0
                               strlen(s->srp_ctx.login))
104
0
            || !WPACKET_close(pkt)
105
0
            || !WPACKET_close(pkt)) {
106
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_SRP,
107
0
                 ERR_R_INTERNAL_ERROR);
108
0
        return EXT_RETURN_FAIL;
109
0
    }
110
111
0
    return EXT_RETURN_SENT;
112
0
}
113
#endif
114
115
#ifndef OPENSSL_NO_EC
116
static int use_ecc(SSL *s)
117
0
{
118
0
    int i, end, ret = 0;
119
0
    unsigned long alg_k, alg_a;
120
0
    STACK_OF(SSL_CIPHER) *cipher_stack = NULL;
121
0
    const uint16_t *pgroups = NULL;
122
0
    size_t num_groups, j;
123
124
    /* See if we support any ECC ciphersuites */
125
0
    if (s->version == SSL3_VERSION)
126
0
        return 0;
127
128
0
    cipher_stack = SSL_get1_supported_ciphers(s);
129
0
    end = sk_SSL_CIPHER_num(cipher_stack);
130
0
    for (i = 0; i < end; i++) {
131
0
        const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
132
133
0
        alg_k = c->algorithm_mkey;
134
0
        alg_a = c->algorithm_auth;
135
0
        if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK))
136
0
                || (alg_a & SSL_aECDSA)
137
0
                || c->min_tls >= TLS1_3_VERSION) {
138
0
            ret = 1;
139
0
            break;
140
0
        }
141
0
    }
142
143
0
    sk_SSL_CIPHER_free(cipher_stack);
144
0
    if (!ret)
145
0
        return 0;
146
147
    /* Check we have at least one EC supported group */
148
0
    tls1_get_supported_groups(s, &pgroups, &num_groups);
149
0
    for (j = 0; j < num_groups; j++) {
150
0
        uint16_t ctmp = pgroups[j];
151
152
0
        if (tls_curve_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED))
153
0
            return 1;
154
0
    }
155
156
0
    return 0;
157
0
}
158
159
EXT_RETURN tls_construct_ctos_ec_pt_formats(SSL *s, WPACKET *pkt,
160
                                            unsigned int context, X509 *x,
161
                                            size_t chainidx)
162
0
{
163
0
    const unsigned char *pformats;
164
0
    size_t num_formats;
165
166
0
    if (!use_ecc(s))
167
0
        return EXT_RETURN_NOT_SENT;
168
169
    /* Add TLS extension ECPointFormats to the ClientHello message */
170
0
    tls1_get_formatlist(s, &pformats, &num_formats);
171
172
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats)
173
               /* Sub-packet for formats extension */
174
0
            || !WPACKET_start_sub_packet_u16(pkt)
175
0
            || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats)
176
0
            || !WPACKET_close(pkt)) {
177
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
178
0
                 SSL_F_TLS_CONSTRUCT_CTOS_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
179
0
        return EXT_RETURN_FAIL;
180
0
    }
181
182
0
    return EXT_RETURN_SENT;
183
0
}
184
185
EXT_RETURN tls_construct_ctos_supported_groups(SSL *s, WPACKET *pkt,
186
                                               unsigned int context, X509 *x,
187
                                               size_t chainidx)
188
0
{
189
0
    const uint16_t *pgroups = NULL;
190
0
    size_t num_groups = 0, i;
191
192
0
    if (!use_ecc(s))
193
0
        return EXT_RETURN_NOT_SENT;
194
195
    /*
196
     * Add TLS extension supported_groups to the ClientHello message
197
     */
198
    /* TODO(TLS1.3): Add support for DHE groups */
199
0
    tls1_get_supported_groups(s, &pgroups, &num_groups);
200
201
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups)
202
               /* Sub-packet for supported_groups extension */
203
0
            || !WPACKET_start_sub_packet_u16(pkt)
204
0
            || !WPACKET_start_sub_packet_u16(pkt)) {
205
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
206
0
                 SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
207
0
                 ERR_R_INTERNAL_ERROR);
208
0
        return EXT_RETURN_FAIL;
209
0
    }
210
    /* Copy curve ID if supported */
211
0
    for (i = 0; i < num_groups; i++) {
212
0
        uint16_t ctmp = pgroups[i];
213
214
0
        if (tls_curve_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) {
215
0
            if (!WPACKET_put_bytes_u16(pkt, ctmp)) {
216
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR,
217
0
                             SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
218
0
                             ERR_R_INTERNAL_ERROR);
219
0
                    return EXT_RETURN_FAIL;
220
0
                }
221
0
        }
222
0
    }
223
0
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
224
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
225
0
                 SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_GROUPS,
226
0
                 ERR_R_INTERNAL_ERROR);
227
0
        return EXT_RETURN_FAIL;
228
0
    }
229
230
0
    return EXT_RETURN_SENT;
231
0
}
232
#endif
233
234
EXT_RETURN tls_construct_ctos_session_ticket(SSL *s, WPACKET *pkt,
235
                                             unsigned int context, X509 *x,
236
                                             size_t chainidx)
237
0
{
238
0
    size_t ticklen;
239
240
0
    if (!tls_use_ticket(s))
241
0
        return EXT_RETURN_NOT_SENT;
242
243
0
    if (!s->new_session && s->session != NULL
244
0
            && s->session->ext.tick != NULL
245
0
            && s->session->ssl_version != TLS1_3_VERSION) {
246
0
        ticklen = s->session->ext.ticklen;
247
0
    } else if (s->session && s->ext.session_ticket != NULL
248
0
               && s->ext.session_ticket->data != NULL) {
249
0
        ticklen = s->ext.session_ticket->length;
250
0
        s->session->ext.tick = OPENSSL_malloc(ticklen);
251
0
        if (s->session->ext.tick == NULL) {
252
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
253
0
                     SSL_F_TLS_CONSTRUCT_CTOS_SESSION_TICKET,
254
0
                     ERR_R_INTERNAL_ERROR);
255
0
            return EXT_RETURN_FAIL;
256
0
        }
257
0
        memcpy(s->session->ext.tick,
258
0
               s->ext.session_ticket->data, ticklen);
259
0
        s->session->ext.ticklen = ticklen;
260
0
    } else {
261
0
        ticklen = 0;
262
0
    }
263
264
0
    if (ticklen == 0 && s->ext.session_ticket != NULL &&
265
0
            s->ext.session_ticket->data == NULL)
266
0
        return EXT_RETURN_NOT_SENT;
267
268
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket)
269
0
            || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, ticklen)) {
270
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
271
0
                 SSL_F_TLS_CONSTRUCT_CTOS_SESSION_TICKET, ERR_R_INTERNAL_ERROR);
272
0
        return EXT_RETURN_FAIL;
273
0
    }
274
275
0
    return EXT_RETURN_SENT;
276
0
}
277
278
EXT_RETURN tls_construct_ctos_sig_algs(SSL *s, WPACKET *pkt,
279
                                       unsigned int context, X509 *x,
280
                                       size_t chainidx)
281
0
{
282
0
    size_t salglen;
283
0
    const uint16_t *salg;
284
285
0
    if (!SSL_CLIENT_USE_SIGALGS(s))
286
0
        return EXT_RETURN_NOT_SENT;
287
288
0
    salglen = tls12_get_psigalgs(s, 1, &salg);
289
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms)
290
               /* Sub-packet for sig-algs extension */
291
0
            || !WPACKET_start_sub_packet_u16(pkt)
292
               /* Sub-packet for the actual list */
293
0
            || !WPACKET_start_sub_packet_u16(pkt)
294
0
            || !tls12_copy_sigalgs(s, pkt, salg, salglen)
295
0
            || !WPACKET_close(pkt)
296
0
            || !WPACKET_close(pkt)) {
297
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_SIG_ALGS,
298
0
                 ERR_R_INTERNAL_ERROR);
299
0
        return EXT_RETURN_FAIL;
300
0
    }
301
302
0
    return EXT_RETURN_SENT;
303
0
}
304
305
#ifndef OPENSSL_NO_OCSP
306
EXT_RETURN tls_construct_ctos_status_request(SSL *s, WPACKET *pkt,
307
                                             unsigned int context, X509 *x,
308
                                             size_t chainidx)
309
0
{
310
0
    int i;
311
312
    /* This extension isn't defined for client Certificates */
313
0
    if (x != NULL)
314
0
        return EXT_RETURN_NOT_SENT;
315
316
0
    if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp)
317
0
        return EXT_RETURN_NOT_SENT;
318
319
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request)
320
               /* Sub-packet for status request extension */
321
0
            || !WPACKET_start_sub_packet_u16(pkt)
322
0
            || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp)
323
               /* Sub-packet for the ids */
324
0
            || !WPACKET_start_sub_packet_u16(pkt)) {
325
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
326
0
                 SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
327
0
        return EXT_RETURN_FAIL;
328
0
    }
329
0
    for (i = 0; i < sk_OCSP_RESPID_num(s->ext.ocsp.ids); i++) {
330
0
        unsigned char *idbytes;
331
0
        OCSP_RESPID *id = sk_OCSP_RESPID_value(s->ext.ocsp.ids, i);
332
0
        int idlen = i2d_OCSP_RESPID(id, NULL);
333
334
0
        if (idlen <= 0
335
                   /* Sub-packet for an individual id */
336
0
                || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes)
337
0
                || i2d_OCSP_RESPID(id, &idbytes) != idlen) {
338
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
339
0
                     SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
340
0
                     ERR_R_INTERNAL_ERROR);
341
0
            return EXT_RETURN_FAIL;
342
0
        }
343
0
    }
344
0
    if (!WPACKET_close(pkt)
345
0
            || !WPACKET_start_sub_packet_u16(pkt)) {
346
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
347
0
                 SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
348
0
        return EXT_RETURN_FAIL;
349
0
    }
350
0
    if (s->ext.ocsp.exts) {
351
0
        unsigned char *extbytes;
352
0
        int extlen = i2d_X509_EXTENSIONS(s->ext.ocsp.exts, NULL);
353
354
0
        if (extlen < 0) {
355
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
356
0
                     SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
357
0
                     ERR_R_INTERNAL_ERROR);
358
0
            return EXT_RETURN_FAIL;
359
0
        }
360
0
        if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes)
361
0
                || i2d_X509_EXTENSIONS(s->ext.ocsp.exts, &extbytes)
362
0
                   != extlen) {
363
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
364
0
                     SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST,
365
0
                     ERR_R_INTERNAL_ERROR);
366
0
            return EXT_RETURN_FAIL;
367
0
       }
368
0
    }
369
0
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
370
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
371
0
                 SSL_F_TLS_CONSTRUCT_CTOS_STATUS_REQUEST, ERR_R_INTERNAL_ERROR);
372
0
        return EXT_RETURN_FAIL;
373
0
    }
374
375
0
    return EXT_RETURN_SENT;
376
0
}
377
#endif
378
379
#ifndef OPENSSL_NO_NEXTPROTONEG
380
EXT_RETURN tls_construct_ctos_npn(SSL *s, WPACKET *pkt, unsigned int context,
381
                                  X509 *x, size_t chainidx)
382
0
{
383
0
    if (s->ctx->ext.npn_select_cb == NULL || !SSL_IS_FIRST_HANDSHAKE(s))
384
0
        return EXT_RETURN_NOT_SENT;
385
386
    /*
387
     * The client advertises an empty extension to indicate its support
388
     * for Next Protocol Negotiation
389
     */
390
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg)
391
0
            || !WPACKET_put_bytes_u16(pkt, 0)) {
392
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_NPN,
393
0
                 ERR_R_INTERNAL_ERROR);
394
0
        return EXT_RETURN_FAIL;
395
0
    }
396
397
0
    return EXT_RETURN_SENT;
398
0
}
399
#endif
400
401
EXT_RETURN tls_construct_ctos_alpn(SSL *s, WPACKET *pkt, unsigned int context,
402
                                   X509 *x, size_t chainidx)
403
0
{
404
0
    s->s3->alpn_sent = 0;
405
406
0
    if (s->ext.alpn == NULL || !SSL_IS_FIRST_HANDSHAKE(s))
407
0
        return EXT_RETURN_NOT_SENT;
408
409
0
    if (!WPACKET_put_bytes_u16(pkt,
410
0
                TLSEXT_TYPE_application_layer_protocol_negotiation)
411
               /* Sub-packet ALPN extension */
412
0
            || !WPACKET_start_sub_packet_u16(pkt)
413
0
            || !WPACKET_sub_memcpy_u16(pkt, s->ext.alpn, s->ext.alpn_len)
414
0
            || !WPACKET_close(pkt)) {
415
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_ALPN,
416
0
                 ERR_R_INTERNAL_ERROR);
417
0
        return EXT_RETURN_FAIL;
418
0
    }
419
0
    s->s3->alpn_sent = 1;
420
421
0
    return EXT_RETURN_SENT;
422
0
}
423
424
425
#ifndef OPENSSL_NO_SRTP
426
EXT_RETURN tls_construct_ctos_use_srtp(SSL *s, WPACKET *pkt,
427
                                       unsigned int context, X509 *x,
428
                                       size_t chainidx)
429
0
{
430
0
    STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(s);
431
0
    int i, end;
432
433
0
    if (clnt == NULL)
434
0
        return EXT_RETURN_NOT_SENT;
435
436
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp)
437
               /* Sub-packet for SRTP extension */
438
0
            || !WPACKET_start_sub_packet_u16(pkt)
439
               /* Sub-packet for the protection profile list */
440
0
            || !WPACKET_start_sub_packet_u16(pkt)) {
441
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP,
442
0
                 ERR_R_INTERNAL_ERROR);
443
0
        return EXT_RETURN_FAIL;
444
0
    }
445
446
0
    end = sk_SRTP_PROTECTION_PROFILE_num(clnt);
447
0
    for (i = 0; i < end; i++) {
448
0
        const SRTP_PROTECTION_PROFILE *prof =
449
0
            sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
450
451
0
        if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) {
452
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
453
0
                     SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP, ERR_R_INTERNAL_ERROR);
454
0
            return EXT_RETURN_FAIL;
455
0
        }
456
0
    }
457
0
    if (!WPACKET_close(pkt)
458
               /* Add an empty use_mki value */
459
0
            || !WPACKET_put_bytes_u8(pkt, 0)
460
0
            || !WPACKET_close(pkt)) {
461
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_USE_SRTP,
462
0
                 ERR_R_INTERNAL_ERROR);
463
0
        return EXT_RETURN_FAIL;
464
0
    }
465
466
0
    return EXT_RETURN_SENT;
467
0
}
468
#endif
469
470
EXT_RETURN tls_construct_ctos_etm(SSL *s, WPACKET *pkt, unsigned int context,
471
                                  X509 *x, size_t chainidx)
472
0
{
473
0
    if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
474
0
        return EXT_RETURN_NOT_SENT;
475
476
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac)
477
0
            || !WPACKET_put_bytes_u16(pkt, 0)) {
478
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_ETM,
479
0
                 ERR_R_INTERNAL_ERROR);
480
0
        return EXT_RETURN_FAIL;
481
0
    }
482
483
0
    return EXT_RETURN_SENT;
484
0
}
485
486
#ifndef OPENSSL_NO_CT
487
EXT_RETURN tls_construct_ctos_sct(SSL *s, WPACKET *pkt, unsigned int context,
488
                                  X509 *x, size_t chainidx)
489
0
{
490
0
    if (s->ct_validation_callback == NULL)
491
0
        return EXT_RETURN_NOT_SENT;
492
493
    /* Not defined for client Certificates */
494
0
    if (x != NULL)
495
0
        return EXT_RETURN_NOT_SENT;
496
497
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp)
498
0
            || !WPACKET_put_bytes_u16(pkt, 0)) {
499
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_SCT,
500
0
                 ERR_R_INTERNAL_ERROR);
501
0
        return EXT_RETURN_FAIL;
502
0
    }
503
504
0
    return EXT_RETURN_SENT;
505
0
}
506
#endif
507
508
EXT_RETURN tls_construct_ctos_ems(SSL *s, WPACKET *pkt, unsigned int context,
509
                                  X509 *x, size_t chainidx)
510
0
{
511
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret)
512
0
            || !WPACKET_put_bytes_u16(pkt, 0)) {
513
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_EMS,
514
0
                 ERR_R_INTERNAL_ERROR);
515
0
        return EXT_RETURN_FAIL;
516
0
    }
517
518
0
    return EXT_RETURN_SENT;
519
0
}
520
521
EXT_RETURN tls_construct_ctos_supported_versions(SSL *s, WPACKET *pkt,
522
                                                 unsigned int context, X509 *x,
523
                                                 size_t chainidx)
524
0
{
525
0
    int currv, min_version, max_version, reason;
526
527
0
    reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
528
0
    if (reason != 0) {
529
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
530
0
                 SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS, reason);
531
0
        return EXT_RETURN_FAIL;
532
0
    }
533
534
    /*
535
     * Don't include this if we can't negotiate TLSv1.3. We can do a straight
536
     * comparison here because we will never be called in DTLS.
537
     */
538
0
    if (max_version < TLS1_3_VERSION)
539
0
        return EXT_RETURN_NOT_SENT;
540
541
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions)
542
0
            || !WPACKET_start_sub_packet_u16(pkt)
543
0
            || !WPACKET_start_sub_packet_u8(pkt)) {
544
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
545
0
                 SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
546
0
                 ERR_R_INTERNAL_ERROR);
547
0
        return EXT_RETURN_FAIL;
548
0
    }
549
550
0
    for (currv = max_version; currv >= min_version; currv--) {
551
0
        if (!WPACKET_put_bytes_u16(pkt, currv)) {
552
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
553
0
                     SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
554
0
                     ERR_R_INTERNAL_ERROR);
555
0
            return EXT_RETURN_FAIL;
556
0
        }
557
0
    }
558
0
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
559
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
560
0
                 SSL_F_TLS_CONSTRUCT_CTOS_SUPPORTED_VERSIONS,
561
0
                 ERR_R_INTERNAL_ERROR);
562
0
        return EXT_RETURN_FAIL;
563
0
    }
564
565
0
    return EXT_RETURN_SENT;
566
0
}
567
568
/*
569
 * Construct a psk_kex_modes extension.
570
 */
571
EXT_RETURN tls_construct_ctos_psk_kex_modes(SSL *s, WPACKET *pkt,
572
                                            unsigned int context, X509 *x,
573
                                            size_t chainidx)
574
0
{
575
0
#ifndef OPENSSL_NO_TLS1_3
576
0
    int nodhe = s->options & SSL_OP_ALLOW_NO_DHE_KEX;
577
578
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk_kex_modes)
579
0
            || !WPACKET_start_sub_packet_u16(pkt)
580
0
            || !WPACKET_start_sub_packet_u8(pkt)
581
0
            || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE_DHE)
582
0
            || (nodhe && !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE))
583
0
            || !WPACKET_close(pkt)
584
0
            || !WPACKET_close(pkt)) {
585
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
586
0
                 SSL_F_TLS_CONSTRUCT_CTOS_PSK_KEX_MODES, ERR_R_INTERNAL_ERROR);
587
0
        return EXT_RETURN_FAIL;
588
0
    }
589
590
0
    s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE_DHE;
591
0
    if (nodhe)
592
0
        s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE;
593
0
#endif
594
595
0
    return EXT_RETURN_SENT;
596
0
}
597
598
#ifndef OPENSSL_NO_TLS1_3
599
static int add_key_share(SSL *s, WPACKET *pkt, unsigned int curve_id)
600
0
{
601
0
    unsigned char *encoded_point = NULL;
602
0
    EVP_PKEY *key_share_key = NULL;
603
0
    size_t encodedlen;
604
605
0
    if (s->s3->tmp.pkey != NULL) {
606
0
        if (!ossl_assert(s->hello_retry_request == SSL_HRR_PENDING)) {
607
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_ADD_KEY_SHARE,
608
0
                     ERR_R_INTERNAL_ERROR);
609
0
            return 0;
610
0
        }
611
        /*
612
         * Could happen if we got an HRR that wasn't requesting a new key_share
613
         */
614
0
        key_share_key = s->s3->tmp.pkey;
615
0
    } else {
616
0
        key_share_key = ssl_generate_pkey_group(s, curve_id);
617
0
        if (key_share_key == NULL) {
618
            /* SSLfatal() already called */
619
0
            return 0;
620
0
        }
621
0
    }
622
623
    /* Encode the public key. */
624
0
    encodedlen = EVP_PKEY_get1_tls_encodedpoint(key_share_key,
625
0
                                                &encoded_point);
626
0
    if (encodedlen == 0) {
627
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_ADD_KEY_SHARE, ERR_R_EC_LIB);
628
0
        goto err;
629
0
    }
630
631
    /* Create KeyShareEntry */
632
0
    if (!WPACKET_put_bytes_u16(pkt, curve_id)
633
0
            || !WPACKET_sub_memcpy_u16(pkt, encoded_point, encodedlen)) {
634
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_ADD_KEY_SHARE,
635
0
                 ERR_R_INTERNAL_ERROR);
636
0
        goto err;
637
0
    }
638
639
    /*
640
     * TODO(TLS1.3): When changing to send more than one key_share we're
641
     * going to need to be able to save more than one EVP_PKEY. For now
642
     * we reuse the existing tmp.pkey
643
     */
644
0
    s->s3->tmp.pkey = key_share_key;
645
0
    s->s3->group_id = curve_id;
646
0
    OPENSSL_free(encoded_point);
647
648
0
    return 1;
649
0
 err:
650
0
    if (s->s3->tmp.pkey == NULL)
651
0
        EVP_PKEY_free(key_share_key);
652
0
    OPENSSL_free(encoded_point);
653
0
    return 0;
654
0
}
655
#endif
656
657
EXT_RETURN tls_construct_ctos_key_share(SSL *s, WPACKET *pkt,
658
                                        unsigned int context, X509 *x,
659
                                        size_t chainidx)
660
0
{
661
0
#ifndef OPENSSL_NO_TLS1_3
662
0
    size_t i, num_groups = 0;
663
0
    const uint16_t *pgroups = NULL;
664
0
    uint16_t curve_id = 0;
665
666
    /* key_share extension */
667
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share)
668
               /* Extension data sub-packet */
669
0
            || !WPACKET_start_sub_packet_u16(pkt)
670
               /* KeyShare list sub-packet */
671
0
            || !WPACKET_start_sub_packet_u16(pkt)) {
672
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE,
673
0
                 ERR_R_INTERNAL_ERROR);
674
0
        return EXT_RETURN_FAIL;
675
0
    }
676
677
0
    tls1_get_supported_groups(s, &pgroups, &num_groups);
678
679
    /*
680
     * TODO(TLS1.3): Make the number of key_shares sent configurable. For
681
     * now, just send one
682
     */
683
0
    if (s->s3->group_id != 0) {
684
0
        curve_id = s->s3->group_id;
685
0
    } else {
686
0
        for (i = 0; i < num_groups; i++) {
687
688
0
            if (!tls_curve_allowed(s, pgroups[i], SSL_SECOP_CURVE_SUPPORTED))
689
0
                continue;
690
691
0
            curve_id = pgroups[i];
692
0
            break;
693
0
        }
694
0
    }
695
696
0
    if (curve_id == 0) {
697
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE,
698
0
                 SSL_R_NO_SUITABLE_KEY_SHARE);
699
0
        return EXT_RETURN_FAIL;
700
0
    }
701
702
0
    if (!add_key_share(s, pkt, curve_id)) {
703
        /* SSLfatal() already called */
704
0
        return EXT_RETURN_FAIL;
705
0
    }
706
707
0
    if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) {
708
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_KEY_SHARE,
709
0
                 ERR_R_INTERNAL_ERROR);
710
0
        return EXT_RETURN_FAIL;
711
0
    }
712
0
    return EXT_RETURN_SENT;
713
#else
714
    return EXT_RETURN_NOT_SENT;
715
#endif
716
0
}
717
718
EXT_RETURN tls_construct_ctos_cookie(SSL *s, WPACKET *pkt, unsigned int context,
719
                                     X509 *x, size_t chainidx)
720
0
{
721
0
    EXT_RETURN ret = EXT_RETURN_FAIL;
722
723
    /* Should only be set if we've had an HRR */
724
0
    if (s->ext.tls13_cookie_len == 0)
725
0
        return EXT_RETURN_NOT_SENT;
726
727
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie)
728
               /* Extension data sub-packet */
729
0
            || !WPACKET_start_sub_packet_u16(pkt)
730
0
            || !WPACKET_sub_memcpy_u16(pkt, s->ext.tls13_cookie,
731
0
                                       s->ext.tls13_cookie_len)
732
0
            || !WPACKET_close(pkt)) {
733
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_COOKIE,
734
0
                 ERR_R_INTERNAL_ERROR);
735
0
        goto end;
736
0
    }
737
738
0
    ret = EXT_RETURN_SENT;
739
0
 end:
740
0
    OPENSSL_free(s->ext.tls13_cookie);
741
0
    s->ext.tls13_cookie = NULL;
742
0
    s->ext.tls13_cookie_len = 0;
743
744
0
    return ret;
745
0
}
746
747
EXT_RETURN tls_construct_ctos_early_data(SSL *s, WPACKET *pkt,
748
                                         unsigned int context, X509 *x,
749
                                         size_t chainidx)
750
0
{
751
0
#ifndef OPENSSL_NO_PSK
752
0
    char identity[PSK_MAX_IDENTITY_LEN + 1];
753
0
#endif  /* OPENSSL_NO_PSK */
754
0
    const unsigned char *id = NULL;
755
0
    size_t idlen = 0;
756
0
    SSL_SESSION *psksess = NULL;
757
0
    SSL_SESSION *edsess = NULL;
758
0
    const EVP_MD *handmd = NULL;
759
760
0
    if (s->hello_retry_request == SSL_HRR_PENDING)
761
0
        handmd = ssl_handshake_md(s);
762
763
0
    if (s->psk_use_session_cb != NULL
764
0
            && (!s->psk_use_session_cb(s, handmd, &id, &idlen, &psksess)
765
0
                || (psksess != NULL
766
0
                    && psksess->ssl_version != TLS1_3_VERSION))) {
767
0
        SSL_SESSION_free(psksess);
768
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
769
0
                 SSL_R_BAD_PSK);
770
0
        return EXT_RETURN_FAIL;
771
0
    }
772
773
0
#ifndef OPENSSL_NO_PSK
774
0
    if (psksess == NULL && s->psk_client_callback != NULL) {
775
0
        unsigned char psk[PSK_MAX_PSK_LEN];
776
0
        size_t psklen = 0;
777
778
0
        memset(identity, 0, sizeof(identity));
779
0
        psklen = s->psk_client_callback(s, NULL, identity, sizeof(identity) - 1,
780
0
                                        psk, sizeof(psk));
781
782
0
        if (psklen > PSK_MAX_PSK_LEN) {
783
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
784
0
                     SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA, ERR_R_INTERNAL_ERROR);
785
0
            return EXT_RETURN_FAIL;
786
0
        } else if (psklen > 0) {
787
0
            const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 };
788
0
            const SSL_CIPHER *cipher;
789
790
0
            idlen = strlen(identity);
791
0
            if (idlen > PSK_MAX_IDENTITY_LEN) {
792
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
793
0
                         SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
794
0
                         ERR_R_INTERNAL_ERROR);
795
0
                return EXT_RETURN_FAIL;
796
0
            }
797
0
            id = (unsigned char *)identity;
798
799
            /*
800
             * We found a PSK using an old style callback. We don't know
801
             * the digest so we default to SHA256 as per the TLSv1.3 spec
802
             */
803
0
            cipher = SSL_CIPHER_find(s, tls13_aes128gcmsha256_id);
804
0
            if (cipher == NULL) {
805
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
806
0
                         SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
807
0
                         ERR_R_INTERNAL_ERROR);
808
0
                return EXT_RETURN_FAIL;
809
0
            }
810
811
0
            psksess = SSL_SESSION_new();
812
0
            if (psksess == NULL
813
0
                    || !SSL_SESSION_set1_master_key(psksess, psk, psklen)
814
0
                    || !SSL_SESSION_set_cipher(psksess, cipher)
815
0
                    || !SSL_SESSION_set_protocol_version(psksess, TLS1_3_VERSION)) {
816
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
817
0
                         SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
818
0
                         ERR_R_INTERNAL_ERROR);
819
0
                OPENSSL_cleanse(psk, psklen);
820
0
                return EXT_RETURN_FAIL;
821
0
            }
822
0
            OPENSSL_cleanse(psk, psklen);
823
0
        }
824
0
    }
825
0
#endif  /* OPENSSL_NO_PSK */
826
827
0
    SSL_SESSION_free(s->psksession);
828
0
    s->psksession = psksess;
829
0
    if (psksess != NULL) {
830
0
        OPENSSL_free(s->psksession_id);
831
0
        s->psksession_id = OPENSSL_memdup(id, idlen);
832
0
        if (s->psksession_id == NULL) {
833
0
            s->psksession_id_len = 0;
834
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
835
0
                     SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA, ERR_R_INTERNAL_ERROR);
836
0
            return EXT_RETURN_FAIL;
837
0
        }
838
0
        s->psksession_id_len = idlen;
839
0
    }
840
841
0
    if (s->early_data_state != SSL_EARLY_DATA_CONNECTING
842
0
            || (s->session->ext.max_early_data == 0
843
0
                && (psksess == NULL || psksess->ext.max_early_data == 0))) {
844
0
        s->max_early_data = 0;
845
0
        return EXT_RETURN_NOT_SENT;
846
0
    }
847
0
    edsess = s->session->ext.max_early_data != 0 ? s->session : psksess;
848
0
    s->max_early_data = edsess->ext.max_early_data;
849
850
0
    if (edsess->ext.hostname != NULL) {
851
0
        if (s->ext.hostname == NULL
852
0
                || (s->ext.hostname != NULL
853
0
                    && strcmp(s->ext.hostname, edsess->ext.hostname) != 0)) {
854
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
855
0
                     SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
856
0
                     SSL_R_INCONSISTENT_EARLY_DATA_SNI);
857
0
            return EXT_RETURN_FAIL;
858
0
        }
859
0
    }
860
861
0
    if ((s->ext.alpn == NULL && edsess->ext.alpn_selected != NULL)) {
862
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
863
0
                 SSL_R_INCONSISTENT_EARLY_DATA_ALPN);
864
0
        return EXT_RETURN_FAIL;
865
0
    }
866
867
    /*
868
     * Verify that we are offering an ALPN protocol consistent with the early
869
     * data.
870
     */
871
0
    if (edsess->ext.alpn_selected != NULL) {
872
0
        PACKET prots, alpnpkt;
873
0
        int found = 0;
874
875
0
        if (!PACKET_buf_init(&prots, s->ext.alpn, s->ext.alpn_len)) {
876
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
877
0
                     SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA, ERR_R_INTERNAL_ERROR);
878
0
            return EXT_RETURN_FAIL;
879
0
        }
880
0
        while (PACKET_get_length_prefixed_1(&prots, &alpnpkt)) {
881
0
            if (PACKET_equal(&alpnpkt, edsess->ext.alpn_selected,
882
0
                             edsess->ext.alpn_selected_len)) {
883
0
                found = 1;
884
0
                break;
885
0
            }
886
0
        }
887
0
        if (!found) {
888
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
889
0
                     SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
890
0
                     SSL_R_INCONSISTENT_EARLY_DATA_ALPN);
891
0
            return EXT_RETURN_FAIL;
892
0
        }
893
0
    }
894
895
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data)
896
0
            || !WPACKET_start_sub_packet_u16(pkt)
897
0
            || !WPACKET_close(pkt)) {
898
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_EARLY_DATA,
899
0
                 ERR_R_INTERNAL_ERROR);
900
0
        return EXT_RETURN_FAIL;
901
0
    }
902
903
    /*
904
     * We set this to rejected here. Later, if the server acknowledges the
905
     * extension, we set it to accepted.
906
     */
907
0
    s->ext.early_data = SSL_EARLY_DATA_REJECTED;
908
0
    s->ext.early_data_ok = 1;
909
910
0
    return EXT_RETURN_SENT;
911
0
}
912
913
0
#define F5_WORKAROUND_MIN_MSG_LEN   0xff
914
0
#define F5_WORKAROUND_MAX_MSG_LEN   0x200
915
916
/*
917
 * PSK pre binder overhead =
918
 *  2 bytes for TLSEXT_TYPE_psk
919
 *  2 bytes for extension length
920
 *  2 bytes for identities list length
921
 *  2 bytes for identity length
922
 *  4 bytes for obfuscated_ticket_age
923
 *  2 bytes for binder list length
924
 *  1 byte for binder length
925
 * The above excludes the number of bytes for the identity itself and the
926
 * subsequent binder bytes
927
 */
928
0
#define PSK_PRE_BINDER_OVERHEAD (2 + 2 + 2 + 2 + 4 + 2 + 1)
929
930
EXT_RETURN tls_construct_ctos_padding(SSL *s, WPACKET *pkt,
931
                                      unsigned int context, X509 *x,
932
                                      size_t chainidx)
933
0
{
934
0
    unsigned char *padbytes;
935
0
    size_t hlen;
936
937
0
    if ((s->options & SSL_OP_TLSEXT_PADDING) == 0)
938
0
        return EXT_RETURN_NOT_SENT;
939
940
    /*
941
     * Add padding to workaround bugs in F5 terminators. See RFC7685.
942
     * This code calculates the length of all extensions added so far but
943
     * excludes the PSK extension (because that MUST be written last). Therefore
944
     * this extension MUST always appear second to last.
945
     */
946
0
    if (!WPACKET_get_total_written(pkt, &hlen)) {
947
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PADDING,
948
0
                 ERR_R_INTERNAL_ERROR);
949
0
        return EXT_RETURN_FAIL;
950
0
    }
951
952
    /*
953
     * If we're going to send a PSK then that will be written out after this
954
     * extension, so we need to calculate how long it is going to be.
955
     */
956
0
    if (s->session->ssl_version == TLS1_3_VERSION
957
0
            && s->session->ext.ticklen != 0
958
0
            && s->session->cipher != NULL) {
959
0
        const EVP_MD *md = ssl_md(s->session->cipher->algorithm2);
960
961
0
        if (md != NULL) {
962
            /*
963
             * Add the fixed PSK overhead, the identity length and the binder
964
             * length.
965
             */
966
0
            hlen +=  PSK_PRE_BINDER_OVERHEAD + s->session->ext.ticklen
967
0
                     + EVP_MD_size(md);
968
0
        }
969
0
    }
970
971
0
    if (hlen > F5_WORKAROUND_MIN_MSG_LEN && hlen < F5_WORKAROUND_MAX_MSG_LEN) {
972
        /* Calculate the amount of padding we need to add */
973
0
        hlen = F5_WORKAROUND_MAX_MSG_LEN - hlen;
974
975
        /*
976
         * Take off the size of extension header itself (2 bytes for type and
977
         * 2 bytes for length bytes), but ensure that the extension is at least
978
         * 1 byte long so as not to have an empty extension last (WebSphere 7.x,
979
         * 8.x are intolerant of that condition)
980
         */
981
0
        if (hlen > 4)
982
0
            hlen -= 4;
983
0
        else
984
0
            hlen = 1;
985
986
0
        if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding)
987
0
                || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) {
988
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PADDING,
989
0
                     ERR_R_INTERNAL_ERROR);
990
0
            return EXT_RETURN_FAIL;
991
0
        }
992
0
        memset(padbytes, 0, hlen);
993
0
    }
994
995
0
    return EXT_RETURN_SENT;
996
0
}
997
998
/*
999
 * Construct the pre_shared_key extension
1000
 */
1001
EXT_RETURN tls_construct_ctos_psk(SSL *s, WPACKET *pkt, unsigned int context,
1002
                                  X509 *x, size_t chainidx)
1003
0
{
1004
0
#ifndef OPENSSL_NO_TLS1_3
1005
0
    uint32_t agesec, agems = 0;
1006
0
    size_t reshashsize = 0, pskhashsize = 0, binderoffset, msglen;
1007
0
    unsigned char *resbinder = NULL, *pskbinder = NULL, *msgstart = NULL;
1008
0
    const EVP_MD *handmd = NULL, *mdres = NULL, *mdpsk = NULL;
1009
0
    int dores = 0;
1010
1011
0
    s->ext.tick_identity = 0;
1012
1013
    /*
1014
     * Note: At this stage of the code we only support adding a single
1015
     * resumption PSK. If we add support for multiple PSKs then the length
1016
     * calculations in the padding extension will need to be adjusted.
1017
     */
1018
1019
    /*
1020
     * If this is an incompatible or new session then we have nothing to resume
1021
     * so don't add this extension.
1022
     */
1023
0
    if (s->session->ssl_version != TLS1_3_VERSION
1024
0
            || (s->session->ext.ticklen == 0 && s->psksession == NULL))
1025
0
        return EXT_RETURN_NOT_SENT;
1026
1027
0
    if (s->hello_retry_request == SSL_HRR_PENDING)
1028
0
        handmd = ssl_handshake_md(s);
1029
1030
0
    if (s->session->ext.ticklen != 0) {
1031
        /* Get the digest associated with the ciphersuite in the session */
1032
0
        if (s->session->cipher == NULL) {
1033
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
1034
0
                     ERR_R_INTERNAL_ERROR);
1035
0
            return EXT_RETURN_FAIL;
1036
0
        }
1037
0
        mdres = ssl_md(s->session->cipher->algorithm2);
1038
0
        if (mdres == NULL) {
1039
            /*
1040
             * Don't recognize this cipher so we can't use the session.
1041
             * Ignore it
1042
             */
1043
0
            goto dopsksess;
1044
0
        }
1045
1046
0
        if (s->hello_retry_request == SSL_HRR_PENDING && mdres != handmd) {
1047
            /*
1048
             * Selected ciphersuite hash does not match the hash for the session
1049
             * so we can't use it.
1050
             */
1051
0
            goto dopsksess;
1052
0
        }
1053
1054
        /*
1055
         * Technically the C standard just says time() returns a time_t and says
1056
         * nothing about the encoding of that type. In practice most
1057
         * implementations follow POSIX which holds it as an integral type in
1058
         * seconds since epoch. We've already made the assumption that we can do
1059
         * this in multiple places in the code, so portability shouldn't be an
1060
         * issue.
1061
         */
1062
0
        agesec = (uint32_t)(time(NULL) - s->session->time);
1063
        /*
1064
         * We calculate the age in seconds but the server may work in ms. Due to
1065
         * rounding errors we could overestimate the age by up to 1s. It is
1066
         * better to underestimate it. Otherwise, if the RTT is very short, when
1067
         * the server calculates the age reported by the client it could be
1068
         * bigger than the age calculated on the server - which should never
1069
         * happen.
1070
         */
1071
0
        if (agesec > 0)
1072
0
            agesec--;
1073
1074
0
        if (s->session->ext.tick_lifetime_hint < agesec) {
1075
            /* Ticket is too old. Ignore it. */
1076
0
            goto dopsksess;
1077
0
        }
1078
1079
        /*
1080
         * Calculate age in ms. We're just doing it to nearest second. Should be
1081
         * good enough.
1082
         */
1083
0
        agems = agesec * (uint32_t)1000;
1084
1085
0
        if (agesec != 0 && agems / (uint32_t)1000 != agesec) {
1086
            /*
1087
             * Overflow. Shouldn't happen unless this is a *really* old session.
1088
             * If so we just ignore it.
1089
             */
1090
0
            goto dopsksess;
1091
0
        }
1092
1093
        /*
1094
         * Obfuscate the age. Overflow here is fine, this addition is supposed
1095
         * to be mod 2^32.
1096
         */
1097
0
        agems += s->session->ext.tick_age_add;
1098
1099
0
        reshashsize = EVP_MD_size(mdres);
1100
0
        s->ext.tick_identity++;
1101
0
        dores = 1;
1102
0
    }
1103
1104
0
 dopsksess:
1105
0
    if (!dores && s->psksession == NULL)
1106
0
        return EXT_RETURN_NOT_SENT;
1107
1108
0
    if (s->psksession != NULL) {
1109
0
        mdpsk = ssl_md(s->psksession->cipher->algorithm2);
1110
0
        if (mdpsk == NULL) {
1111
            /*
1112
             * Don't recognize this cipher so we can't use the session.
1113
             * If this happens it's an application bug.
1114
             */
1115
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
1116
0
                     SSL_R_BAD_PSK);
1117
0
            return EXT_RETURN_FAIL;
1118
0
        }
1119
1120
0
        if (s->hello_retry_request == SSL_HRR_PENDING && mdpsk != handmd) {
1121
            /*
1122
             * Selected ciphersuite hash does not match the hash for the PSK
1123
             * session. This is an application bug.
1124
             */
1125
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
1126
0
                     SSL_R_BAD_PSK);
1127
0
            return EXT_RETURN_FAIL;
1128
0
        }
1129
1130
0
        pskhashsize = EVP_MD_size(mdpsk);
1131
0
    }
1132
1133
    /* Create the extension, but skip over the binder for now */
1134
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk)
1135
0
            || !WPACKET_start_sub_packet_u16(pkt)
1136
0
            || !WPACKET_start_sub_packet_u16(pkt)) {
1137
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
1138
0
                 ERR_R_INTERNAL_ERROR);
1139
0
        return EXT_RETURN_FAIL;
1140
0
    }
1141
1142
0
    if (dores) {
1143
0
        if (!WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick,
1144
0
                                           s->session->ext.ticklen)
1145
0
                || !WPACKET_put_bytes_u32(pkt, agems)) {
1146
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
1147
0
                     ERR_R_INTERNAL_ERROR);
1148
0
            return EXT_RETURN_FAIL;
1149
0
        }
1150
0
    }
1151
1152
0
    if (s->psksession != NULL) {
1153
0
        if (!WPACKET_sub_memcpy_u16(pkt, s->psksession_id,
1154
0
                                    s->psksession_id_len)
1155
0
                || !WPACKET_put_bytes_u32(pkt, 0)) {
1156
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
1157
0
                     ERR_R_INTERNAL_ERROR);
1158
0
            return EXT_RETURN_FAIL;
1159
0
        }
1160
0
        s->ext.tick_identity++;
1161
0
    }
1162
1163
0
    if (!WPACKET_close(pkt)
1164
0
            || !WPACKET_get_total_written(pkt, &binderoffset)
1165
0
            || !WPACKET_start_sub_packet_u16(pkt)
1166
0
            || (dores
1167
0
                && !WPACKET_sub_allocate_bytes_u8(pkt, reshashsize, &resbinder))
1168
0
            || (s->psksession != NULL
1169
0
                && !WPACKET_sub_allocate_bytes_u8(pkt, pskhashsize, &pskbinder))
1170
0
            || !WPACKET_close(pkt)
1171
0
            || !WPACKET_close(pkt)
1172
0
            || !WPACKET_get_total_written(pkt, &msglen)
1173
               /*
1174
                * We need to fill in all the sub-packet lengths now so we can
1175
                * calculate the HMAC of the message up to the binders
1176
                */
1177
0
            || !WPACKET_fill_lengths(pkt)) {
1178
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CTOS_PSK,
1179
0
                 ERR_R_INTERNAL_ERROR);
1180
0
        return EXT_RETURN_FAIL;
1181
0
    }
1182
1183
0
    msgstart = WPACKET_get_curr(pkt) - msglen;
1184
1185
0
    if (dores
1186
0
            && tls_psk_do_binder(s, mdres, msgstart, binderoffset, NULL,
1187
0
                                 resbinder, s->session, 1, 0) != 1) {
1188
        /* SSLfatal() already called */
1189
0
        return EXT_RETURN_FAIL;
1190
0
    }
1191
1192
0
    if (s->psksession != NULL
1193
0
            && tls_psk_do_binder(s, mdpsk, msgstart, binderoffset, NULL,
1194
0
                                 pskbinder, s->psksession, 1, 1) != 1) {
1195
        /* SSLfatal() already called */
1196
0
        return EXT_RETURN_FAIL;
1197
0
    }
1198
1199
0
    return EXT_RETURN_SENT;
1200
#else
1201
    return EXT_RETURN_NOT_SENT;
1202
#endif
1203
0
}
1204
1205
EXT_RETURN tls_construct_ctos_post_handshake_auth(SSL *s, WPACKET *pkt,
1206
                                                  unsigned int context,
1207
                                                  X509 *x, size_t chainidx)
1208
0
{
1209
0
#ifndef OPENSSL_NO_TLS1_3
1210
0
    if (!s->pha_enabled)
1211
0
        return EXT_RETURN_NOT_SENT;
1212
1213
    /* construct extension - 0 length, no contents */
1214
0
    if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_post_handshake_auth)
1215
0
            || !WPACKET_start_sub_packet_u16(pkt)
1216
0
            || !WPACKET_close(pkt)) {
1217
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1218
0
                 SSL_F_TLS_CONSTRUCT_CTOS_POST_HANDSHAKE_AUTH,
1219
0
                 ERR_R_INTERNAL_ERROR);
1220
0
        return EXT_RETURN_FAIL;
1221
0
    }
1222
1223
0
    s->post_handshake_auth = SSL_PHA_EXT_SENT;
1224
1225
0
    return EXT_RETURN_SENT;
1226
#else
1227
    return EXT_RETURN_NOT_SENT;
1228
#endif
1229
0
}
1230
1231
1232
/*
1233
 * Parse the server's renegotiation binding and abort if it's not right
1234
 */
1235
int tls_parse_stoc_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
1236
                               X509 *x, size_t chainidx)
1237
0
{
1238
0
    size_t expected_len = s->s3->previous_client_finished_len
1239
0
        + s->s3->previous_server_finished_len;
1240
0
    size_t ilen;
1241
0
    const unsigned char *data;
1242
1243
    /* Check for logic errors */
1244
0
    if (!ossl_assert(expected_len == 0
1245
0
                     || s->s3->previous_client_finished_len != 0)
1246
0
        || !ossl_assert(expected_len == 0
1247
0
                        || s->s3->previous_server_finished_len != 0)) {
1248
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
1249
0
                 ERR_R_INTERNAL_ERROR);
1250
0
        return 0;
1251
0
    }
1252
1253
    /* Parse the length byte */
1254
0
    if (!PACKET_get_1_len(pkt, &ilen)) {
1255
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
1256
0
                 SSL_R_RENEGOTIATION_ENCODING_ERR);
1257
0
        return 0;
1258
0
    }
1259
1260
    /* Consistency check */
1261
0
    if (PACKET_remaining(pkt) != ilen) {
1262
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
1263
0
                 SSL_R_RENEGOTIATION_ENCODING_ERR);
1264
0
        return 0;
1265
0
    }
1266
1267
    /* Check that the extension matches */
1268
0
    if (ilen != expected_len) {
1269
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
1270
0
                 SSL_R_RENEGOTIATION_MISMATCH);
1271
0
        return 0;
1272
0
    }
1273
1274
0
    if (!PACKET_get_bytes(pkt, &data, s->s3->previous_client_finished_len)
1275
0
        || memcmp(data, s->s3->previous_client_finished,
1276
0
                  s->s3->previous_client_finished_len) != 0) {
1277
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
1278
0
                 SSL_R_RENEGOTIATION_MISMATCH);
1279
0
        return 0;
1280
0
    }
1281
1282
0
    if (!PACKET_get_bytes(pkt, &data, s->s3->previous_server_finished_len)
1283
0
        || memcmp(data, s->s3->previous_server_finished,
1284
0
                  s->s3->previous_server_finished_len) != 0) {
1285
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_RENEGOTIATE,
1286
0
                 SSL_R_RENEGOTIATION_MISMATCH);
1287
0
        return 0;
1288
0
    }
1289
0
    s->s3->send_connection_binding = 1;
1290
1291
0
    return 1;
1292
0
}
1293
1294
/* Parse the server's max fragment len extension packet */
1295
int tls_parse_stoc_maxfragmentlen(SSL *s, PACKET *pkt, unsigned int context,
1296
                                  X509 *x, size_t chainidx)
1297
0
{
1298
0
    unsigned int value;
1299
1300
0
    if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) {
1301
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_MAXFRAGMENTLEN,
1302
0
                 SSL_R_BAD_EXTENSION);
1303
0
        return 0;
1304
0
    }
1305
1306
    /* |value| should contains a valid max-fragment-length code. */
1307
0
    if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) {
1308
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1309
0
                 SSL_F_TLS_PARSE_STOC_MAXFRAGMENTLEN,
1310
0
                 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
1311
0
        return 0;
1312
0
    }
1313
1314
    /* Must be the same value as client-configured one who was sent to server */
1315
    /*-
1316
     * RFC 6066: if a client receives a maximum fragment length negotiation
1317
     * response that differs from the length it requested, ...
1318
     * It must abort with SSL_AD_ILLEGAL_PARAMETER alert
1319
     */
1320
0
    if (value != s->ext.max_fragment_len_mode) {
1321
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1322
0
                 SSL_F_TLS_PARSE_STOC_MAXFRAGMENTLEN,
1323
0
                 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
1324
0
        return 0;
1325
0
    }
1326
1327
    /*
1328
     * Maximum Fragment Length Negotiation succeeded.
1329
     * The negotiated Maximum Fragment Length is binding now.
1330
     */
1331
0
    s->session->ext.max_fragment_len_mode = value;
1332
1333
0
    return 1;
1334
0
}
1335
1336
int tls_parse_stoc_server_name(SSL *s, PACKET *pkt, unsigned int context,
1337
                               X509 *x, size_t chainidx)
1338
0
{
1339
0
    if (s->ext.hostname == NULL) {
1340
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
1341
0
                 ERR_R_INTERNAL_ERROR);
1342
0
        return 0;
1343
0
    }
1344
1345
0
    if (PACKET_remaining(pkt) > 0) {
1346
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
1347
0
                 SSL_R_BAD_EXTENSION);
1348
0
        return 0;
1349
0
    }
1350
1351
0
    if (!s->hit) {
1352
0
        if (s->session->ext.hostname != NULL) {
1353
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
1354
0
                     ERR_R_INTERNAL_ERROR);
1355
0
            return 0;
1356
0
        }
1357
0
        s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
1358
0
        if (s->session->ext.hostname == NULL) {
1359
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SERVER_NAME,
1360
0
                     ERR_R_INTERNAL_ERROR);
1361
0
            return 0;
1362
0
        }
1363
0
    }
1364
1365
0
    return 1;
1366
0
}
1367
1368
#ifndef OPENSSL_NO_EC
1369
int tls_parse_stoc_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
1370
                                 X509 *x, size_t chainidx)
1371
0
{
1372
0
    size_t ecpointformats_len;
1373
0
    PACKET ecptformatlist;
1374
1375
0
    if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) {
1376
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS,
1377
0
                 SSL_R_BAD_EXTENSION);
1378
0
        return 0;
1379
0
    }
1380
0
    if (!s->hit) {
1381
0
        ecpointformats_len = PACKET_remaining(&ecptformatlist);
1382
0
        if (ecpointformats_len == 0) {
1383
0
            SSLfatal(s, SSL_AD_DECODE_ERROR,
1384
0
                     SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS, SSL_R_BAD_LENGTH);
1385
0
            return 0;
1386
0
        }
1387
1388
0
        s->ext.peer_ecpointformats_len = 0;
1389
0
        OPENSSL_free(s->ext.peer_ecpointformats);
1390
0
        s->ext.peer_ecpointformats = OPENSSL_malloc(ecpointformats_len);
1391
0
        if (s->ext.peer_ecpointformats == NULL) {
1392
0
            s->ext.peer_ecpointformats_len = 0;
1393
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1394
0
                     SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
1395
0
            return 0;
1396
0
        }
1397
1398
0
        s->ext.peer_ecpointformats_len = ecpointformats_len;
1399
1400
0
        if (!PACKET_copy_bytes(&ecptformatlist,
1401
0
                               s->ext.peer_ecpointformats,
1402
0
                               ecpointformats_len)) {
1403
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1404
0
                     SSL_F_TLS_PARSE_STOC_EC_PT_FORMATS, ERR_R_INTERNAL_ERROR);
1405
0
            return 0;
1406
0
        }
1407
0
    }
1408
1409
0
    return 1;
1410
0
}
1411
#endif
1412
1413
int tls_parse_stoc_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
1414
                                  X509 *x, size_t chainidx)
1415
0
{
1416
0
    if (s->ext.session_ticket_cb != NULL &&
1417
0
        !s->ext.session_ticket_cb(s, PACKET_data(pkt),
1418
0
                              PACKET_remaining(pkt),
1419
0
                              s->ext.session_ticket_cb_arg)) {
1420
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1421
0
                 SSL_F_TLS_PARSE_STOC_SESSION_TICKET, SSL_R_BAD_EXTENSION);
1422
0
        return 0;
1423
0
    }
1424
1425
0
    if (!tls_use_ticket(s)) {
1426
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION,
1427
0
                 SSL_F_TLS_PARSE_STOC_SESSION_TICKET, SSL_R_BAD_EXTENSION);
1428
0
        return 0;
1429
0
    }
1430
0
    if (PACKET_remaining(pkt) > 0) {
1431
0
        SSLfatal(s, SSL_AD_DECODE_ERROR,
1432
0
                 SSL_F_TLS_PARSE_STOC_SESSION_TICKET, SSL_R_BAD_EXTENSION);
1433
0
        return 0;
1434
0
    }
1435
1436
0
    s->ext.ticket_expected = 1;
1437
1438
0
    return 1;
1439
0
}
1440
1441
#ifndef OPENSSL_NO_OCSP
1442
int tls_parse_stoc_status_request(SSL *s, PACKET *pkt, unsigned int context,
1443
                                  X509 *x, size_t chainidx)
1444
0
{
1445
0
    if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
1446
        /* We ignore this if the server sends a CertificateRequest */
1447
        /* TODO(TLS1.3): Add support for this */
1448
0
        return 1;
1449
0
    }
1450
1451
    /*
1452
     * MUST only be sent if we've requested a status
1453
     * request message. In TLS <= 1.2 it must also be empty.
1454
     */
1455
0
    if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) {
1456
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION,
1457
0
                 SSL_F_TLS_PARSE_STOC_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
1458
0
        return 0;
1459
0
    }
1460
0
    if (!SSL_IS_TLS13(s) && PACKET_remaining(pkt) > 0) {
1461
0
        SSLfatal(s, SSL_AD_DECODE_ERROR,
1462
0
                 SSL_F_TLS_PARSE_STOC_STATUS_REQUEST, SSL_R_BAD_EXTENSION);
1463
0
        return 0;
1464
0
    }
1465
1466
0
    if (SSL_IS_TLS13(s)) {
1467
        /* We only know how to handle this if it's for the first Certificate in
1468
         * the chain. We ignore any other responses.
1469
         */
1470
0
        if (chainidx != 0)
1471
0
            return 1;
1472
1473
        /* SSLfatal() already called */
1474
0
        return tls_process_cert_status_body(s, pkt);
1475
0
    }
1476
1477
    /* Set flag to expect CertificateStatus message */
1478
0
    s->ext.status_expected = 1;
1479
1480
0
    return 1;
1481
0
}
1482
#endif
1483
1484
1485
#ifndef OPENSSL_NO_CT
1486
int tls_parse_stoc_sct(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1487
                       size_t chainidx)
1488
0
{
1489
0
    if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) {
1490
        /* We ignore this if the server sends it in a CertificateRequest */
1491
        /* TODO(TLS1.3): Add support for this */
1492
0
        return 1;
1493
0
    }
1494
1495
    /*
1496
     * Only take it if we asked for it - i.e if there is no CT validation
1497
     * callback set, then a custom extension MAY be processing it, so we
1498
     * need to let control continue to flow to that.
1499
     */
1500
0
    if (s->ct_validation_callback != NULL) {
1501
0
        size_t size = PACKET_remaining(pkt);
1502
1503
        /* Simply copy it off for later processing */
1504
0
        OPENSSL_free(s->ext.scts);
1505
0
        s->ext.scts = NULL;
1506
1507
0
        s->ext.scts_len = (uint16_t)size;
1508
0
        if (size > 0) {
1509
0
            s->ext.scts = OPENSSL_malloc(size);
1510
0
            if (s->ext.scts == NULL) {
1511
0
                s->ext.scts_len = 0;
1512
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SCT,
1513
0
                         ERR_R_MALLOC_FAILURE);
1514
0
                return 0;
1515
0
            }
1516
0
            if (!PACKET_copy_bytes(pkt, s->ext.scts, size)) {
1517
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_SCT,
1518
0
                         ERR_R_INTERNAL_ERROR);
1519
0
                return 0;
1520
0
            }
1521
0
        }
1522
0
    } else {
1523
0
        ENDPOINT role = (context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
1524
0
                        ? ENDPOINT_CLIENT : ENDPOINT_BOTH;
1525
1526
        /*
1527
         * If we didn't ask for it then there must be a custom extension,
1528
         * otherwise this is unsolicited.
1529
         */
1530
0
        if (custom_ext_find(&s->cert->custext, role,
1531
0
                            TLSEXT_TYPE_signed_certificate_timestamp,
1532
0
                            NULL) == NULL) {
1533
0
            SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_F_TLS_PARSE_STOC_SCT,
1534
0
                     SSL_R_BAD_EXTENSION);
1535
0
            return 0;
1536
0
        }
1537
1538
0
        if (!custom_ext_parse(s, context,
1539
0
                             TLSEXT_TYPE_signed_certificate_timestamp,
1540
0
                             PACKET_data(pkt), PACKET_remaining(pkt),
1541
0
                             x, chainidx)) {
1542
            /* SSLfatal already called */
1543
0
            return 0;
1544
0
        }
1545
0
    }
1546
1547
0
    return 1;
1548
0
}
1549
#endif
1550
1551
1552
#ifndef OPENSSL_NO_NEXTPROTONEG
1553
/*
1554
 * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1555
 * elements of zero length are allowed and the set of elements must exactly
1556
 * fill the length of the block. Returns 1 on success or 0 on failure.
1557
 */
1558
static int ssl_next_proto_validate(SSL *s, PACKET *pkt)
1559
0
{
1560
0
    PACKET tmp_protocol;
1561
1562
0
    while (PACKET_remaining(pkt)) {
1563
0
        if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol)
1564
0
            || PACKET_remaining(&tmp_protocol) == 0) {
1565
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL_NEXT_PROTO_VALIDATE,
1566
0
                     SSL_R_BAD_EXTENSION);
1567
0
            return 0;
1568
0
        }
1569
0
    }
1570
1571
0
    return 1;
1572
0
}
1573
1574
int tls_parse_stoc_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1575
                       size_t chainidx)
1576
0
{
1577
0
    unsigned char *selected;
1578
0
    unsigned char selected_len;
1579
0
    PACKET tmppkt;
1580
1581
    /* Check if we are in a renegotiation. If so ignore this extension */
1582
0
    if (!SSL_IS_FIRST_HANDSHAKE(s))
1583
0
        return 1;
1584
1585
    /* We must have requested it. */
1586
0
    if (s->ctx->ext.npn_select_cb == NULL) {
1587
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_F_TLS_PARSE_STOC_NPN,
1588
0
                 SSL_R_BAD_EXTENSION);
1589
0
        return 0;
1590
0
    }
1591
1592
    /* The data must be valid */
1593
0
    tmppkt = *pkt;
1594
0
    if (!ssl_next_proto_validate(s, &tmppkt)) {
1595
        /* SSLfatal() already called */
1596
0
        return 0;
1597
0
    }
1598
0
    if (s->ctx->ext.npn_select_cb(s, &selected, &selected_len,
1599
0
                                  PACKET_data(pkt),
1600
0
                                  PACKET_remaining(pkt),
1601
0
                                  s->ctx->ext.npn_select_cb_arg) !=
1602
0
             SSL_TLSEXT_ERR_OK) {
1603
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PARSE_STOC_NPN,
1604
0
                 SSL_R_BAD_EXTENSION);
1605
0
        return 0;
1606
0
    }
1607
1608
    /*
1609
     * Could be non-NULL if server has sent multiple NPN extensions in
1610
     * a single Serverhello
1611
     */
1612
0
    OPENSSL_free(s->ext.npn);
1613
0
    s->ext.npn = OPENSSL_malloc(selected_len);
1614
0
    if (s->ext.npn == NULL) {
1615
0
        s->ext.npn_len = 0;
1616
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_NPN,
1617
0
                 ERR_R_INTERNAL_ERROR);
1618
0
        return 0;
1619
0
    }
1620
1621
0
    memcpy(s->ext.npn, selected, selected_len);
1622
0
    s->ext.npn_len = selected_len;
1623
0
    s->s3->npn_seen = 1;
1624
1625
0
    return 1;
1626
0
}
1627
#endif
1628
1629
int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1630
                        size_t chainidx)
1631
0
{
1632
0
    size_t len;
1633
1634
    /* We must have requested it. */
1635
0
    if (!s->s3->alpn_sent) {
1636
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_F_TLS_PARSE_STOC_ALPN,
1637
0
                 SSL_R_BAD_EXTENSION);
1638
0
        return 0;
1639
0
    }
1640
    /*-
1641
     * The extension data consists of:
1642
     *   uint16 list_length
1643
     *   uint8 proto_length;
1644
     *   uint8 proto[proto_length];
1645
     */
1646
0
    if (!PACKET_get_net_2_len(pkt, &len)
1647
0
        || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len)
1648
0
        || PACKET_remaining(pkt) != len) {
1649
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
1650
0
                 SSL_R_BAD_EXTENSION);
1651
0
        return 0;
1652
0
    }
1653
0
    OPENSSL_free(s->s3->alpn_selected);
1654
0
    s->s3->alpn_selected = OPENSSL_malloc(len);
1655
0
    if (s->s3->alpn_selected == NULL) {
1656
0
        s->s3->alpn_selected_len = 0;
1657
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
1658
0
                 ERR_R_INTERNAL_ERROR);
1659
0
        return 0;
1660
0
    }
1661
0
    if (!PACKET_copy_bytes(pkt, s->s3->alpn_selected, len)) {
1662
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
1663
0
                 SSL_R_BAD_EXTENSION);
1664
0
        return 0;
1665
0
    }
1666
0
    s->s3->alpn_selected_len = len;
1667
1668
0
    if (s->session->ext.alpn_selected == NULL
1669
0
            || s->session->ext.alpn_selected_len != len
1670
0
            || memcmp(s->session->ext.alpn_selected, s->s3->alpn_selected, len)
1671
0
               != 0) {
1672
        /* ALPN not consistent with the old session so cannot use early_data */
1673
0
        s->ext.early_data_ok = 0;
1674
0
    }
1675
0
    if (!s->hit) {
1676
        /*
1677
         * This is a new session and so alpn_selected should have been
1678
         * initialised to NULL. We should update it with the selected ALPN.
1679
         */
1680
0
        if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
1681
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
1682
0
                     ERR_R_INTERNAL_ERROR);
1683
0
            return 0;
1684
0
        }
1685
0
        s->session->ext.alpn_selected =
1686
0
            OPENSSL_memdup(s->s3->alpn_selected, s->s3->alpn_selected_len);
1687
0
        if (s->session->ext.alpn_selected == NULL) {
1688
0
            s->session->ext.alpn_selected_len = 0;
1689
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_ALPN,
1690
0
                     ERR_R_INTERNAL_ERROR);
1691
0
            return 0;
1692
0
        }
1693
0
        s->session->ext.alpn_selected_len = s->s3->alpn_selected_len;
1694
0
    }
1695
1696
0
    return 1;
1697
0
}
1698
1699
#ifndef OPENSSL_NO_SRTP
1700
int tls_parse_stoc_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1701
                            size_t chainidx)
1702
0
{
1703
0
    unsigned int id, ct, mki;
1704
0
    int i;
1705
0
    STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
1706
0
    SRTP_PROTECTION_PROFILE *prof;
1707
1708
0
    if (!PACKET_get_net_2(pkt, &ct) || ct != 2
1709
0
            || !PACKET_get_net_2(pkt, &id)
1710
0
            || !PACKET_get_1(pkt, &mki)
1711
0
            || PACKET_remaining(pkt) != 0) {
1712
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_USE_SRTP,
1713
0
                 SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1714
0
        return 0;
1715
0
    }
1716
1717
0
    if (mki != 0) {
1718
        /* Must be no MKI, since we never offer one */
1719
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_USE_SRTP,
1720
0
                 SSL_R_BAD_SRTP_MKI_VALUE);
1721
0
        return 0;
1722
0
    }
1723
1724
    /* Throw an error if the server gave us an unsolicited extension */
1725
0
    clnt = SSL_get_srtp_profiles(s);
1726
0
    if (clnt == NULL) {
1727
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_USE_SRTP,
1728
0
                 SSL_R_NO_SRTP_PROFILES);
1729
0
        return 0;
1730
0
    }
1731
1732
    /*
1733
     * Check to see if the server gave us something we support (and
1734
     * presumably offered)
1735
     */
1736
0
    for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
1737
0
        prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i);
1738
1739
0
        if (prof->id == id) {
1740
0
            s->srtp_profile = prof;
1741
0
            return 1;
1742
0
        }
1743
0
    }
1744
1745
0
    SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_USE_SRTP,
1746
0
             SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1747
0
    return 0;
1748
0
}
1749
#endif
1750
1751
int tls_parse_stoc_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1752
                       size_t chainidx)
1753
0
{
1754
    /* Ignore if inappropriate ciphersuite */
1755
0
    if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)
1756
0
            && s->s3->tmp.new_cipher->algorithm_mac != SSL_AEAD
1757
0
            && s->s3->tmp.new_cipher->algorithm_enc != SSL_RC4
1758
0
            && s->s3->tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT
1759
0
            && s->s3->tmp.new_cipher->algorithm_enc != SSL_eGOST2814789CNT12)
1760
0
        s->ext.use_etm = 1;
1761
1762
0
    return 1;
1763
0
}
1764
1765
int tls_parse_stoc_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1766
                       size_t chainidx)
1767
0
{
1768
0
    s->s3->flags |= TLS1_FLAGS_RECEIVED_EXTMS;
1769
0
    if (!s->hit)
1770
0
        s->session->flags |= SSL_SESS_FLAG_EXTMS;
1771
1772
0
    return 1;
1773
0
}
1774
1775
int tls_parse_stoc_supported_versions(SSL *s, PACKET *pkt, unsigned int context,
1776
                                      X509 *x, size_t chainidx)
1777
0
{
1778
0
    unsigned int version;
1779
1780
0
    if (!PACKET_get_net_2(pkt, &version)
1781
0
            || PACKET_remaining(pkt) != 0) {
1782
0
        SSLfatal(s, SSL_AD_DECODE_ERROR,
1783
0
                 SSL_F_TLS_PARSE_STOC_SUPPORTED_VERSIONS,
1784
0
                 SSL_R_LENGTH_MISMATCH);
1785
0
        return 0;
1786
0
    }
1787
1788
    /*
1789
     * The only protocol version we support which is valid in this extension in
1790
     * a ServerHello is TLSv1.3 therefore we shouldn't be getting anything else.
1791
     */
1792
0
    if (version != TLS1_3_VERSION) {
1793
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1794
0
                 SSL_F_TLS_PARSE_STOC_SUPPORTED_VERSIONS,
1795
0
                 SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
1796
0
        return 0;
1797
0
    }
1798
1799
    /* We ignore this extension for HRRs except to sanity check it */
1800
0
    if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)
1801
0
        return 1;
1802
1803
    /* We just set it here. We validate it in ssl_choose_client_version */
1804
0
    s->version = version;
1805
1806
0
    return 1;
1807
0
}
1808
1809
int tls_parse_stoc_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1810
                             size_t chainidx)
1811
0
{
1812
0
#ifndef OPENSSL_NO_TLS1_3
1813
0
    unsigned int group_id;
1814
0
    PACKET encoded_pt;
1815
0
    EVP_PKEY *ckey = s->s3->tmp.pkey, *skey = NULL;
1816
1817
    /* Sanity check */
1818
0
    if (ckey == NULL || s->s3->peer_tmp != NULL) {
1819
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
1820
0
                 ERR_R_INTERNAL_ERROR);
1821
0
        return 0;
1822
0
    }
1823
1824
0
    if (!PACKET_get_net_2(pkt, &group_id)) {
1825
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
1826
0
                 SSL_R_LENGTH_MISMATCH);
1827
0
        return 0;
1828
0
    }
1829
1830
0
    if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) {
1831
0
        const uint16_t *pgroups = NULL;
1832
0
        size_t i, num_groups;
1833
1834
0
        if (PACKET_remaining(pkt) != 0) {
1835
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
1836
0
                     SSL_R_LENGTH_MISMATCH);
1837
0
            return 0;
1838
0
        }
1839
1840
        /*
1841
         * It is an error if the HelloRetryRequest wants a key_share that we
1842
         * already sent in the first ClientHello
1843
         */
1844
0
        if (group_id == s->s3->group_id) {
1845
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1846
0
                     SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
1847
0
            return 0;
1848
0
        }
1849
1850
        /* Validate the selected group is one we support */
1851
0
        tls1_get_supported_groups(s, &pgroups, &num_groups);
1852
0
        for (i = 0; i < num_groups; i++) {
1853
0
            if (group_id == pgroups[i])
1854
0
                break;
1855
0
        }
1856
0
        if (i >= num_groups
1857
0
                || !tls_curve_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)) {
1858
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1859
0
                     SSL_F_TLS_PARSE_STOC_KEY_SHARE, SSL_R_BAD_KEY_SHARE);
1860
0
            return 0;
1861
0
        }
1862
1863
0
        s->s3->group_id = group_id;
1864
0
        EVP_PKEY_free(s->s3->tmp.pkey);
1865
0
        s->s3->tmp.pkey = NULL;
1866
0
        return 1;
1867
0
    }
1868
1869
0
    if (group_id != s->s3->group_id) {
1870
        /*
1871
         * This isn't for the group that we sent in the original
1872
         * key_share!
1873
         */
1874
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
1875
0
                 SSL_R_BAD_KEY_SHARE);
1876
0
        return 0;
1877
0
    }
1878
1879
0
    if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt)
1880
0
            || PACKET_remaining(&encoded_pt) == 0) {
1881
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
1882
0
                 SSL_R_LENGTH_MISMATCH);
1883
0
        return 0;
1884
0
    }
1885
1886
0
    skey = EVP_PKEY_new();
1887
0
    if (skey == NULL || EVP_PKEY_copy_parameters(skey, ckey) <= 0) {
1888
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
1889
0
                 ERR_R_MALLOC_FAILURE);
1890
0
        EVP_PKEY_free(skey);
1891
0
        return 0;
1892
0
    }
1893
0
    if (!EVP_PKEY_set1_tls_encodedpoint(skey, PACKET_data(&encoded_pt),
1894
0
                                        PACKET_remaining(&encoded_pt))) {
1895
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_KEY_SHARE,
1896
0
                 SSL_R_BAD_ECPOINT);
1897
0
        EVP_PKEY_free(skey);
1898
0
        return 0;
1899
0
    }
1900
1901
0
    if (ssl_derive(s, ckey, skey, 1) == 0) {
1902
        /* SSLfatal() already called */
1903
0
        EVP_PKEY_free(skey);
1904
0
        return 0;
1905
0
    }
1906
0
    s->s3->peer_tmp = skey;
1907
0
#endif
1908
1909
0
    return 1;
1910
0
}
1911
1912
int tls_parse_stoc_cookie(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1913
                       size_t chainidx)
1914
0
{
1915
0
    PACKET cookie;
1916
1917
0
    if (!PACKET_as_length_prefixed_2(pkt, &cookie)
1918
0
            || !PACKET_memdup(&cookie, &s->ext.tls13_cookie,
1919
0
                              &s->ext.tls13_cookie_len)) {
1920
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_COOKIE,
1921
0
                 SSL_R_LENGTH_MISMATCH);
1922
0
        return 0;
1923
0
    }
1924
1925
0
    return 1;
1926
0
}
1927
1928
int tls_parse_stoc_early_data(SSL *s, PACKET *pkt, unsigned int context,
1929
                              X509 *x, size_t chainidx)
1930
0
{
1931
0
    if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) {
1932
0
        unsigned long max_early_data;
1933
1934
0
        if (!PACKET_get_net_4(pkt, &max_early_data)
1935
0
                || PACKET_remaining(pkt) != 0) {
1936
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_EARLY_DATA,
1937
0
                     SSL_R_INVALID_MAX_EARLY_DATA);
1938
0
            return 0;
1939
0
        }
1940
1941
0
        s->session->ext.max_early_data = max_early_data;
1942
1943
0
        return 1;
1944
0
    }
1945
1946
0
    if (PACKET_remaining(pkt) != 0) {
1947
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_EARLY_DATA,
1948
0
                 SSL_R_BAD_EXTENSION);
1949
0
        return 0;
1950
0
    }
1951
1952
0
    if (!s->ext.early_data_ok
1953
0
            || !s->hit) {
1954
        /*
1955
         * If we get here then we didn't send early data, or we didn't resume
1956
         * using the first identity, or the SNI/ALPN is not consistent so the
1957
         * server should not be accepting it.
1958
         */
1959
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_EARLY_DATA,
1960
0
                 SSL_R_BAD_EXTENSION);
1961
0
        return 0;
1962
0
    }
1963
1964
0
    s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
1965
1966
0
    return 1;
1967
0
}
1968
1969
int tls_parse_stoc_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
1970
                       size_t chainidx)
1971
0
{
1972
0
#ifndef OPENSSL_NO_TLS1_3
1973
0
    unsigned int identity;
1974
1975
0
    if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) {
1976
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PARSE_STOC_PSK,
1977
0
                 SSL_R_LENGTH_MISMATCH);
1978
0
        return 0;
1979
0
    }
1980
1981
0
    if (identity >= (unsigned int)s->ext.tick_identity) {
1982
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PARSE_STOC_PSK,
1983
0
                 SSL_R_BAD_PSK_IDENTITY);
1984
0
        return 0;
1985
0
    }
1986
1987
    /*
1988
     * Session resumption tickets are always sent before PSK tickets. If the
1989
     * ticket index is 0 then it must be for a session resumption ticket if we
1990
     * sent two tickets, or if we didn't send a PSK ticket.
1991
     */
1992
0
    if (identity == 0 && (s->psksession == NULL || s->ext.tick_identity == 2)) {
1993
0
        s->hit = 1;
1994
0
        SSL_SESSION_free(s->psksession);
1995
0
        s->psksession = NULL;
1996
0
        return 1;
1997
0
    }
1998
1999
0
    if (s->psksession == NULL) {
2000
        /* Should never happen */
2001
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PARSE_STOC_PSK,
2002
0
                 ERR_R_INTERNAL_ERROR);
2003
0
        return 0;
2004
0
    }
2005
2006
    /*
2007
     * If we used the external PSK for sending early_data then s->early_secret
2008
     * is already set up, so don't overwrite it. Otherwise we copy the
2009
     * early_secret across that we generated earlier.
2010
     */
2011
0
    if ((s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
2012
0
                && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
2013
0
            || s->session->ext.max_early_data > 0
2014
0
            || s->psksession->ext.max_early_data == 0)
2015
0
        memcpy(s->early_secret, s->psksession->early_secret, EVP_MAX_MD_SIZE);
2016
2017
0
    SSL_SESSION_free(s->session);
2018
0
    s->session = s->psksession;
2019
0
    s->psksession = NULL;
2020
0
    s->hit = 1;
2021
    /* Early data is only allowed if we used the first ticket */
2022
0
    if (identity != 0)
2023
0
        s->ext.early_data_ok = 0;
2024
0
#endif
2025
2026
0
    return 1;
2027
0
}