Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/ssl/statem/statem_srvr.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4
 * Copyright 2005 Nokia. All rights reserved.
5
 *
6
 * Licensed under the Apache License 2.0 (the "License").  You may not use
7
 * this file except in compliance with the License.  You can obtain a copy
8
 * in the file LICENSE in the source distribution or at
9
 * https://www.openssl.org/source/license.html
10
 */
11
12
#include <stdio.h>
13
#include "../ssl_local.h"
14
#include "statem_local.h"
15
#include "internal/constant_time.h"
16
#include "internal/cryptlib.h"
17
#include <openssl/buffer.h>
18
#include <openssl/rand.h>
19
#include <openssl/objects.h>
20
#include <openssl/evp.h>
21
#include <openssl/x509.h>
22
#include <openssl/dh.h>
23
#include <openssl/rsa.h>
24
#include <openssl/bn.h>
25
#include <openssl/md5.h>
26
#include <openssl/trace.h>
27
#include <openssl/core_names.h>
28
#include <openssl/asn1t.h>
29
30
0
#define TICKET_NONCE_SIZE 8
31
32
typedef struct {
33
    ASN1_TYPE *kxBlob;
34
    ASN1_TYPE *opaqueBlob;
35
} GOST_KX_MESSAGE;
36
37
DECLARE_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
38
39
ASN1_SEQUENCE(GOST_KX_MESSAGE) = {
40
    ASN1_SIMPLE(GOST_KX_MESSAGE, kxBlob, ASN1_ANY),
41
    ASN1_OPT(GOST_KX_MESSAGE, opaqueBlob, ASN1_ANY),
42
0
} ASN1_SEQUENCE_END(GOST_KX_MESSAGE)
43
0
44
0
IMPLEMENT_ASN1_FUNCTIONS(GOST_KX_MESSAGE)
45
0
46
0
static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt);
47
0
48
0
/*
49
0
 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
50
0
 * handshake state transitions when a TLSv1.3 server is reading messages from
51
0
 * the client. The message type that the client has sent is provided in |mt|.
52
0
 * The current state is in |s->statem.hand_state|.
53
0
 *
54
0
 * Return values are 1 for success (transition allowed) and  0 on error
55
0
 * (transition not allowed)
56
0
 */
57
0
static int ossl_statem_server13_read_transition(SSL *s, int mt)
58
99
{
59
99
    OSSL_STATEM *st = &s->statem;
60
61
    /*
62
     * Note: There is no case for TLS_ST_BEFORE because at that stage we have
63
     * not negotiated TLSv1.3 yet, so that case is handled by
64
     * ossl_statem_server_read_transition()
65
     */
66
99
    switch (st->hand_state) {
67
0
    default:
68
0
        break;
69
70
99
    case TLS_ST_EARLY_DATA:
71
99
        if (s->hello_retry_request == SSL_HRR_PENDING) {
72
97
            if (mt == SSL3_MT_CLIENT_HELLO) {
73
94
                st->hand_state = TLS_ST_SR_CLNT_HELLO;
74
94
                return 1;
75
94
            }
76
3
            break;
77
97
        } else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
78
0
            if (mt == SSL3_MT_END_OF_EARLY_DATA) {
79
0
                st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA;
80
0
                return 1;
81
0
            }
82
0
            break;
83
0
        }
84
        /* Fall through */
85
86
2
    case TLS_ST_SR_END_OF_EARLY_DATA:
87
2
    case TLS_ST_SW_FINISHED:
88
2
        if (s->s3.tmp.cert_request) {
89
0
            if (mt == SSL3_MT_CERTIFICATE) {
90
0
                st->hand_state = TLS_ST_SR_CERT;
91
0
                return 1;
92
0
            }
93
2
        } else {
94
2
            if (mt == SSL3_MT_FINISHED) {
95
1
                st->hand_state = TLS_ST_SR_FINISHED;
96
1
                return 1;
97
1
            }
98
2
        }
99
1
        break;
100
101
1
    case TLS_ST_SR_CERT:
102
0
        if (s->session->peer == NULL) {
103
0
            if (mt == SSL3_MT_FINISHED) {
104
0
                st->hand_state = TLS_ST_SR_FINISHED;
105
0
                return 1;
106
0
            }
107
0
        } else {
108
0
            if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
109
0
                st->hand_state = TLS_ST_SR_CERT_VRFY;
110
0
                return 1;
111
0
            }
112
0
        }
113
0
        break;
114
115
0
    case TLS_ST_SR_CERT_VRFY:
116
0
        if (mt == SSL3_MT_FINISHED) {
117
0
            st->hand_state = TLS_ST_SR_FINISHED;
118
0
            return 1;
119
0
        }
120
0
        break;
121
122
0
    case TLS_ST_OK:
123
        /*
124
         * Its never ok to start processing handshake messages in the middle of
125
         * early data (i.e. before we've received the end of early data alert)
126
         */
127
0
        if (s->early_data_state == SSL_EARLY_DATA_READING)
128
0
            break;
129
130
0
        if (mt == SSL3_MT_CERTIFICATE
131
0
            && s->post_handshake_auth == SSL_PHA_REQUESTED) {
132
0
            st->hand_state = TLS_ST_SR_CERT;
133
0
            return 1;
134
0
        }
135
136
0
        if (mt == SSL3_MT_KEY_UPDATE) {
137
0
            st->hand_state = TLS_ST_SR_KEY_UPDATE;
138
0
            return 1;
139
0
        }
140
0
        break;
141
99
    }
142
143
    /* No valid transition found */
144
4
    return 0;
145
99
}
146
147
/*
148
 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
149
 * handshake state transitions when the server is reading messages from the
150
 * client. The message type that the client has sent is provided in |mt|. The
151
 * current state is in |s->statem.hand_state|.
152
 *
153
 * Return values are 1 for success (transition allowed) and  0 on error
154
 * (transition not allowed)
155
 */
156
int ossl_statem_server_read_transition(SSL *s, int mt)
157
6.67k
{
158
6.67k
    OSSL_STATEM *st = &s->statem;
159
160
6.67k
    if (SSL_IS_TLS13(s)) {
161
99
        if (!ossl_statem_server13_read_transition(s, mt))
162
4
            goto err;
163
95
        return 1;
164
99
    }
165
166
6.57k
    switch (st->hand_state) {
167
0
    default:
168
0
        break;
169
170
4.46k
    case TLS_ST_BEFORE:
171
4.46k
    case TLS_ST_OK:
172
4.46k
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
173
4.46k
        if (mt == SSL3_MT_CLIENT_HELLO) {
174
4.45k
            st->hand_state = TLS_ST_SR_CLNT_HELLO;
175
4.45k
            return 1;
176
4.45k
        }
177
13
        break;
178
179
1.58k
    case TLS_ST_SW_SRVR_DONE:
180
        /*
181
         * If we get a CKE message after a ServerDone then either
182
         * 1) We didn't request a Certificate
183
         * OR
184
         * 2) If we did request one then
185
         *      a) We allow no Certificate to be returned
186
         *      AND
187
         *      b) We are running SSL3 (in TLS1.0+ the client must return a 0
188
         *         list if we requested a certificate)
189
         */
190
1.58k
        if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
191
1.53k
            if (s->s3.tmp.cert_request) {
192
0
                if (s->version == SSL3_VERSION) {
193
0
                    if ((s->verify_mode & SSL_VERIFY_PEER)
194
0
                        && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
195
                        /*
196
                         * This isn't an unexpected message as such - we're just
197
                         * not going to accept it because we require a client
198
                         * cert.
199
                         */
200
0
                        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
201
0
                            SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
202
0
                        return 0;
203
0
                    }
204
0
                    st->hand_state = TLS_ST_SR_KEY_EXCH;
205
0
                    return 1;
206
0
                }
207
1.53k
            } else {
208
1.53k
                st->hand_state = TLS_ST_SR_KEY_EXCH;
209
1.53k
                return 1;
210
1.53k
            }
211
1.53k
        } else if (s->s3.tmp.cert_request) {
212
0
            if (mt == SSL3_MT_CERTIFICATE) {
213
0
                st->hand_state = TLS_ST_SR_CERT;
214
0
                return 1;
215
0
            }
216
0
        }
217
50
        break;
218
219
50
    case TLS_ST_SR_CERT:
220
0
        if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
221
0
            st->hand_state = TLS_ST_SR_KEY_EXCH;
222
0
            return 1;
223
0
        }
224
0
        break;
225
226
486
    case TLS_ST_SR_KEY_EXCH:
227
        /*
228
         * We should only process a CertificateVerify message if we have
229
         * received a Certificate from the client. If so then |s->session->peer|
230
         * will be non NULL. In some instances a CertificateVerify message is
231
         * not required even if the peer has sent a Certificate (e.g. such as in
232
         * the case of static DH). In that case |st->no_cert_verify| should be
233
         * set.
234
         */
235
486
        if (s->session->peer == NULL || st->no_cert_verify) {
236
486
            if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
237
                /*
238
                 * For the ECDH ciphersuites when the client sends its ECDH
239
                 * pub key in a certificate, the CertificateVerify message is
240
                 * not sent. Also for GOST ciphersuites when the client uses
241
                 * its key from the certificate for key exchange.
242
                 */
243
468
                st->hand_state = TLS_ST_SR_CHANGE;
244
468
                return 1;
245
468
            }
246
486
        } else {
247
0
            if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
248
0
                st->hand_state = TLS_ST_SR_CERT_VRFY;
249
0
                return 1;
250
0
            }
251
0
        }
252
18
        break;
253
254
18
    case TLS_ST_SR_CERT_VRFY:
255
0
        if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
256
0
            st->hand_state = TLS_ST_SR_CHANGE;
257
0
            return 1;
258
0
        }
259
0
        break;
260
261
14
    case TLS_ST_SR_CHANGE:
262
14
#ifndef OPENSSL_NO_NEXTPROTONEG
263
14
        if (s->s3.npn_seen) {
264
0
            if (mt == SSL3_MT_NEXT_PROTO) {
265
0
                st->hand_state = TLS_ST_SR_NEXT_PROTO;
266
0
                return 1;
267
0
            }
268
14
        } else {
269
14
#endif
270
14
            if (mt == SSL3_MT_FINISHED) {
271
10
                st->hand_state = TLS_ST_SR_FINISHED;
272
10
                return 1;
273
10
            }
274
14
#ifndef OPENSSL_NO_NEXTPROTONEG
275
14
        }
276
4
#endif
277
4
        break;
278
279
4
#ifndef OPENSSL_NO_NEXTPROTONEG
280
4
    case TLS_ST_SR_NEXT_PROTO:
281
0
        if (mt == SSL3_MT_FINISHED) {
282
0
            st->hand_state = TLS_ST_SR_FINISHED;
283
0
            return 1;
284
0
        }
285
0
        break;
286
0
#endif
287
288
23
    case TLS_ST_SW_FINISHED:
289
23
        if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
290
16
            st->hand_state = TLS_ST_SR_CHANGE;
291
16
            return 1;
292
16
        }
293
7
        break;
294
6.57k
    }
295
296
96
err:
297
    /* No valid transition found */
298
96
    if (SSL_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
299
0
        BIO *rbio;
300
301
        /*
302
         * CCS messages don't have a message sequence number so this is probably
303
         * because of an out-of-order CCS. We'll just drop it.
304
         */
305
0
        s->init_num = 0;
306
0
        s->rwstate = SSL_READING;
307
0
        rbio = SSL_get_rbio(s);
308
0
        BIO_clear_retry_flags(rbio);
309
0
        BIO_set_retry_read(rbio);
310
0
        return 0;
311
0
    }
312
96
    SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
313
96
    return 0;
314
96
}
315
316
/*
317
 * Should we send a ServerKeyExchange message?
318
 *
319
 * Valid return values are:
320
 *   1: Yes
321
 *   0: No
322
 */
323
static int send_server_key_exchange(SSL *s)
324
26.0k
{
325
26.0k
    unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
326
327
    /*
328
     * only send a ServerKeyExchange if DH or fortezza but we have a
329
     * sign only certificate PSK: may send PSK identity hints For
330
     * ECC ciphersuites, we send a serverKeyExchange message only if
331
     * the cipher suite is either ECDH-anon or ECDHE. In other cases,
332
     * the server certificate contains the server's public key for
333
     * key exchange.
334
     */
335
26.0k
    if (alg_k & (SSL_kDHE | SSL_kECDHE)
336
    /*
337
     * PSK: send ServerKeyExchange if PSK identity hint if
338
     * provided
339
     */
340
14.0k
#ifndef OPENSSL_NO_PSK
341
        /* Only send SKE if we have identity hint for plain PSK */
342
14.0k
        || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
343
0
            && s->cert->psk_identity_hint)
344
        /* For other PSK always send SKE */
345
14.0k
        || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
346
14.0k
#endif
347
14.0k
#ifndef OPENSSL_NO_SRP
348
        /* SRP: send ServerKeyExchange */
349
14.0k
        || (alg_k & SSL_kSRP)
350
26.0k
#endif
351
26.0k
    ) {
352
12.0k
        return 1;
353
12.0k
    }
354
355
14.0k
    return 0;
356
26.0k
}
357
358
/*
359
 * Should we send a CertificateRequest message?
360
 *
361
 * Valid return values are:
362
 *   1: Yes
363
 *   0: No
364
 */
365
int send_certificate_request(SSL *s)
366
31.5k
{
367
31.5k
    if (
368
        /* don't request cert unless asked for it: */
369
31.5k
        s->verify_mode & SSL_VERIFY_PEER
370
        /*
371
         * don't request if post-handshake-only unless doing
372
         * post-handshake in TLSv1.3:
373
         */
374
0
        && (!SSL_IS_TLS13(s) || !(s->verify_mode & SSL_VERIFY_POST_HANDSHAKE)
375
0
            || s->post_handshake_auth == SSL_PHA_REQUEST_PENDING)
376
        /*
377
         * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
378
         * a second time:
379
         */
380
0
        && (s->certreqs_sent < 1 || !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
381
        /*
382
         * never request cert in anonymous ciphersuites (see
383
         * section "Certificate request" in SSL 3 drafts and in
384
         * RFC 2246):
385
         */
386
0
        && (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL)
387
            /*
388
             * ... except when the application insists on
389
             * verification (against the specs, but statem_clnt.c accepts
390
             * this for SSL 3)
391
             */
392
0
            || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
393
        /* don't request certificate for SRP auth */
394
0
        && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aSRP)
395
        /*
396
         * With normal PSK Certificates and Certificate Requests
397
         * are omitted
398
         */
399
0
        && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
400
0
        return 1;
401
0
    }
402
403
31.5k
    return 0;
404
31.5k
}
405
406
/*
407
 * ossl_statem_server13_write_transition() works out what handshake state to
408
 * move to next when a TLSv1.3 server is writing messages to be sent to the
409
 * client.
410
 */
411
static WRITE_TRAN ossl_statem_server13_write_transition(SSL *s)
412
4.53k
{
413
4.53k
    OSSL_STATEM *st = &s->statem;
414
415
    /*
416
     * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
417
     * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
418
     */
419
420
4.53k
    switch (st->hand_state) {
421
0
    default:
422
        /* Shouldn't happen */
423
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
424
0
        return WRITE_TRAN_ERROR;
425
426
0
    case TLS_ST_OK:
427
0
        if (s->key_update != SSL_KEY_UPDATE_NONE) {
428
0
            st->hand_state = TLS_ST_SW_KEY_UPDATE;
429
0
            return WRITE_TRAN_CONTINUE;
430
0
        }
431
0
        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
432
0
            st->hand_state = TLS_ST_SW_CERT_REQ;
433
0
            return WRITE_TRAN_CONTINUE;
434
0
        }
435
0
        if (s->ext.extra_tickets_expected > 0) {
436
0
            st->hand_state = TLS_ST_SW_SESSION_TICKET;
437
0
            return WRITE_TRAN_CONTINUE;
438
0
        }
439
        /* Try to read from the client instead */
440
0
        return WRITE_TRAN_FINISHED;
441
442
650
    case TLS_ST_SR_CLNT_HELLO:
443
650
        st->hand_state = TLS_ST_SW_SRVR_HELLO;
444
650
        return WRITE_TRAN_CONTINUE;
445
446
645
    case TLS_ST_SW_SRVR_HELLO:
447
645
        if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
448
645
            && s->hello_retry_request != SSL_HRR_COMPLETE)
449
614
            st->hand_state = TLS_ST_SW_CHANGE;
450
31
        else if (s->hello_retry_request == SSL_HRR_PENDING)
451
0
            st->hand_state = TLS_ST_EARLY_DATA;
452
31
        else
453
31
            st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
454
645
        return WRITE_TRAN_CONTINUE;
455
456
614
    case TLS_ST_SW_CHANGE:
457
614
        if (s->hello_retry_request == SSL_HRR_PENDING)
458
150
            st->hand_state = TLS_ST_EARLY_DATA;
459
464
        else
460
464
            st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
461
614
        return WRITE_TRAN_CONTINUE;
462
463
495
    case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
464
495
        if (s->hit)
465
0
            st->hand_state = TLS_ST_SW_FINISHED;
466
495
        else if (send_certificate_request(s))
467
0
            st->hand_state = TLS_ST_SW_CERT_REQ;
468
495
        else
469
495
            st->hand_state = TLS_ST_SW_CERT;
470
471
495
        return WRITE_TRAN_CONTINUE;
472
473
0
    case TLS_ST_SW_CERT_REQ:
474
0
        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
475
0
            s->post_handshake_auth = SSL_PHA_REQUESTED;
476
0
            st->hand_state = TLS_ST_OK;
477
0
        } else {
478
0
            st->hand_state = TLS_ST_SW_CERT;
479
0
        }
480
0
        return WRITE_TRAN_CONTINUE;
481
482
495
    case TLS_ST_SW_CERT:
483
495
        st->hand_state = TLS_ST_SW_CERT_VRFY;
484
495
        return WRITE_TRAN_CONTINUE;
485
486
495
    case TLS_ST_SW_CERT_VRFY:
487
495
        st->hand_state = TLS_ST_SW_FINISHED;
488
495
        return WRITE_TRAN_CONTINUE;
489
490
495
    case TLS_ST_SW_FINISHED:
491
495
        st->hand_state = TLS_ST_EARLY_DATA;
492
495
        return WRITE_TRAN_CONTINUE;
493
494
645
    case TLS_ST_EARLY_DATA:
495
645
        return WRITE_TRAN_FINISHED;
496
497
0
    case TLS_ST_SR_FINISHED:
498
        /*
499
         * Technically we have finished the handshake at this point, but we're
500
         * going to remain "in_init" for now and write out any session tickets
501
         * immediately.
502
         */
503
0
        if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
504
0
            s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
505
0
        } else if (!s->ext.ticket_expected) {
506
            /*
507
             * If we're not going to renew the ticket then we just finish the
508
             * handshake at this point.
509
             */
510
0
            st->hand_state = TLS_ST_OK;
511
0
            return WRITE_TRAN_CONTINUE;
512
0
        }
513
0
        if (s->num_tickets > s->sent_tickets)
514
0
            st->hand_state = TLS_ST_SW_SESSION_TICKET;
515
0
        else
516
0
            st->hand_state = TLS_ST_OK;
517
0
        return WRITE_TRAN_CONTINUE;
518
519
0
    case TLS_ST_SR_KEY_UPDATE:
520
0
    case TLS_ST_SW_KEY_UPDATE:
521
0
        st->hand_state = TLS_ST_OK;
522
0
        return WRITE_TRAN_CONTINUE;
523
524
0
    case TLS_ST_SW_SESSION_TICKET:
