174664626SKris Kennaway /* asn_pack.c */ 2db522d3aSSimon L. B. Nielsen /* Written by Dr Stephen N Henson (steve@openssl.org) 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 635c87c606SMark Murray #ifndef NO_ASN1_OLD 645c87c606SMark Murray 6574664626SKris Kennaway /* ASN1 packing and unpacking functions */ 6674664626SKris Kennaway 6774664626SKris Kennaway /* Turn an ASN1 encoded SEQUENCE OF into a STACK of structures */ 6874664626SKris Kennaway 691f13597dSJung-uk Kim STACK_OF(OPENSSL_BLOCK) *ASN1_seq_unpack(const unsigned char *buf, int len, 701f13597dSJung-uk Kim d2i_of_void *d2i, void (*free_func)(OPENSSL_BLOCK)) 7174664626SKris Kennaway { 721f13597dSJung-uk Kim STACK_OF(OPENSSL_BLOCK) *sk; 733b4e3dcbSSimon L. B. Nielsen const unsigned char *pbuf; 7474664626SKris Kennaway pbuf = buf; 7574664626SKris Kennaway if (!(sk = d2i_ASN1_SET(NULL, &pbuf, len, d2i, free_func, 7674664626SKris Kennaway V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL))) 7774664626SKris Kennaway ASN1err(ASN1_F_ASN1_SEQ_UNPACK,ASN1_R_DECODE_ERROR); 7874664626SKris Kennaway return sk; 7974664626SKris Kennaway } 8074664626SKris Kennaway 8174664626SKris Kennaway /* Turn a STACK structures into an ASN1 encoded SEQUENCE OF structure in a 82ddd58736SKris Kennaway * OPENSSL_malloc'ed buffer 8374664626SKris Kennaway */ 8474664626SKris Kennaway 851f13597dSJung-uk Kim unsigned char *ASN1_seq_pack(STACK_OF(OPENSSL_BLOCK) *safes, i2d_of_void *i2d, 863b4e3dcbSSimon L. B. Nielsen unsigned char **buf, int *len) 8774664626SKris Kennaway { 8874664626SKris Kennaway int safelen; 8974664626SKris Kennaway unsigned char *safe, *p; 9074664626SKris Kennaway if (!(safelen = i2d_ASN1_SET(safes, NULL, i2d, V_ASN1_SEQUENCE, 9174664626SKris Kennaway V_ASN1_UNIVERSAL, IS_SEQUENCE))) { 9274664626SKris Kennaway ASN1err(ASN1_F_ASN1_SEQ_PACK,ASN1_R_ENCODE_ERROR); 9374664626SKris Kennaway return NULL; 9474664626SKris Kennaway } 95ddd58736SKris Kennaway if (!(safe = OPENSSL_malloc (safelen))) { 9674664626SKris Kennaway ASN1err(ASN1_F_ASN1_SEQ_PACK,ERR_R_MALLOC_FAILURE); 9774664626SKris Kennaway return NULL; 9874664626SKris Kennaway } 9974664626SKris Kennaway p = safe; 10074664626SKris Kennaway i2d_ASN1_SET(safes, &p, i2d, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 10174664626SKris Kennaway IS_SEQUENCE); 10274664626SKris Kennaway if (len) *len = safelen; 10374664626SKris Kennaway if (buf) *buf = safe; 10474664626SKris Kennaway return safe; 10574664626SKris Kennaway } 10674664626SKris Kennaway 10774664626SKris Kennaway /* Extract an ASN1 object from an ASN1_STRING */ 10874664626SKris Kennaway 1093b4e3dcbSSimon L. B. Nielsen void *ASN1_unpack_string(ASN1_STRING *oct, d2i_of_void *d2i) 11074664626SKris Kennaway { 1113b4e3dcbSSimon L. B. Nielsen const unsigned char *p; 11274664626SKris Kennaway char *ret; 11374664626SKris Kennaway 11474664626SKris Kennaway p = oct->data; 11574664626SKris Kennaway if(!(ret = d2i(NULL, &p, oct->length))) 11674664626SKris Kennaway ASN1err(ASN1_F_ASN1_UNPACK_STRING,ASN1_R_DECODE_ERROR); 11774664626SKris Kennaway return ret; 11874664626SKris Kennaway } 11974664626SKris Kennaway 12074664626SKris Kennaway /* Pack an ASN1 object into an ASN1_STRING */ 12174664626SKris Kennaway 1223b4e3dcbSSimon L. B. Nielsen ASN1_STRING *ASN1_pack_string(void *obj, i2d_of_void *i2d, ASN1_STRING **oct) 12374664626SKris Kennaway { 12474664626SKris Kennaway unsigned char *p; 12574664626SKris Kennaway ASN1_STRING *octmp; 12674664626SKris Kennaway 12774664626SKris Kennaway if (!oct || !*oct) { 12874664626SKris Kennaway if (!(octmp = ASN1_STRING_new ())) { 12974664626SKris Kennaway ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE); 13074664626SKris Kennaway return NULL; 13174664626SKris Kennaway } 13274664626SKris Kennaway if (oct) *oct = octmp; 13374664626SKris Kennaway } else octmp = *oct; 13474664626SKris Kennaway 13574664626SKris Kennaway if (!(octmp->length = i2d(obj, NULL))) { 13674664626SKris Kennaway ASN1err(ASN1_F_ASN1_PACK_STRING,ASN1_R_ENCODE_ERROR); 137*a93cbc2bSJung-uk Kim goto err; 13874664626SKris Kennaway } 139ddd58736SKris Kennaway if (!(p = OPENSSL_malloc (octmp->length))) { 14074664626SKris Kennaway ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE); 141*a93cbc2bSJung-uk Kim goto err; 14274664626SKris Kennaway } 14374664626SKris Kennaway octmp->data = p; 14474664626SKris Kennaway i2d (obj, &p); 14574664626SKris Kennaway return octmp; 146*a93cbc2bSJung-uk Kim err: 147*a93cbc2bSJung-uk Kim if (!oct || !*oct) 148*a93cbc2bSJung-uk Kim { 149*a93cbc2bSJung-uk Kim ASN1_STRING_free(octmp); 150*a93cbc2bSJung-uk Kim if (oct) 151*a93cbc2bSJung-uk Kim *oct = NULL; 152*a93cbc2bSJung-uk Kim } 153*a93cbc2bSJung-uk Kim return NULL; 15474664626SKris Kennaway } 15574664626SKris Kennaway 1565c87c606SMark Murray #endif 1575c87c606SMark Murray 1585c87c606SMark Murray /* ASN1_ITEM versions of the above */ 1595c87c606SMark Murray 1605c87c606SMark Murray ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct) 1615c87c606SMark Murray { 1625c87c606SMark Murray ASN1_STRING *octmp; 1635c87c606SMark Murray 1645c87c606SMark Murray if (!oct || !*oct) { 1655c87c606SMark Murray if (!(octmp = ASN1_STRING_new ())) { 1663b4e3dcbSSimon L. B. Nielsen ASN1err(ASN1_F_ASN1_ITEM_PACK,ERR_R_MALLOC_FAILURE); 1675c87c606SMark Murray return NULL; 1685c87c606SMark Murray } 1695c87c606SMark Murray if (oct) *oct = octmp; 1705c87c606SMark Murray } else octmp = *oct; 1715c87c606SMark Murray 1725c87c606SMark Murray if(octmp->data) { 1735c87c606SMark Murray OPENSSL_free(octmp->data); 1745c87c606SMark Murray octmp->data = NULL; 1755c87c606SMark Murray } 1765c87c606SMark Murray 1775c87c606SMark Murray if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) { 1783b4e3dcbSSimon L. B. Nielsen ASN1err(ASN1_F_ASN1_ITEM_PACK,ASN1_R_ENCODE_ERROR); 1795c87c606SMark Murray return NULL; 1805c87c606SMark Murray } 1815c87c606SMark Murray if (!octmp->data) { 1823b4e3dcbSSimon L. B. Nielsen ASN1err(ASN1_F_ASN1_ITEM_PACK,ERR_R_MALLOC_FAILURE); 1835c87c606SMark Murray return NULL; 1845c87c606SMark Murray } 1855c87c606SMark Murray return octmp; 1865c87c606SMark Murray } 1875c87c606SMark Murray 1885c87c606SMark Murray /* Extract an ASN1 object from an ASN1_STRING */ 1895c87c606SMark Murray 1905c87c606SMark Murray void *ASN1_item_unpack(ASN1_STRING *oct, const ASN1_ITEM *it) 1915c87c606SMark Murray { 1923b4e3dcbSSimon L. B. Nielsen const unsigned char *p; 1935c87c606SMark Murray void *ret; 1945c87c606SMark Murray 1955c87c606SMark Murray p = oct->data; 1965c87c606SMark Murray if(!(ret = ASN1_item_d2i(NULL, &p, oct->length, it))) 1973b4e3dcbSSimon L. B. Nielsen ASN1err(ASN1_F_ASN1_ITEM_UNPACK,ASN1_R_DECODE_ERROR); 1985c87c606SMark Murray return ret; 1995c87c606SMark Murray } 200