Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/ssl/statem/statem_clnt.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4
 * Copyright 2005 Nokia. All rights reserved.
5
 *
6
 * Licensed under the Apache License 2.0 (the "License").  You may not use
7
 * this file except in compliance with the License.  You can obtain a copy
8
 * in the file LICENSE in the source distribution or at
9
 * https://www.openssl.org/source/license.html
10
 */
11
12
#include <stdio.h>
13
#include <time.h>
14
#include <assert.h>
15
#include "../ssl_local.h"
16
#include "statem_local.h"
17
#include <openssl/buffer.h>
18
#include <openssl/rand.h>
19
#include <openssl/objects.h>
20
#include <openssl/evp.h>
21
#include <openssl/md5.h>
22
#include <openssl/dh.h>
23
#include <openssl/rsa.h>
24
#include <openssl/bn.h>
25
#include <openssl/engine.h>
26
#include <openssl/trace.h>
27
#include <openssl/core_names.h>
28
#include <openssl/param_build.h>
29
#include "internal/cryptlib.h"
30
#include "internal/comp.h"
31
#include "internal/ssl_unwrap.h"
32
33
static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL_CONNECTION *s,
34
    PACKET *pkt);
35
static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL_CONNECTION *s,
36
    PACKET *pkt);
37
38
static ossl_inline int cert_req_allowed(SSL_CONNECTION *s);
39
static int key_exchange_expected(SSL_CONNECTION *s);
40
static int ssl_cipher_list_to_bytes(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *sk,
41
    WPACKET *pkt);
42
43
static ossl_inline int received_server_cert(SSL_CONNECTION *sc)
44
5.21k
{
45
5.21k
    return sc->session->peer_rpk != NULL || sc->session->peer != NULL;
46
5.21k
}
47
48
/*
49
 * Is a CertificateRequest message allowed at the moment or not?
50
 *
51
 *  Return values are:
52
 *  1: Yes
53
 *  0: No
54
 */
55
static ossl_inline int cert_req_allowed(SSL_CONNECTION *s)
56
700
{
57
    /* TLS does not like anon-DH with client cert */
58
700
    if ((s->version > SSL3_VERSION
59
655
            && (s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL))
60
689
        || (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
61
11
        return 0;
62
63
689
    return 1;
64
700
}
65
66
/*
67
 * Should we expect the ServerKeyExchange message or not?
68
 *
69
 *  Return values are:
70
 *  1: Yes
71
 *  0: No
72
 */
73
static int key_exchange_expected(SSL_CONNECTION *s)
74
26.1k
{
75
26.1k
    long alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
76
77
    /*
78
     * Can't skip server key exchange if this is an ephemeral
79
     * ciphersuite or for SRP
80
     */
81
26.1k
    if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK | SSL_kSRP)) {
82
19.8k
        return 1;
83
19.8k
    }
84
85
6.33k
    return 0;
86
26.1k
}
87
88
/*
89
 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
90
 * handshake state transitions when a TLS1.3 client is reading messages from the
91
 * server. The message type that the server has sent is provided in |mt|. The
92
 * current state is in |s->statem.hand_state|.
93
 *
94
 * Return values are 1 for success (transition allowed) and  0 on error
95
 * (transition not allowed)
96
 */
97
static int ossl_statem_client13_read_transition(SSL_CONNECTION *s, int mt)
98
82.6k
{
99
82.6k
    OSSL_STATEM *st = &s->statem;
100
101
    /*
102
     * Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't
103
     * yet negotiated TLSv1.3 at that point so that is handled by
104
     * ossl_statem_client_read_transition()
105
     */
106
107
82.6k
    switch (st->hand_state) {
108
0
    default:
109
0
        break;
110
111
0
    case TLS_ST_CW_CLNT_HELLO:
112
        /*
113
         * This must a ClientHello following a HelloRetryRequest, so the only
114
         * thing we can get now is a ServerHello.
115
         */
116
0
        if (mt == SSL3_MT_SERVER_HELLO) {
117
0
            st->hand_state = TLS_ST_CR_SRVR_HELLO;
118
0
            return 1;
119
0
        }
120
0
        break;
121
122
21.9k
    case TLS_ST_CR_SRVR_HELLO:
123
21.9k
        if (mt == SSL3_MT_ENCRYPTED_EXTENSIONS) {
124
21.8k
            st->hand_state = TLS_ST_CR_ENCRYPTED_EXTENSIONS;
125
21.8k
            return 1;
126
21.8k
        }
127
137
        break;
128
129
20.4k
    case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
130
20.4k
        if (s->hit) {
131
0
            if (mt == SSL3_MT_FINISHED) {
132
0
                st->hand_state = TLS_ST_CR_FINISHED;
133
0
                return 1;
134
0
            }
135
20.4k
        } else {
136
20.4k
            if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
137
168
                st->hand_state = TLS_ST_CR_CERT_REQ;
138
168
                return 1;
139
168
            }
140
20.2k
            if (mt == SSL3_MT_CERTIFICATE) {
141
20.2k
                st->hand_state = TLS_ST_CR_CERT;
142
20.2k
                return 1;
143
20.2k
            }
144
#ifndef OPENSSL_NO_COMP_ALG
145
            if (mt == SSL3_MT_COMPRESSED_CERTIFICATE
146
                && s->ext.compress_certificate_sent) {
147
                st->hand_state = TLS_ST_CR_COMP_CERT;
148
                return 1;
149
            }
150
#endif
151
20.2k
        }
152
20
        break;
153
154
20
    case TLS_ST_CR_CERT_REQ:
155
0
        if (mt == SSL3_MT_CERTIFICATE) {
156
0
            st->hand_state = TLS_ST_CR_CERT;
157
0
            return 1;
158
0
        }
159
#ifndef OPENSSL_NO_COMP_ALG
160
        if (mt == SSL3_MT_COMPRESSED_CERTIFICATE
161
            && s->ext.compress_certificate_sent) {
162
            st->hand_state = TLS_ST_CR_COMP_CERT;
163
            return 1;
164
        }
165
#endif
166
0
        break;
167
168
17.0k
    case TLS_ST_CR_CERT:
169
17.0k
    case TLS_ST_CR_COMP_CERT:
170
17.0k
        if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
171
16.7k
            st->hand_state = TLS_ST_CR_CERT_VRFY;
172
16.7k
            return 1;
173
16.7k
        }
174
360
        break;
175
176
16.0k
    case TLS_ST_CR_CERT_VRFY:
177
16.0k
        if (mt == SSL3_MT_FINISHED) {
178
12.4k
            st->hand_state = TLS_ST_CR_FINISHED;
179
12.4k
            return 1;
180
12.4k
        }
181
3.52k
        break;
182
183
7.17k
    case TLS_ST_OK:
184
7.17k
        if (mt == SSL3_MT_NEWSESSION_TICKET) {
185
6.99k
            st->hand_state = TLS_ST_CR_SESSION_TICKET;
186
6.99k
            return 1;
187
6.99k
        }
188
178
        if (mt == SSL3_MT_KEY_UPDATE && !SSL_IS_QUIC_HANDSHAKE(s)) {
189
0
            st->hand_state = TLS_ST_CR_KEY_UPDATE;
190
0
            return 1;
191
0
        }
192
178
        if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
193
#if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION
194
            /* Restore digest for PHA before adding message.*/
195
#error Internal DTLS version error
196
#endif
197
5
            if (!SSL_CONNECTION_IS_DTLS(s)
198
5
                && s->post_handshake_auth == SSL_PHA_EXT_SENT) {
199
0
                s->post_handshake_auth = SSL_PHA_REQUESTED;
200
                /*
201
                 * In TLS, this is called before the message is added to the
202
                 * digest. In DTLS, this is expected to be called after adding
203
                 * to the digest. Either move the digest restore, or add the
204
                 * message here after the swap, or do it after the clientFinished?
205
                 */
206
0
                if (!tls13_restore_handshake_digest_for_pha(s)) {
207
                    /* SSLfatal() already called */
208
0
                    return 0;
209
0
                }
210
0
                st->hand_state = TLS_ST_CR_CERT_REQ;
211
0
                return 1;
212
0
            }
213
5
        }
214
178
        break;
215
82.6k
    }
216
217
    /* No valid transition found */
218
4.22k
    return 0;
219
82.6k
}
220
221
/*
222
 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
223
 * handshake state transitions when the client is reading messages from the
224
 * server. The message type that the server has sent is provided in |mt|. The
225
 * current state is in |s->statem.hand_state|.
226
 *
227
 * Return values are 1 for success (transition allowed) and  0 on error
228
 * (transition not allowed)
229
 */
230
int ossl_statem_client_read_transition(SSL_CONNECTION *s, int mt)
231
227k
{
232
227k
    OSSL_STATEM *st = &s->statem;
233
227k
    int ske_expected;
234
235
    /*
236
     * Note that after writing the first ClientHello we don't know what version
237
     * we are going to negotiate yet, so we don't take this branch until later.
238
     */
239
227k
    if (SSL_CONNECTION_IS_TLS13(s)) {
240
82.6k
        if (!ossl_statem_client13_read_transition(s, mt))
241
4.22k
            goto err;
242
78.4k
        return 1;
243
82.6k
    }
244
245
145k
    switch (st->hand_state) {
246
0
    default:
247
0
        break;
248
249
78.3k
    case TLS_ST_CW_CLNT_HELLO:
250
78.3k
        if (mt == SSL3_MT_SERVER_HELLO) {
251
73.4k
            st->hand_state = TLS_ST_CR_SRVR_HELLO;
252
73.4k
            return 1;
253
73.4k
        }
254
255
4.86k
        if (SSL_CONNECTION_IS_DTLS(s)) {
256
4.27k
            if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
257
4.06k
                st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
258
4.06k
                return 1;
259
4.06k
            }
260
4.27k
        }
261
794
        break;
262
263
794
    case TLS_ST_EARLY_DATA:
264
        /*
265
         * We've not actually selected TLSv1.3 yet, but we have sent early
266
         * data. The only thing allowed now is a ServerHello or a
267
         * HelloRetryRequest.
268
         */
269
0
        if (mt == SSL3_MT_SERVER_HELLO) {
270
0
            st->hand_state = TLS_ST_CR_SRVR_HELLO;
271
0
            return 1;
272
0
        }
273
0
        break;
274
275
40.5k
    case TLS_ST_CR_SRVR_HELLO:
276
40.5k
        if (s->hit) {
277
0
            if (s->ext.ticket_expected) {
278
0
                if (mt == SSL3_MT_NEWSESSION_TICKET) {
279
0
                    st->hand_state = TLS_ST_CR_SESSION_TICKET;
280
0
                    return 1;
281
0
                }
282
0
            } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
283
0
                st->hand_state = TLS_ST_CR_CHANGE;
284
0
                return 1;
285
0
            }
286
40.5k
        } else {
287
40.5k
            if (SSL_CONNECTION_IS_DTLS(s)
288
13.8k
                && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
289
355
                st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
290
355
                return 1;
291
40.1k
            } else if (s->version >= TLS1_VERSION
292
38.8k
                && s->ext.session_secret_cb != NULL
293
0
                && s->session->ext.tick != NULL
294
0
                && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
295
                /*
296
                 * Normally, we can tell if the server is resuming the session
297
                 * from the session ID. EAP-FAST (RFC 4851), however, relies on
298
                 * the next server message after the ServerHello to determine if
299
                 * the server is resuming.
300
                 */
301
0
                s->hit = 1;
302
0
                st->hand_state = TLS_ST_CR_CHANGE;
303
0
                return 1;
304
40.1k
            } else if (!(s->s3.tmp.new_cipher->algorithm_auth
305
40.1k
                           & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
306
28.1k
                if (mt == SSL3_MT_CERTIFICATE) {
307
28.1k
                    st->hand_state = TLS_ST_CR_CERT;
308
28.1k
                    return 1;
309
28.1k
                }
310
28.1k
            } else {
311
11.9k
                ske_expected = key_exchange_expected(s);
312
                /* SKE is optional for some PSK ciphersuites */
313
11.9k
                if (ske_expected
314
0
                    || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)
315
11.9k
                        && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
316
11.9k
                    if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
317
11.9k
                        st->hand_state = TLS_ST_CR_KEY_EXCH;
318
11.9k
                        return 1;
319
11.9k
                    }
320
11.9k
                } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
321
0
                    && cert_req_allowed(s)) {
322
0
                    st->hand_state = TLS_ST_CR_CERT_REQ;
323
0
                    return 1;
324
0
                } else if (mt == SSL3_MT_SERVER_DONE) {
325
0
                    st->hand_state = TLS_ST_CR_SRVR_DONE;
326
0
                    return 1;
327
0
                }
328
11.9k
            }
329
40.5k
        }
330
138
        break;
331
332
11.2k
    case TLS_ST_CR_CERT:
333
11.2k
    case TLS_ST_CR_COMP_CERT:
334
        /*
335
         * The CertificateStatus message is optional even if
336
         * |ext.status_expected| is set
337
         */
338
11.2k
        if (s->ext.status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) {
339
0
            st->hand_state = TLS_ST_CR_CERT_STATUS;
340
0
            return 1;
341
0
        }
342
        /* Fall through */
343
344
11.2k
    case TLS_ST_CR_CERT_STATUS:
345
11.2k
        ske_expected = key_exchange_expected(s);
346
        /* SKE is optional for some PSK ciphersuites */
347
11.2k
        if (ske_expected || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK) && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
348
5.32k
            if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
349
5.23k
                st->hand_state = TLS_ST_CR_KEY_EXCH;
350
5.23k
                return 1;
351
5.23k
            }
352
90
            goto err;
353
5.32k
        }
354
        /* Fall through */
355
356
12.9k
    case TLS_ST_CR_KEY_EXCH:
357
12.9k
        if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
358
698
            if (cert_req_allowed(s)) {
359
686
                st->hand_state = TLS_ST_CR_CERT_REQ;
360
686
                return 1;
361
686
            }
362
12
            goto err;
363
698
        }
364
        /* Fall through */
365
366
12.2k
    case TLS_ST_CR_CERT_REQ:
367
12.2k
        if (mt == SSL3_MT_SERVER_DONE) {
368
11.8k
            st->hand_state = TLS_ST_CR_SRVR_DONE;
369
11.8k
            return 1;
370
11.8k
        }
371
468
        break;
372
373
4.51k
    case TLS_ST_CW_FINISHED:
374
4.51k
        if (s->ext.ticket_expected) {
375
367
            if (mt == SSL3_MT_NEWSESSION_TICKET) {
376
337
                st->hand_state = TLS_ST_CR_SESSION_TICKET;
377
337
                return 1;
378
337
            }
379
4.14k
        } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
380
4.04k
            st->hand_state = TLS_ST_CR_CHANGE;
381
4.04k
            return 1;
382
4.04k
        }
383
130
        break;
384
385
211
    case TLS_ST_CR_SESSION_TICKET:
386
211
        if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
387
173
            st->hand_state = TLS_ST_CR_CHANGE;
388
173
            return 1;
389
173
        }
390
38
        break;
391
392
1.78k
    case TLS_ST_CR_CHANGE:
393
1.78k
        if (mt == SSL3_MT_FINISHED) {
394
1.56k
            st->hand_state = TLS_ST_CR_FINISHED;
395
1.56k
            return 1;
396
1.56k
        }
397
219
        break;
398
399
1.33k
    case TLS_ST_OK:
400
1.33k
        if (mt == SSL3_MT_HELLO_REQUEST) {
401
1.25k
            st->hand_state = TLS_ST_CR_HELLO_REQ;
402
1.25k
            return 1;
403
1.25k
        }
404
77
        break;
405
145k
    }
406
407
6.18k
err:
408
    /* No valid transition found */
409
6.18k
    if (SSL_CONNECTION_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
410
12
        BIO *rbio;
411
412
        /*
413
         * CCS messages don't have a message sequence number so this is probably
414
         * because of an out-of-order CCS. We'll just drop it.
415
         */
416
12
        s->init_num = 0;
417
12
        s->rwstate = SSL_READING;
418
12
        rbio = SSL_get_rbio(SSL_CONNECTION_GET_SSL(s));
419
12
        BIO_clear_retry_flags(rbio);
420
12
        BIO_set_retry_read(rbio);
421
12
        return 0;
422
12
    }
423
6.18k
    SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
424
6.17k
    return 0;
425
6.18k
}
426
427
static int do_compressed_cert(SSL_CONNECTION *sc)
428
0
{
429
    /* If we negotiated RPK, we won't try to compress it */
430
0
    return sc->ext.client_cert_type == TLSEXT_cert_type_x509
431
0
        && sc->ext.compress_certificate_from_peer[0] != TLSEXT_comp_cert_none;
432
0
}
433
434
/*
435
 * ossl_statem_client13_write_transition() works out what handshake state to
436
 * move to next when the TLSv1.3 client is writing messages to be sent to the
437
 * server.
438
 */
439
static WRITE_TRAN ossl_statem_client13_write_transition(SSL_CONNECTION *s)
440
19.7k
{
441
19.7k
    OSSL_STATEM *st = &s->statem;
442
443
    /*
444
     * Note: There are no cases for TLS_ST_BEFORE because we haven't negotiated
445
     * TLSv1.3 yet at that point. They are handled by
446
     * ossl_statem_client_write_transition().
447
     */
448
19.7k
    switch (st->hand_state) {
449
0
    default:
450
        /* Shouldn't happen */
451
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
452
0
        return WRITE_TRAN_ERROR;
453
454
0
    case TLS_ST_CR_CERT_REQ:
455
0
        if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
456
0
            if (do_compressed_cert(s))
457
0
                st->hand_state = TLS_ST_CW_COMP_CERT;
458
0
            else
459
0
                st->hand_state = TLS_ST_CW_CERT;
460
0
            return WRITE_TRAN_CONTINUE;
461
0
        }
462
        /*
463
         * We should only get here if we received a CertificateRequest after
464
         * we already sent close_notify
465
         */
466
0
        if (!ossl_assert((s->shutdown & SSL_SENT_SHUTDOWN) != 0)) {
467
            /* Shouldn't happen - same as default case */
468
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
469
0
            return WRITE_TRAN_ERROR;
470
0
        }
471
0
        st->hand_state = TLS_ST_OK;
472
0
        return WRITE_TRAN_CONTINUE;
473
474
7.18k
    case TLS_ST_CR_FINISHED:
475
7.18k
        if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY
476
7.18k
            || s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING)
477
0
            st->hand_state = TLS_ST_PENDING_EARLY_DATA_END;
478
7.18k
        else if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
479
0
            && s->hello_retry_request == SSL_HRR_NONE)
480
0
            st->hand_state = TLS_ST_CW_CHANGE;
481
7.18k
        else if (s->s3.tmp.cert_req == 0)
482
7.18k
            st->hand_state = TLS_ST_CW_FINISHED;
483
0
        else if (do_compressed_cert(s))
484
0
            st->hand_state = TLS_ST_CW_COMP_CERT;
485
0
        else
486
0
            st->hand_state = TLS_ST_CW_CERT;
487
488
7.18k
        s->ts_msg_read = ossl_time_now();
489
7.18k
        return WRITE_TRAN_CONTINUE;
490
491
0
    case TLS_ST_PENDING_EARLY_DATA_END:
492
0
        if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED && !SSL_NO_EOED(s)) {
493
0
            st->hand_state = TLS_ST_CW_END_OF_EARLY_DATA;
494
0
            return WRITE_TRAN_CONTINUE;
495
0
        }
496
        /* Fall through */
497
498
0
    case TLS_ST_CW_END_OF_EARLY_DATA:
499
0
    case TLS_ST_CW_CHANGE:
500
0
        if (s->s3.tmp.cert_req == 0)
501
0
            st->hand_state = TLS_ST_CW_FINISHED;
502
0
        else if (do_compressed_cert(s))
503
0
            st->hand_state = TLS_ST_CW_COMP_CERT;
504
0
        else
505
0
            st->hand_state = TLS_ST_CW_CERT;
506
0
        return WRITE_TRAN_CONTINUE;
507
508
0
    case TLS_ST_CW_COMP_CERT:
509
0
    case TLS_ST_CW_CERT:
510
        /* If a non-empty Certificate we also send CertificateVerify */
511
0
        st->hand_state = (s->s3.tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY
512
0
                                                   : TLS_ST_CW_FINISHED;
513
0
        return WRITE_TRAN_CONTINUE;
514
515
0
    case TLS_ST_CW_CERT_VRFY:
516
0
        st->hand_state = TLS_ST_CW_FINISHED;
517
0
        return WRITE_TRAN_CONTINUE;
518
519
0
    case TLS_ST_CR_KEY_UPDATE:
520
0
    case TLS_ST_CW_KEY_UPDATE:
521
1.24k
    case TLS_ST_CR_SESSION_TICKET:
522
8.42k
    case TLS_ST_CW_FINISHED:
523
8.42k
        st->hand_state = TLS_ST_OK;
524
8.42k
        return WRITE_TRAN_CONTINUE;
525
526
4.15k
    case TLS_ST_OK:
527
4.15k
        if (s->key_update != SSL_KEY_UPDATE_NONE) {
528
0
            st->hand_state = TLS_ST_CW_KEY_UPDATE;
529
0
            return WRITE_TRAN_CONTINUE;
530
0
        }
531
532
        /* Try to read from the server instead */
533
4.15k
        return WRITE_TRAN_FINISHED;
534
19.7k
    }
535
19.7k
}
536
537
/*
538
 * ossl_statem_client_write_transition() works out what handshake state to
539
 * move to next when the client is writing messages to be sent to the server.
540
 */