525
        /* In a resumption we only ever send a maximum of one new ticket.
526
         * Following an initial handshake we send the number of tickets we have
527
         * been configured for.
528
         */
529
0
        if (!SSL_IS_FIRST_HANDSHAKE(s) && s->ext.extra_tickets_expected > 0) {
530
0
            return WRITE_TRAN_CONTINUE;
531
0
        } else if (s->hit || s->num_tickets <= s->sent_tickets) {
532
            /* We've written enough tickets out. */
533
0
            st->hand_state = TLS_ST_OK;
534
0
        }
535
0
        return WRITE_TRAN_CONTINUE;
536
4.53k
    }
537
4.53k
}
538
539
/*
540
 * ossl_statem_server_write_transition() works out what handshake state to move
541
 * to next when the server is writing messages to be sent to the client.
542
 */
543
WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
544
254k
{
545
254k
    OSSL_STATEM *st = &s->statem;
546
547
    /*
548
     * Note that before the ClientHello we don't know what version we are going
549
     * to negotiate yet, so we don't take this branch until later
550
     */
551
552
254k
    if (SSL_IS_TLS13(s))
553
26.6k
        return ossl_statem_server13_write_transition(s);
554
555
227k
    switch (st->hand_state) {
556
0
    default:
557
        /* Shouldn't happen */
558
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
559
0
        return WRITE_TRAN_ERROR;
560
561
28.8k
    case TLS_ST_OK:
562
28.8k
        if (st->request_state == TLS_ST_SW_HELLO_REQ) {
563
            /* We must be trying to renegotiate */
564
0
            st->hand_state = TLS_ST_SW_HELLO_REQ;
565
0
            st->request_state = TLS_ST_BEFORE;
566
0
            return WRITE_TRAN_CONTINUE;
567
0
        }
568
        /* Must be an incoming ClientHello */
569
28.8k
        if (!tls_setup_handshake(s)) {
570
            /* SSLfatal() already called */
571
0
            return WRITE_TRAN_ERROR;
572
0
        }
573
        /* Fall through */
574
575
80.2k
    case TLS_ST_BEFORE:
576
        /* Just go straight to trying to read from the client */
577
80.2k
        return WRITE_TRAN_FINISHED;
578
579
0
    case TLS_ST_SW_HELLO_REQ:
580
0
        st->hand_state = TLS_ST_OK;
581
0
        return WRITE_TRAN_CONTINUE;
582
583
54.6k
    case TLS_ST_SR_CLNT_HELLO:
584
54.6k
        if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
585
12.8k
            && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) {
586
0
            st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
587
54.6k
        } else if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
588
            /* We must have rejected the renegotiation */
589
28.3k
            st->hand_state = TLS_ST_OK;
590
28.3k
            return WRITE_TRAN_CONTINUE;
591
28.3k
        } else {
592
26.2k
            st->hand_state = TLS_ST_SW_SRVR_HELLO;
593
26.2k
        }
594
26.2k
        return WRITE_TRAN_CONTINUE;
595
596
0
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
597
0
        return WRITE_TRAN_FINISHED;
598
599
26.2k
    case TLS_ST_SW_SRVR_HELLO:
600
26.2k
        if (s->hit) {
601
189
            if (s->ext.ticket_expected)
602
0
                st->hand_state = TLS_ST_SW_SESSION_TICKET;
603
189
            else
604
189
                st->hand_state = TLS_ST_SW_CHANGE;
605
26.0k
        } else {
606
            /* Check if it is anon DH or anon ECDH, */
607
            /* normal PSK or SRP */
608
26.0k
            if (!(s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
609
23.7k
                st->hand_state = TLS_ST_SW_CERT;
610
23.7k
            } else if (send_server_key_exchange(s)) {
611
2.27k
                st->hand_state = TLS_ST_SW_KEY_EXCH;
612
2.27k
            } else if (send_certificate_request(s)) {
613
0
                st->hand_state = TLS_ST_SW_CERT_REQ;
614
0
            } else {
615
0
                st->hand_state = TLS_ST_SW_SRVR_DONE;
616
0
            }
617
26.0k
        }
618
26.2k
        return WRITE_TRAN_CONTINUE;
619
620
23.7k
    case TLS_ST_SW_CERT:
621
23.7k
        if (s->ext.status_expected) {
622
0
            st->hand_state = TLS_ST_SW_CERT_STATUS;
623
0
            return WRITE_TRAN_CONTINUE;
624
0
        }
625
        /* Fall through */
626
627
23.7k
    case TLS_ST_SW_CERT_STATUS:
628
23.7k
        if (send_server_key_exchange(s)) {
629
9.73k
            st->hand_state = TLS_ST_SW_KEY_EXCH;
630
9.73k
            return WRITE_TRAN_CONTINUE;
631
9.73k
        }
632
        /* Fall through */
633
634
26.0k
    case TLS_ST_SW_KEY_EXCH:
635
26.0k
        if (send_certificate_request(s)) {
636
0
            st->hand_state = TLS_ST_SW_CERT_REQ;
637
0
            return WRITE_TRAN_CONTINUE;
638
0
        }
639
        /* Fall through */
640
641
26.0k
    case TLS_ST_SW_CERT_REQ:
642
26.0k
        st->hand_state = TLS_ST_SW_SRVR_DONE;
643
26.0k
        return WRITE_TRAN_CONTINUE;
644
645
26.0k
    case TLS_ST_SW_SRVR_DONE:
646
26.0k
        return WRITE_TRAN_FINISHED;
647
648
1.49k
    case TLS_ST_SR_FINISHED:
649
1.49k
        if (s->hit) {
650
0
            st->hand_state = TLS_ST_OK;
651
0
            return WRITE_TRAN_CONTINUE;
652
1.49k
        } else if (s->ext.ticket_expected) {
653
37
            st->hand_state = TLS_ST_SW_SESSION_TICKET;
654
1.46k
        } else {
655
1.46k
            st->hand_state = TLS_ST_SW_CHANGE;
656
1.46k
        }
657
1.49k
        return WRITE_TRAN_CONTINUE;
658
659
37
    case TLS_ST_SW_SESSION_TICKET:
660
37
        st->hand_state = TLS_ST_SW_CHANGE;
661
37
        return WRITE_TRAN_CONTINUE;
662
663
1.68k
    case TLS_ST_SW_CHANGE:
664
1.68k
        st->hand_state = TLS_ST_SW_FINISHED;
665
1.68k
        return WRITE_TRAN_CONTINUE;
666
667
1.68k
    case TLS_ST_SW_FINISHED:
668
1.68k
        if (s->hit) {
669
189
            return WRITE_TRAN_FINISHED;
670
189
        }
671
1.49k
        st->hand_state = TLS_ST_OK;
672
1.49k
        return WRITE_TRAN_CONTINUE;
673
227k
    }
674
227k
}
675
676
/*
677
 * Perform any pre work that needs to be done prior to sending a message from
678
 * the server to the client.
679
 */
680
WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
681
53.7k
{
682
53.7k
    OSSL_STATEM *st = &s->statem;
683
684
53.7k
    switch (st->hand_state) {
685
22.7k
    default:
686
        /* No pre work to be done */
687
22.7k
        break;
688
689
22.7k
    case TLS_ST_SW_HELLO_REQ:
690
0
        s->shutdown = 0;
691
0
        if (SSL_IS_DTLS(s))
692
0
            dtls1_clear_sent_buffer(s);
693
0
        break;
694
695
0
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
696
0
        s->shutdown = 0;
697
0
        if (SSL_IS_DTLS(s)) {
698
0
            dtls1_clear_sent_buffer(s);
699
            /* We don't buffer this message so don't use the timer */
700
0
            st->use_timer = 0;
701
0
        }
702
0
        break;
703
704
13.8k
    case TLS_ST_SW_SRVR_HELLO:
705
13.8k
        if (SSL_IS_DTLS(s)) {
706
            /*
707
             * Messages we write from now on should be buffered and
708
             * retransmitted if necessary, so we need to use the timer now
709
             */
710
5.60k
            st->use_timer = 1;
711
5.60k
        }
712
13.8k
        break;
713
714
11.9k
    case TLS_ST_SW_SRVR_DONE:
715
#ifndef OPENSSL_NO_SCTP
716
        if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
717
            /* Calls SSLfatal() as required */
718
            return dtls_wait_for_dry(s);
719
        }
720
#endif
721
11.9k
        return WORK_FINISHED_CONTINUE;
722
723
18
    case TLS_ST_SW_SESSION_TICKET:
724
18
        if (SSL_IS_TLS13(s) && s->sent_tickets == 0
725
0
            && s->ext.extra_tickets_expected == 0) {
726
            /*
727
             * Actually this is the end of the handshake, but we're going
728
             * straight into writing the session ticket out. So we finish off
729
             * the handshake, but keep the various buffers active.
730
             *
731
             * Calls SSLfatal as required.
732
             */
733
0
            return tls_finish_handshake(s, wst, 0, 0);
734
0
        }
735
18
        if (SSL_IS_DTLS(s)) {
736
            /*
737
             * We're into the last flight. We don't retransmit the last flight
738
             * unless we need to, so we don't use the timer
739
             */
740
0
            st->use_timer = 0;
741
0
        }
742
18
        break;
743
744
2.38k
    case TLS_ST_SW_CHANGE:
745
2.38k
        if (SSL_IS_TLS13(s))
746
1.72k
            break;
747
        /* Writes to s->session are only safe for initial handshakes */
748
662
        if (s->session->cipher == NULL) {
749
0
            s->session->cipher = s->s3.tmp.new_cipher;
750
662
        } else if (s->session->cipher != s->s3.tmp.new_cipher) {
751
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
752
0
            return WORK_ERROR;
753
0
        }
754
662
        if (!s->method->ssl3_enc->setup_key_block(s)) {
755
            /* SSLfatal() already called */
756
0
            return WORK_ERROR;
757
0
        }
758
662
        if (SSL_IS_DTLS(s)) {
759
            /*
760
             * We're into the last flight. We don't retransmit the last flight
761
             * unless we need to, so we don't use the timer. This might have
762
             * already been set to 0 if we sent a NewSessionTicket message,
763
             * but we'll set it again here in case we didn't.
764
             */
765
0
            st->use_timer = 0;
766
0
        }
767
662
        return WORK_FINISHED_CONTINUE;
768
769
1.76k
    case TLS_ST_EARLY_DATA:
770
1.76k
        if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING
771
1.05k
            && (s->s3.flags & TLS1_FLAGS_STATELESS) == 0)
772
1.05k
            return WORK_FINISHED_CONTINUE;
773
        /* Fall through */
774
775
1.74k
    case TLS_ST_OK:
776
        /* Calls SSLfatal() as required */
777
1.74k
        return tls_finish_handshake(s, wst, 1, 1);
778
53.7k
    }
779
780
38.3k
    return WORK_FINISHED_CONTINUE;
781
53.7k
}
782
783
static ossl_inline int conn_is_closed(void)
784
0
{
785
0
    switch (get_last_sys_error()) {
786
0
#if defined(EPIPE)
787
0
    case EPIPE:
788
0
        return 1;
789
0
#endif
790
0
#if defined(ECONNRESET)
791
0
    case ECONNRESET:
792
0
        return 1;
793
0
#endif
794
#if defined(WSAECONNRESET)
795
    case WSAECONNRESET:
796
        return 1;
797
#endif
798
0
    default:
799
0
        return 0;
800
0
    }
801
0
}
802
803
/*
804
 * Perform any work that needs to be done after sending a message from the
805
 * server to the client.
806
 */
807
WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
808
10.5k
{
809
10.5k
    OSSL_STATEM *st = &s->statem;
810
811
10.5k
    s->init_num = 0;
812
813
10.5k
    switch (st->hand_state) {
814
4.72k
    default:
815
        /* No post work to be done */
816
4.72k
        break;
817
818
4.72k
    case TLS_ST_SW_HELLO_REQ:
819
0
        if (statem_flush(s) != 1)
820
0
            return WORK_MORE_A;
821
0
        if (!ssl3_init_finished_mac(s)) {
822
            /* SSLfatal() already called */
823
0
            return WORK_ERROR;
824
0
        }
825
0
        break;
826
827
0
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
828
0
        if (statem_flush(s) != 1)
829
0
            return WORK_MORE_A;
830
        /* HelloVerifyRequest resets Finished MAC */
831
0
        if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
832
            /* SSLfatal() already called */
833
0
            return WORK_ERROR;
834
0
        }
835
        /*
836
         * The next message should be another ClientHello which we need to
837
         * treat like it was the first packet
838
         */
839
0
        s->first_packet = 1;
840
0
        break;
841
842
2.65k
    case TLS_ST_SW_SRVR_HELLO:
843
2.65k
        if (SSL_IS_TLS13(s) && s->hello_retry_request == SSL_HRR_PENDING) {
844
150
            if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0
845
0
                && statem_flush(s) != 1)
846
0
                return WORK_MORE_A;
847
150
            break;
848
150
        }
849
#ifndef OPENSSL_NO_SCTP
850
        if (SSL_IS_DTLS(s) && s->hit) {
851
            unsigned char sctpauthkey[64];
852
            char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
853
            size_t labellen;
854
855
            /*
856
             * Add new shared key for SCTP-Auth, will be ignored if no
857
             * SCTP used.
858
             */
859
            memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
860
                sizeof(DTLS1_SCTP_AUTH_LABEL));
861
862
            /* Don't include the terminating zero. */
863
            labellen = sizeof(labelbuffer) - 1;
864
            if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
865
                labellen += 1;
866
867
            if (SSL_export_keying_material(s, sctpauthkey,
868
                    sizeof(sctpauthkey), labelbuffer,
869
                    labellen, NULL, 0,
870
                    0)
871
                <= 0) {
872
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
873
                return WORK_ERROR;
874
            }
875
876
            BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
877
                sizeof(sctpauthkey), sctpauthkey);
878
        }
879
#endif
880
2.50k
        if (!SSL_IS_TLS13(s)
881
495
            || ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
882
495
                && s->hello_retry_request != SSL_HRR_COMPLETE))
883
2.46k
            break;
884
        /* Fall through */
885
886
681
    case TLS_ST_SW_CHANGE:
887
681
        if (s->hello_retry_request == SSL_HRR_PENDING) {
888
150
            if (!statem_flush(s))
889
0
                return WORK_MORE_A;
890
150
            break;
891
150
        }
892
893
531
        if (SSL_IS_TLS13(s)) {
894
495
            if (!s->method->ssl3_enc->setup_key_block(s)
895
495
                || !s->method->ssl3_enc->change_cipher_state(s,
896
495
                    SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
897
                /* SSLfatal() already called */
898
0
                return WORK_ERROR;
899
0
            }
900
901
495
            if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED
902
495
                && !s->method->ssl3_enc->change_cipher_state(s,
903
495
                    SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
904
                /* SSLfatal() already called */
905
0
                return WORK_ERROR;
906
0
            }
907
            /*
908
             * We don't yet know whether the next record we are going to receive
909
             * is an unencrypted alert, an encrypted alert, or an encrypted
910
             * handshake message. We temporarily tolerate unencrypted alerts.
911
             */
912
495
            s->statem.enc_read_state = ENC_READ_STATE_ALLOW_PLAIN_ALERTS;
913
495
            break;
914
495
        }
915
916
#ifndef OPENSSL_NO_SCTP
917
        if (SSL_IS_DTLS(s) && !s->hit) {
918
            /*
919
             * Change to new shared key of SCTP-Auth, will be ignored if
920
             * no SCTP used.
921
             */
922
            BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
923
                0, NULL);
924
        }
925
#endif
926
36
        if (!s->method->ssl3_enc->change_cipher_state(s,
927
36
                SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
928
            /* SSLfatal() already called */
929
0
            return WORK_ERROR;
930
0
        }
931
932
36
        if (SSL_IS_DTLS(s))
933
0
            dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
934
36
        break;
935
936
1.96k
    case TLS_ST_SW_SRVR_DONE:
937
1.96k
        if (statem_flush(s) != 1)
938
0
            return WORK_MORE_A;
939
1.96k
        break;
940
941
1.96k
    case TLS_ST_SW_FINISHED:
942
531
        if (statem_flush(s) != 1)
943
0
            return WORK_MORE_A;
944
#ifndef OPENSSL_NO_SCTP
945
        if (SSL_IS_DTLS(s) && s->hit) {
946
            /*
947
             * Change to new shared key of SCTP-Auth, will be ignored if
948
             * no SCTP used.
949
             */
950
            BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
951
                0, NULL);
952
        }
953
#endif
954
531
        if (SSL_IS_TLS13(s)) {
955
            /* TLS 1.3 gets the secret size from the handshake md */
956
495
            size_t dummy;
957
495
            if (!s->method->ssl3_enc->generate_master_secret(s,
958
495
                    s->master_secret, s->handshake_secret, 0,
959
495
                    &dummy)
960
495
                || !s->method->ssl3_enc->change_cipher_state(s,
961
495
                    SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
962
                /* SSLfatal() already called */
963
0
                return WORK_ERROR;
964
495
        }
965
531
        break;
966
967
531
    case TLS_ST_SW_CERT_REQ:
968
0
        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
969
0
            if (statem_flush(s) != 1)
970
0
                return WORK_MORE_A;
971
0
        }
972
0
        break;
973
974
0
    case TLS_ST_SW_KEY_UPDATE:
975
0
        if (statem_flush(s) != 1)
976
0
            return WORK_MORE_A;
977
0
        if (!tls13_update_key(s, 1)) {
978
            /* SSLfatal() already called */
979
0
            return WORK_ERROR;
980
0
        }
981
0
        break;
982
983
0
    case TLS_ST_SW_SESSION_TICKET:
984
0
        clear_sys_error();
985
0
        if (SSL_IS_TLS13(s) && statem_flush(s) != 1) {
986
0
            if (SSL_get_error(s, 0) == SSL_ERROR_SYSCALL
987
0
                && conn_is_closed()) {
988
                /*
989
                 * We ignore connection closed errors in TLSv1.3 when sending a
990
                 * NewSessionTicket and behave as if we were successful. This is
991
                 * so that we are still able to read data sent to us by a client
992
                 * that closes soon after the end of the handshake without
993
                 * waiting to read our post-handshake NewSessionTickets.
994
                 */
995
0
                s->rwstate = SSL_NOTHING;
996
0
                break;
997
0
            }
998
999
0
            return WORK_MORE_A;
1000
0
        }
1001
0
        break;
1002
10.5k
    }
1003
1004
10.5k
    return WORK_FINISHED_CONTINUE;
1005
10.5k
}
1006
1007
/*
1008
 * Get the message construction function and message type for sending from the
1009
 * server
1010
 *
1011
 * Valid return values are:
1012
 *   1: Success
1013
 *   0: Error
1014
 */
1015
int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
1016
    confunc_f *confunc, int *mt)
