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