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