1ebfedea0SLionel Sambuc /* asn_pack.c */
2*0a6a1f1dSLionel Sambuc /*
3*0a6a1f1dSLionel Sambuc * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4*0a6a1f1dSLionel Sambuc * 1999.
5ebfedea0SLionel Sambuc */
6ebfedea0SLionel Sambuc /* ====================================================================
7ebfedea0SLionel Sambuc * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc * are met:
12ebfedea0SLionel Sambuc *
13ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
18ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
19ebfedea0SLionel Sambuc * distribution.
20ebfedea0SLionel Sambuc *
21ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
22ebfedea0SLionel Sambuc * software must display the following acknowledgment:
23ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
24ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25ebfedea0SLionel Sambuc *
26ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
28ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
29ebfedea0SLionel Sambuc * licensing@OpenSSL.org.
30ebfedea0SLionel Sambuc *
31ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
32ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
33ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
34ebfedea0SLionel Sambuc *
35ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
36ebfedea0SLionel Sambuc * acknowledgment:
37ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
38ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39ebfedea0SLionel Sambuc *
40ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
52ebfedea0SLionel Sambuc * ====================================================================
53ebfedea0SLionel Sambuc *
54ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
55ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
56ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
57ebfedea0SLionel Sambuc *
58ebfedea0SLionel Sambuc */
59ebfedea0SLionel Sambuc
60ebfedea0SLionel Sambuc #include <stdio.h>
61ebfedea0SLionel Sambuc #include "cryptlib.h"
62ebfedea0SLionel Sambuc #include <openssl/asn1.h>
63ebfedea0SLionel Sambuc
64ebfedea0SLionel Sambuc #ifndef NO_ASN1_OLD
65ebfedea0SLionel Sambuc
66ebfedea0SLionel Sambuc /* ASN1 packing and unpacking functions */
67ebfedea0SLionel Sambuc
68ebfedea0SLionel Sambuc /* Turn an ASN1 encoded SEQUENCE OF into a STACK of structures */
69ebfedea0SLionel Sambuc
STACK_OF(OPENSSL_BLOCK)70ebfedea0SLionel Sambuc STACK_OF(OPENSSL_BLOCK) *ASN1_seq_unpack(const unsigned char *buf, int len,
71*0a6a1f1dSLionel Sambuc d2i_of_void *d2i,
72*0a6a1f1dSLionel Sambuc void (*free_func) (OPENSSL_BLOCK))
73ebfedea0SLionel Sambuc {
74ebfedea0SLionel Sambuc STACK_OF(OPENSSL_BLOCK) *sk;
75ebfedea0SLionel Sambuc const unsigned char *pbuf;
76ebfedea0SLionel Sambuc pbuf = buf;
77ebfedea0SLionel Sambuc if (!(sk = d2i_ASN1_SET(NULL, &pbuf, len, d2i, free_func,
78ebfedea0SLionel Sambuc V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL)))
79ebfedea0SLionel Sambuc ASN1err(ASN1_F_ASN1_SEQ_UNPACK, ASN1_R_DECODE_ERROR);
80ebfedea0SLionel Sambuc return sk;
81ebfedea0SLionel Sambuc }
82ebfedea0SLionel Sambuc
83*0a6a1f1dSLionel Sambuc /*
84*0a6a1f1dSLionel Sambuc * Turn a STACK structures into an ASN1 encoded SEQUENCE OF structure in a
85ebfedea0SLionel Sambuc * OPENSSL_malloc'ed buffer
86ebfedea0SLionel Sambuc */
87ebfedea0SLionel Sambuc
ASN1_seq_pack(STACK_OF (OPENSSL_BLOCK)* safes,i2d_of_void * i2d,unsigned char ** buf,int * len)88ebfedea0SLionel Sambuc unsigned char *ASN1_seq_pack(STACK_OF(OPENSSL_BLOCK) *safes, i2d_of_void *i2d,
89ebfedea0SLionel Sambuc unsigned char **buf, int *len)
90ebfedea0SLionel Sambuc {
91ebfedea0SLionel Sambuc int safelen;
92ebfedea0SLionel Sambuc unsigned char *safe, *p;
93ebfedea0SLionel Sambuc if (!(safelen = i2d_ASN1_SET(safes, NULL, i2d, V_ASN1_SEQUENCE,
94ebfedea0SLionel Sambuc V_ASN1_UNIVERSAL, IS_SEQUENCE))) {
95ebfedea0SLionel Sambuc ASN1err(ASN1_F_ASN1_SEQ_PACK, ASN1_R_ENCODE_ERROR);
96ebfedea0SLionel Sambuc return NULL;
97ebfedea0SLionel Sambuc }
98ebfedea0SLionel Sambuc if (!(safe = OPENSSL_malloc(safelen))) {
99ebfedea0SLionel Sambuc ASN1err(ASN1_F_ASN1_SEQ_PACK, ERR_R_MALLOC_FAILURE);
100ebfedea0SLionel Sambuc return NULL;
101ebfedea0SLionel Sambuc }
102ebfedea0SLionel Sambuc p = safe;
103ebfedea0SLionel Sambuc i2d_ASN1_SET(safes, &p, i2d, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL,
104ebfedea0SLionel Sambuc IS_SEQUENCE);
105*0a6a1f1dSLionel Sambuc if (len)
106*0a6a1f1dSLionel Sambuc *len = safelen;
107*0a6a1f1dSLionel Sambuc if (buf)
108*0a6a1f1dSLionel Sambuc *buf = safe;
109ebfedea0SLionel Sambuc return safe;
110ebfedea0SLionel Sambuc }
111ebfedea0SLionel Sambuc
112ebfedea0SLionel Sambuc /* Extract an ASN1 object from an ASN1_STRING */
113ebfedea0SLionel Sambuc
ASN1_unpack_string(ASN1_STRING * oct,d2i_of_void * d2i)114ebfedea0SLionel Sambuc void *ASN1_unpack_string(ASN1_STRING *oct, d2i_of_void *d2i)
115ebfedea0SLionel Sambuc {
116ebfedea0SLionel Sambuc const unsigned char *p;
117ebfedea0SLionel Sambuc char *ret;
118ebfedea0SLionel Sambuc
119ebfedea0SLionel Sambuc p = oct->data;
120ebfedea0SLionel Sambuc if (!(ret = d2i(NULL, &p, oct->length)))
121ebfedea0SLionel Sambuc ASN1err(ASN1_F_ASN1_UNPACK_STRING, ASN1_R_DECODE_ERROR);
122ebfedea0SLionel Sambuc return ret;
123ebfedea0SLionel Sambuc }
124ebfedea0SLionel Sambuc
125ebfedea0SLionel Sambuc /* Pack an ASN1 object into an ASN1_STRING */
126ebfedea0SLionel Sambuc
ASN1_pack_string(void * obj,i2d_of_void * i2d,ASN1_STRING ** oct)127ebfedea0SLionel Sambuc ASN1_STRING *ASN1_pack_string(void *obj, i2d_of_void *i2d, ASN1_STRING **oct)
128ebfedea0SLionel Sambuc {
129ebfedea0SLionel Sambuc unsigned char *p;
130ebfedea0SLionel Sambuc ASN1_STRING *octmp;
131ebfedea0SLionel Sambuc
132ebfedea0SLionel Sambuc if (!oct || !*oct) {
133ebfedea0SLionel Sambuc if (!(octmp = ASN1_STRING_new())) {
134ebfedea0SLionel Sambuc ASN1err(ASN1_F_ASN1_PACK_STRING, ERR_R_MALLOC_FAILURE);
135ebfedea0SLionel Sambuc return NULL;
136ebfedea0SLionel Sambuc }
137*0a6a1f1dSLionel Sambuc if (oct)
138*0a6a1f1dSLionel Sambuc *oct = octmp;
139*0a6a1f1dSLionel Sambuc } else
140*0a6a1f1dSLionel Sambuc octmp = *oct;
141ebfedea0SLionel Sambuc
142ebfedea0SLionel Sambuc if (!(octmp->length = i2d(obj, NULL))) {
143ebfedea0SLionel Sambuc ASN1err(ASN1_F_ASN1_PACK_STRING, ASN1_R_ENCODE_ERROR);
144*0a6a1f1dSLionel Sambuc goto err;
145ebfedea0SLionel Sambuc }
146ebfedea0SLionel Sambuc if (!(p = OPENSSL_malloc(octmp->length))) {
147ebfedea0SLionel Sambuc ASN1err(ASN1_F_ASN1_PACK_STRING, ERR_R_MALLOC_FAILURE);
148*0a6a1f1dSLionel Sambuc goto err;
149ebfedea0SLionel Sambuc }
150ebfedea0SLionel Sambuc octmp->data = p;
151ebfedea0SLionel Sambuc i2d(obj, &p);
152ebfedea0SLionel Sambuc return octmp;
153*0a6a1f1dSLionel Sambuc err:
154*0a6a1f1dSLionel Sambuc if (!oct || !*oct) {
155*0a6a1f1dSLionel Sambuc ASN1_STRING_free(octmp);
156*0a6a1f1dSLionel Sambuc if (oct)
157*0a6a1f1dSLionel Sambuc *oct = NULL;
158*0a6a1f1dSLionel Sambuc }
159*0a6a1f1dSLionel Sambuc return NULL;
160ebfedea0SLionel Sambuc }
161ebfedea0SLionel Sambuc
162ebfedea0SLionel Sambuc #endif
163ebfedea0SLionel Sambuc
164ebfedea0SLionel Sambuc /* ASN1_ITEM versions of the above */
165ebfedea0SLionel Sambuc
ASN1_item_pack(void * obj,const ASN1_ITEM * it,ASN1_STRING ** oct)166ebfedea0SLionel Sambuc ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
167ebfedea0SLionel Sambuc {
168ebfedea0SLionel Sambuc ASN1_STRING *octmp;
169ebfedea0SLionel Sambuc
170ebfedea0SLionel Sambuc if (!oct || !*oct) {
171ebfedea0SLionel Sambuc if (!(octmp = ASN1_STRING_new())) {
172ebfedea0SLionel Sambuc ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE);
173ebfedea0SLionel Sambuc return NULL;
174ebfedea0SLionel Sambuc }
175*0a6a1f1dSLionel Sambuc if (oct)
176*0a6a1f1dSLionel Sambuc *oct = octmp;
177*0a6a1f1dSLionel Sambuc } else
178*0a6a1f1dSLionel Sambuc octmp = *oct;
179ebfedea0SLionel Sambuc
180ebfedea0SLionel Sambuc if (octmp->data) {
181ebfedea0SLionel Sambuc OPENSSL_free(octmp->data);
182ebfedea0SLionel Sambuc octmp->data = NULL;
183ebfedea0SLionel Sambuc }
184ebfedea0SLionel Sambuc
185ebfedea0SLionel Sambuc if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) {
186ebfedea0SLionel Sambuc ASN1err(ASN1_F_ASN1_ITEM_PACK, ASN1_R_ENCODE_ERROR);
187ebfedea0SLionel Sambuc return NULL;
188ebfedea0SLionel Sambuc }
189ebfedea0SLionel Sambuc if (!octmp->data) {
190ebfedea0SLionel Sambuc ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE);
191ebfedea0SLionel Sambuc return NULL;
192ebfedea0SLionel Sambuc }
193ebfedea0SLionel Sambuc return octmp;
194ebfedea0SLionel Sambuc }
195ebfedea0SLionel Sambuc
196ebfedea0SLionel Sambuc /* Extract an ASN1 object from an ASN1_STRING */
197ebfedea0SLionel Sambuc
ASN1_item_unpack(ASN1_STRING * oct,const ASN1_ITEM * it)198ebfedea0SLionel Sambuc void *ASN1_item_unpack(ASN1_STRING *oct, const ASN1_ITEM *it)
199ebfedea0SLionel Sambuc {
200ebfedea0SLionel Sambuc const unsigned char *p;
201ebfedea0SLionel Sambuc void *ret;
202ebfedea0SLionel Sambuc
203ebfedea0SLionel Sambuc p = oct->data;
204ebfedea0SLionel Sambuc if (!(ret = ASN1_item_d2i(NULL, &p, oct->length, it)))
205ebfedea0SLionel Sambuc ASN1err(ASN1_F_ASN1_ITEM_UNPACK, ASN1_R_DECODE_ERROR);
206ebfedea0SLionel Sambuc return ret;
207ebfedea0SLionel Sambuc }
208