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
2.18k
{
77
2.18k
  ASN1_VALUE *ret = NULL;
78
2.18k
  if (ASN1_item_ex_new(&ret, it) > 0)
79
2.18k
    return ret;
80
0
  return NULL;
81
2.18k
}
82
83
/* Allocate an ASN1 structure */
84
85
int
86
ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
87
2.18k
{
88
2.18k
  return asn1_item_ex_new(pval, it);
89
2.18k
}
90
91
static int
92
asn1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
93
2.18k
{
94
2.18k
  const ASN1_TEMPLATE *tt = NULL;
95
2.18k
  const ASN1_EXTERN_FUNCS *ef;
96
2.18k
  const ASN1_AUX *aux = it->funcs;
97
2.18k
  ASN1_aux_cb *asn1_cb = NULL;
98
2.18k
  ASN1_VALUE **pseqval;
99
2.18k
  int i;
100
101
2.18k
  if (aux != NULL && aux->asn1_cb != NULL)
102
0
    asn1_cb = aux->asn1_cb;
103
104
2.18k
  *pval = NULL;
105
106
2.18k
  switch (it->itype) {
107
0
  case ASN1_ITYPE_EXTERN:
108
0
    ef = it->funcs;
109
0
    if (ef && ef->asn1_ex_new) {
110
0
      if (!ef->asn1_ex_new(pval, it))
111
0
        goto memerr;
112
0
    }
113
0
    break;
114
115
2.18k
  case ASN1_ITYPE_PRIMITIVE:
116
2.18k
    if (it->templates) {
117
0
      if (!ASN1_template_new(pval, it->templates))
118
0
        goto memerr;
119
2.18k
    } else if (!ASN1_primitive_new(pval, it))
120
0
      goto memerr;
121
2.18k
    break;
122
123
2.18k
  case ASN1_ITYPE_MSTRING:
124
0
    if (!ASN1_primitive_new(pval, it))
125
0
      goto memerr;
126
0
    break;
127
128
0
  case ASN1_ITYPE_CHOICE:
129
0
    if (asn1_cb) {
130
0
      i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
131
0
      if (!i)
132
0
        goto auxerr;
133
0
      if (i == 2) {
134
0
        return 1;
135
0
      }
136
0
    }
137
0
    *pval = calloc(1, it->size);
138
0
    if (!*pval)
139
0
      goto memerr;
140
0
    asn1_set_choice_selector(pval, -1, it);
141
0
    if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
142
0
      goto auxerr;
143
0
    break;
144
145
0
  case ASN1_ITYPE_NDEF_SEQUENCE:
146
0
  case ASN1_ITYPE_SEQUENCE:
147
0
    if (asn1_cb) {
148
0
      i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
149
0
      if (!i)
150
0
        goto auxerr;
151
0
      if (i == 2) {
152
0
        return 1;
153
0
      }
154
0
    }
155
0
    *pval = calloc(1, it->size);
156
0
    if (!*pval)
157
0
      goto memerr;
158
0
    asn1_do_lock(pval, 0, it);
159
0
    asn1_enc_init(pval, it);
160
0
    for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
161
0
      pseqval = asn1_get_field_ptr(pval, tt);
162
0
      if (!ASN1_template_new(pseqval, tt))
163
0
        goto memerr;
164
0
    }
165
0
    if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
166
0
      goto auxerr;
167
0
    break;
168
2.18k
  }
169
2.18k
  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.18k
}
181
182
static void
183
asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
184
0
{
185
0
  const ASN1_EXTERN_FUNCS *ef;
186
187
0
  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
0
  case ASN1_ITYPE_PRIMITIVE:
197
0
    if (it->templates)
198
0
      asn1_template_clear(pval, it->templates);
199
0
    else
200
0
      asn1_primitive_clear(pval, it);
201
0
    break;
202
203
0
  case ASN1_ITYPE_MSTRING:
204
0
    asn1_primitive_clear(pval, it);
205
0
    break;
206
207
0
  case ASN1_ITYPE_CHOICE:
208
0
  case ASN1_ITYPE_SEQUENCE:
209
0
  case ASN1_ITYPE_NDEF_SEQUENCE:
210
0
    *pval = NULL;
211
0
    break;
212
0
  }
