tasn_utl.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 <assert.h>
60 #include <string.h>
61 
62 #include <openssl/asn1t.h>
63 #include <openssl/mem.h>
64 #include <openssl/obj.h>
65 #include <openssl/err.h>
66 #include <openssl/thread.h>
67 
68 #include "../internal.h"
69 #include "internal.h"
70 
71 
72 /* Utility functions for manipulating fields and offsets */
73 
74 /* Add 'offset' to 'addr' */
75 #define offset2ptr(addr, offset) (void *)(((char *)(addr)) + (offset))
76 
77 /* Given an ASN1_ITEM CHOICE type return the selector value */
79  int *sel = offset2ptr(*pval, it->utype);
80  return *sel;
81 }
82 
83 /* Given an ASN1_ITEM CHOICE type set the selector value, return old value. */
85  const ASN1_ITEM *it) {
86  int *sel, ret;
87  sel = offset2ptr(*pval, it->utype);
88  ret = *sel;
89  *sel = value;
90  return ret;
91 }
92 
94  const ASN1_ITEM *it) {
95  if (it->itype != ASN1_ITYPE_SEQUENCE) {
96  return NULL;
97  }
98  const ASN1_AUX *aux = it->funcs;
99  if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) {
100  return NULL;
101  }
102  return offset2ptr(*pval, aux->ref_offset);
103 }
104 
106  CRYPTO_refcount_t *references = asn1_get_references(pval, it);
107  if (references != NULL) {
108  *references = 1;
109  }
110 }
111 
113  CRYPTO_refcount_t *references = asn1_get_references(pval, it);
114  if (references != NULL) {
115  return CRYPTO_refcount_dec_and_test_zero(references);
116  }
117  return 1;
118 }
119 
121  assert(it->itype == ASN1_ITYPE_SEQUENCE);
122  const ASN1_AUX *aux;
123  if (!pval || !*pval) {
124  return NULL;
125  }
126  aux = it->funcs;
127  if (!aux || !(aux->flags & ASN1_AFLG_ENCODING)) {
128  return NULL;
129  }
130  return offset2ptr(*pval, aux->enc_offset);
131 }
132 
133 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) {
134  ASN1_ENCODING *enc;
135  enc = asn1_get_enc_ptr(pval, it);
136  if (enc) {
137  enc->enc = NULL;
138  enc->len = 0;
139  enc->alias_only = 0;
140  enc->alias_only_on_next_parse = 0;
141  enc->modified = 1;
142  }
143 }
144 
145 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
146  ASN1_ENCODING *enc;
147  enc = asn1_get_enc_ptr(pval, it);
148  if (enc) {
149  if (enc->enc && !enc->alias_only) {
150  OPENSSL_free(enc->enc);
151  }
152  enc->enc = NULL;
153  enc->len = 0;
154  enc->alias_only = 0;
155  enc->alias_only_on_next_parse = 0;
156  enc->modified = 1;
157  }
158 }
159 
160 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
161  const ASN1_ITEM *it) {
162  ASN1_ENCODING *enc;
163  enc = asn1_get_enc_ptr(pval, it);
164  if (!enc) {
165  return 1;
166  }
167 
168  if (!enc->alias_only) {
169  OPENSSL_free(enc->enc);
170  }
171 
173  enc->alias_only_on_next_parse = 0;
174 
175  if (enc->alias_only) {
176  enc->enc = (uint8_t *) in;
177  } else {
178  enc->enc = OPENSSL_malloc(inlen);
179  if (!enc->enc) {
180  return 0;
181  }
182  OPENSSL_memcpy(enc->enc, in, inlen);
183  }
184 
185  enc->len = inlen;
186  enc->modified = 0;
187 
188  return 1;
189 }
190 
191 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
192  const ASN1_ITEM *it) {
193  ASN1_ENCODING *enc;
194  enc = asn1_get_enc_ptr(pval, it);
195  if (!enc || enc->modified) {
196  return 0;
197  }
198  if (out) {
199  OPENSSL_memcpy(*out, enc->enc, enc->len);
200  *out += enc->len;
201  }
202  if (len) {
203  *len = enc->len;
204  }
205  return 1;
206 }
207 
208 /* Given an ASN1_TEMPLATE get a pointer to a field */
210  ASN1_VALUE **pvaltmp;
211  if (tt->flags & ASN1_TFLG_COMBINE) {
212  return pval;
213  }
214  pvaltmp = offset2ptr(*pval, tt->offset);
215  /* NOTE for BOOLEAN types the field is just a plain int so we can't return
216  * int **, so settle for (int *). */
217  return pvaltmp;
218 }
219 
220 /* Handle ANY DEFINED BY template, find the selector, look up the relevant
221  * ASN1_TEMPLATE in the table and return it. */
223  int nullerr) {
224  const ASN1_ADB *adb;
225  const ASN1_ADB_TABLE *atbl;
226  long selector;
227  ASN1_VALUE **sfld;
228  int i;
229  if (!(tt->flags & ASN1_TFLG_ADB_MASK)) {
230  return tt;
231  }
232 
233  /* Else ANY DEFINED BY ... get the table */
234  adb = ASN1_ADB_ptr(tt->item);
235 
236  /* Get the selector field */
237  sfld = offset2ptr(*pval, adb->offset);
238 
239  /* Check if NULL */
240  if (*sfld == NULL) {
241  if (!adb->null_tt) {
242  goto err;
243  }
244  return adb->null_tt;
245  }
246 
247  /* Convert type to a long:
248  * NB: don't check for NID_undef here because it
249  * might be a legitimate value in the table */
250  if (tt->flags & ASN1_TFLG_ADB_OID) {
251  selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
252  } else {
253  selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
254  }
255 
256  /* Try to find matching entry in table Maybe should check application types
257  * first to allow application override? Might also be useful to have a flag
258  * which indicates table is sorted and we can do a binary search. For now
259  * stick to a linear search. */
260 
261  for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) {
262  if (atbl->value == selector) {
263  return &atbl->tt;
264  }
265  }
266 
267  /* FIXME: need to search application table too */
268 
269  /* No match, return default type */
270  if (!adb->default_tt) {
271  goto err;
272  }
273  return adb->default_tt;
274 
275 err:
276  /* FIXME: should log the value or OID of unsupported type */
277  if (nullerr) {
279  }
280  return NULL;
281 }
ASN1_TFLG_ADB_MASK
#define ASN1_TFLG_ADB_MASK
Definition: asn1t.h:441
ASN1_ADB_st::default_tt
const ASN1_TEMPLATE * default_tt
Definition: asn1t.h:375
asn1_enc_save
int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, const ASN1_ITEM *it)
Definition: tasn_utl.c:160
ASN1_TEMPLATE_st::item
ASN1_ITEM_EXP * item
Definition: asn1t.h:356
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
regen-readme.it
it
Definition: regen-readme.py:15
asn1_get_field_ptr
ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
Definition: tasn_utl.c:209
ASN1_TEMPLATE_st::offset
unsigned long offset
Definition: asn1t.h:354
ASN1_ENCODING_st::alias_only_on_next_parse
unsigned alias_only_on_next_parse
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:126
ASN1_AFLG_REFCOUNT
#define ASN1_AFLG_REFCOUNT
Definition: asn1t.h:589
ASN1_ENCODING_st::len
long len
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:117
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
ASN1_INTEGER_get
#define ASN1_INTEGER_get
Definition: boringssl_prefix_symbols.h:645
string.h
asn1_get_references
static CRYPTO_refcount_t * asn1_get_references(ASN1_VALUE **pval, const ASN1_ITEM *it)
Definition: tasn_utl.c:93
error_ref_leak.err
err
Definition: error_ref_leak.py:35
asn1_refcount_dec_and_test_zero
int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it)
Definition: tasn_utl.c:112
ASN1_ITYPE_SEQUENCE
#define ASN1_ITYPE_SEQUENCE
Definition: asn1t.h:507
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
ASN1_AUX_st::enc_offset
int enc_offset
Definition: asn1t.h:583
asn1_object_st
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:102
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
ASN1_TFLG_COMBINE
#define ASN1_TFLG_COMBINE
Definition: asn1t.h:455
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
ASN1_ENCODING_st::modified
int modified
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:118
OBJ_obj2nid
#define OBJ_obj2nid
Definition: boringssl_prefix_symbols.h:1857
asn1_enc_init
void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
Definition: tasn_utl.c:133
ASN1_ITEM_st
Definition: asn1t.h:459
internal.h
asn1_get_enc_ptr
static ASN1_ENCODING * asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
Definition: tasn_utl.c:120
offset2ptr
#define offset2ptr(addr, offset)
Definition: tasn_utl.c:75
ASN1_ADB_TABLE_st
Definition: asn1t.h:379
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
ASN1_ADB_TABLE_st::tt
const ASN1_TEMPLATE tt
Definition: asn1t.h:381
err.h
ASN1_ADB_ptr
#define ASN1_ADB_ptr(iptr)
Definition: asn1t.h:79
ASN1_ADB_st::null_tt
const ASN1_TEMPLATE * null_tt
Definition: asn1t.h:376
asn1t.h
ASN1_ENCODING_st::alias_only
unsigned alias_only
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:122
asn1_get_choice_selector
int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
Definition: tasn_utl.c:78
ASN1_ADB_st
Definition: asn1t.h:369
ASN1_AFLG_ENCODING
#define ASN1_AFLG_ENCODING
Definition: asn1t.h:591
ASN1_ADB_TABLE_st::value
long value
Definition: asn1t.h:380
ASN1_ENCODING_st::enc
unsigned char * enc
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:116
value
const char * value
Definition: hpack_parser_table.cc:165
asn1_refcount_set_one
void asn1_refcount_set_one(ASN1_VALUE **pval, const ASN1_ITEM *it)
Definition: tasn_utl.c:105
ASN1_ADB_st::tbl
const ASN1_ADB_TABLE * tbl
Definition: asn1t.h:373
asn1_do_adb
const ASN1_TEMPLATE * asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr)
Definition: tasn_utl.c:222
ASN1_AUX_st::flags
int flags
Definition: asn1t.h:580
asn1_enc_restore
int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it)
Definition: tasn_utl.c:191
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
ASN1_ADB_st::offset
unsigned long offset
Definition: asn1t.h:371
ASN1_ENCODING_st
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:115
asn1_set_choice_selector
int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it)
Definition: tasn_utl.c:84
ASN1_TEMPLATE_st
Definition: asn1t.h:351
ASN1_VALUE
struct ASN1_VALUE_st ASN1_VALUE
Definition: asn1.h:320
ASN1_TFLG_ADB_OID
#define ASN1_TFLG_ADB_OID
Definition: asn1t.h:443
ASN1_TEMPLATE_st::flags
unsigned long flags
Definition: asn1t.h:352
obj.h
mem.h
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
ASN1_AUX_st::ref_offset
int ref_offset
Definition: asn1t.h:581
asn1_enc_free
void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
Definition: tasn_utl.c:145
ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE
#define ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE
Definition: asn1.h:2029
thread.h
CRYPTO_refcount_t
uint32_t CRYPTO_refcount_t
Definition: thread.h:101
ASN1_AUX_st
Definition: asn1t.h:578
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
asn1_string_st
Definition: asn1.h:543
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
asn1.h
CRYPTO_refcount_dec_and_test_zero
#define CRYPTO_refcount_dec_and_test_zero
Definition: boringssl_prefix_symbols.h:1190


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