xref: /freebsd/crypto/openssl/crypto/asn1/asn_pack.c (revision 74664626283603849f4acbd49e8c8288c08cff4d)
174664626SKris Kennaway /* asn_pack.c */
274664626SKris Kennaway /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
374664626SKris Kennaway  * project 1999.
474664626SKris Kennaway  */
574664626SKris Kennaway /* ====================================================================
674664626SKris Kennaway  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
774664626SKris Kennaway  *
874664626SKris Kennaway  * Redistribution and use in source and binary forms, with or without
974664626SKris Kennaway  * modification, are permitted provided that the following conditions
1074664626SKris Kennaway  * are met:
1174664626SKris Kennaway  *
1274664626SKris Kennaway  * 1. Redistributions of source code must retain the above copyright
1374664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
1474664626SKris Kennaway  *
1574664626SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
1674664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer in
1774664626SKris Kennaway  *    the documentation and/or other materials provided with the
1874664626SKris Kennaway  *    distribution.
1974664626SKris Kennaway  *
2074664626SKris Kennaway  * 3. All advertising materials mentioning features or use of this
2174664626SKris Kennaway  *    software must display the following acknowledgment:
2274664626SKris Kennaway  *    "This product includes software developed by the OpenSSL Project
2374664626SKris Kennaway  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2474664626SKris Kennaway  *
2574664626SKris Kennaway  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2674664626SKris Kennaway  *    endorse or promote products derived from this software without
2774664626SKris Kennaway  *    prior written permission. For written permission, please contact
2874664626SKris Kennaway  *    licensing@OpenSSL.org.
2974664626SKris Kennaway  *
3074664626SKris Kennaway  * 5. Products derived from this software may not be called "OpenSSL"
3174664626SKris Kennaway  *    nor may "OpenSSL" appear in their names without prior written
3274664626SKris Kennaway  *    permission of the OpenSSL Project.
3374664626SKris Kennaway  *
3474664626SKris Kennaway  * 6. Redistributions of any form whatsoever must retain the following
3574664626SKris Kennaway  *    acknowledgment:
3674664626SKris Kennaway  *    "This product includes software developed by the OpenSSL Project
3774664626SKris Kennaway  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3874664626SKris Kennaway  *
3974664626SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4074664626SKris Kennaway  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4174664626SKris Kennaway  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4274664626SKris Kennaway  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4374664626SKris Kennaway  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4474664626SKris Kennaway  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4574664626SKris Kennaway  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4674664626SKris Kennaway  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4774664626SKris Kennaway  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4874664626SKris Kennaway  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4974664626SKris Kennaway  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
5074664626SKris Kennaway  * OF THE POSSIBILITY OF SUCH DAMAGE.
5174664626SKris Kennaway  * ====================================================================
5274664626SKris Kennaway  *
5374664626SKris Kennaway  * This product includes cryptographic software written by Eric Young
5474664626SKris Kennaway  * (eay@cryptsoft.com).  This product includes software written by Tim
5574664626SKris Kennaway  * Hudson (tjh@cryptsoft.com).
5674664626SKris Kennaway  *
5774664626SKris Kennaway  */
5874664626SKris Kennaway 
5974664626SKris Kennaway #include <stdio.h>
6074664626SKris Kennaway #include "cryptlib.h"
6174664626SKris Kennaway #include <openssl/asn1.h>
6274664626SKris Kennaway 
6374664626SKris Kennaway /* ASN1 packing and unpacking functions */
6474664626SKris Kennaway 
6574664626SKris Kennaway /* Turn an ASN1 encoded SEQUENCE OF into a STACK of structures */
6674664626SKris Kennaway 
6774664626SKris Kennaway STACK *ASN1_seq_unpack(unsigned char *buf, int len, char *(*d2i)(),
6874664626SKris Kennaway 	     void (*free_func)())
6974664626SKris Kennaway {
7074664626SKris Kennaway     STACK *sk;
7174664626SKris Kennaway     unsigned char *pbuf;
7274664626SKris Kennaway     pbuf =  buf;
7374664626SKris Kennaway     if (!(sk = d2i_ASN1_SET(NULL, &pbuf, len, d2i, free_func,
7474664626SKris Kennaway 					V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL)))
7574664626SKris Kennaway 		 ASN1err(ASN1_F_ASN1_SEQ_UNPACK,ASN1_R_DECODE_ERROR);
7674664626SKris Kennaway     return sk;
7774664626SKris Kennaway }
7874664626SKris Kennaway 
7974664626SKris Kennaway /* Turn a STACK structures into an ASN1 encoded SEQUENCE OF structure in a
8074664626SKris Kennaway  * Malloc'ed buffer
8174664626SKris Kennaway  */
8274664626SKris Kennaway 
8374664626SKris Kennaway unsigned char *ASN1_seq_pack(STACK *safes, int (*i2d)(), unsigned char **buf,
8474664626SKris Kennaway 	     int *len)
8574664626SKris Kennaway {
8674664626SKris Kennaway 	int safelen;
8774664626SKris Kennaway 	unsigned char *safe, *p;
8874664626SKris Kennaway 	if (!(safelen = i2d_ASN1_SET(safes, NULL, i2d, V_ASN1_SEQUENCE,
8974664626SKris Kennaway 					      V_ASN1_UNIVERSAL, IS_SEQUENCE))) {
9074664626SKris Kennaway 		ASN1err(ASN1_F_ASN1_SEQ_PACK,ASN1_R_ENCODE_ERROR);
9174664626SKris Kennaway 		return NULL;
9274664626SKris Kennaway 	}
9374664626SKris Kennaway 	if (!(safe = Malloc (safelen))) {
9474664626SKris Kennaway 		ASN1err(ASN1_F_ASN1_SEQ_PACK,ERR_R_MALLOC_FAILURE);
9574664626SKris Kennaway 		return NULL;
9674664626SKris Kennaway 	}
9774664626SKris Kennaway 	p = safe;
9874664626SKris Kennaway 	i2d_ASN1_SET(safes, &p, i2d, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL,
9974664626SKris Kennaway 								 IS_SEQUENCE);
10074664626SKris Kennaway 	if (len) *len = safelen;
10174664626SKris Kennaway 	if (buf) *buf = safe;
10274664626SKris Kennaway 	return safe;
10374664626SKris Kennaway }
10474664626SKris Kennaway 
10574664626SKris Kennaway /* Extract an ASN1 object from an ASN1_STRING */
10674664626SKris Kennaway 
10774664626SKris Kennaway void *ASN1_unpack_string (ASN1_STRING *oct, char *(*d2i)())
10874664626SKris Kennaway {
10974664626SKris Kennaway 	unsigned char *p;
11074664626SKris Kennaway 	char *ret;
11174664626SKris Kennaway 
11274664626SKris Kennaway 	p = oct->data;
11374664626SKris Kennaway 	if(!(ret = d2i(NULL, &p, oct->length)))
11474664626SKris Kennaway 		ASN1err(ASN1_F_ASN1_UNPACK_STRING,ASN1_R_DECODE_ERROR);
11574664626SKris Kennaway 	return ret;
11674664626SKris Kennaway }
11774664626SKris Kennaway 
11874664626SKris Kennaway /* Pack an ASN1 object into an ASN1_STRING */
11974664626SKris Kennaway 
12074664626SKris Kennaway ASN1_STRING *ASN1_pack_string (void *obj, int (*i2d)(), ASN1_STRING **oct)
12174664626SKris Kennaway {
12274664626SKris Kennaway 	unsigned char *p;
12374664626SKris Kennaway 	ASN1_STRING *octmp;
12474664626SKris Kennaway 
12574664626SKris Kennaway 	if (!oct || !*oct) {
12674664626SKris Kennaway 		if (!(octmp = ASN1_STRING_new ())) {
12774664626SKris Kennaway 			ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE);
12874664626SKris Kennaway 			return NULL;
12974664626SKris Kennaway 		}
13074664626SKris Kennaway 		if (oct) *oct = octmp;
13174664626SKris Kennaway 	} else octmp = *oct;
13274664626SKris Kennaway 
13374664626SKris Kennaway 	if (!(octmp->length = i2d(obj, NULL))) {
13474664626SKris Kennaway 		ASN1err(ASN1_F_ASN1_PACK_STRING,ASN1_R_ENCODE_ERROR);
13574664626SKris Kennaway 		return NULL;
13674664626SKris Kennaway 	}
13774664626SKris Kennaway 	if (!(p = Malloc (octmp->length))) {
13874664626SKris Kennaway 		ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE);
13974664626SKris Kennaway 		return NULL;
14074664626SKris Kennaway 	}
14174664626SKris Kennaway 	octmp->data = p;
14274664626SKris Kennaway 	i2d (obj, &p);
14374664626SKris Kennaway 	return octmp;
14474664626SKris Kennaway }
14574664626SKris Kennaway 
146