Coverage Report

Created: 2022-08-24 06:30

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