862a555efa6398a69cc3ff1718bc1a8c7a79a177
[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)
1135 CERT *c;
1136         {
1137         CERT_PKEY *cpk;
1138         int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
1139         int rsa_enc_export,dh_rsa_export,dh_dsa_export;
1140         int rsa_tmp_export,dh_tmp_export;
1141         unsigned long mask,emask;
1142
1143         if ((c == NULL) || (c->valid)) return;
1144
1145 #ifndef NO_RSA
1146         rsa_tmp=((c->rsa_tmp != NULL) || (c->rsa_tmp_cb != NULL))?1:0;
1147         rsa_tmp_export=((c->rsa_tmp_cb != NULL) ||
1148                 (rsa_tmp && (RSA_size(c->rsa_tmp)*8 <= 512)))?1:0;
1149 #else
1150         rsa_tmp=rsa_tmp_export=0;
1151 #endif
1152 #ifndef NO_DH
1153         dh_tmp=((c->dh_tmp != NULL) || (c->dh_tmp_cb != NULL))?1:0;
1154         dh_tmp_export=((c->dh_tmp_cb != NULL) ||
1155                 (dh_tmp && (DH_size(c->dh_tmp)*8 <= 512)))?1:0;
1156 #else
1157         dh_tmp=dh_tmp_export=0;
1158 #endif
1159
1160         cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
1161         rsa_enc= ((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1162         rsa_enc_export=(rsa_enc && (EVP_PKEY_size(cpk->privatekey)*8 <= 512))?1:0;
1163         cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
1164         rsa_sign=((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1165         cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
1166         dsa_sign=((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1167         cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
1168         dh_rsa=  ((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1169         dh_rsa_export=(dh_rsa && (EVP_PKEY_size(cpk->privatekey)*8 <= 512))?1:0;
1170         cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
1171 /* FIX THIS EAY EAY EAY */
1172         dh_dsa=  ((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1173         dh_dsa_export=(dh_dsa && (EVP_PKEY_size(cpk->privatekey)*8 <= 512))?1:0;
1174
1175         mask=0;
1176         emask=0;
1177
1178 #ifdef CIPHER_DEBUG
1179         printf("rt=%d dht=%d re=%d rs=%d ds=%d dhr=%d dhd=%d\n",
1180                 rsa_tmp,dh_tmp,
1181                 rsa_enc,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
1182 #endif
1183
1184         if (rsa_enc || (rsa_tmp && rsa_sign))
1185                 mask|=SSL_kRSA;
1186         if (rsa_enc_export || (rsa_tmp_export && rsa_sign))
1187                 emask|=SSL_kRSA;
1188
1189 #if 0
1190         /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
1191         if (    (dh_tmp || dh_rsa || dh_dsa) && 
1192                 (rsa_enc || rsa_sign || dsa_sign))
1193                 mask|=SSL_kEDH;
1194         if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
1195                 (rsa_enc || rsa_sign || dsa_sign))
1196                 emask|=SSL_kEDH;
1197 #endif
1198
1199         if (dh_tmp_export) 
1200                 emask|=SSL_kEDH;
1201
1202         if (dh_tmp)
1203                 mask|=SSL_kEDH;
1204
1205         if (dh_rsa) mask|=SSL_kDHr;
1206         if (dh_rsa_export) emask|=SSL_kDHr;
1207
1208         if (dh_dsa) mask|=SSL_kDHd;
1209         if (dh_dsa_export) emask|=SSL_kDHd;
1210
1211         if (rsa_enc || rsa_sign)
1212                 {
1213                 mask|=SSL_aRSA;
1214                 emask|=SSL_aRSA;
1215                 }
1216
1217         if (dsa_sign)
1218                 {
1219                 mask|=SSL_aDSS;
1220                 emask|=SSL_aDSS;
1221                 }
1222
1223 #ifdef SSL_ALLOW_ADH
1224         mask|=SSL_aNULL;
1225         emask|=SSL_aNULL;
1226 #endif
1227
1228         c->mask=mask;
1229         c->export_mask=emask;
1230         c->valid=1;
1231         }
1232
1233 /* THIS NEEDS CLEANING UP */
1234 X509 *ssl_get_server_send_cert(s)
1235 SSL *s;
1236         {
1237         unsigned long alg,mask,kalg;
1238         CERT *c;
1239         int i,_export;
1240
1241         c=s->cert;
1242         ssl_set_cert_masks(c);
1243         alg=s->s3->tmp.new_cipher->algorithms;
1244         _export=SSL_IS_EXPORT(alg);
1245         mask=_export?c->export_mask:c->mask;
1246         kalg=alg&(SSL_MKEY_MASK|SSL_AUTH_MASK);
1247
1248         if      (kalg & SSL_kDHr)
1249                 i=SSL_PKEY_DH_RSA;
1250         else if (kalg & SSL_kDHd)
1251                 i=SSL_PKEY_DH_DSA;
1252         else if (kalg & SSL_aDSS)
1253                 i=SSL_PKEY_DSA_SIGN;
1254         else if (kalg & SSL_aRSA)
1255                 {
1256                 if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
1257                         i=SSL_PKEY_RSA_SIGN;
1258                 else
1259                         i=SSL_PKEY_RSA_ENC;
1260                 }
1261         else /* if (kalg & SSL_aNULL) */
1262                 {
1263                 SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,SSL_R_INTERNAL_ERROR);
1264                 return(NULL);
1265                 }
1266         if (c->pkeys[i].x509 == NULL) return(NULL);
1267         return(c->pkeys[i].x509);
1268         }
1269
1270 EVP_PKEY *ssl_get_sign_pkey(s,cipher)
1271 SSL *s;
1272 SSL_CIPHER *cipher;
1273         {
1274         unsigned long alg;
1275         CERT *c;
1276
1277         alg=cipher->algorithms;
1278         c=s->cert;
1279
1280         if ((alg & SSL_aDSS) &&
1281                 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
1282                 return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey);
1283         else if (alg & SSL_aRSA)
1284                 {
1285                 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
1286                         return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey);
1287                 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
1288                         return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey);
1289                 else
1290                         return(NULL);
1291                 }
1292         else /* if (alg & SSL_aNULL) */
1293                 {
1294                 SSLerr(SSL_F_SSL_GET_SIGN_PKEY,SSL_R_INTERNAL_ERROR);
1295                 return(NULL);
1296                 }
1297         }
1298
1299 void ssl_update_cache(s,mode)
1300 SSL *s;
1301 int mode;
1302         {
1303         int i;
1304
1305         /* If the session_id_length is 0, we are not supposed to cache it,
1306          * and it would be rather hard to do anyway :-) */
1307         if (s->session->session_id_length == 0) return;
1308
1309         if ((s->ctx->session_cache_mode & mode)
1310                 && (!s->hit)
1311                 && SSL_CTX_add_session(s->ctx,s->session)
1312                 && (s->ctx->new_session_cb != NULL))
1313                 {
1314                 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
1315                 if (!s->ctx->new_session_cb(s,s->session))
1316                         SSL_SESSION_free(s->session);
1317                 }
1318
1319         /* auto flush every 255 connections */
1320         i=s->ctx->session_cache_mode;
1321         if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
1322                 ((i & mode) == mode))
1323                 {
1324                 if (  (((mode & SSL_SESS_CACHE_CLIENT)
1325                         ?s->ctx->stats.sess_connect_good
1326                         :s->ctx->stats.sess_accept_good) & 0xff) == 0xff)
1327                         {
1328                         SSL_CTX_flush_sessions(s->ctx,time(NULL));
1329                         }
1330                 }
1331         }
1332
1333 SSL_METHOD *SSL_get_ssl_method(s)
1334 SSL *s;
1335         {
1336         return(s->method);
1337         }
1338
1339 int SSL_set_ssl_method(s,meth)
1340 SSL *s;
1341 SSL_METHOD *meth;
1342         {
1343         int conn= -1;
1344         int ret=1;
1345
1346         if (s->method != meth)
1347                 {
1348                 if (s->handshake_func != NULL)
1349                         conn=(s->handshake_func == s->method->ssl_connect);
1350
1351                 if (s->method->version == meth->version)
1352                         s->method=meth;
1353                 else
1354                         {
1355                         s->method->ssl_free(s);
1356                         s->method=meth;
1357                         ret=s->method->ssl_new(s);
1358                         }
1359
1360                 if (conn == 1)
1361                         s->handshake_func=meth->ssl_connect;
1362                 else if (conn == 0)
1363                         s->handshake_func=meth->ssl_accept;
1364                 }
1365         return(ret);
1366         }
1367
1368 int SSL_get_error(s,i)
1369 SSL *s;
1370 int i;
1371         {
1372         int reason;
1373         unsigned long l;
1374         BIO *bio;
1375
1376         if (i > 0) return(SSL_ERROR_NONE);
1377
1378         /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
1379          * etc, where we do encode the error */
1380         if ((l=ERR_peek_error()) != 0)
1381                 {
1382                 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
1383                         return(SSL_ERROR_SYSCALL);
1384                 else
1385                         return(SSL_ERROR_SSL);
1386                 }
1387
1388         if ((i < 0) && SSL_want_read(s))
1389                 {
1390                 bio=SSL_get_rbio(s);
1391                 if (BIO_should_read(bio))
1392                         return(SSL_ERROR_WANT_READ);
1393                 else if (BIO_should_write(bio))
1394                         return(SSL_ERROR_WANT_WRITE);
1395                 else if (BIO_should_io_special(bio))
1396                         {
1397                         reason=BIO_get_retry_reason(bio);
1398                         if (reason == BIO_RR_CONNECT)
1399                                 return(SSL_ERROR_WANT_CONNECT);
1400                         else
1401                                 return(SSL_ERROR_SYSCALL); /* unknown */
1402                         }
1403                 }
1404
1405         if ((i < 0) && SSL_want_write(s))
1406                 {
1407                 bio=SSL_get_wbio(s);
1408                 if (BIO_should_write(bio))
1409                         return(SSL_ERROR_WANT_WRITE);
1410                 else if (BIO_should_read(bio))
1411                         return(SSL_ERROR_WANT_READ);
1412                 else if (BIO_should_io_special(bio))
1413                         {
1414                         reason=BIO_get_retry_reason(bio);
1415                         if (reason == BIO_RR_CONNECT)
1416                                 return(SSL_ERROR_WANT_CONNECT);
1417                         else
1418                                 return(SSL_ERROR_SYSCALL);
1419                         }
1420                 }
1421         if ((i < 0) && SSL_want_x509_lookup(s))
1422                 {
1423                 return(SSL_ERROR_WANT_X509_LOOKUP);
1424                 }
1425
1426         if (i == 0)
1427                 {
1428                 if (s->version == SSL2_VERSION)
1429                         {
1430                         /* assume it is the socket being closed */
1431                         return(SSL_ERROR_ZERO_RETURN);
1432                         }
1433                 else
1434                         {
1435                         if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
1436                                 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
1437                                 return(SSL_ERROR_ZERO_RETURN);
1438                         }
1439                 }
1440         return(SSL_ERROR_SYSCALL);
1441         }
1442
1443 int SSL_do_handshake(s)
1444 SSL *s;
1445         {
1446         int ret=1;
1447
1448         if (s->handshake_func == NULL)
1449                 {
1450                 SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
1451                 return(-1);
1452                 }
1453
1454         s->method->ssl_renegotiate_check(s);
1455
1456         if (SSL_in_init(s) || SSL_in_before(s))
1457                 {
1458                 ret=s->handshake_func(s);
1459                 }
1460         return(ret);
1461         }
1462
1463 /* For the next 2 functions, SSL_clear() sets shutdown and so
1464  * one of these calls will reset it */
1465 void SSL_set_accept_state(s)
1466 SSL *s;
1467         {
1468         s->server=1;
1469         s->shutdown=0;
1470         s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
1471         s->handshake_func=s->method->ssl_accept;
1472         /* clear the current cipher */
1473         ssl_clear_cipher_ctx(s);
1474         }
1475
1476 void SSL_set_connect_state(s)
1477 SSL *s;
1478         {
1479         s->server=0;
1480         s->shutdown=0;
1481         s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
1482         s->handshake_func=s->method->ssl_connect;
1483         /* clear the current cipher */
1484         ssl_clear_cipher_ctx(s);
1485         }
1486
1487 int ssl_undefined_function(s)
1488 SSL *s;
1489         {
1490         SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1491         return(0);
1492         }
1493
1494 SSL_METHOD *ssl_bad_method(ver)
1495 int ver;
1496         {
1497         SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1498         return(NULL);
1499         }
1500
1501 char *SSL_get_version(s)
1502 SSL *s;
1503         {
1504         if (s->version == TLS1_VERSION)
1505                 return("TLSv1");
1506         else if (s->version == SSL3_VERSION)
1507                 return("SSLv3");
1508         else if (s->version == SSL2_VERSION)
1509                 return("SSLv2");
1510         else
1511                 return("unknown");
1512         }
1513
1514 SSL *SSL_dup(s)
1515 SSL *s;
1516         {
1517         STACK *sk;
1518         X509_NAME *xn;
1519         SSL *ret;
1520         int i;
1521                  
1522         if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL) return(NULL);
1523                           
1524         /* This copies version, session-id, SSL_METHOD and 'cert' */
1525         SSL_copy_session_id(ret,s);
1526
1527         SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
1528         SSL_set_verify(ret,SSL_get_verify_mode(s),
1529                 SSL_get_verify_callback(s));
1530
1531         SSL_set_info_callback(ret,SSL_get_info_callback(s));
1532         
1533         ret->debug=s->debug;
1534         ret->options=s->options;
1535
1536         /* copy app data, a little dangerous perhaps */
1537         if (!CRYPTO_dup_ex_data(ssl_meth,&ret->ex_data,&s->ex_data))
1538                 goto err;
1539
1540         /* setup rbio, and wbio */
1541         if (s->rbio != NULL)
1542                 {
1543                 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
1544                         goto err;
1545                 }
1546         if (s->wbio != NULL)
1547                 {
1548                 if (s->wbio != s->rbio)
1549                         {
1550                         if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
1551                                 goto err;
1552                         }
1553                 else
1554                         ret->wbio=ret->rbio;
1555                 }
1556
1557         /* dup the cipher_list and cipher_list_by_id stacks */
1558         if (s->cipher_list != NULL)
1559                 {
1560                 if ((ret->cipher_list=sk_dup(s->cipher_list)) == NULL)
1561                         goto err;
1562                 }
1563         if (s->cipher_list_by_id != NULL)
1564                 if ((ret->cipher_list_by_id=sk_dup(s->cipher_list_by_id))
1565                         == NULL)
1566                         goto err;
1567
1568         /* Dup the client_CA list */
1569         if (s->client_CA != NULL)
1570                 {
1571                 if ((sk=sk_dup(s->client_CA)) == NULL) goto err;
1572                 ret->client_CA=sk;
1573                 for (i=0; i<sk_num(sk); i++)
1574                         {
1575                         xn=(X509_NAME *)sk_value(sk,i);
1576                         if ((sk_value(sk,i)=(char *)X509_NAME_dup(xn)) == NULL)
1577                                 {
1578                                 X509_NAME_free(xn);
1579                                 goto err;
1580                                 }
1581                         }
1582                 }
1583
1584         ret->shutdown=s->shutdown;
1585         ret->state=s->state;
1586         ret->handshake_func=s->handshake_func;
1587         ret->server=s->server;
1588
1589         if (0)
1590                 {
1591 err:
1592                 if (ret != NULL) SSL_free(ret);
1593                 ret=NULL;
1594                 }
1595         return(ret);
1596         }
1597
1598 void ssl_clear_cipher_ctx(s)
1599 SSL *s;
1600         {
1601         if (s->enc_read_ctx != NULL)
1602                 {
1603                 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
1604                 Free(s->enc_read_ctx);
1605                 s->enc_read_ctx=NULL;
1606                 }
1607         if (s->enc_write_ctx != NULL)
1608                 {
1609                 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
1610                 Free(s->enc_write_ctx);
1611                 s->enc_write_ctx=NULL;
1612                 }
1613         if (s->expand != NULL)
1614                 {
1615                 COMP_CTX_free(s->expand);
1616                 s->expand=NULL;
1617                 }
1618         if (s->compress != NULL)
1619                 {
1620                 COMP_CTX_free(s->compress);
1621                 s->compress=NULL;
1622                 }
1623         }
1624
1625 /* Fix this function so that it takes an optional type parameter */
1626 X509 *SSL_get_certificate(s)
1627 SSL *s;
1628         {
1629         if (s->cert != NULL)
1630                 return(s->cert->key->x509);
1631         else
1632                 return(NULL);
1633         }
1634
1635 /* Fix this function so that it takes an optional type parameter */
1636 EVP_PKEY *SSL_get_privatekey(s)
1637 SSL *s;
1638         {
1639         if (s->cert != NULL)
1640                 return(s->cert->key->privatekey);
1641         else
1642                 return(NULL);
1643         }
1644
1645 SSL_CIPHER *SSL_get_current_cipher(s)
1646 SSL *s;
1647         {
1648         if ((s->session != NULL) && (s->session->cipher != NULL))
1649                 return(s->session->cipher);
1650         return(NULL);
1651         }
1652
1653 int ssl_init_wbio_buffer(s,push)
1654 SSL *s;
1655 int push;
1656         {
1657         BIO *bbio;
1658
1659         if (s->bbio == NULL)
1660                 {
1661                 bbio=BIO_new(BIO_f_buffer());
1662                 if (bbio == NULL) return(0);
1663                 s->bbio=bbio;
1664                 }
1665         else
1666                 {
1667                 bbio=s->bbio;
1668                 if (s->bbio == s->wbio)
1669                         s->wbio=BIO_pop(s->wbio);
1670                 }
1671         BIO_reset(bbio);
1672 /*      if (!BIO_set_write_buffer_size(bbio,16*1024)) */
1673         if (!BIO_set_read_buffer_size(bbio,1))
1674                 {
1675                 SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
1676                 return(0);
1677                 }
1678         if (push)
1679                 {
1680                 if (s->wbio != bbio)
1681                         s->wbio=BIO_push(bbio,s->wbio);
1682                 }
1683         else
1684                 {
1685                 if (s->wbio == bbio)
1686                         s->wbio=BIO_pop(bbio);
1687                 }
1688         return(1);
1689         }
1690
1691 void ssl_free_wbio_buffer(s)
1692 SSL *s;
1693         {
1694         BIO *under;
1695
1696         if (s->bbio == NULL) return;
1697
1698         if (s->bbio == s->wbio)
1699                 {
1700                 /* remove buffering */
1701                 under=BIO_pop(s->wbio);
1702                 if (under != NULL)
1703                         s->wbio=under;
1704                 else
1705                         abort(); /* ok */
1706                 }
1707         BIO_free(s->bbio);
1708         s->bbio=NULL;
1709         }
1710         
1711 void SSL_CTX_set_quiet_shutdown(ctx,mode)
1712 SSL_CTX *ctx;
1713 int mode;
1714         {
1715         ctx->quiet_shutdown=mode;
1716         }
1717
1718 int SSL_CTX_get_quiet_shutdown(ctx)
1719 SSL_CTX *ctx;
1720         {
1721         return(ctx->quiet_shutdown);
1722         }
1723
1724 void SSL_set_quiet_shutdown(s,mode)
1725 SSL *s;
1726 int mode;
1727         {
1728         s->quiet_shutdown=mode;
1729         }
1730
1731 int SSL_get_quiet_shutdown(s)
1732 SSL *s;
1733         {
1734         return(s->quiet_shutdown);
1735         }
1736
1737 void SSL_set_shutdown(s,mode)
1738 SSL *s;
1739 int mode;
1740         {
1741         s->shutdown=mode;
1742         }
1743
1744 int SSL_get_shutdown(s)
1745 SSL *s;
1746         {
1747         return(s->shutdown);
1748         }
1749
1750 int SSL_version(s)
1751 SSL *s;
1752         {
1753         return(s->version);
1754         }
1755
1756 SSL_CTX *SSL_get_SSL_CTX(ssl)
1757 SSL *ssl;
1758         {
1759         return(ssl->ctx);
1760         }
1761
1762 #ifndef NO_STDIO
1763 int SSL_CTX_set_default_verify_paths(ctx)
1764 SSL_CTX *ctx;
1765         {
1766         return(X509_STORE_set_default_paths(ctx->cert_store));
1767         }
1768
1769 int SSL_CTX_load_verify_locations(ctx,CAfile,CApath)
1770 SSL_CTX *ctx;
1771 char *CAfile;
1772 char *CApath;
1773         {
1774         return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
1775         }
1776 #endif
1777
1778 void SSL_set_info_callback(ssl,cb)
1779 SSL *ssl;
1780 void (*cb)();
1781         {
1782         ssl->info_callback=cb;
1783         }
1784
1785 void (*SSL_get_info_callback(ssl))()
1786 SSL *ssl;
1787         {
1788         return((void (*)())ssl->info_callback);
1789         }
1790
1791 int SSL_state(ssl)
1792 SSL *ssl;
1793         {
1794         return(ssl->state);
1795         }
1796
1797 void SSL_set_verify_result(ssl,arg)
1798 SSL *ssl;
1799 long arg;
1800         {
1801         ssl->verify_result=arg;
1802         }
1803
1804 long SSL_get_verify_result(ssl)
1805 SSL *ssl;
1806         {
1807         return(ssl->verify_result);
1808         }
1809
1810 int SSL_get_ex_new_index(argl,argp,new_func,dup_func,free_func)
1811 long argl;
1812 char *argp;
1813 int (*new_func)();
1814 int (*dup_func)();
1815 void (*free_func)();
1816         {
1817         ssl_meth_num++;
1818         return(CRYPTO_get_ex_new_index(ssl_meth_num-1,
1819                 &ssl_meth,argl,argp,new_func,dup_func,free_func));
1820         }
1821
1822 int SSL_set_ex_data(s,idx,arg)
1823 SSL *s;
1824 int idx;
1825 void *arg;
1826         {
1827         return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
1828         }
1829
1830 void *SSL_get_ex_data(s,idx)
1831 SSL *s;
1832 int idx;
1833         {
1834         return(CRYPTO_get_ex_data(&s->ex_data,idx));
1835         }
1836
1837 int SSL_CTX_get_ex_new_index(argl,argp,new_func,dup_func,free_func)
1838 long argl;
1839 char *argp;
1840 int (*new_func)();
1841 int (*dup_func)();
1842 void (*free_func)();
1843         {
1844         ssl_ctx_meth_num++;
1845         return(CRYPTO_get_ex_new_index(ssl_ctx_meth_num-1,
1846                 &ssl_ctx_meth,argl,argp,new_func,dup_func,free_func));
1847         }
1848
1849 int SSL_CTX_set_ex_data(s,idx,arg)
1850 SSL_CTX *s;
1851 int idx;
1852 void *arg;
1853         {
1854         return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
1855         }
1856
1857 void *SSL_CTX_get_ex_data(s,idx)
1858 SSL_CTX *s;
1859 int idx;
1860         {
1861         return(CRYPTO_get_ex_data(&s->ex_data,idx));
1862         }
1863
1864 int ssl_ok(s)
1865 SSL *s;
1866         {
1867         return(1);
1868         }
1869
1870 X509_STORE *SSL_CTX_get_cert_store(ctx)
1871 SSL_CTX *ctx;
1872         {
1873         return(ctx->cert_store);
1874         }
1875
1876 void SSL_CTX_set_cert_store(ctx,store)
1877 SSL_CTX *ctx;
1878 X509_STORE *store;
1879         {
1880         if (ctx->cert_store != NULL)
1881                 X509_STORE_free(ctx->cert_store);
1882         ctx->cert_store=store;
1883         }
1884
1885 int SSL_want(s)
1886 SSL *s;
1887         {
1888         return(s->rwstate);
1889         }
1890
1891 void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,int export))
1892     { SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,0,(char *)cb); }
1893
1894 void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int export))
1895     { SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,0,(char *)dh); }
1896
1897 #if defined(_WINDLL) && defined(WIN16)
1898 #include "../crypto/bio/bss_file.c"
1899 #endif
1900