1017
113k
{
1018
113k
    OSSL_STATEM *st = &s->statem;
1019
1020
113k
    switch (st->hand_state) {
1021
0
    default:
1022
        /* Shouldn't happen */
1023
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
1024
0
        return 0;
1025
1026
5.28k
    case TLS_ST_SW_CHANGE:
1027
5.28k
        if (SSL_IS_DTLS(s))
1028
0
            *confunc = dtls_construct_change_cipher_spec;
1029
5.28k
        else
1030
5.28k
            *confunc = tls_construct_change_cipher_spec;
1031
5.28k
        *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
1032
5.28k
        break;
1033
1034
0
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
1035
0
        *confunc = dtls_construct_hello_verify_request;
1036
0
        *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
1037
0
        break;
1038
1039
0
    case TLS_ST_SW_HELLO_REQ:
1040
        /* No construction function needed */
1041
0
        *confunc = NULL;
1042
0
        *mt = SSL3_MT_HELLO_REQUEST;
1043
0
        break;
1044
1045
29.9k
    case TLS_ST_SW_SRVR_HELLO:
1046
29.9k
        *confunc = tls_construct_server_hello;
1047
29.9k
        *mt = SSL3_MT_SERVER_HELLO;
1048
29.9k
        break;
1049
1050
26.7k
    case TLS_ST_SW_CERT:
1051
26.7k
        *confunc = tls_construct_server_certificate;
1052
26.7k
        *mt = SSL3_MT_CERTIFICATE;
1053
26.7k
        break;
1054
1055
2.99k
    case TLS_ST_SW_CERT_VRFY:
1056
2.99k
        *confunc = tls_construct_cert_verify;
1057
2.99k
        *mt = SSL3_MT_CERTIFICATE_VERIFY;
1058
2.99k
        break;
1059
1060
12.0k
    case TLS_ST_SW_KEY_EXCH:
1061
12.0k
        *confunc = tls_construct_server_key_exchange;
1062
12.0k
        *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
1063
12.0k
        break;
1064
1065
0
    case TLS_ST_SW_CERT_REQ:
1066
0
        *confunc = tls_construct_certificate_request;
1067
0
        *mt = SSL3_MT_CERTIFICATE_REQUEST;
1068
0
        break;
1069
1070
26.0k
    case TLS_ST_SW_SRVR_DONE:
1071
26.0k
        *confunc = tls_construct_server_done;
1072
26.0k
        *mt = SSL3_MT_SERVER_DONE;
1073
26.0k
        break;
1074
1075
37
    case TLS_ST_SW_SESSION_TICKET:
1076
37
        *confunc = tls_construct_new_session_ticket;
1077
37
        *mt = SSL3_MT_NEWSESSION_TICKET;
1078
37
        break;
1079
1080
0
    case TLS_ST_SW_CERT_STATUS:
1081
0
        *confunc = tls_construct_cert_status;
1082
0
        *mt = SSL3_MT_CERTIFICATE_STATUS;
1083
0
        break;
1084
1085
4.67k
    case TLS_ST_SW_FINISHED:
1086
4.67k
        *confunc = tls_construct_finished;
1087
4.67k
        *mt = SSL3_MT_FINISHED;
1088
4.67k
        break;
1089
1090
2.37k
    case TLS_ST_EARLY_DATA:
1091
2.37k
        *confunc = NULL;
1092
2.37k
        *mt = SSL3_MT_DUMMY;
1093
2.37k
        break;
1094
1095
2.99k
    case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
1096
2.99k
        *confunc = tls_construct_encrypted_extensions;
1097
2.99k
        *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
1098
2.99k
        break;
1099
1100
0
    case TLS_ST_SW_KEY_UPDATE:
1101
0
        *confunc = tls_construct_key_update;
1102
0
        *mt = SSL3_MT_KEY_UPDATE;
1103
0
        break;
1104
113k
    }
1105
1106
113k
    return 1;
1107
113k
}
1108
1109
/*
1110
 * Maximum size (excluding the Handshake header) of a ClientHello message,
1111
 * calculated as follows:
1112
 *
1113
 *  2 + # client_version
1114
 *  32 + # only valid length for random
1115
 *  1 + # length of session_id
1116
 *  32 + # maximum size for session_id
1117
 *  2 + # length of cipher suites
1118
 *  2^16-2 + # maximum length of cipher suites array
1119
 *  1 + # length of compression_methods
1120
 *  2^8-1 + # maximum length of compression methods
1121
 *  2 + # length of extensions
1122
 *  2^16-1 # maximum length of extensions
1123
 */
1124
4.55k
#define CLIENT_HELLO_MAX_LENGTH 131396
1125
1126
1.53k
#define CLIENT_KEY_EXCH_MAX_LENGTH 2048
1127
0
#define NEXT_PROTO_MAX_LENGTH 514
1128
1129
/*
1130
 * Returns the maximum allowed length for the current message that we are
1131
 * reading. Excludes the message header.
1132
 */
1133
size_t ossl_statem_server_max_message_size(SSL *s)
1134
6.57k
{
1135
6.57k
    OSSL_STATEM *st = &s->statem;
1136
1137
6.57k
    switch (st->hand_state) {
1138
0
    default:
1139
        /* Shouldn't happen */
1140
0
        return 0;
1141
1142
4.55k
    case TLS_ST_SR_CLNT_HELLO:
1143
4.55k
        return CLIENT_HELLO_MAX_LENGTH;
1144
1145
0
    case TLS_ST_SR_END_OF_EARLY_DATA:
1146
0
        return END_OF_EARLY_DATA_MAX_LENGTH;
1147
1148
0
    case TLS_ST_SR_CERT:
1149
0
        return s->max_cert_list;
1150
1151
1.53k
    case TLS_ST_SR_KEY_EXCH:
1152
1.53k
        return CLIENT_KEY_EXCH_MAX_LENGTH;
1153
1154
0
    case TLS_ST_SR_CERT_VRFY:
1155
0
        return SSL3_RT_MAX_PLAIN_LENGTH;
1156
1157
0
#ifndef OPENSSL_NO_NEXTPROTONEG
1158
0
    case TLS_ST_SR_NEXT_PROTO:
1159
0
        return NEXT_PROTO_MAX_LENGTH;
1160
0
#endif
1161
1162
484
    case TLS_ST_SR_CHANGE:
1163
484
        return CCS_MAX_LENGTH;
1164
1165
11
    case TLS_ST_SR_FINISHED:
1166
11
        return FINISHED_MAX_LENGTH;
1167
1168
0
    case TLS_ST_SR_KEY_UPDATE:
1169
0
        return KEY_UPDATE_MAX_LENGTH;
1170
6.57k
    }
1171
6.57k
}
1172
1173
/*
1174
 * Process a message that the server has received from the client.
1175
 */
1176
MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
1177
104k
{
1178
104k
    OSSL_STATEM *st = &s->statem;
1179
1180
104k
    switch (st->hand_state) {
1181
0
    default:
1182
        /* Shouldn't happen */
1183
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1184
0
        return MSG_PROCESS_ERROR;
1185
1186
72.2k
    case TLS_ST_SR_CLNT_HELLO:
1187
72.2k
        return tls_process_client_hello(s, pkt);
1188
1189
0
    case TLS_ST_SR_END_OF_EARLY_DATA:
1190
0
        return tls_process_end_of_early_data(s, pkt);
1191
1192
0
    case TLS_ST_SR_CERT:
1193
0
        return tls_process_client_certificate(s, pkt);
1194
1195
19.0k
    case TLS_ST_SR_KEY_EXCH:
1196
19.0k
        return tls_process_client_key_exchange(s, pkt);
1197
1198
0
    case TLS_ST_SR_CERT_VRFY:
1199
0
        return tls_process_cert_verify(s, pkt);
1200
1201
0
#ifndef OPENSSL_NO_NEXTPROTONEG
1202
0
    case TLS_ST_SR_NEXT_PROTO:
1203
0
        return tls_process_next_proto(s, pkt);
1204
0
#endif
1205
1206
11.2k
    case TLS_ST_SR_CHANGE:
1207
11.2k
        return tls_process_change_cipher_spec(s, pkt);
1208
1209
1.53k
    case TLS_ST_SR_FINISHED:
1210
1.53k
        return tls_process_finished(s, pkt);
1211
1212
0
    case TLS_ST_SR_KEY_UPDATE:
1213
0
        return tls_process_key_update(s, pkt);
1214
104k
    }
1215
104k
}
1216
1217
/*
1218
 * Perform any further processing required following the receipt of a message
1219
 * from the client
1220
 */
1221
WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
1222
59.2k
{
1223
59.2k
    OSSL_STATEM *st = &s->statem;
1224
1225
59.2k
    switch (st->hand_state) {
1226
0
    default:
1227
        /* Shouldn't happen */
1228
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1229
0
        return WORK_ERROR;
1230
1231
42.3k
    case TLS_ST_SR_CLNT_HELLO:
1232
42.3k
        return tls_post_process_client_hello(s, wst);
1233
1234
16.9k
    case TLS_ST_SR_KEY_EXCH:
1235
16.9k
        return tls_post_process_client_key_exchange(s, wst);
1236
59.2k
    }
1237
59.2k
}
1238
1239
#ifndef OPENSSL_NO_SRP
1240
/* Returns 1 on success, 0 for retryable error, -1 for fatal error */
1241
static int ssl_check_srp_ext_ClientHello(SSL *s)
1242
29.9k
{
1243
29.9k
    int ret;
1244
29.9k
    int al = SSL_AD_UNRECOGNIZED_NAME;
1245
1246
29.9k
    if ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) && (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
1247
0
        if (s->srp_ctx.login == NULL) {
1248
            /*
1249
             * RFC 5054 says SHOULD reject, we do so if There is no srp
1250
             * login name
1251
             */
1252
0
            SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
1253
0
                SSL_R_PSK_IDENTITY_NOT_FOUND);
1254
0
            return -1;
1255
0
        } else {
1256
0
            ret = ssl_srp_server_param_with_username_intern(s, &al);
1257
0
            if (ret < 0)
1258
0
                return 0;
1259
0
            if (ret == SSL3_AL_FATAL) {
1260
0
                SSLfatal(s, al,
1261
0
                    al == SSL_AD_UNKNOWN_PSK_IDENTITY
1262
0
                        ? SSL_R_PSK_IDENTITY_NOT_FOUND
1263
0
                        : SSL_R_CLIENTHELLO_TLSEXT);
1264
0
                return -1;
1265
0
            }
1266
0
        }
1267
0
    }
1268
29.9k
    return 1;
1269
29.9k
}
1270
#endif
1271
1272
int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
1273
    size_t cookie_len)
1274
0
{
1275
    /* Always use DTLS 1.0 version: see RFC 6347 */
1276
0
    if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
1277
0
        || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
1278
0
        return 0;
1279
1280
0
    return 1;
1281
0
}
1282
1283
int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt)
1284
0
{
1285
0
    unsigned int cookie_leni;
1286
0
    if (s->ctx->app_gen_cookie_cb == NULL || s->ctx->app_gen_cookie_cb(s, s->d1->cookie, &cookie_leni) == 0 || cookie_leni > DTLS1_COOKIE_LENGTH) {
1287
0
        SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
1288
0
        return 0;
1289
0
    }
1290
0
    s->d1->cookie_len = cookie_leni;
1291
1292
0
    if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
1293
0
            s->d1->cookie_len)) {
1294
0
        SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
1295
0
        return 0;
1296
0
    }
1297
1298
0
    return 1;
1299
0
}
1300
1301
/*-
1302
 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1303
 * SecureTransport using the TLS extension block in |hello|.
1304
 * Safari, since 10.6, sends exactly these extensions, in this order:
1305
 *   SNI,
1306
 *   elliptic_curves
1307
 *   ec_point_formats
1308
 *   signature_algorithms (for TLSv1.2 only)
1309
 *
1310
 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1311
 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1312
 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1313
 * 10.8..10.8.3 (which don't work).
1314
 */
1315
static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
1316
0
{
1317
0
    static const unsigned char kSafariExtensionsBlock[] = {
1318
0
        0x00,
1319
0
        0x0a, /* elliptic_curves extension */
1320
0
        0x00,
1321
0
        0x08, /* 8 bytes */
1322
0
        0x00,
1323
0
        0x06, /* 6 bytes of curve ids */
1324
0
        0x00,
1325
0
        0x17, /* P-256 */
1326
0
        0x00,
1327
0
        0x18, /* P-384 */
1328
0
        0x00,
1329
0
        0x19, /* P-521 */
1330
1331
0
        0x00,
1332
0
        0x0b, /* ec_point_formats */
1333
0
        0x00,
1334
0
        0x02, /* 2 bytes */
1335
0
        0x01, /* 1 point format */
1336
0
        0x00, /* uncompressed */
1337
        /* The following is only present in TLS 1.2 */
1338
0
        0x00,
1339
0
        0x0d, /* signature_algorithms */
1340
0
        0x00,
1341
0
        0x0c, /* 12 bytes */
1342
0
        0x00,
1343
0
        0x0a, /* 10 bytes */
1344
0
        0x05,
1345
0
        0x01, /* SHA-384/RSA */
1346
0
        0x04,
1347
0
        0x01, /* SHA-256/RSA */
1348
0
        0x02,
1349
0
        0x01, /* SHA-1/RSA */
1350
0
        0x04,
1351
0
        0x03, /* SHA-256/ECDSA */
1352
0
        0x02,
1353
0
        0x03, /* SHA-1/ECDSA */
1354
0
    };
1355
    /* Length of the common prefix (first two extensions). */
1356
0
    static const size_t kSafariCommonExtensionsLength = 18;
1357
0
    unsigned int type;
1358
0
    PACKET sni, tmppkt;
1359
0
    size_t ext_len;
1360
1361
0
    tmppkt = hello->extensions;
1362
1363
0
    if (!PACKET_forward(&tmppkt, 2)
1364
0
        || !PACKET_get_net_2(&tmppkt, &type)
1365
0
        || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1366
0
        return;
1367
0
    }
1368
1369
0
    if (type != TLSEXT_TYPE_server_name)
1370
0
        return;
1371
1372
0
    ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ? sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1373
1374
0
    s->s3.is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1375
0
        ext_len);
1376
0
}
1377
1378
#define RENEG_OPTIONS_OK(options)             \
1379
56.7k
    ((options & SSL_OP_NO_RENEGOTIATION) == 0 \
1380
56.7k
        && (options & SSL_OP_ALLOW_CLIENT_RENEGOTIATION) != 0)
1381
1382
MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
1383
72.2k
{
1384
    /* |cookie| will only be initialized for DTLS. */
1385
72.2k
    PACKET session_id, compression, extensions, cookie;
1386
72.2k
    static const unsigned char null_compression = 0;
1387
72.2k
    CLIENTHELLO_MSG *clienthello = NULL;
1388
1389
    /* Check if this is actually an unexpected renegotiation ClientHello */
1390
72.2k
    if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
1391
28.3k
        if (!ossl_assert(!SSL_IS_TLS13(s))) {
1392
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1393
0
            goto err;
1394
0
        }
1395
28.3k
        if (!RENEG_OPTIONS_OK(s->options)
1396
0
            || (!s->s3.send_connection_binding
1397
0
                && (s->options
1398
0
                       & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
1399
28.3k
                    == 0)) {
1400
28.3k
            ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
1401
28.3k
            return MSG_PROCESS_FINISHED_READING;
1402
28.3k
        }
1403
0
        s->renegotiate = 1;
1404
0
        s->new_session = 1;
1405
0
    }
1406
1407
43.8k
    clienthello = OPENSSL_zalloc(sizeof(*clienthello));
1408
43.8k
    if (clienthello == NULL) {
1409
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1410
0
        goto err;
1411
0
    }
1412
1413
    /*
1414
     * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1415
     */
1416
43.8k
    clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
1417
43.8k
    PACKET_null_init(&cookie);
1418
1419
43.8k
    if (clienthello->isv2) {
1420
5.98k
        unsigned int mt;
1421
1422
5.98k
        if (!SSL_IS_FIRST_HANDSHAKE(s)
1423
5.98k
            || s->hello_retry_request != SSL_HRR_NONE) {
1424
0
            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
1425
0
            goto err;
1426
0
        }
1427
1428
        /*-
1429
         * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1430
         * header is sent directly on the wire, not wrapped as a TLS
1431
         * record. Our record layer just processes the message length and passes
1432
         * the rest right through. Its format is:
1433
         * Byte  Content
1434
         * 0-1   msg_length - decoded by the record layer
1435
         * 2     msg_type - s->init_msg points here
1436
         * 3-4   version
1437
         * 5-6   cipher_spec_length
1438
         * 7-8   session_id_length
1439
         * 9-10  challenge_length
1440
         * ...   ...
1441
         */
1442
1443
5.98k
        if (!PACKET_get_1(pkt, &mt)
1444
5.98k
            || mt != SSL2_MT_CLIENT_HELLO) {
1445
            /*
1446
             * Should never happen. We should have tested this in the record
1447
             * layer in order to have determined that this is a SSLv2 record
1448
             * in the first place
1449
             */
1450
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1451
0
            goto err;
1452
0
        }
1453
5.98k
    }
1454
1455
43.8k
    if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
1456
104
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
1457
104
        goto err;
1458
104
    }
1459
1460
    /* Parse the message and load client random. */
1461
43.7k
    if (clienthello->isv2) {
1462
        /*
1463
         * Handle an SSLv2 backwards compatible ClientHello
1464
         * Note, this is only for SSLv3+ using the backward compatible format.
1465
         * Real SSLv2 is not supported, and is rejected below.
1466
         */
1467
5.98k
        unsigned int ciphersuite_len, session_id_len, challenge_len;
1468
5.98k
        PACKET challenge;
1469
1470
5.98k
        if (!PACKET_get_net_2(pkt, &ciphersuite_len)
1471
5.98k
            || !PACKET_get_net_2(pkt, &session_id_len)
1472
5.98k
            || !PACKET_get_net_2(pkt, &challenge_len)) {
1473
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH);
1474
0
            goto err;
1475
0
        }
1476
1477
5.98k
        if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1478
123
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_MISMATCH);
1479
123
            goto err;
1480
123
        }
1481
1482
5.85k
        if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,
1483
5.85k
                ciphersuite_len)
1484
5.78k
            || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)
1485
5.77k
            || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
1486
            /* No extensions. */
1487
5.68k
            || PACKET_remaining(pkt) != 0) {
1488
347
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH);
1489
347
            goto err;
1490
347
        }
1491
5.51k
        clienthello->session_id_len = session_id_len;
1492
1493
        /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1494
         * here rather than sizeof(clienthello->random) because that is the limit
1495
         * for SSLv3 and it is fixed. It won't change even if
1496
         * sizeof(clienthello->random) does.
1497
         */
1498
5.51k
        challenge_len = challenge_len > SSL3_RANDOM_SIZE
1499
5.51k
            ? SSL3_RANDOM_SIZE
1500
5.51k
            : challenge_len;
1501
5.51k
        memset(clienthello->random, 0, SSL3_RANDOM_SIZE);
1502
5.51k
        if (!PACKET_copy_bytes(&challenge,
1503
5.51k
                clienthello->random + SSL3_RANDOM_SIZE - challenge_len, challenge_len)
1504
            /* Advertise only null compression. */
1505
5.51k
            || !PACKET_buf_init(&compression, &null_compression, 1)) {
1506
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1507
0
            goto err;
1508
0
        }
1509
1510
5.51k
        PACKET_null_init(&clienthello->extensions);
1511
37.7k
    } else {
1512
        /* Regular ClientHello. */
1513
37.7k
        if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
1514
37.7k
            || !PACKET_get_length_prefixed_1(pkt, &session_id)
1515
37.6k
            || !PACKET_copy_all(&session_id, clienthello->session_id,
1516
37.6k
                SSL_MAX_SSL_SESSION_ID_LENGTH,
1517
37.6k
                &clienthello->session_id_len)) {
1518
154
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1519
154
            goto err;
1520
154
        }
1521
1522
37.6k
        if (SSL_IS_DTLS(s)) {
1523
16.4k
            if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
1524
28
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1525
28
                goto err;
1526
28
            }
1527
16.4k
            if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
1528
16.4k
                    DTLS1_COOKIE_LENGTH,
1529
16.4k
                    &clienthello->dtls_cookie_len)) {
1530
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1531
0
                goto err;
1532
0
            }
