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