541
WRITE_TRAN ossl_statem_client_write_transition(SSL_CONNECTION *s)
542
178k
{
543
178k
    OSSL_STATEM *st = &s->statem;
544
545
    /*
546
     * Note that immediately before/after a ClientHello we don't know what
547
     * version we are going to negotiate yet, so we don't take this branch until
548
     * later
549
     */
550
178k
    if (SSL_CONNECTION_IS_TLS13(s))
551
19.7k
        return ossl_statem_client13_write_transition(s);
552
553
158k
    switch (st->hand_state) {
554
0
    default:
555
        /* Shouldn't happen */
556
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
557
0
        return WRITE_TRAN_ERROR;
558
559
825
    case TLS_ST_OK:
560
825
        if (!s->renegotiate) {
561
            /*
562
             * We haven't requested a renegotiation ourselves so we must have
563
             * received a message from the server. Better read it.
564
             */
565
825
            return WRITE_TRAN_FINISHED;
566
825
        }
567
        /* Renegotiation */
568
        /* fall thru */
569
62.7k
    case TLS_ST_BEFORE:
570
62.7k
        st->hand_state = TLS_ST_CW_CLNT_HELLO;
571
62.7k
        return WRITE_TRAN_CONTINUE;
572
573
65.1k
    case TLS_ST_CW_CLNT_HELLO:
574
65.1k
        if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
575
0
            && !SSL_IS_QUIC_HANDSHAKE(s)) {
576
            /*
577
             * We are assuming this is a TLSv1.3 connection, although we haven't
578
             * actually selected a version yet.
579
             */
580
0
            if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
581
0
                st->hand_state = TLS_ST_CW_CHANGE;
582
0
            else
583
0
                st->hand_state = TLS_ST_EARLY_DATA;
584
0
            return WRITE_TRAN_CONTINUE;
585
0
        }
586
        /*
587
         * No transition at the end of writing because we don't know what
588
         * we will be sent
589
         */
590
65.1k
        s->ts_msg_write = ossl_time_now();
591
65.1k
        return WRITE_TRAN_FINISHED;
592
593
201
    case TLS_ST_CR_SRVR_HELLO:
594
        /*
595
         * We only get here in TLSv1.3. We just received an HRR, so issue a
596
         * CCS unless middlebox compat mode is off, or we already issued one
597
         * because we did early data.
598
         */
599
201
        if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
600
179
            && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING)
601
179
            st->hand_state = TLS_ST_CW_CHANGE;
602
22
        else
603
22
            st->hand_state = TLS_ST_CW_CLNT_HELLO;
604
201
        return WRITE_TRAN_CONTINUE;
605
606
0
    case TLS_ST_EARLY_DATA:
607
0
        s->ts_msg_write = ossl_time_now();
608
0
        return WRITE_TRAN_FINISHED;
609
610
1.46k
    case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
611
1.46k
        st->hand_state = TLS_ST_CW_CLNT_HELLO;
612
1.46k
        return WRITE_TRAN_CONTINUE;
613
614
6.99k
    case TLS_ST_CR_SRVR_DONE:
615
6.99k
        s->ts_msg_read = ossl_time_now();
616
6.99k
        if (s->s3.tmp.cert_req)
617
20
            st->hand_state = TLS_ST_CW_CERT;
618
6.97k
        else
619
6.97k
            st->hand_state = TLS_ST_CW_KEY_EXCH;
620
6.99k
        return WRITE_TRAN_CONTINUE;
621
622
20
    case TLS_ST_CW_CERT:
623
20
        st->hand_state = TLS_ST_CW_KEY_EXCH;
624
20
        return WRITE_TRAN_CONTINUE;
625
626
6.51k
    case TLS_ST_CW_KEY_EXCH:
627
        /*
628
         * For TLS, cert_req is set to 2, so a cert chain of nothing is
629
         * sent, but no verify packet is sent
630
         */
631
        /*
632
         * XXX: For now, we do not support client authentication in ECDH
633
         * cipher suites with ECDH (rather than ECDSA) certificates. We
634
         * need to skip the certificate verify message when client's
635
         * ECDH public key is sent inside the client certificate.
636
         */
637
6.51k
        if (s->s3.tmp.cert_req == 1) {
638
0
            st->hand_state = TLS_ST_CW_CERT_VRFY;
639
6.51k
        } else {
640
6.51k
            st->hand_state = TLS_ST_CW_CHANGE;
641
6.51k
        }
642
6.51k
        if (s->s3.flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
643
0
            st->hand_state = TLS_ST_CW_CHANGE;
644
0
        }
645
6.51k
        return WRITE_TRAN_CONTINUE;
646
647
0
    case TLS_ST_CW_CERT_VRFY:
648
0
        st->hand_state = TLS_ST_CW_CHANGE;
649
0
        return WRITE_TRAN_CONTINUE;
650
651
6.68k
    case TLS_ST_CW_CHANGE:
652
6.68k
        if (s->hello_retry_request == SSL_HRR_PENDING) {
653
179
            st->hand_state = TLS_ST_CW_CLNT_HELLO;
654
6.51k
        } else if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) {
655
0
            st->hand_state = TLS_ST_EARLY_DATA;
656
6.51k
        } else {
657
#if defined(OPENSSL_NO_NEXTPROTONEG)
658
            st->hand_state = TLS_ST_CW_FINISHED;
659
#else
660
6.51k
            if (!SSL_CONNECTION_IS_DTLS(s) && s->s3.npn_seen)
661
0
                st->hand_state = TLS_ST_CW_NEXT_PROTO;
662
6.51k
            else
663
6.51k
                st->hand_state = TLS_ST_CW_FINISHED;
664
6.51k
#endif
665
6.51k
        }
666
6.68k
        return WRITE_TRAN_CONTINUE;
667
668
0
#if !defined(OPENSSL_NO_NEXTPROTONEG)
669
0
    case TLS_ST_CW_NEXT_PROTO:
670
0
        st->hand_state = TLS_ST_CW_FINISHED;
671
0
        return WRITE_TRAN_CONTINUE;
672
0
#endif
673
674
6.51k
    case TLS_ST_CW_FINISHED:
675
6.51k
        if (s->hit) {
676
0
            st->hand_state = TLS_ST_OK;
677
0
            return WRITE_TRAN_CONTINUE;
678
6.51k
        } else {
679
6.51k
            return WRITE_TRAN_FINISHED;
680
6.51k
        }
681
682
930
    case TLS_ST_CR_FINISHED:
683
930
        if (s->hit) {
684
0
            st->hand_state = TLS_ST_CW_CHANGE;
685
0
            return WRITE_TRAN_CONTINUE;
686
930
        } else {
687
930
            st->hand_state = TLS_ST_OK;
688
930
            return WRITE_TRAN_CONTINUE;
689
930
        }
690
691
766
    case TLS_ST_CR_HELLO_REQ:
692
        /*
693
         * If we can renegotiate now then do so, otherwise wait for a more
694
         * convenient time.
695
         */
696
766
        if (ssl3_renegotiate_check(SSL_CONNECTION_GET_SSL(s), 1)) {
697
766
            if (!tls_setup_handshake(s)) {
698
                /* SSLfatal() already called */
699
0
                return WRITE_TRAN_ERROR;
700
0
            }
701
766
            st->hand_state = TLS_ST_CW_CLNT_HELLO;
702
766
            return WRITE_TRAN_CONTINUE;
703
766
        }
704
0
        st->hand_state = TLS_ST_OK;
705
0
        return WRITE_TRAN_CONTINUE;
706
158k
    }
707
158k
}
708
709
/*
710
 * Perform any pre work that needs to be done prior to sending a message from
711
 * the client to the server.
712
 */
713
WORK_STATE ossl_statem_client_pre_work(SSL_CONNECTION *s, WORK_STATE wst)
714
172k
{
715
172k
    OSSL_STATEM *st = &s->statem;
716
717
172k
    switch (st->hand_state) {
718
34.6k
    default:
719
        /* No pre work to be done */
720
34.6k
        break;
721
722
111k
    case TLS_ST_CW_CLNT_HELLO:
723
111k
        s->shutdown = 0;
724
111k
        if (SSL_CONNECTION_IS_DTLS(s)) {
725
            /* every DTLS ClientHello resets Finished MAC */
726
26.2k
            if (!ssl3_init_finished_mac(s)) {
727
                /* SSLfatal() already called */
728
0
                return WORK_ERROR;
729
0
            }
730
84.8k
        } else if (s->ext.early_data == SSL_EARLY_DATA_REJECTED) {
731
            /*
732
             * This must be a second ClientHello after an HRR following an
733
             * earlier rejected attempt to send early data. Since we were
734
             * previously encrypting the early data we now need to reset the
735
             * write record layer in order to write in plaintext again.
736
             */
737
0
            if (!ssl_set_new_record_layer(s,
738
0
                    TLS_ANY_VERSION,
739
0
                    OSSL_RECORD_DIRECTION_WRITE,
740
0
                    OSSL_RECORD_PROTECTION_LEVEL_NONE,
741
0
                    NULL, 0, NULL, 0, NULL, 0, NULL, 0,
742
0
                    NULL, 0, NID_undef, NULL, NULL,
743
0
                    NULL)) {
744
                /* SSLfatal already called */
745
0
                return WORK_ERROR;
746
0
            }
747
0
        }
748
111k
        break;
749
750
111k
    case TLS_ST_CW_CHANGE:
751
10.7k
        if (SSL_CONNECTION_IS_DTLS(s)) {
752
2.77k
            if (s->hit) {
753
                /*
754
                 * We're into the last flight so we don't retransmit these
755
                 * messages unless we need to.
756
                 */
757
0
                st->use_timer = 0;
758
0
            }
759
#ifndef OPENSSL_NO_SCTP
760
            if (BIO_dgram_is_sctp(SSL_get_wbio(SSL_CONNECTION_GET_SSL(s)))) {
761
                /* Calls SSLfatal() as required */
762
                return dtls_wait_for_dry(s);
763
            }
764
#endif
765
2.77k
        }
766
10.7k
        break;
767
768
0
    case TLS_ST_PENDING_EARLY_DATA_END:
769
        /*
770
         * If we've been called by SSL_do_handshake()/SSL_write(), or we did not
771
         * attempt to write early data before calling SSL_read() then we press
772
         * on with the handshake. Otherwise we pause here.
773
         */
774
0
        if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING
775
0
            || s->early_data_state == SSL_EARLY_DATA_NONE)
776
0
            return WORK_FINISHED_CONTINUE;
777
        /* Fall through */
778
779
0
    case TLS_ST_EARLY_DATA:
780
0
        return tls_finish_handshake(s, wst, 0, 1);
781
782
16.0k
    case TLS_ST_OK:
783
        /* Calls SSLfatal() as required */
784
16.0k
        return tls_finish_handshake(s, wst, 1, 1);
785
172k
    }
786
787
156k
    return WORK_FINISHED_CONTINUE;
788
172k
}
789
790
/*
791
 * Perform any work that needs to be done after sending a message from the
792
 * client to the server.
793
 */
794
WORK_STATE ossl_statem_client_post_work(SSL_CONNECTION *s, WORK_STATE wst)
795
92.1k
{
796
92.1k
    OSSL_STATEM *st = &s->statem;
797
92.1k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
798
799
92.1k
    s->init_num = 0;
800
801
92.1k
    switch (st->hand_state) {
802
20
    default:
803
        /* No post work to be done */
804
20
        break;
805
806
65.1k
    case TLS_ST_CW_CLNT_HELLO:
807
65.1k
        if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
808
0
            && s->max_early_data > 0) {
809
            /*
810
             * We haven't selected TLSv1.3 yet so we don't call the change
811
             * cipher state function associated with the SSL_METHOD. Instead
812
             * we call tls13_change_cipher_state() directly.
813
             */
814
0
            if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0) {
815
0
                if (!tls13_change_cipher_state(s,
816
0
                        SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
817
                    /* SSLfatal() already called */
818
0
                    return WORK_ERROR;
819
0
                }
820
0
            }
821
            /* else we're in compat mode so we delay flushing until after CCS */
822
65.1k
        } else if (!statem_flush(s)) {
823
0
            return WORK_MORE_A;
824
0
        }
825
826
65.1k
        if (SSL_CONNECTION_IS_DTLS(s)) {
827
            /* Treat the next message as the first packet */
828
14.9k
            s->first_packet = 1;
829
14.9k
        }
830
65.1k
        break;
831
832
6.51k
    case TLS_ST_CW_KEY_EXCH:
833
6.51k
        if (tls_client_key_exchange_post_work(s) == 0) {
834
            /* SSLfatal() already called */
835
0
            return WORK_ERROR;
836
0
        }
837
6.51k
        break;
838
839
6.68k
    case TLS_ST_CW_CHANGE:
840
6.68k
        if (SSL_CONNECTION_IS_TLS13(s)
841
6.68k
            || s->hello_retry_request == SSL_HRR_PENDING)
842
179
            break;
843
6.51k
        if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
844
0
            && s->max_early_data > 0) {
845
            /*
846
             * We haven't selected TLSv1.3 yet so we don't call the change
847
             * cipher state function associated with the SSL_METHOD. Instead
848
             * we call tls13_change_cipher_state() directly.
849
             */
850
0
            if (!tls13_change_cipher_state(s,
851
0
                    SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE))
852
0
                return WORK_ERROR;
853
0
            break;
854
0
        }
855
6.51k
        s->session->cipher = s->s3.tmp.new_cipher;
856
#ifdef OPENSSL_NO_COMP
857
        s->session->compress_meth = 0;
858
#else
859
6.51k
        if (s->s3.tmp.new_compression == NULL)
860
6.51k
            s->session->compress_meth = 0;
861
0
        else
862
0
            s->session->compress_meth = s->s3.tmp.new_compression->id;
863
6.51k
#endif
864
6.51k
        if (!ssl->method->ssl3_enc->setup_key_block(s)) {
865
            /* SSLfatal() already called */
866
0
            return WORK_ERROR;
867
0
        }
868
869
6.51k
        if (!ssl->method->ssl3_enc->change_cipher_state(s,
870
6.51k
                SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
871
            /* SSLfatal() already called */
872
0
            return WORK_ERROR;
873
0
        }
874
875
#ifndef OPENSSL_NO_SCTP
876
        if (SSL_CONNECTION_IS_DTLS(s) && s->hit) {
877
            /*
878
             * Change to new shared key of SCTP-Auth, will be ignored if
879
             * no SCTP used.
880
             */
881
            BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
882
                0, NULL);
883
        }
884
#endif
885
6.51k
        break;
886
887
13.6k
    case TLS_ST_CW_FINISHED:
888
#ifndef OPENSSL_NO_SCTP
889
        if (wst == WORK_MORE_A && SSL_CONNECTION_IS_DTLS(s) && s->hit == 0) {
890
            /*
891
             * Change to new shared key of SCTP-Auth, will be ignored if
892
             * no SCTP used.
893
             */
894
            BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
895
                0, NULL);
896
        }
897
#endif
898
13.6k
        if (statem_flush(s) != 1)
899
0
            return WORK_MORE_B;
900
901
13.6k
        if (SSL_CONNECTION_IS_TLS13(s)) {
902
7.18k
            if (!tls13_save_handshake_digest_for_pha(s)) {
903
                /* SSLfatal() already called */
904
0
                return WORK_ERROR;
905
0
            }
906
7.18k
            if (s->post_handshake_auth != SSL_PHA_REQUESTED) {
907
7.18k
                if (!ssl->method->ssl3_enc->change_cipher_state(s,
908
7.18k
                        SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
909
                    /* SSLfatal() already called */
910
0
                    return WORK_ERROR;
911
0
                }
912
                /*
913
                 * For QUIC we deferred setting up these keys until now so
914
                 * that we can ensure write keys are always set up before read
915
                 * keys.
916
                 */
917
7.18k
                if (SSL_IS_QUIC_HANDSHAKE(s)
918
7.18k
                    && !ssl->method->ssl3_enc->change_cipher_state(s,
919
7.18k
                        SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
920
                    /* SSLfatal() already called */
921
6
                    return WORK_ERROR;
922
6
                }
923
7.18k
            }
924
7.18k
        }
925
13.6k
        break;
926
927
13.6k
    case TLS_ST_CW_KEY_UPDATE:
928
0
        if (statem_flush(s) != 1)
929
0
            return WORK_MORE_A;
930
0
        if (!tls13_update_key(s, 1)) {
931
            /* SSLfatal() already called */
932
0
            return WORK_ERROR;
933
0
        }
934
0
        break;
935
92.1k
    }
936
937
92.0k
    return WORK_FINISHED_CONTINUE;
938
92.1k
}
939
940
/*
941
 * Get the message construction function and message type for sending from the
942
 * client
943
 *
944
 * Valid return values are:
945
 *   1: Success
946
 *   0: Error
947
 */
948
int ossl_statem_client_construct_message(SSL_CONNECTION *s,
949
    confunc_f *confunc, int *mt)
950
167k
{
951
167k
    OSSL_STATEM *st = &s->statem;
952
953
167k
    switch (st->hand_state) {
954
0
    default:
955
        /* Shouldn't happen */
956
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE);
957
0
        return 0;
958
959
12.0k
    case TLS_ST_CW_CHANGE:
960
12.0k
        if (SSL_CONNECTION_IS_DTLS(s))
961
2.77k
            *confunc = dtls_construct_change_cipher_spec;
962
9.29k
        else
963
9.29k
            *confunc = tls_construct_change_cipher_spec;
964
12.0k
        *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
965
12.0k
        break;
966
967
117k
    case TLS_ST_CW_CLNT_HELLO:
968
117k
        *confunc = tls_construct_client_hello;
969
117k
        *mt = SSL3_MT_CLIENT_HELLO;
970
117k
        break;
971
972
0
    case TLS_ST_CW_END_OF_EARLY_DATA:
973
0
        *confunc = tls_construct_end_of_early_data;
974
0
        *mt = SSL3_MT_END_OF_EARLY_DATA;
975
0
        break;
976
977
0
    case TLS_ST_PENDING_EARLY_DATA_END:
978
0
        *confunc = NULL;
979
0
        *mt = SSL3_MT_DUMMY;
980
0
        break;
981
982
29
    case TLS_ST_CW_CERT:
983
29
        *confunc = tls_construct_client_certificate;
984
29
        *mt = SSL3_MT_CERTIFICATE;
985
29
        break;
986
987
#ifndef OPENSSL_NO_COMP_ALG
988
    case TLS_ST_CW_COMP_CERT:
989
        *confunc = tls_construct_client_compressed_certificate;
990
        *mt = SSL3_MT_COMPRESSED_CERTIFICATE;
991
        break;
992
#endif
993
994
13.2k
    case TLS_ST_CW_KEY_EXCH:
995
13.2k
        *confunc = tls_construct_client_key_exchange;
996
13.2k
        *mt = SSL3_MT_CLIENT_KEY_EXCHANGE;
997
13.2k
        break;
998
999
0
    case TLS_ST_CW_CERT_VRFY:
1000
0
        *confunc = tls_construct_cert_verify;
1001
0
        *mt = SSL3_MT_CERTIFICATE_VERIFY;
1002
0
        break;
1003
1004
0
#if !defined(OPENSSL_NO_NEXTPROTONEG)
1005
0
    case TLS_ST_CW_NEXT_PROTO:
1006
0
        *confunc = tls_construct_next_proto;
1007
0
        *mt = SSL3_MT_NEXT_PROTO;
1008
0
        break;
1009
0
#endif
1010
24.0k
    case TLS_ST_CW_FINISHED:
1011
24.0k
        *confunc = tls_construct_finished;
1012
24.0k
        *mt = SSL3_MT_FINISHED;
1013
24.0k
        break;
1014
1015
0
    case TLS_ST_CW_KEY_UPDATE:
1016
0
        *confunc = tls_construct_key_update;
1017
0
        *mt = SSL3_MT_KEY_UPDATE;
1018
0
        break;
1019
167k
    }
1020
1021
167k
    return 1;
1022
167k
}
1023
1024
/*
1025
 * Returns the maximum allowed length for the current message that we are
1026
 * reading. Excludes the message header.
1027
 */
1028
size_t ossl_statem_client_max_message_size(SSL_CONNECTION *s)
1029
221k
{
1030
221k
    OSSL_STATEM *st = &s->statem;
1031
1032
221k
    switch (st->hand_state) {
1033
1.25k
    default:
1034
        /* Shouldn't happen */
1035
1.25k
        return 0;
1036
1037
73.4k
    case TLS_ST_CR_SRVR_HELLO:
1038
73.4k
        return SERVER_HELLO_MAX_LENGTH;
1039
1040
4.42k
    case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
1041
4.42k
        return HELLO_VERIFY_REQUEST_MAX_LENGTH;
1042
1043
0
    case TLS_ST_CR_COMP_CERT:
1044
48.3k
    case TLS_ST_CR_CERT:
1045
48.3k
        return s->max_cert_list;
1046
1047
16.7k
    case TLS_ST_CR_CERT_VRFY:
1048
16.7k
        return CERTIFICATE_VERIFY_MAX_LENGTH;
1049
1050
0
    case TLS_ST_CR_CERT_STATUS:
1051
0
        return SSL3_RT_MAX_PLAIN_LENGTH;
1052
1053
17.1k
    case TLS_ST_CR_KEY_EXCH:
1054
17.1k
        return SERVER_KEY_EXCH_MAX_LENGTH;
1055
1056
854
    case TLS_ST_CR_CERT_REQ:
1057
        /*
1058
         * Set to s->max_cert_list for compatibility with previous releases. In
1059
         * practice these messages can get quite long if servers are configured
1060
         * to provide a long list of acceptable CAs
1061
         */
1062
854
        return s->max_cert_list;
1063
1064
11.8k
    case TLS_ST_CR_SRVR_DONE:
1065
11.8k
        return SERVER_HELLO_DONE_MAX_LENGTH;
1066
1067
4.22k
    case TLS_ST_CR_CHANGE:
1068
4.22k
        if (s->version == DTLS1_BAD_VER)
1069
0
            return 3;
1070
4.22k
        return CCS_MAX_LENGTH;
1071
1072
7.33k
    case TLS_ST_CR_SESSION_TICKET:
1073
7.33k
        return (SSL_CONNECTION_IS_TLS13(s)) ? SESSION_TICKET_MAX_LENGTH_TLS13
1074
7.33k
                                            : SESSION_TICKET_MAX_LENGTH_TLS12;
1075
1076
14.0k
    case TLS_ST_CR_FINISHED:
1077
14.0k
        return FINISHED_MAX_LENGTH;
1078
1079
21.8k
    case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
1080
21.8k
        return ENCRYPTED_EXTENSIONS_MAX_LENGTH;
1081
1082
0
    case TLS_ST_CR_KEY_UPDATE:
1083
0
        return KEY_UPDATE_MAX_LENGTH;
1084
221k
    }
1085
221k
}
1086
1087
/*
1088
 * Process a message that the client has received from the server.
1089
 */
