174664626SKris Kennaway /* asn_pack.c */ 2*6f9291ceSJung-uk Kim /* 3*6f9291ceSJung-uk Kim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 4*6f9291ceSJung-uk Kim * 1999. 574664626SKris Kennaway */ 674664626SKris Kennaway /* ==================================================================== 774664626SKris Kennaway * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 874664626SKris Kennaway * 974664626SKris Kennaway * Redistribution and use in source and binary forms, with or without 1074664626SKris Kennaway * modification, are permitted provided that the following conditions 1174664626SKris Kennaway * are met: 1274664626SKris Kennaway * 1374664626SKris Kennaway * 1. Redistributions of source code must retain the above copyright 1474664626SKris Kennaway * notice, this list of conditions and the following disclaimer. 1574664626SKris Kennaway * 1674664626SKris Kennaway * 2. Redistributions in binary form must reproduce the above copyright 1774664626SKris Kennaway * notice, this list of conditions and the following disclaimer in 1874664626SKris Kennaway * the documentation and/or other materials provided with the 1974664626SKris Kennaway * distribution. 2074664626SKris Kennaway * 2174664626SKris Kennaway * 3. All advertising materials mentioning features or use of this 2274664626SKris Kennaway * software must display the following acknowledgment: 2374664626SKris Kennaway * "This product includes software developed by the OpenSSL Project 2474664626SKris Kennaway * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 2574664626SKris Kennaway * 2674664626SKris Kennaway * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 2774664626SKris Kennaway * endorse or promote products derived from this software without 2874664626SKris Kennaway * prior written permission. For written permission, please contact 2974664626SKris Kennaway * licensing@OpenSSL.org. 3074664626SKris Kennaway * 3174664626SKris Kennaway * 5. Products derived from this software may not be called "OpenSSL" 3274664626SKris Kennaway * nor may "OpenSSL" appear in their names without prior written 3374664626SKris Kennaway * permission of the OpenSSL Project. 3474664626SKris Kennaway * 3574664626SKris Kennaway * 6. Redistributions of any form whatsoever must retain the following 3674664626SKris Kennaway * acknowledgment: 3774664626SKris Kennaway * "This product includes software developed by the OpenSSL Project 3874664626SKris Kennaway * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 3974664626SKris Kennaway * 4074664626SKris Kennaway * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 4174664626SKris Kennaway * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4274664626SKris Kennaway * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4374664626SKris Kennaway * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4474664626SKris Kennaway * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4574664626SKris Kennaway * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4674664626SKris Kennaway * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 4774664626SKris Kennaway * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4874664626SKris Kennaway * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4974664626SKris Kennaway * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5074664626SKris Kennaway * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 5174664626SKris Kennaway * OF THE POSSIBILITY OF SUCH DAMAGE. 5274664626SKris Kennaway * ==================================================================== 5374664626SKris Kennaway * 5474664626SKris Kennaway * This product includes cryptographic software written by Eric Young 5574664626SKris Kennaway * (eay@cryptsoft.com). This product includes software written by Tim 5674664626SKris Kennaway * Hudson (tjh@cryptsoft.com). 5774664626SKris Kennaway * 5874664626SKris Kennaway */ 5974664626SKris Kennaway 6074664626SKris Kennaway #include <stdio.h> 6174664626SKris Kennaway #include "cryptlib.h" 6274664626SKris Kennaway #include <openssl/asn1.h> 6374664626SKris Kennaway 645c87c606SMark Murray #ifndef NO_ASN1_OLD 655c87c606SMark Murray 6674664626SKris Kennaway /* ASN1 packing and unpacking functions */ 6774664626SKris Kennaway 6874664626SKris Kennaway /* Turn an ASN1 encoded SEQUENCE OF into a STACK of structures */ 6974664626SKris Kennaway 701f13597dSJung-uk Kim STACK_OF(OPENSSL_BLOCK) *ASN1_seq_unpack(const unsigned char *buf, int len, 71*6f9291ceSJung-uk Kim d2i_of_void *d2i, 72*6f9291ceSJung-uk Kim void (*free_func) (OPENSSL_BLOCK)) 7374664626SKris Kennaway { 741f13597dSJung-uk Kim STACK_OF(OPENSSL_BLOCK) *sk; 753b4e3dcbSSimon L. B. Nielsen const unsigned char *pbuf; 7674664626SKris Kennaway pbuf = buf; 7774664626SKris Kennaway if (!(sk = d2i_ASN1_SET(NULL, &pbuf, len, d2i, free_func, 7874664626SKris Kennaway V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL))) 7974664626SKris Kennaway ASN1err(ASN1_F_ASN1_SEQ_UNPACK, ASN1_R_DECODE_ERROR); 8074664626SKris Kennaway return sk; 8174664626SKris Kennaway } 8274664626SKris Kennaway 83*6f9291ceSJung-uk Kim /* 84*6f9291ceSJung-uk Kim * Turn a STACK structures into an ASN1 encoded SEQUENCE OF structure in a 85ddd58736SKris Kennaway * OPENSSL_malloc'ed buffer 8674664626SKris Kennaway */ 8774664626SKris Kennaway 881f13597dSJung-uk Kim unsigned char *ASN1_seq_pack(STACK_OF(OPENSSL_BLOCK) *safes, i2d_of_void *i2d, 893b4e3dcbSSimon L. B. Nielsen unsigned char **buf, int *len) 9074664626SKris Kennaway { 9174664626SKris Kennaway int safelen; 9274664626SKris Kennaway unsigned char *safe, *p; 9374664626SKris Kennaway if (!(safelen = i2d_ASN1_SET(safes, NULL, i2d, V_ASN1_SEQUENCE, 9474664626SKris Kennaway V_ASN1_UNIVERSAL, IS_SEQUENCE))) { 9574664626SKris Kennaway ASN1err(ASN1_F_ASN1_SEQ_PACK, ASN1_R_ENCODE_ERROR); 9674664626SKris Kennaway return NULL; 9774664626SKris Kennaway } 98ddd58736SKris Kennaway if (!(safe = OPENSSL_malloc(safelen))) { 9974664626SKris Kennaway ASN1err(ASN1_F_ASN1_SEQ_PACK, ERR_R_MALLOC_FAILURE); 10074664626SKris Kennaway return NULL; 10174664626SKris Kennaway } 10274664626SKris Kennaway p = safe; 10374664626SKris Kennaway i2d_ASN1_SET(safes, &p, i2d, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 10474664626SKris Kennaway IS_SEQUENCE); 105*6f9291ceSJung-uk Kim if (len) 106*6f9291ceSJung-uk Kim *len = safelen; 107*6f9291ceSJung-uk Kim if (buf) 108*6f9291ceSJung-uk Kim *buf = safe; 10974664626SKris Kennaway return safe; 11074664626SKris Kennaway } 11174664626SKris Kennaway 11274664626SKris Kennaway /* Extract an ASN1 object from an ASN1_STRING */ 11374664626SKris Kennaway 1143b4e3dcbSSimon L. B. Nielsen void *ASN1_unpack_string(ASN1_STRING *oct, d2i_of_void *d2i) 11574664626SKris Kennaway { 1163b4e3dcbSSimon L. B. Nielsen const unsigned char *p; 11774664626SKris Kennaway char *ret; 11874664626SKris Kennaway 11974664626SKris Kennaway p = oct->data; 12074664626SKris Kennaway if (!(ret = d2i(NULL, &p, oct->length))) 12174664626SKris Kennaway ASN1err(ASN1_F_ASN1_UNPACK_STRING, ASN1_R_DECODE_ERROR); 12274664626SKris Kennaway return ret; 12374664626SKris Kennaway } 12474664626SKris Kennaway 12574664626SKris Kennaway /* Pack an ASN1 object into an ASN1_STRING */ 12674664626SKris Kennaway 1273b4e3dcbSSimon L. B. Nielsen ASN1_STRING *ASN1_pack_string(void *obj, i2d_of_void *i2d, ASN1_STRING **oct) 12874664626SKris Kennaway { 12974664626SKris Kennaway unsigned char *p; 13074664626SKris Kennaway ASN1_STRING *octmp; 13174664626SKris Kennaway 13274664626SKris Kennaway if (!oct || !*oct) { 13374664626SKris Kennaway if (!(octmp = ASN1_STRING_new())) { 13474664626SKris Kennaway ASN1err(ASN1_F_ASN1_PACK_STRING, ERR_R_MALLOC_FAILURE); 13574664626SKris Kennaway return NULL; 13674664626SKris Kennaway } 137*6f9291ceSJung-uk Kim if (oct) 138*6f9291ceSJung-uk Kim *oct = octmp; 139*6f9291ceSJung-uk Kim } else 140*6f9291ceSJung-uk Kim octmp = *oct; 14174664626SKris Kennaway 14274664626SKris Kennaway if (!(octmp->length = i2d(obj, NULL))) { 14374664626SKris Kennaway ASN1err(ASN1_F_ASN1_PACK_STRING, ASN1_R_ENCODE_ERROR); 144a93cbc2bSJung-uk Kim goto err; 14574664626SKris Kennaway } 146ddd58736SKris Kennaway if (!(p = OPENSSL_malloc(octmp->length))) { 14774664626SKris Kennaway ASN1err(ASN1_F_ASN1_PACK_STRING, ERR_R_MALLOC_FAILURE); 148a93cbc2bSJung-uk Kim goto err; 14974664626SKris Kennaway } 15074664626SKris Kennaway octmp->data = p; 15174664626SKris Kennaway i2d(obj, &p); 15274664626SKris Kennaway return octmp; 153a93cbc2bSJung-uk Kim err: 154*6f9291ceSJung-uk Kim if (!oct || !*oct) { 155a93cbc2bSJung-uk Kim ASN1_STRING_free(octmp); 156a93cbc2bSJung-uk Kim if (oct) 157a93cbc2bSJung-uk Kim *oct = NULL; 158a93cbc2bSJung-uk Kim } 159a93cbc2bSJung-uk Kim return NULL; 16074664626SKris Kennaway } 16174664626SKris Kennaway 1625c87c606SMark Murray #endif 1635c87c606SMark Murray 1645c87c606SMark Murray /* ASN1_ITEM versions of the above */ 1655c87c606SMark Murray 1665c87c606SMark Murray ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct) 1675c87c606SMark Murray { 1685c87c606SMark Murray ASN1_STRING *octmp; 1695c87c606SMark Murray 1705c87c606SMark Murray if (!oct || !*oct) { 1715c87c606SMark Murray if (!(octmp = ASN1_STRING_new())) { 1723b4e3dcbSSimon L. B. Nielsen ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE); 1735c87c606SMark Murray return NULL; 1745c87c606SMark Murray } 175*6f9291ceSJung-uk Kim if (oct) 176*6f9291ceSJung-uk Kim *oct = octmp; 177*6f9291ceSJung-uk Kim } else 178*6f9291ceSJung-uk Kim octmp = *oct; 1795c87c606SMark Murray 1805c87c606SMark Murray if (octmp->data) { 1815c87c606SMark Murray OPENSSL_free(octmp->data); 1825c87c606SMark Murray octmp->data = NULL; 1835c87c606SMark Murray } 1845c87c606SMark Murray 1855c87c606SMark Murray if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) { 1863b4e3dcbSSimon L. B. Nielsen ASN1err(ASN1_F_ASN1_ITEM_PACK, ASN1_R_ENCODE_ERROR); 1875c87c606SMark Murray return NULL; 1885c87c606SMark Murray } 1895c87c606SMark Murray if (!octmp->data) { 1903b4e3dcbSSimon L. B. Nielsen ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE); 1915c87c606SMark Murray return NULL; 1925c87c606SMark Murray } 1935c87c606SMark Murray return octmp; 1945c87c606SMark Murray } 1955c87c606SMark Murray 1965c87c606SMark Murray /* Extract an ASN1 object from an ASN1_STRING */ 1975c87c606SMark Murray 1985c87c606SMark Murray void *ASN1_item_unpack(ASN1_STRING *oct, const ASN1_ITEM *it) 1995c87c606SMark Murray { 2003b4e3dcbSSimon L. B. Nielsen const unsigned char *p; 2015c87c606SMark Murray void *ret; 2025c87c606SMark Murray 2035c87c606SMark Murray p = oct->data; 2045c87c606SMark Murray if (!(ret = ASN1_item_d2i(NULL, &p, oct->length, it))) 2053b4e3dcbSSimon L. B. Nielsen ASN1err(ASN1_F_ASN1_ITEM_UNPACK, ASN1_R_DECODE_ERROR); 2065c87c606SMark Murray return ret; 2075c87c606SMark Murray } 208