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