xref: /onnv-gate/usr/src/common/openssl/crypto/evp/bio_md.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/evp/bio_md.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 /* BIO_put and BIO_get both add to the digest,
660Sstevel@tonic-gate  * BIO_gets returns the digest */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static int md_write(BIO *h, char const *buf, int num);
690Sstevel@tonic-gate static int md_read(BIO *h, char *buf, int size);
700Sstevel@tonic-gate /*static int md_puts(BIO *h, const char *str); */
710Sstevel@tonic-gate static int md_gets(BIO *h, char *str, int size);
720Sstevel@tonic-gate static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
730Sstevel@tonic-gate static int md_new(BIO *h);
740Sstevel@tonic-gate static int md_free(BIO *data);
750Sstevel@tonic-gate static long md_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static BIO_METHOD methods_md=
780Sstevel@tonic-gate 	{
790Sstevel@tonic-gate 	BIO_TYPE_MD,"message digest",
800Sstevel@tonic-gate 	md_write,
810Sstevel@tonic-gate 	md_read,
820Sstevel@tonic-gate 	NULL, /* md_puts, */
830Sstevel@tonic-gate 	md_gets,
840Sstevel@tonic-gate 	md_ctrl,
850Sstevel@tonic-gate 	md_new,
860Sstevel@tonic-gate 	md_free,
870Sstevel@tonic-gate 	md_callback_ctrl,
880Sstevel@tonic-gate 	};
890Sstevel@tonic-gate 
BIO_f_md(void)900Sstevel@tonic-gate BIO_METHOD *BIO_f_md(void)
910Sstevel@tonic-gate 	{
920Sstevel@tonic-gate 	return(&methods_md);
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 
md_new(BIO * bi)950Sstevel@tonic-gate static int md_new(BIO *bi)
960Sstevel@tonic-gate 	{
970Sstevel@tonic-gate 	EVP_MD_CTX *ctx;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	ctx=EVP_MD_CTX_create();
1000Sstevel@tonic-gate 	if (ctx == NULL) return(0);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	bi->init=0;
1030Sstevel@tonic-gate 	bi->ptr=(char *)ctx;
1040Sstevel@tonic-gate 	bi->flags=0;
1050Sstevel@tonic-gate 	return(1);
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
md_free(BIO * a)1080Sstevel@tonic-gate static int md_free(BIO *a)
1090Sstevel@tonic-gate 	{
1100Sstevel@tonic-gate 	if (a == NULL) return(0);
1110Sstevel@tonic-gate 	EVP_MD_CTX_destroy(a->ptr);
1120Sstevel@tonic-gate 	a->ptr=NULL;
1130Sstevel@tonic-gate 	a->init=0;
1140Sstevel@tonic-gate 	a->flags=0;
1150Sstevel@tonic-gate 	return(1);
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
md_read(BIO * b,char * out,int outl)1180Sstevel@tonic-gate static int md_read(BIO *b, char *out, int outl)
1190Sstevel@tonic-gate 	{
1200Sstevel@tonic-gate 	int ret=0;
1210Sstevel@tonic-gate 	EVP_MD_CTX *ctx;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	if (out == NULL) return(0);
1240Sstevel@tonic-gate 	ctx=b->ptr;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	ret=BIO_read(b->next_bio,out,outl);
1290Sstevel@tonic-gate 	if (b->init)
1300Sstevel@tonic-gate 		{
1310Sstevel@tonic-gate 		if (ret > 0)
1320Sstevel@tonic-gate 			{
1330Sstevel@tonic-gate 			EVP_DigestUpdate(ctx,(unsigned char *)out,
1340Sstevel@tonic-gate 				(unsigned int)ret);
1350Sstevel@tonic-gate 			}
1360Sstevel@tonic-gate 		}
1370Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
1380Sstevel@tonic-gate 	BIO_copy_next_retry(b);
1390Sstevel@tonic-gate 	return(ret);
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 
md_write(BIO * b,const char * in,int inl)1420Sstevel@tonic-gate static int md_write(BIO *b, const char *in, int inl)
1430Sstevel@tonic-gate 	{
1440Sstevel@tonic-gate 	int ret=0;
1450Sstevel@tonic-gate 	EVP_MD_CTX *ctx;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if ((in == NULL) || (inl <= 0)) return(0);
1480Sstevel@tonic-gate 	ctx=b->ptr;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if ((ctx != NULL) && (b->next_bio != NULL))
1510Sstevel@tonic-gate 		ret=BIO_write(b->next_bio,in,inl);
1520Sstevel@tonic-gate 	if (b->init)
1530Sstevel@tonic-gate 		{
1540Sstevel@tonic-gate 		if (ret > 0)
1550Sstevel@tonic-gate 			{
156*2139Sjp161948 			EVP_DigestUpdate(ctx,(const unsigned char *)in,
1570Sstevel@tonic-gate 				(unsigned int)ret);
1580Sstevel@tonic-gate 			}
1590Sstevel@tonic-gate 		}
1600Sstevel@tonic-gate 	BIO_clear_retry_flags(b);
1610Sstevel@tonic-gate 	BIO_copy_next_retry(b);
1620Sstevel@tonic-gate 	return(ret);
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 
md_ctrl(BIO * b,int cmd,long num,void * ptr)1650Sstevel@tonic-gate static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
1660Sstevel@tonic-gate 	{
1670Sstevel@tonic-gate 	EVP_MD_CTX *ctx,*dctx,**pctx;
1680Sstevel@tonic-gate 	const EVP_MD **ppmd;
1690Sstevel@tonic-gate 	EVP_MD *md;
1700Sstevel@tonic-gate 	long ret=1;
1710Sstevel@tonic-gate 	BIO *dbio;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	ctx=b->ptr;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	switch (cmd)
1760Sstevel@tonic-gate 		{
1770Sstevel@tonic-gate 	case BIO_CTRL_RESET:
1780Sstevel@tonic-gate 		if (b->init)
179*2139Sjp161948 			ret = EVP_DigestInit_ex(ctx,ctx->digest, NULL);
1800Sstevel@tonic-gate 		else
1810Sstevel@tonic-gate 			ret=0;
182*2139Sjp161948 		if (ret > 0)
183*2139Sjp161948 			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
1840Sstevel@tonic-gate 		break;
1850Sstevel@tonic-gate 	case BIO_C_GET_MD:
1860Sstevel@tonic-gate 		if (b->init)
1870Sstevel@tonic-gate 			{
1880Sstevel@tonic-gate 			ppmd=ptr;
1890Sstevel@tonic-gate 			*ppmd=ctx->digest;
1900Sstevel@tonic-gate 			}
1910Sstevel@tonic-gate 		else
1920Sstevel@tonic-gate 			ret=0;
1930Sstevel@tonic-gate 		break;
1940Sstevel@tonic-gate 	case BIO_C_GET_MD_CTX:
1950Sstevel@tonic-gate 		if (b->init)
1960Sstevel@tonic-gate 			{
1970Sstevel@tonic-gate 			pctx=ptr;
1980Sstevel@tonic-gate 			*pctx=ctx;
1990Sstevel@tonic-gate 			}
2000Sstevel@tonic-gate 		else
2010Sstevel@tonic-gate 			ret=0;
2020Sstevel@tonic-gate 		break;
2030Sstevel@tonic-gate 	case BIO_C_DO_STATE_MACHINE:
2040Sstevel@tonic-gate 		BIO_clear_retry_flags(b);
2050Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
2060Sstevel@tonic-gate 		BIO_copy_next_retry(b);
2070Sstevel@tonic-gate 		break;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	case BIO_C_SET_MD:
2100Sstevel@tonic-gate 		md=ptr;
211*2139Sjp161948 		ret = EVP_DigestInit_ex(ctx,md, NULL);
212*2139Sjp161948 		if (ret > 0)
213*2139Sjp161948 			b->init=1;
2140Sstevel@tonic-gate 		break;
2150Sstevel@tonic-gate 	case BIO_CTRL_DUP:
2160Sstevel@tonic-gate 		dbio=ptr;
2170Sstevel@tonic-gate 		dctx=dbio->ptr;
2180Sstevel@tonic-gate 		EVP_MD_CTX_copy_ex(dctx,ctx);
2190Sstevel@tonic-gate 		b->init=1;
2200Sstevel@tonic-gate 		break;
2210Sstevel@tonic-gate 	default:
2220Sstevel@tonic-gate 		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
2230Sstevel@tonic-gate 		break;
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	return(ret);
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
md_callback_ctrl(BIO * b,int cmd,bio_info_cb * fp)2280Sstevel@tonic-gate static long md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
2290Sstevel@tonic-gate 	{
2300Sstevel@tonic-gate 	long ret=1;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (b->next_bio == NULL) return(0);
2330Sstevel@tonic-gate 	switch (cmd)
2340Sstevel@tonic-gate 		{
2350Sstevel@tonic-gate 	default:
2360Sstevel@tonic-gate 		ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
2370Sstevel@tonic-gate 		break;
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 	return(ret);
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
md_gets(BIO * bp,char * buf,int size)2420Sstevel@tonic-gate static int md_gets(BIO *bp, char *buf, int size)
2430Sstevel@tonic-gate 	{
2440Sstevel@tonic-gate 	EVP_MD_CTX *ctx;
2450Sstevel@tonic-gate 	unsigned int ret;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	ctx=bp->ptr;
2490Sstevel@tonic-gate 	if (size < ctx->digest->md_size)
2500Sstevel@tonic-gate 		return(0);
2510Sstevel@tonic-gate 	EVP_DigestFinal_ex(ctx,(unsigned char *)buf,&ret);
2520Sstevel@tonic-gate 	return((int)ret);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate /*
2560Sstevel@tonic-gate static int md_puts(bp,str)
2570Sstevel@tonic-gate BIO *bp;
2580Sstevel@tonic-gate char *str;
2590Sstevel@tonic-gate 	{
2600Sstevel@tonic-gate 	return(-1);
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate */
2630Sstevel@tonic-gate 
264