1533
            /*
1534
             * If we require cookies and this ClientHello doesn't contain one,
1535
             * just return since we do not want to allocate any memory yet.
1536
             * So check cookie length...
1537
             */
1538
16.4k
            if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1539
0
                if (clienthello->dtls_cookie_len == 0) {
1540
0
                    OPENSSL_free(clienthello);
1541
0
                    return MSG_PROCESS_FINISHED_READING;
1542
0
                }
1543
0
            }
1544
16.4k
        }
1545
1546
37.6k
        if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
1547
127
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1548
127
            goto err;
1549
127
        }
1550
1551
37.4k
        if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
1552
40
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1553
40
            goto err;
1554
40
        }
1555
1556
        /* Could be empty. */
1557
37.4k
        if (PACKET_remaining(pkt) == 0) {
1558
5.46k
            PACKET_null_init(&clienthello->extensions);
1559
31.9k
        } else {
1560
31.9k
            if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)
1561
31.8k
                || PACKET_remaining(pkt) != 0) {
1562
260
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1563
260
                goto err;
1564
260
            }
1565
31.9k
        }
1566
37.4k
    }
1567
1568
42.6k
    if (!PACKET_copy_all(&compression, clienthello->compressions,
1569
42.6k
            MAX_COMPRESSIONS_SIZE,
1570
42.6k
            &clienthello->compressions_len)) {
1571
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1572
0
        goto err;
1573
0
    }
1574
1575
    /* Preserve the raw extensions PACKET for later use */
1576
42.6k
    extensions = clienthello->extensions;
1577
42.6k
    if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,
1578
42.6k
            &clienthello->pre_proc_exts,
1579
42.6k
            &clienthello->pre_proc_exts_len, 1)) {
1580
        /* SSLfatal already been called */
1581
346
        goto err;
1582
346
    }
1583
42.3k
    s->clienthello = clienthello;
1584
1585
42.3k
    return MSG_PROCESS_CONTINUE_PROCESSING;
1586
1587
1.52k
err:
1588
1.52k
    if (clienthello != NULL)
1589
1.52k
        OPENSSL_free(clienthello->pre_proc_exts);
1590
1.52k
    OPENSSL_free(clienthello);
1591
1592
1.52k
    return MSG_PROCESS_ERROR;
1593
42.6k
}
1594
1595
static int tls_early_post_process_client_hello(SSL *s)
1596
4.21k
{
1597
4.21k
    unsigned int j;
1598
4.21k
    int i, al = SSL_AD_INTERNAL_ERROR;
1599
4.21k
    int protverr;
1600
4.21k
    size_t loop;
1601
4.21k
    unsigned long id;
1602
4.21k
#ifndef OPENSSL_NO_COMP
1603
4.21k
    SSL_COMP *comp = NULL;
1604
4.21k
#endif
1605
4.21k
    const SSL_CIPHER *c;
1606
4.21k
    STACK_OF(SSL_CIPHER) *ciphers = NULL;
1607
4.21k
    STACK_OF(SSL_CIPHER) *scsvs = NULL;
1608
4.21k
    CLIENTHELLO_MSG *clienthello = s->clienthello;
1609
4.21k
    DOWNGRADE dgrd = DOWNGRADE_NONE;
1610
1611
    /* Finished parsing the ClientHello, now we can start processing it */
1612
    /* Give the ClientHello callback a crack at things */
1613
4.21k
    if (s->ctx->client_hello_cb != NULL) {
1614
        /* A failure in the ClientHello callback terminates the connection. */
1615
0
        switch (s->ctx->client_hello_cb(s, &al, s->ctx->client_hello_cb_arg)) {
1616
0
        case SSL_CLIENT_HELLO_SUCCESS:
1617
0
            break;
1618
0
        case SSL_CLIENT_HELLO_RETRY:
1619
0
            s->rwstate = SSL_CLIENT_HELLO_CB;
1620
0
            return -1;
1621
0
        case SSL_CLIENT_HELLO_ERROR:
1622
0
        default:
1623
0
            SSLfatal(s, al, SSL_R_CALLBACK_FAILED);
1624
0
            goto err;
1625
0
        }
1626
0
    }
1627
1628
    /* Set up the client_random */
1629
4.21k
    memcpy(s->s3.client_random, clienthello->random, SSL3_RANDOM_SIZE);
1630
1631
    /* Choose the version */
1632
1633
4.21k
    if (clienthello->isv2) {
1634
862
        if (clienthello->legacy_version == SSL2_VERSION
1635
861
            || (clienthello->legacy_version & 0xff00)
1636
861
                != (SSL3_VERSION_MAJOR << 8)) {
1637
            /*
1638
             * This is real SSLv2 or something completely unknown. We don't
1639
             * support it.
1640
             */
1641
14
            SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNKNOWN_PROTOCOL);
1642
14
            goto err;
1643
14
        }
1644
        /* SSLv3/TLS */
1645
848
        s->client_version = clienthello->legacy_version;
1646
848
    }
1647
    /*
1648
     * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1649
     * versions are potentially compatible. Version negotiation comes later.
1650
     */
1651
4.20k
    if (!SSL_IS_DTLS(s)) {
1652
4.20k
        protverr = ssl_choose_server_version(s, clienthello, &dgrd);
1653
4.20k
    } else if (s->method->version != DTLS_ANY_VERSION && DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) {
1654
0
        protverr = SSL_R_VERSION_TOO_LOW;
1655
0
    } else {
1656
0
        protverr = 0;
1657
0
    }
1658
1659
4.20k
    if (protverr) {
1660
104
        if (SSL_IS_FIRST_HANDSHAKE(s)) {
1661
            /* like ssl3_get_record, send alert using remote version number */
1662
104
            s->version = s->client_version = clienthello->legacy_version;
1663
104
        }
1664
104
        SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr);
1665
104
        goto err;
1666
104
    }
1667
1668
    /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1669
4.09k
    if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
1670
1
        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
1671
1
        goto err;
1672
1
    }
1673
1674
4.09k
    if (SSL_IS_DTLS(s)) {
1675
        /* Empty cookie was already handled above by returning early. */
1676
0
        if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1677
0
            if (s->ctx->app_verify_cookie_cb != NULL) {
1678
0
                if (s->ctx->app_verify_cookie_cb(s, clienthello->dtls_cookie,
1679
0
                        clienthello->dtls_cookie_len)
1680
0
                    == 0) {
1681
0
                    SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1682
0
                        SSL_R_COOKIE_MISMATCH);
1683
0
                    goto err;
1684
                    /* else cookie verification succeeded */
1685
0
                }
1686
                /* default verification */
1687
0
            } else if (s->d1->cookie_len != clienthello->dtls_cookie_len
1688
0
                || memcmp(clienthello->dtls_cookie, s->d1->cookie,
1689
0
                       s->d1->cookie_len)
1690
0
                    != 0) {
1691
0
                SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_COOKIE_MISMATCH);
1692
0
                goto err;
1693
0
            }
1694
0
            s->d1->cookie_verified = 1;
1695
0
        }
1696
0
        if (s->method->version == DTLS_ANY_VERSION) {
1697
0
            protverr = ssl_choose_server_version(s, clienthello, &dgrd);
1698
0
            if (protverr != 0) {
1699
0
                s->version = s->client_version;
1700
0
                SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr);
1701
0
                goto err;
1702
0
            }
1703
0
        }
1704
0
    }
1705
1706
4.09k
    s->hit = 0;
1707
1708
4.09k
    if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites,
1709
4.09k
            clienthello->isv2)
1710
4.08k
        || !bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, &scsvs,
1711
4.08k
            clienthello->isv2, 1)) {
1712
        /* SSLfatal() already called */
1713
15
        goto err;
1714
15
    }
1715
1716
4.08k
    s->s3.send_connection_binding = 0;
1717
    /* Check what signalling cipher-suite values were received. */
1718
4.08k
    if (scsvs != NULL) {
1719
5.42k
        for (i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {
1720
1.35k
            c = sk_SSL_CIPHER_value(scsvs, i);
1721
1.35k
            if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {
1722
1.25k
                if (s->renegotiate) {
1723
                    /* SCSV is fatal if renegotiating */
1724
0
                    SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1725
0
                        SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1726
0
                    goto err;
1727
0
                }
1728
1.25k
                s->s3.send_connection_binding = 1;
1729
1.25k
            } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV && !ssl_check_version_downgrade(s)) {
1730
                /*
1731
                 * This SCSV indicates that the client previously tried
1732
                 * a higher version.  We should fail if the current version
1733
                 * is an unexpected downgrade, as that indicates that the first
1734
                 * connection may have been tampered with in order to trigger
1735
                 * an insecure downgrade.
1736
                 */
1737
4
                SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK,
1738
4
                    SSL_R_INAPPROPRIATE_FALLBACK);
1739
4
                goto err;
1740
4
            }
1741
1.35k
        }
1742
4.08k
    }
1743
1744
    /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1745
4.07k
    if (SSL_IS_TLS13(s)) {
1746
950
        const SSL_CIPHER *cipher = ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(s));
1747
1748
950
        if (cipher == NULL) {
1749
8
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER);
1750
8
            goto err;
1751
8
        }
1752
942
        if (s->hello_retry_request == SSL_HRR_PENDING
1753
70
            && (s->s3.tmp.new_cipher == NULL
1754
70
                || s->s3.tmp.new_cipher->id != cipher->id)) {
1755
            /*
1756
             * A previous HRR picked a different ciphersuite to the one we
1757
             * just selected. Something must have changed.
1758
             */
1759
1
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER);
1760
1
            goto err;
1761
1
        }
1762
941
        s->s3.tmp.new_cipher = cipher;
1763
941
    }
1764
1765
    /* We need to do this before getting the session */
1766
4.06k
    if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
1767
4.06k
            SSL_EXT_CLIENT_HELLO,
1768
4.06k
            clienthello->pre_proc_exts, NULL, 0)) {
1769
        /* SSLfatal() already called */
1770
1
        goto err;
1771
1
    }
1772
1773
    /*
1774
     * We don't allow resumption in a backwards compatible ClientHello.
1775
     * In TLS1.1+, session_id MUST be empty.
1776
     *
1777
     * Versions before 0.9.7 always allow clients to resume sessions in
1778
     * renegotiation. 0.9.7 and later allow this by default, but optionally
1779
     * ignore resumption requests with flag
1780
     * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1781
     * than a change to default behavior so that applications relying on
1782
     * this for security won't even compile against older library versions).
1783
     * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1784
     * request renegotiation but not a new session (s->new_session remains
1785
     * unset): for servers, this essentially just means that the
1786
     * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1787
     * ignored.
1788
     */
1789
4.06k
    if (clienthello->isv2 || (s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1790
831
        if (!ssl_get_new_session(s, 1)) {
1791
            /* SSLfatal() already called */
1792
0
            goto err;
1793
0
        }
1794
3.23k
    } else {
1795
3.23k
        i = ssl_get_prev_session(s, clienthello);
1796
3.23k
        if (i == 1) {
1797
            /* previous session */
1798
46
            s->hit = 1;
1799
3.19k
        } else if (i == -1) {
1800
            /* SSLfatal() already called */
1801
71
            goto err;
1802
3.12k
        } else {
1803
            /* i == 0 */
1804
3.12k
            if (!ssl_get_new_session(s, 1)) {
1805
                /* SSLfatal() already called */
1806
0
                goto err;
1807
0
            }
1808
3.12k
        }
1809
3.23k
    }
1810
1811
3.99k
    if (SSL_IS_TLS13(s)) {
1812
874
        memcpy(s->tmp_session_id, s->clienthello->session_id,
1813
874
            s->clienthello->session_id_len);
1814
874
        s->tmp_session_id_len = s->clienthello->session_id_len;
1815
874
    }
1816
1817
    /*
1818
     * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1819
     * ciphersuite compatibility with the session as part of resumption.
1820
     */
1821
3.99k
    if (!SSL_IS_TLS13(s) && s->hit) {
1822
45
        j = 0;
1823
45
        id = s->session->cipher->id;
1824
1825
45
        OSSL_TRACE_BEGIN(TLS_CIPHER)
1826
0
        {
1827
0
            BIO_printf(trc_out, "client sent %d ciphers\n",
1828
0
                sk_SSL_CIPHER_num(ciphers));
1829
0
        }
1830
228
        for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1831
222
            c = sk_SSL_CIPHER_value(ciphers, i);
1832
222
            if (trc_out != NULL)
1833
0
                BIO_printf(trc_out, "client [%2d of %2d]:%s\n", i,
1834
0
                    sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1835
222
            if (c->id == id) {
1836
39
                j = 1;
1837
39
                break;
1838
39
            }
1839
222
        }
1840
45
        if (j == 0) {
1841
            /*
1842
             * we need to have the cipher in the cipher list if we are asked
1843
             * to reuse it
1844
             */
1845
6
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1846
6
                SSL_R_REQUIRED_CIPHER_MISSING);
1847
6
            OSSL_TRACE_CANCEL(TLS_CIPHER);
1848
6
            goto err;
1849
6
        }
1850
45
        OSSL_TRACE_END(TLS_CIPHER);
1851
45
    }
1852
1853
5.69k
    for (loop = 0; loop < clienthello->compressions_len; loop++) {
1854
5.66k
        if (clienthello->compressions[loop] == 0)
1855
3.96k
            break;
1856
5.66k
    }
1857
1858
3.99k
    if (loop >= clienthello->compressions_len) {
1859
        /* no compress */
1860
28
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_COMPRESSION_SPECIFIED);
1861
28
        goto err;
1862
28
    }
1863
1864
3.96k
    if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1865
0
        ssl_check_for_safari(s, clienthello);
1866
1867
    /* TLS extensions */
1868
3.96k
    if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,
1869
3.96k
            clienthello->pre_proc_exts, NULL, 0, 1)) {
1870
        /* SSLfatal() already called */
1871
874
        goto err;
1872
874
    }
1873
1874
    /*
1875
     * Check if we want to use external pre-shared secret for this handshake
1876
     * for not reused session only. We need to generate server_random before
1877
     * calling tls_session_secret_cb in order to allow SessionTicket
1878
     * processing to use it in key derivation.
1879
     */
1880
3.08k
    {
1881
3.08k
        unsigned char *pos;
1882
3.08k
        pos = s->s3.server_random;
1883
3.08k
        if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) {
1884
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1885
0
            goto err;
1886
0
        }
1887
3.08k
    }
1888
1889
3.08k
    if (!s->hit
1890
3.05k
        && s->version >= TLS1_VERSION
1891
3.05k
        && !SSL_IS_TLS13(s)
1892
2.37k
        && !SSL_IS_DTLS(s)
1893
2.37k
        && s->ext.session_secret_cb) {
1894
0
        const SSL_CIPHER *pref_cipher = NULL;
1895
        /*
1896
         * s->session->master_key_length is a size_t, but this is an int for
1897
         * backwards compat reasons
1898
         */
1899
0
        int master_key_length;
1900
1901
0
        master_key_length = sizeof(s->session->master_key);
1902
0
        if (s->ext.session_secret_cb(s, s->session->master_key,
1903
0
                &master_key_length, ciphers,
1904
0
                &pref_cipher,
1905
0
                s->ext.session_secret_cb_arg)
1906
0
            && master_key_length > 0) {
1907
0
            s->session->master_key_length = master_key_length;
1908
0
            s->hit = 1;
1909
0
            s->peer_ciphers = ciphers;
1910
0
            s->session->verify_result = X509_V_OK;
1911
1912
0
            ciphers = NULL;
1913
1914
            /* check if some cipher was preferred by call back */
1915
0
            if (pref_cipher == NULL)
1916
0
                pref_cipher = ssl3_choose_cipher(s, s->peer_ciphers,
1917
0
                    SSL_get_ciphers(s));
1918
0
            if (pref_cipher == NULL) {
1919
0
                SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER);
1920
0
                goto err;
1921
0
            }
1922
1923
0
            s->session->cipher = pref_cipher;
1924
0
            sk_SSL_CIPHER_free(s->cipher_list);
1925
0
            s->cipher_list = sk_SSL_CIPHER_dup(s->peer_ciphers);
1926
0
            sk_SSL_CIPHER_free(s->cipher_list_by_id);
1927
0
            s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->peer_ciphers);
1928
0
        }
1929
0
    }
1930
1931
    /*
1932
     * Worst case, we will use the NULL compression, but if we have other
1933
     * options, we will now look for them.  We have complen-1 compression
1934
     * algorithms from the client, starting at q.
1935
     */
1936
3.08k
    s->s3.tmp.new_compression = NULL;
1937
3.08k
    if (SSL_IS_TLS13(s)) {
1938
        /*
1939
         * We already checked above that the NULL compression method appears in
1940
         * the list. Now we check there aren't any others (which is illegal in
1941
         * a TLSv1.3 ClientHello.
1942
         */
1943
683
        if (clienthello->compressions_len != 1) {
1944
1
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1945
1
                SSL_R_INVALID_COMPRESSION_ALGORITHM);
1946
1
            goto err;
1947
1
        }
1948
683
    }
1949
2.40k
#ifndef OPENSSL_NO_COMP
1950
    /* This only happens if we have a cache hit */
1951
2.40k
    else if (s->session->compress_meth != 0) {
1952
0
        int m, comp_id = s->session->compress_meth;
1953
0
        unsigned int k;
1954
        /* Perform sanity checks on resumed compression algorithm */
1955
        /* Can't disable compression */
1956
0
        if (!ssl_allow_compression(s)) {
1957
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1958
0
                SSL_R_INCONSISTENT_COMPRESSION);
1959
0
            goto err;
1960
0
        }
1961
        /* Look for resumed compression method */
1962
0
        for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1963
0
            comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1964
0
            if (comp_id == comp->id) {
1965
0
                s->s3.tmp.new_compression = comp;
1966
0
                break;
1967
0
            }
1968
0
        }
1969
0
        if (s->s3.tmp.new_compression == NULL) {
1970
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1971
0
                SSL_R_INVALID_COMPRESSION_ALGORITHM);
1972
0
            goto err;
1973
0
        }
1974
        /* Look for resumed method in compression list */
1975
0
        for (k = 0; k < clienthello->compressions_len; k++) {
1976
0
            if (clienthello->compressions[k] == comp_id)
1977
0
                break;
1978
0
        }
1979
0
        if (k >= clienthello->compressions_len) {
1980
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1981
0
                SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
1982
0
            goto err;
1983
0
        }
1984
2.40k
    } else if (s->hit) {
1985
36
        comp = NULL;
1986
2.37k
    } else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1987
        /* See if we have a match */
1988
0
        int m, nn, v, done = 0;
1989
0
        unsigned int o;
1990
1991
0
        nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1992
0
        for (m = 0; m < nn; m++) {
1993
0
            comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1994
0
            v = comp->id;
1995
0
            for (o = 0; o < clienthello->compressions_len; o++) {
1996
0
                if (v == clienthello->compressions[o]) {
1997
0
                    done = 1;
1998
0
                    break;
1999
0
                }
2000
0
            }
2001
0
            if (done)
2002
0
                break;
2003
0
        }
2004
0
        if (done)
2005
0
            s->s3.tmp.new_compression = comp;
2006
0
        else
2007
0
            comp = NULL;
2008
0
    }
2009
#else
2010
    /*
2011
     * If compression is disabled we'd better not try to resume a session
2012
     * using compression.
2013
     */
2014
    if (s->session->compress_meth != 0) {
2015
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION);
2016
        goto err;
2017
    }
2018
#endif
2019
2020
    /*
2021
     * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher
2022
     */
