Coverage Report

Created: 2022-11-30 06:20

/src/openssl/ssl/s3_srvr.c
Line
Count
Source (jump to first uncovered line)
1
/* ssl/s3_srvr.c */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3
 * All rights reserved.
4
 *
5
 * This package is an SSL implementation written
6
 * by Eric Young (eay@cryptsoft.com).
7
 * The implementation was written so as to conform with Netscapes SSL.
8
 *
9
 * This library is free for commercial and non-commercial use as long as
10
 * the following conditions are aheared to.  The following conditions
11
 * apply to all code found in this distribution, be it the RC4, RSA,
12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13
 * included with this distribution is covered by the same copyright terms
14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15
 *
16
 * Copyright remains Eric Young's, and as such any Copyright notices in
17
 * the code are not to be removed.
18
 * If this package is used in a product, Eric Young should be given attribution
19
 * as the author of the parts of the library used.
20
 * This can be in the form of a textual message at program startup or
21
 * in documentation (online or textual) provided with the package.
22
 *
23
 * Redistribution and use in source and binary forms, with or without
24
 * modification, are permitted provided that the following conditions
25
 * are met:
26
 * 1. Redistributions of source code must retain the copyright
27
 *    notice, this list of conditions and the following disclaimer.
28
 * 2. Redistributions in binary form must reproduce the above copyright
29
 *    notice, this list of conditions and the following disclaimer in the
30
 *    documentation and/or other materials provided with the distribution.
31
 * 3. All advertising materials mentioning features or use of this software
32
 *    must display the following acknowledgement:
33
 *    "This product includes cryptographic software written by
34
 *     Eric Young (eay@cryptsoft.com)"
35
 *    The word 'cryptographic' can be left out if the rouines from the library
36
 *    being used are not cryptographic related :-).
37
 * 4. If you include any Windows specific code (or a derivative thereof) from
38
 *    the apps directory (application code) you must include an acknowledgement:
39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
 * SUCH DAMAGE.
52
 *
53
 * The licence and distribution terms for any publically available version or
54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55
 * copied and put under another distribution licence
56
 * [including the GNU Public Licence.]
57
 */
58
/* ====================================================================
59
 * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
60
 *
61
 * Redistribution and use in source and binary forms, with or without
62
 * modification, are permitted provided that the following conditions
63
 * are met:
64
 *
65
 * 1. Redistributions of source code must retain the above copyright
66
 *    notice, this list of conditions and the following disclaimer.
67
 *
68
 * 2. Redistributions in binary form must reproduce the above copyright
69
 *    notice, this list of conditions and the following disclaimer in
70
 *    the documentation and/or other materials provided with the
71
 *    distribution.
72
 *
73
 * 3. All advertising materials mentioning features or use of this
74
 *    software must display the following acknowledgment:
75
 *    "This product includes software developed by the OpenSSL Project
76
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77
 *
78
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79
 *    endorse or promote products derived from this software without
80
 *    prior written permission. For written permission, please contact
81
 *    openssl-core@openssl.org.
82
 *
83
 * 5. Products derived from this software may not be called "OpenSSL"
84
 *    nor may "OpenSSL" appear in their names without prior written
85
 *    permission of the OpenSSL Project.
86
 *
87
 * 6. Redistributions of any form whatsoever must retain the following
88
 *    acknowledgment:
89
 *    "This product includes software developed by the OpenSSL Project
90
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91
 *
92
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103
 * OF THE POSSIBILITY OF SUCH DAMAGE.
104
 * ====================================================================
105
 *
106
 * This product includes cryptographic software written by Eric Young
107
 * (eay@cryptsoft.com).  This product includes software written by Tim
108
 * Hudson (tjh@cryptsoft.com).
109
 *
110
 */
111
/* ====================================================================
112
 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113
 *
114
 * Portions of the attached software ("Contribution") are developed by
115
 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
116
 *
117
 * The Contribution is licensed pursuant to the OpenSSL open source
118
 * license provided above.
119
 *
120
 * ECC cipher suite support in OpenSSL originally written by
121
 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
122
 *
123
 */
124
/* ====================================================================
125
 * Copyright 2005 Nokia. All rights reserved.
126
 *
127
 * The portions of the attached software ("Contribution") is developed by
128
 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
129
 * license.
130
 *
131
 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132
 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133
 * support (see RFC 4279) to OpenSSL.
134
 *
135
 * No patent licenses or other rights except those expressly stated in
136
 * the OpenSSL open source license shall be deemed granted or received
137
 * expressly, by implication, estoppel, or otherwise.
138
 *
139
 * No assurances are provided by Nokia that the Contribution does not
140
 * infringe the patent or other intellectual property rights of any third
141
 * party or that the license provides you with all the necessary rights
142
 * to make use of the Contribution.
143
 *
144
 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145
 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146
 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147
 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
148
 * OTHERWISE.
149
 */
150
151
#define REUSE_CIPHER_BUG
152
#define NETSCAPE_HANG_BUG
153
154
#include <stdio.h>
155
#include "ssl_locl.h"
156
#include "kssl_lcl.h"
157
#include "../crypto/constant_time_locl.h"
158
#include <openssl/buffer.h>
159
#include <openssl/rand.h>
160
#include <openssl/objects.h>
161
#include <openssl/evp.h>
162
#include <openssl/hmac.h>
163
#include <openssl/x509.h>
164
#ifndef OPENSSL_NO_DH
165
# include <openssl/dh.h>
166
#endif
167
#include <openssl/bn.h>
168
#ifndef OPENSSL_NO_KRB5
169
# include <openssl/krb5_asn.h>
170
#endif
171
#include <openssl/md5.h>
172
173
#ifndef OPENSSL_NO_SSL3_METHOD
174
static const SSL_METHOD *ssl3_get_server_method(int ver);
175
176
static const SSL_METHOD *ssl3_get_server_method(int ver)
177
0
{
178
0
    if (ver == SSL3_VERSION)
179
0
        return (SSLv3_server_method());
180
0
    else
181
0
        return (NULL);
182
0
}
183
184
IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
185
                         ssl3_accept,
186
                         ssl_undefined_function, ssl3_get_server_method)
187
#endif
188
#ifndef OPENSSL_NO_SRP
189
static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
190
0
{
191
0
    int ret = SSL_ERROR_NONE;
192
193
0
    *al = SSL_AD_UNRECOGNIZED_NAME;
194
195
0
    if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
196
0
        (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
197
0
        if (s->srp_ctx.login == NULL) {
198
            /*
199
             * RFC 5054 says SHOULD reject, we do so if There is no srp
200
             * login name
201
             */
202
0
            ret = SSL3_AL_FATAL;
203
0
            *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
204
0
        } else {
205
0
            ret = SSL_srp_server_param_with_username(s, al);
206
0
        }
207
0
    }
208
0
    return ret;
209
0
}
210
#endif
211
212
int ssl3_accept(SSL *s)
213
0
{
214
0
    BUF_MEM *buf;
215
0
    unsigned long alg_k, Time = (unsigned long)time(NULL);
216
0
    void (*cb) (const SSL *ssl, int type, int val) = NULL;
217
0
    int ret = -1;
218
0
    int new_state, state, skip = 0;
219
220
0
    RAND_add(&Time, sizeof(Time), 0);
221
0
    ERR_clear_error();
222
0
    clear_sys_error();
223
224
0
    if (s->info_callback != NULL)
225
0
        cb = s->info_callback;
226
0
    else if (s->ctx->info_callback != NULL)
227
0
        cb = s->ctx->info_callback;
228
229
    /* init things to blank */
230
0
    s->in_handshake++;
231
0
    if (!SSL_in_init(s) || SSL_in_before(s))
232
0
        SSL_clear(s);
233
234
0
    if (s->cert == NULL) {
235
0
        SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_NO_CERTIFICATE_SET);
236
0
        return (-1);
237
0
    }
238
0
#ifndef OPENSSL_NO_HEARTBEATS
239
    /*
240
     * If we're awaiting a HeartbeatResponse, pretend we already got and
241
     * don't await it anymore, because Heartbeats don't make sense during
242
     * handshakes anyway.
243
     */
244
0
    if (s->tlsext_hb_pending) {
245
0
        s->tlsext_hb_pending = 0;
246
0
        s->tlsext_hb_seq++;
247
0
    }
248
0
#endif
249
250
0
    for (;;) {
251
0
        state = s->state;
252
253
0
        switch (s->state) {
254
0
        case SSL_ST_RENEGOTIATE:
255
0
            s->renegotiate = 1;
256
            /* s->state=SSL_ST_ACCEPT; */
257
258
0
        case SSL_ST_BEFORE:
259
0
        case SSL_ST_ACCEPT:
260
0
        case SSL_ST_BEFORE | SSL_ST_ACCEPT:
261
0
        case SSL_ST_OK | SSL_ST_ACCEPT:
262
263
0
            s->server = 1;
264
0
            if (cb != NULL)
265
0
                cb(s, SSL_CB_HANDSHAKE_START, 1);
266
267
0
            if ((s->version >> 8) != 3) {
268
0
                SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
269
0
                s->state = SSL_ST_ERR;
270
0
                return -1;
271
0
            }
272
0
            s->type = SSL_ST_ACCEPT;
273
274
0
            if (s->init_buf == NULL) {
275
0
                if ((buf = BUF_MEM_new()) == NULL) {
276
0
                    ret = -1;
277
0
                    s->state = SSL_ST_ERR;
278
0
                    goto end;
279
0
                }
280
0
                if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
281
0
                    BUF_MEM_free(buf);
282
0
                    ret = -1;
283
0
                    s->state = SSL_ST_ERR;
284
0
                    goto end;
285
0
                }
286
0
                s->init_buf = buf;
287
0
            }
288
289
0
            if (!ssl3_setup_buffers(s)) {
290
0
                ret = -1;
291
0
                s->state = SSL_ST_ERR;
292
0
                goto end;
293
0
            }
294
295
0
            s->init_num = 0;
296
0
            s->s3->flags &= ~TLS1_FLAGS_SKIP_CERT_VERIFY;
297
0
            s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
298
            /*
299
             * Should have been reset by ssl3_get_finished, too.
300
             */
301
0
            s->s3->change_cipher_spec = 0;
302
303
0
            if (s->state != SSL_ST_RENEGOTIATE) {
304
                /*
305
                 * Ok, we now need to push on a buffering BIO so that the
306
                 * output is sent in a way that TCP likes :-)
307
                 */
308
0
                if (!ssl_init_wbio_buffer(s, 1)) {
309
0
                    ret = -1;
310
0
                    s->state = SSL_ST_ERR;
311
0
                    goto end;
312
0
                }
313
314
0
                if (!ssl3_init_finished_mac(s)) {
315
0
                    ret = -1;
316
0
                    s->state = SSL_ST_ERR;
317
0
                    goto end;
318
0
                }
319
320
0
                s->state = SSL3_ST_SR_CLNT_HELLO_A;
321
0
                s->ctx->stats.sess_accept++;
322
0
            } else if (!s->s3->send_connection_binding &&
323
0
                       !(s->options &
324
0
                         SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
325
                /*
326
                 * Server attempting to renegotiate with client that doesn't
327
                 * support secure renegotiation.
328
                 */
329
0
                SSLerr(SSL_F_SSL3_ACCEPT,
330
0
                       SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
331
0
                ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
332
0
                ret = -1;
333
0
                s->state = SSL_ST_ERR;
334
0
                goto end;
335
0
            } else {
336
                /*
337
                 * s->state == SSL_ST_RENEGOTIATE, we will just send a
338
                 * HelloRequest
339
                 */
340
0
                s->ctx->stats.sess_accept_renegotiate++;
341
0
                s->state = SSL3_ST_SW_HELLO_REQ_A;
342
0
            }
343
0
            break;
344
345
0
        case SSL3_ST_SW_HELLO_REQ_A:
346
0
        case SSL3_ST_SW_HELLO_REQ_B:
347
348
0
            s->shutdown = 0;
349
0
            ret = ssl3_send_hello_request(s);
350
0
            if (ret <= 0)
351
0
                goto end;
352
0
            s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
353
0
            s->state = SSL3_ST_SW_FLUSH;
354
0
            s->init_num = 0;
355
356
0
            if (!ssl3_init_finished_mac(s)) {
357
0
                ret = -1;
358
0
                s->state = SSL_ST_ERR;
359
0
                goto end;
360
0
            }
361
0
            break;
362
363
0
        case SSL3_ST_SW_HELLO_REQ_C:
364
0
            s->state = SSL_ST_OK;
365
0
            break;
366
367
0
        case SSL3_ST_SR_CLNT_HELLO_A:
368
0
        case SSL3_ST_SR_CLNT_HELLO_B:
369
0
        case SSL3_ST_SR_CLNT_HELLO_C:
370
371
0
            s->shutdown = 0;
372
0
            ret = ssl3_get_client_hello(s);
373
0
            if (ret <= 0)
374
0
                goto end;
375
0
#ifndef OPENSSL_NO_SRP
376
0
            s->state = SSL3_ST_SR_CLNT_HELLO_D;
377
0
        case SSL3_ST_SR_CLNT_HELLO_D:
378
0
            {
379
0
                int al;
380
0
                if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
381
                    /*
382
                     * callback indicates firther work to be done
383
                     */
384
0
                    s->rwstate = SSL_X509_LOOKUP;
385
0
                    goto end;
386
0
                }
387
0
                if (ret != SSL_ERROR_NONE) {
388
0
                    ssl3_send_alert(s, SSL3_AL_FATAL, al);
389
                    /*
390
                     * This is not really an error but the only means to for
391
                     * a client to detect whether srp is supported.
392
                     */
393
0
                    if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
394
0
                        SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_CLIENTHELLO_TLSEXT);
395
0
                    ret = -1;
396
0
                    s->state = SSL_ST_ERR;
397
0
                    goto end;
398
0
                }
399
0
            }
400
0
#endif
401
402
0
            s->renegotiate = 2;
403
0
            s->state = SSL3_ST_SW_SRVR_HELLO_A;
404
0
            s->init_num = 0;
405
0
            break;
406
407
0
        case SSL3_ST_SW_SRVR_HELLO_A:
408
0
        case SSL3_ST_SW_SRVR_HELLO_B:
409
0
            ret = ssl3_send_server_hello(s);
410
0
            if (ret <= 0)
411
0
                goto end;
412
0
#ifndef OPENSSL_NO_TLSEXT
413
0
            if (s->hit) {
414
0
                if (s->tlsext_ticket_expected)
415
0
                    s->state = SSL3_ST_SW_SESSION_TICKET_A;
416
0
                else
417
0
                    s->state = SSL3_ST_SW_CHANGE_A;
418
0
            }
419
#else
420
            if (s->hit)
421
                s->state = SSL3_ST_SW_CHANGE_A;
422
#endif
423
0
            else
424
0
                s->state = SSL3_ST_SW_CERT_A;
425
0
            s->init_num = 0;
426
0
            break;
427
428
0
        case SSL3_ST_SW_CERT_A:
429
0
        case SSL3_ST_SW_CERT_B:
430
            /* Check if it is anon DH or anon ECDH, */
431
            /* normal PSK or KRB5 or SRP */
432
0
            if (!
433
0
                (s->s3->tmp.
434
0
                 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aKRB5 |
435
0
                                               SSL_aSRP))
436
0
&& !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
437
0
                ret = ssl3_send_server_certificate(s);
438
0
                if (ret <= 0)
439
0
                    goto end;
440
0
#ifndef OPENSSL_NO_TLSEXT
441
0
                if (s->tlsext_status_expected)
442
0
                    s->state = SSL3_ST_SW_CERT_STATUS_A;
443
0
                else
444
0
                    s->state = SSL3_ST_SW_KEY_EXCH_A;
445
0
            } else {
446
0
                skip = 1;
447
0
                s->state = SSL3_ST_SW_KEY_EXCH_A;
448
0
            }
449
#else
450
            } else
451
                skip = 1;
452
453
            s->state = SSL3_ST_SW_KEY_EXCH_A;
454
#endif
455
0
            s->init_num = 0;
456
0
            break;
457
458
0
        case SSL3_ST_SW_KEY_EXCH_A:
459
0
        case SSL3_ST_SW_KEY_EXCH_B:
460
0
            alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
461
462
            /*
463
             * clear this, it may get reset by
464
             * send_server_key_exchange
465
             */
466
0
            s->s3->tmp.use_rsa_tmp = 0;
467
468
            /*
469
             * only send if a DH key exchange, fortezza or RSA but we have a
470
             * sign only certificate PSK: may send PSK identity hints For
471
             * ECC ciphersuites, we send a serverKeyExchange message only if
472
             * the cipher suite is either ECDH-anon or ECDHE. In other cases,
473
             * the server certificate contains the server's public key for
474
             * key exchange.
475
             */
476
0
            if (0
477
                /*
478
                 * PSK: send ServerKeyExchange if PSK identity hint if
479
                 * provided
480
                 */
481
0
#ifndef OPENSSL_NO_PSK
482
0
                || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
483
0
#endif
484
0
#ifndef OPENSSL_NO_SRP
485
                /* SRP: send ServerKeyExchange */
486
0
                || (alg_k & SSL_kSRP)
487
0
#endif
488
0
                || (alg_k & SSL_kEDH)
489
0
                || (alg_k & SSL_kEECDH)
490
0
                || ((alg_k & SSL_kRSA)
491
0
                    && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
492
0
                        || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
493
0
                            && EVP_PKEY_size(s->cert->pkeys
494
0
                                             [SSL_PKEY_RSA_ENC].privatekey) *
495
0
                            8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
496
0
                        )
497
0
                    )
498
0
                )
499
0
                ) {
500
0
                ret = ssl3_send_server_key_exchange(s);
501
0
                if (ret <= 0)
502
0
                    goto end;
503
0
            } else
