Coverage Report

Created: 2018-08-29 13:53

/src/openssl/ssl/statem/statem_srvr.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2018 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 OpenSSL license (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 "../ssl_locl.h"
14
#include "statem_locl.h"
15
#include "internal/constant_time_locl.h"
16
#include "internal/cryptlib.h"
17
#include <openssl/buffer.h>
18
#include <openssl/rand.h>
19
#include <openssl/objects.h>
20
#include <openssl/evp.h>
21
#include <openssl/hmac.h>
22
#include <openssl/x509.h>
23
#include <openssl/dh.h>
24
#include <openssl/bn.h>
25
#include <openssl/md5.h>
26
27
0
#define TICKET_NONCE_SIZE       8
28
29
static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt);
30
31
/*
32
 * ossl_statem_server13_read_transition() encapsulates the logic for the allowed
33
 * handshake state transitions when a TLSv1.3 server is reading messages from
34
 * the client. The message type that the client has sent is provided in |mt|.
35
 * The current state is in |s->statem.hand_state|.
36
 *
37
 * Return values are 1 for success (transition allowed) and  0 on error
38
 * (transition not allowed)
39
 */
40
static int ossl_statem_server13_read_transition(SSL *s, int mt)
41
0
{
42
0
    OSSL_STATEM *st = &s->statem;
43
0
44
0
    /*
45
0
     * Note: There is no case for TLS_ST_BEFORE because at that stage we have
46
0
     * not negotiated TLSv1.3 yet, so that case is handled by
47
0
     * ossl_statem_server_read_transition()
48
0
     */
49
0
    switch (st->hand_state) {
50
0
    default:
51
0
        break;
52
0
53
0
    case TLS_ST_EARLY_DATA:
54
0
        if (s->hello_retry_request == SSL_HRR_PENDING) {
55
0
            if (mt == SSL3_MT_CLIENT_HELLO) {
56
0
                st->hand_state = TLS_ST_SR_CLNT_HELLO;
57
0
                return 1;
58
0
            }
59
0
            break;
60
0
        } else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
61
0
            if (mt == SSL3_MT_END_OF_EARLY_DATA) {
62
0
                st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA;
63
0
                return 1;
64
0
            }
65
0
            break;
66
0
        }
67
0
        /* Fall through */
68
0
69
0
    case TLS_ST_SR_END_OF_EARLY_DATA:
70
0
    case TLS_ST_SW_FINISHED:
71
0
        if (s->s3->tmp.cert_request) {
72
0
            if (mt == SSL3_MT_CERTIFICATE) {
73
0
                st->hand_state = TLS_ST_SR_CERT;
74
0
                return 1;
75
0
            }
76
0
        } else {
77
0
            if (mt == SSL3_MT_FINISHED) {
78
0
                st->hand_state = TLS_ST_SR_FINISHED;
79
0
                return 1;
80
0
            }
81
0
        }
82
0
        break;
83
0
84
0
    case TLS_ST_SR_CERT:
85
0
        if (s->session->peer == NULL) {
86
0
            if (mt == SSL3_MT_FINISHED) {
87
0
                st->hand_state = TLS_ST_SR_FINISHED;
88
0
                return 1;
89
0
            }
90
0
        } else {
91
0
            if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
92
0
                st->hand_state = TLS_ST_SR_CERT_VRFY;
93
0
                return 1;
94
0
            }
95
0
        }
96
0
        break;
97
0
98
0
    case TLS_ST_SR_CERT_VRFY:
99
0
        if (mt == SSL3_MT_FINISHED) {
100
0
            st->hand_state = TLS_ST_SR_FINISHED;
101
0
            return 1;
102
0
        }
103
0
        break;
104
0
105
0
    case TLS_ST_OK:
106
0
        /*
107
0
         * Its never ok to start processing handshake messages in the middle of
108
0
         * early data (i.e. before we've received the end of early data alert)
109
0
         */
110
0
        if (s->early_data_state == SSL_EARLY_DATA_READING)
111
0
            break;
112
0
113
0
        if (mt == SSL3_MT_CERTIFICATE
114
0
                && s->post_handshake_auth == SSL_PHA_REQUESTED) {
115
0
            st->hand_state = TLS_ST_SR_CERT;
116
0
            return 1;
117
0
        }
118
0
119
0
        if (mt == SSL3_MT_KEY_UPDATE) {
120
0
            st->hand_state = TLS_ST_SR_KEY_UPDATE;
121
0
            return 1;
122
0
        }
123
0
        break;
124
0
    }
125
0
126
0
    /* No valid transition found */
127
0
    return 0;
128
0
}
129
130
/*
131
 * ossl_statem_server_read_transition() encapsulates the logic for the allowed
132
 * handshake state transitions when the server is reading messages from the
133
 * client. The message type that the client has sent is provided in |mt|. The
134
 * current state is in |s->statem.hand_state|.
135
 *
136
 * Return values are 1 for success (transition allowed) and  0 on error
137
 * (transition not allowed)
138
 */
139
int ossl_statem_server_read_transition(SSL *s, int mt)
140
0
{
141
0
    OSSL_STATEM *st = &s->statem;
142
0
143
0
    if (SSL_IS_TLS13(s)) {
144
0
        if (!ossl_statem_server13_read_transition(s, mt))
145
0
            goto err;
146
0
        return 1;
147
0
    }
148
0
149
0
    switch (st->hand_state) {
150
0
    default:
151
0
        break;
152
0
153
0
    case TLS_ST_BEFORE:
154
0
    case TLS_ST_OK:
155
0
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
156
0
        if (mt == SSL3_MT_CLIENT_HELLO) {
157
0
            st->hand_state = TLS_ST_SR_CLNT_HELLO;
158
0
            return 1;
159
0
        }
160
0
        break;
161
0
162
0
    case TLS_ST_SW_SRVR_DONE:
163
0
        /*
164
0
         * If we get a CKE message after a ServerDone then either
165
0
         * 1) We didn't request a Certificate
166
0
         * OR
167
0
         * 2) If we did request one then
168
0
         *      a) We allow no Certificate to be returned
169
0
         *      AND
170
0
         *      b) We are running SSL3 (in TLS1.0+ the client must return a 0
171
0
         *         list if we requested a certificate)
172
0
         */
173
0
        if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
174
0
            if (s->s3->tmp.cert_request) {
175
0
                if (s->version == SSL3_VERSION) {
176
0
                    if ((s->verify_mode & SSL_VERIFY_PEER)
177
0
                        && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
178
0
                        /*
179
0
                         * This isn't an unexpected message as such - we're just
180
0
                         * not going to accept it because we require a client
181
0
                         * cert.
182
0
                         */
183
0
                        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
184
0
                                 SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
185
0
                                 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
186
0
                        return 0;
187
0
                    }
188
0
                    st->hand_state = TLS_ST_SR_KEY_EXCH;
189
0
                    return 1;
190
0
                }
191
0
            } else {
192
0
                st->hand_state = TLS_ST_SR_KEY_EXCH;
193
0
                return 1;
194
0
            }
195
0
        } else if (s->s3->tmp.cert_request) {
196
0
            if (mt == SSL3_MT_CERTIFICATE) {
197
0
                st->hand_state = TLS_ST_SR_CERT;
198
0
                return 1;
199
0
            }
200
0
        }
201
0
        break;
202
0
203
0
    case TLS_ST_SR_CERT:
204
0
        if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) {
205
0
            st->hand_state = TLS_ST_SR_KEY_EXCH;
206
0
            return 1;
207
0
        }
208
0
        break;
209
0
210
0
    case TLS_ST_SR_KEY_EXCH:
211
0
        /*
212
0
         * We should only process a CertificateVerify message if we have
213
0
         * received a Certificate from the client. If so then |s->session->peer|
214
0
         * will be non NULL. In some instances a CertificateVerify message is
215
0
         * not required even if the peer has sent a Certificate (e.g. such as in
216
0
         * the case of static DH). In that case |st->no_cert_verify| should be
217
0
         * set.
218
0
         */
219
0
        if (s->session->peer == NULL || st->no_cert_verify) {
220
0
            if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
221
0
                /*
222
0
                 * For the ECDH ciphersuites when the client sends its ECDH
223
0
                 * pub key in a certificate, the CertificateVerify message is
224
0
                 * not sent. Also for GOST ciphersuites when the client uses
225
0
                 * its key from the certificate for key exchange.
226
0
                 */
227
0
                st->hand_state = TLS_ST_SR_CHANGE;
228
0
                return 1;
229
0
            }
230
0
        } else {
231
0
            if (mt == SSL3_MT_CERTIFICATE_VERIFY) {
232
0
                st->hand_state = TLS_ST_SR_CERT_VRFY;
233
0
                return 1;
234
0
            }
235
0
        }
236
0
        break;
237
0
238
0
    case TLS_ST_SR_CERT_VRFY:
239
0
        if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
240
0
            st->hand_state = TLS_ST_SR_CHANGE;
241
0
            return 1;
242
0
        }
243
0
        break;
244
0
245
0
    case TLS_ST_SR_CHANGE:
246
0
#ifndef OPENSSL_NO_NEXTPROTONEG
247
0
        if (s->s3->npn_seen) {
248
0
            if (mt == SSL3_MT_NEXT_PROTO) {
249
0
                st->hand_state = TLS_ST_SR_NEXT_PROTO;
250
0
                return 1;
251
0
            }
252
0
        } else {
253
0
#endif
254
0
            if (mt == SSL3_MT_FINISHED) {
255
0
                st->hand_state = TLS_ST_SR_FINISHED;
256
0
                return 1;
257
0
            }
258
0
#ifndef OPENSSL_NO_NEXTPROTONEG
259
0
        }
260
0
#endif
261
0
        break;
262
0
263
0
#ifndef OPENSSL_NO_NEXTPROTONEG
264
0
    case TLS_ST_SR_NEXT_PROTO:
265
0
        if (mt == SSL3_MT_FINISHED) {
266
0
            st->hand_state = TLS_ST_SR_FINISHED;
267
0
            return 1;
268
0
        }
269
0
        break;
270
0
#endif
271
0
272
0
    case TLS_ST_SW_FINISHED:
273
0
        if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
274
0
            st->hand_state = TLS_ST_SR_CHANGE;
275
0
            return 1;
276
0
        }
277
0
        break;
278
0
    }
279
0
280
0
 err:
281
0
    /* No valid transition found */
282
0
    if (SSL_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
283
0
        BIO *rbio;
284
0
285
0
        /*
286
0
         * CCS messages don't have a message sequence number so this is probably
287
0
         * because of an out-of-order CCS. We'll just drop it.
288
0
         */
289
0
        s->init_num = 0;
290
0
        s->rwstate = SSL_READING;
291
0
        rbio = SSL_get_rbio(s);
292
0
        BIO_clear_retry_flags(rbio);
293
0
        BIO_set_retry_read(rbio);
294
0
        return 0;
295
0
    }
296
0
    SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE,
297
0
             SSL_F_OSSL_STATEM_SERVER_READ_TRANSITION,
298
0
             SSL_R_UNEXPECTED_MESSAGE);
299
0
    return 0;
300
0
}
301
302
/*
303
 * Should we send a ServerKeyExchange message?
304
 *
305
 * Valid return values are:
306
 *   1: Yes
307
 *   0: No
308
 */
309
static int send_server_key_exchange(SSL *s)
310
0
{
311
0
    unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
312
0
313
0
    /*
314
0
     * only send a ServerKeyExchange if DH or fortezza but we have a
315
0
     * sign only certificate PSK: may send PSK identity hints For
316
0
     * ECC ciphersuites, we send a serverKeyExchange message only if
317
0
     * the cipher suite is either ECDH-anon or ECDHE. In other cases,
318
0
     * the server certificate contains the server's public key for
319
0
     * key exchange.
320
0
     */
321
0
    if (alg_k & (SSL_kDHE | SSL_kECDHE)
322
0
        /*
323
0
         * PSK: send ServerKeyExchange if PSK identity hint if
324
0
         * provided
325
0
         */
326
0
#ifndef OPENSSL_NO_PSK
327
0
        /* Only send SKE if we have identity hint for plain PSK */
328
0
        || ((alg_k & (SSL_kPSK | SSL_kRSAPSK))
329
0
            && s->cert->psk_identity_hint)
330
0
        /* For other PSK always send SKE */
331
0
        || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK)))
332
0
#endif
333
0
#ifndef OPENSSL_NO_SRP
334
0
        /* SRP: send ServerKeyExchange */
335
0
        || (alg_k & SSL_kSRP)
336
0
#endif
337
0
        ) {
338
0
        return 1;
339
0
    }
340
0
341
0
    return 0;
342
0
}
343
344
/*
345
 * Should we send a CertificateRequest message?
346
 *
347
 * Valid return values are:
348
 *   1: Yes
349
 *   0: No
350
 */
351
int send_certificate_request(SSL *s)
352
0
{
353
0
    if (
354
0
           /* don't request cert unless asked for it: */
355
0
           s->verify_mode & SSL_VERIFY_PEER
356
0
           /*
357
0
            * don't request if post-handshake-only unless doing
358
0
            * post-handshake in TLSv1.3:
359
0
            */
360
0
           && (!SSL_IS_TLS13(s) || !(s->verify_mode & SSL_VERIFY_POST_HANDSHAKE)
361
0
               || s->post_handshake_auth == SSL_PHA_REQUEST_PENDING)
362
0
           /*
363
0
            * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
364
0
            * a second time:
365
0
            */
366
0
           && (s->certreqs_sent < 1 ||
367
0
               !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE))
368
0
           /*
369
0
            * never request cert in anonymous ciphersuites (see
370
0
            * section "Certificate request" in SSL 3 drafts and in
371
0
            * RFC 2246):
372
0
            */
373
0
           && (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
374
0
               /*
375
0
                * ... except when the application insists on
376
0
                * verification (against the specs, but statem_clnt.c accepts
377
0
                * this for SSL 3)
378
0
                */
379
0
               || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
380
0
           /* don't request certificate for SRP auth */
381
0
           && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
382
0
           /*
383
0
            * With normal PSK Certificates and Certificate Requests
384
0
            * are omitted
385
0
            */
386
0
           && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)) {
387
0
        return 1;
388
0
    }
389
0
390
0
    return 0;
391
0
}
392
393
/*
394
 * ossl_statem_server13_write_transition() works out what handshake state to
395
 * move to next when a TLSv1.3 server is writing messages to be sent to the
396
 * client.
397
 */
398
static WRITE_TRAN ossl_statem_server13_write_transition(SSL *s)
399
0
{
400
0
    OSSL_STATEM *st = &s->statem;
401
0
402
0
    /*
403
0
     * No case for TLS_ST_BEFORE, because at that stage we have not negotiated
404
0
     * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition()
405
0
     */
406
0
407
0
    switch (st->hand_state) {
408
0
    default:
409
0
        /* Shouldn't happen */
410
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
411
0
                 SSL_F_OSSL_STATEM_SERVER13_WRITE_TRANSITION,
412
0
                 ERR_R_INTERNAL_ERROR);
413
0
        return WRITE_TRAN_ERROR;
414
0
415
0
    case TLS_ST_OK:
416
0
        if (s->key_update != SSL_KEY_UPDATE_NONE) {
417
0
            st->hand_state = TLS_ST_SW_KEY_UPDATE;
418
0
            return WRITE_TRAN_CONTINUE;
419
0
        }
420
0
        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
421
0
            st->hand_state = TLS_ST_SW_CERT_REQ;
422
0
            return WRITE_TRAN_CONTINUE;
423
0
        }
424
0
        /* Try to read from the client instead */
425
0
        return WRITE_TRAN_FINISHED;
426
0
427
0
    case TLS_ST_SR_CLNT_HELLO:
428
0
        st->hand_state = TLS_ST_SW_SRVR_HELLO;
429
0
        return WRITE_TRAN_CONTINUE;
430
0
431
0
    case TLS_ST_SW_SRVR_HELLO:
432
0
        if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
433
0
                && s->hello_retry_request != SSL_HRR_COMPLETE)
434
0
            st->hand_state = TLS_ST_SW_CHANGE;
435
0
        else if (s->hello_retry_request == SSL_HRR_PENDING)
436
0
            st->hand_state = TLS_ST_EARLY_DATA;
437
0
        else
438
0
            st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
439
0
        return WRITE_TRAN_CONTINUE;
440
0
441
0
    case TLS_ST_SW_CHANGE:
442
0
        if (s->hello_retry_request == SSL_HRR_PENDING)
443
0
            st->hand_state = TLS_ST_EARLY_DATA;
444
0
        else
445
0
            st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS;
446
0
        return WRITE_TRAN_CONTINUE;
447
0
448
0
    case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
449
0
        if (s->hit)
450
0
            st->hand_state = TLS_ST_SW_FINISHED;
451
0
        else if (send_certificate_request(s))
452
0
            st->hand_state = TLS_ST_SW_CERT_REQ;
453
0
        else
454
0
            st->hand_state = TLS_ST_SW_CERT;
455
0
456
0
        return WRITE_TRAN_CONTINUE;
457
0
458
0
    case TLS_ST_SW_CERT_REQ:
459
0
        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
460
0
            s->post_handshake_auth = SSL_PHA_REQUESTED;
461
0
            st->hand_state = TLS_ST_OK;
462
0
        } else {
463
0
            st->hand_state = TLS_ST_SW_CERT;
464
0
        }
465
0
        return WRITE_TRAN_CONTINUE;
466
0
467
0
    case TLS_ST_SW_CERT:
468
0
        st->hand_state = TLS_ST_SW_CERT_VRFY;
469
0
        return WRITE_TRAN_CONTINUE;
470
0
471
0
    case TLS_ST_SW_CERT_VRFY:
472
0
        st->hand_state = TLS_ST_SW_FINISHED;
473
0
        return WRITE_TRAN_CONTINUE;
474
0
475
0
    case TLS_ST_SW_FINISHED:
476
0
        st->hand_state = TLS_ST_EARLY_DATA;
477
0
        return WRITE_TRAN_CONTINUE;
478
0
479
0
    case TLS_ST_EARLY_DATA:
480
0
        return WRITE_TRAN_FINISHED;
481
0
482
0
    case TLS_ST_SR_FINISHED:
483
0
        /*
484
0
         * Technically we have finished the handshake at this point, but we're
485
0
         * going to remain "in_init" for now and write out any session tickets
486
0
         * immediately.
487
0
         */
488
0
        if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
489
0
            s->post_handshake_auth = SSL_PHA_EXT_RECEIVED;
490
0
        } else if (!s->ext.ticket_expected) {
491
0
            /*
492
0
             * If we're not going to renew the ticket then we just finish the
493
0
             * handshake at this point.
494
0
             */
495
0
            st->hand_state = TLS_ST_OK;
496
0
            return WRITE_TRAN_CONTINUE;
497
0
        }
498
0
        if (s->num_tickets > s->sent_tickets)
499
0
            st->hand_state = TLS_ST_SW_SESSION_TICKET;
500
0
        else
501
0
            st->hand_state = TLS_ST_OK;
502
0
        return WRITE_TRAN_CONTINUE;
503
0
504
0
    case TLS_ST_SR_KEY_UPDATE:
505
0
        if (s->key_update != SSL_KEY_UPDATE_NONE) {
506
0
            st->hand_state = TLS_ST_SW_KEY_UPDATE;
507
0
            return WRITE_TRAN_CONTINUE;
508
0
        }
509
0
        /* Fall through */
510
0
511
0
    case TLS_ST_SW_KEY_UPDATE:
512
0
        st->hand_state = TLS_ST_OK;
513
0
        return WRITE_TRAN_CONTINUE;
514
0
515
0
    case TLS_ST_SW_SESSION_TICKET:
516
0
        /* In a resumption we only ever send a maximum of one new ticket.
517
0
         * Following an initial handshake we send the number of tickets we have
518
0
         * been configured for.
519
0
         */
520
0
        if (s->hit || s->num_tickets <= s->sent_tickets) {
521
0
            /* We've written enough tickets out. */
522
0
            st->hand_state = TLS_ST_OK;
523
0
        }
524
0
        return WRITE_TRAN_CONTINUE;
525
0
    }
526
0
}
527
528
/*
529
 * ossl_statem_server_write_transition() works out what handshake state to move
530
 * to next when the server is writing messages to be sent to the client.
531
 */
