155714Skris/* asn_pack.c */
2296341Sdelphij/*
3296341Sdelphij * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4296341Sdelphij * 1999.
555714Skris */
655714Skris/* ====================================================================
755714Skris * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
855714Skris *
955714Skris * Redistribution and use in source and binary forms, with or without
1055714Skris * modification, are permitted provided that the following conditions
1155714Skris * are met:
1255714Skris *
1355714Skris * 1. Redistributions of source code must retain the above copyright
14296341Sdelphij *    notice, this list of conditions and the following disclaimer.
1555714Skris *
1655714Skris * 2. Redistributions in binary form must reproduce the above copyright
1755714Skris *    notice, this list of conditions and the following disclaimer in
1855714Skris *    the documentation and/or other materials provided with the
1955714Skris *    distribution.
2055714Skris *
2155714Skris * 3. All advertising materials mentioning features or use of this
2255714Skris *    software must display the following acknowledgment:
2355714Skris *    "This product includes software developed by the OpenSSL Project
2455714Skris *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2555714Skris *
2655714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2755714Skris *    endorse or promote products derived from this software without
2855714Skris *    prior written permission. For written permission, please contact
2955714Skris *    licensing@OpenSSL.org.
3055714Skris *
3155714Skris * 5. Products derived from this software may not be called "OpenSSL"
3255714Skris *    nor may "OpenSSL" appear in their names without prior written
3355714Skris *    permission of the OpenSSL Project.
3455714Skris *
3555714Skris * 6. Redistributions of any form whatsoever must retain the following
3655714Skris *    acknowledgment:
3755714Skris *    "This product includes software developed by the OpenSSL Project
3855714Skris *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3955714Skris *
4055714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4155714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4255714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4355714Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4455714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4555714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4655714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4755714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4955714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5055714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
5155714Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
5255714Skris * ====================================================================
5355714Skris *
5455714Skris * This product includes cryptographic software written by Eric Young
5555714Skris * (eay@cryptsoft.com).  This product includes software written by Tim
5655714Skris * Hudson (tjh@cryptsoft.com).
5755714Skris *
5855714Skris */
5955714Skris
6055714Skris#include <stdio.h>
6155714Skris#include "cryptlib.h"
6255714Skris#include <openssl/asn1.h>
6355714Skris
64109998Smarkm#ifndef NO_ASN1_OLD
65109998Smarkm
6655714Skris/* ASN1 packing and unpacking functions */
6755714Skris
6855714Skris/* Turn an ASN1 encoded SEQUENCE OF into a STACK of structures */
6955714Skris
70238405SjkimSTACK_OF(OPENSSL_BLOCK) *ASN1_seq_unpack(const unsigned char *buf, int len,
71296341Sdelphij                                         d2i_of_void *d2i,
72296341Sdelphij                                         void (*free_func) (OPENSSL_BLOCK))
7355714Skris{
74238405Sjkim    STACK_OF(OPENSSL_BLOCK) *sk;
75160814Ssimon    const unsigned char *pbuf;
76296341Sdelphij    pbuf = buf;
7755714Skris    if (!(sk = d2i_ASN1_SET(NULL, &pbuf, len, d2i, free_func,
78296341Sdelphij                            V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL)))
79296341Sdelphij        ASN1err(ASN1_F_ASN1_SEQ_UNPACK, ASN1_R_DECODE_ERROR);
8055714Skris    return sk;
8155714Skris}
8255714Skris
83296341Sdelphij/*
84296341Sdelphij * Turn a STACK structures into an ASN1 encoded SEQUENCE OF structure in a
8568651Skris * OPENSSL_malloc'ed buffer
8655714Skris */
8755714Skris
88238405Sjkimunsigned char *ASN1_seq_pack(STACK_OF(OPENSSL_BLOCK) *safes, i2d_of_void *i2d,
89296341Sdelphij                             unsigned char **buf, int *len)
9055714Skris{
91296341Sdelphij    int safelen;
92296341Sdelphij    unsigned char *safe, *p;
93296341Sdelphij    if (!(safelen = i2d_ASN1_SET(safes, NULL, i2d, V_ASN1_SEQUENCE,
94296341Sdelphij                                 V_ASN1_UNIVERSAL, IS_SEQUENCE))) {
95296341Sdelphij        ASN1err(ASN1_F_ASN1_SEQ_PACK, ASN1_R_ENCODE_ERROR);
96296341Sdelphij        return NULL;
97296341Sdelphij    }
98296341Sdelphij    if (!(safe = OPENSSL_malloc(safelen))) {
99296341Sdelphij        ASN1err(ASN1_F_ASN1_SEQ_PACK, ERR_R_MALLOC_FAILURE);
100296341Sdelphij        return NULL;
101296341Sdelphij    }
102296341Sdelphij    p = safe;
103296341Sdelphij    i2d_ASN1_SET(safes, &p, i2d, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL,
104296341Sdelphij                 IS_SEQUENCE);
105296341Sdelphij    if (len)
106296341Sdelphij        *len = safelen;
107296341Sdelphij    if (buf)
108296341Sdelphij        *buf = safe;
109296341Sdelphij    return safe;
11055714Skris}
11155714Skris
11255714Skris/* Extract an ASN1 object from an ASN1_STRING */
11355714Skris
114160814Ssimonvoid *ASN1_unpack_string(ASN1_STRING *oct, d2i_of_void *d2i)
11555714Skris{
116296341Sdelphij    const unsigned char *p;
117296341Sdelphij    char *ret;
11855714Skris
119296341Sdelphij    p = oct->data;
120296341Sdelphij    if (!(ret = d2i(NULL, &p, oct->length)))
121296341Sdelphij        ASN1err(ASN1_F_ASN1_UNPACK_STRING, ASN1_R_DECODE_ERROR);
122296341Sdelphij    return ret;
12355714Skris}
12455714Skris
12555714Skris/* Pack an ASN1 object into an ASN1_STRING */
12655714Skris
127160814SsimonASN1_STRING *ASN1_pack_string(void *obj, i2d_of_void *i2d, ASN1_STRING **oct)
12855714Skris{
129296341Sdelphij    unsigned char *p;
130296341Sdelphij    ASN1_STRING *octmp;
13155714Skris
132296341Sdelphij    if (!oct || !*oct) {
133296341Sdelphij        if (!(octmp = ASN1_STRING_new())) {
134296341Sdelphij            ASN1err(ASN1_F_ASN1_PACK_STRING, ERR_R_MALLOC_FAILURE);
135296341Sdelphij            return NULL;
136296341Sdelphij        }
137296341Sdelphij        if (oct)
138296341Sdelphij            *oct = octmp;
139296341Sdelphij    } else
140296341Sdelphij        octmp = *oct;
141296341Sdelphij
142296341Sdelphij    if (!(octmp->length = i2d(obj, NULL))) {
143296341Sdelphij        ASN1err(ASN1_F_ASN1_PACK_STRING, ASN1_R_ENCODE_ERROR);
144296341Sdelphij        goto err;
145296341Sdelphij    }
146296341Sdelphij    if (!(p = OPENSSL_malloc(octmp->length))) {
147296341Sdelphij        ASN1err(ASN1_F_ASN1_PACK_STRING, ERR_R_MALLOC_FAILURE);
148296341Sdelphij        goto err;
149296341Sdelphij    }
150296341Sdelphij    octmp->data = p;
151296341Sdelphij    i2d(obj, &p);
152296341Sdelphij    return octmp;
153296341Sdelphij err:
154296341Sdelphij    if (!oct || !*oct) {
155296341Sdelphij        ASN1_STRING_free(octmp);
156296341Sdelphij        if (oct)
157296341Sdelphij            *oct = NULL;
158296341Sdelphij    }
159296341Sdelphij    return NULL;
16055714Skris}
16155714Skris
162109998Smarkm#endif
163109998Smarkm
164109998Smarkm/* ASN1_ITEM versions of the above */
165109998Smarkm
166109998SmarkmASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
167109998Smarkm{
168296341Sdelphij    ASN1_STRING *octmp;
169109998Smarkm
170296341Sdelphij    if (!oct || !*oct) {
171296341Sdelphij        if (!(octmp = ASN1_STRING_new())) {
172296341Sdelphij            ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE);
173296341Sdelphij            return NULL;
174296341Sdelphij        }
175296341Sdelphij        if (oct)
176296341Sdelphij            *oct = octmp;
177296341Sdelphij    } else
178296341Sdelphij        octmp = *oct;
179109998Smarkm
180296341Sdelphij    if (octmp->data) {
181296341Sdelphij        OPENSSL_free(octmp->data);
182296341Sdelphij        octmp->data = NULL;
183296341Sdelphij    }
184296341Sdelphij
185296341Sdelphij    if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) {
186296341Sdelphij        ASN1err(ASN1_F_ASN1_ITEM_PACK, ASN1_R_ENCODE_ERROR);
187296341Sdelphij        return NULL;
188296341Sdelphij    }
189296341Sdelphij    if (!octmp->data) {
190296341Sdelphij        ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE);
191296341Sdelphij        return NULL;
192296341Sdelphij    }
193296341Sdelphij    return octmp;
194109998Smarkm}
195109998Smarkm
196109998Smarkm/* Extract an ASN1 object from an ASN1_STRING */
197109998Smarkm
198109998Smarkmvoid *ASN1_item_unpack(ASN1_STRING *oct, const ASN1_ITEM *it)
199109998Smarkm{
200296341Sdelphij    const unsigned char *p;
201296341Sdelphij    void *ret;
202109998Smarkm
203296341Sdelphij    p = oct->data;
204296341Sdelphij    if (!(ret = ASN1_item_d2i(NULL, &p, oct->length, it)))
205296341Sdelphij        ASN1err(ASN1_F_ASN1_ITEM_UNPACK, ASN1_R_DECODE_ERROR);
206296341Sdelphij    return ret;
207109998Smarkm}
208