504
0
                skip = 1;
505
506
0
            s->state = SSL3_ST_SW_CERT_REQ_A;
507
0
            s->init_num = 0;
508
0
            break;
509
510
0
        case SSL3_ST_SW_CERT_REQ_A:
511
0
        case SSL3_ST_SW_CERT_REQ_B:
512
0
            if (                /* don't request cert unless asked for it: */
513
0
                   !(s->verify_mode & SSL_VERIFY_PEER) ||
514
                   /*
515
                    * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
516
                    * during re-negotiation:
517
                    */
518
0
                   (s->s3->tmp.finish_md_len != 0 &&
519
0
                    (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
520
                   /*
521
                    * never request cert in anonymous ciphersuites (see
522
                    * section "Certificate request" in SSL 3 drafts and in
523
                    * RFC 2246):
524
                    */
525
0
                   ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
526
                    /*
527
                     * ... except when the application insists on
528
                     * verification (against the specs, but s3_clnt.c accepts
529
                     * this for SSL 3)
530
                     */
531
0
                    !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
532
                   /*
533
                    * never request cert in Kerberos ciphersuites
534
                    */
535
0
                   (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) ||
536
                   /* don't request certificate for SRP auth */
537
0
                   (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
538
                   /*
539
                    * With normal PSK Certificates and Certificate Requests
540
                    * are omitted
541
                    */
542
0
                   || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
543
                /* no cert request */
544
0
                skip = 1;
545
0
                s->s3->tmp.cert_request = 0;
546
0
                s->state = SSL3_ST_SW_SRVR_DONE_A;
547
0
                if (s->s3->handshake_buffer) {
548
0
                    if (!ssl3_digest_cached_records(s)) {
549
0
                        s->state = SSL_ST_ERR;
550
0
                        return -1;
551
0
                    }
552
0
                }
553
0
            } else {
554
0
                s->s3->tmp.cert_request = 1;
555
0
                ret = ssl3_send_certificate_request(s);
556
0
                if (ret <= 0)
557
0
                    goto end;
558
#ifndef NETSCAPE_HANG_BUG
559
                s->state = SSL3_ST_SW_SRVR_DONE_A;
560
#else
561
0
                s->state = SSL3_ST_SW_FLUSH;
562
0
                s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
563
0
#endif
564
0
                s->init_num = 0;
565
0
            }
566
0
            break;
567
568
0
        case SSL3_ST_SW_SRVR_DONE_A:
569
0
        case SSL3_ST_SW_SRVR_DONE_B:
570
0
            ret = ssl3_send_server_done(s);
571
0
            if (ret <= 0)
572
0
                goto end;
573
0
            s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
574
0
            s->state = SSL3_ST_SW_FLUSH;
575
0
            s->init_num = 0;
576
0
            break;
577
578
0
        case SSL3_ST_SW_FLUSH:
579
580
            /*
581
             * This code originally checked to see if any data was pending
582
             * using BIO_CTRL_INFO and then flushed. This caused problems as
583
             * documented in PR#1939. The proposed fix doesn't completely
584
             * resolve this issue as buggy implementations of
585
             * BIO_CTRL_PENDING still exist. So instead we just flush
586
             * unconditionally.
587
             */
588
589
0
            s->rwstate = SSL_WRITING;
590
0
            if (BIO_flush(s->wbio) <= 0) {
591
0
                ret = -1;
592
0
                goto end;
593
0
            }
594
0
            s->rwstate = SSL_NOTHING;
595
596
0
            s->state = s->s3->tmp.next_state;
597
0
            break;
598
599
0
        case SSL3_ST_SR_CERT_A:
600
0
        case SSL3_ST_SR_CERT_B:
601
0
            if (s->s3->tmp.cert_request) {
602
0
                ret = ssl3_get_client_certificate(s);
603
0
                if (ret <= 0)
604
0
                    goto end;
605
0
            }
606
0
            s->init_num = 0;
607
0
            s->state = SSL3_ST_SR_KEY_EXCH_A;
608
0
            break;
609
610
0
        case SSL3_ST_SR_KEY_EXCH_A:
611
0
        case SSL3_ST_SR_KEY_EXCH_B:
612
0
            ret = ssl3_get_client_key_exchange(s);
613
0
            if (ret <= 0)
614
0
                goto end;
615
0
            if (ret == 2) {
616
                /*
617
                 * For the ECDH ciphersuites when the client sends its ECDH
618
                 * pub key in a certificate, the CertificateVerify message is
619
                 * not sent. Also for GOST ciphersuites when the client uses
620
                 * its key from the certificate for key exchange.
621
                 */
622
#if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
623
                s->state = SSL3_ST_SR_FINISHED_A;
624
#else
625
0
                if (s->s3->next_proto_neg_seen)
626
0
                    s->state = SSL3_ST_SR_NEXT_PROTO_A;
627
0
                else
628
0
                    s->state = SSL3_ST_SR_FINISHED_A;
629
0
#endif
630
0
                s->init_num = 0;
631
0
            } else if (SSL_USE_SIGALGS(s)) {
632
0
                s->state = SSL3_ST_SR_CERT_VRFY_A;
633
0
                s->init_num = 0;
634
0
                if (!s->session->peer)
635
0
                    break;
636
                /*
637
                 * For sigalgs freeze the handshake buffer at this point and
638
                 * digest cached records.
639
                 */
640
0
                if (!s->s3->handshake_buffer) {
641
0
                    SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
642
0
                    s->state = SSL_ST_ERR;
643
0
                    return -1;
644
0
                }
645
0
                s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE;
646
0
                if (!ssl3_digest_cached_records(s)) {
647
0
                    s->state = SSL_ST_ERR;
648
0
                    return -1;
649
0
                }
650
0
            } else {
651
0
                int offset = 0;
652
0
                int dgst_num;
653
654
0
                s->state = SSL3_ST_SR_CERT_VRFY_A;
655
0
                s->init_num = 0;
656
657
                /*
658
                 * We need to get hashes here so if there is a client cert,
659
                 * it can be verified FIXME - digest processing for
660
                 * CertificateVerify should be generalized. But it is next
661
                 * step
662
                 */
663
0
                if (s->s3->handshake_buffer) {
664
0
                    if (!ssl3_digest_cached_records(s)) {
665
0
                        s->state = SSL_ST_ERR;
666
0
                        return -1;
667
0
                    }
668
0
                }
669
0
                for (dgst_num = 0; dgst_num < SSL_MAX_DIGEST; dgst_num++)
670
0
                    if (s->s3->handshake_dgst[dgst_num]) {
671
0
                        int dgst_size;
672
673
0
                        s->method->ssl3_enc->cert_verify_mac(s,
674
0
                                                             EVP_MD_CTX_type
675
0
                                                             (s->
676
0
                                                              s3->handshake_dgst
677
0
                                                              [dgst_num]),
678
0
                                                             &(s->s3->
679
0
                                                               tmp.cert_verify_md
680
0
                                                               [offset]));
681
0
                        dgst_size =
682
0
                            EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
683
0
                        if (dgst_size < 0) {
684
0
                            s->state = SSL_ST_ERR;
685
0
                            ret = -1;
686
0
                            goto end;
687
0
                        }
688
0
                        offset += dgst_size;
689
0
                    }
690
0
            }
691
0
            break;
692
693
0
        case SSL3_ST_SR_CERT_VRFY_A:
694
0
        case SSL3_ST_SR_CERT_VRFY_B:
695
0
            ret = ssl3_get_cert_verify(s);
696
0
            if (ret <= 0)
697
0
                goto end;
698
699
#if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
700
            s->state = SSL3_ST_SR_FINISHED_A;
701
#else
702
0
            if (s->s3->next_proto_neg_seen)
703
0
                s->state = SSL3_ST_SR_NEXT_PROTO_A;
704
0
            else
705
0
                s->state = SSL3_ST_SR_FINISHED_A;
706
0
#endif
707
0
            s->init_num = 0;
708
0
            break;
709
710
0
#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
711
0
        case SSL3_ST_SR_NEXT_PROTO_A:
712
0
        case SSL3_ST_SR_NEXT_PROTO_B:
713
            /*
714
             * Enable CCS for NPN. Receiving a CCS clears the flag, so make
715
             * sure not to re-enable it to ban duplicates. This *should* be the
716
             * first time we have received one - but we check anyway to be
717
             * cautious.
718
             * s->s3->change_cipher_spec is set when a CCS is
719
             * processed in s3_pkt.c, and remains set until
720
             * the client's Finished message is read.
721
             */
722
0
            if (!s->s3->change_cipher_spec)
723
0
                s->s3->flags |= SSL3_FLAGS_CCS_OK;
724
725
0
            ret = ssl3_get_next_proto(s);
726
0
            if (ret <= 0)
727
0
                goto end;
728
0
            s->init_num = 0;
729
0
            s->state = SSL3_ST_SR_FINISHED_A;
730
0
            break;
731
0
#endif
732
733
0
        case SSL3_ST_SR_FINISHED_A:
734
0
        case SSL3_ST_SR_FINISHED_B:
735
            /*
736
             * Enable CCS for handshakes without NPN. In NPN the CCS flag has
737
             * already been set. Receiving a CCS clears the flag, so make
738
             * sure not to re-enable it to ban duplicates.
739
             * s->s3->change_cipher_spec is set when a CCS is
740
             * processed in s3_pkt.c, and remains set until
741
             * the client's Finished message is read.
742
             */
743
0
            if (!s->s3->change_cipher_spec)
744
0
                s->s3->flags |= SSL3_FLAGS_CCS_OK;
745
0
            ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
746
0
                                    SSL3_ST_SR_FINISHED_B);
747
0
            if (ret <= 0)
748
0
                goto end;
749
0
            if (s->hit)
750
0
                s->state = SSL_ST_OK;
751
0
#ifndef OPENSSL_NO_TLSEXT
752
0
            else if (s->tlsext_ticket_expected)
753
0
                s->state = SSL3_ST_SW_SESSION_TICKET_A;
754
0
#endif
755
0
            else
756
0
                s->state = SSL3_ST_SW_CHANGE_A;
757
0
            s->init_num = 0;
758
0
            break;
759
760
0
#ifndef OPENSSL_NO_TLSEXT
761
0
        case SSL3_ST_SW_SESSION_TICKET_A:
762
0
        case SSL3_ST_SW_SESSION_TICKET_B:
763
0
            ret = ssl3_send_newsession_ticket(s);
764
0
            if (ret <= 0)
765
0
                goto end;
766
0
            s->state = SSL3_ST_SW_CHANGE_A;
767
0
            s->init_num = 0;
768
0
            break;
769
770
0
        case SSL3_ST_SW_CERT_STATUS_A:
771
0
        case SSL3_ST_SW_CERT_STATUS_B:
772
0
            ret = ssl3_send_cert_status(s);
773
0
            if (ret <= 0)
774
0
                goto end;
775
0
            s->state = SSL3_ST_SW_KEY_EXCH_A;
776
0
            s->init_num = 0;
777
0
            break;
778
779
0
#endif
780
781
0
        case SSL3_ST_SW_CHANGE_A:
782
0
        case SSL3_ST_SW_CHANGE_B:
783
784
0
            s->session->cipher = s->s3->tmp.new_cipher;
785
0
            if (!s->method->ssl3_enc->setup_key_block(s)) {
786
0
                ret = -1;
787
0
                s->state = SSL_ST_ERR;
788
0
                goto end;
789
0
            }
790
791
0
            ret = ssl3_send_change_cipher_spec(s,
792
0
                                               SSL3_ST_SW_CHANGE_A,
793
0
                                               SSL3_ST_SW_CHANGE_B);
794
795
0
            if (ret <= 0)
796
0
                goto end;
797
0
            s->state = SSL3_ST_SW_FINISHED_A;
798
0
            s->init_num = 0;
799
800
0
            if (!s->method->ssl3_enc->change_cipher_state(s,
801
0
                                                          SSL3_CHANGE_CIPHER_SERVER_WRITE))
802
0
            {
803
0
                ret = -1;
804
0
                s->state = SSL_ST_ERR;
805
0
                goto end;
806
0
            }
807
808
0
            break;
809
810
0
        case SSL3_ST_SW_FINISHED_A:
811
0
        case SSL3_ST_SW_FINISHED_B:
812
0
            ret = ssl3_send_finished(s,
813
0
                                     SSL3_ST_SW_FINISHED_A,
814
0
                                     SSL3_ST_SW_FINISHED_B,
815
0
                                     s->method->
816
0
                                     ssl3_enc->server_finished_label,
817
0
                                     s->method->
818
0
                                     ssl3_enc->server_finished_label_len);
819
0
            if (ret <= 0)
820
0
                goto end;
821
0
            s->state = SSL3_ST_SW_FLUSH;
822
0
            if (s->hit) {
823
#if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
824
                s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
825
#else
826
0
                if (s->s3->next_proto_neg_seen) {
827
0
                    s->s3->tmp.next_state = SSL3_ST_SR_NEXT_PROTO_A;
828
0
                } else
829
0
                    s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
830
0
#endif
831
0
            } else
832
0
                s->s3->tmp.next_state = SSL_ST_OK;
833
0
            s->init_num = 0;
834
0
            break;
835
836
0
        case SSL_ST_OK:
837
            /* clean a few things up */
838
0
            ssl3_cleanup_key_block(s);
839
840
0
            BUF_MEM_free(s->init_buf);
841
0
            s->init_buf = NULL;
842
843
            /* remove buffering on output */
844
0
            ssl_free_wbio_buffer(s);
845
846
0
            s->init_num = 0;
847
848
0
            if (s->renegotiate == 2) { /* skipped if we just sent a
849
                                        * HelloRequest */
850
0
                s->renegotiate = 0;
851
0
                s->new_session = 0;
852
853
0
                ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
854
855
0
                s->ctx->stats.sess_accept_good++;
856
                /* s->server=1; */
857
0
                s->handshake_func = ssl3_accept;
858
859
0
                if (cb != NULL)
860
0
                    cb(s, SSL_CB_HANDSHAKE_DONE, 1);
861
0
            }
862
863
0
            ret = 1;
864
0
            goto end;
865
            /* break; */
866
867
0
        case SSL_ST_ERR:
868
0
        default:
869
0
            SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNKNOWN_STATE);
870
0
            ret = -1;
871
0
            goto end;
872
            /* break; */
873
0
        }
874
875
0
        if (!s->s3->tmp.reuse_message && !skip) {
876
0
            if (s->debug) {
877
0
                if ((ret = BIO_flush(s->wbio)) <= 0)
878
0
                    goto end;
879
0
            }
880
881
0
            if ((cb != NULL) && (s->state != state)) {
882
0
                new_state = s->state;
883
0
                s->state = state;
884
0
                cb(s, SSL_CB_ACCEPT_LOOP, 1);
885
0
                s->state = new_state;
886
0
            }
887
0
        }
888
0
        skip = 0;
889
0
    }
890
0
 end:
891
    /* BIO_flush(s->wbio); */
892
893
0
    s->in_handshake--;
894
0
    if (cb != NULL)
895
0
        cb(s, SSL_CB_ACCEPT_EXIT, ret);
896
0
    return (ret);
897
0
}
898
899
int ssl3_send_hello_request(SSL *s)
900
0
{
901
902
0
    if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
903
0
        ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0);
904
0
        s->state = SSL3_ST_SW_HELLO_REQ_B;
905
0
    }
906
907
    /* SSL3_ST_SW_HELLO_REQ_B */
908
0
    return ssl_do_write(s);
909
0
}
910
911
int ssl3_get_client_hello(SSL *s)
912
0
{
913
0
    int i, j, ok, al = SSL_AD_INTERNAL_ERROR, ret = -1, cookie_valid = 0;
914
0
    unsigned int cookie_len;
915
0
    long n;
916
0
    unsigned long id;
917
0
    unsigned char *p, *d;
918
0
    SSL_CIPHER *c;
919
0
#ifndef OPENSSL_NO_COMP
920
0
    unsigned char *q;
921
0
    SSL_COMP *comp = NULL;
922
0
#endif
923
0
    STACK_OF(SSL_CIPHER) *ciphers = NULL;
924
925
0
    if (s->state == SSL3_ST_SR_CLNT_HELLO_C && !s->first_packet)
926
0
        goto retry_cert;
927
928
    /*
929
     * We do this so that we will respond with our native type. If we are
930
     * TLSv1 and we get SSLv3, we will respond with TLSv1, This down
931
     * switching should be handled by a different method. If we are SSLv3, we
932
     * will respond with SSLv3, even if prompted with TLSv1.
933
     */
934
0
    if (s->state == SSL3_ST_SR_CLNT_HELLO_A) {
935
0
        s->state = SSL3_ST_SR_CLNT_HELLO_B;
936
0
    }
937
0
    s->first_packet = 1;
938
0
    n = s->method->ssl_get_message(s,
939
0
                                   SSL3_ST_SR_CLNT_HELLO_B,
940
0
                                   SSL3_ST_SR_CLNT_HELLO_C,
941
0
                                   SSL3_MT_CLIENT_HELLO,
942
0
                                   SSL3_RT_MAX_PLAIN_LENGTH, &ok);
943
944
0
    if (!ok)
945
0
        return ((int)n);
946
0
    s->first_packet = 0;
947
0
    d = p = (unsigned char *)s->init_msg;
948
949
    /*
950
     * 2 bytes for client version, SSL3_RANDOM_SIZE bytes for random, 1 byte
951
     * for session id length
952
     */
953
0
    if (n < 2 + SSL3_RANDOM_SIZE + 1) {
954
0
        al = SSL_AD_DECODE_ERROR;
955
0
        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
956
0
        goto f_err;
957
0
    }
958
959
    /*
960
     * use version from inside client hello, not from record header (may
961
     * differ: see RFC 2246, Appendix E, second paragraph)
962
     */
963
0
    s->client_version = (((int)p[0]) << 8) | (int)p[1];
964
0
    p += 2;
965
966
0
    if (SSL_IS_DTLS(s) ? (s->client_version > s->version &&
967
0
                          s->method->version != DTLS_ANY_VERSION)
968
0
        : (s->client_version < s->version)) {
969
0
        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
970
0
        if ((s->client_version >> 8) == SSL3_VERSION_MAJOR &&
971
0
            !s->enc_write_ctx && !s->write_hash) {
972
            /*
973
             * similar to ssl3_get_record, send alert using remote version
974
             * number
975
             */
976
0
            s->version = s->client_version;
977
0
        }
978
0
        al = SSL_AD_PROTOCOL_VERSION;
979
0
        goto f_err;
980
0
    }
981
982
    /*
983
     * If we require cookies and this ClientHello doesn't contain one, just
984
     * return since we do not want to allocate any memory yet. So check
985
     * cookie length...
986
     */
987
0
    if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
988
0
        unsigned int session_length, cookie_length;
989
990
0
        session_length = *(p + SSL3_RANDOM_SIZE);
991
992
0
        if (SSL3_RANDOM_SIZE + session_length + 1
993
0
                >= (unsigned int)((d + n) - p)) {
994
0
            al = SSL_AD_DECODE_ERROR;
995
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
996
0
            goto f_err;
997
0
        }
998
0
        cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1);