532
WRITE_TRAN ossl_statem_server_write_transition(SSL *s)
533
0
{
534
0
    OSSL_STATEM *st = &s->statem;
535
0
536
0
    /*
537
0
     * Note that before the ClientHello we don't know what version we are going
538
0
     * to negotiate yet, so we don't take this branch until later
539
0
     */
540
0
541
0
    if (SSL_IS_TLS13(s))
542
0
        return ossl_statem_server13_write_transition(s);
543
0
544
0
    switch (st->hand_state) {
545
0
    default:
546
0
        /* Shouldn't happen */
547
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
548
0
                 SSL_F_OSSL_STATEM_SERVER_WRITE_TRANSITION,
549
0
                 ERR_R_INTERNAL_ERROR);
550
0
        return WRITE_TRAN_ERROR;
551
0
552
0
    case TLS_ST_OK:
553
0
        if (st->request_state == TLS_ST_SW_HELLO_REQ) {
554
0
            /* We must be trying to renegotiate */
555
0
            st->hand_state = TLS_ST_SW_HELLO_REQ;
556
0
            st->request_state = TLS_ST_BEFORE;
557
0
            return WRITE_TRAN_CONTINUE;
558
0
        }
559
0
        /* Must be an incoming ClientHello */
560
0
        if (!tls_setup_handshake(s)) {
561
0
            /* SSLfatal() already called */
562
0
            return WRITE_TRAN_ERROR;
563
0
        }
564
0
        /* Fall through */
565
0
566
0
    case TLS_ST_BEFORE:
567
0
        /* Just go straight to trying to read from the client */
568
0
        return WRITE_TRAN_FINISHED;
569
0
570
0
    case TLS_ST_SW_HELLO_REQ:
571
0
        st->hand_state = TLS_ST_OK;
572
0
        return WRITE_TRAN_CONTINUE;
573
0
574
0
    case TLS_ST_SR_CLNT_HELLO:
575
0
        if (SSL_IS_DTLS(s) && !s->d1->cookie_verified
576
0
            && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) {
577
0
            st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST;
578
0
        } else if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
579
0
            /* We must have rejected the renegotiation */
580
0
            st->hand_state = TLS_ST_OK;
581
0
            return WRITE_TRAN_CONTINUE;
582
0
        } else {
583
0
            st->hand_state = TLS_ST_SW_SRVR_HELLO;
584
0
        }
585
0
        return WRITE_TRAN_CONTINUE;
586
0
587
0
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
588
0
        return WRITE_TRAN_FINISHED;
589
0
590
0
    case TLS_ST_SW_SRVR_HELLO:
591
0
        if (s->hit) {
592
0
            if (s->ext.ticket_expected)
593
0
                st->hand_state = TLS_ST_SW_SESSION_TICKET;
594
0
            else
595
0
                st->hand_state = TLS_ST_SW_CHANGE;
596
0
        } else {
597
0
            /* Check if it is anon DH or anon ECDH, */
598
0
            /* normal PSK or SRP */
599
0
            if (!(s->s3->tmp.new_cipher->algorithm_auth &
600
0
                  (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
601
0
                st->hand_state = TLS_ST_SW_CERT;
602
0
            } else if (send_server_key_exchange(s)) {
603
0
                st->hand_state = TLS_ST_SW_KEY_EXCH;
604
0
            } else if (send_certificate_request(s)) {
605
0
                st->hand_state = TLS_ST_SW_CERT_REQ;
606
0
            } else {
607
0
                st->hand_state = TLS_ST_SW_SRVR_DONE;
608
0
            }
609
0
        }
610
0
        return WRITE_TRAN_CONTINUE;
611
0
612
0
    case TLS_ST_SW_CERT:
613
0
        if (s->ext.status_expected) {
614
0
            st->hand_state = TLS_ST_SW_CERT_STATUS;
615
0
            return WRITE_TRAN_CONTINUE;
616
0
        }
617
0
        /* Fall through */
618
0
619
0
    case TLS_ST_SW_CERT_STATUS:
620
0
        if (send_server_key_exchange(s)) {
621
0
            st->hand_state = TLS_ST_SW_KEY_EXCH;
622
0
            return WRITE_TRAN_CONTINUE;
623
0
        }
624
0
        /* Fall through */
625
0
626
0
    case TLS_ST_SW_KEY_EXCH:
627
0
        if (send_certificate_request(s)) {
628
0
            st->hand_state = TLS_ST_SW_CERT_REQ;
629
0
            return WRITE_TRAN_CONTINUE;
630
0
        }
631
0
        /* Fall through */
632
0
633
0
    case TLS_ST_SW_CERT_REQ:
634
0
        st->hand_state = TLS_ST_SW_SRVR_DONE;
635
0
        return WRITE_TRAN_CONTINUE;
636
0
637
0
    case TLS_ST_SW_SRVR_DONE:
638
0
        return WRITE_TRAN_FINISHED;
639
0
640
0
    case TLS_ST_SR_FINISHED:
641
0
        if (s->hit) {
642
0
            st->hand_state = TLS_ST_OK;
643
0
            return WRITE_TRAN_CONTINUE;
644
0
        } else if (s->ext.ticket_expected) {
645
0
            st->hand_state = TLS_ST_SW_SESSION_TICKET;
646
0
        } else {
647
0
            st->hand_state = TLS_ST_SW_CHANGE;
648
0
        }
649
0
        return WRITE_TRAN_CONTINUE;
650
0
651
0
    case TLS_ST_SW_SESSION_TICKET:
652
0
        st->hand_state = TLS_ST_SW_CHANGE;
653
0
        return WRITE_TRAN_CONTINUE;
654
0
655
0
    case TLS_ST_SW_CHANGE:
656
0
        st->hand_state = TLS_ST_SW_FINISHED;
657
0
        return WRITE_TRAN_CONTINUE;
658
0
659
0
    case TLS_ST_SW_FINISHED:
660
0
        if (s->hit) {
661
0
            return WRITE_TRAN_FINISHED;
662
0
        }
663
0
        st->hand_state = TLS_ST_OK;
664
0
        return WRITE_TRAN_CONTINUE;
665
0
    }
666
0
}
667
668
/*
669
 * Perform any pre work that needs to be done prior to sending a message from
670
 * the server to the client.
671
 */
672
WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst)
673
0
{
674
0
    OSSL_STATEM *st = &s->statem;
675
0
676
0
    switch (st->hand_state) {
677
0
    default:
678
0
        /* No pre work to be done */
679
0
        break;
680
0
681
0
    case TLS_ST_SW_HELLO_REQ:
682
0
        s->shutdown = 0;
683
0
        if (SSL_IS_DTLS(s))
684
0
            dtls1_clear_sent_buffer(s);
685
0
        break;
686
0
687
0
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
688
0
        s->shutdown = 0;
689
0
        if (SSL_IS_DTLS(s)) {
690
0
            dtls1_clear_sent_buffer(s);
691
0
            /* We don't buffer this message so don't use the timer */
692
0
            st->use_timer = 0;
693
0
        }
694
0
        break;
695
0
696
0
    case TLS_ST_SW_SRVR_HELLO:
697
0
        if (SSL_IS_DTLS(s)) {
698
0
            /*
699
0
             * Messages we write from now on should be buffered and
700
0
             * retransmitted if necessary, so we need to use the timer now
701
0
             */
702
0
            st->use_timer = 1;
703
0
        }
704
0
        break;
705
0
706
0
    case TLS_ST_SW_SRVR_DONE:
707
#ifndef OPENSSL_NO_SCTP
708
        if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
709
            /* Calls SSLfatal() as required */
710
            return dtls_wait_for_dry(s);
711
        }
712
#endif
713
        return WORK_FINISHED_CONTINUE;
714
0
715
0
    case TLS_ST_SW_SESSION_TICKET:
716
0
        if (SSL_IS_TLS13(s) && s->sent_tickets == 0) {
717
0
            /*
718
0
             * Actually this is the end of the handshake, but we're going
719
0
             * straight into writing the session ticket out. So we finish off
720
0
             * the handshake, but keep the various buffers active.
721
0
             *
722
0
             * Calls SSLfatal as required.
723
0
             */
724
0
            return tls_finish_handshake(s, wst, 0, 0);
725
0
        } if (SSL_IS_DTLS(s)) {
726
0
            /*
727
0
             * We're into the last flight. We don't retransmit the last flight
728
0
             * unless we need to, so we don't use the timer
729
0
             */
730
0
            st->use_timer = 0;
731
0
        }
732
0
        break;
733
0
734
0
    case TLS_ST_SW_CHANGE:
735
0
        if (SSL_IS_TLS13(s))
736
0
            break;
737
0
        s->session->cipher = s->s3->tmp.new_cipher;
738
0
        if (!s->method->ssl3_enc->setup_key_block(s)) {
739
0
            /* SSLfatal() already called */
740
0
            return WORK_ERROR;
741
0
        }
742
0
        if (SSL_IS_DTLS(s)) {
743
0
            /*
744
0
             * We're into the last flight. We don't retransmit the last flight
745
0
             * unless we need to, so we don't use the timer. This might have
746
0
             * already been set to 0 if we sent a NewSessionTicket message,
747
0
             * but we'll set it again here in case we didn't.
748
0
             */
749
0
            st->use_timer = 0;
750
0
        }
751
0
        return WORK_FINISHED_CONTINUE;
752
0
753
0
    case TLS_ST_EARLY_DATA:
754
0
        if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING
755
0
                && (s->s3->flags & TLS1_FLAGS_STATELESS) == 0)
756
0
            return WORK_FINISHED_CONTINUE;
757
0
        /* Fall through */
758
0
759
0
    case TLS_ST_OK:
760
0
        /* Calls SSLfatal() as required */
761
0
        return tls_finish_handshake(s, wst, 1, 1);
762
0
    }
763
0
764
0
    return WORK_FINISHED_CONTINUE;
765
0
}
766
767
/*
768
 * Perform any work that needs to be done after sending a message from the
769
 * server to the client.
770
 */
771
WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst)
772
0
{
773
0
    OSSL_STATEM *st = &s->statem;
774
0
775
0
    s->init_num = 0;
776
0
777
0
    switch (st->hand_state) {
778
0
    default:
779
0
        /* No post work to be done */
780
0
        break;
781
0
782
0
    case TLS_ST_SW_HELLO_REQ:
783
0
        if (statem_flush(s) != 1)
784
0
            return WORK_MORE_A;
785
0
        if (!ssl3_init_finished_mac(s)) {
786
0
            /* SSLfatal() already called */
787
0
            return WORK_ERROR;
788
0
        }
789
0
        break;
790
0
791
0
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
792
0
        if (statem_flush(s) != 1)
793
0
            return WORK_MORE_A;
794
0
        /* HelloVerifyRequest resets Finished MAC */
795
0
        if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) {
796
0
            /* SSLfatal() already called */
797
0
            return WORK_ERROR;
798
0
        }
799
0
        /*
800
0
         * The next message should be another ClientHello which we need to
801
0
         * treat like it was the first packet
802
0
         */
803
0
        s->first_packet = 1;
804
0
        break;
805
0
806
0
    case TLS_ST_SW_SRVR_HELLO:
807
0
        if (SSL_IS_TLS13(s) && s->hello_retry_request == SSL_HRR_PENDING) {
808
0
            if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0
809
0
                    && statem_flush(s) != 1)
810
0
                return WORK_MORE_A;
811
0
            break;
812
0
        }
813
#ifndef OPENSSL_NO_SCTP
814
        if (SSL_IS_DTLS(s) && s->hit) {
815
            unsigned char sctpauthkey[64];
816
            char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
817
818
            /*
819
             * Add new shared key for SCTP-Auth, will be ignored if no
820
             * SCTP used.
821
             */
822
            memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
823
                   sizeof(DTLS1_SCTP_AUTH_LABEL));
824
825
            if (SSL_export_keying_material(s, sctpauthkey,
826
                                           sizeof(sctpauthkey), labelbuffer,
827
                                           sizeof(labelbuffer), NULL, 0,
828
                                           0) <= 0) {
829
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
830
                         SSL_F_OSSL_STATEM_SERVER_POST_WORK,
831
                         ERR_R_INTERNAL_ERROR);
832
                return WORK_ERROR;
833
            }
834
835
            BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
836
                     sizeof(sctpauthkey), sctpauthkey);
837
        }
838
#endif
839
0
        if (!SSL_IS_TLS13(s)
840
0
                || ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0
841
0
                    && s->hello_retry_request != SSL_HRR_COMPLETE))
842
0
            break;
843
0
        /* Fall through */
844
0
845
0
    case TLS_ST_SW_CHANGE:
846
0
        if (s->hello_retry_request == SSL_HRR_PENDING) {
847
0
            if (!statem_flush(s))
848
0
                return WORK_MORE_A;
849
0
            break;
850
0
        }
851
0
852
0
        if (SSL_IS_TLS13(s)) {
853
0
            if (!s->method->ssl3_enc->setup_key_block(s)
854
0
                || !s->method->ssl3_enc->change_cipher_state(s,
855
0
                        SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
856
0
                /* SSLfatal() already called */
857
0
                return WORK_ERROR;
858
0
            }
859
0
860
0
            if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED
861
0
                && !s->method->ssl3_enc->change_cipher_state(s,
862
0
                        SSL3_CC_HANDSHAKE |SSL3_CHANGE_CIPHER_SERVER_READ)) {
863
0
                /* SSLfatal() already called */
864
0
                return WORK_ERROR;
865
0
            }
866
0
            /*
867
0
             * We don't yet know whether the next record we are going to receive
868
0
             * is an unencrypted alert, an encrypted alert, or an encrypted
869
0
             * handshake message. We temporarily tolerate unencrypted alerts.
870
0
             */
871
0
            s->statem.enc_read_state = ENC_READ_STATE_ALLOW_PLAIN_ALERTS;
872
0
            break;
873
0
        }
874
0
875
#ifndef OPENSSL_NO_SCTP
876
        if (SSL_IS_DTLS(s) && !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(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
882
                     0, NULL);
883
        }
884
#endif
885
0
        if (!s->method->ssl3_enc->change_cipher_state(s,
886
0
                                                      SSL3_CHANGE_CIPHER_SERVER_WRITE))
887
0
        {
888
0
            /* SSLfatal() already called */
889
0
            return WORK_ERROR;
890
0
        }
891
0
892
0
        if (SSL_IS_DTLS(s))
893
0
            dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
894
0
        break;
895
0
896
0
    case TLS_ST_SW_SRVR_DONE:
897
0
        if (statem_flush(s) != 1)
898
0
            return WORK_MORE_A;
899
0
        break;
900
0
901
0
    case TLS_ST_SW_FINISHED:
902
0
        if (statem_flush(s) != 1)
903
0
            return WORK_MORE_A;
904
#ifndef OPENSSL_NO_SCTP
905
        if (SSL_IS_DTLS(s) && s->hit) {
906
            /*
907
             * Change to new shared key of SCTP-Auth, will be ignored if
908
             * no SCTP used.
909
             */
910
            BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
911
                     0, NULL);
912
        }
913
#endif
914
0
        if (SSL_IS_TLS13(s)) {
915
0
            if (!s->method->ssl3_enc->generate_master_secret(s,
916
0
                        s->master_secret, s->handshake_secret, 0,
917
0
                        &s->session->master_key_length)
918
0
                || !s->method->ssl3_enc->change_cipher_state(s,
919
0
                        SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE))
920
0
            /* SSLfatal() already called */
921
0
            return WORK_ERROR;
922
0
        }
923
0
        break;
924
0
925
0
    case TLS_ST_SW_CERT_REQ:
926
0
        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
927
0
            if (statem_flush(s) != 1)
928
0
                return WORK_MORE_A;
929
0
        }
930
0
        break;
931
0
932
0
    case TLS_ST_SW_KEY_UPDATE:
933
0
        if (statem_flush(s) != 1)
934
0
            return WORK_MORE_A;
935
0
        if (!tls13_update_key(s, 1)) {
936
0
            /* SSLfatal() already called */
937
0
            return WORK_ERROR;
938
0
        }
939
0
        break;
940
0
941
0
    case TLS_ST_SW_SESSION_TICKET:
942
0
        if (SSL_IS_TLS13(s) && statem_flush(s) != 1)
943
0
            return WORK_MORE_A;
944
0
        break;
945
0
    }
946
0
947
0
    return WORK_FINISHED_CONTINUE;
948
0
}
949
950
/*
951
 * Get the message construction function and message type for sending from the
952
 * server
953
 *
954
 * Valid return values are:
955
 *   1: Success
956
 *   0: Error
957
 */
958
int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
959
                                         confunc_f *confunc, int *mt)
960
0
{
961
0
    OSSL_STATEM *st = &s->statem;
962
0
963
0
    switch (st->hand_state) {
964
0
    default:
965
0
        /* Shouldn't happen */
966
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
967
0
                 SSL_F_OSSL_STATEM_SERVER_CONSTRUCT_MESSAGE,
968
0
                 SSL_R_BAD_HANDSHAKE_STATE);
969
0
        return 0;
970
0
971
0
    case TLS_ST_SW_CHANGE:
972
0
        if (SSL_IS_DTLS(s))
973
0
            *confunc = dtls_construct_change_cipher_spec;
974
0
        else
975
0
            *confunc = tls_construct_change_cipher_spec;
976
0
        *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
977
0
        break;
978
0
979
0
    case DTLS_ST_SW_HELLO_VERIFY_REQUEST:
980
0
        *confunc = dtls_construct_hello_verify_request;
981
0
        *mt = DTLS1_MT_HELLO_VERIFY_REQUEST;
982
0
        break;
983
0
984
0
    case TLS_ST_SW_HELLO_REQ:
985
0
        /* No construction function needed */
986
0
        *confunc = NULL;
987
0
        *mt = SSL3_MT_HELLO_REQUEST;
988
0
        break;
989
0
990
0
    case TLS_ST_SW_SRVR_HELLO:
991
0
        *confunc = tls_construct_server_hello;
992
0
        *mt = SSL3_MT_SERVER_HELLO;
993
0
        break;
994
0
995
0
    case TLS_ST_SW_CERT:
996
0
        *confunc = tls_construct_server_certificate;
997
0
        *mt = SSL3_MT_CERTIFICATE;
998
0
        break;
999
0
1000
0
    case TLS_ST_SW_CERT_VRFY:
1001
0
        *confunc = tls_construct_cert_verify;
1002
0
        *mt = SSL3_MT_CERTIFICATE_VERIFY;
1003
0
        break;
1004
0
1005
0
1006
0
    case TLS_ST_SW_KEY_EXCH:
1007
0
        *confunc = tls_construct_server_key_exchange;
1008
0
        *mt = SSL3_MT_SERVER_KEY_EXCHANGE;
1009
0
        break;
1010
0
1011
0
    case TLS_ST_SW_CERT_REQ:
1012
0
        *confunc = tls_construct_certificate_request;
1013
0
        *mt = SSL3_MT_CERTIFICATE_REQUEST;
1014
0
        break;
1015
0
1016
0
    case TLS_ST_SW_SRVR_DONE:
1017
0
        *confunc = tls_construct_server_done;
1018
0
        *mt = SSL3_MT_SERVER_DONE;
1019
0
        break;
1020
0
1021
0
    case TLS_ST_SW_SESSION_TICKET:
1022
0
        *confunc = tls_construct_new_session_ticket;
1023
0
        *mt = SSL3_MT_NEWSESSION_TICKET;
1024
0
        break;
1025
0
1026
0
    case TLS_ST_SW_CERT_STATUS:
1027
0
        *confunc = tls_construct_cert_status;
1028
0
        *mt = SSL3_MT_CERTIFICATE_STATUS;
1029
0
        break;
1030
0
1031
0
    case TLS_ST_SW_FINISHED:
1032
0
        *confunc = tls_construct_finished;
1033
0
        *mt = SSL3_MT_FINISHED;
1034
0
        break;
1035
0
1036
0
    case TLS_ST_EARLY_DATA:
1037
0
        *confunc = NULL;
1038
0
        *mt = SSL3_MT_DUMMY;
1039
0
        break;
1040
0
1041
0
    case TLS_ST_SW_ENCRYPTED_EXTENSIONS:
1042
0
        *confunc = tls_construct_encrypted_extensions;
1043
0
        *mt = SSL3_MT_ENCRYPTED_EXTENSIONS;
1044
0
        break;
1045
0
1046
0
    case TLS_ST_SW_KEY_UPDATE:
1047
0
        *confunc = tls_construct_key_update;
1048
0
        *mt = SSL3_MT_KEY_UPDATE;
1049
0
        break;
1050
0
    }
1051
0
1052
0
    return 1;
1053
0
}
1054
1055
/*
1056
 * Maximum size (excluding the Handshake header) of a ClientHello message,
1057
 * calculated as follows:
1058
 *
1059
 *  2 + # client_version
1060
 *  32 + # only valid length for random
1061
 *  1 + # length of session_id
1062
 *  32 + # maximum size for session_id
1063
 *  2 + # length of cipher suites
1064
 *  2^16-2 + # maximum length of cipher suites array
1065
 *  1 + # length of compression_methods
1066
 *  2^8-1 + # maximum length of compression methods
1067
 *  2 + # length of extensions
1068
 *  2^16-1 # maximum length of extensions
1069
 */
1070
0
#define CLIENT_HELLO_MAX_LENGTH         131396
1071
1072
0
#define CLIENT_KEY_EXCH_MAX_LENGTH      2048
1073
0
#define NEXT_PROTO_MAX_LENGTH           514
1074
1075
/*
1076
 * Returns the maximum allowed length for the current message that we are
1077
 * reading. Excludes the message header.
1078
 */