1090
MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL_CONNECTION *s,
1091
    PACKET *pkt)
1092
228k
{
1093
228k
    OSSL_STATEM *st = &s->statem;
1094
1095
228k
    switch (st->hand_state) {
1096
0
    default:
1097
        /* Shouldn't happen */
1098
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1099
0
        return MSG_PROCESS_ERROR;
1100
1101
77.6k
    case TLS_ST_CR_SRVR_HELLO:
1102
77.6k
        return tls_process_server_hello(s, pkt);
1103
1104
4.34k
    case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
1105
4.34k
        return dtls_process_hello_verify(s, pkt);
1106
1107
52.0k
    case TLS_ST_CR_CERT:
1108
52.0k
        return tls_process_server_certificate(s, pkt);
1109
1110
#ifndef OPENSSL_NO_COMP_ALG
1111
    case TLS_ST_CR_COMP_CERT:
1112
        return tls_process_server_compressed_certificate(s, pkt);
1113
#endif
1114
1115
16.2k
    case TLS_ST_CR_CERT_VRFY:
1116
16.2k
        return tls_process_cert_verify(s, pkt);
1117
1118
0
    case TLS_ST_CR_CERT_STATUS:
1119
0
        return tls_process_cert_status(s, pkt);
1120
1121
19.6k
    case TLS_ST_CR_KEY_EXCH:
1122
19.6k
        return tls_process_key_exchange(s, pkt);
1123
1124
971
    case TLS_ST_CR_CERT_REQ:
1125
971
        return tls_process_certificate_request(s, pkt);
1126
1127
13.2k
    case TLS_ST_CR_SRVR_DONE:
1128
13.2k
        return tls_process_server_done(s, pkt);
1129
1130
4.58k
    case TLS_ST_CR_CHANGE:
1131
4.58k
        return tls_process_change_cipher_spec(s, pkt);
1132
1133
2.68k
    case TLS_ST_CR_SESSION_TICKET:
1134
2.68k
        return tls_process_new_session_ticket(s, pkt);
1135
1136
14.0k
    case TLS_ST_CR_FINISHED:
1137
14.0k
        return tls_process_finished(s, pkt);
1138
1139
1.23k
    case TLS_ST_CR_HELLO_REQ:
1140
1.23k
        return tls_process_hello_req(s, pkt);
1141
1142
21.8k
    case TLS_ST_CR_ENCRYPTED_EXTENSIONS:
1143
21.8k
        return tls_process_encrypted_extensions(s, pkt);
1144
1145
0
    case TLS_ST_CR_KEY_UPDATE:
1146
0
        return tls_process_key_update(s, pkt);
1147
228k
    }
1148
228k
}
1149
1150
/*
1151
 * Perform any further processing required following the receipt of a message
1152
 * from the server
1153
 */
1154
WORK_STATE ossl_statem_client_post_process_message(SSL_CONNECTION *s,
1155
    WORK_STATE wst)
1156
31.0k
{
1157
31.0k
    OSSL_STATEM *st = &s->statem;
1158
1159
31.0k
    switch (st->hand_state) {
1160
0
    default:
1161
        /* Shouldn't happen */
1162
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1163
0
        return WORK_ERROR;
1164
1165
30.9k
    case TLS_ST_CR_CERT:
1166
30.9k
    case TLS_ST_CR_COMP_CERT:
1167
30.9k
        return tls_post_process_server_certificate(s, wst);
1168
1169
0
    case TLS_ST_CR_CERT_VRFY:
1170
45
    case TLS_ST_CR_CERT_REQ:
1171
45
        return tls_prepare_client_certificate(s, wst);
1172
31.0k
    }
1173
31.0k
}
1174
1175
CON_FUNC_RETURN tls_construct_client_hello(SSL_CONNECTION *s, WPACKET *pkt)
1176
117k
{
1177
117k
    unsigned char *p;
1178
117k
    size_t sess_id_len;
1179
117k
    int i, protverr;
1180
117k
#ifndef OPENSSL_NO_COMP
1181
117k
    SSL_COMP *comp;
1182
117k
#endif
1183
117k
    SSL_SESSION *sess = s->session;
1184
117k
    unsigned char *session_id;
1185
117k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1186
1187
    /* Work out what SSL/TLS/DTLS version to use */
1188
117k
    protverr = ssl_set_client_hello_version(s);
1189
117k
    if (protverr != 0) {
1190
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, protverr);
1191
0
        return CON_FUNC_ERROR;
1192
0
    }
1193
1194
117k
    if (sess == NULL
1195
5.98k
        || !ssl_version_supported(s, sess->ssl_version, NULL)
1196
117k
        || !SSL_SESSION_is_resumable(sess)) {
1197
117k
        if (s->hello_retry_request == SSL_HRR_NONE
1198
116k
            && !ssl_get_new_session(s, 0)) {
1199
            /* SSLfatal() already called */
1200
0
            return CON_FUNC_ERROR;
1201
0
        }
1202
117k
    }
1203
    /* else use the pre-loaded session */
1204
1205
117k
    p = s->s3.client_random;
1206
1207
    /*
1208
     * for DTLS if client_random is initialized, reuse it, we are
1209
     * required to use same upon reply to HelloVerify
1210
     */
1211
117k
    if (SSL_CONNECTION_IS_DTLS(s)) {
1212
26.2k
        size_t idx;
1213
26.2k
        i = 1;
1214
727k
        for (idx = 0; idx < sizeof(s->s3.client_random); idx++) {
1215
705k
            if (p[idx]) {
1216
4.30k
                i = 0;
1217
4.30k
                break;
1218
4.30k
            }
1219
705k
        }
1220
91.6k
    } else {
1221
91.6k
        i = (s->hello_retry_request == SSL_HRR_NONE);
1222
91.6k
    }
1223
1224
117k
    if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3.client_random), DOWNGRADE_NONE) <= 0) {
1225
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1226
0
        return CON_FUNC_ERROR;
1227
0
    }
1228
1229
    /*-
1230
     * version indicates the negotiated version: for example from
1231
     * an SSLv2/v3 compatible client hello). The client_version
1232
     * field is the maximum version we permit and it is also
1233
     * used in RSA encrypted premaster secrets. Some servers can
1234
     * choke if we initially report a higher version then
1235
     * renegotiate to a lower one in the premaster secret. This
1236
     * didn't happen with TLS 1.0 as most servers supported it
1237
     * but it can with TLS 1.1 or later if the server only supports
1238
     * 1.0.
1239
     *
1240
     * Possible scenario with previous logic:
1241
     *      1. Client hello indicates TLS 1.2
1242
     *      2. Server hello says TLS 1.0
1243
     *      3. RSA encrypted premaster secret uses 1.2.
1244
     *      4. Handshake proceeds using TLS 1.0.
1245
     *      5. Server sends hello request to renegotiate.
1246
     *      6. Client hello indicates TLS v1.0 as we now
1247
     *         know that is maximum server supports.
1248
     *      7. Server chokes on RSA encrypted premaster secret
1249
     *         containing version 1.0.
1250
     *
1251
     * For interoperability it should be OK to always use the
1252
     * maximum version we support in client hello and then rely
1253
     * on the checking of version to ensure the servers isn't
1254
     * being inconsistent: for example initially negotiating with
1255
     * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
1256
     * client_version in client hello and not resetting it to
1257
     * the negotiated version.
1258
     *
1259
     * For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the
1260
     * supported_versions extension for the real supported versions.
1261
     */
1262
117k
    if (!WPACKET_put_bytes_u16(pkt, s->client_version)
1263
117k
        || !WPACKET_memcpy(pkt, s->s3.client_random, SSL3_RANDOM_SIZE)) {
1264
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1265
0
        return CON_FUNC_ERROR;
1266
0
    }
1267
1268
    /* Session ID */
1269
117k
    session_id = s->session->session_id;
1270
117k
    if (s->new_session || s->session->ssl_version == TLS1_3_VERSION) {
1271
90.4k
        if (s->version == TLS1_3_VERSION
1272
90.4k
            && (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0) {
1273
39.9k
            sess_id_len = sizeof(s->tmp_session_id);
1274
39.9k
            s->tmp_session_id_len = sess_id_len;
1275
39.9k
            session_id = s->tmp_session_id;
1276
39.9k
            if (s->hello_retry_request == SSL_HRR_NONE
1277
39.5k
                && RAND_bytes_ex(sctx->libctx, s->tmp_session_id,
1278
39.5k
                       sess_id_len, 0)
1279
39.5k
                    <= 0) {
1280
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1281
0
                return CON_FUNC_ERROR;
1282
0
            }
1283
50.4k
        } else {
1284
50.4k
            sess_id_len = 0;
1285
50.4k
        }
1286
90.4k
    } else {
1287
27.4k
        assert(s->session->session_id_length <= sizeof(s->session->session_id));
1288
27.4k
        sess_id_len = s->session->session_id_length;
1289
27.4k
        if (s->version == TLS1_3_VERSION) {
1290
0
            s->tmp_session_id_len = sess_id_len;
1291
0
            memcpy(s->tmp_session_id, s->session->session_id, sess_id_len);
1292
0
        }
1293
27.4k
    }
1294
117k
    if (!WPACKET_start_sub_packet_u8(pkt)
1295
117k
        || (sess_id_len != 0 && !WPACKET_memcpy(pkt, session_id, sess_id_len))
1296
117k
        || !WPACKET_close(pkt)) {
1297
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1298
0
        return CON_FUNC_ERROR;
1299
0
    }
1300
1301
    /* cookie stuff for DTLS */
1302
117k
    if (SSL_CONNECTION_IS_DTLS(s)) {
1303
26.2k
        if (s->d1->cookie_len > sizeof(s->d1->cookie)
1304
26.2k
            || !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie,
1305
26.2k
                s->d1->cookie_len)) {
1306
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1307
0
            return CON_FUNC_ERROR;
1308
0
        }
1309
26.2k
    }
1310
1311
    /* Ciphers supported */
1312
117k
    if (!WPACKET_start_sub_packet_u16(pkt)) {
1313
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1314
0
        return CON_FUNC_ERROR;
1315
0
    }
1316
1317
117k
    if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(SSL_CONNECTION_GET_SSL(s)),
1318
117k
            pkt)) {
1319
        /* SSLfatal() already called */
1320
0
        return CON_FUNC_ERROR;
1321
0
    }
1322
117k
    if (!WPACKET_close(pkt)) {
1323
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1324
0
        return CON_FUNC_ERROR;
1325
0
    }
1326
1327
    /* COMPRESSION */
1328
117k
    if (!WPACKET_start_sub_packet_u8(pkt)) {
1329
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1330
0
        return CON_FUNC_ERROR;
1331
0
    }
1332
117k
#ifndef OPENSSL_NO_COMP
1333
117k
    if (ssl_allow_compression(s)
1334
0
        && sctx->comp_methods
1335
0
        && (SSL_CONNECTION_IS_DTLS(s)
1336
0
            || s->s3.tmp.max_ver < TLS1_3_VERSION)) {
1337
0
        int compnum = sk_SSL_COMP_num(sctx->comp_methods);
1338
0
        for (i = 0; i < compnum; i++) {
1339
0
            comp = sk_SSL_COMP_value(sctx->comp_methods, i);
1340
0
            if (!WPACKET_put_bytes_u8(pkt, comp->id)) {
1341
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1342
0
                return CON_FUNC_ERROR;
1343
0
            }
1344
0
        }
1345
0
    }
1346
117k
#endif
1347
    /* Add the NULL method */
1348
117k
    if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) {
1349
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1350
0
        return CON_FUNC_ERROR;
1351
0
    }
1352
1353
    /* TLS extensions */
1354
117k
    if (!tls_construct_extensions(s, pkt, SSL_EXT_CLIENT_HELLO, NULL, 0)) {
1355
        /* SSLfatal() already called */
1356
0
        return CON_FUNC_ERROR;
1357
0
    }
1358
1359
117k
    return CON_FUNC_SUCCESS;
1360
117k
}
1361
1362
MSG_PROCESS_RETURN dtls_process_hello_verify(SSL_CONNECTION *s, PACKET *pkt)
1363
4.34k
{
1364
4.34k
    size_t cookie_len;
1365
4.34k
    PACKET cookiepkt;
1366
1367
4.34k
    if (!PACKET_forward(pkt, 2)
1368
4.33k
        || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
1369
46
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1370
46
        return MSG_PROCESS_ERROR;
1371
46
    }
1372
1373
4.30k
    cookie_len = PACKET_remaining(&cookiepkt);
1374
4.30k
    if (cookie_len > sizeof(s->d1->cookie)) {
1375
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_TOO_LONG);
1376
0
        return MSG_PROCESS_ERROR;
1377
0
    }
1378
1379
4.30k
    if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
1380
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1381
0
        return MSG_PROCESS_ERROR;
1382
0
    }
1383
4.30k
    s->d1->cookie_len = cookie_len;
1384
1385
4.30k
    return MSG_PROCESS_FINISHED_READING;
1386
4.30k
}
1387
1388
static int set_client_ciphersuite(SSL_CONNECTION *s,
1389
    const unsigned char *cipherchars)
1390
68.2k
{
1391
68.2k
    STACK_OF(SSL_CIPHER) *sk;
1392
68.2k
    const SSL_CIPHER *c;
1393
68.2k
    int i;
1394
68.2k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1395
1396
68.2k
    c = ssl_get_cipher_by_char(s, cipherchars, 0);
1397
68.2k
    if (c == NULL) {
1398
        /* unknown cipher */
1399
265
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CIPHER_RETURNED);
1400
265
        return 0;
1401
265
    }
1402
    /*
1403
     * If it is a disabled cipher we either didn't send it in client hello,
1404
     * or it's not allowed for the selected protocol. So we return an error.
1405
     */
1406
67.9k
    if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK, 1)) {
1407
133
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
1408
133
        return 0;
1409
133
    }
1410
1411
67.8k
    sk = ssl_get_ciphers_by_id(s);
1412
67.8k
    i = sk_SSL_CIPHER_find(sk, c);
1413
67.8k
    if (i < 0) {
1414
        /* we did not say we would use this cipher */
1415
31
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
1416
31
        return 0;
1417
31
    }
1418
1419
67.7k
    if (SSL_CONNECTION_IS_TLS13(s) && s->s3.tmp.new_cipher != NULL
1420
67
        && s->s3.tmp.new_cipher->id != c->id) {
1421
        /* ServerHello selected a different ciphersuite to that in the HRR */
1422
6
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED);
1423
6
        return 0;
1424
6
    }
1425
1426
    /*
1427
     * Depending on the session caching (internal/external), the cipher
1428
     * and/or cipher_id values may not be set. Make sure that cipher_id is
1429
     * set and use it for comparison.
1430
     */
1431
67.7k
    if (s->session->cipher != NULL)
1432
27
        s->session->cipher_id = s->session->cipher->id;
1433
67.7k
    if (s->hit && (s->session->cipher_id != c->id)) {
1434
6
        if (SSL_CONNECTION_IS_TLS13(s)) {
1435
0
            const EVP_MD *md = ssl_md(sctx, c->algorithm2);
1436
1437
0
            if (!ossl_assert(s->session->cipher != NULL)) {
1438
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1439
0
                return 0;
1440
0
            }
1441
            /*
1442
             * In TLSv1.3 it is valid for the server to select a different
1443
             * ciphersuite as long as the hash is the same.
1444
             */
1445
0
            if (md == NULL
1446
0
                || md != ssl_md(sctx, s->session->cipher->algorithm2)) {
1447
0
                SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1448
0
                    SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED);
1449
0
                return 0;
1450
0
            }
1451
6
        } else {
1452
            /*
1453
             * Prior to TLSv1.3 resuming a session always meant using the same
1454
             * ciphersuite.
1455
             */
1456
6
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1457
6
                SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1458
6
            return 0;
1459
6
        }
1460
6
    }
1461
67.7k
    s->s3.tmp.new_cipher = c;
1462
1463
67.7k
    return 1;
1464
67.7k
}
1465
1466
MSG_PROCESS_RETURN tls_process_server_hello(SSL_CONNECTION *s, PACKET *pkt)
1467
42.7k
{
1468
42.7k
    PACKET session_id, extpkt;
1469
42.7k
    size_t session_id_len;
1470
42.7k
    const unsigned char *cipherchars;
1471
42.7k
    int hrr = 0;
1472
42.7k
    unsigned int compression;
1473
42.7k
    unsigned int sversion;
1474
42.7k
    unsigned int context;
1475
42.7k
    RAW_EXTENSION *extensions = NULL;
1476
42.7k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1477
42.7k
    SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s);
1478
42.7k
#ifndef OPENSSL_NO_COMP
1479
42.7k
    SSL_COMP *comp;
1480
42.7k
#endif
1481
1482
42.7k
    if (!PACKET_get_net_2(pkt, &sversion)) {
1483
56
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1484
56
        goto err;
1485
56
    }
1486
1487
    /* load the server random */
1488
42.6k
    if (s->version == TLS1_3_VERSION
1489
33.3k
        && sversion == TLS1_2_VERSION
1490
26.0k
        && PACKET_remaining(pkt) >= SSL3_RANDOM_SIZE
1491
26.0k
        && memcmp(hrrrandom, PACKET_data(pkt), SSL3_RANDOM_SIZE) == 0) {
1492
444
        if (s->hello_retry_request != SSL_HRR_NONE) {
1493
4
            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
1494
4
            goto err;
1495
4
        }
1496
440
        s->hello_retry_request = SSL_HRR_PENDING;
1497
        /* Tell the record layer that we know we're going to get TLSv1.3 */
1498
440
        if (!ssl_set_record_protocol_version(s, s->version)) {
1499
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1500
0
            goto err;
1501
0
        }
1502
440
        hrr = 1;
1503
440
        if (!PACKET_forward(pkt, SSL3_RANDOM_SIZE)) {
1504
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1505
0
            goto err;
1506
0
        }
1507
42.2k
    } else {
1508
42.2k
        if (!PACKET_copy_bytes(pkt, s->s3.server_random, SSL3_RANDOM_SIZE)) {
1509
86
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1510
86
            goto err;
1511
86
        }
1512
42.2k
    }
1513
1514
    /* Get the session-id. */
1515
42.6k
    if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
1516
275
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1517
275
        goto err;
1518
275
    }
1519
42.3k
    session_id_len = PACKET_remaining(&session_id);
1520
42.3k
    if (session_id_len > sizeof(s->session->session_id)
1521
42.3k
        || session_id_len > SSL3_SESSION_ID_SIZE) {
1522
23
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1523
23
        goto err;
1524
23
    }
1525
1526
42.3k
    if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
1527
26
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1528
26
        goto err;
1529
26
    }
1530
1531
42.2k
    if (!PACKET_get_1(pkt, &compression)) {
1532
13
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
1533
13
        goto err;
1534
13
    }
1535
1536
    /* TLS extensions */
1537
42.2k
    if (PACKET_remaining(pkt) == 0 && !hrr) {
1538
336
        PACKET_null_init(&extpkt);
1539
41.9k
    } else if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
1540
41.5k
        || PACKET_remaining(pkt) != 0) {
1541
409
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
1542
409
        goto err;
1543
409
    }
1544
1545
41.8k
    if (!hrr) {
1546
41.4k
        if (!tls_collect_extensions(s, &extpkt,
1547
41.4k
                SSL_EXT_TLS1_2_SERVER_HELLO
1548
41.4k
                    | SSL_EXT_TLS1_3_SERVER_HELLO,
1549
41.4k
                &extensions, NULL, 1)) {
1550
            /* SSLfatal() already called */
1551
346
            goto err;
1552
346
        }
1553
1554
41.0k
        if (!ssl_choose_client_version(s, sversion, extensions)) {
1555
            /* SSLfatal() already called */
1556
548
            goto err;
1557
548
        }
1558
41.0k
    }
1559
1560
40.9k
    if (SSL_CONNECTION_IS_TLS13(s) || hrr) {
1561
15.4k
        if (compression != 0) {
1562
21
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1563
21
                SSL_R_INVALID_COMPRESSION_ALGORITHM);
1564
21
            goto err;
1565
21
        }
