Coverage Report

Created: 2025-11-16 06:40

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