Coverage Report

Created: 2022-11-30 06:20

/src/openssl/ssl/s3_clnt.c
Line
Count
Source (jump to first uncovered line)
1
/* ssl/s3_clnt.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
#include <stdio.h>
152
#include "ssl_locl.h"
153
#include "kssl_lcl.h"
154
#include <openssl/buffer.h>
155
#include <openssl/rand.h>
156
#include <openssl/objects.h>
157
#include <openssl/evp.h>
158
#include <openssl/md5.h>
159
#ifdef OPENSSL_FIPS
160
# include <openssl/fips.h>
161
#endif
162
#ifndef OPENSSL_NO_DH
163
# include <openssl/dh.h>
164
#endif
165
#include <openssl/bn.h>
166
#ifndef OPENSSL_NO_ENGINE
167
# include <openssl/engine.h>
168
#endif
169
170
static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
171
#ifndef OPENSSL_NO_TLSEXT
172
static int ssl3_check_finished(SSL *s);
173
#endif
174
175
#ifndef OPENSSL_NO_SSL3_METHOD
176
static const SSL_METHOD *ssl3_get_client_method(int ver)
177
0
{
178
0
    if (ver == SSL3_VERSION)
179
0
        return (SSLv3_client_method());
180
0
    else
181
0
        return (NULL);
182
0
}
183
184
IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
185
                         ssl_undefined_function,
186
                         ssl3_connect, ssl3_get_client_method)
187
#endif
188
int ssl3_connect(SSL *s)
189
0
{
190
0
    BUF_MEM *buf = NULL;
191
0
    unsigned long Time = (unsigned long)time(NULL);
192
0
    void (*cb) (const SSL *ssl, int type, int val) = NULL;
193
0
    int ret = -1;
194
0
    int new_state, state, skip = 0;
195
196
0
    RAND_add(&Time, sizeof(Time), 0);
197
0
    ERR_clear_error();
198
0
    clear_sys_error();
199
200
0
    if (s->info_callback != NULL)
201
0
        cb = s->info_callback;
202
0
    else if (s->ctx->info_callback != NULL)
203
0
        cb = s->ctx->info_callback;
204
205
0
    s->in_handshake++;
206
0
    if (!SSL_in_init(s) || SSL_in_before(s))
207
0
        SSL_clear(s);
208
209
0
#ifndef OPENSSL_NO_HEARTBEATS
210
    /*
211
     * If we're awaiting a HeartbeatResponse, pretend we already got and
212
     * don't await it anymore, because Heartbeats don't make sense during
213
     * handshakes anyway.
214
     */
215
0
    if (s->tlsext_hb_pending) {
216
0
        s->tlsext_hb_pending = 0;
217
0
        s->tlsext_hb_seq++;
218
0
    }
219
0
#endif
220
221
0
    for (;;) {
222
0
        state = s->state;
223
224
0
        switch (s->state) {
225
0
        case SSL_ST_RENEGOTIATE:
226
0
            s->renegotiate = 1;
227
0
            s->state = SSL_ST_CONNECT;
228
0
            s->ctx->stats.sess_connect_renegotiate++;
229
            /* break */
230
0
        case SSL_ST_BEFORE:
231
0
        case SSL_ST_CONNECT:
232
0
        case SSL_ST_BEFORE | SSL_ST_CONNECT:
233
0
        case SSL_ST_OK | SSL_ST_CONNECT:
234
235
0
            s->server = 0;
236
0
            if (cb != NULL)
237
0
                cb(s, SSL_CB_HANDSHAKE_START, 1);
238
239
0
            if ((s->version & 0xff00) != 0x0300) {
240
0
                SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
241
0
                s->state = SSL_ST_ERR;
242
0
                ret = -1;
243
0
                goto end;
244
0
            }
245
246
            /* s->version=SSL3_VERSION; */
247
0
            s->type = SSL_ST_CONNECT;
248
249
0
            if (s->init_buf == NULL) {
250
0
                if ((buf = BUF_MEM_new()) == NULL) {
251
0
                    ret = -1;
252
0
                    s->state = SSL_ST_ERR;
253
0
                    goto end;
254
0
                }
255
0
                if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
256
0
                    ret = -1;
257
0
                    s->state = SSL_ST_ERR;
258
0
                    goto end;
259
0
                }
260
0
                s->init_buf = buf;
261
0
                buf = NULL;
262
0
            }
263
264
0
            if (!ssl3_setup_buffers(s)) {
265
0
                ret = -1;
266
0
                s->state = SSL_ST_ERR;
267
0
                goto end;
268
0
            }
269
270
            /* setup buffing BIO */
271
0
            if (!ssl_init_wbio_buffer(s, 0)) {
272
0
                ret = -1;
273
0
                s->state = SSL_ST_ERR;
274
0
                goto end;
275
0
            }
276
277
            /* don't push the buffering BIO quite yet */
278
279
0
            if (!ssl3_init_finished_mac(s)) {
280
0
                ret = -1;
281
0
                s->state = SSL_ST_ERR;
282
0
                goto end;
283
0
            }
284
285
0
            s->state = SSL3_ST_CW_CLNT_HELLO_A;
286
0
            s->ctx->stats.sess_connect++;
287
0
            s->init_num = 0;
288
0
            s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
289
            /*
290
             * Should have been reset by ssl3_get_finished, too.
291
             */
292
0
            s->s3->change_cipher_spec = 0;
293
0
            break;
294
295
0
        case SSL3_ST_CW_CLNT_HELLO_A:
296
0
        case SSL3_ST_CW_CLNT_HELLO_B:
297
298
0
            s->shutdown = 0;
299
0
            ret = ssl3_client_hello(s);
300
0
            if (ret <= 0)
301
0
                goto end;
302
0
            s->state = SSL3_ST_CR_SRVR_HELLO_A;
303
0
            s->init_num = 0;
304
305
            /* turn on buffering for the next lot of output */
306
0
            if (s->bbio != s->wbio)
307
0
                s->wbio = BIO_push(s->bbio, s->wbio);
308
309
0
            break;
310
311
0
        case SSL3_ST_CR_SRVR_HELLO_A:
312
0
        case SSL3_ST_CR_SRVR_HELLO_B:
313
0
            ret = ssl3_get_server_hello(s);
314
0
            if (ret <= 0)
315
0
                goto end;
316
317
0
            if (s->hit) {
318
0
                s->state = SSL3_ST_CR_FINISHED_A;
319
0
#ifndef OPENSSL_NO_TLSEXT
320
0
                if (s->tlsext_ticket_expected) {
321
                    /* receive renewed session ticket */
322
0
                    s->state = SSL3_ST_CR_SESSION_TICKET_A;
323
0
                }
324
0
#endif
325
0
            } else {
326
0
                s->state = SSL3_ST_CR_CERT_A;
327
0
            }
328
0
            s->init_num = 0;
329
0
            break;
330
0
        case SSL3_ST_CR_CERT_A:
331
0
        case SSL3_ST_CR_CERT_B:
332
0
#ifndef OPENSSL_NO_TLSEXT
333
            /* Noop (ret = 0) for everything but EAP-FAST. */
334
0
            ret = ssl3_check_finished(s);
335
0
            if (ret < 0)
336
0
                goto end;
337
0
            if (ret == 1) {
338
0
                s->hit = 1;
339
0
                s->state = SSL3_ST_CR_FINISHED_A;
340
0
                s->init_num = 0;
341
0
                break;
342
0
            }
343
0
#endif
344
            /* Check if it is anon DH/ECDH, SRP auth */
345
            /* or PSK */
346
0
            if (!
347
0
                (s->s3->tmp.
348
0
                 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
349
0
                    && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
350
0
                ret = ssl3_get_server_certificate(s);
351
0
                if (ret <= 0)
352
0
                    goto end;
353
0
#ifndef OPENSSL_NO_TLSEXT
354
0
                if (s->tlsext_status_expected)
355
0
                    s->state = SSL3_ST_CR_CERT_STATUS_A;
356
0
                else
357
0
                    s->state = SSL3_ST_CR_KEY_EXCH_A;
358
0
            } else {
359
0
                skip = 1;
360
0
                s->state = SSL3_ST_CR_KEY_EXCH_A;
361
0
            }
362
#else
363
            } else
364
                skip = 1;
365
366
            s->state = SSL3_ST_CR_KEY_EXCH_A;
367
#endif
368
0
            s->init_num = 0;
369
0
            break;
370
371
0
        case SSL3_ST_CR_KEY_EXCH_A:
372
0
        case SSL3_ST_CR_KEY_EXCH_B:
373
0
            ret = ssl3_get_key_exchange(s);
374
0
            if (ret <= 0)
375
0
                goto end;
376
0
            s->state = SSL3_ST_CR_CERT_REQ_A;
377
0
            s->init_num = 0;
378
379
            /*
380
             * at this point we check that we have the required stuff from
381
             * the server
382
             */
383
0
            if (!ssl3_check_cert_and_algorithm(s)) {
384
0
                ret = -1;
385
0
                s->state = SSL_ST_ERR;
386
0
                goto end;
387
0
            }
388
0
            break;
389
390
0
        case SSL3_ST_CR_CERT_REQ_A:
391
0
        case SSL3_ST_CR_CERT_REQ_B:
392
0
            ret = ssl3_get_certificate_request(s);
393
0
            if (ret <= 0)
394
0
                goto end;
395
0
            s->state = SSL3_ST_CR_SRVR_DONE_A;
396
0
            s->init_num = 0;
397
0
            break;
398
399
0
        case SSL3_ST_CR_SRVR_DONE_A:
400
0
        case SSL3_ST_CR_SRVR_DONE_B:
401
0
            ret = ssl3_get_server_done(s);
402
0
            if (ret <= 0)
403
0
                goto end;
404
0
#ifndef OPENSSL_NO_SRP
405
0
            if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
406
0
                if ((ret = SRP_Calc_A_param(s)) <= 0) {
407
0
                    SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
408
0
                    ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
409
0
                    s->state = SSL_ST_ERR;
410
0
                    goto end;
411
0
                }
412
0
            }
413
0
#endif
414
0
            if (s->s3->tmp.cert_req)
415
0
                s->state = SSL3_ST_CW_CERT_A;
416
0
            else
417
0
                s->state = SSL3_ST_CW_KEY_EXCH_A;
418
0
            s->init_num = 0;
419
420
0
            break;
421
422
0
        case SSL3_ST_CW_CERT_A:
423
0
        case SSL3_ST_CW_CERT_B:
424
0
        case SSL3_ST_CW_CERT_C:
425
0
        case SSL3_ST_CW_CERT_D:
426
0
            ret = ssl3_send_client_certificate(s);
427
0
            if (ret <= 0)
428
0
                goto end;
429
0
            s->state = SSL3_ST_CW_KEY_EXCH_A;
430
0
            s->init_num = 0;
431
0
            break;
432
433
0
        case SSL3_ST_CW_KEY_EXCH_A:
434
0
        case SSL3_ST_CW_KEY_EXCH_B:
435
0
            ret = ssl3_send_client_key_exchange(s);
436
0
            if (ret <= 0)
437
0
                goto end;
438
            /*
439
             * EAY EAY EAY need to check for DH fix cert sent back
440
             */
441
            /*
442
             * For TLS, cert_req is set to 2, so a cert chain of nothing is
443
             * sent, but no verify packet is sent
444
             */
445
            /*
446
             * XXX: For now, we do not support client authentication in ECDH
447
             * cipher suites with ECDH (rather than ECDSA) certificates. We
448
             * need to skip the certificate verify message when client's
449
             * ECDH public key is sent inside the client certificate.
450
             */
451
0
            if (s->s3->tmp.cert_req == 1) {
452
0
                s->state = SSL3_ST_CW_CERT_VRFY_A;
453
0
            } else {
454
0
                s->state = SSL3_ST_CW_CHANGE_A;
455
0
            }
456
0
            if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
457
0
                s->state = SSL3_ST_CW_CHANGE_A;
458
0
            }
459
460
0
            s->init_num = 0;
461
0
            break;
462
463
0
        case SSL3_ST_CW_CERT_VRFY_A:
464
0
        case SSL3_ST_CW_CERT_VRFY_B:
465
0
            ret = ssl3_send_client_verify(s);
466
0
            if (ret <= 0)
467
0
                goto end;
468
0
            s->state = SSL3_ST_CW_CHANGE_A;
469
0
            s->init_num = 0;
470
0
            break;
471
472
0
        case SSL3_ST_CW_CHANGE_A:
473
0
        case SSL3_ST_CW_CHANGE_B:
474
0
            ret = ssl3_send_change_cipher_spec(s,
475
0
                                               SSL3_ST_CW_CHANGE_A,
476
0
                                               SSL3_ST_CW_CHANGE_B);
477
0
            if (ret <= 0)
478
0
                goto end;
479
480
#if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
481
            s->state = SSL3_ST_CW_FINISHED_A;
482
#else
483
0
            if (s->s3->next_proto_neg_seen)
484
0
                s->state = SSL3_ST_CW_NEXT_PROTO_A;
485
0
            else
486
0
                s->state = SSL3_ST_CW_FINISHED_A;
487
0
#endif
488
0
            s->init_num = 0;
489
490
0
            s->session->cipher = s->s3->tmp.new_cipher;
491
#ifdef OPENSSL_NO_COMP
492
            s->session->compress_meth = 0;
493
#else
494
0
            if (s->s3->tmp.new_compression == NULL)
495
0
                s->session->compress_meth = 0;
496
0
            else
497
0
                s->session->compress_meth = s->s3->tmp.new_compression->id;
498
0
#endif
499
0
            if (!s->method->ssl3_enc->setup_key_block(s)) {
500
0
                ret = -1;
501
0
                s->state = SSL_ST_ERR;
502
0
                goto end;
503
0
            }
504
505
0
            if (!s->method->ssl3_enc->change_cipher_state(s,
506
0
                                                          SSL3_CHANGE_CIPHER_CLIENT_WRITE))
507
0
            {
508
0
                ret = -1;
509
0
                s->state = SSL_ST_ERR;
510
0
                goto end;
511
0
            }
512
513
0
            break;
514
515
0
#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
516
0
        case SSL3_ST_CW_NEXT_PROTO_A:
517
0
        case SSL3_ST_CW_NEXT_PROTO_B:
518
0
            ret = ssl3_send_next_proto(s);
519
0
            if (ret <= 0)
520
0
                goto end;
521
0
            s->state = SSL3_ST_CW_FINISHED_A;
522
0
            break;
523
0
#endif
524
525
0
        case SSL3_ST_CW_FINISHED_A:
526
0
        case SSL3_ST_CW_FINISHED_B:
527
0
            ret = ssl3_send_finished(s,
528
0
                                     SSL3_ST_CW_FINISHED_A,
529
0
                                     SSL3_ST_CW_FINISHED_B,
530
0
                                     s->method->
531
0
                                     ssl3_enc->client_finished_label,
532
0
                                     s->method->
533
0
                                     ssl3_enc->client_finished_label_len);
534
0
            if (ret <= 0)
535
0
                goto end;
536
0
            s->state = SSL3_ST_CW_FLUSH;
537
538
            /* clear flags */
539
0
            s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
540
0
            if (s->hit) {
541
0
                s->s3->tmp.next_state = SSL_ST_OK;
542
0
                if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
543
0
                    s->state = SSL_ST_OK;
544
0
                    s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
545
0
                    s->s3->delay_buf_pop_ret = 0;
546
0
                }
547
0
            } else {
548
0
#ifndef OPENSSL_NO_TLSEXT
549
                /*
550
                 * Allow NewSessionTicket if ticket expected
551
                 */
552
0
                if (s->tlsext_ticket_expected)
553
0
                    s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
554
0
                else
555
0
#endif
556
557
0
                    s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
558
0
            }
559
0
            s->init_num = 0;
560
0
            break;
561
562
0
#ifndef OPENSSL_NO_TLSEXT
563
0
        case SSL3_ST_CR_SESSION_TICKET_A:
564
0
        case SSL3_ST_CR_SESSION_TICKET_B:
565
0
            ret = ssl3_get_new_session_ticket(s);
566
0
            if (ret <= 0)
567
0
                goto end;
568
0
            s->state = SSL3_ST_CR_FINISHED_A;
569
0
            s->init_num = 0;
570
0
            break;
571
572
0
        case SSL3_ST_CR_CERT_STATUS_A:
573
0
        case SSL3_ST_CR_CERT_STATUS_B:
574
0
            ret = ssl3_get_cert_status(s);
575
0
            if (ret <= 0)
576
0
                goto end;
577
0
            s->state = SSL3_ST_CR_KEY_EXCH_A;
578
0
            s->init_num = 0;
579
0
            break;