999
1000
0
        if (cookie_length == 0)
1001
0
            return 1;
1002
0
    }
1003
1004
    /* load the client random */
1005
0
    memcpy(s->s3->client_random, p, SSL3_RANDOM_SIZE);
1006
0
    p += SSL3_RANDOM_SIZE;
1007
1008
    /* get the session-id */
1009
0
    j = *(p++);
1010
1011
0
    if ((d + n) - p < j) {
1012
0
        al = SSL_AD_DECODE_ERROR;
1013
0
        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1014
0
        goto f_err;
1015
0
    }
1016
1017
0
    if ((j < 0) || (j > SSL_MAX_SSL_SESSION_ID_LENGTH)) {
1018
0
        al = SSL_AD_DECODE_ERROR;
1019
0
        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1020
0
        goto f_err;
1021
0
    }
1022
1023
0
    s->hit = 0;
1024
    /*
1025
     * Versions before 0.9.7 always allow clients to resume sessions in
1026
     * renegotiation. 0.9.7 and later allow this by default, but optionally
1027
     * ignore resumption requests with flag
1028
     * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1029
     * than a change to default behavior so that applications relying on this
1030
     * for security won't even compile against older library versions).
1031
     * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1032
     * request renegotiation but not a new session (s->new_session remains
1033
     * unset): for servers, this essentially just means that the
1034
     * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be ignored.
1035
     */
1036
0
    if ((s->new_session
1037
0
         && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1038
0
        if (!ssl_get_new_session(s, 1))
1039
0
            goto err;
1040
0
    } else {
1041
0
        i = ssl_get_prev_session(s, p, j, d + n);
1042
        /*
1043
         * Only resume if the session's version matches the negotiated
1044
         * version.
1045
         * RFC 5246 does not provide much useful advice on resumption
1046
         * with a different protocol version. It doesn't forbid it but
1047
         * the sanity of such behaviour would be questionable.
1048
         * In practice, clients do not accept a version mismatch and
1049
         * will abort the handshake with an error.
1050
         */
1051
0
        if (i == 1 && s->version == s->session->ssl_version) { /* previous
1052
                                                                * session */
1053
0
            s->hit = 1;
1054
0
        } else if (i == -1)
1055
0
            goto err;
1056
0
        else {                  /* i == 0 */
1057
1058
0
            if (!ssl_get_new_session(s, 1))
1059
0
                goto err;
1060
0
        }
1061
0
    }
1062
1063
0
    p += j;
1064
1065
0
    if (SSL_IS_DTLS(s)) {
1066
        /* cookie stuff */
1067
0
        if ((d + n) - p < 1) {
1068
0
            al = SSL_AD_DECODE_ERROR;
1069
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1070
0
            goto f_err;
1071
0
        }
1072
0
        cookie_len = *(p++);
1073
1074
0
        if ((unsigned int)((d + n ) - p) < cookie_len) {
1075
0
            al = SSL_AD_DECODE_ERROR;
1076
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1077
0
            goto f_err;
1078
0
        }
1079
1080
        /*
1081
         * The ClientHello may contain a cookie even if the
1082
         * HelloVerify message has not been sent--make sure that it
1083
         * does not cause an overflow.
1084
         */
1085
0
        if (cookie_len > sizeof(s->d1->rcvd_cookie)) {
1086
            /* too much data */
1087
0
            al = SSL_AD_DECODE_ERROR;
1088
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1089
0
            goto f_err;
1090
0
        }
1091
1092
        /* verify the cookie if appropriate option is set. */
1093
0
        if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && cookie_len > 0) {
1094
0
            memcpy(s->d1->rcvd_cookie, p, cookie_len);
1095
1096
0
            if (s->ctx->app_verify_cookie_cb != NULL) {
1097
0
                if (s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
1098
0
                                                 cookie_len) == 0) {
1099
0
                    al = SSL_AD_HANDSHAKE_FAILURE;
1100
0
                    SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1101
0
                           SSL_R_COOKIE_MISMATCH);
1102
0
                    goto f_err;
1103
0
                }
1104
                /* else cookie verification succeeded */
1105
0
            }
1106
            /* default verification */
1107
0
            else if (memcmp(s->d1->rcvd_cookie, s->d1->cookie,
1108
0
                            s->d1->cookie_len) != 0) {
1109
0
                al = SSL_AD_HANDSHAKE_FAILURE;
1110
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1111
0
                goto f_err;
1112
0
            }
1113
0
            cookie_valid = 1;
1114
0
        }
1115
1116
0
        p += cookie_len;
1117
0
        if (s->method->version == DTLS_ANY_VERSION) {
1118
            /* Select version to use */
1119
0
            if (s->client_version <= DTLS1_2_VERSION &&
1120
0
                !(s->options & SSL_OP_NO_DTLSv1_2)) {
1121
0
                s->version = DTLS1_2_VERSION;
1122
0
                s->method = DTLSv1_2_server_method();
1123
0
            } else if (tls1_suiteb(s)) {
1124
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1125
0
                       SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1126
0
                s->version = s->client_version;
1127
0
                al = SSL_AD_PROTOCOL_VERSION;
1128
0
                goto f_err;
1129
0
            } else if (s->client_version <= DTLS1_VERSION &&
1130
0
                       !(s->options & SSL_OP_NO_DTLSv1)) {
1131
0
                s->version = DTLS1_VERSION;
1132
0
                s->method = DTLSv1_server_method();
1133
0
            } else {
1134
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1135
0
                       SSL_R_WRONG_VERSION_NUMBER);
1136
0
                s->version = s->client_version;
1137
0
                al = SSL_AD_PROTOCOL_VERSION;
1138
0
                goto f_err;
1139
0
            }
1140
0
            s->session->ssl_version = s->version;
1141
0
        }
1142
0
    }
1143
1144
0
    if ((d + n ) - p < 2) {
1145
0
        al = SSL_AD_DECODE_ERROR;
1146
0
        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1147
0
        goto f_err;
1148
0
    }
1149
0
    n2s(p, i);
1150
1151
0
    if (i == 0) {
1152
0
        al = SSL_AD_ILLEGAL_PARAMETER;
1153
0
        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
1154
0
        goto f_err;
1155
0
    }
1156
1157
    /* i bytes of cipher data + 1 byte for compression length later */
1158
0
    if ((d + n) - p < i + 1) {
1159
        /* not enough data */
1160
0
        al = SSL_AD_DECODE_ERROR;
1161
0
        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1162
0
        goto f_err;
1163
0
    }
1164
0
    if (ssl_bytes_to_cipher_list(s, p, i, &(ciphers)) == NULL) {
1165
0
        goto err;
1166
0
    }
1167
0
    p += i;
1168
1169
    /* If it is a hit, check that the cipher is in the list */
1170
0
    if (s->hit) {
1171
0
        j = 0;
1172
0
        id = s->session->cipher->id;
1173
1174
#ifdef CIPHER_DEBUG
1175
        fprintf(stderr, "client sent %d ciphers\n",
1176
                sk_SSL_CIPHER_num(ciphers));
1177
#endif
1178
0
        for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1179
0
            c = sk_SSL_CIPHER_value(ciphers, i);
1180
#ifdef CIPHER_DEBUG
1181
            fprintf(stderr, "client [%2d of %2d]:%s\n",
1182
                    i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1183
#endif
1184
0
            if (c->id == id) {
1185
0
                j = 1;
1186
0
                break;
1187
0
            }
1188
0
        }
1189
        /*
1190
         * Disabled because it can be used in a ciphersuite downgrade attack:
1191
         * CVE-2010-4180.
1192
         */
1193
#if 0
1194
        if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)
1195
            && (sk_SSL_CIPHER_num(ciphers) == 1)) {
1196
            /*
1197
             * Special case as client bug workaround: the previously used
1198
             * cipher may not be in the current list, the client instead
1199
             * might be trying to continue using a cipher that before wasn't
1200
             * chosen due to server preferences.  We'll have to reject the
1201
             * connection if the cipher is not enabled, though.
1202
             */
1203
            c = sk_SSL_CIPHER_value(ciphers, 0);
1204
            if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) {
1205
                s->session->cipher = c;
1206
                j = 1;
1207
            }
1208
        }
1209
#endif
1210
0
        if (j == 0) {
1211
            /*
1212
             * we need to have the cipher in the cipher list if we are asked
1213
             * to reuse it
1214
             */
1215
0
            al = SSL_AD_ILLEGAL_PARAMETER;
1216
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1217
0
                   SSL_R_REQUIRED_CIPHER_MISSING);
1218
0
            goto f_err;
1219
0
        }
1220
0
    }
1221
1222
    /* compression */
1223
0
    i = *(p++);
1224
0
    if ((d + n) - p < i) {
1225
        /* not enough data */
1226
0
        al = SSL_AD_DECODE_ERROR;
1227
0
        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1228
0
        goto f_err;
1229
0
    }
1230
0
#ifndef OPENSSL_NO_COMP
1231
0
    q = p;
1232
0
#endif
1233
0
    for (j = 0; j < i; j++) {
1234
0
        if (p[j] == 0)
1235
0
            break;
1236
0
    }
1237
1238
0
    p += i;
1239
0
    if (j >= i) {
1240
        /* no compress */
1241
0
        al = SSL_AD_DECODE_ERROR;
1242
0
        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1243
0
        goto f_err;
1244
0
    }
1245
0
#ifndef OPENSSL_NO_TLSEXT
1246
    /* TLS extensions */
1247
0
    if (s->version >= SSL3_VERSION) {
1248
0
        if (!ssl_parse_clienthello_tlsext(s, &p, d + n)) {
1249
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1250
0
            goto err;
1251
0
        }
1252
0
    }
1253
1254
    /*
1255
     * Check if we want to use external pre-shared secret for this handshake
1256
     * for not reused session only. We need to generate server_random before
1257
     * calling tls_session_secret_cb in order to allow SessionTicket
1258
     * processing to use it in key derivation.
1259
     */
1260
0
    {
1261
0
        unsigned char *pos;
1262
0
        pos = s->s3->server_random;
1263
0
        if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
1264
0
            goto f_err;
1265
0
        }
1266
0
    }
1267
1268
0
    if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
1269
0
        SSL_CIPHER *pref_cipher = NULL;
1270
1271
0
        s->session->master_key_length = sizeof(s->session->master_key);
1272
0
        if (s->tls_session_secret_cb(s, s->session->master_key,
1273
0
                                     &s->session->master_key_length, ciphers,
1274
0
                                     &pref_cipher,
1275
0
                                     s->tls_session_secret_cb_arg)) {
1276
0
            s->hit = 1;
1277
0
            s->session->ciphers = ciphers;
1278
0
            s->session->verify_result = X509_V_OK;
1279
1280
0
            ciphers = NULL;
1281
1282
            /* check if some cipher was preferred by call back */
1283
0
            pref_cipher =
1284
0
                pref_cipher ? pref_cipher : ssl3_choose_cipher(s,
1285
0
                                                               s->
1286
0
                                                               session->ciphers,
1287
0
                                                               SSL_get_ciphers
1288
0
                                                               (s));
1289
0
            if (pref_cipher == NULL) {
1290
0
                al = SSL_AD_HANDSHAKE_FAILURE;
1291
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1292
0
                goto f_err;
1293
0
            }
1294
1295
0
            s->session->cipher = pref_cipher;
1296
1297
0
            if (s->cipher_list)
1298
0
                sk_SSL_CIPHER_free(s->cipher_list);
1299
1300
0
            if (s->cipher_list_by_id)
1301
0
                sk_SSL_CIPHER_free(s->cipher_list_by_id);
1302
1303
0
            s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1304
0
            s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1305
0
        }
1306
0
    }
1307
0
#endif
1308
1309
    /*
1310
     * Worst case, we will use the NULL compression, but if we have other
1311
     * options, we will now look for them.  We have i-1 compression
1312
     * algorithms from the client, starting at q.
1313
     */
1314
0
    s->s3->tmp.new_compression = NULL;
1315
0
#ifndef OPENSSL_NO_COMP
1316
    /* This only happens if we have a cache hit */
1317
0
    if (s->session->compress_meth != 0) {
1318
0
        int m, comp_id = s->session->compress_meth;
1319
        /* Perform sanity checks on resumed compression algorithm */
1320
        /* Can't disable compression */
1321
0
        if (s->options & SSL_OP_NO_COMPRESSION) {
1322
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1323
0
                   SSL_R_INCONSISTENT_COMPRESSION);
1324
0
            goto f_err;
1325
0
        }
1326
        /* Look for resumed compression method */
1327
0
        for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1328
0
            comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1329
0
            if (comp_id == comp->id) {
1330
0
                s->s3->tmp.new_compression = comp;
1331
0
                break;
1332
0
            }
1333
0
        }
1334
0
        if (s->s3->tmp.new_compression == NULL) {
1335
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1336
0
                   SSL_R_INVALID_COMPRESSION_ALGORITHM);
1337
0
            goto f_err;
1338
0
        }
1339
        /* Look for resumed method in compression list */
1340
0
        for (m = 0; m < i; m++) {
1341
0
            if (q[m] == comp_id)
1342
0
                break;
1343
0
        }
1344
0
        if (m >= i) {
1345
0
            al = SSL_AD_ILLEGAL_PARAMETER;
1346
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1347
0
                   SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
1348
0
            goto f_err;
1349
0
        }
1350
0
    } else if (s->hit)
1351
0
        comp = NULL;
1352
0
    else if (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods) {
1353
        /* See if we have a match */
1354
0
        int m, nn, o, v, done = 0;
1355
1356
0
        nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1357
0
        for (m = 0; m < nn; m++) {
1358
0
            comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1359
0
            v = comp->id;
1360
0
            for (o = 0; o < i; o++) {
1361
0
                if (v == q[o]) {
1362
0
                    done = 1;
1363
0
                    break;
1364
0
                }
1365
0
            }
1366
0
            if (done)
1367
0
                break;
1368
0
        }
1369
0
        if (done)
1370
0
            s->s3->tmp.new_compression = comp;
1371
0
        else
1372
0
            comp = NULL;
1373
0
    }
1374
#else
1375
    /*
1376
     * If compression is disabled we'd better not try to resume a session
1377
     * using compression.
1378
     */
1379
    if (s->session->compress_meth != 0) {
1380
        SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1381
        goto f_err;
1382
    }
1383
#endif
1384
1385
    /*
1386
     * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1387
     */
1388
1389
0
    if (!s->hit) {
1390
#ifdef OPENSSL_NO_COMP
1391
        s->session->compress_meth = 0;
1392
#else
1393
0
        s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1394
0
#endif
1395
0
        if (s->session->ciphers != NULL)
1396
0
            sk_SSL_CIPHER_free(s->session->ciphers);
1397
0
        s->session->ciphers = ciphers;
1398
0
        if (ciphers == NULL) {
1399
0
            al = SSL_AD_INTERNAL_ERROR;
1400
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1401
0
            goto f_err;
1402
0
        }
1403
0
        ciphers = NULL;
1404
0
        if (!tls1_set_server_sigalgs(s)) {
1405
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1406
0
            goto err;
1407
0
        }
1408
        /* Let cert callback update server certificates if required */
1409
0
 retry_cert:
1410
0
        if (s->cert->cert_cb) {
1411
0
            int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1412
0
            if (rv == 0) {
1413
0
                al = SSL_AD_INTERNAL_ERROR;
1414
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CERT_CB_ERROR);
1415
0
                goto f_err;
1416
0
            }
1417
0
            if (rv < 0) {
1418
0
                s->rwstate = SSL_X509_LOOKUP;
1419
0
                return -1;
1420
0
            }
1421
0
            s->rwstate = SSL_NOTHING;
1422
0
        }
1423
0
        c = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1424
1425
0
        if (c == NULL) {
1426
0
            al = SSL_AD_HANDSHAKE_FAILURE;
1427
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1428
0
            goto f_err;
1429
0
        }
1430
0
        s->s3->tmp.new_cipher = c;
