sl@0: /* crypto/evp/bio_ber.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 "cryptlib.h" sl@0: #include sl@0: #include sl@0: sl@0: static int ber_write(BIO *h,char *buf,int num); sl@0: static int ber_read(BIO *h,char *buf,int size); sl@0: /*static int ber_puts(BIO *h,char *str); */ sl@0: /*static int ber_gets(BIO *h,char *str,int size); */ sl@0: static long ber_ctrl(BIO *h,int cmd,long arg1,char *arg2); sl@0: static int ber_new(BIO *h); sl@0: static int ber_free(BIO *data); sl@0: static long ber_callback_ctrl(BIO *h,int cmd,void *(*fp)()); sl@0: #define BER_BUF_SIZE (32) sl@0: sl@0: /* This is used to hold the state of the BER objects being read. */ sl@0: typedef struct ber_struct sl@0: { sl@0: int tag; sl@0: int class; sl@0: long length; sl@0: int inf; sl@0: int num_left; sl@0: int depth; sl@0: } BER_CTX; sl@0: sl@0: typedef struct bio_ber_struct sl@0: { sl@0: int tag; sl@0: int class; sl@0: long length; sl@0: int inf; sl@0: sl@0: /* most of the following are used when doing non-blocking IO */ sl@0: /* reading */ sl@0: long num_left; /* number of bytes still to read/write in block */ sl@0: int depth; /* used with indefinite encoding. */ sl@0: int finished; /* No more read data */ sl@0: sl@0: /* writting */ sl@0: char *w_addr; sl@0: int w_offset; sl@0: int w_left; sl@0: sl@0: int buf_len; sl@0: int buf_off; sl@0: unsigned char buf[BER_BUF_SIZE]; sl@0: } BIO_BER_CTX; sl@0: sl@0: static BIO_METHOD methods_ber= sl@0: { sl@0: BIO_TYPE_CIPHER,"cipher", sl@0: ber_write, sl@0: ber_read, sl@0: NULL, /* ber_puts, */ sl@0: NULL, /* ber_gets, */ sl@0: ber_ctrl, sl@0: ber_new, sl@0: ber_free, sl@0: ber_callback_ctrl, sl@0: }; sl@0: sl@0: BIO_METHOD *BIO_f_ber(void) sl@0: { sl@0: return(&methods_ber); sl@0: } sl@0: sl@0: static int ber_new(BIO *bi) sl@0: { sl@0: BIO_BER_CTX *ctx; sl@0: sl@0: ctx=(BIO_BER_CTX *)OPENSSL_malloc(sizeof(BIO_BER_CTX)); sl@0: if (ctx == NULL) return(0); sl@0: sl@0: memset((char *)ctx,0,sizeof(BIO_BER_CTX)); sl@0: sl@0: bi->init=0; sl@0: bi->ptr=(char *)ctx; sl@0: bi->flags=0; sl@0: return(1); sl@0: } sl@0: sl@0: static int ber_free(BIO *a) sl@0: { sl@0: BIO_BER_CTX *b; sl@0: sl@0: if (a == NULL) return(0); sl@0: b=(BIO_BER_CTX *)a->ptr; sl@0: OPENSSL_cleanse(a->ptr,sizeof(BIO_BER_CTX)); sl@0: OPENSSL_free(a->ptr); sl@0: a->ptr=NULL; sl@0: a->init=0; sl@0: a->flags=0; sl@0: return(1); sl@0: } sl@0: sl@0: int bio_ber_get_header(BIO *bio, BIO_BER_CTX *ctx) sl@0: { sl@0: char buf[64]; sl@0: int i,j,n; sl@0: int ret; sl@0: unsigned char *p; sl@0: unsigned long length sl@0: int tag; sl@0: int class; sl@0: long max; sl@0: sl@0: BIO_clear_retry_flags(b); sl@0: sl@0: /* Pack the buffer down if there is a hole at the front */ sl@0: if (ctx->buf_off != 0) sl@0: { sl@0: p=ctx->buf; sl@0: j=ctx->buf_off; sl@0: n=ctx->buf_len-j; sl@0: for (i=0; ibuf_len-j; sl@0: ctx->buf_off=0; sl@0: } sl@0: sl@0: /* If there is more room, read some more data */ sl@0: i=BER_BUF_SIZE-ctx->buf_len; sl@0: if (i) sl@0: { sl@0: i=BIO_read(bio->next_bio,&(ctx->buf[ctx->buf_len]),i); sl@0: if (i <= 0) sl@0: { sl@0: BIO_copy_next_retry(b); sl@0: return(i); sl@0: } sl@0: else sl@0: ctx->buf_len+=i; sl@0: } sl@0: sl@0: max=ctx->buf_len; sl@0: p=ctx->buf; sl@0: ret=ASN1_get_object(&p,&length,&tag,&class,max); sl@0: sl@0: if (ret & 0x80) sl@0: { sl@0: if ((ctx->buf_len < BER_BUF_SIZE) && sl@0: (ERR_GET_REASON(ERR_peek_error()) == ASN1_R_TOO_LONG)) sl@0: { sl@0: ERR_clear_error(); /* clear the error */ sl@0: BIO_set_retry_read(b); sl@0: } sl@0: return(-1); sl@0: } sl@0: sl@0: /* We have no error, we have a header, so make use of it */ sl@0: sl@0: if ((ctx->tag >= 0) && (ctx->tag != tag)) sl@0: { sl@0: BIOerr(BIO_F_BIO_BER_GET_HEADER,BIO_R_TAG_MISMATCH); sl@0: sprintf(buf,"tag=%d, got %d",ctx->tag,tag); sl@0: ERR_add_error_data(1,buf); sl@0: return(-1); sl@0: } sl@0: if (ret & 0x01) sl@0: if (ret & V_ASN1_CONSTRUCTED) sl@0: } sl@0: sl@0: static int ber_read(BIO *b, char *out, int outl) sl@0: { sl@0: int ret=0,i,n; sl@0: BIO_BER_CTX *ctx; sl@0: sl@0: BIO_clear_retry_flags(b); sl@0: sl@0: if (out == NULL) return(0); sl@0: ctx=(BIO_BER_CTX *)b->ptr; sl@0: sl@0: if ((ctx == NULL) || (b->next_bio == NULL)) return(0); sl@0: sl@0: if (ctx->finished) return(0); sl@0: sl@0: again: sl@0: /* First see if we are half way through reading a block */ sl@0: if (ctx->num_left > 0) sl@0: { sl@0: if (ctx->num_left < outl) sl@0: n=ctx->num_left; sl@0: else sl@0: n=outl; sl@0: i=BIO_read(b->next_bio,out,n); sl@0: if (i <= 0) sl@0: { sl@0: BIO_copy_next_retry(b); sl@0: return(i); sl@0: } sl@0: ctx->num_left-=i; sl@0: outl-=i; sl@0: ret+=i; sl@0: if (ctx->num_left <= 0) sl@0: { sl@0: ctx->depth--; sl@0: if (ctx->depth <= 0) sl@0: ctx->finished=1; sl@0: } sl@0: if (outl <= 0) sl@0: return(ret); sl@0: else sl@0: goto again; sl@0: } sl@0: else /* we need to read another BER header */ sl@0: { sl@0: } sl@0: } sl@0: sl@0: static int ber_write(BIO *b, char *in, int inl) sl@0: { sl@0: int ret=0,n,i; sl@0: BIO_ENC_CTX *ctx; sl@0: sl@0: ctx=(BIO_ENC_CTX *)b->ptr; sl@0: ret=inl; sl@0: sl@0: BIO_clear_retry_flags(b); sl@0: n=ctx->buf_len-ctx->buf_off; sl@0: while (n > 0) sl@0: { sl@0: i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); sl@0: if (i <= 0) sl@0: { sl@0: BIO_copy_next_retry(b); sl@0: return(i); sl@0: } sl@0: ctx->buf_off+=i; sl@0: n-=i; sl@0: } sl@0: /* at this point all pending data has been written */ sl@0: sl@0: if ((in == NULL) || (inl <= 0)) return(0); sl@0: sl@0: ctx->buf_off=0; sl@0: while (inl > 0) sl@0: { sl@0: n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl; sl@0: EVP_CipherUpdate(&(ctx->cipher), sl@0: (unsigned char *)ctx->buf,&ctx->buf_len, sl@0: (unsigned char *)in,n); sl@0: inl-=n; sl@0: in+=n; sl@0: sl@0: ctx->buf_off=0; sl@0: n=ctx->buf_len; sl@0: while (n > 0) sl@0: { sl@0: i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); sl@0: if (i <= 0) sl@0: { sl@0: BIO_copy_next_retry(b); sl@0: return(i); sl@0: } sl@0: n-=i; sl@0: ctx->buf_off+=i; sl@0: } sl@0: ctx->buf_len=0; sl@0: ctx->buf_off=0; sl@0: } sl@0: BIO_copy_next_retry(b); sl@0: return(ret); sl@0: } sl@0: sl@0: static long ber_ctrl(BIO *b, int cmd, long num, char *ptr) sl@0: { sl@0: BIO *dbio; sl@0: BIO_ENC_CTX *ctx,*dctx; sl@0: long ret=1; sl@0: int i; sl@0: sl@0: ctx=(BIO_ENC_CTX *)b->ptr; sl@0: sl@0: switch (cmd) sl@0: { sl@0: case BIO_CTRL_RESET: sl@0: ctx->ok=1; sl@0: ctx->finished=0; sl@0: EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL, sl@0: ctx->cipher.berrypt); sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: case BIO_CTRL_EOF: /* More to read */ sl@0: if (ctx->cont <= 0) sl@0: ret=1; sl@0: else sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: case BIO_CTRL_WPENDING: sl@0: ret=ctx->buf_len-ctx->buf_off; sl@0: if (ret <= 0) sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: case BIO_CTRL_PENDING: /* More to read in buffer */ sl@0: ret=ctx->buf_len-ctx->buf_off; sl@0: if (ret <= 0) sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: case BIO_CTRL_FLUSH: sl@0: /* do a final write */ sl@0: again: sl@0: while (ctx->buf_len != ctx->buf_off) sl@0: { sl@0: i=ber_write(b,NULL,0); sl@0: if (i < 0) sl@0: { sl@0: ret=i; sl@0: break; sl@0: } sl@0: } sl@0: sl@0: if (!ctx->finished) sl@0: { sl@0: ctx->finished=1; sl@0: ctx->buf_off=0; sl@0: ret=EVP_CipherFinal_ex(&(ctx->cipher), sl@0: (unsigned char *)ctx->buf, sl@0: &(ctx->buf_len)); sl@0: ctx->ok=(int)ret; sl@0: if (ret <= 0) break; sl@0: sl@0: /* push out the bytes */ sl@0: goto again; sl@0: } sl@0: sl@0: /* Finally flush the underlying BIO */ sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: case BIO_C_GET_CIPHER_STATUS: sl@0: ret=(long)ctx->ok; sl@0: break; sl@0: case BIO_C_DO_STATE_MACHINE: sl@0: BIO_clear_retry_flags(b); sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: BIO_copy_next_retry(b); sl@0: break; sl@0: sl@0: case BIO_CTRL_DUP: sl@0: dbio=(BIO *)ptr; sl@0: dctx=(BIO_ENC_CTX *)dbio->ptr; sl@0: memcpy(&(dctx->cipher),&(ctx->cipher),sizeof(ctx->cipher)); sl@0: dbio->init=1; sl@0: break; sl@0: default: sl@0: ret=BIO_ctrl(b->next_bio,cmd,num,ptr); sl@0: break; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: static long ber_callback_ctrl(BIO *b, int cmd, void *(*fp)()) sl@0: { sl@0: long ret=1; sl@0: sl@0: if (b->next_bio == NULL) return(0); sl@0: switch (cmd) sl@0: { sl@0: default: sl@0: ret=BIO_callback_ctrl(b->next_bio,cmd,fp); sl@0: break; sl@0: } sl@0: return(ret); sl@0: } sl@0: sl@0: /* sl@0: void BIO_set_cipher_ctx(b,c) sl@0: BIO *b; sl@0: EVP_CIPHER_ctx *c; sl@0: { sl@0: if (b == NULL) return; sl@0: sl@0: if ((b->callback != NULL) && sl@0: (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0)) sl@0: return; sl@0: sl@0: b->init=1; sl@0: ctx=(BIO_ENC_CTX *)b->ptr; sl@0: memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX)); sl@0: sl@0: if (b->callback != NULL) sl@0: b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L); sl@0: } sl@0: */ sl@0: sl@0: void BIO_set_cipher(BIO *b, EVP_CIPHER *c, unsigned char *k, unsigned char *i, sl@0: int e) sl@0: { sl@0: BIO_ENC_CTX *ctx; sl@0: sl@0: if (b == NULL) return; sl@0: sl@0: if ((b->callback != NULL) && sl@0: (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0)) sl@0: return; sl@0: sl@0: b->init=1; sl@0: ctx=(BIO_ENC_CTX *)b->ptr; sl@0: EVP_CipherInit_ex(&(ctx->cipher),c,NULL,k,i,e); sl@0: sl@0: if (b->callback != NULL) sl@0: b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L); sl@0: } sl@0: