]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/s3_srvr.c
Check for ClientHello message overruns
[thirdparty/openssl.git] / ssl / s3_srvr.c
1 /* ssl/s3_srvr.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-2005 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 #define REUSE_CIPHER_BUG
126 #define NETSCAPE_HANG_BUG
127
128 #include <stdio.h>
129 #include "ssl_locl.h"
130 #include "kssl_lcl.h"
131 #include "../crypto/constant_time_locl.h"
132 #include <openssl/buffer.h>
133 #include <openssl/rand.h>
134 #include <openssl/objects.h>
135 #include <openssl/evp.h>
136 #include <openssl/hmac.h>
137 #include <openssl/x509.h>
138 #ifndef OPENSSL_NO_DH
139 # include <openssl/dh.h>
140 #endif
141 #include <openssl/bn.h>
142 #ifndef OPENSSL_NO_KRB5
143 # include <openssl/krb5_asn.h>
144 #endif
145 #include <openssl/md5.h>
146
147 static SSL_METHOD *ssl3_get_server_method(int ver);
148 #ifndef OPENSSL_NO_ECDH
149 static int nid2curve_id(int nid);
150 #endif
151
152 static SSL_METHOD *ssl3_get_server_method(int ver)
153 {
154 if (ver == SSL3_VERSION)
155 return (SSLv3_server_method());
156 else
157 return (NULL);
158 }
159
160 IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
161 ssl3_accept,
162 ssl_undefined_function, ssl3_get_server_method)
163
164 int ssl3_accept(SSL *s)
165 {
166 BUF_MEM *buf;
167 unsigned long l, Time = (unsigned long)time(NULL);
168 void (*cb) (const SSL *ssl, int type, int val) = NULL;
169 int ret = -1;
170 int new_state, state, skip = 0;
171
172 RAND_add(&Time, sizeof(Time), 0);
173 ERR_clear_error();
174 clear_sys_error();
175
176 if (s->info_callback != NULL)
177 cb = s->info_callback;
178 else if (s->ctx->info_callback != NULL)
179 cb = s->ctx->info_callback;
180
181 /* init things to blank */
182 s->in_handshake++;
183 if (!SSL_in_init(s) || SSL_in_before(s))
184 SSL_clear(s);
185
186 if (s->cert == NULL) {
187 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_NO_CERTIFICATE_SET);
188 return (-1);
189 }
190
191 for (;;) {
192 state = s->state;
193
194 switch (s->state) {
195 case SSL_ST_RENEGOTIATE:
196 s->new_session = 1;
197 /* s->state=SSL_ST_ACCEPT; */
198
199 case SSL_ST_BEFORE:
200 case SSL_ST_ACCEPT:
201 case SSL_ST_BEFORE | SSL_ST_ACCEPT:
202 case SSL_ST_OK | SSL_ST_ACCEPT:
203
204 s->server = 1;
205 if (cb != NULL)
206 cb(s, SSL_CB_HANDSHAKE_START, 1);
207
208 if ((s->version >> 8) != 3) {
209 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
210 return -1;
211 }
212 s->type = SSL_ST_ACCEPT;
213
214 if (s->init_buf == NULL) {
215 if ((buf = BUF_MEM_new()) == NULL) {
216 ret = -1;
217 goto end;
218 }
219 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
220 ret = -1;
221 goto end;
222 }
223 s->init_buf = buf;
224 }
225
226 if (!ssl3_setup_buffers(s)) {
227 ret = -1;
228 goto end;
229 }
230
231 s->init_num = 0;
232 s->s3->flags &= ~SSL3_FLAGS_SGC_RESTART_DONE;
233
234 if (s->state != SSL_ST_RENEGOTIATE) {
235 /*
236 * Ok, we now need to push on a buffering BIO so that the
237 * output is sent in a way that TCP likes :-)
238 */
239 if (!ssl_init_wbio_buffer(s, 1)) {
240 ret = -1;
241 goto end;
242 }
243
244 ssl3_init_finished_mac(s);
245 s->state = SSL3_ST_SR_CLNT_HELLO_A;
246 s->ctx->stats.sess_accept++;
247 } else if (!s->s3->send_connection_binding &&
248 !(s->options &
249 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
250 /*
251 * Server attempting to renegotiate with client that doesn't
252 * support secure renegotiation.
253 */
254 SSLerr(SSL_F_SSL3_ACCEPT,
255 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
256 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
257 ret = -1;
258 goto end;
259 } else {
260 /*
261 * s->state == SSL_ST_RENEGOTIATE, we will just send a
262 * HelloRequest
263 */
264 s->ctx->stats.sess_accept_renegotiate++;
265 s->state = SSL3_ST_SW_HELLO_REQ_A;
266 }
267 break;
268
269 case SSL3_ST_SW_HELLO_REQ_A:
270 case SSL3_ST_SW_HELLO_REQ_B:
271
272 s->shutdown = 0;
273 ret = ssl3_send_hello_request(s);
274 if (ret <= 0)
275 goto end;
276 s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
277 s->state = SSL3_ST_SW_FLUSH;
278 s->init_num = 0;
279
280 ssl3_init_finished_mac(s);
281 break;
282
283 case SSL3_ST_SW_HELLO_REQ_C:
284 s->state = SSL_ST_OK;
285 break;
286
287 case SSL3_ST_SR_CLNT_HELLO_A:
288 case SSL3_ST_SR_CLNT_HELLO_B:
289 case SSL3_ST_SR_CLNT_HELLO_C:
290
291 s->shutdown = 0;
292 ret = ssl3_get_client_hello(s);
293 if (ret <= 0)
294 goto end;
295 s->new_session = 2;
296 s->state = SSL3_ST_SW_SRVR_HELLO_A;
297 s->init_num = 0;
298 break;
299
300 case SSL3_ST_SW_SRVR_HELLO_A:
301 case SSL3_ST_SW_SRVR_HELLO_B:
302 ret = ssl3_send_server_hello(s);
303 if (ret <= 0)
304 goto end;
305 #ifndef OPENSSL_NO_TLSEXT
306 if (s->hit) {
307 if (s->tlsext_ticket_expected)
308 s->state = SSL3_ST_SW_SESSION_TICKET_A;
309 else
310 s->state = SSL3_ST_SW_CHANGE_A;
311 }
312 #else
313 if (s->hit)
314 s->state = SSL3_ST_SW_CHANGE_A;
315 #endif
316 else
317 s->state = SSL3_ST_SW_CERT_A;
318 s->init_num = 0;
319 break;
320
321 case SSL3_ST_SW_CERT_A:
322 case SSL3_ST_SW_CERT_B:
323 /* Check if it is anon DH or anon ECDH or KRB5 */
324 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)
325 && !(s->s3->tmp.new_cipher->algorithms & SSL_aKRB5)) {
326 ret = ssl3_send_server_certificate(s);
327 if (ret <= 0)
328 goto end;
329 #ifndef OPENSSL_NO_TLSEXT
330 if (s->tlsext_status_expected)
331 s->state = SSL3_ST_SW_CERT_STATUS_A;
332 else
333 s->state = SSL3_ST_SW_KEY_EXCH_A;
334 } else {
335 skip = 1;
336 s->state = SSL3_ST_SW_KEY_EXCH_A;
337 }
338 #else
339 } else
340 skip = 1;
341
342 s->state = SSL3_ST_SW_KEY_EXCH_A;
343 #endif
344 s->init_num = 0;
345 break;
346
347 case SSL3_ST_SW_KEY_EXCH_A:
348 case SSL3_ST_SW_KEY_EXCH_B:
349 l = s->s3->tmp.new_cipher->algorithms;
350
351 /*
352 * clear this, it may get reset by send_server_key_exchange
353 */
354 s->s3->tmp.use_rsa_tmp = 0;
355
356 /*
357 * only send if a DH key exchange, fortezza or RSA but we have a
358 * sign only certificate For ECC ciphersuites, we send a
359 * serverKeyExchange message only if the cipher suite is either
360 * ECDH-anon or ECDHE. In other cases, the server certificate
361 * contains the server's public key for key exchange.
362 */
363 if ((l & SSL_kECDHE)
364 || (l & (SSL_DH | SSL_kFZA))
365 || ((l & SSL_kRSA)
366 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
367 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
368 && EVP_PKEY_size(s->cert->pkeys
369 [SSL_PKEY_RSA_ENC].privatekey) *
370 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
371 )
372 )
373 )
374 ) {
375 ret = ssl3_send_server_key_exchange(s);
376 if (ret <= 0)
377 goto end;
378 } else
379 skip = 1;
380
381 s->state = SSL3_ST_SW_CERT_REQ_A;
382 s->init_num = 0;
383 break;
384
385 case SSL3_ST_SW_CERT_REQ_A:
386 case SSL3_ST_SW_CERT_REQ_B:
387 if ( /* don't request cert unless asked for it: */
388 !(s->verify_mode & SSL_VERIFY_PEER) ||
389 /*
390 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
391 * during re-negotiation:
392 */
393 ((s->session->peer != NULL) &&
394 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
395 /*
396 * never request cert in anonymous ciphersuites (see
397 * section "Certificate request" in SSL 3 drafts and in
398 * RFC 2246):
399 */
400 ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
401 /*
402 * ... except when the application insists on
403 * verification (against the specs, but s3_clnt.c accepts
404 * this for SSL 3)
405 */
406 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
407 /*
408 * never request cert in Kerberos ciphersuites
409 */
410 (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5)) {
411 /* no cert request */
412 skip = 1;
413 s->s3->tmp.cert_request = 0;
414 s->state = SSL3_ST_SW_SRVR_DONE_A;
415 } else {
416 s->s3->tmp.cert_request = 1;
417 ret = ssl3_send_certificate_request(s);
418 if (ret <= 0)
419 goto end;
420 #ifndef NETSCAPE_HANG_BUG
421 s->state = SSL3_ST_SW_SRVR_DONE_A;
422 #else
423 s->state = SSL3_ST_SW_FLUSH;
424 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
425 #endif
426 s->init_num = 0;
427 }
428 break;
429
430 case SSL3_ST_SW_SRVR_DONE_A:
431 case SSL3_ST_SW_SRVR_DONE_B:
432 ret = ssl3_send_server_done(s);
433 if (ret <= 0)
434 goto end;
435 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
436 s->state = SSL3_ST_SW_FLUSH;
437 s->init_num = 0;
438 break;
439
440 case SSL3_ST_SW_FLUSH:
441
442 /*
443 * This code originally checked to see if any data was pending
444 * using BIO_CTRL_INFO and then flushed. This caused problems as
445 * documented in PR#1939. The proposed fix doesn't completely
446 * resolve this issue as buggy implementations of
447 * BIO_CTRL_PENDING still exist. So instead we just flush
448 * unconditionally.
449 */
450
451 s->rwstate = SSL_WRITING;
452 if (BIO_flush(s->wbio) <= 0) {
453 ret = -1;
454 goto end;
455 }
456 s->rwstate = SSL_NOTHING;
457
458 s->state = s->s3->tmp.next_state;
459 break;
460
461 case SSL3_ST_SR_CERT_A:
462 case SSL3_ST_SR_CERT_B:
463 /* Check for second client hello (MS SGC) */
464 ret = ssl3_check_client_hello(s);
465 if (ret <= 0)
466 goto end;
467 if (ret == 2)
468 s->state = SSL3_ST_SR_CLNT_HELLO_C;
469 else {
470 if (s->s3->tmp.cert_request) {
471 ret = ssl3_get_client_certificate(s);
472 if (ret <= 0)
473 goto end;
474 }
475 s->init_num = 0;
476 s->state = SSL3_ST_SR_KEY_EXCH_A;
477 }
478 break;
479
480 case SSL3_ST_SR_KEY_EXCH_A:
481 case SSL3_ST_SR_KEY_EXCH_B:
482 ret = ssl3_get_client_key_exchange(s);
483 if (ret <= 0)
484 goto end;
485 if (ret == 2) {
486 /*
487 * For the ECDH ciphersuites when the client sends its ECDH
488 * pub key in a certificate, the CertificateVerify message is
489 * not sent.
490 */
491 s->state = SSL3_ST_SR_FINISHED_A;
492 s->init_num = 0;
493 } else {
494 s->state = SSL3_ST_SR_CERT_VRFY_A;
495 s->init_num = 0;
496
497 /*
498 * We need to get hashes here so if there is a client cert,
499 * it can be verified
500 */
501 s->method->ssl3_enc->cert_verify_mac(s,
502 &(s->s3->finish_dgst1),
503 &(s->s3->
504 tmp.cert_verify_md
505 [0]));
506 s->method->ssl3_enc->cert_verify_mac(s,
507 &(s->s3->finish_dgst2),
508 &(s->s3->
509 tmp.cert_verify_md
510 [MD5_DIGEST_LENGTH]));
511 }
512 break;
513
514 case SSL3_ST_SR_CERT_VRFY_A:
515 case SSL3_ST_SR_CERT_VRFY_B:
516
517 s->s3->flags |= SSL3_FLAGS_CCS_OK;
518 /* we should decide if we expected this one */
519 ret = ssl3_get_cert_verify(s);
520 if (ret <= 0)
521 goto end;
522
523 s->state = SSL3_ST_SR_FINISHED_A;
524 s->init_num = 0;
525 break;
526
527 case SSL3_ST_SR_FINISHED_A:
528 case SSL3_ST_SR_FINISHED_B:
529 s->s3->flags |= SSL3_FLAGS_CCS_OK;
530 ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
531 SSL3_ST_SR_FINISHED_B);
532 if (ret <= 0)
533 goto end;
534 if (s->hit)
535 s->state = SSL_ST_OK;
536 #ifndef OPENSSL_NO_TLSEXT
537 else if (s->tlsext_ticket_expected)
538 s->state = SSL3_ST_SW_SESSION_TICKET_A;
539 #endif
540 else
541 s->state = SSL3_ST_SW_CHANGE_A;
542 s->init_num = 0;
543 break;
544
545 #ifndef OPENSSL_NO_TLSEXT
546 case SSL3_ST_SW_SESSION_TICKET_A:
547 case SSL3_ST_SW_SESSION_TICKET_B:
548 ret = ssl3_send_newsession_ticket(s);
549 if (ret <= 0)
550 goto end;
551 s->state = SSL3_ST_SW_CHANGE_A;
552 s->init_num = 0;
553 break;
554
555 case SSL3_ST_SW_CERT_STATUS_A:
556 case SSL3_ST_SW_CERT_STATUS_B:
557 ret = ssl3_send_cert_status(s);
558 if (ret <= 0)
559 goto end;
560 s->state = SSL3_ST_SW_KEY_EXCH_A;
561 s->init_num = 0;
562 break;
563
564 #endif
565
566 case SSL3_ST_SW_CHANGE_A:
567 case SSL3_ST_SW_CHANGE_B:
568
569 s->session->cipher = s->s3->tmp.new_cipher;
570 if (!s->method->ssl3_enc->setup_key_block(s)) {
571 ret = -1;
572 goto end;
573 }
574
575 ret = ssl3_send_change_cipher_spec(s,
576 SSL3_ST_SW_CHANGE_A,
577 SSL3_ST_SW_CHANGE_B);
578
579 if (ret <= 0)
580 goto end;
581 s->state = SSL3_ST_SW_FINISHED_A;
582 s->init_num = 0;
583
584 if (!s->method->ssl3_enc->change_cipher_state(s,
585 SSL3_CHANGE_CIPHER_SERVER_WRITE))
586 {
587 ret = -1;
588 goto end;
589 }
590
591 break;
592
593 case SSL3_ST_SW_FINISHED_A:
594 case SSL3_ST_SW_FINISHED_B:
595 ret = ssl3_send_finished(s,
596 SSL3_ST_SW_FINISHED_A,
597 SSL3_ST_SW_FINISHED_B,
598 s->method->
599 ssl3_enc->server_finished_label,
600 s->method->
601 ssl3_enc->server_finished_label_len);
602 if (ret <= 0)
603 goto end;
604 s->state = SSL3_ST_SW_FLUSH;
605 if (s->hit)
606 s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
607 else
608 s->s3->tmp.next_state = SSL_ST_OK;
609 s->init_num = 0;
610 break;
611
612 case SSL_ST_OK:
613 /* clean a few things up */
614 ssl3_cleanup_key_block(s);
615
616 BUF_MEM_free(s->init_buf);
617 s->init_buf = NULL;
618
619 /* remove buffering on output */
620 ssl_free_wbio_buffer(s);
621
622 s->init_num = 0;
623
624 if (s->new_session == 2) { /* skipped if we just sent a
625 * HelloRequest */
626 /*
627 * actually not necessarily a 'new' session unless
628 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set
629 */
630
631 s->new_session = 0;
632
633 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
634
635 s->ctx->stats.sess_accept_good++;
636 /* s->server=1; */
637 s->handshake_func = ssl3_accept;
638
639 if (cb != NULL)
640 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
641 }
642
643 ret = 1;
644 goto end;
645 /* break; */
646
647 default:
648 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNKNOWN_STATE);
649 ret = -1;
650 goto end;
651 /* break; */
652 }
653
654 if (!s->s3->tmp.reuse_message && !skip) {
655 if (s->debug) {
656 if ((ret = BIO_flush(s->wbio)) <= 0)
657 goto end;
658 }
659
660 if ((cb != NULL) && (s->state != state)) {
661 new_state = s->state;
662 s->state = state;
663 cb(s, SSL_CB_ACCEPT_LOOP, 1);
664 s->state = new_state;
665 }
666 }
667 skip = 0;
668 }
669 end:
670 /* BIO_flush(s->wbio); */
671
672 s->in_handshake--;
673 if (cb != NULL)
674 cb(s, SSL_CB_ACCEPT_EXIT, ret);
675 return (ret);
676 }
677
678 int ssl3_send_hello_request(SSL *s)
679 {
680 unsigned char *p;
681
682 if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
683 p = (unsigned char *)s->init_buf->data;
684 *(p++) = SSL3_MT_HELLO_REQUEST;
685 *(p++) = 0;
686 *(p++) = 0;
687 *(p++) = 0;
688
689 s->state = SSL3_ST_SW_HELLO_REQ_B;
690 /* number of bytes to write */
691 s->init_num = 4;
692 s->init_off = 0;
693 }
694
695 /* SSL3_ST_SW_HELLO_REQ_B */
696 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
697 }
698
699 int ssl3_check_client_hello(SSL *s)
700 {
701 int ok;
702 long n;
703
704 /*
705 * this function is called when we really expect a Certificate message,
706 * so permit appropriate message length
707 */
708 n = s->method->ssl_get_message(s,
709 SSL3_ST_SR_CERT_A,
710 SSL3_ST_SR_CERT_B,
711 -1, s->max_cert_list, &ok);
712 if (!ok)
713 return ((int)n);
714 s->s3->tmp.reuse_message = 1;
715 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO) {
716 /*
717 * We only allow the client to restart the handshake once per
718 * negotiation.
719 */
720 if (s->s3->flags & SSL3_FLAGS_SGC_RESTART_DONE) {
721 SSLerr(SSL_F_SSL3_CHECK_CLIENT_HELLO,
722 SSL_R_MULTIPLE_SGC_RESTARTS);
723 return -1;
724 }
725 /*
726 * Throw away what we have done so far in the current handshake,
727 * which will now be aborted. (A full SSL_clear would be too much.)
728 */
729 #ifndef OPENSSL_NO_DH
730 if (s->s3->tmp.dh != NULL) {
731 DH_free(s->s3->tmp.dh);
732 s->s3->tmp.dh = NULL;
733 }
734 #endif
735 #ifndef OPENSSL_NO_ECDH
736 if (s->s3->tmp.ecdh != NULL) {
737 EC_KEY_free(s->s3->tmp.ecdh);
738 s->s3->tmp.ecdh = NULL;
739 }
740 #endif
741 s->s3->flags |= SSL3_FLAGS_SGC_RESTART_DONE;
742 return 2;
743 }
744 return 1;
745 }
746
747 int ssl3_get_client_hello(SSL *s)
748 {
749 int i, j, ok, al, ret = -1;
750 unsigned int cookie_len;
751 long n;
752 unsigned long id;
753 unsigned char *p, *d, *q;
754 SSL_CIPHER *c;
755 #ifndef OPENSSL_NO_COMP
756 SSL_COMP *comp = NULL;
757 #endif
758 STACK_OF(SSL_CIPHER) *ciphers = NULL;
759
760 /*
761 * We do this so that we will respond with our native type. If we are
762 * TLSv1 and we get SSLv3, we will respond with TLSv1, This down
763 * switching should be handled by a different method. If we are SSLv3, we
764 * will respond with SSLv3, even if prompted with TLSv1.
765 */
766 if (s->state == SSL3_ST_SR_CLNT_HELLO_A) {
767 s->state = SSL3_ST_SR_CLNT_HELLO_B;
768 }
769 s->first_packet = 1;
770 n = s->method->ssl_get_message(s,
771 SSL3_ST_SR_CLNT_HELLO_B,
772 SSL3_ST_SR_CLNT_HELLO_C,
773 SSL3_MT_CLIENT_HELLO,
774 SSL3_RT_MAX_PLAIN_LENGTH, &ok);
775
776 if (!ok)
777 return ((int)n);
778 s->first_packet = 0;
779 d = p = (unsigned char *)s->init_msg;
780
781 /*
782 * 2 bytes for client version, SSL3_RANDOM_SIZE bytes for random, 1 byte
783 * for session id length
784 */
785 if (n < 2 + SSL3_RANDOM_SIZE + 1) {
786 al = SSL_AD_DECODE_ERROR;
787 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
788 goto f_err;
789 }
790
791 /*
792 * use version from inside client hello, not from record header (may
793 * differ: see RFC 2246, Appendix E, second paragraph)
794 */
795 s->client_version = (((int)p[0]) << 8) | (int)p[1];
796 p += 2;
797
798 if ((s->version == DTLS1_VERSION && s->client_version > s->version) ||
799 (s->version != DTLS1_VERSION && s->client_version < s->version)) {
800 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
801 if ((s->client_version >> 8) == SSL3_VERSION_MAJOR) {
802 /*
803 * similar to ssl3_get_record, send alert using remote version
804 * number
805 */
806 s->version = s->client_version;
807 }
808 al = SSL_AD_PROTOCOL_VERSION;
809 goto f_err;
810 }
811
812 /*
813 * If we require cookies and this ClientHello doesn't contain one, just
814 * return since we do not want to allocate any memory yet. So check
815 * cookie length...
816 */
817 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
818 unsigned int session_length, cookie_length;
819
820 session_length = *(p + SSL3_RANDOM_SIZE);
821
822 if (p + SSL3_RANDOM_SIZE + session_length + 1 >= d + n) {
823 al = SSL_AD_DECODE_ERROR;
824 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
825 goto f_err;
826 }
827 cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1);
828
829 if (cookie_length == 0)
830 return 1;
831 }
832
833 /* load the client random */
834 memcpy(s->s3->client_random, p, SSL3_RANDOM_SIZE);
835 p += SSL3_RANDOM_SIZE;
836
837 /* get the session-id */
838 j = *(p++);
839
840 if (p + j > d + n) {
841 al = SSL_AD_DECODE_ERROR;
842 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
843 goto f_err;
844 }
845
846 s->hit = 0;
847 /*
848 * Versions before 0.9.7 always allow session reuse during renegotiation
849 * (i.e. when s->new_session is true), option
850 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7. Maybe
851 * this optional behaviour should always have been the default, but we
852 * cannot safely change the default behaviour (or new applications might
853 * be written that become totally unsecure when compiled with an earlier
854 * library version)
855 */
856 if ((s->new_session
857 && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
858 if (!ssl_get_new_session(s, 1))
859 goto err;
860 } else {
861 i = ssl_get_prev_session(s, p, j, d + n);
862 if (i == 1) { /* previous session */
863 s->hit = 1;
864 } else if (i == -1)
865 goto err;
866 else { /* i == 0 */
867
868 if (!ssl_get_new_session(s, 1))
869 goto err;
870 }
871 }
872
873 p += j;
874
875 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) {
876 /* cookie stuff */
877 if (p + 1 > d + n) {
878 al = SSL_AD_DECODE_ERROR;
879 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
880 goto f_err;
881 }
882 cookie_len = *(p++);
883
884 if (p + cookie_len > d + n) {
885 al = SSL_AD_DECODE_ERROR;
886 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
887 goto f_err;
888 }
889
890 /*
891 * The ClientHello may contain a cookie even if the
892 * HelloVerify message has not been sent--make sure that it
893 * does not cause an overflow.
894 */
895 if (cookie_len > sizeof(s->d1->rcvd_cookie)) {
896 /* too much data */
897 al = SSL_AD_DECODE_ERROR;
898 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
899 goto f_err;
900 }
901
902 /* verify the cookie if appropriate option is set. */
903 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && cookie_len > 0) {
904 memcpy(s->d1->rcvd_cookie, p, cookie_len);
905
906 if (s->ctx->app_verify_cookie_cb != NULL) {
907 if (s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
908 cookie_len) == 0) {
909 al = SSL_AD_HANDSHAKE_FAILURE;
910 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
911 SSL_R_COOKIE_MISMATCH);
912 goto f_err;
913 }
914 /* else cookie verification succeeded */
915 }
916 /* default verification */
917 else if (memcmp(s->d1->rcvd_cookie, s->d1->cookie,
918 s->d1->cookie_len) != 0) {
919 al = SSL_AD_HANDSHAKE_FAILURE;
920 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
921 goto f_err;
922 }
923
924 ret = 2;
925 }
926
927 p += cookie_len;
928 }
929
930 if (p + 2 > d + n) {
931 al = SSL_AD_DECODE_ERROR;
932 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
933 goto f_err;
934 }
935 n2s(p, i);
936 if ((i == 0) && (j != 0)) {
937 /* we need a cipher if we are not resuming a session */
938 al = SSL_AD_ILLEGAL_PARAMETER;
939 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
940 goto f_err;
941 }
942
943 /* i bytes of cipher data + 1 byte for compression length later */
944 if ((p + i + 1) > (d + n)) {
945 /* not enough data */
946 al = SSL_AD_DECODE_ERROR;
947 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
948 goto f_err;
949 }
950 if ((i > 0) && (ssl_bytes_to_cipher_list(s, p, i, &(ciphers))
951 == NULL)) {
952 goto err;
953 }
954 p += i;
955
956 /* If it is a hit, check that the cipher is in the list */
957 if ((s->hit) && (i > 0)) {
958 j = 0;
959 id = s->session->cipher->id;
960
961 #ifdef CIPHER_DEBUG
962 printf("client sent %d ciphers\n", sk_num(ciphers));
963 #endif
964 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
965 c = sk_SSL_CIPHER_value(ciphers, i);
966 #ifdef CIPHER_DEBUG
967 printf("client [%2d of %2d]:%s\n",
968 i, sk_num(ciphers), SSL_CIPHER_get_name(c));
969 #endif
970 if (c->id == id) {
971 j = 1;
972 break;
973 }
974 }
975 /*
976 * Disabled because it can be used in a ciphersuite downgrade attack:
977 * CVE-2010-4180.
978 */
979 #if 0
980 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)
981 && (sk_SSL_CIPHER_num(ciphers) == 1)) {
982 /*
983 * Special case as client bug workaround: the previously used
984 * cipher may not be in the current list, the client instead
985 * might be trying to continue using a cipher that before wasn't
986 * chosen due to server preferences. We'll have to reject the
987 * connection if the cipher is not enabled, though.
988 */
989 c = sk_SSL_CIPHER_value(ciphers, 0);
990 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) {
991 s->session->cipher = c;
992 j = 1;
993 }
994 }
995 #endif
996 if (j == 0) {
997 /*
998 * we need to have the cipher in the cipher list if we are asked
999 * to reuse it
1000 */
1001 al = SSL_AD_ILLEGAL_PARAMETER;
1002 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1003 SSL_R_REQUIRED_CIPHER_MISSING);
1004 goto f_err;
1005 }
1006 }
1007
1008 /* compression */
1009 i = *(p++);
1010 if ((p + i) > (d + n)) {
1011 /* not enough data */
1012 al = SSL_AD_DECODE_ERROR;
1013 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1014 goto f_err;
1015 }
1016 q = p;
1017 for (j = 0; j < i; j++) {
1018 if (p[j] == 0)
1019 break;
1020 }
1021
1022 p += i;
1023 if (j >= i) {
1024 /* no compress */
1025 al = SSL_AD_DECODE_ERROR;
1026 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1027 goto f_err;
1028 }
1029 #ifndef OPENSSL_NO_TLSEXT
1030 /* TLS extensions */
1031 if (s->version >= SSL3_VERSION) {
1032 if (!ssl_parse_clienthello_tlsext(s, &p, d, n, &al)) {
1033 /* 'al' set by ssl_parse_clienthello_tlsext */
1034 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1035 goto f_err;
1036 }
1037 }
1038 if (ssl_check_clienthello_tlsext_early(s) <= 0) {
1039 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1040 goto err;
1041 }
1042 #endif
1043 /*
1044 * Worst case, we will use the NULL compression, but if we have other
1045 * options, we will now look for them. We have i-1 compression
1046 * algorithms from the client, starting at q.
1047 */
1048 s->s3->tmp.new_compression = NULL;
1049 #ifndef OPENSSL_NO_COMP
1050 if (s->ctx->comp_methods != NULL) {
1051 /* See if we have a match */
1052 int m, nn, o, v, done = 0;
1053
1054 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1055 for (m = 0; m < nn; m++) {
1056 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1057 v = comp->id;
1058 for (o = 0; o < i; o++) {
1059 if (v == q[o]) {
1060 done = 1;
1061 break;
1062 }
1063 }
1064 if (done)
1065 break;
1066 }
1067 if (done)
1068 s->s3->tmp.new_compression = comp;
1069 else
1070 comp = NULL;
1071 }
1072 #endif
1073
1074 /* TLS does not mind if there is extra stuff */
1075 #if 0
1076 /*
1077 * SSL 3.0 does not mind either, so we should disable this test (was
1078 * enabled in 0.9.6d through 0.9.6j and 0.9.7 through 0.9.7b, in earlier
1079 * SSLeay/OpenSSL releases this test existed but was buggy)
1080 */
1081 if (s->version == SSL3_VERSION) {
1082 if (p < (d + n)) {
1083 /*
1084 * wrong number of bytes, there could be more to follow
1085 */
1086 al = SSL_AD_DECODE_ERROR;
1087 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1088 goto f_err;
1089 }
1090 }
1091 #endif
1092
1093 /*
1094 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1095 */
1096
1097 if (!s->hit) {
1098 #ifdef OPENSSL_NO_COMP
1099 s->session->compress_meth = 0;
1100 #else
1101 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1102 #endif
1103 if (s->session->ciphers != NULL)
1104 sk_SSL_CIPHER_free(s->session->ciphers);
1105 s->session->ciphers = ciphers;
1106 if (ciphers == NULL) {
1107 al = SSL_AD_ILLEGAL_PARAMETER;
1108 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_PASSED);
1109 goto f_err;
1110 }
1111 ciphers = NULL;
1112 c = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1113
1114 if (c == NULL) {
1115 al = SSL_AD_HANDSHAKE_FAILURE;
1116 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1117 goto f_err;
1118 }
1119 s->s3->tmp.new_cipher = c;
1120 } else {
1121 /* Session-id reuse */
1122 #ifdef REUSE_CIPHER_BUG
1123 STACK_OF(SSL_CIPHER) *sk;
1124 SSL_CIPHER *nc = NULL;
1125 SSL_CIPHER *ec = NULL;
1126
1127 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) {
1128 sk = s->session->ciphers;
1129 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1130 c = sk_SSL_CIPHER_value(sk, i);
1131 if (c->algorithms & SSL_eNULL)
1132 nc = c;
1133 if (SSL_C_IS_EXPORT(c))
1134 ec = c;
1135 }
1136 if (nc != NULL)
1137 s->s3->tmp.new_cipher = nc;
1138 else if (ec != NULL)
1139 s->s3->tmp.new_cipher = ec;
1140 else
1141 s->s3->tmp.new_cipher = s->session->cipher;
1142 } else
1143 #endif
1144 s->s3->tmp.new_cipher = s->session->cipher;
1145 }
1146
1147 /*-
1148 * we now have the following setup.
1149 * client_random
1150 * cipher_list - our prefered list of ciphers
1151 * ciphers - the clients prefered list of ciphers
1152 * compression - basically ignored right now
1153 * ssl version is set - sslv3
1154 * s->session - The ssl session has been setup.
1155 * s->hit - session reuse flag
1156 * s->tmp.new_cipher - the new cipher to use.
1157 */
1158
1159 #ifndef OPENSSL_NO_TLSEXT
1160 /* Handles TLS extensions that we couldn't check earlier */
1161 if (s->version >= SSL3_VERSION) {
1162 if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1163 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1164 goto err;
1165 }
1166 }
1167 #endif
1168
1169 if (ret < 0)
1170 ret = 1;
1171 if (0) {
1172 f_err:
1173 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1174 }
1175 err:
1176 if (ciphers != NULL)
1177 sk_SSL_CIPHER_free(ciphers);
1178 return (ret);
1179 }
1180
1181 int ssl3_send_server_hello(SSL *s)
1182 {
1183 unsigned char *buf;
1184 unsigned char *p, *d;
1185 int i, sl;
1186 unsigned long l, Time;
1187
1188 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1189 buf = (unsigned char *)s->init_buf->data;
1190 p = s->s3->server_random;
1191 Time = (unsigned long)time(NULL); /* Time */
1192 l2n(Time, p);
1193 if (RAND_pseudo_bytes(p, SSL3_RANDOM_SIZE - 4) <= 0)
1194 return -1;
1195 /* Do the message type and length last */
1196 d = p = &(buf[4]);
1197
1198 *(p++) = s->version >> 8;
1199 *(p++) = s->version & 0xff;
1200
1201 /* Random stuff */
1202 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1203 p += SSL3_RANDOM_SIZE;
1204
1205 /*
1206 * now in theory we have 3 options to sending back the session id.
1207 * If it is a re-use, we send back the old session-id, if it is a new
1208 * session, we send back the new session-id or we send back a 0
1209 * length session-id if we want it to be single use. Currently I will
1210 * not implement the '0' length session-id 12-Jan-98 - I'll now
1211 * support the '0' length stuff. We also have an additional case
1212 * where stateless session resumption is successful: we always send
1213 * back the old session id. In this case s->hit is non zero: this can
1214 * only happen if stateless session resumption is succesful if session
1215 * caching is disabled so existing functionality is unaffected.
1216 */
1217 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1218 && !s->hit)
1219 s->session->session_id_length = 0;
1220
1221 sl = s->session->session_id_length;
1222 if (sl > (int)sizeof(s->session->session_id)) {
1223 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1224 return -1;
1225 }
1226 *(p++) = sl;
1227 memcpy(p, s->session->session_id, sl);
1228 p += sl;
1229
1230 /* put the cipher */
1231 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1232 p += i;
1233
1234 /* put the compression method */
1235 #ifdef OPENSSL_NO_COMP
1236 *(p++) = 0;
1237 #else
1238 if (s->s3->tmp.new_compression == NULL)
1239 *(p++) = 0;
1240 else
1241 *(p++) = s->s3->tmp.new_compression->id;
1242 #endif
1243 #ifndef OPENSSL_NO_TLSEXT
1244 if ((p =
1245 ssl_add_serverhello_tlsext(s, p,
1246 buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
1247 NULL) {
1248 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1249 return -1;
1250 }
1251 #endif
1252 /* do the header */
1253 l = (p - d);
1254 d = buf;
1255 *(d++) = SSL3_MT_SERVER_HELLO;
1256 l2n3(l, d);
1257
1258 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1259 /* number of bytes to write */
1260 s->init_num = p - buf;
1261 s->init_off = 0;
1262 }
1263
1264 /* SSL3_ST_SW_SRVR_HELLO_B */
1265 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1266 }
1267
1268 int ssl3_send_server_done(SSL *s)
1269 {
1270 unsigned char *p;
1271
1272 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1273 p = (unsigned char *)s->init_buf->data;
1274
1275 /* do the header */
1276 *(p++) = SSL3_MT_SERVER_DONE;
1277 *(p++) = 0;
1278 *(p++) = 0;
1279 *(p++) = 0;
1280
1281 s->state = SSL3_ST_SW_SRVR_DONE_B;
1282 /* number of bytes to write */
1283 s->init_num = 4;
1284 s->init_off = 0;
1285 }
1286
1287 /* SSL3_ST_SW_SRVR_DONE_B */
1288 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1289 }
1290
1291 int ssl3_send_server_key_exchange(SSL *s)
1292 {
1293 #ifndef OPENSSL_NO_RSA
1294 unsigned char *q;
1295 int j, num;
1296 RSA *rsa;
1297 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1298 unsigned int u;
1299 #endif
1300 #ifndef OPENSSL_NO_DH
1301 DH *dh = NULL, *dhp;
1302 #endif
1303 #ifndef OPENSSL_NO_ECDH
1304 EC_KEY *ecdh = NULL, *ecdhp;
1305 unsigned char *encodedPoint = NULL;
1306 int encodedlen = 0;
1307 int curve_id = 0;
1308 BN_CTX *bn_ctx = NULL;
1309 #endif
1310 EVP_PKEY *pkey;
1311 unsigned char *p, *d;
1312 int al, i;
1313 unsigned long type;
1314 int n;
1315 CERT *cert;
1316 BIGNUM *r[4];
1317 int nr[4], kn;
1318 BUF_MEM *buf;
1319 EVP_MD_CTX md_ctx;
1320
1321 EVP_MD_CTX_init(&md_ctx);
1322 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1323 type = s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
1324 cert = s->cert;
1325
1326 buf = s->init_buf;
1327
1328 r[0] = r[1] = r[2] = r[3] = NULL;
1329 n = 0;
1330 #ifndef OPENSSL_NO_RSA
1331 if (type & SSL_kRSA) {
1332 rsa = cert->rsa_tmp;
1333 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1334 rsa = s->cert->rsa_tmp_cb(s,
1335 SSL_C_IS_EXPORT(s->s3->
1336 tmp.new_cipher),
1337 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1338 tmp.new_cipher));
1339 if (rsa == NULL) {
1340 al = SSL_AD_HANDSHAKE_FAILURE;
1341 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1342 SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1343 goto f_err;
1344 }
1345 RSA_up_ref(rsa);
1346 cert->rsa_tmp = rsa;
1347 }
1348 if (rsa == NULL) {
1349 al = SSL_AD_HANDSHAKE_FAILURE;
1350 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1351 SSL_R_MISSING_TMP_RSA_KEY);
1352 goto f_err;
1353 }
1354 r[0] = rsa->n;
1355 r[1] = rsa->e;
1356 s->s3->tmp.use_rsa_tmp = 1;
1357 } else
1358 #endif
1359 #ifndef OPENSSL_NO_DH
1360 if (type & SSL_kEDH) {
1361 dhp = cert->dh_tmp;
1362 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1363 dhp = s->cert->dh_tmp_cb(s,
1364 SSL_C_IS_EXPORT(s->s3->
1365 tmp.new_cipher),
1366 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1367 tmp.new_cipher));
1368 if (dhp == NULL) {
1369 al = SSL_AD_HANDSHAKE_FAILURE;
1370 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1371 SSL_R_MISSING_TMP_DH_KEY);
1372 goto f_err;
1373 }
1374
1375 if (s->s3->tmp.dh != NULL) {
1376 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1377 ERR_R_INTERNAL_ERROR);
1378 goto err;
1379 }
1380
1381 if ((dh = DHparams_dup(dhp)) == NULL) {
1382 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1383 goto err;
1384 }
1385
1386 s->s3->tmp.dh = dh;
1387 if ((dhp->pub_key == NULL ||
1388 dhp->priv_key == NULL ||
1389 (s->options & SSL_OP_SINGLE_DH_USE))) {
1390 if (!DH_generate_key(dh)) {
1391 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1392 goto err;
1393 }
1394 } else {
1395 dh->pub_key = BN_dup(dhp->pub_key);
1396 dh->priv_key = BN_dup(dhp->priv_key);
1397 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1398 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1399 goto err;
1400 }
1401 }
1402 r[0] = dh->p;
1403 r[1] = dh->g;
1404 r[2] = dh->pub_key;
1405 } else
1406 #endif
1407 #ifndef OPENSSL_NO_ECDH
1408 if (type & SSL_kECDHE) {
1409 const EC_GROUP *group;
1410
1411 ecdhp = cert->ecdh_tmp;
1412 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) {
1413 ecdhp = s->cert->ecdh_tmp_cb(s,
1414 SSL_C_IS_EXPORT(s->s3->
1415 tmp.new_cipher),
1416 SSL_C_EXPORT_PKEYLENGTH(s->
1417 s3->tmp.new_cipher));
1418 }
1419 if (ecdhp == NULL) {
1420 al = SSL_AD_HANDSHAKE_FAILURE;
1421 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1422 SSL_R_MISSING_TMP_ECDH_KEY);
1423 goto f_err;
1424 }
1425
1426 if (s->s3->tmp.ecdh != NULL) {
1427 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1428 ERR_R_INTERNAL_ERROR);
1429 goto err;
1430 }
1431
1432 /* Duplicate the ECDH structure. */
1433 if (ecdhp == NULL) {
1434 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1435 goto err;
1436 }
1437 if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1438 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1439 goto err;
1440 }
1441
1442 s->s3->tmp.ecdh = ecdh;
1443 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1444 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1445 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1446 if (!EC_KEY_generate_key(ecdh)) {
1447 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1448 ERR_R_ECDH_LIB);
1449 goto err;
1450 }
1451 }
1452
1453 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1454 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1455 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1456 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1457 goto err;
1458 }
1459
1460 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1461 (EC_GROUP_get_degree(group) > 163)) {
1462 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1463 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1464 goto err;
1465 }
1466
1467 /*
1468 * XXX: For now, we only support ephemeral ECDH keys over named
1469 * (not generic) curves. For supported named curves, curve_id is
1470 * non-zero.
1471 */
1472 if ((curve_id = nid2curve_id(EC_GROUP_get_curve_name(group)))
1473 == 0) {
1474 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1475 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1476 goto err;
1477 }
1478
1479 /*
1480 * Encode the public key. First check the size of encoding and
1481 * allocate memory accordingly.
1482 */
1483 encodedlen = EC_POINT_point2oct(group,
1484 EC_KEY_get0_public_key(ecdh),
1485 POINT_CONVERSION_UNCOMPRESSED,
1486 NULL, 0, NULL);
1487
1488 encodedPoint = (unsigned char *)
1489 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1490 bn_ctx = BN_CTX_new();
1491 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1492 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1493 ERR_R_MALLOC_FAILURE);
1494 goto err;
1495 }
1496
1497 encodedlen = EC_POINT_point2oct(group,
1498 EC_KEY_get0_public_key(ecdh),
1499 POINT_CONVERSION_UNCOMPRESSED,
1500 encodedPoint, encodedlen, bn_ctx);
1501
1502 if (encodedlen == 0) {
1503 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1504 goto err;
1505 }
1506
1507 BN_CTX_free(bn_ctx);
1508 bn_ctx = NULL;
1509
1510 /*
1511 * XXX: For now, we only support named (not generic) curves in
1512 * ECDH ephemeral key exchanges. In this situation, we need four
1513 * additional bytes to encode the entire ServerECDHParams
1514 * structure.
1515 */
1516 n = 4 + encodedlen;
1517
1518 /*
1519 * We'll generate the serverKeyExchange message explicitly so we
1520 * can set these to NULLs
1521 */
1522 r[0] = NULL;
1523 r[1] = NULL;
1524 r[2] = NULL;
1525 r[3] = NULL;
1526 } else
1527 #endif /* !OPENSSL_NO_ECDH */
1528 {
1529 al = SSL_AD_HANDSHAKE_FAILURE;
1530 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1531 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1532 goto f_err;
1533 }
1534 for (i = 0; r[i] != NULL; i++) {
1535 nr[i] = BN_num_bytes(r[i]);
1536 n += 2 + nr[i];
1537 }
1538
1539 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) {
1540 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher))
1541 == NULL) {
1542 al = SSL_AD_DECODE_ERROR;
1543 goto f_err;
1544 }
1545 kn = EVP_PKEY_size(pkey);
1546 } else {
1547 pkey = NULL;
1548 kn = 0;
1549 }
1550
1551 if (!BUF_MEM_grow_clean(buf, n + 4 + kn)) {
1552 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
1553 goto err;
1554 }
1555 d = (unsigned char *)s->init_buf->data;
1556 p = &(d[4]);
1557
1558 for (i = 0; r[i] != NULL; i++) {
1559 s2n(nr[i], p);
1560 BN_bn2bin(r[i], p);
1561 p += nr[i];
1562 }
1563
1564 #ifndef OPENSSL_NO_ECDH
1565 if (type & SSL_kECDHE) {
1566 /*
1567 * XXX: For now, we only support named (not generic) curves. In
1568 * this situation, the serverKeyExchange message has: [1 byte
1569 * CurveType], [2 byte CurveName] [1 byte length of encoded
1570 * point], followed by the actual encoded point itself
1571 */
1572 *p = NAMED_CURVE_TYPE;
1573 p += 1;
1574 *p = 0;
1575 p += 1;
1576 *p = curve_id;
1577 p += 1;
1578 *p = encodedlen;
1579 p += 1;
1580 memcpy((unsigned char *)p,
1581 (unsigned char *)encodedPoint, encodedlen);
1582 OPENSSL_free(encodedPoint);
1583 encodedPoint = NULL;
1584 p += encodedlen;
1585 }
1586 #endif
1587
1588 /* not anonymous */
1589 if (pkey != NULL) {
1590 /*
1591 * n is the length of the params, they start at &(d[4]) and p
1592 * points to the space at the end.
1593 */
1594 #ifndef OPENSSL_NO_RSA
1595 if (pkey->type == EVP_PKEY_RSA) {
1596 q = md_buf;
1597 j = 0;
1598 for (num = 2; num > 0; num--) {
1599 EVP_MD_CTX_set_flags(&md_ctx,
1600 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1601 EVP_DigestInit_ex(&md_ctx, (num == 2)
1602 ? s->ctx->md5 : s->ctx->sha1, NULL);
1603 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1604 SSL3_RANDOM_SIZE);
1605 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1606 SSL3_RANDOM_SIZE);
1607 EVP_DigestUpdate(&md_ctx, &(d[4]), n);
1608 EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
1609 q += i;
1610 j += i;
1611 }
1612 if (RSA_sign(NID_md5_sha1, md_buf, j,
1613 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
1614 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
1615 goto err;
1616 }
1617 s2n(u, p);
1618 n += u + 2;
1619 } else
1620 #endif
1621 #if !defined(OPENSSL_NO_DSA)
1622 if (pkey->type == EVP_PKEY_DSA) {
1623 /* lets do DSS */
1624 EVP_SignInit_ex(&md_ctx, EVP_dss1(), NULL);
1625 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1626 SSL3_RANDOM_SIZE);
1627 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1628 SSL3_RANDOM_SIZE);
1629 EVP_SignUpdate(&md_ctx, &(d[4]), n);
1630 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1631 (unsigned int *)&i, pkey)) {
1632 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_DSA);
1633 goto err;
1634 }
1635 s2n(i, p);
1636 n += i + 2;
1637 } else
1638 #endif
1639 #if !defined(OPENSSL_NO_ECDSA)
1640 if (pkey->type == EVP_PKEY_EC) {
1641 /* let's do ECDSA */
1642 EVP_SignInit_ex(&md_ctx, EVP_ecdsa(), NULL);
1643 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1644 SSL3_RANDOM_SIZE);
1645 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1646 SSL3_RANDOM_SIZE);
1647 EVP_SignUpdate(&md_ctx, &(d[4]), n);
1648 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1649 (unsigned int *)&i, pkey)) {
1650 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1651 ERR_LIB_ECDSA);
1652 goto err;
1653 }
1654 s2n(i, p);
1655 n += i + 2;
1656 } else
1657 #endif
1658 {
1659 /* Is this error check actually needed? */
1660 al = SSL_AD_HANDSHAKE_FAILURE;
1661 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1662 SSL_R_UNKNOWN_PKEY_TYPE);
1663 goto f_err;
1664 }
1665 }
1666
1667 *(d++) = SSL3_MT_SERVER_KEY_EXCHANGE;
1668 l2n3(n, d);
1669
1670 /*
1671 * we should now have things packed up, so lets send it off
1672 */
1673 s->init_num = n + 4;
1674 s->init_off = 0;
1675 }
1676
1677 s->state = SSL3_ST_SW_KEY_EXCH_B;
1678 EVP_MD_CTX_cleanup(&md_ctx);
1679 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1680 f_err:
1681 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1682 err:
1683 #ifndef OPENSSL_NO_ECDH
1684 if (encodedPoint != NULL)
1685 OPENSSL_free(encodedPoint);
1686 BN_CTX_free(bn_ctx);
1687 #endif
1688 EVP_MD_CTX_cleanup(&md_ctx);
1689 return (-1);
1690 }
1691
1692 int ssl3_send_certificate_request(SSL *s)
1693 {
1694 unsigned char *p, *d;
1695 int i, j, nl, off, n;
1696 STACK_OF(X509_NAME) *sk = NULL;
1697 X509_NAME *name;
1698 BUF_MEM *buf;
1699
1700 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
1701 buf = s->init_buf;
1702
1703 d = p = (unsigned char *)&(buf->data[4]);
1704
1705 /* get the list of acceptable cert types */
1706 p++;
1707 n = ssl3_get_req_cert_type(s, p);
1708 d[0] = n;
1709 p += n;
1710 n++;
1711
1712 off = n;
1713 p += 2;
1714 n += 2;
1715
1716 sk = SSL_get_client_CA_list(s);
1717 nl = 0;
1718 if (sk != NULL) {
1719 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
1720 name = sk_X509_NAME_value(sk, i);
1721 j = i2d_X509_NAME(name, NULL);
1722 if (!BUF_MEM_grow_clean(buf, 4 + n + j + 2)) {
1723 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,
1724 ERR_R_BUF_LIB);
1725 goto err;
1726 }
1727 p = (unsigned char *)&(buf->data[4 + n]);
1728 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) {
1729 s2n(j, p);
1730 i2d_X509_NAME(name, &p);
1731 n += 2 + j;
1732 nl += 2 + j;
1733 } else {
1734 d = p;
1735 i2d_X509_NAME(name, &p);
1736 j -= 2;
1737 s2n(j, d);
1738 j += 2;
1739 n += j;
1740 nl += j;
1741 }
1742 }
1743 }
1744 /* else no CA names */
1745 p = (unsigned char *)&(buf->data[4 + off]);
1746 s2n(nl, p);
1747
1748 d = (unsigned char *)buf->data;
1749 *(d++) = SSL3_MT_CERTIFICATE_REQUEST;
1750 l2n3(n, d);
1751
1752 /*
1753 * we should now have things packed up, so lets send it off
1754 */
1755
1756 s->init_num = n + 4;
1757 s->init_off = 0;
1758 #ifdef NETSCAPE_HANG_BUG
1759 if (!BUF_MEM_grow_clean(buf, s->init_num + 4)) {
1760 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, ERR_R_BUF_LIB);
1761 goto err;
1762 }
1763 p = (unsigned char *)s->init_buf->data + s->init_num;
1764
1765 /* do the header */
1766 *(p++) = SSL3_MT_SERVER_DONE;
1767 *(p++) = 0;
1768 *(p++) = 0;
1769 *(p++) = 0;
1770 s->init_num += 4;
1771 #endif
1772
1773 s->state = SSL3_ST_SW_CERT_REQ_B;
1774 }
1775
1776 /* SSL3_ST_SW_CERT_REQ_B */
1777 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1778 err:
1779 return (-1);
1780 }
1781
1782 int ssl3_get_client_key_exchange(SSL *s)
1783 {
1784 int i, al, ok;
1785 long n;
1786 unsigned long l;
1787 unsigned char *p;
1788 #ifndef OPENSSL_NO_RSA
1789 RSA *rsa = NULL;
1790 EVP_PKEY *pkey = NULL;
1791 #endif
1792 #ifndef OPENSSL_NO_DH
1793 BIGNUM *pub = NULL;
1794 DH *dh_srvr;
1795 #endif
1796 #ifndef OPENSSL_NO_KRB5
1797 KSSL_ERR kssl_err;
1798 #endif /* OPENSSL_NO_KRB5 */
1799
1800 #ifndef OPENSSL_NO_ECDH
1801 EC_KEY *srvr_ecdh = NULL;
1802 EVP_PKEY *clnt_pub_pkey = NULL;
1803 EC_POINT *clnt_ecpoint = NULL;
1804 BN_CTX *bn_ctx = NULL;
1805 #endif
1806
1807 n = s->method->ssl_get_message(s,
1808 SSL3_ST_SR_KEY_EXCH_A,
1809 SSL3_ST_SR_KEY_EXCH_B,
1810 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok);
1811
1812 if (!ok)
1813 return ((int)n);
1814 p = (unsigned char *)s->init_msg;
1815
1816 l = s->s3->tmp.new_cipher->algorithms;
1817
1818 #ifndef OPENSSL_NO_RSA
1819 if (l & SSL_kRSA) {
1820 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
1821 int decrypt_len;
1822 unsigned char decrypt_good, version_good;
1823
1824 /* FIX THIS UP EAY EAY EAY EAY */
1825 if (s->s3->tmp.use_rsa_tmp) {
1826 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1827 rsa = s->cert->rsa_tmp;
1828 /*
1829 * Don't do a callback because rsa_tmp should be sent already
1830 */
1831 if (rsa == NULL) {
1832 al = SSL_AD_HANDSHAKE_FAILURE;
1833 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1834 SSL_R_MISSING_TMP_RSA_PKEY);
1835 goto f_err;
1836
1837 }
1838 } else {
1839 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1840 if ((pkey == NULL) ||
1841 (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) {
1842 al = SSL_AD_HANDSHAKE_FAILURE;
1843 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1844 SSL_R_MISSING_RSA_CERTIFICATE);
1845 goto f_err;
1846 }
1847 rsa = pkey->pkey.rsa;
1848 }
1849
1850 /* TLS and [incidentally] DTLS, including pre-0.9.8f */
1851 if (s->version > SSL3_VERSION && s->client_version != DTLS1_BAD_VER) {
1852 n2s(p, i);
1853 if (n != i + 2) {
1854 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
1855 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1856 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1857 goto err;
1858 } else
1859 p -= 2;
1860 } else
1861 n = i;
1862 }
1863
1864 /*
1865 * We must not leak whether a decryption failure occurs because of
1866 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
1867 * section 7.4.7.1). The code follows that advice of the TLS RFC and
1868 * generates a random premaster secret for the case that the decrypt
1869 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
1870 */
1871
1872 /*
1873 * should be RAND_bytes, but we cannot work around a failure.
1874 */
1875 if (RAND_pseudo_bytes(rand_premaster_secret,
1876 sizeof(rand_premaster_secret)) <= 0)
1877 goto err;
1878 decrypt_len =
1879 RSA_private_decrypt((int)n, p, p, rsa, RSA_PKCS1_PADDING);
1880 ERR_clear_error();
1881
1882 /*
1883 * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
1884 * be 0xff if so and zero otherwise.
1885 */
1886 decrypt_good =
1887 constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
1888
1889 /*
1890 * If the version in the decrypted pre-master secret is correct then
1891 * version_good will be 0xff, otherwise it'll be zero. The
1892 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1893 * (http://eprint.iacr.org/2003/052/) exploits the version number
1894 * check as a "bad version oracle". Thus version checks are done in
1895 * constant time and are treated like any other decryption error.
1896 */
1897 version_good =
1898 constant_time_eq_8(p[0], (unsigned)(s->client_version >> 8));
1899 version_good &=
1900 constant_time_eq_8(p[1], (unsigned)(s->client_version & 0xff));
1901
1902 /*
1903 * The premaster secret must contain the same version number as the
1904 * ClientHello to detect version rollback attacks (strangely, the
1905 * protocol does not offer such protection for DH ciphersuites).
1906 * However, buggy clients exist that send the negotiated protocol
1907 * version instead if the server does not support the requested
1908 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
1909 * clients.
1910 */
1911 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
1912 unsigned char workaround_good;
1913 workaround_good =
1914 constant_time_eq_8(p[0], (unsigned)(s->version >> 8));
1915 workaround_good &=
1916 constant_time_eq_8(p[1], (unsigned)(s->version & 0xff));
1917 version_good |= workaround_good;
1918 }
1919
1920 /*
1921 * Both decryption and version must be good for decrypt_good to
1922 * remain non-zero (0xff).
1923 */
1924 decrypt_good &= version_good;
1925
1926 /*
1927 * Now copy rand_premaster_secret over p using decrypt_good_mask.
1928 */
1929 for (i = 0; i < (int)sizeof(rand_premaster_secret); i++) {
1930 p[i] = constant_time_select_8(decrypt_good, p[i],
1931 rand_premaster_secret[i]);
1932 }
1933
1934 s->session->master_key_length =
1935 s->method->ssl3_enc->generate_master_secret(s,
1936 s->
1937 session->master_key,
1938 p, i);
1939 OPENSSL_cleanse(p, i);
1940 } else
1941 #endif
1942 #ifndef OPENSSL_NO_DH
1943 if (l & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
1944 n2s(p, i);
1945 if (n != i + 2) {
1946 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) {
1947 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1948 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1949 goto err;
1950 } else {
1951 p -= 2;
1952 i = (int)n;
1953 }
1954 }
1955
1956 if (n == 0L) { /* the parameters are in the cert */
1957 al = SSL_AD_HANDSHAKE_FAILURE;
1958 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1959 SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1960 goto f_err;
1961 } else {
1962 if (s->s3->tmp.dh == NULL) {
1963 al = SSL_AD_HANDSHAKE_FAILURE;
1964 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1965 SSL_R_MISSING_TMP_DH_KEY);
1966 goto f_err;
1967 } else
1968 dh_srvr = s->s3->tmp.dh;
1969 }
1970
1971 pub = BN_bin2bn(p, i, NULL);
1972 if (pub == NULL) {
1973 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
1974 goto err;
1975 }
1976
1977 i = DH_compute_key(p, pub, dh_srvr);
1978
1979 if (i <= 0) {
1980 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1981 BN_clear_free(pub);
1982 goto err;
1983 }
1984
1985 DH_free(s->s3->tmp.dh);
1986 s->s3->tmp.dh = NULL;
1987
1988 BN_clear_free(pub);
1989 pub = NULL;
1990 s->session->master_key_length =
1991 s->method->ssl3_enc->generate_master_secret(s,
1992 s->
1993 session->master_key,
1994 p, i);
1995 OPENSSL_cleanse(p, i);
1996 } else
1997 #endif
1998 #ifndef OPENSSL_NO_KRB5
1999 if (l & SSL_kKRB5) {
2000 krb5_error_code krb5rc;
2001 krb5_data enc_ticket;
2002 krb5_data authenticator;
2003 krb5_data enc_pms;
2004 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2005 EVP_CIPHER_CTX ciph_ctx;
2006 EVP_CIPHER *enc = NULL;
2007 unsigned char iv[EVP_MAX_IV_LENGTH];
2008 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_BLOCK_LENGTH];
2009 int padl, outl;
2010 krb5_timestamp authtime = 0;
2011 krb5_ticket_times ttimes;
2012
2013 EVP_CIPHER_CTX_init(&ciph_ctx);
2014
2015 if (!kssl_ctx)
2016 kssl_ctx = kssl_ctx_new();
2017
2018 n2s(p, i);
2019 enc_ticket.length = i;
2020
2021 if (n < (int)enc_ticket.length + 6) {
2022 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2023 SSL_R_DATA_LENGTH_TOO_LONG);
2024 goto err;
2025 }
2026
2027 enc_ticket.data = (char *)p;
2028 p += enc_ticket.length;
2029
2030 n2s(p, i);
2031 authenticator.length = i;
2032
2033 if (n < (int)(enc_ticket.length + authenticator.length) + 6) {
2034 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2035 SSL_R_DATA_LENGTH_TOO_LONG);
2036 goto err;
2037 }
2038
2039 authenticator.data = (char *)p;
2040 p += authenticator.length;
2041
2042 n2s(p, i);
2043 enc_pms.length = i;
2044 enc_pms.data = (char *)p;
2045 p += enc_pms.length;
2046
2047 /*
2048 * Note that the length is checked again below, ** after decryption
2049 */
2050 if (enc_pms.length > sizeof pms) {
2051 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2052 SSL_R_DATA_LENGTH_TOO_LONG);
2053 goto err;
2054 }
2055
2056 if (n != (long)(enc_ticket.length + authenticator.length +
2057 enc_pms.length + 6)) {
2058 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2059 SSL_R_DATA_LENGTH_TOO_LONG);
2060 goto err;
2061 }
2062
2063 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
2064 &kssl_err)) != 0) {
2065 # ifdef KSSL_DEBUG
2066 printf("kssl_sget_tkt rtn %d [%d]\n", krb5rc, kssl_err.reason);
2067 if (kssl_err.text)
2068 printf("kssl_err text= %s\n", kssl_err.text);
2069 # endif /* KSSL_DEBUG */
2070 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2071 goto err;
2072 }
2073
2074 /*
2075 * Note: no authenticator is not considered an error, ** but will
2076 * return authtime == 0.
2077 */
2078 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
2079 &authtime, &kssl_err)) != 0) {
2080 # ifdef KSSL_DEBUG
2081 printf("kssl_check_authent rtn %d [%d]\n",
2082 krb5rc, kssl_err.reason);
2083 if (kssl_err.text)
2084 printf("kssl_err text= %s\n", kssl_err.text);
2085 # endif /* KSSL_DEBUG */
2086 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2087 goto err;
2088 }
2089
2090 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0) {
2091 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
2092 goto err;
2093 }
2094 # ifdef KSSL_DEBUG
2095 kssl_ctx_show(kssl_ctx);
2096 # endif /* KSSL_DEBUG */
2097
2098 enc = kssl_map_enc(kssl_ctx->enctype);
2099 if (enc == NULL)
2100 goto err;
2101
2102 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2103
2104 if (!EVP_DecryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv)) {
2105 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2106 SSL_R_DECRYPTION_FAILED);
2107 goto err;
2108 }
2109 if (!EVP_DecryptUpdate(&ciph_ctx, pms, &outl,
2110 (unsigned char *)enc_pms.data, enc_pms.length))
2111 {
2112 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2113 SSL_R_DECRYPTION_FAILED);
2114 goto err;
2115 }
2116 if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2117 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2118 SSL_R_DATA_LENGTH_TOO_LONG);
2119 goto err;
2120 }
2121 if (!EVP_DecryptFinal_ex(&ciph_ctx, &(pms[outl]), &padl)) {
2122 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2123 SSL_R_DECRYPTION_FAILED);
2124 goto err;
2125 }
2126 outl += padl;
2127 if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2128 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2129 SSL_R_DATA_LENGTH_TOO_LONG);
2130 goto err;
2131 }
2132 if (!((pms[0] == (s->client_version >> 8))
2133 && (pms[1] == (s->client_version & 0xff)))) {
2134 /*
2135 * The premaster secret must contain the same version number as
2136 * the ClientHello to detect version rollback attacks (strangely,
2137 * the protocol does not offer such protection for DH
2138 * ciphersuites). However, buggy clients exist that send random
2139 * bytes instead of the protocol version. If
2140 * SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
2141 * (Perhaps we should have a separate BUG value for the Kerberos
2142 * cipher)
2143 */
2144 if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG)) {
2145 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2146 SSL_AD_DECODE_ERROR);
2147 goto err;
2148 }
2149 }
2150
2151 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2152
2153 s->session->master_key_length =
2154 s->method->ssl3_enc->generate_master_secret(s,
2155 s->
2156 session->master_key,
2157 pms, outl);
2158
2159 if (kssl_ctx->client_princ) {
2160 size_t len = strlen(kssl_ctx->client_princ);
2161 if (len < SSL_MAX_KRB5_PRINCIPAL_LENGTH) {
2162 s->session->krb5_client_princ_len = len;
2163 memcpy(s->session->krb5_client_princ, kssl_ctx->client_princ,
2164 len);
2165 }
2166 }
2167
2168 /*- Was doing kssl_ctx_free() here,
2169 * but it caused problems for apache.
2170 * kssl_ctx = kssl_ctx_free(kssl_ctx);
2171 * if (s->kssl_ctx) s->kssl_ctx = NULL;
2172 */
2173 } else
2174 #endif /* OPENSSL_NO_KRB5 */
2175
2176 #ifndef OPENSSL_NO_ECDH
2177 if ((l & SSL_kECDH) || (l & SSL_kECDHE)) {
2178 int ret = 1;
2179 int field_size = 0;
2180 const EC_KEY *tkey;
2181 const EC_GROUP *group;
2182 const BIGNUM *priv_key;
2183
2184 /* initialize structures for server's ECDH key pair */
2185 if ((srvr_ecdh = EC_KEY_new()) == NULL) {
2186 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2187 goto err;
2188 }
2189
2190 /* Let's get server private key and group information */
2191 if (l & SSL_kECDH) {
2192 /* use the certificate */
2193 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2194 } else {
2195 /*
2196 * use the ephermeral values we saved when generating the
2197 * ServerKeyExchange msg.
2198 */
2199 tkey = s->s3->tmp.ecdh;
2200 }
2201
2202 group = EC_KEY_get0_group(tkey);
2203 priv_key = EC_KEY_get0_private_key(tkey);
2204
2205 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2206 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
2207 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2208 goto err;
2209 }
2210
2211 /* Let's get client's public key */
2212 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
2213 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2214 goto err;
2215 }
2216
2217 if (n == 0L) {
2218 /* Client Publickey was in Client Certificate */
2219
2220 if (l & SSL_kECDHE) {
2221 al = SSL_AD_HANDSHAKE_FAILURE;
2222 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2223 SSL_R_MISSING_TMP_ECDH_KEY);
2224 goto f_err;
2225 }
2226 if (((clnt_pub_pkey = X509_get_pubkey(s->session->peer))
2227 == NULL) || (clnt_pub_pkey->type != EVP_PKEY_EC)) {
2228 /*
2229 * XXX: For now, we do not support client authentication
2230 * using ECDH certificates so this branch (n == 0L) of the
2231 * code is never executed. When that support is added, we
2232 * ought to ensure the key received in the certificate is
2233 * authorized for key agreement. ECDH_compute_key implicitly
2234 * checks that the two ECDH shares are for the same group.
2235 */
2236 al = SSL_AD_HANDSHAKE_FAILURE;
2237 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2238 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2239 goto f_err;
2240 }
2241
2242 if (EC_POINT_copy(clnt_ecpoint,
2243 EC_KEY_get0_public_key(clnt_pub_pkey->
2244 pkey.ec)) == 0) {
2245 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2246 goto err;
2247 }
2248 ret = 2; /* Skip certificate verify processing */
2249 } else {
2250 /*
2251 * Get client's public key from encoded point in the
2252 * ClientKeyExchange message.
2253 */
2254 if ((bn_ctx = BN_CTX_new()) == NULL) {
2255 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2256 ERR_R_MALLOC_FAILURE);
2257 goto err;
2258 }
2259
2260 /* Get encoded point length */
2261 i = *p;
2262 p += 1;
2263 if (n != 1 + i) {
2264 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2265 goto err;
2266 }
2267 if (EC_POINT_oct2point(group, clnt_ecpoint, p, i, bn_ctx) == 0) {
2268 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2269 goto err;
2270 }
2271 /*
2272 * p is pointing to somewhere in the buffer currently, so set it
2273 * to the start
2274 */
2275 p = (unsigned char *)s->init_buf->data;
2276 }
2277
2278 /* Compute the shared pre-master secret */
2279 field_size = EC_GROUP_get_degree(group);
2280 if (field_size <= 0) {
2281 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2282 goto err;
2283 }
2284 i = ECDH_compute_key(p, (field_size + 7) / 8, clnt_ecpoint, srvr_ecdh,
2285 NULL);
2286 if (i <= 0) {
2287 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2288 goto err;
2289 }
2290
2291 EVP_PKEY_free(clnt_pub_pkey);
2292 EC_POINT_free(clnt_ecpoint);
2293 if (srvr_ecdh != NULL)
2294 EC_KEY_free(srvr_ecdh);
2295 BN_CTX_free(bn_ctx);
2296
2297 /* Compute the master secret */
2298 s->session->master_key_length =
2299 s->method->ssl3_enc->generate_master_secret(s,
2300 s->
2301 session->master_key,
2302 p, i);
2303
2304 OPENSSL_cleanse(p, i);
2305 return (ret);
2306 } else
2307 #endif
2308 {
2309 al = SSL_AD_HANDSHAKE_FAILURE;
2310 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
2311 goto f_err;
2312 }
2313
2314 return (1);
2315 f_err:
2316 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2317 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
2318 err:
2319 #endif
2320 #ifndef OPENSSL_NO_ECDH
2321 EVP_PKEY_free(clnt_pub_pkey);
2322 EC_POINT_free(clnt_ecpoint);
2323 if (srvr_ecdh != NULL)
2324 EC_KEY_free(srvr_ecdh);
2325 BN_CTX_free(bn_ctx);
2326 #endif
2327 return (-1);
2328 }
2329
2330 int ssl3_get_cert_verify(SSL *s)
2331 {
2332 EVP_PKEY *pkey = NULL;
2333 unsigned char *p;
2334 int al, ok, ret = 0;
2335 long n;
2336 int type = 0, i, j;
2337 X509 *peer;
2338
2339 n = s->method->ssl_get_message(s,
2340 SSL3_ST_SR_CERT_VRFY_A,
2341 SSL3_ST_SR_CERT_VRFY_B,
2342 -1, SSL3_RT_MAX_PLAIN_LENGTH, &ok);
2343
2344 if (!ok)
2345 return ((int)n);
2346
2347 if (s->session->peer != NULL) {
2348 peer = s->session->peer;
2349 pkey = X509_get_pubkey(peer);
2350 type = X509_certificate_type(peer, pkey);
2351 } else {
2352 peer = NULL;
2353 pkey = NULL;
2354 }
2355
2356 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) {
2357 s->s3->tmp.reuse_message = 1;
2358 if (peer != NULL) {
2359 al = SSL_AD_UNEXPECTED_MESSAGE;
2360 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_MISSING_VERIFY_MESSAGE);
2361 goto f_err;
2362 }
2363 ret = 1;
2364 goto end;
2365 }
2366
2367 if (peer == NULL) {
2368 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_NO_CLIENT_CERT_RECEIVED);
2369 al = SSL_AD_UNEXPECTED_MESSAGE;
2370 goto f_err;
2371 }
2372
2373 if (!(type & EVP_PKT_SIGN)) {
2374 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2375 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2376 al = SSL_AD_ILLEGAL_PARAMETER;
2377 goto f_err;
2378 }
2379
2380 if (s->s3->change_cipher_spec) {
2381 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_CCS_RECEIVED_EARLY);
2382 al = SSL_AD_UNEXPECTED_MESSAGE;
2383 goto f_err;
2384 }
2385
2386 /* we now have a signature that we need to verify */
2387 p = (unsigned char *)s->init_msg;
2388 n2s(p, i);
2389 n -= 2;
2390 if (i > n) {
2391 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
2392 al = SSL_AD_DECODE_ERROR;
2393 goto f_err;
2394 }
2395
2396 j = EVP_PKEY_size(pkey);
2397 if ((i > j) || (n > j) || (n <= 0)) {
2398 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
2399 al = SSL_AD_DECODE_ERROR;
2400 goto f_err;
2401 }
2402 #ifndef OPENSSL_NO_RSA
2403 if (pkey->type == EVP_PKEY_RSA) {
2404 i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
2405 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i,
2406 pkey->pkey.rsa);
2407 if (i < 0) {
2408 al = SSL_AD_DECRYPT_ERROR;
2409 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_DECRYPT);
2410 goto f_err;
2411 }
2412 if (i == 0) {
2413 al = SSL_AD_DECRYPT_ERROR;
2414 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_SIGNATURE);
2415 goto f_err;
2416 }
2417 } else
2418 #endif
2419 #ifndef OPENSSL_NO_DSA
2420 if (pkey->type == EVP_PKEY_DSA) {
2421 j = DSA_verify(pkey->save_type,
2422 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2423 SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa);
2424 if (j <= 0) {
2425 /* bad signature */
2426 al = SSL_AD_DECRYPT_ERROR;
2427 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_DSA_SIGNATURE);
2428 goto f_err;
2429 }
2430 } else
2431 #endif
2432 #ifndef OPENSSL_NO_ECDSA
2433 if (pkey->type == EVP_PKEY_EC) {
2434 j = ECDSA_verify(pkey->save_type,
2435 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2436 SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec);
2437 if (j <= 0) {
2438 /* bad signature */
2439 al = SSL_AD_DECRYPT_ERROR;
2440 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
2441 goto f_err;
2442 }
2443 } else
2444 #endif
2445 {
2446 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
2447 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
2448 goto f_err;
2449 }
2450
2451 ret = 1;
2452 if (0) {
2453 f_err:
2454 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2455 }
2456 end:
2457 EVP_PKEY_free(pkey);
2458 return (ret);
2459 }
2460
2461 int ssl3_get_client_certificate(SSL *s)
2462 {
2463 int i, ok, al, ret = -1;
2464 X509 *x = NULL;
2465 unsigned long l, nc, llen, n;
2466 const unsigned char *p, *q;
2467 unsigned char *d;
2468 STACK_OF(X509) *sk = NULL;
2469
2470 n = s->method->ssl_get_message(s,
2471 SSL3_ST_SR_CERT_A,
2472 SSL3_ST_SR_CERT_B,
2473 -1, s->max_cert_list, &ok);
2474
2475 if (!ok)
2476 return ((int)n);
2477
2478 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
2479 if ((s->verify_mode & SSL_VERIFY_PEER) &&
2480 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2481 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2482 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2483 al = SSL_AD_HANDSHAKE_FAILURE;
2484 goto f_err;
2485 }
2486 /*
2487 * If tls asked for a client cert, the client must return a 0 list
2488 */
2489 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) {
2490 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2491 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2492 al = SSL_AD_UNEXPECTED_MESSAGE;
2493 goto f_err;
2494 }
2495 s->s3->tmp.reuse_message = 1;
2496 return (1);
2497 }
2498
2499 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
2500 al = SSL_AD_UNEXPECTED_MESSAGE;
2501 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_WRONG_MESSAGE_TYPE);
2502 goto f_err;
2503 }
2504 p = d = (unsigned char *)s->init_msg;
2505
2506 if ((sk = sk_X509_new_null()) == NULL) {
2507 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2508 goto err;
2509 }
2510
2511 n2l3(p, llen);
2512 if (llen + 3 != n) {
2513 al = SSL_AD_DECODE_ERROR;
2514 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
2515 goto f_err;
2516 }
2517 for (nc = 0; nc < llen;) {
2518 n2l3(p, l);
2519 if ((l + nc + 3) > llen) {
2520 al = SSL_AD_DECODE_ERROR;
2521 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2522 SSL_R_CERT_LENGTH_MISMATCH);
2523 goto f_err;
2524 }
2525
2526 q = p;
2527 x = d2i_X509(NULL, &p, l);
2528 if (x == NULL) {
2529 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
2530 goto err;
2531 }
2532 if (p != (q + l)) {
2533 al = SSL_AD_DECODE_ERROR;
2534 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2535 SSL_R_CERT_LENGTH_MISMATCH);
2536 goto f_err;
2537 }
2538 if (!sk_X509_push(sk, x)) {
2539 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2540 goto err;
2541 }
2542 x = NULL;
2543 nc += l + 3;
2544 }
2545
2546 if (sk_X509_num(sk) <= 0) {
2547 /* TLS does not mind 0 certs returned */
2548 if (s->version == SSL3_VERSION) {
2549 al = SSL_AD_HANDSHAKE_FAILURE;
2550 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2551 SSL_R_NO_CERTIFICATES_RETURNED);
2552 goto f_err;
2553 }
2554 /* Fail for TLS only if we required a certificate */
2555 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2556 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
2557 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2558 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2559 al = SSL_AD_HANDSHAKE_FAILURE;
2560 goto f_err;
2561 }
2562 } else {
2563 i = ssl_verify_cert_chain(s, sk);
2564 if (i <= 0) {
2565 al = ssl_verify_alarm_type(s->verify_result);
2566 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
2567 SSL_R_NO_CERTIFICATE_RETURNED);
2568 goto f_err;
2569 }
2570 }
2571
2572 if (s->session->peer != NULL) /* This should not be needed */
2573 X509_free(s->session->peer);
2574 s->session->peer = sk_X509_shift(sk);
2575 s->session->verify_result = s->verify_result;
2576
2577 /*
2578 * With the current implementation, sess_cert will always be NULL when we
2579 * arrive here.
2580 */
2581 if (s->session->sess_cert == NULL) {
2582 s->session->sess_cert = ssl_sess_cert_new();
2583 if (s->session->sess_cert == NULL) {
2584 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2585 goto err;
2586 }
2587 }
2588 if (s->session->sess_cert->cert_chain != NULL)
2589 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2590 s->session->sess_cert->cert_chain = sk;
2591 /*
2592 * Inconsistency alert: cert_chain does *not* include the peer's own
2593 * certificate, while we do include it in s3_clnt.c
2594 */
2595
2596 sk = NULL;
2597
2598 ret = 1;
2599 if (0) {
2600 f_err:
2601 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2602 }
2603 err:
2604 if (x != NULL)
2605 X509_free(x);
2606 if (sk != NULL)
2607 sk_X509_pop_free(sk, X509_free);
2608 return (ret);
2609 }
2610
2611 int ssl3_send_server_certificate(SSL *s)
2612 {
2613 unsigned long l;
2614 X509 *x;
2615
2616 if (s->state == SSL3_ST_SW_CERT_A) {
2617 x = ssl_get_server_send_cert(s);
2618 if (x == NULL &&
2619 /* VRS: allow null cert if auth == KRB5 */
2620 (s->s3->tmp.new_cipher->algorithms
2621 & (SSL_MKEY_MASK | SSL_AUTH_MASK))
2622 != (SSL_aKRB5 | SSL_kKRB5)) {
2623 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
2624 return (0);
2625 }
2626
2627 l = ssl3_output_cert_chain(s, x);
2628 s->state = SSL3_ST_SW_CERT_B;
2629 s->init_num = (int)l;
2630 s->init_off = 0;
2631 }
2632
2633 /* SSL3_ST_SW_CERT_B */
2634 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
2635 }
2636
2637 #ifndef OPENSSL_NO_ECDH
2638 /* This is the complement of curve_id2nid in s3_clnt.c. */
2639 static int nid2curve_id(int nid)
2640 {
2641 /*
2642 * ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001) (no changes
2643 * in draft-ietf-tls-ecc-03.txt [June 2003])
2644 */
2645 switch (nid) {
2646 case NID_sect163k1: /* sect163k1 (1) */
2647 return 1;
2648 case NID_sect163r1: /* sect163r1 (2) */
2649 return 2;
2650 case NID_sect163r2: /* sect163r2 (3) */
2651 return 3;
2652 case NID_sect193r1: /* sect193r1 (4) */
2653 return 4;
2654 case NID_sect193r2: /* sect193r2 (5) */
2655 return 5;
2656 case NID_sect233k1: /* sect233k1 (6) */
2657 return 6;
2658 case NID_sect233r1: /* sect233r1 (7) */
2659 return 7;
2660 case NID_sect239k1: /* sect239k1 (8) */
2661 return 8;
2662 case NID_sect283k1: /* sect283k1 (9) */
2663 return 9;
2664 case NID_sect283r1: /* sect283r1 (10) */
2665 return 10;
2666 case NID_sect409k1: /* sect409k1 (11) */
2667 return 11;
2668 case NID_sect409r1: /* sect409r1 (12) */
2669 return 12;
2670 case NID_sect571k1: /* sect571k1 (13) */
2671 return 13;
2672 case NID_sect571r1: /* sect571r1 (14) */
2673 return 14;
2674 case NID_secp160k1: /* secp160k1 (15) */
2675 return 15;
2676 case NID_secp160r1: /* secp160r1 (16) */
2677 return 16;
2678 case NID_secp160r2: /* secp160r2 (17) */
2679 return 17;
2680 case NID_secp192k1: /* secp192k1 (18) */
2681 return 18;
2682 case NID_X9_62_prime192v1: /* secp192r1 (19) */
2683 return 19;
2684 case NID_secp224k1: /* secp224k1 (20) */
2685 return 20;
2686 case NID_secp224r1: /* secp224r1 (21) */
2687 return 21;
2688 case NID_secp256k1: /* secp256k1 (22) */
2689 return 22;
2690 case NID_X9_62_prime256v1: /* secp256r1 (23) */
2691 return 23;
2692 case NID_secp384r1: /* secp384r1 (24) */
2693 return 24;
2694 case NID_secp521r1: /* secp521r1 (25) */
2695 return 25;
2696 default:
2697 return 0;
2698 }
2699 }
2700 #endif
2701 #ifndef OPENSSL_NO_TLSEXT
2702 int ssl3_send_newsession_ticket(SSL *s)
2703 {
2704 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
2705 unsigned char *p, *senc, *macstart;
2706 int len, slen;
2707 unsigned int hlen;
2708 EVP_CIPHER_CTX ctx;
2709 HMAC_CTX hctx;
2710 SSL_CTX *tctx = s->initial_ctx;
2711 unsigned char iv[EVP_MAX_IV_LENGTH];
2712 unsigned char key_name[16];
2713
2714 /* get session encoding length */
2715 slen = i2d_SSL_SESSION(s->session, NULL);
2716 /*
2717 * Some length values are 16 bits, so forget it if session is too
2718 * long
2719 */
2720 if (slen > 0xFF00)
2721 return -1;
2722 /*-
2723 * Grow buffer if need be: the length calculation is as
2724 * follows 1 (size of message name) + 3 (message length
2725 * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) +
2726 * 16 (key name) + max_iv_len (iv length) +
2727 * session_length + max_enc_block_size (max encrypted session
2728 * length) + max_md_size (HMAC).
2729 */
2730 if (!BUF_MEM_grow(s->init_buf,
2731 26 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH +
2732 EVP_MAX_MD_SIZE + slen))
2733 return -1;
2734 senc = OPENSSL_malloc(slen);
2735 if (!senc)
2736 return -1;
2737 p = senc;
2738 i2d_SSL_SESSION(s->session, &p);
2739
2740 p = (unsigned char *)s->init_buf->data;
2741 /* do the header */
2742 *(p++) = SSL3_MT_NEWSESSION_TICKET;
2743 /* Skip message length for now */
2744 p += 3;
2745 EVP_CIPHER_CTX_init(&ctx);
2746 HMAC_CTX_init(&hctx);
2747 /*
2748 * Initialize HMAC and cipher contexts. If callback present it does
2749 * all the work otherwise use generated values from parent ctx.
2750 */
2751 if (tctx->tlsext_ticket_key_cb) {
2752 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
2753 &hctx, 1) < 0) {
2754 OPENSSL_free(senc);
2755 return -1;
2756 }
2757 } else {
2758 RAND_pseudo_bytes(iv, 16);
2759 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2760 tctx->tlsext_tick_aes_key, iv);
2761 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2762 tlsext_tick_md(), NULL);
2763 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
2764 }
2765 l2n(s->session->tlsext_tick_lifetime_hint, p);
2766 /* Skip ticket length for now */
2767 p += 2;
2768 /* Output key name */
2769 macstart = p;
2770 memcpy(p, key_name, 16);
2771 p += 16;
2772 /* output IV */
2773 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
2774 p += EVP_CIPHER_CTX_iv_length(&ctx);
2775 /* Encrypt session data */
2776 EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
2777 p += len;
2778 EVP_EncryptFinal(&ctx, p, &len);
2779 p += len;
2780 EVP_CIPHER_CTX_cleanup(&ctx);
2781
2782 HMAC_Update(&hctx, macstart, p - macstart);
2783 HMAC_Final(&hctx, p, &hlen);
2784 HMAC_CTX_cleanup(&hctx);
2785
2786 p += hlen;
2787 /* Now write out lengths: p points to end of data written */
2788 /* Total length */
2789 len = p - (unsigned char *)s->init_buf->data;
2790 p = (unsigned char *)s->init_buf->data + 1;
2791 l2n3(len - 4, p); /* Message length */
2792 p += 4;
2793 s2n(len - 10, p); /* Ticket length */
2794
2795 /* number of bytes to write */
2796 s->init_num = len;
2797 s->state = SSL3_ST_SW_SESSION_TICKET_B;
2798 s->init_off = 0;
2799 OPENSSL_free(senc);
2800 }
2801
2802 /* SSL3_ST_SW_SESSION_TICKET_B */
2803 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
2804 }
2805
2806 int ssl3_send_cert_status(SSL *s)
2807 {
2808 if (s->state == SSL3_ST_SW_CERT_STATUS_A) {
2809 unsigned char *p;
2810 /*-
2811 * Grow buffer if need be: the length calculation is as
2812 * follows 1 (message type) + 3 (message length) +
2813 * 1 (ocsp response type) + 3 (ocsp response length)
2814 * + (ocsp response)
2815 */
2816 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen))
2817 return -1;
2818
2819 p = (unsigned char *)s->init_buf->data;
2820
2821 /* do the header */
2822 *(p++) = SSL3_MT_CERTIFICATE_STATUS;
2823 /* message length */
2824 l2n3(s->tlsext_ocsp_resplen + 4, p);
2825 /* status type */
2826 *(p++) = s->tlsext_status_type;
2827 /* length of OCSP response */
2828 l2n3(s->tlsext_ocsp_resplen, p);
2829 /* actual response */
2830 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
2831 /* number of bytes to write */
2832 s->init_num = 8 + s->tlsext_ocsp_resplen;
2833 s->state = SSL3_ST_SW_CERT_STATUS_B;
2834 s->init_off = 0;
2835 }
2836
2837 /* SSL3_ST_SW_CERT_STATUS_B */
2838 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
2839 }
2840 #endif