1431
0
    } else {
1432
        /* Session-id reuse */
1433
0
#ifdef REUSE_CIPHER_BUG
1434
0
        STACK_OF(SSL_CIPHER) *sk;
1435
0
        SSL_CIPHER *nc = NULL;
1436
0
        SSL_CIPHER *ec = NULL;
1437
1438
0
        if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) {
1439
0
            sk = s->session->ciphers;
1440
0
            for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1441
0
                c = sk_SSL_CIPHER_value(sk, i);
1442
0
                if (c->algorithm_enc & SSL_eNULL)
1443
0
                    nc = c;
1444
0
                if (SSL_C_IS_EXPORT(c))
1445
0
                    ec = c;
1446
0
            }
1447
0
            if (nc != NULL)
1448
0
                s->s3->tmp.new_cipher = nc;
1449
0
            else if (ec != NULL)
1450
0
                s->s3->tmp.new_cipher = ec;
1451
0
            else
1452
0
                s->s3->tmp.new_cipher = s->session->cipher;
1453
0
        } else
1454
0
#endif
1455
0
            s->s3->tmp.new_cipher = s->session->cipher;
1456
0
    }
1457
1458
0
    if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) {
1459
0
        if (!ssl3_digest_cached_records(s))
1460
0
            goto f_err;
1461
0
    }
1462
1463
    /*-
1464
    * we now have the following setup.
1465
     * client_random
1466
     * cipher_list          - our prefered list of ciphers
1467
     * ciphers              - the clients prefered list of ciphers
1468
     * compression          - basically ignored right now
1469
     * ssl version is set   - sslv3
1470
     * s->session           - The ssl session has been setup.
1471
     * s->hit               - session reuse flag
1472
     * s->tmp.new_cipher    - the new cipher to use.
1473
     */
1474
1475
    /* Handles TLS extensions that we couldn't check earlier */
1476
0
    if (s->version >= SSL3_VERSION) {
1477
0
        if (!ssl_check_clienthello_tlsext_late(s, &al)) {
1478
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1479
0
            goto f_err;
1480
0
        }
1481
0
    }
1482
1483
0
    ret = cookie_valid ? 2 : 1;
1484
0
    if (0) {
1485
0
 f_err:
1486
0
        ssl3_send_alert(s, SSL3_AL_FATAL, al);
1487
0
 err:
1488
0
        s->state = SSL_ST_ERR;
1489
0
    }
1490
1491
0
    if (ciphers != NULL)
1492
0
        sk_SSL_CIPHER_free(ciphers);
1493
0
    return ret;
1494
0
}
1495
1496
int ssl3_send_server_hello(SSL *s)
1497
0
{
1498
0
    unsigned char *buf;
1499
0
    unsigned char *p, *d;
1500
0
    int i, sl;
1501
0
    int al = 0;
1502
0
    unsigned long l;
1503
1504
0
    if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1505
0
        buf = (unsigned char *)s->init_buf->data;
1506
#ifdef OPENSSL_NO_TLSEXT
1507
        p = s->s3->server_random;
1508
        if (ssl_fill_hello_random(s, 1, p, SSL3_RANDOM_SIZE) <= 0) {
1509
            s->state = SSL_ST_ERR;
1510
            return -1;
1511
        }
1512
#endif
1513
        /* Do the message type and length last */
1514
0
        d = p = ssl_handshake_start(s);
1515
1516
0
        *(p++) = s->version >> 8;
1517
0
        *(p++) = s->version & 0xff;
1518
1519
        /* Random stuff */
1520
0
        memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1521
0
        p += SSL3_RANDOM_SIZE;
1522
1523
        /*-
1524
         * There are several cases for the session ID to send
1525
         * back in the server hello:
1526
         * - For session reuse from the session cache,
1527
         *   we send back the old session ID.
1528
         * - If stateless session reuse (using a session ticket)
1529
         *   is successful, we send back the client's "session ID"
1530
         *   (which doesn't actually identify the session).
1531
         * - If it is a new session, we send back the new
1532
         *   session ID.
1533
         * - However, if we want the new session to be single-use,
1534
         *   we send back a 0-length session ID.
1535
         * s->hit is non-zero in either case of session reuse,
1536
         * so the following won't overwrite an ID that we're supposed
1537
         * to send back.
1538
         */
1539
0
        if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1540
0
            && !s->hit)
1541
0
            s->session->session_id_length = 0;
1542
1543
0
        sl = s->session->session_id_length;
1544
0
        if (sl > (int)sizeof(s->session->session_id)) {
1545
0
            SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1546
0
            s->state = SSL_ST_ERR;
1547
0
            return -1;
1548
0
        }
1549
0
        *(p++) = sl;
1550
0
        memcpy(p, s->session->session_id, sl);
1551
0
        p += sl;
1552
1553
        /* put the cipher */
1554
0
        i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1555
0
        p += i;
1556
1557
        /* put the compression method */
1558
#ifdef OPENSSL_NO_COMP
1559
        *(p++) = 0;
1560
#else
1561
0
        if (s->s3->tmp.new_compression == NULL)
1562
0
            *(p++) = 0;
1563
0
        else
1564
0
            *(p++) = s->s3->tmp.new_compression->id;
1565
0
#endif
1566
0
#ifndef OPENSSL_NO_TLSEXT
1567
0
        if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1568
0
            SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1569
0
            s->state = SSL_ST_ERR;
1570
0
            return -1;
1571
0
        }
1572
0
        if ((p =
1573
0
             ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
1574
0
                                        &al)) == NULL) {
1575
0
            ssl3_send_alert(s, SSL3_AL_FATAL, al);
1576
0
            SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1577
0
            s->state = SSL_ST_ERR;
1578
0
            return -1;
1579
0
        }
1580
0
#endif
1581
        /* do the header */
1582
0
        l = (p - d);
1583
0
        ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l);
1584
0
        s->state = SSL3_ST_SW_SRVR_HELLO_B;
1585
0
    }
1586
1587
    /* SSL3_ST_SW_SRVR_HELLO_B */
1588
0
    return ssl_do_write(s);
1589
0
}
1590
1591
int ssl3_send_server_done(SSL *s)
1592
0
{
1593
1594
0
    if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1595
0
        ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0);
1596
0
        s->state = SSL3_ST_SW_SRVR_DONE_B;
1597
0
    }
1598
1599
    /* SSL3_ST_SW_SRVR_DONE_B */
1600
0
    return ssl_do_write(s);
1601
0
}
1602
1603
int ssl3_send_server_key_exchange(SSL *s)
1604
0
{
1605
0
#ifndef OPENSSL_NO_RSA
1606
0
    unsigned char *q;
1607
0
    int j, num;
1608
0
    RSA *rsa;
1609
0
    unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1610
0
    unsigned int u;
1611
0
#endif
1612
0
#ifndef OPENSSL_NO_DH
1613
# ifdef OPENSSL_NO_RSA
1614
    int j;
1615
# endif
1616
0
    DH *dh = NULL, *dhp;
1617
0
#endif
1618
0
#ifndef OPENSSL_NO_ECDH
1619
0
    EC_KEY *ecdh = NULL, *ecdhp;
1620
0
    unsigned char *encodedPoint = NULL;
1621
0
    int encodedlen = 0;
1622
0
    int curve_id = 0;
1623
0
    BN_CTX *bn_ctx = NULL;
1624
0
#endif
1625
0
    EVP_PKEY *pkey;
1626
0
    const EVP_MD *md = NULL;
1627
0
    unsigned char *p, *d;
1628
0
    int al, i;
1629
0
    unsigned long type;
1630
0
    int n;
1631
0
    CERT *cert;
1632
0
    BIGNUM *r[4];
1633
0
    int nr[4], kn;
1634
0
    BUF_MEM *buf;
1635
0
    EVP_MD_CTX md_ctx;
1636
1637
0
    EVP_MD_CTX_init(&md_ctx);
1638
0
    if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1639
0
        type = s->s3->tmp.new_cipher->algorithm_mkey;
1640
0
        cert = s->cert;
1641
1642
0
        buf = s->init_buf;
1643
1644
0
        r[0] = r[1] = r[2] = r[3] = NULL;
1645
0
        n = 0;
1646
0
#ifndef OPENSSL_NO_RSA
1647
0
        if (type & SSL_kRSA) {
1648
0
            rsa = cert->rsa_tmp;
1649
0
            if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1650
0
                rsa = s->cert->rsa_tmp_cb(s,
1651
0
                                          SSL_C_IS_EXPORT(s->s3->
1652
0
                                                          tmp.new_cipher),
1653
0
                                          SSL_C_EXPORT_PKEYLENGTH(s->s3->
1654
0
                                                                  tmp.new_cipher));
1655
0
                if (rsa == NULL) {
1656
0
                    al = SSL_AD_HANDSHAKE_FAILURE;
1657
0
                    SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1658
0
                           SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1659
0
                    goto f_err;
1660
0
                }
1661
0
                RSA_up_ref(rsa);
1662
0
                cert->rsa_tmp = rsa;
1663
0
            }
1664
0
            if (rsa == NULL) {
1665
0
                al = SSL_AD_HANDSHAKE_FAILURE;
1666
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1667
0
                       SSL_R_MISSING_TMP_RSA_KEY);
1668
0
                goto f_err;
1669
0
            }
1670
0
            r[0] = rsa->n;
1671
0
            r[1] = rsa->e;
1672
0
            s->s3->tmp.use_rsa_tmp = 1;
1673
0
        } else
1674
0
#endif
1675
0
#ifndef OPENSSL_NO_DH
1676
0
        if (type & SSL_kEDH) {
1677
0
            dhp = cert->dh_tmp;
1678
0
            if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1679
0
                dhp = s->cert->dh_tmp_cb(s,
1680
0
                                         SSL_C_IS_EXPORT(s->s3->
1681
0
                                                         tmp.new_cipher),
1682
0
                                         SSL_C_EXPORT_PKEYLENGTH(s->s3->
1683
0
                                                                 tmp.new_cipher));
1684
0
            if (dhp == NULL) {
1685
0
                al = SSL_AD_HANDSHAKE_FAILURE;
1686
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1687
0
                       SSL_R_MISSING_TMP_DH_KEY);
1688
0
                goto f_err;
1689
0
            }
1690
1691
0
            if (s->s3->tmp.dh != NULL) {
1692
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1693
0
                       ERR_R_INTERNAL_ERROR);
1694
0
                goto err;
1695
0
            }
1696
1697
0
            if ((dh = DHparams_dup(dhp)) == NULL) {
1698
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1699
0
                goto err;
1700
0
            }
1701
1702
0
            s->s3->tmp.dh = dh;
1703
0
            if (!DH_generate_key(dh)) {
1704
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1705
0
                goto err;
1706
0
            }
1707
0
            r[0] = dh->p;
1708
0
            r[1] = dh->g;
1709
0
            r[2] = dh->pub_key;
1710
0
        } else
1711
0
#endif
1712
0
#ifndef OPENSSL_NO_ECDH
1713
0
        if (type & SSL_kEECDH) {
1714
0
            const EC_GROUP *group;
1715
1716
0
            if (s->s3->tmp.ecdh != NULL) {
1717
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1718
0
                       ERR_R_INTERNAL_ERROR);
1719
0
                goto err;
1720
0
            }
1721
1722
0
            ecdhp = cert->ecdh_tmp;
1723
0
            if (s->cert->ecdh_tmp_auto) {
1724
                /* Get NID of appropriate shared curve */
1725
0
                int nid = tls1_shared_curve(s, -2);
1726
0
                if (nid != NID_undef)
1727
0
                    ecdhp = EC_KEY_new_by_curve_name(nid);
1728
0
            } else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb) {
1729
0
                ecdhp = s->cert->ecdh_tmp_cb(s,
1730
0
                                             SSL_C_IS_EXPORT(s->s3->
1731
0
                                                             tmp.new_cipher),
1732
0
                                             SSL_C_EXPORT_PKEYLENGTH(s->
1733
0
                                                                     s3->tmp.new_cipher));
1734
0
            }
1735
0
            if (ecdhp == NULL) {
1736
0
                al = SSL_AD_HANDSHAKE_FAILURE;
1737
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1738
0
                       SSL_R_MISSING_TMP_ECDH_KEY);
1739
0
                goto f_err;
1740
0
            }
1741
1742
            /* Duplicate the ECDH structure. */
1743
0
            if (s->cert->ecdh_tmp_auto)
1744
0
                ecdh = ecdhp;
1745
0
            else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1746
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1747
0
                goto err;
1748
0
            }
1749
1750
0
            s->s3->tmp.ecdh = ecdh;
1751
0
            if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1752
0
                (EC_KEY_get0_private_key(ecdh) == NULL) ||
1753
0
                (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1754
0
                if (!EC_KEY_generate_key(ecdh)) {
1755
0
                    SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1756
0
                           ERR_R_ECDH_LIB);
1757
0
                    goto err;
1758
0
                }
1759
0
            }
1760
1761
0
            if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1762
0
                (EC_KEY_get0_public_key(ecdh) == NULL) ||
1763
0
                (EC_KEY_get0_private_key(ecdh) == NULL)) {
1764
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1765
0
                goto err;
1766
0
            }
1767
1768
0
            if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1769
0
                (EC_GROUP_get_degree(group) > 163)) {
1770
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1771
0
                       SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1772
0
                goto err;
1773
0
            }
1774
1775
            /*
1776
             * XXX: For now, we only support ephemeral ECDH keys over named
1777
             * (not generic) curves. For supported named curves, curve_id is
1778
             * non-zero.
1779
             */
1780
0
            if ((curve_id =
1781
0
                 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1782
0
                == 0) {
1783
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1784
0
                       SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1785
0
                goto err;
1786
0
            }
1787
1788
            /*
1789
             * Encode the public key. First check the size of encoding and
1790
             * allocate memory accordingly.
1791
             */
1792
0
            encodedlen = EC_POINT_point2oct(group,
1793
0
                                            EC_KEY_get0_public_key(ecdh),
1794
0
                                            POINT_CONVERSION_UNCOMPRESSED,
1795
0
                                            NULL, 0, NULL);
1796
1797
0
            encodedPoint = (unsigned char *)
1798
0
                OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1799
0
            bn_ctx = BN_CTX_new();
1800
0
            if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1801
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1802
0
                       ERR_R_MALLOC_FAILURE);
1803
0
                goto err;
1804
0
            }
1805
1806
0
            encodedlen = EC_POINT_point2oct(group,
1807
0
                                            EC_KEY_get0_public_key(ecdh),
1808
0
                                            POINT_CONVERSION_UNCOMPRESSED,
1809
0
                                            encodedPoint, encodedlen, bn_ctx);
1810
1811
0
            if (encodedlen == 0) {
1812
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1813
0
                goto err;
1814
0
            }
1815
1816
0
            BN_CTX_free(bn_ctx);
1817
0
            bn_ctx = NULL;
1818
1819
            /*
1820
             * XXX: For now, we only support named (not generic) curves in
1821
             * ECDH ephemeral key exchanges. In this situation, we need four
1822
             * additional bytes to encode the entire ServerECDHParams
1823
             * structure.
1824
             */
1825
0
            n = 4 + encodedlen;
1826
1827
            /*
1828
             * We'll generate the serverKeyExchange message explicitly so we
1829
             * can set these to NULLs
1830
             */
1831
0
            r[0] = NULL;
1832
0
            r[1] = NULL;
1833
0
            r[2] = NULL;
1834
0
            r[3] = NULL;
1835
0
        } else
1836
0
#endif                          /* !OPENSSL_NO_ECDH */
1837
0
#ifndef OPENSSL_NO_PSK
1838
0
        if (type & SSL_kPSK) {
1839
            /*
1840
             * reserve size for record length and PSK identity hint
1841
             */
1842
0
            n += 2 + strlen(s->ctx->psk_identity_hint);
1843
0
        } else
1844
0
#endif                          /* !OPENSSL_NO_PSK */
1845
0
#ifndef OPENSSL_NO_SRP
1846
0
        if (type & SSL_kSRP) {
1847
0
            if ((s->srp_ctx.N == NULL) ||
1848
0
                (s->srp_ctx.g == NULL) ||
1849
0
                (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
1850
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1851
0
                       SSL_R_MISSING_SRP_PARAM);
1852
0
                goto err;
1853
0
            }
1854
0
            r[0] = s->srp_ctx.N;
1855
0
            r[1] = s->srp_ctx.g;
1856
0
            r[2] = s->srp_ctx.s;
1857
0
            r[3] = s->srp_ctx.B;
1858
0
        } else
1859
0
#endif
1860
0
        {
1861
0
            al = SSL_AD_HANDSHAKE_FAILURE;
1862
0
            SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1863
0
                   SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1864
0
            goto f_err;
1865
0
        }
1866
0
        for (i = 0; i < 4 && r[i] != NULL; i++) {
1867
0
            nr[i] = BN_num_bytes(r[i]);
1868
0
#ifndef OPENSSL_NO_SRP
1869
0
            if ((i == 2) && (type & SSL_kSRP))
1870
0
                n += 1 + nr[i];
1871
0
            else
1872
0
#endif
1873
0
#ifndef OPENSSL_NO_DH
1874
            /*
1875
             * for interoperability with some versions of the Microsoft TLS
1876
             * stack, we need to zero pad the DHE pub key to the same length
1877
             * as the prime, so use the length of the prime here
1878
             */
1879
0
            if ((i == 2) && (type & (SSL_kEDH)))
1880
0
                n += 2 + nr[0];
1881
0
            else
1882
0
#endif
1883
0
                n += 2 + nr[i];
1884
0
        }
1885
1886
0
        if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
1887
0
            && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
1888
0
            if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md))
1889
0
                == NULL) {
1890
0
                al = SSL_AD_DECODE_ERROR;
1891
0
                goto f_err;
1892
0
            }
1893
0
            kn = EVP_PKEY_size(pkey);
1894
            /* Allow space for signature algorithm */
1895
0
            if (SSL_USE_SIGALGS(s))
1896
0
                kn += 2;
1897
            /* Allow space for signature length */
