tasn_new.c revision 280304
1/* tasn_new.c */
2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4 * 2000.
5 */
6/* ====================================================================
7 * Copyright (c) 2000-2004 The OpenSSL Project.  All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in
18 *    the documentation and/or other materials provided with the
19 *    distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 *    software must display the following acknowledgment:
23 *    "This product includes software developed by the OpenSSL Project
24 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 *    endorse or promote products derived from this software without
28 *    prior written permission. For written permission, please contact
29 *    licensing@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 *    nor may "OpenSSL" appear in their names without prior written
33 *    permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 *    acknowledgment:
37 *    "This product includes software developed by the OpenSSL Project
38 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com).  This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
59
60#include <stddef.h>
61#include <openssl/asn1.h>
62#include <openssl/objects.h>
63#include <openssl/err.h>
64#include <openssl/asn1t.h>
65#include <string.h>
66
67static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
68                                    int combine);
69static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
70static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
71static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
72
73ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
74{
75    ASN1_VALUE *ret = NULL;
76    if (ASN1_item_ex_new(&ret, it) > 0)
77        return ret;
78    return NULL;
79}
80
81/* Allocate an ASN1 structure */
82
83int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
84{
85    return asn1_item_ex_combine_new(pval, it, 0);
86}
87
88static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
89                                    int combine)
90{
91    const ASN1_TEMPLATE *tt = NULL;
92    const ASN1_COMPAT_FUNCS *cf;
93    const ASN1_EXTERN_FUNCS *ef;
94    const ASN1_AUX *aux = it->funcs;
95    ASN1_aux_cb *asn1_cb;
96    ASN1_VALUE **pseqval;
97    int i;
98    if (aux && aux->asn1_cb)
99        asn1_cb = aux->asn1_cb;
100    else
101        asn1_cb = 0;
102
103    if (!combine)
104        *pval = NULL;
105
106#ifdef CRYPTO_MDEBUG
107    if (it->sname)
108        CRYPTO_push_info(it->sname);
109#endif
110
111    switch (it->itype) {
112
113    case ASN1_ITYPE_EXTERN:
114        ef = it->funcs;
115        if (ef && ef->asn1_ex_new) {
116            if (!ef->asn1_ex_new(pval, it))
117                goto memerr;
118        }
119        break;
120
121    case ASN1_ITYPE_COMPAT:
122        cf = it->funcs;
123        if (cf && cf->asn1_new) {
124            *pval = cf->asn1_new();
125            if (!*pval)
126                goto memerr;
127        }
128        break;
129
130    case ASN1_ITYPE_PRIMITIVE:
131        if (it->templates) {
132            if (!ASN1_template_new(pval, it->templates))
133                goto memerr;
134        } else if (!ASN1_primitive_new(pval, it))
135            goto memerr;
136        break;
137
138    case ASN1_ITYPE_MSTRING:
139        if (!ASN1_primitive_new(pval, it))
140            goto memerr;
141        break;
142
143    case ASN1_ITYPE_CHOICE:
144        if (asn1_cb) {
145            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
146            if (!i)
147                goto auxerr;
148            if (i == 2) {
149#ifdef CRYPTO_MDEBUG
150                if (it->sname)
151                    CRYPTO_pop_info();
152#endif
153                return 1;
154            }
155        }
156        if (!combine) {
157            *pval = OPENSSL_malloc(it->size);
158            if (!*pval)
159                goto memerr;
160            memset(*pval, 0, it->size);
161        }
162        asn1_set_choice_selector(pval, -1, it);
163        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
164            goto auxerr;
165        break;
166
167    case ASN1_ITYPE_NDEF_SEQUENCE:
168    case ASN1_ITYPE_SEQUENCE:
169        if (asn1_cb) {
170            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
171            if (!i)
172                goto auxerr;
173            if (i == 2) {
174#ifdef CRYPTO_MDEBUG
175                if (it->sname)
176                    CRYPTO_pop_info();
177#endif
178                return 1;
179            }
180        }
181        if (!combine) {
182            *pval = OPENSSL_malloc(it->size);
183            if (!*pval)
184                goto memerr;
185            memset(*pval, 0, it->size);
186            asn1_do_lock(pval, 0, it);
187            asn1_enc_init(pval, it);
188        }
189        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
190            pseqval = asn1_get_field_ptr(pval, tt);
191            if (!ASN1_template_new(pseqval, tt))
192                goto memerr;
193        }
194        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
195            goto auxerr;
196        break;
197    }
198#ifdef CRYPTO_MDEBUG
199    if (it->sname)
200        CRYPTO_pop_info();
201#endif
202    return 1;
203
204 memerr:
205    ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ERR_R_MALLOC_FAILURE);
206#ifdef CRYPTO_MDEBUG
207    if (it->sname)
208        CRYPTO_pop_info();
209#endif
210    return 0;
211
212 auxerr:
213    ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ASN1_R_AUX_ERROR);
214    ASN1_item_ex_free(pval, it);
215#ifdef CRYPTO_MDEBUG
216    if (it->sname)
217        CRYPTO_pop_info();
218#endif
219    return 0;
220
221}
222
223static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
224{
225    const ASN1_EXTERN_FUNCS *ef;
226
227    switch (it->itype) {
228
229    case ASN1_ITYPE_EXTERN:
230        ef = it->funcs;
231        if (ef && ef->asn1_ex_clear)
232            ef->asn1_ex_clear(pval, it);
233        else
234            *pval = NULL;
235        break;
236
237    case ASN1_ITYPE_PRIMITIVE:
238        if (it->templates)
239            asn1_template_clear(pval, it->templates);
240        else
241            asn1_primitive_clear(pval, it);
242        break;
243
244    case ASN1_ITYPE_MSTRING:
245        asn1_primitive_clear(pval, it);
246        break;
247
248    case ASN1_ITYPE_COMPAT:
249    case ASN1_ITYPE_CHOICE:
250    case ASN1_ITYPE_SEQUENCE:
251    case ASN1_ITYPE_NDEF_SEQUENCE:
252        *pval = NULL;
253        break;
254    }
255}
256
257int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
258{
259    const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
260    int ret;
261    if (tt->flags & ASN1_TFLG_OPTIONAL) {
262        asn1_template_clear(pval, tt);
263        return 1;
264    }
265    /* If ANY DEFINED BY nothing to do */
266
267    if (tt->flags & ASN1_TFLG_ADB_MASK) {
268        *pval = NULL;
269        return 1;
270    }
271#ifdef CRYPTO_MDEBUG
272    if (tt->field_name)
273        CRYPTO_push_info(tt->field_name);
274#endif
275    /* If SET OF or SEQUENCE OF, its a STACK */
276    if (tt->flags & ASN1_TFLG_SK_MASK) {
277        STACK_OF(ASN1_VALUE) *skval;
278        skval = sk_ASN1_VALUE_new_null();
279        if (!skval) {
280            ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
281            ret = 0;
282            goto done;
283        }
284        *pval = (ASN1_VALUE *)skval;
285        ret = 1;
286        goto done;
287    }
288    /* Otherwise pass it back to the item routine */
289    ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
290 done:
291#ifdef CRYPTO_MDEBUG
292    if (it->sname)
293        CRYPTO_pop_info();
294#endif
295    return ret;
296}
297
298static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
299{
300    /* If ADB or STACK just NULL the field */
301    if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
302        *pval = NULL;
303    else
304        asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
305}
306
307/*
308 * NB: could probably combine most of the real XXX_new() behaviour and junk
309 * all the old functions.
310 */
311
312int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
313{
314    ASN1_TYPE *typ;
315    ASN1_STRING *str;
316    int utype;
317
318    if (!it)
319        return 0;
320
321    if (it->funcs) {
322        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
323        if (pf->prim_new)
324            return pf->prim_new(pval, it);
325    }
326
327    if (it->itype == ASN1_ITYPE_MSTRING)
328        utype = -1;
329    else
330        utype = it->utype;
331    switch (utype) {
332    case V_ASN1_OBJECT:
333        *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
334        return 1;
335
336    case V_ASN1_BOOLEAN:
337        *(ASN1_BOOLEAN *)pval = it->size;
338        return 1;
339
340    case V_ASN1_NULL:
341        *pval = (ASN1_VALUE *)1;
342        return 1;
343
344    case V_ASN1_ANY:
345        typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
346        if (!typ)
347            return 0;
348        typ->value.ptr = NULL;
349        typ->type = -1;
350        *pval = (ASN1_VALUE *)typ;
351        break;
352
353    default:
354        str = ASN1_STRING_type_new(utype);
355        if (it->itype == ASN1_ITYPE_MSTRING && str)
356            str->flags |= ASN1_STRING_FLAG_MSTRING;
357        *pval = (ASN1_VALUE *)str;
358        break;
359    }
360    if (*pval)
361        return 1;
362    return 0;
363}
364
365static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
366{
367    int utype;
368    if (it && it->funcs) {
369        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
370        if (pf->prim_clear)
371            pf->prim_clear(pval, it);
372        else
373            *pval = NULL;
374        return;
375    }
376    if (!it || (it->itype == ASN1_ITYPE_MSTRING))
377        utype = -1;
378    else
379        utype = it->utype;
380    if (utype == V_ASN1_BOOLEAN)
381        *(ASN1_BOOLEAN *)pval = it->size;
382    else
383        *pval = NULL;
384}
385