1566
1567
15.4k
        if (session_id_len != s->tmp_session_id_len
1568
15.4k
            || memcmp(PACKET_data(&session_id), s->tmp_session_id,
1569
15.4k
                   session_id_len)
1570
15.4k
                != 0) {
1571
42
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INVALID_SESSION_ID);
1572
42
            goto err;
1573
42
        }
1574
15.4k
    }
1575
1576
40.9k
    if (hrr) {
1577
408
        if (!set_client_ciphersuite(s, cipherchars)) {
1578
            /* SSLfatal() already called */
1579
23
            goto err;
1580
23
        }
1581
1582
385
        return tls_process_as_hello_retry_request(s, &extpkt);
1583
408
    }
1584
1585
    /*
1586
     * Now we have chosen the version we need to check again that the extensions
1587
     * are appropriate for this version.
1588
     */
1589
40.4k
    context = SSL_CONNECTION_IS_TLS13(s) ? SSL_EXT_TLS1_3_SERVER_HELLO
1590
40.4k
                                         : SSL_EXT_TLS1_2_SERVER_HELLO;
1591
40.4k
    if (!tls_validate_all_contexts(s, context, extensions)) {
1592
23
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
1593
23
        goto err;
1594
23
    }
1595
1596
40.4k
    s->hit = 0;
1597
1598
40.4k
    if (SSL_CONNECTION_IS_TLS13(s)) {
1599
        /*
1600
         * In TLSv1.3 a ServerHello message signals a key change so the end of
1601
         * the message must be on a record boundary.
1602
         */
1603
14.9k
        if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
1604
6
            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1605
6
                SSL_R_NOT_ON_RECORD_BOUNDARY);
1606
6
            goto err;
1607
6
        }
1608
1609
        /* This will set s->hit if we are resuming */
1610
14.9k
        if (!tls_parse_extension(s, TLSEXT_IDX_psk,
1611
14.9k
                SSL_EXT_TLS1_3_SERVER_HELLO,
1612
14.9k
                extensions, NULL, 0)) {
1613
            /* SSLfatal() already called */
1614
0
            goto err;
1615
0
        }
1616
25.5k
    } else {
1617
        /*
1618
         * Check if we can resume the session based on external pre-shared
1619
         * secret. EAP-FAST (RFC 4851) supports two types of session resumption.
1620
         * Resumption based on server-side state works with session IDs.
1621
         * Resumption based on pre-shared Protected Access Credentials (PACs)
1622
         * works by overriding the SessionTicket extension at the application
1623
         * layer, and does not send a session ID. (We do not know whether
1624
         * EAP-FAST servers would honour the session ID.) Therefore, the session
1625
         * ID alone is not a reliable indicator of session resumption, so we
1626
         * first check if we can resume, and later peek at the next handshake
1627
         * message to see if the server wants to resume.
1628
         */
1629
25.5k
        if (s->version >= TLS1_VERSION
1630
24.8k
            && s->ext.session_secret_cb != NULL && s->session->ext.tick) {
1631
0
            const SSL_CIPHER *pref_cipher = NULL;
1632
            /*
1633
             * s->session->master_key_length is a size_t, but this is an int for
1634
             * backwards compat reasons
1635
             */
1636
0
            int master_key_length;
1637
1638
0
            master_key_length = sizeof(s->session->master_key);
1639
0
            if (s->ext.session_secret_cb(ussl, s->session->master_key,
1640
0
                    &master_key_length,
1641
0
                    NULL, &pref_cipher,
1642
0
                    s->ext.session_secret_cb_arg)
1643
0
                && master_key_length > 0) {
1644
0
                s->session->master_key_length = master_key_length;
1645
0
                s->session->cipher = pref_cipher ? pref_cipher : ssl_get_cipher_by_char(s, cipherchars, 0);
1646
0
            } else {
1647
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1648
0
                goto err;
1649
0
            }
1650
0
        }
1651
1652
25.5k
        if (session_id_len != 0
1653
11.1k
            && session_id_len == s->session->session_id_length
1654
41
            && memcmp(PACKET_data(&session_id), s->session->session_id,
1655
41
                   session_id_len)
1656
41
                == 0)
1657
16
            s->hit = 1;
1658
25.5k
    }
1659
1660
40.4k
    if (s->hit) {
1661
16
        if (s->sid_ctx_length != s->session->sid_ctx_length
1662
16
            || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1663
            /* actually a client application bug */
1664
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1665
0
                SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1666
0
            goto err;
1667
0
        }
1668
40.4k
    } else {
1669
        /*
1670
         * If we were trying for session-id reuse but the server
1671
         * didn't resume, make a new SSL_SESSION.
1672
         * In the case of EAP-FAST and PAC, we do not send a session ID,
1673
         * so the PAC-based session secret is always preserved. It'll be
1674
         * overwritten if the server refuses resumption.
1675
         */
1676
40.4k
        if (s->session->session_id_length > 0) {
1677
28
            ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_miss);
1678
28
            if (!ssl_get_new_session(s, 0)) {
1679
                /* SSLfatal() already called */
1680
0
                goto err;
1681
0
            }
1682
28
        }
1683
1684
40.4k
        s->session->ssl_version = s->version;
1685
        /*
1686
         * In TLSv1.2 and below we save the session id we were sent so we can
1687
         * resume it later. In TLSv1.3 the session id we were sent is just an
1688
         * echo of what we originally sent in the ClientHello and should not be
1689
         * used for resumption.
1690
         */
1691
40.4k
        if (!SSL_CONNECTION_IS_TLS13(s)) {
1692
25.4k
            s->session->session_id_length = session_id_len;
1693
            /* session_id_len could be 0 */
1694
25.4k
            if (session_id_len > 0)
1695
11.0k
                memcpy(s->session->session_id, PACKET_data(&session_id),
1696
11.0k
                    session_id_len);
1697
25.4k
        }
1698
40.4k
    }
1699
1700
    /* Session version and negotiated protocol version should match */
1701
40.4k
    if (s->version != s->session->ssl_version) {
1702
0
        SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1703
0
            SSL_R_SSL_SESSION_VERSION_MISMATCH);
1704
0
        goto err;
1705
0
    }
1706
    /*
1707
     * Now that we know the version, update the check to see if it's an allowed
1708
     * version.
1709
     */
1710
40.4k
    s->s3.tmp.min_ver = s->version;
1711
40.4k
    s->s3.tmp.max_ver = s->version;
1712
1713
40.4k
    if (!set_client_ciphersuite(s, cipherchars)) {
1714
        /* SSLfatal() already called */
1715
233
        goto err;
1716
233
    }
1717
1718
#ifdef OPENSSL_NO_COMP
1719
    if (compression != 0) {
1720
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1721
            SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1722
        goto err;
1723
    }
1724
    /*
1725
     * If compression is disabled we'd better not try to resume a session
1726
     * using compression.
1727
     */
1728
    if (s->session->compress_meth != 0) {
1729
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION);
1730
        goto err;
1731
    }
1732
#else
1733
40.2k
    if (s->hit && compression != s->session->compress_meth) {
1734
5
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1735
5
            SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1736
5
        goto err;
1737
5
    }
1738
40.2k
    if (compression == 0)
1739
40.1k
        comp = NULL;
1740
65
    else if (!ssl_allow_compression(s)) {
1741
65
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COMPRESSION_DISABLED);
1742
65
        goto err;
1743
65
    } else {
1744
0
        comp = ssl3_comp_find(SSL_CONNECTION_GET_CTX(s)->comp_methods,
1745
0
            compression);
1746
0
    }
1747
1748
40.1k
    if (compression != 0 && comp == NULL) {
1749
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1750
0
            SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1751
0
        goto err;
1752
40.1k
    } else {
1753
40.1k
        s->s3.tmp.new_compression = comp;
1754
40.1k
    }
1755
40.1k
#endif
1756
1757
40.1k
    if (!tls_parse_all_extensions(s, context, extensions, NULL, 0, 1)) {
1758
        /* SSLfatal() already called */
1759
446
        goto err;
1760
446
    }
1761
1762
#ifndef OPENSSL_NO_SCTP
1763
    if (SSL_CONNECTION_IS_DTLS(s) && s->hit) {
1764
        unsigned char sctpauthkey[64];
1765
        char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1766
        size_t labellen;
1767
1768
        /*
1769
         * Add new shared key for SCTP-Auth, will be ignored if
1770
         * no SCTP used.
1771
         */
1772
        memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1773
            sizeof(DTLS1_SCTP_AUTH_LABEL));
1774
1775
        /* Don't include the terminating zero. */
1776
        labellen = sizeof(labelbuffer) - 1;
1777
        if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
1778
            labellen += 1;
1779
1780
        if (SSL_export_keying_material(ssl, sctpauthkey,
1781
                sizeof(sctpauthkey),
1782
                labelbuffer,
1783
                labellen, NULL, 0, 0)
1784
            <= 0) {
1785
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1786
            goto err;
1787
        }
1788
1789
        BIO_ctrl(SSL_get_wbio(ssl),
1790
            BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1791
            sizeof(sctpauthkey), sctpauthkey);
1792
    }
1793
#endif
1794
1795
    /*
1796
     * In TLSv1.3 we have some post-processing to change cipher state, otherwise
1797
     * we're done with this message
1798
     */
1799
39.7k
    if (SSL_CONNECTION_IS_TLS13(s)) {
1800
14.6k
        if (!ssl->method->ssl3_enc->setup_key_block(s)
1801
14.6k
            || !tls13_store_handshake_traffic_hash(s)) {
1802
            /* SSLfatal() already called */
1803
0
            goto err;
1804
0
        }
1805
        /*
1806
         * If we're not doing early-data and we're not going to send a dummy CCS
1807
         * (i.e. no middlebox compat mode) then we can change the write keys
1808
         * immediately. Otherwise we have to defer this until after all possible
1809
         * early data is written. We could just always defer until the last
1810
         * moment except QUIC needs it done at the same time as the read keys
1811
         * are changed. Since QUIC doesn't do TLS early data or need middlebox
1812
         * compat this doesn't cause a problem.
1813
         */
1814
14.6k
        if (SSL_IS_QUIC_HANDSHAKE(s)
1815
311
            || (s->early_data_state == SSL_EARLY_DATA_NONE
1816
14.3k
                && (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0)) {
1817
14.3k
            if (!ssl->method->ssl3_enc->change_cipher_state(s,
1818
14.3k
                    SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
1819
                /* SSLfatal() already called */
1820
0
                goto err;
1821
0
            }
1822
14.3k
        }
1823
14.6k
        if (!ssl->method->ssl3_enc->change_cipher_state(s,
1824
14.6k
                SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
1825
            /* SSLfatal() already called */
1826
14
            goto err;
1827
14
        }
1828
14.6k
    }
1829
1830
39.7k
    OPENSSL_free(extensions);
1831
39.7k
    return MSG_PROCESS_CONTINUE_READING;
1832
2.66k
err:
1833
2.66k
    OPENSSL_free(extensions);
1834
2.66k
    return MSG_PROCESS_ERROR;
1835
39.7k
}
1836
1837
static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL_CONNECTION *s,
1838
    PACKET *extpkt)
1839
705
{
1840
705
    RAW_EXTENSION *extensions = NULL;
1841
1842
    /*
1843
     * If we were sending early_data then any alerts should not be sent using
1844
     * the old wrlmethod.
1845
     */
1846
705
    if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING
1847
0
        && !ssl_set_new_record_layer(s,
1848
0
            TLS_ANY_VERSION,
1849
0
            OSSL_RECORD_DIRECTION_WRITE,
1850
0
            OSSL_RECORD_PROTECTION_LEVEL_NONE,
1851
0
            NULL, 0, NULL, 0, NULL, 0, NULL, 0,
1852
0
            NULL, 0, NID_undef, NULL, NULL, NULL)) {
1853
        /* SSLfatal already called */
1854
0
        goto err;
1855
0
    }
1856
    /* We are definitely going to be using TLSv1.3 */
1857
705
    s->rlayer.wrlmethod->set_protocol_version(s->rlayer.wrl, TLS1_3_VERSION);
1858
1859
705
    if (!tls_collect_extensions(s, extpkt, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
1860
705
            &extensions, NULL, 1)
1861
657
        || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST,
1862
657
            extensions, NULL, 0, 1)) {
1863
        /* SSLfatal() already called */
1864
313
        goto err;
1865
313
    }
1866
1867
392
    OPENSSL_free(extensions);
1868
392
    extensions = NULL;
1869
1870
392
    if (s->ext.tls13_cookie_len == 0 && s->s3.tmp.pkey != NULL) {
1871
        /*
1872
         * We didn't receive a cookie or a new key_share so the next
1873
         * ClientHello will not change
1874
         */
1875
14
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CHANGE_FOLLOWING_HRR);
1876
14
        goto err;
1877
14
    }
1878
1879
    /*
1880
     * Re-initialise the Transcript Hash. We're going to prepopulate it with
1881
     * a synthetic message_hash in place of ClientHello1.
1882
     */
1883
378
    if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
1884
        /* SSLfatal() already called */
1885
0
        goto err;
1886
0
    }
1887
1888
    /*
1889
     * Add this message to the Transcript Hash. Normally this is done
1890
     * automatically prior to the message processing stage. However due to the
1891
     * need to create the synthetic message hash, we defer that step until now
1892
     * for HRR messages.
1893
     */
1894
378
    if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1895
378
            s->init_num + SSL3_HM_HEADER_LENGTH)) {
1896
        /* SSLfatal() already called */
1897
0
        goto err;
1898
0
    }
1899
1900
378
    return MSG_PROCESS_FINISHED_READING;
1901
327
err:
1902
327
    OPENSSL_free(extensions);
1903
327
    return MSG_PROCESS_ERROR;
1904
378
}
1905
1906
MSG_PROCESS_RETURN tls_process_server_rpk(SSL_CONNECTION *sc, PACKET *pkt)
1907
0
{
1908
0
    EVP_PKEY *peer_rpk = NULL;
1909
1910
0
    if (!tls_process_rpk(sc, pkt, &peer_rpk)) {
1911
        /* SSLfatal() already called */
1912
0
        return MSG_PROCESS_ERROR;
1913
0
    }
1914
1915
0
    if (peer_rpk == NULL) {
1916
0
        SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_CERTIFICATE);
1917
0
        return MSG_PROCESS_ERROR;
1918
0
    }
1919
1920
0
    EVP_PKEY_free(sc->session->peer_rpk);
1921
0
    sc->session->peer_rpk = peer_rpk;
1922
1923
0
    return MSG_PROCESS_CONTINUE_PROCESSING;
1924
0
}
1925
1926
static WORK_STATE tls_post_process_server_rpk(SSL_CONNECTION *sc,
1927
    WORK_STATE wst)
1928
0
{
1929
0
    size_t certidx;
1930
0
    const SSL_CERT_LOOKUP *clu;
1931
0
    int v_ok;
1932
1933
0
    if (sc->session->peer_rpk == NULL) {
1934
0
        SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER,
1935
0
            SSL_R_INVALID_RAW_PUBLIC_KEY);
1936
0
        return WORK_ERROR;
1937
0
    }
1938
1939
0
    if (sc->rwstate == SSL_RETRY_VERIFY)
1940
0
        sc->rwstate = SSL_NOTHING;
1941
1942
0
    ERR_set_mark();
1943
0
    v_ok = ssl_verify_rpk(sc, sc->session->peer_rpk);
1944
0
    if (v_ok <= 0 && sc->verify_mode != SSL_VERIFY_NONE) {
1945
0
        ERR_clear_last_mark();
1946
0
        SSLfatal(sc, ssl_x509err2alert(sc->verify_result),
1947
0
            SSL_R_CERTIFICATE_VERIFY_FAILED);
1948
0
        return WORK_ERROR;
1949
0
    }
1950
0
    ERR_pop_to_mark(); /* but we keep s->verify_result */
1951
0
    if (v_ok > 0 && sc->rwstate == SSL_RETRY_VERIFY) {
1952
0
        return WORK_MORE_A;
1953
0
    }
1954
1955
0
    if ((clu = ssl_cert_lookup_by_pkey(sc->session->peer_rpk, &certidx,
1956
0
             SSL_CONNECTION_GET_CTX(sc)))
1957
0
        == NULL) {
1958
0
        SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1959
0
        return WORK_ERROR;
1960
0
    }
1961
1962
    /*
1963
     * Check certificate type is consistent with ciphersuite. For TLS 1.3
1964
     * skip check since TLS 1.3 ciphersuites can be used with any certificate
1965
     * type.
1966
     */
1967
0
    if (!SSL_CONNECTION_IS_TLS13(sc)) {
1968
0
        if ((clu->amask & sc->s3.tmp.new_cipher->algorithm_auth) == 0) {
1969
0
            SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_RPK_TYPE);
1970
0
            return WORK_ERROR;
1971
0
        }
1972
0
    }
1973
1974
    /* Ensure there is no peer/peer_chain */
1975
0
    X509_free(sc->session->peer);
1976
0
    sc->session->peer = NULL;
1977
0
    sk_X509_pop_free(sc->session->peer_chain, X509_free);
1978
0
    sc->session->peer_chain = NULL;
1979
0
    sc->session->verify_result = sc->verify_result;
1980
1981
    /* Save the current hash state for when we receive the CertificateVerify */
1982
0
    if (SSL_CONNECTION_IS_TLS13(sc)
1983
0
        && !ssl_handshake_hash(sc, sc->cert_verify_hash,
1984
0
            sizeof(sc->cert_verify_hash),
1985
0
            &sc->cert_verify_hash_len)) {
1986
        /* SSLfatal() already called */
1987
0
        return WORK_ERROR;
1988
0
    }
1989
1990
0
    return WORK_FINISHED_CONTINUE;
1991
0
}
1992
1993
/* prepare server cert verification by setting s->session->peer_chain from pkt */
1994
MSG_PROCESS_RETURN tls_process_server_certificate(SSL_CONNECTION *s,
1995
    PACKET *pkt)
1996
48.2k
{
1997
48.2k
    unsigned long cert_list_len, cert_len;
1998
48.2k
    X509 *x = NULL;
1999
48.2k
    const unsigned char *certstart, *certbytes;
2000
48.2k
    size_t chainidx;
2001
48.2k
    unsigned int context = 0;
2002
48.2k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2003
2004
48.2k
    if (s->ext.server_cert_type == TLSEXT_cert_type_rpk)
2005
0
        return tls_process_server_rpk(s, pkt);
2006
48.2k
    if (s->ext.server_cert_type != TLSEXT_cert_type_x509) {
2007
0
        SSLfatal(s, SSL_AD_UNSUPPORTED_CERTIFICATE,
2008
0
            SSL_R_UNKNOWN_CERTIFICATE_TYPE);
2009
0
        goto err;
2010
0
    }
2011
2012
48.2k
    if ((s->session->peer_chain = sk_X509_new_null()) == NULL) {
2013
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
2014
0
        goto err;
2015
0
    }
2016
2017
48.2k
    if ((SSL_CONNECTION_IS_TLS13(s) && !PACKET_get_1(pkt, &context))
2018
48.2k
        || context != 0
2019
48.2k
        || !PACKET_get_net_3(pkt, &cert_list_len)
2020
48.2k
        || PACKET_remaining(pkt) != cert_list_len
2021
47.8k
        || PACKET_remaining(pkt) == 0) {
2022
461
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2023
461
        goto err;
2024
461
    }
2025
86.0k
    for (chainidx = 0; PACKET_remaining(pkt); chainidx++) {
2026
55.0k
        if (!PACKET_get_net_3(pkt, &cert_len)
2027
55.0k
            || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
2028
270
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
2029
270
            goto err;
2030
270
        }
2031
2032
54.7k
        certstart = certbytes;
2033
54.7k
        x = X509_new_ex(sctx->libctx, sctx->propq);
2034
54.7k
        if (x == NULL) {
2035
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB);
2036
0
            goto err;
2037
0
        }
2038
54.7k
        if (d2i_X509(&x, (const unsigned char **)&certbytes,
2039
54.7k
                cert_len)
2040
54.7k
            == NULL) {
2041
16.4k
            SSLfatal(s, SSL_AD_BAD_CERTIFICATE, ERR_R_ASN1_LIB);
2042
16.4k
            goto err;
2043
16.4k
        }
2044
2045
38.2k
        if (certbytes != (certstart + cert_len)) {
2046
30
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH);
2047
30
            goto err;
2048
30
        }
2049
2050
38.2k
        if (SSL_CONNECTION_IS_TLS13(s)) {
2051
17.2k
            RAW_EXTENSION *rawexts = NULL;
2052
17.2k
            PACKET extensions;
2053
2054
17.2k
            if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
2055
65
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
2056
65
                goto err;
2057
65
            }
2058
17.1k
            if (!tls_collect_extensions(s, &extensions,
2059
17.1k
                    SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
2060
17.1k
                    NULL, chainidx == 0)
2061
17.1k
                || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
2062
17.1k
                    rawexts, x, chainidx,
2063
17.1k
                    PACKET_remaining(pkt) == 0)) {
2064
5
                OPENSSL_free(rawexts);
2065
                /* SSLfatal already called */
2066
5
                goto err;
2067
5
            }
2068
17.1k
            OPENSSL_free(rawexts);
2069
17.1k
        }
2070
2071
38.1k
        if (!sk_X509_push(s->session->peer_chain, x)) {
2072
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
2073
0
            goto err;
2074
0
        }
2075
38.1k
        x = NULL;
2076
38.1k
    }
2077
30.9k
    return MSG_PROCESS_CONTINUE_PROCESSING;
2078
2079
17.2k
err:
2080
17.2k
    X509_free(x);
2081
17.2k
    OSSL_STACK_OF_X509_free(s->session->peer_chain);
