xref: /onnv-gate/usr/src/common/openssl/crypto/evp/bio_enc.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* crypto/evp/bio_enc.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <stdio.h>
60*0Sstevel@tonic-gate #include <errno.h>
61*0Sstevel@tonic-gate #include "cryptlib.h"
62*0Sstevel@tonic-gate #include <openssl/buffer.h>
63*0Sstevel@tonic-gate #include <openssl/evp.h>
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate static int enc_write(BIO *h, const char *buf, int num);
66*0Sstevel@tonic-gate static int enc_read(BIO *h, char *buf, int size);
67*0Sstevel@tonic-gate /*static int enc_puts(BIO *h, const char *str); */
68*0Sstevel@tonic-gate /*static int enc_gets(BIO *h, char *str, int size); */
69*0Sstevel@tonic-gate static long enc_ctrl(BIO *h, int cmd, long arg1, void *arg2);
70*0Sstevel@tonic-gate static int enc_new(BIO *h);
71*0Sstevel@tonic-gate static int enc_free(BIO *data);
72*0Sstevel@tonic-gate static long enc_callback_ctrl(BIO *h, int cmd, bio_info_cb *fps);
73*0Sstevel@tonic-gate #define ENC_BLOCK_SIZE	(1024*4)
74*0Sstevel@tonic-gate #define BUF_OFFSET	EVP_MAX_BLOCK_LENGTH
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate typedef struct enc_struct
77*0Sstevel@tonic-gate 	{
78*0Sstevel@tonic-gate 	int buf_len;
79*0Sstevel@tonic-gate 	int buf_off;
80*0Sstevel@tonic-gate 	int cont;		/* <= 0 when finished */
81*0Sstevel@tonic-gate 	int finished;
82*0Sstevel@tonic-gate 	int ok;			/* bad decrypt */
83*0Sstevel@tonic-gate 	EVP_CIPHER_CTX cipher;
84*0Sstevel@tonic-gate 	/* buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate
85*0Sstevel@tonic-gate 	 * can return up to a block more data than is presented to it
86*0Sstevel@tonic-gate 	 */
87*0Sstevel@tonic-gate 	char buf[ENC_BLOCK_SIZE+BUF_OFFSET+2];
88*0Sstevel@tonic-gate 	} BIO_ENC_CTX;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate static BIO_METHOD methods_enc=
91*0Sstevel@tonic-gate 	{
92*0Sstevel@tonic-gate 	BIO_TYPE_CIPHER,"cipher",
93*0Sstevel@tonic-gate 	enc_write,
94*0Sstevel@tonic-gate 	enc_read,
95*0Sstevel@tonic-gate 	NULL, /* enc_puts, */
96*0Sstevel@tonic-gate 	NULL, /* enc_gets, */
97*0Sstevel@tonic-gate 	enc_ctrl,
98*0Sstevel@tonic-gate 	enc_new,
99*0Sstevel@tonic-gate 	enc_free,
100*0Sstevel@tonic-gate 	enc_callback_ctrl,
101*0Sstevel@tonic-gate 	};
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate BIO_METHOD *BIO_f_cipher(void)
104*0Sstevel@tonic-gate 	{
105*0Sstevel@tonic-gate 	return(&methods_enc);
106*0Sstevel@tonic-gate 	}
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate static int enc_new(BIO *bi)
109*0Sstevel@tonic-gate 	{
110*0Sstevel@tonic-gate 	BIO_ENC_CTX *ctx;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	ctx=(BIO_ENC_CTX *)OPENSSL_malloc(sizeof(BIO_ENC_CTX));
113*0Sstevel@tonic-gate 	if (ctx == NULL) return(0);
114*0Sstevel@tonic-gate 	EVP_CIPHER_CTX_init(&ctx->cipher);
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	ctx->buf_len=0;
117*0Sstevel@tonic-gate 	ctx->buf_off=0;
118*0Sstevel@tonic-gate 	ctx->cont=1;
119*0Sstevel@tonic-gate 	ctx->finished=0;
120*0Sstevel@tonic-gate 	ctx->ok=1;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	bi->init=0;
123*0Sstevel@tonic-gate 	bi->ptr=(char *)ctx;
124*0Sstevel@tonic-gate 	bi->flags=0;
125*0Sstevel@tonic-gate 	return(1);
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate static int enc_free(BIO *a)
129*0Sstevel@tonic-gate 	{
130*0Sstevel@tonic-gate 	BIO_ENC_CTX *b;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	if (a == NULL) return(0);
133*0Sstevel@tonic-gate 	b=(BIO_ENC_CTX *)a->ptr;
134*0Sstevel@tonic-gate 	EVP_CIPHER_CTX_cleanup(&(b->cipher));
135*0Sstevel@tonic-gate 	OPENSSL_cleanse(a->ptr,sizeof(BIO_ENC_CTX));
136*0Sstevel@tonic-gate 	OPENSSL_free(a->ptr);
137*0Sstevel@tonic-gate 	a->ptr=NULL;
138*0Sstevel@tonic-gate 	a->init=0;
139*0Sstevel@tonic-gate 	a->flags=0;
140*0Sstevel@tonic-gate 	return(1);
141*0Sstevel@tonic-gate 	}
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate static int enc_read(BIO *b, char *out, int outl)
144*0Sstevel@tonic-gate 	{
145*0Sstevel@tonic-gate 	int ret=0,i;
146*0Sstevel@tonic-gate 	BIO_ENC_CTX *ctx;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	if (out == NULL) return(0);
149*0Sstevel@tonic-gate 	ctx=(BIO_ENC_CTX *)b->ptr;
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	/* First check if there are bytes decoded/encoded */
154*0Sstevel@tonic-gate 	if (ctx->buf_len > 0)
155*0Sstevel@tonic-gate 		{
156*0Sstevel@tonic-gate 		i=ctx->buf_len-ctx->buf_off;
157*0Sstevel@tonic-gate 		if (i > outl) i=outl;
158*0Sstevel@tonic-gate 		memcpy(out,&(ctx->buf[ctx->buf_off]),i);
159*0Sstevel@tonic-gate 		ret=i;
160*0Sstevel@tonic-gate 		out+=i;
161*0Sstevel@tonic-gate 		outl-=i;
162*0Sstevel@tonic-gate 		ctx->buf_off+=i;
163*0Sstevel@tonic-gate 		if (ctx->buf_len == ctx->buf_off)
164*0Sstevel@tonic-gate 			{
165*0Sstevel@tonic-gate 			ctx->buf_len=0;
166*0Sstevel@tonic-gate 			ctx->buf_off=0;
167*0Sstevel@tonic-gate 			}
168*0Sstevel@tonic-gate 		}
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	/* At this point, we have room of outl bytes and an empty
171*0Sstevel@tonic-gate 	 * buffer, so we should read in some more. */
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	while (outl > 0)
174*0Sstevel@tonic-gate 		{
175*0Sstevel@tonic-gate 		if (ctx->cont <= 0) break;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 		/* read in at IV offset, read the EVP_Cipher
178*0Sstevel@tonic-gate 		 * documentation about why */
179*0Sstevel@tonic-gate 		i=BIO_read(b->next_bio,&(ctx->buf[BUF_OFFSET]),ENC_BLOCK_SIZE);
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 		if (i <= 0)
182*0Sstevel@tonic-gate 			{
183*0Sstevel@tonic-gate 			/* Should be continue next time we are called? */
184*0Sstevel@tonic-gate 			if (!BIO_should_retry(b->next_bio))
185*0Sstevel@tonic-gate 				{
186*0Sstevel@tonic-gate 				ctx->cont=i;
187*0Sstevel@tonic-gate 				i=EVP_CipherFinal_ex(&(ctx->cipher),
188*0Sstevel@tonic-gate 					(unsigned char *)ctx->buf,
189*0Sstevel@tonic-gate 					&(ctx->buf_len));
190*0Sstevel@tonic-gate 				ctx->ok=i;
191*0Sstevel@tonic-gate 				ctx->buf_off=0;
192*0Sstevel@tonic-gate 				}
193*0Sstevel@tonic-gate 			else
194*0Sstevel@tonic-gate 				{
195*0Sstevel@tonic-gate 				ret=(ret == 0)?i:ret;
196*0Sstevel@tonic-gate 				break;
197*0Sstevel@tonic-gate 				}
198*0Sstevel@tonic-gate 			}
199*0Sstevel@tonic-gate 		else
200*0Sstevel@tonic-gate 			{
201*0Sstevel@tonic-gate 			EVP_CipherUpdate(&(ctx->cipher),
202*0Sstevel@tonic-gate 				(unsigned char *)ctx->buf,&ctx->buf_len,
203*0Sstevel@tonic-gate 				(unsigned char *)&(ctx->buf[BUF_OFFSET]),i);
204*0Sstevel@tonic-gate 			ctx->cont=1;
205*0Sstevel@tonic-gate 			/* Note: it is possible for EVP_CipherUpdate to
206*0Sstevel@tonic-gate 			 * decrypt zero bytes because this is or looks like
207*0Sstevel@tonic-gate 			 * the final block: if this happens we should retry
208*0Sstevel@tonic-gate 			 * and either read more data or decrypt the final
209*0Sstevel@tonic-gate 			 * block
210*0Sstevel@tonic-gate 			 */
211*0Sstevel@tonic-gate 			if(ctx->buf_len == 0) continue;
212*0Sstevel@tonic-gate 			}
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 		if (ctx->buf_len <= outl)
215*0Sstevel@tonic-gate 			i=ctx->buf_len;
216*0Sstevel@tonic-gate 		else
217*0Sstevel@tonic-gate 			i=outl;
218*0Sstevel@tonic-gate 		if (i <= 0) break;
219*0Sstevel@tonic-gate 		memcpy(out,ctx->buf,i);
220*0Sstevel@tonic-gate 		ret+=i;
221*0Sstevel@tonic-gate 		ctx->buf_off=i;
222*0Sstevel@tonic-gate 		outl-=i;
223*0Sstevel@tonic-gate 		out+=i;
224*0Sstevel@tonic-gate 		}
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
227*0Sstevel@tonic-gate 	BIO_copy_next_retry(b);
228*0Sstevel@tonic-gate 	return((ret == 0)?ctx->cont:ret);
229*0Sstevel@tonic-gate 	}
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate static int enc_write(BIO *b, const char *in, int inl)
232*0Sstevel@tonic-gate 	{
233*0Sstevel@tonic-gate 	int ret=0,n,i;
234*0Sstevel@tonic-gate 	BIO_ENC_CTX *ctx;
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	ctx=(BIO_ENC_CTX *)b->ptr;
237*0Sstevel@tonic-gate 	ret=inl;
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
240*0Sstevel@tonic-gate 	n=ctx->buf_len-ctx->buf_off;
241*0Sstevel@tonic-gate 	while (n > 0)
242*0Sstevel@tonic-gate 		{
243*0Sstevel@tonic-gate 		i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
244*0Sstevel@tonic-gate 		if (i <= 0)
245*0Sstevel@tonic-gate 			{
246*0Sstevel@tonic-gate 			BIO_copy_next_retry(b);
247*0Sstevel@tonic-gate 			return(i);
248*0Sstevel@tonic-gate 			}
249*0Sstevel@tonic-gate 		ctx->buf_off+=i;
250*0Sstevel@tonic-gate 		n-=i;
251*0Sstevel@tonic-gate 		}
252*0Sstevel@tonic-gate 	/* at this point all pending data has been written */
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	if ((in == NULL) || (inl <= 0)) return(0);
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	ctx->buf_off=0;
257*0Sstevel@tonic-gate 	while (inl > 0)
258*0Sstevel@tonic-gate 		{
259*0Sstevel@tonic-gate 		n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
260*0Sstevel@tonic-gate 		EVP_CipherUpdate(&(ctx->cipher),
261*0Sstevel@tonic-gate 			(unsigned char *)ctx->buf,&ctx->buf_len,
262*0Sstevel@tonic-gate 			(unsigned char *)in,n);
263*0Sstevel@tonic-gate 		inl-=n;
264*0Sstevel@tonic-gate 		in+=n;
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 		ctx->buf_off=0;
267*0Sstevel@tonic-gate 		n=ctx->buf_len;
268*0Sstevel@tonic-gate 		while (n > 0)
269*0Sstevel@tonic-gate 			{
270*0Sstevel@tonic-gate 			i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
271*0Sstevel@tonic-gate 			if (i <= 0)
272*0Sstevel@tonic-gate 				{
273*0Sstevel@tonic-gate 				BIO_copy_next_retry(b);
274*0Sstevel@tonic-gate 				return (ret == inl) ? i : ret - inl;
275*0Sstevel@tonic-gate 				}
276*0Sstevel@tonic-gate 			n-=i;
277*0Sstevel@tonic-gate 			ctx->buf_off+=i;
278*0Sstevel@tonic-gate 			}
279*0Sstevel@tonic-gate 		ctx->buf_len=0;
280*0Sstevel@tonic-gate 		ctx->buf_off=0;
281*0Sstevel@tonic-gate 		}
282*0Sstevel@tonic-gate 	BIO_copy_next_retry(b);
283*0Sstevel@tonic-gate 	return(ret);
284*0Sstevel@tonic-gate 	}
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
287*0Sstevel@tonic-gate 	{
288*0Sstevel@tonic-gate 	BIO *dbio;
289*0Sstevel@tonic-gate 	BIO_ENC_CTX *ctx,*dctx;
290*0Sstevel@tonic-gate 	long ret=1;
291*0Sstevel@tonic-gate 	int i;
292*0Sstevel@tonic-gate 	EVP_CIPHER_CTX **c_ctx;
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	ctx=(BIO_ENC_CTX *)b->ptr;
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	switch (cmd)
297*0Sstevel@tonic-gate 		{
298*0Sstevel@tonic-gate 	case BIO_CTRL_RESET:
299*0Sstevel@tonic-gate 		ctx->ok=1;
300*0Sstevel@tonic-gate 		ctx->finished=0;
301*0Sstevel@tonic-gate 		EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL,
302*0Sstevel@tonic-gate 			ctx->cipher.encrypt);
303*0Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
304*0Sstevel@tonic-gate 		break;
305*0Sstevel@tonic-gate 	case BIO_CTRL_EOF:	/* More to read */
306*0Sstevel@tonic-gate 		if (ctx->cont <= 0)
307*0Sstevel@tonic-gate 			ret=1;
308*0Sstevel@tonic-gate 		else
309*0Sstevel@tonic-gate 			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
310*0Sstevel@tonic-gate 		break;
311*0Sstevel@tonic-gate 	case BIO_CTRL_WPENDING:
312*0Sstevel@tonic-gate 		ret=ctx->buf_len-ctx->buf_off;
313*0Sstevel@tonic-gate 		if (ret <= 0)
314*0Sstevel@tonic-gate 			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
315*0Sstevel@tonic-gate 		break;
316*0Sstevel@tonic-gate 	case BIO_CTRL_PENDING: /* More to read in buffer */
317*0Sstevel@tonic-gate 		ret=ctx->buf_len-ctx->buf_off;
318*0Sstevel@tonic-gate 		if (ret <= 0)
319*0Sstevel@tonic-gate 			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
320*0Sstevel@tonic-gate 		break;
321*0Sstevel@tonic-gate 	case BIO_CTRL_FLUSH:
322*0Sstevel@tonic-gate 		/* do a final write */
323*0Sstevel@tonic-gate again:
324*0Sstevel@tonic-gate 		while (ctx->buf_len != ctx->buf_off)
325*0Sstevel@tonic-gate 			{
326*0Sstevel@tonic-gate 			i=enc_write(b,NULL,0);
327*0Sstevel@tonic-gate 			if (i < 0)
328*0Sstevel@tonic-gate 				return i;
329*0Sstevel@tonic-gate 			}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 		if (!ctx->finished)
332*0Sstevel@tonic-gate 			{
333*0Sstevel@tonic-gate 			ctx->finished=1;
334*0Sstevel@tonic-gate 			ctx->buf_off=0;
335*0Sstevel@tonic-gate 			ret=EVP_CipherFinal_ex(&(ctx->cipher),
336*0Sstevel@tonic-gate 				(unsigned char *)ctx->buf,
337*0Sstevel@tonic-gate 				&(ctx->buf_len));
338*0Sstevel@tonic-gate 			ctx->ok=(int)ret;
339*0Sstevel@tonic-gate 			if (ret <= 0) break;
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 			/* push out the bytes */
342*0Sstevel@tonic-gate 			goto again;
343*0Sstevel@tonic-gate 			}
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 		/* Finally flush the underlying BIO */
346*0Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
347*0Sstevel@tonic-gate 		break;
348*0Sstevel@tonic-gate 	case BIO_C_GET_CIPHER_STATUS:
349*0Sstevel@tonic-gate 		ret=(long)ctx->ok;
350*0Sstevel@tonic-gate 		break;
351*0Sstevel@tonic-gate 	case BIO_C_DO_STATE_MACHINE:
352*0Sstevel@tonic-gate 		BIO_clear_retry_flags(b);
353*0Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
354*0Sstevel@tonic-gate 		BIO_copy_next_retry(b);
355*0Sstevel@tonic-gate 		break;
356*0Sstevel@tonic-gate 	case BIO_C_GET_CIPHER_CTX:
357*0Sstevel@tonic-gate 		c_ctx=(EVP_CIPHER_CTX **)ptr;
358*0Sstevel@tonic-gate 		(*c_ctx)= &(ctx->cipher);
359*0Sstevel@tonic-gate 		b->init=1;
360*0Sstevel@tonic-gate 		break;
361*0Sstevel@tonic-gate 	case BIO_CTRL_DUP:
362*0Sstevel@tonic-gate 		dbio=(BIO *)ptr;
363*0Sstevel@tonic-gate 		dctx=(BIO_ENC_CTX *)dbio->ptr;
364*0Sstevel@tonic-gate 		memcpy(&(dctx->cipher),&(ctx->cipher),sizeof(ctx->cipher));
365*0Sstevel@tonic-gate 		dbio->init=1;
366*0Sstevel@tonic-gate 		break;
367*0Sstevel@tonic-gate 	default:
368*0Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
369*0Sstevel@tonic-gate 		break;
370*0Sstevel@tonic-gate 		}
371*0Sstevel@tonic-gate 	return(ret);
372*0Sstevel@tonic-gate 	}
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
375*0Sstevel@tonic-gate 	{
376*0Sstevel@tonic-gate 	long ret=1;
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 	if (b->next_bio == NULL) return(0);
379*0Sstevel@tonic-gate 	switch (cmd)
380*0Sstevel@tonic-gate 		{
381*0Sstevel@tonic-gate 	default:
382*0Sstevel@tonic-gate 		ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
383*0Sstevel@tonic-gate 		break;
384*0Sstevel@tonic-gate 		}
385*0Sstevel@tonic-gate 	return(ret);
386*0Sstevel@tonic-gate 	}
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate /*
389*0Sstevel@tonic-gate void BIO_set_cipher_ctx(b,c)
390*0Sstevel@tonic-gate BIO *b;
391*0Sstevel@tonic-gate EVP_CIPHER_ctx *c;
392*0Sstevel@tonic-gate 	{
393*0Sstevel@tonic-gate 	if (b == NULL) return;
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	if ((b->callback != NULL) &&
396*0Sstevel@tonic-gate 		(b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
397*0Sstevel@tonic-gate 		return;
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 	b->init=1;
400*0Sstevel@tonic-gate 	ctx=(BIO_ENC_CTX *)b->ptr;
401*0Sstevel@tonic-gate 	memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	if (b->callback != NULL)
404*0Sstevel@tonic-gate 		b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
405*0Sstevel@tonic-gate 	}
406*0Sstevel@tonic-gate */
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate void BIO_set_cipher(BIO *b, const EVP_CIPHER *c, unsigned char *k,
409*0Sstevel@tonic-gate 	     unsigned char *i, int e)
410*0Sstevel@tonic-gate 	{
411*0Sstevel@tonic-gate 	BIO_ENC_CTX *ctx;
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	if (b == NULL) return;
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 	if ((b->callback != NULL) &&
416*0Sstevel@tonic-gate 		(b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,0L) <= 0))
417*0Sstevel@tonic-gate 		return;
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 	b->init=1;
420*0Sstevel@tonic-gate 	ctx=(BIO_ENC_CTX *)b->ptr;
421*0Sstevel@tonic-gate 	EVP_CipherInit_ex(&(ctx->cipher),c,NULL, k,i,e);
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 	if (b->callback != NULL)
424*0Sstevel@tonic-gate 		b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,1L);
425*0Sstevel@tonic-gate 	}
426*0Sstevel@tonic-gate 
427