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