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