2023
2024
3.08k
    if (!s->hit || SSL_IS_TLS13(s)) {
2025
3.05k
        sk_SSL_CIPHER_free(s->peer_ciphers);
2026
3.05k
        s->peer_ciphers = ciphers;
2027
3.05k
        if (ciphers == NULL) {
2028
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2029
0
            goto err;
2030
0
        }
2031
3.05k
        ciphers = NULL;
2032
3.05k
    }
2033
2034
3.08k
    if (!s->hit) {
2035
#ifdef OPENSSL_NO_COMP
2036
        s->session->compress_meth = 0;
2037
#else
2038
3.05k
        s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
2039
3.05k
#endif
2040
3.05k
        if (!tls1_set_server_sigalgs(s)) {
2041
            /* SSLfatal() already called */
2042
16
            goto err;
2043
16
        }
2044
3.05k
    }
2045
2046
3.07k
    sk_SSL_CIPHER_free(ciphers);
2047
3.07k
    sk_SSL_CIPHER_free(scsvs);
2048
3.07k
    OPENSSL_free(clienthello->pre_proc_exts);
2049
3.07k
    OPENSSL_free(s->clienthello);
2050
3.07k
    s->clienthello = NULL;
2051
3.07k
    return 1;
2052
1.14k
err:
2053
1.14k
    sk_SSL_CIPHER_free(ciphers);
2054
1.14k
    sk_SSL_CIPHER_free(scsvs);
2055
1.14k
    OPENSSL_free(clienthello->pre_proc_exts);
2056
1.14k
    OPENSSL_free(s->clienthello);
2057
1.14k
    s->clienthello = NULL;
2058
2059
1.14k
    return 0;
2060
3.08k
}
2061
2062
/*
2063
 * Call the status request callback if needed. Upon success, returns 1.
2064
 * Upon failure, returns 0.
2065
 */
2066
static int tls_handle_status_request(SSL *s)
2067
18.9k
{
2068
18.9k
    s->ext.status_expected = 0;
2069
2070
    /*
2071
     * If status request then ask callback what to do. Note: this must be
2072
     * called after servername callbacks in case the certificate has changed,
2073
     * and must be called after the cipher has been chosen because this may
2074
     * influence which certificate is sent
2075
     */
2076
18.9k
    if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && s->ctx != NULL
2077
1.20k
        && s->ctx->ext.status_cb != NULL) {
2078
0
        int ret;
2079
2080
        /* If no certificate can't return certificate status */
2081
0
        if (s->s3.tmp.cert != NULL) {
2082
            /*
2083
             * Set current certificate to one we will use so SSL_get_certificate
2084
             * et al can pick it up.
2085
             */
2086
0
            s->cert->key = s->s3.tmp.cert;
2087
0
            ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
2088
0
            switch (ret) {
2089
                /* We don't want to send a status request response */
2090
0
            case SSL_TLSEXT_ERR_NOACK:
2091
0
                s->ext.status_expected = 0;
2092
0
                break;
2093
                /* status request response should be sent */
2094
0
            case SSL_TLSEXT_ERR_OK:
2095
0
                if (s->ext.ocsp.resp)
2096
0
                    s->ext.status_expected = 1;
2097
0
                break;
2098
                /* something bad happened */
2099
0
            case SSL_TLSEXT_ERR_ALERT_FATAL:
2100
0
            default:
2101
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CLIENTHELLO_TLSEXT);
2102
0
                return 0;
2103
0
            }
2104
0
        }
2105
0
    }
2106
2107
18.9k
    return 1;
2108
18.9k
}
2109
2110
/*
2111
 * Call the alpn_select callback if needed. Upon success, returns 1.
2112
 * Upon failure, returns 0.
2113
 */
2114
int tls_handle_alpn(SSL *s)
2115
30.3k
{
2116
30.3k
    const unsigned char *selected = NULL;
2117
30.3k
    unsigned char selected_len = 0;
2118
2119
30.3k
    if (s->ctx->ext.alpn_select_cb != NULL && s->s3.alpn_proposed != NULL) {
2120
0
        int r = s->ctx->ext.alpn_select_cb(s, &selected, &selected_len,
2121
0
            s->s3.alpn_proposed,
2122
0
            (unsigned int)s->s3.alpn_proposed_len,
2123
0
            s->ctx->ext.alpn_select_cb_arg);
2124
2125
0
        if (r == SSL_TLSEXT_ERR_OK) {
2126
0
            OPENSSL_free(s->s3.alpn_selected);
2127
0
            s->s3.alpn_selected = OPENSSL_memdup(selected, selected_len);
2128
0
            if (s->s3.alpn_selected == NULL) {
2129
0
                s->s3.alpn_selected_len = 0;
2130
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2131
0
                return 0;
2132
0
            }
2133
0
            s->s3.alpn_selected_len = selected_len;
2134
0
#ifndef OPENSSL_NO_NEXTPROTONEG
2135
            /* ALPN takes precedence over NPN. */
2136
0
            s->s3.npn_seen = 0;
2137
0
#endif
2138
2139
            /* Check ALPN is consistent with session */
2140
0
            if (s->session->ext.alpn_selected == NULL
2141
0
                || selected_len != s->session->ext.alpn_selected_len
2142
0
                || memcmp(selected, s->session->ext.alpn_selected,
2143
0
                       selected_len)
2144
0
                    != 0) {
2145
                /* Not consistent so can't be used for early_data */
2146
0
                s->ext.early_data_ok = 0;
2147
2148
0
                if (!s->hit) {
2149
                    /*
2150
                     * This is a new session and so alpn_selected should have
2151
                     * been initialised to NULL. We should update it with the
2152
                     * selected ALPN.
2153
                     */
2154
0
                    if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
2155
0
                        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2156
0
                            ERR_R_INTERNAL_ERROR);
2157
0
                        return 0;
2158
0
                    }
2159
0
                    s->session->ext.alpn_selected = OPENSSL_memdup(selected,
2160
0
                        selected_len);
2161
0
                    if (s->session->ext.alpn_selected == NULL) {
2162
0
                        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2163
0
                            ERR_R_INTERNAL_ERROR);
2164
0
                        return 0;
2165
0
                    }
2166
0
                    s->session->ext.alpn_selected_len = selected_len;
2167
0
                }
2168
0
            }
2169
2170
0
            return 1;
2171
0
        } else if (r != SSL_TLSEXT_ERR_NOACK) {
2172
0
            SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL,
2173
0
                SSL_R_NO_APPLICATION_PROTOCOL);
2174
0
            return 0;
2175
0
        }
2176
        /*
2177
         * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
2178
         * present.
2179
         */
2180
0
    }
2181
2182
    /* Check ALPN is consistent with session */
2183
30.3k
    if (s->session->ext.alpn_selected != NULL) {
2184
        /* Not consistent so can't be used for early_data */
2185
0
        s->ext.early_data_ok = 0;
2186
0
    }
2187
2188
30.3k
    return 1;
2189
30.3k
}
2190
2191
WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
2192
42.3k
{
2193
42.3k
    const SSL_CIPHER *cipher;
2194
2195
42.3k
    if (wst == WORK_MORE_A) {
2196
42.3k
        int rv = tls_early_post_process_client_hello(s);
2197
42.3k
        if (rv == 0) {
2198
            /* SSLfatal() was already called */
2199
9.60k
            goto err;
2200
9.60k
        }
2201
32.7k
        if (rv < 0)
2202
0
            return WORK_MORE_A;
2203
32.7k
        wst = WORK_MORE_B;
2204
32.7k
    }
2205
32.7k
    if (wst == WORK_MORE_B) {
2206
32.7k
        if (!s->hit || SSL_IS_TLS13(s)) {
2207
            /* Let cert callback update server certificates if required */
2208
32.5k
            if (!s->hit && s->cert->cert_cb != NULL) {
2209
0
                int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2210
0
                if (rv == 0) {
2211
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CERT_CB_ERROR);
2212
0
                    goto err;
2213
0
                }
2214
0
                if (rv < 0) {
2215
0
                    s->rwstate = SSL_X509_LOOKUP;
2216
0
                    return WORK_MORE_B;
2217
0
                }
2218
0
                s->rwstate = SSL_NOTHING;
2219
0
            }
2220
2221
            /* In TLSv1.3 we selected the ciphersuite before resumption */
2222
32.5k
            if (!SSL_IS_TLS13(s)) {
2223
28.6k
                cipher = ssl3_choose_cipher(s, s->peer_ciphers, SSL_get_ciphers(s));
2224
2225
28.6k
                if (cipher == NULL) {
2226
1.43k
                    SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2227
1.43k
                        SSL_R_NO_SHARED_CIPHER);
2228
1.43k
                    goto err;
2229
1.43k
                }
2230
27.1k
                s->s3.tmp.new_cipher = cipher;
2231
27.1k
            }
2232
31.1k
            if (!s->hit) {
2233
31.1k
                if (!tls_choose_sigalg(s, 1)) {
2234
                    /* SSLfatal already called */
2235
1.32k
                    goto err;
2236
1.32k
                }
2237
                /* check whether we should disable session resumption */
2238
29.7k
                if (s->not_resumable_session_cb != NULL)
2239
0
                    s->session->not_resumable = s->not_resumable_session_cb(s,
2240
0
                        ((s->s3.tmp.new_cipher->algorithm_mkey
2241
0
                             & (SSL_kDHE | SSL_kECDHE))
2242
0
                            != 0));
2243
29.7k
                if (s->session->not_resumable)
2244
                    /* do not send a session ticket */
2245
0
                    s->ext.ticket_expected = 0;
2246
29.7k
            }
2247
31.1k
        } else {
2248
            /* Session-id reuse */
2249
189
            s->s3.tmp.new_cipher = s->session->cipher;
2250
189
        }
2251
2252
        /*-
2253
         * we now have the following setup.
2254
         * client_random
2255
         * cipher_list          - our preferred list of ciphers
2256
         * ciphers              - the clients preferred list of ciphers
2257
         * compression          - basically ignored right now
2258
         * ssl version is set   - sslv3
2259
         * s->session           - The ssl session has been setup.
2260
         * s->hit               - session reuse flag
2261
         * s->s3.tmp.new_cipher - the new cipher to use.
2262
         */
2263
2264
        /*
2265
         * Call status_request callback if needed. Has to be done after the
2266
         * certificate callbacks etc above.
2267
         */
2268
29.9k
        if (!tls_handle_status_request(s)) {
2269
            /* SSLfatal() already called */
2270
0
            goto err;
2271
0
        }
2272
        /*
2273
         * Call alpn_select callback if needed.  Has to be done after SNI and
2274
         * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
2275
         * we already did this because cipher negotiation happens earlier, and
2276
         * we must handle ALPN before we decide whether to accept early_data.
2277
         */
2278
29.9k
        if (!SSL_IS_TLS13(s) && !tls_handle_alpn(s)) {
2279
            /* SSLfatal() already called */
2280
0
            goto err;
2281
0
        }
2282
2283
29.9k
        wst = WORK_MORE_C;
2284
29.9k
    }
2285
29.9k
#ifndef OPENSSL_NO_SRP
2286
29.9k
    if (wst == WORK_MORE_C) {
2287
29.9k
        int ret;
2288
29.9k
        if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) {
2289
            /*
2290
             * callback indicates further work to be done
2291
             */
2292
0
            s->rwstate = SSL_X509_LOOKUP;
2293
0
            return WORK_MORE_C;
2294
0
        }
2295
29.9k
        if (ret < 0) {
2296
            /* SSLfatal() already called */
2297
0
            goto err;
2298
0
        }
2299
29.9k
    }
2300
29.9k
#endif
2301
2302
29.9k
    return WORK_FINISHED_STOP;
2303
12.3k
err:
2304
12.3k
    return WORK_ERROR;
2305
29.9k
}
2306
2307
int tls_construct_server_hello(SSL *s, WPACKET *pkt)
2308
29.9k
{
2309
29.9k
    int compm;
2310
29.9k
    size_t sl, len;
2311
29.9k
    int version;
2312
29.9k
    unsigned char *session_id;
2313
29.9k
    int usetls13 = SSL_IS_TLS13(s) || s->hello_retry_request == SSL_HRR_PENDING;
2314
2315
29.9k
    version = usetls13 ? TLS1_2_VERSION : s->version;
2316
29.9k
    if (!WPACKET_put_bytes_u16(pkt, version)
2317
        /*
2318
         * Random stuff. Filling of the server_random takes place in
2319
         * tls_process_client_hello()
2320
         */
2321
29.9k
        || !WPACKET_memcpy(pkt,
2322
29.9k
            s->hello_retry_request == SSL_HRR_PENDING
2323
29.9k
                ? hrrrandom
2324
29.9k
                : s->s3.server_random,
2325
29.9k
            SSL3_RANDOM_SIZE)) {
2326
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2327
0
        return 0;
2328
0
    }
2329
2330
    /*-
2331
     * There are several cases for the session ID to send
2332
     * back in the server hello:
2333
     * - For session reuse from the session cache,
2334
     *   we send back the old session ID.
2335
     * - If stateless session reuse (using a session ticket)
2336
     *   is successful, we send back the client's "session ID"
2337
     *   (which doesn't actually identify the session).
2338
     * - If it is a new session, we send back the new
2339
     *   session ID.
2340
     * - However, if we want the new session to be single-use,
2341
     *   we send back a 0-length session ID.
2342
     * - In TLSv1.3 we echo back the session id sent to us by the client
2343
     *   regardless
2344
     * s->hit is non-zero in either case of session reuse,
2345
     * so the following won't overwrite an ID that we're supposed
2346
     * to send back.
2347
     */
2348
29.9k
    if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
2349
0
        && !s->hit)
2350
0
        s->session->session_id_length = 0;
2351
2352
29.9k
    if (usetls13) {
2353
3.74k
        sl = s->tmp_session_id_len;
2354
3.74k
        session_id = s->tmp_session_id;
2355
26.2k
    } else {
2356
26.2k
        sl = s->session->session_id_length;
2357
26.2k
        session_id = s->session->session_id;
2358
26.2k
    }
2359
2360
29.9k
    if (sl > sizeof(s->session->session_id)) {
2361
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2362
0
        return 0;
2363
0
    }
2364
2365
    /* set up the compression method */
2366
#ifdef OPENSSL_NO_COMP
2367
    compm = 0;
2368
#else
2369
29.9k
    if (usetls13 || s->s3.tmp.new_compression == NULL)
2370
29.9k
        compm = 0;
2371
0
    else
2372
0
        compm = s->s3.tmp.new_compression->id;
2373
29.9k
#endif
2374
2375
29.9k
    if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl)
2376
29.9k
        || !s->method->put_cipher_by_char(s->s3.tmp.new_cipher, pkt, &len)
2377
29.9k
        || !WPACKET_put_bytes_u8(pkt, compm)) {
2378
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2379
0
        return 0;
2380
0
    }
2381
2382
29.9k
    if (!tls_construct_extensions(s, pkt,
2383
29.9k
            s->hello_retry_request == SSL_HRR_PENDING
2384
29.9k
                ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2385
29.9k
                : (SSL_IS_TLS13(s)
2386
29.2k
                          ? SSL_EXT_TLS1_3_SERVER_HELLO
2387
29.2k
                          : SSL_EXT_TLS1_2_SERVER_HELLO),
2388
29.9k
            NULL, 0)) {
2389
        /* SSLfatal() already called */
2390
39
        return 0;
2391
39
    }
2392
2393
29.9k
    if (s->hello_retry_request == SSL_HRR_PENDING) {
2394
        /* Ditch the session. We'll create a new one next time around */
2395
712
        SSL_SESSION_free(s->session);
2396
712
        s->session = NULL;
2397
712
        s->hit = 0;
2398
2399
        /*
2400
         * Re-initialise the Transcript Hash. We're going to prepopulate it with
2401
         * a synthetic message_hash in place of ClientHello1.
2402
         */
2403
712
        if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
2404
            /* SSLfatal() already called */
2405
0
            return 0;
2406
0
        }
2407
29.2k
    } else if (!(s->verify_mode & SSL_VERIFY_PEER)
2408
29.2k
        && !ssl3_digest_cached_records(s, 0)) {
2409
0
        /* SSLfatal() already called */;
2410
0
        return 0;
2411
0
    }
2412
2413
29.9k
    return 1;
2414
29.9k
}
2415
2416
int tls_construct_server_done(SSL *s, WPACKET *pkt)
2417
20.7k
{
2418
20.7k
    if (!s->s3.tmp.cert_request) {
2419
20.7k
        if (!ssl3_digest_cached_records(s, 0)) {
2420
            /* SSLfatal() already called */
2421
0
            return 0;
2422
0
        }
2423
20.7k
    }
2424
20.7k
    return 1;
2425
20.7k
}
2426
2427
int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
2428
9.47k
{
2429
9.47k
    EVP_PKEY *pkdh = NULL;
2430
9.47k
    unsigned char *encodedPoint = NULL;
2431
9.47k
    size_t encodedlen = 0;
2432
9.47k
    int curve_id = 0;
2433
9.47k
    const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg;
2434
9.47k
    int i;
2435
9.47k
    unsigned long type;
2436
9.47k
    BIGNUM *r[4];
2437
9.47k
    EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
2438
9.47k
    EVP_PKEY_CTX *pctx = NULL;
2439
9.47k
    size_t paramlen, paramoffset;
2440
9.47k
    int freer = 0, ret = 0;
2441
2442
9.47k
    if (!WPACKET_get_total_written(pkt, &paramoffset)) {
2443
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2444
0
        goto err;
2445
0
    }
2446
2447
9.47k
    if (md_ctx == NULL) {
2448
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2449
0
        goto err;
2450
0
    }
2451
2452
9.47k
    type = s->s3.tmp.new_cipher->algorithm_mkey;
2453
2454
9.47k
    r[0] = r[1] = r[2] = r[3] = NULL;
2455
9.47k
#ifndef OPENSSL_NO_PSK
2456
    /* Plain PSK or RSAPSK nothing to do */
2457
9.47k
    if (type & (SSL_kPSK | SSL_kRSAPSK)) {
2458
0
    } else
2459
9.47k
#endif /* !OPENSSL_NO_PSK */
2460
9.47k
        if (type & (SSL_kDHE | SSL_kDHEPSK)) {
2461
0
            CERT *cert = s->cert;
2462
0
            EVP_PKEY *pkdhp = NULL;
2463
2464
0
            if (s->cert->dh_tmp_auto) {
2465
0
                pkdh = ssl_get_auto_dh(s);
2466
0
                if (pkdh == NULL) {
2467
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2468
0
                    goto err;
2469
0
                }
2470
0
                pkdhp = pkdh;
2471
0
            } else {
2472
0
                pkdhp = cert->dh_tmp;
2473
0
            }
2474
0
#if !defined(OPENSSL_NO_DEPRECATED_3_0)
2475
0
            if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
2476
0
                pkdh = ssl_dh_to_pkey(s->cert->dh_tmp_cb(s, 0, 1024));
2477
0
                if (pkdh == NULL) {
2478
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2479
0
                    goto err;
2480
0
                }
2481
0
                pkdhp = pkdh;
2482
0
            }
2483
0
#endif
2484
0
            if (pkdhp == NULL) {
2485
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);
2486
0
                goto err;
2487
0
            }
2488
0
            if (!ssl_security(s, SSL_SECOP_TMP_DH,
2489
0
                    EVP_PKEY_get_security_bits(pkdhp), 0, pkdhp)) {
2490
0
                SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL);
2491
0
                goto err;
2492
0
            }
2493
0
            if (s->s3.tmp.pkey != NULL) {
2494
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2495
0
                goto err;
2496
0
            }
2497
2498
0
            s->s3.tmp.pkey = ssl_generate_pkey(s, pkdhp);
2499
0
            if (s->s3.tmp.pkey == NULL) {
2500
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2501
0
                goto err;
2502
0
            }
