Coverage Report

Created: 2026-07-23 06:28

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