Create a duplicate of the SSL_CTX's CERT in SSL_new instead of copying
[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->cert) ssl_cert_free(s->session->cert);
771         s->session->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->cert != NULL)
858                 {
859 #ifndef NO_RSA
860                 if (s->session->cert->rsa_tmp != NULL)
861                         {
862                         RSA_free(s->session->cert->rsa_tmp);
863                         s->session->cert->rsa_tmp=NULL;
864                         }
865 #endif
866 #ifndef NO_DH
867                 if (s->session->cert->dh_tmp)
868                         {
869                         DH_free(s->session->cert->dh_tmp);
870                         s->session->cert->dh_tmp=NULL;
871                         }
872 #endif
873                 }
874         else
875                 {
876                 s->session->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 /*              s->session->cert->rsa_tmp=rsa;*/
922                 /* this should be because we are using an export cipher */
923                 if (alg & SSL_aRSA)
924                         pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509);
925                 else
926                         {
927                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
928                         goto err;
929                         }
930                 s->session->cert->rsa_tmp=rsa;
931                 }
932         else
933 #endif
934 #ifndef NO_DH
935                 if (alg & SSL_kEDH)
936                 {
937                 if ((dh=DH_new()) == NULL)
938                         {
939                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
940                         goto err;
941                         }
942                 n2s(p,i);
943                 param_len=i+2;
944                 if (param_len > n)
945                         {
946                         al=SSL_AD_DECODE_ERROR;
947                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
948                         goto f_err;
949                         }
950                 if (!(dh->p=BN_bin2bn(p,i,NULL)))
951                         {
952                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
953                         goto err;
954                         }
955                 p+=i;
956
957                 n2s(p,i);
958                 param_len+=i+2;
959                 if (param_len > n)
960                         {
961                         al=SSL_AD_DECODE_ERROR;
962                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
963                         goto f_err;
964                         }
965                 if (!(dh->g=BN_bin2bn(p,i,NULL)))
966                         {
967                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
968                         goto err;
969                         }
970                 p+=i;
971
972                 n2s(p,i);
973                 param_len+=i+2;
974                 if (param_len > n)
975                         {
976                         al=SSL_AD_DECODE_ERROR;
977                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
978                         goto f_err;
979                         }
980                 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
981                         {
982                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
983                         goto err;
984                         }
985                 p+=i;
986                 n-=param_len;
987
988 #ifndef NO_RSA
989                 if (alg & SSL_aRSA)
990                         pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509);
991                 else
992 #endif
993 #ifndef NO_DSA
994                 if (alg & SSL_aDSS)
995                         pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_DSA_SIGN].x509);
996 #endif
997                 /* else anonymous DH, so no certificate or pkey. */
998
999                 s->session->cert->dh_tmp=dh;
1000                 dh=NULL;
1001                 }
1002         else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
1003                 {
1004                 al=SSL_AD_ILLEGAL_PARAMETER;
1005                 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1006                 goto f_err;
1007                 }
1008 #endif
1009         if (alg & SSL_aFZA)
1010                 {
1011                 al=SSL_AD_HANDSHAKE_FAILURE;
1012                 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1013                 goto f_err;
1014                 }
1015
1016
1017         /* p points to the next byte, there are 'n' bytes left */
1018
1019
1020         /* if it was signed, check the signature */
1021         if (pkey != NULL)
1022                 {
1023                 n2s(p,i);
1024                 n-=2;
1025                 j=EVP_PKEY_size(pkey);
1026
1027                 if ((i != n) || (n > j) || (n <= 0))
1028                         {
1029                         /* wrong packet length */
1030                         al=SSL_AD_DECODE_ERROR;
1031                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
1032                         goto f_err;
1033                         }
1034
1035 #ifndef NO_RSA
1036                 if (pkey->type == EVP_PKEY_RSA)
1037                         {
1038                         int num;
1039
1040                         j=0;
1041                         q=md_buf;
1042                         for (num=2; num > 0; num--)
1043                                 {
1044                                 EVP_DigestInit(&md_ctx,(num == 2)
1045                                         ?s->ctx->md5:s->ctx->sha1);
1046                                 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1047                                 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1048                                 EVP_DigestUpdate(&md_ctx,param,param_len);
1049                                 EVP_DigestFinal(&md_ctx,q,(unsigned int *)&i);
1050                                 q+=i;
1051                                 j+=i;
1052                                 }
1053                         i=RSA_public_decrypt((int)n,p,p,pkey->pkey.rsa,
1054                                 RSA_PKCS1_PADDING);
1055                         if (i <= 0)
1056                                 {
1057                                 al=SSL_AD_DECRYPT_ERROR;
1058                                 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1059                                 goto f_err;
1060                                 }
1061                         if ((j != i) || (memcmp(p,md_buf,i) != 0))
1062                                 {
1063                                 /* bad signature */
1064                                 al=SSL_AD_DECRYPT_ERROR;
1065                                 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1066                                 goto f_err;
1067                                 }
1068                         }
1069                 else
1070 #endif
1071 #ifndef NO_DSA
1072                         if (pkey->type == EVP_PKEY_DSA)
1073                         {
1074                         /* lets do DSS */
1075                         EVP_VerifyInit(&md_ctx,EVP_dss1());
1076                         EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1077                         EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1078                         EVP_VerifyUpdate(&md_ctx,param,param_len);
1079                         if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
1080                                 {
1081                                 /* bad signature */
1082                                 al=SSL_AD_DECRYPT_ERROR;
1083                                 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1084                                 goto f_err;
1085                                 }
1086                         }
1087                 else
1088 #endif
1089                         {
1090                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1091                         goto err;
1092                         }
1093                 }
1094         else
1095                 {
1096                 /* still data left over */
1097                 if (!(alg & SSL_aNULL))
1098                         {
1099                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1100                         goto err;
1101                         }
1102                 if (n != 0)
1103                         {
1104                         al=SSL_AD_DECODE_ERROR;
1105                         SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1106                         goto f_err;
1107                         }
1108                 }
1109         EVP_PKEY_free(pkey);
1110         return(1);
1111 f_err:
1112         ssl3_send_alert(s,SSL3_AL_FATAL,al);
1113 err:
1114         EVP_PKEY_free(pkey);
1115         return(-1);
1116         }
1117
1118 static int ssl3_get_certificate_request(SSL *s)
1119         {
1120         int ok,ret=0;
1121         unsigned long n,nc,l;
1122         unsigned int llen,ctype_num,i;
1123         X509_NAME *xn=NULL;
1124         unsigned char *p,*d,*q;
1125         STACK_OF(X509_NAME) *ca_sk=NULL;
1126
1127         n=ssl3_get_message(s,
1128                 SSL3_ST_CR_CERT_REQ_A,
1129                 SSL3_ST_CR_CERT_REQ_B,
1130                 -1,
1131 #if defined(MSDOS) && !defined(WIN32)
1132                 1024*30,  /* 30k max cert list :-) */
1133 #else
1134                 1024*100, /* 100k max cert list :-) */
1135 #endif
1136                 &ok);
1137
1138         if (!ok) return((int)n);
1139
1140         s->s3->tmp.cert_req=0;
1141
1142         if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1143                 {
1144                 s->s3->tmp.reuse_message=1;
1145                 return(1);
1146                 }
1147
1148         if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1149                 {
1150                 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1151                 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
1152                 goto err;
1153                 }
1154
1155         /* TLS does not like anon-DH with client cert */
1156         if (s->version > SSL3_VERSION)
1157                 {
1158                 l=s->s3->tmp.new_cipher->algorithms;
1159                 if (l & SSL_aNULL)
1160                         {
1161                         ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1162                         SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1163                         goto err;
1164                         }
1165                 }
1166
1167         d=p=(unsigned char *)s->init_buf->data;
1168
1169         if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
1170                 {
1171                 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1172                 goto err;
1173                 }
1174
1175         /* get the certificate types */
1176         ctype_num= *(p++);
1177         if (ctype_num > SSL3_CT_NUMBER)
1178                 ctype_num=SSL3_CT_NUMBER;
1179         for (i=0; i<ctype_num; i++)
1180                 s->s3->tmp.ctype[i]= p[i];
1181         p+=ctype_num;
1182
1183         /* get the CA RDNs */
1184         n2s(p,llen);
1185 #if 0
1186 {
1187 FILE *out;
1188 out=fopen("/tmp/vsign.der","w");
1189 fwrite(p,1,llen,out);
1190 fclose(out);
1191 }
1192 #endif
1193
1194         if ((llen+ctype_num+2+1) != n)
1195                 {
1196                 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1197                 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
1198                 goto err;
1199                 }
1200
1201         for (nc=0; nc<llen; )
1202                 {
1203                 n2s(p,l);
1204                 if ((l+nc+2) > llen)
1205                         {
1206                         if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1207                                 goto cont; /* netscape bugs */
1208                         ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1209                         SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
1210                         goto err;
1211                         }
1212
1213                 q=p;
1214
1215                 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1216                         {
1217                         /* If netscape tollerance is on, ignore errors */
1218                         if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
1219                                 goto cont;
1220                         else
1221                                 {
1222                                 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1223                                 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
1224                                 goto err;
1225                                 }
1226                         }
1227
1228                 if (q != (p+l))
1229                         {
1230                         ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1231                         SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1232                         goto err;
1233                         }
1234                 if (!sk_X509_NAME_push(ca_sk,xn))
1235                         {
1236                         SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1237                         goto err;
1238                         }
1239
1240                 p+=l;
1241                 nc+=l+2;
1242                 }
1243
1244         if (0)
1245                 {
1246 cont:
1247                 ERR_clear_error();
1248                 }
1249
1250         /* we should setup a certficate to return.... */
1251         s->s3->tmp.cert_req=1;
1252         s->s3->tmp.ctype_num=ctype_num;
1253         if (s->s3->tmp.ca_names != NULL)
1254                 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
1255         s->s3->tmp.ca_names=ca_sk;
1256         ca_sk=NULL;
1257
1258         ret=1;
1259 err:
1260         if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
1261         return(ret);
1262         }
1263
1264 static int ca_dn_cmp(X509_NAME **a, X509_NAME **b)
1265         {
1266         return(X509_NAME_cmp(*a,*b));
1267         }
1268
1269 static int ssl3_get_server_done(SSL *s)
1270         {
1271         int ok,ret=0;
1272         long n;
1273
1274         n=ssl3_get_message(s,
1275                 SSL3_ST_CR_SRVR_DONE_A,
1276                 SSL3_ST_CR_SRVR_DONE_B,
1277                 SSL3_MT_SERVER_DONE,
1278                 30, /* should be very small, like 0 :-) */
1279                 &ok);
1280
1281         if (!ok) return((int)n);
1282         if (n > 0)
1283                 {
1284                 /* should contain no data */
1285                 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
1286                 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
1287                 }
1288         ret=1;
1289         return(ret);
1290         }
1291
1292 static int ssl3_send_client_key_exchange(SSL *s)
1293         {
1294         unsigned char *p,*d;
1295         int n;
1296         unsigned long l;
1297 #ifndef NO_RSA
1298         unsigned char *q;
1299         EVP_PKEY *pkey=NULL;
1300 #endif
1301
1302         if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1303                 {
1304                 d=(unsigned char *)s->init_buf->data;
1305                 p= &(d[4]);
1306
1307                 l=s->s3->tmp.new_cipher->algorithms;
1308
1309 #ifndef NO_RSA
1310                 if (l & SSL_kRSA)
1311                         {
1312                         RSA *rsa;
1313                         unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1314
1315                         if (s->session->cert->rsa_tmp != NULL)
1316                                 rsa=s->session->cert->rsa_tmp;
1317                         else
1318                                 {
1319                                 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509);
1320                                 if ((pkey == NULL) ||
1321                                         (pkey->type != EVP_PKEY_RSA) ||
1322                                         (pkey->pkey.rsa == NULL))
1323                                         {
1324                                         SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1325                                         goto err;
1326                                         }
1327                                 rsa=pkey->pkey.rsa;
1328                                 }
1329                                 
1330                         tmp_buf[0]=s->client_version>>8;
1331                         tmp_buf[1]=s->client_version&0xff;
1332                         RAND_bytes(&(tmp_buf[2]),SSL_MAX_MASTER_KEY_LENGTH-2);
1333
1334                         s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
1335
1336                         q=p;
1337                         /* Fix buf for TLS and beyond */
1338                         if (s->version > SSL3_VERSION)
1339                                 p+=2;
1340                         n=RSA_public_encrypt(SSL_MAX_MASTER_KEY_LENGTH,
1341                                 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
1342 #ifdef PKCS1_CHECK
1343                         if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
1344                         if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
1345 #endif
1346                         if (n <= 0)
1347                                 {
1348                                 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
1349                                 goto err;
1350                                 }
1351
1352                         /* Fix buf for TLS and beyond */
1353                         if (s->version > SSL3_VERSION)
1354                                 {
1355                                 s2n(n,q);
1356                                 n+=2;
1357                                 }
1358
1359                         s->session->master_key_length=
1360                                 s->method->ssl3_enc->generate_master_secret(s,
1361                                         s->session->master_key,
1362                                         tmp_buf,SSL_MAX_MASTER_KEY_LENGTH);
1363                         memset(tmp_buf,0,SSL_MAX_MASTER_KEY_LENGTH);
1364                         }
1365                 else
1366 #endif
1367 #ifndef NO_DH
1368                 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1369                         {
1370                         DH *dh_srvr,*dh_clnt;
1371
1372                         if (s->session->cert->dh_tmp != NULL)
1373                                 dh_srvr=s->session->cert->dh_tmp;
1374                         else
1375                                 {
1376                                 /* we get them from the cert */
1377                                 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1378                                 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1379                                 goto err;
1380                                 }
1381                         
1382                         /* generate a new random key */
1383                         if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
1384                                 {
1385                                 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1386                                 goto err;
1387                                 }
1388                         if (!DH_generate_key(dh_clnt))
1389                                 {
1390                                 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1391                                 goto err;
1392                                 }
1393
1394                         /* use the 'p' output buffer for the DH key, but
1395                          * make sure to clear it out afterwards */
1396
1397                         n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
1398
1399                         if (n <= 0)
1400                                 {
1401                                 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1402                                 goto err;
1403                                 }
1404
1405                         /* generate master key from the result */
1406                         s->session->master_key_length=
1407                                 s->method->ssl3_enc->generate_master_secret(s,
1408                                         s->session->master_key,p,n);
1409                         /* clean up */
1410                         memset(p,0,n);
1411
1412                         /* send off the data */
1413                         n=BN_num_bytes(dh_clnt->pub_key);
1414                         s2n(n,p);
1415                         BN_bn2bin(dh_clnt->pub_key,p);
1416                         n+=2;
1417
1418                         DH_free(dh_clnt);
1419
1420                         /* perhaps clean things up a bit EAY EAY EAY EAY*/
1421                         }
1422                 else
1423 #endif
1424                         {
1425                         ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1426                         SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1427                         goto err;
1428                         }
1429                 
1430                 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1431                 l2n3(n,d);
1432
1433                 s->state=SSL3_ST_CW_KEY_EXCH_B;
1434                 /* number of bytes to write */
1435                 s->init_num=n+4;
1436                 s->init_off=0;
1437                 }
1438
1439         /* SSL3_ST_CW_KEY_EXCH_B */
1440         return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1441 err:
1442         return(-1);
1443         }
1444
1445 static int ssl3_send_client_verify(SSL *s)
1446         {
1447         unsigned char *p,*d;
1448         unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1449         EVP_PKEY *pkey;
1450 #ifndef NO_RSA
1451         int i=0;
1452 #endif
1453         unsigned long n;
1454 #ifndef NO_DSA
1455         int j;
1456 #endif
1457
1458         if (s->state == SSL3_ST_CW_CERT_VRFY_A)
1459                 {
1460                 d=(unsigned char *)s->init_buf->data;
1461                 p= &(d[4]);
1462                 pkey=s->cert->key->privatekey;
1463
1464                 s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
1465                         &(data[MD5_DIGEST_LENGTH]));
1466
1467 #ifndef NO_RSA
1468                 if (pkey->type == EVP_PKEY_RSA)
1469                         {
1470                         s->method->ssl3_enc->cert_verify_mac(s,
1471                                 &(s->s3->finish_dgst1),&(data[0]));
1472                         i=RSA_private_encrypt(
1473                                 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1474                                 data,&(p[2]),pkey->pkey.rsa,
1475                                 RSA_PKCS1_PADDING);
1476                         if (i <= 0)
1477                                 {
1478                                 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
1479                                 goto err;
1480                                 }
1481                         s2n(i,p);
1482                         n=i+2;
1483                         }
1484                 else
1485 #endif
1486 #ifndef NO_DSA
1487                         if (pkey->type == EVP_PKEY_DSA)
1488                         {
1489                         if (!DSA_sign(pkey->save_type,
1490                                 &(data[MD5_DIGEST_LENGTH]),
1491                                 SHA_DIGEST_LENGTH,&(p[2]),
1492                                 (unsigned int *)&j,pkey->pkey.dsa))
1493                                 {
1494                                 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
1495                                 goto err;
1496                                 }
1497                         s2n(j,p);
1498                         n=j+2;
1499                         }
1500                 else
1501 #endif
1502                         {
1503                         SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,SSL_R_INTERNAL_ERROR);
1504                         goto err;
1505                         }
1506                 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
1507                 l2n3(n,d);
1508
1509                 s->init_num=(int)n+4;
1510                 s->init_off=0;
1511                 }
1512         return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1513 err:
1514         return(-1);
1515         }
1516
1517 static int ssl3_send_client_certificate(SSL *s)
1518         {
1519         X509 *x509=NULL;
1520         EVP_PKEY *pkey=NULL;
1521         int i;
1522         unsigned long l;
1523
1524         if (s->state == SSL3_ST_CW_CERT_A)
1525                 {
1526                 if ((s->cert == NULL) ||
1527                         (s->cert->key->x509 == NULL) ||
1528                         (s->cert->key->privatekey == NULL))
1529                         s->state=SSL3_ST_CW_CERT_B;
1530                 else
1531                         s->state=SSL3_ST_CW_CERT_C;
1532                 }
1533
1534         /* We need to get a client cert */
1535         if (s->state == SSL3_ST_CW_CERT_B)
1536                 {
1537                 /* If we get an error, we need to
1538                  * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1539                  * We then get retied later */
1540                 i=0;
1541                 if (s->ctx->client_cert_cb != NULL)
1542                         i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
1543                 if (i < 0)
1544                         {
1545                         s->rwstate=SSL_X509_LOOKUP;
1546                         return(-1);
1547                         }
1548                 s->rwstate=SSL_NOTHING;
1549                 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
1550                         {
1551                         s->state=SSL3_ST_CW_CERT_B;
1552                         if (    !SSL_use_certificate(s,x509) ||
1553                                 !SSL_use_PrivateKey(s,pkey))
1554                                 i=0;
1555                         }
1556                 else if (i == 1)
1557                         {
1558                         i=0;
1559                         SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1560                         }
1561
1562                 if (x509 != NULL) X509_free(x509);
1563                 if (pkey != NULL) EVP_PKEY_free(pkey);
1564                 if (i == 0)
1565                         {
1566                         if (s->version == SSL3_VERSION)
1567                                 {
1568                                 s->s3->tmp.cert_req=0;
1569                                 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
1570                                 return(1);
1571                                 }
1572                         else
1573                                 {
1574                                 s->s3->tmp.cert_req=2;
1575                                 }
1576                         }
1577
1578                 /* Ok, we have a cert */
1579                 s->state=SSL3_ST_CW_CERT_C;
1580                 }
1581
1582         if (s->state == SSL3_ST_CW_CERT_C)
1583                 {
1584                 s->state=SSL3_ST_CW_CERT_D;
1585                 l=ssl3_output_cert_chain(s,
1586                         (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
1587                 s->init_num=(int)l;
1588                 s->init_off=0;
1589                 }
1590         /* SSL3_ST_CW_CERT_D */
1591         return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1592         }
1593
1594 #define has_bits(i,m)   (((i)&(m)) == (m))
1595
1596 static int ssl3_check_cert_and_algorithm(SSL *s)
1597         {
1598         int i,idx;
1599         long algs;
1600         EVP_PKEY *pkey=NULL;
1601         CERT *c;
1602 #ifndef NO_RSA
1603         RSA *rsa;
1604 #endif
1605 #ifndef NO_DH
1606         DH *dh;
1607 #endif
1608
1609         c=s->session->cert;
1610
1611         if (c == NULL)
1612                 {
1613                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_INTERNAL_ERROR);
1614                 goto err;
1615                 }
1616
1617         algs=s->s3->tmp.new_cipher->algorithms;
1618
1619         /* we don't have a certificate */
1620         if (algs & (SSL_aDH|SSL_aNULL))
1621                 return(1);
1622
1623 #ifndef NO_RSA
1624         rsa=s->session->cert->rsa_tmp;
1625 #endif
1626 #ifndef NO_DH
1627         dh=s->session->cert->dh_tmp;
1628 #endif
1629
1630         /* This is the passed certificate */
1631
1632         idx=c->cert_type;
1633         pkey=X509_get_pubkey(c->pkeys[idx].x509);
1634         i=X509_certificate_type(c->pkeys[idx].x509,pkey);
1635         EVP_PKEY_free(pkey);
1636
1637         
1638         /* Check that we have a certificate if we require one */
1639         if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
1640                 {
1641                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
1642                 goto f_err;
1643                 }
1644 #ifndef NO_DSA
1645         else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
1646                 {
1647                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
1648                 goto f_err;
1649                 }
1650 #endif
1651 #ifndef NO_RSA
1652         if ((algs & SSL_kRSA) &&
1653                 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
1654                 {
1655                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
1656                 goto f_err;
1657                 }
1658 #endif
1659 #ifndef NO_DH
1660         if ((algs & SSL_kEDH) &&
1661                 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
1662                 {
1663                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
1664                 goto f_err;
1665                 }
1666         else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
1667                 {
1668                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
1669                 goto f_err;
1670                 }
1671 #ifndef NO_DSA
1672         else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
1673                 {
1674                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
1675                 goto f_err;
1676                 }
1677 #endif
1678 #endif
1679
1680         if (SSL_IS_EXPORT(algs) && !has_bits(i,EVP_PKT_EXP))
1681                 {
1682 #ifndef NO_RSA
1683                 if (algs & SSL_kRSA)
1684                         {
1685                         if (rsa == NULL
1686                             || RSA_size(rsa) > SSL_EXPORT_PKEYLENGTH(algs))
1687                                 {
1688                                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
1689                                 goto f_err;
1690                                 }
1691                         }
1692                 else
1693 #endif
1694 #ifndef NO_DH
1695                         if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1696                             {
1697                             if (dh == NULL
1698                                 || DH_size(dh) > SSL_EXPORT_PKEYLENGTH(algs))
1699                                 {
1700                                 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
1701                                 goto f_err;
1702                                 }
1703                         }
1704                 else
1705 #endif
1706                         {
1707                         SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1708                         goto f_err;
1709                         }
1710                 }
1711         return(1);
1712 f_err:
1713         ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1714 err:
1715         return(0);
1716         }
1717