os/ossrv/ssl/libcrypto/src/crypto/asn1/a_d2i_fp.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/asn1/a_d2i_fp.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,260 @@
     1.4 +/* crypto/asn1/a_d2i_fp.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 +#include <stdio.h>
    1.63 +#include "cryptlib.h"
    1.64 +#include <openssl/buffer.h>
    1.65 +#include <openssl/asn1_mac.h>
    1.66 +
    1.67 +static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb);
    1.68 +
    1.69 +#ifndef NO_OLD_ASN1
    1.70 +#ifndef OPENSSL_NO_FP_API
    1.71 +
    1.72 +EXPORT_C void *ASN1_d2i_fp(void *(*xnew)(void), d2i_of_void *d2i, FILE *in, void **x)
    1.73 +        {
    1.74 +        BIO *b;
    1.75 +        void *ret;
    1.76 +
    1.77 +        if ((b=BIO_new(BIO_s_file())) == NULL)
    1.78 +		{
    1.79 +		ASN1err(ASN1_F_ASN1_D2I_FP,ERR_R_BUF_LIB);
    1.80 +                return(NULL);
    1.81 +		}
    1.82 +        BIO_set_fp(b,in,BIO_NOCLOSE);
    1.83 +        ret=ASN1_d2i_bio(xnew,d2i,b,x);
    1.84 +        BIO_free(b);
    1.85 +        return(ret);
    1.86 +        }
    1.87 +#endif
    1.88 +
    1.89 +EXPORT_C void *ASN1_d2i_bio(void *(*xnew)(void), d2i_of_void *d2i, BIO *in, void **x)
    1.90 +	{
    1.91 +	BUF_MEM *b = NULL;
    1.92 +	const unsigned char *p;
    1.93 +	void *ret=NULL;
    1.94 +	int len;
    1.95 +
    1.96 +	len = asn1_d2i_read_bio(in, &b);
    1.97 +	if(len < 0) goto err;
    1.98 +
    1.99 +	p=(unsigned char *)b->data;
   1.100 +	ret=d2i(x,&p,len);
   1.101 +err:
   1.102 +	if (b != NULL) BUF_MEM_free(b);
   1.103 +	return(ret);
   1.104 +	}
   1.105 +
   1.106 +#endif
   1.107 +
   1.108 +EXPORT_C void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x)
   1.109 +	{
   1.110 +	BUF_MEM *b = NULL;
   1.111 +	const unsigned char *p;
   1.112 +	void *ret=NULL;
   1.113 +	int len;
   1.114 +
   1.115 +	len = asn1_d2i_read_bio(in, &b);
   1.116 +	if(len < 0) goto err;
   1.117 +
   1.118 +	p=(const unsigned char *)b->data;
   1.119 +	ret=ASN1_item_d2i(x,&p,len, it);
   1.120 +err:
   1.121 +	if (b != NULL) BUF_MEM_free(b);
   1.122 +	return(ret);
   1.123 +	}
   1.124 +
   1.125 +#ifndef OPENSSL_NO_FP_API
   1.126 +EXPORT_C void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
   1.127 +        {
   1.128 +        BIO *b;
   1.129 +        char *ret;
   1.130 +
   1.131 +        if ((b=BIO_new(BIO_s_file())) == NULL)
   1.132 +		{
   1.133 +		ASN1err(ASN1_F_ASN1_ITEM_D2I_FP,ERR_R_BUF_LIB);
   1.134 +                return(NULL);
   1.135 +		}
   1.136 +        BIO_set_fp(b,in,BIO_NOCLOSE);
   1.137 +        ret=ASN1_item_d2i_bio(it,b,x);
   1.138 +        BIO_free(b);
   1.139 +        return(ret);
   1.140 +        }
   1.141 +#endif
   1.142 +
   1.143 +#define HEADER_SIZE   8
   1.144 +static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
   1.145 +	{
   1.146 +	BUF_MEM *b;
   1.147 +	unsigned char *p;
   1.148 +	int i;
   1.149 +	int ret=-1;
   1.150 +	ASN1_const_CTX c;
   1.151 +	int want=HEADER_SIZE;
   1.152 +	int eos=0;
   1.153 +#if defined(__GNUC__) && defined(__ia64)
   1.154 +	/* pathetic compiler bug in all known versions as of Nov. 2002 */
   1.155 +	long off=0;
   1.156 +#else
   1.157 +	int off=0;
   1.158 +#endif
   1.159 +	int len=0;
   1.160 +
   1.161 +	b=BUF_MEM_new();
   1.162 +	if (b == NULL)
   1.163 +		{
   1.164 +		ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ERR_R_MALLOC_FAILURE);
   1.165 +		return -1;
   1.166 +		}
   1.167 +
   1.168 +	ERR_clear_error();
   1.169 +	for (;;)
   1.170 +		{
   1.171 +		if (want >= (len-off))
   1.172 +			{
   1.173 +			want-=(len-off);
   1.174 +
   1.175 +			if (!BUF_MEM_grow_clean(b,len+want))
   1.176 +				{
   1.177 +				ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ERR_R_MALLOC_FAILURE);
   1.178 +				goto err;
   1.179 +				}
   1.180 +			i=BIO_read(in,&(b->data[len]),want);
   1.181 +			if ((i < 0) && ((len-off) == 0))
   1.182 +				{
   1.183 +				ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_NOT_ENOUGH_DATA);
   1.184 +				goto err;
   1.185 +				}
   1.186 +			if (i > 0)
   1.187 +				len+=i;
   1.188 +			}
   1.189 +		/* else data already loaded */
   1.190 +
   1.191 +		p=(unsigned char *)&(b->data[off]);
   1.192 +		c.p=p;
   1.193 +		c.inf=ASN1_get_object(&(c.p),&(c.slen),&(c.tag),&(c.xclass),
   1.194 +			len-off);
   1.195 +		if (c.inf & 0x80)
   1.196 +			{
   1.197 +			unsigned long e;
   1.198 +
   1.199 +			e=ERR_GET_REASON(ERR_peek_error());
   1.200 +			if (e != ASN1_R_TOO_LONG)
   1.201 +				goto err;
   1.202 +			else
   1.203 +				ERR_clear_error(); /* clear error */
   1.204 +			}
   1.205 +		i=c.p-p;/* header length */
   1.206 +		off+=i;	/* end of data */
   1.207 +
   1.208 +		if (c.inf & 1)
   1.209 +			{
   1.210 +			/* no data body so go round again */
   1.211 +			eos++;
   1.212 +			want=HEADER_SIZE;
   1.213 +			}
   1.214 +		else if (eos && (c.slen == 0) && (c.tag == V_ASN1_EOC))
   1.215 +			{
   1.216 +			/* eos value, so go back and read another header */
   1.217 +			eos--;
   1.218 +			if (eos <= 0)
   1.219 +				break;
   1.220 +			else
   1.221 +				want=HEADER_SIZE;
   1.222 +			}
   1.223 +		else 
   1.224 +			{
   1.225 +			/* suck in c.slen bytes of data */
   1.226 +			want=(int)c.slen;
   1.227 +			if (want > (len-off))
   1.228 +				{
   1.229 +				want-=(len-off);
   1.230 +				if (!BUF_MEM_grow_clean(b,len+want))
   1.231 +					{
   1.232 +					ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ERR_R_MALLOC_FAILURE);
   1.233 +					goto err;
   1.234 +					}
   1.235 +				while (want > 0)
   1.236 +					{
   1.237 +					i=BIO_read(in,&(b->data[len]),want);
   1.238 +					if (i <= 0)
   1.239 +						{
   1.240 +						ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
   1.241 +						    ASN1_R_NOT_ENOUGH_DATA);
   1.242 +						goto err;
   1.243 +						}
   1.244 +					len+=i;
   1.245 +					want -= i;
   1.246 +					}
   1.247 +				}
   1.248 +			off+=(int)c.slen;
   1.249 +			if (eos <= 0)
   1.250 +				{
   1.251 +				break;
   1.252 +				}
   1.253 +			else
   1.254 +				want=HEADER_SIZE;
   1.255 +			}
   1.256 +		}
   1.257 +
   1.258 +	*pb = b;
   1.259 +	return off;
   1.260 +err:
   1.261 +	if (b != NULL) BUF_MEM_free(b);
   1.262 +	return(ret);
   1.263 +	}