2bc0e3f2b2498de8c069c122a55566f2fac33573
[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 int (*cb)();
1047         {
1048         ctx->default_verify_mode=mode;
1049         ctx->default_verify_callback=cb;
1050         /* This needs cleaning up EAY EAY EAY */
1051         X509_STORE_set_verify_cb_func(ctx->cert_store,cb);
1052         }
1053
1054 void ssl_set_cert_masks(c)
1055 CERT *c;
1056         {
1057         CERT_PKEY *cpk;
1058         int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
1059         int rsa_enc_export,dh_rsa_export,dh_dsa_export;
1060         int rsa_tmp_export,dh_tmp_export;
1061         unsigned long mask,emask;
1062
1063         if ((c == NULL) || (c->valid)) return;
1064
1065 #ifndef NO_RSA
1066         rsa_tmp=((c->rsa_tmp != NULL) || (c->rsa_tmp_cb != NULL))?1:0;
1067         rsa_tmp_export=((c->rsa_tmp_cb != NULL) ||
1068                 (rsa_tmp && (RSA_size(c->rsa_tmp)*8 <= 512)))?1:0;
1069 #else
1070         rsa_tmp=rsa_tmp_export=0;
1071 #endif
1072 #ifndef NO_DH
1073         dh_tmp=((c->dh_tmp != NULL) || (c->dh_tmp_cb != NULL))?1:0;
1074         dh_tmp_export=((c->dh_tmp_cb != NULL) ||
1075                 (dh_tmp && (DH_size(c->dh_tmp)*8 <= 512)))?1:0;
1076 #else
1077         dh_tmp=dh_tmp_export=0;
1078 #endif
1079
1080         cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
1081         rsa_enc= ((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1082         rsa_enc_export=(rsa_enc && (EVP_PKEY_size(cpk->privatekey)*8 <= 512))?1:0;
1083         cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
1084         rsa_sign=((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1085         cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
1086         dsa_sign=((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1087         cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
1088         dh_rsa=  ((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1089         dh_rsa_export=(dh_rsa && (EVP_PKEY_size(cpk->privatekey)*8 <= 512))?1:0;
1090         cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
1091 /* FIX THIS EAY EAY EAY */
1092         dh_dsa=  ((cpk->x509 != NULL) && (cpk->privatekey != NULL))?1:0;
1093         dh_dsa_export=(dh_dsa && (EVP_PKEY_size(cpk->privatekey)*8 <= 512))?1:0;
1094
1095         mask=0;
1096         emask=0;
1097
1098 #ifdef CIPHER_DEBUG
1099         printf("rt=%d dht=%d re=%d rs=%d ds=%d dhr=%d dhd=%d\n",
1100                 rsa_tmp,dh_tmp,
1101                 rsa_enc,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
1102 #endif
1103
1104         if (rsa_enc || (rsa_tmp && rsa_sign))
1105                 mask|=SSL_kRSA;
1106         if (rsa_enc_export || (rsa_tmp_export && rsa_sign))
1107                 emask|=SSL_kRSA;
1108
1109 #if 0
1110         /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
1111         if (    (dh_tmp || dh_rsa || dh_dsa) && 
1112                 (rsa_enc || rsa_sign || dsa_sign))
1113                 mask|=SSL_kEDH;
1114         if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
1115                 (rsa_enc || rsa_sign || dsa_sign))
1116                 emask|=SSL_kEDH;
1117 #endif
1118
1119         if (dh_tmp_export) 
1120                 emask|=SSL_kEDH;
1121
1122         if (dh_tmp)
1123                 mask|=SSL_kEDH;
1124
1125         if (dh_rsa) mask|=SSL_kDHr;
1126         if (dh_rsa_export) emask|=SSL_kDHr;
1127
1128         if (dh_dsa) mask|=SSL_kDHd;
1129         if (dh_dsa_export) emask|=SSL_kDHd;
1130
1131         if (rsa_enc || rsa_sign)
1132                 {
1133                 mask|=SSL_aRSA;
1134                 emask|=SSL_aRSA;
1135                 }
1136
1137         if (dsa_sign)
1138                 {
1139                 mask|=SSL_aDSS;
1140                 emask|=SSL_aDSS;
1141                 }
1142
1143 #ifdef SSL_ALLOW_ADH
1144         mask|=SSL_aNULL;
1145         emask|=SSL_aNULL;
1146 #endif
1147
1148         c->mask=mask;
1149         c->export_mask=emask;
1150         c->valid=1;
1151         }
1152
1153 /* THIS NEEDS CLEANING UP */
1154 X509 *ssl_get_server_send_cert(s)
1155 SSL *s;
1156         {
1157         unsigned long alg,mask,kalg;
1158         CERT *c;
1159         int i,export;
1160
1161         c=s->cert;
1162         ssl_set_cert_masks(c);
1163         alg=s->s3->tmp.new_cipher->algorithms;
1164         export=(alg & SSL_EXPORT)?1:0;
1165         mask=(export)?c->export_mask:c->mask;
1166         kalg=alg&(SSL_MKEY_MASK|SSL_AUTH_MASK);
1167
1168         if      (kalg & SSL_kDHr)
1169                 i=SSL_PKEY_DH_RSA;
1170         else if (kalg & SSL_kDHd)
1171                 i=SSL_PKEY_DH_DSA;
1172         else if (kalg & SSL_aDSS)
1173                 i=SSL_PKEY_DSA_SIGN;
1174         else if (kalg & SSL_aRSA)
1175                 {
1176                 if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
1177                         i=SSL_PKEY_RSA_SIGN;
1178                 else
1179                         i=SSL_PKEY_RSA_ENC;
1180                 }
1181         else /* if (kalg & SSL_aNULL) */
1182                 {
1183                 SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,SSL_R_INTERNAL_ERROR);
1184                 return(NULL);
1185                 }
1186         if (c->pkeys[i].x509 == NULL) return(NULL);
1187         return(c->pkeys[i].x509);
1188         }
1189
1190 EVP_PKEY *ssl_get_sign_pkey(s,cipher)
1191 SSL *s;
1192 SSL_CIPHER *cipher;
1193         {
1194         unsigned long alg;
1195         CERT *c;
1196
1197         alg=cipher->algorithms;
1198         c=s->cert;
1199
1200         if ((alg & SSL_aDSS) &&
1201                 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
1202                 return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey);
1203         else if (alg & SSL_aRSA)
1204                 {
1205                 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
1206                         return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey);
1207                 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
1208                         return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey);
1209                 else
1210                         return(NULL);
1211                 }
1212         else /* if (alg & SSL_aNULL) */
1213                 {
1214                 SSLerr(SSL_F_SSL_GET_SIGN_PKEY,SSL_R_INTERNAL_ERROR);
1215                 return(NULL);
1216                 }
1217         }
1218
1219 void ssl_update_cache(s,mode)
1220 SSL *s;
1221 int mode;
1222         {
1223         int i;
1224
1225         /* If the session_id_length is 0, we are not supposed to cache it,
1226          * and it would be rather hard to do anyway :-) */
1227         if (s->session->session_id_length == 0) return;
1228
1229         if ((s->ctx->session_cache_mode & mode)
1230                 && (!s->hit)
1231                 && SSL_CTX_add_session(s->ctx,s->session)
1232                 && (s->ctx->new_session_cb != NULL))
1233                 {
1234                 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
1235                 if (!s->ctx->new_session_cb(s,s->session))
1236                         SSL_SESSION_free(s->session);
1237                 }
1238
1239         /* auto flush every 255 connections */
1240         i=s->ctx->session_cache_mode;
1241         if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
1242                 ((i & mode) == mode))
1243                 {
1244                 if (  (((mode & SSL_SESS_CACHE_CLIENT)
1245                         ?s->ctx->sess_connect_good
1246                         :s->ctx->sess_accept_good) & 0xff) == 0xff)
1247                         {
1248                         SSL_CTX_flush_sessions(s->ctx,time(NULL));
1249                         }
1250                 }
1251         }
1252
1253 SSL_METHOD *SSL_get_ssl_method(s)
1254 SSL *s;
1255         {
1256         return(s->method);
1257         }
1258
1259 int SSL_set_ssl_method(s,meth)
1260 SSL *s;
1261 SSL_METHOD *meth;
1262         {
1263         int conn= -1;
1264         int ret=1;
1265
1266         if (s->method != meth)
1267                 {
1268                 if (s->handshake_func != NULL)
1269                         conn=(s->handshake_func == s->method->ssl_connect);
1270
1271                 if (s->method->version == meth->version)
1272                         s->method=meth;
1273                 else
1274                         {
1275                         s->method->ssl_free(s);
1276                         s->method=meth;
1277                         ret=s->method->ssl_new(s);
1278                         }
1279
1280                 if (conn == 1)
1281                         s->handshake_func=meth->ssl_connect;
1282                 else if (conn == 0)
1283                         s->handshake_func=meth->ssl_accept;
1284                 }
1285         return(ret);
1286         }
1287
1288 int SSL_get_error(s,i)
1289 SSL *s;
1290 int i;
1291         {
1292         int reason;
1293         BIO *bio;
1294
1295         if (i > 0) return(SSL_ERROR_NONE);
1296
1297         if (ERR_peek_error() != 0)
1298                 return(SSL_ERROR_SSL);
1299
1300         if ((i < 0) && SSL_want_read(s))
1301                 {
1302                 bio=SSL_get_rbio(s);
1303                 if (BIO_should_read(bio))
1304                         return(SSL_ERROR_WANT_READ);
1305                 else if (BIO_should_write(bio))
1306                         return(SSL_ERROR_WANT_WRITE);
1307                 else if (BIO_should_io_special(bio))
1308                         {
1309                         reason=BIO_get_retry_reason(bio);
1310                         if (reason == BIO_RR_CONNECT)
1311                                 return(SSL_ERROR_WANT_CONNECT);
1312                         else
1313                                 return(SSL_ERROR_SYSCALL); /* unknown */
1314                         }
1315                 }
1316
1317         if ((i < 0) && SSL_want_write(s))
1318                 {
1319                 bio=SSL_get_wbio(s);
1320                 if (BIO_should_write(bio))
1321                         return(SSL_ERROR_WANT_WRITE);
1322                 else if (BIO_should_read(bio))
1323                         return(SSL_ERROR_WANT_READ);
1324                 else if (BIO_should_io_special(bio))
1325                         {
1326                         reason=BIO_get_retry_reason(bio);
1327                         if (reason == BIO_RR_CONNECT)
1328                                 return(SSL_ERROR_WANT_CONNECT);
1329                         else
1330                                 return(SSL_ERROR_SYSCALL);
1331                         }
1332                 }
1333         if ((i < 0) && SSL_want_x509_lookup(s))
1334                 {
1335                 return(SSL_ERROR_WANT_X509_LOOKUP);
1336                 }
1337
1338         if (i == 0)
1339                 {
1340                 if (s->version == SSL2_VERSION)
1341                         {
1342                         /* assume it is the socket being closed */
1343                         return(SSL_ERROR_ZERO_RETURN);
1344                         }
1345                 else
1346                         {
1347                         if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
1348                                 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
1349                                 return(SSL_ERROR_ZERO_RETURN);
1350                         }
1351                 }
1352         return(SSL_ERROR_SYSCALL);
1353         }
1354
1355 int SSL_do_handshake(s)
1356 SSL *s;
1357         {
1358         int ret=1;
1359
1360         if (s->handshake_func == NULL)
1361                 {
1362                 SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
1363                 return(-1);
1364                 }
1365
1366         s->method->ssl_renegotiate_check(s);
1367
1368         if (SSL_in_init(s) || SSL_in_before(s))
1369                 {
1370                 ret=s->handshake_func(s);
1371                 }
1372         return(ret);
1373         }
1374
1375 /* For the next 2 functions, SSL_clear() sets shutdown and so
1376  * one of these calls will reset it */
1377 void SSL_set_accept_state(s)
1378 SSL *s;
1379         {
1380         s->shutdown=0;
1381         s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
1382         s->handshake_func=s->method->ssl_accept;
1383         /* clear the current cipher */
1384         ssl_clear_cipher_ctx(s);
1385         }
1386
1387 void SSL_set_connect_state(s)
1388 SSL *s;
1389         {
1390         s->shutdown=0;
1391         s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
1392         s->handshake_func=s->method->ssl_connect;
1393         /* clear the current cipher */
1394         ssl_clear_cipher_ctx(s);
1395         }
1396
1397 int ssl_undefined_function(s)
1398 SSL *s;
1399         {
1400         SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1401         return(0);
1402         }
1403
1404 SSL_METHOD *ssl_bad_method(ver)
1405 int ver;
1406         {
1407         SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1408         return(NULL);
1409         }
1410
1411 char *SSL_get_version(s)
1412 SSL *s;
1413         {
1414         if (s->version == TLS1_VERSION)
1415                 return("TLSv1");
1416         else if (s->version == SSL3_VERSION)
1417                 return("SSLv3");
1418         else if (s->version == SSL2_VERSION)
1419                 return("SSLv2");
1420         else
1421                 return("unknown");
1422         }
1423
1424 SSL *SSL_dup(s)
1425 SSL *s;
1426         {
1427         STACK *sk;
1428         X509_NAME *xn;
1429         SSL *ret;
1430         int i;
1431                  
1432         if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL) return(NULL);
1433                           
1434         /* This copies version, session-id, SSL_METHOD and 'cert' */
1435         SSL_copy_session_id(ret,s);
1436
1437         SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
1438         SSL_set_verify(ret,SSL_get_verify_mode(s),
1439                 SSL_get_verify_callback(s));
1440
1441         SSL_set_info_callback(ret,SSL_get_info_callback(s));
1442         
1443         ret->debug=s->debug;
1444         ret->options=s->options;
1445
1446         /* copy app data, a little dangerous perhaps */
1447         if (!CRYPTO_dup_ex_data(ssl_meth,&ret->ex_data,&s->ex_data))
1448                 goto err;
1449
1450         /* setup rbio, and wbio */
1451         if (s->rbio != NULL)
1452                 {
1453                 if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
1454                         goto err;
1455                 }
1456         if (s->wbio != NULL)
1457                 {
1458                 if (s->wbio != s->rbio)
1459                         {
1460                         if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
1461                                 goto err;
1462                         }
1463                 else
1464                         ret->wbio=ret->rbio;
1465                 }
1466
1467         /* dup the cipher_list and cipher_list_by_id stacks */
1468         if (s->cipher_list != NULL)
1469                 {
1470                 if ((ret->cipher_list=sk_dup(s->cipher_list)) == NULL)
1471                         goto err;
1472                 }
1473         if (s->cipher_list_by_id != NULL)
1474                 if ((ret->cipher_list_by_id=sk_dup(s->cipher_list_by_id))
1475                         == NULL)
1476                         goto err;
1477
1478         /* Dup the client_CA list */
1479         if (s->client_CA != NULL)
1480                 {
1481                 if ((sk=sk_dup(s->client_CA)) == NULL) goto err;
1482                 ret->client_CA=sk;
1483                 for (i=0; i<sk_num(sk); i++)
1484                         {
1485                         xn=(X509_NAME *)sk_value(sk,i);
1486                         if ((sk_value(sk,i)=(char *)X509_NAME_dup(xn)) == NULL)
1487                                 {
1488                                 X509_NAME_free(xn);
1489                                 goto err;
1490                                 }
1491                         }
1492                 }
1493
1494         ret->shutdown=s->shutdown;
1495         ret->state=s->state;
1496         ret->handshake_func=s->handshake_func;
1497
1498         if (0)
1499                 {
1500 err:
1501                 if (ret != NULL) SSL_free(ret);
1502                 ret=NULL;
1503                 }
1504         return(ret);
1505         }
1506
1507 void ssl_clear_cipher_ctx(s)
1508 SSL *s;
1509         {
1510         if (s->enc_read_ctx != NULL)
1511                 {
1512                 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
1513                 Free(s->enc_read_ctx);
1514                 s->enc_read_ctx=NULL;
1515                 }
1516         if (s->enc_write_ctx != NULL)
1517                 {
1518                 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
1519                 Free(s->enc_write_ctx);
1520                 s->enc_write_ctx=NULL;
1521                 }
1522         }
1523
1524 /* Fix this function so that it takes an optional type parameter */
1525 X509 *SSL_get_certificate(s)
1526 SSL *s;
1527         {
1528         if (s->cert != NULL)
1529                 return(s->cert->key->x509);
1530         else
1531                 return(NULL);
1532         }
1533
1534 /* Fix this function so that it takes an optional type parameter */
1535 EVP_PKEY *SSL_get_privatekey(s)
1536 SSL *s;
1537         {
1538         if (s->cert != NULL)
1539                 return(s->cert->key->privatekey);
1540         else
1541                 return(NULL);
1542         }
1543
1544 SSL_CIPHER *SSL_get_current_cipher(s)
1545 SSL *s;
1546         {
1547         if ((s->session != NULL) && (s->session->cipher != NULL))
1548                 return(s->session->cipher);
1549         return(NULL);
1550         }
1551
1552 int ssl_init_wbio_buffer(s,push)
1553 SSL *s;
1554 int push;
1555         {
1556         BIO *bbio;
1557
1558         if (s->bbio == NULL)
1559                 {
1560                 bbio=BIO_new(BIO_f_buffer());
1561                 if (bbio == NULL) return(0);
1562                 s->bbio=bbio;
1563                 }
1564         else
1565                 {
1566                 bbio=s->bbio;
1567                 if (s->bbio == s->wbio)
1568                         s->wbio=BIO_pop(s->wbio);
1569                 }
1570         BIO_reset(bbio);
1571 /*      if (!BIO_set_write_buffer_size(bbio,16*1024)) */
1572         if (!BIO_set_read_buffer_size(bbio,1))
1573                 {
1574                 SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
1575                 return(0);
1576                 }
1577         if (push)
1578                 {
1579                 if (s->wbio != bbio)
1580                         s->wbio=BIO_push(bbio,s->wbio);
1581                 }
1582         else
1583                 {
1584                 if (s->wbio == bbio)
1585                         s->wbio=BIO_pop(bbio);
1586                 }
1587         return(1);
1588         }
1589         
1590 void SSL_CTX_set_quiet_shutdown(ctx,mode)
1591 SSL_CTX *ctx;
1592 int mode;
1593         {
1594         ctx->quiet_shutdown=mode;
1595         }
1596
1597 int SSL_CTX_get_quiet_shutdown(ctx)
1598 SSL_CTX *ctx;
1599         {
1600         return(ctx->quiet_shutdown);
1601         }
1602
1603 void SSL_set_quiet_shutdown(s,mode)
1604 SSL *s;
1605 int mode;
1606         {
1607         s->quiet_shutdown=mode;
1608         }
1609
1610 int SSL_get_quiet_shutdown(s)
1611 SSL *s;
1612         {
1613         return(s->quiet_shutdown);
1614         }
1615
1616 void SSL_set_shutdown(s,mode)
1617 SSL *s;
1618 int mode;
1619         {
1620         s->shutdown=mode;
1621         }
1622
1623 int SSL_get_shutdown(s)
1624 SSL *s;
1625         {
1626         return(s->shutdown);
1627         }
1628
1629 int SSL_version(s)
1630 SSL *s;
1631         {
1632         return(s->version);
1633         }
1634
1635 SSL_CTX *SSL_get_SSL_CTX(ssl)
1636 SSL *ssl;
1637         {
1638         return(ssl->ctx);
1639         }
1640
1641 #ifndef NO_STDIO
1642 int SSL_CTX_set_default_verify_paths(ctx)
1643 SSL_CTX *ctx;
1644         {
1645         return(X509_STORE_set_default_paths(ctx->cert_store));
1646         }
1647
1648 int SSL_CTX_load_verify_locations(ctx,CAfile,CApath)
1649 SSL_CTX *ctx;
1650 char *CAfile;
1651 char *CApath;
1652         {
1653         return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
1654         }
1655 #endif
1656
1657 void SSL_set_info_callback(ssl,cb)
1658 SSL *ssl;
1659 void (*cb)();
1660         {
1661         ssl->info_callback=cb;
1662         }
1663
1664 void (*SSL_get_info_callback(ssl))()
1665 SSL *ssl;
1666         {
1667         return((void (*)())ssl->info_callback);
1668         }
1669
1670 int SSL_state(ssl)
1671 SSL *ssl;
1672         {
1673         return(ssl->state);
1674         }
1675
1676 void SSL_set_verify_result(ssl,arg)
1677 SSL *ssl;
1678 long arg;
1679         {
1680         ssl->verify_result=arg;
1681         }
1682
1683 long SSL_get_verify_result(ssl)
1684 SSL *ssl;
1685         {
1686         return(ssl->verify_result);
1687         }
1688
1689 int SSL_get_ex_new_index(argl,argp,new_func,dup_func,free_func)
1690 long argl;
1691 char *argp;
1692 int (*new_func)();
1693 int (*dup_func)();
1694 void (*free_func)();
1695         {
1696         ssl_meth_num++;
1697         return(CRYPTO_get_ex_new_index(ssl_meth_num-1,
1698                 &ssl_meth,argl,argp,new_func,dup_func,free_func));
1699         }
1700
1701 int SSL_set_ex_data(s,idx,arg)
1702 SSL *s;
1703 int idx;
1704 char *arg;
1705         {
1706         return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
1707         }
1708
1709 char *SSL_get_ex_data(s,idx)
1710 SSL *s;
1711 int idx;
1712         {
1713         return(CRYPTO_get_ex_data(&s->ex_data,idx));
1714         }
1715
1716 int SSL_CTX_get_ex_new_index(argl,argp,new_func,dup_func,free_func)
1717 long argl;
1718 char *argp;
1719 int (*new_func)();
1720 int (*dup_func)();
1721 void (*free_func)();
1722         {
1723         ssl_ctx_meth_num++;
1724         return(CRYPTO_get_ex_new_index(ssl_ctx_meth_num-1,
1725                 &ssl_ctx_meth,argl,argp,new_func,dup_func,free_func));
1726         }
1727
1728 int SSL_CTX_set_ex_data(s,idx,arg)
1729 SSL_CTX *s;
1730 int idx;
1731 char *arg;
1732         {
1733         return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
1734         }
1735
1736 char *SSL_CTX_get_ex_data(s,idx)
1737 SSL_CTX *s;
1738 int idx;
1739         {
1740         return(CRYPTO_get_ex_data(&s->ex_data,idx));
1741         }
1742
1743 int ssl_ok(s)
1744 SSL *s;
1745         {
1746         return(1);
1747         }
1748
1749 void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,int export))
1750     { SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,0,(char *)cb); }
1751
1752 void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int export))
1753     { SSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,0,(char *)dh); }
1754
1755 #if defined(_WINDLL) && defined(WIN16)
1756 #include "../crypto/bio/bss_file.c"
1757 #endif
1758