1079
size_t ossl_statem_server_max_message_size(SSL *s)
1080
0
{
1081
0
    OSSL_STATEM *st = &s->statem;
1082
0
1083
0
    switch (st->hand_state) {
1084
0
    default:
1085
0
        /* Shouldn't happen */
1086
0
        return 0;
1087
0
1088
0
    case TLS_ST_SR_CLNT_HELLO:
1089
0
        return CLIENT_HELLO_MAX_LENGTH;
1090
0
1091
0
    case TLS_ST_SR_END_OF_EARLY_DATA:
1092
0
        return END_OF_EARLY_DATA_MAX_LENGTH;
1093
0
1094
0
    case TLS_ST_SR_CERT:
1095
0
        return s->max_cert_list;
1096
0
1097
0
    case TLS_ST_SR_KEY_EXCH:
1098
0
        return CLIENT_KEY_EXCH_MAX_LENGTH;
1099
0
1100
0
    case TLS_ST_SR_CERT_VRFY:
1101
0
        return SSL3_RT_MAX_PLAIN_LENGTH;
1102
0
1103
0
#ifndef OPENSSL_NO_NEXTPROTONEG
1104
0
    case TLS_ST_SR_NEXT_PROTO:
1105
0
        return NEXT_PROTO_MAX_LENGTH;
1106
0
#endif
1107
0
1108
0
    case TLS_ST_SR_CHANGE:
1109
0
        return CCS_MAX_LENGTH;
1110
0
1111
0
    case TLS_ST_SR_FINISHED:
1112
0
        return FINISHED_MAX_LENGTH;
1113
0
1114
0
    case TLS_ST_SR_KEY_UPDATE:
1115
0
        return KEY_UPDATE_MAX_LENGTH;
1116
0
    }
1117
0
}
1118
1119
/*
1120
 * Process a message that the server has received from the client.
1121
 */
1122
MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt)
1123
0
{
1124
0
    OSSL_STATEM *st = &s->statem;
1125
0
1126
0
    switch (st->hand_state) {
1127
0
    default:
1128
0
        /* Shouldn't happen */
1129
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1130
0
                 SSL_F_OSSL_STATEM_SERVER_PROCESS_MESSAGE,
1131
0
                 ERR_R_INTERNAL_ERROR);
1132
0
        return MSG_PROCESS_ERROR;
1133
0
1134
0
    case TLS_ST_SR_CLNT_HELLO:
1135
0
        return tls_process_client_hello(s, pkt);
1136
0
1137
0
    case TLS_ST_SR_END_OF_EARLY_DATA:
1138
0
        return tls_process_end_of_early_data(s, pkt);
1139
0
1140
0
    case TLS_ST_SR_CERT:
1141
0
        return tls_process_client_certificate(s, pkt);
1142
0
1143
0
    case TLS_ST_SR_KEY_EXCH:
1144
0
        return tls_process_client_key_exchange(s, pkt);
1145
0
1146
0
    case TLS_ST_SR_CERT_VRFY:
1147
0
        return tls_process_cert_verify(s, pkt);
1148
0
1149
0
#ifndef OPENSSL_NO_NEXTPROTONEG
1150
0
    case TLS_ST_SR_NEXT_PROTO:
1151
0
        return tls_process_next_proto(s, pkt);
1152
0
#endif
1153
0
1154
0
    case TLS_ST_SR_CHANGE:
1155
0
        return tls_process_change_cipher_spec(s, pkt);
1156
0
1157
0
    case TLS_ST_SR_FINISHED:
1158
0
        return tls_process_finished(s, pkt);
1159
0
1160
0
    case TLS_ST_SR_KEY_UPDATE:
1161
0
        return tls_process_key_update(s, pkt);
1162
0
1163
0
    }
1164
0
}
1165
1166
/*
1167
 * Perform any further processing required following the receipt of a message
1168
 * from the client
1169
 */
1170
WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst)
1171
0
{
1172
0
    OSSL_STATEM *st = &s->statem;
1173
0
1174
0
    switch (st->hand_state) {
1175
0
    default:
1176
0
        /* Shouldn't happen */
1177
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1178
0
                 SSL_F_OSSL_STATEM_SERVER_POST_PROCESS_MESSAGE,
1179
0
                 ERR_R_INTERNAL_ERROR);
1180
0
        return WORK_ERROR;
1181
0
1182
0
    case TLS_ST_SR_CLNT_HELLO:
1183
0
        return tls_post_process_client_hello(s, wst);
1184
0
1185
0
    case TLS_ST_SR_KEY_EXCH:
1186
0
        return tls_post_process_client_key_exchange(s, wst);
1187
0
    }
1188
0
}
1189
1190
#ifndef OPENSSL_NO_SRP
1191
/* Returns 1 on success, 0 for retryable error, -1 for fatal error */
1192
static int ssl_check_srp_ext_ClientHello(SSL *s)
1193
0
{
1194
0
    int ret;
1195
0
    int al = SSL_AD_UNRECOGNIZED_NAME;
1196
0
1197
0
    if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
1198
0
        (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
1199
0
        if (s->srp_ctx.login == NULL) {
1200
0
            /*
1201
0
             * RFC 5054 says SHOULD reject, we do so if There is no srp
1202
0
             * login name
1203
0
             */
1204
0
            SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
1205
0
                     SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO,
1206
0
                     SSL_R_PSK_IDENTITY_NOT_FOUND);
1207
0
            return -1;
1208
0
        } else {
1209
0
            ret = SSL_srp_server_param_with_username(s, &al);
1210
0
            if (ret < 0)
1211
0
                return 0;
1212
0
            if (ret == SSL3_AL_FATAL) {
1213
0
                SSLfatal(s, al, SSL_F_SSL_CHECK_SRP_EXT_CLIENTHELLO,
1214
0
                         al == SSL_AD_UNKNOWN_PSK_IDENTITY
1215
0
                         ? SSL_R_PSK_IDENTITY_NOT_FOUND
1216
0
                         : SSL_R_CLIENTHELLO_TLSEXT);
1217
0
                return -1;
1218
0
            }
1219
0
        }
1220
0
    }
1221
0
    return 1;
1222
0
}
1223
#endif
1224
1225
int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie,
1226
                                  size_t cookie_len)
1227
0
{
1228
0
    /* Always use DTLS 1.0 version: see RFC 6347 */
1229
0
    if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION)
1230
0
            || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len))
1231
0
        return 0;
1232
0
1233
0
    return 1;
1234
0
}
1235
1236
int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt)
1237
0
{
1238
0
    unsigned int cookie_leni;
1239
0
    if (s->ctx->app_gen_cookie_cb == NULL ||
1240
0
        s->ctx->app_gen_cookie_cb(s, s->d1->cookie,
1241
0
                                  &cookie_leni) == 0 ||
1242
0
        cookie_leni > 255) {
1243
0
        SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
1244
0
                 SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
1245
0
        return 0;
1246
0
    }
1247
0
    s->d1->cookie_len = cookie_leni;
1248
0
1249
0
    if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie,
1250
0
                                              s->d1->cookie_len)) {
1251
0
        SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS_CONSTRUCT_HELLO_VERIFY_REQUEST,
1252
0
                 ERR_R_INTERNAL_ERROR);
1253
0
        return 0;
1254
0
    }
1255
0
1256
0
    return 1;
1257
0
}
1258
1259
#ifndef OPENSSL_NO_EC
1260
/*-
1261
 * ssl_check_for_safari attempts to fingerprint Safari using OS X
1262
 * SecureTransport using the TLS extension block in |hello|.
1263
 * Safari, since 10.6, sends exactly these extensions, in this order:
1264
 *   SNI,
1265
 *   elliptic_curves
1266
 *   ec_point_formats
1267
 *   signature_algorithms (for TLSv1.2 only)
1268
 *
1269
 * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
1270
 * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
1271
 * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
1272
 * 10.8..10.8.3 (which don't work).
1273
 */
1274
static void ssl_check_for_safari(SSL *s, const CLIENTHELLO_MSG *hello)
1275
0
{
1276
0
    static const unsigned char kSafariExtensionsBlock[] = {
1277
0
        0x00, 0x0a,             /* elliptic_curves extension */
1278
0
        0x00, 0x08,             /* 8 bytes */
1279
0
        0x00, 0x06,             /* 6 bytes of curve ids */
1280
0
        0x00, 0x17,             /* P-256 */
1281
0
        0x00, 0x18,             /* P-384 */
1282
0
        0x00, 0x19,             /* P-521 */
1283
0
1284
0
        0x00, 0x0b,             /* ec_point_formats */
1285
0
        0x00, 0x02,             /* 2 bytes */
1286
0
        0x01,                   /* 1 point format */
1287
0
        0x00,                   /* uncompressed */
1288
0
        /* The following is only present in TLS 1.2 */
1289
0
        0x00, 0x0d,             /* signature_algorithms */
1290
0
        0x00, 0x0c,             /* 12 bytes */
1291
0
        0x00, 0x0a,             /* 10 bytes */
1292
0
        0x05, 0x01,             /* SHA-384/RSA */
1293
0
        0x04, 0x01,             /* SHA-256/RSA */
1294
0
        0x02, 0x01,             /* SHA-1/RSA */
1295
0
        0x04, 0x03,             /* SHA-256/ECDSA */
1296
0
        0x02, 0x03,             /* SHA-1/ECDSA */
1297
0
    };
1298
0
    /* Length of the common prefix (first two extensions). */
1299
0
    static const size_t kSafariCommonExtensionsLength = 18;
1300
0
    unsigned int type;
1301
0
    PACKET sni, tmppkt;
1302
0
    size_t ext_len;
1303
0
1304
0
    tmppkt = hello->extensions;
1305
0
1306
0
    if (!PACKET_forward(&tmppkt, 2)
1307
0
        || !PACKET_get_net_2(&tmppkt, &type)
1308
0
        || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) {
1309
0
        return;
1310
0
    }
1311
0
1312
0
    if (type != TLSEXT_TYPE_server_name)
1313
0
        return;
1314
0
1315
0
    ext_len = TLS1_get_client_version(s) >= TLS1_2_VERSION ?
1316
0
        sizeof(kSafariExtensionsBlock) : kSafariCommonExtensionsLength;
1317
0
1318
0
    s->s3->is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock,
1319
0
                                             ext_len);
1320
0
}
1321
#endif                          /* !OPENSSL_NO_EC */
1322
1323
MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt)
1324
0
{
1325
0
    /* |cookie| will only be initialized for DTLS. */
1326
0
    PACKET session_id, compression, extensions, cookie;
1327
0
    static const unsigned char null_compression = 0;
1328
0
    CLIENTHELLO_MSG *clienthello = NULL;
1329
0
1330
0
    /* Check if this is actually an unexpected renegotiation ClientHello */
1331
0
    if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) {
1332
0
        if (!ossl_assert(!SSL_IS_TLS13(s))) {
1333
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1334
0
                     ERR_R_INTERNAL_ERROR);
1335
0
            goto err;
1336
0
        }
1337
0
        if ((s->options & SSL_OP_NO_RENEGOTIATION) != 0
1338
0
                || (!s->s3->send_connection_binding
1339
0
                    && (s->options
1340
0
                        & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) == 0)) {
1341
0
            ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
1342
0
            return MSG_PROCESS_FINISHED_READING;
1343
0
        }
1344
0
        s->renegotiate = 1;
1345
0
        s->new_session = 1;
1346
0
    }
1347
0
1348
0
    clienthello = OPENSSL_zalloc(sizeof(*clienthello));
1349
0
    if (clienthello == NULL) {
1350
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1351
0
                 ERR_R_INTERNAL_ERROR);
1352
0
        goto err;
1353
0
    }
1354
0
1355
0
    /*
1356
0
     * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure.
1357
0
     */
1358
0
    clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer);
1359
0
    PACKET_null_init(&cookie);
1360
0
1361
0
    if (clienthello->isv2) {
1362
0
        unsigned int mt;
1363
0
1364
0
        if (!SSL_IS_FIRST_HANDSHAKE(s)
1365
0
                || s->hello_retry_request != SSL_HRR_NONE) {
1366
0
            SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1367
0
                     SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_UNEXPECTED_MESSAGE);
1368
0
            goto err;
1369
0
        }
1370
0
1371
0
        /*-
1372
0
         * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
1373
0
         * header is sent directly on the wire, not wrapped as a TLS
1374
0
         * record. Our record layer just processes the message length and passes
1375
0
         * the rest right through. Its format is:
1376
0
         * Byte  Content
1377
0
         * 0-1   msg_length - decoded by the record layer
1378
0
         * 2     msg_type - s->init_msg points here
1379
0
         * 3-4   version
1380
0
         * 5-6   cipher_spec_length
1381
0
         * 7-8   session_id_length
1382
0
         * 9-10  challenge_length
1383
0
         * ...   ...
1384
0
         */
1385
0
1386
0
        if (!PACKET_get_1(pkt, &mt)
1387
0
            || mt != SSL2_MT_CLIENT_HELLO) {
1388
0
            /*
1389
0
             * Should never happen. We should have tested this in the record
1390
0
             * layer in order to have determined that this is a SSLv2 record
1391
0
             * in the first place
1392
0
             */
1393
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1394
0
                     ERR_R_INTERNAL_ERROR);
1395
0
            goto err;
1396
0
        }
1397
0
    }
1398
0
1399
0
    if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) {
1400
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1401
0
                 SSL_R_LENGTH_TOO_SHORT);
1402
0
        goto err;
1403
0
    }
1404
0
1405
0
    /* Parse the message and load client random. */
1406
0
    if (clienthello->isv2) {
1407
0
        /*
1408
0
         * Handle an SSLv2 backwards compatible ClientHello
1409
0
         * Note, this is only for SSLv3+ using the backward compatible format.
1410
0
         * Real SSLv2 is not supported, and is rejected below.
1411
0
         */
1412
0
        unsigned int ciphersuite_len, session_id_len, challenge_len;
1413
0
        PACKET challenge;
1414
0
1415
0
        if (!PACKET_get_net_2(pkt, &ciphersuite_len)
1416
0
            || !PACKET_get_net_2(pkt, &session_id_len)
1417
0
            || !PACKET_get_net_2(pkt, &challenge_len)) {
1418
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1419
0
                     SSL_R_RECORD_LENGTH_MISMATCH);
1420
0
            goto err;
1421
0
        }
1422
0
1423
0
        if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
1424
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1425
0
                     SSL_F_TLS_PROCESS_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1426
0
            goto err;
1427
0
        }
1428
0
1429
0
        if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites,
1430
0
                                   ciphersuite_len)
1431
0
            || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len)
1432
0
            || !PACKET_get_sub_packet(pkt, &challenge, challenge_len)
1433
0
            /* No extensions. */
1434
0
            || PACKET_remaining(pkt) != 0) {
1435
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1436
0
                     SSL_R_RECORD_LENGTH_MISMATCH);
1437
0
            goto err;
1438
0
        }
1439
0
        clienthello->session_id_len = session_id_len;
1440
0
1441
0
        /* Load the client random and compression list. We use SSL3_RANDOM_SIZE
1442
0
         * here rather than sizeof(clienthello->random) because that is the limit
1443
0
         * for SSLv3 and it is fixed. It won't change even if
1444
0
         * sizeof(clienthello->random) does.
1445
0
         */
1446
0
        challenge_len = challenge_len > SSL3_RANDOM_SIZE
1447
0
                        ? SSL3_RANDOM_SIZE : challenge_len;
1448
0
        memset(clienthello->random, 0, SSL3_RANDOM_SIZE);
1449
0
        if (!PACKET_copy_bytes(&challenge,
1450
0
                               clienthello->random + SSL3_RANDOM_SIZE -
1451
0
                               challenge_len, challenge_len)
1452
0
            /* Advertise only null compression. */
1453
0
            || !PACKET_buf_init(&compression, &null_compression, 1)) {
1454
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1455
0
                     ERR_R_INTERNAL_ERROR);
1456
0
            goto err;
1457
0
        }
1458
0
1459
0
        PACKET_null_init(&clienthello->extensions);
1460
0
    } else {
1461
0
        /* Regular ClientHello. */
1462
0
        if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE)
1463
0
            || !PACKET_get_length_prefixed_1(pkt, &session_id)
1464
0
            || !PACKET_copy_all(&session_id, clienthello->session_id,
1465
0
                    SSL_MAX_SSL_SESSION_ID_LENGTH,
1466
0
                    &clienthello->session_id_len)) {
1467
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1468
0
                     SSL_R_LENGTH_MISMATCH);
1469
0
            goto err;
1470
0
        }
1471
0
1472
0
        if (SSL_IS_DTLS(s)) {
1473
0
            if (!PACKET_get_length_prefixed_1(pkt, &cookie)) {
1474
0
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1475
0
                         SSL_R_LENGTH_MISMATCH);
1476
0
                goto err;
1477
0
            }
1478
0
            if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie,
1479
0
                                 DTLS1_COOKIE_LENGTH,
1480
0
                                 &clienthello->dtls_cookie_len)) {
1481
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1482
0
                         SSL_F_TLS_PROCESS_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1483
0
                goto err;
1484
0
            }
1485
0
            /*
1486
0
             * If we require cookies and this ClientHello doesn't contain one,
1487
0
             * just return since we do not want to allocate any memory yet.
1488
0
             * So check cookie length...
1489
0
             */
1490
0
            if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1491
0
                if (clienthello->dtls_cookie_len == 0)
1492
0
                    return MSG_PROCESS_FINISHED_READING;
1493
0
            }
1494
0
        }
1495
0
1496
0
        if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) {
1497
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1498
0
                     SSL_R_LENGTH_MISMATCH);
1499
0
            goto err;
1500
0
        }
1501
0
1502
0
        if (!PACKET_get_length_prefixed_1(pkt, &compression)) {
1503
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1504
0
                     SSL_R_LENGTH_MISMATCH);
1505
0
            goto err;
1506
0
        }
1507
0
1508
0
        /* Could be empty. */
1509
0
        if (PACKET_remaining(pkt) == 0) {
1510
0
            PACKET_null_init(&clienthello->extensions);
1511
0
        } else {
1512
0
            if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions)
1513
0
                    || PACKET_remaining(pkt) != 0) {
1514
0
                SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1515
0
                         SSL_R_LENGTH_MISMATCH);
1516
0
                goto err;
1517
0
            }
1518
0
        }
1519
0
    }
1520
0
1521
0
    if (!PACKET_copy_all(&compression, clienthello->compressions,
1522
0
                         MAX_COMPRESSIONS_SIZE,
1523
0
                         &clienthello->compressions_len)) {
1524
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_HELLO,
1525
0
                 ERR_R_INTERNAL_ERROR);
1526
0
        goto err;
1527
0
    }
1528
0
1529
0
    /* Preserve the raw extensions PACKET for later use */
1530
0
    extensions = clienthello->extensions;
1531
0
    if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO,
1532
0
                                &clienthello->pre_proc_exts,
1533
0
                                &clienthello->pre_proc_exts_len, 1)) {
1534
0
        /* SSLfatal already been called */
1535
0
        goto err;
1536
0
    }
1537
0
    s->clienthello = clienthello;
1538
0
1539
0
    return MSG_PROCESS_CONTINUE_PROCESSING;
1540
0
1541
0
 err:
1542
0
    if (clienthello != NULL)
1543
0
        OPENSSL_free(clienthello->pre_proc_exts);
1544
0
    OPENSSL_free(clienthello);
1545
0
1546
0
    return MSG_PROCESS_ERROR;
1547
0
}
1548
1549
static int tls_early_post_process_client_hello(SSL *s)
1550
0
{
1551
0
    unsigned int j;
1552
0
    int i, al = SSL_AD_INTERNAL_ERROR;
1553
0
    int protverr;
1554
0
    size_t loop;
1555
0
    unsigned long id;
1556
0
#ifndef OPENSSL_NO_COMP
1557
0
    SSL_COMP *comp = NULL;
1558
0
#endif
1559
0
    const SSL_CIPHER *c;
1560
0
    STACK_OF(SSL_CIPHER) *ciphers = NULL;
1561
0
    STACK_OF(SSL_CIPHER) *scsvs = NULL;
1562
0
    CLIENTHELLO_MSG *clienthello = s->clienthello;
1563
0
    DOWNGRADE dgrd = DOWNGRADE_NONE;
1564
0
1565
0
    /* Finished parsing the ClientHello, now we can start processing it */
1566
0
    /* Give the ClientHello callback a crack at things */
1567
0
    if (s->ctx->client_hello_cb != NULL) {
1568
0
        /* A failure in the ClientHello callback terminates the connection. */
1569
0
        switch (s->ctx->client_hello_cb(s, &al, s->ctx->client_hello_cb_arg)) {
1570
0
        case SSL_CLIENT_HELLO_SUCCESS:
1571
0
            break;
1572
0
        case SSL_CLIENT_HELLO_RETRY:
1573
0
            s->rwstate = SSL_CLIENT_HELLO_CB;
1574
0
            return -1;
1575
0
        case SSL_CLIENT_HELLO_ERROR:
1576
0
        default:
1577
0
            SSLfatal(s, al,
1578
0
                     SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1579
0
                     SSL_R_CALLBACK_FAILED);
1580
0
            goto err;
1581
0
        }
1582
0
    }
1583
0
1584
0
    /* Set up the client_random */
1585
0
    memcpy(s->s3->client_random, clienthello->random, SSL3_RANDOM_SIZE);
1586
0
1587
0
    /* Choose the version */
1588
0
1589
0
    if (clienthello->isv2) {
1590
0
        if (clienthello->legacy_version == SSL2_VERSION
1591
0
                || (clienthello->legacy_version & 0xff00)
1592
0
                   != (SSL3_VERSION_MAJOR << 8)) {
1593
0
            /*
1594
0
             * This is real SSLv2 or something completely unknown. We don't
1595
0
             * support it.
1596
0
             */
1597
0
            SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1598
0
                     SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1599
0
                     SSL_R_UNKNOWN_PROTOCOL);
1600
0
            goto err;
1601
0
        }
1602
0
        /* SSLv3/TLS */
1603
0
        s->client_version = clienthello->legacy_version;
1604
0
    }
