Change functions to ANSI C.
[openssl.git] / ssl / s2_clnt.c
1 /* ssl/s2_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 "rand.h"
61 #include "buffer.h"
62 #include "objects.h"
63 #include "ssl_locl.h"
64 #include "evp.h"
65
66 #ifndef NOPROTO
67 static SSL_METHOD *ssl2_get_client_method(int ver);
68 static int get_server_finished(SSL *s);
69 static int get_server_verify(SSL *s);
70 static int get_server_hello(SSL *s);
71 static int client_hello(SSL *s); 
72 static int client_master_key(SSL *s);
73 static int client_finished(SSL *s);
74 static int client_certificate(SSL *s);
75 static int ssl_rsa_public_encrypt(CERT *c, int len, unsigned char *from,
76         unsigned char *to,int padding);
77 #else
78 static SSL_METHOD *ssl2_get_client_method();
79 static int get_server_finished();
80 static int get_server_verify();
81 static int get_server_hello();
82 static int client_hello(); 
83 static int client_master_key();
84 static int client_finished();
85 static int client_certificate();
86 static int ssl_rsa_public_encrypt();
87 #endif
88
89 #define BREAK   break
90
91 static SSL_METHOD *ssl2_get_client_method(int ver)
92         {
93         if (ver == SSL2_VERSION)
94                 return(SSLv2_client_method());
95         else
96                 return(NULL);
97         }
98
99 SSL_METHOD *SSLv2_client_method(void)
100         {
101         static int init=1;
102         static SSL_METHOD SSLv2_client_data;
103
104         if (init)
105                 {
106                 init=0;
107                 memcpy((char *)&SSLv2_client_data,(char *)sslv2_base_method(),
108                         sizeof(SSL_METHOD));
109                 SSLv2_client_data.ssl_connect=ssl2_connect;
110                 SSLv2_client_data.get_ssl_method=ssl2_get_client_method;
111                 }
112         return(&SSLv2_client_data);
113         }
114
115 int ssl2_connect(SSL *s)
116         {
117         unsigned long l=time(NULL);
118         BUF_MEM *buf=NULL;
119         int ret= -1;
120         void (*cb)()=NULL;
121         int new_state,state;
122
123         RAND_seed(&l,sizeof(l));
124         ERR_clear_error();
125         clear_sys_error();
126
127         if (s->info_callback != NULL)
128                 cb=s->info_callback;
129         else if (s->ctx->info_callback != NULL)
130                 cb=s->ctx->info_callback;
131
132         /* init things to blank */
133         if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
134         s->in_handshake++;
135
136         for (;;)
137                 {
138                 state=s->state;
139
140                 switch (s->state)
141                         {
142                 case SSL_ST_BEFORE:
143                 case SSL_ST_CONNECT:
144                 case SSL_ST_BEFORE|SSL_ST_CONNECT:
145                 case SSL_ST_OK|SSL_ST_CONNECT:
146
147                         s->server=0;
148                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
149
150                         s->version=SSL2_VERSION;
151                         s->type=SSL_ST_CONNECT;
152
153                         buf=s->init_buf;
154                         if ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL))
155                                 {
156                                 ret= -1;
157                                 goto end;
158                                 }
159                         if (!BUF_MEM_grow(buf,
160                                 SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
161                                 {
162                                 ret= -1;
163                                 goto end;
164                                 }
165                         s->init_buf=buf;
166                         s->init_num=0;
167                         s->state=SSL2_ST_SEND_CLIENT_HELLO_A;
168                         s->ctx->stats.sess_connect++;
169                         s->handshake_func=ssl2_connect;
170                         BREAK;
171
172                 case SSL2_ST_SEND_CLIENT_HELLO_A:
173                 case SSL2_ST_SEND_CLIENT_HELLO_B:
174                         s->shutdown=0;
175                         ret=client_hello(s);
176                         if (ret <= 0) goto end;
177                         s->init_num=0;
178                         s->state=SSL2_ST_GET_SERVER_HELLO_A;
179                         BREAK;
180                 
181                 case SSL2_ST_GET_SERVER_HELLO_A:
182                 case SSL2_ST_GET_SERVER_HELLO_B:
183                         ret=get_server_hello(s);
184                         if (ret <= 0) goto end;
185                         s->init_num=0;
186                         if (!s->hit) /* new session */
187                                 {
188                                 s->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_A;
189                                 BREAK; 
190                                 }
191                         else
192                                 {
193                                 s->state=SSL2_ST_CLIENT_START_ENCRYPTION;
194                                 break;
195                                 }
196         
197                 case SSL2_ST_SEND_CLIENT_MASTER_KEY_A:
198                 case SSL2_ST_SEND_CLIENT_MASTER_KEY_B:
199                         ret=client_master_key(s);
200                         if (ret <= 0) goto end;
201                         s->init_num=0;
202                         s->state=SSL2_ST_CLIENT_START_ENCRYPTION;
203                         break;
204
205                 case SSL2_ST_CLIENT_START_ENCRYPTION:
206                         /* Ok, we now have all the stuff needed to
207                          * start encrypting, so lets fire it up :-) */
208                         if (!ssl2_enc_init(s,1))
209                                 {
210                                 ret= -1;
211                                 goto end;
212                                 }
213                         s->s2->clear_text=0;
214                         s->state=SSL2_ST_SEND_CLIENT_FINISHED_A;
215                         break;
216
217                 case SSL2_ST_SEND_CLIENT_FINISHED_A:
218                 case SSL2_ST_SEND_CLIENT_FINISHED_B:
219                         ret=client_finished(s);
220                         if (ret <= 0) goto end;
221                         s->init_num=0;
222                         s->state=SSL2_ST_GET_SERVER_VERIFY_A;
223                         break;
224
225                 case SSL2_ST_GET_SERVER_VERIFY_A:
226                 case SSL2_ST_GET_SERVER_VERIFY_B:
227                         ret=get_server_verify(s);
228                         if (ret <= 0) goto end;
229                         s->init_num=0;
230                         s->state=SSL2_ST_GET_SERVER_FINISHED_A;
231                         break;
232
233                 case SSL2_ST_GET_SERVER_FINISHED_A:
234                 case SSL2_ST_GET_SERVER_FINISHED_B:
235                         ret=get_server_finished(s);
236                         if (ret <= 0) goto end;
237                         break;
238
239                 case SSL2_ST_SEND_CLIENT_CERTIFICATE_A:
240                 case SSL2_ST_SEND_CLIENT_CERTIFICATE_B:
241                 case SSL2_ST_SEND_CLIENT_CERTIFICATE_C:
242                 case SSL2_ST_SEND_CLIENT_CERTIFICATE_D:
243                 case SSL2_ST_X509_GET_CLIENT_CERTIFICATE:
244                         ret=client_certificate(s);
245                         if (ret <= 0) goto end;
246                         s->init_num=0;
247                         s->state=SSL2_ST_GET_SERVER_FINISHED_A;
248                         break;
249
250                 case SSL_ST_OK:
251                         if (s->init_buf != NULL)
252                                 {
253                                 BUF_MEM_free(s->init_buf);
254                                 s->init_buf=NULL;
255                                 }
256                         s->init_num=0;
257                 /*      ERR_clear_error();*/
258
259                         /* If we want to cache session-ids in the client
260                          * and we sucessfully add the session-id to the
261                          * cache, and there is a callback, then pass it out.
262                          * 26/11/96 - eay - only add if not a re-used session.
263                          */
264
265                         ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
266                         if (s->hit) s->ctx->stats.sess_hit++;
267
268                         ret=1;
269                         /* s->server=0; */
270                         s->ctx->stats.sess_connect_good++;
271
272                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
273
274                         goto end;
275                         /* break; */
276                 default:
277                         SSLerr(SSL_F_SSL2_CONNECT,SSL_R_UNKNOWN_STATE);
278                         return(-1);
279                         /* break; */
280                         }
281
282                 if ((cb != NULL) && (s->state != state))
283                         {
284                         new_state=s->state;
285                         s->state=state;
286                         cb(s,SSL_CB_CONNECT_LOOP,1);
287                         s->state=new_state;
288                         }
289                 }
290 end:
291         s->in_handshake--;
292         if (cb != NULL) 
293                 cb(s,SSL_CB_CONNECT_EXIT,ret);
294         return(ret);
295         }
296
297 static int get_server_hello(SSL *s)
298         {
299         unsigned char *buf;
300         unsigned char *p;
301         int i,j;
302         STACK_OF(SSL_CIPHER) *sk=NULL,*cl;
303
304         buf=(unsigned char *)s->init_buf->data;
305         p=buf;
306         if (s->state == SSL2_ST_GET_SERVER_HELLO_A)
307                 {
308                 i=ssl2_read(s,(char *)&(buf[s->init_num]),11-s->init_num);
309                 if (i < (11-s->init_num)) 
310                         return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i));
311
312                 if (*(p++) != SSL2_MT_SERVER_HELLO)
313                         {
314                         if (p[-1] != SSL2_MT_ERROR)
315                                 {
316                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
317                                 SSLerr(SSL_F_GET_SERVER_HELLO,
318                                         SSL_R_READ_WRONG_PACKET_TYPE);
319                                 }
320                         else
321                                 SSLerr(SSL_F_GET_SERVER_HELLO,
322                                         SSL_R_PEER_ERROR);
323                         return(-1);
324                         }
325                 s->hit=(*(p++))?1:0;
326                 s->s2->tmp.cert_type= *(p++);
327                 n2s(p,i);
328                 if (i < s->version) s->version=i;
329                 n2s(p,i); s->s2->tmp.cert_length=i;
330                 n2s(p,i); s->s2->tmp.csl=i;
331                 n2s(p,i); s->s2->tmp.conn_id_length=i;
332                 s->state=SSL2_ST_GET_SERVER_HELLO_B;
333                 s->init_num=0;
334                 }
335
336         /* SSL2_ST_GET_SERVER_HELLO_B */
337         j=s->s2->tmp.cert_length+s->s2->tmp.csl+s->s2->tmp.conn_id_length
338                 - s->init_num;
339         i=ssl2_read(s,(char *)&(buf[s->init_num]),j);
340         if (i != j) return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i));
341
342         /* things are looking good */
343
344         p=buf;
345         if (s->hit)
346                 {
347                 if (s->s2->tmp.cert_length != 0) 
348                         {
349                         SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_LENGTH_NOT_ZERO);
350                         return(-1);
351                         }
352                 if (s->s2->tmp.cert_type != 0)
353                         {
354                         if (!(s->options &
355                                 SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG))
356                                 {
357                                 SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_TYPE_NOT_ZERO);
358                                 return(-1);
359                                 }
360                         }
361                 if (s->s2->tmp.csl != 0)
362                         {
363                         SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CIPHER_LIST_NOT_ZERO);
364                         return(-1);
365                         }
366                 }
367         else
368                 {
369 #ifdef undef
370                 /* very bad */
371                 memset(s->session->session_id,0,
372                         SSL_MAX_SSL_SESSION_ID_LENGTH_IN_BYTES);
373                 s->session->session_id_length=0;
374                 */
375 #endif
376
377                 /* we need to do this incase we were trying to reuse a 
378                  * client session but others are already reusing it.
379                  * If this was a new 'blank' session ID, the session-id
380                  * length will still be 0 */
381                 if (s->session->session_id_length > 0)
382                         {
383                         if (!ssl_get_new_session(s,0))
384                                 {
385                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
386                                 return(-1);
387                                 }
388                         }
389
390                 if (ssl2_set_certificate(s,s->s2->tmp.cert_type,
391                         s->s2->tmp.cert_length,p) <= 0)
392                         {
393                         ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
394                         return(-1);
395                         }
396                 p+=s->s2->tmp.cert_length;
397
398                 if (s->s2->tmp.csl == 0)
399                         {
400                         ssl2_return_error(s,SSL2_PE_NO_CIPHER);
401                         SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_LIST);
402                         return(-1);
403                         }
404
405                 /* We have just received a list of ciphers back from the
406                  * server.  We need to get the ones that match, then select
407                  * the one we want the most :-). */
408
409                 /* load the ciphers */
410                 sk=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.csl,
411                                             &s->session->ciphers);
412                 p+=s->s2->tmp.csl;
413                 if (sk == NULL)
414                         {
415                         ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
416                         SSLerr(SSL_F_GET_SERVER_HELLO,ERR_R_MALLOC_FAILURE);
417                         return(-1);
418                         }
419
420                 sk_SSL_CIPHER_set_cmp_func(sk,ssl_cipher_ptr_id_cmp);
421
422                 /* get the array of ciphers we will accept */
423                 cl=ssl_get_ciphers_by_id(s);
424
425                 /* In theory we could have ciphers sent back that we
426                  * don't want to use but that does not matter since we
427                  * will check against the list we origionally sent and
428                  * for performance reasons we should not bother to match
429                  * the two lists up just to check. */
430                 for (i=0; i<sk_SSL_CIPHER_num(cl); i++)
431                         {
432                         if (sk_SSL_CIPHER_find(sk,
433                                                sk_SSL_CIPHER_value(cl,i)) >= 0)
434                                 break;
435                         }
436
437                 if (i >= sk_SSL_CIPHER_num(cl))
438                         {
439                         ssl2_return_error(s,SSL2_PE_NO_CIPHER);
440                         SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_MATCH);
441                         return(-1);
442                         }
443                 s->session->cipher=sk_SSL_CIPHER_value(cl,i);
444                 }
445
446         if ((s->session != NULL) && (s->session->peer != NULL))
447                 X509_free(s->session->peer);
448
449         /* hmmm, can we have the problem of the other session with this
450          * cert, Free's it before we increment the reference count. */
451         CRYPTO_w_lock(CRYPTO_LOCK_X509);
452         s->session->peer=s->session->cert->key->x509;
453         CRYPTO_add(&s->session->peer->references,1,CRYPTO_LOCK_X509);
454         CRYPTO_w_unlock(CRYPTO_LOCK_X509);
455
456         s->s2->conn_id_length=s->s2->tmp.conn_id_length;
457         memcpy(s->s2->conn_id,p,s->s2->tmp.conn_id_length);
458         return(1);
459         }
460
461 static int client_hello(SSL *s)
462         {
463         unsigned char *buf;
464         unsigned char *p,*d;
465 /*      CIPHER **cipher;*/
466         int i,n,j;
467
468         buf=(unsigned char *)s->init_buf->data;
469         if (s->state == SSL2_ST_SEND_CLIENT_HELLO_A)
470                 {
471                 if ((s->session == NULL) ||
472                         (s->session->ssl_version != s->version))
473                         {
474                         if (!ssl_get_new_session(s,0))
475                                 {
476                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
477                                 return(-1);
478                                 }
479                         }
480                 /* else use the pre-loaded session */
481
482                 p=buf;                                  /* header */
483                 d=p+9;                                  /* data section */
484                 *(p++)=SSL2_MT_CLIENT_HELLO;            /* type */
485                 s2n(SSL2_VERSION,p);                    /* version */
486                 n=j=0;
487
488                 n=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),d);
489                 d+=n;
490
491                 if (n == 0)
492                         {
493                         SSLerr(SSL_F_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
494                         return(-1);
495                         }
496
497                 s2n(n,p);                       /* cipher spec num bytes */
498
499                 if ((s->session->session_id_length > 0) &&
500                         (s->session->session_id_length <=
501                         SSL2_MAX_SSL_SESSION_ID_LENGTH))
502                         {
503                         i=s->session->session_id_length;
504                         s2n(i,p);               /* session id length */
505                         memcpy(d,s->session->session_id,(unsigned int)i);
506                         d+=i;
507                         }
508                 else
509                         {
510                         s2n(0,p);
511                         }
512
513                 s->s2->challenge_length=SSL2_CHALLENGE_LENGTH;
514                 s2n(SSL2_CHALLENGE_LENGTH,p);           /* challenge length */
515                 /*challenge id data*/
516                 RAND_bytes(s->s2->challenge,SSL2_CHALLENGE_LENGTH);
517                 memcpy(d,s->s2->challenge,SSL2_CHALLENGE_LENGTH);
518                 d+=SSL2_CHALLENGE_LENGTH;
519
520                 s->state=SSL2_ST_SEND_CLIENT_HELLO_B;
521                 s->init_num=d-buf;
522                 s->init_off=0;
523                 }
524         /* SSL2_ST_SEND_CLIENT_HELLO_B */
525         return(ssl2_do_write(s));
526         }
527
528 static int client_master_key(SSL *s)
529         {
530         unsigned char *buf;
531         unsigned char *p,*d;
532         int clear,enc,karg,i;
533         SSL_SESSION *sess;
534         const EVP_CIPHER *c;
535         const EVP_MD *md;
536
537         buf=(unsigned char *)s->init_buf->data;
538         if (s->state == SSL2_ST_SEND_CLIENT_MASTER_KEY_A)
539                 {
540
541                 if (!ssl_cipher_get_evp(s->session,&c,&md,NULL))
542                         {
543                         ssl2_return_error(s,SSL2_PE_NO_CIPHER);
544                         SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
545                         return(-1);
546                         }
547                 sess=s->session;
548                 p=buf;
549                 d=p+10;
550                 *(p++)=SSL2_MT_CLIENT_MASTER_KEY;/* type */
551
552                 i=ssl_put_cipher_by_char(s,sess->cipher,p);
553                 p+=i;
554
555                 /* make key_arg data */
556                 i=EVP_CIPHER_iv_length(c);
557                 sess->key_arg_length=i;
558                 if (i > 0) RAND_bytes(sess->key_arg,i);
559
560                 /* make a master key */
561                 i=EVP_CIPHER_key_length(c);
562                 sess->master_key_length=i;
563                 if (i > 0) RAND_bytes(sess->master_key,i);
564
565                 if (sess->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
566                         enc=8;
567                 else if (SSL_C_IS_EXPORT(sess->cipher))
568                         enc=5;
569                 else
570                         enc=i;
571
572                 if (i < enc)
573                         {
574                         ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
575                         SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_CIPHER_TABLE_SRC_ERROR);
576                         return(-1);
577                         }
578                 clear=i-enc;
579                 s2n(clear,p);
580                 memcpy(d,sess->master_key,(unsigned int)clear);
581                 d+=clear;
582
583                 enc=ssl_rsa_public_encrypt(sess->cert,enc,
584                         &(sess->master_key[clear]),d,
585                         (s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING);
586                 if (enc <= 0)
587                         {
588                         ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
589                         SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PUBLIC_KEY_ENCRYPT_ERROR);
590                         return(-1);
591                         }
592 #ifdef PKCS1_CHECK
593                 if (s->options & SSL_OP_PKCS1_CHECK_1) d[1]++;
594                 if (s->options & SSL_OP_PKCS1_CHECK_2)
595                         sess->master_key[clear]++;
596 #endif
597                 s2n(enc,p);
598                 d+=enc;
599                 karg=sess->key_arg_length;      
600                 s2n(karg,p); /* key arg size */
601                 memcpy(d,sess->key_arg,(unsigned int)karg);
602                 d+=karg;
603
604                 s->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_B;
605                 s->init_num=d-buf;
606                 s->init_off=0;
607                 }
608
609         /* SSL2_ST_SEND_CLIENT_MASTER_KEY_B */
610         return(ssl2_do_write(s));
611         }
612
613 static int client_finished(SSL *s)
614         {
615         unsigned char *p;
616
617         if (s->state == SSL2_ST_SEND_CLIENT_FINISHED_A)
618                 {
619                 p=(unsigned char *)s->init_buf->data;
620                 *(p++)=SSL2_MT_CLIENT_FINISHED;
621                 memcpy(p,s->s2->conn_id,(unsigned int)s->s2->conn_id_length);
622
623                 s->state=SSL2_ST_SEND_CLIENT_FINISHED_B;
624                 s->init_num=s->s2->conn_id_length+1;
625                 s->init_off=0;
626                 }
627         return(ssl2_do_write(s));
628         }
629
630 /* read the data and then respond */
631 static int client_certificate(SSL *s)
632         {
633         unsigned char *buf;
634         unsigned char *p,*d;
635         int i;
636         unsigned int n;
637         int cert_ch_len=0;
638         unsigned char *cert_ch;
639
640         buf=(unsigned char *)s->init_buf->data;
641         cert_ch= &(buf[2]);
642
643         /* We have a cert associated with the SSL, so attach it to
644          * the session if it does not have one */
645
646         if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_A)
647                 {
648                 i=ssl2_read(s,(char *)&(buf[s->init_num]),
649                         SSL2_MAX_CERT_CHALLENGE_LENGTH+1-s->init_num);
650                 if (i<(SSL2_MIN_CERT_CHALLENGE_LENGTH+1-s->init_num))
651                         return(ssl2_part_read(s,SSL_F_CLIENT_CERTIFICATE,i));
652
653                 /* type=buf[0]; */
654                 /* type eq x509 */
655                 if (buf[1] != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)
656                         {
657                         ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
658                         SSLerr(SSL_F_CLIENT_CERTIFICATE,SSL_R_BAD_AUTHENTICATION_TYPE);
659                         return(-1);
660                         }
661                 cert_ch_len=i-1;
662
663                 if ((s->cert == NULL) ||
664                         (s->cert->key->x509 == NULL) ||
665                         (s->cert->key->privatekey == NULL))
666                         {
667                         s->state=SSL2_ST_X509_GET_CLIENT_CERTIFICATE;
668                         }
669                 else
670                         s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_C;
671                 }
672
673         if (s->state == SSL2_ST_X509_GET_CLIENT_CERTIFICATE)
674                 {
675                 X509 *x509=NULL;
676                 EVP_PKEY *pkey=NULL;
677
678                 /* If we get an error we need to
679                  * ssl->rwstate=SSL_X509_LOOKUP;
680                  * return(error);
681                  * We should then be retried when things are ok and we
682                  * can get a cert or not */
683
684                 i=0;
685                 if (s->ctx->client_cert_cb != NULL)
686                         {
687                         i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
688                         }
689
690                 if (i < 0)
691                         {
692                         s->rwstate=SSL_X509_LOOKUP;
693                         return(-1);
694                         }
695                 s->rwstate=SSL_NOTHING;
696
697                 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
698                         {
699                         s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_C;
700                         if (    !SSL_use_certificate(s,x509) || 
701                                 !SSL_use_PrivateKey(s,pkey))
702                                 {
703                                 i=0;
704                                 }
705                         X509_free(x509);
706                         EVP_PKEY_free(pkey);
707                         }
708                 else if (i == 1)
709                         {
710                         if (x509 != NULL) X509_free(x509);
711                         if (pkey != NULL) EVP_PKEY_free(pkey);
712                         SSLerr(SSL_F_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
713                         i=0;
714                         }
715
716                 if (i == 0)
717                         {
718                         /* We have no client certificate to respond with
719                          * so send the correct error message back */
720                         s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_B;
721                         p=buf;
722                         *(p++)=SSL2_MT_ERROR;
723                         s2n(SSL2_PE_NO_CERTIFICATE,p);
724                         s->init_off=0;
725                         s->init_num=3;
726                         /* Write is done at the end */
727                         }
728                 }
729
730         if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_B)
731                 {
732                 return(ssl2_do_write(s));
733                 }
734
735         if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_C)
736                 {
737                 EVP_MD_CTX ctx;
738
739                 /* ok, now we calculate the checksum
740                  * do it first so we can reuse buf :-) */
741                 p=buf;
742                 EVP_SignInit(&ctx,s->ctx->rsa_md5);
743                 EVP_SignUpdate(&ctx,s->s2->key_material,
744                         (unsigned int)s->s2->key_material_length);
745                 EVP_SignUpdate(&ctx,cert_ch,(unsigned int)cert_ch_len);
746                 n=i2d_X509(s->session->cert->key->x509,&p);
747                 EVP_SignUpdate(&ctx,buf,(unsigned int)n);
748
749                 p=buf;
750                 d=p+6;
751                 *(p++)=SSL2_MT_CLIENT_CERTIFICATE;
752                 *(p++)=SSL2_CT_X509_CERTIFICATE;
753                 n=i2d_X509(s->cert->key->x509,&d);
754                 s2n(n,p);
755
756                 if (!EVP_SignFinal(&ctx,d,&n,s->cert->key->privatekey))
757                         {
758                         /* this is not good.  If things have failed it
759                          * means there so something wrong with the key.
760                          * We will contiune with a 0 length signature
761                          */
762                         }
763                 memset(&ctx,0,sizeof(ctx));
764                 s2n(n,p);
765                 d+=n;
766
767                 s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_D;
768                 s->init_num=d-buf;
769                 s->init_off=0;
770                 }
771         /* if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_D) */
772         return(ssl2_do_write(s));
773         }
774
775 static int get_server_verify(SSL *s)
776         {
777         unsigned char *p;
778         int i;
779
780         p=(unsigned char *)s->init_buf->data;
781         if (s->state == SSL2_ST_GET_SERVER_VERIFY_A)
782                 {
783                 i=ssl2_read(s,(char *)&(p[s->init_num]),1-s->init_num);
784                 if (i < (1-s->init_num)) 
785                         return(ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i));
786
787                 s->state= SSL2_ST_GET_SERVER_VERIFY_B;
788                 s->init_num=0;
789                 if (*p != SSL2_MT_SERVER_VERIFY)
790                         {
791                         if (p[0] != SSL2_MT_ERROR)
792                                 {
793                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
794                                 SSLerr(SSL_F_GET_SERVER_VERIFY,
795                                         SSL_R_READ_WRONG_PACKET_TYPE);
796                                 }
797                         else
798                                 SSLerr(SSL_F_GET_SERVER_VERIFY,
799                                         SSL_R_PEER_ERROR);
800                         return(-1);
801                         }
802                 }
803         
804         p=(unsigned char *)s->init_buf->data;
805         i=ssl2_read(s,(char *)&(p[s->init_num]),
806                 (unsigned int)s->s2->challenge_length-s->init_num);
807         if (i < ((int)s->s2->challenge_length-s->init_num))
808                 return(ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i));
809         if (memcmp(p,s->s2->challenge,(unsigned int)s->s2->challenge_length) != 0)
810                 {
811                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
812                 SSLerr(SSL_F_GET_SERVER_VERIFY,SSL_R_CHALLENGE_IS_DIFFERENT);
813                 return(-1);
814                 }
815         return(1);
816         }
817
818 static int get_server_finished(SSL *s)
819         {
820         unsigned char *buf;
821         unsigned char *p;
822         int i;
823
824         buf=(unsigned char *)s->init_buf->data;
825         p=buf;
826         if (s->state == SSL2_ST_GET_SERVER_FINISHED_A)
827                 {
828                 i=ssl2_read(s,(char *)&(buf[s->init_num]),1-s->init_num);
829                 if (i < (1-s->init_num))
830                         return(ssl2_part_read(s,SSL_F_GET_SERVER_FINISHED,i));
831                 s->init_num=i;
832                 if (*p == SSL2_MT_REQUEST_CERTIFICATE)
833                         {
834                         s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_A;
835                         return(1);
836                         }
837                 else if (*p != SSL2_MT_SERVER_FINISHED)
838                         {
839                         if (p[0] != SSL2_MT_ERROR)
840                                 {
841                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
842                                 SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE);
843                                 }
844                         else
845                                 SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_PEER_ERROR);
846                         return(-1);
847                         }
848                 s->state=SSL_ST_OK;
849                 s->init_num=0;
850                 }
851
852         i=ssl2_read(s,(char *)&(buf[s->init_num]),
853                 SSL2_SSL_SESSION_ID_LENGTH-s->init_num);
854         if (i < (SSL2_SSL_SESSION_ID_LENGTH-s->init_num))
855                 return(ssl2_part_read(s,SSL_F_GET_SERVER_FINISHED,i));
856
857         if (!s->hit) /* new session */
858                 {
859                 /* new session-id */
860                 /* Make sure we were not trying to re-use an old SSL_SESSION
861                  * or bad things can happen */
862                 /* ZZZZZZZZZZZZZ */
863                 s->session->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
864                 memcpy(s->session->session_id,p,SSL2_SSL_SESSION_ID_LENGTH);
865                 }
866         else
867                 {
868                 if (!(s->options & SSL_OP_MICROSOFT_SESS_ID_BUG))
869                         {
870                         if (memcmp(buf,s->session->session_id,
871                                 (unsigned int)s->session->session_id_length) != 0)
872                                 {
873                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
874                                 SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_SSL_SESSION_ID_IS_DIFFERENT);
875                                 return(-1);
876                                 }
877                         }
878                 }
879         return(1);
880         }
881
882 /* loads in the certificate from the server */
883 int ssl2_set_certificate(SSL *s, int type, int len, unsigned char *data)
884         {
885         STACK_OF(X509) *sk=NULL;
886         EVP_PKEY *pkey=NULL;
887         CERT *c=NULL;
888         int i;
889         X509 *x509=NULL;
890         int ret=0;
891         
892         x509=d2i_X509(NULL,&data,(long)len);
893         if (x509 == NULL)
894                 {
895                 SSLerr(SSL_F_SSL2_SET_CERTIFICATE,ERR_R_X509_LIB);
896                 goto err;
897                 }
898
899         if ((sk=sk_X509_new_null()) == NULL || !sk_X509_push(sk,x509))
900                 {
901                 SSLerr(SSL_F_SSL2_SET_CERTIFICATE,ERR_R_MALLOC_FAILURE);
902                 goto err;
903                 }
904
905         i=ssl_verify_cert_chain(s,sk);
906                 
907         if ((s->verify_mode != SSL_VERIFY_NONE) && (!i))
908                 {
909                 SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
910                 goto err;
911                 }
912
913         /* cert for ssl */
914         c=ssl_cert_new();
915         if (c == NULL)
916                 {
917                 ret= -1;
918                 goto err;
919                 }
920
921         /* cert for session */
922         if (s->session->cert) ssl_cert_free(s->session->cert);
923         s->session->cert=c;
924
925 /*      c->cert_type=type; */
926
927         c->pkeys[SSL_PKEY_RSA_ENC].x509=x509;
928         c->key= &(c->pkeys[SSL_PKEY_RSA_ENC]);
929
930         pkey=X509_get_pubkey(x509);
931         x509=NULL;
932         if (pkey == NULL)
933                 {
934                 SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_UNABLE_TO_EXTRACT_PUBLIC_KEY);
935                 goto err;
936                 }
937         if (pkey->type != EVP_PKEY_RSA)
938                 {
939                 SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_PUBLIC_KEY_NOT_RSA);
940                 goto err;
941                 }
942
943         if (!ssl_set_cert_type(c,SSL2_CT_X509_CERTIFICATE))
944                 goto err;
945         ret=1;
946 err:
947         sk_X509_free(sk);
948         X509_free(x509);
949         EVP_PKEY_free(pkey);
950         return(ret);
951         }
952
953 static int ssl_rsa_public_encrypt(CERT *c, int len, unsigned char *from,
954              unsigned char *to, int padding)
955         {
956         EVP_PKEY *pkey=NULL;
957         int i= -1;
958
959         if ((c == NULL) || (c->key->x509 == NULL) ||
960                 ((pkey=X509_get_pubkey(c->key->x509)) == NULL))
961                 {
962                 SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,SSL_R_NO_PUBLICKEY);
963                 return(-1);
964                 }
965         if (pkey->type != EVP_PKEY_RSA)
966                 {
967                 SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA);
968                 goto end;
969                 }
970
971         /* we have the public key */
972         i=RSA_public_encrypt(len,from,to,pkey->pkey.rsa,padding);
973         if (i < 0)
974                 SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,ERR_R_RSA_LIB);
975 end:
976         EVP_PKEY_free(pkey);
977         return(i);
978         }
979