More CVS ignore stuff...
[openssl.git] / ssl / ssl_lib.c
1 /* ssl/ssl_lib.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 "objects.h"
61 #include "lhash.h"
62 #include "ssl_locl.h"
63
64 char *SSL_version_str="OpenSSL 0.9.2 31-Dec-1998";
65
66 static STACK *ssl_meth=NULL;
67 static STACK *ssl_ctx_meth=NULL;
68 static int ssl_meth_num=0;
69 static int ssl_ctx_meth_num=0;
70
71 SSL3_ENC_METHOD ssl3_undef_enc_method={
72         ssl_undefined_function,
73         ssl_undefined_function,
74         ssl_undefined_function,
75         ssl_undefined_function,
76         ssl_undefined_function,
77         ssl_undefined_function,
78         };
79
80 int SSL_clear(s)
81 SSL *s;
82         {
83         int state;
84
85         if (s->method == NULL)
86                 {
87                 SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);
88                 return(0);
89                 }
90
91         s->error=0;
92         s->hit=0;
93         s->shutdown=0;
94
95 #if 0
96         /* This is set if we are doing dynamic renegotiation so keep
97          * the old cipher.  It is sort of a SSL_clear_lite :-) */
98         if (s->new_session) return(1);
99 #endif
100
101         state=s->state; /* Keep to check if we throw away the session-id */
102         s->type=0;
103
104         s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
105
106         s->version=s->method->version;
107         s->client_version=s->version;
108         s->rwstate=SSL_NOTHING;
109         s->rstate=SSL_ST_READ_HEADER;
110         s->read_ahead=s->ctx->read_ahead;
111
112         if (s->init_buf != NULL)
113                 {
114                 BUF_MEM_free(s->init_buf);
115                 s->init_buf=NULL;
116                 }
117
118         ssl_clear_cipher_ctx(s);
119
120         if (ssl_clear_bad_session(s))
121                 {
122                 SSL_SESSION_free(s->session);
123                 s->session=NULL;
124                 }
125
126         s->first_packet=0;
127
128 #if 1
129         /* Check to see if we were changed into a different method, if
130          * so, revert back if we are not doing session-id reuse. */
131         if ((s->session == NULL) && (s->method != s->ctx->method))
132                 {
133                 s->method->ssl_free(s);
134                 s->method=s->ctx->method;
135                 if (!s->method->ssl_new(s))
136                         return(0);
137                 }
138         else
139 #endif
140                 s->method->ssl_clear(s);
141         return(1);
142         }
143
144 /* Used to change an SSL_CTXs default SSL method type */
145 int SSL_CTX_set_ssl_version(ctx,meth)
146 SSL_CTX *ctx;
147 SSL_METHOD *meth;
148         {
149         STACK *sk;
150
151         ctx->method=meth;
152
153         sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
154                 &(ctx->cipher_list_by_id),SSL_DEFAULT_CIPHER_LIST);
155         if ((sk == NULL) || (sk_num(sk) <= 0))
156                 {
157                 SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
158                 return(0);
159                 }
160         return(1);
161         }
162
163 SSL *SSL_new(ctx)
164 SSL_CTX *ctx;
165         {
166         SSL *s;
167
168         if (ctx == NULL)
169                 {
170                 SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);
171                 return(NULL);
172                 }
173         if (ctx->method == NULL)
174                 {
175                 SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
176                 return(NULL);
177                 }
178
179         s=(SSL *)Malloc(sizeof(SSL));
180         if (s == NULL) goto err;
181         memset(s,0,sizeof(SSL));
182
183         if (ctx->default_cert != NULL)
184                 {
185                 CRYPTO_add(&ctx->default_cert->references,1,
186                         CRYPTO_LOCK_SSL_CERT);
187                 s->cert=ctx->default_cert;
188                 }
189         else
190                 s->cert=NULL;
191         s->verify_mode=ctx->verify_mode;
192         s->verify_callback=ctx->default_verify_callback;
193         CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
194         s->ctx=ctx;
195
196         s->verify_result=X509_V_OK;
197
198         s->method=ctx->method;
199
200         if (!s->method->ssl_new(s))
201                 {
202                 SSL_CTX_free(ctx);
203                 Free(s);
204                 goto err;
205                 }
206
207         s->quiet_shutdown=ctx->quiet_shutdown;
208         s->references=1;
209         s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
210         s->options=ctx->options;
211         SSL_clear(s);
212
213         CRYPTO_new_ex_data(ssl_meth,(char *)s,&s->ex_data);
214
215         return(s);
216 err:
217         SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
218         return(NULL);
219         }
220
221 void SSL_free(s)
222 SSL *s;
223         {
224         int i;
225
226         if(s == NULL)
227             return;
228
229         i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
230 #ifdef REF_PRINT
231         REF_PRINT("SSL",s);
232 #endif
233         if (i > 0) return;
234 #ifdef REF_CHECK
235         if (i < 0)
236                 {
237                 fprintf(stderr,"SSL_free, bad reference count\n");
238                 abort(); /* ok */
239                 }
240 #endif
241
242         CRYPTO_free_ex_data(ssl_meth,(char *)s,&s->ex_data);
243
244         if (s->bbio != NULL)
245                 {
246                 /* If the buffering BIO is in place, pop it off */
247                 if (s->bbio == s->wbio)
248                         {
249                         s->wbio=BIO_pop(s->wbio);
250                         }
251                 BIO_free(s->bbio);
252                 s->bbio=NULL;
253                 }
254         if (s->rbio != NULL)
255                 BIO_free_all(s->rbio);
256         if ((s->wbio != NULL) && (s->wbio != s->rbio))
257                 BIO_free_all(s->wbio);
258
259         if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
260
261         /* add extra stuff */
262         if (s->cipher_list != NULL) sk_free(s->cipher_list);
263         if (s->cipher_list_by_id != NULL) sk_free(s->cipher_list_by_id);
264
265         /* Make the next call work :-) */
266         if (s->session != NULL)
267                 {
268                 ssl_clear_bad_session(s);
269                 SSL_SESSION_free(s->session);
270                 }
271
272         ssl_clear_cipher_ctx(s);
273
274         if (s->cert != NULL) ssl_cert_free(s->cert);
275         /* Free up if allocated */
276
277         if (s->ctx) SSL_CTX_free(s->ctx);
278
279         if (s->client_CA != NULL)
280                 sk_pop_free(s->client_CA,X509_NAME_free);
281
282         if (s->method != NULL) s->method->ssl_free(s);
283
284         Free((char *)s);
285         }
286
287 void SSL_set_bio(s, rbio,wbio)
288 SSL *s;
289 BIO *rbio;
290 BIO *wbio;
291         {
292         /* If the output buffering BIO is still in place, remove it
293          */
294         if (s->bbio != NULL)
295                 {
296                 if (s->wbio == s->bbio)
297                         {
298                         s->wbio=s->wbio->next_bio;
299                         s->bbio->next_bio=NULL;
300                         }
301                 }
302         if ((s->rbio != NULL) && (s->rbio != rbio))
303                 BIO_free_all(s->rbio);
304         if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
305                 BIO_free_all(s->wbio);
306         s->rbio=rbio;
307         s->wbio=wbio;
308         }
309
310 BIO *SSL_get_rbio(s)
311 SSL *s;
312         { return(s->rbio); }
313
314 BIO *SSL_get_wbio(s)
315 SSL *s;
316         { return(s->wbio); }
317
318 int SSL_get_fd(s)
319 SSL *s;
320         {
321         int ret= -1;
322         BIO *b,*r;
323
324         b=SSL_get_rbio(s);
325         r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
326         if (r != NULL)
327                 BIO_get_fd(r,&ret);
328         return(ret);
329         }
330
331 #ifndef NO_SOCK
332 int SSL_set_fd(s, fd)
333 SSL *s;
334 int fd;
335         {
336         int ret=0;
337         BIO *bio=NULL;
338
339         bio=BIO_new(BIO_s_socket());
340
341         if (bio == NULL)
342                 {
343                 SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
344                 goto err;
345                 }
346         BIO_set_fd(bio,fd,BIO_NOCLOSE);
347         SSL_set_bio(s,bio,bio);
348         ret=1;
349 err:
350         return(ret);
351         }
352
353 int SSL_set_wfd(s, fd)
354 SSL *s;
355 int fd;
356         {
357         int ret=0;
358         BIO *bio=NULL;
359
360         if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
361                 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
362                 {
363                 bio=BIO_new(BIO_s_socket());
364
365                 if (bio == NULL)
366                         { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; }
367                 BIO_set_fd(bio,fd,BIO_NOCLOSE);
368                 SSL_set_bio(s,SSL_get_rbio(s),bio);
369                 }
370         else
371                 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
372         ret=1;
373 err:
374         return(ret);
375         }
376
377 int SSL_set_rfd(s, fd)
378 SSL *s;
379 int fd;
380         {
381         int ret=0;
382         BIO *bio=NULL;
383
384         if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
385                 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
386                 {
387                 bio=BIO_new(BIO_s_socket());
388
389                 if (bio == NULL)
390                         {
391                         SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB);
392                         goto err;
393                         }
394                 BIO_set_fd(bio,fd,BIO_NOCLOSE);
395                 SSL_set_bio(s,bio,SSL_get_wbio(s));
396                 }
397         else
398                 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
399         ret=1;
400 err:
401         return(ret);
402         }
403 #endif
404
405 int SSL_get_verify_mode(s)
406 SSL *s;
407         {
408         return(s->verify_mode);
409         }
410
411 int (*SSL_get_verify_callback(s))()
412 SSL *s;
413         {
414         return(s->verify_callback);
415         }
416
417 int SSL_CTX_get_verify_mode(ctx)
418 SSL_CTX *ctx;
419         {
420         return(ctx->verify_mode);
421         }
422
423 int (*SSL_CTX_get_verify_callback(ctx))()
424 SSL_CTX *ctx;
425         {
426         return(ctx->default_verify_callback);
427         }
428
429 void SSL_set_verify(s, mode, callback)
430 SSL *s;
431 int mode;
432 int (*callback)();
433         {
434         s->verify_mode=mode;
435         if (callback != NULL)
436                 s->verify_callback=callback;
437         }
438
439 void SSL_set_read_ahead(s, yes)
440 SSL *s;
441 int yes;
442         {
443         s->read_ahead=yes;
444         }
445
446 int SSL_get_read_ahead(s)
447 SSL *s;
448         {
449         return(s->read_ahead);
450         }
451
452 int SSL_pending(s)
453 SSL *s;
454         {
455         return(s->method->ssl_pending(s));
456         }
457
458 X509 *SSL_get_peer_certificate(s)
459 SSL *s;
460         {
461         X509 *r;
462         
463         if ((s == NULL) || (s->session == NULL))
464                 r=NULL;
465         else
466                 r=s->session->peer;
467
468         if (r == NULL) return(r);
469
470         CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
471
472         return(r);
473         }
474
475 STACK *SSL_get_peer_cert_chain(s)
476 SSL *s;
477         {
478         STACK *r;
479         
480         if ((s == NULL) || (s->session == NULL) || (s->session->cert == NULL))
481                 r=NULL;
482         else
483                 r=s->session->cert->cert_chain;
484
485         return(r);
486         }
487
488 /* Now in theory, since the calling process own 't' it should be safe to
489  * modify.  We need to be able to read f without being hassled */
490 void SSL_copy_session_id(t,f)
491 SSL *t,*f;
492         {
493         CERT *tmp;
494
495         /* Do we need to to SSL locking? */
496         SSL_set_session(t,SSL_get_session(f));
497
498         /* what if we are setup as SSLv2 but want to talk SSLv3 or
499          * vice-versa */
500         if (t->method != f->method)
501                 {
502                 t->method->ssl_free(t); /* cleanup current */
503                 t->method=f->method;    /* change method */
504                 t->method->ssl_new(t);  /* setup new */
505                 }
506
507         tmp=t->cert;
508         if (f->cert != NULL)
509                 {
510                 CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT);
511                 t->cert=f->cert;
512                 }
513         else
514                 t->cert=NULL;
515         if (tmp != NULL) ssl_cert_free(tmp);
516         }
517
518 /* Fix this so it checks all the valid key/cert options */
519 int SSL_CTX_check_private_key(ctx)
520 SSL_CTX *ctx;
521         {
522         if (    (ctx == NULL) ||
523                 (ctx->default_cert == NULL) ||
524                 (ctx->default_cert->key->x509 == NULL))
525                 {
526                 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
527                 return(0);
528                 }
529         if      (ctx->default_cert->key->privatekey == NULL)
530                 {
531                 SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
532                 return(0);
533                 }
534         return(X509_check_private_key(ctx->default_cert->key->x509, ctx->default_cert->key->privatekey));
535         }
536
537 /* Fix this function so that it takes an optional type parameter */
538 int SSL_check_private_key(ssl)
539 SSL *ssl;
540         {
541         if (ssl == NULL)
542                 {
543                 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER);
544                 return(0);
545                 }
546         if (ssl->cert == NULL)
547                 return(SSL_CTX_check_private_key(ssl->ctx));
548         if (ssl->cert->key->x509 == NULL)
549                 {
550                 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
551                 return(0);
552                 }
553         if (ssl->cert->key->privatekey == NULL)
554                 {
555                 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
556                 return(0);
557                 }
558         return(X509_check_private_key(ssl->cert->key->x509,
559                 ssl->cert->key->privatekey));
560         }
561
562 int SSL_accept(s)
563 SSL *s;
564         {
565         return(s->method->ssl_accept(s));
566         }
567
568 int SSL_connect(s)
569 SSL *s;
570         {
571         return(s->method->ssl_connect(s));
572         }
573
574 long SSL_get_default_timeout(s)
575 SSL *s;
576         {
577         return(s->method->get_timeout());
578         }
579
580 int SSL_read(s,buf,num)
581 SSL *s;
582 char *buf;
583 int num;
584         {
585         if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
586                 {
587                 s->rwstate=SSL_NOTHING;
588                 return(0);
589                 }
590         return(s->method->ssl_read(s,buf,num));
591         }
592
593 int SSL_peek(s,buf,num)
594 SSL *s;
595 char *buf;
596 int num;
597         {
598         if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
599                 {
600                 return(0);
601                 }
602         return(s->method->ssl_peek(s,buf,num));
603         }
604
605 int SSL_write(s,buf,num)
606 SSL *s;
607 const char *buf;
608 int num;
609         {
610         if (s->shutdown & SSL_SENT_SHUTDOWN)
611                 {
612                 s->rwstate=SSL_NOTHING;
613                 SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN);
614                 return(-1);
615                 }
616         return(s->method->ssl_write(s,buf,num));
617         }
618
619 int SSL_shutdown(s)
620 SSL *s;
621         {
622         if ((s != NULL) && !SSL_in_init(s))
623                 return(s->method->ssl_shutdown(s));
624         else
625                 return(1);
626         }
627
628 int SSL_renegotiate(s)
629 SSL *s;
630         {
631         s->new_session=1;
632         return(s->method->ssl_renegotiate(s));
633         }
634
635 long SSL_ctrl(s,cmd,larg,parg)
636 SSL *s;
637 int cmd;
638 long larg;
639 char *parg;
640         {
641         long l;
642
643         switch (cmd)
644                 {
645         case SSL_CTRL_GET_READ_AHEAD:
646                 return(s->read_ahead);
647         case SSL_CTRL_SET_READ_AHEAD:
648                 l=s->read_ahead;
649                 s->read_ahead=larg;
650                 return(l);
651         case SSL_CTRL_OPTIONS:
652                 return(s->options|=larg);
653         default:
654                 return(s->method->ssl_ctrl(s,cmd,larg,parg));
655                 }
656         return(0);
657         }
658
659 long SSL_CTX_ctrl(ctx,cmd,larg,parg)
660 SSL_CTX *ctx;
661 int cmd;
662 long larg;
663 char *parg;
664         {
665         long l;
666
667         switch (cmd)
668                 {
669         case SSL_CTRL_GET_READ_AHEAD:
670                 return(ctx->read_ahead);
671         case SSL_CTRL_SET_READ_AHEAD:
672                 l=ctx->read_ahead;
673                 ctx->read_ahead=larg;
674                 return(l);
675
676         case SSL_CTRL_SET_SESS_CACHE_SIZE:
677                 l=ctx->session_cache_size;
678                 ctx->session_cache_size=larg;
679                 return(l);
680         case SSL_CTRL_GET_SESS_CACHE_SIZE:
681                 return(ctx->session_cache_size);
682         case SSL_CTRL_SET_SESS_CACHE_MODE:
683                 l=ctx->session_cache_mode;
684                 ctx->session_cache_mode=larg;
685                 return(l);
686         case SSL_CTRL_GET_SESS_CACHE_MODE:
687                 return(ctx->session_cache_mode);
688
689         case SSL_CTRL_SESS_NUMBER:
690                 return(ctx->sessions->num_items);
691         case SSL_CTRL_SESS_CONNECT:
692                 return(ctx->stats.sess_connect);
693         case SSL_CTRL_SESS_CONNECT_GOOD:
694                 return(ctx->stats.sess_connect_good);
695         case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
696                 return(ctx->stats.sess_connect_renegotiate);
697         case SSL_CTRL_SESS_ACCEPT:
698                 return(ctx->stats.sess_accept);
699         case SSL_CTRL_SESS_ACCEPT_GOOD:
700                 return(ctx->stats.sess_accept_good);
701         case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
702                 return(ctx->stats.sess_accept_renegotiate);
703         case SSL_CTRL_SESS_HIT:
704                 return(ctx->stats.sess_hit);
705         case SSL_CTRL_SESS_CB_HIT:
706                 return(ctx->stats.sess_cb_hit);
707         case SSL_CTRL_SESS_MISSES:
708                 return(ctx->stats.sess_miss);
709         case SSL_CTRL_SESS_TIMEOUTS:
710                 return(ctx->stats.sess_timeout);
711         case SSL_CTRL_SESS_CACHE_FULL:
712                 return(ctx->stats.sess_cache_full);
713         case SSL_CTRL_OPTIONS:
714                 return(ctx->options|=larg);
715         default:
716                 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
717                 }
718         return(0);
719         }
720
721 int ssl_cipher_id_cmp(a,b)
722 SSL_CIPHER *a,*b;
723         {
724         long l;
725
726         l=a->id-b->id;
727         if (l == 0L)
728                 return(0);
729         else
730                 return((l > 0)?1:-1);
731         }
732
733 int ssl_cipher_ptr_id_cmp(ap,bp)
734 SSL_CIPHER **ap,**bp;
735         {
736         long l;
737
738         l=(*ap)->id-(*bp)->id;
739         if (l == 0L)
740                 return(0);
741         else
742                 return((l > 0)?1:-1);
743         }
744
745 /* return a STACK of the ciphers available for the SSL and in order of
746  * preference */
747 STACK *SSL_get_ciphers(s)
748 SSL *s;
749         {
750         if ((s != NULL) && (s->cipher_list != NULL))
751                 {
752                 return(s->cipher_list);
753                 }
754         else if ((s->ctx != NULL) &&
755                 (s->ctx->cipher_list != NULL))
756                 {
757                 return(s->ctx->cipher_list);
758                 }
759         return(NULL);
760         }
761
762 /* return a STACK of the ciphers available for the SSL and in order of
763  * algorithm id */
764 STACK *ssl_get_ciphers_by_id(s)
765 SSL *s;
766         {
767         if ((s != NULL) && (s->cipher_list_by_id != NULL))
768                 {
769                 return(s->cipher_list_by_id);
770                 }
771         else if ((s != NULL) && (s->ctx != NULL) &&
772                 (s->ctx->cipher_list_by_id != NULL))
773                 {
774                 return(s->ctx->cipher_list_by_id);
775                 }
776         return(NULL);
777         }
778
779 /* The old interface to get the same thing as SSL_get_ciphers() */
780 char *SSL_get_cipher_list(s,n)
781 SSL *s;
782 int n;
783         {
784         SSL_CIPHER *c;
785         STACK *sk;
786
787         if (s == NULL) return(NULL);
788         sk=SSL_get_ciphers(s);
789         if ((sk == NULL) || (sk_num(sk) <= n))
790                 return(NULL);
791         c=(SSL_CIPHER *)sk_value(sk,n);
792         if (c == NULL) return(NULL);
793         return(c->name);
794         }
795
796 /* specify the ciphers to be used by defaut by the SSL_CTX */
797 int SSL_CTX_set_cipher_list(ctx,str)
798 SSL_CTX *ctx;
799 char *str;
800         {
801         STACK *sk;
802         
803         sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
804                 &ctx->cipher_list_by_id,str);
805 /* XXXX */
806         return((sk == NULL)?0:1);
807         }
808
809 /* specify the ciphers to be used by the SSL */
810 int SSL_set_cipher_list(s, str)
811 SSL *s;
812 char *str;
813         {
814         STACK *sk;
815         
816         sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
817                 &s->cipher_list_by_id,str);
818 /* XXXX */
819         return((sk == NULL)?0:1);
820         }
821
822 /* works well for SSLv2, not so good for SSLv3 */
823 char *SSL_get_shared_ciphers(s,buf,len)
824 SSL *s;
825 char *buf;
826 int len;
827         {
828         char *p,*cp;
829         STACK *sk;
830         SSL_CIPHER *c;
831         int i;
832
833         if ((s->session == NULL) || (s->session->ciphers == NULL) ||
834                 (len < 2))
835                 return(NULL);
836
837         p=buf;
838         sk=s->session->ciphers;
839         for (i=0; i<sk_num(sk); i++)
840                 {
841                 /* Decrement for either the ':' or a '\0' */
842                 len--;
843                 c=(SSL_CIPHER *)sk_value(sk,i);
844                 for (cp=c->name; *cp; )
845                         {
846                         if (len-- == 0)
847                                 {
848                                 *p='\0';
849                                 return(buf);
850                                 }
851                         else
852                                 *(p++)= *(cp++);
853                         }
854                 *(p++)=':';
855                 }
856         p[-1]='\0';
857         return(buf);
858         }
859
860 int ssl_cipher_list_to_bytes(s,sk,p)
861 SSL *s;
862 STACK *sk;
863 unsigned char *p;
864         {
865         int i,j=0;
866         SSL_CIPHER *c;
867         unsigned char *q;
868
869         if (sk == NULL) return(0);
870         q=p;
871
872         for (i=0; i<sk_num(sk); i++)
873                 {
874                 c=(SSL_CIPHER *)sk_value(sk,i);
875                 j=ssl_put_cipher_by_char(s,c,p);
876                 p+=j;
877                 }
878         return(p-q);
879         }
880
881 STACK *ssl_bytes_to_cipher_list(s,p,num,skp)
882 SSL *s;
883 unsigned char *p;
884 int num;
885 STACK **skp;
886         {
887         SSL_CIPHER *c;
888         STACK *sk;
889         int i,n;
890
891         n=ssl_put_cipher_by_char(s,NULL,NULL);
892         if ((num%n) != 0)
893                 {
894                 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
895                 return(NULL);
896                 }
897         if ((skp == NULL) || (*skp == NULL))
898                 sk=sk_new(NULL); /* change perhaps later */
899         else
900                 {
901                 sk= *skp;
902                 sk_zero(sk);
903                 }
904
905         for (i=0; i<num; i+=n)
906                 {
907                 c=ssl_get_cipher_by_char(s,p);
908                 p+=n;
909                 if (c != NULL)
910                         {
911                         if (!sk_push(sk,(char *)c))
912                                 {
913                                 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
914                                 goto err;
915                                 }
916                         }
917                 }
918
919         if (skp != NULL)
920                 *skp=sk;
921         return(sk);
922 err:
923         if ((skp == NULL) || (*skp == NULL))
924                 sk_free(sk);
925         return(NULL);
926         }
927
928 unsigned long SSL_SESSION_hash(a)
929 SSL_SESSION *a;
930         {
931         unsigned long l;
932
933         l=(unsigned long)
934                 ((unsigned int) a->session_id[0]     )|
935                 ((unsigned int) a->session_id[1]<< 8L)|
936                 ((unsigned long)a->session_id[2]<<16L)|
937                 ((unsigned long)a->session_id[3]<<24L);
938         return(l);
939         }
940
941 int SSL_SESSION_cmp(a, b)
942 SSL_SESSION *a;
943 SSL_SESSION *b;
944         {
945         if (a->ssl_version != b->ssl_version)
946                 return(1);
947         if (a->session_id_length != b->session_id_length)
948                 return(1);
949         return(memcmp(a->session_id,b->session_id,a->session_id_length));
950         }
951
952 SSL_CTX *SSL_CTX_new(meth)
953 SSL_METHOD *meth;
954         {
955         SSL_CTX *ret=NULL;
956         
957         if (meth == NULL)
958                 {
959                 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);
960                 return(NULL);
961                 }
962
963         if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
964                 {
965                 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
966                 goto err;
967                 }
968         ret=(SSL_CTX *)Malloc(sizeof(SSL_CTX));
969         if (ret == NULL)
970                 goto err;
971
972         memset(ret,0,sizeof(SSL_CTX));
973
974         ret->method=meth;
975
976         ret->cert_store=NULL;
977         ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
978         ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
979         ret->session_cache_head=NULL;
980         ret->session_cache_tail=NULL;
981
982         /* We take the system default */
983         ret->session_timeout=meth->get_timeout();
984
985         ret->new_session_cb=NULL;
986         ret->remove_session_cb=NULL;
987         ret->get_session_cb=NULL;
988
989         memset((char *)&ret->stats,0,sizeof(ret->stats));
990
991         ret->references=1;
992         ret->quiet_shutdown=0;
993
994 /*      ret->cipher=NULL;*/
995 /*      ret->s2->challenge=NULL;
996         ret->master_key=NULL;
997         ret->key_arg=NULL;
998         ret->s2->conn_id=NULL; */
999
1000         ret->info_callback=NULL;
1001
1002         ret->app_verify_callback=NULL;
1003         ret->app_verify_arg=NULL;
1004
1005         ret->read_ahead=0;
1006         ret->verify_mode=SSL_VERIFY_NONE;
1007         ret->default_verify_callback=NULL;
1008         if ((ret->default_cert=ssl_cert_new()) == NULL)
1009                 goto err;
1010
1011         ret->default_passwd_callback=NULL;
1012         ret->client_cert_cb=NULL;
1013
1014         ret->sessions=lh_new(SSL_SESSION_hash,SSL_SESSION_cmp);
1015         if (ret->sessions == NULL) goto err;
1016         ret->cert_store=X509_STORE_new();
1017         if (ret->cert_store == NULL) goto err;
1018
1019         ssl_create_cipher_list(ret->method,
1020                 &ret->cipher_list,&ret->cipher_list_by_id,
1021                 SSL_DEFAULT_CIPHER_LIST);
1022         if ((ret->cipher_list == NULL) || (sk_num(ret->cipher_list) <= 0))
1023                 {
1024                 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);
1025                 goto err2;
1026                 }
1027
1028         if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)
1029                 {
1030                 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);
1031                 goto err2;
1032                 }
1033         if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)
1034                 {
1035                 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
1036                 goto err2;
1037                 }
1038         if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)
1039                 {
1040                 SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
1041                 goto err2;
1042                 }
1043
1044         if ((ret->client_CA=sk_new_null()) == NULL)
1045                 goto err;
1046
1047         CRYPTO_new_ex_data(ssl_ctx_meth,(char *)ret,&ret->ex_data);
1048
1049         ret->extra_certs=NULL;
1050         ret->comp_methods=SSL_COMP_get_compression_methods();
1051
1052         return(ret);
1053 err:
1054         SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
1055 err2:
1056         if (ret != NULL) SSL_CTX_free(ret);
1057         return(NULL);
1058         }
1059
1060 void SSL_CTX_free(a)
1061 SSL_CTX *a;
1062         {
1063         int i;
1064
1065         if (a == NULL) return;
1066
1067         i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
1068 #ifdef REF_PRINT
1069         REF_PRINT("SSL_CTX",a);
1070 #endif
1071         if (i > 0) return;
1072 #ifdef REF_CHECK
1073         if (i < 0)
1074                 {
1075                 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
1076                 abort(); /* ok */
1077                 }
1078 #endif
1079         CRYPTO_free_ex_data(ssl_ctx_meth,(char *)a,&a->ex_data);
1080
1081         if (a->sessions != NULL)
1082                 {
1083                 SSL_CTX_flush_sessions(a,0);
1084                 lh_free(a->sessions);
1085                 }
1086         if (a->cert_store != NULL)
1087                 X509_STORE_free(a->cert_store);
1088         if (a->cipher_list != NULL)
1089                 sk_free(a->cipher_list);
1090         if (a->cipher_list_by_id != NULL)
1091                 sk_free(a->cipher_list_by_id);
1092         if (a->default_cert != NULL)
1093                 ssl_cert_free(a->default_cert);
1094         if (a->client_CA != NULL)
1095                 sk_pop_free(a->client_CA,X509_NAME_free);
1096         if (a->extra_certs != NULL)
1097                 sk_pop_free(a->extra_certs,X509_free);
1098         if (a->comp_methods != NULL)
1099                 sk_pop_free(a->comp_methods,free);
1100         Free((char *)a);
1101         }
1102
1103 void SSL_CTX_set_default_passwd_cb(ctx,cb)
1104 SSL_CTX *ctx;
1105 int (*cb)();
1106         {
1107         ctx->default_passwd_callback=cb;
1108         }
1109
1110 void SSL_CTX_set_cert_verify_cb(ctx,cb,arg)
1111 SSL_CTX *ctx;
1112 int (*cb)();
1113 char *arg;
1114         {
1115         ctx->app_verify_callback=cb;
1116         ctx->app_verify_arg=arg;
1117         }
1118
1119 void SSL_CTX_set_verify(ctx,mode,cb)
1120 SSL_CTX *ctx;
1121 int mode;
1122 #ifndef NOPROTO
1123 int (*cb)(int, X509_STORE_CTX *);
1124 #else
1125 int (*cb)();
1126 #endif
1127         {
1128         ctx->verify_mode=mode;
1129         ctx->default_verify_callback=cb;
1130         /* This needs cleaning up EAY EAY EAY */
1131         X509_STORE_set_verify_cb_func(ctx->cert_store,cb);
1132         }
1133
1134 void ssl_set_cert_masks(c,cipher)
1135 CERT *c;
1136 SSL_CIPHER *cipher;
1137         {
1138         CERT_PKEY *cpk;
1139         int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
1140         int rsa_enc_export,dh_rsa_export,dh_dsa_export;
1141         int rsa_tmp_export,dh_tmp_export,kl;
1142         unsigned long mask,emask;
1143
1144         if ((c == NULL) || (c->valid)) return;
1145
1146         kl=SSL_C_EXPORT_PKEYLENGTH(cipher);
1147
1148 #ifndef NO_RSA
1149         rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
1150         rsa_tmp_export=(c->rsa_tmp_cb != NULL ||
1151                 (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl));
1152 #else
1153         rsa_tmp=rsa_tmp_export=0;
1154 #endif
1155 #ifndef NO_DH
1156         dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
1157         dh_tmp_export=(c->dh_tmp_cb != NULL ||
1158                 (dh_tmp && DH_size(c->dh_tmp)*8 <= kl));
1159 #else
1160         dh_tmp=dh_tmp_export=0;
1161 #endif
1162
1163         cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
1164         rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL);
1165         rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1166         cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
1167         rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
1168         cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
1169         dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
1170         cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
1171         dh_rsa=  (cpk->x509 != NULL && cpk->privatekey != NULL);
1172         dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1173         cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
1174 /* FIX THIS EAY EAY EAY */
1175         dh_dsa=  (cpk->x509 != NULL && cpk->privatekey != NULL);
1176         dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1177
1178         mask=0;
1179         emask=0;
1180
1181 #ifdef CIPHER_DEBUG
1182         printf("rt=%d dht=%d re=%d rs=%d ds=%d dhr=%d dhd=%d\n",
1183                 rsa_tmp,dh_tmp,
1184                 rsa_enc,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
1185 #endif
1186
1187         if (rsa_enc || (rsa_tmp && rsa_sign))
1188                 mask|=SSL_kRSA;
1189         if (rsa_enc_export || (rsa_tmp_export && rsa_sign))
1190                 emask|=SSL_kRSA;
1191
1192 #if 0
1193         /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
1194         if (    (dh_tmp || dh_rsa || dh_dsa) && 
1195                 (rsa_enc || rsa_sign || dsa_sign))
1196                 mask|=SSL_kEDH;
1197         if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
1198                 (rsa_enc || rsa_sign || dsa_sign))
1199                 emask|=SSL_kEDH;
1200 #endif
1201
1202         if (dh_tmp_export) 
1203                 emask|=SSL_kEDH;
1204
1205         if (dh_tmp)
1206                 mask|=SSL_kEDH;
1207
1208         if (dh_rsa) mask|=SSL_kDHr;
1209         if (dh_rsa_export) emask|=SSL_kDHr;
1210
1211         if (dh_dsa) mask|=SSL_kDHd;
1212         if (dh_dsa_export) emask|=SSL_kDHd;
1213
1214         if (rsa_enc || rsa_sign)
1215                 {
1216                 mask|=SSL_aRSA;
1217                 emask|=SSL_aRSA;
1218                 }
1219
1220         if (dsa_sign)
1221                 {
1222                 mask|=SSL_aDSS;
1223                 emask|=SSL_aDSS;
1224                 }
1225
1226 #ifdef SSL_ALLOW_ADH
1227         mask|=SSL_aNULL;
1228         emask|=SSL_aNULL;
1229 #endif
1230
1231         c->mask=mask;
1232         c->export_mask=emask;
1233         c->valid=1;
1234         }
1235
1236 /* THIS NEEDS CLEANING UP */
1237 X509 *ssl_get_server_send_cert(s)
1238 SSL *s;
1239         {
1240         unsigned long alg,mask,kalg;
1241         CERT *c;
1242         int i,export;
1243
1244         c=s->cert;
1245         ssl_set_cert_masks(c,s->s3->tmp.new_cipher);
1246         alg=s->s3->tmp.new_cipher->algorithms;
1247         export=SSL_IS_EXPORT(alg);
1248         mask=export?c->export_mask:c->mask;
1249         kalg=alg&(SSL_MKEY_MASK|SSL_AUTH_MASK);
1250
1251         if      (kalg & SSL_kDHr)
1252                 i=SSL_PKEY_DH_RSA;
1253         else if (kalg & SSL_kDHd)
1254                 i=SSL_PKEY_DH_DSA;
1255         else if (kalg & SSL_aDSS)
1256                 i=SSL_PKEY_DSA_SIGN;
1257         else if (kalg & SSL_aRSA)
1258                 {
1259                 if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
1260                         i=SSL_PKEY_RSA_SIGN;
1261                 else
1262                         i=SSL_PKEY_RSA_ENC;
1263                 }
1264         else /* if (kalg & SSL_aNULL) */
1265                 {
1266                 SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,SSL_R_INTERNAL_ERROR);
1267                 return(NULL);
1268                 }
1269         if (c->pkeys[i].x509 == NULL) return(NULL);
1270         return(c->pkeys[i].x509);
1271         }
1272
1273 EVP_PKEY *ssl_get_sign_pkey(s,cipher)
1274 SSL *s;
1275 SSL_CIPHER *cipher;
1276         {
1277         unsigned long alg;
1278         CERT *c;
1279
1280         alg=cipher->algorithms;
1281         c=s->cert;
1282
1283         if ((alg & SSL_aDSS) &&
1284                 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
1285                 return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey);
1286         else if (alg & SSL_aRSA)
1287                 {
1288                 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
1289                         return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey);
1290                 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
1291                         return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey);
1292                 else
1293                         return(NULL);
1294                 }
1295         else /* if (alg & SSL_aNULL) */
1296                 {
1297                 SSLerr(SSL_F_SSL_GET_SIGN_PKEY,SSL_R_INTERNAL_ERROR);
1298                 return(NULL);
1299                 }
1300         }
1301
1302 void ssl_update_cache(s,mode)
1303 SSL *s;
1304 int mode;
1305         {
1306         int i;
1307
1308         /* If the session_id_length is 0, we are not supposed to cache it,
1309          * and it would be rather hard to do anyway :-) */
1310         if (s->session->session_id_length == 0) return;
1311
1312         if ((s->ctx->session_cache_mode & mode)
1313                 && (!s->hit)
1314                 && SSL_CTX_add_session(s->ctx,s->session)
1315                 && (s->ctx->new_session_cb != NULL))
1316                 {
1317                 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
1318                 if (!s->ctx->new_session_cb(s,s->session))
1319                         SSL_SESSION_free(s->session);
1320                 }
1321
1322         /* auto flush every 255 connections */
1323         i=s->ctx->session_cache_mode;
1324         if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
1325                 ((i & mode) == mode))
1326                 {
1327                 if (  (((mode & SSL_SESS_CACHE_CLIENT)
1328                         ?s->ctx->stats.sess_connect_good
1329                         :s->ctx->stats.sess_accept_good) & 0xff) == 0xff)
1330                         {
1331                         SSL_CTX_flush_sessions(s->ctx,time(NULL));
1332                         }
1333                 }
1334         }
1335
1336 SSL_METHOD *SSL_get_ssl_method(s)
1337 SSL *s;
1338         {
1339         return(s->method);
1340         }
1341
1342 int SSL_set_ssl_method(s,meth)
1343 SSL *s;
1344 SSL_METHOD *meth;
1345         {
1346         int conn= -1;
1347         int ret=1;
1348
1349         if (s->method != meth)
1350                 {
1351                 if (s->handshake_func != NULL)
1352                         conn=(s->handshake_func == s->method->ssl_connect);
1353
1354                 if (s->method->version == meth->version)
1355                         s->method=meth;
1356                 else
1357                         {
1358                         s->method->ssl_free(s);
1359                         s->method=meth;
1360                         ret=s->method->ssl_new(s);
1361                         }
1362
1363                 if (conn == 1)
1364                         s->handshake_func=meth->ssl_connect;
1365                 else if (conn == 0)
1366                         s->handshake_func=meth->ssl_accept;
1367                 }
1368         return(ret);
1369         }
1370
1371 int SSL_get_error(s,i)
1372 SSL *s;
1373 int i;
1374         {
1375         int reason;
1376         unsigned long l;
1377         BIO *bio;
1378
1379         if (i > 0) return(SSL_ERROR_NONE);
1380
1381         /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
1382          * etc, where we do encode the error */
1383         if ((l=ERR_peek_error()) != 0)
1384                 {
1385                 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
1386                         return(SSL_ERROR_SYSCALL);
1387                 else
1388                         return(SSL_ERROR_SSL);
1389                 }
1390
1391         if ((i < 0) && SSL_want_read(s))
1392                 {
1393                 bio=SSL_get_rbio(s);
1394                 if (BIO_should_read(bio))
1395                         return(SSL_ERROR_WANT_READ);
1396                 else if (BIO_should_write(bio))
1397                         return(SSL_ERROR_WANT_WRITE);
1398                 else if (BIO_should_io_special(bio))
1399                         {
1400                         reason=BIO_get_retry_reason(bio);
1401                         if (reason == BIO_RR_CONNECT)
1402                                 return(SSL_ERROR_WANT_CONNECT);
1403                         else
1404                                 return(SSL_ERROR_SYSCALL); /* unknown */
1405                         }
1406                 }
1407
1408         if ((i < 0) && SSL_want_write(s))
1409                 {
1410                 bio=SSL_get_wbio(s);
1411                 if (BIO_should_write(bio))
1412                         return(SSL_ERROR_WANT_WRITE);
1413                 else if (BIO_should_read(bio))
1414                         return(SSL_ERROR_WANT_READ);
1415                 else if (BIO_should_io_special(bio))
1416                         {
1417                         reason=BIO_get_retry_reason(bio);
1418                         if (reason == BIO_RR_CONNECT)
1419                                 return(SSL_ERROR_WANT_CONNECT);
1420                         else
1421                                 return(SSL_ERROR_SYSCALL);
1422                         }
1423                 }
1424         if ((i < 0) && SSL_want_x509_lookup(s))
1425                 {
1426                 return(SSL_ERROR_WANT_X509_LOOKUP);
1427                 }
1428
1429         if (i == 0)
1430                 {
1431                 if (s->version == SSL2_VERSION)
1432                         {
1433                         /* assume it is the socket being closed */
1434                         return(SSL_ERROR_ZERO_RETURN);
1435                         }
1436                 else
1437                         {
1438                         if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
1439                                 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
1440                                 return(SSL_ERROR_ZERO_RETURN);
1441                         }
1442                 }
1443         return(SSL_ERROR_SYSCALL);
1444         }
1445
1446 int SSL_do_handshake(s)
1447 SSL *s;
1448         {
1449         int ret=1;
1450
1451         if (s->handshake_func == NULL)
1452                 {
1453                 SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
1454                 return(-1);
1455                 }
1456
1457         s->method->ssl_renegotiate_check(s);
1458
1459         if (SSL_in_init(s) || SSL_in_before(s))
1460                 {
1461                 ret=s->handshake_func(s);
1462                 }
1463         return(ret);
1464         }
1465
1466 /* For the next 2 functions, SSL_clear() sets shutdown and so
1467  * one of these calls will reset it */
1468 void SSL_set_accept_state(s)
1469 SSL *s;
1470         {
1471         s->server=1;
1472         s->shutdown=0;
1473         s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
1474         s->handshake_func=s->method->ssl_accept;
1475         /* clear the current cipher */
1476         ssl_clear_cipher_ctx(s);
1477         }
1478
1479 void SSL_set_connect_state(s)
1480 SSL *s;
1481         {
1482         s->server=0;
1483         s->shutdown=0;
1484         s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
1485         s->handshake_func=s->method->ssl_connect;
1486         /* clear the current cipher */
1487         ssl_clear_cipher_ctx(s);
1488         }
1489
1490 int ssl_undefined_function(s)
1491 SSL *s;
1492         {
1493         SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1494         return(0);
1495         }
1496
1497 SSL_METHOD *ssl_bad_method(ver)
1498 int ver;
1499         {
1500         SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1501         return(NULL);
1502         }
1503
1504 char *SSL_get_version(s)
1505 SSL *s;
1506         {
1507         if (s->version == TLS1_VERSION)
1508                 return("TLSv1");
1509         else if (s->version == SSL3_VERSION)
1510                 return("SSLv3");
1511         else if (s->version == SSL2_VERSION)
1512                 return("SSLv2");
1513         else
1514                 return("unknown");
1515         }
1516
1517 SSL *SSL_dup(s)
1518 SSL *s;
1519         {
1520         STACK *sk;
1521         X509_NAME *xn;
1522         SSL *ret;
1523         int i;
1524                  
1525         if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL) return(NULL);
1526                           
1527         /* This copies version, session-id, SSL_METHOD and 'cert' */
1528         SSL_copy_session_id(ret,s);
1529
1530         SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
1531         SSL_set_verify(ret,SSL_get_verify_mode(s),
1532                 SSL_get_verify_callback(s));
1533
1534         SSL_set_info_callback(ret,SSL_get_info_callback(s));
1535         
1536         ret->debug=s->debug;
1537         ret->options=s->options;
1538
1539         /* copy app data, a little dangerous perhaps */
1540         if (!CRYPTO_dup_ex_data(ssl_meth,&ret->ex_data,&s->ex_data))
1541                 goto err;
1542
1543         /* setup rbio, and wbio */
1544         if (s->rbio != NULL)
1545                 {
1546                 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
1547                         goto err;
1548                 }
1549         if (s->wbio != NULL)
1550                 {
1551                 if (s->wbio != s->rbio)
1552                         {
1553                         if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
1554                                 goto err;
1555                         }
1556                 else
1557                         ret->wbio=ret->rbio;
1558                 }
1559
1560         /* dup the cipher_list and cipher_list_by_id stacks */
1561         if (s->cipher_list != NULL)
1562                 {
1563                 if ((ret->cipher_list=sk_dup(s->cipher_list)) == NULL)
1564                         goto err;
1565                 }
1566         if (s->cipher_list_by_id != NULL)
1567                 if ((ret->cipher_list_by_id=sk_dup(s->cipher_list_by_id))
1568                         == NULL)
1569                         goto err;
1570
1571         /* Dup the client_CA list */
1572         if (s->client_CA != NULL)
1573                 {
1574                 if ((sk=sk_dup(s->client_CA)) == NULL) goto err;
1575                 ret->client_CA=sk;
1576                 for (i=0; i<sk_num(sk); i++)
1577                         {
1578                         xn=(X509_NAME *)sk_value(sk,i);
1579                         if ((sk_value(sk,i)=(char *)X509_NAME_dup(xn)) == NULL)
1580                                 {
1581                                 X509_NAME_free(xn);
1582                                 goto err;
1583                                 }
1584                         }
1585                 }
1586
1587         ret->shutdown=s->shutdown;
1588         ret->state=s->state;
1589         ret->handshake_func=s->handshake_func;
1590         ret->server=s->server;
1591
1592         if (0)
1593                 {
1594 err:
1595                 if (ret != NULL) SSL_free(ret);
1596                 ret=NULL;
1597                 }
1598         return(ret);
1599         }
1600
1601 void ssl_clear_cipher_ctx(s)
1602 SSL *s;
1603         {
1604         if (s->enc_read_ctx != NULL)
1605                 {
1606                 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
1607                 Free(s->enc_read_ctx);
1608                 s->enc_read_ctx=NULL;
1609                 }
1610         if (s->enc_write_ctx != NULL)
1611                 {
1612                 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
1613                 Free(s->enc_write_ctx);
1614                 s->enc_write_ctx=NULL;
1615                 }
1616         if (s->expand != NULL)
1617                 {
1618                 COMP_CTX_free(s->expand);
1619                 s->expand=NULL;
1620                 }
1621         if (s->compress != NULL)
1622                 {
1623                 COMP_CTX_free(s->compress);
1624                 s->compress=NULL;
1625                 }
1626         }
1627
1628 /* Fix this function so that it takes an optional type parameter */
1629 X509 *SSL_get_certificate(s)
1630 SSL *s;
1631         {
1632         if (s->cert != NULL)
1633                 return(s->cert->key->x509);
1634         else
1635                 return(NULL);
1636         }
1637
1638 /* Fix this function so that it takes an optional type parameter */
1639 EVP_PKEY *SSL_get_privatekey(s)
1640 SSL *s;
1641         {
1642         if (s->cert != NULL)
1643                 return(s->cert->key->privatekey);
1644         else
1645                 return(NULL);
1646         }
1647
1648 SSL_CIPHER *SSL_get_current_cipher(s)
1649 SSL *s;
1650         {
1651         if ((s->session != NULL) && (s->session->cipher != NULL))
1652                 return(s->session->cipher);
1653         return(NULL);
1654         }
1655
1656 int ssl_init_wbio_buffer(s,push)
1657 SSL *s;
1658 int push;
1659         {
1660         BIO *bbio;
1661
1662         if (s->bbio == NULL)
1663                 {
1664                 bbio=BIO_new(BIO_f_buffer());
1665                 if (bbio == NULL) return(0);
1666                 s->bbio=bbio;
1667                 }
1668         else
1669                 {
1670                 bbio=s->bbio;
1671                 if (s->bbio == s->wbio)
1672                         s->wbio=BIO_pop(s->wbio);
1673                 }
1674         BIO_reset(bbio);
1675 /*      if (!BIO_set_write_buffer_size(bbio,16*1024)) */
1676         if (!BIO_set_read_buffer_size(bbio,1))
1677                 {
1678                 SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
1679                 return(0);
1680                 }
1681         if (push)
1682                 {
1683                 if (s->wbio != bbio)
1684                         s->wbio=BIO_push(bbio,s->wbio);
1685                 }
1686         else
1687                 {
1688                 if (s->wbio == bbio)
1689                         s->wbio=BIO_pop(bbio);
1690                 }
1691         return(1);
1692         }
1693
1694 void ssl_free_wbio_buffer(s)
1695 SSL *s;
1696         {
1697         BIO *under;
1698
1699         if (s->bbio == NULL) return;
1700
1701         if (s->bbio == s->wbio)
1702                 {
1703                 /* remove buffering */
1704                 under=BIO_pop(s->wbio);
1705                 if (under != NULL)
1706                         s->wbio=under;
1707                 else
1708                         abort(); /* ok */
1709                 }
1710         BIO_free(s->bbio);
1711         s->bbio=NULL;
1712         }
1713         
1714 void SSL_CTX_set_quiet_shutdown(ctx,mode)
1715 SSL_CTX *ctx;
1716 int mode;
1717         {
1718         ctx->quiet_shutdown=mode;
1719         }
1720
1721 int SSL_CTX_get_quiet_shutdown(ctx)
1722 SSL_CTX *ctx;
1723         {
1724         return(ctx->quiet_shutdown);
1725         }
1726
1727 void SSL_set_quiet_shutdown(s,mode)
1728 SSL *s;
1729 int mode;
1730         {
1731         s->quiet_shutdown=mode;
1732         }
1733
1734 int SSL_get_quiet_shutdown(s)
1735 SSL *s;
1736         {
1737         return(s->quiet_shutdown);
1738         }
1739
1740 void SSL_set_shutdown(s,mode)
1741 SSL *s;
1742 int mode;
1743         {
1744         s->shutdown=mode;
1745         }
1746
1747 int SSL_get_shutdown(s)
1748 SSL *s;
1749         {
1750         return(s->shutdown);
1751         }
1752
1753 int SSL_version(s)
1754 SSL *s;
1755         {
1756         return(s->version);
1757         }
1758
1759 SSL_CTX *SSL_get_SSL_CTX(ssl)
1760 SSL *ssl;
1761         {
1762         return(ssl->ctx);
1763         }
1764
1765 #ifndef NO_STDIO
1766 int SSL_CTX_set_default_verify_paths(ctx)
1767 SSL_CTX *ctx;
1768         {
1769         return(X509_STORE_set_default_paths(ctx->cert_store));
1770         }
1771
1772 int SSL_CTX_load_verify_locations(ctx,CAfile,CApath)
1773 SSL_CTX *ctx;
1774 char *CAfile;
1775 char *CApath;
1776         {
1777         return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
1778         }
1779 #endif
1780
1781 void SSL_set_info_callback(ssl,cb)
1782 SSL *ssl;
1783 void (*cb)();
1784         {
1785         ssl->info_callback=cb;
1786         }
1787
1788 void (*SSL_get_info_callback(ssl))()
1789 SSL *ssl;
1790         {
1791         return((void (*)())ssl->info_callback);
1792         }
1793
1794 int SSL_state(ssl)
1795 SSL *ssl;
1796         {
1797         return(ssl->state);
1798         }
1799
1800 void SSL_set_verify_result(ssl,arg)
1801 SSL *ssl;
1802 long arg;
1803         {
1804         ssl->verify_result=arg;
1805         }
1806
1807 long SSL_get_verify_result(ssl)
1808 SSL *ssl;
1809         {
1810         return(ssl->verify_result);
1811         }
1812
1813 int SSL_get_ex_new_index(argl,argp,new_func,dup_func,free_func)
1814 long argl;
1815 char *argp;
1816 int (*new_func)();
1817 int (*dup_func)();
1818 void (*free_func)();
1819         {
1820         ssl_meth_num++;
1821         return(CRYPTO_get_ex_new_index(ssl_meth_num-1,
1822                 &ssl_meth,argl,argp,new_func,dup_func,free_func));
1823         }
1824
1825 int SSL_set_ex_data(s,idx,arg)
1826 SSL *s;
1827 int idx;
1828 void *arg;
1829         {
1830         return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
1831         }
1832
1833 void *SSL_get_ex_data(s,idx)
1834 SSL *s;
1835 int idx;
1836         {
1837         return(CRYPTO_get_ex_data(&s->ex_data,idx));
1838         }
1839
1840 int SSL_CTX_get_ex_new_index(argl,argp,new_func,dup_func,free_func)
1841 long argl;
1842 char *argp;
1843 int (*new_func)();
1844 int (*dup_func)();
1845 void (*free_func)();
1846         {
1847         ssl_ctx_meth_num++;
1848         return(CRYPTO_get_ex_new_index(ssl_ctx_meth_num-1,
1849                 &ssl_ctx_meth,argl,argp,new_func,dup_func,free_func));
1850         }
1851
1852 int SSL_CTX_set_ex_data(s,idx,arg)
1853 SSL_CTX *s;
1854 int idx;
1855 void *arg;
1856         {
1857         return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
1858         }
1859
1860 void *SSL_CTX_get_ex_data(s,idx)
1861 SSL_CTX *s;
1862 int idx;
1863         {
1864         return(CRYPTO_get_ex_data(&s->ex_data,idx));
1865         }
1866
1867 int ssl_ok(s)
1868 SSL *s;
1869         {
1870         return(1);
1871         }
1872
1873 X509_STORE *SSL_CTX_get_cert_store(ctx)
1874 SSL_CTX *ctx;
1875         {
1876         return(ctx->cert_store);
1877         }
1878
1879 void SSL_CTX_set_cert_store(ctx,store)
1880 SSL_CTX *ctx;
1881 X509_STORE *store;
1882         {
1883         if (ctx->cert_store != NULL)
1884                 X509_STORE_free(ctx->cert_store);
1885         ctx->cert_store=store;
1886         }
1887
1888 int SSL_want(s)
1889 SSL *s;
1890         {
1891         return(s->rwstate);
1892         }
1893
1894 void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,int export,
1895                                                           int keylength))
1896     { SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,0,(char *)cb); }
1897
1898 void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int export,
1899                                                         int keylength))
1900     { SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,0,(char *)dh); }
1901
1902 #if defined(_WINDLL) && defined(WIN16)
1903 #include "../crypto/bio/bss_file.c"
1904 #endif
1905