2082
17.2k
    s->session->peer_chain = NULL;
2083
17.2k
    return MSG_PROCESS_ERROR;
2084
47.8k
}
2085
2086
/*
2087
 * Verify the s->session->peer_chain and check server cert type.
2088
 * On success set s->session->peer and s->session->verify_result.
2089
 * Else the peer certificate verification callback may request retry.
2090
 */
2091
WORK_STATE tls_post_process_server_certificate(SSL_CONNECTION *s,
2092
    WORK_STATE wst)
2093
19.0k
{
2094
19.0k
    X509 *x;
2095
19.0k
    EVP_PKEY *pkey = NULL;
2096
19.0k
    const SSL_CERT_LOOKUP *clu;
2097
19.0k
    size_t certidx;
2098
19.0k
    int i;
2099
2100
19.0k
    if (s->ext.server_cert_type == TLSEXT_cert_type_rpk)
2101
0
        return tls_post_process_server_rpk(s, wst);
2102
2103
19.0k
    if (s->rwstate == SSL_RETRY_VERIFY)
2104
0
        s->rwstate = SSL_NOTHING;
2105
2106
    /*
2107
     * The documented interface is that SSL_VERIFY_PEER should be set in order
2108
     * for client side verification of the server certificate to take place.
2109
     * However, historically the code has only checked that *any* flag is set
2110
     * to cause server verification to take place. Use of the other flags makes
2111
     * no sense in client mode. An attempt to clean up the semantics was
2112
     * reverted because at least one application *only* set
2113
     * SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused
2114
     * server verification to take place, after the clean up it silently did
2115
     * nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags
2116
     * sent to them because they are void functions. Therefore, we now use the
2117
     * (less clean) historic behaviour of performing validation if any flag is
2118
     * set. The *documented* interface remains the same.
2119
     */
2120
19.0k
    ERR_set_mark();
2121
19.0k
    i = ssl_verify_cert_chain(s, s->session->peer_chain);
2122
19.0k
    if (i <= 0 && s->verify_mode != SSL_VERIFY_NONE) {
2123
0
        ERR_clear_last_mark();
2124
0
        SSLfatal(s, ssl_x509err2alert(s->verify_result),
2125
0
            SSL_R_CERTIFICATE_VERIFY_FAILED);
2126
0
        return WORK_ERROR;
2127
0
    }
2128
19.0k
    ERR_pop_to_mark(); /* but we keep s->verify_result */
2129
19.0k
    if (i > 0 && s->rwstate == SSL_RETRY_VERIFY)
2130
0
        return WORK_MORE_A;
2131
2132
    /*
2133
     * Inconsistency alert: cert_chain does include the peer's certificate,
2134
     * which we don't include in statem_srvr.c
2135
     */
2136
19.0k
    x = sk_X509_value(s->session->peer_chain, 0);
2137
2138
19.0k
    pkey = X509_get0_pubkey(x);
2139
2140
19.0k
    if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
2141
856
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2142
856
            SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
2143
856
        return WORK_ERROR;
2144
856
    }
2145
2146
18.2k
    if ((clu = ssl_cert_lookup_by_pkey(pkey, &certidx,
2147
18.2k
             SSL_CONNECTION_GET_CTX(s)))
2148
18.2k
        == NULL) {
2149
20
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
2150
20
        return WORK_ERROR;
2151
20
    }
2152
    /*
2153
     * Check certificate type is consistent with ciphersuite. For TLS 1.3
2154
     * skip check since TLS 1.3 ciphersuites can be used with any certificate
2155
     * type.
2156
     */
2157
18.2k
    if (!SSL_CONNECTION_IS_TLS13(s)) {
2158
8.22k
        if ((clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0) {
2159
96
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CERTIFICATE_TYPE);
2160
96
            return WORK_ERROR;
2161
96
        }
2162
8.22k
    }
2163
2164
18.1k
    if (!X509_up_ref(x)) {
2165
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2166
0
        return WORK_ERROR;
2167
0
    }
2168
2169
18.1k
    X509_free(s->session->peer);
2170
18.1k
    s->session->peer = x;
2171
18.1k
    s->session->verify_result = s->verify_result;
2172
    /* Ensure there is no RPK */
2173
18.1k
    EVP_PKEY_free(s->session->peer_rpk);
2174
18.1k
    s->session->peer_rpk = NULL;
2175
2176
    /* Save the current hash state for when we receive the CertificateVerify */
2177
18.1k
    if (SSL_CONNECTION_IS_TLS13(s)
2178
9.98k
        && !ssl_handshake_hash(s, s->cert_verify_hash,
2179
9.98k
            sizeof(s->cert_verify_hash),
2180
9.98k
            &s->cert_verify_hash_len)) {
2181
0
        /* SSLfatal() already called */;
2182
0
        return WORK_ERROR;
2183
0
    }
2184
18.1k
    return WORK_FINISHED_CONTINUE;
2185
18.1k
}
2186
2187
#ifndef OPENSSL_NO_COMP_ALG
2188
MSG_PROCESS_RETURN tls_process_server_compressed_certificate(SSL_CONNECTION *sc, PACKET *pkt)
2189
{
2190
    MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
2191
    PACKET tmppkt;
2192
    BUF_MEM *buf = BUF_MEM_new();
2193
2194
    if (tls13_process_compressed_certificate(sc, pkt, &tmppkt, buf) != MSG_PROCESS_ERROR)
2195
        ret = tls_process_server_certificate(sc, &tmppkt);
2196
2197
    BUF_MEM_free(buf);
2198
    return ret;
2199
}
2200
#endif
2201
2202
static int tls_process_ske_psk_preamble(SSL_CONNECTION *s, PACKET *pkt)
2203
0
{
2204
0
#ifndef OPENSSL_NO_PSK
2205
0
    PACKET psk_identity_hint;
2206
2207
    /* PSK ciphersuites are preceded by an identity hint */
2208
2209
0
    if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
2210
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2211
0
        return 0;
2212
0
    }
2213
2214
    /*
2215
     * Store PSK identity hint for later use, hint is used in
2216
     * tls_construct_client_key_exchange.  Assume that the maximum length of
2217
     * a PSK identity hint can be as long as the maximum length of a PSK
2218
     * identity.
2219
     */
2220
0
    if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
2221
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DATA_LENGTH_TOO_LONG);
2222
0
        return 0;
2223
0
    }
2224
2225
0
    if (PACKET_remaining(&psk_identity_hint) == 0) {
2226
0
        OPENSSL_free(s->session->psk_identity_hint);
2227
0
        s->session->psk_identity_hint = NULL;
2228
0
    } else if (!PACKET_strndup(&psk_identity_hint,
2229
0
                   &s->session->psk_identity_hint)) {
2230
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2231
0
        return 0;
2232
0
    }
2233
2234
0
    return 1;
2235
#else
2236
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2237
    return 0;
2238
#endif
2239
0
}
2240
2241
static int tls_process_ske_srp(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey)
2242
0
{
2243
0
#ifndef OPENSSL_NO_SRP
2244
0
    PACKET prime, generator, salt, server_pub;
2245
2246
0
    if (!PACKET_get_length_prefixed_2(pkt, &prime)
2247
0
        || !PACKET_get_length_prefixed_2(pkt, &generator)
2248
0
        || !PACKET_get_length_prefixed_1(pkt, &salt)
2249
0
        || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
2250
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2251
0
        return 0;
2252
0
    }
2253
2254
0
    if ((s->srp_ctx.N = BN_bin2bn(PACKET_data(&prime),
2255
0
             (int)PACKET_remaining(&prime), NULL))
2256
0
            == NULL
2257
0
        || (s->srp_ctx.g = BN_bin2bn(PACKET_data(&generator),
2258
0
                (int)PACKET_remaining(&generator), NULL))
2259
0
            == NULL
2260
0
        || (s->srp_ctx.s = BN_bin2bn(PACKET_data(&salt),
2261
0
                (int)PACKET_remaining(&salt), NULL))
2262
0
            == NULL
2263
0
        || (s->srp_ctx.B = BN_bin2bn(PACKET_data(&server_pub),
2264
0
                (int)PACKET_remaining(&server_pub), NULL))
2265
0
            == NULL) {
2266
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
2267
0
        return 0;
2268
0
    }
2269
2270
0
    if (!srp_verify_server_param(s)) {
2271
        /* SSLfatal() already called */
2272
0
        return 0;
2273
0
    }
2274
2275
    /* We must check if there is a certificate */
2276
0
    if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
2277
0
        *pkey = tls_get_peer_pkey(s);
2278
2279
0
    return 1;
2280
#else
2281
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2282
    return 0;
2283
#endif
2284
0
}
2285
2286
static int tls_process_ske_dhe(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey)
2287
11.1k
{
2288
11.1k
    PACKET prime, generator, pub_key;
2289
11.1k
    EVP_PKEY *peer_tmp = NULL;
2290
11.1k
    BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL;
2291
11.1k
    EVP_PKEY_CTX *pctx = NULL;
2292
11.1k
    OSSL_PARAM *params = NULL;
2293
11.1k
    OSSL_PARAM_BLD *tmpl = NULL;
2294
11.1k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2295
11.1k
    int ret = 0;
2296
2297
11.1k
    if (!PACKET_get_length_prefixed_2(pkt, &prime)
2298
11.0k
        || !PACKET_get_length_prefixed_2(pkt, &generator)
2299
10.9k
        || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
2300
330
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2301
330
        return 0;
2302
330
    }
2303
2304
10.8k
    p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL);
2305
10.8k
    g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator),
2306
10.8k
        NULL);
2307
10.8k
    bnpub_key = BN_bin2bn(PACKET_data(&pub_key),
2308
10.8k
        (int)PACKET_remaining(&pub_key), NULL);
2309
10.8k
    if (p == NULL || g == NULL || bnpub_key == NULL) {
2310
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB);
2311
0
        goto err;
2312
0
    }
2313
2314
10.8k
    tmpl = OSSL_PARAM_BLD_new();
2315
10.8k
    if (tmpl == NULL
2316
10.8k
        || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
2317
10.8k
        || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g)
2318
10.8k
        || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
2319
10.8k
            bnpub_key)
2320
10.8k
        || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
2321
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2322
0
        goto err;
2323
0
    }
2324
2325
10.8k
    pctx = EVP_PKEY_CTX_new_from_name(sctx->libctx, "DH", sctx->propq);
2326
10.8k
    if (pctx == NULL) {
2327
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2328
0
        goto err;
2329
0
    }
2330
10.8k
    if (EVP_PKEY_fromdata_init(pctx) <= 0
2331
10.8k
        || EVP_PKEY_fromdata(pctx, &peer_tmp, EVP_PKEY_KEYPAIR, params) <= 0) {
2332
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_DH_VALUE);
2333
0
        goto err;
2334
0
    }
2335
2336
10.8k
    EVP_PKEY_CTX_free(pctx);
2337
10.8k
    pctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, peer_tmp, sctx->propq);
2338
10.8k
    if (pctx == NULL
2339
        /*
2340
         * EVP_PKEY_param_check() will verify that the DH params are using
2341
         * a safe prime. In this context, because we're using ephemeral DH,
2342
         * we're ok with it not being a safe prime.
2343
         * EVP_PKEY_param_check_quick() skips the safe prime check.
2344
         */
2345
10.8k
        || EVP_PKEY_param_check_quick(pctx) != 1
2346
8.02k
        || EVP_PKEY_public_check(pctx) != 1) {
2347
3.19k
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_DH_VALUE);
2348
3.19k
        goto err;
2349
3.19k
    }
2350
2351
7.60k
    if (!ssl_security(s, SSL_SECOP_TMP_DH,
2352
7.60k
            EVP_PKEY_get_security_bits(peer_tmp),
2353
7.60k
            0, peer_tmp)) {
2354
15
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL);
2355
15
        goto err;
2356
15
    }
2357
2358
7.59k
    s->s3.peer_tmp = peer_tmp;
2359
7.59k
    peer_tmp = NULL;
2360
2361
    /*
2362
     * FIXME: This makes assumptions about which ciphersuites come with
2363
     * public keys. We should have a less ad-hoc way of doing this
2364
     */
2365
7.59k
    if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS))
2366
3.42k
        *pkey = tls_get_peer_pkey(s);
2367
    /* else anonymous DH, so no certificate or pkey. */
2368
2369
7.59k
    ret = 1;
2370
2371
10.8k
err:
2372
10.8k
    OSSL_PARAM_BLD_free(tmpl);
2373
10.8k
    OSSL_PARAM_free(params);
2374
10.8k
    EVP_PKEY_free(peer_tmp);
2375
10.8k
    EVP_PKEY_CTX_free(pctx);
2376
10.8k
    BN_free(p);
2377
10.8k
    BN_free(g);
2378
10.8k
    BN_free(bnpub_key);
2379
2380
10.8k
    return ret;
2381
7.59k
}
2382
2383
static int tls_process_ske_ecdhe(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey)
2384
8.50k
{
2385
8.50k
    PACKET encoded_pt;
2386
8.50k
    unsigned int curve_type, curve_id;
2387
2388
    /*
2389
     * Extract elliptic curve parameters and the server's ephemeral ECDH
2390
     * public key. We only support named (not generic) curves and
2391
     * ECParameters in this case is just three bytes.
2392
     */
2393
8.50k
    if (!PACKET_get_1(pkt, &curve_type) || !PACKET_get_net_2(pkt, &curve_id)) {
2394
21
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
2395
21
        return 0;
2396
21
    }
2397
    /*
2398
     * Check curve is named curve type and one of our preferences, if not
2399
     * server has sent an invalid curve.
2400
     */
2401
8.48k
    if (curve_type != NAMED_CURVE_TYPE
2402
8.45k
        || !tls1_check_group_id(s, curve_id, 1)) {
2403
185
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE);
2404
185
        return 0;
2405
185
    }
2406
2407
8.30k
    if ((s->s3.peer_tmp = ssl_generate_param_group(s, curve_id)) == NULL) {
2408
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2409
0
            SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
2410
0
        return 0;
2411
0
    }
2412
2413
8.30k
    if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
2414
74
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2415
74
        return 0;
2416
74
    }
2417
2418
8.22k
    if (EVP_PKEY_set1_encoded_public_key(s->s3.peer_tmp,
2419
8.22k
            PACKET_data(&encoded_pt),
2420
8.22k
            PACKET_remaining(&encoded_pt))
2421
8.22k
        <= 0) {
2422
1.07k
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT);
2423
1.07k
        return 0;
2424
1.07k
    }
2425
2426
    /*
2427
     * The ECC/TLS specification does not mention the use of DSA to sign
2428
     * ECParameters in the server key exchange message. We do support RSA
2429
     * and ECDSA.
2430
     */
2431
7.14k
    if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA)
2432
1.44k
        *pkey = tls_get_peer_pkey(s);
2433
5.70k
    else if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aRSA)
2434
849
        *pkey = tls_get_peer_pkey(s);
2435
    /* else anonymous ECDH, so no certificate or pkey. */
2436
2437
    /* Cache the agreed upon group in the SSL_SESSION */
2438
7.14k
    s->session->kex_group = curve_id;
2439
7.14k
    return 1;
2440
8.22k
}
2441
2442
MSG_PROCESS_RETURN tls_process_key_exchange(SSL_CONNECTION *s, PACKET *pkt)
2443
19.6k
{
2444
19.6k
    long alg_k;
2445
19.6k
    EVP_PKEY *pkey = NULL;
2446
19.6k
    EVP_MD_CTX *md_ctx = NULL;
2447
19.6k
    EVP_PKEY_CTX *pctx = NULL;
2448
19.6k
    PACKET save_param_start, signature;
2449
19.6k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2450
2451
19.6k
    alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
2452
2453
19.6k
    save_param_start = *pkt;
2454
2455
19.6k
    EVP_PKEY_free(s->s3.peer_tmp);
2456
19.6k
    s->s3.peer_tmp = NULL;
2457
2458
19.6k
    if (alg_k & SSL_PSK) {
2459
0
        if (!tls_process_ske_psk_preamble(s, pkt)) {
2460
            /* SSLfatal() already called */
2461
0
            goto err;
2462
0
        }
2463
0
    }
2464
2465
    /* Nothing else to do for plain PSK or RSAPSK */
2466
19.6k
    if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
2467
19.6k
    } else if (alg_k & SSL_kSRP) {
2468
0
        if (!tls_process_ske_srp(s, pkt, &pkey)) {
2469
            /* SSLfatal() already called */
2470
0
            goto err;
2471
0
        }
2472
19.6k
    } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
2473
11.1k
        if (!tls_process_ske_dhe(s, pkt, &pkey)) {
2474
            /* SSLfatal() already called */
2475
3.54k
            goto err;
2476
3.54k
        }
2477
11.1k
    } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
2478
8.50k
        if (!tls_process_ske_ecdhe(s, pkt, &pkey)) {
2479
            /* SSLfatal() already called */
2480
1.35k
            goto err;
2481
1.35k
        }
2482
8.50k
    } else if (alg_k) {
2483
0
        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
2484
0
        goto err;
2485
0
    }
2486
2487
    /* if it was signed, check the signature */
2488
14.7k
    if (pkey != NULL) {
2489
5.71k
        PACKET params;
2490
5.71k
        const EVP_MD *md = NULL;
2491
5.71k
        unsigned char *tbs;
2492
5.71k
        size_t tbslen;
2493
5.71k
        int rv;
2494
2495
        /*
2496
         * |pkt| now points to the beginning of the signature, so the difference
2497
         * equals the length of the parameters.
2498
         */
2499
5.71k
        if (!PACKET_get_sub_packet(&save_param_start, &params,
2500
5.71k
                PACKET_remaining(&save_param_start) - PACKET_remaining(pkt))) {
2501
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR);
2502
0
            goto err;
2503
0
        }
2504
2505
5.71k
        if (SSL_USE_SIGALGS(s)) {
2506
3.77k
            unsigned int sigalg;
2507
2508
3.77k
            if (!PACKET_get_net_2(pkt, &sigalg)) {
2509
21
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT);
2510
21
                goto err;
2511
21
            }
2512
3.75k
            if (tls12_check_peer_sigalg(s, sigalg, pkey) <= 0) {
2513
                /* SSLfatal() already called */
2514
197
                goto err;
2515
197
            }
2516
3.75k
        } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
2517
9
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2518
9
                SSL_R_LEGACY_SIGALG_DISALLOWED_OR_UNSUPPORTED);
2519
9
            goto err;
2520
9
        }
2521
2522
5.49k
        if (!tls1_lookup_md(sctx, s->s3.tmp.peer_sigalg, &md)) {
2523
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2524
0
                SSL_R_NO_SUITABLE_DIGEST_ALGORITHM);
2525
0
            goto err;
2526
0
        }
2527
5.49k
        if (SSL_USE_SIGALGS(s))
2528
5.49k
            OSSL_TRACE1(TLS, "USING TLSv1.2 HASH %s\n",
2529
5.49k
                md == NULL ? "n/a" : EVP_MD_get0_name(md));
2530
2531
5.49k
        if (!PACKET_get_length_prefixed_2(pkt, &signature)
2532
5.37k
            || PACKET_remaining(pkt) != 0) {
2533
183
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2534
183
            goto err;
2535
183
        }
2536
2537
5.30k
        md_ctx = EVP_MD_CTX_new();
2538
5.30k
        if (md_ctx == NULL) {
2539
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2540
0
            goto err;
2541
0
        }
2542
2543
5.30k
        if (EVP_DigestVerifyInit_ex(md_ctx, &pctx,
2544
5.30k
                md == NULL ? NULL : EVP_MD_get0_name(md),
2545
5.30k
                sctx->libctx, sctx->propq, pkey,
2546
5.30k
                NULL)
2547
5.30k
            <= 0) {
2548
39
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2549
39
            goto err;
2550
39
        }
2551
5.26k
        if (SSL_USE_PSS(s)) {
2552
472
            if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
2553
472
                || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
2554
472
                       RSA_PSS_SALTLEN_DIGEST)
2555
472
                    <= 0) {
2556
1
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2557
1
                goto err;
2558
1
            }
2559
472
        }
2560
5.26k
        tbslen = construct_key_exchange_tbs(s, &tbs, PACKET_data(&params),
2561
5.26k
            PACKET_remaining(&params));
2562
5.26k
        if (tbslen == 0) {
2563
            /* SSLfatal() already called */
2564
0
            goto err;
2565
0
        }
2566
2567
5.26k
        rv = EVP_DigestVerify(md_ctx, PACKET_data(&signature),
2568
5.26k
            PACKET_remaining(&signature), tbs, tbslen);
2569
5.26k
        OPENSSL_free(tbs);
2570
5.26k
        if (rv <= 0) {
2571
5.19k
            SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_SIGNATURE);
2572
5.19k
            goto err;
2573
5.19k
        }
2574
75
        EVP_MD_CTX_free(md_ctx);
2575
75
        md_ctx = NULL;
2576
9.02k
    } else {
2577
        /* aNULL, aSRP or PSK do not need public keys */
2578
9.02k
        if (!(s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
2579
0
            && !(alg_k & SSL_PSK)) {
2580
            /* Might be wrong key type, check it */
2581
0
            if (ssl3_check_cert_and_algorithm(s)) {
2582
0
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_DATA);
2583
0
            }
2584
            /* else this shouldn't happen, SSLfatal() already called */
2585
0
            goto err;
2586
0
        }
2587
        /* still data left over */
2588
9.02k
        if (PACKET_remaining(pkt) != 0) {
2589
530
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_EXTRA_DATA_IN_MESSAGE);
2590
530
            goto err;
2591
530
        }