1605
0
    /*
1606
0
     * Do SSL/TLS version negotiation if applicable. For DTLS we just check
1607
0
     * versions are potentially compatible. Version negotiation comes later.
1608
0
     */
1609
0
    if (!SSL_IS_DTLS(s)) {
1610
0
        protverr = ssl_choose_server_version(s, clienthello, &dgrd);
1611
0
    } else if (s->method->version != DTLS_ANY_VERSION &&
1612
0
               DTLS_VERSION_LT((int)clienthello->legacy_version, s->version)) {
1613
0
        protverr = SSL_R_VERSION_TOO_LOW;
1614
0
    } else {
1615
0
        protverr = 0;
1616
0
    }
1617
0
1618
0
    if (protverr) {
1619
0
        if (SSL_IS_FIRST_HANDSHAKE(s)) {
1620
0
            /* like ssl3_get_record, send alert using remote version number */
1621
0
            s->version = s->client_version = clienthello->legacy_version;
1622
0
        }
1623
0
        SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1624
0
                 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
1625
0
        goto err;
1626
0
    }
1627
0
1628
0
    /* TLSv1.3 specifies that a ClientHello must end on a record boundary */
1629
0
    if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
1630
0
        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1631
0
                 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1632
0
                 SSL_R_NOT_ON_RECORD_BOUNDARY);
1633
0
        goto err;
1634
0
    }
1635
0
1636
0
    if (SSL_IS_DTLS(s)) {
1637
0
        /* Empty cookie was already handled above by returning early. */
1638
0
        if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1639
0
            if (s->ctx->app_verify_cookie_cb != NULL) {
1640
0
                if (s->ctx->app_verify_cookie_cb(s, clienthello->dtls_cookie,
1641
0
                        clienthello->dtls_cookie_len) == 0) {
1642
0
                    SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1643
0
                             SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1644
0
                             SSL_R_COOKIE_MISMATCH);
1645
0
                    goto err;
1646
0
                    /* else cookie verification succeeded */
1647
0
                }
1648
0
                /* default verification */
1649
0
            } else if (s->d1->cookie_len != clienthello->dtls_cookie_len
1650
0
                    || memcmp(clienthello->dtls_cookie, s->d1->cookie,
1651
0
                              s->d1->cookie_len) != 0) {
1652
0
                SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1653
0
                         SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1654
0
                         SSL_R_COOKIE_MISMATCH);
1655
0
                goto err;
1656
0
            }
1657
0
            s->d1->cookie_verified = 1;
1658
0
        }
1659
0
        if (s->method->version == DTLS_ANY_VERSION) {
1660
0
            protverr = ssl_choose_server_version(s, clienthello, &dgrd);
1661
0
            if (protverr != 0) {
1662
0
                s->version = s->client_version;
1663
0
                SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1664
0
                         SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, protverr);
1665
0
                goto err;
1666
0
            }
1667
0
        }
1668
0
    }
1669
0
1670
0
    s->hit = 0;
1671
0
1672
0
    if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites,
1673
0
                              clienthello->isv2) ||
1674
0
        !bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, &scsvs,
1675
0
                              clienthello->isv2, 1)) {
1676
0
        /* SSLfatal() already called */
1677
0
        goto err;
1678
0
    }
1679
0
1680
0
    s->s3->send_connection_binding = 0;
1681
0
    /* Check what signalling cipher-suite values were received. */
1682
0
    if (scsvs != NULL) {
1683
0
        for(i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) {
1684
0
            c = sk_SSL_CIPHER_value(scsvs, i);
1685
0
            if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) {
1686
0
                if (s->renegotiate) {
1687
0
                    /* SCSV is fatal if renegotiating */
1688
0
                    SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1689
0
                             SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1690
0
                             SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1691
0
                    goto err;
1692
0
                }
1693
0
                s->s3->send_connection_binding = 1;
1694
0
            } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV &&
1695
0
                       !ssl_check_version_downgrade(s)) {
1696
0
                /*
1697
0
                 * This SCSV indicates that the client previously tried
1698
0
                 * a higher version.  We should fail if the current version
1699
0
                 * is an unexpected downgrade, as that indicates that the first
1700
0
                 * connection may have been tampered with in order to trigger
1701
0
                 * an insecure downgrade.
1702
0
                 */
1703
0
                SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK,
1704
0
                         SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1705
0
                         SSL_R_INAPPROPRIATE_FALLBACK);
1706
0
                goto err;
1707
0
            }
1708
0
        }
1709
0
    }
1710
0
1711
0
    /* For TLSv1.3 we must select the ciphersuite *before* session resumption */
1712
0
    if (SSL_IS_TLS13(s)) {
1713
0
        const SSL_CIPHER *cipher =
1714
0
            ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(s));
1715
0
1716
0
        if (cipher == NULL) {
1717
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1718
0
                     SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1719
0
                     SSL_R_NO_SHARED_CIPHER);
1720
0
            goto err;
1721
0
        }
1722
0
        if (s->hello_retry_request == SSL_HRR_PENDING
1723
0
                && (s->s3->tmp.new_cipher == NULL
1724
0
                    || s->s3->tmp.new_cipher->id != cipher->id)) {
1725
0
            /*
1726
0
             * A previous HRR picked a different ciphersuite to the one we
1727
0
             * just selected. Something must have changed.
1728
0
             */
1729
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1730
0
                     SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1731
0
                     SSL_R_BAD_CIPHER);
1732
0
            goto err;
1733
0
        }
1734
0
        s->s3->tmp.new_cipher = cipher;
1735
0
    }
1736
0
1737
0
    /* We need to do this before getting the session */
1738
0
    if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret,
1739
0
                             SSL_EXT_CLIENT_HELLO,
1740
0
                             clienthello->pre_proc_exts, NULL, 0)) {
1741
0
        /* SSLfatal() already called */
1742
0
        goto err;
1743
0
    }
1744
0
1745
0
    /*
1746
0
     * We don't allow resumption in a backwards compatible ClientHello.
1747
0
     * TODO(openssl-team): in TLS1.1+, session_id MUST be empty.
1748
0
     *
1749
0
     * Versions before 0.9.7 always allow clients to resume sessions in
1750
0
     * renegotiation. 0.9.7 and later allow this by default, but optionally
1751
0
     * ignore resumption requests with flag
1752
0
     * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1753
0
     * than a change to default behavior so that applications relying on
1754
0
     * this for security won't even compile against older library versions).
1755
0
     * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1756
0
     * request renegotiation but not a new session (s->new_session remains
1757
0
     * unset): for servers, this essentially just means that the
1758
0
     * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1759
0
     * ignored.
1760
0
     */
1761
0
    if (clienthello->isv2 ||
1762
0
        (s->new_session &&
1763
0
         (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1764
0
        if (!ssl_get_new_session(s, 1)) {
1765
0
            /* SSLfatal() already called */
1766
0
            goto err;
1767
0
        }
1768
0
    } else {
1769
0
        i = ssl_get_prev_session(s, clienthello);
1770
0
        if (i == 1) {
1771
0
            /* previous session */
1772
0
            s->hit = 1;
1773
0
        } else if (i == -1) {
1774
0
            /* SSLfatal() already called */
1775
0
            goto err;
1776
0
        } else {
1777
0
            /* i == 0 */
1778
0
            if (!ssl_get_new_session(s, 1)) {
1779
0
                /* SSLfatal() already called */
1780
0
                goto err;
1781
0
            }
1782
0
        }
1783
0
    }
1784
0
1785
0
    if (SSL_IS_TLS13(s)) {
1786
0
        memcpy(s->tmp_session_id, s->clienthello->session_id,
1787
0
               s->clienthello->session_id_len);
1788
0
        s->tmp_session_id_len = s->clienthello->session_id_len;
1789
0
    }
1790
0
1791
0
    /*
1792
0
     * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check
1793
0
     * ciphersuite compatibility with the session as part of resumption.
1794
0
     */
1795
0
    if (!SSL_IS_TLS13(s) && s->hit) {
1796
0
        j = 0;
1797
0
        id = s->session->cipher->id;
1798
0
1799
#ifdef CIPHER_DEBUG
1800
        fprintf(stderr, "client sent %d ciphers\n", sk_SSL_CIPHER_num(ciphers));
1801
#endif
1802
0
        for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1803
0
            c = sk_SSL_CIPHER_value(ciphers, i);
1804
#ifdef CIPHER_DEBUG
1805
            fprintf(stderr, "client [%2d of %2d]:%s\n",
1806
                    i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1807
#endif
1808
0
            if (c->id == id) {
1809
0
                j = 1;
1810
0
                break;
1811
0
            }
1812
0
        }
1813
0
        if (j == 0) {
1814
0
            /*
1815
0
             * we need to have the cipher in the cipher list if we are asked
1816
0
             * to reuse it
1817
0
             */
1818
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1819
0
                     SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1820
0
                     SSL_R_REQUIRED_CIPHER_MISSING);
1821
0
            goto err;
1822
0
        }
1823
0
    }
1824
0
1825
0
    for (loop = 0; loop < clienthello->compressions_len; loop++) {
1826
0
        if (clienthello->compressions[loop] == 0)
1827
0
            break;
1828
0
    }
1829
0
1830
0
    if (loop >= clienthello->compressions_len) {
1831
0
        /* no compress */
1832
0
        SSLfatal(s, SSL_AD_DECODE_ERROR,
1833
0
                 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1834
0
                 SSL_R_NO_COMPRESSION_SPECIFIED);
1835
0
        goto err;
1836
0
    }
1837
0
1838
0
#ifndef OPENSSL_NO_EC
1839
0
    if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1840
0
        ssl_check_for_safari(s, clienthello);
1841
0
#endif                          /* !OPENSSL_NO_EC */
1842
0
1843
0
    /* TLS extensions */
1844
0
    if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO,
1845
0
                                  clienthello->pre_proc_exts, NULL, 0, 1)) {
1846
0
        /* SSLfatal() already called */
1847
0
        goto err;
1848
0
    }
1849
0
1850
0
    /*
1851
0
     * Check if we want to use external pre-shared secret for this handshake
1852
0
     * for not reused session only. We need to generate server_random before
1853
0
     * calling tls_session_secret_cb in order to allow SessionTicket
1854
0
     * processing to use it in key derivation.
1855
0
     */
1856
0
    {
1857
0
        unsigned char *pos;
1858
0
        pos = s->s3->server_random;
1859
0
        if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) {
1860
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1861
0
                     SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1862
0
                     ERR_R_INTERNAL_ERROR);
1863
0
            goto err;
1864
0
        }
1865
0
    }
1866
0
1867
0
    if (!s->hit
1868
0
            && s->version >= TLS1_VERSION
1869
0
            && !SSL_IS_TLS13(s)
1870
0
            && !SSL_IS_DTLS(s)
1871
0
            && s->ext.session_secret_cb) {
1872
0
        const SSL_CIPHER *pref_cipher = NULL;
1873
0
        /*
1874
0
         * s->session->master_key_length is a size_t, but this is an int for
1875
0
         * backwards compat reasons
1876
0
         */
1877
0
        int master_key_length;
1878
0
1879
0
        master_key_length = sizeof(s->session->master_key);
1880
0
        if (s->ext.session_secret_cb(s, s->session->master_key,
1881
0
                                     &master_key_length, ciphers,
1882
0
                                     &pref_cipher,
1883
0
                                     s->ext.session_secret_cb_arg)
1884
0
                && master_key_length > 0) {
1885
0
            s->session->master_key_length = master_key_length;
1886
0
            s->hit = 1;
1887
0
            s->session->ciphers = ciphers;
1888
0
            s->session->verify_result = X509_V_OK;
1889
0
1890
0
            ciphers = NULL;
1891
0
1892
0
            /* check if some cipher was preferred by call back */
1893
0
            if (pref_cipher == NULL)
1894
0
                pref_cipher = ssl3_choose_cipher(s, s->session->ciphers,
1895
0
                                                 SSL_get_ciphers(s));
1896
0
            if (pref_cipher == NULL) {
1897
0
                SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1898
0
                         SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1899
0
                         SSL_R_NO_SHARED_CIPHER);
1900
0
                goto err;
1901
0
            }
1902
0
1903
0
            s->session->cipher = pref_cipher;
1904
0
            sk_SSL_CIPHER_free(s->cipher_list);
1905
0
            s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1906
0
            sk_SSL_CIPHER_free(s->cipher_list_by_id);
1907
0
            s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1908
0
        }
1909
0
    }
1910
0
1911
0
    /*
1912
0
     * Worst case, we will use the NULL compression, but if we have other
1913
0
     * options, we will now look for them.  We have complen-1 compression
1914
0
     * algorithms from the client, starting at q.
1915
0
     */
1916
0
    s->s3->tmp.new_compression = NULL;
1917
0
    if (SSL_IS_TLS13(s)) {
1918
0
        /*
1919
0
         * We already checked above that the NULL compression method appears in
1920
0
         * the list. Now we check there aren't any others (which is illegal in
1921
0
         * a TLSv1.3 ClientHello.
1922
0
         */
1923
0
        if (clienthello->compressions_len != 1) {
1924
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1925
0
                     SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1926
0
                     SSL_R_INVALID_COMPRESSION_ALGORITHM);
1927
0
            goto err;
1928
0
        }
1929
0
    }
1930
0
#ifndef OPENSSL_NO_COMP
1931
0
    /* This only happens if we have a cache hit */
1932
0
    else if (s->session->compress_meth != 0) {
1933
0
        int m, comp_id = s->session->compress_meth;
1934
0
        unsigned int k;
1935
0
        /* Perform sanity checks on resumed compression algorithm */
1936
0
        /* Can't disable compression */
1937
0
        if (!ssl_allow_compression(s)) {
1938
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1939
0
                     SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1940
0
                     SSL_R_INCONSISTENT_COMPRESSION);
1941
0
            goto err;
1942
0
        }
1943
0
        /* Look for resumed compression method */
1944
0
        for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1945
0
            comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1946
0
            if (comp_id == comp->id) {
1947
0
                s->s3->tmp.new_compression = comp;
1948
0
                break;
1949
0
            }
1950
0
        }
1951
0
        if (s->s3->tmp.new_compression == NULL) {
1952
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
1953
0
                     SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1954
0
                     SSL_R_INVALID_COMPRESSION_ALGORITHM);
1955
0
            goto err;
1956
0
        }
1957
0
        /* Look for resumed method in compression list */
1958
0
        for (k = 0; k < clienthello->compressions_len; k++) {
1959
0
            if (clienthello->compressions[k] == comp_id)
1960
0
                break;
1961
0
        }
1962
0
        if (k >= clienthello->compressions_len) {
1963
0
            SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1964
0
                     SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
1965
0
                     SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING);
1966
0
            goto err;
1967
0
        }
1968
0
    } else if (s->hit) {
1969
0
        comp = NULL;
1970
0
    } else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1971
0
        /* See if we have a match */
1972
0
        int m, nn, v, done = 0;
1973
0
        unsigned int o;
1974
0
1975
0
        nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1976
0
        for (m = 0; m < nn; m++) {
1977
0
            comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1978
0
            v = comp->id;
1979
0
            for (o = 0; o < clienthello->compressions_len; o++) {
1980
0
                if (v == clienthello->compressions[o]) {
1981
0
                    done = 1;
1982
0
                    break;
1983
0
                }
1984
0
            }
1985
0
            if (done)
1986
0
                break;
1987
0
        }
1988
0
        if (done)
1989
0
            s->s3->tmp.new_compression = comp;
1990
0
        else
1991
0
            comp = NULL;
1992
0
    }
1993
#else
1994
    /*
1995
     * If compression is disabled we'd better not try to resume a session
1996
     * using compression.
1997
     */
1998
    if (s->session->compress_meth != 0) {
1999
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2000
                 SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
2001
                 SSL_R_INCONSISTENT_COMPRESSION);
2002
        goto err;
2003
    }
2004
#endif
2005
2006
0
    /*
2007
0
     * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
2008
0
     */
2009
0
2010
0
    if (!s->hit || SSL_IS_TLS13(s)) {
2011
0
        sk_SSL_CIPHER_free(s->session->ciphers);
2012
0
        s->session->ciphers = ciphers;
2013
0
        if (ciphers == NULL) {
2014
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2015
0
                     SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO,
2016
0
                     ERR_R_INTERNAL_ERROR);
2017
0
            goto err;
2018
0
        }
2019
0
        ciphers = NULL;
2020
0
    }
2021
0
2022
0
    if (!s->hit) {
2023
#ifdef OPENSSL_NO_COMP
2024
        s->session->compress_meth = 0;
2025
#else
2026
0
        s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
2027
0
#endif
2028
0
        if (!tls1_set_server_sigalgs(s)) {
2029
0
            /* SSLfatal() already called */
2030
0
            goto err;
2031
0
        }
2032
0
    }
2033
0
2034
0
    sk_SSL_CIPHER_free(ciphers);
2035
0
    sk_SSL_CIPHER_free(scsvs);
2036
0
    OPENSSL_free(clienthello->pre_proc_exts);
2037
0
    OPENSSL_free(s->clienthello);
2038
0
    s->clienthello = NULL;
2039
0
    return 1;
2040
0
 err:
2041
0
    sk_SSL_CIPHER_free(ciphers);
2042
0
    sk_SSL_CIPHER_free(scsvs);
2043
0
    OPENSSL_free(clienthello->pre_proc_exts);
2044
0
    OPENSSL_free(s->clienthello);
2045
0
    s->clienthello = NULL;
2046
0
2047
0
    return 0;
2048
0
}
2049
2050
/*
2051
 * Call the status request callback if needed. Upon success, returns 1.
2052
 * Upon failure, returns 0.
2053
 */
2054
static int tls_handle_status_request(SSL *s)
2055
0
{
2056
0
    s->ext.status_expected = 0;
2057
0
2058
0
    /*
2059
0
     * If status request then ask callback what to do. Note: this must be
2060
0
     * called after servername callbacks in case the certificate has changed,
2061
0
     * and must be called after the cipher has been chosen because this may
2062
0
     * influence which certificate is sent
2063
0
     */
2064
0
    if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && s->ctx != NULL
2065
0
            && s->ctx->ext.status_cb != NULL) {
2066
0
        int ret;
2067
0
2068
0
        /* If no certificate can't return certificate status */
2069
0
        if (s->s3->tmp.cert != NULL) {
2070
0
            /*
2071
0
             * Set current certificate to one we will use so SSL_get_certificate
2072
0
             * et al can pick it up.
2073
0
             */
2074
0
            s->cert->key = s->s3->tmp.cert;
2075
0
            ret = s->ctx->ext.status_cb(s, s->ctx->ext.status_arg);
2076
0
            switch (ret) {
2077
0
                /* We don't want to send a status request response */
2078
0
            case SSL_TLSEXT_ERR_NOACK:
2079
0
                s->ext.status_expected = 0;
2080
0
                break;
2081
0
                /* status request response should be sent */
2082
0
            case SSL_TLSEXT_ERR_OK:
2083
0
                if (s->ext.ocsp.resp)
2084
0
                    s->ext.status_expected = 1;
2085
0
                break;
2086
0
                /* something bad happened */
2087
0
            case SSL_TLSEXT_ERR_ALERT_FATAL:
2088
0
            default:
2089
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2090
0
                         SSL_F_TLS_HANDLE_STATUS_REQUEST,
2091
0
                         SSL_R_CLIENTHELLO_TLSEXT);
2092
0
                return 0;
2093
0
            }
2094
0
        }
2095
0
    }
2096
0
2097
0
    return 1;
2098
0
}
2099
2100
/*
2101
 * Call the alpn_select callback if needed. Upon success, returns 1.
2102
 * Upon failure, returns 0.
2103
 */
