10Sstevel@tonic-gate /* crypto/asn1/a_d2i_fp.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 "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/buffer.h>
620Sstevel@tonic-gate #include <openssl/asn1_mac.h>
630Sstevel@tonic-gate
640Sstevel@tonic-gate static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb);
650Sstevel@tonic-gate
660Sstevel@tonic-gate #ifndef NO_OLD_ASN1
670Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
680Sstevel@tonic-gate
ASN1_d2i_fp(void * (* xnew)(void),d2i_of_void * d2i,FILE * in,void ** x)69*2139Sjp161948 void *ASN1_d2i_fp(void *(*xnew)(void), d2i_of_void *d2i, FILE *in, void **x)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate BIO *b;
72*2139Sjp161948 void *ret;
730Sstevel@tonic-gate
740Sstevel@tonic-gate if ((b=BIO_new(BIO_s_file())) == NULL)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate ASN1err(ASN1_F_ASN1_D2I_FP,ERR_R_BUF_LIB);
770Sstevel@tonic-gate return(NULL);
780Sstevel@tonic-gate }
790Sstevel@tonic-gate BIO_set_fp(b,in,BIO_NOCLOSE);
800Sstevel@tonic-gate ret=ASN1_d2i_bio(xnew,d2i,b,x);
810Sstevel@tonic-gate BIO_free(b);
820Sstevel@tonic-gate return(ret);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate #endif
850Sstevel@tonic-gate
ASN1_d2i_bio(void * (* xnew)(void),d2i_of_void * d2i,BIO * in,void ** x)86*2139Sjp161948 void *ASN1_d2i_bio(void *(*xnew)(void), d2i_of_void *d2i, BIO *in, void **x)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate BUF_MEM *b = NULL;
89*2139Sjp161948 const unsigned char *p;
90*2139Sjp161948 void *ret=NULL;
910Sstevel@tonic-gate int len;
920Sstevel@tonic-gate
930Sstevel@tonic-gate len = asn1_d2i_read_bio(in, &b);
940Sstevel@tonic-gate if(len < 0) goto err;
950Sstevel@tonic-gate
960Sstevel@tonic-gate p=(unsigned char *)b->data;
970Sstevel@tonic-gate ret=d2i(x,&p,len);
980Sstevel@tonic-gate err:
990Sstevel@tonic-gate if (b != NULL) BUF_MEM_free(b);
1000Sstevel@tonic-gate return(ret);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate #endif
1040Sstevel@tonic-gate
ASN1_item_d2i_bio(const ASN1_ITEM * it,BIO * in,void * x)1050Sstevel@tonic-gate void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate BUF_MEM *b = NULL;
108*2139Sjp161948 const unsigned char *p;
1090Sstevel@tonic-gate void *ret=NULL;
1100Sstevel@tonic-gate int len;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate len = asn1_d2i_read_bio(in, &b);
1130Sstevel@tonic-gate if(len < 0) goto err;
1140Sstevel@tonic-gate
115*2139Sjp161948 p=(const unsigned char *)b->data;
1160Sstevel@tonic-gate ret=ASN1_item_d2i(x,&p,len, it);
1170Sstevel@tonic-gate err:
1180Sstevel@tonic-gate if (b != NULL) BUF_MEM_free(b);
1190Sstevel@tonic-gate return(ret);
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
ASN1_item_d2i_fp(const ASN1_ITEM * it,FILE * in,void * x)1230Sstevel@tonic-gate void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate BIO *b;
1260Sstevel@tonic-gate char *ret;
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate if ((b=BIO_new(BIO_s_file())) == NULL)
1290Sstevel@tonic-gate {
130*2139Sjp161948 ASN1err(ASN1_F_ASN1_ITEM_D2I_FP,ERR_R_BUF_LIB);
1310Sstevel@tonic-gate return(NULL);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate BIO_set_fp(b,in,BIO_NOCLOSE);
1340Sstevel@tonic-gate ret=ASN1_item_d2i_bio(it,b,x);
1350Sstevel@tonic-gate BIO_free(b);
1360Sstevel@tonic-gate return(ret);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate #endif
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate #define HEADER_SIZE 8
asn1_d2i_read_bio(BIO * in,BUF_MEM ** pb)1410Sstevel@tonic-gate static int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate BUF_MEM *b;
1440Sstevel@tonic-gate unsigned char *p;
1450Sstevel@tonic-gate int i;
1460Sstevel@tonic-gate int ret=-1;
147*2139Sjp161948 ASN1_const_CTX c;
1480Sstevel@tonic-gate int want=HEADER_SIZE;
1490Sstevel@tonic-gate int eos=0;
1500Sstevel@tonic-gate #if defined(__GNUC__) && defined(__ia64)
1510Sstevel@tonic-gate /* pathetic compiler bug in all known versions as of Nov. 2002 */
1520Sstevel@tonic-gate long off=0;
1530Sstevel@tonic-gate #else
1540Sstevel@tonic-gate int off=0;
1550Sstevel@tonic-gate #endif
1560Sstevel@tonic-gate int len=0;
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate b=BUF_MEM_new();
1590Sstevel@tonic-gate if (b == NULL)
1600Sstevel@tonic-gate {
161*2139Sjp161948 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ERR_R_MALLOC_FAILURE);
1620Sstevel@tonic-gate return -1;
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate ERR_clear_error();
1660Sstevel@tonic-gate for (;;)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate if (want >= (len-off))
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate want-=(len-off);
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate if (!BUF_MEM_grow_clean(b,len+want))
1730Sstevel@tonic-gate {
174*2139Sjp161948 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ERR_R_MALLOC_FAILURE);
1750Sstevel@tonic-gate goto err;
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate i=BIO_read(in,&(b->data[len]),want);
1780Sstevel@tonic-gate if ((i < 0) && ((len-off) == 0))
1790Sstevel@tonic-gate {
180*2139Sjp161948 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ASN1_R_NOT_ENOUGH_DATA);
1810Sstevel@tonic-gate goto err;
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate if (i > 0)
1840Sstevel@tonic-gate len+=i;
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate /* else data already loaded */
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate p=(unsigned char *)&(b->data[off]);
1890Sstevel@tonic-gate c.p=p;
1900Sstevel@tonic-gate c.inf=ASN1_get_object(&(c.p),&(c.slen),&(c.tag),&(c.xclass),
1910Sstevel@tonic-gate len-off);
1920Sstevel@tonic-gate if (c.inf & 0x80)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate unsigned long e;
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate e=ERR_GET_REASON(ERR_peek_error());
1970Sstevel@tonic-gate if (e != ASN1_R_TOO_LONG)
1980Sstevel@tonic-gate goto err;
1990Sstevel@tonic-gate else
200*2139Sjp161948 ERR_clear_error(); /* clear error */
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate i=c.p-p;/* header length */
2030Sstevel@tonic-gate off+=i; /* end of data */
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate if (c.inf & 1)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate /* no data body so go round again */
2080Sstevel@tonic-gate eos++;
2090Sstevel@tonic-gate want=HEADER_SIZE;
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate else if (eos && (c.slen == 0) && (c.tag == V_ASN1_EOC))
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate /* eos value, so go back and read another header */
2140Sstevel@tonic-gate eos--;
2150Sstevel@tonic-gate if (eos <= 0)
2160Sstevel@tonic-gate break;
2170Sstevel@tonic-gate else
2180Sstevel@tonic-gate want=HEADER_SIZE;
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate else
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate /* suck in c.slen bytes of data */
2230Sstevel@tonic-gate want=(int)c.slen;
2240Sstevel@tonic-gate if (want > (len-off))
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate want-=(len-off);
2270Sstevel@tonic-gate if (!BUF_MEM_grow_clean(b,len+want))
2280Sstevel@tonic-gate {
229*2139Sjp161948 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,ERR_R_MALLOC_FAILURE);
2300Sstevel@tonic-gate goto err;
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate while (want > 0)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate i=BIO_read(in,&(b->data[len]),want);
2350Sstevel@tonic-gate if (i <= 0)
2360Sstevel@tonic-gate {
237*2139Sjp161948 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
2380Sstevel@tonic-gate ASN1_R_NOT_ENOUGH_DATA);
2390Sstevel@tonic-gate goto err;
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate len+=i;
2420Sstevel@tonic-gate want -= i;
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate off+=(int)c.slen;
2460Sstevel@tonic-gate if (eos <= 0)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate break;
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate else
2510Sstevel@tonic-gate want=HEADER_SIZE;
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate *pb = b;
2560Sstevel@tonic-gate return off;
2570Sstevel@tonic-gate err:
2580Sstevel@tonic-gate if (b != NULL) BUF_MEM_free(b);
2590Sstevel@tonic-gate return(ret);
2600Sstevel@tonic-gate }
261