Coverage Report

Created: 2025-06-13 06:58

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