Coverage Report

Created: 2025-12-04 06:33

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