tasn_new.c
Go to the documentation of this file.
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 int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
71  int combine);
72 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
73 static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
74 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
75 static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
76 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
77 
79 {
80  ASN1_VALUE *ret = NULL;
81  if (ASN1_item_ex_new(&ret, it) > 0)
82  return ret;
83  return NULL;
84 }
85 
86 /* Allocate an ASN1 structure */
87 
89 {
90  return asn1_item_ex_combine_new(pval, it, 0);
91 }
92 
93 static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
94  int combine)
95 {
96  const ASN1_TEMPLATE *tt = NULL;
97  const ASN1_EXTERN_FUNCS *ef;
98  ASN1_VALUE **pseqval;
99  int i;
100 
101  switch (it->itype) {
102 
103  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;
110 
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;
118 
119  case ASN1_ITYPE_MSTRING:
120  if (!ASN1_primitive_new(pval, it))
121  goto memerr;
122  break;
123 
124  case ASN1_ITYPE_CHOICE: {
125  const ASN1_AUX *aux = it->funcs;
126  ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
127  if (asn1_cb) {
128  i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
129  if (!i)
130  goto auxerr;
131  if (i == 2) {
132  return 1;
133  }
134  }
135  if (!combine) {
136  *pval = OPENSSL_malloc(it->size);
137  if (!*pval)
138  goto memerr;
139  OPENSSL_memset(*pval, 0, it->size);
140  }
141  asn1_set_choice_selector(pval, -1, it);
142  if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
143  goto auxerr2;
144  break;
145  }
146 
147  case ASN1_ITYPE_SEQUENCE: {
148  const ASN1_AUX *aux = it->funcs;
149  ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
150  if (asn1_cb) {
151  i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
152  if (!i)
153  goto auxerr;
154  if (i == 2) {
155  return 1;
156  }
157  }
158  if (!combine) {
159  *pval = OPENSSL_malloc(it->size);
160  if (!*pval)
161  goto memerr;
162  OPENSSL_memset(*pval, 0, it->size);
163  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))
169  goto memerr2;
170  }
171  if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
172  goto auxerr2;
173  break;
174  }
175  }
176  return 1;
177 
178  memerr2:
179  asn1_item_combine_free(pval, it, combine);
180  memerr:
182  return 0;
183 
184  auxerr2:
185  asn1_item_combine_free(pval, it, combine);
186  auxerr:
188  return 0;
189 
190 }
191 
192 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
193 {
194  const ASN1_EXTERN_FUNCS *ef;
195 
196  switch (it->itype) {
197 
198  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;
205 
207  if (it->templates)
208  asn1_template_clear(pval, it->templates);
209  else
210  asn1_primitive_clear(pval, it);
211  break;
212 
213  case ASN1_ITYPE_MSTRING:
214  asn1_primitive_clear(pval, it);
215  break;
216 
217  case ASN1_ITYPE_CHOICE:
218  case ASN1_ITYPE_SEQUENCE:
219  *pval = NULL;
220  break;
221  }
222 }
223 
224 static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
225 {
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 */
233 
234  if (tt->flags & ASN1_TFLG_ADB_MASK) {
235  *pval = NULL;
236  return 1;
237  }
238  /* 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) {
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 */
253  done:
254  return ret;
255 }
256 
257 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
258 {
259  /* If ADB or STACK just NULL the field */
261  *pval = NULL;
262  else
263  asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
264 }
265 
266 /*
267  * NB: could probably combine most of the real XXX_new() behaviour and junk
268  * all the old functions.
269  */
270 
271 static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
272 {
273  ASN1_TYPE *typ;
274  int utype;
275 
276  if (!it)
277  return 0;
278 
279  /* Historically, |it->funcs| for primitive types contained an
280  * |ASN1_PRIMITIVE_FUNCS| table of calbacks. */
281  assert(it->funcs == NULL);
282 
283  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;
291 
292  case V_ASN1_BOOLEAN:
293  *(ASN1_BOOLEAN *)pval = it->size;
294  return 1;
295 
296  case V_ASN1_NULL:
297  *pval = (ASN1_VALUE *)1;
298  return 1;
299 
300  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;
308 
309  default:
310  *pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
311  break;
312  }
313  if (*pval)
314  return 1;
315  return 0;
316 }
317 
318 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
319 {
320  int utype;
321  /* Historically, |it->funcs| for primitive types contained an
322  * |ASN1_PRIMITIVE_FUNCS| table of calbacks. */
323  assert(it == NULL || it->funcs == NULL);
324  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 }
ASN1_TFLG_ADB_MASK
#define ASN1_TFLG_ADB_MASK
Definition: asn1t.h:441
ASN1_ITYPE_CHOICE
#define ASN1_ITYPE_CHOICE
Definition: asn1t.h:509
V_ASN1_ANY
#define V_ASN1_ANY
Definition: asn1.h:121
asn1_template_clear
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
Definition: tasn_new.c:257
ASN1_TEMPLATE_st::item
ASN1_ITEM_EXP * item
Definition: asn1t.h:356
ASN1_OP_NEW_PRE
#define ASN1_OP_NEW_PRE
Definition: asn1t.h:595
ASN1_TFLG_SK_MASK
#define ASN1_TFLG_SK_MASK
Definition: asn1t.h:396
regen-readme.it
it
Definition: regen-readme.py:15
ASN1_BOOLEAN
int ASN1_BOOLEAN
Definition: base.h:335
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
string.h
ASN1_EXTERN_FUNCS_st
Definition: asn1t.h:547
ASN1_STRING_type_new
#define ASN1_STRING_type_new
Definition: boringssl_prefix_symbols.h:695
ASN1_item_new
ASN1_VALUE * ASN1_item_new(const ASN1_ITEM *it)
Definition: tasn_new.c:78
asn1_cb
static int asn1_cb(const char *elem, int len, void *bitstr)
Definition: asn1_gen.c:287
ASN1_ITYPE_SEQUENCE
#define ASN1_ITYPE_SEQUENCE
Definition: asn1t.h:507
asn1_item_clear
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
Definition: tasn_new.c:192
ASN1_ITEM_ptr
#define ASN1_ITEM_ptr(iptr)
Definition: asn1.h:316
ASN1_EXTERN_FUNCS_st::asn1_ex_new
ASN1_ex_new_func * asn1_ex_new
Definition: asn1t.h:549
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
OBJ_nid2obj
#define OBJ_nid2obj
Definition: boringssl_prefix_symbols.h:1855
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
ASN1_TFLG_COMBINE
#define ASN1_TFLG_COMBINE
Definition: asn1t.h:455
asn1_get_field_ptr
#define asn1_get_field_ptr
Definition: boringssl_prefix_symbols.h:2835
ASN1_AUX_st::asn1_cb
ASN1_aux_cb * asn1_cb
Definition: asn1t.h:582
ASN1_TFLG_OPTIONAL
#define ASN1_TFLG_OPTIONAL
Definition: asn1t.h:387
asn1_type_st::type
int type
Definition: asn1.h:1482
ASN1_ITEM_st
Definition: asn1t.h:459
internal.h
STACK_OF
#define STACK_OF(type)
Definition: stack.h:125
ASN1_EXTERN_FUNCS_st::asn1_ex_clear
ASN1_ex_free_func * asn1_ex_clear
Definition: asn1t.h:551
ASN1_primitive_new
static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
Definition: tasn_new.c:271
done
struct tab * done
Definition: bloaty/third_party/zlib/examples/enough.c:176
err.h
asn1t.h
ASN1_ITYPE_EXTERN
#define ASN1_ITYPE_EXTERN
Definition: asn1t.h:511
NID_undef
#define NID_undef
Definition: nid.h:85
ASN1_item_ex_new
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
Definition: tasn_new.c:88
asn1_set_choice_selector
#define asn1_set_choice_selector
Definition: boringssl_prefix_symbols.h:2841
asn1_item_ex_combine_new
static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
Definition: tasn_new.c:93
ASN1_OP_NEW_POST
#define ASN1_OP_NEW_POST
Definition: asn1t.h:596
V_ASN1_BOOLEAN
#define V_ASN1_BOOLEAN
Definition: asn1.h:125
asn1_enc_init
#define asn1_enc_init
Definition: boringssl_prefix_symbols.h:2830
ASN1_aux_cb
int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it, void *exarg)
Definition: asn1t.h:575
ASN1_template_new
static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
Definition: tasn_new.c:224
asn1_type_st::ptr
char * ptr
Definition: asn1.h:1484
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
asn1_refcount_set_one
#define asn1_refcount_set_one
Definition: boringssl_prefix_symbols.h:2840
ASN1_TEMPLATE_st
Definition: asn1t.h:351
V_ASN1_NULL
#define V_ASN1_NULL
Definition: asn1.h:129
ASN1_VALUE
struct ASN1_VALUE_st ASN1_VALUE
Definition: asn1.h:320
ASN1_ITYPE_MSTRING
#define ASN1_ITYPE_MSTRING
Definition: asn1t.h:513
ASN1_TEMPLATE_st::flags
unsigned long flags
Definition: asn1t.h:352
obj.h
asn1_type_st::value
union asn1_type_st::@361 value
ASN1_ITYPE_PRIMITIVE
#define ASN1_ITYPE_PRIMITIVE
Definition: asn1t.h:505
asn1_primitive_clear
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
Definition: tasn_new.c:318
asn1_item_combine_free
#define asn1_item_combine_free
Definition: boringssl_prefix_symbols.h:2838
mem.h
asn1_type_st
Definition: asn1.h:1481
ASN1_AUX_st
Definition: asn1t.h:578
V_ASN1_OBJECT
#define V_ASN1_OBJECT
Definition: asn1.h:130
ASN1_R_AUX_ERROR
#define ASN1_R_AUX_ERROR
Definition: asn1.h:1944
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
asn1.h
ERR_R_MALLOC_FAILURE
#define ERR_R_MALLOC_FAILURE
Definition: err.h:371


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:25