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