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