sl@0: /* crypto/bio/bio_lib.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: sl@0: EXPORT_C BIO *BIO_new(BIO_METHOD *method) sl@0: { sl@0: BIO *ret=NULL; sl@0: sl@0: ret=(BIO *)OPENSSL_malloc(sizeof(BIO)); sl@0: if (ret == NULL) sl@0: { sl@0: BIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE); sl@0: return(NULL); sl@0: } sl@0: #ifdef SYMBIAN sl@0: memset(ret,0,sizeof(BIO)); sl@0: #endif sl@0: if (!BIO_set(ret,method)) sl@0: { sl@0: OPENSSL_free(ret); sl@0: ret=NULL; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C int BIO_set(BIO *bio, BIO_METHOD *method) sl@0: { sl@0: bio->method=method; sl@0: bio->callback=NULL; sl@0: bio->cb_arg=NULL; sl@0: bio->init=0; sl@0: bio->shutdown=1; sl@0: bio->flags=0; sl@0: bio->retry_reason=0; sl@0: bio->num=0; sl@0: bio->ptr=NULL; sl@0: bio->prev_bio=NULL; sl@0: bio->next_bio=NULL; sl@0: bio->references=1; sl@0: bio->num_read=0L; sl@0: bio->num_write=0L; sl@0: CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); sl@0: if (method->create != NULL) sl@0: if (!method->create(bio)) sl@0: { sl@0: CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, sl@0: &bio->ex_data); sl@0: return(0); sl@0: } sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C int BIO_free(BIO *a) sl@0: { sl@0: int ret=0,i; sl@0: sl@0: if (a == NULL) return(0); sl@0: sl@0: i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_BIO); sl@0: #ifdef REF_PRINT sl@0: REF_PRINT("BIO",a); sl@0: #endif sl@0: if (i > 0) return(1); sl@0: #ifdef REF_CHECK sl@0: if (i < 0) sl@0: { sl@0: fprintf(stderr,"BIO_free, bad reference count\n"); sl@0: abort(); sl@0: } sl@0: #endif sl@0: if ((a->callback != NULL) && sl@0: ((i=(int)a->callback(a,BIO_CB_FREE,NULL,0,0L,1L)) <= 0)) sl@0: return(i); sl@0: sl@0: CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data); sl@0: sl@0: if ((a->method == NULL) || (a->method->destroy == NULL)) return(1); sl@0: ret=a->method->destroy(a); sl@0: OPENSSL_free(a); sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C void BIO_vfree(BIO *a) sl@0: { BIO_free(a); } sl@0: sl@0: EXPORT_C void BIO_clear_flags(BIO *b, int flags) sl@0: { sl@0: b->flags &= ~flags; sl@0: } sl@0: sl@0: EXPORT_C int BIO_test_flags(const BIO *b, int flags) sl@0: { sl@0: return (b->flags & flags); sl@0: } sl@0: sl@0: EXPORT_C void BIO_set_flags(BIO *b, int flags) sl@0: { sl@0: b->flags |= flags; sl@0: } sl@0: sl@0: EXPORT_C long (*BIO_get_callback(const BIO *b))(struct bio_st *,int,const char *,int, long,long) sl@0: { sl@0: return b->callback; sl@0: } sl@0: sl@0: EXPORT_C void BIO_set_callback(BIO *b, long (*cb)(struct bio_st *,int,const char *,int, long,long)) sl@0: { sl@0: b->callback = cb; sl@0: } sl@0: sl@0: EXPORT_C void BIO_set_callback_arg(BIO *b, char *arg) sl@0: { sl@0: b->cb_arg = arg; sl@0: } sl@0: sl@0: EXPORT_C char * BIO_get_callback_arg(const BIO *b) sl@0: { sl@0: return b->cb_arg; sl@0: } sl@0: sl@0: EXPORT_C const char * BIO_method_name(const BIO *b) sl@0: { sl@0: return b->method->name; sl@0: } sl@0: sl@0: EXPORT_C int BIO_method_type(const BIO *b) sl@0: { sl@0: return b->method->type; sl@0: } sl@0: EXPORT_C int BIO_read(BIO *b, void *out, int outl) sl@0: { sl@0: int i; sl@0: long (*cb)(BIO *,int,const char *,int,long,long); sl@0: sl@0: if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) sl@0: { sl@0: BIOerr(BIO_F_BIO_READ,BIO_R_UNSUPPORTED_METHOD); sl@0: return(-2); sl@0: } sl@0: sl@0: cb=b->callback; sl@0: if ((cb != NULL) && sl@0: ((i=(int)cb(b,BIO_CB_READ,out,outl,0L,1L)) <= 0)) sl@0: return(i); sl@0: sl@0: if (!b->init) sl@0: { sl@0: BIOerr(BIO_F_BIO_READ,BIO_R_UNINITIALIZED); sl@0: return(-2); sl@0: } sl@0: sl@0: i=b->method->bread(b,out,outl); sl@0: sl@0: if (i > 0) b->num_read+=(unsigned long)i; sl@0: sl@0: if (cb != NULL) sl@0: i=(int)cb(b,BIO_CB_READ|BIO_CB_RETURN,out,outl, sl@0: 0L,(long)i); sl@0: return(i); sl@0: } sl@0: sl@0: EXPORT_C int BIO_write(BIO *b, const void *in, int inl) sl@0: { sl@0: int i; sl@0: long (*cb)(BIO *,int,const char *,int,long,long); sl@0: sl@0: if (b == NULL) sl@0: return(0); sl@0: sl@0: cb=b->callback; sl@0: if ((b->method == NULL) || (b->method->bwrite == NULL)) sl@0: { sl@0: BIOerr(BIO_F_BIO_WRITE,BIO_R_UNSUPPORTED_METHOD); sl@0: return(-2); sl@0: } sl@0: sl@0: if ((cb != NULL) && sl@0: ((i=(int)cb(b,BIO_CB_WRITE,in,inl,0L,1L)) <= 0)) sl@0: return(i); sl@0: sl@0: if (!b->init) sl@0: { sl@0: BIOerr(BIO_F_BIO_WRITE,BIO_R_UNINITIALIZED); sl@0: return(-2); sl@0: } sl@0: sl@0: i=b->method->bwrite(b,in,inl); sl@0: sl@0: if (i > 0) b->num_write+=(unsigned long)i; sl@0: sl@0: if (cb != NULL) sl@0: i=(int)cb(b,BIO_CB_WRITE|BIO_CB_RETURN,in,inl, sl@0: 0L,(long)i); sl@0: return(i); sl@0: } sl@0: sl@0: EXPORT_C int BIO_puts(BIO *b, const char *in) sl@0: { sl@0: int i; sl@0: long (*cb)(BIO *,int,const char *,int,long,long); sl@0: sl@0: if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) sl@0: { sl@0: BIOerr(BIO_F_BIO_PUTS,BIO_R_UNSUPPORTED_METHOD); sl@0: return(-2); sl@0: } sl@0: sl@0: cb=b->callback; sl@0: sl@0: if ((cb != NULL) && sl@0: ((i=(int)cb(b,BIO_CB_PUTS,in,0,0L,1L)) <= 0)) sl@0: return(i); sl@0: sl@0: if (!b->init) sl@0: { sl@0: BIOerr(BIO_F_BIO_PUTS,BIO_R_UNINITIALIZED); sl@0: return(-2); sl@0: } sl@0: sl@0: i=b->method->bputs(b,in); sl@0: sl@0: if (i > 0) b->num_write+=(unsigned long)i; sl@0: sl@0: if (cb != NULL) sl@0: i=(int)cb(b,BIO_CB_PUTS|BIO_CB_RETURN,in,0, sl@0: 0L,(long)i); sl@0: return(i); sl@0: } sl@0: sl@0: EXPORT_C int BIO_gets(BIO *b, char *in, int inl) sl@0: { sl@0: int i; sl@0: long (*cb)(BIO *,int,const char *,int,long,long); sl@0: sl@0: if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) sl@0: { sl@0: BIOerr(BIO_F_BIO_GETS,BIO_R_UNSUPPORTED_METHOD); sl@0: return(-2); sl@0: } sl@0: sl@0: cb=b->callback; sl@0: sl@0: if ((cb != NULL) && sl@0: ((i=(int)cb(b,BIO_CB_GETS,in,inl,0L,1L)) <= 0)) sl@0: return(i); sl@0: sl@0: if (!b->init) sl@0: { sl@0: BIOerr(BIO_F_BIO_GETS,BIO_R_UNINITIALIZED); sl@0: return(-2); sl@0: } sl@0: sl@0: i=b->method->bgets(b,in,inl); sl@0: sl@0: if (cb != NULL) sl@0: i=(int)cb(b,BIO_CB_GETS|BIO_CB_RETURN,in,inl, sl@0: 0L,(long)i); sl@0: return(i); sl@0: } sl@0: sl@0: EXPORT_C int BIO_indent(BIO *b,int indent,int max) sl@0: { sl@0: if(indent < 0) sl@0: indent=0; sl@0: if(indent > max) sl@0: indent=max; sl@0: while(indent--) sl@0: if(BIO_puts(b," ") != 1) sl@0: return 0; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) sl@0: { sl@0: int i; sl@0: sl@0: i=iarg; sl@0: return(BIO_ctrl(b,cmd,larg,(char *)&i)); sl@0: } sl@0: sl@0: EXPORT_C char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) sl@0: { sl@0: char *p=NULL; sl@0: sl@0: if (BIO_ctrl(b,cmd,larg,(char *)&p) <= 0) sl@0: return(NULL); sl@0: else sl@0: return(p); sl@0: } sl@0: sl@0: EXPORT_C long BIO_ctrl(BIO *b, int cmd, long larg, void *parg) sl@0: { sl@0: long ret; sl@0: long (*cb)(BIO *,int,const char *,int,long,long); sl@0: sl@0: if (b == NULL) return(0); sl@0: sl@0: if ((b->method == NULL) || (b->method->ctrl == NULL)) sl@0: { sl@0: BIOerr(BIO_F_BIO_CTRL,BIO_R_UNSUPPORTED_METHOD); sl@0: return(-2); sl@0: } sl@0: sl@0: cb=b->callback; sl@0: sl@0: if ((cb != NULL) && sl@0: ((ret=cb(b,BIO_CB_CTRL,parg,cmd,larg,1L)) <= 0)) sl@0: return(ret); sl@0: sl@0: ret=b->method->ctrl(b,cmd,larg,parg); sl@0: sl@0: if (cb != NULL) sl@0: ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,parg,cmd, sl@0: larg,ret); sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C long BIO_callback_ctrl(BIO *b, int cmd, void (*fp)(struct bio_st *, int, const char *, int, long, long)) sl@0: { sl@0: long ret; sl@0: long (*cb)(BIO *,int,const char *,int,long,long); sl@0: sl@0: if (b == NULL) return(0); sl@0: sl@0: if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) sl@0: { sl@0: BIOerr(BIO_F_BIO_CALLBACK_CTRL,BIO_R_UNSUPPORTED_METHOD); sl@0: return(-2); sl@0: } sl@0: sl@0: cb=b->callback; sl@0: sl@0: if ((cb != NULL) && sl@0: ((ret=cb(b,BIO_CB_CTRL,(void *)&fp,cmd,0,1L)) <= 0)) sl@0: return(ret); sl@0: sl@0: ret=b->method->callback_ctrl(b,cmd,fp); sl@0: sl@0: if (cb != NULL) sl@0: ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,(void *)&fp,cmd, sl@0: 0,ret); sl@0: return(ret); sl@0: } sl@0: sl@0: /* It is unfortunate to duplicate in functions what the BIO_(w)pending macros sl@0: * do; but those macros have inappropriate return type, and for interfacing sl@0: * from other programming languages, C macros aren't much of a help anyway. */ sl@0: EXPORT_C size_t BIO_ctrl_pending(BIO *bio) sl@0: { sl@0: return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL); sl@0: } sl@0: sl@0: EXPORT_C size_t BIO_ctrl_wpending(BIO *bio) sl@0: { sl@0: return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL); sl@0: } sl@0: sl@0: sl@0: /* put the 'bio' on the end of b's list of operators */ sl@0: EXPORT_C BIO *BIO_push(BIO *b, BIO *bio) sl@0: { sl@0: BIO *lb; sl@0: sl@0: if (b == NULL) return(bio); sl@0: lb=b; sl@0: while (lb->next_bio != NULL) sl@0: lb=lb->next_bio; sl@0: lb->next_bio=bio; sl@0: if (bio != NULL) sl@0: bio->prev_bio=lb; sl@0: /* called to do internal processing */ sl@0: BIO_ctrl(b,BIO_CTRL_PUSH,0,NULL); sl@0: return(b); sl@0: } sl@0: sl@0: /* Remove the first and return the rest */ sl@0: EXPORT_C BIO *BIO_pop(BIO *b) sl@0: { sl@0: BIO *ret; sl@0: sl@0: if (b == NULL) return(NULL); sl@0: ret=b->next_bio; sl@0: sl@0: BIO_ctrl(b,BIO_CTRL_POP,0,NULL); sl@0: sl@0: if (b->prev_bio != NULL) sl@0: b->prev_bio->next_bio=b->next_bio; sl@0: if (b->next_bio != NULL) sl@0: b->next_bio->prev_bio=b->prev_bio; sl@0: sl@0: b->next_bio=NULL; sl@0: b->prev_bio=NULL; sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C BIO *BIO_get_retry_BIO(BIO *bio, int *reason) sl@0: { sl@0: BIO *b,*last; sl@0: sl@0: b=last=bio; sl@0: for (;;) sl@0: { sl@0: if (!BIO_should_retry(b)) break; sl@0: last=b; sl@0: b=b->next_bio; sl@0: if (b == NULL) break; sl@0: } sl@0: if (reason != NULL) *reason=last->retry_reason; sl@0: return(last); sl@0: } sl@0: sl@0: EXPORT_C int BIO_get_retry_reason(BIO *bio) sl@0: { sl@0: return(bio->retry_reason); sl@0: } sl@0: sl@0: EXPORT_C BIO *BIO_find_type(BIO *bio, int type) sl@0: { sl@0: int mt,mask; sl@0: sl@0: if(!bio) return NULL; sl@0: mask=type&0xff; sl@0: do { sl@0: if (bio->method != NULL) sl@0: { sl@0: mt=bio->method->type; sl@0: sl@0: if (!mask) sl@0: { sl@0: if (mt & type) return(bio); sl@0: } sl@0: else if (mt == type) sl@0: return(bio); sl@0: } sl@0: bio=bio->next_bio; sl@0: } while (bio != NULL); sl@0: return(NULL); sl@0: } sl@0: sl@0: EXPORT_C BIO *BIO_next(BIO *b) sl@0: { sl@0: if(!b) return NULL; sl@0: return b->next_bio; sl@0: } sl@0: sl@0: EXPORT_C void BIO_free_all(BIO *bio) sl@0: { sl@0: BIO *b; sl@0: int ref; sl@0: sl@0: while (bio != NULL) sl@0: { sl@0: b=bio; sl@0: ref=b->references; sl@0: bio=bio->next_bio; sl@0: BIO_free(b); sl@0: /* Since ref count > 1, don't free anyone else. */ sl@0: if (ref > 1) break; sl@0: } sl@0: } sl@0: sl@0: EXPORT_C BIO *BIO_dup_chain(BIO *in) sl@0: { sl@0: BIO *ret=NULL,*eoc=NULL,*bio,*new; sl@0: sl@0: for (bio=in; bio != NULL; bio=bio->next_bio) sl@0: { sl@0: if ((new=BIO_new(bio->method)) == NULL) goto err; sl@0: new->callback=bio->callback; sl@0: new->cb_arg=bio->cb_arg; sl@0: new->init=bio->init; sl@0: new->shutdown=bio->shutdown; sl@0: new->flags=bio->flags; sl@0: sl@0: /* This will let SSL_s_sock() work with stdin/stdout */ sl@0: new->num=bio->num; sl@0: sl@0: if (!BIO_dup_state(bio,(char *)new)) sl@0: { sl@0: BIO_free(new); sl@0: goto err; sl@0: } sl@0: sl@0: /* copy app data */ sl@0: if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new->ex_data, sl@0: &bio->ex_data)) sl@0: goto err; sl@0: sl@0: if (ret == NULL) sl@0: { sl@0: eoc=new; sl@0: ret=eoc; sl@0: } sl@0: else sl@0: { sl@0: BIO_push(eoc,new); sl@0: eoc=new; sl@0: } sl@0: } sl@0: return(ret); sl@0: err: sl@0: if (ret != NULL) sl@0: BIO_free(ret); sl@0: return(NULL); sl@0: } sl@0: sl@0: EXPORT_C void BIO_copy_next_retry(BIO *b) sl@0: { sl@0: BIO_set_flags(b,BIO_get_retry_flags(b->next_bio)); sl@0: b->retry_reason=b->next_bio->retry_reason; sl@0: } sl@0: sl@0: EXPORT_C int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, sl@0: CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) sl@0: { sl@0: return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO, argl, argp, sl@0: new_func, dup_func, free_func); sl@0: } sl@0: sl@0: EXPORT_C int BIO_set_ex_data(BIO *bio, int idx, void *data) sl@0: { sl@0: return(CRYPTO_set_ex_data(&(bio->ex_data),idx,data)); sl@0: } sl@0: sl@0: EXPORT_C void *BIO_get_ex_data(BIO *bio, int idx) sl@0: { sl@0: return(CRYPTO_get_ex_data(&(bio->ex_data),idx)); sl@0: } sl@0: sl@0: EXPORT_C unsigned long BIO_number_read(BIO *bio) sl@0: { sl@0: if(bio) return bio->num_read; sl@0: return 0; sl@0: } sl@0: sl@0: EXPORT_C unsigned long BIO_number_written(BIO *bio) sl@0: { sl@0: if(bio) return bio->num_write; sl@0: return 0; sl@0: } sl@0: sl@0: IMPLEMENT_STACK_OF(BIO)