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