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