Coverage Report

Created: 2025-11-16 06:40

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