-Wall implies -Wuninitialized.
[openssl.git] / ssl / s2_srvr.c
1 /* ssl/s2_srvr.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 "bio.h"
61 #include "rand.h"
62 #include "objects.h"
63 #include "ssl_locl.h"
64 #include "evp.h"
65
66 #ifndef NOPROTO
67 static SSL_METHOD *ssl2_get_server_method(int ver);
68 static int get_client_master_key(SSL *s);
69 static int get_client_hello(SSL *s);
70 static int server_hello(SSL *s); 
71 static int get_client_finished(SSL *s);
72 static int server_verify(SSL *s);
73 static int server_finish(SSL *s);
74 static int request_certificate(SSL *s);
75 static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
76         unsigned char *to,int padding);
77 #else
78 static SSL_METHOD *ssl2_get_server_method();
79 static int get_client_master_key();
80 static int get_client_hello();
81 static int server_hello(); 
82 static int get_client_finished();
83 static int server_verify();
84 static int server_finish();
85 static int request_certificate();
86 static int ssl_rsa_private_decrypt();
87 #endif
88
89 #define BREAK   break
90
91 static SSL_METHOD *ssl2_get_server_method(int ver)
92         {
93         if (ver == SSL2_VERSION)
94                 return(SSLv2_server_method());
95         else
96                 return(NULL);
97         }
98
99 SSL_METHOD *SSLv2_server_method(void)
100         {
101         static int init=1;
102         static SSL_METHOD SSLv2_server_data;
103
104         if (init)
105                 {
106                 init=0;
107                 memcpy((char *)&SSLv2_server_data,(char *)sslv2_base_method(),
108                         sizeof(SSL_METHOD));
109                 SSLv2_server_data.ssl_accept=ssl2_accept;
110                 SSLv2_server_data.get_ssl_method=ssl2_get_server_method;
111                 }
112         return(&SSLv2_server_data);
113         }
114
115 int ssl2_accept(SSL *s)
116         {
117         unsigned long l=time(NULL);
118         BUF_MEM *buf=NULL;
119         int ret= -1;
120         long num1;
121         void (*cb)()=NULL;
122         int new_state,state;
123
124         RAND_seed(&l,sizeof(l));
125         ERR_clear_error();
126         clear_sys_error();
127
128         if (s->info_callback != NULL)
129                 cb=s->info_callback;
130         else if (s->ctx->info_callback != NULL)
131                 cb=s->ctx->info_callback;
132
133         /* init things to blank */
134         if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
135         s->in_handshake++;
136
137         if (((s->session == NULL) || (s->session->cert == NULL)) &&
138                 (s->cert == NULL))
139                 {
140                 SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
141                 return(-1);
142                 }
143
144         clear_sys_error();
145         for (;;)
146                 {
147                 state=s->state;
148
149                 switch (s->state)
150                         {
151                 case SSL_ST_BEFORE:
152                 case SSL_ST_ACCEPT:
153                 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
154                 case SSL_ST_OK|SSL_ST_ACCEPT:
155
156                         s->server=1;
157                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
158
159                         s->version=SSL2_VERSION;
160                         s->type=SSL_ST_ACCEPT;
161
162                         buf=s->init_buf;
163                         if ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL))
164                                 { ret= -1; goto end; }
165                         if (!BUF_MEM_grow(buf,(int)
166                                 SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
167                                 { ret= -1; goto end; }
168                         s->init_buf=buf;
169                         s->init_num=0;
170                         s->ctx->stats.sess_accept++;
171                         s->handshake_func=ssl2_accept;
172                         s->state=SSL2_ST_GET_CLIENT_HELLO_A;
173                         BREAK;
174
175                 case SSL2_ST_GET_CLIENT_HELLO_A:
176                 case SSL2_ST_GET_CLIENT_HELLO_B:
177                 case SSL2_ST_GET_CLIENT_HELLO_C:
178                         s->shutdown=0;
179                         ret=get_client_hello(s);
180                         if (ret <= 0) goto end;
181                         s->init_num=0;
182                         s->state=SSL2_ST_SEND_SERVER_HELLO_A;
183                         BREAK;
184
185                 case SSL2_ST_SEND_SERVER_HELLO_A:
186                 case SSL2_ST_SEND_SERVER_HELLO_B:
187                         ret=server_hello(s);
188                         if (ret <= 0) goto end;
189                         s->init_num=0;
190                         if (!s->hit)
191                                 {
192                                 s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_A;
193                                 BREAK;
194                                 }
195                         else
196                                 {
197                                 s->state=SSL2_ST_SERVER_START_ENCRYPTION;
198                                 BREAK;
199                                 }
200                 case SSL2_ST_GET_CLIENT_MASTER_KEY_A:
201                 case SSL2_ST_GET_CLIENT_MASTER_KEY_B:
202                         ret=get_client_master_key(s);
203                         if (ret <= 0) goto end;
204                         s->init_num=0;
205                         s->state=SSL2_ST_SERVER_START_ENCRYPTION;
206                         BREAK;
207
208                 case SSL2_ST_SERVER_START_ENCRYPTION:
209                         /* Ok we how have sent all the stuff needed to
210                          * start encrypting, the next packet back will
211                          * be encrypted. */
212                         if (!ssl2_enc_init(s,0))
213                                 { ret= -1; goto end; }
214                         s->s2->clear_text=0;
215                         s->state=SSL2_ST_SEND_SERVER_VERIFY_A;
216                         BREAK;
217
218                 case SSL2_ST_SEND_SERVER_VERIFY_A:
219                 case SSL2_ST_SEND_SERVER_VERIFY_B:
220                         ret=server_verify(s);
221                         if (ret <= 0) goto end;
222                         s->init_num=0;
223                         if (s->hit)
224                                 {
225                                 /* If we are in here, we have been
226                                  * buffering the output, so we need to
227                                  * flush it and remove buffering from
228                                  * future traffic */
229                                 s->state=SSL2_ST_SEND_SERVER_VERIFY_C;
230                                 BREAK;
231                                 }
232                         else
233                                 {
234                                 s->state=SSL2_ST_GET_CLIENT_FINISHED_A;
235                                 break;
236                                 }
237
238                 case SSL2_ST_SEND_SERVER_VERIFY_C:
239                         /* get the number of bytes to write */
240                         num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
241                         if (num1 != 0)
242                                 {
243                                 s->rwstate=SSL_WRITING;
244                                 num1=BIO_flush(s->wbio);
245                                 if (num1 <= 0) { ret= -1; goto end; }
246                                 s->rwstate=SSL_NOTHING;
247                                 }
248
249                         /* flushed and now remove buffering */
250                         s->wbio=BIO_pop(s->wbio);
251
252                         s->state=SSL2_ST_GET_CLIENT_FINISHED_A;
253                         BREAK;
254
255                 case SSL2_ST_GET_CLIENT_FINISHED_A:
256                 case SSL2_ST_GET_CLIENT_FINISHED_B:
257                         ret=get_client_finished(s);
258                         if (ret <= 0)
259                                 goto end;
260                         s->init_num=0;
261                         s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_A;
262                         BREAK;
263
264                 case SSL2_ST_SEND_REQUEST_CERTIFICATE_A:
265                 case SSL2_ST_SEND_REQUEST_CERTIFICATE_B:
266                 case SSL2_ST_SEND_REQUEST_CERTIFICATE_C:
267                 case SSL2_ST_SEND_REQUEST_CERTIFICATE_D:
268                         /* don't do a 'request certificate' if we
269                          * don't want to, or we already have one, and
270                          * we only want to do it once. */
271                         if (!(s->verify_mode & SSL_VERIFY_PEER) ||
272                                 ((s->session->peer != NULL) &&
273                                 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)))
274                                 {
275                                 s->state=SSL2_ST_SEND_SERVER_FINISHED_A;
276                                 break;
277                                 }
278                         else
279                                 {
280                                 ret=request_certificate(s);
281                                 if (ret <= 0) goto end;
282                                 s->init_num=0;
283                                 s->state=SSL2_ST_SEND_SERVER_FINISHED_A;
284                                 }
285                         BREAK;
286
287                 case SSL2_ST_SEND_SERVER_FINISHED_A:
288                 case SSL2_ST_SEND_SERVER_FINISHED_B:
289                         ret=server_finish(s);
290                         if (ret <= 0) goto end;
291                         s->init_num=0;
292                         s->state=SSL_ST_OK;
293                         break;
294
295                 case SSL_ST_OK:
296                         BUF_MEM_free(s->init_buf);
297                         ssl_free_wbio_buffer(s);
298                         s->init_buf=NULL;
299                         s->init_num=0;
300                 /*      ERR_clear_error();*/
301
302                         ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
303
304                         s->ctx->stats.sess_accept_good++;
305                         /* s->server=1; */
306                         ret=1;
307
308                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
309
310                         goto end;
311                         /* BREAK; */
312
313                 default:
314                         SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_UNKNOWN_STATE);
315                         ret= -1;
316                         goto end;
317                         /* BREAK; */
318                         }
319                 
320                 if ((cb != NULL) && (s->state != state))
321                         {
322                         new_state=s->state;
323                         s->state=state;
324                         cb(s,SSL_CB_ACCEPT_LOOP,1);
325                         s->state=new_state;
326                         }
327                 }
328 end:
329         s->in_handshake--;
330         if (cb != NULL)
331                 cb(s,SSL_CB_ACCEPT_EXIT,ret);
332         return(ret);
333         }
334
335 static int get_client_master_key(SSL *s)
336         {
337         int export,i,n,keya,ek;
338         unsigned char *p;
339         SSL_CIPHER *cp;
340         const EVP_CIPHER *c;
341         const EVP_MD *md;
342
343         p=(unsigned char *)s->init_buf->data;
344         if (s->state == SSL2_ST_GET_CLIENT_MASTER_KEY_A)
345                 {
346                 i=ssl2_read(s,(char *)&(p[s->init_num]),10-s->init_num);
347
348                 if (i < (10-s->init_num))
349                         return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
350                 if (*(p++) != SSL2_MT_CLIENT_MASTER_KEY)
351                         {
352                         if (p[-1] != SSL2_MT_ERROR)
353                                 {
354                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
355                                 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_READ_WRONG_PACKET_TYPE);
356                                 }
357                         else
358                                 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,
359                                         SSL_R_PEER_ERROR);
360                         return(-1);
361                         }
362
363                 cp=ssl2_get_cipher_by_char(p);
364                 if (cp == NULL)
365                         {
366                         ssl2_return_error(s,SSL2_PE_NO_CIPHER);
367                         SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,
368                                 SSL_R_NO_CIPHER_MATCH);
369                         return(-1);
370                         }
371                 s->session->cipher= cp;
372
373                 p+=3;
374                 n2s(p,i); s->s2->tmp.clear=i;
375                 n2s(p,i); s->s2->tmp.enc=i;
376                 n2s(p,i); s->session->key_arg_length=i;
377                 s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_B;
378                 s->init_num=0;
379                 }
380
381         /* SSL2_ST_GET_CLIENT_MASTER_KEY_B */
382         p=(unsigned char *)s->init_buf->data;
383         keya=s->session->key_arg_length;
384         n=s->s2->tmp.clear+s->s2->tmp.enc+keya - s->init_num;
385         i=ssl2_read(s,(char *)&(p[s->init_num]),n);
386         if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
387
388         memcpy(s->session->key_arg,&(p[s->s2->tmp.clear+s->s2->tmp.enc]),
389                 (unsigned int)keya);
390
391         if (s->session->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL)
392                 {
393                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
394                 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_NO_PRIVATEKEY);
395                 return(-1);
396                 }
397         i=ssl_rsa_private_decrypt(s->cert,s->s2->tmp.enc,
398                 &(p[s->s2->tmp.clear]),&(p[s->s2->tmp.clear]),
399                 (s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING);
400
401         export=SSL_C_IS_EXPORT(s->session->cipher);
402         
403         if (!ssl_cipher_get_evp(s->session,&c,&md,NULL))
404                 {
405                 ssl2_return_error(s,SSL2_PE_NO_CIPHER);
406                 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
407                 return(0);
408                 }
409
410         if (s->session->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
411                 {
412                 export=1;
413                 ek=8;
414                 }
415         else
416                 ek=5;
417
418         /* bad decrypt */
419 #if 1
420         /* If a bad decrypt, continue with protocol but with a
421          * dud master secret */
422         if ((i < 0) ||
423                 ((!export && (i != EVP_CIPHER_key_length(c)))
424                 || ( export && ((i != ek) || (s->s2->tmp.clear+i !=
425                         EVP_CIPHER_key_length(c))))))
426                 {
427                 if (export)
428                         i=ek;
429                 else
430                         i=EVP_CIPHER_key_length(c);
431                 RAND_bytes(p,i);
432                 }
433 #else
434         if (i < 0)
435                 {
436                 error=1;
437                 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_BAD_RSA_DECRYPT);
438                 }
439         /* incorrect number of key bytes for non export cipher */
440         else if ((!export && (i != EVP_CIPHER_key_length(c)))
441                 || ( export && ((i != ek) || (s->s2->tmp.clear+i !=
442                         EVP_CIPHER_key_length(c)))))
443                 {
444                 error=1;
445                 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_WRONG_NUMBER_OF_KEY_BITS);
446                 }
447         if (error)
448                 {
449                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
450                 return(-1);
451                 }
452 #endif
453
454         if (export) i+=s->s2->tmp.clear;
455         s->session->master_key_length=i;
456         memcpy(s->session->master_key,p,(unsigned int)i);
457         return(1);
458         }
459
460 static int get_client_hello(SSL *s)
461         {
462         int i,n;
463         unsigned char *p;
464         STACK_OF(SSL_CIPHER) *cs; /* a stack of SSL_CIPHERS */
465         STACK_OF(SSL_CIPHER) *cl; /* the ones we want to use */
466         int z;
467
468         /* This is a bit of a hack to check for the correct packet
469          * type the first time round. */
470         if (s->state == SSL2_ST_GET_CLIENT_HELLO_A)
471                 {
472                 s->first_packet=1;
473                 s->state=SSL2_ST_GET_CLIENT_HELLO_B;
474                 }
475
476         p=(unsigned char *)s->init_buf->data;
477         if (s->state == SSL2_ST_GET_CLIENT_HELLO_B)
478                 {
479                 i=ssl2_read(s,(char *)&(p[s->init_num]),9-s->init_num);
480                 if (i < (9-s->init_num)) 
481                         return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
482         
483                 if (*(p++) != SSL2_MT_CLIENT_HELLO)
484                         {
485                         if (p[-1] != SSL2_MT_ERROR)
486                                 {
487                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
488                                 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_READ_WRONG_PACKET_TYPE);
489                                 }
490                         else
491                                 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_PEER_ERROR);
492                         return(-1);
493                         }
494                 n2s(p,i);
495                 if (i < s->version) s->version=i;
496                 n2s(p,i); s->s2->tmp.cipher_spec_length=i;
497                 n2s(p,i); s->s2->tmp.session_id_length=i;
498                 n2s(p,i); s->s2->challenge_length=i;
499                 if (    (i < SSL2_MIN_CHALLENGE_LENGTH) ||
500                         (i > SSL2_MAX_CHALLENGE_LENGTH))
501                         {
502                         SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_INVALID_CHALLENGE_LENGTH);
503                         return(-1);
504                         }
505                 s->state=SSL2_ST_GET_CLIENT_HELLO_C;
506                 s->init_num=0;
507                 }
508
509         /* SSL2_ST_GET_CLIENT_HELLO_C */
510         p=(unsigned char *)s->init_buf->data;
511         n=s->s2->tmp.cipher_spec_length+s->s2->challenge_length+
512                 s->s2->tmp.session_id_length-s->init_num;
513         i=ssl2_read(s,(char *)&(p[s->init_num]),n);
514         if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
515
516         /* get session-id before cipher stuff so we can get out session
517          * structure if it is cached */
518         /* session-id */
519         if ((s->s2->tmp.session_id_length != 0) && 
520                 (s->s2->tmp.session_id_length != SSL2_SSL_SESSION_ID_LENGTH))
521                 {
522                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
523                 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_BAD_SSL_SESSION_ID_LENGTH);
524                 return(-1);
525                 }
526
527         if (s->s2->tmp.session_id_length == 0)
528                 {
529                 if (!ssl_get_new_session(s,1))
530                         {
531                         ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
532                         return(-1);
533                         }
534                 }
535         else
536                 {
537                 i=ssl_get_prev_session(s,&(p[s->s2->tmp.cipher_spec_length]),
538                         s->s2->tmp.session_id_length);
539                 if (i == 1)
540                         { /* previous session */
541                         s->hit=1;
542                         }
543                 else if (i == -1)
544                         {
545                         ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
546                         return(-1);
547                         }
548                 else
549                         {
550                         if (s->cert == NULL)
551                                 {
552                                 ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
553                                 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_NO_CERTIFICATE_SET);
554                                 return(-1);
555                                 }
556
557                         if (!ssl_get_new_session(s,1))
558                                 {
559                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
560                                 return(-1);
561                                 }
562                         }
563                 }
564
565         if (!s->hit)
566                 {
567                 cs=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.cipher_spec_length,
568                         &s->session->ciphers);
569                 if (cs == NULL) goto mem_err;
570
571                 cl=ssl_get_ciphers_by_id(s);
572
573                 for (z=0; z<sk_SSL_CIPHER_num(cs); z++)
574                         {
575                         if (sk_SSL_CIPHER_find(cl,sk_SSL_CIPHER_value(cs,z)) < 0)
576                                 {
577                                 sk_SSL_CIPHER_delete(cs,z);
578                                 z--;
579                                 }
580                         }
581
582                 /* s->session->ciphers should now have a list of
583                  * ciphers that are on both the client and server.
584                  * This list is ordered by the order the client sent
585                  * the ciphers.
586                  */
587                 }
588         p+=s->s2->tmp.cipher_spec_length;
589         /* done cipher selection */
590
591         /* session id extracted already */
592         p+=s->s2->tmp.session_id_length;
593
594         /* challenge */
595         memcpy(s->s2->challenge,p,(unsigned int)s->s2->challenge_length);
596         return(1);
597 mem_err:
598         SSLerr(SSL_F_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE);
599         return(0);
600         }
601
602 static int server_hello(SSL *s)
603         {
604         unsigned char *p,*d;
605         int n,hit;
606         STACK_OF(SSL_CIPHER) *sk;
607
608         p=(unsigned char *)s->init_buf->data;
609         if (s->state == SSL2_ST_SEND_SERVER_HELLO_A)
610                 {
611                 d=p+11;
612                 *(p++)=SSL2_MT_SERVER_HELLO;            /* type */
613                 hit=s->hit;
614                 *(p++)=(unsigned char)hit;
615                 if (!hit)
616                         {                       /* else add cert to session */
617                         CRYPTO_add(&s->cert->references,1,CRYPTO_LOCK_SSL_CERT);
618                         if (s->session->cert != NULL)
619                                 ssl_cert_free(s->session->cert);
620                         s->session->cert=s->cert;               
621                         }
622                 else    /* We have a session id-cache hit, if the
623                          * session-id has no certificate listed against
624                          * the 'cert' structure, grab the 'old' one
625                          * listed against the SSL connection */
626                         {
627                         if (s->session->cert == NULL)
628                                 {
629                                 CRYPTO_add(&s->cert->references,1,
630                                         CRYPTO_LOCK_SSL_CERT);
631                                 s->session->cert=s->cert;
632                                 }
633                         }
634
635                 if (s->session->cert == NULL)
636                         {
637                         ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
638                         SSLerr(SSL_F_SERVER_HELLO,SSL_R_NO_CERTIFICATE_SPECIFIED);
639                         return(-1);
640                         }
641
642                 if (hit)
643                         {
644                         *(p++)=0;               /* no certificate type */
645                         s2n(s->version,p);      /* version */
646                         s2n(0,p);               /* cert len */
647                         s2n(0,p);               /* ciphers len */
648                         }
649                 else
650                         {
651                         /* EAY EAY */
652                         /* put certificate type */
653                         *(p++)=SSL2_CT_X509_CERTIFICATE;
654                         s2n(s->version,p);      /* version */
655                         n=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
656                         s2n(n,p);               /* certificate length */
657                         i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&d);
658                         n=0;
659                         
660                         /* lets send out the ciphers we like in the
661                          * prefered order */
662                         sk= s->session->ciphers;
663                         n=ssl_cipher_list_to_bytes(s,s->session->ciphers,d);
664                         d+=n;
665                         s2n(n,p);               /* add cipher length */
666                         }
667
668                 /* make and send conn_id */
669                 s2n(SSL2_CONNECTION_ID_LENGTH,p);       /* add conn_id length */
670                 s->s2->conn_id_length=SSL2_CONNECTION_ID_LENGTH;
671                 RAND_bytes(s->s2->conn_id,(int)s->s2->conn_id_length);
672                 memcpy(d,s->s2->conn_id,SSL2_CONNECTION_ID_LENGTH);
673                 d+=SSL2_CONNECTION_ID_LENGTH;
674
675                 s->state=SSL2_ST_SEND_SERVER_HELLO_B;
676                 s->init_num=d-(unsigned char *)s->init_buf->data;
677                 s->init_off=0;
678                 }
679         /* SSL2_ST_SEND_SERVER_HELLO_B */
680         /* If we are using TCP/IP, the performace is bad if we do 2
681          * writes without a read between them.  This occurs when
682          * Session-id reuse is used, so I will put in a buffering module
683          */
684         if (s->hit)
685                 {
686                 if (!ssl_init_wbio_buffer(s,1)) return(-1);
687                 }
688  
689         return(ssl2_do_write(s));
690         }
691
692 static int get_client_finished(SSL *s)
693         {
694         unsigned char *p;
695         int i;
696
697         p=(unsigned char *)s->init_buf->data;
698         if (s->state == SSL2_ST_GET_CLIENT_FINISHED_A)
699                 {
700                 i=ssl2_read(s,(char *)&(p[s->init_num]),1-s->init_num);
701                 if (i < 1-s->init_num)
702                         return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
703
704                 if (*p != SSL2_MT_CLIENT_FINISHED)
705                         {
706                         if (*p != SSL2_MT_ERROR)
707                                 {
708                                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
709                                 SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE);
710                                 }
711                         else
712                                 SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_PEER_ERROR);
713                         return(-1);
714                         }
715                 s->init_num=0;
716                 s->state=SSL2_ST_GET_CLIENT_FINISHED_B;
717                 }
718
719         /* SSL2_ST_GET_CLIENT_FINISHED_B */
720         i=ssl2_read(s,(char *)&(p[s->init_num]),s->s2->conn_id_length-s->init_num);
721         if (i < (int)s->s2->conn_id_length-s->init_num)
722                 {
723                 return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
724                 }
725         if (memcmp(p,s->s2->conn_id,(unsigned int)s->s2->conn_id_length) != 0)
726                 {
727                 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
728                 SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_CONNECTION_ID_IS_DIFFERENT);
729                 return(-1);
730                 }
731         return(1);
732         }
733
734 static int server_verify(SSL *s)
735         {
736         unsigned char *p;
737
738         if (s->state == SSL2_ST_SEND_SERVER_VERIFY_A)
739                 {
740                 p=(unsigned char *)s->init_buf->data;
741                 *(p++)=SSL2_MT_SERVER_VERIFY;
742                 memcpy(p,s->s2->challenge,(unsigned int)s->s2->challenge_length);
743                 /* p+=s->s2->challenge_length; */
744
745                 s->state=SSL2_ST_SEND_SERVER_VERIFY_B;
746                 s->init_num=s->s2->challenge_length+1;
747                 s->init_off=0;
748                 }
749         return(ssl2_do_write(s));
750         }
751
752 static int server_finish(SSL *s)
753         {
754         unsigned char *p;
755
756         if (s->state == SSL2_ST_SEND_SERVER_FINISHED_A)
757                 {
758                 p=(unsigned char *)s->init_buf->data;
759                 *(p++)=SSL2_MT_SERVER_FINISHED;
760
761                 memcpy(p,s->session->session_id,
762                         (unsigned int)s->session->session_id_length);
763                 /* p+=s->session->session_id_length; */
764
765                 s->state=SSL2_ST_SEND_SERVER_FINISHED_B;
766                 s->init_num=s->session->session_id_length+1;
767                 s->init_off=0;
768                 }
769
770         /* SSL2_ST_SEND_SERVER_FINISHED_B */
771         return(ssl2_do_write(s));
772         }
773
774 /* send the request and check the response */
775 static int request_certificate(SSL *s)
776         {
777         unsigned char *p,*p2,*buf2;
778         unsigned char *ccd;
779         int i,j,ctype,ret= -1;
780         X509 *x509=NULL;
781         STACK_OF(X509) *sk=NULL;
782
783         ccd=s->s2->tmp.ccl;
784         if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_A)
785                 {
786                 p=(unsigned char *)s->init_buf->data;
787                 *(p++)=SSL2_MT_REQUEST_CERTIFICATE;
788                 *(p++)=SSL2_AT_MD5_WITH_RSA_ENCRYPTION;
789                 RAND_bytes(ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
790                 memcpy(p,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
791
792                 s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_B;
793                 s->init_num=SSL2_MIN_CERT_CHALLENGE_LENGTH+2;
794                 s->init_off=0;
795                 }
796
797         if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_B)
798                 {
799                 i=ssl2_do_write(s);
800                 if (i <= 0)
801                         {
802                         ret=i;
803                         goto end;
804                         }
805
806                 s->init_num=0;
807                 s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_C;
808                 }
809
810         if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_C)
811                 {
812                 p=(unsigned char *)s->init_buf->data;
813                 i=ssl2_read(s,(char *)&(p[s->init_num]),6-s->init_num);
814                 if (i < 3)
815                         {
816                         ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
817                         goto end;
818                         }
819
820                 if ((*p == SSL2_MT_ERROR) && (i >= 3))
821                         {
822                         n2s(p,i);
823                         if (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
824                                 {
825                                 ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
826                                 SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
827                                 goto end;
828                                 }
829                         ret=1;
830                         goto end;
831                         }
832                 if ((*(p++) != SSL2_MT_CLIENT_CERTIFICATE) || (i < 6))
833                         {
834                         ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
835                         SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_SHORT_READ);
836                         goto end;
837                         }
838                 /* ok we have a response */
839                 /* certificate type, there is only one right now. */
840                 ctype= *(p++);
841                 if (ctype != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)
842                         {
843                         ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
844                         SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_RESPONSE_ARGUMENT);
845                         goto end;
846                         }
847                 n2s(p,i); s->s2->tmp.clen=i;
848                 n2s(p,i); s->s2->tmp.rlen=i;
849                 s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_D;
850                 s->init_num=0;
851                 }
852
853         /* SSL2_ST_SEND_REQUEST_CERTIFICATE_D */
854         p=(unsigned char *)s->init_buf->data;
855         j=s->s2->tmp.clen+s->s2->tmp.rlen-s->init_num;
856         i=ssl2_read(s,(char *)&(p[s->init_num]),j);
857         if (i < j) 
858                 {
859                 ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
860                 goto end;
861                 }
862
863         x509=(X509 *)d2i_X509(NULL,&p,(long)s->s2->tmp.clen);
864         if (x509 == NULL)
865                 {
866                 SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_X509_LIB);
867                 goto msg_end;
868                 }
869
870         if (((sk=sk_X509_new_null()) == NULL) || (!sk_X509_push(sk,x509)))
871                 {
872                 SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
873                 goto msg_end;
874                 }
875
876         i=ssl_verify_cert_chain(s,sk);
877
878         if (i)  /* we like the packet, now check the chksum */
879                 {
880                 EVP_MD_CTX ctx;
881                 EVP_PKEY *pkey=NULL;
882
883                 EVP_VerifyInit(&ctx,s->ctx->rsa_md5);
884                 EVP_VerifyUpdate(&ctx,s->s2->key_material,
885                         (unsigned int)s->s2->key_material_length);
886                 EVP_VerifyUpdate(&ctx,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
887
888                 i=i2d_X509(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
889                 buf2=(unsigned char *)Malloc((unsigned int)i);
890                 if (buf2 == NULL)
891                         {
892                         SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
893                         goto msg_end;
894                         }
895                 p2=buf2;
896                 i=i2d_X509(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&p2);
897                 EVP_VerifyUpdate(&ctx,buf2,(unsigned int)i);
898                 Free(buf2);
899
900                 pkey=X509_get_pubkey(x509);
901                 if (pkey == NULL) goto end;
902                 i=EVP_VerifyFinal(&ctx,p,s->s2->tmp.rlen,pkey);
903                 EVP_PKEY_free(pkey);
904                 memset(&ctx,0,sizeof(ctx));
905
906                 if (i) 
907                         {
908                         if (s->session->peer != NULL)
909                                 X509_free(s->session->peer);
910                         s->session->peer=x509;
911                         CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
912                         ret=1;
913                         goto end;
914                         }
915                 else
916                         {
917                         SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_CHECKSUM);
918                         goto msg_end;
919                         }
920                 }
921         else
922                 {
923 msg_end:
924                 ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
925                 }
926 end:
927         sk_X509_free(sk);
928         X509_free(x509);
929         return(ret);
930         }
931
932 static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
933              unsigned char *to, int padding)
934         {
935         RSA *rsa;
936         int i;
937
938         if ((c == NULL) || (c->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL))
939                 {
940                 SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_NO_PRIVATEKEY);
941                 return(-1);
942                 }
943         if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey->type != EVP_PKEY_RSA)
944                 {
945                 SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA);
946                 return(-1);
947                 }
948         rsa=c->pkeys[SSL_PKEY_RSA_ENC].privatekey->pkey.rsa;
949
950         /* we have the public key */
951         i=RSA_private_decrypt(len,from,to,rsa,padding);
952         if (i < 0)
953                 SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,ERR_R_RSA_LIB);
954         return(i);
955         }
956