Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/asn1/tasn_utl.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/asn1.h>
5
6 #include <assert.h>
7 #include <string.h>
8
9 #include <openssl/asn1t.h>
10 #include <openssl/err.h>
11 #include <openssl/mem.h>
12 #include <openssl/obj.h>
13 #include <openssl/thread.h>
14
15 #include "../internal.h"
16 #include "internal.h"
17
18
19 // Utility functions for manipulating fields and offsets
20
21 // Add 'offset' to 'addr'
22 #define offset2ptr(addr, offset) (void *)(((char *)(addr)) + (offset))
23
24 // Given an ASN1_ITEM CHOICE type return the selector value
25 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) {
26 int *sel = offset2ptr(*pval, it->utype);
27 return *sel;
28 }
29
30 // Given an ASN1_ITEM CHOICE type set the selector value, return old value.
31 int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
32 const ASN1_ITEM *it) {
33 int *sel, ret;
34 sel = offset2ptr(*pval, it->utype);
35 ret = *sel;
36 *sel = value;
37 return ret;
38 }
39
40 static CRYPTO_refcount_t *asn1_get_references(ASN1_VALUE **pval,
41 const ASN1_ITEM *it) {
42 if (it->itype != ASN1_ITYPE_SEQUENCE) {
43 return NULL;
44 }
45 const ASN1_AUX *aux = it->funcs;
46 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) {
47 return NULL;
48 }
49 return offset2ptr(*pval, aux->ref_offset);
50 }
51
52 void asn1_refcount_set_one(ASN1_VALUE **pval, const ASN1_ITEM *it) {
53 CRYPTO_refcount_t *references = asn1_get_references(pval, it);
54 if (references != NULL) {
55 *references = 1;
56 }
57 }
58
59 int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it) {
60 CRYPTO_refcount_t *references = asn1_get_references(pval, it);
61 if (references != NULL) {
62 return CRYPTO_refcount_dec_and_test_zero(references);
63 }
64 return 1;
65 }
66
67 static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) {
68 assert(it->itype == ASN1_ITYPE_SEQUENCE);
69 const ASN1_AUX *aux;
70 if (!pval || !*pval) {
71 return NULL;
72 }
73 aux = it->funcs;
74 if (!aux || !(aux->flags & ASN1_AFLG_ENCODING)) {
75 return NULL;
76 }
77 return offset2ptr(*pval, aux->enc_offset);
78 }
79
80 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) {
81 ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
82 if (enc) {
83 enc->enc = NULL;
84 enc->len = 0;
85 enc->alias_only = 0;
86 enc->alias_only_on_next_parse = 0;
87 }
88 }
89
90 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
91 ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
92 if (enc) {
93 asn1_encoding_clear(enc);
94 }
95 }
96
97 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
98 const ASN1_ITEM *it) {
99 ASN1_ENCODING *enc;
100 enc = asn1_get_enc_ptr(pval, it);
101 if (!enc) {
102 return 1;
103 }
104
105 if (!enc->alias_only) {
106 OPENSSL_free(enc->enc);
107 }
108
109 enc->alias_only = enc->alias_only_on_next_parse;
110 enc->alias_only_on_next_parse = 0;
111
112 if (enc->alias_only) {
113 enc->enc = (uint8_t *)in;
114 } else {
115 enc->enc = OPENSSL_memdup(in, inlen);
116 if (!enc->enc) {
117 return 0;
118 }
119 }
120
121 enc->len = inlen;
122 return 1;
123 }
124
125 void asn1_encoding_clear(ASN1_ENCODING *enc) {
126 if (!enc->alias_only) {
127 OPENSSL_free(enc->enc);
128 }
129 enc->enc = NULL;
130 enc->len = 0;
131 enc->alias_only = 0;
132 enc->alias_only_on_next_parse = 0;
133 }
134
135 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
136 const ASN1_ITEM *it) {
137 ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
138 if (!enc || enc->len == 0) {
139 return 0;
140 }
141 if (out) {
142 OPENSSL_memcpy(*out, enc->enc, enc->len);
143 *out += enc->len;
144 }
145 if (len) {
146 *len = enc->len;
147 }
148 return 1;
149 }
150
151 // Given an ASN1_TEMPLATE get a pointer to a field
152 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
153 ASN1_VALUE **pvaltmp;
154 if (tt->flags & ASN1_TFLG_COMBINE) {
155 return pval;
156 }
157 pvaltmp = offset2ptr(*pval, tt->offset);
158 // NOTE for BOOLEAN types the field is just a plain int so we can't return
159 // int **, so settle for (int *).
160 return pvaltmp;
161 }
162
163 // Handle ANY DEFINED BY template, find the selector, look up the relevant
164 // ASN1_TEMPLATE in the table and return it.
165 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
166 int nullerr) {
167 const ASN1_ADB *adb;
168 const ASN1_ADB_TABLE *atbl;
169 ASN1_VALUE **sfld;
170 int i;
171 if (!(tt->flags & ASN1_TFLG_ADB_MASK)) {
172 return tt;
173 }
174
175 // Else ANY DEFINED BY ... get the table
176 adb = ASN1_ADB_ptr(tt->item);
177
178 // Get the selector field
179 sfld = offset2ptr(*pval, adb->offset);
180
181 // Check if NULL
182 if (*sfld == NULL) {
183 if (!adb->null_tt) {
184 goto err;
185 }
186 return adb->null_tt;
187 }
188
189 // Convert type to a NID:
190 // NB: don't check for NID_undef here because it
191 // might be a legitimate value in the table
192 assert(tt->flags & ASN1_TFLG_ADB_OID);
193 int selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
194
195 // Try to find matching entry in table Maybe should check application types
196 // first to allow application override? Might also be useful to have a flag
197 // which indicates table is sorted and we can do a binary search. For now
198 // stick to a linear search.
199
200 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) {
201 if (atbl->value == selector) {
202 return &atbl->tt;
203 }
204 }
205
206 // FIXME: need to search application table too
207
208 // No match, return default type
209 if (!adb->default_tt) {
210 goto err;
211 }
212 return adb->default_tt;
213
214 err:
215 // FIXME: should log the value or OID of unsupported type
216 if (nullerr) {
217 OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
218 }
219 return NULL;
220 }