Coverage Report

Created: 2024-05-21 06:52

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