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