580
0
#endif
581
582
0
        case SSL3_ST_CR_FINISHED_A:
583
0
        case SSL3_ST_CR_FINISHED_B:
584
0
            if (!s->s3->change_cipher_spec)
585
0
                s->s3->flags |= SSL3_FLAGS_CCS_OK;
586
0
            ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
587
0
                                    SSL3_ST_CR_FINISHED_B);
588
0
            if (ret <= 0)
589
0
                goto end;
590
591
0
            if (s->hit)
592
0
                s->state = SSL3_ST_CW_CHANGE_A;
593
0
            else
594
0
                s->state = SSL_ST_OK;
595
0
            s->init_num = 0;
596
0
            break;
597
598
0
        case SSL3_ST_CW_FLUSH:
599
0
            s->rwstate = SSL_WRITING;
600
0
            if (BIO_flush(s->wbio) <= 0) {
601
0
                ret = -1;
602
0
                goto end;
603
0
            }
604
0
            s->rwstate = SSL_NOTHING;
605
0
            s->state = s->s3->tmp.next_state;
606
0
            break;
607
608
0
        case SSL_ST_OK:
609
            /* clean a few things up */
610
0
            ssl3_cleanup_key_block(s);
611
612
0
            if (s->init_buf != NULL) {
613
0
                BUF_MEM_free(s->init_buf);
614
0
                s->init_buf = NULL;
615
0
            }
616
617
            /*
618
             * If we are not 'joining' the last two packets, remove the
619
             * buffering now
620
             */
621
0
            if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
622
0
                ssl_free_wbio_buffer(s);
623
            /* else do it later in ssl3_write */
624
625
0
            s->init_num = 0;
626
0
            s->renegotiate = 0;
627
0
            s->new_session = 0;
628
629
0
            ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
630
0
            if (s->hit)
631
0
                s->ctx->stats.sess_hit++;
632
633
0
            ret = 1;
634
            /* s->server=0; */
635
0
            s->handshake_func = ssl3_connect;
636
0
            s->ctx->stats.sess_connect_good++;
637
638
0
            if (cb != NULL)
639
0
                cb(s, SSL_CB_HANDSHAKE_DONE, 1);
640
641
0
            goto end;
642
            /* break; */
643
644
0
        case SSL_ST_ERR:
645
0
        default:
646
0
            SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
647
0
            ret = -1;
648
0
            goto end;
649
            /* break; */
650
0
        }
651
652
        /* did we do anything */
653
0
        if (!s->s3->tmp.reuse_message && !skip) {
654
0
            if (s->debug) {
655
0
                if ((ret = BIO_flush(s->wbio)) <= 0)
656
0
                    goto end;
657
0
            }
658
659
0
            if ((cb != NULL) && (s->state != state)) {
660
0
                new_state = s->state;
661
0
                s->state = state;
662
0
                cb(s, SSL_CB_CONNECT_LOOP, 1);
663
0
                s->state = new_state;
664
0
            }
665
0
        }
666
0
        skip = 0;
667
0
    }
668
0
 end:
669
0
    s->in_handshake--;
670
0
    if (buf != NULL)
671
0
        BUF_MEM_free(buf);
672
0
    if (cb != NULL)
673
0
        cb(s, SSL_CB_CONNECT_EXIT, ret);
674
0
    return (ret);
675
0
}
676
677
int ssl3_client_hello(SSL *s)
678
0
{
679
0
    unsigned char *buf;
680
0
    unsigned char *p, *d;
681
0
    int i;
682
0
    unsigned long l;
683
0
    int al = 0;
684
0
#ifndef OPENSSL_NO_COMP
685
0
    int j;
686
0
    SSL_COMP *comp;
687
0
#endif
688
689
0
    buf = (unsigned char *)s->init_buf->data;
690
0
    if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
691
0
        SSL_SESSION *sess = s->session;
692
0
        if ((sess == NULL) || (sess->ssl_version != s->version) ||
693
#ifdef OPENSSL_NO_TLSEXT
694
            !sess->session_id_length ||
695
#else
696
            /*
697
             * In the case of EAP-FAST, we can have a pre-shared
698
             * "ticket" without a session ID.
699
             */
700
0
            (!sess->session_id_length && !sess->tlsext_tick) ||
701
0
#endif
702
0
            (sess->not_resumable)) {
703
0
            if (!ssl_get_new_session(s, 0))
704
0
                goto err;
705
0
        }
706
0
        if (s->method->version == DTLS_ANY_VERSION) {
707
            /* Determine which DTLS version to use */
708
0
            int options = s->options;
709
            /* If DTLS 1.2 disabled correct the version number */
710
0
            if (options & SSL_OP_NO_DTLSv1_2) {
711
0
                if (tls1_suiteb(s)) {
712
0
                    SSLerr(SSL_F_SSL3_CLIENT_HELLO,
713
0
                           SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
714
0
                    goto err;
715
0
                }
716
                /*
717
                 * Disabling all versions is silly: return an error.
718
                 */
719
0
                if (options & SSL_OP_NO_DTLSv1) {
720
0
                    SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_WRONG_SSL_VERSION);
721
0
                    goto err;
722
0
                }
723
                /*
724
                 * Update method so we don't use any DTLS 1.2 features.
725
                 */
726
0
                s->method = DTLSv1_client_method();
727
0
                s->version = DTLS1_VERSION;
728
0
            } else {
729
                /*
730
                 * We only support one version: update method
731
                 */
732
0
                if (options & SSL_OP_NO_DTLSv1)
733
0
                    s->method = DTLSv1_2_client_method();
734
0
                s->version = DTLS1_2_VERSION;
735
0
            }
736
0
            s->client_version = s->version;
737
0
        }
738
        /* else use the pre-loaded session */
739
740
0
        p = s->s3->client_random;
741
742
        /*
743
         * for DTLS if client_random is initialized, reuse it, we are
744
         * required to use same upon reply to HelloVerify
745
         */
746
0
        if (SSL_IS_DTLS(s)) {
747
0
            size_t idx;
748
0
            i = 1;
749
0
            for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
750
0
                if (p[idx]) {
751
0
                    i = 0;
752
0
                    break;
753
0
                }
754
0
            }
755
0
        } else
756
0
            i = 1;
757
758
0
        if (i && ssl_fill_hello_random(s, 0, p,
759
0
                                       sizeof(s->s3->client_random)) <= 0)
760
0
            goto err;
761
762
        /* Do the message type and length last */
763
0
        d = p = ssl_handshake_start(s);
764
765
        /*-
766
         * version indicates the negotiated version: for example from
767
         * an SSLv2/v3 compatible client hello). The client_version
768
         * field is the maximum version we permit and it is also
769
         * used in RSA encrypted premaster secrets. Some servers can
770
         * choke if we initially report a higher version then
771
         * renegotiate to a lower one in the premaster secret. This
772
         * didn't happen with TLS 1.0 as most servers supported it
773
         * but it can with TLS 1.1 or later if the server only supports
774
         * 1.0.
775
         *
776
         * Possible scenario with previous logic:
777
         *      1. Client hello indicates TLS 1.2
778
         *      2. Server hello says TLS 1.0
779
         *      3. RSA encrypted premaster secret uses 1.2.
780
         *      4. Handhaked proceeds using TLS 1.0.
781
         *      5. Server sends hello request to renegotiate.
782
         *      6. Client hello indicates TLS v1.0 as we now
783
         *         know that is maximum server supports.
784
         *      7. Server chokes on RSA encrypted premaster secret
785
         *         containing version 1.0.
786
         *
787
         * For interoperability it should be OK to always use the
788
         * maximum version we support in client hello and then rely
789
         * on the checking of version to ensure the servers isn't
790
         * being inconsistent: for example initially negotiating with
791
         * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
792
         * client_version in client hello and not resetting it to
793
         * the negotiated version.
794
         */
795
#if 0
796
        *(p++) = s->version >> 8;
797
        *(p++) = s->version & 0xff;
798
        s->client_version = s->version;
799
#else
800
0
        *(p++) = s->client_version >> 8;
801
0
        *(p++) = s->client_version & 0xff;
802
0
#endif
803
804
        /* Random stuff */
805
0
        memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
806
0
        p += SSL3_RANDOM_SIZE;
807
808
        /* Session ID */
809
0
        if (s->new_session)
810
0
            i = 0;
811
0
        else
812
0
            i = s->session->session_id_length;
813
0
        *(p++) = i;
814
0
        if (i != 0) {
815
0
            if (i > (int)sizeof(s->session->session_id)) {
816
0
                SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
817
0
                goto err;
818
0
            }
819
0
            memcpy(p, s->session->session_id, i);
820
0
            p += i;
821
0
        }
822
823
        /* cookie stuff for DTLS */
824
0
        if (SSL_IS_DTLS(s)) {
825
0
            if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
826
0
                SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
827
0
                goto err;
828
0
            }
829
0
            *(p++) = s->d1->cookie_len;
830
0
            memcpy(p, s->d1->cookie, s->d1->cookie_len);
831
0
            p += s->d1->cookie_len;
832
0
        }
833
834
        /* Ciphers supported */
835
0
        i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
836
0
        if (i == 0) {
837
0
            SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
838
0
            goto err;
839
0
        }
840
#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
841
        /*
842
         * Some servers hang if client hello > 256 bytes as hack workaround
843
         * chop number of supported ciphers to keep it well below this if we
844
         * use TLS v1.2
845
         */
846
        if (TLS1_get_version(s) >= TLS1_2_VERSION
847
            && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
848
            i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
849
#endif
850
0
        s2n(i, p);
851
0
        p += i;
852
853
        /* COMPRESSION */
854
#ifdef OPENSSL_NO_COMP
855
        *(p++) = 1;
856
#else
857
858
0
        if ((s->options & SSL_OP_NO_COMPRESSION)
859
0
            || !s->ctx->comp_methods)
860
0
            j = 0;
861
0
        else
862
0
            j = sk_SSL_COMP_num(s->ctx->comp_methods);
863
0
        *(p++) = 1 + j;
864
0
        for (i = 0; i < j; i++) {
865
0
            comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
866
0
            *(p++) = comp->id;
867
0
        }
868
0
#endif
869
0
        *(p++) = 0;             /* Add the NULL method */
870
871
0
#ifndef OPENSSL_NO_TLSEXT
872
        /* TLS extensions */
873
0
        if (ssl_prepare_clienthello_tlsext(s) <= 0) {
874
0
            SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
875
0
            goto err;
876
0
        }
877
0
        if ((p =
878
0
             ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
879
0
                                        &al)) == NULL) {
880
0
            ssl3_send_alert(s, SSL3_AL_FATAL, al);
881
0
            SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
882
0
            goto err;
883
0
        }
884
0
#endif
885
886
0
        l = p - d;
887
0
        ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
888
0
        s->state = SSL3_ST_CW_CLNT_HELLO_B;
889
0
    }
890
891
    /* SSL3_ST_CW_CLNT_HELLO_B */
892
0
    return ssl_do_write(s);
893
0
 err:
894
0
    s->state = SSL_ST_ERR;
895
0
    return (-1);
896
0
}
897
898
int ssl3_get_server_hello(SSL *s)
899
0
{
900
0
    STACK_OF(SSL_CIPHER) *sk;
901
0
    const SSL_CIPHER *c;
902
0
    CERT *ct = s->cert;
903
0
    unsigned char *p, *d;
904
0
    int i, al = SSL_AD_INTERNAL_ERROR, ok;
905
0
    unsigned int j;
906
0
    long n;
907
0
#ifndef OPENSSL_NO_COMP
908
0
    SSL_COMP *comp;
909
0
#endif
910
    /*
911
     * Hello verify request and/or server hello version may not match so set
912
     * first packet if we're negotiating version.
913
     */
914
0
    if (SSL_IS_DTLS(s))
915
0
        s->first_packet = 1;
916
917
0
    n = s->method->ssl_get_message(s,
918
0
                                   SSL3_ST_CR_SRVR_HELLO_A,
919
0
                                   SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
920
921
0
    if (!ok)
922
0
        return ((int)n);
923
924
0
    if (SSL_IS_DTLS(s)) {
925
0
        s->first_packet = 0;
926
0
        if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
927
0
            if (s->d1->send_cookie == 0) {
928
0
                s->s3->tmp.reuse_message = 1;
929
0
                return 1;
930
0
            } else {            /* already sent a cookie */
931
932
0
                al = SSL_AD_UNEXPECTED_MESSAGE;
933
0
                SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
934
0
                goto f_err;
935
0
            }
936
0
        }
937
0
    }
938
939
0
    if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
940
0
        al = SSL_AD_UNEXPECTED_MESSAGE;
941
0
        SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
942
0
        goto f_err;
943
0
    }
944
945
0
    d = p = (unsigned char *)s->init_msg;
946
0
    if (s->method->version == DTLS_ANY_VERSION) {
947
        /* Work out correct protocol version to use */
948
0
        int hversion = (p[0] << 8) | p[1];
949
0
        int options = s->options;
950
0
        if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
951
0
            s->method = DTLSv1_2_client_method();
952
0
        else if (tls1_suiteb(s)) {
953
0
            SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
954
0
                   SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
955
0
            s->version = hversion;
956
0
            al = SSL_AD_PROTOCOL_VERSION;
957
0
            goto f_err;
958
0
        } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
959
0
            s->method = DTLSv1_client_method();
960
0
        else {
961
0
            SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
962
0
            s->version = hversion;
963
0
            al = SSL_AD_PROTOCOL_VERSION;
964
0
            goto f_err;
965
0
        }
966
0
        s->session->ssl_version = s->version = s->method->version;
967
0
    }
968
969
0
    if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
970
0
        SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
971
0
        s->version = (s->version & 0xff00) | p[1];
972
0
        al = SSL_AD_PROTOCOL_VERSION;
973
0
        goto f_err;
974
0
    }
975
0
    p += 2;
976
977
    /* load the server hello data */
978
    /* load the server random */
979
0
    memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
980
0
    p += SSL3_RANDOM_SIZE;
981
982
0
    s->hit = 0;
983
984
    /* get the session-id */
985
0
    j = *(p++);
986
987
0
    if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
988
0
        al = SSL_AD_ILLEGAL_PARAMETER;
989
0
        SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
990
0
        goto f_err;
991
0
    }
992
0
#ifndef OPENSSL_NO_TLSEXT
993
    /*
994
     * Check if we can resume the session based on external pre-shared secret.
995
     * EAP-FAST (RFC 4851) supports two types of session resumption.
996
     * Resumption based on server-side state works with session IDs.
997
     * Resumption based on pre-shared Protected Access Credentials (PACs)
998
     * works by overriding the SessionTicket extension at the application
999
     * layer, and does not send a session ID. (We do not know whether EAP-FAST
1000
     * servers would honour the session ID.) Therefore, the session ID alone
1001
     * is not a reliable indicator of session resumption, so we first check if
1002
     * we can resume, and later peek at the next handshake message to see if the
1003
     * server wants to resume.
1004
     */
1005
0
    if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1006
0
        s->session->tlsext_tick) {
1007
0
        SSL_CIPHER *pref_cipher = NULL;
1008
0
        s->session->master_key_length = sizeof(s->session->master_key);
1009
0
        if (s->tls_session_secret_cb(s, s->session->master_key,
1010
0
                                     &s->session->master_key_length,
1011
0
                                     NULL, &pref_cipher,
1012
0
                                     s->tls_session_secret_cb_arg)) {
1013
0
            s->session->cipher = pref_cipher ?
1014
0
                pref_cipher : ssl_get_cipher_by_char(s, p + j);
1015
0
        } else {
1016
0
            SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1017
0
            al = SSL_AD_INTERNAL_ERROR;
1018
0
            goto f_err;
1019
0
        }
1020
0
    }
1021
0
#endif                          /* OPENSSL_NO_TLSEXT */
1022
1023
0
    if (j != 0 && j == s->session->session_id_length
1024
0
        && memcmp(p, s->session->session_id, j) == 0) {
1025
0
        if (s->sid_ctx_length != s->session->sid_ctx_length
1026
0
            || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1027
            /* actually a client application bug */
1028
0
            al = SSL_AD_ILLEGAL_PARAMETER;
1029
0
            SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1030
0
                   SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1031
0
            goto f_err;
1032
0
        }
1033
0
        s->hit = 1;
1034
0
    } else {
1035
        /*
1036
         * If we were trying for session-id reuse but the server
1037
         * didn't echo the ID, make a new SSL_SESSION.
1038
         * In the case of EAP-FAST and PAC, we do not send a session ID,
1039
         * so the PAC-based session secret is always preserved. It'll be
1040
         * overwritten if the server refuses resumption.
1041
         */
1042
0
        if (s->session->session_id_length > 0) {
1043
0
            if (!ssl_get_new_session(s, 0)) {
1044
0
                goto f_err;
1045
0
            }
1046
0
        }
1047
0
        s->session->session_id_length = j;
1048
0
        memcpy(s->session->session_id, p, j); /* j could be 0 */
1049
0
    }