2592
9.02k
    }
2593
2594
8.56k
    return MSG_PROCESS_CONTINUE_READING;
2595
11.0k
err:
2596
11.0k
    EVP_MD_CTX_free(md_ctx);
2597
11.0k
    return MSG_PROCESS_ERROR;
2598
14.7k
}
2599
2600
MSG_PROCESS_RETURN tls_process_certificate_request(SSL_CONNECTION *s,
2601
    PACKET *pkt)
2602
852
{
2603
    /* Clear certificate validity flags */
2604
852
    if (s->s3.tmp.valid_flags != NULL)
2605
0
        memset(s->s3.tmp.valid_flags, 0, s->ssl_pkey_num * sizeof(uint32_t));
2606
852
    else
2607
852
        s->s3.tmp.valid_flags = OPENSSL_zalloc(s->ssl_pkey_num * sizeof(uint32_t));
2608
2609
    /* Give up for good if allocation didn't work */
2610
852
    if (s->s3.tmp.valid_flags == NULL)
2611
0
        return 0;
2612
2613
852
    if (SSL_CONNECTION_IS_TLS13(s)) {
2614
168
        PACKET reqctx, extensions;
2615
168
        RAW_EXTENSION *rawexts = NULL;
2616
2617
168
        if ((s->shutdown & SSL_SENT_SHUTDOWN) != 0) {
2618
            /*
2619
             * We already sent close_notify. This can only happen in TLSv1.3
2620
             * post-handshake messages. We can't reasonably respond to this, so
2621
             * we just ignore it
2622
             */
2623
0
            return MSG_PROCESS_FINISHED_READING;
2624
0
        }
2625
2626
        /* Free and zero certificate types: it is not present in TLS 1.3 */
2627
168
        OPENSSL_free(s->s3.tmp.ctype);
2628
168
        s->s3.tmp.ctype = NULL;
2629
168
        s->s3.tmp.ctype_len = 0;
2630
168
        OPENSSL_free(s->pha_context);
2631
168
        s->pha_context = NULL;
2632
168
        s->pha_context_len = 0;
2633
2634
168
        if (!PACKET_get_length_prefixed_1(pkt, &reqctx) || !PACKET_memdup(&reqctx, &s->pha_context, &s->pha_context_len)) {
2635
10
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2636
10
            return MSG_PROCESS_ERROR;
2637
10
        }
2638
2639
158
        if (!PACKET_get_length_prefixed_2(pkt, &extensions)) {
2640
15
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
2641
15
            return MSG_PROCESS_ERROR;
2642
15
        }
2643
143
        if (!tls_collect_extensions(s, &extensions,
2644
143
                SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
2645
143
                &rawexts, NULL, 1)
2646
124
            || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
2647
143
                rawexts, NULL, 0, 1)) {
2648
            /* SSLfatal() already called */
2649
143
            OPENSSL_free(rawexts);
2650
143
            return MSG_PROCESS_ERROR;
2651
143
        }
2652
0
        OPENSSL_free(rawexts);
2653
0
        if (!tls1_process_sigalgs(s)) {
2654
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_LENGTH);
2655
0
            return MSG_PROCESS_ERROR;
2656
0
        }
2657
684
    } else {
2658
684
        PACKET ctypes;
2659
2660
        /* get the certificate types */
2661
684
        if (!PACKET_get_length_prefixed_1(pkt, &ctypes)) {
2662
18
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2663
18
            return MSG_PROCESS_ERROR;
2664
18
        }
2665
2666
666
        if (!PACKET_memdup(&ctypes, &s->s3.tmp.ctype, &s->s3.tmp.ctype_len)) {
2667
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2668
0
            return MSG_PROCESS_ERROR;
2669
0
        }
2670
2671
666
        if (SSL_USE_SIGALGS(s)) {
2672
592
            PACKET sigalgs;
2673
2674
592
            if (!PACKET_get_length_prefixed_2(pkt, &sigalgs)) {
2675
84
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2676
84
                return MSG_PROCESS_ERROR;
2677
84
            }
2678
2679
            /*
2680
             * Despite this being for certificates, preserve compatibility
2681
             * with pre-TLS 1.3 and use the regular sigalgs field.
2682
             */
2683
508
            if (!tls1_save_sigalgs(s, &sigalgs, 0)) {
2684
25
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2685
25
                    SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2686
25
                return MSG_PROCESS_ERROR;
2687
25
            }
2688
483
            if (!tls1_process_sigalgs(s)) {
2689
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
2690
0
                return MSG_PROCESS_ERROR;
2691
0
            }
2692
483
        }
2693
2694
        /* get the CA RDNs */
2695
557
        if (!parse_ca_names(s, pkt)) {
2696
            /* SSLfatal() already called */
2697
477
            return MSG_PROCESS_ERROR;
2698
477
        }
2699
557
    }
2700
2701
80
    if (PACKET_remaining(pkt) != 0) {
2702
35
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2703
35
        return MSG_PROCESS_ERROR;
2704
35
    }
2705
2706
    /* we should setup a certificate to return.... */
2707
45
    s->s3.tmp.cert_req = 1;
2708
2709
    /*
2710
     * In TLSv1.3 we don't prepare the client certificate yet. We wait until
2711
     * after the CertificateVerify message has been received. This is because
2712
     * in TLSv1.3 the CertificateRequest arrives before the Certificate message
2713
     * but in TLSv1.2 it is the other way around. We want to make sure that
2714
     * SSL_get1_peer_certificate() returns something sensible in
2715
     * client_cert_cb.
2716
     */
2717
45
    if (SSL_CONNECTION_IS_TLS13(s)
2718
0
        && s->post_handshake_auth != SSL_PHA_REQUESTED)
2719
0
        return MSG_PROCESS_CONTINUE_READING;
2720
2721
45
    return MSG_PROCESS_CONTINUE_PROCESSING;
2722
45
}
2723
2724
MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL_CONNECTION *s,
2725
    PACKET *pkt)
2726
2.15k
{
2727
2.15k
    unsigned int ticklen;
2728
2.15k
    unsigned long ticket_lifetime_hint, age_add = 0;
2729
2.15k
    unsigned int sess_len;
2730
2.15k
    RAW_EXTENSION *exts = NULL;
2731
2.15k
    PACKET nonce;
2732
2.15k
    EVP_MD *sha256 = NULL;
2733
2.15k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2734
2735
2.15k
    PACKET_null_init(&nonce);
2736
2737
2.15k
    if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
2738
2.13k
        || (SSL_CONNECTION_IS_TLS13(s)
2739
1.85k
            && (!PACKET_get_net_4(pkt, &age_add)
2740
1.84k
                || !PACKET_get_length_prefixed_1(pkt, &nonce)))
2741
2.12k
        || !PACKET_get_net_2(pkt, &ticklen)
2742
2.11k
        || (SSL_CONNECTION_IS_TLS13(s) ? (ticklen == 0
2743
1.83k
                                             || PACKET_remaining(pkt) < ticklen)
2744
2.11k
                                       : PACKET_remaining(pkt) != ticklen)) {
2745
137
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2746
137
        goto err;
2747
137
    }
2748
2749
    /*
2750
     * Server is allowed to change its mind (in <=TLSv1.2) and send an empty
2751
     * ticket. We already checked this TLSv1.3 case above, so it should never
2752
     * be 0 here in that instance
2753
     */
2754
2.01k
    if (ticklen == 0)
2755
23
        return MSG_PROCESS_CONTINUE_READING;
2756
2757
    /*
2758
     * Sessions must be immutable once they go into the session cache. Otherwise
2759
     * we can get multi-thread problems. Therefore we don't "update" sessions,
2760
     * we replace them with a duplicate. In TLSv1.3 we need to do this every
2761
     * time a NewSessionTicket arrives because those messages arrive
2762
     * post-handshake and the session may have already gone into the session
2763
     * cache.
2764
     */
2765
1.99k
    if (SSL_CONNECTION_IS_TLS13(s) || s->session->session_id_length > 0) {
2766
1.81k
        SSL_SESSION *new_sess;
2767
2768
        /*
2769
         * We reused an existing session, so we need to replace it with a new
2770
         * one
2771
         */
2772
1.81k
        if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2773
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
2774
0
            goto err;
2775
0
        }
2776
2777
1.81k
        if ((s->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) != 0
2778
0
            && !SSL_CONNECTION_IS_TLS13(s)) {
2779
            /*
2780
             * In TLSv1.2 and below the arrival of a new tickets signals that
2781
             * any old ticket we were using is now out of date, so we remove the
2782
             * old session from the cache. We carry on if this fails
2783
             */
2784
0
            SSL_CTX_remove_session(s->session_ctx, s->session);
2785
0
        }
2786
2787
1.81k
        SSL_SESSION_free(s->session);
2788
1.81k
        s->session = new_sess;
2789
1.81k
    }
2790
2791
1.99k
    s->session->time = ossl_time_now();
2792
1.99k
    ssl_session_calculate_timeout(s->session);
2793
2794
1.99k
    OPENSSL_free(s->session->ext.tick);
2795
1.99k
    s->session->ext.tick = NULL;
2796
1.99k
    s->session->ext.ticklen = 0;
2797
2798
1.99k
    s->session->ext.tick = OPENSSL_malloc(ticklen);
2799
1.99k
    if (s->session->ext.tick == NULL) {
2800
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
2801
0
        goto err;
2802
0
    }
2803
1.99k
    if (!PACKET_copy_bytes(pkt, s->session->ext.tick, ticklen)) {
2804
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2805
0
        goto err;
2806
0
    }
2807
2808
1.99k
    s->session->ext.tick_lifetime_hint = ticket_lifetime_hint;
2809
1.99k
    s->session->ext.tick_age_add = age_add;
2810
1.99k
    s->session->ext.ticklen = ticklen;
2811
2812
1.99k
    if (SSL_CONNECTION_IS_TLS13(s)) {
2813
1.80k
        PACKET extpkt;
2814
2815
1.80k
        if (!PACKET_as_length_prefixed_2(pkt, &extpkt)
2816
1.76k
            || PACKET_remaining(pkt) != 0) {
2817
45
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2818
45
            goto err;
2819
45
        }
2820
2821
1.76k
        if (!tls_collect_extensions(s, &extpkt,
2822
1.76k
                SSL_EXT_TLS1_3_NEW_SESSION_TICKET, &exts,
2823
1.76k
                NULL, 1)
2824
1.75k
            || !tls_parse_all_extensions(s,
2825
1.75k
                SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
2826
1.75k
                exts, NULL, 0, 1)) {
2827
            /* SSLfatal() already called */
2828
4
            goto err;
2829
4
        }
2830
1.76k
    }
2831
2832
    /*
2833
     * There are two ways to detect a resumed ticket session. One is to set
2834
     * an appropriate session ID and then the server must return a match in
2835
     * ServerHello. This allows the normal client session ID matching to work
2836
     * and we know much earlier that the ticket has been accepted. The
2837
     * other way is to set zero length session ID when the ticket is
2838
     * presented and rely on the handshake to determine session resumption.
2839
     * We choose the former approach because this fits in with assumptions
2840
     * elsewhere in OpenSSL. The session ID is set to the SHA256 hash of the
2841
     * ticket.
2842
     */
2843
1.94k
    sha256 = EVP_MD_fetch(sctx->libctx, "SHA2-256", sctx->propq);
2844
1.94k
    if (sha256 == NULL) {
2845
        /* Error is already recorded */
2846
0
        SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
2847
0
        goto err;
2848
0
    }
2849
    /*
2850
     * We use sess_len here because EVP_Digest expects an int
2851
     * but s->session->session_id_length is a size_t
2852
     */
2853
1.94k
    if (!EVP_Digest(s->session->ext.tick, ticklen,
2854
1.94k
            s->session->session_id, &sess_len,
2855
1.94k
            sha256, NULL)) {
2856
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
2857
0
        goto err;
2858
0
    }
2859
1.94k
    EVP_MD_free(sha256);
2860
1.94k
    sha256 = NULL;
2861
1.94k
    s->session->session_id_length = sess_len;
2862
1.94k
    s->session->not_resumable = 0;
2863
2864
    /* This is a standalone message in TLSv1.3, so there is no more to read */
2865
1.94k
    if (SSL_CONNECTION_IS_TLS13(s)) {
2866
1.75k
        const EVP_MD *md = ssl_handshake_md(s);
2867
1.75k
        int hashleni = EVP_MD_get_size(md);
2868
1.75k
        size_t hashlen;
2869
        /* ASCII: "resumption", in hex for EBCDIC compatibility */
2870
1.75k
        static const unsigned char nonce_label[] = { 0x72, 0x65, 0x73, 0x75, 0x6D,
2871
1.75k
            0x70, 0x74, 0x69, 0x6F, 0x6E };
2872
2873
        /* Ensure cast to size_t is safe */
2874
1.75k
        if (!ossl_assert(hashleni > 0)) {
2875
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2876
0
            goto err;
2877
0
        }
2878
1.75k
        hashlen = (size_t)hashleni;
2879
2880
1.75k
        if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
2881
1.75k
                nonce_label,
2882
1.75k
                sizeof(nonce_label),
2883
1.75k
                PACKET_data(&nonce),
2884
1.75k
                PACKET_remaining(&nonce),
2885
1.75k
                s->session->master_key,
2886
1.75k
                hashlen, 1)) {
2887
            /* SSLfatal() already called */
2888
0
            goto err;
2889
0
        }
2890
1.75k
        s->session->master_key_length = hashlen;
2891
2892
1.75k
        OPENSSL_free(exts);
2893
1.75k
        ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
2894
1.75k
        return MSG_PROCESS_FINISHED_READING;
2895
1.75k
    }
2896
2897
187
    return MSG_PROCESS_CONTINUE_READING;
2898
186
err:
2899
186
    EVP_MD_free(sha256);
2900
186
    OPENSSL_free(exts);
2901
186
    return MSG_PROCESS_ERROR;
2902
1.94k
}
2903
2904
/*
2905
 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
2906
 * parse a separate message. Returns 1 on success or 0 on failure
2907
 */
2908
int tls_process_cert_status_body(SSL_CONNECTION *s, PACKET *pkt)
2909
0
{
2910
0
    size_t resplen;
2911
0
    unsigned int type;
2912
2913
0
    if (!PACKET_get_1(pkt, &type)
2914
0
        || type != TLSEXT_STATUSTYPE_ocsp) {
2915
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_UNSUPPORTED_STATUS_TYPE);
2916
0
        return 0;
2917
0
    }
2918
0
    if (!PACKET_get_net_3_len(pkt, &resplen)
2919
0
        || PACKET_remaining(pkt) != resplen) {
2920
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2921
0
        return 0;
2922
0
    }
2923
0
    s->ext.ocsp.resp = OPENSSL_malloc(resplen);
2924
0
    if (s->ext.ocsp.resp == NULL) {
2925
0
        s->ext.ocsp.resp_len = 0;
2926
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
2927
0
        return 0;
2928
0
    }
2929
0
    s->ext.ocsp.resp_len = resplen;
2930
0
    if (!PACKET_copy_bytes(pkt, s->ext.ocsp.resp, resplen)) {
2931
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
2932
0
        return 0;
2933
0
    }
2934
2935
0
    return 1;
2936
0
}
2937
2938
MSG_PROCESS_RETURN tls_process_cert_status(SSL_CONNECTION *s, PACKET *pkt)
2939
0
{
2940
0
    if (!tls_process_cert_status_body(s, pkt)) {
2941
        /* SSLfatal() already called */
2942
0
        return MSG_PROCESS_ERROR;
2943
0
    }
2944
2945
0
    return MSG_PROCESS_CONTINUE_READING;
2946
0
}
2947
2948
/*
2949
 * Perform miscellaneous checks and processing after we have received the
2950
 * server's initial flight. In TLS1.3 this is after the Server Finished message.
2951
 * In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0
2952
 * on failure.
2953
 */
2954
int tls_process_initial_server_flight(SSL_CONNECTION *s)
2955
25.6k
{
2956
25.6k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2957
2958
    /*
2959
     * at this point we check that we have the required stuff from
2960
     * the server
2961
     */
2962
25.6k
    if (!ssl3_check_cert_and_algorithm(s)) {
2963
        /* SSLfatal() already called */
2964
9
        return 0;
2965
9
    }
2966
2967
    /*
2968
     * Call the ocsp status callback if needed. The |ext.ocsp.resp| and
2969
     * |ext.ocsp.resp_len| values will be set if we actually received a status
2970
     * message, or NULL and -1 otherwise
2971
     */
2972
25.6k
    if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing
2973
0
        && sctx->ext.status_cb != NULL) {
2974
0
        int ret = sctx->ext.status_cb(SSL_CONNECTION_GET_USER_SSL(s),
2975
0
            sctx->ext.status_arg);
2976
2977
0
        if (ret == 0) {
2978
0
            SSLfatal(s, SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE,
2979
0
                SSL_R_INVALID_STATUS_RESPONSE);
2980
0
            return 0;
2981
0
        }
2982
0
        if (ret < 0) {
2983
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2984
0
                SSL_R_OCSP_CALLBACK_FAILURE);
2985
0
            return 0;
2986
0
        }
2987
0
    }
2988
25.6k
#ifndef OPENSSL_NO_CT
2989
25.6k
    if (s->ct_validation_callback != NULL) {
2990
        /* Note we validate the SCTs whether or not we abort on error */
2991
0
        if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) {
2992
            /* SSLfatal() already called */
2993
0
            return 0;
2994
0
        }
2995
0
    }
2996
25.6k
#endif
2997
2998
25.6k
    return 1;
2999
25.6k
}
3000
3001
MSG_PROCESS_RETURN tls_process_server_done(SSL_CONNECTION *s, PACKET *pkt)
3002
13.2k
{
3003
13.2k
    if (PACKET_remaining(pkt) > 0) {
3004
        /* should contain no data */
3005
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
3006
0
        return MSG_PROCESS_ERROR;
3007
0
    }
3008
13.2k
#ifndef OPENSSL_NO_SRP
3009
13.2k
    if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
3010
0
        if (ssl_srp_calc_a_param_intern(s) <= 0) {
3011
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SRP_A_CALC);
3012
0
            return MSG_PROCESS_ERROR;
3013
0
        }
3014
0
    }
3015
13.2k
#endif
3016
3017
13.2k
    if (!tls_process_initial_server_flight(s)) {
3018
        /* SSLfatal() already called */
3019
9
        return MSG_PROCESS_ERROR;
3020
9
    }
3021
3022
13.2k
    return MSG_PROCESS_FINISHED_READING;
3023
13.2k
}
3024
3025
static int tls_construct_cke_psk_preamble(SSL_CONNECTION *s, WPACKET *pkt)
3026
0
{
3027
0
#ifndef OPENSSL_NO_PSK
3028
0
    int ret = 0;
3029
    /*
3030
     * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
3031
     * \0-terminated identity. The last byte is for us for simulating
3032
     * strnlen.
3033
     */
3034
0
    char identity[PSK_MAX_IDENTITY_LEN + 1];
3035
0
    size_t identitylen = 0;
3036
0
    unsigned char psk[PSK_MAX_PSK_LEN];
3037
0
    unsigned char *tmppsk = NULL;
3038
0
    char *tmpidentity = NULL;
3039
0
    size_t psklen = 0;
3040
3041
0
    if (s->psk_client_callback == NULL) {
3042
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_CLIENT_CB);
3043
0
        goto err;
3044
0
    }
3045
3046
0
    memset(identity, 0, sizeof(identity));
3047
3048
0
    psklen = s->psk_client_callback(SSL_CONNECTION_GET_USER_SSL(s),
3049
0
        s->session->psk_identity_hint,
3050
0
        identity, sizeof(identity) - 1,
3051
0
        psk, sizeof(psk));
3052
3053
0
    if (psklen > PSK_MAX_PSK_LEN) {
3054
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR);
3055
0
        psklen = PSK_MAX_PSK_LEN; /* Avoid overrunning the array on cleanse */
3056
0
        goto err;
3057
0
    } else if (psklen == 0) {
3058
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_PSK_IDENTITY_NOT_FOUND);
3059
0
        goto err;
3060
0
    }
3061
3062
0
    identitylen = strlen(identity);
3063
0
    if (identitylen > PSK_MAX_IDENTITY_LEN) {
3064
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3065
0
        goto err;
3066
0
    }
3067
3068
0
    tmppsk = OPENSSL_memdup(psk, psklen);
3069
0
    tmpidentity = OPENSSL_strdup(identity);
3070
0
    if (tmppsk == NULL || tmpidentity == NULL) {
3071
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
3072
0
        goto err;
3073
0
    }
3074
3075
0
    OPENSSL_free(s->s3.tmp.psk);
3076
0
    s->s3.tmp.psk = tmppsk;
3077
0
    s->s3.tmp.psklen = psklen;
3078
0
    tmppsk = NULL;
3079
0
    OPENSSL_free(s->session->psk_identity);
3080
0
    s->session->psk_identity = tmpidentity;
3081
0
    tmpidentity = NULL;
3082
3083
0
    if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) {
3084
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3085
0
        goto err;
3086
0
    }
3087
3088
0
    ret = 1;
3089
3090
0
err:
3091
0
    OPENSSL_cleanse(psk, psklen);
3092
0
    OPENSSL_cleanse(identity, sizeof(identity));
3093
0
    OPENSSL_clear_free(tmppsk, psklen);
3094
0
    OPENSSL_clear_free(tmpidentity, identitylen);
3095
3096
0
    return ret;
3097
#else
3098
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3099
    return 0;
