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.c1966
1 files changed, 1966 insertions, 0 deletions
diff --git a/src/lib/libssl/s3_clnt.c b/src/lib/libssl/s3_clnt.c
new file mode 100644
index 0000000000..27df7a5a64
--- /dev/null
+++ b/src/lib/libssl/s3_clnt.c
@@ -0,0 +1,1966 @@
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 die(i <= sizeof s->session->session_id);
549 memcpy(p,s->session->session_id,i);
550 p+=i;
551 }
552
553 /* Ciphers supported */
554 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]));
555 if (i == 0)
556 {
557 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
558 goto err;
559 }
560 s2n(i,p);
561 p+=i;
562
563 /* COMPRESSION */
564 if (s->ctx->comp_methods == NULL)
565 j=0;
566 else
567 j=sk_SSL_COMP_num(s->ctx->comp_methods);
568 *(p++)=1+j;
569 for (i=0; i<j; i++)
570 {
571 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
572 *(p++)=comp->id;
573 }
574 *(p++)=0; /* Add the NULL method */
575
576 l=(p-d);
577 d=buf;
578 *(d++)=SSL3_MT_CLIENT_HELLO;
579 l2n3(l,d);
580
581 s->state=SSL3_ST_CW_CLNT_HELLO_B;
582 /* number of bytes to write */
583 s->init_num=p-buf;
584 s->init_off=0;
585 }
586
587 /* SSL3_ST_CW_CLNT_HELLO_B */
588 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
589err:
590 return(-1);
591 }
592
593static int ssl3_get_server_hello(SSL *s)
594 {
595 STACK_OF(SSL_CIPHER) *sk;
596 SSL_CIPHER *c;
597 unsigned char *p,*d;
598 int i,al,ok;
599 unsigned int j;
600 long n;
601 SSL_COMP *comp;
602
603 n=ssl3_get_message(s,
604 SSL3_ST_CR_SRVR_HELLO_A,
605 SSL3_ST_CR_SRVR_HELLO_B,
606 SSL3_MT_SERVER_HELLO,
607 300, /* ?? */
608 &ok);
609
610 if (!ok) return((int)n);
611 d=p=(unsigned char *)s->init_msg;
612
613 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
614 {
615 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
616 s->version=(s->version&0xff00)|p[1];
617 al=SSL_AD_PROTOCOL_VERSION;
618 goto f_err;
619 }
620 p+=2;
621
622 /* load the server hello data */
623 /* load the server random */
624 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
625 p+=SSL3_RANDOM_SIZE;
626
627 /* get the session-id */
628 j= *(p++);
629
630 if(j > sizeof s->session->session_id)
631 {
632 al=SSL_AD_ILLEGAL_PARAMETER;
633 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
634 SSL_R_SSL3_SESSION_ID_TOO_LONG);
635 goto f_err;
636 }
637
638 if ((j != 0) && (j != SSL3_SESSION_ID_SIZE))
639 {
640 /* SSLref returns 16 :-( */
641 if (j < SSL2_SSL_SESSION_ID_LENGTH)
642 {
643 al=SSL_AD_ILLEGAL_PARAMETER;
644 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_SHORT);
645 goto f_err;
646 }
647 }
648 if (j != 0 && j == s->session->session_id_length
649 && memcmp(p,s->session->session_id,j) == 0)
650 {
651 if(s->sid_ctx_length != s->session->sid_ctx_length
652 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
653 {
654 al=SSL_AD_ILLEGAL_PARAMETER;
655 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
656 goto f_err;
657 }
658 s->hit=1;
659 }
660 else /* a miss or crap from the other end */
661 {
662 /* If we were trying for session-id reuse, make a new
663 * SSL_SESSION so we don't stuff up other people */
664 s->hit=0;
665 if (s->session->session_id_length > 0)
666 {
667 if (!ssl_get_new_session(s,0))
668 {
669 al=SSL_AD_INTERNAL_ERROR;
670 goto f_err;
671 }
672 }
673 s->session->session_id_length=j;
674 memcpy(s->session->session_id,p,j); /* j could be 0 */
675 }
676 p+=j;
677 c=ssl_get_cipher_by_char(s,p);
678 if (c == NULL)
679 {
680 /* unknown cipher */
681 al=SSL_AD_ILLEGAL_PARAMETER;
682 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
683 goto f_err;
684 }
685 p+=ssl_put_cipher_by_char(s,NULL,NULL);
686
687 sk=ssl_get_ciphers_by_id(s);
688 i=sk_SSL_CIPHER_find(sk,c);
689 if (i < 0)
690 {
691 /* we did not say we would use this cipher */
692 al=SSL_AD_ILLEGAL_PARAMETER;
693 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
694 goto f_err;
695 }
696
697 if (s->hit && (s->session->cipher != c))
698 {
699 if (!(s->options &
700 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
701 {
702 al=SSL_AD_ILLEGAL_PARAMETER;
703 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
704 goto f_err;
705 }
706 }
707 s->s3->tmp.new_cipher=c;
708
709 /* lets get the compression algorithm */
710 /* COMPRESSION */
711 j= *(p++);
712 if (j == 0)
713 comp=NULL;
714 else
715 comp=ssl3_comp_find(s->ctx->comp_methods,j);
716
717 if ((j != 0) && (comp == NULL))
718 {
719 al=SSL_AD_ILLEGAL_PARAMETER;
720 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
721 goto f_err;
722 }
723 else
724 {
725 s->s3->tmp.new_compression=comp;
726 }
727
728 if (p != (d+n))
729 {
730 /* wrong packet length */
731 al=SSL_AD_DECODE_ERROR;
732 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
733 goto err;
734 }
735
736 return(1);
737f_err:
738 ssl3_send_alert(s,SSL3_AL_FATAL,al);
739err:
740 return(-1);
741 }
742
743static int ssl3_get_server_certificate(SSL *s)
744 {
745 int al,i,ok,ret= -1;
746 unsigned long n,nc,llen,l;
747 X509 *x=NULL;
748 unsigned char *p,*d,*q;
749 STACK_OF(X509) *sk=NULL;
750 SESS_CERT *sc;
751 EVP_PKEY *pkey=NULL;
752 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
753
754 n=ssl3_get_message(s,
755 SSL3_ST_CR_CERT_A,
756 SSL3_ST_CR_CERT_B,
757 -1,
758 s->max_cert_list,
759 &ok);
760
761 if (!ok) return((int)n);
762
763 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
764 {
765 s->s3->tmp.reuse_message=1;
766 return(1);
767 }
768
769 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
770 {
771 al=SSL_AD_UNEXPECTED_MESSAGE;
772 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
773 goto f_err;
774 }
775 d=p=(unsigned char *)s->init_msg;
776
777 if ((sk=sk_X509_new_null()) == NULL)
778 {
779 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
780 goto err;
781 }
782
783 n2l3(p,llen);
784 if (llen+3 != n)
785 {
786 al=SSL_AD_DECODE_ERROR;
787 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
788 goto f_err;
789 }
790 for (nc=0; nc<llen; )
791 {
792 n2l3(p,l);
793 if ((l+nc+3) > llen)
794 {
795 al=SSL_AD_DECODE_ERROR;
796 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
797 goto f_err;
798 }
799
800 q=p;
801 x=d2i_X509(NULL,&q,l);
802 if (x == NULL)
803 {
804 al=SSL_AD_BAD_CERTIFICATE;
805 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
806 goto f_err;
807 }
808 if (q != (p+l))
809 {
810 al=SSL_AD_DECODE_ERROR;
811 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
812 goto f_err;
813 }
814 if (!sk_X509_push(sk,x))
815 {
816 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
817 goto err;
818 }
819 x=NULL;
820 nc+=l+3;
821 p=q;
822 }
823
824 i=ssl_verify_cert_chain(s,sk);
825 if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)
826#ifndef OPENSSL_NO_KRB5
827 && (s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK))
828 != (SSL_aKRB5|SSL_kKRB5)
829#endif /* OPENSSL_NO_KRB5 */
830 )
831 {
832 al=ssl_verify_alarm_type(s->verify_result);
833 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
834 goto f_err;
835 }
836 ERR_clear_error(); /* but we keep s->verify_result */
837
838 sc=ssl_sess_cert_new();
839 if (sc == NULL) goto err;
840
841 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
842 s->session->sess_cert=sc;
843
844 sc->cert_chain=sk;
845 /* Inconsistency alert: cert_chain does include the peer's
846 * certificate, which we don't include in s3_srvr.c */
847 x=sk_X509_value(sk,0);
848 sk=NULL;
849 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
850
851 pkey=X509_get_pubkey(x);
852
853 /* VRS: allow null cert if auth == KRB5 */
854 need_cert = ((s->s3->tmp.new_cipher->algorithms
855 & (SSL_MKEY_MASK|SSL_AUTH_MASK))
856 == (SSL_aKRB5|SSL_kKRB5))? 0: 1;
857
858#ifdef KSSL_DEBUG
859 printf("pkey,x = %p, %p\n", pkey,x);
860 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
861 printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name,
862 s->s3->tmp.new_cipher->algorithms, need_cert);
863#endif /* KSSL_DEBUG */
864
865 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
866 {
867 x=NULL;
868 al=SSL3_AL_FATAL;
869 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
870 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
871 goto f_err;
872 }
873
874 i=ssl_cert_type(x,pkey);
875 if (need_cert && i < 0)
876 {
877 x=NULL;
878 al=SSL3_AL_FATAL;
879 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
880 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
881 goto f_err;
882 }
883
884 if (need_cert)
885 {
886 sc->peer_cert_type=i;
887 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
888 /* Why would the following ever happen?
889 * We just created sc a couple of lines ago. */
890 if (sc->peer_pkeys[i].x509 != NULL)
891 X509_free(sc->peer_pkeys[i].x509);
892 sc->peer_pkeys[i].x509=x;
893 sc->peer_key= &(sc->peer_pkeys[i]);
894
895 if (s->session->peer != NULL)
896 X509_free(s->session->peer);
897 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
898 s->session->peer=x;
899 }
900 else
901 {
902 sc->peer_cert_type=i;
903 sc->peer_key= NULL;
904
905 if (s->session->peer != NULL)
906 X509_free(s->session->peer);
907 s->session->peer=NULL;
908 }
909 s->session->verify_result = s->verify_result;
910
911 x=NULL;
912 ret=1;
913
914 if (0)
915 {
916f_err:
917 ssl3_send_alert(s,SSL3_AL_FATAL,al);
918 }
919err:
920 EVP_PKEY_free(pkey);
921 X509_free(x);
922 sk_X509_pop_free(sk,X509_free);
923 return(ret);
924 }
925
926static int ssl3_get_key_exchange(SSL *s)
927 {
928#ifndef OPENSSL_NO_RSA
929 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
930#endif
931 EVP_MD_CTX md_ctx;
932 unsigned char *param,*p;
933 int al,i,j,param_len,ok;
934 long n,alg;
935 EVP_PKEY *pkey=NULL;
936#ifndef OPENSSL_NO_RSA
937 RSA *rsa=NULL;
938#endif
939#ifndef OPENSSL_NO_DH
940 DH *dh=NULL;
941#endif
942
943 /* use same message size as in ssl3_get_certificate_request()
944 * as ServerKeyExchange message may be skipped */
945 n=ssl3_get_message(s,
946 SSL3_ST_CR_KEY_EXCH_A,
947 SSL3_ST_CR_KEY_EXCH_B,
948 -1,
949 s->max_cert_list,
950 &ok);
951
952 if (!ok) return((int)n);
953
954 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
955 {
956 s->s3->tmp.reuse_message=1;
957 return(1);
958 }
959
960 param=p=(unsigned char *)s->init_msg;
961
962 if (s->session->sess_cert != NULL)
963 {
964#ifndef OPENSSL_NO_RSA
965 if (s->session->sess_cert->peer_rsa_tmp != NULL)
966 {
967 RSA_free(s->session->sess_cert->peer_rsa_tmp);
968 s->session->sess_cert->peer_rsa_tmp=NULL;
969 }
970#endif
971#ifndef OPENSSL_NO_DH
972 if (s->session->sess_cert->peer_dh_tmp)
973 {
974 DH_free(s->session->sess_cert->peer_dh_tmp);
975 s->session->sess_cert->peer_dh_tmp=NULL;
976 }
977#endif
978 }
979 else
980 {
981 s->session->sess_cert=ssl_sess_cert_new();
982 }
983
984 param_len=0;
985 alg=s->s3->tmp.new_cipher->algorithms;
986 EVP_MD_CTX_init(&md_ctx);
987
988#ifndef OPENSSL_NO_RSA
989 if (alg & SSL_kRSA)
990 {
991 if ((rsa=RSA_new()) == NULL)
992 {
993 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
994 goto err;
995 }
996 n2s(p,i);
997 param_len=i+2;
998 if (param_len > n)
999 {
1000 al=SSL_AD_DECODE_ERROR;
1001 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
1002 goto f_err;
1003 }
1004 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
1005 {
1006 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1007 goto err;
1008 }
1009 p+=i;
1010
1011 n2s(p,i);
1012 param_len+=i+2;
1013 if (param_len > n)
1014 {
1015 al=SSL_AD_DECODE_ERROR;
1016 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
1017 goto f_err;
1018 }
1019 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
1020 {
1021 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1022 goto err;
1023 }
1024 p+=i;
1025 n-=param_len;
1026
1027 /* this should be because we are using an export cipher */
1028 if (alg & SSL_aRSA)
1029 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1030 else
1031 {
1032 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1033 goto err;
1034 }
1035 s->session->sess_cert->peer_rsa_tmp=rsa;
1036 rsa=NULL;
1037 }
1038#else /* OPENSSL_NO_RSA */
1039 if (0)
1040 ;
1041#endif
1042#ifndef OPENSSL_NO_DH
1043 else if (alg & SSL_kEDH)
1044 {
1045 if ((dh=DH_new()) == NULL)
1046 {
1047 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
1048 goto err;
1049 }
1050 n2s(p,i);
1051 param_len=i+2;
1052 if (param_len > n)
1053 {
1054 al=SSL_AD_DECODE_ERROR;
1055 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
1056 goto f_err;
1057 }
1058 if (!(dh->p=BN_bin2bn(p,i,NULL)))
1059 {
1060 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1061 goto err;
1062 }
1063 p+=i;
1064
1065 n2s(p,i);
1066 param_len+=i+2;
1067 if (param_len > n)
1068 {
1069 al=SSL_AD_DECODE_ERROR;
1070 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
1071 goto f_err;
1072 }
1073 if (!(dh->g=BN_bin2bn(p,i,NULL)))
1074 {
1075 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1076 goto err;
1077 }
1078 p+=i;
1079
1080 n2s(p,i);
1081 param_len+=i+2;
1082 if (param_len > n)
1083 {
1084 al=SSL_AD_DECODE_ERROR;
1085 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
1086 goto f_err;
1087 }
1088 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
1089 {
1090 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
1091 goto err;
1092 }
1093 p+=i;
1094 n-=param_len;
1095
1096#ifndef OPENSSL_NO_RSA
1097 if (alg & SSL_aRSA)
1098 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1099#else
1100 if (0)
1101 ;
1102#endif
1103#ifndef OPENSSL_NO_DSA
1104 else if (alg & SSL_aDSS)
1105 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
1106#endif
1107 /* else anonymous DH, so no certificate or pkey. */
1108
1109 s->session->sess_cert->peer_dh_tmp=dh;
1110 dh=NULL;
1111 }
1112 else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
1113 {
1114 al=SSL_AD_ILLEGAL_PARAMETER;
1115 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1116 goto f_err;
1117 }
1118#endif /* !OPENSSL_NO_DH */
1119 if (alg & SSL_aFZA)
1120 {
1121 al=SSL_AD_HANDSHAKE_FAILURE;
1122 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1123 goto f_err;
1124 }
1125
1126
1127 /* p points to the next byte, there are 'n' bytes left */
1128
1129
1130 /* if it was signed, check the signature */
1131 if (pkey != NULL)
1132 {
1133 n2s(p,i);
1134 n-=2;
1135 j=EVP_PKEY_size(pkey);
1136
1137 if ((i != n) || (n > j) || (n <= 0))
1138 {
1139 /* wrong packet length */
1140 al=SSL_AD_DECODE_ERROR;
1141 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1142 goto f_err;
1143 }
1144
1145#ifndef OPENSSL_NO_RSA
1146 if (pkey->type == EVP_PKEY_RSA)
1147 {
1148 int num;
1149
1150 j=0;
1151 q=md_buf;
1152 for (num=2; num > 0; num--)
1153 {
1154 EVP_DigestInit_ex(&md_ctx,(num == 2)
1155 ?s->ctx->md5:s->ctx->sha1, NULL);
1156 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1157 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1158 EVP_DigestUpdate(&md_ctx,param,param_len);
1159 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
1160 q+=i;
1161 j+=i;
1162 }
1163 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
1164 pkey->pkey.rsa);
1165 if (i < 0)
1166 {
1167 al=SSL_AD_DECRYPT_ERROR;
1168 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1169 goto f_err;
1170 }
1171 if (i == 0)
1172 {
1173 /* bad signature */
1174 al=SSL_AD_DECRYPT_ERROR;
1175 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1176 goto f_err;
1177 }
1178 }
1179 else
1180#endif
1181#ifndef OPENSSL_NO_DSA
1182 if (pkey->type == EVP_PKEY_DSA)
1183 {
1184 /* lets do DSS */
1185 EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL);
1186 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1187 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1188 EVP_VerifyUpdate(&md_ctx,param,param_len);
1189 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
1190 {
1191 /* bad signature */
1192 al=SSL_AD_DECRYPT_ERROR;
1193 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1194 goto f_err;
1195 }
1196 }
1197 else
1198#endif
1199 {
1200 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1201 goto err;
1202 }
1203 }
1204 else
1205 {
1206 /* still data left over */
1207 if (!(alg & SSL_aNULL))
1208 {
1209 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1210 goto err;
1211 }
1212 if (n != 0)
1213 {
1214 al=SSL_AD_DECODE_ERROR;
1215 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1216 goto f_err;
1217 }
1218 }
1219 EVP_PKEY_free(pkey);
1220 EVP_MD_CTX_cleanup(&md_ctx);
1221 return(1);
1222f_err:
1223 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1224err:
1225 EVP_PKEY_free(pkey);
1226#ifndef OPENSSL_NO_RSA
1227 if (rsa != NULL)
1228 RSA_free(rsa);
1229#endif
1230#ifndef OPENSSL_NO_DH
1231 if (dh != NULL)
1232 DH_free(dh);
1233#endif
1234 EVP_MD_CTX_cleanup(&md_ctx);
1235 return(-1);
1236 }
1237
1238static int ssl3_get_certificate_request(SSL *s)
1239 {
1240 int ok,ret=0;
1241 unsigned long n,nc,l;
1242 unsigned int llen,ctype_num,i;
1243 X509_NAME *xn=NULL;
1244 unsigned char *p,*d,*q;
1245 STACK_OF(X509_NAME) *ca_sk=NULL;
1246
1247 n=ssl3_get_message(s,
1248 SSL3_ST_CR_CERT_REQ_A,
1249 SSL3_ST_CR_CERT_REQ_B,
1250 -1,
1251 s->max_cert_list,
1252 &ok);
1253
1254 if (!ok) return((int)n);
1255
1256 s->s3->tmp.cert_req=0;
1257
1258 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1259 {
1260 s->s3->tmp.reuse_message=1;
1261 return(1);
1262 }
1263
1264 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1265 {
1266 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1267 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
1268 goto err;
1269 }
1270
1271 /* TLS does not like anon-DH with client cert */
1272 if (s->version > SSL3_VERSION)
1273 {
1274 l=s->s3->tmp.new_cipher->algorithms;
1275 if (l & SSL_aNULL)
1276 {
1277 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1278 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1279 goto err;
1280 }
1281 }
1282
1283 d=p=(unsigned char *)s->init_msg;
1284
1285 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
1286 {
1287 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1288 goto err;
1289 }
1290
1291 /* get the certificate types */
1292 ctype_num= *(p++);
1293 if (ctype_num > SSL3_CT_NUMBER)
1294 ctype_num=SSL3_CT_NUMBER;
1295 for (i=0; i<ctype_num; i++)
1296 s->s3->tmp.ctype[i]= p[i];
1297 p+=ctype_num;
1298
1299 /* get the CA RDNs */
1300 n2s(p,llen);
1301#if 0
1302{
1303FILE *out;
1304out=fopen("/tmp/vsign.der","w");
1305fwrite(p,1,llen,out);
1306fclose(out);
1307}
1308#endif
1309
1310 if ((llen+ctype_num+2+1) != n)
1311 {
1312 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1313 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
1314 goto err;
1315 }
1316
1317 for (nc=0; nc<llen; )
1318 {
1319 n2s(p,l);
1320 if ((l+nc+2) > llen)
1321 {
1322 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1323 goto cont; /* netscape bugs */
1324 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1325 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
1326 goto err;
1327 }
1328
1329 q=p;
1330
1331 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1332 {
1333 /* If netscape tolerance is on, ignore errors */
1334 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
1335 goto cont;
1336 else
1337 {
1338 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1339 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
1340 goto err;
1341 }
1342 }
1343
1344 if (q != (p+l))
1345 {
1346 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1347 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1348 goto err;
1349 }
1350 if (!sk_X509_NAME_push(ca_sk,xn))
1351 {
1352 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1353 goto err;
1354 }
1355
1356 p+=l;
1357 nc+=l+2;
1358 }
1359
1360 if (0)
1361 {
1362cont:
1363 ERR_clear_error();
1364 }
1365
1366 /* we should setup a certificate to return.... */
1367 s->s3->tmp.cert_req=1;
1368 s->s3->tmp.ctype_num=ctype_num;
1369 if (s->s3->tmp.ca_names != NULL)
1370 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1371 s->s3->tmp.ca_names=ca_sk;
1372 ca_sk=NULL;
1373
1374 ret=1;
1375err:
1376 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1377 return(ret);
1378 }
1379
1380static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
1381 {
1382 return(X509_NAME_cmp(*a,*b));
1383 }
1384
1385static int ssl3_get_server_done(SSL *s)
1386 {
1387 int ok,ret=0;
1388 long n;
1389
1390 n=ssl3_get_message(s,
1391 SSL3_ST_CR_SRVR_DONE_A,
1392 SSL3_ST_CR_SRVR_DONE_B,
1393 SSL3_MT_SERVER_DONE,
1394 30, /* should be very small, like 0 :-) */
1395 &ok);
1396
1397 if (!ok) return((int)n);
1398 if (n > 0)
1399 {
1400 /* should contain no data */
1401 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1402 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
1403 return -1;
1404 }
1405 ret=1;
1406 return(ret);
1407 }
1408
1409static int ssl3_send_client_key_exchange(SSL *s)
1410 {
1411 unsigned char *p,*d;
1412 int n;
1413 unsigned long l;
1414#ifndef OPENSSL_NO_RSA
1415 unsigned char *q;
1416 EVP_PKEY *pkey=NULL;
1417#endif
1418#ifndef OPENSSL_NO_KRB5
1419 KSSL_ERR kssl_err;
1420#endif /* OPENSSL_NO_KRB5 */
1421
1422 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1423 {
1424 d=(unsigned char *)s->init_buf->data;
1425 p= &(d[4]);
1426
1427 l=s->s3->tmp.new_cipher->algorithms;
1428
1429 /* Fool emacs indentation */
1430 if (0) {}
1431#ifndef OPENSSL_NO_RSA
1432 else if (l & SSL_kRSA)
1433 {
1434 RSA *rsa;
1435 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1436
1437 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1438 rsa=s->session->sess_cert->peer_rsa_tmp;
1439 else
1440 {
1441 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1442 if ((pkey == NULL) ||
1443 (pkey->type != EVP_PKEY_RSA) ||
1444 (pkey->pkey.rsa == NULL))
1445 {
1446 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1447 goto err;
1448 }
1449 rsa=pkey->pkey.rsa;
1450 EVP_PKEY_free(pkey);
1451 }
1452
1453 tmp_buf[0]=s->client_version>>8;
1454 tmp_buf[1]=s->client_version&0xff;
1455 if (RAND_bytes(&(tmp_buf[2]),SSL_MAX_MASTER_KEY_LENGTH-2) <= 0)
1456 goto err;
1457
1458 s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
1459
1460 q=p;
1461 /* Fix buf for TLS and beyond */
1462 if (s->version > SSL3_VERSION)
1463 p+=2;
1464 n=RSA_public_encrypt(SSL_MAX_MASTER_KEY_LENGTH,
1465 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
1466#ifdef PKCS1_CHECK
1467 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
1468 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
1469#endif
1470 if (n <= 0)
1471 {
1472 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
1473 goto err;
1474 }
1475
1476 /* Fix buf for TLS and beyond */
1477 if (s->version > SSL3_VERSION)
1478 {
1479 s2n(n,q);
1480 n+=2;
1481 }
1482
1483 s->session->master_key_length=
1484 s->method->ssl3_enc->generate_master_secret(s,
1485 s->session->master_key,
1486 tmp_buf,SSL_MAX_MASTER_KEY_LENGTH);
1487 memset(tmp_buf,0,SSL_MAX_MASTER_KEY_LENGTH);
1488 }
1489#endif
1490#ifndef OPENSSL_NO_KRB5
1491 else if (l & SSL_kKRB5)
1492 {
1493 krb5_error_code krb5rc;
1494 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1495 /* krb5_data krb5_ap_req; */
1496 krb5_data *enc_ticket;
1497 krb5_data authenticator, *authp = NULL;
1498 EVP_CIPHER_CTX ciph_ctx;
1499 EVP_CIPHER *enc = NULL;
1500 unsigned char iv[EVP_MAX_IV_LENGTH];
1501 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1502 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH
1503 + EVP_MAX_IV_LENGTH];
1504 int padl, outl = sizeof(epms);
1505
1506 EVP_CIPHER_CTX_init(&ciph_ctx);
1507
1508#ifdef KSSL_DEBUG
1509 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
1510 l, SSL_kKRB5);
1511#endif /* KSSL_DEBUG */
1512
1513 authp = NULL;
1514#ifdef KRB5SENDAUTH
1515 if (KRB5SENDAUTH) authp = &authenticator;
1516#endif /* KRB5SENDAUTH */
1517
1518 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
1519 &kssl_err);
1520 enc = kssl_map_enc(kssl_ctx->enctype);
1521 if (enc == NULL)
1522 goto err;
1523#ifdef KSSL_DEBUG
1524 {
1525 printf("kssl_cget_tkt rtn %d\n", krb5rc);
1526 if (krb5rc && kssl_err.text)
1527 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
1528 }
1529#endif /* KSSL_DEBUG */
1530
1531 if (krb5rc)
1532 {
1533 ssl3_send_alert(s,SSL3_AL_FATAL,
1534 SSL_AD_HANDSHAKE_FAILURE);
1535 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1536 kssl_err.reason);
1537 goto err;
1538 }
1539
1540 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ
1541 ** in place of RFC 2712 KerberosWrapper, as in:
1542 **
1543 ** Send ticket (copy to *p, set n = length)
1544 ** n = krb5_ap_req.length;
1545 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
1546 ** if (krb5_ap_req.data)
1547 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
1548 **
1549 ** Now using real RFC 2712 KerberosWrapper
1550 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
1551 ** Note: 2712 "opaque" types are here replaced
1552 ** with a 2-byte length followed by the value.
1553 ** Example:
1554 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
1555 ** Where "xx xx" = length bytes. Shown here with
1556 ** optional authenticator omitted.
1557 */
1558
1559 /* KerberosWrapper.Ticket */
1560 s2n(enc_ticket->length,p);
1561 memcpy(p, enc_ticket->data, enc_ticket->length);
1562 p+= enc_ticket->length;
1563 n = enc_ticket->length + 2;
1564
1565 /* KerberosWrapper.Authenticator */
1566 if (authp && authp->length)
1567 {
1568 s2n(authp->length,p);
1569 memcpy(p, authp->data, authp->length);
1570 p+= authp->length;
1571 n+= authp->length + 2;
1572
1573 free(authp->data);
1574 authp->data = NULL;
1575 authp->length = 0;
1576 }
1577 else
1578 {
1579 s2n(0,p);/* null authenticator length */
1580 n+=2;
1581 }
1582
1583 if (RAND_bytes(tmp_buf,SSL_MAX_MASTER_KEY_LENGTH) <= 0)
1584 goto err;
1585
1586 /* 20010420 VRS. Tried it this way; failed.
1587 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
1588 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
1589 ** kssl_ctx->length);
1590 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
1591 */
1592
1593 memset(iv, 0, EVP_MAX_IV_LENGTH); /* per RFC 1510 */
1594 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
1595 kssl_ctx->key,iv);
1596 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
1597 SSL_MAX_MASTER_KEY_LENGTH);
1598 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
1599 outl += padl;
1600 die(outl <= sizeof epms);
1601 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
1602
1603 /* KerberosWrapper.EncryptedPreMasterSecret */
1604 s2n(outl,p);
1605 memcpy(p, epms, outl);
1606 p+=outl;
1607 n+=outl + 2;
1608
1609 s->session->master_key_length=
1610 s->method->ssl3_enc->generate_master_secret(s,
1611 s->session->master_key,
1612 tmp_buf, SSL_MAX_MASTER_KEY_LENGTH);
1613
1614 memset(tmp_buf, 0, SSL_MAX_MASTER_KEY_LENGTH);
1615 memset(epms, 0, outl);
1616 }
1617#endif
1618#ifndef OPENSSL_NO_DH
1619 else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1620 {
1621 DH *dh_srvr,*dh_clnt;
1622
1623 if (s->session->sess_cert->peer_dh_tmp != NULL)
1624 dh_srvr=s->session->sess_cert->peer_dh_tmp;
1625 else
1626 {
1627 /* we get them from the cert */
1628 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1629 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1630 goto err;
1631 }
1632
1633 /* generate a new random key */
1634 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
1635 {
1636 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1637 goto err;
1638 }
1639 if (!DH_generate_key(dh_clnt))
1640 {
1641 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1642 goto err;
1643 }
1644
1645 /* use the 'p' output buffer for the DH key, but
1646 * make sure to clear it out afterwards */
1647
1648 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
1649
1650 if (n <= 0)
1651 {
1652 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1653 goto err;
1654 }
1655
1656 /* generate master key from the result */
1657 s->session->master_key_length=
1658 s->method->ssl3_enc->generate_master_secret(s,
1659 s->session->master_key,p,n);
1660 /* clean up */
1661 memset(p,0,n);
1662
1663 /* send off the data */
1664 n=BN_num_bytes(dh_clnt->pub_key);
1665 s2n(n,p);
1666 BN_bn2bin(dh_clnt->pub_key,p);
1667 n+=2;
1668
1669 DH_free(dh_clnt);
1670
1671 /* perhaps clean things up a bit EAY EAY EAY EAY*/
1672 }
1673#endif
1674 else
1675 {
1676 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1677 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
1678 goto err;
1679 }
1680
1681 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1682 l2n3(n,d);
1683
1684 s->state=SSL3_ST_CW_KEY_EXCH_B;
1685 /* number of bytes to write */
1686 s->init_num=n+4;
1687 s->init_off=0;
1688 }
1689
1690 /* SSL3_ST_CW_KEY_EXCH_B */
1691 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1692err:
1693 return(-1);
1694 }
1695
1696static int ssl3_send_client_verify(SSL *s)
1697 {
1698 unsigned char *p,*d;
1699 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1700 EVP_PKEY *pkey;
1701#ifndef OPENSSL_NO_RSA
1702 unsigned u=0;
1703#endif
1704 unsigned long n;
1705#ifndef OPENSSL_NO_DSA
1706 int j;
1707#endif
1708
1709 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
1710 {
1711 d=(unsigned char *)s->init_buf->data;
1712 p= &(d[4]);
1713 pkey=s->cert->key->privatekey;
1714
1715 s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
1716 &(data[MD5_DIGEST_LENGTH]));
1717
1718#ifndef OPENSSL_NO_RSA
1719 if (pkey->type == EVP_PKEY_RSA)
1720 {
1721 s->method->ssl3_enc->cert_verify_mac(s,
1722 &(s->s3->finish_dgst1),&(data[0]));
1723 if (RSA_sign(NID_md5_sha1, data,
1724 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1725 &(p[2]), &u, pkey->pkey.rsa) <= 0 )
1726 {
1727 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
1728 goto err;
1729 }
1730 s2n(u,p);
1731 n=u+2;
1732 }
1733 else
1734#endif
1735#ifndef OPENSSL_NO_DSA
1736 if (pkey->type == EVP_PKEY_DSA)
1737 {
1738 if (!DSA_sign(pkey->save_type,
1739 &(data[MD5_DIGEST_LENGTH]),
1740 SHA_DIGEST_LENGTH,&(p[2]),
1741 (unsigned int *)&j,pkey->pkey.dsa))
1742 {
1743 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
1744 goto err;
1745 }
1746 s2n(j,p);
1747 n=j+2;
1748 }
1749 else
1750#endif
1751 {
1752 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
1753 goto err;
1754 }
1755 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
1756 l2n3(n,d);
1757
1758 s->init_num=(int)n+4;
1759 s->init_off=0;
1760 }
1761 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1762err:
1763 return(-1);
1764 }
1765
1766static int ssl3_send_client_certificate(SSL *s)
1767 {
1768 X509 *x509=NULL;
1769 EVP_PKEY *pkey=NULL;
1770 int i;
1771 unsigned long l;
1772
1773 if (s->state == SSL3_ST_CW_CERT_A)
1774 {
1775 if ((s->cert == NULL) ||
1776 (s->cert->key->x509 == NULL) ||
1777 (s->cert->key->privatekey == NULL))
1778 s->state=SSL3_ST_CW_CERT_B;
1779 else
1780 s->state=SSL3_ST_CW_CERT_C;
1781 }
1782
1783 /* We need to get a client cert */
1784 if (s->state == SSL3_ST_CW_CERT_B)
1785 {
1786 /* If we get an error, we need to
1787 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1788 * We then get retied later */
1789 i=0;
1790 if (s->ctx->client_cert_cb != NULL)
1791 i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
1792 if (i < 0)
1793 {
1794 s->rwstate=SSL_X509_LOOKUP;
1795 return(-1);
1796 }
1797 s->rwstate=SSL_NOTHING;
1798 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
1799 {
1800 s->state=SSL3_ST_CW_CERT_B;
1801 if ( !SSL_use_certificate(s,x509) ||
1802 !SSL_use_PrivateKey(s,pkey))
1803 i=0;
1804 }
1805 else if (i == 1)
1806 {
1807 i=0;
1808 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1809 }
1810
1811 if (x509 != NULL) X509_free(x509);
1812 if (pkey != NULL) EVP_PKEY_free(pkey);
1813 if (i == 0)
1814 {
1815 if (s->version == SSL3_VERSION)
1816 {
1817 s->s3->tmp.cert_req=0;
1818 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
1819 return(1);
1820 }
1821 else
1822 {
1823 s->s3->tmp.cert_req=2;
1824 }
1825 }
1826
1827 /* Ok, we have a cert */
1828 s->state=SSL3_ST_CW_CERT_C;
1829 }
1830
1831 if (s->state == SSL3_ST_CW_CERT_C)
1832 {
1833 s->state=SSL3_ST_CW_CERT_D;
1834 l=ssl3_output_cert_chain(s,
1835 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
1836 s->init_num=(int)l;
1837 s->init_off=0;
1838 }
1839 /* SSL3_ST_CW_CERT_D */
1840 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1841 }
1842
1843#define has_bits(i,m) (((i)&(m)) == (m))
1844
1845static int ssl3_check_cert_and_algorithm(SSL *s)
1846 {
1847 int i,idx;
1848 long algs;
1849 EVP_PKEY *pkey=NULL;
1850 SESS_CERT *sc;
1851#ifndef OPENSSL_NO_RSA
1852 RSA *rsa;
1853#endif
1854#ifndef OPENSSL_NO_DH
1855 DH *dh;
1856#endif
1857
1858 sc=s->session->sess_cert;
1859
1860 if (sc == NULL)
1861 {
1862 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
1863 goto err;
1864 }
1865
1866 algs=s->s3->tmp.new_cipher->algorithms;
1867
1868 /* we don't have a certificate */
1869 if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5))
1870 return(1);
1871
1872#ifndef OPENSSL_NO_RSA
1873 rsa=s->session->sess_cert->peer_rsa_tmp;
1874#endif
1875#ifndef OPENSSL_NO_DH
1876 dh=s->session->sess_cert->peer_dh_tmp;
1877#endif
1878
1879 /* This is the passed certificate */
1880
1881 idx=sc->peer_cert_type;
1882 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
1883 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
1884 EVP_PKEY_free(pkey);
1885
1886
1887 /* Check that we have a certificate if we require one */
1888 if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
1889 {
1890 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
1891 goto f_err;
1892 }
1893#ifndef OPENSSL_NO_DSA
1894 else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
1895 {
1896 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
1897 goto f_err;
1898 }
1899#endif
1900#ifndef OPENSSL_NO_RSA
1901 if ((algs & SSL_kRSA) &&
1902 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
1903 {
1904 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
1905 goto f_err;
1906 }
1907#endif
1908#ifndef OPENSSL_NO_DH
1909 if ((algs & SSL_kEDH) &&
1910 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
1911 {
1912 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
1913 goto f_err;
1914 }
1915 else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
1916 {
1917 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
1918 goto f_err;
1919 }
1920#ifndef OPENSSL_NO_DSA
1921 else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
1922 {
1923 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
1924 goto f_err;
1925 }
1926#endif
1927#endif
1928
1929 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
1930 {
1931#ifndef OPENSSL_NO_RSA
1932 if (algs & SSL_kRSA)
1933 {
1934 if (rsa == NULL
1935 || RSA_size(rsa) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
1936 {
1937 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
1938 goto f_err;
1939 }
1940 }
1941 else
1942#endif
1943#ifndef OPENSSL_NO_DH
1944 if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1945 {
1946 if (dh == NULL
1947 || DH_size(dh) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
1948 {
1949 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
1950 goto f_err;
1951 }
1952 }
1953 else
1954#endif
1955 {
1956 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1957 goto f_err;
1958 }
1959 }
1960 return(1);
1961f_err:
1962 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1963err:
1964 return(0);
1965 }
1966