1050
0
    p += j;
1051
0
    c = ssl_get_cipher_by_char(s, p);
1052
0
    if (c == NULL) {
1053
        /* unknown cipher */
1054
0
        al = SSL_AD_ILLEGAL_PARAMETER;
1055
0
        SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1056
0
        goto f_err;
1057
0
    }
1058
    /* Set version disabled mask now we know version */
1059
0
    if (!SSL_USE_TLS1_2_CIPHERS(s))
1060
0
        ct->mask_ssl = SSL_TLSV1_2;
1061
0
    else
1062
0
        ct->mask_ssl = 0;
1063
    /*
1064
     * If it is a disabled cipher we didn't send it in client hello, so
1065
     * return an error.
1066
     */
1067
0
    if (c->algorithm_ssl & ct->mask_ssl ||
1068
0
        c->algorithm_mkey & ct->mask_k || c->algorithm_auth & ct->mask_a) {
1069
0
        al = SSL_AD_ILLEGAL_PARAMETER;
1070
0
        SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1071
0
        goto f_err;
1072
0
    }
1073
0
    p += ssl_put_cipher_by_char(s, NULL, NULL);
1074
1075
0
    sk = ssl_get_ciphers_by_id(s);
1076
0
    i = sk_SSL_CIPHER_find(sk, c);
1077
0
    if (i < 0) {
1078
        /* we did not say we would use this cipher */
1079
0
        al = SSL_AD_ILLEGAL_PARAMETER;
1080
0
        SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1081
0
        goto f_err;
1082
0
    }
1083
1084
    /*
1085
     * Depending on the session caching (internal/external), the cipher
1086
     * and/or cipher_id values may not be set. Make sure that cipher_id is
1087
     * set and use it for comparison.
1088
     */
1089
0
    if (s->session->cipher)
1090
0
        s->session->cipher_id = s->session->cipher->id;
1091
0
    if (s->hit && (s->session->cipher_id != c->id)) {
1092
/* Workaround is now obsolete */
1093
#if 0
1094
        if (!(s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
1095
#endif
1096
0
        {
1097
0
            al = SSL_AD_ILLEGAL_PARAMETER;
1098
0
            SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1099
0
                   SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1100
0
            goto f_err;
1101
0
        }
1102
0
    }
1103
0
    s->s3->tmp.new_cipher = c;
1104
    /*
1105
     * Don't digest cached records if no sigalgs: we may need them for client
1106
     * authentication.
1107
     */
1108
0
    if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
1109
0
        goto f_err;
1110
    /* lets get the compression algorithm */
1111
    /* COMPRESSION */
1112
#ifdef OPENSSL_NO_COMP
1113
    if (*(p++) != 0) {
1114
        al = SSL_AD_ILLEGAL_PARAMETER;
1115
        SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1116
               SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1117
        goto f_err;
1118
    }
1119
    /*
1120
     * If compression is disabled we'd better not try to resume a session
1121
     * using compression.
1122
     */
1123
    if (s->session->compress_meth != 0) {
1124
        SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1125
        goto f_err;
1126
    }
1127
#else
1128
0
    j = *(p++);
1129
0
    if (s->hit && j != s->session->compress_meth) {
1130
0
        al = SSL_AD_ILLEGAL_PARAMETER;
1131
0
        SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1132
0
               SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1133
0
        goto f_err;
1134
0
    }
1135
0
    if (j == 0)
1136
0
        comp = NULL;
1137
0
    else if (s->options & SSL_OP_NO_COMPRESSION) {
1138
0
        al = SSL_AD_ILLEGAL_PARAMETER;
1139
0
        SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1140
0
        goto f_err;
1141
0
    } else
1142
0
        comp = ssl3_comp_find(s->ctx->comp_methods, j);
1143
1144
0
    if ((j != 0) && (comp == NULL)) {
1145
0
        al = SSL_AD_ILLEGAL_PARAMETER;
1146
0
        SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1147
0
               SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1148
0
        goto f_err;
1149
0
    } else {
1150
0
        s->s3->tmp.new_compression = comp;
1151
0
    }
1152
0
#endif
1153
1154
0
#ifndef OPENSSL_NO_TLSEXT
1155
    /* TLS extensions */
1156
0
    if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) {
1157
0
        SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1158
0
        goto err;
1159
0
    }
1160
0
#endif
1161
1162
0
    if (p != (d + n)) {
1163
        /* wrong packet length */
1164
0
        al = SSL_AD_DECODE_ERROR;
1165
0
        SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1166
0
        goto f_err;
1167
0
    }
1168
1169
0
    return (1);
1170
0
 f_err:
1171
0
    ssl3_send_alert(s, SSL3_AL_FATAL, al);
1172
0
 err:
1173
0
    s->state = SSL_ST_ERR;
1174
0
    return (-1);
1175
0
}
1176
1177
int ssl3_get_server_certificate(SSL *s)
1178
0
{
1179
0
    int al, i, ok, ret = -1;
1180
0
    unsigned long n, nc, llen, l;
1181
0
    X509 *x = NULL;
1182
0
    const unsigned char *q, *p;
1183
0
    unsigned char *d;
1184
0
    STACK_OF(X509) *sk = NULL;
1185
0
    SESS_CERT *sc;
1186
0
    EVP_PKEY *pkey = NULL;
1187
0
    int need_cert = 1;          /* VRS: 0=> will allow null cert if auth ==
1188
                                 * KRB5 */
1189
1190
0
    n = s->method->ssl_get_message(s,
1191
0
                                   SSL3_ST_CR_CERT_A,
1192
0
                                   SSL3_ST_CR_CERT_B,
1193
0
                                   -1, s->max_cert_list, &ok);
1194
1195
0
    if (!ok)
1196
0
        return ((int)n);
1197
1198
0
    if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
1199
0
        ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
1200
0
         (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) {
1201
0
        s->s3->tmp.reuse_message = 1;
1202
0
        return (1);
1203
0
    }
1204
1205
0
    if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1206
0
        al = SSL_AD_UNEXPECTED_MESSAGE;
1207
0
        SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1208
0
        goto f_err;
1209
0
    }
1210
0
    p = d = (unsigned char *)s->init_msg;
1211
1212
0
    if ((sk = sk_X509_new_null()) == NULL) {
1213
0
        SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1214
0
        goto err;
1215
0
    }
1216
1217
0
    n2l3(p, llen);
1218
0
    if (llen + 3 != n) {
1219
0
        al = SSL_AD_DECODE_ERROR;
1220
0
        SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1221
0
        goto f_err;
1222
0
    }
1223
0
    for (nc = 0; nc < llen;) {
1224
0
        if (nc + 3 > llen) {
1225
0
            al = SSL_AD_DECODE_ERROR;
1226
0
            SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1227
0
                   SSL_R_CERT_LENGTH_MISMATCH);
1228
0
            goto f_err;
1229
0
        }
1230
0
        n2l3(p, l);
1231
0
        if ((l + nc + 3) > llen) {
1232
0
            al = SSL_AD_DECODE_ERROR;
1233
0
            SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1234
0
                   SSL_R_CERT_LENGTH_MISMATCH);
1235
0
            goto f_err;
1236
0
        }
1237
1238
0
        q = p;
1239
0
        x = d2i_X509(NULL, &q, l);
1240
0
        if (x == NULL) {
1241
0
            al = SSL_AD_BAD_CERTIFICATE;
1242
0
            SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1243
0
            goto f_err;
1244
0
        }
1245
0
        if (q != (p + l)) {
1246
0
            al = SSL_AD_DECODE_ERROR;
1247
0
            SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1248
0
                   SSL_R_CERT_LENGTH_MISMATCH);
1249
0
            goto f_err;
1250
0
        }
1251
0
        if (!sk_X509_push(sk, x)) {
1252
0
            SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1253
0
            goto err;
1254
0
        }
1255
0
        x = NULL;
1256
0
        nc += l + 3;
1257
0
        p = q;
1258
0
    }
1259
1260
0
    i = ssl_verify_cert_chain(s, sk);
1261
0
    if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1262
#ifndef OPENSSL_NO_KRB5
1263
        && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1264
             (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1265
#endif                          /* OPENSSL_NO_KRB5 */
1266
0
        ) {
1267
0
        al = ssl_verify_alarm_type(s->verify_result);
1268
0
        SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1269
0
               SSL_R_CERTIFICATE_VERIFY_FAILED);
1270
0
        goto f_err;
1271
0
    }
1272
0
    ERR_clear_error();          /* but we keep s->verify_result */
1273
1274
0
    sc = ssl_sess_cert_new();
1275
0
    if (sc == NULL)
1276
0
        goto err;
1277
1278
0
    if (s->session->sess_cert)
1279
0
        ssl_sess_cert_free(s->session->sess_cert);
1280
0
    s->session->sess_cert = sc;
1281
1282
0
    sc->cert_chain = sk;
1283
    /*
1284
     * Inconsistency alert: cert_chain does include the peer's certificate,
1285
     * which we don't include in s3_srvr.c
1286
     */
1287
0
    x = sk_X509_value(sk, 0);
1288
0
    sk = NULL;
1289
    /*
1290
     * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1291
     */
1292
1293
0
    pkey = X509_get_pubkey(x);
1294
1295
    /* VRS: allow null cert if auth == KRB5 */
1296
0
    need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1297
0
                 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1298
0
        ? 0 : 1;
1299
1300
#ifdef KSSL_DEBUG
1301
    fprintf(stderr, "pkey,x = %p, %p\n", pkey, x);
1302
    fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey));
1303
    fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n",
1304
            s->s3->tmp.new_cipher->name,
1305
            s->s3->tmp.new_cipher->algorithm_mkey,
1306
            s->s3->tmp.new_cipher->algorithm_auth, need_cert);
1307
#endif                          /* KSSL_DEBUG */
1308
1309
0
    if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) {
1310
0
        x = NULL;
1311
0
        al = SSL3_AL_FATAL;
1312
0
        SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1313
0
               SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1314
0
        goto f_err;
1315
0
    }
1316
1317
0
    i = ssl_cert_type(x, pkey);
1318
0
    if (need_cert && i < 0) {
1319
0
        x = NULL;
1320
0
        al = SSL3_AL_FATAL;
1321
0
        SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1322
0
               SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1323
0
        goto f_err;
1324
0
    }
1325
1326
0
    if (need_cert) {
1327
0
        int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1328
0
        if (exp_idx >= 0 && i != exp_idx) {
1329
0
            x = NULL;
1330
0
            al = SSL_AD_ILLEGAL_PARAMETER;
1331
0
            SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1332
0
                   SSL_R_WRONG_CERTIFICATE_TYPE);
1333
0
            goto f_err;
1334
0
        }
1335
0
        sc->peer_cert_type = i;
1336
0
        CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1337
        /*
1338
         * Why would the following ever happen? We just created sc a couple
1339
         * of lines ago.
1340
         */
1341
0
        if (sc->peer_pkeys[i].x509 != NULL)
1342
0
            X509_free(sc->peer_pkeys[i].x509);
1343
0
        sc->peer_pkeys[i].x509 = x;
1344
0
        sc->peer_key = &(sc->peer_pkeys[i]);
1345
1346
0
        if (s->session->peer != NULL)
1347
0
            X509_free(s->session->peer);
1348
0
        CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1349
0
        s->session->peer = x;
1350
0
    } else {
1351
0
        sc->peer_cert_type = i;
1352
0
        sc->peer_key = NULL;
1353
1354
0
        if (s->session->peer != NULL)
1355
0
            X509_free(s->session->peer);
1356
0
        s->session->peer = NULL;
1357
0
    }
1358
0
    s->session->verify_result = s->verify_result;
1359
1360
0
    x = NULL;
1361
0
    ret = 1;
1362
0
    if (0) {
1363
0
 f_err:
1364
0
        ssl3_send_alert(s, SSL3_AL_FATAL, al);
1365
0
 err:
1366
0
        s->state = SSL_ST_ERR;
1367
0
    }
1368
1369
0
    EVP_PKEY_free(pkey);
1370
0
    X509_free(x);
1371
0
    sk_X509_pop_free(sk, X509_free);
1372
0
    return (ret);
1373
0
}
1374
1375
int ssl3_get_key_exchange(SSL *s)
1376
0
{
1377
0
#ifndef OPENSSL_NO_RSA
1378
0
    unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1379
0
#endif
1380
0
    EVP_MD_CTX md_ctx;
1381
0
    unsigned char *param, *p;
1382
0
    int al, j, ok;
1383
0
    long i, param_len, n, alg_k, alg_a;
1384
0
    EVP_PKEY *pkey = NULL;
1385
0
    const EVP_MD *md = NULL;
1386
0
#ifndef OPENSSL_NO_RSA
1387
0
    RSA *rsa = NULL;
1388
0
#endif
1389
0
#ifndef OPENSSL_NO_DH
1390
0
    DH *dh = NULL;
1391
0
#endif
1392
0
#ifndef OPENSSL_NO_ECDH
1393
0
    EC_KEY *ecdh = NULL;
1394
0
    BN_CTX *bn_ctx = NULL;
1395
0
    EC_POINT *srvr_ecpoint = NULL;
1396
0
    int curve_nid = 0;
1397
0
    int encoded_pt_len = 0;
1398
0
#endif
1399
1400
0
    EVP_MD_CTX_init(&md_ctx);
1401
1402
    /*
1403
     * use same message size as in ssl3_get_certificate_request() as
1404
     * ServerKeyExchange message may be skipped
1405
     */
1406
0
    n = s->method->ssl_get_message(s,
1407
0
                                   SSL3_ST_CR_KEY_EXCH_A,
1408
0
                                   SSL3_ST_CR_KEY_EXCH_B,
1409
0
                                   -1, s->max_cert_list, &ok);
1410
0
    if (!ok)
1411
0
        return ((int)n);
1412
1413
0
    alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1414
1415
0
    if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1416
        /*
1417
         * Can't skip server key exchange if this is an ephemeral
1418
         * ciphersuite.
1419
         */
1420
0
        if (alg_k & (SSL_kDHE | SSL_kECDHE)) {
1421
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1422
0
            al = SSL_AD_UNEXPECTED_MESSAGE;
1423
0
            goto f_err;
1424
0
        }
1425
0
#ifndef OPENSSL_NO_PSK
1426
        /*
1427
         * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1428
         * identity hint is sent. Set session->sess_cert anyway to avoid
1429
         * problems later.
1430
         */
1431
0
        if (alg_k & SSL_kPSK) {
1432
0
            s->session->sess_cert = ssl_sess_cert_new();
1433
0
            if (s->ctx->psk_identity_hint)
1434
0
                OPENSSL_free(s->ctx->psk_identity_hint);
1435
0
            s->ctx->psk_identity_hint = NULL;
1436
0
        }
1437
0
#endif
1438
0
        s->s3->tmp.reuse_message = 1;
1439
0
        return (1);
1440
0
    }
1441
1442
0
    param = p = (unsigned char *)s->init_msg;
1443
0
    if (s->session->sess_cert != NULL) {
1444
0
#ifndef OPENSSL_NO_RSA
1445
0
        if (s->session->sess_cert->peer_rsa_tmp != NULL) {
1446
0
            RSA_free(s->session->sess_cert->peer_rsa_tmp);
1447
0
            s->session->sess_cert->peer_rsa_tmp = NULL;
1448
0
        }
1449
0
#endif
1450
0
#ifndef OPENSSL_NO_DH
1451
0
        if (s->session->sess_cert->peer_dh_tmp) {
1452
0
            DH_free(s->session->sess_cert->peer_dh_tmp);
1453
0
            s->session->sess_cert->peer_dh_tmp = NULL;
1454
0
        }
1455
0
#endif
1456
0
#ifndef OPENSSL_NO_ECDH
1457
0
        if (s->session->sess_cert->peer_ecdh_tmp) {
1458
0
            EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1459
0
            s->session->sess_cert->peer_ecdh_tmp = NULL;
1460
0
        }
1461
0
#endif
1462
0
    } else {
1463
0
        s->session->sess_cert = ssl_sess_cert_new();
1464
0
    }
1465
1466
    /* Total length of the parameters including the length prefix */
1467
0
    param_len = 0;
1468
1469
0
    alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1470
1471
0
    al = SSL_AD_DECODE_ERROR;