1898
0
            kn += 2;
1899
0
        } else {
1900
0
            pkey = NULL;
1901
0
            kn = 0;
1902
0
        }
1903
1904
0
        if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
1905
0
            SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
1906
0
            goto err;
1907
0
        }
1908
0
        d = p = ssl_handshake_start(s);
1909
1910
0
        for (i = 0; i < 4 && r[i] != NULL; i++) {
1911
0
#ifndef OPENSSL_NO_SRP
1912
0
            if ((i == 2) && (type & SSL_kSRP)) {
1913
0
                *p = nr[i];
1914
0
                p++;
1915
0
            } else
1916
0
#endif
1917
0
#ifndef OPENSSL_NO_DH
1918
            /*
1919
             * for interoperability with some versions of the Microsoft TLS
1920
             * stack, we need to zero pad the DHE pub key to the same length
1921
             * as the prime
1922
             */
1923
0
            if ((i == 2) && (type & (SSL_kEDH))) {
1924
0
                s2n(nr[0], p);
1925
0
                for (j = 0; j < (nr[0] - nr[2]); ++j) {
1926
0
                    *p = 0;
1927
0
                    ++p;
1928
0
                }
1929
0
            } else
1930
0
#endif
1931
0
                s2n(nr[i], p);
1932
0
            BN_bn2bin(r[i], p);
1933
0
            p += nr[i];
1934
0
        }
1935
1936
0
#ifndef OPENSSL_NO_ECDH
1937
0
        if (type & SSL_kEECDH) {
1938
            /*
1939
             * XXX: For now, we only support named (not generic) curves. In
1940
             * this situation, the serverKeyExchange message has: [1 byte
1941
             * CurveType], [2 byte CurveName] [1 byte length of encoded
1942
             * point], followed by the actual encoded point itself
1943
             */
1944
0
            *p = NAMED_CURVE_TYPE;
1945
0
            p += 1;
1946
0
            *p = 0;
1947
0
            p += 1;
1948
0
            *p = curve_id;
1949
0
            p += 1;
1950
0
            *p = encodedlen;
1951
0
            p += 1;
1952
0
            memcpy((unsigned char *)p,
1953
0
                   (unsigned char *)encodedPoint, encodedlen);
1954
0
            OPENSSL_free(encodedPoint);
1955
0
            encodedPoint = NULL;
1956
0
            p += encodedlen;
1957
0
        }
1958
0
#endif
1959
1960
0
#ifndef OPENSSL_NO_PSK
1961
0
        if (type & SSL_kPSK) {
1962
            /* copy PSK identity hint */
1963
0
            s2n(strlen(s->ctx->psk_identity_hint), p);
1964
0
            strncpy((char *)p, s->ctx->psk_identity_hint,
1965
0
                    strlen(s->ctx->psk_identity_hint));
1966
0
            p += strlen(s->ctx->psk_identity_hint);
1967
0
        }
1968
0
#endif
1969
1970
        /* not anonymous */
1971
0
        if (pkey != NULL) {
1972
            /*
1973
             * n is the length of the params, they start at &(d[4]) and p
1974
             * points to the space at the end.
1975
             */
1976
0
#ifndef OPENSSL_NO_RSA
1977
0
            if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1978
0
                q = md_buf;
1979
0
                j = 0;
1980
0
                for (num = 2; num > 0; num--) {
1981
0
                    EVP_MD_CTX_set_flags(&md_ctx,
1982
0
                                         EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1983
0
                    if (EVP_DigestInit_ex(&md_ctx,
1984
0
                                          (num == 2) ? s->ctx->md5
1985
0
                                                     : s->ctx->sha1,
1986
0
                                          NULL) <= 0
1987
0
                        || EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1988
0
                                            SSL3_RANDOM_SIZE) <= 0
1989
0
                        || EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1990
0
                                            SSL3_RANDOM_SIZE) <= 0
1991
0
                        || EVP_DigestUpdate(&md_ctx, d, n) <= 0
1992
0
                        || EVP_DigestFinal_ex(&md_ctx, q,
1993
0
                                              (unsigned int *)&i) <= 0) {
1994
0
                        SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1995
0
                               ERR_LIB_EVP);
1996
0
                        al = SSL_AD_INTERNAL_ERROR;
1997
0
                        goto f_err;
1998
0
                    }
1999
0
                    q += i;
2000
0
                    j += i;
2001
0
                }
2002
0
                if (RSA_sign(NID_md5_sha1, md_buf, j,
2003
0
                             &(p[2]), &u, pkey->pkey.rsa) <= 0) {
2004
0
                    SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
2005
0
                    goto err;
2006
0
                }
2007
0
                s2n(u, p);
2008
0
                n += u + 2;
2009
0
            } else
2010
0
#endif
2011
0
            if (md) {
2012
                /* send signature algorithm */
2013
0
                if (SSL_USE_SIGALGS(s)) {
2014
0
                    if (!tls12_get_sigandhash(p, pkey, md)) {
2015
                        /* Should never happen */
2016
0
                        al = SSL_AD_INTERNAL_ERROR;
2017
0
                        SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2018
0
                               ERR_R_INTERNAL_ERROR);
2019
0
                        goto f_err;
2020
0
                    }
2021
0
                    p += 2;
2022
0
                }
2023
#ifdef SSL_DEBUG
2024
                fprintf(stderr, "Using hash %s\n", EVP_MD_name(md));
2025
#endif
2026
0
                if (EVP_SignInit_ex(&md_ctx, md, NULL) <= 0
2027
0
                        || EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
2028
0
                                          SSL3_RANDOM_SIZE) <= 0
2029
0
                        || EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
2030
0
                                          SSL3_RANDOM_SIZE) <= 0
2031
0
                        || EVP_SignUpdate(&md_ctx, d, n) <= 0
2032
0
                        || EVP_SignFinal(&md_ctx, &(p[2]),
2033
0
                                         (unsigned int *)&i, pkey) <= 0) {
2034
0
                    SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_EVP);
2035
0
                    al = SSL_AD_INTERNAL_ERROR;
2036
0
                    goto f_err;
2037
0
                }
2038
0
                s2n(i, p);
2039
0
                n += i + 2;
2040
0
                if (SSL_USE_SIGALGS(s))
2041
0
                    n += 2;
2042
0
            } else {
2043
                /* Is this error check actually needed? */
2044
0
                al = SSL_AD_HANDSHAKE_FAILURE;
2045
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2046
0
                       SSL_R_UNKNOWN_PKEY_TYPE);
2047
0
                goto f_err;
2048
0
            }
2049
0
        }
2050
2051
0
        ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n);
2052
0
    }
2053
2054
0
    s->state = SSL3_ST_SW_KEY_EXCH_B;
2055
0
    EVP_MD_CTX_cleanup(&md_ctx);
2056
0
    return ssl_do_write(s);
2057
0
 f_err:
2058
0
    ssl3_send_alert(s, SSL3_AL_FATAL, al);
2059
0
 err:
2060
0
#ifndef OPENSSL_NO_ECDH
2061
0
    if (encodedPoint != NULL)
2062
0
        OPENSSL_free(encodedPoint);
2063
0
    BN_CTX_free(bn_ctx);
2064
0
#endif
2065
0
    EVP_MD_CTX_cleanup(&md_ctx);
2066
0
    s->state = SSL_ST_ERR;
2067
0
    return (-1);
2068
0
}
2069
2070
int ssl3_send_certificate_request(SSL *s)
2071
0
{
2072
0
    unsigned char *p, *d;
2073
0
    int i, j, nl, off, n;
2074
0
    STACK_OF(X509_NAME) *sk = NULL;
2075
0
    X509_NAME *name;
2076
0
    BUF_MEM *buf;
2077
2078
0
    if (s->state == SSL3_ST_SW_CERT_REQ_A) {
2079
0
        buf = s->init_buf;
2080
2081
0
        d = p = ssl_handshake_start(s);
2082
2083
        /* get the list of acceptable cert types */
2084
0
        p++;
2085
0
        n = ssl3_get_req_cert_type(s, p);
2086
0
        d[0] = n;
2087
0
        p += n;
2088
0
        n++;
2089
2090
0
        if (SSL_USE_SIGALGS(s)) {
2091
0
            const unsigned char *psigs;
2092
0
            nl = tls12_get_psigalgs(s, 1, &psigs);
2093
0
            s2n(nl, p);
2094
0
            memcpy(p, psigs, nl);
2095
0
            p += nl;
2096
0
            n += nl + 2;
2097
0
        }
2098
2099
0
        off = n;
2100
0
        p += 2;
2101
0
        n += 2;
2102
2103
0
        sk = SSL_get_client_CA_list(s);
2104
0
        nl = 0;
2105
0
        if (sk != NULL) {
2106
0
            for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2107
0
                name = sk_X509_NAME_value(sk, i);
2108
0
                j = i2d_X509_NAME(name, NULL);
2109
0
                if (!BUF_MEM_grow_clean
2110
0
                    (buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
2111
0
                    SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,
2112
0
                           ERR_R_BUF_LIB);
2113
0
                    goto err;
2114
0
                }
2115
0
                p = ssl_handshake_start(s) + n;
2116
0
                if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) {
2117
0
                    s2n(j, p);
2118
0
                    i2d_X509_NAME(name, &p);
2119
0
                    n += 2 + j;
2120
0
                    nl += 2 + j;
2121
0
                } else {
2122
0
                    d = p;
2123
0
                    i2d_X509_NAME(name, &p);
2124
0
                    j -= 2;
2125
0
                    s2n(j, d);
2126
0
                    j += 2;
2127
0
                    n += j;
2128
0
                    nl += j;
2129
0
                }
2130
0
            }
2131
0
        }
2132
        /* else no CA names */
2133
0
        p = ssl_handshake_start(s) + off;
2134
0
        s2n(nl, p);
2135
2136
0
        ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n);
2137
2138
0
#ifdef NETSCAPE_HANG_BUG
2139
0
        if (!SSL_IS_DTLS(s)) {
2140
0
            if (!BUF_MEM_grow_clean(buf, s->init_num + 4)) {
2141
0
                SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, ERR_R_BUF_LIB);
2142
0
                goto err;
2143
0
            }
2144
0
            p = (unsigned char *)s->init_buf->data + s->init_num;
2145
            /* do the header */
2146
0
            *(p++) = SSL3_MT_SERVER_DONE;
2147
0
            *(p++) = 0;
2148
0
            *(p++) = 0;
2149
0
            *(p++) = 0;
2150
0
            s->init_num += 4;
2151
0
        }
2152
0
#endif
2153
2154
0
        s->state = SSL3_ST_SW_CERT_REQ_B;
2155
0
    }
2156
2157
    /* SSL3_ST_SW_CERT_REQ_B */
2158
0
    return ssl_do_write(s);
2159
0
 err:
2160
0
    s->state = SSL_ST_ERR;
2161
0
    return (-1);
2162
0
}
2163
2164
int ssl3_get_client_key_exchange(SSL *s)
2165
0
{
2166
0
    int i, al, ok;
2167
0
    long n;
2168
0
    unsigned long alg_k;
2169
0
    unsigned char *p;
2170
0
#ifndef OPENSSL_NO_RSA
2171
0
    RSA *rsa = NULL;
2172
0
    EVP_PKEY *pkey = NULL;
2173
0
#endif
2174
0
#ifndef OPENSSL_NO_DH
2175
0
    BIGNUM *pub = NULL;
2176
0
    DH *dh_srvr, *dh_clnt = NULL;
2177
0
#endif
2178
#ifndef OPENSSL_NO_KRB5
2179
    KSSL_ERR kssl_err;
2180
#endif                          /* OPENSSL_NO_KRB5 */
2181
2182
0
#ifndef OPENSSL_NO_ECDH
2183
0
    EC_KEY *srvr_ecdh = NULL;
2184
0
    EVP_PKEY *clnt_pub_pkey = NULL;
2185
0
    EC_POINT *clnt_ecpoint = NULL;
2186
0
    BN_CTX *bn_ctx = NULL;
2187
0
#endif
2188
2189
0
    n = s->method->ssl_get_message(s,
2190
0
                                   SSL3_ST_SR_KEY_EXCH_A,
2191
0
                                   SSL3_ST_SR_KEY_EXCH_B,
2192
0
                                   SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok);
2193
2194
0
    if (!ok)
2195
0
        return ((int)n);
2196
0
    p = (unsigned char *)s->init_msg;
2197
2198
0
    alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2199
2200
0
#ifndef OPENSSL_NO_RSA
2201
0
    if (alg_k & SSL_kRSA) {
2202
0
        unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2203
0
        int decrypt_len;
2204
0
        unsigned char decrypt_good, version_good;
2205
0
        size_t j, padding_len;
2206
2207
        /* FIX THIS UP EAY EAY EAY EAY */
2208
0
        if (s->s3->tmp.use_rsa_tmp) {
2209
0
            if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
2210
0
                rsa = s->cert->rsa_tmp;
2211
            /*
2212
             * Don't do a callback because rsa_tmp should be sent already
2213
             */
2214
0
            if (rsa == NULL) {
2215
0
                al = SSL_AD_HANDSHAKE_FAILURE;
2216
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2217
0
                       SSL_R_MISSING_TMP_RSA_PKEY);
2218
0
                goto f_err;
2219
2220
0
            }
2221
0
        } else {
2222
0
            pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
2223
0
            if ((pkey == NULL) ||
2224
0
                (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) {
2225
0
                al = SSL_AD_HANDSHAKE_FAILURE;
2226
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2227
0
                       SSL_R_MISSING_RSA_CERTIFICATE);
2228
0
                goto f_err;
2229
0
            }
2230
0
            rsa = pkey->pkey.rsa;
2231
0
        }
2232
2233
        /* TLS and [incidentally] DTLS{0xFEFF} */
2234
0
        if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER) {
2235
0
            n2s(p, i);
2236
0
            if (n != i + 2) {
2237
0
                if (!(s->options & SSL_OP_TLS_D5_BUG)) {
2238
0
                    al = SSL_AD_DECODE_ERROR;
2239
0
                    SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2240
0
                           SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2241
0
                    goto f_err;
2242
0
                } else
2243
0
                    p -= 2;
2244
0
            } else
2245
0
                n = i;
2246
0
        }
2247
2248
        /*
2249
         * Reject overly short RSA ciphertext because we want to be sure
2250
         * that the buffer size makes it safe to iterate over the entire
2251
         * size of a premaster secret (SSL_MAX_MASTER_KEY_LENGTH). The
2252
         * actual expected size is larger due to RSA padding, but the
2253
         * bound is sufficient to be safe.
2254
         */
2255
0
        if (n < SSL_MAX_MASTER_KEY_LENGTH) {
2256
0
            al = SSL_AD_DECRYPT_ERROR;
2257
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2258
0
                   SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2259
0
            goto f_err;
2260
0
        }
2261
2262
        /*
2263
         * We must not leak whether a decryption failure occurs because of
2264
         * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2265
         * section 7.4.7.1). The code follows that advice of the TLS RFC and
2266
         * generates a random premaster secret for the case that the decrypt
2267
         * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2268
         */
2269
2270
0
        if (RAND_bytes(rand_premaster_secret,
2271
0
                       sizeof(rand_premaster_secret)) <= 0)
2272
0
            goto err;
2273
2274
        /*
2275
         * Decrypt with no padding. PKCS#1 padding will be removed as part of
2276
         * the timing-sensitive code below.
2277
         */
2278
0
        decrypt_len =
2279
0
            RSA_private_decrypt((int)n, p, p, rsa, RSA_NO_PADDING);
2280
0
        if (decrypt_len < 0)
2281
0
            goto err;
2282
2283
        /* Check the padding. See RFC 3447, section 7.2.2. */
2284
2285
        /*
2286
         * The smallest padded premaster is 11 bytes of overhead. Small keys
2287
         * are publicly invalid, so this may return immediately. This ensures
2288
         * PS is at least 8 bytes.
2289
         */
2290
0
        if (decrypt_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) {
2291
0
            al = SSL_AD_DECRYPT_ERROR;
2292
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2293
0
                   SSL_R_DECRYPTION_FAILED);
2294
0
            goto f_err;
2295
0
        }
2296
2297
0
        padding_len = decrypt_len - SSL_MAX_MASTER_KEY_LENGTH;
2298
0
        decrypt_good = constant_time_eq_int_8(p[0], 0) &
2299
0
                       constant_time_eq_int_8(p[1], 2);
2300
0
        for (j = 2; j < padding_len - 1; j++) {
2301
0
            decrypt_good &= ~constant_time_is_zero_8(p[j]);
2302
0
        }
2303
0
        decrypt_good &= constant_time_is_zero_8(p[padding_len - 1]);
2304
0
        p += padding_len;
2305
2306
        /*
2307
         * If the version in the decrypted pre-master secret is correct then
2308
         * version_good will be 0xff, otherwise it'll be zero. The
2309
         * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2310
         * (http://eprint.iacr.org/2003/052/) exploits the version number
2311
         * check as a "bad version oracle". Thus version checks are done in
2312
         * constant time and are treated like any other decryption error.
2313
         */
2314
0
        version_good =
2315
0
            constant_time_eq_8(p[0], (unsigned)(s->client_version >> 8));
2316
0
        version_good &=
2317
0
            constant_time_eq_8(p[1], (unsigned)(s->client_version & 0xff));
2318
2319
        /*
2320
         * The premaster secret must contain the same version number as the
2321
         * ClientHello to detect version rollback attacks (strangely, the
2322
         * protocol does not offer such protection for DH ciphersuites).
2323
         * However, buggy clients exist that send the negotiated protocol
2324
         * version instead if the server does not support the requested
2325
         * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2326
         * clients.
2327
         */
2328
0
        if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2329
