10Sstevel@tonic-gate /* crypto/evp/bio_ber.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <errno.h>
610Sstevel@tonic-gate #include "cryptlib.h"
620Sstevel@tonic-gate #include <openssl/buffer.h>
630Sstevel@tonic-gate #include <openssl/evp.h>
640Sstevel@tonic-gate
650Sstevel@tonic-gate static int ber_write(BIO *h,char *buf,int num);
660Sstevel@tonic-gate static int ber_read(BIO *h,char *buf,int size);
670Sstevel@tonic-gate /*static int ber_puts(BIO *h,char *str); */
680Sstevel@tonic-gate /*static int ber_gets(BIO *h,char *str,int size); */
690Sstevel@tonic-gate static long ber_ctrl(BIO *h,int cmd,long arg1,char *arg2);
700Sstevel@tonic-gate static int ber_new(BIO *h);
710Sstevel@tonic-gate static int ber_free(BIO *data);
720Sstevel@tonic-gate static long ber_callback_ctrl(BIO *h,int cmd,void *(*fp)());
730Sstevel@tonic-gate #define BER_BUF_SIZE (32)
740Sstevel@tonic-gate
750Sstevel@tonic-gate /* This is used to hold the state of the BER objects being read. */
760Sstevel@tonic-gate typedef struct ber_struct
770Sstevel@tonic-gate {
780Sstevel@tonic-gate int tag;
790Sstevel@tonic-gate int class;
800Sstevel@tonic-gate long length;
810Sstevel@tonic-gate int inf;
820Sstevel@tonic-gate int num_left;
830Sstevel@tonic-gate int depth;
840Sstevel@tonic-gate } BER_CTX;
850Sstevel@tonic-gate
860Sstevel@tonic-gate typedef struct bio_ber_struct
870Sstevel@tonic-gate {
880Sstevel@tonic-gate int tag;
890Sstevel@tonic-gate int class;
900Sstevel@tonic-gate long length;
910Sstevel@tonic-gate int inf;
920Sstevel@tonic-gate
930Sstevel@tonic-gate /* most of the following are used when doing non-blocking IO */
940Sstevel@tonic-gate /* reading */
950Sstevel@tonic-gate long num_left; /* number of bytes still to read/write in block */
960Sstevel@tonic-gate int depth; /* used with indefinite encoding. */
970Sstevel@tonic-gate int finished; /* No more read data */
980Sstevel@tonic-gate
990Sstevel@tonic-gate /* writting */
1000Sstevel@tonic-gate char *w_addr;
1010Sstevel@tonic-gate int w_offset;
1020Sstevel@tonic-gate int w_left;
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate int buf_len;
1050Sstevel@tonic-gate int buf_off;
1060Sstevel@tonic-gate unsigned char buf[BER_BUF_SIZE];
1070Sstevel@tonic-gate } BIO_BER_CTX;
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate static BIO_METHOD methods_ber=
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate BIO_TYPE_CIPHER,"cipher",
1120Sstevel@tonic-gate ber_write,
1130Sstevel@tonic-gate ber_read,
1140Sstevel@tonic-gate NULL, /* ber_puts, */
1150Sstevel@tonic-gate NULL, /* ber_gets, */
1160Sstevel@tonic-gate ber_ctrl,
1170Sstevel@tonic-gate ber_new,
1180Sstevel@tonic-gate ber_free,
1190Sstevel@tonic-gate ber_callback_ctrl,
1200Sstevel@tonic-gate };
1210Sstevel@tonic-gate
BIO_f_ber(void)1220Sstevel@tonic-gate BIO_METHOD *BIO_f_ber(void)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate return(&methods_ber);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate
ber_new(BIO * bi)1270Sstevel@tonic-gate static int ber_new(BIO *bi)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate BIO_BER_CTX *ctx;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate ctx=(BIO_BER_CTX *)OPENSSL_malloc(sizeof(BIO_BER_CTX));
1320Sstevel@tonic-gate if (ctx == NULL) return(0);
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate memset((char *)ctx,0,sizeof(BIO_BER_CTX));
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate bi->init=0;
1370Sstevel@tonic-gate bi->ptr=(char *)ctx;
1380Sstevel@tonic-gate bi->flags=0;
1390Sstevel@tonic-gate return(1);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate
ber_free(BIO * a)1420Sstevel@tonic-gate static int ber_free(BIO *a)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate BIO_BER_CTX *b;
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate if (a == NULL) return(0);
1470Sstevel@tonic-gate b=(BIO_BER_CTX *)a->ptr;
1480Sstevel@tonic-gate OPENSSL_cleanse(a->ptr,sizeof(BIO_BER_CTX));
1490Sstevel@tonic-gate OPENSSL_free(a->ptr);
1500Sstevel@tonic-gate a->ptr=NULL;
1510Sstevel@tonic-gate a->init=0;
1520Sstevel@tonic-gate a->flags=0;
1530Sstevel@tonic-gate return(1);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate
bio_ber_get_header(BIO * bio,BIO_BER_CTX * ctx)1560Sstevel@tonic-gate int bio_ber_get_header(BIO *bio, BIO_BER_CTX *ctx)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate char buf[64];
1590Sstevel@tonic-gate int i,j,n;
1600Sstevel@tonic-gate int ret;
1610Sstevel@tonic-gate unsigned char *p;
1620Sstevel@tonic-gate unsigned long length
1630Sstevel@tonic-gate int tag;
1640Sstevel@tonic-gate int class;
1650Sstevel@tonic-gate long max;
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate BIO_clear_retry_flags(b);
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /* Pack the buffer down if there is a hole at the front */
1700Sstevel@tonic-gate if (ctx->buf_off != 0)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate p=ctx->buf;
1730Sstevel@tonic-gate j=ctx->buf_off;
1740Sstevel@tonic-gate n=ctx->buf_len-j;
1750Sstevel@tonic-gate for (i=0; i<n; i++)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate p[0]=p[j];
1780Sstevel@tonic-gate p++;
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate ctx->buf_len-j;
1810Sstevel@tonic-gate ctx->buf_off=0;
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate /* If there is more room, read some more data */
1850Sstevel@tonic-gate i=BER_BUF_SIZE-ctx->buf_len;
1860Sstevel@tonic-gate if (i)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate i=BIO_read(bio->next_bio,&(ctx->buf[ctx->buf_len]),i);
1890Sstevel@tonic-gate if (i <= 0)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate BIO_copy_next_retry(b);
1920Sstevel@tonic-gate return(i);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate else
1950Sstevel@tonic-gate ctx->buf_len+=i;
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate max=ctx->buf_len;
1990Sstevel@tonic-gate p=ctx->buf;
2000Sstevel@tonic-gate ret=ASN1_get_object(&p,&length,&tag,&class,max);
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate if (ret & 0x80)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate if ((ctx->buf_len < BER_BUF_SIZE) &&
2050Sstevel@tonic-gate (ERR_GET_REASON(ERR_peek_error()) == ASN1_R_TOO_LONG))
2060Sstevel@tonic-gate {
207*2139Sjp161948 ERR_clear_error(); /* clear the error */
2080Sstevel@tonic-gate BIO_set_retry_read(b);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate return(-1);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate /* We have no error, we have a header, so make use of it */
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate if ((ctx->tag >= 0) && (ctx->tag != tag))
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate BIOerr(BIO_F_BIO_BER_GET_HEADER,BIO_R_TAG_MISMATCH);
2180Sstevel@tonic-gate sprintf(buf,"tag=%d, got %d",ctx->tag,tag);
2190Sstevel@tonic-gate ERR_add_error_data(1,buf);
2200Sstevel@tonic-gate return(-1);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate if (ret & 0x01)
2230Sstevel@tonic-gate if (ret & V_ASN1_CONSTRUCTED)
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
ber_read(BIO * b,char * out,int outl)2260Sstevel@tonic-gate static int ber_read(BIO *b, char *out, int outl)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate int ret=0,i,n;
2290Sstevel@tonic-gate BIO_BER_CTX *ctx;
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate BIO_clear_retry_flags(b);
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate if (out == NULL) return(0);
2340Sstevel@tonic-gate ctx=(BIO_BER_CTX *)b->ptr;
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate if (ctx->finished) return(0);
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate again:
2410Sstevel@tonic-gate /* First see if we are half way through reading a block */
2420Sstevel@tonic-gate if (ctx->num_left > 0)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate if (ctx->num_left < outl)
2450Sstevel@tonic-gate n=ctx->num_left;
2460Sstevel@tonic-gate else
2470Sstevel@tonic-gate n=outl;
2480Sstevel@tonic-gate i=BIO_read(b->next_bio,out,n);
2490Sstevel@tonic-gate if (i <= 0)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate BIO_copy_next_retry(b);
2520Sstevel@tonic-gate return(i);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate ctx->num_left-=i;
2550Sstevel@tonic-gate outl-=i;
2560Sstevel@tonic-gate ret+=i;
2570Sstevel@tonic-gate if (ctx->num_left <= 0)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate ctx->depth--;
2600Sstevel@tonic-gate if (ctx->depth <= 0)
2610Sstevel@tonic-gate ctx->finished=1;
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate if (outl <= 0)
2640Sstevel@tonic-gate return(ret);
2650Sstevel@tonic-gate else
2660Sstevel@tonic-gate goto again;
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate else /* we need to read another BER header */
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
ber_write(BIO * b,char * in,int inl)2730Sstevel@tonic-gate static int ber_write(BIO *b, char *in, int inl)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate int ret=0,n,i;
2760Sstevel@tonic-gate BIO_ENC_CTX *ctx;
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate ctx=(BIO_ENC_CTX *)b->ptr;
2790Sstevel@tonic-gate ret=inl;
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate BIO_clear_retry_flags(b);
2820Sstevel@tonic-gate n=ctx->buf_len-ctx->buf_off;
2830Sstevel@tonic-gate while (n > 0)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
2860Sstevel@tonic-gate if (i <= 0)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate BIO_copy_next_retry(b);
2890Sstevel@tonic-gate return(i);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate ctx->buf_off+=i;
2920Sstevel@tonic-gate n-=i;
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate /* at this point all pending data has been written */
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate if ((in == NULL) || (inl <= 0)) return(0);
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate ctx->buf_off=0;
2990Sstevel@tonic-gate while (inl > 0)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
3020Sstevel@tonic-gate EVP_CipherUpdate(&(ctx->cipher),
3030Sstevel@tonic-gate (unsigned char *)ctx->buf,&ctx->buf_len,
3040Sstevel@tonic-gate (unsigned char *)in,n);
3050Sstevel@tonic-gate inl-=n;
3060Sstevel@tonic-gate in+=n;
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate ctx->buf_off=0;
3090Sstevel@tonic-gate n=ctx->buf_len;
3100Sstevel@tonic-gate while (n > 0)
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
3130Sstevel@tonic-gate if (i <= 0)
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate BIO_copy_next_retry(b);
3160Sstevel@tonic-gate return(i);
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate n-=i;
3190Sstevel@tonic-gate ctx->buf_off+=i;
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate ctx->buf_len=0;
3220Sstevel@tonic-gate ctx->buf_off=0;
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate BIO_copy_next_retry(b);
3250Sstevel@tonic-gate return(ret);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate
ber_ctrl(BIO * b,int cmd,long num,char * ptr)3280Sstevel@tonic-gate static long ber_ctrl(BIO *b, int cmd, long num, char *ptr)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate BIO *dbio;
3310Sstevel@tonic-gate BIO_ENC_CTX *ctx,*dctx;
3320Sstevel@tonic-gate long ret=1;
3330Sstevel@tonic-gate int i;
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate ctx=(BIO_ENC_CTX *)b->ptr;
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate switch (cmd)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate case BIO_CTRL_RESET:
3400Sstevel@tonic-gate ctx->ok=1;
3410Sstevel@tonic-gate ctx->finished=0;
3420Sstevel@tonic-gate EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL,
3430Sstevel@tonic-gate ctx->cipher.berrypt);
3440Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3450Sstevel@tonic-gate break;
3460Sstevel@tonic-gate case BIO_CTRL_EOF: /* More to read */
3470Sstevel@tonic-gate if (ctx->cont <= 0)
3480Sstevel@tonic-gate ret=1;
3490Sstevel@tonic-gate else
3500Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3510Sstevel@tonic-gate break;
3520Sstevel@tonic-gate case BIO_CTRL_WPENDING:
3530Sstevel@tonic-gate ret=ctx->buf_len-ctx->buf_off;
3540Sstevel@tonic-gate if (ret <= 0)
3550Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3560Sstevel@tonic-gate break;
3570Sstevel@tonic-gate case BIO_CTRL_PENDING: /* More to read in buffer */
3580Sstevel@tonic-gate ret=ctx->buf_len-ctx->buf_off;
3590Sstevel@tonic-gate if (ret <= 0)
3600Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3610Sstevel@tonic-gate break;
3620Sstevel@tonic-gate case BIO_CTRL_FLUSH:
3630Sstevel@tonic-gate /* do a final write */
3640Sstevel@tonic-gate again:
3650Sstevel@tonic-gate while (ctx->buf_len != ctx->buf_off)
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate i=ber_write(b,NULL,0);
3680Sstevel@tonic-gate if (i < 0)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate ret=i;
3710Sstevel@tonic-gate break;
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate if (!ctx->finished)
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate ctx->finished=1;
3780Sstevel@tonic-gate ctx->buf_off=0;
3790Sstevel@tonic-gate ret=EVP_CipherFinal_ex(&(ctx->cipher),
3800Sstevel@tonic-gate (unsigned char *)ctx->buf,
3810Sstevel@tonic-gate &(ctx->buf_len));
3820Sstevel@tonic-gate ctx->ok=(int)ret;
3830Sstevel@tonic-gate if (ret <= 0) break;
3840Sstevel@tonic-gate
3850Sstevel@tonic-gate /* push out the bytes */
3860Sstevel@tonic-gate goto again;
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate /* Finally flush the underlying BIO */
3900Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3910Sstevel@tonic-gate break;
3920Sstevel@tonic-gate case BIO_C_GET_CIPHER_STATUS:
3930Sstevel@tonic-gate ret=(long)ctx->ok;
3940Sstevel@tonic-gate break;
3950Sstevel@tonic-gate case BIO_C_DO_STATE_MACHINE:
3960Sstevel@tonic-gate BIO_clear_retry_flags(b);
3970Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3980Sstevel@tonic-gate BIO_copy_next_retry(b);
3990Sstevel@tonic-gate break;
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate case BIO_CTRL_DUP:
4020Sstevel@tonic-gate dbio=(BIO *)ptr;
4030Sstevel@tonic-gate dctx=(BIO_ENC_CTX *)dbio->ptr;
4040Sstevel@tonic-gate memcpy(&(dctx->cipher),&(ctx->cipher),sizeof(ctx->cipher));
4050Sstevel@tonic-gate dbio->init=1;
4060Sstevel@tonic-gate break;
4070Sstevel@tonic-gate default:
4080Sstevel@tonic-gate ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
4090Sstevel@tonic-gate break;
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate return(ret);
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate
ber_callback_ctrl(BIO * b,int cmd,void * (* fp)())4140Sstevel@tonic-gate static long ber_callback_ctrl(BIO *b, int cmd, void *(*fp)())
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate long ret=1;
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate if (b->next_bio == NULL) return(0);
4190Sstevel@tonic-gate switch (cmd)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate default:
4220Sstevel@tonic-gate ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
4230Sstevel@tonic-gate break;
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate return(ret);
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate /*
4290Sstevel@tonic-gate void BIO_set_cipher_ctx(b,c)
4300Sstevel@tonic-gate BIO *b;
4310Sstevel@tonic-gate EVP_CIPHER_ctx *c;
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate if (b == NULL) return;
4340Sstevel@tonic-gate
4350Sstevel@tonic-gate if ((b->callback != NULL) &&
4360Sstevel@tonic-gate (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
4370Sstevel@tonic-gate return;
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate b->init=1;
4400Sstevel@tonic-gate ctx=(BIO_ENC_CTX *)b->ptr;
4410Sstevel@tonic-gate memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate if (b->callback != NULL)
4440Sstevel@tonic-gate b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate */
4470Sstevel@tonic-gate
BIO_set_cipher(BIO * b,EVP_CIPHER * c,unsigned char * k,unsigned char * i,int e)4480Sstevel@tonic-gate void BIO_set_cipher(BIO *b, EVP_CIPHER *c, unsigned char *k, unsigned char *i,
4490Sstevel@tonic-gate int e)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate BIO_ENC_CTX *ctx;
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate if (b == NULL) return;
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate if ((b->callback != NULL) &&
4560Sstevel@tonic-gate (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
4570Sstevel@tonic-gate return;
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate b->init=1;
4600Sstevel@tonic-gate ctx=(BIO_ENC_CTX *)b->ptr;
4610Sstevel@tonic-gate EVP_CipherInit_ex(&(ctx->cipher),c,NULL,k,i,e);
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate if (b->callback != NULL)
4640Sstevel@tonic-gate b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate
467