xref: /onnv-gate/usr/src/common/openssl/crypto/evp/bio_ok.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/evp/bio_ok.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 /*
600Sstevel@tonic-gate 	From: Arne Ansper <arne@cyber.ee>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	Why BIO_f_reliable?
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	I wrote function which took BIO* as argument, read data from it
650Sstevel@tonic-gate 	and processed it. Then I wanted to store the input file in
660Sstevel@tonic-gate 	encrypted form. OK I pushed BIO_f_cipher to the BIO stack
670Sstevel@tonic-gate 	and everything was OK. BUT if user types wrong password
680Sstevel@tonic-gate 	BIO_f_cipher outputs only garbage and my function crashes. Yes
690Sstevel@tonic-gate 	I can and I should fix my function, but BIO_f_cipher is
700Sstevel@tonic-gate 	easy way to add encryption support to many existing applications
710Sstevel@tonic-gate 	and it's hard to debug and fix them all.
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	So I wanted another BIO which would catch the incorrect passwords and
740Sstevel@tonic-gate 	file damages which cause garbage on BIO_f_cipher's output.
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	The easy way is to push the BIO_f_md and save the checksum at
770Sstevel@tonic-gate 	the end of the file. However there are several problems with this
780Sstevel@tonic-gate 	approach:
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	1) you must somehow separate checksum from actual data.
810Sstevel@tonic-gate 	2) you need lot's of memory when reading the file, because you
820Sstevel@tonic-gate 	must read to the end of the file and verify the checksum before
830Sstevel@tonic-gate 	letting the application to read the data.
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	BIO_f_reliable tries to solve both problems, so that you can
860Sstevel@tonic-gate 	read and write arbitrary long streams using only fixed amount
870Sstevel@tonic-gate 	of memory.
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	BIO_f_reliable splits data stream into blocks. Each block is prefixed
900Sstevel@tonic-gate 	with it's length and suffixed with it's digest. So you need only
910Sstevel@tonic-gate 	several Kbytes of memory to buffer single block before verifying
920Sstevel@tonic-gate 	it's digest.
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	BIO_f_reliable goes further and adds several important capabilities:
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	1) the digest of the block is computed over the whole stream
970Sstevel@tonic-gate 	-- so nobody can rearrange the blocks or remove or replace them.
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	2) to detect invalid passwords right at the start BIO_f_reliable
1000Sstevel@tonic-gate 	adds special prefix to the stream. In order to avoid known plain-text
1010Sstevel@tonic-gate 	attacks this prefix is generated as follows:
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 		*) digest is initialized with random seed instead of
1040Sstevel@tonic-gate 		standardized one.
1050Sstevel@tonic-gate 		*) same seed is written to output
1060Sstevel@tonic-gate 		*) well-known text is then hashed and the output
1070Sstevel@tonic-gate 		of the digest is also written to output.
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	reader can now read the seed from stream, hash the same string
1100Sstevel@tonic-gate 	and then compare the digest output.
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	Bad things: BIO_f_reliable knows what's going on in EVP_Digest. I
1130Sstevel@tonic-gate 	initially wrote and tested this code on x86 machine and wrote the
1140Sstevel@tonic-gate 	digests out in machine-dependent order :( There are people using
1150Sstevel@tonic-gate 	this code and I cannot change this easily without making existing
1160Sstevel@tonic-gate 	data files unreadable.
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate */
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate #include <stdio.h>
1210Sstevel@tonic-gate #include <errno.h>
122*2139Sjp161948 #include <assert.h>
1230Sstevel@tonic-gate #include "cryptlib.h"
1240Sstevel@tonic-gate #include <openssl/buffer.h>
1250Sstevel@tonic-gate #include <openssl/bio.h>
1260Sstevel@tonic-gate #include <openssl/evp.h>
1270Sstevel@tonic-gate #include <openssl/rand.h>
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate static int ok_write(BIO *h, const char *buf, int num);
1300Sstevel@tonic-gate static int ok_read(BIO *h, char *buf, int size);
1310Sstevel@tonic-gate static long ok_ctrl(BIO *h, int cmd, long arg1, void *arg2);
1320Sstevel@tonic-gate static int ok_new(BIO *h);
1330Sstevel@tonic-gate static int ok_free(BIO *data);
1340Sstevel@tonic-gate static long ok_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static void sig_out(BIO* b);
1370Sstevel@tonic-gate static void sig_in(BIO* b);
1380Sstevel@tonic-gate static void block_out(BIO* b);
1390Sstevel@tonic-gate static void block_in(BIO* b);
1400Sstevel@tonic-gate #define OK_BLOCK_SIZE	(1024*4)
1410Sstevel@tonic-gate #define OK_BLOCK_BLOCK	4
1420Sstevel@tonic-gate #define IOBS		(OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE)
1430Sstevel@tonic-gate #define WELLKNOWN "The quick brown fox jumped over the lazy dog's back."
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate typedef struct ok_struct
1460Sstevel@tonic-gate 	{
147*2139Sjp161948 	size_t buf_len;
148*2139Sjp161948 	size_t buf_off;
149*2139Sjp161948 	size_t buf_len_save;
150*2139Sjp161948 	size_t buf_off_save;
1510Sstevel@tonic-gate 	int cont;		/* <= 0 when finished */
1520Sstevel@tonic-gate 	int finished;
1530Sstevel@tonic-gate 	EVP_MD_CTX md;
1540Sstevel@tonic-gate 	int blockout;		/* output block is ready */
1550Sstevel@tonic-gate 	int sigio;		/* must process signature */
1560Sstevel@tonic-gate 	unsigned char buf[IOBS];
1570Sstevel@tonic-gate 	} BIO_OK_CTX;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate static BIO_METHOD methods_ok=
1600Sstevel@tonic-gate 	{
1610Sstevel@tonic-gate 	BIO_TYPE_CIPHER,"reliable",
1620Sstevel@tonic-gate 	ok_write,
1630Sstevel@tonic-gate 	ok_read,
1640Sstevel@tonic-gate 	NULL, /* ok_puts, */
1650Sstevel@tonic-gate 	NULL, /* ok_gets, */
1660Sstevel@tonic-gate 	ok_ctrl,
1670Sstevel@tonic-gate 	ok_new,
1680Sstevel@tonic-gate 	ok_free,
1690Sstevel@tonic-gate 	ok_callback_ctrl,
1700Sstevel@tonic-gate 	};
1710Sstevel@tonic-gate 
BIO_f_reliable(void)1720Sstevel@tonic-gate BIO_METHOD *BIO_f_reliable(void)
1730Sstevel@tonic-gate 	{
1740Sstevel@tonic-gate 	return(&methods_ok);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
ok_new(BIO * bi)1770Sstevel@tonic-gate static int ok_new(BIO *bi)
1780Sstevel@tonic-gate 	{
1790Sstevel@tonic-gate 	BIO_OK_CTX *ctx;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	ctx=(BIO_OK_CTX *)OPENSSL_malloc(sizeof(BIO_OK_CTX));
1820Sstevel@tonic-gate 	if (ctx == NULL) return(0);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	ctx->buf_len=0;
1850Sstevel@tonic-gate 	ctx->buf_off=0;
1860Sstevel@tonic-gate 	ctx->buf_len_save=0;
1870Sstevel@tonic-gate 	ctx->buf_off_save=0;
1880Sstevel@tonic-gate 	ctx->cont=1;
1890Sstevel@tonic-gate 	ctx->finished=0;
1900Sstevel@tonic-gate 	ctx->blockout= 0;
1910Sstevel@tonic-gate 	ctx->sigio=1;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	EVP_MD_CTX_init(&ctx->md);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	bi->init=0;
1960Sstevel@tonic-gate 	bi->ptr=(char *)ctx;
1970Sstevel@tonic-gate 	bi->flags=0;
1980Sstevel@tonic-gate 	return(1);
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 
ok_free(BIO * a)2010Sstevel@tonic-gate static int ok_free(BIO *a)
2020Sstevel@tonic-gate 	{
2030Sstevel@tonic-gate 	if (a == NULL) return(0);
2040Sstevel@tonic-gate 	EVP_MD_CTX_cleanup(&((BIO_OK_CTX *)a->ptr)->md);
2050Sstevel@tonic-gate 	OPENSSL_cleanse(a->ptr,sizeof(BIO_OK_CTX));
2060Sstevel@tonic-gate 	OPENSSL_free(a->ptr);
2070Sstevel@tonic-gate 	a->ptr=NULL;
2080Sstevel@tonic-gate 	a->init=0;
2090Sstevel@tonic-gate 	a->flags=0;
2100Sstevel@tonic-gate 	return(1);
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 
ok_read(BIO * b,char * out,int outl)2130Sstevel@tonic-gate static int ok_read(BIO *b, char *out, int outl)
2140Sstevel@tonic-gate 	{
2150Sstevel@tonic-gate 	int ret=0,i,n;
2160Sstevel@tonic-gate 	BIO_OK_CTX *ctx;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (out == NULL) return(0);
2190Sstevel@tonic-gate 	ctx=(BIO_OK_CTX *)b->ptr;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	while(outl > 0)
2240Sstevel@tonic-gate 		{
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 		/* copy clean bytes to output buffer */
2270Sstevel@tonic-gate 		if (ctx->blockout)
2280Sstevel@tonic-gate 			{
2290Sstevel@tonic-gate 			i=ctx->buf_len-ctx->buf_off;
2300Sstevel@tonic-gate 			if (i > outl) i=outl;
2310Sstevel@tonic-gate 			memcpy(out,&(ctx->buf[ctx->buf_off]),i);
2320Sstevel@tonic-gate 			ret+=i;
2330Sstevel@tonic-gate 			out+=i;
2340Sstevel@tonic-gate 			outl-=i;
2350Sstevel@tonic-gate 			ctx->buf_off+=i;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 			/* all clean bytes are out */
2380Sstevel@tonic-gate 			if (ctx->buf_len == ctx->buf_off)
2390Sstevel@tonic-gate 				{
2400Sstevel@tonic-gate 				ctx->buf_off=0;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 				/* copy start of the next block into proper place */
2430Sstevel@tonic-gate 				if(ctx->buf_len_save- ctx->buf_off_save > 0)
2440Sstevel@tonic-gate 					{
2450Sstevel@tonic-gate 					ctx->buf_len= ctx->buf_len_save- ctx->buf_off_save;
2460Sstevel@tonic-gate 					memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]),
2470Sstevel@tonic-gate 							ctx->buf_len);
2480Sstevel@tonic-gate 					}
2490Sstevel@tonic-gate 				else
2500Sstevel@tonic-gate 					{
2510Sstevel@tonic-gate 					ctx->buf_len=0;
2520Sstevel@tonic-gate 					}
2530Sstevel@tonic-gate 				ctx->blockout= 0;
2540Sstevel@tonic-gate 				}
2550Sstevel@tonic-gate 			}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 		/* output buffer full -- cancel */
2580Sstevel@tonic-gate 		if (outl == 0) break;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 		/* no clean bytes in buffer -- fill it */
2610Sstevel@tonic-gate 		n=IOBS- ctx->buf_len;
2620Sstevel@tonic-gate 		i=BIO_read(b->next_bio,&(ctx->buf[ctx->buf_len]),n);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		if (i <= 0) break;	/* nothing new */
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 		ctx->buf_len+= i;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 		/* no signature yet -- check if we got one */
2690Sstevel@tonic-gate 		if (ctx->sigio == 1) sig_in(b);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 		/* signature ok -- check if we got block */
2720Sstevel@tonic-gate 		if (ctx->sigio == 0) block_in(b);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 		/* invalid block -- cancel */
2750Sstevel@tonic-gate 		if (ctx->cont <= 0) break;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
2800Sstevel@tonic-gate 	BIO_copy_next_retry(b);
2810Sstevel@tonic-gate 	return(ret);
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 
ok_write(BIO * b,const char * in,int inl)2840Sstevel@tonic-gate static int ok_write(BIO *b, const char *in, int inl)
2850Sstevel@tonic-gate 	{
2860Sstevel@tonic-gate 	int ret=0,n,i;
2870Sstevel@tonic-gate 	BIO_OK_CTX *ctx;
2880Sstevel@tonic-gate 
289*2139Sjp161948 	if (inl <= 0) return inl;
290*2139Sjp161948 
2910Sstevel@tonic-gate 	ctx=(BIO_OK_CTX *)b->ptr;
2920Sstevel@tonic-gate 	ret=inl;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	if(ctx->sigio) sig_out(b);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	do{
2990Sstevel@tonic-gate 		BIO_clear_retry_flags(b);
3000Sstevel@tonic-gate 		n=ctx->buf_len-ctx->buf_off;
3010Sstevel@tonic-gate 		while (ctx->blockout && n > 0)
3020Sstevel@tonic-gate 			{
3030Sstevel@tonic-gate 			i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
3040Sstevel@tonic-gate 			if (i <= 0)
3050Sstevel@tonic-gate 				{
3060Sstevel@tonic-gate 				BIO_copy_next_retry(b);
3070Sstevel@tonic-gate 				if(!BIO_should_retry(b))
3080Sstevel@tonic-gate 					ctx->cont= 0;
3090Sstevel@tonic-gate 				return(i);
3100Sstevel@tonic-gate 				}
3110Sstevel@tonic-gate 			ctx->buf_off+=i;
3120Sstevel@tonic-gate 			n-=i;
3130Sstevel@tonic-gate 			}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		/* at this point all pending data has been written */
3160Sstevel@tonic-gate 		ctx->blockout= 0;
3170Sstevel@tonic-gate 		if (ctx->buf_len == ctx->buf_off)
3180Sstevel@tonic-gate 			{
3190Sstevel@tonic-gate 			ctx->buf_len=OK_BLOCK_BLOCK;
3200Sstevel@tonic-gate 			ctx->buf_off=0;
3210Sstevel@tonic-gate 			}
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 		if ((in == NULL) || (inl <= 0)) return(0);
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 		n= (inl+ ctx->buf_len > OK_BLOCK_SIZE+ OK_BLOCK_BLOCK) ?
326*2139Sjp161948 			(int)(OK_BLOCK_SIZE+OK_BLOCK_BLOCK-ctx->buf_len) : inl;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		memcpy((unsigned char *)(&(ctx->buf[ctx->buf_len])),(unsigned char *)in,n);
3290Sstevel@tonic-gate 		ctx->buf_len+= n;
3300Sstevel@tonic-gate 		inl-=n;
3310Sstevel@tonic-gate 		in+=n;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 		if(ctx->buf_len >= OK_BLOCK_SIZE+ OK_BLOCK_BLOCK)
3340Sstevel@tonic-gate 			{
3350Sstevel@tonic-gate 			block_out(b);
3360Sstevel@tonic-gate 			}
3370Sstevel@tonic-gate 	}while(inl > 0);
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
3400Sstevel@tonic-gate 	BIO_copy_next_retry(b);
3410Sstevel@tonic-gate 	return(ret);
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 
ok_ctrl(BIO * b,int cmd,long num,void * ptr)3440Sstevel@tonic-gate static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
3450Sstevel@tonic-gate 	{
3460Sstevel@tonic-gate 	BIO_OK_CTX *ctx;
3470Sstevel@tonic-gate 	EVP_MD *md;
3480Sstevel@tonic-gate 	const EVP_MD **ppmd;
3490Sstevel@tonic-gate 	long ret=1;
3500Sstevel@tonic-gate 	int i;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	ctx=b->ptr;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	switch (cmd)
3550Sstevel@tonic-gate 		{
3560Sstevel@tonic-gate 	case BIO_CTRL_RESET:
3570Sstevel@tonic-gate 		ctx->buf_len=0;
3580Sstevel@tonic-gate 		ctx->buf_off=0;
3590Sstevel@tonic-gate 		ctx->buf_len_save=0;
3600Sstevel@tonic-gate 		ctx->buf_off_save=0;
3610Sstevel@tonic-gate 		ctx->cont=1;
3620Sstevel@tonic-gate 		ctx->finished=0;
3630Sstevel@tonic-gate 		ctx->blockout= 0;
3640Sstevel@tonic-gate 		ctx->sigio=1;
3650Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3660Sstevel@tonic-gate 		break;
3670Sstevel@tonic-gate 	case BIO_CTRL_EOF:	/* More to read */
3680Sstevel@tonic-gate 		if (ctx->cont <= 0)
3690Sstevel@tonic-gate 			ret=1;
3700Sstevel@tonic-gate 		else
3710Sstevel@tonic-gate 			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3720Sstevel@tonic-gate 		break;
3730Sstevel@tonic-gate 	case BIO_CTRL_PENDING: /* More to read in buffer */
3740Sstevel@tonic-gate 	case BIO_CTRL_WPENDING: /* More to read in buffer */
3750Sstevel@tonic-gate 		ret=ctx->blockout ? ctx->buf_len-ctx->buf_off : 0;
3760Sstevel@tonic-gate 		if (ret <= 0)
3770Sstevel@tonic-gate 			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
3780Sstevel@tonic-gate 		break;
3790Sstevel@tonic-gate 	case BIO_CTRL_FLUSH:
3800Sstevel@tonic-gate 		/* do a final write */
3810Sstevel@tonic-gate 		if(ctx->blockout == 0)
3820Sstevel@tonic-gate 			block_out(b);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 		while (ctx->blockout)
3850Sstevel@tonic-gate 			{
3860Sstevel@tonic-gate 			i=ok_write(b,NULL,0);
3870Sstevel@tonic-gate 			if (i < 0)
3880Sstevel@tonic-gate 				{
3890Sstevel@tonic-gate 				ret=i;
3900Sstevel@tonic-gate 				break;
3910Sstevel@tonic-gate 				}
3920Sstevel@tonic-gate 			}
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 		ctx->finished=1;
3950Sstevel@tonic-gate 		ctx->buf_off=ctx->buf_len=0;
3960Sstevel@tonic-gate 		ctx->cont=(int)ret;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 		/* Finally flush the underlying BIO */
3990Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
4000Sstevel@tonic-gate 		break;
4010Sstevel@tonic-gate 	case BIO_C_DO_STATE_MACHINE:
4020Sstevel@tonic-gate 		BIO_clear_retry_flags(b);
4030Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
4040Sstevel@tonic-gate 		BIO_copy_next_retry(b);
4050Sstevel@tonic-gate 		break;
4060Sstevel@tonic-gate 	case BIO_CTRL_INFO:
4070Sstevel@tonic-gate 		ret=(long)ctx->cont;
4080Sstevel@tonic-gate 		break;
4090Sstevel@tonic-gate 	case BIO_C_SET_MD:
4100Sstevel@tonic-gate 		md=ptr;
4110Sstevel@tonic-gate 		EVP_DigestInit_ex(&ctx->md, md, NULL);
4120Sstevel@tonic-gate 		b->init=1;
4130Sstevel@tonic-gate 		break;
4140Sstevel@tonic-gate 	case BIO_C_GET_MD:
4150Sstevel@tonic-gate 		if (b->init)
4160Sstevel@tonic-gate 			{
4170Sstevel@tonic-gate 			ppmd=ptr;
4180Sstevel@tonic-gate 			*ppmd=ctx->md.digest;
4190Sstevel@tonic-gate 			}
4200Sstevel@tonic-gate 		else
4210Sstevel@tonic-gate 			ret=0;
4220Sstevel@tonic-gate 		break;
4230Sstevel@tonic-gate 	default:
4240Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
4250Sstevel@tonic-gate 		break;
4260Sstevel@tonic-gate 		}
4270Sstevel@tonic-gate 	return(ret);
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 
ok_callback_ctrl(BIO * b,int cmd,bio_info_cb * fp)4300Sstevel@tonic-gate static long ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
4310Sstevel@tonic-gate 	{
4320Sstevel@tonic-gate 	long ret=1;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	if (b->next_bio == NULL) return(0);
4350Sstevel@tonic-gate 	switch (cmd)
4360Sstevel@tonic-gate 		{
4370Sstevel@tonic-gate 	default:
4380Sstevel@tonic-gate 		ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
4390Sstevel@tonic-gate 		break;
4400Sstevel@tonic-gate 		}
4410Sstevel@tonic-gate 	return(ret);
4420Sstevel@tonic-gate 	}
4430Sstevel@tonic-gate 
longswap(void * _ptr,size_t len)444*2139Sjp161948 static void longswap(void *_ptr, size_t len)
445*2139Sjp161948 {	const union { long one; char little; } is_endian = {1};
4460Sstevel@tonic-gate 
447*2139Sjp161948 	if (is_endian.little) {
448*2139Sjp161948 		size_t i;
449*2139Sjp161948 		unsigned char *p=_ptr,c;
450*2139Sjp161948 
451*2139Sjp161948 		for(i= 0;i < len;i+= 4) {
452*2139Sjp161948 			c=p[0],p[0]=p[3],p[3]=c;
453*2139Sjp161948 			c=p[1],p[1]=p[2],p[2]=c;
454*2139Sjp161948 		}
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate 
sig_out(BIO * b)4580Sstevel@tonic-gate static void sig_out(BIO* b)
4590Sstevel@tonic-gate 	{
4600Sstevel@tonic-gate 	BIO_OK_CTX *ctx;
4610Sstevel@tonic-gate 	EVP_MD_CTX *md;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	ctx=b->ptr;
4640Sstevel@tonic-gate 	md=&ctx->md;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	if(ctx->buf_len+ 2* md->digest->md_size > OK_BLOCK_SIZE) return;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	EVP_DigestInit_ex(md, md->digest, NULL);
4690Sstevel@tonic-gate 	/* FIXME: there's absolutely no guarantee this makes any sense at all,
4700Sstevel@tonic-gate 	 * particularly now EVP_MD_CTX has been restructured.
4710Sstevel@tonic-gate 	 */
4720Sstevel@tonic-gate 	RAND_pseudo_bytes(md->md_data, md->digest->md_size);
4730Sstevel@tonic-gate 	memcpy(&(ctx->buf[ctx->buf_len]), md->md_data, md->digest->md_size);
4740Sstevel@tonic-gate 	longswap(&(ctx->buf[ctx->buf_len]), md->digest->md_size);
4750Sstevel@tonic-gate 	ctx->buf_len+= md->digest->md_size;
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN));
4780Sstevel@tonic-gate 	EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL);
4790Sstevel@tonic-gate 	ctx->buf_len+= md->digest->md_size;
4800Sstevel@tonic-gate 	ctx->blockout= 1;
4810Sstevel@tonic-gate 	ctx->sigio= 0;
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 
sig_in(BIO * b)4840Sstevel@tonic-gate static void sig_in(BIO* b)
4850Sstevel@tonic-gate 	{
4860Sstevel@tonic-gate 	BIO_OK_CTX *ctx;
4870Sstevel@tonic-gate 	EVP_MD_CTX *md;
4880Sstevel@tonic-gate 	unsigned char tmp[EVP_MAX_MD_SIZE];
4890Sstevel@tonic-gate 	int ret= 0;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	ctx=b->ptr;
4920Sstevel@tonic-gate 	md=&ctx->md;
4930Sstevel@tonic-gate 
494*2139Sjp161948 	if((int)(ctx->buf_len-ctx->buf_off) < 2*md->digest->md_size) return;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	EVP_DigestInit_ex(md, md->digest, NULL);
4970Sstevel@tonic-gate 	memcpy(md->md_data, &(ctx->buf[ctx->buf_off]), md->digest->md_size);
4980Sstevel@tonic-gate 	longswap(md->md_data, md->digest->md_size);
4990Sstevel@tonic-gate 	ctx->buf_off+= md->digest->md_size;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN));
5020Sstevel@tonic-gate 	EVP_DigestFinal_ex(md, tmp, NULL);
5030Sstevel@tonic-gate 	ret= memcmp(&(ctx->buf[ctx->buf_off]), tmp, md->digest->md_size) == 0;
5040Sstevel@tonic-gate 	ctx->buf_off+= md->digest->md_size;
5050Sstevel@tonic-gate 	if(ret == 1)
5060Sstevel@tonic-gate 		{
5070Sstevel@tonic-gate 		ctx->sigio= 0;
5080Sstevel@tonic-gate 		if(ctx->buf_len != ctx->buf_off)
5090Sstevel@tonic-gate 			{
5100Sstevel@tonic-gate 			memmove(ctx->buf, &(ctx->buf[ctx->buf_off]), ctx->buf_len- ctx->buf_off);
5110Sstevel@tonic-gate 			}
5120Sstevel@tonic-gate 		ctx->buf_len-= ctx->buf_off;
5130Sstevel@tonic-gate 		ctx->buf_off= 0;
5140Sstevel@tonic-gate 		}
5150Sstevel@tonic-gate 	else
5160Sstevel@tonic-gate 		{
5170Sstevel@tonic-gate 		ctx->cont= 0;
5180Sstevel@tonic-gate 		}
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
block_out(BIO * b)5210Sstevel@tonic-gate static void block_out(BIO* b)
5220Sstevel@tonic-gate 	{
5230Sstevel@tonic-gate 	BIO_OK_CTX *ctx;
5240Sstevel@tonic-gate 	EVP_MD_CTX *md;
5250Sstevel@tonic-gate 	unsigned long tl;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	ctx=b->ptr;
5280Sstevel@tonic-gate 	md=&ctx->md;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	tl= ctx->buf_len- OK_BLOCK_BLOCK;
531*2139Sjp161948 	ctx->buf[0]=(unsigned char)(tl>>24);
532*2139Sjp161948 	ctx->buf[1]=(unsigned char)(tl>>16);
533*2139Sjp161948 	ctx->buf[2]=(unsigned char)(tl>>8);
534*2139Sjp161948 	ctx->buf[3]=(unsigned char)(tl);
5350Sstevel@tonic-gate 	EVP_DigestUpdate(md, (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl);
5360Sstevel@tonic-gate 	EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL);
5370Sstevel@tonic-gate 	ctx->buf_len+= md->digest->md_size;
5380Sstevel@tonic-gate 	ctx->blockout= 1;
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 
block_in(BIO * b)5410Sstevel@tonic-gate static void block_in(BIO* b)
5420Sstevel@tonic-gate 	{
5430Sstevel@tonic-gate 	BIO_OK_CTX *ctx;
5440Sstevel@tonic-gate 	EVP_MD_CTX *md;
545*2139Sjp161948 	unsigned long tl= 0;
5460Sstevel@tonic-gate 	unsigned char tmp[EVP_MAX_MD_SIZE];
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	ctx=b->ptr;
5490Sstevel@tonic-gate 	md=&ctx->md;
5500Sstevel@tonic-gate 
551*2139Sjp161948 	assert(sizeof(tl)>=OK_BLOCK_BLOCK);	/* always true */
552*2139Sjp161948 	tl =ctx->buf[0]; tl<<=8;
553*2139Sjp161948 	tl|=ctx->buf[1]; tl<<=8;
554*2139Sjp161948 	tl|=ctx->buf[2]; tl<<=8;
555*2139Sjp161948 	tl|=ctx->buf[3];
556*2139Sjp161948 
5570Sstevel@tonic-gate 	if (ctx->buf_len < tl+ OK_BLOCK_BLOCK+ md->digest->md_size) return;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	EVP_DigestUpdate(md, (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl);
5600Sstevel@tonic-gate 	EVP_DigestFinal_ex(md, tmp, NULL);
5610Sstevel@tonic-gate 	if(memcmp(&(ctx->buf[tl+ OK_BLOCK_BLOCK]), tmp, md->digest->md_size) == 0)
5620Sstevel@tonic-gate 		{
5630Sstevel@tonic-gate 		/* there might be parts from next block lurking around ! */
5640Sstevel@tonic-gate 		ctx->buf_off_save= tl+ OK_BLOCK_BLOCK+ md->digest->md_size;
5650Sstevel@tonic-gate 		ctx->buf_len_save= ctx->buf_len;
5660Sstevel@tonic-gate 		ctx->buf_off= OK_BLOCK_BLOCK;
5670Sstevel@tonic-gate 		ctx->buf_len= tl+ OK_BLOCK_BLOCK;
5680Sstevel@tonic-gate 		ctx->blockout= 1;
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 	else
5710Sstevel@tonic-gate 		{
5720Sstevel@tonic-gate 		ctx->cont= 0;
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
576