2104
int tls_handle_alpn(SSL *s)
2105
0
{
2106
0
    const unsigned char *selected = NULL;
2107
0
    unsigned char selected_len = 0;
2108
0
2109
0
    if (s->ctx->ext.alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
2110
0
        int r = s->ctx->ext.alpn_select_cb(s, &selected, &selected_len,
2111
0
                                           s->s3->alpn_proposed,
2112
0
                                           (unsigned int)s->s3->alpn_proposed_len,
2113
0
                                           s->ctx->ext.alpn_select_cb_arg);
2114
0
2115
0
        if (r == SSL_TLSEXT_ERR_OK) {
2116
0
            OPENSSL_free(s->s3->alpn_selected);
2117
0
            s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
2118
0
            if (s->s3->alpn_selected == NULL) {
2119
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_HANDLE_ALPN,
2120
0
                         ERR_R_INTERNAL_ERROR);
2121
0
                return 0;
2122
0
            }
2123
0
            s->s3->alpn_selected_len = selected_len;
2124
0
#ifndef OPENSSL_NO_NEXTPROTONEG
2125
0
            /* ALPN takes precedence over NPN. */
2126
0
            s->s3->npn_seen = 0;
2127
0
#endif
2128
0
2129
0
            /* Check ALPN is consistent with session */
2130
0
            if (s->session->ext.alpn_selected == NULL
2131
0
                        || selected_len != s->session->ext.alpn_selected_len
2132
0
                        || memcmp(selected, s->session->ext.alpn_selected,
2133
0
                                  selected_len) != 0) {
2134
0
                /* Not consistent so can't be used for early_data */
2135
0
                s->ext.early_data_ok = 0;
2136
0
2137
0
                if (!s->hit) {
2138
0
                    /*
2139
0
                     * This is a new session and so alpn_selected should have
2140
0
                     * been initialised to NULL. We should update it with the
2141
0
                     * selected ALPN.
2142
0
                     */
2143
0
                    if (!ossl_assert(s->session->ext.alpn_selected == NULL)) {
2144
0
                        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2145
0
                                 SSL_F_TLS_HANDLE_ALPN,
2146
0
                                 ERR_R_INTERNAL_ERROR);
2147
0
                        return 0;
2148
0
                    }
2149
0
                    s->session->ext.alpn_selected = OPENSSL_memdup(selected,
2150
0
                                                                   selected_len);
2151
0
                    if (s->session->ext.alpn_selected == NULL) {
2152
0
                        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2153
0
                                 SSL_F_TLS_HANDLE_ALPN,
2154
0
                                 ERR_R_INTERNAL_ERROR);
2155
0
                        return 0;
2156
0
                    }
2157
0
                    s->session->ext.alpn_selected_len = selected_len;
2158
0
                }
2159
0
            }
2160
0
2161
0
            return 1;
2162
0
        } else if (r != SSL_TLSEXT_ERR_NOACK) {
2163
0
            SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL, SSL_F_TLS_HANDLE_ALPN,
2164
0
                     SSL_R_NO_APPLICATION_PROTOCOL);
2165
0
            return 0;
2166
0
        }
2167
0
        /*
2168
0
         * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was
2169
0
         * present.
2170
0
         */
2171
0
    }
2172
0
2173
0
    /* Check ALPN is consistent with session */
2174
0
    if (s->session->ext.alpn_selected != NULL) {
2175
0
        /* Not consistent so can't be used for early_data */
2176
0
        s->ext.early_data_ok = 0;
2177
0
    }
2178
0
2179
0
    return 1;
2180
0
}
2181
2182
WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst)
2183
0
{
2184
0
    const SSL_CIPHER *cipher;
2185
0
2186
0
    if (wst == WORK_MORE_A) {
2187
0
        int rv = tls_early_post_process_client_hello(s);
2188
0
        if (rv == 0) {
2189
0
            /* SSLfatal() was already called */
2190
0
            goto err;
2191
0
        }
2192
0
        if (rv < 0)
2193
0
            return WORK_MORE_A;
2194
0
        wst = WORK_MORE_B;
2195
0
    }
2196
0
    if (wst == WORK_MORE_B) {
2197
0
        if (!s->hit || SSL_IS_TLS13(s)) {
2198
0
            /* Let cert callback update server certificates if required */
2199
0
            if (!s->hit && s->cert->cert_cb != NULL) {
2200
0
                int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
2201
0
                if (rv == 0) {
2202
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2203
0
                             SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
2204
0
                             SSL_R_CERT_CB_ERROR);
2205
0
                    goto err;
2206
0
                }
2207
0
                if (rv < 0) {
2208
0
                    s->rwstate = SSL_X509_LOOKUP;
2209
0
                    return WORK_MORE_B;
2210
0
                }
2211
0
                s->rwstate = SSL_NOTHING;
2212
0
            }
2213
0
2214
0
            /* In TLSv1.3 we selected the ciphersuite before resumption */
2215
0
            if (!SSL_IS_TLS13(s)) {
2216
0
                cipher =
2217
0
                    ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
2218
0
2219
0
                if (cipher == NULL) {
2220
0
                    SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2221
0
                             SSL_F_TLS_POST_PROCESS_CLIENT_HELLO,
2222
0
                             SSL_R_NO_SHARED_CIPHER);
2223
0
                    goto err;
2224
0
                }
2225
0
                s->s3->tmp.new_cipher = cipher;
2226
0
            }
2227
0
            if (!s->hit) {
2228
0
                if (!tls_choose_sigalg(s, 1)) {
2229
0
                    /* SSLfatal already called */
2230
0
                    goto err;
2231
0
                }
2232
0
                /* check whether we should disable session resumption */
2233
0
                if (s->not_resumable_session_cb != NULL)
2234
0
                    s->session->not_resumable =
2235
0
                        s->not_resumable_session_cb(s,
2236
0
                            ((s->s3->tmp.new_cipher->algorithm_mkey
2237
0
                              & (SSL_kDHE | SSL_kECDHE)) != 0));
2238
0
                if (s->session->not_resumable)
2239
0
                    /* do not send a session ticket */
2240
0
                    s->ext.ticket_expected = 0;
2241
0
            }
2242
0
        } else {
2243
0
            /* Session-id reuse */
2244
0
            s->s3->tmp.new_cipher = s->session->cipher;
2245
0
        }
2246
0
2247
0
        /*-
2248
0
         * we now have the following setup.
2249
0
         * client_random
2250
0
         * cipher_list          - our preferred list of ciphers
2251
0
         * ciphers              - the clients preferred list of ciphers
2252
0
         * compression          - basically ignored right now
2253
0
         * ssl version is set   - sslv3
2254
0
         * s->session           - The ssl session has been setup.
2255
0
         * s->hit               - session reuse flag
2256
0
         * s->s3->tmp.new_cipher- the new cipher to use.
2257
0
         */
2258
0
2259
0
        /*
2260
0
         * Call status_request callback if needed. Has to be done after the
2261
0
         * certificate callbacks etc above.
2262
0
         */
2263
0
        if (!tls_handle_status_request(s)) {
2264
0
            /* SSLfatal() already called */
2265
0
            goto err;
2266
0
        }
2267
0
        /*
2268
0
         * Call alpn_select callback if needed.  Has to be done after SNI and
2269
0
         * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
2270
0
         * we already did this because cipher negotiation happens earlier, and
2271
0
         * we must handle ALPN before we decide whether to accept early_data.
2272
0
         */
2273
0
        if (!SSL_IS_TLS13(s) && !tls_handle_alpn(s)) {
2274
0
            /* SSLfatal() already called */
2275
0
            goto err;
2276
0
        }
2277
0
2278
0
        wst = WORK_MORE_C;
2279
0
    }
2280
0
#ifndef OPENSSL_NO_SRP
2281
0
    if (wst == WORK_MORE_C) {
2282
0
        int ret;
2283
0
        if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) {
2284
0
            /*
2285
0
             * callback indicates further work to be done
2286
0
             */
2287
0
            s->rwstate = SSL_X509_LOOKUP;
2288
0
            return WORK_MORE_C;
2289
0
        }
2290
0
        if (ret < 0) {
2291
0
            /* SSLfatal() already called */
2292
0
            goto err;
2293
0
        }
2294
0
    }
2295
0
#endif
2296
0
2297
0
    return WORK_FINISHED_STOP;
2298
0
 err:
2299
0
    return WORK_ERROR;
2300
0
}
2301
2302
int tls_construct_server_hello(SSL *s, WPACKET *pkt)
2303
0
{
2304
0
    int compm;
2305
0
    size_t sl, len;
2306
0
    int version;
2307
0
    unsigned char *session_id;
2308
0
    int usetls13 = SSL_IS_TLS13(s) || s->hello_retry_request == SSL_HRR_PENDING;
2309
0
2310
0
    version = usetls13 ? TLS1_2_VERSION : s->version;
2311
0
    if (!WPACKET_put_bytes_u16(pkt, version)
2312
0
               /*
2313
0
                * Random stuff. Filling of the server_random takes place in
2314
0
                * tls_process_client_hello()
2315
0
                */
2316
0
            || !WPACKET_memcpy(pkt,
2317
0
                               s->hello_retry_request == SSL_HRR_PENDING
2318
0
                                   ? hrrrandom : s->s3->server_random,
2319
0
                               SSL3_RANDOM_SIZE)) {
2320
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
2321
0
                 ERR_R_INTERNAL_ERROR);
2322
0
        return 0;
2323
0
    }
2324
0
2325
0
    /*-
2326
0
     * There are several cases for the session ID to send
2327
0
     * back in the server hello:
2328
0
     * - For session reuse from the session cache,
2329
0
     *   we send back the old session ID.
2330
0
     * - If stateless session reuse (using a session ticket)
2331
0
     *   is successful, we send back the client's "session ID"
2332
0
     *   (which doesn't actually identify the session).
2333
0
     * - If it is a new session, we send back the new
2334
0
     *   session ID.
2335
0
     * - However, if we want the new session to be single-use,
2336
0
     *   we send back a 0-length session ID.
2337
0
     * - In TLSv1.3 we echo back the session id sent to us by the client
2338
0
     *   regardless
2339
0
     * s->hit is non-zero in either case of session reuse,
2340
0
     * so the following won't overwrite an ID that we're supposed
2341
0
     * to send back.
2342
0
     */
2343
0
    if (s->session->not_resumable ||
2344
0
        (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
2345
0
         && !s->hit))
2346
0
        s->session->session_id_length = 0;
2347
0
2348
0
    if (usetls13) {
2349
0
        sl = s->tmp_session_id_len;
2350
0
        session_id = s->tmp_session_id;
2351
0
    } else {
2352
0
        sl = s->session->session_id_length;
2353
0
        session_id = s->session->session_id;
2354
0
    }
2355
0
2356
0
    if (sl > sizeof(s->session->session_id)) {
2357
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
2358
0
                 ERR_R_INTERNAL_ERROR);
2359
0
        return 0;
2360
0
    }
2361
0
2362
0
    /* set up the compression method */
2363
#ifdef OPENSSL_NO_COMP
2364
    compm = 0;
2365
#else
2366
0
    if (usetls13 || s->s3->tmp.new_compression == NULL)
2367
0
        compm = 0;
2368
0
    else
2369
0
        compm = s->s3->tmp.new_compression->id;
2370
0
#endif
2371
0
2372
0
    if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl)
2373
0
            || !s->method->put_cipher_by_char(s->s3->tmp.new_cipher, pkt, &len)
2374
0
            || !WPACKET_put_bytes_u8(pkt, compm)) {
2375
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_SERVER_HELLO,
2376
0
                 ERR_R_INTERNAL_ERROR);
2377
0
        return 0;
2378
0
    }
2379
0
2380
0
    if (!tls_construct_extensions(s, pkt,
2381
0
                                  s->hello_retry_request == SSL_HRR_PENDING
2382
0
                                      ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
2383
0
                                      : (SSL_IS_TLS13(s)
2384
0
                                          ? SSL_EXT_TLS1_3_SERVER_HELLO
2385
0
                                          : SSL_EXT_TLS1_2_SERVER_HELLO),
2386
0
                                  NULL, 0)) {
2387
0
        /* SSLfatal() already called */
2388
0
        return 0;
2389
0
    }
2390
0
2391
0
    if (s->hello_retry_request == SSL_HRR_PENDING) {
2392
0
        /* Ditch the session. We'll create a new one next time around */
2393
0
        SSL_SESSION_free(s->session);
2394
0
        s->session = NULL;
2395
0
        s->hit = 0;
2396
0
2397
0
        /*
2398
0
         * Re-initialise the Transcript Hash. We're going to prepopulate it with
2399
0
         * a synthetic message_hash in place of ClientHello1.
2400
0
         */
2401
0
        if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) {
2402
0
            /* SSLfatal() already called */
2403
0
            return 0;
2404
0
        }
2405
0
    } else if (!(s->verify_mode & SSL_VERIFY_PEER)
2406
0
                && !ssl3_digest_cached_records(s, 0)) {
2407
0
        /* SSLfatal() already called */;
2408
0
        return 0;
2409
0
    }
2410
0
2411
0
    return 1;
2412
0
}
2413
2414
int tls_construct_server_done(SSL *s, WPACKET *pkt)
2415
0
{
2416
0
    if (!s->s3->tmp.cert_request) {
2417
0
        if (!ssl3_digest_cached_records(s, 0)) {
2418
0
            /* SSLfatal() already called */
2419
0
            return 0;
2420
0
        }
2421
0
    }
2422
0
    return 1;
2423
0
}
2424
2425
int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt)
2426
0
{
2427
0
#ifndef OPENSSL_NO_DH
2428
0
    EVP_PKEY *pkdh = NULL;
2429
0
#endif
2430
0
#ifndef OPENSSL_NO_EC
2431
0
    unsigned char *encodedPoint = NULL;
2432
0
    size_t encodedlen = 0;
2433
0
    int curve_id = 0;
2434
0
#endif
2435
0
    const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
2436
0
    int i;
2437
0
    unsigned long type;
2438
0
    const BIGNUM *r[4];
2439
0
    EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
2440
0
    EVP_PKEY_CTX *pctx = NULL;
2441
0
    size_t paramlen, paramoffset;
2442
0
2443
0
    if (!WPACKET_get_total_written(pkt, &paramoffset)) {
2444
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2445
0
                 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2446
0
        goto err;
2447
0
    }
2448
0
2449
0
    if (md_ctx == NULL) {
2450
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2451
0
                 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2452
0
        goto err;
2453
0
    }
2454
0
2455
0
    type = s->s3->tmp.new_cipher->algorithm_mkey;
2456
0
2457
0
    r[0] = r[1] = r[2] = r[3] = NULL;
2458
0
#ifndef OPENSSL_NO_PSK
2459
0
    /* Plain PSK or RSAPSK nothing to do */
2460
0
    if (type & (SSL_kPSK | SSL_kRSAPSK)) {
2461
0
    } else
2462
0
#endif                          /* !OPENSSL_NO_PSK */
2463
0
#ifndef OPENSSL_NO_DH
2464
0
    if (type & (SSL_kDHE | SSL_kDHEPSK)) {
2465
0
        CERT *cert = s->cert;
2466
0
2467
0
        EVP_PKEY *pkdhp = NULL;
2468
0
        DH *dh;
2469
0
2470
0
        if (s->cert->dh_tmp_auto) {
2471
0
            DH *dhp = ssl_get_auto_dh(s);
2472
0
            pkdh = EVP_PKEY_new();
2473
0
            if (pkdh == NULL || dhp == NULL) {
2474
0
                DH_free(dhp);
2475
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2476
0
                         SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2477
0
                         ERR_R_INTERNAL_ERROR);
2478
0
                goto err;
2479
0
            }
2480
0
            EVP_PKEY_assign_DH(pkdh, dhp);
2481
0
            pkdhp = pkdh;
2482
0
        } else {
2483
0
            pkdhp = cert->dh_tmp;
2484
0
        }
2485
0
        if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) {
2486
0
            DH *dhp = s->cert->dh_tmp_cb(s, 0, 1024);
2487
0
            pkdh = ssl_dh_to_pkey(dhp);
2488
0
            if (pkdh == NULL) {
2489
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2490
0
                         SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2491
0
                         ERR_R_INTERNAL_ERROR);
2492
0
                goto err;
2493
0
            }
2494
0
            pkdhp = pkdh;
2495
0
        }
2496
0
        if (pkdhp == NULL) {
2497
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2498
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2499
0
                     SSL_R_MISSING_TMP_DH_KEY);
2500
0
            goto err;
2501
0
        }
2502
0
        if (!ssl_security(s, SSL_SECOP_TMP_DH,
2503
0
                          EVP_PKEY_security_bits(pkdhp), 0, pkdhp)) {
2504
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2505
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2506
0
                     SSL_R_DH_KEY_TOO_SMALL);
2507
0
            goto err;
2508
0
        }
2509
0
        if (s->s3->tmp.pkey != NULL) {
2510
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2511
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2512
0
                     ERR_R_INTERNAL_ERROR);
2513
0
            goto err;
2514
0
        }
2515
0
2516
0
        s->s3->tmp.pkey = ssl_generate_pkey(pkdhp);
2517
0
        if (s->s3->tmp.pkey == NULL) {
2518
0
            /* SSLfatal() already called */
2519
0
            goto err;
2520
0
        }
2521
0
2522
0
        dh = EVP_PKEY_get0_DH(s->s3->tmp.pkey);
2523
0
        if (dh == NULL) {
2524
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2525
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2526
0
                     ERR_R_INTERNAL_ERROR);
2527
0
            goto err;
2528
0
        }
2529
0
2530
0
        EVP_PKEY_free(pkdh);
2531
0
        pkdh = NULL;
2532
0
2533
0
        DH_get0_pqg(dh, &r[0], NULL, &r[1]);
2534
0
        DH_get0_key(dh, &r[2], NULL);
2535
0
    } else
2536
0
#endif
2537
0
#ifndef OPENSSL_NO_EC
2538
0
    if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2539
0
2540
0
        if (s->s3->tmp.pkey != NULL) {
2541
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2542
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2543
0
                     ERR_R_INTERNAL_ERROR);
2544
0
            goto err;
2545
0
        }
2546
0
2547
0
        /* Get NID of appropriate shared curve */
2548
0
        curve_id = tls1_shared_group(s, -2);
2549
0
        if (curve_id == 0) {
2550
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
2551
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2552
0
                     SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
2553
0
            goto err;
2554
0
        }
2555
0
        s->s3->tmp.pkey = ssl_generate_pkey_group(s, curve_id);
2556
0
        /* Generate a new key for this curve */
2557
0
        if (s->s3->tmp.pkey == NULL) {
2558
0
            /* SSLfatal() already called */
2559
0
            goto err;
2560
0
        }
2561
0
2562
0
        /* Encode the public key. */
2563
0
        encodedlen = EVP_PKEY_get1_tls_encodedpoint(s->s3->tmp.pkey,
2564
0
                                                    &encodedPoint);
2565
0
        if (encodedlen == 0) {
2566
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2567
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_EC_LIB);
2568
0
            goto err;
2569
0
        }
2570
0
2571
0
        /*
2572
0
         * We'll generate the serverKeyExchange message explicitly so we
2573
0
         * can set these to NULLs
2574
0
         */
2575
0
        r[0] = NULL;
2576
0
        r[1] = NULL;
2577
0
        r[2] = NULL;
2578
0
        r[3] = NULL;
2579
0
    } else
2580
0
#endif                          /* !OPENSSL_NO_EC */
2581
0
#ifndef OPENSSL_NO_SRP
2582
0
    if (type & SSL_kSRP) {
2583
0
        if ((s->srp_ctx.N == NULL) ||
2584
0
            (s->srp_ctx.g == NULL) ||
2585
0
            (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
2586
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2587
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2588
0
                     SSL_R_MISSING_SRP_PARAM);
2589
0
            goto err;
2590
0
        }
2591
0
        r[0] = s->srp_ctx.N;
2592
0
        r[1] = s->srp_ctx.g;
2593
0
        r[2] = s->srp_ctx.s;
2594
0
        r[3] = s->srp_ctx.B;
2595
0
    } else
2596
0
#endif
2597
0
    {
2598
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2599
0
                 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2600
0
                 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2601
0
        goto err;
2602
0
    }
2603
0
2604
0
    if (((s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0)
2605
0
        || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) {
2606
0
        lu = NULL;
2607
0
    } else if (lu == NULL) {
2608
0
        SSLfatal(s, SSL_AD_DECODE_ERROR,
2609
0
                 SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2610
0
        goto err;
2611
0
    }
2612
0
2613
0
#ifndef OPENSSL_NO_PSK
2614
0
    if (type & SSL_PSK) {
2615
0
        size_t len = (s->cert->psk_identity_hint == NULL)
2616
0
                        ? 0 : strlen(s->cert->psk_identity_hint);
2617
0
2618
0
        /*
2619
0
         * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already
2620
0
         * checked this when we set the identity hint - but just in case
2621
0
         */
2622
0
        if (len > PSK_MAX_IDENTITY_LEN
2623
0
                || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint,
2624
0
                                           len)) {
2625
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2626
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2627
0
                     ERR_R_INTERNAL_ERROR);
2628
0
            goto err;
2629
0
        }
2630
0
    }
2631
0
#endif
2632
0
2633
0
    for (i = 0; i < 4 && r[i] != NULL; i++) {
2634
0
        unsigned char *binval;
2635
0
        int res;
2636
0
2637
0
#ifndef OPENSSL_NO_SRP
2638
0
        if ((i == 2) && (type & SSL_kSRP)) {
2639
0
            res = WPACKET_start_sub_packet_u8(pkt);
2640
0
        } else
2641
0
#endif
2642
0
            res = WPACKET_start_sub_packet_u16(pkt);
2643
0
2644
0
        if (!res) {
2645
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2646
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2647
0
                     ERR_R_INTERNAL_ERROR);
2648
0
            goto err;
2649
0
        }