1472
1473
0
#ifndef OPENSSL_NO_PSK
1474
0
    if (alg_k & SSL_kPSK) {
1475
0
        param_len = 2;
1476
0
        if (param_len > n) {
1477
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1478
0
            goto f_err;
1479
0
        }
1480
0
        n2s(p, i);
1481
1482
        /*
1483
         * Store PSK identity hint for later use, hint is used in
1484
         * ssl3_send_client_key_exchange.  Assume that the maximum length of
1485
         * a PSK identity hint can be as long as the maximum length of a PSK
1486
         * identity.
1487
         */
1488
0
        if (i > PSK_MAX_IDENTITY_LEN) {
1489
0
            al = SSL_AD_HANDSHAKE_FAILURE;
1490
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1491
0
            goto f_err;
1492
0
        }
1493
0
        if (i > n - param_len) {
1494
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1495
0
                   SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1496
0
            goto f_err;
1497
0
        }
1498
0
        param_len += i;
1499
1500
0
        s->session->psk_identity_hint = BUF_strndup((char *)p, i);
1501
0
        if (s->session->psk_identity_hint == NULL) {
1502
0
            al = SSL_AD_HANDSHAKE_FAILURE;
1503
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1504
0
            goto f_err;
1505
0
        }
1506
1507
0
        p += i;
1508
0
        n -= param_len;
1509
0
    } else
1510
0
#endif                          /* !OPENSSL_NO_PSK */
1511
0
#ifndef OPENSSL_NO_SRP
1512
0
    if (alg_k & SSL_kSRP) {
1513
0
        param_len = 2;
1514
0
        if (param_len > n) {
1515
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1516
0
            goto f_err;
1517
0
        }
1518
0
        n2s(p, i);
1519
1520
0
        if (i > n - param_len) {
1521
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1522
0
            goto f_err;
1523
0
        }
1524
0
        param_len += i;
1525
1526
0
        if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) {
1527
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1528
0
            goto err;
1529
0
        }
1530
0
        p += i;
1531
1532
0
        if (2 > n - param_len) {
1533
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1534
0
            goto f_err;
1535
0
        }
1536
0
        param_len += 2;
1537
1538
0
        n2s(p, i);
1539
1540
0
        if (i > n - param_len) {
1541
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1542
0
            goto f_err;
1543
0
        }
1544
0
        param_len += i;
1545
1546
0
        if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) {
1547
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1548
0
            goto err;
1549
0
        }
1550
0
        p += i;
1551
1552
0
        if (1 > n - param_len) {
1553
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1554
0
            goto f_err;
1555
0
        }
1556
0
        param_len += 1;
1557
1558
0
        i = (unsigned int)(p[0]);
1559
0
        p++;
1560
1561
0
        if (i > n - param_len) {
1562
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1563
0
            goto f_err;
1564
0
        }
1565
0
        param_len += i;
1566
1567
0
        if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) {
1568
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1569
0
            goto err;
1570
0
        }
1571
0
        p += i;
1572
1573
0
        if (2 > n - param_len) {
1574
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1575
0
            goto f_err;
1576
0
        }
1577
0
        param_len += 2;
1578
1579
0
        n2s(p, i);
1580
1581
0
        if (i > n - param_len) {
1582
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1583
0
            goto f_err;
1584
0
        }
1585
0
        param_len += i;
1586
1587
0
        if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) {
1588
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1589
0
            goto err;
1590
0
        }
1591
0
        p += i;
1592
0
        n -= param_len;
1593
1594
0
        if (!srp_verify_server_param(s, &al)) {
1595
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1596
0
            goto f_err;
1597
0
        }
1598
1599
/* We must check if there is a certificate */
1600
0
# ifndef OPENSSL_NO_RSA
1601
0
        if (alg_a & SSL_aRSA)
1602
0
            pkey =
1603
0
                X509_get_pubkey(s->session->
1604
0
                                sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1605
# else
1606
        if (0) ;
1607
# endif
1608
0
# ifndef OPENSSL_NO_DSA
1609
0
        else if (alg_a & SSL_aDSS)
1610
0
            pkey =
1611
0
                X509_get_pubkey(s->session->
1612
0
                                sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1613
0
                                x509);
1614
0
# endif
1615
0
    } else
1616
0
#endif                          /* !OPENSSL_NO_SRP */
1617
0
#ifndef OPENSSL_NO_RSA
1618
0
    if (alg_k & SSL_kRSA) {
1619
        /* Temporary RSA keys only allowed in export ciphersuites */
1620
0
        if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1621
0
            al = SSL_AD_UNEXPECTED_MESSAGE;
1622
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1623
0
            goto f_err;
1624
0
        }
1625
0
        if ((rsa = RSA_new()) == NULL) {
1626
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1627
0
            goto err;
1628
0
        }
1629
1630
0
        param_len = 2;
1631
0
        if (param_len > n) {
1632
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1633
0
            goto f_err;
1634
0
        }
1635
0
        n2s(p, i);
1636
1637
0
        if (i > n - param_len) {
1638
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1639
0
            goto f_err;
1640
0
        }
1641
0
        param_len += i;
1642
1643
0
        if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) {
1644
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1645
0
            goto err;
1646
0
        }
1647
0
        p += i;
1648
1649
0
        if (2 > n - param_len) {
1650
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1651
0
            goto f_err;
1652
0
        }
1653
0
        param_len += 2;
1654
1655
0
        n2s(p, i);
1656
1657
0
        if (i > n - param_len) {
1658
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1659
0
            goto f_err;
1660
0
        }
1661
0
        param_len += i;
1662
1663
0
        if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) {
1664
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1665
0
            goto err;
1666
0
        }
1667
0
        p += i;
1668
0
        n -= param_len;
1669
1670
        /* this should be because we are using an export cipher */
1671
0
        if (alg_a & SSL_aRSA)
1672
0
            pkey =
1673
0
                X509_get_pubkey(s->session->
1674
0
                                sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1675
0
        else {
1676
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1677
0
            goto err;
1678
0
        }
1679
1680
0
        if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1681
0
            al = SSL_AD_UNEXPECTED_MESSAGE;
1682
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1683
0
            goto f_err;
1684
0
        }
1685
1686
0
        s->session->sess_cert->peer_rsa_tmp = rsa;
1687
0
        rsa = NULL;
1688
0
    }
1689
#else                           /* OPENSSL_NO_RSA */
1690
    if (0) ;
1691
#endif
1692
0
#ifndef OPENSSL_NO_DH
1693
0
    else if (alg_k & SSL_kEDH) {
1694
0
        if ((dh = DH_new()) == NULL) {
1695
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1696
0
            goto err;
1697
0
        }
1698
1699
0
        param_len = 2;
1700
0
        if (param_len > n) {
1701
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1702
0
            goto f_err;
1703
0
        }
1704
0
        n2s(p, i);
1705
1706
0
        if (i > n - param_len) {
1707
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1708
0
            goto f_err;
1709
0
        }
1710
0
        param_len += i;
1711
1712
0
        if (!(dh->p = BN_bin2bn(p, i, NULL))) {
1713
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1714
0
            goto err;
1715
0
        }
1716
0
        p += i;
1717
1718
0
        if (2 > n - param_len) {
1719
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1720
0
            goto f_err;
1721
0
        }
1722
0
        param_len += 2;
1723
1724
0
        n2s(p, i);
1725
1726
0
        if (i > n - param_len) {
1727
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1728
0
            goto f_err;
1729
0
        }
1730
0
        param_len += i;
1731
1732
0
        if (!(dh->g = BN_bin2bn(p, i, NULL))) {
1733
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1734
0
            goto err;
1735
0
        }
1736
0
        p += i;
1737
1738
0
        if (2 > n - param_len) {
1739
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1740
0
            goto f_err;
1741
0
        }
1742
0
        param_len += 2;
1743
1744
0
        n2s(p, i);
1745
1746
0
        if (i > n - param_len) {
1747
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1748
0
            goto f_err;
1749
0
        }
1750
0
        param_len += i;
1751
1752
0
        if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) {
1753
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1754
0
            goto err;
1755
0
        }
1756
0
        p += i;
1757
0
        n -= param_len;
1758
1759
0
        if (BN_is_zero(dh->pub_key)) {
1760
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_VALUE);
1761
0
            goto f_err;
1762
0
        }
1763
1764
        /*-
1765
         * Check that p and g are suitable enough
1766
         *
1767
         * p is odd
1768
         * 1 < g < p - 1
1769
         */
1770
0
        {
1771
0
            BIGNUM *tmp = NULL;
1772
1773
0
            if (!BN_is_odd(dh->p)) {
1774
0
                SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_VALUE);
1775
0
                goto f_err;
1776
0
            }
1777
0
            if (BN_is_negative(dh->g) || BN_is_zero(dh->g)
1778
0
                || BN_is_one(dh->g)) {
1779
0
                SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_VALUE);
1780
0
                goto f_err;
1781
0
            }
1782
0
            if ((tmp = BN_new()) == NULL
1783
0
                || BN_copy(tmp, dh->p) == NULL
1784
0
                || !BN_sub_word(tmp, 1)) {
1785
0
                BN_free(tmp);
1786
0
                SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1787
0
                goto err;
1788
0
            }
1789
0
            if (BN_cmp(dh->g, tmp) >= 0) {
1790
0
                BN_free(tmp);
1791
0
                SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_VALUE);
1792
0
                goto f_err;
1793
0
            }
1794
0
            BN_free(tmp);
1795
0
        }
1796
1797
0
# ifndef OPENSSL_NO_RSA
1798
0
        if (alg_a & SSL_aRSA)
1799
0
            pkey =
1800
0
                X509_get_pubkey(s->session->
1801
0
                                sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1802
# else
1803
        if (0) ;
1804
# endif
1805
0
# ifndef OPENSSL_NO_DSA
1806
0
        else if (alg_a & SSL_aDSS)
1807
0
            pkey =
1808
0
                X509_get_pubkey(s->session->
1809
0
                                sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1810
0
                                x509);
1811
0
# endif
1812
        /* else anonymous DH, so no certificate or pkey. */
1813
1814
0
        s->session->sess_cert->peer_dh_tmp = dh;
1815
0
        dh = NULL;
1816
0
    } else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) {
1817
0
        al = SSL_AD_ILLEGAL_PARAMETER;
1818
0
        SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1819
0
               SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1820
0
        goto f_err;
1821
0
    }
1822
0
#endif                          /* !OPENSSL_NO_DH */
1823
1824
0
#ifndef OPENSSL_NO_ECDH
1825
0
    else if (alg_k & SSL_kEECDH) {
1826
0
        EC_GROUP *ngroup;
1827
0
        const EC_GROUP *group;
1828
1829
0
        if ((ecdh = EC_KEY_new()) == NULL) {
1830
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1831
0
            goto err;
1832
0
        }
1833
1834
        /*
1835
         * Extract elliptic curve parameters and the server's ephemeral ECDH
1836
         * public key. Keep accumulating lengths of various components in
1837
         * param_len and make sure it never exceeds n.
1838
         */
1839
1840
        /*
1841
         * XXX: For now we only support named (not generic) curves and the
1842
         * ECParameters in this case is just three bytes. We also need one
1843
         * byte for the length of the encoded point
1844
         */
1845
0
        param_len = 4;
1846
0
        if (param_len > n) {
1847
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1848
0
            goto f_err;
1849
0
        }
1850
        /*
1851
         * Check curve is one of our preferences, if not server has sent an
1852
         * invalid curve. ECParameters is 3 bytes.
1853
         */
1854
0
        if (!tls1_check_curve(s, p, 3)) {
1855
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1856
0
            goto f_err;
1857
0
        }
1858
1859
0
        if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
1860
0
            al = SSL_AD_INTERNAL_ERROR;
1861
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1862
0
                   SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1863
0
            goto f_err;
1864
0
        }
1865
1866
0
        ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1867
0
        if (ngroup == NULL) {
1868
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1869
0
            goto err;
1870
0
        }
1871
0
        if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1872
0
            EC_GROUP_free(ngroup);
1873
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1874
0
            goto err;
1875
0
        }
1876
0
        EC_GROUP_free(ngroup);
1877
1878
0
        group = EC_KEY_get0_group(ecdh);
1879
1880
0
        if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1881
0
            (EC_GROUP_get_degree(group) > 163)) {
1882
0
            al = SSL_AD_EXPORT_RESTRICTION;
1883
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1884
0
                   SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1885
0
            goto f_err;
1886
0
        }
1887
1888
0
        p += 3;
1889
1890
        /* Next, get the encoded ECPoint */
1891
0
        if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1892
0
            ((bn_ctx = BN_CTX_new()) == NULL)) {
1893
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1894
0
            goto err;
1895
0
        }
1896
1897
0
        encoded_pt_len = *p;    /* length of encoded point */
1898
0
        p += 1;
1899
1900
0
        if ((encoded_pt_len > n - param_len) ||
1901
0
            (EC_POINT_oct2point(group, srvr_ecpoint,
1902
0
                                p, encoded_pt_len, bn_ctx) == 0)) {
1903
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1904
0
            goto f_err;
1905
0
        }
1906
0
        param_len += encoded_pt_len;
1907
1908
0
        n -= param_len;
1909
0
        p += encoded_pt_len;
1910
1911
        /*
1912
         * The ECC/TLS specification does not mention the use of DSA to sign
1913
         * ECParameters in the server key exchange message. We do support RSA
1914
         * and ECDSA.
1915
         */
1916
0
        if (0) ;
1917
0
# ifndef OPENSSL_NO_RSA
1918
0
        else if (alg_a & SSL_aRSA)
1919
0
            pkey =
1920
0
                X509_get_pubkey(s->session->
1921
0
                                sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1922
0
# endif
1923
0
# ifndef OPENSSL_NO_ECDSA
1924
0
        else if (alg_a & SSL_aECDSA)
1925
0
            pkey =
1926
0
                X509_get_pubkey(s->session->
1927
0
                                sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1928
0
# endif
1929
        /* else anonymous ECDH, so no certificate or pkey. */
1930
0
        EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1931
0
        s->session->sess_cert->peer_ecdh_tmp = ecdh;
1932
0
        ecdh = NULL;
1933
0
        BN_CTX_free(bn_ctx);
1934
0
        bn_ctx = NULL;
1935
0
        EC_POINT_free(srvr_ecpoint);
1936
0
        srvr_ecpoint = NULL;
1937
0
    } else if (alg_k) {
1938
0
        al = SSL_AD_UNEXPECTED_MESSAGE;
1939
0
        SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1940
0
        goto f_err;
1941
0
    }
1942
0
#endif                          /* !OPENSSL_NO_ECDH */
1943
1944
    /* p points to the next byte, there are 'n' bytes left */
1945
1946
    /* if it was signed, check the signature */
1947
0
    if (pkey != NULL) {
1948
0
        if (SSL_USE_SIGALGS(s)) {
1949
0
            int rv;
1950
0
            if (2 > n) {
1951
0
                SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1952
0
                goto f_err;
1953
0
            }
1954
0
            rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1955
0
            if (rv == -1)
1956
0
                goto err;
1957
0
            else if (rv == 0) {
1958
0
                goto f_err;
1959
0
            }
1960
#ifdef SSL_DEBUG
1961
            fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1962
#endif
1963
0
            p += 2;
1964
0
            n -= 2;
1965
0
        } else
1966
0
            md = EVP_sha1();
1967
1968
0
        if (2 > n) {
1969
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1970
0
            goto f_err;
1971
0
        }
1972
0
        n2s(p, i);
1973
0
        n -= 2;
1974
0
        j = EVP_PKEY_size(pkey);
1975
1976
        /*
1977
         * Check signature length. If n is 0 then signature is empty
1978
         */
1979
0
        if ((i != n) || (n > j) || (n <= 0)) {
1980
            /* wrong packet length */
1981
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1982
0
            goto f_err;
1983
0
        }
1984
0
#ifndef OPENSSL_NO_RSA
1985
0
        if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1986
0
            int num;
1987
0
            unsigned int size;
1988
1989
0
            j = 0;
1990
0
            q = md_buf;
1991
0
            for (num = 2; num > 0; num--) {
1992
0
                EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1993
0
                if (EVP_DigestInit_ex(&md_ctx,
1994
0
                                      (num == 2) ? s->ctx->md5 : s->ctx->sha1,
1995
0
                                      NULL) <= 0
1996
0
                        || EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1997
0
                                            SSL3_RANDOM_SIZE) <= 0
1998
0
                        || EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1999
0
                                            SSL3_RANDOM_SIZE) <= 0
2000
0
                        || EVP_DigestUpdate(&md_ctx, param, param_len) <= 0
2001
0
                        || EVP_DigestFinal_ex(&md_ctx, q, &size) <= 0) {
2002
0
                    SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
2003
0
                           ERR_R_INTERNAL_ERROR);
2004
0
                    al = SSL_AD_INTERNAL_ERROR;
2005
0
                    goto f_err;
2006
0
                }
