Coverage Report

Created: 2025-12-31 06:58

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