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