summaryrefslogtreecommitdiff
path: root/src/lib/libssl/s3_clnt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/s3_clnt.c')
-rw-r--r--src/lib/libssl/s3_clnt.c1963
1 files changed, 0 insertions, 1963 deletions
diff --git a/src/lib/libssl/s3_clnt.c b/src/lib/libssl/s3_clnt.c
deleted file mode 100644
index 7da9363ef5..0000000000
--- a/src/lib/libssl/s3_clnt.c
+++ /dev/null
@@ -1,1963 +0,0 @@
1/* ssl/s3_clnt.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-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 */
111
112#include <stdio.h>
113#include "ssl_locl.h"
114#include "kssl_lcl.h"
115#include <openssl/buffer.h>
116#include <openssl/rand.h>
117#include <openssl/objects.h>
118#include <openssl/evp.h>
119#include <openssl/md5.h>
120
121static SSL_METHOD *ssl3_get_client_method(int ver);
122static int ssl3_client_hello(SSL *s);
123static int ssl3_get_server_hello(SSL *s);
124static int ssl3_get_certificate_request(SSL *s);
125static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
126static int ssl3_get_server_done(SSL *s);
127static int ssl3_send_client_verify(SSL *s);
128static int ssl3_send_client_certificate(SSL *s);
129static int ssl3_send_client_key_exchange(SSL *s);
130static int ssl3_get_key_exchange(SSL *s);
131static int ssl3_get_server_certificate(SSL *s);
132static int ssl3_check_cert_and_algorithm(SSL *s);
133static SSL_METHOD *ssl3_get_client_method(int ver)
134 {
135 if (ver == SSL3_VERSION)
136 return(SSLv3_client_method());
137 else
138 return(NULL);
139 }
140
141SSL_METHOD *SSLv3_client_method(void)
142 {
143 static int init=1;
144 static SSL_METHOD SSLv3_client_data;
145
146 if (init)
147 {
148 init=0;
149 memcpy((char *)&SSLv3_client_data,(char *)sslv3_base_method(),
150 sizeof(SSL_METHOD));
151 SSLv3_client_data.ssl_connect=ssl3_connect;
152 SSLv3_client_data.get_ssl_method=ssl3_get_client_method;
153 }
154 return(&SSLv3_client_data);
155 }
156
157int ssl3_connect(SSL *s)
158 {
159 BUF_MEM *buf;
160 unsigned long Time=time(NULL),l;
161 long num1;
162 void (*cb)(const SSL *ssl,int type,int val)=NULL;
163 int ret= -1;
164 int new_state,state,skip=0;;
165
166 RAND_add(&Time,sizeof(Time),0);
167 ERR_clear_error();
168 clear_sys_error();
169
170 if (s->info_callback != NULL)
171 cb=s->info_callback;
172 else if (s->ctx->info_callback != NULL)
173 cb=s->ctx->info_callback;
174
175 s->in_handshake++;
176 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
177
178 for (;;)
179 {
180 state=s->state;
181
182 switch(s->state)
183 {
184 case SSL_ST_RENEGOTIATE:
185 s->new_session=1;
186 s->state=SSL_ST_CONNECT;
187 s->ctx->stats.sess_connect_renegotiate++;
188 /* break */
189 case SSL_ST_BEFORE:
190 case SSL_ST_CONNECT:
191 case SSL_ST_BEFORE|SSL_ST_CONNECT:
192 case SSL_ST_OK|SSL_ST_CONNECT:
193
194 s->server=0;
195 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
196
197 if ((s->version & 0xff00 ) != 0x0300)
198 {
199 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
200 ret = -1;
201 goto end;
202 }
203
204 /* s->version=SSL3_VERSION; */
205 s->type=SSL_ST_CONNECT;
206
207 if (s->init_buf == NULL)
208 {
209 if ((buf=BUF_MEM_new()) == NULL)
210 {
211 ret= -1;
212 goto end;
213 }
214 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
215 {
216 ret= -1;
217 goto end;
218 }
219 s->init_buf=buf;
220 }
221
222 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
223
224 /* setup buffing BIO */
225 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
226
227 /* don't push the buffering BIO quite yet */
228
229 ssl3_init_finished_mac(s);
230
231 s->state=SSL3_ST_CW_CLNT_HELLO_A;
232 s->ctx->stats.sess_connect++;
233 s->init_num=0;
234 break;
235
236 case SSL3_ST_CW_CLNT_HELLO_A:
237 case SSL3_ST_CW_CLNT_HELLO_B:
238
239 s->shutdown=0;
240 ret=ssl3_client_hello(s);
241 if (ret <= 0) goto end;
242 s->state=SSL3_ST_CR_SRVR_HELLO_A;
243 s->init_num=0;
244
245 /* turn on buffering for the next lot of output */
246 if (s->bbio != s->wbio)
247 s->wbio=BIO_push(s->bbio,s->wbio);
248
249 break;
250
251 case SSL3_ST_CR_SRVR_HELLO_A:
252 case SSL3_ST_CR_SRVR_HELLO_B:
253 ret=ssl3_get_server_hello(s);
254 if (ret <= 0) goto end;
255 if (s->hit)
256 s->state=SSL3_ST_CR_FINISHED_A;
257 else
258 s->state=SSL3_ST_CR_CERT_A;
259 s->init_num=0;
260 break;
261
262 case SSL3_ST_CR_CERT_A:
263 case SSL3_ST_CR_CERT_B:
264 /* Check if it is anon DH */
265 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
266 {
267 ret=ssl3_get_server_certificate(s);
268 if (ret <= 0) goto end;
269 }
270 else
271 skip=1;
272 s->state=SSL3_ST_CR_KEY_EXCH_A;
273 s->init_num=0;
274 break;
275
276 case SSL3_ST_CR_KEY_EXCH_A:
277 case SSL3_ST_CR_KEY_EXCH_B:
278 ret=ssl3_get_key_exchange(s);
279 if (ret <= 0) goto end;
280 s->state=SSL3_ST_CR_CERT_REQ_A;
281 s->init_num=0;
282
283 /* at this point we check that we have the
284 * required stuff from the server */
285 if (!ssl3_check_cert_and_algorithm(s))
286 {
287 ret= -1;
288 goto end;
289 }
290 break;
291
292 case SSL3_ST_CR_CERT_REQ_A:
293 case SSL3_ST_CR_CERT_REQ_B:
294 ret=ssl3_get_certificate_request(s);
295 if (ret <= 0) goto end;
296 s->state=SSL3_ST_CR_SRVR_DONE_A;
297 s->init_num=0;
298 break;
299
300 case SSL3_ST_CR_SRVR_DONE_A:
301 case SSL3_ST_CR_SRVR_DONE_B:
302 ret=ssl3_get_server_done(s);
303 if (ret <= 0) goto end;
304 if (s->s3->tmp.cert_req)
305 s->state=SSL3_ST_CW_CERT_A;
306 else
307 s->state=SSL3_ST_CW_KEY_EXCH_A;
308 s->init_num=0;
309
310 break;
311
312 case SSL3_ST_CW_CERT_A:
313 case SSL3_ST_CW_CERT_B:
314 case SSL3_ST_CW_CERT_C:
315 case SSL3_ST_CW_CERT_D:
316 ret=ssl3_send_client_certificate(s);
317 if (ret <= 0) goto end;
318 s->state=SSL3_ST_CW_KEY_EXCH_A;
319 s->init_num=0;
320 break;
321
322 case SSL3_ST_CW_KEY_EXCH_A:
323 case SSL3_ST_CW_KEY_EXCH_B:
324 ret=ssl3_send_client_key_exchange(s);
325 if (ret <= 0) goto end;
326 l=s->s3->tmp.new_cipher->algorithms;
327 /* EAY EAY EAY need to check for DH fix cert
328 * sent back */
329 /* For TLS, cert_req is set to 2, so a cert chain
330 * of nothing is sent, but no verify packet is sent */
331 if (s->s3->tmp.cert_req == 1)
332 {
333 s->state=SSL3_ST_CW_CERT_VRFY_A;
334 }
335 else
336 {
337 s->state=SSL3_ST_CW_CHANGE_A;
338 s->s3->change_cipher_spec=0;
339 }
340
341 s->init_num=0;
342 break;
343
344 case SSL3_ST_CW_CERT_VRFY_A:
345 case SSL3_ST_CW_CERT_VRFY_B:
346 ret=ssl3_send_client_verify(s);
347 if (ret <= 0) goto end;
348 s->state=SSL3_ST_CW_CHANGE_A;
349 s->init_num=0;
350 s->s3->change_cipher_spec=0;
351 break;
352
353 case SSL3_ST_CW_CHANGE_A:
354 case SSL3_ST_CW_CHANGE_B:
355 ret=ssl3_send_change_cipher_spec(s,
356 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
357 if (ret <= 0) goto end;
358 s->state=SSL3_ST_CW_FINISHED_A;
359 s->init_num=0;
360
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;
367 if (!s->method->ssl3_enc->setup_key_block(s))
368 {
369 ret= -1;
370 goto end;
371 }
372
373 if (!s->method->ssl3_enc->change_cipher_state(s,
374 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
375 {
376 ret= -1;
377 goto end;
378 }
379
380 break;
381
382 case SSL3_ST_CW_FINISHED_A:
383 case SSL3_ST_CW_FINISHED_B:
384 ret=ssl3_send_finished(s,
385 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
386 s->method->ssl3_enc->client_finished_label,
387 s->method->ssl3_enc->client_finished_label_len);
388 if (ret <= 0) goto end;
389 s->state=SSL3_ST_CW_FLUSH;
390
391 /* clear flags */
392 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
393 if (s->hit)
394 {
395 s->s3->tmp.next_state=SSL_ST_OK;
396 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
397 {
398 s->state=SSL_ST_OK;
399 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
400 s->s3->delay_buf_pop_ret=0;
401 }
402 }
403 else
404 {
405 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
406 }
407 s->init_num=0;
408 break;
409
410 case SSL3_ST_CR_FINISHED_A:
411 case SSL3_ST_CR_FINISHED_B:
412
413 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
414 SSL3_ST_CR_FINISHED_B);
415 if (ret <= 0) goto end;
416
417 if (s->hit)
418 s->state=SSL3_ST_CW_CHANGE_A;
419 else
420 s->state=SSL_ST_OK;
421 s->init_num=0;
422 break;
423
424 case SSL3_ST_CW_FLUSH:
425 /* number of bytes to be flushed */
426 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
427 if (num1 > 0)
428 {
429 s->rwstate=SSL_WRITING;
430 num1=BIO_flush(s->wbio);
431 if (num1 <= 0) { ret= -1; goto end; }
432 s->rwstate=SSL_NOTHING;
433 }
434
435 s->state=s->s3->tmp.next_state;
436 break;
437
438 case SSL_ST_OK:
439 /* clean a few things up */
440 ssl3_cleanup_key_block(s);
441
442 if (s->init_buf != NULL)
443 {
444 BUF_MEM_free(s->init_buf);
445 s->init_buf=NULL;
446 }
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 */
453
454 s->init_num=0;
455 s->new_session=0;
456
457 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
458 if (s->hit) s->ctx->stats.sess_hit++;
459
460 ret=1;
461 /* s->server=0; */
462 s->handshake_func=ssl3_connect;
463 s->ctx->stats.sess_connect_good++;
464
465 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
466
467 goto end;
468 /* break; */
469
470 default:
471 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
472 ret= -1;
473 goto end;
474 /* break; */
475 }
476
477 /* did we do anything */
478 if (!s->s3->tmp.reuse_message && !skip)
479 {
480 if (s->debug)
481 {
482 if ((ret=BIO_flush(s->wbio)) <= 0)
483 goto end;
484 }
485
486 if ((cb != NULL) && (s->state != state))
487 {
488 new_state=s->state;
489 s->state=state;
490 cb(s,SSL_CB_CONNECT_LOOP,1);
491 s->state=new_state;
492 }
493 }
494 skip=0;
495 }
496end:
497 s->in_handshake--;
498 if (cb != NULL)
499 cb(s,SSL_CB_CONNECT_EXIT,ret);
500 return(ret);
501 }
502
503
504static int ssl3_client_hello(SSL *s)
505 {
506 unsigned char *buf;
507 unsigned char *p,*d;
508 int i,j;
509 unsigned long Time,l;
510 SSL_COMP *comp;
511
512 buf=(unsigned char *)s->init_buf->data;
513 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
514 {
515 if ((s->session == NULL) ||
516 (s->session->ssl_version != s->version) ||
517 (s->session->not_resumable))
518 {
519 if (!ssl_get_new_session(s,0))
520 goto err;
521 }
522 /* else use the pre-loaded session */
523
524 p=s->s3->client_random;
525 Time=time(NULL); /* Time */
526 l2n(Time,p);
527 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
528
529 /* Do the message type and length last */
530 d=p= &(buf[4]);
531
532 *(p++)=s->version>>8;
533 *(p++)=s->version&0xff;
534 s->client_version=s->version;
535
536 /* Random stuff */
537 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
538 p+=SSL3_RANDOM_SIZE;
539
540 /* Session ID */
541 if (s->new_session)
542 i=0;
543 else
544 i=s->session->session_id_length;
545 *(p++)=i;
546 if (i != 0)
547 {
548 if (i > sizeof s->session->session_id)
549 {
550 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
551 goto err;
552 }
553 memcpy(p,s->session->session_id,i);
554 p+=i;
555 }
556
557 /* Ciphers supported */
558 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]));
559 if (i == 0)
560 {
561 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
562 goto err;
563 }
564 s2n(i,p);
565 p+=i;
566
567 /* COMPRESSION */
568 if (s->ctx->comp_methods == NULL)
569 j=0;
570 else
571 j=sk_SSL_COMP_num(s->ctx->comp_methods);
572 *(p++)=1+j;
573 for (i=0; i<j; i++)
574 {
575 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
576 *(p++)=comp->id;
577 }
578 *(p++)=0; /* Add the NULL method */
579
580 l=(p-d);
581 d=buf;
582 *(d++)=SSL3_MT_CLIENT_HELLO;
583 l2n3(l,d);
584
585 s->state=SSL3_ST_CW_CLNT_HELLO_B;
586 /* number of bytes to write */
587 s->init_num=p-buf;
588 s->init_off=0;
589 }
590
591 /* SSL3_ST_CW_CLNT_HELLO_B */
592 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
593err:
594 return(-1);
595 }
596
597static int ssl3_get_server_hello(SSL *s)
598 {
599 STACK_OF(SSL_CIPHER) *sk;
600 SSL_CIPHER *c;
601 unsigned char *p,*d;
602 int i,al,ok;
603 unsigned int j;
604 long n;
605 SSL_COMP *comp;
606
607 n=ssl3_get_message(s,
608 SSL3_ST_CR_SRVR_HELLO_A,
609 SSL3_ST_CR_SRVR_HELLO_B,
610 SSL3_MT_SERVER_HELLO,
611 300, /* ?? */
612 &ok);
613
614 if (!ok) return((int)n);
615 d=p=(unsigned char *)s->init_msg;
616
617 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
618 {
619 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
620 s->version=(s->version&0xff00)|p[1];
621 al=SSL_AD_PROTOCOL_VERSION;
622 goto f_err;
623 }
624 p+=2;
625
626 /* load the server hello data */
627 /* load the server random */
628 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
629 p+=SSL3_RANDOM_SIZE;
630
631 /* get the session-id */
632 j= *(p++);
633
634 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
635 {
636 al=SSL_AD_ILLEGAL_PARAMETER;
637 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG);
638 goto f_err;
639 }
640 if (j != 0 && j == s->session->session_id_length
641 && memcmp(p,s->session->session_id,j) == 0)
642 {
643 if(s->sid_ctx_length != s->session->sid_ctx_length
644 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
645 {
646 /* actually a client application bug */
647 al=SSL_AD_ILLEGAL_PARAMETER;
648 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
649 goto f_err;
650 }
651 s->hit=1;
652 }
653 else /* a miss or crap from the other end */
654 {
655 /* If we were trying for session-id reuse, make a new
656 * SSL_SESSION so we don't stuff up other people */
657 s->hit=0;
658 if (s->session->session_id_length > 0)
659 {
660 if (!ssl_get_new_session(s,0))
661 {
662 al=SSL_AD_INTERNAL_ERROR;
663 goto f_err;
664 }
665 }
666 s->session->session_id_length=j;
667 memcpy(s->session->session_id,p,j); /* j could be 0 */
668 }
669 p+=j;
670 c=ssl_get_cipher_by_char(s,p);
671 if (c == NULL)
672 {
673 /* unknown cipher */
674 al=SSL_AD_ILLEGAL_PARAMETER;
675 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
676 goto f_err;
677 }
678 p+=ssl_put_cipher_by_char(s,NULL,NULL);
679
680 sk=ssl_get_ciphers_by_id(s);
681 i=sk_SSL_CIPHER_find(sk,c);
682 if (i < 0)
683 {
684 /* we did not say we would use this cipher */
685 al=SSL_AD_ILLEGAL_PARAMETER;
686 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
687 goto f_err;
688 }
689
690 if (s->hit && (s->session->cipher != c))
691 {
692 if (!(s->options &
693 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
694 {
695 al=SSL_AD_ILLEGAL_PARAMETER;
696 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
697 goto f_err;
698 }
699 }
700 s->s3->tmp.new_cipher=c;
701
702 /* lets get the compression algorithm */
703 /* COMPRESSION */
704 j= *(p++);
705 if (j == 0)
706 comp=NULL;
707 else
708 comp=ssl3_comp_find(s->ctx->comp_methods,j);
709
710 if ((j != 0) && (comp == NULL))
711 {
712 al=SSL_AD_ILLEGAL_PARAMETER;
713 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
714 goto f_err;
715 }
716 else
717 {
718 s->s3->tmp.new_compression=comp;
719 }
720
721 if (p != (d+n))
722 {
723 /* wrong packet length */
724 al=SSL_AD_DECODE_ERROR;
725 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
726 goto err;
727 }
728
729 return(1);
730f_err:
731 ssl3_send_alert(s,SSL3_AL_FATAL,al);
732err:
733 return(-1);
734 }
735
736static int ssl3_get_server_certificate(SSL *s)
737 {
738 int al,i,ok,ret= -1;
739 unsigned long n,nc,llen,l;
740 X509 *x=NULL;
741 unsigned char *p,*d,*q;
742 STACK_OF(X509) *sk=NULL;
743 SESS_CERT *sc;
744 EVP_PKEY *pkey=NULL;
745 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
746
747 n=ssl3_get_message(s,
748 SSL3_ST_CR_CERT_A,
749 SSL3_ST_CR_CERT_B,
750 -1,
751 s->max_cert_list,
752 &ok);
753
754 if (!ok) return((int)n);
755
756 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
757 {
758 s->s3->tmp.reuse_message=1;
759 return(1);
760 }
761
762 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
763 {
764 al=SSL_AD_UNEXPECTED_MESSAGE;
765 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
766 goto f_err;
767 }
768 d=p=(unsigned char *)s->init_msg;
769
770 if ((sk=sk_X509_new_null()) == NULL)
771 {
772 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
773 goto err;
774 }
775
776 n2l3(p,llen);
777 if (llen+3 != n)
778 {
779 al=SSL_AD_DECODE_ERROR;
780 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
781 goto f_err;
782 }
783 for (nc=0; nc<llen; )
784 {
785 n2l3(p,l);
786 if ((l+nc+3) > llen)
787 {
788 al=SSL_AD_DECODE_ERROR;
789 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
790 goto f_err;
791 }
792
793 q=p;
794 x=d2i_X509(NULL,&q,l);
795 if (x == NULL)
796 {
797 al=SSL_AD_BAD_CERTIFICATE;
798 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
799 goto f_err;
800 }
801 if (q != (p+l))
802 {
803 al=SSL_AD_DECODE_ERROR;
804 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
805 goto f_err;
806 }
807 if (!sk_X509_push(sk,x))
808 {
809 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
810 goto err;
811 }
812 x=NULL;
813 nc+=l+3;
814 p=q;
815 }
816
817 i=ssl_verify_cert_chain(s,sk);
818 if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)
819#ifndef OPENSSL_NO_KRB5
820 && (s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK))
821 != (SSL_aKRB5|SSL_kKRB5)
822#endif /* OPENSSL_NO_KRB5 */
823 )
824 {
825 al=ssl_verify_alarm_type(s->verify_result);
826 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
827 goto f_err;
828 }
829 ERR_clear_error(); /* but we keep s->verify_result */
830
831 sc=ssl_sess_cert_new();
832 if (sc == NULL) goto err;
833
834 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
835 s->session->sess_cert=sc;
836
837 sc->cert_chain=sk;
838 /* Inconsistency alert: cert_chain does include the peer's
839 * certificate, which we don't include in s3_srvr.c */
840 x=sk_X509_value(sk,0);
841 sk=NULL;
842 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
843
844 pkey=X509_get_pubkey(x);
845
846 /* VRS: allow null cert if auth == KRB5 */
847 need_cert = ((s->s3->tmp.new_cipher->algorithms
848 & (SSL_MKEY_MASK|SSL_AUTH_MASK))
849 == (SSL_aKRB5|SSL_kKRB5))? 0: 1;
850
851#ifdef KSSL_DEBUG
852 printf("pkey,x = %p, %p\n", pkey,x);
853 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
854 printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name,
855 s->s3->tmp.new_cipher->algorithms, need_cert);
856#endif /* KSSL_DEBUG */
857
858 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
859 {
860 x=NULL;
861 al=SSL3_AL_FATAL;
862 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
863 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
864 goto f_err;
865 }
866
867 i=ssl_cert_type(x,pkey);
868 if (need_cert && i < 0)
869 {
870 x=NULL;
871 al=SSL3_AL_FATAL;
872 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
873 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
874 goto f_err;
875 }
876
877 if (need_cert)
878 {
879 sc->peer_cert_type=i;
880 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
881 /* Why would the following ever happen?
882 * We just created sc a couple of lines ago. */
883 if (sc->peer_pkeys[i].x509 != NULL)
884 X509_free(sc->peer_pkeys[i].x509);
885 sc->peer_pkeys[i].x509=x;
886 sc->peer_key= &(sc->peer_pkeys[i]);
887
888 if (s->session->peer != NULL)
889 X509_free(s->session->peer);
890 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
891 s->session->peer=x;
892 }
893 else
894 {
895 sc->peer_cert_type=i;
896 sc->peer_key= NULL;
897
898 if (s->session->peer != NULL)
899 X509_free(s->session->peer);
900 s->session->peer=NULL;
901 }
902 s->session->verify_result = s->verify_result;
903
904 x=NULL;
905 ret=1;
906
907 if (0)
908 {
909f_err:
910 ssl3_send_alert(s,SSL3_AL_FATAL,al);
911 }
912err:
913 EVP_PKEY_free(pkey);
914 X509_free(x);
915 sk_X509_pop_free(sk,X509_free);
916 return(ret);
917 }
918
919static int ssl3_get_key_exchange(SSL *s)
920 {
921#ifndef OPENSSL_NO_RSA
922 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
923#endif
924 EVP_MD_CTX md_ctx;
925 unsigned char *param,*p;
926 int al,i,j,param_len,ok;
927 long n,alg;
928 EVP_PKEY *pkey=NULL;
929#ifndef OPENSSL_NO_RSA
930 RSA *rsa=NULL;
931#endif
932#ifndef OPENSSL_NO_DH
933 DH *dh=NULL;
934#endif
935
936 /* use same message size as in ssl3_get_certificate_request()
937 * as ServerKeyExchange message may be skipped */
938 n=ssl3_get_message(s,
939 SSL3_ST_CR_KEY_EXCH_A,
940 SSL3_ST_CR_KEY_EXCH_B,
941 -1,
942 s->max_cert_list,
943 &ok);
944
945 if (!ok) return((int)n);
946
947 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
948 {
949 s->s3->tmp.reuse_message=1;
950 return(1);
951 }
952
953 param=p=(unsigned char *)s->init_msg;
954
955 if (s->session->sess_cert != NULL)
956 {
957#ifndef OPENSSL_NO_RSA
958 if (s->session->sess_cert->peer_rsa_tmp != NULL)
959 {
960 RSA_free(s->session->sess_cert->peer_rsa_tmp);
961 s->session->sess_cert->peer_rsa_tmp=NULL;
962 }
963#endif
964#ifndef OPENSSL_NO_DH
965 if (s->session->sess_cert->peer_dh_tmp)
966 {
967 DH_free(s->session->sess_cert->peer_dh_tmp);
968 s->session->sess_cert->peer_dh_tmp=NULL;
969 }
970#endif
971 }
972 else
973 {
974 s->session->sess_cert=ssl_sess_cert_new();
975 }
976
977 param_len=0;
978 alg=s->s3->tmp.new_cipher->algorithms;
979 EVP_MD_CTX_init(&md_ctx);
980
981#ifndef OPENSSL_NO_RSA
982 if (alg & SSL_kRSA)
983 {
984 if ((rsa=RSA_new()) == NULL)
985 {
986 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
987 goto err;
988 }
989 n2s(p,i);
990 param_len=i+2;
991 if (param_len > n)
992 {
993 al=SSL_AD_DECODE_ERROR;
994 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
995 goto f_err;
996 }
997 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
998 {
999 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1000 goto err;
1001 }
1002 p+=i;
1003
1004 n2s(p,i);
1005 param_len+=i+2;
1006 if (param_len > n)
1007 {
1008 al=SSL_AD_DECODE_ERROR;
1009 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
1010 goto f_err;
1011 }
1012 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
1013 {
1014 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1015 goto err;
1016 }
1017 p+=i;
1018 n-=param_len;
1019
1020 /* this should be because we are using an export cipher */
1021 if (alg & SSL_aRSA)
1022 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1023 else
1024 {
1025 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1026 goto err;
1027 }
1028 s->session->sess_cert->peer_rsa_tmp=rsa;
1029 rsa=NULL;
1030 }
1031#else /* OPENSSL_NO_RSA */
1032 if (0)
1033 ;
1034#endif
1035#ifndef OPENSSL_NO_DH
1036 else if (alg & SSL_kEDH)
1037 {
1038 if ((dh=DH_new()) == NULL)
1039 {
1040 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
1041 goto err;
1042 }
1043 n2s(p,i);
1044 param_len=i+2;
1045 if (param_len > n)
1046 {
1047 al=SSL_AD_DECODE_ERROR;
1048 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
1049 goto f_err;
1050 }
1051 if (!(dh->p=BN_bin2bn(p,i,NULL)))
1052 {
1053 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1054 goto err;
1055 }
1056 p+=i;
1057
1058 n2s(p,i);
1059 param_len+=i+2;
1060 if (param_len > n)
1061 {
1062 al=SSL_AD_DECODE_ERROR;
1063 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
1064 goto f_err;
1065 }
1066 if (!(dh->g=BN_bin2bn(p,i,NULL)))
1067 {
1068 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1069 goto err;
1070 }
1071 p+=i;
1072
1073 n2s(p,i);
1074 param_len+=i+2;
1075 if (param_len > n)
1076 {
1077 al=SSL_AD_DECODE_ERROR;
1078 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
1079 goto f_err;
1080 }
1081 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
1082 {
1083 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1084 goto err;
1085 }
1086 p+=i;
1087 n-=param_len;
1088
1089#ifndef OPENSSL_NO_RSA
1090 if (alg & SSL_aRSA)
1091 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1092#else
1093 if (0)
1094 ;
1095#endif
1096#ifndef OPENSSL_NO_DSA
1097 else if (alg & SSL_aDSS)
1098 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1099#endif
1100 /* else anonymous DH, so no certificate or pkey. */
1101
1102 s->session->sess_cert->peer_dh_tmp=dh;
1103 dh=NULL;
1104 }
1105 else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
1106 {
1107 al=SSL_AD_ILLEGAL_PARAMETER;
1108 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1109 goto f_err;
1110 }
1111#endif /* !OPENSSL_NO_DH */
1112 if (alg & SSL_aFZA)
1113 {
1114 al=SSL_AD_HANDSHAKE_FAILURE;
1115 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1116 goto f_err;
1117 }
1118
1119
1120 /* p points to the next byte, there are 'n' bytes left */
1121
1122
1123 /* if it was signed, check the signature */
1124 if (pkey != NULL)
1125 {
1126 n2s(p,i);
1127 n-=2;
1128 j=EVP_PKEY_size(pkey);
1129
1130 if ((i != n) || (n > j) || (n <= 0))
1131 {
1132 /* wrong packet length */
1133 al=SSL_AD_DECODE_ERROR;
1134 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1135 goto f_err;
1136 }
1137
1138#ifndef OPENSSL_NO_RSA
1139 if (pkey->type == EVP_PKEY_RSA)
1140 {
1141 int num;
1142
1143 j=0;
1144 q=md_buf;
1145 for (num=2; num > 0; num--)
1146 {
1147 EVP_DigestInit_ex(&md_ctx,(num == 2)
1148 ?s->ctx->md5:s->ctx->sha1, NULL);
1149 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1150 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1151 EVP_DigestUpdate(&md_ctx,param,param_len);
1152 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
1153 q+=i;
1154 j+=i;
1155 }
1156 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
1157 pkey->pkey.rsa);
1158 if (i < 0)
1159 {
1160 al=SSL_AD_DECRYPT_ERROR;
1161 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1162 goto f_err;
1163 }
1164 if (i == 0)
1165 {
1166 /* bad signature */
1167 al=SSL_AD_DECRYPT_ERROR;
1168 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1169 goto f_err;
1170 }
1171 }
1172 else
1173#endif
1174#ifndef OPENSSL_NO_DSA
1175 if (pkey->type == EVP_PKEY_DSA)
1176 {
1177 /* lets do DSS */
1178 EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL);
1179 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1180 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1181 EVP_VerifyUpdate(&md_ctx,param,param_len);
1182 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
1183 {
1184 /* bad signature */
1185 al=SSL_AD_DECRYPT_ERROR;
1186 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1187 goto f_err;
1188 }
1189 }
1190 else
1191#endif
1192 {
1193 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1194 goto err;
1195 }
1196 }
1197 else
1198 {
1199 /* still data left over */
1200 if (!(alg & SSL_aNULL))
1201 {
1202 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1203 goto err;
1204 }
1205 if (n != 0)
1206 {
1207 al=SSL_AD_DECODE_ERROR;
1208 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1209 goto f_err;
1210 }
1211 }
1212 EVP_PKEY_free(pkey);
1213 EVP_MD_CTX_cleanup(&md_ctx);
1214 return(1);
1215f_err:
1216 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1217err:
1218 EVP_PKEY_free(pkey);
1219#ifndef OPENSSL_NO_RSA
1220 if (rsa != NULL)
1221 RSA_free(rsa);
1222#endif
1223#ifndef OPENSSL_NO_DH
1224 if (dh != NULL)
1225 DH_free(dh);
1226#endif
1227 EVP_MD_CTX_cleanup(&md_ctx);
1228 return(-1);
1229 }
1230
1231static int ssl3_get_certificate_request(SSL *s)
1232 {
1233 int ok,ret=0;
1234 unsigned long n,nc,l;
1235 unsigned int llen,ctype_num,i;
1236 X509_NAME *xn=NULL;
1237 unsigned char *p,*d,*q;
1238 STACK_OF(X509_NAME) *ca_sk=NULL;
1239
1240 n=ssl3_get_message(s,
1241 SSL3_ST_CR_CERT_REQ_A,
1242 SSL3_ST_CR_CERT_REQ_B,
1243 -1,
1244 s->max_cert_list,
1245 &ok);
1246
1247 if (!ok) return((int)n);
1248
1249 s->s3->tmp.cert_req=0;
1250
1251 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1252 {
1253 s->s3->tmp.reuse_message=1;
1254 return(1);
1255 }
1256
1257 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1258 {
1259 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1260 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
1261 goto err;
1262 }
1263
1264 /* TLS does not like anon-DH with client cert */
1265 if (s->version > SSL3_VERSION)
1266 {
1267 l=s->s3->tmp.new_cipher->algorithms;
1268 if (l & SSL_aNULL)
1269 {
1270 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1271 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1272 goto err;
1273 }
1274 }
1275
1276 d=p=(unsigned char *)s->init_msg;
1277
1278 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
1279 {
1280 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1281 goto err;
1282 }
1283
1284 /* get the certificate types */
1285 ctype_num= *(p++);
1286 if (ctype_num > SSL3_CT_NUMBER)
1287 ctype_num=SSL3_CT_NUMBER;
1288 for (i=0; i<ctype_num; i++)
1289 s->s3->tmp.ctype[i]= p[i];
1290 p+=ctype_num;
1291
1292 /* get the CA RDNs */
1293 n2s(p,llen);
1294#if 0
1295{
1296FILE *out;
1297out=fopen("/tmp/vsign.der","w");
1298fwrite(p,1,llen,out);
1299fclose(out);
1300}
1301#endif
1302
1303 if ((llen+ctype_num+2+1) != n)
1304 {
1305 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1306 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
1307 goto err;
1308 }
1309
1310 for (nc=0; nc<llen; )
1311 {
1312 n2s(p,l);
1313 if ((l+nc+2) > llen)
1314 {
1315 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1316 goto cont; /* netscape bugs */
1317 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1318 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
1319 goto err;
1320 }
1321
1322 q=p;
1323
1324 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1325 {
1326 /* If netscape tolerance is on, ignore errors */
1327 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
1328 goto cont;
1329 else
1330 {
1331 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1332 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
1333 goto err;
1334 }
1335 }
1336
1337 if (q != (p+l))
1338 {
1339 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1340 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1341 goto err;
1342 }
1343 if (!sk_X509_NAME_push(ca_sk,xn))
1344 {
1345 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1346 goto err;
1347 }
1348
1349 p+=l;
1350 nc+=l+2;
1351 }
1352
1353 if (0)
1354 {
1355cont:
1356 ERR_clear_error();
1357 }
1358
1359 /* we should setup a certificate to return.... */
1360 s->s3->tmp.cert_req=1;
1361 s->s3->tmp.ctype_num=ctype_num;
1362 if (s->s3->tmp.ca_names != NULL)
1363 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1364 s->s3->tmp.ca_names=ca_sk;
1365 ca_sk=NULL;
1366
1367 ret=1;
1368err:
1369 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1370 return(ret);
1371 }
1372
1373static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
1374 {
1375 return(X509_NAME_cmp(*a,*b));
1376 }
1377
1378static int ssl3_get_server_done(SSL *s)
1379 {
1380 int ok,ret=0;
1381 long n;
1382
1383 n=ssl3_get_message(s,
1384 SSL3_ST_CR_SRVR_DONE_A,
1385 SSL3_ST_CR_SRVR_DONE_B,
1386 SSL3_MT_SERVER_DONE,
1387 30, /* should be very small, like 0 :-) */
1388 &ok);
1389
1390 if (!ok) return((int)n);
1391 if (n > 0)
1392 {
1393 /* should contain no data */
1394 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1395 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
1396 return -1;
1397 }
1398 ret=1;
1399 return(ret);
1400 }
1401
1402static int ssl3_send_client_key_exchange(SSL *s)
1403 {
1404 unsigned char *p,*d;
1405 int n;
1406 unsigned long l;
1407#ifndef OPENSSL_NO_RSA
1408 unsigned char *q;
1409 EVP_PKEY *pkey=NULL;
1410#endif
1411#ifndef OPENSSL_NO_KRB5
1412 KSSL_ERR kssl_err;
1413#endif /* OPENSSL_NO_KRB5 */
1414
1415 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1416 {
1417 d=(unsigned char *)s->init_buf->data;
1418 p= &(d[4]);
1419
1420 l=s->s3->tmp.new_cipher->algorithms;
1421
1422 /* Fool emacs indentation */
1423 if (0) {}
1424#ifndef OPENSSL_NO_RSA
1425 else if (l & SSL_kRSA)
1426 {
1427 RSA *rsa;
1428 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1429
1430 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1431 rsa=s->session->sess_cert->peer_rsa_tmp;
1432 else
1433 {
1434 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1435 if ((pkey == NULL) ||
1436 (pkey->type != EVP_PKEY_RSA) ||
1437 (pkey->pkey.rsa == NULL))
1438 {
1439 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1440 goto err;
1441 }
1442 rsa=pkey->pkey.rsa;
1443 EVP_PKEY_free(pkey);
1444 }
1445
1446 tmp_buf[0]=s->client_version>>8;
1447 tmp_buf[1]=s->client_version&0xff;
1448 if (RAND_bytes(&(tmp_buf[2]),SSL_MAX_MASTER_KEY_LENGTH-2) <= 0)
1449 goto err;
1450
1451 s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
1452
1453 q=p;
1454 /* Fix buf for TLS and beyond */
1455 if (s->version > SSL3_VERSION)
1456 p+=2;
1457 n=RSA_public_encrypt(SSL_MAX_MASTER_KEY_LENGTH,
1458 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
1459#ifdef PKCS1_CHECK
1460 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
1461 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
1462#endif
1463 if (n <= 0)
1464 {
1465 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
1466 goto err;
1467 }
1468
1469 /* Fix buf for TLS and beyond */
1470 if (s->version > SSL3_VERSION)
1471 {
1472 s2n(n,q);
1473 n+=2;
1474 }
1475
1476 s->session->master_key_length=
1477 s->method->ssl3_enc->generate_master_secret(s,
1478 s->session->master_key,
1479 tmp_buf,SSL_MAX_MASTER_KEY_LENGTH);
1480 memset(tmp_buf,0,SSL_MAX_MASTER_KEY_LENGTH);
1481 }
1482#endif
1483#ifndef OPENSSL_NO_KRB5
1484 else if (l & SSL_kKRB5)
1485 {
1486 krb5_error_code krb5rc;
1487 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1488 /* krb5_data krb5_ap_req; */
1489 krb5_data *enc_ticket;
1490 krb5_data authenticator, *authp = NULL;
1491 EVP_CIPHER_CTX ciph_ctx;
1492 EVP_CIPHER *enc = NULL;
1493 unsigned char iv[EVP_MAX_IV_LENGTH];
1494 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1495 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
1496 + EVP_MAX_IV_LENGTH];
1497 int padl, outl = sizeof(epms);
1498
1499 EVP_CIPHER_CTX_init(&ciph_ctx);
1500
1501#ifdef KSSL_DEBUG
1502 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
1503 l, SSL_kKRB5);
1504#endif /* KSSL_DEBUG */
1505
1506 authp = NULL;
1507#ifdef KRB5SENDAUTH
1508 if (KRB5SENDAUTH) authp = &authenticator;
1509#endif /* KRB5SENDAUTH */
1510
1511 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
1512 &kssl_err);
1513 enc = kssl_map_enc(kssl_ctx->enctype);
1514 if (enc == NULL)
1515 goto err;
1516#ifdef KSSL_DEBUG
1517 {
1518 printf("kssl_cget_tkt rtn %d\n", krb5rc);
1519 if (krb5rc && kssl_err.text)
1520 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
1521 }
1522#endif /* KSSL_DEBUG */
1523
1524 if (krb5rc)
1525 {
1526 ssl3_send_alert(s,SSL3_AL_FATAL,
1527 SSL_AD_HANDSHAKE_FAILURE);
1528 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1529 kssl_err.reason);
1530 goto err;
1531 }
1532
1533 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
1534 ** in place of RFC 2712 KerberosWrapper, as in:
1535 **
1536 ** Send ticket (copy to *p, set n = length)
1537 ** n = krb5_ap_req.length;
1538 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
1539 ** if (krb5_ap_req.data)
1540 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
1541 **
1542 ** Now using real RFC 2712 KerberosWrapper
1543 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
1544 ** Note: 2712 "opaque" types are here replaced
1545 ** with a 2-byte length followed by the value.
1546 ** Example:
1547 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
1548 ** Where "xx xx" = length bytes. Shown here with
1549 ** optional authenticator omitted.
1550 */
1551
1552 /* KerberosWrapper.Ticket */
1553 s2n(enc_ticket->length,p);
1554 memcpy(p, enc_ticket->data, enc_ticket->length);
1555 p+= enc_ticket->length;
1556 n = enc_ticket->length + 2;
1557
1558 /* KerberosWrapper.Authenticator */
1559 if (authp && authp->length)
1560 {
1561 s2n(authp->length,p);
1562 memcpy(p, authp->data, authp->length);
1563 p+= authp->length;
1564 n+= authp->length + 2;
1565
1566 free(authp->data);
1567 authp->data = NULL;
1568 authp->length = 0;
1569 }
1570 else
1571 {
1572 s2n(0,p);/* null authenticator length */
1573 n+=2;
1574 }
1575
1576 if (RAND_bytes(tmp_buf,SSL_MAX_MASTER_KEY_LENGTH) <= 0)
1577 goto err;
1578
1579 /* 20010420 VRS. Tried it this way; failed.
1580 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
1581 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
1582 ** kssl_ctx->length);
1583 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
1584 */
1585
1586 memset(iv, 0, EVP_MAX_IV_LENGTH); /* per RFC 1510 */
1587 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
1588 kssl_ctx->key,iv);
1589 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
1590 SSL_MAX_MASTER_KEY_LENGTH);
1591 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
1592 outl += padl;
1593 if (outl > sizeof epms)
1594 {
1595 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1596 goto err;
1597 }
1598 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
1599
1600 /* KerberosWrapper.EncryptedPreMasterSecret */
1601 s2n(outl,p);
1602 memcpy(p, epms, outl);
1603 p+=outl;
1604 n+=outl + 2;
1605
1606 s->session->master_key_length=
1607 s->method->ssl3_enc->generate_master_secret(s,
1608 s->session->master_key,
1609 tmp_buf, SSL_MAX_MASTER_KEY_LENGTH);
1610
1611 memset(tmp_buf, 0, SSL_MAX_MASTER_KEY_LENGTH);
1612 memset(epms, 0, outl);
1613 }
1614#endif
1615#ifndef OPENSSL_NO_DH
1616 else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1617 {
1618 DH *dh_srvr,*dh_clnt;
1619
1620 if (s->session->sess_cert->peer_dh_tmp != NULL)
1621 dh_srvr=s->session->sess_cert->peer_dh_tmp;
1622 else
1623 {
1624 /* we get them from the cert */
1625 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1626 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1627 goto err;
1628 }
1629
1630 /* generate a new random key */
1631 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
1632 {
1633 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1634 goto err;
1635 }
1636 if (!DH_generate_key(dh_clnt))
1637 {
1638 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1639 goto err;
1640 }
1641
1642 /* use the 'p' output buffer for the DH key, but
1643 * make sure to clear it out afterwards */
1644
1645 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
1646
1647 if (n <= 0)
1648 {
1649 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1650 goto err;
1651 }
1652
1653 /* generate master key from the result */
1654 s->session->master_key_length=
1655 s->method->ssl3_enc->generate_master_secret(s,
1656 s->session->master_key,p,n);
1657 /* clean up */
1658 memset(p,0,n);
1659
1660 /* send off the data */
1661 n=BN_num_bytes(dh_clnt->pub_key);
1662 s2n(n,p);
1663 BN_bn2bin(dh_clnt->pub_key,p);
1664 n+=2;
1665
1666 DH_free(dh_clnt);
1667
1668 /* perhaps clean things up a bit EAY EAY EAY EAY*/
1669 }
1670#endif
1671 else
1672 {
1673 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1674 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1675 goto err;
1676 }
1677
1678 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1679 l2n3(n,d);
1680
1681 s->state=SSL3_ST_CW_KEY_EXCH_B;
1682 /* number of bytes to write */
1683 s->init_num=n+4;
1684 s->init_off=0;
1685 }
1686
1687 /* SSL3_ST_CW_KEY_EXCH_B */
1688 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1689err:
1690 return(-1);
1691 }
1692
1693static int ssl3_send_client_verify(SSL *s)
1694 {
1695 unsigned char *p,*d;
1696 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1697 EVP_PKEY *pkey;
1698#ifndef OPENSSL_NO_RSA
1699 unsigned u=0;
1700#endif
1701 unsigned long n;
1702#ifndef OPENSSL_NO_DSA
1703 int j;
1704#endif
1705
1706 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
1707 {
1708 d=(unsigned char *)s->init_buf->data;
1709 p= &(d[4]);
1710 pkey=s->cert->key->privatekey;
1711
1712 s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
1713 &(data[MD5_DIGEST_LENGTH]));
1714
1715#ifndef OPENSSL_NO_RSA
1716 if (pkey->type == EVP_PKEY_RSA)
1717 {
1718 s->method->ssl3_enc->cert_verify_mac(s,
1719 &(s->s3->finish_dgst1),&(data[0]));
1720 if (RSA_sign(NID_md5_sha1, data,
1721 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1722 &(p[2]), &u, pkey->pkey.rsa) <= 0 )
1723 {
1724 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
1725 goto err;
1726 }
1727 s2n(u,p);
1728 n=u+2;
1729 }
1730 else
1731#endif
1732#ifndef OPENSSL_NO_DSA
1733 if (pkey->type == EVP_PKEY_DSA)
1734 {
1735 if (!DSA_sign(pkey->save_type,
1736 &(data[MD5_DIGEST_LENGTH]),
1737 SHA_DIGEST_LENGTH,&(p[2]),
1738 (unsigned int *)&j,pkey->pkey.dsa))
1739 {
1740 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
1741 goto err;
1742 }
1743 s2n(j,p);
1744 n=j+2;
1745 }
1746 else
1747#endif
1748 {
1749 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
1750 goto err;
1751 }
1752 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
1753 l2n3(n,d);
1754
1755 s->init_num=(int)n+4;
1756 s->init_off=0;
1757 }
1758 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1759err:
1760 return(-1);
1761 }
1762
1763static int ssl3_send_client_certificate(SSL *s)
1764 {
1765 X509 *x509=NULL;
1766 EVP_PKEY *pkey=NULL;
1767 int i;
1768 unsigned long l;
1769
1770 if (s->state == SSL3_ST_CW_CERT_A)
1771 {
1772 if ((s->cert == NULL) ||
1773 (s->cert->key->x509 == NULL) ||
1774 (s->cert->key->privatekey == NULL))
1775 s->state=SSL3_ST_CW_CERT_B;
1776 else
1777 s->state=SSL3_ST_CW_CERT_C;
1778 }
1779
1780 /* We need to get a client cert */
1781 if (s->state == SSL3_ST_CW_CERT_B)
1782 {
1783 /* If we get an error, we need to
1784 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1785 * We then get retied later */
1786 i=0;
1787 if (s->ctx->client_cert_cb != NULL)
1788 i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
1789 if (i < 0)
1790 {
1791 s->rwstate=SSL_X509_LOOKUP;
1792 return(-1);
1793 }
1794 s->rwstate=SSL_NOTHING;
1795 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
1796 {
1797 s->state=SSL3_ST_CW_CERT_B;
1798 if ( !SSL_use_certificate(s,x509) ||
1799 !SSL_use_PrivateKey(s,pkey))
1800 i=0;
1801 }
1802 else if (i == 1)
1803 {
1804 i=0;
1805 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1806 }
1807
1808 if (x509 != NULL) X509_free(x509);
1809 if (pkey != NULL) EVP_PKEY_free(pkey);
1810 if (i == 0)
1811 {
1812 if (s->version == SSL3_VERSION)
1813 {
1814 s->s3->tmp.cert_req=0;
1815 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
1816 return(1);
1817 }
1818 else
1819 {
1820 s->s3->tmp.cert_req=2;
1821 }
1822 }
1823
1824 /* Ok, we have a cert */
1825 s->state=SSL3_ST_CW_CERT_C;
1826 }
1827
1828 if (s->state == SSL3_ST_CW_CERT_C)
1829 {
1830 s->state=SSL3_ST_CW_CERT_D;
1831 l=ssl3_output_cert_chain(s,
1832 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
1833 s->init_num=(int)l;
1834 s->init_off=0;
1835 }
1836 /* SSL3_ST_CW_CERT_D */
1837 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1838 }
1839
1840#define has_bits(i,m) (((i)&(m)) == (m))
1841
1842static int ssl3_check_cert_and_algorithm(SSL *s)
1843 {
1844 int i,idx;
1845 long algs;
1846 EVP_PKEY *pkey=NULL;
1847 SESS_CERT *sc;
1848#ifndef OPENSSL_NO_RSA
1849 RSA *rsa;
1850#endif
1851#ifndef OPENSSL_NO_DH
1852 DH *dh;
1853#endif
1854
1855 sc=s->session->sess_cert;
1856
1857 if (sc == NULL)
1858 {
1859 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
1860 goto err;
1861 }
1862
1863 algs=s->s3->tmp.new_cipher->algorithms;
1864
1865 /* we don't have a certificate */
1866 if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5))
1867 return(1);
1868
1869#ifndef OPENSSL_NO_RSA
1870 rsa=s->session->sess_cert->peer_rsa_tmp;
1871#endif
1872#ifndef OPENSSL_NO_DH
1873 dh=s->session->sess_cert->peer_dh_tmp;
1874#endif
1875
1876 /* This is the passed certificate */
1877
1878 idx=sc->peer_cert_type;
1879 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
1880 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
1881 EVP_PKEY_free(pkey);
1882
1883
1884 /* Check that we have a certificate if we require one */
1885 if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
1886 {
1887 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
1888 goto f_err;
1889 }
1890#ifndef OPENSSL_NO_DSA
1891 else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
1892 {
1893 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
1894 goto f_err;
1895 }
1896#endif
1897#ifndef OPENSSL_NO_RSA
1898 if ((algs & SSL_kRSA) &&
1899 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
1900 {
1901 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
1902 goto f_err;
1903 }
1904#endif
1905#ifndef OPENSSL_NO_DH
1906 if ((algs & SSL_kEDH) &&
1907 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
1908 {
1909 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
1910 goto f_err;
1911 }
1912 else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
1913 {
1914 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
1915 goto f_err;
1916 }
1917#ifndef OPENSSL_NO_DSA
1918 else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
1919 {
1920 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
1921 goto f_err;
1922 }
1923#endif
1924#endif
1925
1926 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
1927 {
1928#ifndef OPENSSL_NO_RSA
1929 if (algs & SSL_kRSA)
1930 {
1931 if (rsa == NULL
1932 || RSA_size(rsa) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
1933 {
1934 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
1935 goto f_err;
1936 }
1937 }
1938 else
1939#endif
1940#ifndef OPENSSL_NO_DH
1941 if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1942 {
1943 if (dh == NULL
1944 || DH_size(dh) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
1945 {
1946 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
1947 goto f_err;
1948 }
1949 }
1950 else
1951#endif
1952 {
1953 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1954 goto f_err;
1955 }
1956 }
1957 return(1);
1958f_err:
1959 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1960err:
1961 return(0);
1962 }
1963