2503
2504
0
            EVP_PKEY_free(pkdh);
2505
0
            pkdh = NULL;
2506
2507
            /* These BIGNUMs need to be freed when we're finished */
2508
0
            freer = 1;
2509
0
            if (!EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_P,
2510
0
                    &r[0])
2511
0
                || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_G,
2512
0
                    &r[1])
2513
0
                || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey,
2514
0
                    OSSL_PKEY_PARAM_PUB_KEY, &r[2])) {
2515
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2516
0
                goto err;
2517
0
            }
2518
9.47k
        } else if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2519
2520
9.47k
            if (s->s3.tmp.pkey != NULL) {
2521
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2522
0
                goto err;
2523
0
            }
2524
2525
            /* Get NID of appropriate shared curve */
2526
9.47k
            curve_id = tls1_shared_group(s, -2);
2527
9.47k
            if (curve_id == 0) {
2528
0
                SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2529
0
                    SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
2530
0
                goto err;
2531
0
            }
2532
            /* Cache the group used in the SSL_SESSION */
2533
9.47k
            s->session->kex_group = curve_id;
2534
            /* Generate a new key for this curve */
2535
9.47k
            s->s3.tmp.pkey = ssl_generate_pkey_group(s, curve_id);
2536
9.47k
            if (s->s3.tmp.pkey == NULL) {
2537
                /* SSLfatal() already called */
2538
0
                goto err;
2539
0
            }
2540
2541
            /* Encode the public key. */
2542
9.47k
            encodedlen = EVP_PKEY_get1_encoded_public_key(s->s3.tmp.pkey,
2543
9.47k
                &encodedPoint);
2544
9.47k
            if (encodedlen == 0) {
2545
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
2546
0
                goto err;
2547
0
            }
2548
2549
            /*
2550
             * We'll generate the serverKeyExchange message explicitly so we
2551
             * can set these to NULLs
2552
             */
2553
9.47k
            r[0] = NULL;
2554
9.47k
            r[1] = NULL;
2555
9.47k
            r[2] = NULL;
2556
9.47k
            r[3] = NULL;
2557
9.47k
        } else
2558
0
#ifndef OPENSSL_NO_SRP
2559
0
            if (type & SSL_kSRP) {
2560
0
            if ((s->srp_ctx.N == NULL) || (s->srp_ctx.g == NULL) || (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
2561
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_SRP_PARAM);
2562
0
                goto err;
2563
0
            }
2564
0
            r[0] = s->srp_ctx.N;
2565
0
            r[1] = s->srp_ctx.g;
2566
0
            r[2] = s->srp_ctx.s;
2567
0
            r[3] = s->srp_ctx.B;
2568
0
        } else
2569
0
#endif
2570
0
        {
2571
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2572
0
            goto err;
2573
0
        }
2574
2575
9.47k
    if (((s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
2576
7.47k
        || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
2577
2.00k
        lu = NULL;
2578
7.47k
    } else if (lu == NULL) {
2579
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
2580
0
        goto err;
2581
0
    }
2582
2583
9.47k
#ifndef OPENSSL_NO_PSK
2584
9.47k
    if (type & SSL_PSK) {
2585
0
        size_t len = (s->cert->psk_identity_hint == NULL)
2586
0
            ? 0
2587
0
            : strlen(s->cert->psk_identity_hint);
2588
2589
        /*
2590
         * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2591
         * checked this when we set the identity hint - but just in case
2592
         */
2593
0
        if (len > PSK_MAX_IDENTITY_LEN
2594
0
            || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
2595
0
                len)) {
2596
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2597
0
            goto err;
2598
0
        }
2599
0
    }
2600
9.47k
#endif
2601
2602
9.47k
    for (i = 0; i < 4 && r[i] != NULL; i++) {
2603
0
        unsigned char *binval;
2604
0
        int res;
2605
2606
0
#ifndef OPENSSL_NO_SRP
2607
0
        if ((i == 2) && (type & SSL_kSRP)) {
2608
0
            res = WPACKET_start_sub_packet_u8(pkt);
2609
0
        } else
2610
0
#endif
2611
0
            res = WPACKET_start_sub_packet_u16(pkt);
2612
2613
0
        if (!res) {
2614
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2615
0
            goto err;
2616
0
        }
2617
2618
        /*-
2619
         * for interoperability with some versions of the Microsoft TLS
2620
         * stack, we need to zero pad the DHE pub key to the same length
2621
         * as the prime
2622
         */
2623
0
        if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
2624
0
            size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
2625
2626
0
            if (len > 0) {
2627
0
                if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
2628
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2629
0
                    goto err;
2630
0
                }
2631
0
                memset(binval, 0, len);
2632
0
            }
2633
0
        }
2634
2635
0
        if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
2636
0
            || !WPACKET_close(pkt)) {
2637
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2638
0
            goto err;
2639
0
        }
2640
2641
0
        BN_bn2bin(r[i], binval);
2642
0
    }
2643
2644
9.47k
    if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2645
        /*
2646
         * We only support named (not generic) curves. In this situation, the
2647
         * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2648
         * [1 byte length of encoded point], followed by the actual encoded
2649
         * point itself
2650
         */
2651
9.47k
        if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
2652
9.47k
            || !WPACKET_put_bytes_u8(pkt, 0)
2653
9.47k
            || !WPACKET_put_bytes_u8(pkt, curve_id)
2654
9.47k
            || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
2655
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2656
0
            goto err;
2657
0
        }
2658
9.47k
        OPENSSL_free(encodedPoint);
2659
9.47k
        encodedPoint = NULL;
2660
9.47k
    }
2661
2662
    /* not anonymous */
2663
9.47k
    if (lu != NULL) {
2664
7.47k
        EVP_PKEY *pkey = s->s3.tmp.cert->privatekey;
2665
7.47k
        const EVP_MD *md;
2666
7.47k
        unsigned char *sigbytes1, *sigbytes2, *tbs;
2667
7.47k
        size_t siglen = 0, tbslen;
2668
2669
7.47k
        if (pkey == NULL || !tls1_lookup_md(s->ctx, lu, &md)) {
2670
            /* Should never happen */
2671
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2672
0
            goto err;
2673
0
        }
2674
        /* Get length of the parameters we have written above */
2675
7.47k
        if (!WPACKET_get_length(pkt, &paramlen)) {
2676
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2677
0
            goto err;
2678
0
        }
2679
        /* send signature algorithm */
2680
7.47k
        if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
2681
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2682
0
            goto err;
2683
0
        }
2684
2685
7.47k
        if (EVP_DigestSignInit_ex(md_ctx, &pctx,
2686
7.47k
                md == NULL ? NULL : EVP_MD_get0_name(md),
2687
7.47k
                s->ctx->libctx, s->ctx->propq, pkey,
2688
7.47k
                NULL)
2689
7.47k
            <= 0) {
2690
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2691
0
            goto err;
2692
0
        }
2693
7.47k
        if (lu->sig == EVP_PKEY_RSA_PSS) {
2694
608
            if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
2695
608
                || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
2696
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2697
0
                goto err;
2698
0
            }
2699
608
        }
2700
7.47k
        tbslen = construct_key_exchange_tbs(s, &tbs,
2701
7.47k
            s->init_buf->data + paramoffset,
2702
7.47k
            paramlen);
2703
7.47k
        if (tbslen == 0) {
2704
            /* SSLfatal() already called */
2705
0
            goto err;
2706
0
        }
2707
2708
7.47k
        if (EVP_DigestSign(md_ctx, NULL, &siglen, tbs, tbslen) <= 0
2709
7.47k
            || !WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
2710
7.47k
            || EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen) <= 0
2711
7.47k
            || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
2712
7.47k
            || sigbytes1 != sigbytes2) {
2713
0
            OPENSSL_free(tbs);
2714
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2715
0
            goto err;
2716
0
        }
2717
7.47k
        OPENSSL_free(tbs);
2718
7.47k
    }
2719
2720
9.47k
    ret = 1;
2721
9.47k
err:
2722
9.47k
    EVP_PKEY_free(pkdh);
2723
9.47k
    OPENSSL_free(encodedPoint);
2724
9.47k
    EVP_MD_CTX_free(md_ctx);
2725
9.47k
    if (freer) {
2726
0
        BN_free(r[0]);
2727
0
        BN_free(r[1]);
2728
0
        BN_free(r[2]);
2729
0
        BN_free(r[3]);
2730
0
    }
2731
9.47k
    return ret;
2732
9.47k
}
2733
2734
int tls_construct_certificate_request(SSL *s, WPACKET *pkt)
2735
0
{
2736
0
    if (SSL_IS_TLS13(s)) {
2737
        /* Send random context when doing post-handshake auth */
2738
0
        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
2739
0
            OPENSSL_free(s->pha_context);
2740
0
            s->pha_context_len = 32;
2741
0
            if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL) {
2742
0
                s->pha_context_len = 0;
2743
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2744
0
                return 0;
2745
0
            }
2746
0
            if (RAND_bytes_ex(s->ctx->libctx, s->pha_context,
2747
0
                    s->pha_context_len, 0)
2748
0
                    <= 0
2749
0
                || !WPACKET_sub_memcpy_u8(pkt, s->pha_context,
2750
0
                    s->pha_context_len)) {
2751
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2752
0
                return 0;
2753
0
            }
2754
            /* reset the handshake hash back to just after the ClientFinished */
2755
0
            if (!tls13_restore_handshake_digest_for_pha(s)) {
2756
                /* SSLfatal() already called */
2757
0
                return 0;
2758
0
            }
2759
0
        } else {
2760
0
            if (!WPACKET_put_bytes_u8(pkt, 0)) {
2761
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2762
0
                return 0;
2763
0
            }
2764
0
        }
2765
2766
0
        if (!tls_construct_extensions(s, pkt,
2767
0
                SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,
2768
0
                0)) {
2769
            /* SSLfatal() already called */
2770
0
            return 0;
2771
0
        }
2772
0
        goto done;
2773
0
    }
2774
2775
    /* get the list of acceptable cert types */
2776
0
    if (!WPACKET_start_sub_packet_u8(pkt)
2777
0
        || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
2778
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2779
0
        return 0;
2780
0
    }
2781
2782
0
    if (SSL_USE_SIGALGS(s)) {
2783
0
        const uint16_t *psigs;
2784
0
        size_t nl = tls12_get_psigalgs(s, 1, &psigs);
2785
2786
0
        if (!WPACKET_start_sub_packet_u16(pkt)
2787
0
            || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
2788
0
            || !tls12_copy_sigalgs(s, pkt, psigs, nl)
2789
0
            || !WPACKET_close(pkt)) {
2790
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2791
0
            return 0;
2792
0
        }
2793
0
    }
2794
2795
0
    if (!construct_ca_names(s, get_ca_names(s), pkt)) {
2796
        /* SSLfatal() already called */
2797
0
        return 0;
2798
0
    }
2799
2800
0
done:
2801
0
    s->certreqs_sent++;
2802
0
    s->s3.tmp.cert_request = 1;
2803
0
    return 1;
2804
0
}
2805
2806
static int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt)
2807
0
{
2808
0
#ifndef OPENSSL_NO_PSK
2809
0
    unsigned char psk[PSK_MAX_PSK_LEN];
2810
0
    size_t psklen;
2811
0
    PACKET psk_identity;
2812
2813
0
    if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
2814
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2815
0
        return 0;
2816
0
    }
2817
0
    if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2818
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DATA_LENGTH_TOO_LONG);
2819
0
        return 0;
2820
0
    }
2821
0
    if (s->psk_server_callback == NULL) {
2822
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_SERVER_CB);
2823
0
        return 0;
2824
0
    }
2825
2826
0
    if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2827
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2828
0
        return 0;
2829
0
    }
2830
2831
0
    psklen = s->psk_server_callback(s, s->session->psk_identity,
2832
0
        psk, sizeof(psk));
2833
2834
0
    if (psklen > PSK_MAX_PSK_LEN) {
2835
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2836
0
        return 0;
2837
0
    } else if (psklen == 0) {
2838
        /*
2839
         * PSK related to the given identity not found
2840
         */
2841
0
        SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY, SSL_R_PSK_IDENTITY_NOT_FOUND);
2842
0
        return 0;
2843
0
    }
2844
2845
0
    OPENSSL_free(s->s3.tmp.psk);
2846
0
    s->s3.tmp.psk = OPENSSL_memdup(psk, psklen);
2847
0
    OPENSSL_cleanse(psk, psklen);
2848
2849
0
    if (s->s3.tmp.psk == NULL) {
2850
0
        s->s3.tmp.psklen = 0;
2851
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2852
0
        return 0;
2853
0
    }
2854
2855
0
    s->s3.tmp.psklen = psklen;
2856
2857
0
    return 1;
2858
#else
2859
    /* Should never happen */
2860
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2861
    return 0;
2862
#endif
2863
0
}
2864
2865
static int tls_process_cke_rsa(SSL *s, PACKET *pkt)
2866
9.26k
{
2867
9.26k
    size_t outlen;
2868
9.26k
    PACKET enc_premaster;
2869
9.26k
    EVP_PKEY *rsa = NULL;
2870
9.26k
    unsigned char *rsa_decrypt = NULL;
2871
9.26k
    int ret = 0;
2872
9.26k
    EVP_PKEY_CTX *ctx = NULL;
2873
9.26k
    OSSL_PARAM params[3], *p = params;
2874
2875
9.26k
    rsa = s->cert->pkeys[SSL_PKEY_RSA].privatekey;
2876
9.26k
    if (rsa == NULL) {
2877
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_RSA_CERTIFICATE);
2878
0
        return 0;
2879
0
    }
2880
2881
    /* SSLv3 and pre-standard DTLS omit the length bytes. */
2882
9.26k
    if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2883
0
        enc_premaster = *pkt;
2884
9.26k
    } else {
2885
9.26k
        if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
2886
9.12k
            || PACKET_remaining(pkt) != 0) {
2887
211
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2888
211
            return 0;
2889
211
        }
2890
9.26k
    }
2891
2892
9.05k
    outlen = SSL_MAX_MASTER_KEY_LENGTH;
2893
9.05k
    rsa_decrypt = OPENSSL_malloc(outlen);
2894
9.05k
    if (rsa_decrypt == NULL) {
2895
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2896
0
        return 0;
2897
0
    }
2898
2899
9.05k
    ctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx, rsa, s->ctx->propq);
2900
9.05k
    if (ctx == NULL) {
2901
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
2902
0
        goto err;
2903
0
    }
2904
2905
    /*
2906
     * We must not leak whether a decryption failure occurs because of
2907
     * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2908
     * section 7.4.7.1). We use the special padding type
2909
     * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automaticaly decrypt the
2910
     * RSA, check the padding and check that the client version is as expected
2911
     * in the premaster secret. If any of that fails then the function appears
2912
     * to return successfully but with a random result. The call below could
2913
     * still fail if the input is publicly invalid.
2914
     * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2915
     */
2916
9.05k
    if (EVP_PKEY_decrypt_init(ctx) <= 0
2917
9.05k
        || EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_WITH_TLS_PADDING) <= 0) {
2918
0
        SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
2919
0
        goto err;
2920
0
    }
2921
2922
9.05k
    *p++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION,
2923
9.05k
        (unsigned int *)&s->client_version);
2924
9.05k
    if ((s->options & SSL_OP_TLS_ROLLBACK_BUG) != 0)
2925
0
        *p++ = OSSL_PARAM_construct_uint(
2926
0
            OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION,
2927
0
            (unsigned int *)&s->version);
2928
9.05k
    *p++ = OSSL_PARAM_construct_end();
2929
2930
9.05k
    if (!EVP_PKEY_CTX_set_params(ctx, params)
2931
9.05k
        || EVP_PKEY_decrypt(ctx, rsa_decrypt, &outlen,
2932
9.05k
               PACKET_data(&enc_premaster),
2933
9.05k
               PACKET_remaining(&enc_premaster))
2934
9.05k
            <= 0) {
2935
34
        SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
2936
34
        goto err;
2937
34
    }
2938
2939
    /*
2940
     * This test should never fail (otherwise we should have failed above) but
2941
     * we double check anyway.
2942
     */
2943
9.01k
    if (outlen != SSL_MAX_MASTER_KEY_LENGTH) {
2944
0
        OPENSSL_cleanse(rsa_decrypt, SSL_MAX_MASTER_KEY_LENGTH);
2945
0
        SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED);
2946
0
        goto err;
2947
0
    }
2948
2949
    /* Also cleanses rsa_decrypt (on success or failure) */
2950
9.01k
    if (!ssl_generate_master_secret(s, rsa_decrypt,
2951
9.01k
            SSL_MAX_MASTER_KEY_LENGTH, 0)) {
2952
        /* SSLfatal() already called */
2953
0
        goto err;
2954
0
    }
2955
2956
9.01k
    ret = 1;
2957
9.05k
err:
2958
9.05k
    OPENSSL_free(rsa_decrypt);
2959
9.05k
    EVP_PKEY_CTX_free(ctx);
2960
9.05k
    return ret;
2961
9.01k
}
2962
2963
static int tls_process_cke_dhe(SSL *s, PACKET *pkt)
2964
0
{
2965
0
    EVP_PKEY *skey = NULL;
2966
0
    unsigned int i;
2967
0
    const unsigned char *data;
2968
0
    EVP_PKEY *ckey = NULL;
2969
0
    int ret = 0;
2970
2971
0
    if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
2972
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2973
0
        goto err;
2974
0
    }
2975
0
    skey = s->s3.tmp.pkey;
2976
0
    if (skey == NULL) {
2977
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY);
2978
0
        goto err;
2979
0
    }
2980
2981
0
    if (PACKET_remaining(pkt) == 0L) {
2982
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_MISSING_TMP_DH_KEY);
2983
0
        goto err;
2984
0
    }
2985
0
    if (!PACKET_get_bytes(pkt, &data, i)) {
2986
        /* We already checked we have enough data */
2987
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2988
0
        goto err;
2989
0
    }
2990
0
    ckey = EVP_PKEY_new();
2991
0
    if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
2992
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
2993
0
        goto err;
2994
0
    }
2995
2996
0
    if (!EVP_PKEY_set1_encoded_public_key(ckey, data, i)) {
2997
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
2998
0
        goto err;
2999
0
    }
3000
3001
0
    if (ssl_derive(s, skey, ckey, 1) == 0) {
3002
        /* SSLfatal() already called */
3003
0
        goto err;
3004
0
    }
3005
3006
0
    ret = 1;
3007
0
    EVP_PKEY_free(s->s3.tmp.pkey);
3008
0
    s->s3.tmp.pkey = NULL;
3009
0
err:
3010
0
    EVP_PKEY_free(ckey);
3011
0
    return ret;
3012
0
}
3013
3014
static int tls_process_cke_ecdhe(SSL *s, PACKET *pkt)
3015
4.57k
{
3016
4.57k
    EVP_PKEY *skey = s->s3.tmp.pkey;
3017
4.57k
    EVP_PKEY *ckey = NULL;
3018
4.57k
    int ret = 0;
3019
3020
4.57k
    if (PACKET_remaining(pkt) == 0L) {
3021
        /* We don't support ECDH client auth */
3022
35
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_TMP_ECDH_KEY);
3023
35
        goto err;
3024
4.54k
    } else {
3025
4.54k
        unsigned int i;
3026
4.54k
        const unsigned char *data;
3027
3028
        /*
3029
         * Get client's public key from encoded point in the
3030
         * ClientKeyExchange message.
3031
         */
3032
3033
        /* Get encoded point length */
3034
4.54k
        if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
3035
4.47k
            || PACKET_remaining(pkt) != 0) {
3036
127
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3037
127
            goto err;
3038
127
        }