2007
0
                q += size;
2008
0
                j += size;
2009
0
            }
2010
0
            i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
2011
0
            if (i < 0) {
2012
0
                al = SSL_AD_DECRYPT_ERROR;
2013
0
                SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
2014
0
                goto f_err;
2015
0
            }
2016
0
            if (i == 0) {
2017
                /* bad signature */
2018
0
                al = SSL_AD_DECRYPT_ERROR;
2019
0
                SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
2020
0
                goto f_err;
2021
0
            }
2022
0
        } else
2023
0
#endif
2024
0
        {
2025
0
            if (EVP_VerifyInit_ex(&md_ctx, md, NULL) <= 0
2026
0
                    || EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
2027
0
                                        SSL3_RANDOM_SIZE) <= 0
2028
0
                    || EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
2029
0
                                        SSL3_RANDOM_SIZE) <= 0
2030
0
                    || EVP_VerifyUpdate(&md_ctx, param, param_len) <= 0) {
2031
0
                al = SSL_AD_INTERNAL_ERROR;
2032
0
                SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EVP_LIB);
2033
0
                goto f_err;
2034
0
            }
2035
0
            if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
2036
                /* bad signature */
2037
0
                al = SSL_AD_DECRYPT_ERROR;
2038
0
                SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
2039
0
                goto f_err;
2040
0
            }
2041
0
        }
2042
0
    } else {
2043
        /* aNULL, aSRP or kPSK do not need public keys */
2044
0
        if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
2045
            /* Might be wrong key type, check it */
2046
0
            if (ssl3_check_cert_and_algorithm(s))
2047
                /* Otherwise this shouldn't happen */
2048
0
                SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2049
0
            goto err;
2050
0
        }
2051
        /* still data left over */
2052
0
        if (n != 0) {
2053
0
            SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
2054
0
            goto f_err;
2055
0
        }
2056
0
    }
2057
0
    EVP_PKEY_free(pkey);
2058
0
    EVP_MD_CTX_cleanup(&md_ctx);
2059
0
    return (1);
2060
0
 f_err:
2061
0
    ssl3_send_alert(s, SSL3_AL_FATAL, al);
2062
0
 err:
2063
0
    EVP_PKEY_free(pkey);
2064
0
#ifndef OPENSSL_NO_RSA
2065
0
    if (rsa != NULL)
2066
0
        RSA_free(rsa);
2067
0
#endif
2068
0
#ifndef OPENSSL_NO_DH
2069
0
    if (dh != NULL)
2070
0
        DH_free(dh);
2071
0
#endif
2072
0
#ifndef OPENSSL_NO_ECDH
2073
0
    BN_CTX_free(bn_ctx);
2074
0
    EC_POINT_free(srvr_ecpoint);
2075
0
    if (ecdh != NULL)
2076
0
        EC_KEY_free(ecdh);
2077
0
#endif
2078
0
    EVP_MD_CTX_cleanup(&md_ctx);
2079
0
    s->state = SSL_ST_ERR;
2080
0
    return (-1);
2081
0
}
2082
2083
int ssl3_get_certificate_request(SSL *s)
2084
0
{
2085
0
    int ok, ret = 0;
2086
0
    unsigned long n, nc, l;
2087
0
    unsigned int llen, ctype_num, i;
2088
0
    X509_NAME *xn = NULL;
2089
0
    const unsigned char *p, *q;
2090
0
    unsigned char *d;
2091
0
    STACK_OF(X509_NAME) *ca_sk = NULL;
2092
2093
0
    n = s->method->ssl_get_message(s,
2094
0
                                   SSL3_ST_CR_CERT_REQ_A,
2095
0
                                   SSL3_ST_CR_CERT_REQ_B,
2096
0
                                   -1, s->max_cert_list, &ok);
2097
2098
0
    if (!ok)
2099
0
        return ((int)n);
2100
2101
0
    s->s3->tmp.cert_req = 0;
2102
2103
0
    if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
2104
0
        s->s3->tmp.reuse_message = 1;
2105
        /*
2106
         * If we get here we don't need any cached handshake records as we
2107
         * wont be doing client auth.
2108
         */
2109
0
        if (s->s3->handshake_buffer) {
2110
0
            if (!ssl3_digest_cached_records(s))
2111
0
                goto err;
2112
0
        }
2113
0
        return (1);
2114
0
    }
2115
2116
0
    if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2117
0
        ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2118
0
        SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2119
0
        goto err;
2120
0
    }
2121
2122
    /* TLS does not like anon-DH with client cert */
2123
0
    if (s->version > SSL3_VERSION) {
2124
0
        if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2125
0
            ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2126
0
            SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2127
0
                   SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2128
0
            goto err;
2129
0
        }
2130
0
    }
2131
2132
0
    p = d = (unsigned char *)s->init_msg;
2133
2134
0
    if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2135
0
        SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2136
0
        goto err;
2137
0
    }
2138
2139
    /* get the certificate types */
2140
0
    ctype_num = *(p++);
2141
0
    if (s->cert->ctypes) {
2142
0
        OPENSSL_free(s->cert->ctypes);
2143
0
        s->cert->ctypes = NULL;
2144
0
    }
2145
0
    if (ctype_num > SSL3_CT_NUMBER) {
2146
        /* If we exceed static buffer copy all to cert structure */
2147
0
        s->cert->ctypes = OPENSSL_malloc(ctype_num);
2148
0
        if (s->cert->ctypes == NULL) {
2149
0
            SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2150
0
            goto err;
2151
0
        }
2152
0
        memcpy(s->cert->ctypes, p, ctype_num);
2153
0
        s->cert->ctype_num = (size_t)ctype_num;
2154
0
        ctype_num = SSL3_CT_NUMBER;
2155
0
    }
2156
0
    for (i = 0; i < ctype_num; i++)
2157
0
        s->s3->tmp.ctype[i] = p[i];
2158
0
    p += p[-1];
2159
0
    if (SSL_USE_SIGALGS(s)) {
2160
0
        n2s(p, llen);
2161
        /*
2162
         * Check we have enough room for signature algorithms and following
2163
         * length value.
2164
         */
2165
0
        if ((unsigned long)(p - d + llen + 2) > n) {
2166
0
            ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2167
0
            SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2168
0
                   SSL_R_DATA_LENGTH_TOO_LONG);
2169
0
            goto err;
2170
0
        }
2171
        /* Clear certificate digests and validity flags */
2172
0
        for (i = 0; i < SSL_PKEY_NUM; i++) {
2173
0
            s->cert->pkeys[i].digest = NULL;
2174
0
            s->cert->pkeys[i].valid_flags = 0;
2175
0
        }
2176
0
        if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) {
2177
0
            ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2178
0
            SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2179
0
                   SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2180
0
            goto err;
2181
0
        }
2182
0
        if (!tls1_process_sigalgs(s)) {
2183
0
            ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2184
0
            SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2185
0
            goto err;
2186
0
        }
2187
0
        p += llen;
2188
0
    }
2189
2190
    /* get the CA RDNs */
2191
0
    n2s(p, llen);
2192
#if 0
2193
    {
2194
        FILE *out;
2195
        out = fopen("/tmp/vsign.der", "w");
2196
        fwrite(p, 1, llen, out);
2197
        fclose(out);
2198
    }
2199
#endif
2200
2201
0
    if ((unsigned long)(p - d + llen) != n) {
2202
0
        ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2203
0
        SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2204
0
        goto err;
2205
0
    }
2206
2207
0
    for (nc = 0; nc < llen;) {
2208
0
        if (nc + 2 > llen) {
2209
0
            ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2210
0
            SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2211
0
            goto err;
2212
0
        }
2213
0
        n2s(p, l);
2214
0
        if ((l + nc + 2) > llen) {
2215
0
            if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
2216
0
                goto cont;      /* netscape bugs */
2217
0
            ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2218
0
            SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2219
0
            goto err;
2220
0
        }
2221
2222
0
        q = p;
2223
2224
0
        if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
2225
            /* If netscape tolerance is on, ignore errors */
2226
0
            if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
2227
0
                goto cont;
2228
0
            else {
2229
0
                ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2230
0
                SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2231
0
                goto err;
2232
0
            }
2233
0
        }
2234
2235
0
        if (q != (p + l)) {
2236
0
            ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2237
0
            SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2238
0
                   SSL_R_CA_DN_LENGTH_MISMATCH);
2239
0
            goto err;
2240
0
        }
2241
0
        if (!sk_X509_NAME_push(ca_sk, xn)) {
2242
0
            SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2243
0
            goto err;
2244
0
        }
2245
0
        xn = NULL;
2246
2247
0
        p += l;
2248
0
        nc += l + 2;
2249
0
    }
2250
2251
0
    if (0) {
2252
0
 cont:
2253
0
        ERR_clear_error();
2254
0
    }
2255
2256
    /* we should setup a certificate to return.... */
2257
0
    s->s3->tmp.cert_req = 1;
2258
0
    s->s3->tmp.ctype_num = ctype_num;
2259
0
    if (s->s3->tmp.ca_names != NULL)
2260
0
        sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2261
0
    s->s3->tmp.ca_names = ca_sk;
2262
0
    ca_sk = NULL;
2263
2264
0
    ret = 1;
2265
0
    goto done;
2266
0
 err:
2267
0
    s->state = SSL_ST_ERR;
2268
0
 done:
2269
0
    X509_NAME_free(xn);
2270
0
    if (ca_sk != NULL)
2271
0
        sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2272
0
    return (ret);
2273
0
}
2274
2275
static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2276
0
{
2277
0
    return (X509_NAME_cmp(*a, *b));
2278
0
}
2279
2280
#ifndef OPENSSL_NO_TLSEXT
2281
int ssl3_get_new_session_ticket(SSL *s)
2282
0
{
2283
0
    int ok, al, ret = 0, ticklen;
2284
0
    long n;
2285
0
    const unsigned char *p;
2286
0
    unsigned char *d;
2287
0
    unsigned long ticket_lifetime_hint;
2288
2289
0
    n = s->method->ssl_get_message(s,
2290
0
                                   SSL3_ST_CR_SESSION_TICKET_A,
2291
0
                                   SSL3_ST_CR_SESSION_TICKET_B,
2292
0
                                   SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2293
2294
0
    if (!ok)
2295
0
        return ((int)n);
2296
2297
0
    if (n < 6) {
2298
        /* need at least ticket_lifetime_hint + ticket length */
2299
0
        al = SSL_AD_DECODE_ERROR;
2300
0
        SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2301
0
        goto f_err;
2302
0
    }
2303
2304
0
    p = d = (unsigned char *)s->init_msg;
2305
2306
0
    n2l(p, ticket_lifetime_hint);
2307
0
    n2s(p, ticklen);
2308
    /* ticket_lifetime_hint + ticket_length + ticket */
2309
0
    if (ticklen + 6 != n) {
2310
0
        al = SSL_AD_DECODE_ERROR;
2311
0
        SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2312
0
        goto f_err;
2313
0
    }
2314
2315
    /* Server is allowed to change its mind and send an empty ticket. */
2316
0
    if (ticklen == 0)
2317
0
        return 1;
2318
2319
0
    if (s->session->session_id_length > 0) {
2320
0
        int i = s->session_ctx->session_cache_mode;
2321
0
        SSL_SESSION *new_sess;
2322
        /*
2323
         * We reused an existing session, so we need to replace it with a new
2324
         * one
2325
         */
2326
0
        if (i & SSL_SESS_CACHE_CLIENT) {
2327
            /*
2328
             * Remove the old session from the cache
2329
             */
2330
0
            if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2331
0
                if (s->session_ctx->remove_session_cb != NULL)
2332
0
                    s->session_ctx->remove_session_cb(s->session_ctx,
2333
0
                                                      s->session);
2334
0
            } else {
2335
                /* We carry on if this fails */
2336
0
                SSL_CTX_remove_session(s->session_ctx, s->session);
2337
0
            }
2338
0
        }
2339
2340
0
        if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2341
0
            al = SSL_AD_INTERNAL_ERROR;
2342
0
            SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2343
0
            goto f_err;
2344
0
        }
2345
2346
0
        SSL_SESSION_free(s->session);
2347
0
        s->session = new_sess;
2348
0
    }
2349
2350
0
    if (s->session->tlsext_tick) {
2351
0
        OPENSSL_free(s->session->tlsext_tick);
2352
0
        s->session->tlsext_ticklen = 0;
2353
0
    }
2354
0
    s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2355
0
    if (!s->session->tlsext_tick) {
2356
0
        SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2357
0
        goto err;
2358
0
    }
2359
0
    memcpy(s->session->tlsext_tick, p, ticklen);
2360
0
    s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
2361
0
    s->session->tlsext_ticklen = ticklen;
2362
    /*
2363
     * There are two ways to detect a resumed ticket session. One is to set
2364
     * an appropriate session ID and then the server must return a match in
2365
     * ServerHello. This allows the normal client session ID matching to work
2366
     * and we know much earlier that the ticket has been accepted. The
2367
     * other way is to set zero length session ID when the ticket is
2368
     * presented and rely on the handshake to determine session resumption.
2369
     * We choose the former approach because this fits in with assumptions
2370
     * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2371
     * SHA256 is disabled) hash of the ticket.
2372
     */
2373
0
    EVP_Digest(p, ticklen,
2374
0
               s->session->session_id, &s->session->session_id_length,
2375
0
# ifndef OPENSSL_NO_SHA256
2376
0
               EVP_sha256(), NULL);
2377
# else
2378
               EVP_sha1(), NULL);
2379
# endif
2380
0
    ret = 1;
2381
0
    return (ret);
2382
0
 f_err:
2383
0
    ssl3_send_alert(s, SSL3_AL_FATAL, al);
2384
0
 err:
2385
0
    s->state = SSL_ST_ERR;
2386
0
    return (-1);
2387
0
}
2388
2389
int ssl3_get_cert_status(SSL *s)
2390
0
{
2391
0
    int ok, al;
2392
0
    unsigned long resplen, n;
2393
0
    const unsigned char *p;
2394
2395
0
    n = s->method->ssl_get_message(s,
2396
0
                                   SSL3_ST_CR_CERT_STATUS_A,
2397
0
                                   SSL3_ST_CR_CERT_STATUS_B,
2398
0
                                   -1, 16384, &ok);
2399
2400
0
    if (!ok)
2401
0
        return ((int)n);
2402
2403
0
    if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
2404
        /*
2405
         * The CertificateStatus message is optional even if
2406
         * tlsext_status_expected is set
2407
         */
2408
0
        s->s3->tmp.reuse_message = 1;
2409
0
    } else {
2410
0
        if (n < 4) {
2411
            /* need at least status type + length */
2412
0
            al = SSL_AD_DECODE_ERROR;
2413
0
            SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2414
0
            goto f_err;
2415
0
        }
2416
0
        p = (unsigned char *)s->init_msg;
2417
0
        if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
2418
0
            al = SSL_AD_DECODE_ERROR;
2419
0
            SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2420
0
            goto f_err;
2421
0
        }
2422
0
        n2l3(p, resplen);
2423
0
        if (resplen + 4 != n) {
2424
0
            al = SSL_AD_DECODE_ERROR;
2425
0
            SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2426
0
            goto f_err;
2427
0
        }
2428
0
        s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2429
0
        if (s->tlsext_ocsp_resp == NULL) {
2430
0
            al = SSL_AD_INTERNAL_ERROR;
2431
0
            SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2432
0
            goto f_err;
2433
0
        }
2434
0
        s->tlsext_ocsp_resplen = resplen;
2435
0
    }
2436
0
    if (s->ctx->tlsext_status_cb) {
2437
0
        int ret;
2438
0
        ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2439
0
        if (ret == 0) {
2440
0
            al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2441
0
            SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2442
0
            goto f_err;
2443
0
        }
2444
0
        if (ret < 0) {
2445
0
            al = SSL_AD_INTERNAL_ERROR;
2446
0
            SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2447
0
            goto f_err;
2448
0
        }
2449
0
    }
2450
0
    return 1;
2451
0
 f_err:
2452
0
    ssl3_send_alert(s, SSL3_AL_FATAL, al);
2453
0
    s->state = SSL_ST_ERR;
2454
0
    return (-1);
