blob: eea219d660444f3191e250d6396c73ea01d9acda [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57#include <openssl/asn1.h>
58
Adam Langley2b2d66d2015-01-30 17:08:37 -080059#include <string.h>
60
Adam Langley95c29f32014-06-20 12:00:00 -070061#include <openssl/asn1t.h>
62#include <openssl/err.h>
63#include <openssl/mem.h>
64#include <openssl/obj.h>
65
David Benjamine3a7bd02021-06-30 10:18:30 -040066#include "internal.h"
David Benjamin17cf2cb2016-12-13 01:07:13 -050067#include "../internal.h"
68
69
Adam Langley95c29f32014-06-20 12:00:00 -070070static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
Adam Langley57707c72016-01-14 11:25:12 -080071 int combine);
Adam Langley95c29f32014-06-20 12:00:00 -070072static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
David Benjamine4da1072020-06-20 10:55:13 -040073static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
Adam Langley95c29f32014-06-20 12:00:00 -070074static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
David Benjamine4da1072020-06-20 10:55:13 -040075static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
Adam Langley95c29f32014-06-20 12:00:00 -070076static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
77
78ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
Adam Langley57707c72016-01-14 11:25:12 -080079{
80 ASN1_VALUE *ret = NULL;
81 if (ASN1_item_ex_new(&ret, it) > 0)
82 return ret;
83 return NULL;
84}
Adam Langley95c29f32014-06-20 12:00:00 -070085
86/* Allocate an ASN1 structure */
87
88int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
Adam Langley57707c72016-01-14 11:25:12 -080089{
90 return asn1_item_ex_combine_new(pval, it, 0);
91}
Adam Langley95c29f32014-06-20 12:00:00 -070092
93static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
Adam Langley57707c72016-01-14 11:25:12 -080094 int combine)
95{
96 const ASN1_TEMPLATE *tt = NULL;
Adam Langley57707c72016-01-14 11:25:12 -080097 const ASN1_EXTERN_FUNCS *ef;
Adam Langley57707c72016-01-14 11:25:12 -080098 ASN1_VALUE **pseqval;
99 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700100
Adam Langley57707c72016-01-14 11:25:12 -0800101 switch (it->itype) {
Adam Langley95c29f32014-06-20 12:00:00 -0700102
Adam Langley57707c72016-01-14 11:25:12 -0800103 case ASN1_ITYPE_EXTERN:
104 ef = it->funcs;
105 if (ef && ef->asn1_ex_new) {
106 if (!ef->asn1_ex_new(pval, it))
107 goto memerr;
108 }
109 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700110
Adam Langley57707c72016-01-14 11:25:12 -0800111 case ASN1_ITYPE_PRIMITIVE:
112 if (it->templates) {
113 if (!ASN1_template_new(pval, it->templates))
114 goto memerr;
115 } else if (!ASN1_primitive_new(pval, it))
116 goto memerr;
117 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700118
Adam Langley57707c72016-01-14 11:25:12 -0800119 case ASN1_ITYPE_MSTRING:
120 if (!ASN1_primitive_new(pval, it))
121 goto memerr;
122 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700123
David Benjaminafed9f72021-08-22 14:26:34 -0400124 case ASN1_ITYPE_CHOICE: {
125 const ASN1_AUX *aux = it->funcs;
126 ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
Adam Langley57707c72016-01-14 11:25:12 -0800127 if (asn1_cb) {
128 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
129 if (!i)
130 goto auxerr;
131 if (i == 2) {
Adam Langley57707c72016-01-14 11:25:12 -0800132 return 1;
133 }
134 }
135 if (!combine) {
136 *pval = OPENSSL_malloc(it->size);
137 if (!*pval)
138 goto memerr;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500139 OPENSSL_memset(*pval, 0, it->size);
Adam Langley57707c72016-01-14 11:25:12 -0800140 }
141 asn1_set_choice_selector(pval, -1, it);
142 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
David Benjaminc4d59252017-02-09 19:41:18 -0500143 goto auxerr2;
Adam Langley57707c72016-01-14 11:25:12 -0800144 break;
David Benjaminafed9f72021-08-22 14:26:34 -0400145 }
Adam Langley95c29f32014-06-20 12:00:00 -0700146
David Benjaminafed9f72021-08-22 14:26:34 -0400147 case ASN1_ITYPE_SEQUENCE: {
148 const ASN1_AUX *aux = it->funcs;
149 ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
Adam Langley57707c72016-01-14 11:25:12 -0800150 if (asn1_cb) {
151 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
152 if (!i)
153 goto auxerr;
154 if (i == 2) {
Adam Langley57707c72016-01-14 11:25:12 -0800155 return 1;
156 }
157 }
158 if (!combine) {
159 *pval = OPENSSL_malloc(it->size);
160 if (!*pval)
161 goto memerr;
David Benjamin17cf2cb2016-12-13 01:07:13 -0500162 OPENSSL_memset(*pval, 0, it->size);
Adam Langley57707c72016-01-14 11:25:12 -0800163 asn1_refcount_set_one(pval, it);
164 asn1_enc_init(pval, it);
165 }
166 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
167 pseqval = asn1_get_field_ptr(pval, tt);
168 if (!ASN1_template_new(pseqval, tt))
David Benjaminc4d59252017-02-09 19:41:18 -0500169 goto memerr2;
Adam Langley57707c72016-01-14 11:25:12 -0800170 }
171 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
David Benjaminc4d59252017-02-09 19:41:18 -0500172 goto auxerr2;
Adam Langley57707c72016-01-14 11:25:12 -0800173 break;
174 }
David Benjaminafed9f72021-08-22 14:26:34 -0400175 }
Adam Langley57707c72016-01-14 11:25:12 -0800176 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700177
David Benjaminc4d59252017-02-09 19:41:18 -0500178 memerr2:
David Benjamin4f94a832017-10-30 11:00:42 -0400179 asn1_item_combine_free(pval, it, combine);
Adam Langley57707c72016-01-14 11:25:12 -0800180 memerr:
181 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
Adam Langley57707c72016-01-14 11:25:12 -0800182 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700183
David Benjaminc4d59252017-02-09 19:41:18 -0500184 auxerr2:
David Benjamin4f94a832017-10-30 11:00:42 -0400185 asn1_item_combine_free(pval, it, combine);
Adam Langley57707c72016-01-14 11:25:12 -0800186 auxerr:
187 OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR);
Adam Langley57707c72016-01-14 11:25:12 -0800188 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700189
Adam Langley57707c72016-01-14 11:25:12 -0800190}
Adam Langley95c29f32014-06-20 12:00:00 -0700191
192static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
Adam Langley57707c72016-01-14 11:25:12 -0800193{
194 const ASN1_EXTERN_FUNCS *ef;
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langley57707c72016-01-14 11:25:12 -0800196 switch (it->itype) {
Adam Langley95c29f32014-06-20 12:00:00 -0700197
Adam Langley57707c72016-01-14 11:25:12 -0800198 case ASN1_ITYPE_EXTERN:
199 ef = it->funcs;
200 if (ef && ef->asn1_ex_clear)
201 ef->asn1_ex_clear(pval, it);
202 else
203 *pval = NULL;
204 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700205
Adam Langley57707c72016-01-14 11:25:12 -0800206 case ASN1_ITYPE_PRIMITIVE:
207 if (it->templates)
208 asn1_template_clear(pval, it->templates);
209 else
210 asn1_primitive_clear(pval, it);
211 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700212
Adam Langley57707c72016-01-14 11:25:12 -0800213 case ASN1_ITYPE_MSTRING:
214 asn1_primitive_clear(pval, it);
215 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700216
Adam Langley57707c72016-01-14 11:25:12 -0800217 case ASN1_ITYPE_CHOICE:
218 case ASN1_ITYPE_SEQUENCE:
Adam Langley57707c72016-01-14 11:25:12 -0800219 *pval = NULL;
220 break;
221 }
222}
Adam Langley95c29f32014-06-20 12:00:00 -0700223
David Benjamine4da1072020-06-20 10:55:13 -0400224static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
Adam Langley57707c72016-01-14 11:25:12 -0800225{
226 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
227 int ret;
228 if (tt->flags & ASN1_TFLG_OPTIONAL) {
229 asn1_template_clear(pval, tt);
230 return 1;
231 }
232 /* If ANY DEFINED BY nothing to do */
Adam Langley95c29f32014-06-20 12:00:00 -0700233
Adam Langley57707c72016-01-14 11:25:12 -0800234 if (tt->flags & ASN1_TFLG_ADB_MASK) {
235 *pval = NULL;
236 return 1;
237 }
Adam Langley57707c72016-01-14 11:25:12 -0800238 /* If SET OF or SEQUENCE OF, its a STACK */
239 if (tt->flags & ASN1_TFLG_SK_MASK) {
240 STACK_OF(ASN1_VALUE) *skval;
241 skval = sk_ASN1_VALUE_new_null();
242 if (!skval) {
243 OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
244 ret = 0;
245 goto done;
246 }
247 *pval = (ASN1_VALUE *)skval;
248 ret = 1;
249 goto done;
250 }
251 /* Otherwise pass it back to the item routine */
252 ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
253 done:
Adam Langley57707c72016-01-14 11:25:12 -0800254 return ret;
255}
Adam Langley95c29f32014-06-20 12:00:00 -0700256
257static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
Adam Langley57707c72016-01-14 11:25:12 -0800258{
259 /* If ADB or STACK just NULL the field */
260 if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
261 *pval = NULL;
262 else
263 asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
264}
Adam Langley95c29f32014-06-20 12:00:00 -0700265
Adam Langley57707c72016-01-14 11:25:12 -0800266/*
267 * NB: could probably combine most of the real XXX_new() behaviour and junk
Adam Langley95c29f32014-06-20 12:00:00 -0700268 * all the old functions.
269 */
270
David Benjamine4da1072020-06-20 10:55:13 -0400271static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
Adam Langley57707c72016-01-14 11:25:12 -0800272{
273 ASN1_TYPE *typ;
Adam Langley57707c72016-01-14 11:25:12 -0800274 int utype;
Adam Langley95c29f32014-06-20 12:00:00 -0700275
Adam Langley57707c72016-01-14 11:25:12 -0800276 if (!it)
277 return 0;
David Benjamine30f3fb2015-03-18 19:55:40 -0400278
David Benjamin3de59492020-06-20 03:05:47 -0400279 /* Historically, |it->funcs| for primitive types contained an
280 * |ASN1_PRIMITIVE_FUNCS| table of calbacks. */
281 assert(it->funcs == NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700282
Adam Langley57707c72016-01-14 11:25:12 -0800283 if (it->itype == ASN1_ITYPE_MSTRING)
284 utype = -1;
285 else
286 utype = it->utype;
287 switch (utype) {
288 case V_ASN1_OBJECT:
289 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
290 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langley57707c72016-01-14 11:25:12 -0800292 case V_ASN1_BOOLEAN:
293 *(ASN1_BOOLEAN *)pval = it->size;
294 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700295
Adam Langley57707c72016-01-14 11:25:12 -0800296 case V_ASN1_NULL:
297 *pval = (ASN1_VALUE *)1;
298 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700299
Adam Langley57707c72016-01-14 11:25:12 -0800300 case V_ASN1_ANY:
301 typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
302 if (!typ)
303 return 0;
304 typ->value.ptr = NULL;
305 typ->type = -1;
306 *pval = (ASN1_VALUE *)typ;
307 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langley57707c72016-01-14 11:25:12 -0800309 default:
David Benjamin046fc132021-08-01 15:20:53 -0400310 *pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
Adam Langley57707c72016-01-14 11:25:12 -0800311 break;
312 }
313 if (*pval)
314 return 1;
315 return 0;
316}
Adam Langley95c29f32014-06-20 12:00:00 -0700317
318static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
Adam Langley57707c72016-01-14 11:25:12 -0800319{
320 int utype;
David Benjamin3de59492020-06-20 03:05:47 -0400321 /* Historically, |it->funcs| for primitive types contained an
322 * |ASN1_PRIMITIVE_FUNCS| table of calbacks. */
323 assert(it == NULL || it->funcs == NULL);
Adam Langley57707c72016-01-14 11:25:12 -0800324 if (!it || (it->itype == ASN1_ITYPE_MSTRING))
325 utype = -1;
326 else
327 utype = it->utype;
328 if (utype == V_ASN1_BOOLEAN)
329 *(ASN1_BOOLEAN *)pval = it->size;
330 else
331 *pval = NULL;
332}