0
            unsigned char workaround_good;
2330
0
            workaround_good =
2331
0
                constant_time_eq_8(p[0], (unsigned)(s->version >> 8));
2332
0
            workaround_good &=
2333
0
                constant_time_eq_8(p[1], (unsigned)(s->version & 0xff));
2334
0
            version_good |= workaround_good;
2335
0
        }
2336
2337
        /*
2338
         * Both decryption and version must be good for decrypt_good to
2339
         * remain non-zero (0xff).
2340
         */
2341
0
        decrypt_good &= version_good;
2342
2343
        /*
2344
         * Now copy rand_premaster_secret over from p using
2345
         * decrypt_good_mask. If decryption failed, then p does not
2346
         * contain valid plaintext, however, a check above guarantees
2347
         * it is still sufficiently large to read from.
2348
         */
2349
0
        for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2350
0
            p[j] = constant_time_select_8(decrypt_good, p[j],
2351
0
                                          rand_premaster_secret[j]);
2352
0
        }
2353
2354
0
        s->session->master_key_length =
2355
0
            s->method->ssl3_enc->generate_master_secret(s,
2356
0
                                                        s->
2357
0
                                                        session->master_key,
2358
0
                                                        p,
2359
0
                                                        sizeof
2360
0
                                                        (rand_premaster_secret));
2361
0
        OPENSSL_cleanse(p, sizeof(rand_premaster_secret));
2362
0
    } else
2363
0
#endif
2364
0
#ifndef OPENSSL_NO_DH
2365
0
    if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
2366
0
        int idx = -1;
2367
0
        EVP_PKEY *skey = NULL;
2368
0
        if (n > 1) {
2369
0
            n2s(p, i);
2370
0
        } else {
2371
0
            if (alg_k & SSL_kDHE) {
2372
0
                al = SSL_AD_HANDSHAKE_FAILURE;
2373
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2374
0
                       SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2375
0
                goto f_err;
2376
0
            }
2377
0
            i = 0;
2378
0
        }
2379
0
        if (n && n != i + 2) {
2380
0
            if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) {
2381
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2382
0
                       SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2383
0
                al = SSL_AD_HANDSHAKE_FAILURE;
2384
0
                goto f_err;
2385
0
            } else {
2386
0
                p -= 2;
2387
0
                i = (int)n;
2388
0
            }
2389
0
        }
2390
0
        if (alg_k & SSL_kDHr)
2391
0
            idx = SSL_PKEY_DH_RSA;
2392
0
        else if (alg_k & SSL_kDHd)
2393
0
            idx = SSL_PKEY_DH_DSA;
2394
0
        if (idx >= 0) {
2395
0
            skey = s->cert->pkeys[idx].privatekey;
2396
0
            if ((skey == NULL) ||
2397
0
                (skey->type != EVP_PKEY_DH) || (skey->pkey.dh == NULL)) {
2398
0
                al = SSL_AD_HANDSHAKE_FAILURE;
2399
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2400
0
                       SSL_R_MISSING_RSA_CERTIFICATE);
2401
0
                goto f_err;
2402
0
            }
2403
0
            dh_srvr = skey->pkey.dh;
2404
0
        } else if (s->s3->tmp.dh == NULL) {
2405
0
            al = SSL_AD_HANDSHAKE_FAILURE;
2406
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2407
0
                   SSL_R_MISSING_TMP_DH_KEY);
2408
0
            goto f_err;
2409
0
        } else
2410
0
            dh_srvr = s->s3->tmp.dh;
2411
2412
0
        if (n == 0L) {
2413
            /* Get pubkey from cert */
2414
0
            EVP_PKEY *clkey = X509_get_pubkey(s->session->peer);
2415
0
            if (clkey) {
2416
0
                if (EVP_PKEY_cmp_parameters(clkey, skey) == 1)
2417
0
                    dh_clnt = EVP_PKEY_get1_DH(clkey);
2418
0
            }
2419
0
            if (dh_clnt == NULL) {
2420
0
                al = SSL_AD_HANDSHAKE_FAILURE;
2421
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2422
0
                       SSL_R_MISSING_TMP_DH_KEY);
2423
0
                goto f_err;
2424
0
            }
2425
0
            EVP_PKEY_free(clkey);
2426
0
            pub = dh_clnt->pub_key;
2427
0
        } else
2428
0
            pub = BN_bin2bn(p, i, NULL);
2429
0
        if (pub == NULL) {
2430
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
2431
0
            goto err;
2432
0
        }
2433
2434
0
        i = DH_compute_key(p, pub, dh_srvr);
2435
2436
0
        if (i <= 0) {
2437
0
            al = SSL_AD_HANDSHAKE_FAILURE;
2438
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2439
0
            BN_clear_free(pub);
2440
0
            goto f_err;
2441
0
        }
2442
2443
0
        DH_free(s->s3->tmp.dh);
2444
0
        s->s3->tmp.dh = NULL;
2445
0
        if (dh_clnt)
2446
0
            DH_free(dh_clnt);
2447
0
        else
2448
0
            BN_clear_free(pub);
2449
0
        pub = NULL;
2450
0
        s->session->master_key_length =
2451
0
            s->method->ssl3_enc->generate_master_secret(s,
2452
0
                                                        s->
2453
0
                                                        session->master_key,
2454
0
                                                        p, i);
2455
0
        OPENSSL_cleanse(p, i);
2456
0
        if (dh_clnt)
2457
0
            return 2;
2458
0
    } else
2459
0
#endif
2460
#ifndef OPENSSL_NO_KRB5
2461
    if (alg_k & SSL_kKRB5) {
2462
        krb5_error_code krb5rc;
2463
        krb5_data enc_ticket;
2464
        krb5_data authenticator;
2465
        krb5_data enc_pms;
2466
        KSSL_CTX *kssl_ctx = s->kssl_ctx;
2467
        EVP_CIPHER_CTX ciph_ctx;
2468
        const EVP_CIPHER *enc = NULL;
2469
        unsigned char iv[EVP_MAX_IV_LENGTH];
2470
        unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_BLOCK_LENGTH];
2471
        int padl, outl;
2472
        krb5_timestamp authtime = 0;
2473
        krb5_ticket_times ttimes;
2474
        int kerr = 0;
2475
2476
        EVP_CIPHER_CTX_init(&ciph_ctx);
2477
2478
        if (!kssl_ctx)
2479
            kssl_ctx = kssl_ctx_new();
2480
2481
        n2s(p, i);
2482
        enc_ticket.length = i;
2483
2484
        if (n < (long)(enc_ticket.length + 6)) {
2485
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2486
                   SSL_R_DATA_LENGTH_TOO_LONG);
2487
            goto err;
2488
        }
2489
2490
        enc_ticket.data = (char *)p;
2491
        p += enc_ticket.length;
2492
2493
        n2s(p, i);
2494
        authenticator.length = i;
2495
2496
        if (n < (long)(enc_ticket.length + authenticator.length + 6)) {
2497
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2498
                   SSL_R_DATA_LENGTH_TOO_LONG);
2499
            goto err;
2500
        }
2501
2502
        authenticator.data = (char *)p;
2503
        p += authenticator.length;
2504
2505
        n2s(p, i);
2506
        enc_pms.length = i;
2507
        enc_pms.data = (char *)p;
2508
        p += enc_pms.length;
2509
2510
        /*
2511
         * Note that the length is checked again below, ** after decryption
2512
         */
2513
        if (enc_pms.length > sizeof pms) {
2514
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2515
                   SSL_R_DATA_LENGTH_TOO_LONG);
2516
            goto err;
2517
        }
2518
2519
        if (n != (long)(enc_ticket.length + authenticator.length +
2520
                        enc_pms.length + 6)) {
2521
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2522
                   SSL_R_DATA_LENGTH_TOO_LONG);
2523
            goto err;
2524
        }
2525
2526
        if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
2527
                                    &kssl_err)) != 0) {
2528
# ifdef KSSL_DEBUG
2529
            fprintf(stderr, "kssl_sget_tkt rtn %d [%d]\n",
2530
                    krb5rc, kssl_err.reason);
2531
            if (kssl_err.text)
2532
                fprintf(stderr, "kssl_err text= %s\n", kssl_err.text);
2533
# endif                         /* KSSL_DEBUG */
2534
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2535
            goto err;
2536
        }
2537
2538
        /*
2539
         * Note: no authenticator is not considered an error, ** but will
2540
         * return authtime == 0.
2541
         */
2542
        if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
2543
                                         &authtime, &kssl_err)) != 0) {
2544
# ifdef KSSL_DEBUG
2545
            fprintf(stderr, "kssl_check_authent rtn %d [%d]\n",
2546
                    krb5rc, kssl_err.reason);
2547
            if (kssl_err.text)
2548
                fprintf(stderr, "kssl_err text= %s\n", kssl_err.text);
2549
# endif                         /* KSSL_DEBUG */
2550
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2551
            goto err;
2552
        }
2553
2554
        if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0) {
2555
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
2556
            goto err;
2557
        }
2558
# ifdef KSSL_DEBUG
2559
        kssl_ctx_show(kssl_ctx);
2560
# endif                         /* KSSL_DEBUG */
2561
2562
        enc = kssl_map_enc(kssl_ctx->enctype);
2563
        if (enc == NULL)
2564
            goto err;
2565
2566
        memset(iv, 0, sizeof iv); /* per RFC 1510 */
2567
2568
        if (!EVP_DecryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv)) {
2569
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2570
                   SSL_R_DECRYPTION_FAILED);
2571
            goto err;
2572
        }
2573
        if (!EVP_DecryptUpdate(&ciph_ctx, pms, &outl,
2574
                               (unsigned char *)enc_pms.data, enc_pms.length))
2575
        {
2576
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2577
                   SSL_R_DECRYPTION_FAILED);
2578
            kerr = 1;
2579
            goto kclean;
2580
        }
2581
        if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2582
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2583
                   SSL_R_DATA_LENGTH_TOO_LONG);
2584
            kerr = 1;
2585
            goto kclean;
2586
        }
2587
        if (!EVP_DecryptFinal_ex(&ciph_ctx, &(pms[outl]), &padl)) {
2588
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2589
                   SSL_R_DECRYPTION_FAILED);
2590
            kerr = 1;
2591
            goto kclean;
2592
        }
2593
        outl += padl;
2594
        if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2595
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2596
                   SSL_R_DATA_LENGTH_TOO_LONG);
2597
            kerr = 1;
2598
            goto kclean;
2599
        }
2600
        if (!((pms[0] == (s->client_version >> 8))
2601
              && (pms[1] == (s->client_version & 0xff)))) {
2602
            /*
2603
             * The premaster secret must contain the same version number as
2604
             * the ClientHello to detect version rollback attacks (strangely,
2605
             * the protocol does not offer such protection for DH
2606
             * ciphersuites). However, buggy clients exist that send random
2607
             * bytes instead of the protocol version. If
2608
             * SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
2609
             * (Perhaps we should have a separate BUG value for the Kerberos
2610
             * cipher)
2611
             */
2612
            if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG)) {
2613
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2614
                       SSL_AD_DECODE_ERROR);
2615
                kerr = 1;
2616
                goto kclean;
2617
            }
2618
        }
2619
2620
        EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2621
2622
        s->session->master_key_length =
2623
            s->method->ssl3_enc->generate_master_secret(s,
2624
                                                        s->
2625
                                                        session->master_key,
2626
                                                        pms, outl);
2627
2628
        if (kssl_ctx->client_princ) {
2629
            size_t len = strlen(kssl_ctx->client_princ);
2630
            if (len < SSL_MAX_KRB5_PRINCIPAL_LENGTH) {
2631
                s->session->krb5_client_princ_len = len;
2632
                memcpy(s->session->krb5_client_princ, kssl_ctx->client_princ,
2633
                       len);
2634
            }
2635
        }
2636
2637
        /*- Was doing kssl_ctx_free() here,
2638
         *  but it caused problems for apache.
2639
         *  kssl_ctx = kssl_ctx_free(kssl_ctx);
2640
         *  if (s->kssl_ctx)  s->kssl_ctx = NULL;
2641
         */
2642
2643
 kclean:
2644
        OPENSSL_cleanse(pms, sizeof(pms));
2645
        if (kerr)
2646
            goto err;
2647
    } else
2648
#endif                          /* OPENSSL_NO_KRB5 */
2649
2650
0
#ifndef OPENSSL_NO_ECDH
2651
0
    if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
2652
0
        int ret = 1;
2653
0
        int field_size = 0;
2654
0
        const EC_KEY *tkey;
2655
0
        const EC_GROUP *group;
2656
0
        const BIGNUM *priv_key;
2657
2658
        /* initialize structures for server's ECDH key pair */
2659
0
        if ((srvr_ecdh = EC_KEY_new()) == NULL) {
2660
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2661
0
            goto err;
2662
0
        }
2663
2664
        /* Let's get server private key and group information */
2665
0
        if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2666
            /* use the certificate */
2667
0
            tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2668
0
        } else {
2669
            /*
2670
             * use the ephermeral values we saved when generating the
2671
             * ServerKeyExchange msg.
2672
             */
2673
0
            tkey = s->s3->tmp.ecdh;
2674
0
        }
2675
2676
0
        group = EC_KEY_get0_group(tkey);
2677
0
        priv_key = EC_KEY_get0_private_key(tkey);
2678
2679
0
        if (!EC_KEY_set_group(srvr_ecdh, group) ||
2680
0
            !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
2681
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2682
0
            goto err;
2683
0
        }
2684
2685
        /* Let's get client's public key */
2686
0
        if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
2687
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2688
0
            goto err;
2689
0
        }
2690
2691
0
        if (n == 0L) {
2692
            /* Client Publickey was in Client Certificate */
2693
2694
0
            if (alg_k & SSL_kEECDH) {
2695
0
                al = SSL_AD_HANDSHAKE_FAILURE;
2696
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2697
0
                       SSL_R_MISSING_TMP_ECDH_KEY);
2698
0
                goto f_err;
2699
0
            }
2700
0
            if (((clnt_pub_pkey = X509_get_pubkey(s->session->peer))
2701
0
                 == NULL) || (clnt_pub_pkey->type != EVP_PKEY_EC)) {
2702
                /*
2703
                 * XXX: For now, we do not support client authentication
2704
                 * using ECDH certificates so this branch (n == 0L) of the
2705
                 * code is never executed. When that support is added, we
2706
                 * ought to ensure the key received in the certificate is
2707
                 * authorized for key agreement. ECDH_compute_key implicitly
2708
                 * checks that the two ECDH shares are for the same group.
2709
                 */
2710
0
                al = SSL_AD_HANDSHAKE_FAILURE;
2711
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2712
0
                       SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2713
0
                goto f_err;
2714
0
            }
2715
2716
0
            if (EC_POINT_copy(clnt_ecpoint,
2717
0
                              EC_KEY_get0_public_key(clnt_pub_pkey->
2718
0
                                                     pkey.ec)) == 0) {
2719
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2720
0
                goto err;
2721
0
            }
2722
0
            ret = 2;            /* Skip certificate verify processing */
2723
0
        } else {
2724
            /*
2725
             * Get client's public key from encoded point in the
2726
             * ClientKeyExchange message.
2727
             */
2728
0
            if ((bn_ctx = BN_CTX_new()) == NULL) {
2729
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2730
0
                       ERR_R_MALLOC_FAILURE);
2731
0
                goto err;
2732
0
            }
2733
2734
            /* Get encoded point length */
2735
0
            i = *p;
2736
0
            p += 1;
2737
0
            if (n != 1 + i) {
2738
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2739
0
                al = SSL_AD_DECODE_ERROR;
2740
0
                goto f_err;
2741
0
            }
2742
0
            if (EC_POINT_oct2point(group, clnt_ecpoint, p, i, bn_ctx) == 0) {
2743
0
                SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2744
0
                al = SSL_AD_HANDSHAKE_FAILURE;
2745
0
                goto f_err;
2746
0
            }
2747
            /*
2748
             * p is pointing to somewhere in the buffer currently, so set it
2749
             * to the start
2750
             */
2751
0
            p = (unsigned char *)s->init_buf->data;
2752
0
        }
2753
2754
        /* Compute the shared pre-master secret */
2755
0
        field_size = EC_GROUP_get_degree(group);
2756
0
        if (field_size <= 0) {
2757
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2758
0
            goto err;
2759
0
        }
2760
0
        i = ECDH_compute_key(p, (field_size + 7) / 8, clnt_ecpoint, srvr_ecdh,
2761
0
                             NULL);
2762
0
        if (i <= 0) {
2763
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2764
0
            goto err;
2765
0
        }
2766
2767
0
        EVP_PKEY_free(clnt_pub_pkey);
2768
0
        EC_POINT_free(clnt_ecpoint);
2769
0
        EC_KEY_free(srvr_ecdh);
2770
0
        BN_CTX_free(bn_ctx);
2771
0
        EC_KEY_free(s->s3->tmp.ecdh);
2772
0
        s->s3->tmp.ecdh = NULL;
2773
2774
        /* Compute the master secret */
2775
0
        s->session->master_key_length =
2776
0
            s->method->ssl3_enc->generate_master_secret(s,
2777
0
                                                        s->
2778
0
                                                        session->master_key,
2779
0
                                                        p, i);
2780
2781
0
        OPENSSL_cleanse(p, i);
2782
0
        return (ret);
2783
0
    } else
