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