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