2784
0
#endif
2785
0
#ifndef OPENSSL_NO_PSK
2786
0
    if (alg_k & SSL_kPSK) {
2787
0
        unsigned char *t = NULL;
2788
0
        unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
2789
0
        unsigned int pre_ms_len = 0, psk_len = 0;
2790
0
        int psk_err = 1;
2791
0
        char tmp_id[PSK_MAX_IDENTITY_LEN + 1];
2792
2793
0
        al = SSL_AD_HANDSHAKE_FAILURE;
2794
2795
0
        n2s(p, i);
2796
0
        if (n != i + 2) {
2797
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2798
0
            goto psk_err;
2799
0
        }
2800
0
        if (i > PSK_MAX_IDENTITY_LEN) {
2801
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2802
0
                   SSL_R_DATA_LENGTH_TOO_LONG);
2803
0
            goto psk_err;
2804
0
        }
2805
0
        if (s->psk_server_callback == NULL) {
2806
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2807
0
                   SSL_R_PSK_NO_SERVER_CB);
2808
0
            goto psk_err;
2809
0
        }
2810
2811
        /*
2812
         * Create guaranteed NULL-terminated identity string for the callback
2813
         */
2814
0
        memcpy(tmp_id, p, i);
2815
0
        memset(tmp_id + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
2816
0
        psk_len = s->psk_server_callback(s, tmp_id,
2817
0
                                         psk_or_pre_ms,
2818
0
                                         sizeof(psk_or_pre_ms));
2819
0
        OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN + 1);
2820
2821
0
        if (psk_len > PSK_MAX_PSK_LEN) {
2822
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2823
0
            goto psk_err;
2824
0
        } else if (psk_len == 0) {
2825
            /*
2826
             * PSK related to the given identity not found
2827
             */
2828
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2829
0
                   SSL_R_PSK_IDENTITY_NOT_FOUND);
2830
0
            al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2831
0
            goto psk_err;
2832
0
        }
2833
2834
        /* create PSK pre_master_secret */
2835
0
        pre_ms_len = 2 + psk_len + 2 + psk_len;
2836
0
        t = psk_or_pre_ms;
2837
0
        memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
2838
0
        s2n(psk_len, t);
2839
0
        memset(t, 0, psk_len);
2840
0
        t += psk_len;
2841
0
        s2n(psk_len, t);
2842
2843
0
        if (s->session->psk_identity != NULL)
2844
0
            OPENSSL_free(s->session->psk_identity);
2845
0
        s->session->psk_identity = BUF_strndup((char *)p, i);
2846
0
        if (s->session->psk_identity == NULL) {
2847
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2848
0
            goto psk_err;
2849
0
        }
2850
2851
0
        if (s->session->psk_identity_hint != NULL)
2852
0
            OPENSSL_free(s->session->psk_identity_hint);
2853
0
        s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2854
0
        if (s->ctx->psk_identity_hint != NULL &&
2855
0
            s->session->psk_identity_hint == NULL) {
2856
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2857
0
            goto psk_err;
2858
0
        }
2859
2860
0
        s->session->master_key_length =
2861
0
            s->method->ssl3_enc->generate_master_secret(s,
2862
0
                                                        s->
2863
0
                                                        session->master_key,
2864
0
                                                        psk_or_pre_ms,
2865
0
                                                        pre_ms_len);
2866
0
        psk_err = 0;
2867
0
 psk_err:
2868
0
        OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2869
0
        if (psk_err != 0)
2870
0
            goto f_err;
2871
0
    } else
2872
0
#endif
2873
0
#ifndef OPENSSL_NO_SRP
2874
0
    if (alg_k & SSL_kSRP) {
2875
0
        int param_len;
2876
2877
0
        n2s(p, i);
2878
0
        param_len = i + 2;
2879
0
        if (param_len > n) {
2880
0
            al = SSL_AD_DECODE_ERROR;
2881
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2882
0
                   SSL_R_BAD_SRP_A_LENGTH);
2883
0
            goto f_err;
2884
0
        }
2885
0
        if (!(s->srp_ctx.A = BN_bin2bn(p, i, NULL))) {
2886
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_BN_LIB);
2887
0
            goto err;
2888
0
        }
2889
0
        if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0
2890
0
            || BN_is_zero(s->srp_ctx.A)) {
2891
0
            al = SSL_AD_ILLEGAL_PARAMETER;
2892
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2893
0
                   SSL_R_BAD_SRP_PARAMETERS);
2894
0
            goto f_err;
2895
0
        }
2896
0
        if (s->session->srp_username != NULL)
2897
0
            OPENSSL_free(s->session->srp_username);
2898
0
        s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2899
0
        if (s->session->srp_username == NULL) {
2900
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2901
0
            goto err;
2902
0
        }
2903
2904
0
        if ((s->session->master_key_length =
2905
0
             SRP_generate_server_master_secret(s,
2906
0
                                               s->session->master_key)) < 0) {
2907
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2908
0
            goto err;
2909
0
        }
2910
2911
0
        p += i;
2912
0
    } else
2913
0
#endif                          /* OPENSSL_NO_SRP */
2914
0
    if (alg_k & SSL_kGOST) {
2915
0
        int ret = 0;
2916
0
        EVP_PKEY_CTX *pkey_ctx;
2917
0
        EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2918
0
        unsigned char premaster_secret[32], *start;
2919
0
        size_t outlen = 32, inlen;
2920
0
        unsigned long alg_a;
2921
0
        int Ttag, Tclass;
2922
0
        long Tlen;
2923
2924
        /* Get our certificate private key */
2925
0
        alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2926
0
        if (alg_a & SSL_aGOST94)
2927
0
            pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey;
2928
0
        else if (alg_a & SSL_aGOST01)
2929
0
            pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2930
2931
0
        pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2932
0
        if (pkey_ctx == NULL) {
2933
0
            al = SSL_AD_INTERNAL_ERROR;
2934
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2935
0
            goto f_err;
2936
0
        }
2937
0
        if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) {
2938
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2939
0
            goto gerr;
2940
0
        }
2941
        /*
2942
         * If client certificate is present and is of the same type, maybe
2943
         * use it for key exchange.  Don't mind errors from
2944
         * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2945
         * client certificate for authorization only.
2946
         */
2947
0
        client_pub_pkey = X509_get_pubkey(s->session->peer);
2948
0
        if (client_pub_pkey) {
2949
0
            if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2950
0
                ERR_clear_error();
2951
0
        }
2952
        /* Decrypt session key */
2953
0
        if (ASN1_get_object
2954
0
            ((const unsigned char **)&p, &Tlen, &Ttag, &Tclass,
2955
0
             n) != V_ASN1_CONSTRUCTED || Ttag != V_ASN1_SEQUENCE
2956
0
            || Tclass != V_ASN1_UNIVERSAL) {
2957
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2958
0
                   SSL_R_DECRYPTION_FAILED);
2959
0
            goto gerr;
2960
0
        }
2961
0
        start = p;
2962
0
        inlen = Tlen;
2963
0
        if (EVP_PKEY_decrypt
2964
0
            (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
2965
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2966
0
                   SSL_R_DECRYPTION_FAILED);
2967
0
            goto gerr;
2968
0
        }
2969
        /* Generate master secret */
2970
0
        s->session->master_key_length =
2971
0
            s->method->ssl3_enc->generate_master_secret(s,
2972
0
                                                        s->
2973
0
                                                        session->master_key,
2974
0
                                                        premaster_secret, 32);
2975
0
        OPENSSL_cleanse(premaster_secret, sizeof(premaster_secret));
2976
        /* Check if pubkey from client certificate was used */
2977
0
        if (EVP_PKEY_CTX_ctrl
2978
0
            (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2979
0
            ret = 2;
2980
0
        else
2981
0
            ret = 1;
2982
0
 gerr:
2983
0
        EVP_PKEY_free(client_pub_pkey);
2984
0
        EVP_PKEY_CTX_free(pkey_ctx);
2985
0
        if (ret)
2986
0
            return ret;
2987
0
        else
2988
0
            goto err;
2989
0
    } else {
2990
0
        al = SSL_AD_HANDSHAKE_FAILURE;
2991
0
        SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
2992
0
        goto f_err;
2993
0
    }
2994
2995
0
    return (1);
2996
0
 f_err:
2997
0
    ssl3_send_alert(s, SSL3_AL_FATAL, al);
2998
0
#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH) || defined(OPENSSL_NO_SRP)
2999
0
 err:
3000
0
#endif
3001
0
#ifndef OPENSSL_NO_ECDH
3002
0
    EVP_PKEY_free(clnt_pub_pkey);
3003
0
    EC_POINT_free(clnt_ecpoint);
3004
0
    if (srvr_ecdh != NULL)
3005
0
        EC_KEY_free(srvr_ecdh);
3006
0
    BN_CTX_free(bn_ctx);
3007
0
#endif
3008
0
    s->state = SSL_ST_ERR;
3009
0
    return (-1);
3010
0
}
3011
3012
int ssl3_get_cert_verify(SSL *s)
3013
0
{
3014
0
    EVP_PKEY *pkey = NULL;
3015
0
    unsigned char *p;
3016
0
    int al, ok, ret = 0;
3017
0
    long n;
3018
0
    int type = 0, i, j;
3019
0
    X509 *peer;
3020
0
    const EVP_MD *md = NULL;
3021
0
    EVP_MD_CTX mctx;
3022
0
    EVP_MD_CTX_init(&mctx);
3023
3024
    /*
3025
     * We should only process a CertificateVerify message if we have received
3026
     * a Certificate from the client. If so then |s->session->peer| will be non
3027
     * NULL. In some instances a CertificateVerify message is not required even
3028
     * if the peer has sent a Certificate (e.g. such as in the case of static
3029
     * DH). In that case the ClientKeyExchange processing will skip the
3030
     * CertificateVerify state so we should not arrive here.
3031
     */
3032
0
    if (s->session->peer == NULL) {
3033
0
        ret = 1;
3034
0
        goto end;
3035
0
    }
3036
3037
0
    n = s->method->ssl_get_message(s,
3038
0
                                   SSL3_ST_SR_CERT_VRFY_A,
3039
0
                                   SSL3_ST_SR_CERT_VRFY_B,
3040
0
                                   SSL3_MT_CERTIFICATE_VERIFY,
3041
0
                                   SSL3_RT_MAX_PLAIN_LENGTH, &ok);
3042
3043
0
    if (!ok)
3044
0
        return ((int)n);
3045
3046
0
    peer = s->session->peer;
3047
0
    pkey = X509_get_pubkey(peer);
3048
0
    if (pkey == NULL) {
3049
0
        al = SSL_AD_INTERNAL_ERROR;
3050
0
        goto f_err;
3051
0
    }
3052
3053
0
    type = X509_certificate_type(peer, pkey);
3054
3055
0
    if (!(type & EVP_PKT_SIGN)) {
3056
0
        SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
3057
0
               SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
3058
0
        al = SSL_AD_ILLEGAL_PARAMETER;
3059
0
        goto f_err;
3060
0
    }
3061
3062
    /* we now have a signature that we need to verify */
3063
0
    p = (unsigned char *)s->init_msg;
3064
    /* Check for broken implementations of GOST ciphersuites */
3065
    /*
3066
     * If key is GOST and n is exactly 64, it is bare signature without
3067
     * length field
3068
     */
3069
0
    if (n == 64 && (pkey->type == NID_id_GostR3410_94 ||
3070
0
                    pkey->type == NID_id_GostR3410_2001)) {
3071
0
        i = 64;
3072
0
    } else {
3073
0
        if (SSL_USE_SIGALGS(s)) {
3074
0
            int rv = tls12_check_peer_sigalg(&md, s, p, pkey);
3075
0
            if (rv == -1) {
3076
0
                al = SSL_AD_INTERNAL_ERROR;
3077
0
                goto f_err;
3078
0
            } else if (rv == 0) {
3079
0
                al = SSL_AD_DECODE_ERROR;
3080
0
                goto f_err;
3081
0
            }
3082
#ifdef SSL_DEBUG
3083
            fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
3084
#endif
3085
0
            p += 2;
3086
0
            n -= 2;
3087
0
        }
3088
0
        n2s(p, i);
3089
0
        n -= 2;
3090
0
        if (i > n) {
3091
0
            SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
3092
0
            al = SSL_AD_DECODE_ERROR;
3093
0
            goto f_err;
3094
0
        }
3095
0
    }
3096
0
    j = EVP_PKEY_size(pkey);
3097
0
    if ((i > j) || (n > j) || (n <= 0)) {
3098
0
        SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
3099
0
        al = SSL_AD_DECODE_ERROR;
3100
0
        goto f_err;
3101
0
    }
3102
3103
0
    if (SSL_USE_SIGALGS(s)) {
3104
0
        long hdatalen = 0;
3105
0
        void *hdata;
3106
0
        hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3107
0
        if (hdatalen <= 0) {
3108
0
            SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3109
0
            al = SSL_AD_INTERNAL_ERROR;
3110
0
            goto f_err;
3111
0
        }
3112
#ifdef SSL_DEBUG
3113
        fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n",
3114
                EVP_MD_name(md));
3115
#endif
3116
0
        if (!EVP_VerifyInit_ex(&mctx, md, NULL)
3117
0
            || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) {
3118
0
            SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_EVP_LIB);
3119
0
            al = SSL_AD_INTERNAL_ERROR;
3120
0
            goto f_err;
3121
0
        }
3122
3123
0
        if (EVP_VerifyFinal(&mctx, p, i, pkey) <= 0) {
3124
0
            al = SSL_AD_DECRYPT_ERROR;
3125
0
            SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
3126
0
            goto f_err;
3127
0
        }
3128
0
    } else
3129
0
#ifndef OPENSSL_NO_RSA
3130
0
    if (pkey->type == EVP_PKEY_RSA) {
3131
0
        i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
3132
0
                       MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i,
3133
0
                       pkey->pkey.rsa);
3134
0
        if (i < 0) {
3135
0
            al = SSL_AD_DECRYPT_ERROR;
3136
0
            SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_DECRYPT);
3137
0
            goto f_err;
3138
0
        }
3139
0
        if (i == 0) {
3140
0
            al = SSL_AD_DECRYPT_ERROR;
3141
0
            SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_SIGNATURE);
3142
0
            goto f_err;
3143
0
        }
3144
0
    } else
3145
0
#endif
3146
0
#ifndef OPENSSL_NO_DSA
3147
0
    if (pkey->type == EVP_PKEY_DSA) {
3148
0
        j = DSA_verify(pkey->save_type,
3149
0
                       &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3150
0
                       SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa);
3151
0
        if (j <= 0) {
3152
            /* bad signature */
3153
0
            al = SSL_AD_DECRYPT_ERROR;
3154
0
            SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_DSA_SIGNATURE);
3155
0
            goto f_err;
3156
0
        }
3157
0
    } else
3158
0
#endif
3159
0
#ifndef OPENSSL_NO_ECDSA
3160
0
    if (pkey->type == EVP_PKEY_EC) {
3161
0
        j = ECDSA_verify(pkey->save_type,
3162
0
                         &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3163
0
                         SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec);
3164
0
        if (j <= 0) {
3165
            /* bad signature */
3166
0
            al = SSL_AD_DECRYPT_ERROR;
3167
0
            SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3168
0
            goto f_err;
3169
0
        }
3170
0
    } else
3171
0
#endif
3172
0
    if (pkey->type == NID_id_GostR3410_94
3173
0
            || pkey->type == NID_id_GostR3410_2001) {
3174
0
        unsigned char signature[64];
3175
0
        int idx;
3176
0
        EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
3177
0
        if (pctx == NULL) {
3178
0
            al = SSL_AD_INTERNAL_ERROR;
3179
0
            SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
3180
0
            goto f_err;
3181
0
        }
3182
0
        if (EVP_PKEY_verify_init(pctx) <= 0) {
3183
0
            EVP_PKEY_CTX_free(pctx);
3184
0
            al = SSL_AD_INTERNAL_ERROR;
3185
0
            SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3186
0
            goto f_err;
3187
0
        }
3188
0
        if (i != 64) {
3189
#ifdef SSL_DEBUG
3190
            fprintf(stderr, "GOST signature length is %d", i);
3191
#endif
3192
0
        }
3193
0
        for (idx = 0; idx < 64; idx++) {
3194
0
            signature[63 - idx] = p[idx];
3195
0
        }
3196
0
        j = EVP_PKEY_verify(pctx, signature, 64, s->s3->tmp.cert_verify_md,
3197
0
                            32);
3198
0
        EVP_PKEY_CTX_free(pctx);
3199
0
        if (j <= 0) {
3200
0
            al = SSL_AD_DECRYPT_ERROR;
3201
0
            SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3202
0
            goto f_err;
3203
0
        }
3204
0
    } else {
3205
0
        SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3206
0
        al = SSL_AD_UNSUPPORTED_CERTIFICATE;
3207
0
        goto f_err;
3208
0
    }
3209
3210
0
    ret = 1;
3211
0
    if (0) {
3212
0
 f_err:
3213
0
        ssl3_send_alert(s, SSL3_AL_FATAL, al);
3214
0
        s->state = SSL_ST_ERR;
3215
0
    }
3216
0
 end:
3217
0
    if (s->s3->handshake_buffer) {
3218
0
        BIO_free(s->s3->handshake_buffer);
3219
0
        s->s3->handshake_buffer = NULL;
3220
0
        s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
3221
0
    }
3222
0
    EVP_MD_CTX_cleanup(&mctx);
3223
0
    EVP_PKEY_free(pkey);
3224
0
    return (ret);