2650
0
2651
0
#ifndef OPENSSL_NO_DH
2652
0
        /*-
2653
0
         * for interoperability with some versions of the Microsoft TLS
2654
0
         * stack, we need to zero pad the DHE pub key to the same length
2655
0
         * as the prime
2656
0
         */
2657
0
        if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) {
2658
0
            size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]);
2659
0
2660
0
            if (len > 0) {
2661
0
                if (!WPACKET_allocate_bytes(pkt, len, &binval)) {
2662
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2663
0
                             SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2664
0
                             ERR_R_INTERNAL_ERROR);
2665
0
                    goto err;
2666
0
                }
2667
0
                memset(binval, 0, len);
2668
0
            }
2669
0
        }
2670
0
#endif
2671
0
        if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval)
2672
0
                || !WPACKET_close(pkt)) {
2673
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2674
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2675
0
                     ERR_R_INTERNAL_ERROR);
2676
0
            goto err;
2677
0
        }
2678
0
2679
0
        BN_bn2bin(r[i], binval);
2680
0
    }
2681
0
2682
0
#ifndef OPENSSL_NO_EC
2683
0
    if (type & (SSL_kECDHE | SSL_kECDHEPSK)) {
2684
0
        /*
2685
0
         * We only support named (not generic) curves. In this situation, the
2686
0
         * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName]
2687
0
         * [1 byte length of encoded point], followed by the actual encoded
2688
0
         * point itself
2689
0
         */
2690
0
        if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE)
2691
0
                || !WPACKET_put_bytes_u8(pkt, 0)
2692
0
                || !WPACKET_put_bytes_u8(pkt, curve_id)
2693
0
                || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) {
2694
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2695
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2696
0
                     ERR_R_INTERNAL_ERROR);
2697
0
            goto err;
2698
0
        }
2699
0
        OPENSSL_free(encodedPoint);
2700
0
        encodedPoint = NULL;
2701
0
    }
2702
0
#endif
2703
0
2704
0
    /* not anonymous */
2705
0
    if (lu != NULL) {
2706
0
        EVP_PKEY *pkey = s->s3->tmp.cert->privatekey;
2707
0
        const EVP_MD *md;
2708
0
        unsigned char *sigbytes1, *sigbytes2, *tbs;
2709
0
        size_t siglen, tbslen;
2710
0
        int rv;
2711
0
2712
0
        if (pkey == NULL || !tls1_lookup_md(lu, &md)) {
2713
0
            /* Should never happen */
2714
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2715
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2716
0
                     ERR_R_INTERNAL_ERROR);
2717
0
            goto err;
2718
0
        }
2719
0
        /* Get length of the parameters we have written above */
2720
0
        if (!WPACKET_get_length(pkt, &paramlen)) {
2721
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2722
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2723
0
                     ERR_R_INTERNAL_ERROR);
2724
0
            goto err;
2725
0
        }
2726
0
        /* send signature algorithm */
2727
0
        if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
2728
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2729
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2730
0
                     ERR_R_INTERNAL_ERROR);
2731
0
            goto err;
2732
0
        }
2733
0
        /*
2734
0
         * Create the signature. We don't know the actual length of the sig
2735
0
         * until after we've created it, so we reserve enough bytes for it
2736
0
         * up front, and then properly allocate them in the WPACKET
2737
0
         * afterwards.
2738
0
         */
2739
0
        siglen = EVP_PKEY_size(pkey);
2740
0
        if (!WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1)
2741
0
            || EVP_DigestSignInit(md_ctx, &pctx, md, NULL, pkey) <= 0) {
2742
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2743
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2744
0
                     ERR_R_INTERNAL_ERROR);
2745
0
            goto err;
2746
0
        }
2747
0
        if (lu->sig == EVP_PKEY_RSA_PSS) {
2748
0
            if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
2749
0
                || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) {
2750
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2751
0
                         SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2752
0
                        ERR_R_EVP_LIB);
2753
0
                goto err;
2754
0
            }
2755
0
        }
2756
0
        tbslen = construct_key_exchange_tbs(s, &tbs,
2757
0
                                            s->init_buf->data + paramoffset,
2758
0
                                            paramlen);
2759
0
        if (tbslen == 0) {
2760
0
            /* SSLfatal() already called */
2761
0
            goto err;
2762
0
        }
2763
0
        rv = EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen);
2764
0
        OPENSSL_free(tbs);
2765
0
        if (rv <= 0 || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2)
2766
0
            || sigbytes1 != sigbytes2) {
2767
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2768
0
                     SSL_F_TLS_CONSTRUCT_SERVER_KEY_EXCHANGE,
2769
0
                     ERR_R_INTERNAL_ERROR);
2770
0
            goto err;
2771
0
        }
2772
0
    }
2773
0
2774
0
    EVP_MD_CTX_free(md_ctx);
2775
0
    return 1;
2776
0
 err:
2777
0
#ifndef OPENSSL_NO_DH
2778
0
    EVP_PKEY_free(pkdh);
2779
0
#endif
2780
0
#ifndef OPENSSL_NO_EC
2781
0
    OPENSSL_free(encodedPoint);
2782
0
#endif
2783
0
    EVP_MD_CTX_free(md_ctx);
2784
0
    return 0;
2785
0
}
2786
2787
int tls_construct_certificate_request(SSL *s, WPACKET *pkt)
2788
0
{
2789
0
    if (SSL_IS_TLS13(s)) {
2790
0
        /* Send random context when doing post-handshake auth */
2791
0
        if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) {
2792
0
            OPENSSL_free(s->pha_context);
2793
0
            s->pha_context_len = 32;
2794
0
            if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL
2795
0
                    || RAND_bytes(s->pha_context, s->pha_context_len) <= 0
2796
0
                    || !WPACKET_sub_memcpy_u8(pkt, s->pha_context, s->pha_context_len)) {
2797
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2798
0
                         SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2799
0
                         ERR_R_INTERNAL_ERROR);
2800
0
                return 0;
2801
0
            }
2802
0
            /* reset the handshake hash back to just after the ClientFinished */
2803
0
            if (!tls13_restore_handshake_digest_for_pha(s)) {
2804
0
                /* SSLfatal() already called */
2805
0
                return 0;
2806
0
            }
2807
0
        } else {
2808
0
            if (!WPACKET_put_bytes_u8(pkt, 0)) {
2809
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2810
0
                         SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2811
0
                         ERR_R_INTERNAL_ERROR);
2812
0
                return 0;
2813
0
            }
2814
0
        }
2815
0
2816
0
        if (!tls_construct_extensions(s, pkt,
2817
0
                                      SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL,
2818
0
                                      0)) {
2819
0
            /* SSLfatal() already called */
2820
0
            return 0;
2821
0
        }
2822
0
        goto done;
2823
0
    }
2824
0
2825
0
    /* get the list of acceptable cert types */
2826
0
    if (!WPACKET_start_sub_packet_u8(pkt)
2827
0
        || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) {
2828
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2829
0
                 SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2830
0
        return 0;
2831
0
    }
2832
0
2833
0
    if (SSL_USE_SIGALGS(s)) {
2834
0
        const uint16_t *psigs;
2835
0
        size_t nl = tls12_get_psigalgs(s, 1, &psigs);
2836
0
2837
0
        if (!WPACKET_start_sub_packet_u16(pkt)
2838
0
                || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
2839
0
                || !tls12_copy_sigalgs(s, pkt, psigs, nl)
2840
0
                || !WPACKET_close(pkt)) {
2841
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2842
0
                     SSL_F_TLS_CONSTRUCT_CERTIFICATE_REQUEST,
2843
0
                     ERR_R_INTERNAL_ERROR);
2844
0
            return 0;
2845
0
        }
2846
0
    }
2847
0
2848
0
    if (!construct_ca_names(s, pkt)) {
2849
0
        /* SSLfatal() already called */
2850
0
        return 0;
2851
0
    }
2852
0
2853
0
 done:
2854
0
    s->certreqs_sent++;
2855
0
    s->s3->tmp.cert_request = 1;
2856
0
    return 1;
2857
0
}
2858
2859
static int tls_process_cke_psk_preamble(SSL *s, PACKET *pkt)
2860
0
{
2861
0
#ifndef OPENSSL_NO_PSK
2862
0
    unsigned char psk[PSK_MAX_PSK_LEN];
2863
0
    size_t psklen;
2864
0
    PACKET psk_identity;
2865
0
2866
0
    if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) {
2867
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2868
0
                 SSL_R_LENGTH_MISMATCH);
2869
0
        return 0;
2870
0
    }
2871
0
    if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) {
2872
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2873
0
                 SSL_R_DATA_LENGTH_TOO_LONG);
2874
0
        return 0;
2875
0
    }
2876
0
    if (s->psk_server_callback == NULL) {
2877
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2878
0
                 SSL_R_PSK_NO_SERVER_CB);
2879
0
        return 0;
2880
0
    }
2881
0
2882
0
    if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) {
2883
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2884
0
                 ERR_R_INTERNAL_ERROR);
2885
0
        return 0;
2886
0
    }
2887
0
2888
0
    psklen = s->psk_server_callback(s, s->session->psk_identity,
2889
0
                                    psk, sizeof(psk));
2890
0
2891
0
    if (psklen > PSK_MAX_PSK_LEN) {
2892
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2893
0
                 ERR_R_INTERNAL_ERROR);
2894
0
        return 0;
2895
0
    } else if (psklen == 0) {
2896
0
        /*
2897
0
         * PSK related to the given identity not found
2898
0
         */
2899
0
        SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY,
2900
0
                 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2901
0
                 SSL_R_PSK_IDENTITY_NOT_FOUND);
2902
0
        return 0;
2903
0
    }
2904
0
2905
0
    OPENSSL_free(s->s3->tmp.psk);
2906
0
    s->s3->tmp.psk = OPENSSL_memdup(psk, psklen);
2907
0
    OPENSSL_cleanse(psk, psklen);
2908
0
2909
0
    if (s->s3->tmp.psk == NULL) {
2910
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2911
0
                 SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE, ERR_R_MALLOC_FAILURE);
2912
0
        return 0;
2913
0
    }
2914
0
2915
0
    s->s3->tmp.psklen = psklen;
2916
0
2917
0
    return 1;
2918
#else
2919
    /* Should never happen */
2920
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_PSK_PREAMBLE,
2921
             ERR_R_INTERNAL_ERROR);
2922
    return 0;
2923
#endif
2924
}
2925
2926
static int tls_process_cke_rsa(SSL *s, PACKET *pkt)
2927
0
{
2928
0
#ifndef OPENSSL_NO_RSA
2929
0
    unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2930
0
    int decrypt_len;
2931
0
    unsigned char decrypt_good, version_good;
2932
0
    size_t j, padding_len;
2933
0
    PACKET enc_premaster;
2934
0
    RSA *rsa = NULL;
2935
0
    unsigned char *rsa_decrypt = NULL;
2936
0
    int ret = 0;
2937
0
2938
0
    rsa = EVP_PKEY_get0_RSA(s->cert->pkeys[SSL_PKEY_RSA].privatekey);
2939
0
    if (rsa == NULL) {
2940
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2941
0
                 SSL_R_MISSING_RSA_CERTIFICATE);
2942
0
        return 0;
2943
0
    }
2944
0
2945
0
    /* SSLv3 and pre-standard DTLS omit the length bytes. */
2946
0
    if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) {
2947
0
        enc_premaster = *pkt;
2948
0
    } else {
2949
0
        if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster)
2950
0
            || PACKET_remaining(pkt) != 0) {
2951
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2952
0
                     SSL_R_LENGTH_MISMATCH);
2953
0
            return 0;
2954
0
        }
2955
0
    }
2956
0
2957
0
    /*
2958
0
     * We want to be sure that the plaintext buffer size makes it safe to
2959
0
     * iterate over the entire size of a premaster secret
2960
0
     * (SSL_MAX_MASTER_KEY_LENGTH). Reject overly short RSA keys because
2961
0
     * their ciphertext cannot accommodate a premaster secret anyway.
2962
0
     */
2963
0
    if (RSA_size(rsa) < SSL_MAX_MASTER_KEY_LENGTH) {
2964
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2965
0
                 RSA_R_KEY_SIZE_TOO_SMALL);
2966
0
        return 0;
2967
0
    }
2968
0
2969
0
    rsa_decrypt = OPENSSL_malloc(RSA_size(rsa));
2970
0
    if (rsa_decrypt == NULL) {
2971
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2972
0
                 ERR_R_MALLOC_FAILURE);
2973
0
        return 0;
2974
0
    }
2975
0
2976
0
    /*
2977
0
     * We must not leak whether a decryption failure occurs because of
2978
0
     * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2979
0
     * section 7.4.7.1). The code follows that advice of the TLS RFC and
2980
0
     * generates a random premaster secret for the case that the decrypt
2981
0
     * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2982
0
     */
2983
0
2984
0
    if (RAND_priv_bytes(rand_premaster_secret,
2985
0
                      sizeof(rand_premaster_secret)) <= 0) {
2986
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
2987
0
                 ERR_R_INTERNAL_ERROR);
2988
0
        goto err;
2989
0
    }
2990
0
2991
0
    /*
2992
0
     * Decrypt with no padding. PKCS#1 padding will be removed as part of
2993
0
     * the timing-sensitive code below.
2994
0
     */
2995
0
     /* TODO(size_t): Convert this function */
2996
0
    decrypt_len = (int)RSA_private_decrypt((int)PACKET_remaining(&enc_premaster),
2997
0
                                           PACKET_data(&enc_premaster),
2998
0
                                           rsa_decrypt, rsa, RSA_NO_PADDING);
2999
0
    if (decrypt_len < 0) {
3000
0
        SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
3001
0
                 ERR_R_INTERNAL_ERROR);
3002
0
        goto err;
3003
0
    }
3004
0
3005
0
    /* Check the padding. See RFC 3447, section 7.2.2. */
3006
0
3007
0
    /*
3008
0
     * The smallest padded premaster is 11 bytes of overhead. Small keys
3009
0
     * are publicly invalid, so this may return immediately. This ensures
3010
0
     * PS is at least 8 bytes.
3011
0
     */
3012
0
    if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) {
3013
0
        SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
3014
0
                 SSL_R_DECRYPTION_FAILED);
3015
0
        goto err;
3016
0
    }
3017
0
3018
0
    padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH;
3019
0
    decrypt_good = constant_time_eq_int_8(rsa_decrypt[0], 0) &
3020
0
        constant_time_eq_int_8(rsa_decrypt[1], 2);
3021
0
    for (j = 2; j < padding_len - 1; j++) {
3022
0
        decrypt_good &= ~constant_time_is_zero_8(rsa_decrypt[j]);
3023
0
    }
3024
0
    decrypt_good &= constant_time_is_zero_8(rsa_decrypt[padding_len - 1]);
3025
0
3026
0
    /*
3027
0
     * If the version in the decrypted pre-master secret is correct then
3028
0
     * version_good will be 0xff, otherwise it'll be zero. The
3029
0
     * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
3030
0
     * (http://eprint.iacr.org/2003/052/) exploits the version number
3031
0
     * check as a "bad version oracle". Thus version checks are done in
3032
0
     * constant time and are treated like any other decryption error.
3033
0
     */
3034
0
    version_good =
3035
0
        constant_time_eq_8(rsa_decrypt[padding_len],
3036
0
                           (unsigned)(s->client_version >> 8));
3037
0
    version_good &=
3038
0
        constant_time_eq_8(rsa_decrypt[padding_len + 1],
3039
0
                           (unsigned)(s->client_version & 0xff));
3040
0
3041
0
    /*
3042
0
     * The premaster secret must contain the same version number as the
3043
0
     * ClientHello to detect version rollback attacks (strangely, the
3044
0
     * protocol does not offer such protection for DH ciphersuites).
3045
0
     * However, buggy clients exist that send the negotiated protocol
3046
0
     * version instead if the server does not support the requested
3047
0
     * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
3048
0
     * clients.
3049
0
     */
3050
0
    if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
3051
0
        unsigned char workaround_good;
3052
0
        workaround_good = constant_time_eq_8(rsa_decrypt[padding_len],
3053
0
                                             (unsigned)(s->version >> 8));
3054
0
        workaround_good &=
3055
0
            constant_time_eq_8(rsa_decrypt[padding_len + 1],
3056
0
                               (unsigned)(s->version & 0xff));
3057
0
        version_good |= workaround_good;
3058
0
    }
3059
0
3060
0
    /*
3061
0
     * Both decryption and version must be good for decrypt_good to
3062
0
     * remain non-zero (0xff).
3063
0
     */
3064
0
    decrypt_good &= version_good;
3065
0
3066
0
    /*
3067
0
     * Now copy rand_premaster_secret over from p using
3068
0
     * decrypt_good_mask. If decryption failed, then p does not
3069
0
     * contain valid plaintext, however, a check above guarantees
3070
0
     * it is still sufficiently large to read from.
3071
0
     */
3072
0
    for (j = 0; j < sizeof(rand_premaster_secret); j++) {
3073
0
        rsa_decrypt[padding_len + j] =
3074
0
            constant_time_select_8(decrypt_good,
3075
0
                                   rsa_decrypt[padding_len + j],
3076
0
                                   rand_premaster_secret[j]);
3077
0
    }
3078
0
3079
0
    if (!ssl_generate_master_secret(s, rsa_decrypt + padding_len,
3080
0
                                    sizeof(rand_premaster_secret), 0)) {
3081
0
        /* SSLfatal() already called */
3082
0
        goto err;
3083
0
    }
3084
0
3085
0
    ret = 1;
3086
0
 err:
3087
0
    OPENSSL_free(rsa_decrypt);
3088
0
    return ret;
3089
#else
3090
    /* Should never happen */
3091
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_RSA,
3092
             ERR_R_INTERNAL_ERROR);
3093
    return 0;
3094
#endif
3095
}
3096
3097
static int tls_process_cke_dhe(SSL *s, PACKET *pkt)
3098
0
{
3099
0
#ifndef OPENSSL_NO_DH
3100
0
    EVP_PKEY *skey = NULL;
3101
0
    DH *cdh;
3102
0
    unsigned int i;
3103
0
    BIGNUM *pub_key;
3104
0
    const unsigned char *data;
3105
0
    EVP_PKEY *ckey = NULL;
3106
0
    int ret = 0;
3107
0
3108
0
    if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) {
3109
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3110
0
               SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
3111
0
        goto err;
3112
0
    }
3113
0
    skey = s->s3->tmp.pkey;
3114
0
    if (skey == NULL) {
3115
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3116
0
                 SSL_R_MISSING_TMP_DH_KEY);
3117
0
        goto err;
3118
0
    }
3119
0
3120
0
    if (PACKET_remaining(pkt) == 0L) {
3121
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3122
0
                 SSL_R_MISSING_TMP_DH_KEY);
3123
0
        goto err;
3124
0
    }
3125
0
    if (!PACKET_get_bytes(pkt, &data, i)) {
3126
0
        /* We already checked we have enough data */
3127
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3128
0
                 ERR_R_INTERNAL_ERROR);
3129
0
        goto err;
3130
0
    }
3131
0
    ckey = EVP_PKEY_new();
3132
0
    if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) {
3133
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3134
0
                 SSL_R_BN_LIB);
3135
0
        goto err;
3136
0
    }
3137
0
3138
0
    cdh = EVP_PKEY_get0_DH(ckey);
3139
0
    pub_key = BN_bin2bn(data, i, NULL);
3140
0
    if (pub_key == NULL || cdh == NULL || !DH_set0_key(cdh, pub_key, NULL)) {
3141
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3142
0
                 ERR_R_INTERNAL_ERROR);
3143
0
        BN_free(pub_key);
3144
0
        goto err;
3145
0
    }
3146
0
3147
0
    if (ssl_derive(s, skey, ckey, 1) == 0) {
3148
0
        /* SSLfatal() already called */
3149
0
        goto err;
3150
0
    }
3151
0
3152
0
    ret = 1;
3153
0
    EVP_PKEY_free(s->s3->tmp.pkey);
3154
0
    s->s3->tmp.pkey = NULL;
3155
0
 err:
3156
0
    EVP_PKEY_free(ckey);
3157
0
    return ret;
3158
#else
3159
    /* Should never happen */
3160
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_DHE,
3161
             ERR_R_INTERNAL_ERROR);
3162
    return 0;
3163
#endif
3164
}
3165
3166
static int tls_process_cke_ecdhe(SSL *s, PACKET *pkt)
3167
0
{
3168
0
#ifndef OPENSSL_NO_EC
3169
0
    EVP_PKEY *skey = s->s3->tmp.pkey;
3170
0
    EVP_PKEY *ckey = NULL;
3171
0
    int ret = 0;
3172
0
3173
0
    if (PACKET_remaining(pkt) == 0L) {
3174
0
        /* We don't support ECDH client auth */
3175
0
        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_PROCESS_CKE_ECDHE,
3176
0
                 SSL_R_MISSING_TMP_ECDH_KEY);
3177
0
        goto err;
3178
0
    } else {
3179
0
        unsigned int i;
3180
0
        const unsigned char *data;
3181
0
3182
0
        /*
3183
0
         * Get client's public key from encoded point in the
3184
0
         * ClientKeyExchange message.
3185
0
         */
3186
0
3187
0
        /* Get encoded point length */
3188
0
        if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i)