3100
#endif
3101
0
}
3102
3103
static int tls_construct_cke_rsa(SSL_CONNECTION *s, WPACKET *pkt)
3104
4.94k
{
3105
4.94k
    unsigned char *encdata = NULL;
3106
4.94k
    EVP_PKEY *pkey = NULL;
3107
4.94k
    EVP_PKEY_CTX *pctx = NULL;
3108
4.94k
    size_t enclen;
3109
4.94k
    unsigned char *pms = NULL;
3110
4.94k
    size_t pmslen = 0;
3111
4.94k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3112
3113
4.94k
    if (!received_server_cert(s)) {
3114
        /*
3115
         * We should always have a server certificate with SSL_kRSA.
3116
         */
3117
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3118
0
        return 0;
3119
0
    }
3120
3121
4.94k
    if ((pkey = tls_get_peer_pkey(s)) == NULL) {
3122
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3123
0
        return 0;
3124
0
    }
3125
3126
4.94k
    if (!EVP_PKEY_is_a(pkey, "RSA")) {
3127
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3128
0
        return 0;
3129
0
    }
3130
3131
4.94k
    pmslen = SSL_MAX_MASTER_KEY_LENGTH;
3132
4.94k
    pms = OPENSSL_malloc(pmslen);
3133
4.94k
    if (pms == NULL) {
3134
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
3135
0
        return 0;
3136
0
    }
3137
3138
4.94k
    pms[0] = s->client_version >> 8;
3139
4.94k
    pms[1] = s->client_version & 0xff;
3140
4.94k
    if (RAND_bytes_ex(sctx->libctx, pms + 2, pmslen - 2, 0) <= 0) {
3141
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_RAND_LIB);
3142
0
        goto err;
3143
0
    }
3144
3145
    /* Fix buf for TLS and beyond */
3146
4.94k
    if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) {
3147
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3148
0
        goto err;
3149
0
    }
3150
3151
4.94k
    pctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pkey, sctx->propq);
3152
4.94k
    if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0
3153
4.94k
        || EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) {
3154
6
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3155
6
        goto err;
3156
6
    }
3157
4.94k
    if (!WPACKET_allocate_bytes(pkt, enclen, &encdata)
3158
4.94k
        || EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) {
3159
588
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_RSA_ENCRYPT);
3160
588
        goto err;
3161
588
    }
3162
4.35k
    EVP_PKEY_CTX_free(pctx);
3163
4.35k
    pctx = NULL;
3164
3165
    /* Fix buf for TLS and beyond */
3166
4.35k
    if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) {
3167
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3168
0
        goto err;
3169
0
    }
3170
3171
    /* Log the premaster secret, if logging is enabled. */
3172
4.35k
    if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen)) {
3173
        /* SSLfatal() already called */
3174
0
        goto err;
3175
0
    }
3176
3177
4.35k
    s->s3.tmp.pms = pms;
3178
4.35k
    s->s3.tmp.pmslen = pmslen;
3179
3180
4.35k
    return 1;
3181
594
err:
3182
594
    OPENSSL_clear_free(pms, pmslen);
3183
594
    EVP_PKEY_CTX_free(pctx);
3184
3185
594
    return 0;
3186
4.35k
}
3187
3188
static int tls_construct_cke_dhe(SSL_CONNECTION *s, WPACKET *pkt)
3189
4.11k
{
3190
4.11k
    EVP_PKEY *ckey = NULL, *skey = NULL;
3191
4.11k
    unsigned char *keybytes = NULL;
3192
4.11k
    int prime_len;
3193
4.11k
    unsigned char *encoded_pub = NULL;
3194
4.11k
    size_t encoded_pub_len, pad_len;
3195
4.11k
    int ret = 0;
3196
3197
4.11k
    skey = s->s3.peer_tmp;
3198
4.11k
    if (skey == NULL) {
3199
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3200
0
        goto err;
3201
0
    }
3202
3203
4.11k
    ckey = ssl_generate_pkey(s, skey);
3204
4.11k
    if (ckey == NULL) {
3205
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3206
0
        goto err;
3207
0
    }
3208
3209
4.11k
    if (ssl_derive(s, ckey, skey, 0) == 0) {
3210
        /* SSLfatal() already called */
3211
0
        goto err;
3212
0
    }
3213
3214
    /* send off the data */
3215
3216
    /* Generate encoding of server key */
3217
4.11k
    encoded_pub_len = EVP_PKEY_get1_encoded_public_key(ckey, &encoded_pub);
3218
4.11k
    if (encoded_pub_len == 0) {
3219
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3220
0
        EVP_PKEY_free(ckey);
3221
0
        return EXT_RETURN_FAIL;
3222
0
    }
3223
3224
    /*
3225
     * For interoperability with some versions of the Microsoft TLS
3226
     * stack, we need to zero pad the DHE pub key to the same length
3227
     * as the prime.
3228
     */
3229
4.11k
    prime_len = EVP_PKEY_get_size(ckey);
3230
4.11k
    pad_len = prime_len - encoded_pub_len;
3231
4.11k
    if (pad_len > 0) {
3232
0
        if (!WPACKET_sub_allocate_bytes_u16(pkt, pad_len, &keybytes)) {
3233
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3234
0
            goto err;
3235
0
        }
3236
0
        memset(keybytes, 0, pad_len);
3237
0
    }
3238
3239
4.11k
    if (!WPACKET_sub_memcpy_u16(pkt, encoded_pub, encoded_pub_len)) {
3240
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3241
0
        goto err;
3242
0
    }
3243
3244
4.11k
    ret = 1;
3245
4.11k
err:
3246
4.11k
    OPENSSL_free(encoded_pub);
3247
4.11k
    EVP_PKEY_free(ckey);
3248
4.11k
    return ret;
3249
4.11k
}
3250
3251
static int tls_construct_cke_ecdhe(SSL_CONNECTION *s, WPACKET *pkt)
3252
3.63k
{
3253
3.63k
    unsigned char *encodedPoint = NULL;
3254
3.63k
    size_t encoded_pt_len = 0;
3255
3.63k
    EVP_PKEY *ckey = NULL, *skey = NULL;
3256
3.63k
    int ret = 0;
3257
3258
3.63k
    skey = s->s3.peer_tmp;
3259
3.63k
    if (skey == NULL) {
3260
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3261
0
        return 0;
3262
0
    }
3263
3264
3.63k
    ckey = ssl_generate_pkey(s, skey);
3265
3.63k
    if (ckey == NULL) {
3266
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
3267
0
        goto err;
3268
0
    }
3269
3270
3.63k
    if (ssl_derive(s, ckey, skey, 0) == 0) {
3271
        /* SSLfatal() already called */
3272
330
        goto err;
3273
330
    }
3274
3275
    /* Generate encoding of client key */
3276
3.30k
    encoded_pt_len = EVP_PKEY_get1_encoded_public_key(ckey, &encodedPoint);
3277
3278
3.30k
    if (encoded_pt_len == 0) {
3279
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB);
3280
0
        goto err;
3281
0
    }
3282
3283
3.30k
    if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) {
3284
548
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3285
548
        goto err;
3286
548
    }
3287
3288
2.76k
    ret = 1;
3289
3.63k
err:
3290
3.63k
    OPENSSL_free(encodedPoint);
3291
3.63k
    EVP_PKEY_free(ckey);
3292
3.63k
    return ret;
3293
2.76k
}
3294
3295
static int tls_construct_cke_gost(SSL_CONNECTION *s, WPACKET *pkt)
3296
0
{
3297
0
#ifndef OPENSSL_NO_GOST
3298
    /* GOST key exchange message creation */
3299
0
    EVP_PKEY_CTX *pkey_ctx = NULL;
3300
0
    EVP_PKEY *pkey = NULL;
3301
0
    size_t msglen;
3302
0
    unsigned int md_len;
3303
0
    unsigned char shared_ukm[32], tmp[256];
3304
0
    EVP_MD_CTX *ukm_hash = NULL;
3305
0
    int dgst_nid = NID_id_GostR3411_94;
3306
0
    unsigned char *pms = NULL;
3307
0
    size_t pmslen = 0;
3308
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3309
3310
0
    if ((s->s3.tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)
3311
0
        dgst_nid = NID_id_GostR3411_2012_256;
3312
3313
    /*
3314
     * Get server certificate PKEY and create ctx from it
3315
     */
3316
0
    if ((pkey = tls_get_peer_pkey(s)) == NULL) {
3317
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3318
0
            SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
3319
0
        return 0;
3320
0
    }
3321
3322
0
    pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx,
3323
0
        pkey,
3324
0
        sctx->propq);
3325
0
    if (pkey_ctx == NULL) {
3326
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3327
0
        return 0;
3328
0
    }
3329
    /*
3330
     * If we have send a certificate, and certificate key
3331
     * parameters match those of server certificate, use
3332
     * certificate key for key exchange
3333
     */
3334
3335
    /* Otherwise, generate ephemeral key pair */
3336
0
    pmslen = 32;
3337
0
    pms = OPENSSL_malloc(pmslen);
3338
0
    if (pms == NULL) {
3339
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
3340
0
        goto err;
3341
0
    }
3342
3343
0
    if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0
3344
        /* Generate session key
3345
         */
3346
0
        || RAND_bytes_ex(sctx->libctx, pms, pmslen, 0) <= 0) {
3347
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3348
0
        goto err;
3349
0
    };
3350
    /*
3351
     * Compute shared IV and store it in algorithm-specific context
3352
     * data
3353
     */
3354
0
    ukm_hash = EVP_MD_CTX_new();
3355
0
    if (ukm_hash == NULL
3356
0
        || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0
3357
0
        || EVP_DigestUpdate(ukm_hash, s->s3.client_random,
3358
0
               SSL3_RANDOM_SIZE)
3359
0
            <= 0
3360
0
        || EVP_DigestUpdate(ukm_hash, s->s3.server_random,
3361
0
               SSL3_RANDOM_SIZE)
3362
0
            <= 0
3363
0
        || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
3364
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3365
0
        goto err;
3366
0
    }
3367
0
    EVP_MD_CTX_free(ukm_hash);
3368
0
    ukm_hash = NULL;
3369
0
    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
3370
0
            EVP_PKEY_CTRL_SET_IV, 8, shared_ukm)
3371
0
        <= 0) {
3372
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3373
0
        goto err;
3374
0
    }
3375
    /* Make GOST keytransport blob message */
3376
    /*
3377
     * Encapsulate it into sequence
3378
     */
3379
0
    msglen = 255;
3380
0
    if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
3381
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3382
0
        goto err;
3383
0
    }
3384
3385
0
    if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)
3386
0
        || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81))
3387
0
        || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) {
3388
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3389
0
        goto err;
3390
0
    }
3391
3392
0
    EVP_PKEY_CTX_free(pkey_ctx);
3393
0
    s->s3.tmp.pms = pms;
3394
0
    s->s3.tmp.pmslen = pmslen;
3395
3396
0
    return 1;
3397
0
err:
3398
0
    EVP_PKEY_CTX_free(pkey_ctx);
3399
0
    OPENSSL_clear_free(pms, pmslen);
3400
0
    EVP_MD_CTX_free(ukm_hash);
3401
0
    return 0;
3402
#else
3403
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3404
    return 0;
3405
#endif
3406
0
}
3407
3408
#ifndef OPENSSL_NO_GOST
3409
int ossl_gost18_cke_cipher_nid(const SSL_CONNECTION *s)
3410
0
{
3411
0
    if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_MAGMA) != 0)
3412
0
        return NID_magma_ctr;
3413
0
    else if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_KUZNYECHIK) != 0)
3414
0
        return NID_kuznyechik_ctr;
3415
3416
0
    return NID_undef;
3417
0
}
3418
3419
int ossl_gost_ukm(const SSL_CONNECTION *s, unsigned char *dgst_buf)
3420
0
{
3421
0
    EVP_MD_CTX *hash = NULL;
3422
0
    unsigned int md_len;
3423
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3424
0
    const EVP_MD *md = ssl_evp_md_fetch(sctx->libctx, NID_id_GostR3411_2012_256,
3425
0
        sctx->propq);
3426
3427
0
    if (md == NULL)
3428
0
        return 0;
3429
3430
0
    if ((hash = EVP_MD_CTX_new()) == NULL
3431
0
        || EVP_DigestInit(hash, md) <= 0
3432
0
        || EVP_DigestUpdate(hash, s->s3.client_random, SSL3_RANDOM_SIZE) <= 0
3433
0
        || EVP_DigestUpdate(hash, s->s3.server_random, SSL3_RANDOM_SIZE) <= 0
3434
0
        || EVP_DigestFinal_ex(hash, dgst_buf, &md_len) <= 0) {
3435
0
        EVP_MD_CTX_free(hash);
3436
0
        ssl_evp_md_free(md);
3437
0
        return 0;
3438
0
    }
3439
3440
0
    EVP_MD_CTX_free(hash);
3441
0
    ssl_evp_md_free(md);
3442
0
    return 1;
3443
0
}
3444
#endif
3445
3446
static int tls_construct_cke_gost18(SSL_CONNECTION *s, WPACKET *pkt)
3447
0
{
3448
0
#ifndef OPENSSL_NO_GOST
3449
    /* GOST 2018 key exchange message creation */
3450
0
    unsigned char rnd_dgst[32];
3451
0
    unsigned char *encdata = NULL;
3452
0
    EVP_PKEY_CTX *pkey_ctx = NULL;
3453
0
    EVP_PKEY *pkey;
3454
0
    unsigned char *pms = NULL;
3455
0
    size_t pmslen = 0;
3456
0
    size_t msglen;
3457
0
    int cipher_nid = ossl_gost18_cke_cipher_nid(s);
3458
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
3459
3460
0
    if (cipher_nid == NID_undef) {
3461
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3462
0
        return 0;
3463
0
    }
3464
3465
0
    if (ossl_gost_ukm(s, rnd_dgst) <= 0) {
3466
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3467
0
        goto err;
3468
0
    }
3469
3470
    /* Pre-master secret - random bytes */
3471
0
    pmslen = 32;
3472
0
    pms = OPENSSL_malloc(pmslen);
3473
0
    if (pms == NULL) {
3474
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
3475
0
        goto err;
3476
0
    }
3477
3478
0
    if (RAND_bytes_ex(sctx->libctx, pms, pmslen, 0) <= 0) {
3479
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3480
0
        goto err;
3481
0
    }
3482
3483
    /* Get server certificate PKEY and create ctx from it */
3484
0
    if ((pkey = tls_get_peer_pkey(s)) == NULL) {
3485
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3486
0
            SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
3487
0
        goto err;
3488
0
    }
3489
3490
0
    pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx,
3491
0
        pkey,
3492
0
        sctx->propq);
3493
0
    if (pkey_ctx == NULL) {
3494
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3495
0
        goto err;
3496
0
    }
3497
3498
0
    if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0) {
3499
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3500
0
        goto err;
3501
0
    };
3502
3503
    /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code */
3504
0
    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
3505
0
            EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst)
3506
0
        <= 0) {
3507
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3508
0
        goto err;
3509
0
    }
3510
3511
0
    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,
3512
0
            EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL)
3513
0
        <= 0) {
3514
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
3515
0
        goto err;
3516
0
    }
3517
3518
0
    if (EVP_PKEY_encrypt(pkey_ctx, NULL, &msglen, pms, pmslen) <= 0) {
3519
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3520
0
        goto err;
3521
0
    }
3522
3523
0
    if (!WPACKET_allocate_bytes(pkt, msglen, &encdata)
3524
0
        || EVP_PKEY_encrypt(pkey_ctx, encdata, &msglen, pms, pmslen) <= 0) {
3525
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
3526
0
        goto err;
3527
0
    }
3528
3529
0
    EVP_PKEY_CTX_free(pkey_ctx);
3530
0
    pkey_ctx = NULL;
3531
0
    s->s3.tmp.pms = pms;
3532
0
    s->s3.tmp.pmslen = pmslen;
3533
3534
0
    return 1;
3535
0
err:
3536
0
    EVP_PKEY_CTX_free(pkey_ctx);
3537
0
    OPENSSL_clear_free(pms, pmslen);
3538
0
    return 0;
3539
#else
3540
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3541
    return 0;
3542
#endif
3543
0
}
3544
3545
static int tls_construct_cke_srp(SSL_CONNECTION *s, WPACKET *pkt)
3546
0
{
3547
0
#ifndef OPENSSL_NO_SRP
3548
0
    unsigned char *abytes = NULL;
3549
3550
0
    if (s->srp_ctx.A == NULL
3551
0
        || !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A),
3552
0
            &abytes)) {
3553
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3554
0
        return 0;
3555
0
    }
3556
0
    BN_bn2bin(s->srp_ctx.A, abytes);
3557
3558
0
    OPENSSL_free(s->session->srp_username);
3559
0
    s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
3560
0
    if (s->session->srp_username == NULL) {
3561
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
3562
0
        return 0;
3563
0
    }
3564
3565
0
    return 1;
3566
#else
3567
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3568
    return 0;
3569
#endif
3570
0
}
3571
3572
CON_FUNC_RETURN tls_construct_client_key_exchange(SSL_CONNECTION *s,
3573
    WPACKET *pkt)
3574
13.2k
{
3575
13.2k
    unsigned long alg_k;
3576
3577
13.2k
    alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
3578
3579
    /*
3580
     * All of the construct functions below call SSLfatal() if necessary so
3581
     * no need to do so here.
3582
     */
3583
13.2k
    if ((alg_k & SSL_PSK)
3584
0
        && !tls_construct_cke_psk_preamble(s, pkt))
3585
0
        goto err;
3586
3587
13.2k
    if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3588
5.52k
        if (!tls_construct_cke_rsa(s, pkt))
3589
728
            goto err;
3590
7.75k
    } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
3591
4.11k
        if (!tls_construct_cke_dhe(s, pkt))
3592
0
            goto err;
3593
4.11k
    } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
3594
3.63k
        if (!tls_construct_cke_ecdhe(s, pkt))
3595
878
            goto err;
3596
3.63k
    } else if (alg_k & SSL_kGOST) {
3597
0
        if (!tls_construct_cke_gost(s, pkt))
3598
0
            goto err;
3599
0
    } else if (alg_k & SSL_kGOST18) {
3600
0
        if (!tls_construct_cke_gost18(s, pkt))
3601
0
            goto err;
3602
0
    } else if (alg_k & SSL_kSRP) {
3603
0
        if (!tls_construct_cke_srp(s, pkt))
3604
0
            goto err;
3605
0
    } else if (!(alg_k & SSL_kPSK)) {
3606
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3607
0
        goto err;
3608
0
    }
3609
3610
11.6k
    return CON_FUNC_SUCCESS;
3611
1.60k
err:
3612
1.60k
    OPENSSL_clear_free(s->s3.tmp.pms, s->s3.tmp.pmslen);
3613
1.60k
    s->s3.tmp.pms = NULL;
3614
1.60k
    s->s3.tmp.pmslen = 0;
3615
1.60k
#ifndef OPENSSL_NO_PSK
3616
1.60k
    OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen);
3617
1.60k
    s->s3.tmp.psk = NULL;
3618
1.60k
    s->s3.tmp.psklen = 0;
3619
1.60k
#endif
3620
1.60k
    return CON_FUNC_ERROR;
3621
13.2k
}
3622
3623
int tls_client_key_exchange_post_work(SSL_CONNECTION *s)
3624
11.6k
{
3625
11.6k
    unsigned char *pms = NULL;
3626
11.6k
    size_t pmslen = 0;
3627
3628
11.6k
    pms = s->s3.tmp.pms;
3629
11.6k
    pmslen = s->s3.tmp.pmslen;
3630
3631
11.6k
#ifndef OPENSSL_NO_SRP
3632
    /* Check for SRP */
3633
11.6k
    if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
3634
0
        if (!srp_generate_client_master_secret(s)) {
3635
            /* SSLfatal() already called */
3636
0
            goto err;
3637
0
        }
3638
0
        return 1;
3639
0
    }
3640
11.6k
#endif
3641
3642
11.6k
    if (pms == NULL && !(s->s3.tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
3643
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_PASSED_INVALID_ARGUMENT);
3644
0
        goto err;
3645
0
    }
3646
11.6k
    if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
3647
        /* SSLfatal() already called */
3648
        /* ssl_generate_master_secret frees the pms even on error */
3649
0
        pms = NULL;
3650
0
        pmslen = 0;
3651
0
        goto err;
3652
0
    }
3653
11.6k
    pms = NULL;
3654
11.6k
    pmslen = 0;
3655
3656
#ifndef OPENSSL_NO_SCTP
3657
    if (SSL_CONNECTION_IS_DTLS(s)) {
3658
        unsigned char sctpauthkey[64];
3659
        char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
3660
        size_t labellen;
3661
        SSL *ssl = SSL_CONNECTION_GET_SSL(s);
3662
3663
        /*
3664
         * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3665
         * used.
3666
         */
3667
        memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
3668
            sizeof(DTLS1_SCTP_AUTH_LABEL));
3669
3670
        /* Don't include the terminating zero. */
3671
        labellen = sizeof(labelbuffer) - 1;
3672
        if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG)
3673
            labellen += 1;
3674
3675
        if (SSL_export_keying_material(ssl, sctpauthkey,
3676
                sizeof(sctpauthkey), labelbuffer,
3677
                labellen, NULL, 0, 0)
3678
            <= 0) {
3679
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3680
            goto err;
3681
        }
3682
3683
        BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
3684
            sizeof(sctpauthkey), sctpauthkey);
3685
    }
3686
#endif
3687
3688
11.6k
    return 1;
3689
0
err:
3690
0
    OPENSSL_clear_free(pms, pmslen);