2455
0
}
2456
#endif
2457
2458
int ssl3_get_server_done(SSL *s)
2459
0
{
2460
0
    int ok, ret = 0;
2461
0
    long n;
2462
2463
    /* Second to last param should be very small, like 0 :-) */
2464
0
    n = s->method->ssl_get_message(s,
2465
0
                                   SSL3_ST_CR_SRVR_DONE_A,
2466
0
                                   SSL3_ST_CR_SRVR_DONE_B,
2467
0
                                   SSL3_MT_SERVER_DONE, 30, &ok);
2468
2469
0
    if (!ok)
2470
0
        return ((int)n);
2471
0
    if (n > 0) {
2472
        /* should contain no data */
2473
0
        ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2474
0
        SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2475
0
        s->state = SSL_ST_ERR;
2476
0
        return -1;
2477
0
    }
2478
0
    ret = 1;
2479
0
    return (ret);
2480
0
}
2481
2482
#ifndef OPENSSL_NO_DH
2483
static DH *get_server_static_dh_key(SESS_CERT *scert)
2484
0
{
2485
0
    DH *dh_srvr = NULL;
2486
0
    EVP_PKEY *spkey = NULL;
2487
0
    int idx = scert->peer_cert_type;
2488
2489
0
    if (idx >= 0)
2490
0
        spkey = X509_get_pubkey(scert->peer_pkeys[idx].x509);
2491
0
    if (spkey) {
2492
0
        dh_srvr = EVP_PKEY_get1_DH(spkey);
2493
0
        EVP_PKEY_free(spkey);
2494
0
    }
2495
0
    if (dh_srvr == NULL)
2496
0
        SSLerr(SSL_F_GET_SERVER_STATIC_DH_KEY, ERR_R_INTERNAL_ERROR);
2497
0
    return dh_srvr;
2498
0
}
2499
#endif
2500
2501
int ssl3_send_client_key_exchange(SSL *s)
2502
0
{
2503
0
    unsigned char *p;
2504
0
    int n;
2505
0
    unsigned long alg_k;
2506
0
#ifndef OPENSSL_NO_RSA
2507
0
    unsigned char *q;
2508
0
    EVP_PKEY *pkey = NULL;
2509
0
#endif
2510
#ifndef OPENSSL_NO_KRB5
2511
    KSSL_ERR kssl_err;
2512
#endif                          /* OPENSSL_NO_KRB5 */
2513
0
#ifndef OPENSSL_NO_ECDH
2514
0
    EC_KEY *clnt_ecdh = NULL;
2515
0
    const EC_POINT *srvr_ecpoint = NULL;
2516
0
    EVP_PKEY *srvr_pub_pkey = NULL;
2517
0
    unsigned char *encodedPoint = NULL;
2518
0
    int encoded_pt_len = 0;
2519
0
    BN_CTX *bn_ctx = NULL;
2520
0
#endif
2521
2522
0
    if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2523
0
        p = ssl_handshake_start(s);
2524
2525
0
        alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2526
2527
        /* Fool emacs indentation */
2528
0
        if (0) {
2529
0
        }
2530
0
#ifndef OPENSSL_NO_RSA
2531
0
        else if (alg_k & SSL_kRSA) {
2532
0
            RSA *rsa;
2533
0
            unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2534
2535
0
            if (s->session->sess_cert == NULL) {
2536
                /*
2537
                 * We should always have a server certificate with SSL_kRSA.
2538
                 */
2539
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2540
0
                       ERR_R_INTERNAL_ERROR);
2541
0
                goto err;
2542
0
            }
2543
2544
0
            if (s->session->sess_cert->peer_rsa_tmp != NULL)
2545
0
                rsa = s->session->sess_cert->peer_rsa_tmp;
2546
0
            else {
2547
0
                pkey =
2548
0
                    X509_get_pubkey(s->session->
2549
0
                                    sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
2550
0
                                    x509);
2551
0
                if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2552
0
                    || (pkey->pkey.rsa == NULL)) {
2553
0
                    SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2554
0
                           ERR_R_INTERNAL_ERROR);
2555
0
                    EVP_PKEY_free(pkey);
2556
0
                    goto err;
2557
0
                }
2558
0
                rsa = pkey->pkey.rsa;
2559
0
                EVP_PKEY_free(pkey);
2560
0
            }
2561
2562
0
            tmp_buf[0] = s->client_version >> 8;
2563
0
            tmp_buf[1] = s->client_version & 0xff;
2564
0
            if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
2565
0
                goto err;
2566
2567
0
            s->session->master_key_length = sizeof tmp_buf;
2568
2569
0
            q = p;
2570
            /* Fix buf for TLS and beyond */
2571
0
            if (s->version > SSL3_VERSION)
2572
0
                p += 2;
2573
0
            n = RSA_public_encrypt(sizeof tmp_buf,
2574
0
                                   tmp_buf, p, rsa, RSA_PKCS1_PADDING);
2575
# ifdef PKCS1_CHECK
2576
            if (s->options & SSL_OP_PKCS1_CHECK_1)
2577
                p[1]++;
2578
            if (s->options & SSL_OP_PKCS1_CHECK_2)
2579
                tmp_buf[0] = 0x70;
2580
# endif
2581
0
            if (n <= 0) {
2582
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2583
0
                       SSL_R_BAD_RSA_ENCRYPT);
2584
0
                goto err;
2585
0
            }
2586
2587
            /* Fix buf for TLS and beyond */
2588
0
            if (s->version > SSL3_VERSION) {
2589
0
                s2n(n, q);
2590
0
                n += 2;
2591
0
            }
2592
2593
0
            s->session->master_key_length =
2594
0
                s->method->ssl3_enc->generate_master_secret(s,
2595
0
                                                            s->
2596
0
                                                            session->master_key,
2597
0
                                                            tmp_buf,
2598
0
                                                            sizeof tmp_buf);
2599
0
            OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2600
0
        }
2601
0
#endif
2602
#ifndef OPENSSL_NO_KRB5
2603
        else if (alg_k & SSL_kKRB5) {
2604
            krb5_error_code krb5rc;
2605
            KSSL_CTX *kssl_ctx = s->kssl_ctx;
2606
            /*  krb5_data   krb5_ap_req;  */
2607
            krb5_data *enc_ticket;
2608
            krb5_data authenticator, *authp = NULL;
2609
            EVP_CIPHER_CTX ciph_ctx;
2610
            const EVP_CIPHER *enc = NULL;
2611
            unsigned char iv[EVP_MAX_IV_LENGTH];
2612
            unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2613
            unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
2614
            int padl, outl = sizeof(epms);
2615
2616
            EVP_CIPHER_CTX_init(&ciph_ctx);
2617
2618
# ifdef KSSL_DEBUG
2619
            fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n",
2620
                    alg_k, SSL_kKRB5);
2621
# endif                         /* KSSL_DEBUG */
2622
2623
            authp = NULL;
2624
# ifdef KRB5SENDAUTH
2625
            if (KRB5SENDAUTH)
2626
                authp = &authenticator;
2627
# endif                         /* KRB5SENDAUTH */
2628
2629
            krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
2630
            enc = kssl_map_enc(kssl_ctx->enctype);
2631
            if (enc == NULL)
2632
                goto err;
2633
# ifdef KSSL_DEBUG
2634
            {
2635
                fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc);
2636
                if (krb5rc && kssl_err.text)
2637
                    fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n",
2638
                            kssl_err.text);
2639
            }
2640
# endif                         /* KSSL_DEBUG */
2641
2642
            if (krb5rc) {
2643
                ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2644
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2645
                goto err;
2646
            }
2647
2648
            /*-
2649
             * 20010406 VRS - Earlier versions used KRB5 AP_REQ
2650
             * in place of RFC 2712 KerberosWrapper, as in:
2651
             *
2652
             * Send ticket (copy to *p, set n = length)
2653
             * n = krb5_ap_req.length;
2654
             * memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2655
             * if (krb5_ap_req.data)
2656
             *   kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2657
             *
2658
             * Now using real RFC 2712 KerberosWrapper
2659
             * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2660
             * Note: 2712 "opaque" types are here replaced
2661
             * with a 2-byte length followed by the value.
2662
             * Example:
2663
             * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2664
             * Where "xx xx" = length bytes.  Shown here with
2665
             * optional authenticator omitted.
2666
             */
2667
2668
            /*  KerberosWrapper.Ticket              */
2669
            s2n(enc_ticket->length, p);
2670
            memcpy(p, enc_ticket->data, enc_ticket->length);
2671
            p += enc_ticket->length;
2672
            n = enc_ticket->length + 2;
2673
2674
            /*  KerberosWrapper.Authenticator       */
2675
            if (authp && authp->length) {
2676
                s2n(authp->length, p);
2677
                memcpy(p, authp->data, authp->length);
2678
                p += authp->length;
2679
                n += authp->length + 2;
2680
2681
                free(authp->data);
2682
                authp->data = NULL;
2683
                authp->length = 0;
2684
            } else {
2685
                s2n(0, p);      /* null authenticator length */
2686
                n += 2;
2687
            }
2688
2689
            tmp_buf[0] = s->client_version >> 8;
2690
            tmp_buf[1] = s->client_version & 0xff;
2691
            if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
2692
                goto err;
2693
2694
            /*-
2695
             * 20010420 VRS.  Tried it this way; failed.
2696
             *      EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2697
             *      EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2698
             *                              kssl_ctx->length);
2699
             *      EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2700
             */
2701
2702
            memset(iv, 0, sizeof iv); /* per RFC 1510 */
2703
            EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
2704
            EVP_EncryptUpdate(&ciph_ctx, epms, &outl, tmp_buf,
2705
                              sizeof tmp_buf);
2706
            EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
2707
            outl += padl;
2708
            if (outl > (int)sizeof epms) {
2709
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2710
                       ERR_R_INTERNAL_ERROR);
2711
                goto err;
2712
            }
2713
            EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2714
2715
            /*  KerberosWrapper.EncryptedPreMasterSecret    */
2716
            s2n(outl, p);
2717
            memcpy(p, epms, outl);
2718
            p += outl;
2719
            n += outl + 2;
2720
2721
            s->session->master_key_length =
2722
                s->method->ssl3_enc->generate_master_secret(s,
2723
                                                            s->
2724
                                                            session->master_key,
2725
                                                            tmp_buf,
2726
                                                            sizeof tmp_buf);
2727
2728
            OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2729
            OPENSSL_cleanse(epms, outl);
2730
        }
2731
#endif
2732
0
#ifndef OPENSSL_NO_DH
2733
0
        else if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
2734
0
            DH *dh_srvr, *dh_clnt;
2735
0
            SESS_CERT *scert = s->session->sess_cert;
2736
2737
0
            if (scert == NULL) {
2738
0
                ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2739
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2740
0
                       SSL_R_UNEXPECTED_MESSAGE);
2741
0
                goto err;
2742
0
            }
2743
2744
0
            if (scert->peer_dh_tmp != NULL) {
2745
0
                dh_srvr = scert->peer_dh_tmp;
2746
0
            } else {
2747
0
                dh_srvr = get_server_static_dh_key(scert);
2748
0
                if (dh_srvr == NULL)
2749
0
                    goto err;
2750
0
            }
2751
2752
0
            if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2753
                /* Use client certificate key */
2754
0
                EVP_PKEY *clkey = s->cert->key->privatekey;
2755
0
                dh_clnt = NULL;
2756
0
                if (clkey)
2757
0
                    dh_clnt = EVP_PKEY_get1_DH(clkey);
2758
0
                if (dh_clnt == NULL) {
2759
0
                    SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2760
0
                           ERR_R_INTERNAL_ERROR);
2761
0
                    goto err;
2762
0
                }
2763
0
            } else {
2764
                /* generate a new random key */
2765
0
                if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2766
0
                    SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2767
0
                    goto err;
2768
0
                }
2769
0
                if (!DH_generate_key(dh_clnt)) {
2770
0
                    SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2771
0
                    DH_free(dh_clnt);
2772
0
                    goto err;
2773
0
                }
2774
0
            }
2775
2776
            /*
2777
             * use the 'p' output buffer for the DH key, but make sure to
2778
             * clear it out afterwards
2779
             */
2780
2781
0
            n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt);
2782
0
            if (scert->peer_dh_tmp == NULL)
2783
0
                DH_free(dh_srvr);
2784
2785
0
            if (n <= 0) {
2786
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2787
0
                DH_free(dh_clnt);
2788
0
                goto err;
2789
0
            }
2790
2791
            /* generate master key from the result */
2792
0
            s->session->master_key_length =
2793
0
                s->method->ssl3_enc->generate_master_secret(s,
2794
0
                                                            s->
2795
0
                                                            session->master_key,
2796
0
                                                            p, n);
2797
            /* clean up */
2798
0
            memset(p, 0, n);
2799
2800
0
            if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2801
0
                n = 0;
2802
0
            else {
2803
                /* send off the data */
2804
0
                n = BN_num_bytes(dh_clnt->pub_key);
2805
0
                s2n(n, p);
2806
0
                BN_bn2bin(dh_clnt->pub_key, p);
2807
0
                n += 2;
2808
0
            }
2809
2810
0
            DH_free(dh_clnt);
2811
0
        }
2812
0
#endif
2813
2814
0
#ifndef OPENSSL_NO_ECDH
2815
0
        else if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
2816
0
            const EC_GROUP *srvr_group = NULL;
2817
0
            EC_KEY *tkey;
2818
0
            int ecdh_clnt_cert = 0;
2819
0
            int field_size = 0;
2820
2821
0
            if (s->session->sess_cert == NULL) {
2822
0
                ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2823
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2824
0
                       SSL_R_UNEXPECTED_MESSAGE);
2825
0
                goto err;
2826
0
            }
2827
2828
            /*
2829
             * Did we send out the client's ECDH share for use in premaster
2830
             * computation as part of client certificate? If so, set
2831
             * ecdh_clnt_cert to 1.
2832
             */
2833
0
            if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2834
                /*-
2835
                 * XXX: For now, we do not support client
2836
                 * authentication using ECDH certificates.
2837
                 * To add such support, one needs to add
2838
                 * code that checks for appropriate
2839
                 * conditions and sets ecdh_clnt_cert to 1.
2840
                 * For example, the cert have an ECC
2841
                 * key on the same curve as the server's
2842
                 * and the key should be authorized for
2843
                 * key agreement.
2844
                 *
2845
                 * One also needs to add code in ssl3_connect
2846
                 * to skip sending the certificate verify
2847
                 * message.
2848
                 *
2849
                 * if ((s->cert->key->privatekey != NULL) &&
2850
                 *     (s->cert->key->privatekey->type ==
2851
                 *      EVP_PKEY_EC) && ...)
2852
                 * ecdh_clnt_cert = 1;
2853
                 */
2854
0
            }
2855
2856
0
            if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
2857
0
                tkey = s->session->sess_cert->peer_ecdh_tmp;
2858
0
            } else {
2859
                /* Get the Server Public Key from Cert */
2860
0
                srvr_pub_pkey =
2861
0
                    X509_get_pubkey(s->session->
2862
0
                                    sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2863
0
                if ((srvr_pub_pkey == NULL)
2864
0
                    || (srvr_pub_pkey->type != EVP_PKEY_EC)
2865
0
                    || (srvr_pub_pkey->pkey.ec == NULL)) {
2866
0
                    SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2867
0
                           ERR_R_INTERNAL_ERROR);
2868
0
                    goto err;
2869
0
                }
2870
2871
0
                tkey = srvr_pub_pkey->pkey.ec;
2872
0
            }
2873
2874
0
            srvr_group = EC_KEY_get0_group(tkey);
2875
0
            srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2876
2877
0
            if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2878
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2879
0
                       ERR_R_INTERNAL_ERROR);
2880
0
                goto err;
2881
0
            }
2882
2883
0
            if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2884
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2885
0
                       ERR_R_MALLOC_FAILURE);
2886
0
                goto err;
2887
0
            }
2888
2889
0
            if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2890
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2891
0
                goto err;
2892
0
            }
2893
0
            if (ecdh_clnt_cert) {
2894
                /*
2895
                 * Reuse key info from our certificate We only need our
2896
                 * private key to perform the ECDH computation.
2897
                 */
2898
0
                const BIGNUM *priv_key;
2899
0
                tkey = s->cert->key->privatekey->pkey.ec;
2900
0
                priv_key = EC_KEY_get0_private_key(tkey);
2901
0
                if (priv_key == NULL) {
2902
0
                    SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2903
0
                           ERR_R_MALLOC_FAILURE);
2904
0
                    goto err;
2905
0
                }
2906
0
                if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2907
0
                    SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2908
0
                    goto err;
2909
0
                }
2910
0
            } else {
2911
                /* Generate a new ECDH key pair */
2912
0
                if (!(EC_KEY_generate_key(clnt_ecdh))) {
2913
0
                    SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2914
0
                           ERR_R_ECDH_LIB);
2915
0
                    goto err;
2916
0
                }
2917
0
            }
