Coverage Report

Created: 2025-12-31 06:58

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