3189
0
            || PACKET_remaining(pkt) != 0) {
3190
0
            SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3191
0
                     SSL_R_LENGTH_MISMATCH);
3192
0
            goto err;
3193
0
        }
3194
0
        ckey = EVP_PKEY_new();
3195
0
        if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) {
3196
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3197
0
                     ERR_R_EVP_LIB);
3198
0
            goto err;
3199
0
        }
3200
0
        if (EVP_PKEY_set1_tls_encodedpoint(ckey, data, i) == 0) {
3201
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3202
0
                     ERR_R_EC_LIB);
3203
0
            goto err;
3204
0
        }
3205
0
    }
3206
0
3207
0
    if (ssl_derive(s, skey, ckey, 1) == 0) {
3208
0
        /* SSLfatal() already called */
3209
0
        goto err;
3210
0
    }
3211
0
3212
0
    ret = 1;
3213
0
    EVP_PKEY_free(s->s3->tmp.pkey);
3214
0
    s->s3->tmp.pkey = NULL;
3215
0
 err:
3216
0
    EVP_PKEY_free(ckey);
3217
0
3218
0
    return ret;
3219
#else
3220
    /* Should never happen */
3221
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_ECDHE,
3222
             ERR_R_INTERNAL_ERROR);
3223
    return 0;
3224
#endif
3225
}
3226
3227
static int tls_process_cke_srp(SSL *s, PACKET *pkt)
3228
0
{
3229
0
#ifndef OPENSSL_NO_SRP
3230
0
    unsigned int i;
3231
0
    const unsigned char *data;
3232
0
3233
0
    if (!PACKET_get_net_2(pkt, &i)
3234
0
        || !PACKET_get_bytes(pkt, &data, i)) {
3235
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3236
0
                 SSL_R_BAD_SRP_A_LENGTH);
3237
0
        return 0;
3238
0
    }
3239
0
    if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) {
3240
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3241
0
                 ERR_R_BN_LIB);
3242
0
        return 0;
3243
0
    }
3244
0
    if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) {
3245
0
        SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_CKE_SRP,
3246
0
                 SSL_R_BAD_SRP_PARAMETERS);
3247
0
        return 0;
3248
0
    }
3249
0
    OPENSSL_free(s->session->srp_username);
3250
0
    s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login);
3251
0
    if (s->session->srp_username == NULL) {
3252
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3253
0
                 ERR_R_MALLOC_FAILURE);
3254
0
        return 0;
3255
0
    }
3256
0
3257
0
    if (!srp_generate_server_master_secret(s)) {
3258
0
        /* SSLfatal() already called */
3259
0
        return 0;
3260
0
    }
3261
0
3262
0
    return 1;
3263
#else
3264
    /* Should never happen */
3265
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_SRP,
3266
             ERR_R_INTERNAL_ERROR);
3267
    return 0;
3268
#endif
3269
}
3270
3271
static int tls_process_cke_gost(SSL *s, PACKET *pkt)
3272
0
{
3273
0
#ifndef OPENSSL_NO_GOST
3274
0
    EVP_PKEY_CTX *pkey_ctx;
3275
0
    EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
3276
0
    unsigned char premaster_secret[32];
3277
0
    const unsigned char *start;
3278
0
    size_t outlen = 32, inlen;
3279
0
    unsigned long alg_a;
3280
0
    unsigned int asn1id, asn1len;
3281
0
    int ret = 0;
3282
0
    PACKET encdata;
3283
0
3284
0
    /* Get our certificate private key */
3285
0
    alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3286
0
    if (alg_a & SSL_aGOST12) {
3287
0
        /*
3288
0
         * New GOST ciphersuites have SSL_aGOST01 bit too
3289
0
         */
3290
0
        pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey;
3291
0
        if (pk == NULL) {
3292
0
            pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey;
3293
0
        }
3294
0
        if (pk == NULL) {
3295
0
            pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
3296
0
        }
3297
0
    } else if (alg_a & SSL_aGOST01) {
3298
0
        pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
3299
0
    }
3300
0
3301
0
    pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
3302
0
    if (pkey_ctx == NULL) {
3303
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3304
0
                 ERR_R_MALLOC_FAILURE);
3305
0
        return 0;
3306
0
    }
3307
0
    if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
3308
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3309
0
                 ERR_R_INTERNAL_ERROR);
3310
0
        return 0;
3311
0
    }
3312
0
    /*
3313
0
     * If client certificate is present and is of the same type, maybe
3314
0
     * use it for key exchange.  Don't mind errors from
3315
0
     * EVP_PKEY_derive_set_peer, because it is completely valid to use a
3316
0
     * client certificate for authorization only.
3317
0
     */
3318
0
    client_pub_pkey = X509_get0_pubkey(s->session->peer);
3319
0
    if (client_pub_pkey) {
3320
0
        if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
3321
0
            ERR_clear_error();
3322
0
    }
3323
0
    /* Decrypt session key */
3324
0
    if (!PACKET_get_1(pkt, &asn1id)
3325
0
            || asn1id != (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)
3326
0
            || !PACKET_peek_1(pkt, &asn1len)) {
3327
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3328
0
                 SSL_R_DECRYPTION_FAILED);
3329
0
        goto err;
3330
0
    }
3331
0
    if (asn1len == 0x81) {
3332
0
        /*
3333
0
         * Long form length. Should only be one byte of length. Anything else
3334
0
         * isn't supported.
3335
0
         * We did a successful peek before so this shouldn't fail
3336
0
         */
3337
0
        if (!PACKET_forward(pkt, 1)) {
3338
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3339
0
                     SSL_R_DECRYPTION_FAILED);
3340
0
            goto err;
3341
0
        }
3342
0
    } else  if (asn1len >= 0x80) {
3343
0
        /*
3344
0
         * Indefinite length, or more than one long form length bytes. We don't
3345
0
         * support it
3346
0
         */
3347
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3348
0
                 SSL_R_DECRYPTION_FAILED);
3349
0
        goto err;
3350
0
    } /* else short form length */
3351
0
3352
0
    if (!PACKET_as_length_prefixed_1(pkt, &encdata)) {
3353
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3354
0
                 SSL_R_DECRYPTION_FAILED);
3355
0
        goto err;
3356
0
    }
3357
0
    inlen = PACKET_remaining(&encdata);
3358
0
    start = PACKET_data(&encdata);
3359
0
3360
0
    if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start,
3361
0
                         inlen) <= 0) {
3362
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3363
0
                 SSL_R_DECRYPTION_FAILED);
3364
0
        goto err;
3365
0
    }
3366
0
    /* Generate master secret */
3367
0
    if (!ssl_generate_master_secret(s, premaster_secret,
3368
0
                                    sizeof(premaster_secret), 0)) {
3369
0
        /* SSLfatal() already called */
3370
0
        goto err;
3371
0
    }
3372
0
    /* Check if pubkey from client certificate was used */
3373
0
    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2,
3374
0
                          NULL) > 0)
3375
0
        s->statem.no_cert_verify = 1;
3376
0
3377
0
    ret = 1;
3378
0
 err:
3379
0
    EVP_PKEY_CTX_free(pkey_ctx);
3380
0
    return ret;
3381
#else
3382
    /* Should never happen */
3383
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CKE_GOST,
3384
             ERR_R_INTERNAL_ERROR);
3385
    return 0;
3386
#endif
3387
}
3388
3389
MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt)
3390
0
{
3391
0
    unsigned long alg_k;
3392
0
3393
0
    alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3394
0
3395
0
    /* For PSK parse and retrieve identity, obtain PSK key */
3396
0
    if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) {
3397
0
        /* SSLfatal() already called */
3398
0
        goto err;
3399
0
    }
3400
0
3401
0
    if (alg_k & SSL_kPSK) {
3402
0
        /* Identity extracted earlier: should be nothing left */
3403
0
        if (PACKET_remaining(pkt) != 0) {
3404
0
            SSLfatal(s, SSL_AD_DECODE_ERROR,
3405
0
                     SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
3406
0
                     SSL_R_LENGTH_MISMATCH);
3407
0
            goto err;
3408
0
        }
3409
0
        /* PSK handled by ssl_generate_master_secret */
3410
0
        if (!ssl_generate_master_secret(s, NULL, 0, 0)) {
3411
0
            /* SSLfatal() already called */
3412
0
            goto err;
3413
0
        }
3414
0
    } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3415
0
        if (!tls_process_cke_rsa(s, pkt)) {
3416
0
            /* SSLfatal() already called */
3417
0
            goto err;
3418
0
        }
3419
0
    } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
3420
0
        if (!tls_process_cke_dhe(s, pkt)) {
3421
0
            /* SSLfatal() already called */
3422
0
            goto err;
3423
0
        }
3424
0
    } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
3425
0
        if (!tls_process_cke_ecdhe(s, pkt)) {
3426
0
            /* SSLfatal() already called */
3427
0
            goto err;
3428
0
        }
3429
0
    } else if (alg_k & SSL_kSRP) {
3430
0
        if (!tls_process_cke_srp(s, pkt)) {
3431
0
            /* SSLfatal() already called */
3432
0
            goto err;
3433
0
        }
3434
0
    } else if (alg_k & SSL_kGOST) {
3435
0
        if (!tls_process_cke_gost(s, pkt)) {
3436
0
            /* SSLfatal() already called */
3437
0
            goto err;
3438
0
        }
3439
0
    } else {
3440
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3441
0
                 SSL_F_TLS_PROCESS_CLIENT_KEY_EXCHANGE,
3442
0
                 SSL_R_UNKNOWN_CIPHER_TYPE);
3443
0
        goto err;
3444
0
    }
3445
0
3446
0
    return MSG_PROCESS_CONTINUE_PROCESSING;
3447
0
 err:
3448
0
#ifndef OPENSSL_NO_PSK
3449
0
    OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
3450
0
    s->s3->tmp.psk = NULL;
3451
0
#endif
3452
0
    return MSG_PROCESS_ERROR;
3453
0
}
3454
3455
WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst)
3456
0
{
3457
#ifndef OPENSSL_NO_SCTP
3458
    if (wst == WORK_MORE_A) {
3459
        if (SSL_IS_DTLS(s)) {
3460
            unsigned char sctpauthkey[64];
3461
            char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
3462
            /*
3463
             * Add new shared key for SCTP-Auth, will be ignored if no SCTP
3464
             * used.
3465
             */
3466
            memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
3467
                   sizeof(DTLS1_SCTP_AUTH_LABEL));
3468
3469
            if (SSL_export_keying_material(s, sctpauthkey,
3470
                                           sizeof(sctpauthkey), labelbuffer,
3471
                                           sizeof(labelbuffer), NULL, 0,
3472
                                           0) <= 0) {
3473
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3474
                         SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
3475
                         ERR_R_INTERNAL_ERROR);
3476
                return WORK_ERROR;
3477
            }
3478
3479
            BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
3480
                     sizeof(sctpauthkey), sctpauthkey);
3481
        }
3482
    }
3483
#endif
3484
3485
0
    if (s->statem.no_cert_verify || !s->session->peer) {
3486
0
        /*
3487
0
         * No certificate verify or no peer certificate so we no longer need
3488
0
         * the handshake_buffer
3489
0
         */
3490
0
        if (!ssl3_digest_cached_records(s, 0)) {
3491
0
            /* SSLfatal() already called */
3492
0
            return WORK_ERROR;
3493
0
        }
3494
0
        return WORK_FINISHED_CONTINUE;
3495
0
    } else {
3496
0
        if (!s->s3->handshake_buffer) {
3497
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3498
0
                     SSL_F_TLS_POST_PROCESS_CLIENT_KEY_EXCHANGE,
3499
0
                     ERR_R_INTERNAL_ERROR);
3500
0
            return WORK_ERROR;
3501
0
        }
3502
0
        /*
3503
0
         * For sigalgs freeze the handshake buffer. If we support
3504
0
         * extms we've done this already so this is a no-op
3505
0
         */
3506
0
        if (!ssl3_digest_cached_records(s, 1)) {
3507
0
            /* SSLfatal() already called */
3508
0
            return WORK_ERROR;
3509
0
        }
3510
0
    }
3511
0
3512
0
    return WORK_FINISHED_CONTINUE;
3513
0
}
3514
3515
MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt)
3516
0
{
3517
0
    int i;
3518
0
    MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
3519
0
    X509 *x = NULL;
3520
0
    unsigned long l;
3521
0
    const unsigned char *certstart, *certbytes;
3522
0
    STACK_OF(X509) *sk = NULL;
3523
0
    PACKET spkt, context;
3524
0
    size_t chainidx;
3525
0
    SSL_SESSION *new_sess = NULL;
3526
0
3527
0
    /*
3528
0
     * To get this far we must have read encrypted data from the client. We no
3529
0
     * longer tolerate unencrypted alerts. This value is ignored if less than
3530
0
     * TLSv1.3
3531
0
     */
3532
0
    s->statem.enc_read_state = ENC_READ_STATE_VALID;
3533
0
3534
0
    if ((sk = sk_X509_new_null()) == NULL) {
3535
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3536
0
                 ERR_R_MALLOC_FAILURE);
3537
0
        goto err;
3538
0
    }
3539
0
3540
0
    if (SSL_IS_TLS13(s) && (!PACKET_get_length_prefixed_1(pkt, &context)
3541
0
                            || (s->pha_context == NULL && PACKET_remaining(&context) != 0)
3542
0
                            || (s->pha_context != NULL &&
3543
0
                                !PACKET_equal(&context, s->pha_context, s->pha_context_len)))) {
3544
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3545
0
                 SSL_R_INVALID_CONTEXT);
3546
0
        goto err;
3547
0
    }
3548
0
3549
0
    if (!PACKET_get_length_prefixed_3(pkt, &spkt)
3550
0
            || PACKET_remaining(pkt) != 0) {
3551
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3552
0
                 SSL_R_LENGTH_MISMATCH);
3553
0
        goto err;
3554
0
    }
3555
0
3556
0
    for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) {
3557
0
        if (!PACKET_get_net_3(&spkt, &l)
3558
0
            || !PACKET_get_bytes(&spkt, &certbytes, l)) {
3559
0
            SSLfatal(s, SSL_AD_DECODE_ERROR,
3560
0
                     SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3561
0
                     SSL_R_CERT_LENGTH_MISMATCH);
3562
0
            goto err;
3563
0
        }
3564
0
3565
0
        certstart = certbytes;
3566
0
        x = d2i_X509(NULL, (const unsigned char **)&certbytes, l);
3567
0
        if (x == NULL) {
3568
0
            SSLfatal(s, SSL_AD_DECODE_ERROR,
3569
0
                     SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3570
0
            goto err;
3571
0
        }
3572
0
        if (certbytes != (certstart + l)) {
3573
0
            SSLfatal(s, SSL_AD_DECODE_ERROR,
3574
0
                     SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3575
0
                     SSL_R_CERT_LENGTH_MISMATCH);
3576
0
            goto err;
3577
0
        }
3578
0
3579
0
        if (SSL_IS_TLS13(s)) {
3580
0
            RAW_EXTENSION *rawexts = NULL;
3581
0
            PACKET extensions;
3582
0
3583
0
            if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) {
3584
0
                SSLfatal(s, SSL_AD_DECODE_ERROR,
3585
0
                         SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3586
0
                         SSL_R_BAD_LENGTH);
3587
0
                goto err;
3588
0
            }
3589
0
            if (!tls_collect_extensions(s, &extensions,
3590
0
                                        SSL_EXT_TLS1_3_CERTIFICATE, &rawexts,
3591
0
                                        NULL, chainidx == 0)
3592
0
                || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE,
3593
0
                                             rawexts, x, chainidx,
3594
0
                                             PACKET_remaining(&spkt) == 0)) {
3595
0
                OPENSSL_free(rawexts);
3596
0
                goto err;
3597
0
            }
3598
0
            OPENSSL_free(rawexts);
3599
0
        }
3600
0
3601
0
        if (!sk_X509_push(sk, x)) {
3602
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3603
0
                     SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3604
0
                     ERR_R_MALLOC_FAILURE);
3605
0
            goto err;
3606
0
        }
3607
0
        x = NULL;
3608
0
    }
3609
0
3610
0
    if (sk_X509_num(sk) <= 0) {
3611
0
        /* TLS does not mind 0 certs returned */
3612
0
        if (s->version == SSL3_VERSION) {
3613
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3614
0
                     SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3615
0
                     SSL_R_NO_CERTIFICATES_RETURNED);
3616
0
            goto err;
3617
0
        }
3618
0
        /* Fail for TLS only if we required a certificate */
3619
0
        else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3620
0
                 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3621
0
            SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED,
3622
0
                     SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3623
0
                     SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3624
0
            goto err;
3625
0
        }
3626
0
        /* No client certificate so digest cached records */
3627
0
        if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s, 0)) {
3628
0
            /* SSLfatal() already called */
3629
0
            goto err;
3630
0
        }
3631
0
    } else {
3632
0
        EVP_PKEY *pkey;
3633
0
        i = ssl_verify_cert_chain(s, sk);
3634
0
        if (i <= 0) {
3635
0
            SSLfatal(s, ssl_x509err2alert(s->verify_result),
3636
0
                     SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3637
0
                     SSL_R_CERTIFICATE_VERIFY_FAILED);
3638
0
            goto err;
3639
0
        }
3640
0
        if (i > 1) {
3641
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3642
0
                     SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE, i);
3643
0
            goto err;
3644
0
        }
3645
0
        pkey = X509_get0_pubkey(sk_X509_value(sk, 0));
3646
0
        if (pkey == NULL) {
3647
0
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
3648
0
                     SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3649
0
                     SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3650
0
            goto err;
3651
0
        }
3652
0
    }
3653
0
3654
0
    /*
3655
0
     * Sessions must be immutable once they go into the session cache. Otherwise
3656
0
     * we can get multi-thread problems. Therefore we don't "update" sessions,
3657
0
     * we replace them with a duplicate. Here, we need to do this every time
3658
0
     * a new certificate is received via post-handshake authentication, as the
3659
0
     * session may have already gone into the session cache.
3660
0
     */
3661
0
3662
0
    if (s->post_handshake_auth == SSL_PHA_REQUESTED) {
3663
0
        if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
3664
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3665
0
                     SSL_F_TLS_PROCESS_CLIENT_CERTIFICATE,
3666
0
                     ERR_R_MALLOC_FAILURE);
3667
0
            goto err;
3668
0
        }
3669
0
3670
0
        SSL_SESSION_free(s->session);
3671
0
        s->session = new_sess;
3672
0
    }
3673
0
3674
0
    X509_free(s->session->peer);
3675
0
    s->session->peer = sk_X509_shift(sk);
3676
0
    s->session->verify_result = s->verify_result;
3677
0
3678
0
    sk_X509_pop_free(s->session->peer_chain, X509_free);
3679
0
    s->session->peer_chain = sk;
3680
0
3681
0
    /*
3682
0
     * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE
3683
0
     * message
3684
0
     */
3685
0
    if (SSL_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) {
3686
0
        /* SSLfatal() already called */
3687
0
        goto err;
3688
0
    }
3689
0
3690
0
    /*
3691
0
     * Inconsistency alert: cert_chain does *not* include the peer's own
3692
0
     * certificate, while we do include it in statem_clnt.c
3693
0
     */
3694
0
    sk = NULL;
3695
0
3696
0
    /* Save the current hash state for when we receive the CertificateVerify */
3697
0
    if (SSL_IS_TLS13(s)) {
3698
0
        if (!ssl_handshake_hash(s, s->cert_verify_hash,
3699
0
                                sizeof(s->cert_verify_hash),
3700
0
                                &s->cert_verify_hash_len)) {
3701
0
            /* SSLfatal() already called */
3702
0
            goto err;
3703
0
        }
3704
0
3705
0
        /* Resend session tickets */
3706
0
        s->sent_tickets = 0;
3707
0
    }
3708
0
3709
0
    ret = MSG_PROCESS_CONTINUE_READING;
3710
0
3711
0
 err:
3712
0
    X509_free(x);
3713
0
    sk_X509_pop_free(sk, X509_free);
3714
0
    return ret;
3715
0
}
3716
3717
int tls_construct_server_certificate(SSL *s, WPACKET *pkt)
3718
0
{
3719
0
    CERT_PKEY *cpk = s->s3->tmp.cert;
3720
0
3721
0
    if (cpk == NULL) {
3722
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3723
0
                 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3724
0
        return 0;
3725
0
    }
3726
0
3727
0
    /*
3728
0
     * In TLSv1.3 the certificate chain is always preceded by a 0 length context
3729
0
     * for the server Certificate message
3730
0
     */
3731
0
    if (SSL_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) {
3732
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3733
0
                 SSL_F_TLS_CONSTRUCT_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3734
0
        return 0;
3735
0
    }
3736
0
    if (!ssl3_output_cert_chain(s, pkt, cpk)) {
3737
0
        /* SSLfatal() already called */
3738
0
        return 0;
3739
0
    }
3740
0
3741
0
    return 1;
3742
0
}
3743
3744
static int create_ticket_prequel(SSL *s, WPACKET *pkt, uint32_t age_add,
3745
                                 unsigned char *tick_nonce)