2918
2919
            /*
2920
             * use the 'p' output buffer for the ECDH key, but make sure to
2921
             * clear it out afterwards
2922
             */
2923
2924
0
            field_size = EC_GROUP_get_degree(srvr_group);
2925
0
            if (field_size <= 0) {
2926
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2927
0
                goto err;
2928
0
            }
2929
0
            n = ECDH_compute_key(p, (field_size + 7) / 8, srvr_ecpoint,
2930
0
                                 clnt_ecdh, NULL);
2931
0
            if (n <= 0) {
2932
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2933
0
                goto err;
2934
0
            }
2935
2936
            /* generate master key from the result */
2937
0
            s->session->master_key_length =
2938
0
                s->method->ssl3_enc->generate_master_secret(s,
2939
0
                                                            s->
2940
0
                                                            session->master_key,
2941
0
                                                            p, n);
2942
2943
0
            memset(p, 0, n);    /* clean up */
2944
2945
0
            if (ecdh_clnt_cert) {
2946
                /* Send empty client key exch message */
2947
0
                n = 0;
2948
0
            } else {
2949
                /*
2950
                 * First check the size of encoding and allocate memory
2951
                 * accordingly.
2952
                 */
2953
0
                encoded_pt_len =
2954
0
                    EC_POINT_point2oct(srvr_group,
2955
0
                                       EC_KEY_get0_public_key(clnt_ecdh),
2956
0
                                       POINT_CONVERSION_UNCOMPRESSED,
2957
0
                                       NULL, 0, NULL);
2958
2959
0
                encodedPoint = (unsigned char *)
2960
0
                    OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2961
0
                bn_ctx = BN_CTX_new();
2962
0
                if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2963
0
                    SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2964
0
                           ERR_R_MALLOC_FAILURE);
2965
0
                    goto err;
2966
0
                }
2967
2968
                /* Encode the public key */
2969
0
                n = EC_POINT_point2oct(srvr_group,
2970
0
                                       EC_KEY_get0_public_key(clnt_ecdh),
2971
0
                                       POINT_CONVERSION_UNCOMPRESSED,
2972
0
                                       encodedPoint, encoded_pt_len, bn_ctx);
2973
2974
0
                *p = n;         /* length of encoded point */
2975
                /* Encoded point will be copied here */
2976
0
                p += 1;
2977
                /* copy the point */
2978
0
                memcpy((unsigned char *)p, encodedPoint, n);
2979
                /* increment n to account for length field */
2980
0
                n += 1;
2981
0
            }
2982
2983
            /* Free allocated memory */
2984
0
            BN_CTX_free(bn_ctx);
2985
0
            if (encodedPoint != NULL)
2986
0
                OPENSSL_free(encodedPoint);
2987
0
            if (clnt_ecdh != NULL)
2988
0
                EC_KEY_free(clnt_ecdh);
2989
0
            EVP_PKEY_free(srvr_pub_pkey);
2990
0
        }
2991
0
#endif                          /* !OPENSSL_NO_ECDH */
2992
0
        else if (alg_k & SSL_kGOST) {
2993
            /* GOST key exchange message creation */
2994
0
            EVP_PKEY_CTX *pkey_ctx;
2995
0
            X509 *peer_cert;
2996
0
            size_t msglen;
2997
0
            unsigned int md_len;
2998
0
            int keytype;
2999
0
            unsigned char premaster_secret[32], shared_ukm[32], tmp[256];
3000
0
            EVP_MD_CTX *ukm_hash;
3001
0
            EVP_PKEY *pub_key;
3002
3003
            /*
3004
             * Get server sertificate PKEY and create ctx from it
3005
             */
3006
0
            peer_cert =
3007
0
                s->session->
3008
0
                sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509;
3009
0
            if (!peer_cert)
3010
0
                peer_cert =
3011
0
                    s->session->
3012
0
                    sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509;
3013
0
            if (!peer_cert) {
3014
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3015
0
                       SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
3016
0
                goto err;
3017
0
            }
3018
3019
0
            pkey_ctx = EVP_PKEY_CTX_new(pub_key =
3020
0
                                        X509_get_pubkey(peer_cert), NULL);
3021
0
            if (pkey_ctx == NULL) {
3022
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3023
0
                       ERR_R_MALLOC_FAILURE);
3024
0
                goto err;
3025
0
            }
3026
            /*
3027
             * If we have send a certificate, and certificate key
3028
             *
3029
             * * parameters match those of server certificate, use
3030
             * certificate key for key exchange
3031
             */
3032
3033
            /* Otherwise, generate ephemeral key pair */
3034
3035
0
            if (pkey_ctx == NULL
3036
0
                    || EVP_PKEY_encrypt_init(pkey_ctx) <= 0
3037
                    /* Generate session key */
3038
0
                    || RAND_bytes(premaster_secret, 32) <= 0) {
3039
0
                EVP_PKEY_CTX_free(pkey_ctx);
3040
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3041
0
                       ERR_R_INTERNAL_ERROR);
3042
0
                goto err;
3043
0
            }
3044
            /*
3045
             * Compute shared IV and store it in algorithm-specific context
3046
             * data
3047
             */
3048
0
            ukm_hash = EVP_MD_CTX_create();
3049
0
            if (EVP_DigestInit(ukm_hash,
3050
0
                               EVP_get_digestbynid(NID_id_GostR3411_94)) <= 0
3051
0
                    || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
3052
0
                                        SSL3_RANDOM_SIZE) <= 0
3053
0
                    || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
3054
0
                                        SSL3_RANDOM_SIZE) <= 0
3055
0
                    || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
3056
0
                EVP_MD_CTX_destroy(ukm_hash);
3057
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3058
0
                       ERR_R_INTERNAL_ERROR);
3059
0
                goto err;
3060
0
            }
3061
0
            EVP_MD_CTX_destroy(ukm_hash);
3062
0
            if (EVP_PKEY_CTX_ctrl
3063
0
                (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
3064
0
                 shared_ukm) < 0) {
3065
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3066
0
                       SSL_R_LIBRARY_BUG);
3067
0
                goto err;
3068
0
            }
3069
            /* Make GOST keytransport blob message */
3070
            /*
3071
             * Encapsulate it into sequence
3072
             */
3073
0
            *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
3074
0
            msglen = 255;
3075
0
            if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, premaster_secret, 32)
3076
0
                <= 0) {
3077
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3078
0
                       SSL_R_LIBRARY_BUG);
3079
0
                goto err;
3080
0
            }
3081
0
            if (msglen >= 0x80) {
3082
0
                *(p++) = 0x81;
3083
0
                *(p++) = msglen & 0xff;
3084
0
                n = msglen + 3;
3085
0
            } else {
3086
0
                *(p++) = msglen & 0xff;
3087
0
                n = msglen + 2;
3088
0
            }
3089
0
            memcpy(p, tmp, msglen);
3090
0
            EVP_PKEY_CTX_free(pkey_ctx);
3091
0
            s->session->master_key_length =
3092
0
                s->method->ssl3_enc->generate_master_secret(s,
3093
0
                                                            s->
3094
0
                                                            session->master_key,
3095
0
                                                            premaster_secret,
3096
0
                                                            32);
3097
0
            EVP_PKEY_free(pub_key);
3098
3099
0
        }
3100
0
#ifndef OPENSSL_NO_SRP
3101
0
        else if (alg_k & SSL_kSRP) {
3102
0
            if (s->srp_ctx.A != NULL) {
3103
                /* send off the data */
3104
0
                n = BN_num_bytes(s->srp_ctx.A);
3105
0
                s2n(n, p);
3106
0
                BN_bn2bin(s->srp_ctx.A, p);
3107
0
                n += 2;
3108
0
            } else {
3109
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3110
0
                       ERR_R_INTERNAL_ERROR);
3111
0
                goto err;
3112
0
            }
3113
0
            if (s->session->srp_username != NULL)
3114
0
                OPENSSL_free(s->session->srp_username);
3115
0
            s->session->srp_username = BUF_strdup(s->srp_ctx.login);
3116
0
            if (s->session->srp_username == NULL) {
3117
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3118
0
                       ERR_R_MALLOC_FAILURE);
3119
0
                goto err;
3120
0
            }
3121
3122
0
            if ((s->session->master_key_length =
3123
0
                 SRP_generate_client_master_secret(s,
3124
0
                                                   s->session->master_key)) <
3125
0
                0) {
3126
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3127
0
                       ERR_R_INTERNAL_ERROR);
3128
0
                goto err;
3129
0
            }
3130
0
        }
3131
0
#endif
3132
0
#ifndef OPENSSL_NO_PSK
3133
0
        else if (alg_k & SSL_kPSK) {
3134
            /*
3135
             * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
3136
             * \0-terminated identity. The last byte is for us for simulating
3137
             * strnlen.
3138
             */
3139
0
            char identity[PSK_MAX_IDENTITY_LEN + 2];
3140
0
            size_t identity_len;
3141
0
            unsigned char *t = NULL;
3142
0
            unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
3143
0
            unsigned int pre_ms_len = 0, psk_len = 0;
3144
0
            int psk_err = 1;
3145
3146
0
            n = 0;
3147
0
            if (s->psk_client_callback == NULL) {
3148
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3149
0
                       SSL_R_PSK_NO_CLIENT_CB);
3150
0
                goto err;
3151
0
            }
3152
3153
0
            memset(identity, 0, sizeof(identity));
3154
0
            psk_len = s->psk_client_callback(s, s->session->psk_identity_hint,
3155
0
                                             identity, sizeof(identity) - 1,
3156
0
                                             psk_or_pre_ms,
3157
0
                                             sizeof(psk_or_pre_ms));
3158
0
            if (psk_len > PSK_MAX_PSK_LEN) {
3159
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3160
0
                       ERR_R_INTERNAL_ERROR);
3161
0
                goto psk_err;
3162
0
            } else if (psk_len == 0) {
3163
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3164
0
                       SSL_R_PSK_IDENTITY_NOT_FOUND);
3165
0
                goto psk_err;
3166
0
            }
3167
0
            identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
3168
0
            identity_len = strlen(identity);
3169
0
            if (identity_len > PSK_MAX_IDENTITY_LEN) {
3170
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3171
0
                       ERR_R_INTERNAL_ERROR);
3172
0
                goto psk_err;
3173
0
            }
3174
            /* create PSK pre_master_secret */
3175
0
            pre_ms_len = 2 + psk_len + 2 + psk_len;
3176
0
            t = psk_or_pre_ms;
3177
0
            memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
3178
0
            s2n(psk_len, t);
3179
0
            memset(t, 0, psk_len);
3180
0
            t += psk_len;
3181
0
            s2n(psk_len, t);
3182
3183
0
            if (s->session->psk_identity_hint != NULL)
3184
0
                OPENSSL_free(s->session->psk_identity_hint);
3185
0
            s->session->psk_identity_hint =
3186
0
                BUF_strdup(s->ctx->psk_identity_hint);
3187
0
            if (s->ctx->psk_identity_hint != NULL
3188
0
                && s->session->psk_identity_hint == NULL) {
3189
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3190
0
                       ERR_R_MALLOC_FAILURE);
3191
0
                goto psk_err;
3192
0
            }
3193
3194
0
            if (s->session->psk_identity != NULL)
3195
0
                OPENSSL_free(s->session->psk_identity);
3196
0
            s->session->psk_identity = BUF_strdup(identity);
3197
0
            if (s->session->psk_identity == NULL) {
3198
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
3199
0
                       ERR_R_MALLOC_FAILURE);
3200
0
                goto psk_err;
3201
0
            }
3202
3203
0
            s->session->master_key_length =
3204
0
                s->method->ssl3_enc->generate_master_secret(s,
3205
0
                                                            s->
3206
0
                                                            session->master_key,
3207
0
                                                            psk_or_pre_ms,
3208
0
                                                            pre_ms_len);
3209
0
            s2n(identity_len, p);
3210
0
            memcpy(p, identity, identity_len);
3211
0
            n = 2 + identity_len;
3212
0
            psk_err = 0;
3213
0
 psk_err:
3214
0
            OPENSSL_cleanse(identity, sizeof(identity));
3215
0
            OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
3216
0
            if (psk_err != 0) {
3217
0
                ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3218
0
                goto err;
3219
0
            }
3220
0
        }
3221
0
#endif
3222
0
        else {
3223
0
            ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3224
0
            SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3225
0
            goto err;
3226
0
        }
3227
3228
0
        ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
3229
0
        s->state = SSL3_ST_CW_KEY_EXCH_B;
3230
0
    }
3231
3232
    /* SSL3_ST_CW_KEY_EXCH_B */
3233
0
    return ssl_do_write(s);
3234
0
 err:
3235
0
#ifndef OPENSSL_NO_ECDH
3236
0
    BN_CTX_free(bn_ctx);
3237
0
    if (encodedPoint != NULL)
3238
0
        OPENSSL_free(encodedPoint);
3239
0
    if (clnt_ecdh != NULL)
3240
0
        EC_KEY_free(clnt_ecdh);
3241
0
    EVP_PKEY_free(srvr_pub_pkey);
3242
0
#endif
3243
0
    s->state = SSL_ST_ERR;
3244
0
    return (-1);
3245
0
}
3246
3247
int ssl3_send_client_verify(SSL *s)
3248
0
{
3249
0
    unsigned char *p;
3250
0
    unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3251
0
    EVP_PKEY *pkey;
3252
0
    EVP_PKEY_CTX *pctx = NULL;
3253
0
    EVP_MD_CTX mctx;
3254
0
    unsigned u = 0;
3255
0
    unsigned long n;
3256
0
    int j;
3257
3258
0
    EVP_MD_CTX_init(&mctx);
3259
3260
0
    if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3261
0
        p = ssl_handshake_start(s);
3262
0
        pkey = s->cert->key->privatekey;
3263
/* Create context from key and test if sha1 is allowed as digest */
3264
0
        pctx = EVP_PKEY_CTX_new(pkey, NULL);
3265
0
        if (pctx == NULL || EVP_PKEY_sign_init(pctx) <= 0) {
3266
0
            SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3267
0
            goto err;
3268
0
        }
3269
0
        if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3270
0
            if (!SSL_USE_SIGALGS(s))
3271
0
                s->method->ssl3_enc->cert_verify_mac(s,
3272
0
                                                     NID_sha1,
3273
0
                                                     &(data
3274
0
                                                       [MD5_DIGEST_LENGTH]));
3275
0
        } else {
3276
0
            ERR_clear_error();
3277
0
        }
3278
        /*
3279
         * For TLS v1.2 send signature algorithm and signature using agreed
3280
         * digest and cached handshake records.
3281
         */
3282
0
        if (SSL_USE_SIGALGS(s)) {
3283
0
            long hdatalen = 0;
3284
0
            void *hdata;
3285
0
            const EVP_MD *md = s->cert->key->digest;
3286
0
            hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3287
0
            if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3288
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3289
0
                goto err;
3290
0
            }
3291
0
            p += 2;
3292
#ifdef SSL_DEBUG
3293
            fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3294
                    EVP_MD_name(md));
3295
#endif
3296
0
            if (!EVP_SignInit_ex(&mctx, md, NULL)
3297
0
                || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3298
0
                || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3299
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3300
0
                goto err;
3301
0
            }
3302
0
            s2n(u, p);
3303
0
            n = u + 4;
3304
0
            if (!ssl3_digest_cached_records(s))
3305
0
                goto err;
3306
0
        } else
3307
0
#ifndef OPENSSL_NO_RSA
3308
0
        if (pkey->type == EVP_PKEY_RSA) {
3309
0
            s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3310
0
            if (RSA_sign(NID_md5_sha1, data,
3311
0
                         MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3312
0
                         &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3313
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3314
0
                goto err;
3315
0
            }
3316
0
            s2n(u, p);
3317
0
            n = u + 2;
3318
0
        } else
3319
0
#endif
3320
0
#ifndef OPENSSL_NO_DSA
3321
0
        if (pkey->type == EVP_PKEY_DSA) {
3322
0
            if (!DSA_sign(pkey->save_type,
3323
0
                          &(data[MD5_DIGEST_LENGTH]),
3324
0
                          SHA_DIGEST_LENGTH, &(p[2]),
3325
0
                          (unsigned int *)&j, pkey->pkey.dsa)) {
3326
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3327
0
                goto err;
3328
0
            }
3329
0
            s2n(j, p);
3330
0
            n = j + 2;
3331
0
        } else
3332
0
#endif
3333
0
#ifndef OPENSSL_NO_ECDSA
3334
0
        if (pkey->type == EVP_PKEY_EC) {
3335
0
            if (!ECDSA_sign(pkey->save_type,
3336
0
                            &(data[MD5_DIGEST_LENGTH]),
3337
0
                            SHA_DIGEST_LENGTH, &(p[2]),
3338
0
                            (unsigned int *)&j, pkey->pkey.ec)) {
3339
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3340
0
                goto err;
3341
0
            }
3342
0
            s2n(j, p);
3343
0
            n = j + 2;
3344
0
        } else
3345
0
#endif
3346
0
        if (pkey->type == NID_id_GostR3410_94
3347
0
                || pkey->type == NID_id_GostR3410_2001) {
3348
0
            unsigned char signbuf[64];
3349
0
            int i;
3350
0
            size_t sigsize = 64;
3351
0
            s->method->ssl3_enc->cert_verify_mac(s,
3352
0
                                                 NID_id_GostR3411_94, data);
3353
0
            if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3354
0
                SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3355
0
                goto err;
3356
0
            }
3357
0
            for (i = 63, j = 0; i >= 0; j++, i--) {
3358
0
                p[2 + j] = signbuf[i];
3359
0
            }
3360
0
            s2n(j, p);
3361
0
            n = j + 2;
3362
0
        } else {
3363
0
            SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3364
0
            goto err;
3365
0
        }
