Coverage Report

Created: 2024-11-21 07:03

/src/boringssl/crypto/asn1/tasn_new.c
Line
Count
Source (jump to first uncovered line)
1
/* 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
59
#include <string.h>
60
61
#include <openssl/asn1t.h>
62
#include <openssl/err.h>
63
#include <openssl/mem.h>
64
#include <openssl/obj.h>
65
66
#include "../internal.h"
67
#include "internal.h"
68
69
70
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
71
static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
72
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
73
static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
74
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
75
76
0
ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it) {
77
0
  ASN1_VALUE *ret = NULL;
78
0
  if (ASN1_item_ex_new(&ret, it) > 0) {
79
0
    return ret;
80
0
  }
81
0
  return NULL;
82
0
}
83
84
// Allocate an ASN1 structure
85
86
0
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) {
87
0
  const ASN1_TEMPLATE *tt = NULL;
88
0
  const ASN1_EXTERN_FUNCS *ef;
89
0
  ASN1_VALUE **pseqval;
90
0
  int i;
91
92
0
  switch (it->itype) {
93
0
    case ASN1_ITYPE_EXTERN:
94
0
      ef = it->funcs;
95
0
      if (ef && ef->asn1_ex_new) {
96
0
        if (!ef->asn1_ex_new(pval, it)) {
97
0
          goto memerr;
98
0
        }
99
0
      }
100
0
      break;
101
102
0
    case ASN1_ITYPE_PRIMITIVE:
103
0
      if (it->templates) {
104
0
        if (!ASN1_template_new(pval, it->templates)) {
105
0
          goto memerr;
106
0
        }
107
0
      } else if (!ASN1_primitive_new(pval, it)) {
108
0
        goto memerr;
109
0
      }
110
0
      break;
111
112
0
    case ASN1_ITYPE_MSTRING:
113
0
      if (!ASN1_primitive_new(pval, it)) {
114
0
        goto memerr;
115
0
      }
116
0
      break;
117
118
0
    case ASN1_ITYPE_CHOICE: {
119
0
      const ASN1_AUX *aux = it->funcs;
120
0
      ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
121
0
      if (asn1_cb) {
122
0
        i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
123
0
        if (!i) {
124
0
          goto auxerr;
125
0
        }
126
0
        if (i == 2) {
127
0
          return 1;
128
0
        }
129
0
      }
130
0
      *pval = OPENSSL_zalloc(it->size);
131
0
      if (!*pval) {
132
0
        goto memerr;
133
0
      }
134
0
      asn1_set_choice_selector(pval, -1, it);
135
0
      if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) {
136
0
        goto auxerr2;
137
0
      }
138
0
      break;
139
0
    }
140
141
0
    case ASN1_ITYPE_SEQUENCE: {
142
0
      const ASN1_AUX *aux = it->funcs;
143
0
      ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
144
0
      if (asn1_cb) {
145
0
        i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
146
0
        if (!i) {
147
0
          goto auxerr;
148
0
        }
149
0
        if (i == 2) {
150
0
          return 1;
151
0
        }
152
0
      }
153
0
      *pval = OPENSSL_zalloc(it->size);
154
0
      if (!*pval) {
155
0
        goto memerr;
156
0
      }
157
0
      asn1_refcount_set_one(pval, it);
158
0
      asn1_enc_init(pval, it);
159
0
      for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
160
0
        pseqval = asn1_get_field_ptr(pval, tt);
161
0
        if (!ASN1_template_new(pseqval, tt)) {
162
0
          goto memerr2;
163
0
        }
164
0
      }
165
0
      if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) {
166
0
        goto auxerr2;
167
0
      }
168
0
      break;
169
0
    }
170
0
  }
171
0
  return 1;
172
173
0
memerr2:
174
0
  ASN1_item_ex_free(pval, it);
175
0
memerr:
176
0
  return 0;
177
178
0
auxerr2:
179
0
  ASN1_item_ex_free(pval, it);
180
0
auxerr:
181
0
  OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR);
182
0
  return 0;
183
0
}
184
185
0
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) {
186
0
  switch (it->itype) {
187
0
    case ASN1_ITYPE_EXTERN:
188
0
      *pval = NULL;
189
0
      break;
190
191
0
    case ASN1_ITYPE_PRIMITIVE:
192
0
      if (it->templates) {
193
0
        asn1_template_clear(pval, it->templates);
194
0
      } else {
195
0
        asn1_primitive_clear(pval, it);
196
0
      }
197
0
      break;
198
199
0
    case ASN1_ITYPE_MSTRING:
200
0
      asn1_primitive_clear(pval, it);
201
0
      break;
202
203
0
    case ASN1_ITYPE_CHOICE:
204
0
    case ASN1_ITYPE_SEQUENCE:
205
0
      *pval = NULL;
206
0
      break;
207
0
  }
208
0
}
209
210
0
static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
211
0
  const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
212
0
  int ret;
213
0
  if (tt->flags & ASN1_TFLG_OPTIONAL) {
214
0
    asn1_template_clear(pval, tt);
215
0
    return 1;
216
0
  }
217
  // If ANY DEFINED BY nothing to do
218
219
0
  if (tt->flags & ASN1_TFLG_ADB_MASK) {
220
0
    *pval = NULL;
221
0
    return 1;
222
0
  }
223
  // If SET OF or SEQUENCE OF, its a STACK
224
0
  if (tt->flags & ASN1_TFLG_SK_MASK) {
225
0
    STACK_OF(ASN1_VALUE) *skval;
226
0
    skval = sk_ASN1_VALUE_new_null();
227
0
    if (!skval) {
228
0
      ret = 0;
229
0
      goto done;
230
0
    }
231
0
    *pval = (ASN1_VALUE *)skval;
232
0
    ret = 1;
233
0
    goto done;
234
0
  }
235
  // Otherwise pass it back to the item routine
236
0
  ret = ASN1_item_ex_new(pval, it);
237
0
done:
238
0
  return ret;
239
0
}
240
241
0
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
242
  // If ADB or STACK just NULL the field
243
0
  if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK)) {
244
0
    *pval = NULL;
245
0
  } else {
246
0
    asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
247
0
  }
248
0
}
249
250
// NB: could probably combine most of the real XXX_new() behaviour and junk
251
// all the old functions.
252
253
0
static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) {
254
0
  if (!it) {
255
0
    return 0;
256
0
  }
257
258
  // Historically, |it->funcs| for primitive types contained an
259
  // |ASN1_PRIMITIVE_FUNCS| table of calbacks.
260
0
  assert(it->funcs == NULL);
261
262
0
  int utype;
263
0
  if (it->itype == ASN1_ITYPE_MSTRING) {
264
0
    utype = -1;
265
0
  } else {
266
0
    utype = it->utype;
267
0
  }
268
0
  switch (utype) {
269
0
    case V_ASN1_OBJECT:
270
0
      *pval = (ASN1_VALUE *)OBJ_get_undef();
271
0
      return 1;
272
273
0
    case V_ASN1_BOOLEAN:
274
0
      *(ASN1_BOOLEAN *)pval = (ASN1_BOOLEAN)it->size;
275
0
      return 1;
276
277
0
    case V_ASN1_NULL:
278
0
      *pval = (ASN1_VALUE *)1;
279
0
      return 1;
280
281
0
    case V_ASN1_ANY: {
282
0
      ASN1_TYPE *typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
283
0
      if (!typ) {
284
0
        return 0;
285
0
      }
286
0
      typ->value.ptr = NULL;
287
0
      typ->type = -1;
288
0
      *pval = (ASN1_VALUE *)typ;
289
0
      break;
290
0
    }
291
292
0
    default:
293
0
      *pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
294
0
      break;
295
0
  }
296
0
  if (*pval) {
297
0
    return 1;
298
0
  }
299
0
  return 0;
300
0
}
301
302
0
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) {
303
0
  int utype;
304
  // Historically, |it->funcs| for primitive types contained an
305
  // |ASN1_PRIMITIVE_FUNCS| table of calbacks.
306
0
  assert(it == NULL || it->funcs == NULL);
307
0
  if (!it || (it->itype == ASN1_ITYPE_MSTRING)) {
308
0
    utype = -1;
309
0
  } else {
310
0
    utype = it->utype;
311
0
  }
312
0
  if (utype == V_ASN1_BOOLEAN) {
313
0
    *(ASN1_BOOLEAN *)pval = (ASN1_BOOLEAN)it->size;
314
0
  } else {
315
0
    *pval = NULL;
316
0
  }
317
0
}