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