3039
4.41k
        if (skey == NULL) {
3040
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_ECDH_KEY);
3041
0
            goto err;
3042
0
        }
3043
3044
4.41k
        ckey = EVP_PKEY_new();
3045
4.41k
        if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
3046
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED);
3047
0
            goto err;
3048
0
        }
3049
3050
4.41k
        if (EVP_PKEY_set1_encoded_public_key(ckey, data, i) <= 0) {
3051
822
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE);
3052
822
            goto err;
3053
822
        }
3054
4.41k
    }
3055
3056
3.59k
    if (ssl_derive(s, skey, ckey, 1) == 0) {
3057
        /* SSLfatal() already called */
3058
29
        goto err;
3059
29
    }
3060
3061
3.56k
    ret = 1;
3062
3.56k
    EVP_PKEY_free(s->s3.tmp.pkey);
3063
3.56k
    s->s3.tmp.pkey = NULL;
3064
4.57k
err:
3065
4.57k
    EVP_PKEY_free(ckey);
3066
3067
4.57k
    return ret;
3068
3.56k
}
3069
3070
static int tls_process_cke_srp(SSL *s, PACKET *pkt)
3071
0
{
3072
0
#ifndef OPENSSL_NO_SRP
3073
0
    unsigned int i;
3074
0
    const unsigned char *data;
3075
3076
0
    if (!PACKET_get_net_2(pkt, &i)
3077
0
        || !PACKET_get_bytes(pkt, &data, i)) {
3078
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRP_A_LENGTH);
3079
0
        return 0;
3080
0
    }
3081
0
    if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
3082
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
3083
0
        return 0;
3084
0
    }
3085
0
    if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
3086
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRP_PARAMETERS);
3087
0
        return 0;
3088
0
    }
3089
0
    OPENSSL_free(s->session->srp_username);
3090
0
    s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
3091
0
    if (s->session->srp_username == NULL) {
3092
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3093
0
        return 0;
3094
0
    }
3095
3096
0
    if (!srp_generate_server_master_secret(s)) {
3097
        /* SSLfatal() already called */
3098
0
        return 0;
3099
0
    }
3100
3101
0
    return 1;
3102
#else
3103
    /* Should never happen */
3104
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3105
    return 0;
3106
#endif
3107
0
}
3108
3109
static int tls_process_cke_gost(SSL *s, PACKET *pkt)
3110
0
{
3111
0
#ifndef OPENSSL_NO_GOST
3112
0
    EVP_PKEY_CTX *pkey_ctx;
3113
0
    EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
3114
0
    unsigned char premaster_secret[32];
3115
0
    const unsigned char *start;
3116
0
    size_t outlen = 32, inlen;
3117
0
    unsigned long alg_a;
3118
0
    GOST_KX_MESSAGE *pKX = NULL;
3119
0
    const unsigned char *ptr;
3120
0
    int ret = 0;
3121
3122
    /* Get our certificate private key */
3123
0
    alg_a = s->s3.tmp.new_cipher->algorithm_auth;
3124
0
    if (alg_a & SSL_aGOST12) {
3125
        /*
3126
         * New GOST ciphersuites have SSL_aGOST01 bit too
3127
         */
3128
0
        pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
3129
0
        if (pk == NULL) {
3130
0
            pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
3131
0
        }
3132
0
        if (pk == NULL) {
3133
0
            pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
3134
0
        }
3135
0
    } else if (alg_a & SSL_aGOST01) {
3136
0
        pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
3137
0
    }
3138
3139
0
    pkey_ctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx, pk, s->ctx->propq);
3140
0
    if (pkey_ctx == NULL) {
3141
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3142
0
        return 0;
3143
0
    }
3144
0
    if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
3145
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3146
0
        goto err;
3147
0
    }
3148
    /*
3149
     * If client certificate is present and is of the same type, maybe
3150
     * use it for key exchange.  Don't mind errors from
3151
     * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3152
     * client certificate for authorization only.
3153
     */
3154
0
    client_pub_pkey = X509_get0_pubkey(s->session->peer);
3155
0
    if (client_pub_pkey) {
3156
0
        if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
3157
0
            ERR_clear_error();
3158
0
    }
3159
3160
0
    ptr = PACKET_data(pkt);
3161
    /* Some implementations provide extra data in the opaqueBlob
3162
     * We have nothing to do with this blob so we just skip it */
3163
0
    pKX = d2i_GOST_KX_MESSAGE(NULL, &ptr, PACKET_remaining(pkt));
3164
0
    if (pKX == NULL
3165
0
        || pKX->kxBlob == NULL
3166
0
        || ASN1_TYPE_get(pKX->kxBlob) != V_ASN1_SEQUENCE) {
3167
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
3168
0
        goto err;
3169
0
    }
3170
3171
0
    if (!PACKET_forward(pkt, ptr - PACKET_data(pkt))) {
3172
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);
3173
0
        goto err;
3174
0
    }
3175
3176
0
    if (PACKET_remaining(pkt) != 0) {
3177
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED);
3178
0
        goto err;
3179
0
    }
3180
3181
0
    inlen = pKX->kxBlob->value.sequence->length;
3182
0
    start = pKX->kxBlob->value.sequence->data;
3183
3184
0
    if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start,
3185
0
            inlen)
3186
0
        <= 0) {
3187
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
3188
0
        goto err;
3189
0
    }
3190
    /* Generate master secret */
3191
0
    if (!ssl_generate_master_secret(s, premaster_secret,
3192
0
            sizeof(premaster_secret), 0)) {
3193
        /* SSLfatal() already called */
3194
0
        goto err;
3195
0
    }
3196
    /* Check if pubkey from client certificate was used */
3197
0
    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,
3198
0
            NULL)
3199
0
        > 0)
3200
0
        s->statem.no_cert_verify = 1;
3201
3202
0
    ret = 1;
3203
0
err:
3204
0
    EVP_PKEY_CTX_free(pkey_ctx);
3205
0
    GOST_KX_MESSAGE_free(pKX);
3206
0
    return ret;
3207
#else
3208
    /* Should never happen */
3209
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3210
    return 0;
3211
#endif
3212
0
}
3213
3214
static int tls_process_cke_gost18(SSL *s, PACKET *pkt)
3215
0
{
3216
0
#ifndef OPENSSL_NO_GOST
3217
0
    unsigned char rnd_dgst[32];
3218
0
    EVP_PKEY_CTX *pkey_ctx = NULL;
3219
0
    EVP_PKEY *pk = NULL;
3220
0
    unsigned char premaster_secret[32];
3221
0
    const unsigned char *start = NULL;
3222
0
    size_t outlen = 32, inlen = 0;
3223
0
    int ret = 0;
3224
0
    int cipher_nid = ossl_gost18_cke_cipher_nid(s);
3225
3226
0
    if (cipher_nid == NID_undef) {
3227
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3228
0
        return 0;
3229
0
    }
3230
3231
0
    if (ossl_gost_ukm(s, rnd_dgst) <= 0) {
3232
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3233
0
        goto err;
3234
0
    }
3235
3236
    /* Get our certificate private key */
3237
0
    pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey != NULL ? s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey : s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
3238
0
    if (pk == NULL) {
3239
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
3240
0
        goto err;
3241
0
    }
3242
3243
0
    pkey_ctx = EVP_PKEY_CTX_new_from_pkey(s->ctx->libctx, pk, s->ctx->propq);
3244
0
    if (pkey_ctx == NULL) {
3245
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3246
0
        goto err;
3247
0
    }
3248
0
    if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
3249
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3250
0
        goto err;
3251
0
    }
3252
3253
    /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code depending on size */
3254
0
    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,
3255
0
            EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst)
3256
0
        <= 0) {
3257
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3258
0
        goto err;
3259
0
    }
3260
3261
0
    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT,
3262
0
            EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL)
3263
0
        <= 0) {
3264
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3265
0
        goto err;
3266
0
    }
3267
0
    inlen = PACKET_remaining(pkt);
3268
0
    start = PACKET_data(pkt);
3269
3270
0
    if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
3271
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED);
3272
0
        goto err;
3273
0
    }
3274
    /* Generate master secret */
3275
0
    if (!ssl_generate_master_secret(s, premaster_secret,
3276
0
            sizeof(premaster_secret), 0)) {
3277
        /* SSLfatal() already called */
3278
0
        goto err;
3279
0
    }
3280
0
    ret = 1;
3281
3282
0
err:
3283
0
    EVP_PKEY_CTX_free(pkey_ctx);
3284
0
    return ret;
3285
#else
3286
    /* Should never happen */
3287
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3288
    return 0;
3289
#endif
3290
0
}
3291
3292
MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
3293
19.0k
{
3294
19.0k
    unsigned long alg_k;
3295
3296
19.0k
    alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
3297
3298
    /* For PSK parse and retrieve identity, obtain PSK key */
3299
19.0k
    if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) {
3300
        /* SSLfatal() already called */
3301
0
        goto err;
3302
0
    }
3303
3304
19.0k
    if (alg_k & SSL_kPSK) {
3305
        /* Identity extracted earlier: should be nothing left */
3306
0
        if (PACKET_remaining(pkt) != 0) {
3307
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3308
0
            goto err;
3309
0
        }
3310
        /* PSK handled by ssl_generate_master_secret */
3311
0
        if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
3312
            /* SSLfatal() already called */
3313
0
            goto err;
3314
0
        }
3315
19.0k
    } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3316
11.6k
        if (!tls_process_cke_rsa(s, pkt)) {
3317
            /* SSLfatal() already called */
3318
302
            goto err;
3319
302
        }
3320
11.6k
    } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
3321
0
        if (!tls_process_cke_dhe(s, pkt)) {
3322
            /* SSLfatal() already called */
3323
0
            goto err;
3324
0
        }
3325
7.35k
    } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
3326
7.35k
        if (!tls_process_cke_ecdhe(s, pkt)) {
3327
            /* SSLfatal() already called */
3328
1.76k
            goto err;
3329
1.76k
        }
3330
7.35k
    } else if (alg_k & SSL_kSRP) {
3331
0
        if (!tls_process_cke_srp(s, pkt)) {
3332
            /* SSLfatal() already called */
3333
0
            goto err;
3334
0
        }
3335
0
    } else if (alg_k & SSL_kGOST) {
3336
0
        if (!tls_process_cke_gost(s, pkt)) {
3337
            /* SSLfatal() already called */
3338
0
            goto err;
3339
0
        }
3340
0
    } else if (alg_k & SSL_kGOST18) {
3341
0
        if (!tls_process_cke_gost18(s, pkt)) {
3342
            /* SSLfatal() already called */
3343
0
            goto err;
3344
0
        }
3345
0
    } else {
3346
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_CIPHER_TYPE);
3347
0
        goto err;
3348
0
    }
3349
3350
16.9k
    return MSG_PROCESS_CONTINUE_PROCESSING;
3351
2.06k
err:
3352
2.06k
#ifndef OPENSSL_NO_PSK
3353
2.06k
    OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);
3354
2.06k
    s->s3.tmp.psk = NULL;
3355
2.06k
    s->s3.tmp.psklen = 0;
3356
2.06k
#endif
3357
2.06k
    return MSG_PROCESS_ERROR;
3358
19.0k
}
3359
3360
WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
3361
16.9k
{
3362
#ifndef OPENSSL_NO_SCTP
3363
    if (wst == WORK_MORE_A) {
3364
        if (SSL_IS_DTLS(s)) {
3365
            unsigned char sctpauthkey[64];
3366
            char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
3367
            size_t labellen;
3368
            /*
3369
             * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3370
             * used.
3371
             */
3372
            memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
3373
                sizeof(DTLS1_SCTP_AUTH_LABEL));
3374
3375
            /* Don't include the terminating zero. */
3376
            labellen = sizeof(labelbuffer) - 1;
3377
            if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
3378
                labellen += 1;
3379
3380
            if (SSL_export_keying_material(s, sctpauthkey,
3381
                    sizeof(sctpauthkey), labelbuffer,
3382
                    labellen, NULL, 0,
3383
                    0)
3384
                <= 0) {
3385
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3386
                return WORK_ERROR;
3387
            }
3388
3389
            BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
3390
                sizeof(sctpauthkey), sctpauthkey);
3391
        }
3392
    }
3393
#endif
3394
3395
16.9k
    if (s->statem.no_cert_verify || !s->session->peer) {
3396
        /*
3397
         * No certificate verify or no peer certificate so we no longer need
3398
         * the handshake_buffer
3399
         */
3400
16.9k
        if (!ssl3_digest_cached_records(s, 0)) {
3401
            /* SSLfatal() already called */
3402
0
            return WORK_ERROR;
3403
0
        }
3404
16.9k
        return WORK_FINISHED_CONTINUE;
3405
16.9k
    } else {
3406
0
        if (!s->s3.handshake_buffer) {
3407
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3408
0
            return WORK_ERROR;
3409
0
        }
3410
        /*
3411
         * For sigalgs freeze the handshake buffer. If we support
3412
         * extms we've done this already so this is a no-op
3413
         */
3414
0
        if (!ssl3_digest_cached_records(s, 1)) {
3415
            /* SSLfatal() already called */
3416
0
            return WORK_ERROR;
3417
0
        }
3418
0
    }
3419
3420
0
    return WORK_FINISHED_CONTINUE;
3421
16.9k
}
3422
3423
MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
3424
0
{
3425
0
    int i;
3426
0
    MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
3427
0
    X509 *x = NULL;
3428
0
    unsigned long l;
3429
0
    const unsigned char *certstart, *certbytes;
3430
0
    STACK_OF(X509) *sk = NULL;
3431
0
    PACKET spkt, context;
3432
0
    size_t chainidx;
3433
0
    SSL_SESSION *new_sess = NULL;
3434
3435
    /*
3436
     * To get this far we must have read encrypted data from the client. We no
3437
     * longer tolerate unencrypted alerts. This value is ignored if less than
3438
     * TLSv1.3
3439
     */
3440
0
    s->statem.enc_read_state = ENC_READ_STATE_VALID;
3441
3442
0
    if ((sk = sk_X509_new_null()) == NULL) {
3443
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3444
0
        goto err;
3445
0
    }
3446
3447
0
    if (SSL_IS_TLS13(s) && (!PACKET_get_length_prefixed_1(pkt, &context) || (s->pha_context == NULL && PACKET_remaining(&context) != 0) || (s->pha_context != NULL && !PACKET_equal(&context, s->pha_context, s->pha_context_len)))) {
3448
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT);
3449
0
        goto err;
3450
0
    }
3451
3452
0
    if (!PACKET_get_length_prefixed_3(pkt, &spkt)
3453
0
        || PACKET_remaining(pkt) != 0) {
3454
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3455
0
        goto err;
3456
0
    }
3457
3458
0
    for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
3459
0
        if (!PACKET_get_net_3(&spkt, &l)
3460
0
            || !PACKET_get_bytes(&spkt, &certbytes, l)) {
3461
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
3462
0
            goto err;
3463
0
        }
3464
3465
0
        certstart = certbytes;
3466
0
        x = X509_new_ex(s->ctx->libctx, s->ctx->propq);
3467
0
        if (x == NULL) {
3468
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_MALLOC_FAILURE);
3469
0
            goto err;
3470
0
        }
3471
0
        if (d2i_X509(&x, (const unsigned char **)&certbytes, l) == NULL) {
3472
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB);
3473
0
            goto err;
3474
0
        }
3475
3476
0
        if (certbytes != (certstart + l)) {
3477
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
3478
0
            goto err;
3479
0
        }
3480
3481
0
        if (SSL_IS_TLS13(s)) {
3482
0
            RAW_EXTENSION *rawexts = NULL;
3483
0
            PACKET extensions;
3484
3485
0
            if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
3486
0
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
3487
0
                goto err;
3488
0
            }
3489
0
            if (!tls_collect_extensions(s, &extensions,
3490
0
                    SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
3491
0
                    NULL, chainidx == 0)
3492
0
                || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
3493
0
                    rawexts, x, chainidx,
3494
0
                    PACKET_remaining(&spkt) == 0)) {
3495
0
                OPENSSL_free(rawexts);
3496
0
                goto err;
3497
0
            }
3498
0
            OPENSSL_free(rawexts);
3499
0
        }
3500
3501
0
        if (!sk_X509_push(sk, x)) {
3502
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3503
0
            goto err;
3504
0
        }
3505
0
        x = NULL;
3506
0
    }
3507
3508
0
    if (sk_X509_num(sk) <= 0) {
3509
        /* TLS does not mind 0 certs returned */
3510
0
        if (s->version == SSL3_VERSION) {
3511
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3512
0
                SSL_R_NO_CERTIFICATES_RETURNED);
3513
0
            goto err;
3514
0
        }
3515
        /* Fail for TLS only if we required a certificate */
3516
0
        else if ((s->verify_mode & SSL_VERIFY_PEER) && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3517
0
            SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED,
3518
0
                SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3519
0
            goto err;
3520
0
        }
3521
        /* No client certificate so digest cached records */
3522
0
        if (s->s3.handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
3523
            /* SSLfatal() already called */
3524
0
            goto err;
3525
0
        }
3526
0
    } else {
3527
0
        EVP_PKEY *pkey;
3528
0
        i = ssl_verify_cert_chain(s, sk);
3529
0
        if (i <= 0) {
3530
0
            SSLfatal(s, ssl_x509err2alert(s->verify_result),
3531
0
                SSL_R_CERTIFICATE_VERIFY_FAILED);
3532
0
            goto err;
3533
0
        }
3534
0
        pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
3535
0
        if (pkey == NULL) {
3536
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3537
0
                SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3538
0
            goto err;
3539
0
        }
3540
0
    }
3541
3542
    /*
3543
     * Sessions must be immutable once they go into the session cache. Otherwise
3544
     * we can get multi-thread problems. Therefore we don't "update" sessions,
3545
     * we replace them with a duplicate. Here, we need to do this every time
3546
     * a new certificate is received via post-handshake authentication, as the
3547
     * session may have already gone into the session cache.
3548
     */
3549
3550
0
    if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
3551
0
        if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
3552
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3553
0
            goto err;
3554
0
        }
3555
3556
0
        SSL_SESSION_free(s->session);
3557
0
        s->session = new_sess;
3558
0
    }
3559
3560
0
    X509_free(s->session->peer);
3561
0
    s->session->peer = sk_X509_shift(sk);
3562
0
    s->session->verify_result = s->verify_result;
3563
3564
0
    sk_X509_pop_free(s->session->peer_chain, X509_free);
3565
0
    s->session->peer_chain = sk;
3566
0
    sk = NULL;
3567
3568
    /*
3569
     * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3570
     * message
3571
     */
3572
0
    if (SSL_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
3573
        /* SSLfatal() already called */
3574
0
        goto err;
3575
0
    }
3576
3577
    /*
3578
     * Inconsistency alert: cert_chain does *not* include the peer's own
3579
     * certificate, while we do include it in statem_clnt.c
3580
     */
3581
3582
    /* Save the current hash state for when we receive the CertificateVerify */
3583
0
    if (SSL_IS_TLS13(s)) {
3584
0
        if (!ssl_handshake_hash(s, s->cert_verify_hash,
3585
0
                sizeof(s->cert_verify_hash),
3586
0
                &s->cert_verify_hash_len)) {
3587
            /* SSLfatal() already called */
3588
0
            goto err;
3589
0
        }
3590
3591
        /* Resend session tickets */
3592
0
        s->sent_tickets = 0;
3593
0
    }
3594
3595
0
    ret = MSG_PROCESS_CONTINUE_READING;
3596
3597
0
err:
3598
0
    X509_free(x);
