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