xref: /onnv-gate/usr/src/common/openssl/crypto/asn1/asn_pack.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* asn_pack.c */
20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
30Sstevel@tonic-gate  * project 1999.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    licensing@OpenSSL.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/asn1.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #ifndef NO_ASN1_OLD
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /* ASN1 packing and unpacking functions */
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /* Turn an ASN1 encoded SEQUENCE OF into a STACK of structures */
680Sstevel@tonic-gate 
ASN1_seq_unpack(const unsigned char * buf,int len,d2i_of_void * d2i,void (* free_func)(void *))69*2139Sjp161948 STACK *ASN1_seq_unpack(const unsigned char *buf, int len,
70*2139Sjp161948 		       d2i_of_void *d2i,void (*free_func)(void *))
710Sstevel@tonic-gate {
720Sstevel@tonic-gate     STACK *sk;
73*2139Sjp161948     const unsigned char *pbuf;
740Sstevel@tonic-gate     pbuf =  buf;
750Sstevel@tonic-gate     if (!(sk = d2i_ASN1_SET(NULL, &pbuf, len, d2i, free_func,
760Sstevel@tonic-gate 					V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL)))
770Sstevel@tonic-gate 		 ASN1err(ASN1_F_ASN1_SEQ_UNPACK,ASN1_R_DECODE_ERROR);
780Sstevel@tonic-gate     return sk;
790Sstevel@tonic-gate }
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /* Turn a STACK structures into an ASN1 encoded SEQUENCE OF structure in a
820Sstevel@tonic-gate  * OPENSSL_malloc'ed buffer
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate 
ASN1_seq_pack(STACK * safes,i2d_of_void * i2d,unsigned char ** buf,int * len)85*2139Sjp161948 unsigned char *ASN1_seq_pack(STACK *safes, i2d_of_void *i2d,
86*2139Sjp161948 			     unsigned char **buf, int *len)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	int safelen;
890Sstevel@tonic-gate 	unsigned char *safe, *p;
900Sstevel@tonic-gate 	if (!(safelen = i2d_ASN1_SET(safes, NULL, i2d, V_ASN1_SEQUENCE,
910Sstevel@tonic-gate 					      V_ASN1_UNIVERSAL, IS_SEQUENCE))) {
920Sstevel@tonic-gate 		ASN1err(ASN1_F_ASN1_SEQ_PACK,ASN1_R_ENCODE_ERROR);
930Sstevel@tonic-gate 		return NULL;
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 	if (!(safe = OPENSSL_malloc (safelen))) {
960Sstevel@tonic-gate 		ASN1err(ASN1_F_ASN1_SEQ_PACK,ERR_R_MALLOC_FAILURE);
970Sstevel@tonic-gate 		return NULL;
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 	p = safe;
1000Sstevel@tonic-gate 	i2d_ASN1_SET(safes, &p, i2d, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL,
1010Sstevel@tonic-gate 								 IS_SEQUENCE);
1020Sstevel@tonic-gate 	if (len) *len = safelen;
1030Sstevel@tonic-gate 	if (buf) *buf = safe;
1040Sstevel@tonic-gate 	return safe;
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /* Extract an ASN1 object from an ASN1_STRING */
1080Sstevel@tonic-gate 
ASN1_unpack_string(ASN1_STRING * oct,d2i_of_void * d2i)109*2139Sjp161948 void *ASN1_unpack_string(ASN1_STRING *oct, d2i_of_void *d2i)
1100Sstevel@tonic-gate {
111*2139Sjp161948 	const unsigned char *p;
1120Sstevel@tonic-gate 	char *ret;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	p = oct->data;
1150Sstevel@tonic-gate 	if(!(ret = d2i(NULL, &p, oct->length)))
1160Sstevel@tonic-gate 		ASN1err(ASN1_F_ASN1_UNPACK_STRING,ASN1_R_DECODE_ERROR);
1170Sstevel@tonic-gate 	return ret;
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /* Pack an ASN1 object into an ASN1_STRING */
1210Sstevel@tonic-gate 
ASN1_pack_string(void * obj,i2d_of_void * i2d,ASN1_STRING ** oct)122*2139Sjp161948 ASN1_STRING *ASN1_pack_string(void *obj, i2d_of_void *i2d, ASN1_STRING **oct)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	unsigned char *p;
1250Sstevel@tonic-gate 	ASN1_STRING *octmp;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	if (!oct || !*oct) {
1280Sstevel@tonic-gate 		if (!(octmp = ASN1_STRING_new ())) {
1290Sstevel@tonic-gate 			ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE);
1300Sstevel@tonic-gate 			return NULL;
1310Sstevel@tonic-gate 		}
1320Sstevel@tonic-gate 		if (oct) *oct = octmp;
1330Sstevel@tonic-gate 	} else octmp = *oct;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if (!(octmp->length = i2d(obj, NULL))) {
1360Sstevel@tonic-gate 		ASN1err(ASN1_F_ASN1_PACK_STRING,ASN1_R_ENCODE_ERROR);
1370Sstevel@tonic-gate 		return NULL;
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 	if (!(p = OPENSSL_malloc (octmp->length))) {
1400Sstevel@tonic-gate 		ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE);
1410Sstevel@tonic-gate 		return NULL;
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 	octmp->data = p;
1440Sstevel@tonic-gate 	i2d (obj, &p);
1450Sstevel@tonic-gate 	return octmp;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate #endif
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /* ASN1_ITEM versions of the above */
1510Sstevel@tonic-gate 
ASN1_item_pack(void * obj,const ASN1_ITEM * it,ASN1_STRING ** oct)1520Sstevel@tonic-gate ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	ASN1_STRING *octmp;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	if (!oct || !*oct) {
1570Sstevel@tonic-gate 		if (!(octmp = ASN1_STRING_new ())) {
158*2139Sjp161948 			ASN1err(ASN1_F_ASN1_ITEM_PACK,ERR_R_MALLOC_FAILURE);
1590Sstevel@tonic-gate 			return NULL;
1600Sstevel@tonic-gate 		}
1610Sstevel@tonic-gate 		if (oct) *oct = octmp;
1620Sstevel@tonic-gate 	} else octmp = *oct;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if(octmp->data) {
1650Sstevel@tonic-gate 		OPENSSL_free(octmp->data);
1660Sstevel@tonic-gate 		octmp->data = NULL;
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) {
170*2139Sjp161948 		ASN1err(ASN1_F_ASN1_ITEM_PACK,ASN1_R_ENCODE_ERROR);
1710Sstevel@tonic-gate 		return NULL;
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 	if (!octmp->data) {
174*2139Sjp161948 		ASN1err(ASN1_F_ASN1_ITEM_PACK,ERR_R_MALLOC_FAILURE);
1750Sstevel@tonic-gate 		return NULL;
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 	return octmp;
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /* Extract an ASN1 object from an ASN1_STRING */
1810Sstevel@tonic-gate 
ASN1_item_unpack(ASN1_STRING * oct,const ASN1_ITEM * it)1820Sstevel@tonic-gate void *ASN1_item_unpack(ASN1_STRING *oct, const ASN1_ITEM *it)
1830Sstevel@tonic-gate {
184*2139Sjp161948 	const unsigned char *p;
1850Sstevel@tonic-gate 	void *ret;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	p = oct->data;
1880Sstevel@tonic-gate 	if(!(ret = ASN1_item_d2i(NULL, &p, oct->length, it)))
189*2139Sjp161948 		ASN1err(ASN1_F_ASN1_ITEM_UNPACK,ASN1_R_DECODE_ERROR);
1900Sstevel@tonic-gate 	return ret;
1910Sstevel@tonic-gate }
192