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