xref: /onnv-gate/usr/src/common/openssl/crypto/evp/bio_enc.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/evp/bio_enc.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 enc_write(BIO *h, const char *buf, int num);
660Sstevel@tonic-gate static int enc_read(BIO *h, char *buf, int size);
670Sstevel@tonic-gate /*static int enc_puts(BIO *h, const char *str); */
680Sstevel@tonic-gate /*static int enc_gets(BIO *h, char *str, int size); */
690Sstevel@tonic-gate static long enc_ctrl(BIO *h, int cmd, long arg1, void *arg2);
700Sstevel@tonic-gate static int enc_new(BIO *h);
710Sstevel@tonic-gate static int enc_free(BIO *data);
720Sstevel@tonic-gate static long enc_callback_ctrl(BIO *h, int cmd, bio_info_cb *fps);
730Sstevel@tonic-gate #define ENC_BLOCK_SIZE	(1024*4)
74*2139Sjp161948 #define BUF_OFFSET	(EVP_MAX_BLOCK_LENGTH*2)
750Sstevel@tonic-gate 
760Sstevel@tonic-gate typedef struct enc_struct
770Sstevel@tonic-gate 	{
780Sstevel@tonic-gate 	int buf_len;
790Sstevel@tonic-gate 	int buf_off;
800Sstevel@tonic-gate 	int cont;		/* <= 0 when finished */
810Sstevel@tonic-gate 	int finished;
820Sstevel@tonic-gate 	int ok;			/* bad decrypt */
830Sstevel@tonic-gate 	EVP_CIPHER_CTX cipher;
840Sstevel@tonic-gate 	/* buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate
850Sstevel@tonic-gate 	 * can return up to a block more data than is presented to it
860Sstevel@tonic-gate 	 */
870Sstevel@tonic-gate 	char buf[ENC_BLOCK_SIZE+BUF_OFFSET+2];
880Sstevel@tonic-gate 	} BIO_ENC_CTX;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static BIO_METHOD methods_enc=
910Sstevel@tonic-gate 	{
920Sstevel@tonic-gate 	BIO_TYPE_CIPHER,"cipher",
930Sstevel@tonic-gate 	enc_write,
940Sstevel@tonic-gate 	enc_read,
950Sstevel@tonic-gate 	NULL, /* enc_puts, */
960Sstevel@tonic-gate 	NULL, /* enc_gets, */
970Sstevel@tonic-gate 	enc_ctrl,
980Sstevel@tonic-gate 	enc_new,
990Sstevel@tonic-gate 	enc_free,
1000Sstevel@tonic-gate 	enc_callback_ctrl,
1010Sstevel@tonic-gate 	};
1020Sstevel@tonic-gate 
BIO_f_cipher(void)1030Sstevel@tonic-gate BIO_METHOD *BIO_f_cipher(void)
1040Sstevel@tonic-gate 	{
1050Sstevel@tonic-gate 	return(&methods_enc);
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
enc_new(BIO * bi)1080Sstevel@tonic-gate static int enc_new(BIO *bi)
1090Sstevel@tonic-gate 	{
1100Sstevel@tonic-gate 	BIO_ENC_CTX *ctx;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	ctx=(BIO_ENC_CTX *)OPENSSL_malloc(sizeof(BIO_ENC_CTX));
1130Sstevel@tonic-gate 	if (ctx == NULL) return(0);
1140Sstevel@tonic-gate 	EVP_CIPHER_CTX_init(&ctx->cipher);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	ctx->buf_len=0;
1170Sstevel@tonic-gate 	ctx->buf_off=0;
1180Sstevel@tonic-gate 	ctx->cont=1;
1190Sstevel@tonic-gate 	ctx->finished=0;
1200Sstevel@tonic-gate 	ctx->ok=1;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	bi->init=0;
1230Sstevel@tonic-gate 	bi->ptr=(char *)ctx;
1240Sstevel@tonic-gate 	bi->flags=0;
1250Sstevel@tonic-gate 	return(1);
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 
enc_free(BIO * a)1280Sstevel@tonic-gate static int enc_free(BIO *a)
1290Sstevel@tonic-gate 	{
1300Sstevel@tonic-gate 	BIO_ENC_CTX *b;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if (a == NULL) return(0);
1330Sstevel@tonic-gate 	b=(BIO_ENC_CTX *)a->ptr;
1340Sstevel@tonic-gate 	EVP_CIPHER_CTX_cleanup(&(b->cipher));
1350Sstevel@tonic-gate 	OPENSSL_cleanse(a->ptr,sizeof(BIO_ENC_CTX));
1360Sstevel@tonic-gate 	OPENSSL_free(a->ptr);
1370Sstevel@tonic-gate 	a->ptr=NULL;
1380Sstevel@tonic-gate 	a->init=0;
1390Sstevel@tonic-gate 	a->flags=0;
1400Sstevel@tonic-gate 	return(1);
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 
enc_read(BIO * b,char * out,int outl)1430Sstevel@tonic-gate static int enc_read(BIO *b, char *out, int outl)
1440Sstevel@tonic-gate 	{
1450Sstevel@tonic-gate 	int ret=0,i;
1460Sstevel@tonic-gate 	BIO_ENC_CTX *ctx;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if (out == NULL) return(0);
1490Sstevel@tonic-gate 	ctx=(BIO_ENC_CTX *)b->ptr;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/* First check if there are bytes decoded/encoded */
1540Sstevel@tonic-gate 	if (ctx->buf_len > 0)
1550Sstevel@tonic-gate 		{
1560Sstevel@tonic-gate 		i=ctx->buf_len-ctx->buf_off;
1570Sstevel@tonic-gate 		if (i > outl) i=outl;
1580Sstevel@tonic-gate 		memcpy(out,&(ctx->buf[ctx->buf_off]),i);
1590Sstevel@tonic-gate 		ret=i;
1600Sstevel@tonic-gate 		out+=i;
1610Sstevel@tonic-gate 		outl-=i;
1620Sstevel@tonic-gate 		ctx->buf_off+=i;
1630Sstevel@tonic-gate 		if (ctx->buf_len == ctx->buf_off)
1640Sstevel@tonic-gate 			{
1650Sstevel@tonic-gate 			ctx->buf_len=0;
1660Sstevel@tonic-gate 			ctx->buf_off=0;
1670Sstevel@tonic-gate 			}
1680Sstevel@tonic-gate 		}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/* At this point, we have room of outl bytes and an empty
1710Sstevel@tonic-gate 	 * buffer, so we should read in some more. */
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	while (outl > 0)
1740Sstevel@tonic-gate 		{
1750Sstevel@tonic-gate 		if (ctx->cont <= 0) break;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 		/* read in at IV offset, read the EVP_Cipher
1780Sstevel@tonic-gate 		 * documentation about why */
1790Sstevel@tonic-gate 		i=BIO_read(b->next_bio,&(ctx->buf[BUF_OFFSET]),ENC_BLOCK_SIZE);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 		if (i <= 0)
1820Sstevel@tonic-gate 			{
1830Sstevel@tonic-gate 			/* Should be continue next time we are called? */
1840Sstevel@tonic-gate 			if (!BIO_should_retry(b->next_bio))
1850Sstevel@tonic-gate 				{
1860Sstevel@tonic-gate 				ctx->cont=i;
1870Sstevel@tonic-gate 				i=EVP_CipherFinal_ex(&(ctx->cipher),
1880Sstevel@tonic-gate 					(unsigned char *)ctx->buf,
1890Sstevel@tonic-gate 					&(ctx->buf_len));
1900Sstevel@tonic-gate 				ctx->ok=i;
1910Sstevel@tonic-gate 				ctx->buf_off=0;
1920Sstevel@tonic-gate 				}
1930Sstevel@tonic-gate 			else
1940Sstevel@tonic-gate 				{
1950Sstevel@tonic-gate 				ret=(ret == 0)?i:ret;
1960Sstevel@tonic-gate 				break;
1970Sstevel@tonic-gate 				}
1980Sstevel@tonic-gate 			}
1990Sstevel@tonic-gate 		else
2000Sstevel@tonic-gate 			{
2010Sstevel@tonic-gate 			EVP_CipherUpdate(&(ctx->cipher),
2020Sstevel@tonic-gate 				(unsigned char *)ctx->buf,&ctx->buf_len,
2030Sstevel@tonic-gate 				(unsigned char *)&(ctx->buf[BUF_OFFSET]),i);
2040Sstevel@tonic-gate 			ctx->cont=1;
2050Sstevel@tonic-gate 			/* Note: it is possible for EVP_CipherUpdate to
2060Sstevel@tonic-gate 			 * decrypt zero bytes because this is or looks like
2070Sstevel@tonic-gate 			 * the final block: if this happens we should retry
2080Sstevel@tonic-gate 			 * and either read more data or decrypt the final
2090Sstevel@tonic-gate 			 * block
2100Sstevel@tonic-gate 			 */
2110Sstevel@tonic-gate 			if(ctx->buf_len == 0) continue;
2120Sstevel@tonic-gate 			}
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 		if (ctx->buf_len <= outl)
2150Sstevel@tonic-gate 			i=ctx->buf_len;
2160Sstevel@tonic-gate 		else
2170Sstevel@tonic-gate 			i=outl;
2180Sstevel@tonic-gate 		if (i <= 0) break;
2190Sstevel@tonic-gate 		memcpy(out,ctx->buf,i);
2200Sstevel@tonic-gate 		ret+=i;
2210Sstevel@tonic-gate 		ctx->buf_off=i;
2220Sstevel@tonic-gate 		outl-=i;
2230Sstevel@tonic-gate 		out+=i;
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
2270Sstevel@tonic-gate 	BIO_copy_next_retry(b);
2280Sstevel@tonic-gate 	return((ret == 0)?ctx->cont:ret);
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 
enc_write(BIO * b,const char * in,int inl)2310Sstevel@tonic-gate static int enc_write(BIO *b, const char *in, int inl)
2320Sstevel@tonic-gate 	{
2330Sstevel@tonic-gate 	int ret=0,n,i;
2340Sstevel@tonic-gate 	BIO_ENC_CTX *ctx;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	ctx=(BIO_ENC_CTX *)b->ptr;
2370Sstevel@tonic-gate 	ret=inl;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
2400Sstevel@tonic-gate 	n=ctx->buf_len-ctx->buf_off;
2410Sstevel@tonic-gate 	while (n > 0)
2420Sstevel@tonic-gate 		{
2430Sstevel@tonic-gate 		i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
2440Sstevel@tonic-gate 		if (i <= 0)
2450Sstevel@tonic-gate 			{
2460Sstevel@tonic-gate 			BIO_copy_next_retry(b);
2470Sstevel@tonic-gate 			return(i);
2480Sstevel@tonic-gate 			}
2490Sstevel@tonic-gate 		ctx->buf_off+=i;
2500Sstevel@tonic-gate 		n-=i;
2510Sstevel@tonic-gate 		}
2520Sstevel@tonic-gate 	/* at this point all pending data has been written */
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	if ((in == NULL) || (inl <= 0)) return(0);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	ctx->buf_off=0;
2570Sstevel@tonic-gate 	while (inl > 0)
2580Sstevel@tonic-gate 		{
2590Sstevel@tonic-gate 		n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
2600Sstevel@tonic-gate 		EVP_CipherUpdate(&(ctx->cipher),
2610Sstevel@tonic-gate 			(unsigned char *)ctx->buf,&ctx->buf_len,
2620Sstevel@tonic-gate 			(unsigned char *)in,n);
2630Sstevel@tonic-gate 		inl-=n;
2640Sstevel@tonic-gate 		in+=n;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 		ctx->buf_off=0;
2670Sstevel@tonic-gate 		n=ctx->buf_len;
2680Sstevel@tonic-gate 		while (n > 0)
2690Sstevel@tonic-gate 			{
2700Sstevel@tonic-gate 			i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
2710Sstevel@tonic-gate 			if (i <= 0)
2720Sstevel@tonic-gate 				{
2730Sstevel@tonic-gate 				BIO_copy_next_retry(b);
2740Sstevel@tonic-gate 				return (ret == inl) ? i : ret - inl;
2750Sstevel@tonic-gate 				}
2760Sstevel@tonic-gate 			n-=i;
2770Sstevel@tonic-gate 			ctx->buf_off+=i;
2780Sstevel@tonic-gate 			}
2790Sstevel@tonic-gate 		ctx->buf_len=0;
2800Sstevel@tonic-gate 		ctx->buf_off=0;
2810Sstevel@tonic-gate 		}
2820Sstevel@tonic-gate 	BIO_copy_next_retry(b);
2830Sstevel@tonic-gate 	return(ret);
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 
enc_ctrl(BIO * b,int cmd,long num,void * ptr)2860Sstevel@tonic-gate static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
2870Sstevel@tonic-gate 	{
2880Sstevel@tonic-gate 	BIO *dbio;
2890Sstevel@tonic-gate 	BIO_ENC_CTX *ctx,*dctx;
2900Sstevel@tonic-gate 	long ret=1;
2910Sstevel@tonic-gate 	int i;
2920Sstevel@tonic-gate 	EVP_CIPHER_CTX **c_ctx;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	ctx=(BIO_ENC_CTX *)b->ptr;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	switch (cmd)
2970Sstevel@tonic-gate 		{
2980Sstevel@tonic-gate 	case BIO_CTRL_RESET:
2990Sstevel@tonic-gate 		ctx->ok=1;
3000Sstevel@tonic-gate 		ctx->finished=0;
3010Sstevel@tonic-gate 		EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL,
3020Sstevel@tonic-gate 			ctx->cipher.encrypt);
3030Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3040Sstevel@tonic-gate 		break;
3050Sstevel@tonic-gate 	case BIO_CTRL_EOF:	/* More to read */
3060Sstevel@tonic-gate 		if (ctx->cont <= 0)
3070Sstevel@tonic-gate 			ret=1;
3080Sstevel@tonic-gate 		else
3090Sstevel@tonic-gate 			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3100Sstevel@tonic-gate 		break;
3110Sstevel@tonic-gate 	case BIO_CTRL_WPENDING:
3120Sstevel@tonic-gate 		ret=ctx->buf_len-ctx->buf_off;
3130Sstevel@tonic-gate 		if (ret <= 0)
3140Sstevel@tonic-gate 			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3150Sstevel@tonic-gate 		break;
3160Sstevel@tonic-gate 	case BIO_CTRL_PENDING: /* More to read in buffer */
3170Sstevel@tonic-gate 		ret=ctx->buf_len-ctx->buf_off;
3180Sstevel@tonic-gate 		if (ret <= 0)
3190Sstevel@tonic-gate 			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3200Sstevel@tonic-gate 		break;
3210Sstevel@tonic-gate 	case BIO_CTRL_FLUSH:
3220Sstevel@tonic-gate 		/* do a final write */
3230Sstevel@tonic-gate again:
3240Sstevel@tonic-gate 		while (ctx->buf_len != ctx->buf_off)
3250Sstevel@tonic-gate 			{
3260Sstevel@tonic-gate 			i=enc_write(b,NULL,0);
3270Sstevel@tonic-gate 			if (i < 0)
3280Sstevel@tonic-gate 				return i;
3290Sstevel@tonic-gate 			}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 		if (!ctx->finished)
3320Sstevel@tonic-gate 			{
3330Sstevel@tonic-gate 			ctx->finished=1;
3340Sstevel@tonic-gate 			ctx->buf_off=0;
3350Sstevel@tonic-gate 			ret=EVP_CipherFinal_ex(&(ctx->cipher),
3360Sstevel@tonic-gate 				(unsigned char *)ctx->buf,
3370Sstevel@tonic-gate 				&(ctx->buf_len));
3380Sstevel@tonic-gate 			ctx->ok=(int)ret;
3390Sstevel@tonic-gate 			if (ret <= 0) break;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 			/* push out the bytes */
3420Sstevel@tonic-gate 			goto again;
3430Sstevel@tonic-gate 			}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 		/* Finally flush the underlying BIO */
3460Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3470Sstevel@tonic-gate 		break;
3480Sstevel@tonic-gate 	case BIO_C_GET_CIPHER_STATUS:
3490Sstevel@tonic-gate 		ret=(long)ctx->ok;
3500Sstevel@tonic-gate 		break;
3510Sstevel@tonic-gate 	case BIO_C_DO_STATE_MACHINE:
3520Sstevel@tonic-gate 		BIO_clear_retry_flags(b);
3530Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3540Sstevel@tonic-gate 		BIO_copy_next_retry(b);
3550Sstevel@tonic-gate 		break;
3560Sstevel@tonic-gate 	case BIO_C_GET_CIPHER_CTX:
3570Sstevel@tonic-gate 		c_ctx=(EVP_CIPHER_CTX **)ptr;
3580Sstevel@tonic-gate 		(*c_ctx)= &(ctx->cipher);
3590Sstevel@tonic-gate 		b->init=1;
3600Sstevel@tonic-gate 		break;
3610Sstevel@tonic-gate 	case BIO_CTRL_DUP:
3620Sstevel@tonic-gate 		dbio=(BIO *)ptr;
3630Sstevel@tonic-gate 		dctx=(BIO_ENC_CTX *)dbio->ptr;
3640Sstevel@tonic-gate 		memcpy(&(dctx->cipher),&(ctx->cipher),sizeof(ctx->cipher));
3650Sstevel@tonic-gate 		dbio->init=1;
3660Sstevel@tonic-gate 		break;
3670Sstevel@tonic-gate 	default:
3680Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3690Sstevel@tonic-gate 		break;
3700Sstevel@tonic-gate 		}
3710Sstevel@tonic-gate 	return(ret);
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 
enc_callback_ctrl(BIO * b,int cmd,bio_info_cb * fp)3740Sstevel@tonic-gate static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
3750Sstevel@tonic-gate 	{
3760Sstevel@tonic-gate 	long ret=1;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	if (b->next_bio == NULL) return(0);
3790Sstevel@tonic-gate 	switch (cmd)
3800Sstevel@tonic-gate 		{
3810Sstevel@tonic-gate 	default:
3820Sstevel@tonic-gate 		ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
3830Sstevel@tonic-gate 		break;
3840Sstevel@tonic-gate 		}
3850Sstevel@tonic-gate 	return(ret);
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate /*
3890Sstevel@tonic-gate void BIO_set_cipher_ctx(b,c)
3900Sstevel@tonic-gate BIO *b;
3910Sstevel@tonic-gate EVP_CIPHER_ctx *c;
3920Sstevel@tonic-gate 	{
3930Sstevel@tonic-gate 	if (b == NULL) return;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	if ((b->callback != NULL) &&
3960Sstevel@tonic-gate 		(b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
3970Sstevel@tonic-gate 		return;
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	b->init=1;
4000Sstevel@tonic-gate 	ctx=(BIO_ENC_CTX *)b->ptr;
4010Sstevel@tonic-gate 	memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	if (b->callback != NULL)
4040Sstevel@tonic-gate 		b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate */
4070Sstevel@tonic-gate 
BIO_set_cipher(BIO * b,const EVP_CIPHER * c,const unsigned char * k,const unsigned char * i,int e)408*2139Sjp161948 void BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
409*2139Sjp161948 	     const unsigned char *i, int e)
4100Sstevel@tonic-gate 	{
4110Sstevel@tonic-gate 	BIO_ENC_CTX *ctx;
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	if (b == NULL) return;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if ((b->callback != NULL) &&
4160Sstevel@tonic-gate 		(b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,0L) <= 0))
4170Sstevel@tonic-gate 		return;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	b->init=1;
4200Sstevel@tonic-gate 	ctx=(BIO_ENC_CTX *)b->ptr;
4210Sstevel@tonic-gate 	EVP_CipherInit_ex(&(ctx->cipher),c,NULL, k,i,e);
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	if (b->callback != NULL)
4240Sstevel@tonic-gate 		b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,1L);
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 
427