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