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