]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/s3_clnt.c
Import of old SSLeay release: SSLeay 0.8.1b
[thirdparty/openssl.git] / ssl / s3_clnt.c
1 /* ssl/s3_clnt.c */
2 /* Copyright (C) 1995-1997 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 #include <stdio.h>
60 #include "buffer.h"
61 #include "rand.h"
62 #include "objects.h"
63 #include "evp.h"
64 #include "ssl_locl.h"
65
66 #define BREAK break
67 /* SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,ERR_R_MALLOC_FAILURE);
68 * SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
69 * SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,ERR_R_MALLOC_FAILURE);
70 * SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
71 * SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
72 * SSLerr(SSL_F_SSL3_GET_SERVER_DONE,ERR_R_MALLOC_FAILURE);
73 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_SHORT);
74 */
75
76 #ifndef NOPROTO
77 static int ssl3_client_hello(SSL *s);
78 static int ssl3_get_server_hello(SSL *s);
79 static int ssl3_get_certificate_request(SSL *s);
80 static int ca_dn_cmp(X509_NAME **a,X509_NAME **b);
81 static int ssl3_get_server_done(SSL *s);
82 static int ssl3_send_client_verify(SSL *s);
83 static int ssl3_send_client_certificate(SSL *s);
84 static int ssl3_send_client_key_exchange(SSL *s);
85 static int ssl3_get_key_exchange(SSL *s);
86 static int ssl3_get_server_certificate(SSL *s);
87 static int ssl3_check_cert_and_algorithm(SSL *s);
88 #else
89 static int ssl3_client_hello();
90 static int ssl3_get_server_hello();
91 static int ssl3_get_certificate_request();
92 static int ca_dn_cmp();
93 static int ssl3_get_server_done();
94 static int ssl3_send_client_verify();
95 static int ssl3_send_client_certificate();
96 static int ssl3_send_client_key_exchange();
97 static int ssl3_get_key_exchange();
98 static int ssl3_get_server_certificate();
99 static int ssl3_check_cert_and_algorithm();
100 #endif
101
102 static SSL_METHOD *ssl3_get_client_method(ver)
103 int ver;
104 {
105 if (ver == 3)
106 return(SSLv3_client_method());
107 else
108 return(NULL);
109 }
110
111 SSL_METHOD *SSLv3_client_method()
112 {
113 static int init=1;
114 static SSL_METHOD SSLv3_client_data;
115
116 if (init)
117 {
118 init=0;
119 memcpy((char *)&SSLv3_client_data,(char *)sslv3_base_method(),
120 sizeof(SSL_METHOD));
121 SSLv3_client_data.ssl_connect=ssl3_connect;
122 SSLv3_client_data.get_ssl_method=ssl3_get_client_method;
123 }
124 return(&SSLv3_client_data);
125 }
126
127 int ssl3_connect(s)
128 SSL *s;
129 {
130 BUF_MEM *buf;
131 unsigned long Time=time(NULL),l;
132 long num1;
133 void (*cb)()=NULL;
134 int ret= -1;
135 BIO *bbio,*under;
136 int new_state,state,skip=0;;
137
138 RAND_seed((unsigned char *)&Time,sizeof(Time));
139 ERR_clear_error();
140 errno=0;
141
142 if (s->info_callback != NULL)
143 cb=s->info_callback;
144 else if (s->ctx->info_callback != NULL)
145 cb=s->ctx->info_callback;
146
147 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
148 s->in_handshake++;
149
150 for (;;)
151 {
152 state=s->state;
153
154 switch(s->state)
155 {
156 case SSL_ST_RENEGOTIATE:
157 s->new_session=1;
158 s->state=SSL_ST_CONNECT;
159 /* break */
160 case SSL_ST_BEFORE:
161 case SSL_ST_CONNECT:
162 case SSL_ST_BEFORE|SSL_ST_CONNECT:
163 case SSL_ST_OK|SSL_ST_CONNECT:
164
165 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
166
167 s->version=3;
168 s->type=SSL_ST_CONNECT;
169
170 if (s->init_buf == NULL)
171 {
172 if ((buf=BUF_MEM_new()) == NULL)
173 {
174 ret= -1;
175 goto end;
176 }
177 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
178 {
179 ret= -1;
180 goto end;
181 }
182 s->init_buf=buf;
183 }
184
185 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
186
187 /* setup buffing BIO */
188 if (s->bbio == NULL)
189 {
190 bbio=BIO_new(BIO_f_buffer());
191 if (bbio == NULL)
192 {
193 SSLerr(SSL_F_SSL3_CONNECT,ERR_LIB_BUF);
194 ret= -1;
195 goto end;
196 }
197 s->bbio=bbio;
198 }
199 else
200 bbio=s->bbio;
201
202 BIO_reset(bbio);
203 if (!BIO_set_write_buffer_size(bbio,16*1024))
204 {
205 SSLerr(SSL_F_SSL3_CONNECT,ERR_LIB_BUF);
206 ret= -1;
207 goto end;
208 }
209
210 /* don't push the buffering BIO quite yet */
211
212 ssl3_init_finished_mac(s);
213
214 s->state=SSL3_ST_CW_CLNT_HELLO_A;
215 s->ctx->sess_connect++;
216 s->init_num=0;
217 break;
218
219 case SSL3_ST_CW_CLNT_HELLO_A:
220 case SSL3_ST_CW_CLNT_HELLO_B:
221
222 s->shutdown=0;
223 ret=ssl3_client_hello(s);
224 if (ret <= 0) goto end;
225 s->state=SSL3_ST_CR_SRVR_HELLO_A;
226 s->init_num=0;
227
228 /* turn on buffering for the next lot of output */
229 s->wbio=BIO_push(s->bbio,s->wbio);
230
231 break;
232
233 case SSL3_ST_CR_SRVR_HELLO_A:
234 case SSL3_ST_CR_SRVR_HELLO_B:
235 ret=ssl3_get_server_hello(s);
236 if (ret <= 0) goto end;
237 if (s->hit)
238 s->state=SSL3_ST_CR_FINISHED_A;
239 else
240 s->state=SSL3_ST_CR_CERT_A;
241 s->init_num=0;
242 break;
243
244 case SSL3_ST_CR_CERT_A:
245 case SSL3_ST_CR_CERT_B:
246 /* Check if it is anon DH */
247 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
248 {
249 ret=ssl3_get_server_certificate(s);
250 if (ret <= 0) goto end;
251 }
252 else
253 skip=1;
254 s->state=SSL3_ST_CR_KEY_EXCH_A;
255 s->init_num=0;
256 break;
257
258 case SSL3_ST_CR_KEY_EXCH_A:
259 case SSL3_ST_CR_KEY_EXCH_B:
260 ret=ssl3_get_key_exchange(s);
261 if (ret <= 0) goto end;
262 s->state=SSL3_ST_CR_CERT_REQ_A;
263 s->init_num=0;
264
265 /* at this point we check that we have the
266 * required stuff from the server */
267 if (!ssl3_check_cert_and_algorithm(s))
268 {
269 ret= -1;
270 goto end;
271 }
272 break;
273
274 case SSL3_ST_CR_CERT_REQ_A:
275 case SSL3_ST_CR_CERT_REQ_B:
276 ret=ssl3_get_certificate_request(s);
277 if (ret <= 0) goto end;
278 s->state=SSL3_ST_CR_SRVR_DONE_A;
279 s->init_num=0;
280 break;
281
282 case SSL3_ST_CR_SRVR_DONE_A:
283 case SSL3_ST_CR_SRVR_DONE_B:
284 ret=ssl3_get_server_done(s);
285 if (ret <= 0) goto end;
286 if (s->s3->tmp.cert_req)
287 s->state=SSL3_ST_CW_CERT_A;
288 else
289 s->state=SSL3_ST_CW_KEY_EXCH_A;
290 s->init_num=0;
291
292 break;
293
294 case SSL3_ST_CW_CERT_A:
295 case SSL3_ST_CW_CERT_B:
296 case SSL3_ST_CW_CERT_C:
297 ret=ssl3_send_client_certificate(s);
298 if (ret <= 0) goto end;
299 s->state=SSL3_ST_CW_KEY_EXCH_A;
300 s->init_num=0;
301 break;
302
303 case SSL3_ST_CW_KEY_EXCH_A:
304 case SSL3_ST_CW_KEY_EXCH_B:
305 ret=ssl3_send_client_key_exchange(s);
306 if (ret <= 0) goto end;
307 l=s->s3->tmp.new_cipher->algorithms;
308 /* EAY EAY EAY need to check for DH fix cert
309 * sent back */
310 if ((s->s3->tmp.cert_req) && 1)
311 {
312 s->state=SSL3_ST_CW_CERT_VRFY_A;
313 }
314 else
315 {
316 s->state=SSL3_ST_CW_CHANGE_A;
317 s->s3->change_cipher_spec=0;
318 }
319
320 s->init_num=0;
321 break;
322
323 case SSL3_ST_CW_CERT_VRFY_A:
324 case SSL3_ST_CW_CERT_VRFY_B:
325 ret=ssl3_send_client_verify(s);
326 if (ret <= 0) goto end;
327 s->state=SSL3_ST_CW_CHANGE_A;
328 s->init_num=0;
329 s->s3->change_cipher_spec=0;
330 break;
331
332 case SSL3_ST_CW_CHANGE_A:
333 case SSL3_ST_CW_CHANGE_B:
334 ret=ssl3_send_change_cipher_spec(s,
335 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
336 if (ret <= 0) goto end;
337 s->state=SSL3_ST_CW_FINISHED_A;
338 s->init_num=0;
339
340 s->session->cipher=s->s3->tmp.new_cipher;
341 if (!ssl3_setup_key_block(s))
342 {
343 ret= -1;
344 goto end;
345 }
346
347 if (!ssl3_change_cipher_state(s,
348 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
349 {
350 ret= -1;
351 goto end;
352 }
353
354 break;
355
356 case SSL3_ST_CW_FINISHED_A:
357 case SSL3_ST_CW_FINISHED_B:
358 ret=ssl3_send_finished(s,
359 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
360 &(ssl3_client_finished_const[0]));
361 if (ret <= 0) goto end;
362 s->state=SSL3_ST_CW_FLUSH;
363
364 /* clear flags */
365 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
366 if (s->hit)
367 {
368 s->s3->tmp.next_state=SSL_ST_OK;
369 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
370 {
371 s->state=SSL_ST_OK;
372 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
373 s->s3->delay_buf_pop_ret=0;
374 }
375 }
376 else
377 {
378 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
379 }
380 s->init_num=0;
381 break;
382
383 case SSL3_ST_CR_FINISHED_A:
384 case SSL3_ST_CR_FINISHED_B:
385
386 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
387 SSL3_ST_CR_FINISHED_B,
388 &(ssl3_server_finished_const[0]));
389 if (ret <= 0) goto end;
390
391 if (s->hit)
392 s->state=SSL3_ST_CW_CHANGE_A;
393 else
394 s->state=SSL_ST_OK;
395 s->init_num=0;
396 break;
397
398 case SSL3_ST_CW_FLUSH:
399 /* number of bytes to be flushed */
400 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
401 if (num1 > 0)
402 {
403 s->rwstate=SSL_WRITING;
404 num1=BIO_flush(s->wbio);
405 if (num1 <= 0) { ret= -1; goto end; }
406 s->rwstate=SSL_NOTHING;
407 }
408
409 s->state=s->s3->tmp.next_state;
410 break;
411
412 case SSL_ST_OK:
413 /* clean a few things up */
414 ssl3_cleanup_key_block(s);
415
416 BUF_MEM_free(s->init_buf);
417 s->init_buf=NULL;
418
419 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
420 {
421 /* remove buffering */
422 under=BIO_pop(s->wbio);
423 if (under != NULL)
424 s->wbio=under;
425 else
426 abort(); /* ok */
427
428 BIO_free(s->bbio);
429 s->bbio=NULL;
430 }
431 /* else do it later */
432
433 s->init_num=0;
434 s->new_session=0;
435
436 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
437
438 ret=1;
439 /* s->server=0; */
440 s->handshake_func=ssl3_connect;
441 s->ctx->sess_connect_good++;
442
443 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
444
445 goto end;
446 break;
447
448 default:
449 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
450 ret= -1;
451 goto end;
452 /* break; */
453 }
454
455 /* did we do anything */
456 if (!s->s3->tmp.reuse_message && !skip)
457 {
458 if (s->debug) BIO_flush(s->wbio);
459
460 if ((cb != NULL) && (s->state != state))
461 {
462 new_state=s->state;
463 s->state=state;
464 cb(s,SSL_CB_CONNECT_LOOP,1);
465 s->state=new_state;
466 }
467 }
468 skip=0;
469 }
470 end:
471 if (cb != NULL)
472 cb(s,SSL_CB_CONNECT_EXIT,ret);
473 s->in_handshake--;
474 return(ret);
475 }
476
477
478 static int ssl3_client_hello(s)
479 SSL *s;
480 {
481 unsigned char *buf;
482 unsigned char *p,*d;
483 int i;
484 unsigned long Time,l;
485
486 buf=(unsigned char *)s->init_buf->data;
487 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
488 {
489 if ((s->session == NULL) ||
490 (s->session->ssl_version != s->version))
491 {
492 if (!ssl_get_new_session(s,0))
493 goto err;
494 }
495 /* else use the pre-loaded session */
496
497 p=s->s3->client_random;
498 Time=time(NULL); /* Time */
499 l2n(Time,p);
500 RAND_bytes(&(p[4]),SSL3_RANDOM_SIZE-sizeof(Time));
501
502 /* Do the message type and length last */
503 d=p= &(buf[4]);
504
505 *(p++)=SSL3_VERSION_MAJOR;
506 *(p++)=SSL3_VERSION_MINOR;
507
508 /* Random stuff */
509 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
510 p+=SSL3_RANDOM_SIZE;
511
512 /* Session ID */
513 if (s->new_session)
514 i=0;
515 else
516 i=s->session->session_id_length;
517 *(p++)=i;
518 if (i != 0)
519 {
520 memcpy(p,s->session->session_id,i);
521 p+=i;
522 }
523
524 /* Ciphers supported */
525 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]));
526 if (i == 0)
527 {
528 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
529 goto err;
530 }
531 s2n(i,p);
532 p+=i;
533
534 /* hardwire in the NULL compression algorithm. */
535 *(p++)=1;
536 *(p++)=0;
537
538 l=(p-d);
539 d=buf;
540 *(d++)=SSL3_MT_CLIENT_HELLO;
541 l2n3(l,d);
542
543 s->state=SSL3_ST_CW_CLNT_HELLO_B;
544 /* number of bytes to write */
545 s->init_num=p-buf;
546 s->init_off=0;
547 }
548
549 /* SSL3_ST_CW_CLNT_HELLO_B */
550 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
551 err:
552 return(-1);
553 }
554
555 static int ssl3_get_server_hello(s)
556 SSL *s;
557 {
558 STACK *sk;
559 SSL_CIPHER *c;
560 unsigned char *p,*d;
561 int i,al,ok;
562 unsigned int j;
563 long n;
564
565 n=ssl3_get_message(s,
566 SSL3_ST_CR_SRVR_HELLO_A,
567 SSL3_ST_CR_SRVR_HELLO_B,
568 SSL3_MT_SERVER_HELLO,
569 300, /* ?? */
570 &ok);
571
572 if (!ok) return((int)n);
573 d=p=(unsigned char *)s->init_buf->data;
574
575 if ((p[0] != SSL3_VERSION_MAJOR) && (p[1] != SSL3_VERSION_MINOR))
576 {
577 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
578 goto err;
579 }
580 p+=2;
581
582 /* load the server hello data */
583 /* load the server random */
584 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
585 p+=SSL3_RANDOM_SIZE;
586
587 /* get the session-id */
588 j= *(p++);
589
590 if ((j != 0) && (j != SSL3_SESSION_ID_SIZE))
591 {
592 /* SSLref returns 16 :-( */
593 if (j < SSL2_SSL_SESSION_ID_LENGTH)
594 {
595 al=SSL3_AD_ILLEGAL_PARAMETER;
596 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_SHORT);
597 goto f_err;
598 }
599 }
600 if (j == 0)
601 {
602 s->hit=0;
603 memset(s->session->session_id,0,SSL_MAX_SSL_SESSION_ID_LENGTH);
604 s->session->session_id_length=0;
605 }
606 else if ((j == s->session->session_id_length) &&
607 (memcmp(p,s->session->session_id,j) == 0))
608 s->hit=1;
609 else
610 {
611 memcpy(s->session->session_id,p,j);
612 s->session->session_id_length=j;
613 s->hit=0;
614 }
615 p+=j;
616 c=ssl_get_cipher_by_char(s,p);
617 if (c == NULL)
618 {
619 /* unknown cipher */
620 al=SSL3_AD_HANDSHAKE_FAILURE;
621 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
622 goto f_err;
623 }
624 p+=ssl_put_cipher_by_char(s,NULL,NULL);
625
626 sk=ssl_get_ciphers_by_id(s);
627 i=sk_find(sk,(char *)c);
628 if (i < 0)
629 {
630 /* we did not say we would use this cipher */
631 al=SSL3_AD_ILLEGAL_PARAMETER;
632 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
633 goto f_err;
634 }
635
636 if (s->hit && (s->session->cipher != c))
637 {
638 if (!(s->ctx->options &
639 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
640 {
641 al=SSL3_AD_ILLEGAL_PARAMETER;
642 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
643 goto f_err;
644 }
645 }
646 s->s3->tmp.new_cipher=c;
647
648 /* lets get the compression algorithm */
649 j= *(p++);
650 if (j != 0)
651 {
652 al=SSL3_AD_HANDSHAKE_FAILURE;
653 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
654 goto f_err;
655 }
656
657 if (p != (d+n))
658 {
659 /* wrong packet length */
660 al=SSL3_AD_ILLEGAL_PARAMETER;
661 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
662 goto err;
663 }
664
665 return(1);
666 f_err:
667 ssl3_send_alert(s,SSL3_AL_FATAL,al);
668 err:
669 return(-1);
670 }
671
672 static int ssl3_get_server_certificate(s)
673 SSL *s;
674 {
675 int al,i,ok,ret= -1;
676 unsigned long n,nc,llen,l;
677 X509 *x=NULL;
678 unsigned char *p,*d,*q;
679 STACK *sk=NULL;
680 CERT *c;
681 EVP_PKEY *pkey=NULL;
682
683 n=ssl3_get_message(s,
684 SSL3_ST_CR_CERT_A,
685 SSL3_ST_CR_CERT_B,
686 -1,
687 #if defined(MSDOS) && !defined(WIN32)
688 1024*30, /* 30k max cert list :-) */
689 #else
690 1024*100, /* 100k max cert list :-) */
691 #endif
692 &ok);
693
694 if (!ok) return((int)n);
695
696 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
697 {
698 s->s3->tmp.reuse_message=1;
699 return(1);
700 }
701
702 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
703 {
704 al=SSL3_AD_UNEXPECTED_MESSAGE;
705 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
706 goto f_err;
707 }
708 d=p=(unsigned char *)s->init_buf->data;
709
710 if ((sk=sk_new_null()) == NULL)
711 {
712 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
713 goto err;
714 }
715
716 n2l3(p,llen);
717 if (llen+3 != n)
718 {
719 al=SSL3_AD_ILLEGAL_PARAMETER;
720 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
721 goto f_err;
722 }
723 for (nc=0; nc<llen; )
724 {
725 n2l3(p,l);
726 if ((l+nc+3) > llen)
727 {
728 al=SSL3_AD_ILLEGAL_PARAMETER;
729 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
730 goto f_err;
731 }
732
733 q=p;
734 x=d2i_X509(NULL,&q,l);
735 if (x == NULL)
736 {
737 al=SSL3_AD_ILLEGAL_PARAMETER;
738 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
739 goto f_err;
740 }
741 if (q != (p+l))
742 {
743 al=SSL3_AD_ILLEGAL_PARAMETER;
744 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
745 goto f_err;
746 }
747 if (!sk_push(sk,(char *)x))
748 {
749 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
750 goto err;
751 }
752 x=NULL;
753 nc+=l+3;
754 p=q;
755 }
756
757 i=ssl_verify_cert_chain(s,sk);
758 if ((s->verify_mode != SSL_VERIFY_NONE) && (!i))
759 {
760 al=ssl_verify_alarm_type(s->verify_result);
761 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
762 goto f_err;
763 }
764
765 c=ssl_cert_new();
766 if (c == NULL) goto err;
767
768 if (s->session->cert) ssl_cert_free(s->session->cert);
769 s->session->cert=c;
770
771 c->cert_chain=sk;
772 x=(X509 *)sk_value(sk,0);
773 sk=NULL;
774
775 pkey=X509_get_pubkey(x);
776
777 if (EVP_PKEY_missing_parameters(pkey))
778 {
779 x=NULL;
780 al=SSL3_AL_FATAL;
781 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
782 goto f_err;
783 }
784
785 i=ssl_cert_type(x,pkey);
786 if (i < 0)
787 {
788 x=NULL;
789 al=SSL3_AL_FATAL;
790 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
791 goto f_err;
792 }
793
794 c->cert_type=i;
795 x->references++;
796 if (c->pkeys[i].x509 != NULL)
797 X509_free(c->pkeys[i].x509);
798 c->pkeys[i].x509=x;
799 c->key= &(c->pkeys[i]);
800
801 if ((s->session != NULL) && (s->session->peer != NULL))
802 X509_free(s->session->peer);
803 x->references++;
804 s->session->peer=x;
805
806 x=NULL;
807 ret=1;
808
809 if (0)
810 {
811 f_err:
812 ssl3_send_alert(s,SSL3_AL_FATAL,al);
813 }
814 err:
815 if (x != NULL) X509_free(x);
816 if (sk != NULL) sk_pop_free(sk,X509_free);
817 return(ret);
818 }
819
820 static int ssl3_get_key_exchange(s)
821 SSL *s;
822 {
823 #ifndef NO_RSA
824 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
825 #endif
826 EVP_MD_CTX md_ctx;
827 unsigned char *param,*p;
828 int al,i,j,param_len,ok;
829 long n,alg;
830 EVP_PKEY *pkey=NULL;
831 RSA *rsa=NULL;
832 DH *dh=NULL;
833
834 n=ssl3_get_message(s,
835 SSL3_ST_CR_KEY_EXCH_A,
836 SSL3_ST_CR_KEY_EXCH_B,
837 -1,
838 1024*8, /* ?? */
839 &ok);
840
841 if (!ok) return((int)n);
842
843 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
844 {
845 s->s3->tmp.reuse_message=1;
846 return(1);
847 }
848
849 param=p=(unsigned char *)s->init_buf->data;
850
851 if (s->session->cert != NULL)
852 {
853 #ifndef NO_RSA
854 if (s->session->cert->rsa_tmp != NULL)
855 {
856 RSA_free(s->session->cert->rsa_tmp);
857 s->session->cert->rsa_tmp=NULL;
858 }
859 #endif
860 #ifndef NO_DH
861 if (s->session->cert->dh_tmp)
862 {
863 DH_free(s->session->cert->dh_tmp);
864 s->session->cert->dh_tmp=NULL;
865 }
866 #endif
867 }
868 else
869 {
870 s->session->cert=ssl_cert_new();
871 }
872
873 param_len=0;
874 alg=s->s3->tmp.new_cipher->algorithms;
875
876 #ifndef NO_RSA
877 if (alg & SSL_kRSA)
878 {
879 if ((rsa=RSA_new()) == NULL)
880 {
881 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
882 goto err;
883 }
884 n2s(p,i);
885 param_len=i+2;
886 if (param_len > n)
887 {
888 al=SSL3_AD_ILLEGAL_PARAMETER;
889 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
890 goto f_err;
891 }
892 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
893 {
894 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
895 goto err;
896 }
897 p+=i;
898
899 n2s(p,i);
900 param_len+=i+2;
901 if (param_len > n)
902 {
903 al=SSL3_AD_ILLEGAL_PARAMETER;
904 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
905 goto f_err;
906 }
907 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
908 {
909 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
910 goto err;
911 }
912 p+=i;
913 n-=param_len;
914
915 /* s->session->cert->rsa_tmp=rsa;*/
916 /* this should be because we are using an export cipher */
917 if (alg & SSL_aRSA)
918 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509);
919 else
920 {
921 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
922 goto err;
923 }
924 s->session->cert->rsa_tmp=rsa;
925 }
926 else
927 #endif
928 #ifndef NO_DH
929 if (alg & SSL_kEDH)
930 {
931 if ((dh=DH_new()) == NULL)
932 {
933 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
934 goto err;
935 }
936 n2s(p,i);
937 param_len=i+2;
938 if (param_len > n)
939 {
940 al=SSL3_AD_ILLEGAL_PARAMETER;
941 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
942 goto f_err;
943 }
944 if (!(dh->p=BN_bin2bn(p,i,NULL)))
945 {
946 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
947 goto err;
948 }
949 p+=i;
950
951 n2s(p,i);
952 param_len+=i+2;
953 if (param_len > n)
954 {
955 al=SSL3_AD_ILLEGAL_PARAMETER;
956 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
957 goto f_err;
958 }
959 if (!(dh->g=BN_bin2bn(p,i,NULL)))
960 {
961 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
962 goto err;
963 }
964 p+=i;
965
966 n2s(p,i);
967 param_len+=i+2;
968 if (param_len > n)
969 {
970 al=SSL3_AD_ILLEGAL_PARAMETER;
971 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
972 goto f_err;
973 }
974 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
975 {
976 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
977 goto err;
978 }
979 p+=i;
980 n-=param_len;
981
982 #ifndef NO_RSA
983 if (alg & SSL_aRSA)
984 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509);
985 else
986 #endif
987 #ifndef NO_DSA
988 if (alg & SSL_aDSS)
989 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_DSA_SIGN].x509);
990 #endif
991 /* else anonymous DH, so no certificate or pkey. */
992
993 s->session->cert->dh_tmp=dh;
994 }
995 else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
996 {
997 al=SSL3_AD_HANDSHAKE_FAILURE;
998 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
999 goto f_err;
1000 }
1001 #endif
1002
1003 /* p points to the next byte, there are 'n' bytes left */
1004
1005
1006 /* if it was signed, check the signature */
1007 if (pkey != NULL)
1008 {
1009 n2s(p,i);
1010 n-=2;
1011 j=EVP_PKEY_size(pkey);
1012
1013 if ((i != n) || (n > j) || (n <= 0))
1014 {
1015 /* wrong packet length */
1016 al=SSL3_AD_ILLEGAL_PARAMETER;
1017 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1018 goto err;
1019 }
1020
1021 #ifndef NO_RSA
1022 if (pkey->type == EVP_PKEY_RSA)
1023 {
1024 int num;
1025
1026 j=0;
1027 q=md_buf;
1028 for (num=2; num > 0; num--)
1029 {
1030 EVP_DigestInit(&md_ctx,(num == 2)?EVP_md5():EVP_sha1());
1031 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1032 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1033 EVP_DigestUpdate(&md_ctx,param,param_len);
1034 EVP_DigestFinal(&md_ctx,q,(unsigned int *)&i);
1035 q+=i;
1036 j+=i;
1037 }
1038 i=RSA_public_decrypt((int)n,p,p,pkey->pkey.rsa,
1039 RSA_PKCS1_PADDING);
1040 if (i <= 0)
1041 {
1042 al=SSL3_AD_ILLEGAL_PARAMETER;
1043 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1044 goto f_err;
1045 }
1046 if ((j != i) || (memcmp(p,md_buf,i) != 0))
1047 {
1048 /* bad signature */
1049 al=SSL3_AD_ILLEGAL_PARAMETER;
1050 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1051 goto f_err;
1052 }
1053 }
1054 else
1055 #endif
1056 #ifndef NO_DSA
1057 if (pkey->type == EVP_PKEY_DSA)
1058 {
1059 /* lets do DSS */
1060 EVP_VerifyInit(&md_ctx,EVP_dss1());
1061 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1062 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1063 EVP_VerifyUpdate(&md_ctx,param,param_len);
1064 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
1065 {
1066 /* bad signature */
1067 al=SSL3_AD_ILLEGAL_PARAMETER;
1068 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1069 goto f_err;
1070 }
1071 }
1072 else
1073 #endif
1074 {
1075 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1076 goto err;
1077 }
1078 }
1079 else
1080 {
1081 /* still data left over */
1082 if (!(alg & SSL_aNULL))
1083 {
1084 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1085 goto err;
1086 }
1087 if (n != 0)
1088 {
1089 al=SSL3_AD_ILLEGAL_PARAMETER;
1090 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1091 goto f_err;
1092 }
1093 }
1094
1095 return(1);
1096 f_err:
1097 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1098 err:
1099 return(-1);
1100 }
1101
1102 static int ssl3_get_certificate_request(s)
1103 SSL *s;
1104 {
1105 int ok,ret=0;
1106 unsigned long n,nc;
1107 unsigned int llen,l,ctype_num,i;
1108 X509_NAME *xn=NULL;
1109 unsigned char *p,*d,*q;
1110 STACK *ca_sk=NULL;
1111
1112 n=ssl3_get_message(s,
1113 SSL3_ST_CR_CERT_REQ_A,
1114 SSL3_ST_CR_CERT_REQ_B,
1115 -1,
1116 #if defined(MSDOS) && !defined(WIN32)
1117 1024*30, /* 30k max cert list :-) */
1118 #else
1119 1024*100, /* 100k max cert list :-) */
1120 #endif
1121 &ok);
1122
1123 if (!ok) return((int)n);
1124
1125 s->s3->tmp.cert_req=0;
1126
1127 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1128 {
1129 s->s3->tmp.reuse_message=1;
1130 return(1);
1131 }
1132
1133 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1134 {
1135 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_UNEXPECTED_MESSAGE);
1136 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
1137 goto err;
1138 }
1139
1140 d=p=(unsigned char *)s->init_buf->data;
1141
1142 if ((ca_sk=sk_new(ca_dn_cmp)) == NULL)
1143 {
1144 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1145 goto err;
1146 }
1147
1148 /* get the certificate types */
1149 ctype_num= *(p++);
1150 if (ctype_num > SSL3_CT_NUMBER)
1151 ctype_num=SSL3_CT_NUMBER;
1152 for (i=0; i<ctype_num; i++)
1153 s->s3->tmp.ctype[i]= p[i];
1154 p+=ctype_num;
1155
1156 /* get the CA RDNs */
1157 n2s(p,llen);
1158 if ((llen+ctype_num+2+1) != n)
1159 {
1160 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_ILLEGAL_PARAMETER);
1161 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
1162 goto err;
1163 }
1164
1165 for (nc=0; nc<llen; )
1166 {
1167 n2s(p,l);
1168 if ((l+nc+2) > llen)
1169 {
1170 if ((s->ctx->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1171 goto cont; /* netscape bugs */
1172 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_ILLEGAL_PARAMETER);
1173 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
1174 goto err;
1175 }
1176
1177 q=p;
1178
1179 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1180 {
1181 /* If netscape tollerance is on, ignore errors */
1182 if (s->ctx->options & SSL_OP_NETSCAPE_CA_DN_BUG)
1183 goto cont;
1184 else
1185 {
1186 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_ILLEGAL_PARAMETER);
1187 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
1188 goto err;
1189 }
1190 }
1191
1192 if (q != (p+l))
1193 {
1194 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_ILLEGAL_PARAMETER);
1195 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1196 goto err;
1197 }
1198 if (!sk_push(ca_sk,(char *)xn))
1199 {
1200 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1201 goto err;
1202 }
1203
1204 p+=l;
1205 nc+=l+2;
1206 }
1207
1208 if (0)
1209 {
1210 cont:
1211 ERR_clear_error();
1212 }
1213
1214 /* we should setup a certficate to return.... */
1215 s->s3->tmp.cert_req=1;
1216 s->s3->tmp.ctype_num=ctype_num;
1217 if (s->s3->tmp.ca_names != NULL)
1218 sk_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1219 s->s3->tmp.ca_names=ca_sk;
1220 ca_sk=NULL;
1221
1222 ret=1;
1223 err:
1224 if (ca_sk != NULL) sk_pop_free(ca_sk,X509_NAME_free);
1225 return(ret);
1226 }
1227
1228 static int ca_dn_cmp(a,b)
1229 X509_NAME **a,**b;
1230 {
1231 return(X509_NAME_cmp(*a,*b));
1232 }
1233
1234 static int ssl3_get_server_done(s)
1235 SSL *s;
1236 {
1237 int ok,ret=0;
1238 long n;
1239
1240 n=ssl3_get_message(s,
1241 SSL3_ST_CR_SRVR_DONE_A,
1242 SSL3_ST_CR_SRVR_DONE_B,
1243 SSL3_MT_SERVER_DONE,
1244 30, /* should be very small, like 0 :-) */
1245 &ok);
1246
1247 if (!ok) return((int)n);
1248 if (n > 0)
1249 {
1250 /* should contain no data */
1251 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_ILLEGAL_PARAMETER);
1252 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
1253 }
1254 ret=1;
1255 return(ret);
1256 }
1257
1258 static int ssl3_send_client_key_exchange(s)
1259 SSL *s;
1260 {
1261 unsigned char *p,*d;
1262 int n;
1263 unsigned long l;
1264 EVP_PKEY *pkey=NULL;
1265
1266 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1267 {
1268 d=(unsigned char *)s->init_buf->data;
1269 p= &(d[4]);
1270
1271 l=s->s3->tmp.new_cipher->algorithms;
1272
1273 #ifndef NO_RSA
1274 if (l & SSL_kRSA)
1275 {
1276 RSA *rsa;
1277 unsigned char tmp_buf[48];
1278
1279 if (s->session->cert->rsa_tmp != NULL)
1280 rsa=s->session->cert->rsa_tmp;
1281 else
1282 {
1283 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509);
1284 if ((pkey == NULL) ||
1285 (pkey->type != EVP_PKEY_RSA) ||
1286 (pkey->pkey.rsa == NULL))
1287 {
1288 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1289 goto err;
1290 }
1291 rsa=pkey->pkey.rsa;
1292 }
1293
1294 tmp_buf[0]=SSL3_VERSION_MAJOR;
1295 tmp_buf[1]=SSL3_VERSION_MINOR;
1296 RAND_bytes(&(tmp_buf[2]),SSL_MAX_MASTER_KEY_LENGTH-2);
1297
1298 s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
1299
1300 n=RSA_public_encrypt(48,tmp_buf,p,rsa,
1301 RSA_PKCS1_PADDING);
1302 if (n <= 0)
1303 {
1304 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
1305 goto err;
1306 }
1307
1308 s->session->master_key_length=
1309 ssl3_generate_master_secret(s,
1310 s->session->master_key,
1311 tmp_buf,48);
1312 memset(tmp_buf,0,48);
1313 }
1314 else
1315 #endif
1316 #ifndef NO_DH
1317 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1318 {
1319 DH *dh_srvr,*dh_clnt;
1320
1321 if (s->session->cert->dh_tmp != NULL)
1322 dh_srvr=s->session->cert->dh_tmp;
1323 else
1324 {
1325 /* we get them from the cert */
1326 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_HANDSHAKE_FAILURE);
1327 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1328 goto err;
1329 }
1330
1331 /* generate a new random key */
1332 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
1333 {
1334 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1335 goto err;
1336 }
1337 if (!DH_generate_key(dh_clnt))
1338 {
1339 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1340 goto err;
1341 }
1342
1343 /* use the 'p' output buffer for the DH key, but
1344 * make sure to clear it out afterwards */
1345 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
1346 if (n <= 0)
1347 {
1348 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1349 goto err;
1350 }
1351
1352 /* generate master key from the result */
1353 s->session->master_key_length=
1354 ssl3_generate_master_secret(s,
1355 s->session->master_key,p,n);
1356 /* clean up */
1357 memset(p,0,n);
1358
1359 /* send off the data */
1360 n=BN_num_bytes(dh_clnt->pub_key);
1361 s2n(n,p);
1362 BN_bn2bin(dh_clnt->pub_key,p);
1363 n+=2;
1364
1365 DH_free(dh_clnt);
1366
1367 /* perhaps clean things up a bit EAY EAY EAY EAY*/
1368 }
1369 else
1370 #endif
1371 {
1372 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_HANDSHAKE_FAILURE);
1373 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1374 goto err;
1375 }
1376
1377 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1378 l2n3(n,d);
1379
1380 s->state=SSL3_ST_CW_KEY_EXCH_B;
1381 /* number of bytes to write */
1382 s->init_num=n+4;
1383 s->init_off=0;
1384 }
1385
1386 /* SSL3_ST_CW_KEY_EXCH_B */
1387 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1388 err:
1389 return(-1);
1390 }
1391
1392 static int ssl3_send_client_verify(s)
1393 SSL *s;
1394 {
1395 unsigned char *p,*d;
1396 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1397 EVP_PKEY *pkey;
1398 int i=0,j;
1399 unsigned long n;
1400
1401 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
1402 {
1403 d=(unsigned char *)s->init_buf->data;
1404 p= &(d[4]);
1405 pkey=s->cert->key->privatekey;
1406
1407 ssl3_final_finish_mac(s,&(s->s3->finish_dgst2),
1408 NULL,&(data[MD5_DIGEST_LENGTH]));
1409
1410 #ifndef NO_RSA
1411 if (pkey->type == EVP_PKEY_RSA)
1412 {
1413 ssl3_final_finish_mac(s,&(s->s3->finish_dgst1),
1414 NULL,&(data[0]));
1415 i=RSA_private_encrypt(
1416 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1417 data,&(p[2]),pkey->pkey.rsa,
1418 RSA_PKCS1_PADDING);
1419 if (i <= 0)
1420 {
1421 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
1422 goto err;
1423 }
1424 s2n(i,p);
1425 n=i+2;
1426 }
1427 else
1428 #endif
1429 #ifndef NO_DSA
1430 if (pkey->type == EVP_PKEY_DSA)
1431 {
1432 if (!DSA_sign(pkey->save_type,
1433 &(data[MD5_DIGEST_LENGTH]),
1434 SHA_DIGEST_LENGTH,&(p[2]),
1435 (unsigned int *)&j,pkey->pkey.dsa))
1436 {
1437 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
1438 goto err;
1439 }
1440 s2n(j,p);
1441 n=j+2;
1442 }
1443 else
1444 #endif
1445 {
1446 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,SSL_R_INTERNAL_ERROR);
1447 goto err;
1448 }
1449 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
1450 l2n3(n,d);
1451
1452 s->init_num=(int)n+4;
1453 s->init_off=0;
1454 }
1455 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1456 err:
1457 return(-1);
1458 }
1459
1460 static int ssl3_send_client_certificate(s)
1461 SSL *s;
1462 {
1463 X509 *x509=NULL;
1464 EVP_PKEY *pkey=NULL;
1465 int i;
1466 unsigned long l;
1467
1468 if (s->state == SSL3_ST_CW_CERT_A)
1469 {
1470 if ((s->cert == NULL) ||
1471 (s->cert->key->x509 == NULL) ||
1472 (s->cert->key->privatekey == NULL))
1473 s->state=SSL3_ST_CW_CERT_B;
1474 else
1475 s->state=SSL3_ST_CW_CERT_C;
1476 }
1477
1478 /* We need to get a client cert */
1479 if (s->state == SSL3_ST_CW_CERT_B)
1480 {
1481 /* If we get an error, we need to
1482 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1483 * We then get retied later */
1484 i=0;
1485 if (s->ctx->client_cert_cb != NULL)
1486 i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
1487 if (i < 0)
1488 {
1489 s->rwstate=SSL_X509_LOOKUP;
1490 return(-1);
1491 }
1492 s->rwstate=SSL_NOTHING;
1493 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
1494 {
1495 s->state=SSL3_ST_CW_CERT_B;
1496 if ( !SSL_use_certificate(s,x509) ||
1497 !SSL_use_PrivateKey(s,pkey))
1498 i=0;
1499 }
1500 else if (i == 1)
1501 {
1502 i=0;
1503 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1504 }
1505
1506 if (x509 != NULL) X509_free(x509);
1507 if (pkey != NULL) EVP_PKEY_free(pkey);
1508 if (i == 0)
1509 {
1510 s->s3->tmp.cert_req=0;
1511 ssl3_send_alert(s,SSL3_AL_WARNING,SSL3_AD_NO_CERTIFICATE);
1512 return(1);
1513 }
1514
1515 /* Ok, we have a cert */
1516 s->state=SSL3_ST_CW_CERT_C;
1517 }
1518
1519 if (s->state == SSL3_ST_CW_CERT_C)
1520 {
1521 s->state=SSL3_ST_CW_CERT_D;
1522 l=ssl3_output_cert_chain(s,s->cert->key->x509);
1523 s->init_num=(int)l;
1524 s->init_off=0;
1525 }
1526 /* SSL3_ST_CW_CERT_D */
1527 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1528 }
1529
1530 #define has_bits(i,m) (((i)&(m)) == (m))
1531
1532 static int ssl3_check_cert_and_algorithm(s)
1533 SSL *s;
1534 {
1535 int i,idx;
1536 long algs;
1537 EVP_PKEY *pkey=NULL;
1538 CERT *c;
1539 RSA *rsa;
1540 DH *dh;
1541
1542 c=s->session->cert;
1543
1544 if (c == NULL)
1545 {
1546 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_INTERNAL_ERROR);
1547 goto err;
1548 }
1549
1550 algs=s->s3->tmp.new_cipher->algorithms;
1551
1552 /* we don't have a certificate */
1553 if (algs & (SSL_aDH|SSL_aNULL))
1554 return(1);
1555
1556 rsa=s->session->cert->rsa_tmp;
1557 dh=s->session->cert->dh_tmp;
1558
1559 /* This is the passed certificate */
1560
1561 idx=c->cert_type;
1562 pkey=X509_get_pubkey(c->pkeys[idx].x509);
1563 i=X509_certificate_type(c->pkeys[idx].x509,pkey);
1564
1565
1566 /* Check that we have a certificate if we require one */
1567 if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
1568 {
1569 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
1570 goto f_err;
1571 }
1572 #ifndef NO_DSA
1573 else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
1574 {
1575 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
1576 goto f_err;
1577 }
1578 #endif
1579
1580 if ((algs & SSL_kRSA) &&
1581 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
1582 {
1583 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
1584 goto f_err;
1585 }
1586 #ifndef NO_DH
1587 else if ((algs & SSL_kEDH) &&
1588 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
1589 {
1590 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
1591 goto f_err;
1592 }
1593 else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
1594 {
1595 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
1596 goto f_err;
1597 }
1598 #ifndef NO_DSA
1599 else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
1600 {
1601 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
1602 goto f_err;
1603 }
1604 #endif
1605 #endif
1606
1607 if ((algs & SSL_EXP) && !has_bits(i,EVP_PKT_EXP))
1608 {
1609 #ifndef NO_RSA
1610 if (algs & SSL_kRSA)
1611 {
1612 if ((rsa == NULL) || (RSA_size(rsa) > 512))
1613 {
1614 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
1615 goto f_err;
1616 }
1617 }
1618 else
1619 #endif
1620 #ifndef NO_DH
1621 if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1622 {
1623 if ((dh == NULL) || (DH_size(dh) > 512))
1624 {
1625 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
1626 goto f_err;
1627 }
1628 }
1629 else
1630 #endif
1631 {
1632 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1633 goto f_err;
1634 }
1635 }
1636 return(1);
1637 f_err:
1638 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_HANDSHAKE_FAILURE);
1639 err:
1640 return(0);
1641 }
1642