asn_pack.c revision 68651
155714Skris/* asn_pack.c */ 255714Skris/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 355714Skris * project 1999. 455714Skris */ 555714Skris/* ==================================================================== 655714Skris * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 755714Skris * 855714Skris * Redistribution and use in source and binary forms, with or without 955714Skris * modification, are permitted provided that the following conditions 1055714Skris * are met: 1155714Skris * 1255714Skris * 1. Redistributions of source code must retain the above copyright 1355714Skris * notice, this list of conditions and the following disclaimer. 1455714Skris * 1555714Skris * 2. Redistributions in binary form must reproduce the above copyright 1655714Skris * notice, this list of conditions and the following disclaimer in 1755714Skris * the documentation and/or other materials provided with the 1855714Skris * distribution. 1955714Skris * 2055714Skris * 3. All advertising materials mentioning features or use of this 2155714Skris * software must display the following acknowledgment: 2255714Skris * "This product includes software developed by the OpenSSL Project 2355714Skris * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 2455714Skris * 2555714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 2655714Skris * endorse or promote products derived from this software without 2755714Skris * prior written permission. For written permission, please contact 2855714Skris * licensing@OpenSSL.org. 2955714Skris * 3055714Skris * 5. Products derived from this software may not be called "OpenSSL" 3155714Skris * nor may "OpenSSL" appear in their names without prior written 3255714Skris * permission of the OpenSSL Project. 3355714Skris * 3455714Skris * 6. Redistributions of any form whatsoever must retain the following 3555714Skris * acknowledgment: 3655714Skris * "This product includes software developed by the OpenSSL Project 3755714Skris * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 3855714Skris * 3955714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 4055714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4155714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4255714Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4355714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4455714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4555714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 4655714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4755714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4855714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4955714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 5055714Skris * OF THE POSSIBILITY OF SUCH DAMAGE. 5155714Skris * ==================================================================== 5255714Skris * 5355714Skris * This product includes cryptographic software written by Eric Young 5455714Skris * (eay@cryptsoft.com). This product includes software written by Tim 5555714Skris * Hudson (tjh@cryptsoft.com). 5655714Skris * 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include "cryptlib.h" 6155714Skris#include <openssl/asn1.h> 6255714Skris 6355714Skris/* ASN1 packing and unpacking functions */ 6455714Skris 6555714Skris/* Turn an ASN1 encoded SEQUENCE OF into a STACK of structures */ 6655714Skris 6755714SkrisSTACK *ASN1_seq_unpack(unsigned char *buf, int len, char *(*d2i)(), 6868651Skris void (*free_func)(void *)) 6955714Skris{ 7055714Skris STACK *sk; 7155714Skris unsigned char *pbuf; 7255714Skris pbuf = buf; 7355714Skris if (!(sk = d2i_ASN1_SET(NULL, &pbuf, len, d2i, free_func, 7455714Skris V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL))) 7555714Skris ASN1err(ASN1_F_ASN1_SEQ_UNPACK,ASN1_R_DECODE_ERROR); 7655714Skris return sk; 7755714Skris} 7855714Skris 7955714Skris/* Turn a STACK structures into an ASN1 encoded SEQUENCE OF structure in a 8068651Skris * OPENSSL_malloc'ed buffer 8155714Skris */ 8255714Skris 8355714Skrisunsigned char *ASN1_seq_pack(STACK *safes, int (*i2d)(), unsigned char **buf, 8455714Skris int *len) 8555714Skris{ 8655714Skris int safelen; 8755714Skris unsigned char *safe, *p; 8855714Skris if (!(safelen = i2d_ASN1_SET(safes, NULL, i2d, V_ASN1_SEQUENCE, 8955714Skris V_ASN1_UNIVERSAL, IS_SEQUENCE))) { 9055714Skris ASN1err(ASN1_F_ASN1_SEQ_PACK,ASN1_R_ENCODE_ERROR); 9155714Skris return NULL; 9255714Skris } 9368651Skris if (!(safe = OPENSSL_malloc (safelen))) { 9455714Skris ASN1err(ASN1_F_ASN1_SEQ_PACK,ERR_R_MALLOC_FAILURE); 9555714Skris return NULL; 9655714Skris } 9755714Skris p = safe; 9855714Skris i2d_ASN1_SET(safes, &p, i2d, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 9955714Skris IS_SEQUENCE); 10055714Skris if (len) *len = safelen; 10155714Skris if (buf) *buf = safe; 10255714Skris return safe; 10355714Skris} 10455714Skris 10555714Skris/* Extract an ASN1 object from an ASN1_STRING */ 10655714Skris 10755714Skrisvoid *ASN1_unpack_string (ASN1_STRING *oct, char *(*d2i)()) 10855714Skris{ 10955714Skris unsigned char *p; 11055714Skris char *ret; 11155714Skris 11255714Skris p = oct->data; 11355714Skris if(!(ret = d2i(NULL, &p, oct->length))) 11455714Skris ASN1err(ASN1_F_ASN1_UNPACK_STRING,ASN1_R_DECODE_ERROR); 11555714Skris return ret; 11655714Skris} 11755714Skris 11855714Skris/* Pack an ASN1 object into an ASN1_STRING */ 11955714Skris 12055714SkrisASN1_STRING *ASN1_pack_string (void *obj, int (*i2d)(), ASN1_STRING **oct) 12155714Skris{ 12255714Skris unsigned char *p; 12355714Skris ASN1_STRING *octmp; 12455714Skris 12555714Skris if (!oct || !*oct) { 12655714Skris if (!(octmp = ASN1_STRING_new ())) { 12755714Skris ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE); 12855714Skris return NULL; 12955714Skris } 13055714Skris if (oct) *oct = octmp; 13155714Skris } else octmp = *oct; 13255714Skris 13355714Skris if (!(octmp->length = i2d(obj, NULL))) { 13455714Skris ASN1err(ASN1_F_ASN1_PACK_STRING,ASN1_R_ENCODE_ERROR); 13555714Skris return NULL; 13655714Skris } 13768651Skris if (!(p = OPENSSL_malloc (octmp->length))) { 13855714Skris ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE); 13955714Skris return NULL; 14055714Skris } 14155714Skris octmp->data = p; 14255714Skris i2d (obj, &p); 14355714Skris return octmp; 14455714Skris} 14555714Skris 146