3225
0
}
3226
3227
int ssl3_get_client_certificate(SSL *s)
3228
0
{
3229
0
    int i, ok, al, ret = -1;
3230
0
    X509 *x = NULL;
3231
0
    unsigned long l, nc, llen, n;
3232
0
    const unsigned char *p, *q;
3233
0
    unsigned char *d;
3234
0
    STACK_OF(X509) *sk = NULL;
3235
3236
0
    n = s->method->ssl_get_message(s,
3237
0
                                   SSL3_ST_SR_CERT_A,
3238
0
                                   SSL3_ST_SR_CERT_B,
3239
0
                                   -1, s->max_cert_list, &ok);
3240
3241
0
    if (!ok)
3242
0
        return ((int)n);
3243
3244
0
    if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
3245
0
        if ((s->verify_mode & SSL_VERIFY_PEER) &&
3246
0
            (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3247
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3248
0
                   SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3249
0
            al = SSL_AD_HANDSHAKE_FAILURE;
3250
0
            goto f_err;
3251
0
        }
3252
        /*
3253
         * If tls asked for a client cert, the client must return a 0 list
3254
         */
3255
0
        if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) {
3256
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3257
0
                   SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
3258
0
            al = SSL_AD_UNEXPECTED_MESSAGE;
3259
0
            goto f_err;
3260
0
        }
3261
0
        s->s3->tmp.reuse_message = 1;
3262
0
        return (1);
3263
0
    }
3264
3265
0
    if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
3266
0
        al = SSL_AD_UNEXPECTED_MESSAGE;
3267
0
        SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_WRONG_MESSAGE_TYPE);
3268
0
        goto f_err;
3269
0
    }
3270
0
    p = d = (unsigned char *)s->init_msg;
3271
3272
0
    if ((sk = sk_X509_new_null()) == NULL) {
3273
0
        SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3274
0
        goto err;
3275
0
    }
3276
3277
0
    n2l3(p, llen);
3278
0
    if (llen + 3 != n) {
3279
0
        al = SSL_AD_DECODE_ERROR;
3280
0
        SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
3281
0
        goto f_err;
3282
0
    }
3283
0
    for (nc = 0; nc < llen;) {
3284
0
        if (nc + 3 > llen) {
3285
0
            al = SSL_AD_DECODE_ERROR;
3286
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3287
0
                   SSL_R_CERT_LENGTH_MISMATCH);
3288
0
            goto f_err;
3289
0
        }
3290
0
        n2l3(p, l);
3291
0
        if ((l + nc + 3) > llen) {
3292
0
            al = SSL_AD_DECODE_ERROR;
3293
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3294
0
                   SSL_R_CERT_LENGTH_MISMATCH);
3295
0
            goto f_err;
3296
0
        }
3297
3298
0
        q = p;
3299
0
        x = d2i_X509(NULL, &p, l);
3300
0
        if (x == NULL) {
3301
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3302
0
            goto err;
3303
0
        }
3304
0
        if (p != (q + l)) {
3305
0
            al = SSL_AD_DECODE_ERROR;
3306
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3307
0
                   SSL_R_CERT_LENGTH_MISMATCH);
3308
0
            goto f_err;
3309
0
        }
3310
0
        if (!sk_X509_push(sk, x)) {
3311
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3312
0
            goto err;
3313
0
        }
3314
0
        x = NULL;
3315
0
        nc += l + 3;
3316
0
    }
3317
3318
0
    if (sk_X509_num(sk) <= 0) {
3319
        /* TLS does not mind 0 certs returned */
3320
0
        if (s->version == SSL3_VERSION) {
3321
0
            al = SSL_AD_HANDSHAKE_FAILURE;
3322
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3323
0
                   SSL_R_NO_CERTIFICATES_RETURNED);
3324
0
            goto f_err;
3325
0
        }
3326
        /* Fail for TLS only if we required a certificate */
3327
0
        else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3328
0
                 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3329
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3330
0
                   SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3331
0
            al = SSL_AD_HANDSHAKE_FAILURE;
3332
0
            goto f_err;
3333
0
        }
3334
        /* No client certificate so digest cached records */
3335
0
        if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) {
3336
0
            al = SSL_AD_INTERNAL_ERROR;
3337
0
            goto f_err;
3338
0
        }
3339
0
    } else {
3340
0
        i = ssl_verify_cert_chain(s, sk);
3341
0
        if (i <= 0) {
3342
0
            al = ssl_verify_alarm_type(s->verify_result);
3343
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3344
0
                   SSL_R_CERTIFICATE_VERIFY_FAILED);
3345
0
            goto f_err;
3346
0
        }
3347
0
    }
3348
3349
0
    if (s->session->peer != NULL) /* This should not be needed */
3350
0
        X509_free(s->session->peer);
3351
0
    s->session->peer = sk_X509_shift(sk);
3352
0
    s->session->verify_result = s->verify_result;
3353
3354
    /*
3355
     * With the current implementation, sess_cert will always be NULL when we
3356
     * arrive here.
3357
     */
3358
0
    if (s->session->sess_cert == NULL) {
3359
0
        s->session->sess_cert = ssl_sess_cert_new();
3360
0
        if (s->session->sess_cert == NULL) {
3361
0
            SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3362
0
            goto err;
3363
0
        }
3364
0
    }
3365
0
    if (s->session->sess_cert->cert_chain != NULL)
3366
0
        sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
3367
0
    s->session->sess_cert->cert_chain = sk;
3368
    /*
3369
     * Inconsistency alert: cert_chain does *not* include the peer's own
3370
     * certificate, while we do include it in s3_clnt.c
3371
     */
3372
3373
0
    sk = NULL;
3374
3375
0
    ret = 1;
3376
0
    if (0) {
3377
0
 f_err:
3378
0
        ssl3_send_alert(s, SSL3_AL_FATAL, al);
3379
0
 err:
3380
0
        s->state = SSL_ST_ERR;
3381
0
    }
3382
3383
0
    if (x != NULL)
3384
0
        X509_free(x);
3385
0
    if (sk != NULL)
3386
0
        sk_X509_pop_free(sk, X509_free);
3387
0
    return (ret);
3388
0
}
3389
3390
int ssl3_send_server_certificate(SSL *s)
3391
0
{
3392
0
    CERT_PKEY *cpk;
3393
3394
0
    if (s->state == SSL3_ST_SW_CERT_A) {
3395
0
        cpk = ssl_get_server_send_pkey(s);
3396
0
        if (cpk == NULL) {
3397
            /* VRS: allow null cert if auth == KRB5 */
3398
0
            if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5) ||
3399
0
                (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5)) {
3400
0
                SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,
3401
0
                       ERR_R_INTERNAL_ERROR);
3402
0
                s->state = SSL_ST_ERR;
3403
0
                return (0);
3404
0
            }
3405
0
        }
3406
3407
0
        if (!ssl3_output_cert_chain(s, cpk)) {
3408
0
            SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3409
0
            s->state = SSL_ST_ERR;
3410
0
            return (0);
3411
0
        }
3412
0
        s->state = SSL3_ST_SW_CERT_B;
3413
0
    }
3414
3415
    /* SSL3_ST_SW_CERT_B */
3416
0
    return ssl_do_write(s);
3417
0
}
3418
3419
#ifndef OPENSSL_NO_TLSEXT
3420
/* send a new session ticket (not necessarily for a new session) */
3421
int ssl3_send_newsession_ticket(SSL *s)
3422
0
{
3423
0
    unsigned char *senc = NULL;
3424
0
    EVP_CIPHER_CTX ctx;
3425
0
    HMAC_CTX hctx;
3426
3427
0
    if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
3428
0
        unsigned char *p, *macstart;
3429
0
        const unsigned char *const_p;
3430
0
        int len, slen_full, slen;
3431
0
        SSL_SESSION *sess;
3432
0
        unsigned int hlen;
3433
0
        SSL_CTX *tctx = s->initial_ctx;
3434
0
        unsigned char iv[EVP_MAX_IV_LENGTH];
3435
0
        unsigned char key_name[16];
3436
3437
        /* get session encoding length */
3438
0
        slen_full = i2d_SSL_SESSION(s->session, NULL);
3439
        /*
3440
         * Some length values are 16 bits, so forget it if session is too
3441
         * long
3442
         */
3443
0
        if (slen_full == 0 || slen_full > 0xFF00) {
3444
0
            s->state = SSL_ST_ERR;
3445
0
            return -1;
3446
0
        }
3447
0
        senc = OPENSSL_malloc(slen_full);
3448
0
        if (!senc) {
3449
0
            s->state = SSL_ST_ERR;
3450
0
            return -1;
3451
0
        }
3452
3453
0
        EVP_CIPHER_CTX_init(&ctx);
3454
0
        HMAC_CTX_init(&hctx);
3455
3456
0
        p = senc;
3457
0
        if (!i2d_SSL_SESSION(s->session, &p))
3458
0
            goto err;
3459
3460
        /*
3461
         * create a fresh copy (not shared with other threads) to clean up
3462
         */
3463
0
        const_p = senc;
3464
0
        sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3465
0
        if (sess == NULL)
3466
0
            goto err;
3467
0
        sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3468
3469
0
        slen = i2d_SSL_SESSION(sess, NULL);
3470
0
        if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
3471
0
            SSL_SESSION_free(sess);
3472
0
            goto err;
3473
0
        }
3474
0
        p = senc;
3475
0
        if (!i2d_SSL_SESSION(sess, &p)) {
3476
0
            SSL_SESSION_free(sess);
3477
0
            goto err;
3478
0
        }
3479
0
        SSL_SESSION_free(sess);
3480
3481
        /*-
3482
         * Grow buffer if need be: the length calculation is as
3483
         * follows handshake_header_length +
3484
         * 4 (ticket lifetime hint) + 2 (ticket length) +
3485
         * 16 (key name) + max_iv_len (iv length) +
3486
         * session_length + max_enc_block_size (max encrypted session
3487
         * length) + max_md_size (HMAC).
3488
         */
3489
0
        if (!BUF_MEM_grow(s->init_buf,
3490
0
                          SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH +
3491
0
                          EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
3492
0
            goto err;
3493
3494
0
        p = ssl_handshake_start(s);
3495
        /*
3496
         * Initialize HMAC and cipher contexts. If callback present it does
3497
         * all the work otherwise use generated values from parent ctx.
3498
         */
3499
0
        if (tctx->tlsext_ticket_key_cb) {
3500
            /* if 0 is returned, write en empty ticket */
3501
0
            int ret = tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
3502
0
                                                 &hctx, 1);
3503
3504
0
            if (ret == 0) {
3505
0
                l2n(0, p); /* timeout */
3506
0
                s2n(0, p); /* length */
3507
0
                ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET,
3508
0
                                         p - ssl_handshake_start(s));
3509
0
                s->state = SSL3_ST_SW_SESSION_TICKET_B;
3510
0
                OPENSSL_free(senc);
3511
0
                EVP_CIPHER_CTX_cleanup(&ctx);
3512
0
                HMAC_CTX_cleanup(&hctx);
3513
0
                return ssl_do_write(s);
3514
0
            }
3515
0
            if (ret < 0)
3516
0
                goto err;
3517
0
        } else {
3518
0
            if (RAND_bytes(iv, 16) <= 0)
3519
0
                goto err;
3520
0
            if (!EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3521
0
                                    tctx->tlsext_tick_aes_key, iv))
3522
0
                goto err;
3523
0
            if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3524
0
                              tlsext_tick_md(), NULL))
3525
0
                goto err;
3526
0
            memcpy(key_name, tctx->tlsext_tick_key_name, 16);
3527
0
        }
3528
3529
        /*
3530
         * Ticket lifetime hint (advisory only): We leave this unspecified
3531
         * for resumed session (for simplicity), and guess that tickets for
3532
         * new sessions will live as long as their sessions.
3533
         */
3534
0
        l2n(s->hit ? 0 : s->session->timeout, p);
3535
3536
        /* Skip ticket length for now */
3537
0
        p += 2;
3538
        /* Output key name */
3539
0
        macstart = p;
3540
0
        memcpy(p, key_name, 16);
3541
0
        p += 16;
3542
        /* output IV */
3543
0
        memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
3544
0
        p += EVP_CIPHER_CTX_iv_length(&ctx);
3545
        /* Encrypt session data */
3546
0
        if (!EVP_EncryptUpdate(&ctx, p, &len, senc, slen))
3547
0
            goto err;
3548
0
        p += len;
3549
0
        if (!EVP_EncryptFinal(&ctx, p, &len))
3550
0
            goto err;
3551
0
        p += len;
3552
3553
0
        if (!HMAC_Update(&hctx, macstart, p - macstart))
3554
0
            goto err;
3555
0
        if (!HMAC_Final(&hctx, p, &hlen))
3556
0
            goto err;
3557
3558
0
        EVP_CIPHER_CTX_cleanup(&ctx);
3559
0
        HMAC_CTX_cleanup(&hctx);
3560
3561
0
        p += hlen;
3562
        /* Now write out lengths: p points to end of data written */
3563
        /* Total length */
3564
0
        len = p - ssl_handshake_start(s);
3565
        /* Skip ticket lifetime hint */
3566
0
        p = ssl_handshake_start(s) + 4;
3567
0
        s2n(len - 6, p);
3568
0
        ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len);
3569
0
        s->state = SSL3_ST_SW_SESSION_TICKET_B;
3570
0
        OPENSSL_free(senc);
3571
0
    }
3572
3573
    /* SSL3_ST_SW_SESSION_TICKET_B */
3574
0
    return ssl_do_write(s);
3575
0
 err:
3576
0
    if (senc)
3577
0
        OPENSSL_free(senc);
3578
0
    EVP_CIPHER_CTX_cleanup(&ctx);
3579
0
    HMAC_CTX_cleanup(&hctx);
3580
0
    s->state = SSL_ST_ERR;
3581
0
    return -1;
3582
0
}
3583
3584
int ssl3_send_cert_status(SSL *s)
3585
0
{
3586
0
    if (s->state == SSL3_ST_SW_CERT_STATUS_A) {
3587
0
        unsigned char *p;
3588
0
        size_t msglen;
3589
3590
        /*-
3591
         * Grow buffer if need be: the length calculation is as
3592
         * follows handshake_header_length +
3593
         * 1 (ocsp response type) + 3 (ocsp response length)
3594
         * + (ocsp response)
3595
         */
3596
0
        msglen = 4 + s->tlsext_ocsp_resplen;
3597
0
        if (!BUF_MEM_grow(s->init_buf, SSL_HM_HEADER_LENGTH(s) + msglen)) {
3598
0
            s->state = SSL_ST_ERR;
3599
0
            return -1;
3600
0
        }
3601
3602
0
        p = ssl_handshake_start(s);
3603
3604
        /* status type */
3605
0
        *(p++) = s->tlsext_status_type;
3606
        /* length of OCSP response */
3607
0
        l2n3(s->tlsext_ocsp_resplen, p);
3608
        /* actual response */
3609
0
        memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
3610
3611
0
        ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_STATUS, msglen);
3612
0
    }
3613
3614
    /* SSL3_ST_SW_CERT_STATUS_B */
3615
0
    return (ssl_do_write(s));
3616
0
}
3617
3618
# ifndef OPENSSL_NO_NEXTPROTONEG
3619
/*
3620
 * ssl3_get_next_proto reads a Next Protocol Negotiation handshake message.
3621
 * It sets the next_proto member in s if found
3622
 */
3623
int ssl3_get_next_proto(SSL *s)
3624
0
{
3625
0
    int ok;
3626
0
    int proto_len, padding_len;
3627
0
    long n;
3628
0
    const unsigned char *p;
3629
3630
    /*
3631
     * Clients cannot send a NextProtocol message if we didn't see the
3632
     * extension in their ClientHello
3633
     */
3634
0
    if (!s->s3->next_proto_neg_seen) {
3635
0
        SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,
3636
0
               SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
3637
0
        s->state = SSL_ST_ERR;
3638
0
        return -1;
3639
0
    }
3640
3641
    /* See the payload format below */
3642
0
    n = s->method->ssl_get_message(s,
3643
0
                                   SSL3_ST_SR_NEXT_PROTO_A,
3644
0
                                   SSL3_ST_SR_NEXT_PROTO_B,
3645
0
                                   SSL3_MT_NEXT_PROTO, 514, &ok);
3646
3647
0
    if (!ok)
3648
0
        return ((int)n);
3649
3650
    /*
3651
     * s->state doesn't reflect whether ChangeCipherSpec has been received in
3652
     * this handshake, but s->s3->change_cipher_spec does (will be reset by
3653
     * ssl3_get_finished).
3654
     */
3655
0
    if (!s->s3->change_cipher_spec) {
3656
0
        SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
3657
0
        s->state = SSL_ST_ERR;
3658
0
        return -1;
3659
0
    }
3660
3661
0
    if (n < 2) {
3662
0
        s->state = SSL_ST_ERR;
3663
0
        return 0;               /* The body must be > 1 bytes long */
3664
0
    }
3665
3666
0
    p = (unsigned char *)s->init_msg;
3667
3668
    /*-
3669
     * The payload looks like:
3670
     *   uint8 proto_len;
3671
     *   uint8 proto[proto_len];
3672
     *   uint8 padding_len;
3673
     *   uint8 padding[padding_len];
3674
     */
3675
0
    proto_len = p[0];
3676
0
    if (proto_len + 2 > s->init_num) {
3677
0
        s->state = SSL_ST_ERR;
3678
0
        return 0;
3679
0
    }
3680
0
    padding_len = p[proto_len + 1];
3681
0
    if (proto_len + padding_len + 2 != s->init_num) {
3682
0
        s->state = SSL_ST_ERR;
3683
0
        return 0;
3684
0
    }
3685
3686
0
    s->next_proto_negotiated = OPENSSL_malloc(proto_len);
3687
0
    if (!s->next_proto_negotiated) {
3688
0
        SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, ERR_R_MALLOC_FAILURE);
3689
0
        s->state = SSL_ST_ERR;
3690
0
        return 0;
3691
0
    }
3692
0
    memcpy(s->next_proto_negotiated, p + 1, proto_len);
3693
0
    s->next_proto_negotiated_len = proto_len;
3694
3695
0
    return 1;
3696
0
}
3697
# endif
3698
3699
#endif