3691
0
    s->s3.tmp.pms = NULL;
3692
0
    s->s3.tmp.pmslen = 0;
3693
0
    return 0;
3694
11.6k
}
3695
3696
/*
3697
 * Check a certificate can be used for client authentication. Currently check
3698
 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3699
 * certificates can be used and optionally checks suitability for Suite B.
3700
 */
3701
static int ssl3_check_client_certificate(SSL_CONNECTION *s)
3702
53
{
3703
    /* If no suitable signature algorithm can't use certificate */
3704
53
    if (!tls_choose_sigalg(s, 0) || s->s3.tmp.sigalg == NULL)
3705
53
        return 0;
3706
    /*
3707
     * If strict mode check suitability of chain before using it. This also
3708
     * adjusts suite B digest if necessary.
3709
     */
3710
0
    if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT && !tls1_check_chain(s, NULL, NULL, NULL, -2))
3711
0
        return 0;
3712
0
    return 1;
3713
0
}
3714
3715
WORK_STATE tls_prepare_client_certificate(SSL_CONNECTION *s, WORK_STATE wst)
3716
40
{
3717
40
    X509 *x509 = NULL;
3718
40
    EVP_PKEY *pkey = NULL;
3719
40
    int i;
3720
40
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
3721
3722
40
    if (wst == WORK_MORE_A) {
3723
        /* Let cert callback update client certificates if required */
3724
40
        if (s->cert->cert_cb) {
3725
0
            i = s->cert->cert_cb(ssl, s->cert->cert_cb_arg);
3726
0
            if (i < 0) {
3727
0
                s->rwstate = SSL_X509_LOOKUP;
3728
0
                return WORK_MORE_A;
3729
0
            }
3730
0
            if (i == 0) {
3731
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED);
3732
0
                return WORK_ERROR;
3733
0
            }
3734
0
            s->rwstate = SSL_NOTHING;
3735
0
        }
3736
40
        if (ssl3_check_client_certificate(s)) {
3737
0
            if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
3738
0
                return WORK_FINISHED_STOP;
3739
0
            }
3740
0
            return WORK_FINISHED_CONTINUE;
3741
0
        }
3742
3743
        /* Fall through to WORK_MORE_B */
3744
40
        wst = WORK_MORE_B;
3745
40
    }
3746
3747
    /* We need to get a client cert */
3748
40
    if (wst == WORK_MORE_B) {
3749
        /*
3750
         * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3751
         * return(-1); We then get retied later
3752
         */
3753
40
        i = ssl_do_client_cert_cb(s, &x509, &pkey);
3754
40
        if (i < 0) {
3755
0
            s->rwstate = SSL_X509_LOOKUP;
3756
0
            return WORK_MORE_B;
3757
0
        }
3758
40
        s->rwstate = SSL_NOTHING;
3759
40
        if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3760
0
            if (!SSL_use_certificate(ssl, x509)
3761
0
                || !SSL_use_PrivateKey(ssl, pkey))
3762
0
                i = 0;
3763
40
        } else if (i == 1) {
3764
0
            i = 0;
3765
0
            ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3766
0
        }
3767
3768
40
        X509_free(x509);
3769
40
        EVP_PKEY_free(pkey);
3770
40
        if (i && !ssl3_check_client_certificate(s))
3771
0
            i = 0;
3772
40
        if (i == 0) {
3773
40
            if (s->version == SSL3_VERSION) {
3774
0
                s->s3.tmp.cert_req = 0;
3775
0
                ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3776
0
                return WORK_FINISHED_CONTINUE;
3777
40
            } else {
3778
40
                s->s3.tmp.cert_req = 2;
3779
40
                s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none;
3780
40
                if (!ssl3_digest_cached_records(s, 0)) {
3781
                    /* SSLfatal() already called */
3782
0
                    return WORK_ERROR;
3783
0
                }
3784
40
            }
3785
40
        }
3786
3787
40
        if (!SSL_CONNECTION_IS_TLS13(s)
3788
0
            || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0)
3789
40
            s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none;
3790
3791
40
        if (s->post_handshake_auth == SSL_PHA_REQUESTED)
3792
0
            return WORK_FINISHED_STOP;
3793
40
        return WORK_FINISHED_CONTINUE;
3794
40
    }
3795
3796
    /* Shouldn't ever get here */
3797
40
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3798
0
    return WORK_ERROR;
3799
40
}
3800
3801
CON_FUNC_RETURN tls_construct_client_certificate(SSL_CONNECTION *s,
3802
    WPACKET *pkt)
3803
20
{
3804
20
    CERT_PKEY *cpk = NULL;
3805
20
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
3806
3807
20
    if (SSL_CONNECTION_IS_TLS13(s)) {
3808
0
        if (s->pha_context == NULL) {
3809
            /* no context available, add 0-length context */
3810
0
            if (!WPACKET_put_bytes_u8(pkt, 0)) {
3811
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3812
0
                return CON_FUNC_ERROR;
3813
0
            }
3814
0
        } else if (!WPACKET_sub_memcpy_u8(pkt, s->pha_context, s->pha_context_len)) {
3815
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3816
0
            return CON_FUNC_ERROR;
3817
0
        }
3818
0
    }
3819
20
    if (s->s3.tmp.cert_req != 2)
3820
0
        cpk = s->cert->key;
3821
20
    switch (s->ext.client_cert_type) {
3822
0
    case TLSEXT_cert_type_rpk:
3823
0
        if (!tls_output_rpk(s, pkt, cpk)) {
3824
            /* SSLfatal() already called */
3825
0
            return CON_FUNC_ERROR;
3826
0
        }
3827
0
        break;
3828
20
    case TLSEXT_cert_type_x509:
3829
20
        if (!ssl3_output_cert_chain(s, pkt, cpk, 0)) {
3830
            /* SSLfatal() already called */
3831
0
            return CON_FUNC_ERROR;
3832
0
        }
3833
20
        break;
3834
20
    default:
3835
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3836
0
        return CON_FUNC_ERROR;
3837
20
    }
3838
3839
    /*
3840
     * If we attempted to write early data or we're in middlebox compat mode
3841
     * then we deferred changing the handshake write keys to the last possible
3842
     * moment. We need to do it now.
3843
     */
3844
20
    if (SSL_CONNECTION_IS_TLS13(s)
3845
0
        && !SSL_IS_QUIC_HANDSHAKE(s)
3846
0
        && SSL_IS_FIRST_HANDSHAKE(s)
3847
0
        && (s->early_data_state != SSL_EARLY_DATA_NONE
3848
0
            || (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
3849
0
        && (!ssl->method->ssl3_enc->change_cipher_state(s,
3850
0
            SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {
3851
        /*
3852
         * This is a fatal error, which leaves enc_write_ctx in an inconsistent
3853
         * state and thus ssl3_send_alert may crash.
3854
         */
3855
0
        SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_CANNOT_CHANGE_CIPHER);
3856
0
        return CON_FUNC_ERROR;
3857
0
    }
3858
3859
20
    return CON_FUNC_SUCCESS;
3860
20
}
3861
3862
#ifndef OPENSSL_NO_COMP_ALG
3863
CON_FUNC_RETURN tls_construct_client_compressed_certificate(SSL_CONNECTION *sc,
3864
    WPACKET *pkt)
3865
{
3866
    SSL *ssl = SSL_CONNECTION_GET_SSL(sc);
3867
    WPACKET tmppkt;
3868
    BUF_MEM *buf = NULL;
3869
    size_t length;
3870
    size_t max_length;
3871
    COMP_METHOD *method;
3872
    COMP_CTX *comp = NULL;
3873
    int comp_len;
3874
    int ret = 0;
3875
    int alg = sc->ext.compress_certificate_from_peer[0];
3876
3877
    /* Note that sc->s3.tmp.cert_req == 2 is checked in write transition */
3878
3879
    if ((buf = BUF_MEM_new()) == NULL || !WPACKET_init(&tmppkt, buf))
3880
        goto err;
3881
3882
    /* Use the |tmppkt| for the to-be-compressed data */
3883
    if (sc->pha_context == NULL) {
3884
        /* no context available, add 0-length context */
3885
        if (!WPACKET_put_bytes_u8(&tmppkt, 0))
3886
            goto err;
3887
    } else if (!WPACKET_sub_memcpy_u8(&tmppkt, sc->pha_context, sc->pha_context_len))
3888
        goto err;
3889
3890
    if (!ssl3_output_cert_chain(sc, &tmppkt, sc->cert->key, 0)) {
3891
        /* SSLfatal() already called */
3892
        goto out;
3893
    }
3894
3895
    /* continue with the real |pkt| */
3896
    if (!WPACKET_put_bytes_u16(pkt, alg)
3897
        || !WPACKET_get_total_written(&tmppkt, &length)
3898
        || !WPACKET_put_bytes_u24(pkt, length))
3899
        goto err;
3900
3901
    switch (alg) {
3902
    case TLSEXT_comp_cert_zlib:
3903
        method = COMP_zlib_oneshot();
3904
        break;
3905
    case TLSEXT_comp_cert_brotli:
3906
        method = COMP_brotli_oneshot();
3907
        break;
3908
    case TLSEXT_comp_cert_zstd:
3909
        method = COMP_zstd_oneshot();
3910
        break;
3911
    default:
3912
        goto err;
3913
    }
3914
    max_length = ossl_calculate_comp_expansion(alg, length);
3915
3916
    if ((comp = COMP_CTX_new(method)) == NULL
3917
        || !WPACKET_start_sub_packet_u24(pkt)
3918
        || !WPACKET_reserve_bytes(pkt, max_length, NULL))
3919
        goto err;
3920
3921
    comp_len = COMP_compress_block(comp, WPACKET_get_curr(pkt), max_length,
3922
        (unsigned char *)buf->data, length);
3923
    if (comp_len <= 0)
3924
        goto err;
3925
3926
    if (!WPACKET_allocate_bytes(pkt, comp_len, NULL)
3927
        || !WPACKET_close(pkt))
3928
        goto err;
3929
3930
    /*
3931
     * If we attempted to write early data or we're in middlebox compat mode
3932
     * then we deferred changing the handshake write keys to the last possible
3933
     * moment. We need to do it now.
3934
     */
3935
    if (SSL_IS_FIRST_HANDSHAKE(sc)
3936
        && !SSL_IS_QUIC_HANDSHAKE(sc)
3937
        && (sc->early_data_state != SSL_EARLY_DATA_NONE
3938
            || (sc->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0)
3939
        && (!ssl->method->ssl3_enc->change_cipher_state(sc,
3940
            SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {
3941
        /*
3942
         * This is a fatal error, which leaves sc->enc_write_ctx in an
3943
         * inconsistent state and thus ssl3_send_alert may crash.
3944
         */
3945
        SSLfatal(sc, SSL_AD_NO_ALERT, SSL_R_CANNOT_CHANGE_CIPHER);
3946
        goto out;
3947
    }
3948
    ret = 1;
3949
    goto out;
3950
3951
err:
3952
    SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3953
out:
3954
    if (buf != NULL) {
3955
        /* If |buf| is NULL, then |tmppkt| could not have been initialized */
3956
        WPACKET_cleanup(&tmppkt);
3957
    }
3958
    BUF_MEM_free(buf);
3959
    COMP_CTX_free(comp);
3960
    return ret;
3961
}
3962
#endif
3963
3964
int ssl3_check_cert_and_algorithm(SSL_CONNECTION *s)
3965
24.1k
{
3966
24.1k
    const SSL_CERT_LOOKUP *clu;
3967
24.1k
    size_t idx;
3968
24.1k
    long alg_k, alg_a;
3969
24.1k
    EVP_PKEY *pkey;
3970
3971
24.1k
    alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
3972
24.1k
    alg_a = s->s3.tmp.new_cipher->algorithm_auth;
3973
3974
    /* we don't have a certificate */
3975
24.1k
    if (!(alg_a & SSL_aCERT))
3976
18.9k
        return 1;
3977
3978
    /* This is the passed certificate */
3979
5.26k
    pkey = tls_get_peer_pkey(s);
3980
5.26k
    clu = ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s));
3981
3982
    /* Check certificate is recognised and suitable for cipher */
3983
5.26k
    if (clu == NULL || (alg_a & clu->amask) == 0) {
3984
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_SIGNING_CERT);
3985
0
        return 0;
3986
0
    }
3987
3988
5.26k
    if (alg_k & (SSL_kRSA | SSL_kRSAPSK) && idx != SSL_PKEY_RSA) {
3989
8
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3990
8
            SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3991
8
        return 0;
3992
8
    }
3993
3994
5.25k
    if ((alg_k & SSL_kDHE) && (s->s3.peer_tmp == NULL)) {
3995
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
3996
0
        return 0;
3997
0
    }
3998
3999
    /* Early out to skip the checks below */
4000
5.25k
    if (s->session->peer_rpk != NULL)
4001
0
        return 1;
4002
4003
5.25k
    if (clu->amask & SSL_aECDSA) {
4004
0
        if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s))
4005
0
            return 1;
4006
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_ECC_CERT);
4007
0
        return 0;
4008
0
    }
4009
4010
5.25k
    return 1;
4011
5.25k
}
4012
4013
#ifndef OPENSSL_NO_NEXTPROTONEG
4014
CON_FUNC_RETURN tls_construct_next_proto(SSL_CONNECTION *s, WPACKET *pkt)
4015
0
{
4016
0
    size_t len, padding_len;
4017
0
    unsigned char *padding = NULL;
4018
4019
0
    len = s->ext.npn_len;
4020
0
    padding_len = 32 - ((len + 2) % 32);
4021
4022
0
    if (!WPACKET_sub_memcpy_u8(pkt, s->ext.npn, len)
4023
0
        || !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) {
4024
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4025
0
        return CON_FUNC_ERROR;
4026
0
    }
4027
4028
0
    memset(padding, 0, padding_len);
4029
4030
0
    return CON_FUNC_SUCCESS;
4031
0
}
4032
#endif
4033
4034
MSG_PROCESS_RETURN tls_process_hello_req(SSL_CONNECTION *s, PACKET *pkt)
4035
1.23k
{
4036
1.23k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
4037
4038
1.23k
    if (PACKET_remaining(pkt) > 0) {
4039
        /* should contain no data */
4040
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
4041
0
        return MSG_PROCESS_ERROR;
4042
0
    }
4043
4044
1.23k
    if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
4045
0
        ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
4046
0
        return MSG_PROCESS_FINISHED_READING;
4047
0
    }
4048
4049
    /*
4050
     * This is a historical discrepancy (not in the RFC) maintained for
4051
     * compatibility reasons. If a TLS client receives a HelloRequest it will
4052
     * attempt an abbreviated handshake. However if a DTLS client receives a
4053
     * HelloRequest it will do a full handshake. Either behaviour is reasonable
4054
     * but doing one for TLS and another for DTLS is odd.
4055
     */
4056
1.23k
    if (SSL_CONNECTION_IS_DTLS(s))
4057
0
        SSL_renegotiate(ssl);
4058
1.23k
    else
4059
1.23k
        SSL_renegotiate_abbreviated(ssl);
4060
4061
1.23k
    return MSG_PROCESS_FINISHED_READING;
4062
1.23k
}
4063
4064
static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL_CONNECTION *s,
4065
    PACKET *pkt)
4066
21.8k
{
4067
21.8k
    PACKET extensions;
4068
21.8k
    RAW_EXTENSION *rawexts = NULL;
4069
4070
21.8k
    if (!PACKET_as_length_prefixed_2(pkt, &extensions)
4071
21.6k
        || PACKET_remaining(pkt) != 0) {
4072
108
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH);
4073
108
        goto err;
4074
108
    }
4075
4076
21.6k
    if (!tls_collect_extensions(s, &extensions,
4077
21.6k
            SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, &rawexts,
4078
21.6k
            NULL, 1)
4079
21.6k
        || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
4080
21.6k
            rawexts, NULL, 0, 1)) {
4081
        /* SSLfatal() already called */
4082
1.24k
        goto err;
4083
1.24k
    }
4084
4085
20.4k
    OPENSSL_free(rawexts);
4086
20.4k
    return MSG_PROCESS_CONTINUE_READING;
4087
4088
1.35k
err:
4089
1.35k
    OPENSSL_free(rawexts);
4090
1.35k
    return MSG_PROCESS_ERROR;
4091
21.6k
}
4092
4093
int ssl_do_client_cert_cb(SSL_CONNECTION *s, X509 **px509, EVP_PKEY **ppkey)
4094
48
{
4095
48
    int i = 0;
4096
48
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
4097
4098
48
#ifndef OPENSSL_NO_ENGINE
4099
48
    if (sctx->client_cert_engine) {
4100
0
        i = tls_engine_load_ssl_client_cert(s, px509, ppkey);
4101
0
        if (i != 0)
4102
0
            return i;
4103
0
    }
4104
48
#endif
4105
48
    if (sctx->client_cert_cb)
4106
0
        i = sctx->client_cert_cb(SSL_CONNECTION_GET_USER_SSL(s), px509, ppkey);
4107
48
    return i;
4108
48
}
4109
4110
int ssl_cipher_list_to_bytes(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *sk,
4111
    WPACKET *pkt)
4112
88.1k
{
4113
88.1k
    int i;
4114
88.1k
    size_t totlen = 0, len, maxlen, maxverok = 0;
4115
88.1k
    int empty_reneg_info_scsv = !s->renegotiate
4116
87.1k
        && !SSL_CONNECTION_IS_DTLS(s)
4117
66.4k
        && ssl_security(s, SSL_SECOP_VERSION, 0, TLS1_VERSION, NULL)
4118
26.6k
        && s->min_proto_version <= TLS1_VERSION;
4119
88.1k
    SSL *ssl = SSL_CONNECTION_GET_SSL(s);
4120
4121
    /* Set disabled masks for this session */
4122
88.1k
    if (!ssl_set_client_disabled(s)) {
4123
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_PROTOCOLS_AVAILABLE);
4124
0
        return 0;
4125
0
    }
4126
4127
88.1k
    if (sk == NULL) {
4128
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4129
0
        return 0;
4130
0
    }
4131
4132
#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
4133
#if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6
4134
#error Max cipher length too short
4135
#endif
4136
    /*
4137
     * Some servers hang if client hello > 256 bytes as hack workaround
4138
     * chop number of supported ciphers to keep it well below this if we
4139
     * use TLS v1.2
4140
     */
4141
    if (TLS1_get_version(ssl) >= TLS1_2_VERSION)
4142
        maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
4143
    else
4144
#endif
4145
        /* Maximum length that can be stored in 2 bytes. Length must be even */
4146
88.1k
        maxlen = 0xfffe;
4147
4148
88.1k
    if (empty_reneg_info_scsv)
4149
26.6k
        maxlen -= 2;
4150
88.1k
    if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV)
4151
0
        maxlen -= 2;
4152
4153
8.53M
    for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) {
4154
8.44M
        const SSL_CIPHER *c;
4155
4156
8.44M
        c = sk_SSL_CIPHER_value(sk, i);
4157
        /* Skip disabled ciphers */
4158
8.44M
        if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0))
4159
3.64M
            continue;
4160
4161
4.79M
        if (!ssl->method->put_cipher_by_char(c, pkt, &len)) {
4162
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4163
0
            return 0;
4164
0
        }
4165
4166
        /* Sanity check that the maximum version we offer has ciphers enabled */
4167
4.79M
        if (!maxverok) {
4168
88.1k
            int minproto = SSL_CONNECTION_IS_DTLS(s) ? c->min_dtls : c->min_tls;
4169
88.1k
            int maxproto = SSL_CONNECTION_IS_DTLS(s) ? c->max_dtls : c->max_tls;
4170
4171
88.1k
            if (ssl_version_cmp(s, maxproto, s->s3.tmp.max_ver) >= 0
4172
88.1k
                && ssl_version_cmp(s, minproto, s->s3.tmp.max_ver) <= 0)
4173
88.1k
                maxverok = 1;
4174
88.1k
        }
4175
4176
4.79M
        totlen += len;
4177
4.79M
    }
4178
4179
88.1k
    if (totlen == 0 || !maxverok) {
4180
0
        const char *maxvertext = !maxverok
4181
0
            ? "No ciphers enabled for max supported SSL/TLS version"
4182
0
            : NULL;
4183
4184
0
        SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_CIPHERS_AVAILABLE,
4185
0
            maxvertext);
4186
0
        return 0;
4187
0
    }
4188
4189
88.1k
    if (totlen != 0) {
4190
88.1k
        if (empty_reneg_info_scsv) {
4191
26.6k
            static const SSL_CIPHER scsv = {
4192
26.6k
                0, NULL, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
4193
26.6k
            };
4194
26.6k
            if (!ssl->method->put_cipher_by_char(&scsv, pkt, &len)) {
4195
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4196
0
                return 0;
4197
0
            }
4198
26.6k
        }
4199
88.1k
        if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
4200
0
            static const SSL_CIPHER scsv = {
4201
0
                0, NULL, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
4202
0
            };
4203
0
            if (!ssl->method->put_cipher_by_char(&scsv, pkt, &len)) {
4204
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
4205
0
                return 0;
4206
0
            }
4207
0
        }
4208
88.1k
    }
4209
4210
88.1k
    return 1;
4211
88.1k
}
4212
4213
CON_FUNC_RETURN tls_construct_end_of_early_data(SSL_CONNECTION *s, WPACKET *pkt)
4214
0
{
4215
0
    if (s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY
4216
0
        && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) {
4217
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
4218
0
        return CON_FUNC_ERROR;
4219
0
    }
4220
4221
0
    s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
4222
0
    return CON_FUNC_SUCCESS;
4223
0
}