Coverage Report

Created: 2026-04-01 06:39

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