3746
0
{
3747
0
    /*
3748
0
     * Ticket lifetime hint: For TLSv1.2 this is advisory only and we leave this
3749
0
     * unspecified for resumed session (for simplicity).
3750
0
     * In TLSv1.3 we reset the "time" field above, and always specify the
3751
0
     * timeout.
3752
0
     */
3753
0
    if (!WPACKET_put_bytes_u32(pkt,
3754
0
                               (s->hit && !SSL_IS_TLS13(s))
3755
0
                               ? 0 : s->session->timeout)) {
3756
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CREATE_TICKET_PREQUEL,
3757
0
                 ERR_R_INTERNAL_ERROR);
3758
0
        return 0;
3759
0
    }
3760
0
3761
0
    if (SSL_IS_TLS13(s)) {
3762
0
        if (!WPACKET_put_bytes_u32(pkt, age_add)
3763
0
                || !WPACKET_sub_memcpy_u8(pkt, tick_nonce, TICKET_NONCE_SIZE)) {
3764
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CREATE_TICKET_PREQUEL,
3765
0
                     ERR_R_INTERNAL_ERROR);
3766
0
            return 0;
3767
0
        }
3768
0
    }
3769
0
3770
0
    /* Start the sub-packet for the actual ticket data */
3771
0
    if (!WPACKET_start_sub_packet_u16(pkt)) {
3772
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CREATE_TICKET_PREQUEL,
3773
0
                 ERR_R_INTERNAL_ERROR);
3774
0
        return 0;
3775
0
    }
3776
0
3777
0
    return 1;
3778
0
}
3779
3780
static int construct_stateless_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
3781
                                      unsigned char *tick_nonce)
3782
0
{
3783
0
    unsigned char *senc = NULL;
3784
0
    EVP_CIPHER_CTX *ctx = NULL;
3785
0
    HMAC_CTX *hctx = NULL;
3786
0
    unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2;
3787
0
    const unsigned char *const_p;
3788
0
    int len, slen_full, slen, lenfinal;
3789
0
    SSL_SESSION *sess;
3790
0
    unsigned int hlen;
3791
0
    SSL_CTX *tctx = s->session_ctx;
3792
0
    unsigned char iv[EVP_MAX_IV_LENGTH];
3793
0
    unsigned char key_name[TLSEXT_KEYNAME_LENGTH];
3794
0
    int iv_len, ok = 0;
3795
0
    size_t macoffset, macendoffset;
3796
0
3797
0
    /* get session encoding length */
3798
0
    slen_full = i2d_SSL_SESSION(s->session, NULL);
3799
0
    /*
3800
0
     * Some length values are 16 bits, so forget it if session is too
3801
0
     * long
3802
0
     */
3803
0
    if (slen_full == 0 || slen_full > 0xFF00) {
3804
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3805
0
                 ERR_R_INTERNAL_ERROR);
3806
0
        goto err;
3807
0
    }
3808
0
    senc = OPENSSL_malloc(slen_full);
3809
0
    if (senc == NULL) {
3810
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3811
0
                 SSL_F_CONSTRUCT_STATELESS_TICKET, ERR_R_MALLOC_FAILURE);
3812
0
        goto err;
3813
0
    }
3814
0
3815
0
    ctx = EVP_CIPHER_CTX_new();
3816
0
    hctx = HMAC_CTX_new();
3817
0
    if (ctx == NULL || hctx == NULL) {
3818
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3819
0
                 ERR_R_MALLOC_FAILURE);
3820
0
        goto err;
3821
0
    }
3822
0
3823
0
    p = senc;
3824
0
    if (!i2d_SSL_SESSION(s->session, &p)) {
3825
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3826
0
                 ERR_R_INTERNAL_ERROR);
3827
0
        goto err;
3828
0
    }
3829
0
3830
0
    /*
3831
0
     * create a fresh copy (not shared with other threads) to clean up
3832
0
     */
3833
0
    const_p = senc;
3834
0
    sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3835
0
    if (sess == NULL) {
3836
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3837
0
                 ERR_R_INTERNAL_ERROR);
3838
0
        goto err;
3839
0
    }
3840
0
3841
0
    slen = i2d_SSL_SESSION(sess, NULL);
3842
0
    if (slen == 0 || slen > slen_full) {
3843
0
        /* shouldn't ever happen */
3844
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3845
0
                 ERR_R_INTERNAL_ERROR);
3846
0
        SSL_SESSION_free(sess);
3847
0
        goto err;
3848
0
    }
3849
0
    p = senc;
3850
0
    if (!i2d_SSL_SESSION(sess, &p)) {
3851
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3852
0
                 ERR_R_INTERNAL_ERROR);
3853
0
        SSL_SESSION_free(sess);
3854
0
        goto err;
3855
0
    }
3856
0
    SSL_SESSION_free(sess);
3857
0
3858
0
    /*
3859
0
     * Initialize HMAC and cipher contexts. If callback present it does
3860
0
     * all the work otherwise use generated values from parent ctx.
3861
0
     */
3862
0
    if (tctx->ext.ticket_key_cb) {
3863
0
        /* if 0 is returned, write an empty ticket */
3864
0
        int ret = tctx->ext.ticket_key_cb(s, key_name, iv, ctx,
3865
0
                                             hctx, 1);
3866
0
3867
0
        if (ret == 0) {
3868
0
3869
0
            /* Put timeout and length */
3870
0
            if (!WPACKET_put_bytes_u32(pkt, 0)
3871
0
                    || !WPACKET_put_bytes_u16(pkt, 0)) {
3872
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3873
0
                         SSL_F_CONSTRUCT_STATELESS_TICKET,
3874
0
                         ERR_R_INTERNAL_ERROR);
3875
0
                goto err;
3876
0
            }
3877
0
            OPENSSL_free(senc);
3878
0
            EVP_CIPHER_CTX_free(ctx);
3879
0
            HMAC_CTX_free(hctx);
3880
0
            return 1;
3881
0
        }
3882
0
        if (ret < 0) {
3883
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3884
0
                     SSL_R_CALLBACK_FAILED);
3885
0
            goto err;
3886
0
        }
3887
0
        iv_len = EVP_CIPHER_CTX_iv_length(ctx);
3888
0
    } else {
3889
0
        const EVP_CIPHER *cipher = EVP_aes_256_cbc();
3890
0
3891
0
        iv_len = EVP_CIPHER_iv_length(cipher);
3892
0
        if (RAND_bytes(iv, iv_len) <= 0
3893
0
                || !EVP_EncryptInit_ex(ctx, cipher, NULL,
3894
0
                                       tctx->ext.secure->tick_aes_key, iv)
3895
0
                || !HMAC_Init_ex(hctx, tctx->ext.secure->tick_hmac_key,
3896
0
                                 sizeof(tctx->ext.secure->tick_hmac_key),
3897
0
                                 EVP_sha256(), NULL)) {
3898
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3899
0
                     ERR_R_INTERNAL_ERROR);
3900
0
            goto err;
3901
0
        }
3902
0
        memcpy(key_name, tctx->ext.tick_key_name,
3903
0
               sizeof(tctx->ext.tick_key_name));
3904
0
    }
3905
0
3906
0
    if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
3907
0
        /* SSLfatal() already called */
3908
0
        goto err;
3909
0
    }
3910
0
3911
0
    if (!WPACKET_get_total_written(pkt, &macoffset)
3912
0
               /* Output key name */
3913
0
            || !WPACKET_memcpy(pkt, key_name, sizeof(key_name))
3914
0
               /* output IV */
3915
0
            || !WPACKET_memcpy(pkt, iv, iv_len)
3916
0
            || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH,
3917
0
                                      &encdata1)
3918
0
               /* Encrypt session data */
3919
0
            || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen)
3920
0
            || !WPACKET_allocate_bytes(pkt, len, &encdata2)
3921
0
            || encdata1 != encdata2
3922
0
            || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal)
3923
0
            || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2)
3924
0
            || encdata1 + len != encdata2
3925
0
            || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH
3926
0
            || !WPACKET_get_total_written(pkt, &macendoffset)
3927
0
            || !HMAC_Update(hctx,
3928
0
                            (unsigned char *)s->init_buf->data + macoffset,
3929
0
                            macendoffset - macoffset)
3930
0
            || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1)
3931
0
            || !HMAC_Final(hctx, macdata1, &hlen)
3932
0
            || hlen > EVP_MAX_MD_SIZE
3933
0
            || !WPACKET_allocate_bytes(pkt, hlen, &macdata2)
3934
0
            || macdata1 != macdata2) {
3935
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3936
0
                 SSL_F_CONSTRUCT_STATELESS_TICKET, ERR_R_INTERNAL_ERROR);
3937
0
        goto err;
3938
0
    }
3939
0
3940
0
    /* Close the sub-packet created by create_ticket_prequel() */
3941
0
    if (!WPACKET_close(pkt)) {
3942
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATELESS_TICKET,
3943
0
                 ERR_R_INTERNAL_ERROR);
3944
0
        goto err;
3945
0
    }
3946
0
3947
0
    ok = 1;
3948
0
 err:
3949
0
    OPENSSL_free(senc);
3950
0
    EVP_CIPHER_CTX_free(ctx);
3951
0
    HMAC_CTX_free(hctx);
3952
0
    return ok;
3953
0
}
3954
3955
static int construct_stateful_ticket(SSL *s, WPACKET *pkt, uint32_t age_add,
3956
                                     unsigned char *tick_nonce)
3957
0
{
3958
0
    if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) {
3959
0
        /* SSLfatal() already called */
3960
0
        return 0;
3961
0
    }
3962
0
3963
0
    if (!WPACKET_memcpy(pkt, s->session->session_id,
3964
0
                        s->session->session_id_length)
3965
0
            || !WPACKET_close(pkt)) {
3966
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_STATEFUL_TICKET,
3967
0
                 ERR_R_INTERNAL_ERROR);
3968
0
        return 0;
3969
0
    }
3970
0
3971
0
    return 1;
3972
0
}
3973
3974
int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt)
3975
0
{
3976
0
    SSL_CTX *tctx = s->session_ctx;
3977
0
    unsigned char tick_nonce[TICKET_NONCE_SIZE];
3978
0
    union {
3979
0
        unsigned char age_add_c[sizeof(uint32_t)];
3980
0
        uint32_t age_add;
3981
0
    } age_add_u;
3982
0
3983
0
    age_add_u.age_add = 0;
3984
0
3985
0
    if (SSL_IS_TLS13(s)) {
3986
0
        size_t i, hashlen;
3987
0
        uint64_t nonce;
3988
0
        static const unsigned char nonce_label[] = "resumption";
3989
0
        const EVP_MD *md = ssl_handshake_md(s);
3990
0
        void (*cb) (const SSL *ssl, int type, int val) = NULL;
3991
0
        int hashleni = EVP_MD_size(md);
3992
0
3993
0
        /* Ensure cast to size_t is safe */
3994
0
        if (!ossl_assert(hashleni >= 0)) {
3995
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
3996
0
                     SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
3997
0
                     ERR_R_INTERNAL_ERROR);
3998
0
            goto err;
3999
0
        }
4000
0
        hashlen = (size_t)hashleni;
4001
0
4002
0
        if (s->info_callback != NULL)
4003
0
            cb = s->info_callback;
4004
0
        else if (s->ctx->info_callback != NULL)
4005
0
            cb = s->ctx->info_callback;
4006
0
4007
0
        if (cb != NULL) {
4008
0
            /*
4009
0
             * We don't start and stop the handshake in between each ticket when
4010
0
             * sending more than one - but it should appear that way to the info
4011
0
             * callback.
4012
0
             */
4013
0
            if (s->sent_tickets != 0) {
4014
0
                ossl_statem_set_in_init(s, 0);
4015
0
                cb(s, SSL_CB_HANDSHAKE_DONE, 1);
4016
0
                ossl_statem_set_in_init(s, 1);
4017
0
            }
4018
0
            cb(s, SSL_CB_HANDSHAKE_START, 1);
4019
0
        }
4020
0
        /*
4021
0
         * If we already sent one NewSessionTicket, or we resumed then
4022
0
         * s->session may already be in a cache and so we must not modify it.
4023
0
         * Instead we need to take a copy of it and modify that.
4024
0
         */
4025
0
        if (s->sent_tickets != 0 || s->hit) {
4026
0
            SSL_SESSION *new_sess = ssl_session_dup(s->session, 0);
4027
0
4028
0
            if (new_sess == NULL) {
4029
0
                /* SSLfatal already called */
4030
0
                goto err;
4031
0
            }
4032
0
4033
0
            SSL_SESSION_free(s->session);
4034
0
            s->session = new_sess;
4035
0
        }
4036
0
4037
0
        if (!ssl_generate_session_id(s, s->session)) {
4038
0
            /* SSLfatal() already called */
4039
0
            goto err;
4040
0
        }
4041
0
        if (RAND_bytes(age_add_u.age_add_c, sizeof(age_add_u)) <= 0) {
4042
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR,
4043
0
                     SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
4044
0
                     ERR_R_INTERNAL_ERROR);
4045
0
            goto err;
4046
0
        }
4047
0
        s->session->ext.tick_age_add = age_add_u.age_add;
4048
0
4049
0
        nonce = s->next_ticket_nonce;
4050
0
        for (i = TICKET_NONCE_SIZE; i > 0; i--) {
4051
0
            tick_nonce[i - 1] = (unsigned char)(nonce & 0xff);
4052
0
            nonce >>= 8;
4053
0
        }
4054
0
4055
0
        if (!tls13_hkdf_expand(s, md, s->resumption_master_secret,
4056
0
                               nonce_label,
4057
0
                               sizeof(nonce_label) - 1,
4058
0
                               tick_nonce,
4059
0
                               TICKET_NONCE_SIZE,
4060
0
                               s->session->master_key,
4061
0
                               hashlen)) {
4062
0
            /* SSLfatal() already called */
4063
0
            goto err;
4064
0
        }
4065
0
        s->session->master_key_length = hashlen;
4066
0
4067
0
        s->session->time = (long)time(NULL);
4068
0
        if (s->s3->alpn_selected != NULL) {
4069
0
            OPENSSL_free(s->session->ext.alpn_selected);
4070
0
            s->session->ext.alpn_selected =
4071
0
                OPENSSL_memdup(s->s3->alpn_selected, s->s3->alpn_selected_len);
4072
0
            if (s->session->ext.alpn_selected == NULL) {
4073
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR,
4074
0
                         SSL_F_TLS_CONSTRUCT_NEW_SESSION_TICKET,
4075
0
                         ERR_R_MALLOC_FAILURE);
4076
0
                goto err;
4077
0
            }
4078
0
            s->session->ext.alpn_selected_len = s->s3->alpn_selected_len;
4079
0
        }
4080
0
        s->session->ext.max_early_data = s->max_early_data;
4081
0
    }
4082
0
4083
0
    if (tctx->generate_ticket_cb != NULL &&
4084
0
        tctx->generate_ticket_cb(s, tctx->ticket_cb_data) == 0)
4085
0
        goto err;
4086
0
4087
0
    /*
4088
0
     * If we are using anti-replay protection then we behave as if
4089
0
     * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there
4090
0
     * is no point in using full stateless tickets.
4091
0
     */
4092
0
    if (SSL_IS_TLS13(s)
4093
0
            && ((s->options & SSL_OP_NO_TICKET) != 0
4094
0
                || (s->max_early_data > 0
4095
0
                    && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))) {
4096
0
        if (!construct_stateful_ticket(s, pkt, age_add_u.age_add, tick_nonce)) {
4097
0
            /* SSLfatal() already called */
4098
0
            goto err;
4099
0
        }
4100
0
    } else if (!construct_stateless_ticket(s, pkt, age_add_u.age_add,
4101
0
                                           tick_nonce)) {
4102
0
        /* SSLfatal() already called */
4103
0
        goto err;
4104
0
    }
4105
0
4106
0
    if (SSL_IS_TLS13(s)) {
4107
0
        if (!tls_construct_extensions(s, pkt,
4108
0
                                      SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
4109
0
                                      NULL, 0)) {
4110
0
            /* SSLfatal() already called */
4111
0
            goto err;
4112
0
        }
4113
0
        /*
4114
0
         * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets|
4115
0
         * gets reset to 0 if we send more tickets following a post-handshake
4116
0
         * auth, but |next_ticket_nonce| does not.
4117
0
         */
4118
0
        s->sent_tickets++;
4119
0
        s->next_ticket_nonce++;
4120
0
        ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
4121
0
    }
4122
0
4123
0
    return 1;
4124
0
 err:
4125
0
    return 0;
4126
0
}
4127
4128
/*
4129
 * In TLSv1.3 this is called from the extensions code, otherwise it is used to
4130
 * create a separate message. Returns 1 on success or 0 on failure.
4131
 */
4132
int tls_construct_cert_status_body(SSL *s, WPACKET *pkt)
4133
0
{
4134
0
    if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type)
4135
0
            || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp,
4136
0
                                       s->ext.ocsp.resp_len)) {
4137
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_STATUS_BODY,
4138
0
                 ERR_R_INTERNAL_ERROR);
4139
0
        return 0;
4140
0
    }
4141
0
4142
0
    return 1;
4143
0
}
4144
4145
int tls_construct_cert_status(SSL *s, WPACKET *pkt)
4146
0
{
4147
0
    if (!tls_construct_cert_status_body(s, pkt)) {
4148
0
        /* SSLfatal() already called */
4149
0
        return 0;
4150
0
    }
4151
0
4152
0
    return 1;
4153
0
}
4154
4155
#ifndef OPENSSL_NO_NEXTPROTONEG
4156
/*
4157
 * tls_process_next_proto reads a Next Protocol Negotiation handshake message.
4158
 * It sets the next_proto member in s if found
4159
 */
4160
MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt)
4161
0
{
4162
0
    PACKET next_proto, padding;
4163
0
    size_t next_proto_len;
4164
0
4165
0
    /*-
4166
0
     * The payload looks like:
4167
0
     *   uint8 proto_len;
4168
0
     *   uint8 proto[proto_len];
4169
0
     *   uint8 padding_len;
4170
0
     *   uint8 padding[padding_len];
4171
0
     */
4172
0
    if (!PACKET_get_length_prefixed_1(pkt, &next_proto)
4173
0
        || !PACKET_get_length_prefixed_1(pkt, &padding)
4174
0
        || PACKET_remaining(pkt) > 0) {
4175
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_NEXT_PROTO,
4176
0
                 SSL_R_LENGTH_MISMATCH);
4177
0
        return MSG_PROCESS_ERROR;
4178
0
    }
4179
0
4180
0
    if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) {
4181
0
        s->ext.npn_len = 0;
4182
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_NEXT_PROTO,
4183
0
                 ERR_R_INTERNAL_ERROR);
4184
0
        return MSG_PROCESS_ERROR;
4185
0
    }
4186
0
4187
0
    s->ext.npn_len = (unsigned char)next_proto_len;
4188
0
4189
0
    return MSG_PROCESS_CONTINUE_READING;
4190
0
}
4191
#endif
4192
4193
static int tls_construct_encrypted_extensions(SSL *s, WPACKET *pkt)
4194
0
{
4195
0
    if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
4196
0
                                  NULL, 0)) {
4197
0
        /* SSLfatal() already called */
4198
0
        return 0;
4199
0
    }
4200
0
4201
0
    return 1;
4202
0
}
4203
4204
MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL *s, PACKET *pkt)
4205
0
{
4206
0
    if (PACKET_remaining(pkt) != 0) {
4207
0
        SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
4208
0
                 SSL_R_LENGTH_MISMATCH);
4209
0
        return MSG_PROCESS_ERROR;
4210
0
    }
4211
0
4212
0
    if (s->early_data_state != SSL_EARLY_DATA_READING
4213
0
            && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) {
4214
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
4215
0
                 ERR_R_INTERNAL_ERROR);
4216
0
        return MSG_PROCESS_ERROR;
4217
0
    }
4218
0
4219
0
    /*
4220
0
     * EndOfEarlyData signals a key change so the end of the message must be on
4221
0
     * a record boundary.
4222
0
     */
4223
0
    if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
4224
0
        SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
4225
0
                 SSL_F_TLS_PROCESS_END_OF_EARLY_DATA,
4226
0
                 SSL_R_NOT_ON_RECORD_BOUNDARY);
4227
0
        return MSG_PROCESS_ERROR;
4228
0
    }
4229
0
4230
0
    s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
4231
0
    if (!s->method->ssl3_enc->change_cipher_state(s,
4232
0
                SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) {
4233
0
        /* SSLfatal() already called */
4234
0
        return MSG_PROCESS_ERROR;
4235
0
    }
4236
0
4237
0
    return MSG_PROCESS_CONTINUE_READING;
4238
0
}