summaryrefslogtreecommitdiff
path: root/src/lib/libssl/s3_clnt.c
diff options
context:
space:
mode:
authormarkus <>2002-09-05 12:51:50 +0000
committermarkus <>2002-09-05 12:51:50 +0000
commit15b5d84f9da2ce4bfae8580e56e34a859f74ad71 (patch)
treebf939e82d7fd73cc8a01cf6959002209972091bc /src/lib/libssl/s3_clnt.c
parent027351f729b9e837200dae6e1520cda6577ab930 (diff)
downloadopenbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.gz
openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.bz2
openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.zip
import openssl-0.9.7-beta1
Diffstat (limited to 'src/lib/libssl/s3_clnt.c')
-rw-r--r--src/lib/libssl/s3_clnt.c760
1 files changed, 519 insertions, 241 deletions
diff --git a/src/lib/libssl/s3_clnt.c b/src/lib/libssl/s3_clnt.c
index 940c6a458f..e5853ede95 100644
--- a/src/lib/libssl/s3_clnt.c
+++ b/src/lib/libssl/s3_clnt.c
@@ -55,29 +55,74 @@
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58/* ====================================================================
59 * Copyright (c) 1998-2002 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 */
58 111
59#include <stdio.h> 112#include <stdio.h>
60#include "buffer.h" 113#include <openssl/buffer.h>
61#include "rand.h" 114#include <openssl/rand.h>
62#include "objects.h" 115#include <openssl/objects.h>
63#include "evp.h" 116#include <openssl/evp.h>
64#include "ssl_locl.h" 117#include "ssl_locl.h"
118#include "kssl_lcl.h"
119#include <openssl/md5.h>
65 120
66#define BREAK break 121static SSL_METHOD *ssl3_get_client_method(int ver);
67/* SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,ERR_R_MALLOC_FAILURE);
68 * SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
69 * SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,ERR_R_MALLOC_FAILURE);
70 * SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
71 * SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
72 * SSLerr(SSL_F_SSL3_GET_SERVER_DONE,ERR_R_MALLOC_FAILURE);
73SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_SHORT);
74 */
75
76#ifndef NOPROTO
77static int ssl3_client_hello(SSL *s); 122static int ssl3_client_hello(SSL *s);
78static int ssl3_get_server_hello(SSL *s); 123static int ssl3_get_server_hello(SSL *s);
79static int ssl3_get_certificate_request(SSL *s); 124static int ssl3_get_certificate_request(SSL *s);
80static int ca_dn_cmp(X509_NAME **a,X509_NAME **b); 125static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
81static int ssl3_get_server_done(SSL *s); 126static int ssl3_get_server_done(SSL *s);
82static int ssl3_send_client_verify(SSL *s); 127static int ssl3_send_client_verify(SSL *s);
83static int ssl3_send_client_certificate(SSL *s); 128static int ssl3_send_client_certificate(SSL *s);
@@ -85,22 +130,7 @@ static int ssl3_send_client_key_exchange(SSL *s);
85static int ssl3_get_key_exchange(SSL *s); 130static int ssl3_get_key_exchange(SSL *s);
86static int ssl3_get_server_certificate(SSL *s); 131static int ssl3_get_server_certificate(SSL *s);
87static int ssl3_check_cert_and_algorithm(SSL *s); 132static int ssl3_check_cert_and_algorithm(SSL *s);
88#else 133static SSL_METHOD *ssl3_get_client_method(int ver)
89static int ssl3_client_hello();
90static int ssl3_get_server_hello();
91static int ssl3_get_certificate_request();
92static int ca_dn_cmp();
93static int ssl3_get_server_done();
94static int ssl3_send_client_verify();
95static int ssl3_send_client_certificate();
96static int ssl3_send_client_key_exchange();
97static int ssl3_get_key_exchange();
98static int ssl3_get_server_certificate();
99static int ssl3_check_cert_and_algorithm();
100#endif
101
102static SSL_METHOD *ssl3_get_client_method(ver)
103int ver;
104 { 134 {
105 if (ver == SSL3_VERSION) 135 if (ver == SSL3_VERSION)
106 return(SSLv3_client_method()); 136 return(SSLv3_client_method());
@@ -108,7 +138,7 @@ int ver;
108 return(NULL); 138 return(NULL);
109 } 139 }
110 140
111SSL_METHOD *SSLv3_client_method() 141SSL_METHOD *SSLv3_client_method(void)
112 { 142 {
113 static int init=1; 143 static int init=1;
114 static SSL_METHOD SSLv3_client_data; 144 static SSL_METHOD SSLv3_client_data;
@@ -124,18 +154,16 @@ SSL_METHOD *SSLv3_client_method()
124 return(&SSLv3_client_data); 154 return(&SSLv3_client_data);
125 } 155 }
126 156
127int ssl3_connect(s) 157int ssl3_connect(SSL *s)
128SSL *s;
129 { 158 {
130 BUF_MEM *buf; 159 BUF_MEM *buf;
131 unsigned long Time=time(NULL),l; 160 unsigned long Time=time(NULL),l;
132 long num1; 161 long num1;
133 void (*cb)()=NULL; 162 void (*cb)(const SSL *ssl,int type,int val)=NULL;
134 int ret= -1; 163 int ret= -1;
135 BIO *under;
136 int new_state,state,skip=0;; 164 int new_state,state,skip=0;;
137 165
138 RAND_seed((unsigned char *)&Time,sizeof(Time)); 166 RAND_add(&Time,sizeof(Time),0);
139 ERR_clear_error(); 167 ERR_clear_error();
140 clear_sys_error(); 168 clear_sys_error();
141 169
@@ -144,8 +172,8 @@ SSL *s;
144 else if (s->ctx->info_callback != NULL) 172 else if (s->ctx->info_callback != NULL)
145 cb=s->ctx->info_callback; 173 cb=s->ctx->info_callback;
146 174
147 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
148 s->in_handshake++; 175 s->in_handshake++;
176 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
149 177
150 for (;;) 178 for (;;)
151 { 179 {
@@ -156,17 +184,23 @@ SSL *s;
156 case SSL_ST_RENEGOTIATE: 184 case SSL_ST_RENEGOTIATE:
157 s->new_session=1; 185 s->new_session=1;
158 s->state=SSL_ST_CONNECT; 186 s->state=SSL_ST_CONNECT;
159 s->ctx->sess_connect_renegotiate++; 187 s->ctx->stats.sess_connect_renegotiate++;
160 /* break */ 188 /* break */
161 case SSL_ST_BEFORE: 189 case SSL_ST_BEFORE:
162 case SSL_ST_CONNECT: 190 case SSL_ST_CONNECT:
163 case SSL_ST_BEFORE|SSL_ST_CONNECT: 191 case SSL_ST_BEFORE|SSL_ST_CONNECT:
164 case SSL_ST_OK|SSL_ST_CONNECT: 192 case SSL_ST_OK|SSL_ST_CONNECT:
165 193
194 s->server=0;
166 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); 195 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
167 196
168 if ((s->version & 0xff00 ) != 0x0300) 197 if ((s->version & 0xff00 ) != 0x0300)
169 abort(); 198 {
199 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
200 ret = -1;
201 goto end;
202 }
203
170 /* s->version=SSL3_VERSION; */ 204 /* s->version=SSL3_VERSION; */
171 s->type=SSL_ST_CONNECT; 205 s->type=SSL_ST_CONNECT;
172 206
@@ -195,7 +229,7 @@ SSL *s;
195 ssl3_init_finished_mac(s); 229 ssl3_init_finished_mac(s);
196 230
197 s->state=SSL3_ST_CW_CLNT_HELLO_A; 231 s->state=SSL3_ST_CW_CLNT_HELLO_A;
198 s->ctx->sess_connect++; 232 s->ctx->stats.sess_connect++;
199 s->init_num=0; 233 s->init_num=0;
200 break; 234 break;
201 235
@@ -278,6 +312,7 @@ SSL *s;
278 case SSL3_ST_CW_CERT_A: 312 case SSL3_ST_CW_CERT_A:
279 case SSL3_ST_CW_CERT_B: 313 case SSL3_ST_CW_CERT_B:
280 case SSL3_ST_CW_CERT_C: 314 case SSL3_ST_CW_CERT_C:
315 case SSL3_ST_CW_CERT_D:
281 ret=ssl3_send_client_certificate(s); 316 ret=ssl3_send_client_certificate(s);
282 if (ret <= 0) goto end; 317 if (ret <= 0) goto end;
283 s->state=SSL3_ST_CW_KEY_EXCH_A; 318 s->state=SSL3_ST_CW_KEY_EXCH_A;
@@ -324,6 +359,11 @@ SSL *s;
324 s->init_num=0; 359 s->init_num=0;
325 360
326 s->session->cipher=s->s3->tmp.new_cipher; 361 s->session->cipher=s->s3->tmp.new_cipher;
362 if (s->s3->tmp.new_compression == NULL)
363 s->session->compress_meth=0;
364 else
365 s->session->compress_meth=
366 s->s3->tmp.new_compression->id;
327 if (!s->method->ssl3_enc->setup_key_block(s)) 367 if (!s->method->ssl3_enc->setup_key_block(s))
328 { 368 {
329 ret= -1; 369 ret= -1;
@@ -343,8 +383,8 @@ SSL *s;
343 case SSL3_ST_CW_FINISHED_B: 383 case SSL3_ST_CW_FINISHED_B:
344 ret=ssl3_send_finished(s, 384 ret=ssl3_send_finished(s,
345 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B, 385 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
346 s->method->ssl3_enc->client_finished, 386 s->method->ssl3_enc->client_finished_label,
347 s->method->ssl3_enc->client_finished_len); 387 s->method->ssl3_enc->client_finished_label_len);
348 if (ret <= 0) goto end; 388 if (ret <= 0) goto end;
349 s->state=SSL3_ST_CW_FLUSH; 389 s->state=SSL3_ST_CW_FLUSH;
350 390
@@ -399,38 +439,33 @@ SSL *s;
399 /* clean a few things up */ 439 /* clean a few things up */
400 ssl3_cleanup_key_block(s); 440 ssl3_cleanup_key_block(s);
401 441
402 BUF_MEM_free(s->init_buf); 442 if (s->init_buf != NULL)
403 s->init_buf=NULL;
404
405 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
406 { 443 {
407 /* remove buffering */ 444 BUF_MEM_free(s->init_buf);
408 under=BIO_pop(s->wbio); 445 s->init_buf=NULL;
409 if (under != NULL)
410 s->wbio=under;
411 else
412 abort(); /* ok */
413
414 BIO_free(s->bbio);
415 s->bbio=NULL;
416 } 446 }
417 /* else do it later */ 447
448 /* If we are not 'joining' the last two packets,
449 * remove the buffering now */
450 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
451 ssl_free_wbio_buffer(s);
452 /* else do it later in ssl3_write */
418 453
419 s->init_num=0; 454 s->init_num=0;
420 s->new_session=0; 455 s->new_session=0;
421 456
422 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT); 457 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
423 if (s->hit) s->ctx->sess_hit++; 458 if (s->hit) s->ctx->stats.sess_hit++;
424 459
425 ret=1; 460 ret=1;
426 /* s->server=0; */ 461 /* s->server=0; */
427 s->handshake_func=ssl3_connect; 462 s->handshake_func=ssl3_connect;
428 s->ctx->sess_connect_good++; 463 s->ctx->stats.sess_connect_good++;
429 464
430 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); 465 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
431 466
432 goto end; 467 goto end;
433 break; 468 /* break; */
434 469
435 default: 470 default:
436 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE); 471 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
@@ -459,26 +494,27 @@ SSL *s;
459 skip=0; 494 skip=0;
460 } 495 }
461end: 496end:
497 s->in_handshake--;
462 if (cb != NULL) 498 if (cb != NULL)
463 cb(s,SSL_CB_CONNECT_EXIT,ret); 499 cb(s,SSL_CB_CONNECT_EXIT,ret);
464 s->in_handshake--;
465 return(ret); 500 return(ret);
466 } 501 }
467 502
468 503
469static int ssl3_client_hello(s) 504static int ssl3_client_hello(SSL *s)
470SSL *s;
471 { 505 {
472 unsigned char *buf; 506 unsigned char *buf;
473 unsigned char *p,*d; 507 unsigned char *p,*d;
474 int i; 508 int i,j;
475 unsigned long Time,l; 509 unsigned long Time,l;
510 SSL_COMP *comp;
476 511
477 buf=(unsigned char *)s->init_buf->data; 512 buf=(unsigned char *)s->init_buf->data;
478 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) 513 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
479 { 514 {
480 if ((s->session == NULL) || 515 if ((s->session == NULL) ||
481 (s->session->ssl_version != s->version)) 516 (s->session->ssl_version != s->version) ||
517 (s->session->not_resumable))
482 { 518 {
483 if (!ssl_get_new_session(s,0)) 519 if (!ssl_get_new_session(s,0))
484 goto err; 520 goto err;
@@ -488,13 +524,14 @@ SSL *s;
488 p=s->s3->client_random; 524 p=s->s3->client_random;
489 Time=time(NULL); /* Time */ 525 Time=time(NULL); /* Time */
490 l2n(Time,p); 526 l2n(Time,p);
491 RAND_bytes(&(p[4]),SSL3_RANDOM_SIZE-sizeof(Time)); 527 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
492 528
493 /* Do the message type and length last */ 529 /* Do the message type and length last */
494 d=p= &(buf[4]); 530 d=p= &(buf[4]);
495 531
496 *(p++)=s->version>>8; 532 *(p++)=s->version>>8;
497 *(p++)=s->version&0xff; 533 *(p++)=s->version&0xff;
534 s->client_version=s->version;
498 535
499 /* Random stuff */ 536 /* Random stuff */
500 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE); 537 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
@@ -522,9 +559,18 @@ SSL *s;
522 s2n(i,p); 559 s2n(i,p);
523 p+=i; 560 p+=i;
524 561
525 /* hardwire in the NULL compression algorithm. */ 562 /* COMPRESSION */
526 *(p++)=1; 563 if (s->ctx->comp_methods == NULL)
527 *(p++)=0; 564 j=0;
565 else
566 j=sk_SSL_COMP_num(s->ctx->comp_methods);
567 *(p++)=1+j;
568 for (i=0; i<j; i++)
569 {
570 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
571 *(p++)=comp->id;
572 }
573 *(p++)=0; /* Add the NULL method */
528 574
529 l=(p-d); 575 l=(p-d);
530 d=buf; 576 d=buf;
@@ -543,15 +589,15 @@ err:
543 return(-1); 589 return(-1);
544 } 590 }
545 591
546static int ssl3_get_server_hello(s) 592static int ssl3_get_server_hello(SSL *s)
547SSL *s;
548 { 593 {
549 STACK *sk; 594 STACK_OF(SSL_CIPHER) *sk;
550 SSL_CIPHER *c; 595 SSL_CIPHER *c;
551 unsigned char *p,*d; 596 unsigned char *p,*d;
552 int i,al,ok; 597 int i,al,ok;
553 unsigned int j; 598 unsigned int j;
554 long n; 599 long n;
600 SSL_COMP *comp;
555 601
556 n=ssl3_get_message(s, 602 n=ssl3_get_message(s,
557 SSL3_ST_CR_SRVR_HELLO_A, 603 SSL3_ST_CR_SRVR_HELLO_A,
@@ -561,7 +607,7 @@ SSL *s;
561 &ok); 607 &ok);
562 608
563 if (!ok) return((int)n); 609 if (!ok) return((int)n);
564 d=p=(unsigned char *)s->init_buf->data; 610 d=p=(unsigned char *)s->init_msg;
565 611
566 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff))) 612 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
567 { 613 {
@@ -590,9 +636,18 @@ SSL *s;
590 goto f_err; 636 goto f_err;
591 } 637 }
592 } 638 }
593 if ((j != 0) && (j == s->session->session_id_length) && 639 if (j != 0 && j == s->session->session_id_length
594 (memcmp(p,s->session->session_id,j) == 0)) 640 && memcmp(p,s->session->session_id,j) == 0)
595 s->hit=1; 641 {
642 if(s->sid_ctx_length != s->session->sid_ctx_length
643 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
644 {
645 al=SSL_AD_ILLEGAL_PARAMETER;
646 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
647 goto f_err;
648 }
649 s->hit=1;
650 }
596 else /* a miss or crap from the other end */ 651 else /* a miss or crap from the other end */
597 { 652 {
598 /* If we were trying for session-id reuse, make a new 653 /* If we were trying for session-id reuse, make a new
@@ -621,7 +676,7 @@ SSL *s;
621 p+=ssl_put_cipher_by_char(s,NULL,NULL); 676 p+=ssl_put_cipher_by_char(s,NULL,NULL);
622 677
623 sk=ssl_get_ciphers_by_id(s); 678 sk=ssl_get_ciphers_by_id(s);
624 i=sk_find(sk,(char *)c); 679 i=sk_SSL_CIPHER_find(sk,c);
625 if (i < 0) 680 if (i < 0)
626 { 681 {
627 /* we did not say we would use this cipher */ 682 /* we did not say we would use this cipher */
@@ -643,13 +698,23 @@ SSL *s;
643 s->s3->tmp.new_cipher=c; 698 s->s3->tmp.new_cipher=c;
644 699
645 /* lets get the compression algorithm */ 700 /* lets get the compression algorithm */
701 /* COMPRESSION */
646 j= *(p++); 702 j= *(p++);
647 if (j != 0) 703 if (j == 0)
704 comp=NULL;
705 else
706 comp=ssl3_comp_find(s->ctx->comp_methods,j);
707
708 if ((j != 0) && (comp == NULL))
648 { 709 {
649 al=SSL_AD_ILLEGAL_PARAMETER; 710 al=SSL_AD_ILLEGAL_PARAMETER;
650 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); 711 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
651 goto f_err; 712 goto f_err;
652 } 713 }
714 else
715 {
716 s->s3->tmp.new_compression=comp;
717 }
653 718
654 if (p != (d+n)) 719 if (p != (d+n))
655 { 720 {
@@ -666,26 +731,22 @@ err:
666 return(-1); 731 return(-1);
667 } 732 }
668 733
669static int ssl3_get_server_certificate(s) 734static int ssl3_get_server_certificate(SSL *s)
670SSL *s;
671 { 735 {
672 int al,i,ok,ret= -1; 736 int al,i,ok,ret= -1;
673 unsigned long n,nc,llen,l; 737 unsigned long n,nc,llen,l;
674 X509 *x=NULL; 738 X509 *x=NULL;
675 unsigned char *p,*d,*q; 739 unsigned char *p,*d,*q;
676 STACK *sk=NULL; 740 STACK_OF(X509) *sk=NULL;
677 CERT *c; 741 SESS_CERT *sc;
678 EVP_PKEY *pkey=NULL; 742 EVP_PKEY *pkey=NULL;
743 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
679 744
680 n=ssl3_get_message(s, 745 n=ssl3_get_message(s,
681 SSL3_ST_CR_CERT_A, 746 SSL3_ST_CR_CERT_A,
682 SSL3_ST_CR_CERT_B, 747 SSL3_ST_CR_CERT_B,
683 -1, 748 -1,
684#if defined(MSDOS) && !defined(WIN32) 749 s->max_cert_list,
685 1024*30, /* 30k max cert list :-) */
686#else
687 1024*100, /* 100k max cert list :-) */
688#endif
689 &ok); 750 &ok);
690 751
691 if (!ok) return((int)n); 752 if (!ok) return((int)n);
@@ -702,9 +763,9 @@ SSL *s;
702 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE); 763 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
703 goto f_err; 764 goto f_err;
704 } 765 }
705 d=p=(unsigned char *)s->init_buf->data; 766 d=p=(unsigned char *)s->init_msg;
706 767
707 if ((sk=sk_new_null()) == NULL) 768 if ((sk=sk_X509_new_null()) == NULL)
708 { 769 {
709 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE); 770 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
710 goto err; 771 goto err;
@@ -741,7 +802,7 @@ SSL *s;
741 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); 802 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
742 goto f_err; 803 goto f_err;
743 } 804 }
744 if (!sk_push(sk,(char *)x)) 805 if (!sk_X509_push(sk,x))
745 { 806 {
746 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE); 807 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
747 goto err; 808 goto err;
@@ -752,53 +813,91 @@ SSL *s;
752 } 813 }
753 814
754 i=ssl_verify_cert_chain(s,sk); 815 i=ssl_verify_cert_chain(s,sk);
755 if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)) 816 if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)
817#ifndef OPENSSL_NO_KRB5
818 && (s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK))
819 != (SSL_aKRB5|SSL_kKRB5)
820#endif /* OPENSSL_NO_KRB5 */
821 )
756 { 822 {
757 al=ssl_verify_alarm_type(s->verify_result); 823 al=ssl_verify_alarm_type(s->verify_result);
758 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED); 824 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
759 goto f_err; 825 goto f_err;
760 } 826 }
827 ERR_clear_error(); /* but we keep s->verify_result */
761 828
762 c=ssl_cert_new(); 829 sc=ssl_sess_cert_new();
763 if (c == NULL) goto err; 830 if (sc == NULL) goto err;
764 831
765 if (s->session->cert) ssl_cert_free(s->session->cert); 832 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
766 s->session->cert=c; 833 s->session->sess_cert=sc;
767 834
768 c->cert_chain=sk; 835 sc->cert_chain=sk;
769 x=(X509 *)sk_value(sk,0); 836 /* Inconsistency alert: cert_chain does include the peer's
837 * certificate, which we don't include in s3_srvr.c */
838 x=sk_X509_value(sk,0);
770 sk=NULL; 839 sk=NULL;
840 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
771 841
772 pkey=X509_get_pubkey(x); 842 pkey=X509_get_pubkey(x);
773 843
774 if (EVP_PKEY_missing_parameters(pkey)) 844 /* VRS: allow null cert if auth == KRB5 */
845 need_cert = ((s->s3->tmp.new_cipher->algorithms
846 & (SSL_MKEY_MASK|SSL_AUTH_MASK))
847 == (SSL_aKRB5|SSL_kKRB5))? 0: 1;
848
849#ifdef KSSL_DEBUG
850 printf("pkey,x = %p, %p\n", pkey,x);
851 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
852 printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name,
853 s->s3->tmp.new_cipher->algorithms, need_cert);
854#endif /* KSSL_DEBUG */
855
856 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
775 { 857 {
776 x=NULL; 858 x=NULL;
777 al=SSL3_AL_FATAL; 859 al=SSL3_AL_FATAL;
778 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); 860 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
861 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
779 goto f_err; 862 goto f_err;
780 } 863 }
781 864
782 i=ssl_cert_type(x,pkey); 865 i=ssl_cert_type(x,pkey);
783 if (i < 0) 866 if (need_cert && i < 0)
784 { 867 {
785 x=NULL; 868 x=NULL;
786 al=SSL3_AL_FATAL; 869 al=SSL3_AL_FATAL;
787 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNKNOWN_CERTIFICATE_TYPE); 870 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
871 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
788 goto f_err; 872 goto f_err;
789 } 873 }
790 874
791 c->cert_type=i; 875 if (need_cert)
792 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509); 876 {
793 if (c->pkeys[i].x509 != NULL) 877 sc->peer_cert_type=i;
794 X509_free(c->pkeys[i].x509); 878 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
795 c->pkeys[i].x509=x; 879 /* Why would the following ever happen?
796 c->key= &(c->pkeys[i]); 880 * We just created sc a couple of lines ago. */
797 881 if (sc->peer_pkeys[i].x509 != NULL)
798 if ((s->session != NULL) && (s->session->peer != NULL)) 882 X509_free(sc->peer_pkeys[i].x509);
799 X509_free(s->session->peer); 883 sc->peer_pkeys[i].x509=x;
800 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509); 884 sc->peer_key= &(sc->peer_pkeys[i]);
801 s->session->peer=x; 885
886 if (s->session->peer != NULL)
887 X509_free(s->session->peer);
888 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
889 s->session->peer=x;
890 }
891 else
892 {
893 sc->peer_cert_type=i;
894 sc->peer_key= NULL;
895
896 if (s->session->peer != NULL)
897 X509_free(s->session->peer);
898 s->session->peer=NULL;
899 }
900 s->session->verify_result = s->verify_result;
802 901
803 x=NULL; 902 x=NULL;
804 ret=1; 903 ret=1;
@@ -809,15 +908,15 @@ f_err:
809 ssl3_send_alert(s,SSL3_AL_FATAL,al); 908 ssl3_send_alert(s,SSL3_AL_FATAL,al);
810 } 909 }
811err: 910err:
812 if (x != NULL) X509_free(x); 911 EVP_PKEY_free(pkey);
813 if (sk != NULL) sk_pop_free(sk,X509_free); 912 X509_free(x);
913 sk_X509_pop_free(sk,X509_free);
814 return(ret); 914 return(ret);
815 } 915 }
816 916
817static int ssl3_get_key_exchange(s) 917static int ssl3_get_key_exchange(SSL *s)
818SSL *s;
819 { 918 {
820#ifndef NO_RSA 919#ifndef OPENSSL_NO_RSA
821 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2]; 920 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
822#endif 921#endif
823 EVP_MD_CTX md_ctx; 922 EVP_MD_CTX md_ctx;
@@ -825,16 +924,20 @@ SSL *s;
825 int al,i,j,param_len,ok; 924 int al,i,j,param_len,ok;
826 long n,alg; 925 long n,alg;
827 EVP_PKEY *pkey=NULL; 926 EVP_PKEY *pkey=NULL;
927#ifndef OPENSSL_NO_RSA
828 RSA *rsa=NULL; 928 RSA *rsa=NULL;
829#ifndef NO_DH 929#endif
930#ifndef OPENSSL_NO_DH
830 DH *dh=NULL; 931 DH *dh=NULL;
831#endif 932#endif
832 933
934 /* use same message size as in ssl3_get_certificate_request()
935 * as ServerKeyExchange message may be skipped */
833 n=ssl3_get_message(s, 936 n=ssl3_get_message(s,
834 SSL3_ST_CR_KEY_EXCH_A, 937 SSL3_ST_CR_KEY_EXCH_A,
835 SSL3_ST_CR_KEY_EXCH_B, 938 SSL3_ST_CR_KEY_EXCH_B,
836 -1, 939 -1,
837 1024*8, /* ?? */ 940 s->max_cert_list,
838 &ok); 941 &ok);
839 942
840 if (!ok) return((int)n); 943 if (!ok) return((int)n);
@@ -845,34 +948,35 @@ SSL *s;
845 return(1); 948 return(1);
846 } 949 }
847 950
848 param=p=(unsigned char *)s->init_buf->data; 951 param=p=(unsigned char *)s->init_msg;
849 952
850 if (s->session->cert != NULL) 953 if (s->session->sess_cert != NULL)
851 { 954 {
852#ifndef NO_RSA 955#ifndef OPENSSL_NO_RSA
853 if (s->session->cert->rsa_tmp != NULL) 956 if (s->session->sess_cert->peer_rsa_tmp != NULL)
854 { 957 {
855 RSA_free(s->session->cert->rsa_tmp); 958 RSA_free(s->session->sess_cert->peer_rsa_tmp);
856 s->session->cert->rsa_tmp=NULL; 959 s->session->sess_cert->peer_rsa_tmp=NULL;
857 } 960 }
858#endif 961#endif
859#ifndef NO_DH 962#ifndef OPENSSL_NO_DH
860 if (s->session->cert->dh_tmp) 963 if (s->session->sess_cert->peer_dh_tmp)
861 { 964 {
862 DH_free(s->session->cert->dh_tmp); 965 DH_free(s->session->sess_cert->peer_dh_tmp);
863 s->session->cert->dh_tmp=NULL; 966 s->session->sess_cert->peer_dh_tmp=NULL;
864 } 967 }
865#endif 968#endif
866 } 969 }
867 else 970 else
868 { 971 {
869 s->session->cert=ssl_cert_new(); 972 s->session->sess_cert=ssl_sess_cert_new();
870 } 973 }
871 974
872 param_len=0; 975 param_len=0;
873 alg=s->s3->tmp.new_cipher->algorithms; 976 alg=s->s3->tmp.new_cipher->algorithms;
977 EVP_MD_CTX_init(&md_ctx);
874 978
875#ifndef NO_RSA 979#ifndef OPENSSL_NO_RSA
876 if (alg & SSL_kRSA) 980 if (alg & SSL_kRSA)
877 { 981 {
878 if ((rsa=RSA_new()) == NULL) 982 if ((rsa=RSA_new()) == NULL)
@@ -911,21 +1015,23 @@ SSL *s;
911 p+=i; 1015 p+=i;
912 n-=param_len; 1016 n-=param_len;
913 1017
914/* s->session->cert->rsa_tmp=rsa;*/
915 /* this should be because we are using an export cipher */ 1018 /* this should be because we are using an export cipher */
916 if (alg & SSL_aRSA) 1019 if (alg & SSL_aRSA)
917 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509); 1020 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
918 else 1021 else
919 { 1022 {
920 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR); 1023 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
921 goto err; 1024 goto err;
922 } 1025 }
923 s->session->cert->rsa_tmp=rsa; 1026 s->session->sess_cert->peer_rsa_tmp=rsa;
1027 rsa=NULL;
924 } 1028 }
925 else 1029#else /* OPENSSL_NO_RSA */
1030 if (0)
1031 ;
926#endif 1032#endif
927#ifndef NO_DH 1033#ifndef OPENSSL_NO_DH
928 if (alg & SSL_kEDH) 1034 else if (alg & SSL_kEDH)
929 { 1035 {
930 if ((dh=DH_new()) == NULL) 1036 if ((dh=DH_new()) == NULL)
931 { 1037 {
@@ -978,18 +1084,21 @@ SSL *s;
978 p+=i; 1084 p+=i;
979 n-=param_len; 1085 n-=param_len;
980 1086
981#ifndef NO_RSA 1087#ifndef OPENSSL_NO_RSA
982 if (alg & SSL_aRSA) 1088 if (alg & SSL_aRSA)
983 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509); 1089 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
984 else 1090#else
1091 if (0)
1092 ;
985#endif 1093#endif
986#ifndef NO_DSA 1094#ifndef OPENSSL_NO_DSA
987 if (alg & SSL_aDSS) 1095 else if (alg & SSL_aDSS)
988 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_DSA_SIGN].x509); 1096 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
989#endif 1097#endif
990 /* else anonymous DH, so no certificate or pkey. */ 1098 /* else anonymous DH, so no certificate or pkey. */
991 1099
992 s->session->cert->dh_tmp=dh; 1100 s->session->sess_cert->peer_dh_tmp=dh;
1101 dh=NULL;
993 } 1102 }
994 else if ((alg & SSL_kDHr) || (alg & SSL_kDHd)) 1103 else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
995 { 1104 {
@@ -997,7 +1106,14 @@ SSL *s;
997 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER); 1106 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
998 goto f_err; 1107 goto f_err;
999 } 1108 }
1000#endif 1109#endif /* !OPENSSL_NO_DH */
1110 if (alg & SSL_aFZA)
1111 {
1112 al=SSL_AD_HANDSHAKE_FAILURE;
1113 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1114 goto f_err;
1115 }
1116
1001 1117
1002 /* p points to the next byte, there are 'n' bytes left */ 1118 /* p points to the next byte, there are 'n' bytes left */
1003 1119
@@ -1014,10 +1130,10 @@ SSL *s;
1014 /* wrong packet length */ 1130 /* wrong packet length */
1015 al=SSL_AD_DECODE_ERROR; 1131 al=SSL_AD_DECODE_ERROR;
1016 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH); 1132 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1017 goto err; 1133 goto f_err;
1018 } 1134 }
1019 1135
1020#ifndef NO_RSA 1136#ifndef OPENSSL_NO_RSA
1021 if (pkey->type == EVP_PKEY_RSA) 1137 if (pkey->type == EVP_PKEY_RSA)
1022 { 1138 {
1023 int num; 1139 int num;
@@ -1026,24 +1142,24 @@ SSL *s;
1026 q=md_buf; 1142 q=md_buf;
1027 for (num=2; num > 0; num--) 1143 for (num=2; num > 0; num--)
1028 { 1144 {
1029 EVP_DigestInit(&md_ctx,(num == 2) 1145 EVP_DigestInit_ex(&md_ctx,(num == 2)
1030 ?s->ctx->md5:s->ctx->sha1); 1146 ?s->ctx->md5:s->ctx->sha1, NULL);
1031 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1147 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1032 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1148 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1033 EVP_DigestUpdate(&md_ctx,param,param_len); 1149 EVP_DigestUpdate(&md_ctx,param,param_len);
1034 EVP_DigestFinal(&md_ctx,q,(unsigned int *)&i); 1150 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
1035 q+=i; 1151 q+=i;
1036 j+=i; 1152 j+=i;
1037 } 1153 }
1038 i=RSA_public_decrypt((int)n,p,p,pkey->pkey.rsa, 1154 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
1039 RSA_PKCS1_PADDING); 1155 pkey->pkey.rsa);
1040 if (i <= 0) 1156 if (i < 0)
1041 { 1157 {
1042 al=SSL_AD_DECRYPT_ERROR; 1158 al=SSL_AD_DECRYPT_ERROR;
1043 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); 1159 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1044 goto f_err; 1160 goto f_err;
1045 } 1161 }
1046 if ((j != i) || (memcmp(p,md_buf,i) != 0)) 1162 if (i == 0)
1047 { 1163 {
1048 /* bad signature */ 1164 /* bad signature */
1049 al=SSL_AD_DECRYPT_ERROR; 1165 al=SSL_AD_DECRYPT_ERROR;
@@ -1053,11 +1169,11 @@ SSL *s;
1053 } 1169 }
1054 else 1170 else
1055#endif 1171#endif
1056#ifndef NO_DSA 1172#ifndef OPENSSL_NO_DSA
1057 if (pkey->type == EVP_PKEY_DSA) 1173 if (pkey->type == EVP_PKEY_DSA)
1058 { 1174 {
1059 /* lets do DSS */ 1175 /* lets do DSS */
1060 EVP_VerifyInit(&md_ctx,EVP_dss1()); 1176 EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL);
1061 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1177 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1062 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1178 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1063 EVP_VerifyUpdate(&md_ctx,param,param_len); 1179 EVP_VerifyUpdate(&md_ctx,param,param_len);
@@ -1072,7 +1188,7 @@ SSL *s;
1072 else 1188 else
1073#endif 1189#endif
1074 { 1190 {
1075 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR); 1191 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1076 goto err; 1192 goto err;
1077 } 1193 }
1078 } 1194 }
@@ -1081,7 +1197,7 @@ SSL *s;
1081 /* still data left over */ 1197 /* still data left over */
1082 if (!(alg & SSL_aNULL)) 1198 if (!(alg & SSL_aNULL))
1083 { 1199 {
1084 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR); 1200 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1085 goto err; 1201 goto err;
1086 } 1202 }
1087 if (n != 0) 1203 if (n != 0)
@@ -1091,33 +1207,39 @@ SSL *s;
1091 goto f_err; 1207 goto f_err;
1092 } 1208 }
1093 } 1209 }
1094 1210 EVP_PKEY_free(pkey);
1211 EVP_MD_CTX_cleanup(&md_ctx);
1095 return(1); 1212 return(1);
1096f_err: 1213f_err:
1097 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1214 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1098err: 1215err:
1216 EVP_PKEY_free(pkey);
1217#ifndef OPENSSL_NO_RSA
1218 if (rsa != NULL)
1219 RSA_free(rsa);
1220#endif
1221#ifndef OPENSSL_NO_DH
1222 if (dh != NULL)
1223 DH_free(dh);
1224#endif
1225 EVP_MD_CTX_cleanup(&md_ctx);
1099 return(-1); 1226 return(-1);
1100 } 1227 }
1101 1228
1102static int ssl3_get_certificate_request(s) 1229static int ssl3_get_certificate_request(SSL *s)
1103SSL *s;
1104 { 1230 {
1105 int ok,ret=0; 1231 int ok,ret=0;
1106 unsigned long n,nc,l; 1232 unsigned long n,nc,l;
1107 unsigned int llen,ctype_num,i; 1233 unsigned int llen,ctype_num,i;
1108 X509_NAME *xn=NULL; 1234 X509_NAME *xn=NULL;
1109 unsigned char *p,*d,*q; 1235 unsigned char *p,*d,*q;
1110 STACK *ca_sk=NULL; 1236 STACK_OF(X509_NAME) *ca_sk=NULL;
1111 1237
1112 n=ssl3_get_message(s, 1238 n=ssl3_get_message(s,
1113 SSL3_ST_CR_CERT_REQ_A, 1239 SSL3_ST_CR_CERT_REQ_A,
1114 SSL3_ST_CR_CERT_REQ_B, 1240 SSL3_ST_CR_CERT_REQ_B,
1115 -1, 1241 -1,
1116#if defined(MSDOS) && !defined(WIN32) 1242 s->max_cert_list,
1117 1024*30, /* 30k max cert list :-) */
1118#else
1119 1024*100, /* 100k max cert list :-) */
1120#endif
1121 &ok); 1243 &ok);
1122 1244
1123 if (!ok) return((int)n); 1245 if (!ok) return((int)n);
@@ -1149,9 +1271,9 @@ SSL *s;
1149 } 1271 }
1150 } 1272 }
1151 1273
1152 d=p=(unsigned char *)s->init_buf->data; 1274 d=p=(unsigned char *)s->init_msg;
1153 1275
1154 if ((ca_sk=sk_new(ca_dn_cmp)) == NULL) 1276 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
1155 { 1277 {
1156 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE); 1278 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1157 goto err; 1279 goto err;
@@ -1167,6 +1289,15 @@ SSL *s;
1167 1289
1168 /* get the CA RDNs */ 1290 /* get the CA RDNs */
1169 n2s(p,llen); 1291 n2s(p,llen);
1292#if 0
1293{
1294FILE *out;
1295out=fopen("/tmp/vsign.der","w");
1296fwrite(p,1,llen,out);
1297fclose(out);
1298}
1299#endif
1300
1170 if ((llen+ctype_num+2+1) != n) 1301 if ((llen+ctype_num+2+1) != n)
1171 { 1302 {
1172 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); 1303 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
@@ -1190,7 +1321,7 @@ SSL *s;
1190 1321
1191 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL) 1322 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1192 { 1323 {
1193 /* If netscape tollerance is on, ignore errors */ 1324 /* If netscape tolerance is on, ignore errors */
1194 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG) 1325 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
1195 goto cont; 1326 goto cont;
1196 else 1327 else
@@ -1207,7 +1338,7 @@ SSL *s;
1207 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH); 1338 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1208 goto err; 1339 goto err;
1209 } 1340 }
1210 if (!sk_push(ca_sk,(char *)xn)) 1341 if (!sk_X509_NAME_push(ca_sk,xn))
1211 { 1342 {
1212 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE); 1343 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1213 goto err; 1344 goto err;
@@ -1223,28 +1354,26 @@ cont:
1223 ERR_clear_error(); 1354 ERR_clear_error();
1224 } 1355 }
1225 1356
1226 /* we should setup a certficate to return.... */ 1357 /* we should setup a certificate to return.... */
1227 s->s3->tmp.cert_req=1; 1358 s->s3->tmp.cert_req=1;
1228 s->s3->tmp.ctype_num=ctype_num; 1359 s->s3->tmp.ctype_num=ctype_num;
1229 if (s->s3->tmp.ca_names != NULL) 1360 if (s->s3->tmp.ca_names != NULL)
1230 sk_pop_free(s->s3->tmp.ca_names,X509_NAME_free); 1361 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1231 s->s3->tmp.ca_names=ca_sk; 1362 s->s3->tmp.ca_names=ca_sk;
1232 ca_sk=NULL; 1363 ca_sk=NULL;
1233 1364
1234 ret=1; 1365 ret=1;
1235err: 1366err:
1236 if (ca_sk != NULL) sk_pop_free(ca_sk,X509_NAME_free); 1367 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1237 return(ret); 1368 return(ret);
1238 } 1369 }
1239 1370
1240static int ca_dn_cmp(a,b) 1371static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
1241X509_NAME **a,**b;
1242 { 1372 {
1243 return(X509_NAME_cmp(*a,*b)); 1373 return(X509_NAME_cmp(*a,*b));
1244 } 1374 }
1245 1375
1246static int ssl3_get_server_done(s) 1376static int ssl3_get_server_done(SSL *s)
1247SSL *s;
1248 { 1377 {
1249 int ok,ret=0; 1378 int ok,ret=0;
1250 long n; 1379 long n;
@@ -1262,18 +1391,24 @@ SSL *s;
1262 /* should contain no data */ 1391 /* should contain no data */
1263 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); 1392 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1264 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH); 1393 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
1394 return -1;
1265 } 1395 }
1266 ret=1; 1396 ret=1;
1267 return(ret); 1397 return(ret);
1268 } 1398 }
1269 1399
1270static int ssl3_send_client_key_exchange(s) 1400static int ssl3_send_client_key_exchange(SSL *s)
1271SSL *s;
1272 { 1401 {
1273 unsigned char *p,*q,*d; 1402 unsigned char *p,*d;
1274 int n; 1403 int n;
1275 unsigned long l; 1404 unsigned long l;
1405#ifndef OPENSSL_NO_RSA
1406 unsigned char *q;
1276 EVP_PKEY *pkey=NULL; 1407 EVP_PKEY *pkey=NULL;
1408#endif
1409#ifndef OPENSSL_NO_KRB5
1410 KSSL_ERR kssl_err;
1411#endif /* OPENSSL_NO_KRB5 */
1277 1412
1278 if (s->state == SSL3_ST_CW_KEY_EXCH_A) 1413 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1279 { 1414 {
@@ -1282,30 +1417,34 @@ SSL *s;
1282 1417
1283 l=s->s3->tmp.new_cipher->algorithms; 1418 l=s->s3->tmp.new_cipher->algorithms;
1284 1419
1285#ifndef NO_RSA 1420 /* Fool emacs indentation */
1286 if (l & SSL_kRSA) 1421 if (0) {}
1422#ifndef OPENSSL_NO_RSA
1423 else if (l & SSL_kRSA)
1287 { 1424 {
1288 RSA *rsa; 1425 RSA *rsa;
1289 unsigned char tmp_buf[48]; 1426 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1290 1427
1291 if (s->session->cert->rsa_tmp != NULL) 1428 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1292 rsa=s->session->cert->rsa_tmp; 1429 rsa=s->session->sess_cert->peer_rsa_tmp;
1293 else 1430 else
1294 { 1431 {
1295 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509); 1432 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1296 if ((pkey == NULL) || 1433 if ((pkey == NULL) ||
1297 (pkey->type != EVP_PKEY_RSA) || 1434 (pkey->type != EVP_PKEY_RSA) ||
1298 (pkey->pkey.rsa == NULL)) 1435 (pkey->pkey.rsa == NULL))
1299 { 1436 {
1300 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR); 1437 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1301 goto err; 1438 goto err;
1302 } 1439 }
1303 rsa=pkey->pkey.rsa; 1440 rsa=pkey->pkey.rsa;
1441 EVP_PKEY_free(pkey);
1304 } 1442 }
1305 1443
1306 tmp_buf[0]=s->version>>8; 1444 tmp_buf[0]=s->client_version>>8;
1307 tmp_buf[1]=s->version&0xff; 1445 tmp_buf[1]=s->client_version&0xff;
1308 RAND_bytes(&(tmp_buf[2]),SSL_MAX_MASTER_KEY_LENGTH-2); 1446 if (RAND_bytes(&(tmp_buf[2]),SSL_MAX_MASTER_KEY_LENGTH-2) <= 0)
1447 goto err;
1309 1448
1310 s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH; 1449 s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
1311 1450
@@ -1315,6 +1454,10 @@ SSL *s;
1315 p+=2; 1454 p+=2;
1316 n=RSA_public_encrypt(SSL_MAX_MASTER_KEY_LENGTH, 1455 n=RSA_public_encrypt(SSL_MAX_MASTER_KEY_LENGTH,
1317 tmp_buf,p,rsa,RSA_PKCS1_PADDING); 1456 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
1457#ifdef PKCS1_CHECK
1458 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
1459 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
1460#endif
1318 if (n <= 0) 1461 if (n <= 0)
1319 { 1462 {
1320 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT); 1463 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
@@ -1331,18 +1474,144 @@ SSL *s;
1331 s->session->master_key_length= 1474 s->session->master_key_length=
1332 s->method->ssl3_enc->generate_master_secret(s, 1475 s->method->ssl3_enc->generate_master_secret(s,
1333 s->session->master_key, 1476 s->session->master_key,
1334 tmp_buf,48); 1477 tmp_buf,SSL_MAX_MASTER_KEY_LENGTH);
1335 memset(tmp_buf,0,48); 1478 memset(tmp_buf,0,SSL_MAX_MASTER_KEY_LENGTH);
1336 } 1479 }
1337 else
1338#endif 1480#endif
1339#ifndef NO_DH 1481#ifndef OPENSSL_NO_KRB5
1340 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) 1482 else if (l & SSL_kKRB5)
1483 {
1484 krb5_error_code krb5rc;
1485 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1486 /* krb5_data krb5_ap_req; */
1487 krb5_data *enc_ticket;
1488 krb5_data authenticator, *authp = NULL;
1489 EVP_CIPHER_CTX ciph_ctx;
1490 EVP_CIPHER *enc = NULL;
1491 unsigned char iv[EVP_MAX_IV_LENGTH];
1492 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1493 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
1494 + EVP_MAX_IV_LENGTH];
1495 int padl, outl = sizeof(epms);
1496
1497 EVP_CIPHER_CTX_init(&ciph_ctx);
1498
1499#ifdef KSSL_DEBUG
1500 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
1501 l, SSL_kKRB5);
1502#endif /* KSSL_DEBUG */
1503
1504 authp = NULL;
1505#ifdef KRB5SENDAUTH
1506 if (KRB5SENDAUTH) authp = &authenticator;
1507#endif /* KRB5SENDAUTH */
1508
1509 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
1510 &kssl_err);
1511 enc = kssl_map_enc(kssl_ctx->enctype);
1512 if (enc == NULL)
1513 goto err;
1514#ifdef KSSL_DEBUG
1515 {
1516 printf("kssl_cget_tkt rtn %d\n", krb5rc);
1517 if (krb5rc && kssl_err.text)
1518 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
1519 }
1520#endif /* KSSL_DEBUG */
1521
1522 if (krb5rc)
1523 {
1524 ssl3_send_alert(s,SSL3_AL_FATAL,
1525 SSL_AD_HANDSHAKE_FAILURE);
1526 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1527 kssl_err.reason);
1528 goto err;
1529 }
1530
1531 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
1532 ** in place of RFC 2712 KerberosWrapper, as in:
1533 **
1534 ** Send ticket (copy to *p, set n = length)
1535 ** n = krb5_ap_req.length;
1536 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
1537 ** if (krb5_ap_req.data)
1538 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
1539 **
1540 ** Now using real RFC 2712 KerberosWrapper
1541 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
1542 ** Note: 2712 "opaque" types are here replaced
1543 ** with a 2-byte length followed by the value.
1544 ** Example:
1545 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
1546 ** Where "xx xx" = length bytes. Shown here with
1547 ** optional authenticator omitted.
1548 */
1549
1550 /* KerberosWrapper.Ticket */
1551 s2n(enc_ticket->length,p);
1552 memcpy(p, enc_ticket->data, enc_ticket->length);
1553 p+= enc_ticket->length;
1554 n = enc_ticket->length + 2;
1555
1556 /* KerberosWrapper.Authenticator */
1557 if (authp && authp->length)
1558 {
1559 s2n(authp->length,p);
1560 memcpy(p, authp->data, authp->length);
1561 p+= authp->length;
1562 n+= authp->length + 2;
1563
1564 free(authp->data);
1565 authp->data = NULL;
1566 authp->length = 0;
1567 }
1568 else
1569 {
1570 s2n(0,p);/* null authenticator length */
1571 n+=2;
1572 }
1573
1574 if (RAND_bytes(tmp_buf,SSL_MAX_MASTER_KEY_LENGTH) <= 0)
1575 goto err;
1576
1577 /* 20010420 VRS. Tried it this way; failed.
1578 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
1579 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
1580 ** kssl_ctx->length);
1581 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
1582 */
1583
1584 memset(iv, 0, EVP_MAX_IV_LENGTH); /* per RFC 1510 */
1585 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
1586 kssl_ctx->key,iv);
1587 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
1588 SSL_MAX_MASTER_KEY_LENGTH);
1589 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
1590 outl += padl;
1591 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
1592
1593 /* KerberosWrapper.EncryptedPreMasterSecret */
1594 s2n(outl,p);
1595 memcpy(p, epms, outl);
1596 p+=outl;
1597 n+=outl + 2;
1598
1599 s->session->master_key_length=
1600 s->method->ssl3_enc->generate_master_secret(s,
1601 s->session->master_key,
1602 tmp_buf, SSL_MAX_MASTER_KEY_LENGTH);
1603
1604 memset(tmp_buf, 0, SSL_MAX_MASTER_KEY_LENGTH);
1605 memset(epms, 0, outl);
1606 }
1607#endif
1608#ifndef OPENSSL_NO_DH
1609 else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1341 { 1610 {
1342 DH *dh_srvr,*dh_clnt; 1611 DH *dh_srvr,*dh_clnt;
1343 1612
1344 if (s->session->cert->dh_tmp != NULL) 1613 if (s->session->sess_cert->peer_dh_tmp != NULL)
1345 dh_srvr=s->session->cert->dh_tmp; 1614 dh_srvr=s->session->sess_cert->peer_dh_tmp;
1346 else 1615 else
1347 { 1616 {
1348 /* we get them from the cert */ 1617 /* we get them from the cert */
@@ -1391,11 +1660,11 @@ SSL *s;
1391 1660
1392 /* perhaps clean things up a bit EAY EAY EAY EAY*/ 1661 /* perhaps clean things up a bit EAY EAY EAY EAY*/
1393 } 1662 }
1394 else
1395#endif 1663#endif
1664 else
1396 { 1665 {
1397 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); 1666 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1398 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR); 1667 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1399 goto err; 1668 goto err;
1400 } 1669 }
1401 1670
@@ -1414,15 +1683,16 @@ err:
1414 return(-1); 1683 return(-1);
1415 } 1684 }
1416 1685
1417static int ssl3_send_client_verify(s) 1686static int ssl3_send_client_verify(SSL *s)
1418SSL *s;
1419 { 1687 {
1420 unsigned char *p,*d; 1688 unsigned char *p,*d;
1421 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; 1689 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1422 EVP_PKEY *pkey; 1690 EVP_PKEY *pkey;
1423 int i=0; 1691#ifndef OPENSSL_NO_RSA
1692 unsigned u=0;
1693#endif
1424 unsigned long n; 1694 unsigned long n;
1425#ifndef NO_DSA 1695#ifndef OPENSSL_NO_DSA
1426 int j; 1696 int j;
1427#endif 1697#endif
1428 1698
@@ -1435,26 +1705,24 @@ SSL *s;
1435 s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2), 1705 s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
1436 &(data[MD5_DIGEST_LENGTH])); 1706 &(data[MD5_DIGEST_LENGTH]));
1437 1707
1438#ifndef NO_RSA 1708#ifndef OPENSSL_NO_RSA
1439 if (pkey->type == EVP_PKEY_RSA) 1709 if (pkey->type == EVP_PKEY_RSA)
1440 { 1710 {
1441 s->method->ssl3_enc->cert_verify_mac(s, 1711 s->method->ssl3_enc->cert_verify_mac(s,
1442 &(s->s3->finish_dgst1),&(data[0])); 1712 &(s->s3->finish_dgst1),&(data[0]));
1443 i=RSA_private_encrypt( 1713 if (RSA_sign(NID_md5_sha1, data,
1444 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, 1714 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1445 data,&(p[2]),pkey->pkey.rsa, 1715 &(p[2]), &u, pkey->pkey.rsa) <= 0 )
1446 RSA_PKCS1_PADDING);
1447 if (i <= 0)
1448 { 1716 {
1449 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB); 1717 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
1450 goto err; 1718 goto err;
1451 } 1719 }
1452 s2n(i,p); 1720 s2n(u,p);
1453 n=i+2; 1721 n=u+2;
1454 } 1722 }
1455 else 1723 else
1456#endif 1724#endif
1457#ifndef NO_DSA 1725#ifndef OPENSSL_NO_DSA
1458 if (pkey->type == EVP_PKEY_DSA) 1726 if (pkey->type == EVP_PKEY_DSA)
1459 { 1727 {
1460 if (!DSA_sign(pkey->save_type, 1728 if (!DSA_sign(pkey->save_type,
@@ -1471,7 +1739,7 @@ SSL *s;
1471 else 1739 else
1472#endif 1740#endif
1473 { 1741 {
1474 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,SSL_R_INTERNAL_ERROR); 1742 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
1475 goto err; 1743 goto err;
1476 } 1744 }
1477 *(d++)=SSL3_MT_CERTIFICATE_VERIFY; 1745 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
@@ -1485,8 +1753,7 @@ err:
1485 return(-1); 1753 return(-1);
1486 } 1754 }
1487 1755
1488static int ssl3_send_client_certificate(s) 1756static int ssl3_send_client_certificate(SSL *s)
1489SSL *s;
1490 { 1757 {
1491 X509 *x509=NULL; 1758 X509 *x509=NULL;
1492 EVP_PKEY *pkey=NULL; 1759 EVP_PKEY *pkey=NULL;
@@ -1565,38 +1832,46 @@ SSL *s;
1565 1832
1566#define has_bits(i,m) (((i)&(m)) == (m)) 1833#define has_bits(i,m) (((i)&(m)) == (m))
1567 1834
1568static int ssl3_check_cert_and_algorithm(s) 1835static int ssl3_check_cert_and_algorithm(SSL *s)
1569SSL *s;
1570 { 1836 {
1571 int i,idx; 1837 int i,idx;
1572 long algs; 1838 long algs;
1573 EVP_PKEY *pkey=NULL; 1839 EVP_PKEY *pkey=NULL;
1574 CERT *c; 1840 SESS_CERT *sc;
1841#ifndef OPENSSL_NO_RSA
1575 RSA *rsa; 1842 RSA *rsa;
1843#endif
1844#ifndef OPENSSL_NO_DH
1576 DH *dh; 1845 DH *dh;
1846#endif
1577 1847
1578 c=s->session->cert; 1848 sc=s->session->sess_cert;
1579 1849
1580 if (c == NULL) 1850 if (sc == NULL)
1581 { 1851 {
1582 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_INTERNAL_ERROR); 1852 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
1583 goto err; 1853 goto err;
1584 } 1854 }
1585 1855
1586 algs=s->s3->tmp.new_cipher->algorithms; 1856 algs=s->s3->tmp.new_cipher->algorithms;
1587 1857
1588 /* we don't have a certificate */ 1858 /* we don't have a certificate */
1589 if (algs & (SSL_aDH|SSL_aNULL)) 1859 if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5))
1590 return(1); 1860 return(1);
1591 1861
1592 rsa=s->session->cert->rsa_tmp; 1862#ifndef OPENSSL_NO_RSA
1593 dh=s->session->cert->dh_tmp; 1863 rsa=s->session->sess_cert->peer_rsa_tmp;
1864#endif
1865#ifndef OPENSSL_NO_DH
1866 dh=s->session->sess_cert->peer_dh_tmp;
1867#endif
1594 1868
1595 /* This is the passed certificate */ 1869 /* This is the passed certificate */
1596 1870
1597 idx=c->cert_type; 1871 idx=sc->peer_cert_type;
1598 pkey=X509_get_pubkey(c->pkeys[idx].x509); 1872 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
1599 i=X509_certificate_type(c->pkeys[idx].x509,pkey); 1873 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
1874 EVP_PKEY_free(pkey);
1600 1875
1601 1876
1602 /* Check that we have a certificate if we require one */ 1877 /* Check that we have a certificate if we require one */
@@ -1605,22 +1880,23 @@ SSL *s;
1605 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT); 1880 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
1606 goto f_err; 1881 goto f_err;
1607 } 1882 }
1608#ifndef NO_DSA 1883#ifndef OPENSSL_NO_DSA
1609 else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN)) 1884 else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
1610 { 1885 {
1611 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT); 1886 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
1612 goto f_err; 1887 goto f_err;
1613 } 1888 }
1614#endif 1889#endif
1615 1890#ifndef OPENSSL_NO_RSA
1616 if ((algs & SSL_kRSA) && 1891 if ((algs & SSL_kRSA) &&
1617 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL))) 1892 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
1618 { 1893 {
1619 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT); 1894 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
1620 goto f_err; 1895 goto f_err;
1621 } 1896 }
1622#ifndef NO_DH 1897#endif
1623 else if ((algs & SSL_kEDH) && 1898#ifndef OPENSSL_NO_DH
1899 if ((algs & SSL_kEDH) &&
1624 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL))) 1900 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
1625 { 1901 {
1626 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY); 1902 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
@@ -1631,7 +1907,7 @@ SSL *s;
1631 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT); 1907 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
1632 goto f_err; 1908 goto f_err;
1633 } 1909 }
1634#ifndef NO_DSA 1910#ifndef OPENSSL_NO_DSA
1635 else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA)) 1911 else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
1636 { 1912 {
1637 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT); 1913 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
@@ -1640,12 +1916,13 @@ SSL *s;
1640#endif 1916#endif
1641#endif 1917#endif
1642 1918
1643 if ((algs & SSL_EXP) && !has_bits(i,EVP_PKT_EXP)) 1919 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
1644 { 1920 {
1645#ifndef NO_RSA 1921#ifndef OPENSSL_NO_RSA
1646 if (algs & SSL_kRSA) 1922 if (algs & SSL_kRSA)
1647 { 1923 {
1648 if ((rsa == NULL) || (RSA_size(rsa) > 512)) 1924 if (rsa == NULL
1925 || RSA_size(rsa) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
1649 { 1926 {
1650 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY); 1927 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
1651 goto f_err; 1928 goto f_err;
@@ -1653,10 +1930,11 @@ SSL *s;
1653 } 1930 }
1654 else 1931 else
1655#endif 1932#endif
1656#ifndef NO_DH 1933#ifndef OPENSSL_NO_DH
1657 if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) 1934 if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1658 { 1935 {
1659 if ((dh == NULL) || (DH_size(dh) > 512)) 1936 if (dh == NULL
1937 || DH_size(dh) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
1660 { 1938 {
1661 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY); 1939 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
1662 goto f_err; 1940 goto f_err;