3366
0
        ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
3367
0
        s->state = SSL3_ST_CW_CERT_VRFY_B;
3368
0
    }
3369
0
    EVP_MD_CTX_cleanup(&mctx);
3370
0
    EVP_PKEY_CTX_free(pctx);
3371
0
    return ssl_do_write(s);
3372
0
 err:
3373
0
    EVP_MD_CTX_cleanup(&mctx);
3374
0
    EVP_PKEY_CTX_free(pctx);
3375
0
    s->state = SSL_ST_ERR;
3376
0
    return (-1);
3377
0
}
3378
3379
/*
3380
 * Check a certificate can be used for client authentication. Currently check
3381
 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3382
 * certificates can be used and optionally checks suitability for Suite B.
3383
 */
3384
static int ssl3_check_client_certificate(SSL *s)
3385
0
{
3386
0
    unsigned long alg_k;
3387
0
    if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3388
0
        return 0;
3389
    /* If no suitable signature algorithm can't use certificate */
3390
0
    if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
3391
0
        return 0;
3392
    /*
3393
     * If strict mode check suitability of chain before using it. This also
3394
     * adjusts suite B digest if necessary.
3395
     */
3396
0
    if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3397
0
        !tls1_check_chain(s, NULL, NULL, NULL, -2))
3398
0
        return 0;
3399
0
    alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3400
    /* See if we can use client certificate for fixed DH */
3401
0
    if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3402
0
        SESS_CERT *scert = s->session->sess_cert;
3403
0
        int i = scert->peer_cert_type;
3404
0
        EVP_PKEY *clkey = NULL, *spkey = NULL;
3405
0
        clkey = s->cert->key->privatekey;
3406
        /* If client key not DH assume it can be used */
3407
0
        if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3408
0
            return 1;
3409
0
        if (i >= 0)
3410
0
            spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
3411
0
        if (spkey) {
3412
            /* Compare server and client parameters */
3413
0
            i = EVP_PKEY_cmp_parameters(clkey, spkey);
3414
0
            EVP_PKEY_free(spkey);
3415
0
            if (i != 1)
3416
0
                return 0;
3417
0
        }
3418
0
        s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3419
0
    }
3420
0
    return 1;
3421
0
}
3422
3423
int ssl3_send_client_certificate(SSL *s)
3424
0
{
3425
0
    X509 *x509 = NULL;
3426
0
    EVP_PKEY *pkey = NULL;
3427
0
    int i;
3428
3429
0
    if (s->state == SSL3_ST_CW_CERT_A) {
3430
        /* Let cert callback update client certificates if required */
3431
0
        if (s->cert->cert_cb) {
3432
0
            i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3433
0
            if (i < 0) {
3434
0
                s->rwstate = SSL_X509_LOOKUP;
3435
0
                return -1;
3436
0
            }
3437
0
            if (i == 0) {
3438
0
                ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3439
0
                s->state = SSL_ST_ERR;
3440
0
                return 0;
3441
0
            }
3442
0
            s->rwstate = SSL_NOTHING;
3443
0
        }
3444
0
        if (ssl3_check_client_certificate(s))
3445
0
            s->state = SSL3_ST_CW_CERT_C;
3446
0
        else
3447
0
            s->state = SSL3_ST_CW_CERT_B;
3448
0
    }
3449
3450
    /* We need to get a client cert */
3451
0
    if (s->state == SSL3_ST_CW_CERT_B) {
3452
        /*
3453
         * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3454
         * return(-1); We then get retied later
3455
         */
3456
0
        i = ssl_do_client_cert_cb(s, &x509, &pkey);
3457
0
        if (i < 0) {
3458
0
            s->rwstate = SSL_X509_LOOKUP;
3459
0
            return (-1);
3460
0
        }
3461
0
        s->rwstate = SSL_NOTHING;
3462
0
        if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3463
0
            s->state = SSL3_ST_CW_CERT_B;
3464
0
            if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3465
0
                i = 0;
3466
0
        } else if (i == 1) {
3467
0
            i = 0;
3468
0
            SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3469
0
                   SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3470
0
        }
3471
3472
0
        if (x509 != NULL)
3473
0
            X509_free(x509);
3474
0
        if (pkey != NULL)
3475
0
            EVP_PKEY_free(pkey);
3476
0
        if (i && !ssl3_check_client_certificate(s))
3477
0
            i = 0;
3478
0
        if (i == 0) {
3479
0
            if (s->version == SSL3_VERSION) {
3480
0
                s->s3->tmp.cert_req = 0;
3481
0
                ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3482
0
                return (1);
3483
0
            } else {
3484
0
                s->s3->tmp.cert_req = 2;
3485
0
            }
3486
0
        }
3487
3488
        /* Ok, we have a cert */
3489
0
        s->state = SSL3_ST_CW_CERT_C;
3490
0
    }
3491
3492
0
    if (s->state == SSL3_ST_CW_CERT_C) {
3493
0
        s->state = SSL3_ST_CW_CERT_D;
3494
0
        if (!ssl3_output_cert_chain(s,
3495
0
                                    (s->s3->tmp.cert_req ==
3496
0
                                     2) ? NULL : s->cert->key)) {
3497
0
            SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3498
0
            ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3499
0
            s->state = SSL_ST_ERR;
3500
0
            return 0;
3501
0
        }
3502
0
    }
3503
    /* SSL3_ST_CW_CERT_D */
3504
0
    return ssl_do_write(s);
3505
0
}
3506
3507
0
#define has_bits(i,m)   (((i)&(m)) == (m))
3508
3509
int ssl3_check_cert_and_algorithm(SSL *s)
3510
0
{
3511
0
    int i, idx;
3512
0
    long alg_k, alg_a;
3513
0
    EVP_PKEY *pkey = NULL;
3514
0
    int pkey_bits;
3515
0
    SESS_CERT *sc;
3516
0
#ifndef OPENSSL_NO_RSA
3517
0
    RSA *rsa;
3518
0
#endif
3519
0
#ifndef OPENSSL_NO_DH
3520
0
    DH *dh;
3521
0
#endif
3522
0
    int al = SSL_AD_HANDSHAKE_FAILURE;
3523
3524
0
    alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3525
0
    alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3526
3527
    /* we don't have a certificate */
3528
0
    if ((alg_a & (SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK))
3529
0
        return (1);
3530
3531
0
    sc = s->session->sess_cert;
3532
0
    if (sc == NULL) {
3533
0
        SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3534
0
        goto err;
3535
0
    }
3536
0
#ifndef OPENSSL_NO_RSA
3537
0
    rsa = s->session->sess_cert->peer_rsa_tmp;
3538
0
#endif
3539
0
#ifndef OPENSSL_NO_DH
3540
0
    dh = s->session->sess_cert->peer_dh_tmp;
3541
0
#endif
3542
3543
    /* This is the passed certificate */
3544
3545
0
    idx = sc->peer_cert_type;
3546
0
#ifndef OPENSSL_NO_ECDH
3547
0
    if (idx == SSL_PKEY_ECC) {
3548
0
        if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
3549
            /* check failed */
3550
0
            SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3551
0
            goto f_err;
3552
0
        } else {
3553
0
            return 1;
3554
0
        }
3555
0
    } else if (alg_a & SSL_aECDSA) {
3556
0
        SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3557
0
               SSL_R_MISSING_ECDSA_SIGNING_CERT);
3558
0
        goto f_err;
3559
0
    } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3560
0
        SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3561
0
        goto f_err;
3562
0
    }
3563
0
#endif
3564
0
    pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
3565
0
    pkey_bits = EVP_PKEY_bits(pkey);
3566
0
    i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
3567
0
    EVP_PKEY_free(pkey);
3568
3569
    /* Check that we have a certificate if we require one */
3570
0
    if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3571
0
        SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3572
0
               SSL_R_MISSING_RSA_SIGNING_CERT);
3573
0
        goto f_err;
3574
0
    }
3575
0
#ifndef OPENSSL_NO_DSA
3576
0
    else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3577
0
        SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3578
0
               SSL_R_MISSING_DSA_SIGNING_CERT);
3579
0
        goto f_err;
3580
0
    }
3581
0
#endif
3582
0
#ifndef OPENSSL_NO_RSA
3583
0
    if (alg_k & SSL_kRSA) {
3584
0
        if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3585
0
            !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3586
0
            SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3587
0
                   SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3588
0
            goto f_err;
3589
0
        } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3590
0
            if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3591
0
                if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3592
0
                    SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3593
0
                           SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3594
0
                    goto f_err;
3595
0
                }
3596
0
                if (rsa != NULL) {
3597
                    /* server key exchange is not allowed. */
3598
0
                    al = SSL_AD_INTERNAL_ERROR;
3599
0
                    SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3600
0
                    goto f_err;
3601
0
                }
3602
0
            }
3603
0
        }
3604
0
    }
3605
0
#endif
3606
0
#ifndef OPENSSL_NO_DH
3607
0
    if ((alg_k & SSL_kEDH) && dh == NULL) {
3608
0
        al = SSL_AD_INTERNAL_ERROR;
3609
0
        SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3610
0
        goto f_err;
3611
0
    }
3612
0
    if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3613
0
               !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3614
0
        SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3615
0
               SSL_R_MISSING_DH_RSA_CERT);
3616
0
        goto f_err;
3617
0
    }
3618
0
# ifndef OPENSSL_NO_DSA
3619
0
    if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3620
0
        !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3621
0
        SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3622
0
               SSL_R_MISSING_DH_DSA_CERT);
3623
0
        goto f_err;
3624
0
    }
3625
0
# endif
3626
3627
0
    if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
3628
0
        int dh_size;
3629
0
        if (alg_k & SSL_kDHE) {
3630
0
            dh_size = BN_num_bits(dh->p);
3631
0
        } else {
3632
0
            DH *dh_srvr = get_server_static_dh_key(sc);
3633
0
            if (dh_srvr == NULL)
3634
0
                goto f_err;
3635
0
            dh_size = BN_num_bits(dh_srvr->p);
3636
0
            DH_free(dh_srvr);
3637
0
        }
3638
3639
0
        if ((!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 1024)
3640
0
            || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 512)) {
3641
0
            SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_DH_KEY_TOO_SMALL);
3642
0
            goto f_err;
3643
0
        }
3644
0
    }
3645
0
#endif  /* !OPENSSL_NO_DH */
3646
3647
0
    if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3648
0
        pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3649
0
#ifndef OPENSSL_NO_RSA
3650
0
        if (alg_k & SSL_kRSA) {
3651
0
            if (rsa == NULL) {
3652
0
                SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3653
0
                       SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3654
0
                goto f_err;
3655
0
            } else if (BN_num_bits(rsa->n) >
3656
0
                SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3657
                /* We have a temporary RSA key but it's too large. */
3658
0
                al = SSL_AD_EXPORT_RESTRICTION;
3659
0
                SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3660
0
                       SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3661
0
                goto f_err;
3662
0
            }
3663
0
        } else
3664
0
#endif
3665
0
#ifndef OPENSSL_NO_DH
3666
0
        if (alg_k & SSL_kDHE) {
3667
0
            if (BN_num_bits(dh->p) >
3668
0
                SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3669
                /* We have a temporary DH key but it's too large. */
3670
0
                al = SSL_AD_EXPORT_RESTRICTION;
3671
0
                SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3672
0
                       SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3673
0
                goto f_err;
3674
0
            }
3675
0
        } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3676
            /* The cert should have had an export DH key. */
3677
0
            al = SSL_AD_EXPORT_RESTRICTION;
3678
0
            SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3679
0
                   SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3680
0
                goto f_err;
3681
0
        } else
3682
0
#endif
3683
0
        {
3684
0
            SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3685
0
                   SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3686
0
            goto f_err;
3687
0
        }
3688
0
    }
3689
0
    return (1);
3690
0
 f_err:
3691
0
    ssl3_send_alert(s, SSL3_AL_FATAL, al);
3692
0
 err:
3693
0
    return (0);
3694
0
}
3695
3696
#ifndef OPENSSL_NO_TLSEXT
3697
/*
3698
 * Normally, we can tell if the server is resuming the session from
3699
 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3700
 * message after the ServerHello to determine if the server is resuming.
3701
 * Therefore, we allow EAP-FAST to peek ahead.
3702
 * ssl3_check_finished returns 1 if we are resuming from an external
3703
 * pre-shared secret, we have a "ticket" and the next server handshake message
3704
 * is Finished; and 0 otherwise. It returns -1 upon an error.
3705
 */
3706
static int ssl3_check_finished(SSL *s)
3707
0
{
3708
0
    int ok = 0;
3709
3710
0
    if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3711
0
        !s->session->tlsext_tick)
3712
0
        return 0;
3713
3714
    /* Need to permit this temporarily, in case the next message is Finished. */
3715
0
    s->s3->flags |= SSL3_FLAGS_CCS_OK;
3716
    /*
3717
     * This function is called when we might get a Certificate message instead,
3718
     * so permit appropriate message length.
3719
     * We ignore the return value as we're only interested in the message type
3720
     * and not its length.
3721
     */
3722
0
    s->method->ssl_get_message(s,
3723
0
                               SSL3_ST_CR_CERT_A,
3724
0
                               SSL3_ST_CR_CERT_B,
3725
0
                               -1, s->max_cert_list, &ok);
3726
0
    s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
3727
3728
0
    if (!ok)
3729
0
        return -1;
3730
3731
0
    s->s3->tmp.reuse_message = 1;
3732
3733
0
    if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
3734
0
        return 1;
3735
3736
    /* If we're not done, then the CCS arrived early and we should bail. */
3737
0
    if (s->s3->change_cipher_spec) {
3738
0
        SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY);
3739
0
        ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
3740
0
        return -1;
3741
0
    }
3742
3743
0
    return 0;
3744
0
}
3745
3746
# ifndef OPENSSL_NO_NEXTPROTONEG
3747
int ssl3_send_next_proto(SSL *s)
3748
0
{
3749
0
    unsigned int len, padding_len;
3750
0
    unsigned char *d;
3751
3752
0
    if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3753
0
        len = s->next_proto_negotiated_len;
3754
0
        padding_len = 32 - ((len + 2) % 32);
3755
0
        d = (unsigned char *)s->init_buf->data;
3756
0
        d[4] = len;
3757
0
        memcpy(d + 5, s->next_proto_negotiated, len);
3758
0
        d[5 + len] = padding_len;
3759
0
        memset(d + 6 + len, 0, padding_len);
3760
0
        *(d++) = SSL3_MT_NEXT_PROTO;
3761
0
        l2n3(2 + len + padding_len, d);
3762
0
        s->state = SSL3_ST_CW_NEXT_PROTO_B;
3763
0
        s->init_num = 4 + 2 + len + padding_len;
3764
0
        s->init_off = 0;
3765
0
    }
3766
3767
0
    return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3768
0
}
3769
#endif                          /* !OPENSSL_NO_NEXTPROTONEG */
3770
#endif                          /* !OPENSSL_NO_TLSEXT */
3771
3772
int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3773
0
{
3774
0
    int i = 0;
3775
0
#ifndef OPENSSL_NO_ENGINE
3776
0
    if (s->ctx->client_cert_engine) {
3777
0
        i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3778
0
                                        SSL_get_client_CA_list(s),
3779
0
                                        px509, ppkey, NULL, NULL, NULL);
3780
0
        if (i != 0)
3781
0
            return i;
3782
0
    }
3783
0
#endif
3784
0
    if (s->ctx->client_cert_cb)
3785
0
        i = s->ctx->client_cert_cb(s, px509, ppkey);
3786
0
    return i;
3787
0
}