Coverage Report

Created: 2025-08-28 07:07

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