3599
0
    sk_X509_pop_free(sk, X509_free);
3600
0
    return ret;
3601
0
}
3602
3603
int tls_construct_server_certificate(SSL *s, WPACKET *pkt)
3604
2.26k
{
3605
2.26k
    CERT_PKEY *cpk = s->s3.tmp.cert;
3606
3607
2.26k
    if (cpk == NULL) {
3608
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3609
0
        return 0;
3610
0
    }
3611
3612
    /*
3613
     * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3614
     * for the server Certificate message
3615
     */
3616
2.26k
    if (SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) {
3617
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3618
0
        return 0;
3619
0
    }
3620
2.26k
    if (!ssl3_output_cert_chain(s, pkt, cpk)) {
3621
        /* SSLfatal() already called */
3622
0
        return 0;
3623
0
    }
3624
3625
2.26k
    return 1;
3626
2.26k
}
3627
3628
static int create_ticket_prequel(SSL *s, WPACKET *pkt, uint32_t age_add,
3629
    unsigned char *tick_nonce)
3630
37
{
3631
37
    uint32_t timeout = (uint32_t)s->session->timeout;
3632
3633
    /*
3634
     * Ticket lifetime hint:
3635
     * In TLSv1.3 we reset the "time" field above, and always specify the
3636
     * timeout, limited to a 1 week period per RFC8446.
3637
     * For TLSv1.2 this is advisory only and we leave this unspecified for
3638
     * resumed session (for simplicity).
3639
     */
3640
37
#define ONE_WEEK_SEC (7 * 24 * 60 * 60)
3641
3642
37
    if (SSL_IS_TLS13(s)) {
3643
0
        if (s->session->timeout > ONE_WEEK_SEC)
3644
0
            timeout = ONE_WEEK_SEC;
3645
37
    } else if (s->hit)
3646
0
        timeout = 0;
3647
3648
37
    if (!WPACKET_put_bytes_u32(pkt, timeout)) {
3649
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3650
0
        return 0;
3651
0
    }
3652
3653
37
    if (SSL_IS_TLS13(s)) {
3654
0
        if (!WPACKET_put_bytes_u32(pkt, age_add)
3655
0
            || !WPACKET_sub_memcpy_u8(pkt, tick_nonce, TICKET_NONCE_SIZE)) {
3656
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3657
0
            return 0;
3658
0
        }
3659
0
    }
3660
3661
    /* Start the sub-packet for the actual ticket data */
3662
37
    if (!WPACKET_start_sub_packet_u16(pkt)) {
3663
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3664
0
        return 0;
3665
0
    }
3666
3667
37
    return 1;
3668
37
}
3669
3670
/*
3671
 * Returns 1 on success, 0 to abort construction of the ticket (non-fatal), or
3672
 * -1 on fatal error
3673
 */
3674
static int construct_stateless_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
3675
    unsigned char *tick_nonce)
3676
{
3677
    unsigned char *senc = NULL;
3678
    EVP_CIPHER_CTX *ctx = NULL;
3679
    SSL_HMAC *hctx = NULL;
3680
    unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
3681
    const unsigned char *const_p;
3682
    int len, slen_full, slen, lenfinal;
3683
    SSL_SESSION *sess;
3684
    size_t hlen;
3685
    SSL_CTX *tctx = s->session_ctx;
3686
    unsigned char iv[EVP_MAX_IV_LENGTH];
3687
    unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
3688
    int iv_len, ok = -1;
3689
    size_t macoffset, macendoffset;
3690
3691
    /* get session encoding length */
3692
    slen_full = i2d_SSL_SESSION(s->session, NULL);
3693
    /*
3694
     * Some length values are 16 bits, so forget it if session is too
3695
     * long
3696
     */
3697
    if (slen_full == 0 || slen_full > 0xFF00) {
3698
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3699
        goto err;
3700
    }
3701
    senc = OPENSSL_malloc(slen_full);
3702
    if (senc == NULL) {
3703
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3704
        goto err;
3705
    }
3706
3707
    ctx = EVP_CIPHER_CTX_new();
3708
    hctx = ssl_hmac_new(tctx);
3709
    if (ctx == NULL || hctx == NULL) {
3710
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3711
        goto err;
3712
    }
3713
3714
    p = senc;
3715
    if (!i2d_SSL_SESSION(s->session, &p)) {
3716
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3717
        goto err;
3718
    }
3719
3720
    /*
3721
     * create a fresh copy (not shared with other threads) to clean up
3722
     */
3723
    const_p = senc;
3724
    sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3725
    if (sess == NULL) {
3726
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3727
        goto err;
3728
    }
3729
3730
    slen = i2d_SSL_SESSION(sess, NULL);
3731
    if (slen == 0 || slen > slen_full) {
3732
        /* shouldn't ever happen */
3733
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3734
        SSL_SESSION_free(sess);
3735
        goto err;
3736
    }
3737
    p = senc;
3738
    if (!i2d_SSL_SESSION(sess, &p)) {
3739
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3740
        SSL_SESSION_free(sess);
3741
        goto err;
3742
    }
3743
    SSL_SESSION_free(sess);
3744
3745
    /*
3746
     * Initialize HMAC and cipher contexts. If callback present it does
3747
     * all the work otherwise use generated values from parent ctx.
3748
     */
3749
#ifndef OPENSSL_NO_DEPRECATED_3_0
3750
    if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL)
3751
#else
3752
    if (tctx->ext.ticket_key_evp_cb != NULL)
3753
#endif
3754
    {
3755
        int ret = 0;
3756
3757
        if (tctx->ext.ticket_key_evp_cb != NULL)
3758
            ret = tctx->ext.ticket_key_evp_cb(s, key_name, iv, ctx,
3759
                ssl_hmac_get0_EVP_MAC_CTX(hctx),
3760
                1);
3761
#ifndef OPENSSL_NO_DEPRECATED_3_0
3762
        else if (tctx->ext.ticket_key_cb != NULL)
3763
            /* if 0 is returned, write an empty ticket */
3764
            ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,
3765
                ssl_hmac_get0_HMAC_CTX(hctx), 1);
3766
#endif
3767
3768
        if (ret == 0) {
3769
            /*
3770
             * In TLSv1.2 we construct a 0 length ticket. In TLSv1.3 a 0
3771
             * length ticket is not allowed so we abort construction of the
3772
             * ticket
3773
             */
3774
            if (SSL_IS_TLS13(s)) {
3775
                ok = 0;
3776
                goto err;
3777
            }
3778
            /* Put timeout and length */
3779
            if (!WPACKET_put_bytes_u32(pkt, 0)
3780
                || !WPACKET_put_bytes_u16(pkt, 0)) {
3781
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3782
                goto err;
3783
            }
3784
            OPENSSL_free(senc);
3785
            EVP_CIPHER_CTX_free(ctx);
3786
            ssl_hmac_free(hctx);
3787
            return 1;
3788
        }
3789
        if (ret < 0) {
3790
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
3791
            goto err;
3792
        }
3793
        iv_len = EVP_CIPHER_CTX_get_iv_length(ctx);
3794
        if (iv_len < 0) {
3795
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3796
            goto err;
3797
        }
3798
    } else {
3799
        EVP_CIPHER *cipher = EVP_CIPHER_fetch(s->ctx->libctx, "AES-256-CBC",
3800
            s->ctx->propq);
3801
3802
        if (cipher == NULL) {
3803
            /* Error is already recorded */
3804
            SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
3805
            goto err;
3806
        }
3807
3808
        iv_len = EVP_CIPHER_get_iv_length(cipher);
3809
        if (iv_len < 0
3810
            || RAND_bytes_ex(s->ctx->libctx, iv, iv_len, 0) <= 0
3811
            || !EVP_EncryptInit_ex(ctx, cipher, NULL,
3812
                tctx->ext.secure->tick_aes_key, iv)
3813
            || !ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key,
3814
                sizeof(tctx->ext.secure->tick_hmac_key),
3815
                "SHA256")) {
3816
            EVP_CIPHER_free(cipher);
3817
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3818
            goto err;
3819
        }
3820
        EVP_CIPHER_free(cipher);
3821
        memcpy(key_name, tctx->ext.tick_key_name,
3822
            sizeof(tctx->ext.tick_key_name));
3823
    }
3824
3825
    if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
3826
        /* SSLfatal() already called */
3827
        goto err;
3828
    }
3829
3830
    if (!WPACKET_get_total_written(pkt, &macoffset)
3831
        /* Output key name */
3832
        || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
3833
        /* output IV */
3834
        || !WPACKET_memcpy(pkt, iv, iv_len)
3835
        || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
3836
            &encdata1)
3837
        /* Encrypt session data */
3838
        || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
3839
        || !WPACKET_allocate_bytes(pkt, len, &encdata2)
3840
        || encdata1 != encdata2
3841
        || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
3842
        || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
3843
        || encdata1 + len != encdata2
3844
        || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
3845
        || !WPACKET_get_total_written(pkt, &macendoffset)
3846
        || !ssl_hmac_update(hctx,
3847
            (unsigned char *)s->init_buf->data + macoffset,
3848
            macendoffset - macoffset)
3849
        || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
3850
        || !ssl_hmac_final(hctx, macdata1, &hlen, EVP_MAX_MD_SIZE)
3851
        || hlen > EVP_MAX_MD_SIZE
3852
        || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
3853
        || macdata1 != macdata2) {
3854
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3855
        goto err;
3856
    }
3857
3858
    /* Close the sub-packet created by create_ticket_prequel() */
3859
    if (!WPACKET_close(pkt)) {
3860
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3861
        goto err;
3862
    }
3863
3864
    ok = 1;
3865
err:
3866
    OPENSSL_free(senc);
3867
    EVP_CIPHER_CTX_free(ctx);
3868
    ssl_hmac_free(hctx);
3869
    return ok;
3870
}
3871
3872
static int construct_stateful_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
3873
    unsigned char *tick_nonce)
3874
0
{
3875
0
    if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
3876
        /* SSLfatal() already called */
3877
0
        return 0;
3878
0
    }
3879
3880
0
    if (!WPACKET_memcpy(pkt, s->session->session_id,
3881
0
            s->session->session_id_length)
3882
0
        || !WPACKET_close(pkt)) {
3883
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3884
0
        return 0;
3885
0
    }
3886
3887
0
    return 1;
3888
0
}
3889
3890
static void tls_update_ticket_counts(SSL *s)
3891
0
{
3892
    /*
3893
     * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
3894
     * gets reset to 0 if we send more tickets following a post-handshake
3895
     * auth, but |next_ticket_nonce| does not.  If we're sending extra
3896
     * tickets, decrement the count of pending extra tickets.
3897
     */
3898
0
    s->sent_tickets++;
3899
0
    s->next_ticket_nonce++;
3900
0
    if (s->ext.extra_tickets_expected > 0)
3901
0
        s->ext.extra_tickets_expected--;
3902
0
}
3903
3904
int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)
3905
4
{
3906
4
    SSL_CTX *tctx = s->session_ctx;
3907
4
    unsigned char tick_nonce[TICKET_NONCE_SIZE];
3908
4
    union {
3909
4
        unsigned char age_add_c[sizeof(uint32_t)];
3910
4
        uint32_t age_add;
3911
4
    } age_add_u;
3912
4
    int ret = 0;
3913
3914
4
    age_add_u.age_add = 0;
3915
3916
4
    if (SSL_IS_TLS13(s)) {
3917
0
        size_t i, hashlen;
3918
0
        uint64_t nonce;
3919
        /* ASCII: "resumption", in hex for EBCDIC compatibility */
3920
0
        static const unsigned char nonce_label[] = { 0x72, 0x65, 0x73, 0x75, 0x6D,
3921
0
            0x70, 0x74, 0x69, 0x6F, 0x6E };
3922
0
        const EVP_MD *md = ssl_handshake_md(s);
3923
0
        int hashleni = EVP_MD_get_size(md);
3924
3925
        /* Ensure cast to size_t is safe */
3926
0
        if (!ossl_assert(hashleni >= 0)) {
3927
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3928
0
            goto err;
3929
0
        }
3930
0
        hashlen = (size_t)hashleni;
3931
3932
        /*
3933
         * If we already sent one NewSessionTicket, or we resumed then
3934
         * s->session may already be in a cache and so we must not modify it.
3935
         * Instead we need to take a copy of it and modify that.
3936
         */
3937
0
        if (s->sent_tickets != 0 || s->hit) {
3938
0
            SSL_SESSION *new_sess = ssl_session_dup(s->session, 0);
3939
3940
0
            if (new_sess == NULL) {
3941
                /* SSLfatal already called */
3942
0
                goto err;
3943
0
            }
3944
3945
0
            SSL_SESSION_free(s->session);
3946
0
            s->session = new_sess;
3947
0
        }
3948
3949
0
        if (!ssl_generate_session_id(s, s->session)) {
3950
            /* SSLfatal() already called */
3951
0
            goto err;
3952
0
        }
3953
0
        if (RAND_bytes_ex(s->ctx->libctx, age_add_u.age_add_c,
3954
0
                sizeof(age_add_u), 0)
3955
0
            <= 0) {
3956
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3957
0
            goto err;
3958
0
        }
3959
0
        s->session->ext.tick_age_add = age_add_u.age_add;
3960
3961
0
        nonce = s->next_ticket_nonce;
3962
0
        for (i = TICKET_NONCE_SIZE; i > 0; i--) {
3963
0
            tick_nonce[i - 1] = (unsigned char)(nonce & 0xff);
3964
0
            nonce >>= 8;
3965
0
        }
3966
3967
0
        if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
3968
0
                nonce_label,
3969
0
                sizeof(nonce_label),
3970
0
                tick_nonce,
3971
0
                TICKET_NONCE_SIZE,
3972
0
                s->session->master_key,
3973
0
                hashlen, 1)) {
3974
            /* SSLfatal() already called */
3975
0
            goto err;
3976
0
        }
3977
0
        s->session->master_key_length = hashlen;
3978
3979
0
        s->session->time = time(NULL);
3980
0
        ssl_session_calculate_timeout(s->session);
3981
0
        if (s->s3.alpn_selected != NULL) {
3982
0
            OPENSSL_free(s->session->ext.alpn_selected);
3983
0
            s->session->ext.alpn_selected = OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len);
3984
0
            if (s->session->ext.alpn_selected == NULL) {
3985
0
                s->session->ext.alpn_selected_len = 0;
3986
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
3987
0
                goto err;
3988
0
            }
3989
0
            s->session->ext.alpn_selected_len = s->s3.alpn_selected_len;
3990
0
        }
3991
0
        s->session->ext.max_early_data = s->max_early_data;
3992
0
    }
3993
3994
4
    if (tctx->generate_ticket_cb != NULL && tctx->generate_ticket_cb(s, tctx->ticket_cb_data) == 0) {
3995
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3996
0
        goto err;
3997
0
    }
3998
    /*
3999
     * If we are using anti-replay protection then we behave as if
4000
     * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
4001
     * is no point in using full stateless tickets.
4002
     */
4003
4
    if (SSL_IS_TLS13(s)
4004
0
        && ((s->options & SSL_OP_NO_TICKET) != 0
4005
0
            || (s->max_early_data > 0
4006
0
                && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))) {
4007
0
        if (!construct_stateful_ticket(s, pkt, age_add_u.age_add, tick_nonce)) {
4008
            /* SSLfatal() already called */
4009
0
            goto err;
4010
0
        }
4011
4
    } else {
4012
4
        int tmpret;
4013
4014
4
        tmpret = construct_stateless_ticket(s, pkt, age_add_u.age_add,
4015
4
            tick_nonce);
4016
4
        if (tmpret != 1) {
4017
0
            if (tmpret == 0) {
4018
0
                ret = 2; /* Non-fatal. Abort construction but continue */
4019
                /* We count this as a success so update the counts anwyay */
4020
0
                tls_update_ticket_counts(s);
4021
0
            }
4022
            /* else SSLfatal() already called */
4023
0
            goto err;
4024
0
        }
4025
4
    }
4026
4027
4
    if (SSL_IS_TLS13(s)) {
4028
0
        if (!tls_construct_extensions(s, pkt,
4029
0
                SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
4030
0
                NULL, 0)) {
4031
            /* SSLfatal() already called */
4032
0
            goto err;
4033
0
        }
4034
0
        tls_update_ticket_counts(s);
4035
0
        ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
4036
0
    }
4037
4038
4
    ret = 1;
4039
4
err:
4040
4
    return ret;
4041
4
}
4042
4043
/*
4044
 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
4045
 * create a separate message. Returns 1 on success or 0 on failure.
4046
 */
4047
int tls_construct_cert_status_body(SSL *s, WPACKET *pkt)
4048
0
{
4049
0
    if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)
4050
0
        || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp,
4051
0
            s->ext.ocsp.resp_len)) {
4052
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4053
0
        return 0;
4054
0
    }
4055
4056
0
    return 1;
4057
0
}
4058
4059
int tls_construct_cert_status(SSL *s, WPACKET *pkt)
4060
0
{
4061
0
    if (!tls_construct_cert_status_body(s, pkt)) {
4062
        /* SSLfatal() already called */
4063
0
        return 0;
4064
0
    }
4065
4066
0
    return 1;
4067
0
}
4068
4069
#ifndef OPENSSL_NO_NEXTPROTONEG
4070
/*
4071
 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
4072
 * It sets the next_proto member in s if found
4073
 */
4074
MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
4075
0
{
4076
0
    PACKET next_proto, padding;
4077
0
    size_t next_proto_len;
4078
4079
    /*-
4080
     * The payload looks like:
4081
     *   uint8 proto_len;
4082
     *   uint8 proto[proto_len];
4083
     *   uint8 padding_len;
4084
     *   uint8 padding[padding_len];
4085
     */
4086
0
    if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
4087
0
        || !PACKET_get_length_prefixed_1(pkt, &padding)
4088
0
        || PACKET_remaining(pkt) > 0) {
4089
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
4090
0
        return MSG_PROCESS_ERROR;
4091
0
    }
4092
4093
0
    if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) {
4094
0
        s->ext.npn_len = 0;
4095
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4096
0
        return MSG_PROCESS_ERROR;
4097
0
    }
4098
4099
0
    s->ext.npn_len = (unsigned char)next_proto_len;
4100
4101
0
    return MSG_PROCESS_CONTINUE_READING;
4102
0
}
4103
#endif
4104
4105
static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt)
4106
2.99k
{
4107
2.99k
    if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
4108
2.99k
            NULL, 0)) {
4109
        /* SSLfatal() already called */
4110
0
        return 0;
4111
0
    }
4112
4113
2.99k
    return 1;
4114
2.99k
}
4115
4116
MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL *s, PACKET *pkt)
4117
0
{
4118
0
    if (PACKET_remaining(pkt) != 0) {
4119
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
4120
0
        return MSG_PROCESS_ERROR;
4121
0
    }
4122
4123
0
    if (s->early_data_state != SSL_EARLY_DATA_READING
4124
0
        && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) {
4125
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4126
0
        return MSG_PROCESS_ERROR;
4127
0
    }
4128
4129
    /*
4130
     * EndOfEarlyData signals a key change so the end of the message must be on
4131
     * a record boundary.
4132
     */
4133
0
    if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
4134
0
        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY);
4135
0
        return MSG_PROCESS_ERROR;
4136
0
    }
4137
4138
0
    s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
4139
0
    if (!s->method->ssl3_enc->change_cipher_state(s,
4140
0
            SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
4141
        /* SSLfatal() already called */
4142
0
        return MSG_PROCESS_ERROR;
4143
0
    }
4144
4145
0
    return MSG_PROCESS_CONTINUE_READING;
4146
0
}