8c3cbde17711e55872c301ca77928073beeb32ec
[openssl.git] / ssl / s2_clnt.c
1 /* ssl/s2_clnt.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include "rand.h"
61 #include "buffer.h"
62 #include "objects.h"
63 #include "ssl_locl.h"
64 #include "evp.h"
65
66 #ifndef NOPROTO
67 static SSL_METHOD *ssl2_get_client_method(int ver);
68 static int get_server_finished(SSL *s);
69 static int get_server_verify(SSL *s);
70 static int get_server_hello(SSL *s);
71 static int client_hello(SSL *s); 
72 static int client_master_key(SSL *s);
73 static int client_finished(SSL *s);
74 static int client_certificate(SSL *s);
75 static int ssl_rsa_public_encrypt(CERT *c, int len, unsigned char *from,
76         unsigned char *to,int padding);
77 #else
78 static SSL_METHOD *ssl2_get_client_method();
79 static int get_server_finished();
80 static int get_server_verify();
81 static int get_server_hello();
82 static int client_hello(); 
83 static int client_master_key();
84 static int client_finished();
85 static int client_certificate();
86 static int ssl_rsa_public_encrypt();
87 #endif
88
89 #define BREAK   break
90
91 static SSL_METHOD *ssl2_get_client_method(ver)
92 int ver;
93         {
94         if (ver == SSL2_VERSION)
95                 return(SSLv2_client_method());
96         else
97                 return(NULL);
98         }
99
100 SSL_METHOD *SSLv2_client_method()
101         {
102         static int init=1;
103         static SSL_METHOD SSLv2_client_data;
104
105         if (init)
106                 {
107                 init=0;
108                 memcpy((char *)&SSLv2_client_data,(char *)sslv2_base_method(),
109                         sizeof(SSL_METHOD));
110                 SSLv2_client_data.ssl_connect=ssl2_connect;
111                 SSLv2_client_data.get_ssl_method=ssl2_get_client_method;
112                 }
113         return(&SSLv2_client_data);
114         }
115
116 int ssl2_connect(s)
117 SSL *s;
118         {
119         unsigned long l=time(NULL);
120         BUF_MEM *buf=NULL;
121         int ret= -1;
122         void (*cb)()=NULL;
123         int new_state,state;
124
125         RAND_seed(&l,sizeof(l));
126         ERR_clear_error();
127         clear_sys_error();
128
129         if (s->info_callback != NULL)
130                 cb=s->info_callback;
131         else if (s->ctx->info_callback != NULL)
132                 cb=s->ctx->info_callback;
133
134         /* init things to blank */
135         if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
136         s->in_handshake++;
137
138         for (;;)
139                 {
140                 state=s->state;
141
142                 switch (s->state)
143                         {
144                 case SSL_ST_BEFORE:
145                 case SSL_ST_CONNECT:
146                 case SSL_ST_BEFORE|SSL_ST_CONNECT:
147                 case SSL_ST_OK|SSL_ST_CONNECT:
148
149                         s->server=0;
150                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
151
152                         s->version=SSL2_VERSION;
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->stats.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                         if (s->init_buf != NULL)
254                                 {
255                                 BUF_MEM_free(s->init_buf);
256                                 s->init_buf=NULL;
257                                 }
258                         s->init_num=0;
259                 /*      ERR_clear_error();*/
260
261                         /* If we want to cache session-ids in the client
262                          * and we sucessfully add the session-id to the
263                          * cache, and there is a callback, then pass it out.
264                          * 26/11/96 - eay - only add if not a re-used session.
265                          */
266
267                         ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
268                         if (s->hit) s->ctx->stats.sess_hit++;
269
270                         ret=1;
271                         /* s->server=0; */
272                         s->ctx->stats.sess_connect_good++;
273
274                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
275
276                         goto end;
277                         /* break; */
278                 default:
279                         SSLerr(SSL_F_SSL2_CONNECT,SSL_R_UNKNOWN_STATE);
280                         return(-1);
281                         /* break; */
282                         }
283
284                 if ((cb != NULL) && (s->state != state))
285                         {
286                         new_state=s->state;
287                         s->state=state;
288                         cb(s,SSL_CB_CONNECT_LOOP,1);
289                         s->state=new_state;
290                         }
291                 }
292 end:
293         s->in_handshake--;
294         if (cb != NULL) 
295                 cb(s,SSL_CB_CONNECT_EXIT,ret);
296         return(ret);
297         }
298
299 static int get_server_hello(s)
300 SSL *s;
301         {
302         unsigned char *buf;
303         unsigned char *p;
304         int i,j;
305         STACK_OF(SSL_CIPHER) *sk=NULL,*cl;
306
307         buf=(unsigned char *)s->init_buf->data;
308         p=buf;
309         if (s->state == SSL2_ST_GET_SERVER_HELLO_A)
310                 {
311                 i=ssl2_read(s,(char *)&(buf[s->init_num]),11-s->init_num);
312                 if (i < (11-s->init_num)) 
313                         return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i));
314
315                 if (*(p++) != SSL2_MT_SERVER_HELLO)
316                         {
317                         if (p[-1] != SSL2_MT_ERROR)
318                                 {
319                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
320                                 SSLerr(SSL_F_GET_SERVER_HELLO,
321                                         SSL_R_READ_WRONG_PACKET_TYPE);
322                                 }
323                         else
324                                 SSLerr(SSL_F_GET_SERVER_HELLO,
325                                         SSL_R_PEER_ERROR);
326                         return(-1);
327                         }
328                 s->hit=(*(p++))?1:0;
329                 s->s2->tmp.cert_type= *(p++);
330                 n2s(p,i);
331                 if (i < s->version) s->version=i;
332                 n2s(p,i); s->s2->tmp.cert_length=i;
333                 n2s(p,i); s->s2->tmp.csl=i;
334                 n2s(p,i); s->s2->tmp.conn_id_length=i;
335                 s->state=SSL2_ST_GET_SERVER_HELLO_B;
336                 s->init_num=0;
337                 }
338
339         /* SSL2_ST_GET_SERVER_HELLO_B */
340         j=s->s2->tmp.cert_length+s->s2->tmp.csl+s->s2->tmp.conn_id_length
341                 - s->init_num;
342         i=ssl2_read(s,(char *)&(buf[s->init_num]),j);
343         if (i != j) return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i));
344
345         /* things are looking good */
346
347         p=buf;
348         if (s->hit)
349                 {
350                 if (s->s2->tmp.cert_length != 0) 
351                         {
352                         SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_LENGTH_NOT_ZERO);
353                         return(-1);
354                         }
355                 if (s->s2->tmp.cert_type != 0)
356                         {
357                         if (!(s->options &
358                                 SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG))
359                                 {
360                                 SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_TYPE_NOT_ZERO);
361                                 return(-1);
362                                 }
363                         }
364                 if (s->s2->tmp.csl != 0)
365                         {
366                         SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CIPHER_LIST_NOT_ZERO);
367                         return(-1);
368                         }
369                 }
370         else
371                 {
372 #ifdef undef
373                 /* very bad */
374                 memset(s->session->session_id,0,
375                         SSL_MAX_SSL_SESSION_ID_LENGTH_IN_BYTES);
376                 s->session->session_id_length=0;
377                 */
378 #endif
379
380                 /* we need to do this incase we were trying to reuse a 
381                  * client session but others are already reusing it.
382                  * If this was a new 'blank' session ID, the session-id
383                  * length will still be 0 */
384                 if (s->session->session_id_length > 0)
385                         {
386                         if (!ssl_get_new_session(s,0))
387                                 {
388                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
389                                 return(-1);
390                                 }
391                         }
392
393                 if (ssl2_set_certificate(s,s->s2->tmp.cert_type,
394                         s->s2->tmp.cert_length,p) <= 0)
395                         {
396                         ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
397                         return(-1);
398                         }
399                 p+=s->s2->tmp.cert_length;
400
401                 if (s->s2->tmp.csl == 0)
402                         {
403                         ssl2_return_error(s,SSL2_PE_NO_CIPHER);
404                         SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_LIST);
405                         return(-1);
406                         }
407
408                 /* We have just received a list of ciphers back from the
409                  * server.  We need to get the ones that match, then select
410                  * the one we want the most :-). */
411
412                 /* load the ciphers */
413                 sk=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.csl,
414                                             &s->session->ciphers);
415                 p+=s->s2->tmp.csl;
416                 if (sk == NULL)
417                         {
418                         ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
419                         SSLerr(SSL_F_GET_SERVER_HELLO,ERR_R_MALLOC_FAILURE);
420                         return(-1);
421                         }
422
423                 sk_SSL_CIPHER_set_cmp_func(sk,ssl_cipher_ptr_id_cmp);
424
425                 /* get the array of ciphers we will accept */
426                 cl=ssl_get_ciphers_by_id(s);
427
428                 /* In theory we could have ciphers sent back that we
429                  * don't want to use but that does not matter since we
430                  * will check against the list we origionally sent and
431                  * for performance reasons we should not bother to match
432                  * the two lists up just to check. */
433                 for (i=0; i<sk_SSL_CIPHER_num(cl); i++)
434                         {
435                         if (sk_SSL_CIPHER_find(sk,
436                                                sk_SSL_CIPHER_value(cl,i)) >= 0)
437                                 break;
438                         }
439
440                 if (i >= sk_SSL_CIPHER_num(cl))
441                         {
442                         ssl2_return_error(s,SSL2_PE_NO_CIPHER);
443                         SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_MATCH);
444                         return(-1);
445                         }
446                 s->session->cipher=sk_SSL_CIPHER_value(cl,i);
447                 }
448
449         if ((s->session != NULL) && (s->session->peer != NULL))
450                 X509_free(s->session->peer);
451
452         /* hmmm, can we have the problem of the other session with this
453          * cert, Free's it before we increment the reference count. */
454         CRYPTO_w_lock(CRYPTO_LOCK_X509);
455         s->session->peer=s->session->cert->key->x509;
456         CRYPTO_add(&s->session->peer->references,1,CRYPTO_LOCK_X509);
457         CRYPTO_w_unlock(CRYPTO_LOCK_X509);
458
459         s->s2->conn_id_length=s->s2->tmp.conn_id_length;
460         memcpy(s->s2->conn_id,p,s->s2->tmp.conn_id_length);
461         return(1);
462         }
463
464 static int client_hello(s)
465 SSL *s;
466         {
467         unsigned char *buf;
468         unsigned char *p,*d;
469 /*      CIPHER **cipher;*/
470         int i,n,j;
471
472         buf=(unsigned char *)s->init_buf->data;
473         if (s->state == SSL2_ST_SEND_CLIENT_HELLO_A)
474                 {
475                 if ((s->session == NULL) ||
476                         (s->session->ssl_version != s->version))
477                         {
478                         if (!ssl_get_new_session(s,0))
479                                 {
480                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
481                                 return(-1);
482                                 }
483                         }
484                 /* else use the pre-loaded session */
485
486                 p=buf;                                  /* header */
487                 d=p+9;                                  /* data section */
488                 *(p++)=SSL2_MT_CLIENT_HELLO;            /* type */
489                 s2n(SSL2_VERSION,p);                    /* version */
490                 n=j=0;
491
492                 n=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),d);
493                 d+=n;
494
495                 if (n == 0)
496                         {
497                         SSLerr(SSL_F_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
498                         return(-1);
499                         }
500
501                 s2n(n,p);                       /* cipher spec num bytes */
502
503                 if ((s->session->session_id_length > 0) &&
504                         (s->session->session_id_length <=
505                         SSL2_MAX_SSL_SESSION_ID_LENGTH))
506                         {
507                         i=s->session->session_id_length;
508                         s2n(i,p);               /* session id length */
509                         memcpy(d,s->session->session_id,(unsigned int)i);
510                         d+=i;
511                         }
512                 else
513                         {
514                         s2n(0,p);
515                         }
516
517                 s->s2->challenge_length=SSL2_CHALLENGE_LENGTH;
518                 s2n(SSL2_CHALLENGE_LENGTH,p);           /* challenge length */
519                 /*challenge id data*/
520                 RAND_bytes(s->s2->challenge,SSL2_CHALLENGE_LENGTH);
521                 memcpy(d,s->s2->challenge,SSL2_CHALLENGE_LENGTH);
522                 d+=SSL2_CHALLENGE_LENGTH;
523
524                 s->state=SSL2_ST_SEND_CLIENT_HELLO_B;
525                 s->init_num=d-buf;
526                 s->init_off=0;
527                 }
528         /* SSL2_ST_SEND_CLIENT_HELLO_B */
529         return(ssl2_do_write(s));
530         }
531
532 static int client_master_key(s)
533 SSL *s;
534         {
535         unsigned char *buf;
536         unsigned char *p,*d;
537         int clear,enc,karg,i;
538         SSL_SESSION *sess;
539         const EVP_CIPHER *c;
540         const EVP_MD *md;
541
542         buf=(unsigned char *)s->init_buf->data;
543         if (s->state == SSL2_ST_SEND_CLIENT_MASTER_KEY_A)
544                 {
545
546                 if (!ssl_cipher_get_evp(s->session,&c,&md,NULL))
547                         {
548                         ssl2_return_error(s,SSL2_PE_NO_CIPHER);
549                         SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
550                         return(-1);
551                         }
552                 sess=s->session;
553                 p=buf;
554                 d=p+10;
555                 *(p++)=SSL2_MT_CLIENT_MASTER_KEY;/* type */
556
557                 i=ssl_put_cipher_by_char(s,sess->cipher,p);
558                 p+=i;
559
560                 /* make key_arg data */
561                 i=EVP_CIPHER_iv_length(c);
562                 sess->key_arg_length=i;
563                 if (i > 0) RAND_bytes(sess->key_arg,i);
564
565                 /* make a master key */
566                 i=EVP_CIPHER_key_length(c);
567                 sess->master_key_length=i;
568                 if (i > 0) RAND_bytes(sess->master_key,i);
569
570                 if (sess->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
571                         enc=8;
572                 else if (SSL_C_IS_EXPORT(sess->cipher))
573                         enc=5;
574                 else
575                         enc=i;
576
577                 if (i < enc)
578                         {
579                         ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
580                         SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_CIPHER_TABLE_SRC_ERROR);
581                         return(-1);
582                         }
583                 clear=i-enc;
584                 s2n(clear,p);
585                 memcpy(d,sess->master_key,(unsigned int)clear);
586                 d+=clear;
587
588                 enc=ssl_rsa_public_encrypt(sess->cert,enc,
589                         &(sess->master_key[clear]),d,
590                         (s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING);
591                 if (enc <= 0)
592                         {
593                         ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
594                         SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PUBLIC_KEY_ENCRYPT_ERROR);
595                         return(-1);
596                         }
597 #ifdef PKCS1_CHECK
598                 if (s->options & SSL_OP_PKCS1_CHECK_1) d[1]++;
599                 if (s->options & SSL_OP_PKCS1_CHECK_2)
600                         sess->master_key[clear]++;
601 #endif
602                 s2n(enc,p);
603                 d+=enc;
604                 karg=sess->key_arg_length;      
605                 s2n(karg,p); /* key arg size */
606                 memcpy(d,sess->key_arg,(unsigned int)karg);
607                 d+=karg;
608
609                 s->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_B;
610                 s->init_num=d-buf;
611                 s->init_off=0;
612                 }
613
614         /* SSL2_ST_SEND_CLIENT_MASTER_KEY_B */
615         return(ssl2_do_write(s));
616         }
617
618 static int client_finished(s)
619 SSL *s;
620         {
621         unsigned char *p;
622
623         if (s->state == SSL2_ST_SEND_CLIENT_FINISHED_A)
624                 {
625                 p=(unsigned char *)s->init_buf->data;
626                 *(p++)=SSL2_MT_CLIENT_FINISHED;
627                 memcpy(p,s->s2->conn_id,(unsigned int)s->s2->conn_id_length);
628
629                 s->state=SSL2_ST_SEND_CLIENT_FINISHED_B;
630                 s->init_num=s->s2->conn_id_length+1;
631                 s->init_off=0;
632                 }
633         return(ssl2_do_write(s));
634         }
635
636 /* read the data and then respond */
637 static int client_certificate(s)
638 SSL *s;
639         {
640         unsigned char *buf;
641         unsigned char *p,*d;
642         int i;
643         unsigned int n;
644         int cert_ch_len=0;
645         unsigned char *cert_ch;
646
647         buf=(unsigned char *)s->init_buf->data;
648         cert_ch= &(buf[2]);
649
650         /* We have a cert associated with the SSL, so attach it to
651          * the session if it does not have one */
652
653         if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_A)
654                 {
655                 i=ssl2_read(s,(char *)&(buf[s->init_num]),
656                         SSL2_MAX_CERT_CHALLENGE_LENGTH+1-s->init_num);
657                 if (i<(SSL2_MIN_CERT_CHALLENGE_LENGTH+1-s->init_num))
658                         return(ssl2_part_read(s,SSL_F_CLIENT_CERTIFICATE,i));
659
660                 /* type=buf[0]; */
661                 /* type eq x509 */
662                 if (buf[1] != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)
663                         {
664                         ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
665                         SSLerr(SSL_F_CLIENT_CERTIFICATE,SSL_R_BAD_AUTHENTICATION_TYPE);
666                         return(-1);
667                         }
668                 cert_ch_len=i-1;
669
670                 if ((s->cert == NULL) ||
671                         (s->cert->key->x509 == NULL) ||
672                         (s->cert->key->privatekey == NULL))
673                         {
674                         s->state=SSL2_ST_X509_GET_CLIENT_CERTIFICATE;
675                         }
676                 else
677                         s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_C;
678                 }
679
680         if (s->state == SSL2_ST_X509_GET_CLIENT_CERTIFICATE)
681                 {
682                 X509 *x509=NULL;
683                 EVP_PKEY *pkey=NULL;
684
685                 /* If we get an error we need to
686                  * ssl->rwstate=SSL_X509_LOOKUP;
687                  * return(error);
688                  * We should then be retried when things are ok and we
689                  * can get a cert or not */
690
691                 i=0;
692                 if (s->ctx->client_cert_cb != NULL)
693                         {
694                         i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
695                         }
696
697                 if (i < 0)
698                         {
699                         s->rwstate=SSL_X509_LOOKUP;
700                         return(-1);
701                         }
702                 s->rwstate=SSL_NOTHING;
703
704                 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
705                         {
706                         s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_C;
707                         if (    !SSL_use_certificate(s,x509) || 
708                                 !SSL_use_PrivateKey(s,pkey))
709                                 {
710                                 i=0;
711                                 }
712                         X509_free(x509);
713                         EVP_PKEY_free(pkey);
714                         }
715                 else if (i == 1)
716                         {
717                         if (x509 != NULL) X509_free(x509);
718                         if (pkey != NULL) EVP_PKEY_free(pkey);
719                         SSLerr(SSL_F_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
720                         i=0;
721                         }
722
723                 if (i == 0)
724                         {
725                         /* We have no client certificate to respond with
726                          * so send the correct error message back */
727                         s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_B;
728                         p=buf;
729                         *(p++)=SSL2_MT_ERROR;
730                         s2n(SSL2_PE_NO_CERTIFICATE,p);
731                         s->init_off=0;
732                         s->init_num=3;
733                         /* Write is done at the end */
734                         }
735                 }
736
737         if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_B)
738                 {
739                 return(ssl2_do_write(s));
740                 }
741
742         if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_C)
743                 {
744                 EVP_MD_CTX ctx;
745
746                 /* ok, now we calculate the checksum
747                  * do it first so we can reuse buf :-) */
748                 p=buf;
749                 EVP_SignInit(&ctx,s->ctx->rsa_md5);
750                 EVP_SignUpdate(&ctx,s->s2->key_material,
751                         (unsigned int)s->s2->key_material_length);
752                 EVP_SignUpdate(&ctx,cert_ch,(unsigned int)cert_ch_len);
753                 n=i2d_X509(s->session->cert->key->x509,&p);
754                 EVP_SignUpdate(&ctx,buf,(unsigned int)n);
755
756                 p=buf;
757                 d=p+6;
758                 *(p++)=SSL2_MT_CLIENT_CERTIFICATE;
759                 *(p++)=SSL2_CT_X509_CERTIFICATE;
760                 n=i2d_X509(s->cert->key->x509,&d);
761                 s2n(n,p);
762
763                 if (!EVP_SignFinal(&ctx,d,&n,s->cert->key->privatekey))
764                         {
765                         /* this is not good.  If things have failed it
766                          * means there so something wrong with the key.
767                          * We will contiune with a 0 length signature
768                          */
769                         }
770                 memset(&ctx,0,sizeof(ctx));
771                 s2n(n,p);
772                 d+=n;
773
774                 s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_D;
775                 s->init_num=d-buf;
776                 s->init_off=0;
777                 }
778         /* if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_D) */
779         return(ssl2_do_write(s));
780         }
781
782 static int get_server_verify(s)
783 SSL *s;
784         {
785         unsigned char *p;
786         int i;
787
788         p=(unsigned char *)s->init_buf->data;
789         if (s->state == SSL2_ST_GET_SERVER_VERIFY_A)
790                 {
791                 i=ssl2_read(s,(char *)&(p[s->init_num]),1-s->init_num);
792                 if (i < (1-s->init_num)) 
793                         return(ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i));
794
795                 s->state= SSL2_ST_GET_SERVER_VERIFY_B;
796                 s->init_num=0;
797                 if (*p != SSL2_MT_SERVER_VERIFY)
798                         {
799                         if (p[0] != SSL2_MT_ERROR)
800                                 {
801                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
802                                 SSLerr(SSL_F_GET_SERVER_VERIFY,
803                                         SSL_R_READ_WRONG_PACKET_TYPE);
804                                 }
805                         else
806                                 SSLerr(SSL_F_GET_SERVER_VERIFY,
807                                         SSL_R_PEER_ERROR);
808                         return(-1);
809                         }
810                 }
811         
812         p=(unsigned char *)s->init_buf->data;
813         i=ssl2_read(s,(char *)&(p[s->init_num]),
814                 (unsigned int)s->s2->challenge_length-s->init_num);
815         if (i < ((int)s->s2->challenge_length-s->init_num))
816                 return(ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i));
817         if (memcmp(p,s->s2->challenge,(unsigned int)s->s2->challenge_length) != 0)
818                 {
819                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
820                 SSLerr(SSL_F_GET_SERVER_VERIFY,SSL_R_CHALLENGE_IS_DIFFERENT);
821                 return(-1);
822                 }
823         return(1);
824         }
825
826 static int get_server_finished(s)
827 SSL *s;
828         {
829         unsigned char *buf;
830         unsigned char *p;
831         int i;
832
833         buf=(unsigned char *)s->init_buf->data;
834         p=buf;
835         if (s->state == SSL2_ST_GET_SERVER_FINISHED_A)
836                 {
837                 i=ssl2_read(s,(char *)&(buf[s->init_num]),1-s->init_num);
838                 if (i < (1-s->init_num))
839                         return(ssl2_part_read(s,SSL_F_GET_SERVER_FINISHED,i));
840                 s->init_num=i;
841                 if (*p == SSL2_MT_REQUEST_CERTIFICATE)
842                         {
843                         s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_A;
844                         return(1);
845                         }
846                 else if (*p != SSL2_MT_SERVER_FINISHED)
847                         {
848                         if (p[0] != SSL2_MT_ERROR)
849                                 {
850                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
851                                 SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE);
852                                 }
853                         else
854                                 SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_PEER_ERROR);
855                         return(-1);
856                         }
857                 s->state=SSL_ST_OK;
858                 s->init_num=0;
859                 }
860
861         i=ssl2_read(s,(char *)&(buf[s->init_num]),
862                 SSL2_SSL_SESSION_ID_LENGTH-s->init_num);
863         if (i < (SSL2_SSL_SESSION_ID_LENGTH-s->init_num))
864                 return(ssl2_part_read(s,SSL_F_GET_SERVER_FINISHED,i));
865
866         if (!s->hit) /* new session */
867                 {
868                 /* new session-id */
869                 /* Make sure we were not trying to re-use an old SSL_SESSION
870                  * or bad things can happen */
871                 /* ZZZZZZZZZZZZZ */
872                 s->session->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
873                 memcpy(s->session->session_id,p,SSL2_SSL_SESSION_ID_LENGTH);
874                 }
875         else
876                 {
877                 if (!(s->options & SSL_OP_MICROSOFT_SESS_ID_BUG))
878                         {
879                         if (memcmp(buf,s->session->session_id,
880                                 (unsigned int)s->session->session_id_length) != 0)
881                                 {
882                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
883                                 SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_SSL_SESSION_ID_IS_DIFFERENT);
884                                 return(-1);
885                                 }
886                         }
887                 }
888         return(1);
889         }
890
891 /* loads in the certificate from the server */
892 int ssl2_set_certificate(s, type, len, data)
893 SSL *s;
894 int type;
895 int len;
896 unsigned char *data;
897         {
898         STACK_OF(X509) *sk=NULL;
899         EVP_PKEY *pkey=NULL;
900         CERT *c=NULL;
901         int i;
902         X509 *x509=NULL;
903         int ret=0;
904         
905         x509=d2i_X509(NULL,&data,(long)len);
906         if (x509 == NULL)
907                 {
908                 SSLerr(SSL_F_SSL2_SET_CERTIFICATE,ERR_R_X509_LIB);
909                 goto err;
910                 }
911
912         if ((sk=sk_X509_new_null()) == NULL || !sk_X509_push(sk,x509))
913                 {
914                 SSLerr(SSL_F_SSL2_SET_CERTIFICATE,ERR_R_MALLOC_FAILURE);
915                 goto err;
916                 }
917
918         i=ssl_verify_cert_chain(s,sk);
919                 
920         if ((s->verify_mode != SSL_VERIFY_NONE) && (!i))
921                 {
922                 SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
923                 goto err;
924                 }
925
926         /* cert for ssl */
927         c=ssl_cert_new();
928         if (c == NULL)
929                 {
930                 ret= -1;
931                 goto err;
932                 }
933
934         /* cert for session */
935         if (s->session->cert) ssl_cert_free(s->session->cert);
936         s->session->cert=c;
937
938 /*      c->cert_type=type; */
939
940         c->pkeys[SSL_PKEY_RSA_ENC].x509=x509;
941         c->key= &(c->pkeys[SSL_PKEY_RSA_ENC]);
942
943         pkey=X509_get_pubkey(x509);
944         x509=NULL;
945         if (pkey == NULL)
946                 {
947                 SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_UNABLE_TO_EXTRACT_PUBLIC_KEY);
948                 goto err;
949                 }
950         if (pkey->type != EVP_PKEY_RSA)
951                 {
952                 SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_PUBLIC_KEY_NOT_RSA);
953                 goto err;
954                 }
955
956         if (!ssl_set_cert_type(c,SSL2_CT_X509_CERTIFICATE))
957                 goto err;
958         ret=1;
959 err:
960         sk_X509_free(sk);
961         X509_free(x509);
962         EVP_PKEY_free(pkey);
963         return(ret);
964         }
965
966 static int ssl_rsa_public_encrypt(c, len, from, to, padding)
967 CERT *c;
968 int len;
969 unsigned char *from;
970 unsigned char *to;
971 int padding;
972         {
973         EVP_PKEY *pkey=NULL;
974         int i= -1;
975
976         if ((c == NULL) || (c->key->x509 == NULL) ||
977                 ((pkey=X509_get_pubkey(c->key->x509)) == NULL))
978                 {
979                 SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,SSL_R_NO_PUBLICKEY);
980                 return(-1);
981                 }
982         if (pkey->type != EVP_PKEY_RSA)
983                 {
984                 SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA);
985                 goto end;
986                 }
987
988         /* we have the public key */
989         i=RSA_public_encrypt(len,from,to,pkey->pkey.rsa,padding);
990         if (i < 0)
991                 SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,ERR_R_RSA_LIB);
992 end:
993         EVP_PKEY_free(pkey);
994         return(i);
995         }
996