Coverage Report

Created: 2018-08-29 13:53

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