os/ossrv/ssl/libcrypto/src/crypto/evp/bio_enc.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/evp/bio_enc.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,451 @@
     1.4 +/* crypto/evp/bio_enc.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +/*
    1.62 + © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    1.63 + */
    1.64 + 
    1.65 +#include <stdio.h>
    1.66 +#include <errno.h>
    1.67 +#include "cryptlib.h"
    1.68 +#include <openssl/buffer.h>
    1.69 +#include <openssl/evp.h>
    1.70 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
    1.71 +#include "libcrypto_wsd_macros.h"
    1.72 +#include "libcrypto_wsd.h"
    1.73 +#endif
    1.74 +
    1.75 +static int enc_write(BIO *h, const char *buf, int num);
    1.76 +static int enc_read(BIO *h, char *buf, int size);
    1.77 +/*static int enc_puts(BIO *h, const char *str); */
    1.78 +/*static int enc_gets(BIO *h, char *str, int size); */
    1.79 +static long enc_ctrl(BIO *h, int cmd, long arg1, void *arg2);
    1.80 +static int enc_new(BIO *h);
    1.81 +static int enc_free(BIO *data);
    1.82 +static long enc_callback_ctrl(BIO *h, int cmd, bio_info_cb *fps);
    1.83 +#define ENC_BLOCK_SIZE	(1024*4)
    1.84 +#define BUF_OFFSET	(EVP_MAX_BLOCK_LENGTH*2)
    1.85 +
    1.86 +typedef struct enc_struct
    1.87 +	{
    1.88 +	int buf_len;
    1.89 +	int buf_off;
    1.90 +	int cont;		/* <= 0 when finished */
    1.91 +	int finished;
    1.92 +	int ok;			/* bad decrypt */
    1.93 +	EVP_CIPHER_CTX cipher;
    1.94 +	/* buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate
    1.95 +	 * can return up to a block more data than is presented to it
    1.96 +	 */
    1.97 +	char buf[ENC_BLOCK_SIZE+BUF_OFFSET+2];
    1.98 +	} BIO_ENC_CTX;
    1.99 +
   1.100 +#ifndef EMULATOR
   1.101 +static BIO_METHOD methods_enc=
   1.102 +	{
   1.103 +	BIO_TYPE_CIPHER,"cipher",
   1.104 +	enc_write,
   1.105 +	enc_read,
   1.106 +	NULL, /* enc_puts, */
   1.107 +	NULL, /* enc_gets, */
   1.108 +	enc_ctrl,
   1.109 +	enc_new,
   1.110 +	enc_free,
   1.111 +	enc_callback_ctrl,
   1.112 +	};
   1.113 +#else
   1.114 +GET_STATIC_VAR_FROM_TLS(methods_enc,bio_enc,BIO_METHOD)
   1.115 +#define methods_enc (*GET_WSD_VAR_NAME(methods_enc,bio_enc, s)())
   1.116 +const BIO_METHOD temp_s_methods_enc=
   1.117 +	{
   1.118 +	BIO_TYPE_CIPHER,"cipher",
   1.119 +	enc_write,
   1.120 +	enc_read,
   1.121 +	NULL, /* enc_puts, */
   1.122 +	NULL, /* enc_gets, */
   1.123 +	enc_ctrl,
   1.124 +	enc_new,
   1.125 +	enc_free,
   1.126 +	enc_callback_ctrl,
   1.127 +	};
   1.128 +
   1.129 +#endif	
   1.130 +
   1.131 +EXPORT_C BIO_METHOD *BIO_f_cipher(void)
   1.132 +	{
   1.133 +	return(&methods_enc);
   1.134 +	}
   1.135 +
   1.136 +static int enc_new(BIO *bi)
   1.137 +	{
   1.138 +	BIO_ENC_CTX *ctx;
   1.139 +
   1.140 +	ctx=(BIO_ENC_CTX *)OPENSSL_malloc(sizeof(BIO_ENC_CTX));
   1.141 +	if (ctx == NULL) return(0);
   1.142 +	EVP_CIPHER_CTX_init(&ctx->cipher);
   1.143 +
   1.144 +	ctx->buf_len=0;
   1.145 +	ctx->buf_off=0;
   1.146 +	ctx->cont=1;
   1.147 +	ctx->finished=0;
   1.148 +	ctx->ok=1;
   1.149 +
   1.150 +	bi->init=0;
   1.151 +	bi->ptr=(char *)ctx;
   1.152 +	bi->flags=0;
   1.153 +	return(1);
   1.154 +	}
   1.155 +
   1.156 +static int enc_free(BIO *a)
   1.157 +	{
   1.158 +	BIO_ENC_CTX *b;
   1.159 +
   1.160 +	if (a == NULL) return(0);
   1.161 +	b=(BIO_ENC_CTX *)a->ptr;
   1.162 +	EVP_CIPHER_CTX_cleanup(&(b->cipher));
   1.163 +	OPENSSL_cleanse(a->ptr,sizeof(BIO_ENC_CTX));
   1.164 +	OPENSSL_free(a->ptr);
   1.165 +	a->ptr=NULL;
   1.166 +	a->init=0;
   1.167 +	a->flags=0;
   1.168 +	return(1);
   1.169 +	}
   1.170 +	
   1.171 +static int enc_read(BIO *b, char *out, int outl)
   1.172 +	{
   1.173 +	int ret=0,i;
   1.174 +	BIO_ENC_CTX *ctx;
   1.175 +
   1.176 +	if (out == NULL) return(0);
   1.177 +	ctx=(BIO_ENC_CTX *)b->ptr;
   1.178 +
   1.179 +	if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
   1.180 +
   1.181 +	/* First check if there are bytes decoded/encoded */
   1.182 +	if (ctx->buf_len > 0)
   1.183 +		{
   1.184 +		i=ctx->buf_len-ctx->buf_off;
   1.185 +		if (i > outl) i=outl;
   1.186 +		memcpy(out,&(ctx->buf[ctx->buf_off]),i);
   1.187 +		ret=i;
   1.188 +		out+=i;
   1.189 +		outl-=i;
   1.190 +		ctx->buf_off+=i;
   1.191 +		if (ctx->buf_len == ctx->buf_off)
   1.192 +			{
   1.193 +			ctx->buf_len=0;
   1.194 +			ctx->buf_off=0;
   1.195 +			}
   1.196 +		}
   1.197 +
   1.198 +	/* At this point, we have room of outl bytes and an empty
   1.199 +	 * buffer, so we should read in some more. */
   1.200 +
   1.201 +	while (outl > 0)
   1.202 +		{
   1.203 +		if (ctx->cont <= 0) break;
   1.204 +
   1.205 +		/* read in at IV offset, read the EVP_Cipher
   1.206 +		 * documentation about why */
   1.207 +		i=BIO_read(b->next_bio,&(ctx->buf[BUF_OFFSET]),ENC_BLOCK_SIZE);
   1.208 +
   1.209 +		if (i <= 0)
   1.210 +			{
   1.211 +			/* Should be continue next time we are called? */
   1.212 +			if (!BIO_should_retry(b->next_bio))
   1.213 +				{
   1.214 +				ctx->cont=i;
   1.215 +				i=EVP_CipherFinal_ex(&(ctx->cipher),
   1.216 +					(unsigned char *)ctx->buf,
   1.217 +					&(ctx->buf_len));
   1.218 +				ctx->ok=i;
   1.219 +				ctx->buf_off=0;
   1.220 +				}
   1.221 +			else 
   1.222 +				{
   1.223 +				ret=(ret == 0)?i:ret;
   1.224 +				break;
   1.225 +				}
   1.226 +			}
   1.227 +		else
   1.228 +			{
   1.229 +			EVP_CipherUpdate(&(ctx->cipher),
   1.230 +				(unsigned char *)ctx->buf,&ctx->buf_len,
   1.231 +				(unsigned char *)&(ctx->buf[BUF_OFFSET]),i);
   1.232 +			ctx->cont=1;
   1.233 +			/* Note: it is possible for EVP_CipherUpdate to
   1.234 +			 * decrypt zero bytes because this is or looks like
   1.235 +			 * the final block: if this happens we should retry
   1.236 +			 * and either read more data or decrypt the final
   1.237 +			 * block
   1.238 +			 */
   1.239 +			if(ctx->buf_len == 0) continue;
   1.240 +			}
   1.241 +
   1.242 +		if (ctx->buf_len <= outl)
   1.243 +			i=ctx->buf_len;
   1.244 +		else
   1.245 +			i=outl;
   1.246 +		if (i <= 0) break;
   1.247 +		memcpy(out,ctx->buf,i);
   1.248 +		ret+=i;
   1.249 +		ctx->buf_off=i;
   1.250 +		outl-=i;
   1.251 +		out+=i;
   1.252 +		}
   1.253 +
   1.254 +	BIO_clear_retry_flags(b);
   1.255 +	BIO_copy_next_retry(b);
   1.256 +	return((ret == 0)?ctx->cont:ret);
   1.257 +	}
   1.258 +
   1.259 +static int enc_write(BIO *b, const char *in, int inl)
   1.260 +	{
   1.261 +	int ret=0,n,i;
   1.262 +	BIO_ENC_CTX *ctx;
   1.263 +
   1.264 +	ctx=(BIO_ENC_CTX *)b->ptr;
   1.265 +	ret=inl;
   1.266 +
   1.267 +	BIO_clear_retry_flags(b);
   1.268 +	n=ctx->buf_len-ctx->buf_off;
   1.269 +	while (n > 0)
   1.270 +		{
   1.271 +		i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
   1.272 +		if (i <= 0)
   1.273 +			{
   1.274 +			BIO_copy_next_retry(b);
   1.275 +			return(i);
   1.276 +			}
   1.277 +		ctx->buf_off+=i;
   1.278 +		n-=i;
   1.279 +		}
   1.280 +	/* at this point all pending data has been written */
   1.281 +
   1.282 +	if ((in == NULL) || (inl <= 0)) return(0);
   1.283 +
   1.284 +	ctx->buf_off=0;
   1.285 +	while (inl > 0)
   1.286 +		{
   1.287 +		n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
   1.288 +		EVP_CipherUpdate(&(ctx->cipher),
   1.289 +			(unsigned char *)ctx->buf,&ctx->buf_len,
   1.290 +			(unsigned char *)in,n);
   1.291 +		inl-=n;
   1.292 +		in+=n;
   1.293 +
   1.294 +		ctx->buf_off=0;
   1.295 +		n=ctx->buf_len;
   1.296 +		while (n > 0)
   1.297 +			{
   1.298 +			i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
   1.299 +			if (i <= 0)
   1.300 +				{
   1.301 +				BIO_copy_next_retry(b);
   1.302 +				return (ret == inl) ? i : ret - inl;
   1.303 +				}
   1.304 +			n-=i;
   1.305 +			ctx->buf_off+=i;
   1.306 +			}
   1.307 +		ctx->buf_len=0;
   1.308 +		ctx->buf_off=0;
   1.309 +		}
   1.310 +	BIO_copy_next_retry(b);
   1.311 +	return(ret);
   1.312 +	}
   1.313 +
   1.314 +static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
   1.315 +	{
   1.316 +	BIO *dbio;
   1.317 +	BIO_ENC_CTX *ctx,*dctx;
   1.318 +	long ret=1;
   1.319 +	int i;
   1.320 +	EVP_CIPHER_CTX **c_ctx;
   1.321 +
   1.322 +	ctx=(BIO_ENC_CTX *)b->ptr;
   1.323 +
   1.324 +	switch (cmd)
   1.325 +		{
   1.326 +	case BIO_CTRL_RESET:
   1.327 +		ctx->ok=1;
   1.328 +		ctx->finished=0;
   1.329 +		EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL,
   1.330 +			ctx->cipher.encrypt);
   1.331 +		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
   1.332 +		break;
   1.333 +	case BIO_CTRL_EOF:	/* More to read */
   1.334 +		if (ctx->cont <= 0)
   1.335 +			ret=1;
   1.336 +		else
   1.337 +			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
   1.338 +		break;
   1.339 +	case BIO_CTRL_WPENDING:
   1.340 +		ret=ctx->buf_len-ctx->buf_off;
   1.341 +		if (ret <= 0)
   1.342 +			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
   1.343 +		break;
   1.344 +	case BIO_CTRL_PENDING: /* More to read in buffer */
   1.345 +		ret=ctx->buf_len-ctx->buf_off;
   1.346 +		if (ret <= 0)
   1.347 +			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
   1.348 +		break;
   1.349 +	case BIO_CTRL_FLUSH:
   1.350 +		/* do a final write */
   1.351 +again:
   1.352 +		while (ctx->buf_len != ctx->buf_off)
   1.353 +			{
   1.354 +			i=enc_write(b,NULL,0);
   1.355 +			if (i < 0)
   1.356 +				return i;
   1.357 +			}
   1.358 +
   1.359 +		if (!ctx->finished)
   1.360 +			{
   1.361 +			ctx->finished=1;
   1.362 +			ctx->buf_off=0;
   1.363 +			ret=EVP_CipherFinal_ex(&(ctx->cipher),
   1.364 +				(unsigned char *)ctx->buf,
   1.365 +				&(ctx->buf_len));
   1.366 +			ctx->ok=(int)ret;
   1.367 +			if (ret <= 0) break;
   1.368 +
   1.369 +			/* push out the bytes */
   1.370 +			goto again;
   1.371 +			}
   1.372 +		
   1.373 +		/* Finally flush the underlying BIO */
   1.374 +		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
   1.375 +		break;
   1.376 +	case BIO_C_GET_CIPHER_STATUS:
   1.377 +		ret=(long)ctx->ok;
   1.378 +		break;
   1.379 +	case BIO_C_DO_STATE_MACHINE:
   1.380 +		BIO_clear_retry_flags(b);
   1.381 +		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
   1.382 +		BIO_copy_next_retry(b);
   1.383 +		break;
   1.384 +	case BIO_C_GET_CIPHER_CTX:
   1.385 +		c_ctx=(EVP_CIPHER_CTX **)ptr;
   1.386 +		(*c_ctx)= &(ctx->cipher);
   1.387 +		b->init=1;
   1.388 +		break;
   1.389 +	case BIO_CTRL_DUP:
   1.390 +		dbio=(BIO *)ptr;
   1.391 +		dctx=(BIO_ENC_CTX *)dbio->ptr;
   1.392 +		memcpy(&(dctx->cipher),&(ctx->cipher),sizeof(ctx->cipher));
   1.393 +		dbio->init=1;
   1.394 +		break;
   1.395 +	default:
   1.396 +		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
   1.397 +		break;
   1.398 +		}
   1.399 +	return(ret);
   1.400 +	}
   1.401 +
   1.402 +static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
   1.403 +	{
   1.404 +	long ret=1;
   1.405 +
   1.406 +	if (b->next_bio == NULL) return(0);
   1.407 +	switch (cmd)
   1.408 +		{
   1.409 +	default:
   1.410 +		ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
   1.411 +		break;
   1.412 +		}
   1.413 +	return(ret);
   1.414 +	}
   1.415 +
   1.416 +/*
   1.417 +void BIO_set_cipher_ctx(b,c)
   1.418 +BIO *b;
   1.419 +EVP_CIPHER_ctx *c;
   1.420 +	{
   1.421 +	if (b == NULL) return;
   1.422 +
   1.423 +	if ((b->callback != NULL) &&
   1.424 +		(b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
   1.425 +		return;
   1.426 +
   1.427 +	b->init=1;
   1.428 +	ctx=(BIO_ENC_CTX *)b->ptr;
   1.429 +	memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
   1.430 +	
   1.431 +	if (b->callback != NULL)
   1.432 +		b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
   1.433 +	}
   1.434 +*/
   1.435 +
   1.436 +EXPORT_C void BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
   1.437 +	     const unsigned char *i, int e)
   1.438 +	{
   1.439 +	BIO_ENC_CTX *ctx;
   1.440 +
   1.441 +	if (b == NULL) return;
   1.442 +
   1.443 +	if ((b->callback != NULL) &&
   1.444 +		(b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,0L) <= 0))
   1.445 +		return;
   1.446 +
   1.447 +	b->init=1;
   1.448 +	ctx=(BIO_ENC_CTX *)b->ptr;
   1.449 +	EVP_CipherInit_ex(&(ctx->cipher),c,NULL, k,i,e);
   1.450 +	
   1.451 +	if (b->callback != NULL)
   1.452 +		b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,1L);
   1.453 +	}
   1.454 +