213
0
}
214
215
int
216
ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
217
0
{
218
0
  const ASN1_ITEM *it = tt->item;
219
0
  int ret;
220
221
0
  if (tt->flags & ASN1_TFLG_OPTIONAL) {
222
0
    asn1_template_clear(pval, tt);
223
0
    return 1;
224
0
  }
225
  /* If ANY DEFINED BY nothing to do */
226
227
0
  if (tt->flags & ASN1_TFLG_ADB_MASK) {
228
0
    *pval = NULL;
229
0
    return 1;
230
0
  }
231
  /* If SET OF or SEQUENCE OF, its a STACK */
232
0
  if (tt->flags & ASN1_TFLG_SK_MASK) {
233
0
    STACK_OF(ASN1_VALUE) *skval;
234
0
    skval = sk_ASN1_VALUE_new_null();
235
0
    if (!skval) {
236
0
      ASN1error(ERR_R_MALLOC_FAILURE);
237
0
      ret = 0;
238
0
      goto done;
239
0
    }
240
0
    *pval = (ASN1_VALUE *)skval;
241
0
    ret = 1;
242
0
    goto done;
243
0
  }
244
  /* Otherwise pass it back to the item routine */
245
0
  ret = asn1_item_ex_new(pval, it);
246
0
 done:
247
0
  return ret;
248
0
}
249
250
static void
251
asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
252
0
{
253
  /* If ADB or STACK just NULL the field */
254
0
  if (tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK))
255
0
    *pval = NULL;
256
0
  else
257
0
    asn1_item_clear(pval, tt->item);
258
0
}
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
2.18k
{
268
2.18k
  ASN1_TYPE *typ;
269
2.18k
  ASN1_STRING *str;
270
2.18k
  int utype;
271
272
2.18k
  if (it != NULL && it->funcs != NULL) {
273
0
    const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
274
275
0
    if (pf->prim_new == NULL)
276
0
      return 0;
277
0
    return pf->prim_new(pval, it);
278
0
  }
279
280
2.18k
  if (!it || (it->itype == ASN1_ITYPE_MSTRING))
281
0
    utype = V_ASN1_UNDEF;
282
2.18k
  else
283
2.18k
    utype = it->utype;
284
2.18k
  switch (utype) {
285
0
  case V_ASN1_OBJECT:
286
0
    *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
287
0
    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
0
  case V_ASN1_ANY:
298
0
    typ = malloc(sizeof(ASN1_TYPE));
299
0
    if (typ != NULL) {
300
0
      typ->value.ptr = NULL;
301
0
      typ->type = V_ASN1_UNDEF;
302
0
    }
303
0
    *pval = (ASN1_VALUE *)typ;
304
0
    break;
305
306
2.18k
  default:
307
2.18k
    str = ASN1_STRING_type_new(utype);
308
2.18k
    if (it != NULL && it->itype == ASN1_ITYPE_MSTRING &&
309
2.18k
        str != NULL)
310
0
      str->flags |= ASN1_STRING_FLAG_MSTRING;
311
2.18k
    *pval = (ASN1_VALUE *)str;
312
2.18k
    break;
313
2.18k
  }
314
2.18k
  if (*pval)
315
2.18k
    return 1;
316
0
  return 0;
317
2.18k
}
318
319
static void
320
asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
321
0
{
322
0
  int utype;
323
324
0
  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
0
  if (!it || (it->itype == ASN1_ITYPE_MSTRING))
335
0
    utype = V_ASN1_UNDEF;
336
0
  else
337
0
    utype = it->utype;
338
0
  if (utype == V_ASN1_BOOLEAN)
339
0
    *(ASN1_BOOLEAN *)pval = it->size;
340
0
  else
341
0
    *pval = NULL;
342
0
}