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