/src/openssl/crypto/asn1/tasn_utl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stddef.h> |
11 | | #include <string.h> |
12 | | #include "internal/cryptlib.h" |
13 | | #include "internal/refcount.h" |
14 | | #include <openssl/asn1.h> |
15 | | #include <openssl/asn1t.h> |
16 | | #include <openssl/objects.h> |
17 | | #include <openssl/err.h> |
18 | | #include "asn1_local.h" |
19 | | |
20 | | /* Utility functions for manipulating fields and offsets */ |
21 | | |
22 | | /* Add 'offset' to 'addr' */ |
23 | 12.8k | #define offset2ptr(addr, offset) (void *)(((char *) addr) + offset) |
24 | | |
25 | | /* |
26 | | * Given an ASN1_ITEM CHOICE type return the selector value |
27 | | */ |
28 | | |
29 | | int ossl_asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) |
30 | 0 | { |
31 | 0 | int *sel = offset2ptr(*pval, it->utype); |
32 | |
|
33 | 0 | return *sel; |
34 | 0 | } |
35 | | |
36 | | int ossl_asn1_get_choice_selector_const(const ASN1_VALUE **pval, |
37 | | const ASN1_ITEM *it) |
38 | 0 | { |
39 | 0 | int *sel = offset2ptr(*pval, it->utype); |
40 | |
|
41 | 0 | return *sel; |
42 | 0 | } |
43 | | |
44 | | /* |
45 | | * Given an ASN1_ITEM CHOICE type set the selector value, return old value. |
46 | | */ |
47 | | |
48 | | int ossl_asn1_set_choice_selector(ASN1_VALUE **pval, int value, |
49 | | const ASN1_ITEM *it) |
50 | 0 | { |
51 | 0 | int *sel, ret; |
52 | |
|
53 | 0 | sel = offset2ptr(*pval, it->utype); |
54 | 0 | ret = *sel; |
55 | 0 | *sel = value; |
56 | 0 | return ret; |
57 | 0 | } |
58 | | |
59 | | /* |
60 | | * Do atomic reference counting. The value 'op' decides what to do. |
61 | | * If it is +1 then the count is incremented. |
62 | | * If |op| is 0, lock is initialised and count is set to 1. |
63 | | * If |op| is -1, count is decremented and the return value is the current |
64 | | * reference count or 0 if no reference count is active. |
65 | | * It returns -1 on initialisation error. |
66 | | * Used by ASN1_SEQUENCE construct of X509, X509_REQ, X509_CRL objects |
67 | | */ |
68 | | int ossl_asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) |
69 | 3.07k | { |
70 | 3.07k | const ASN1_AUX *aux; |
71 | 3.07k | CRYPTO_REF_COUNT *lck; |
72 | 3.07k | CRYPTO_RWLOCK **lock; |
73 | 3.07k | int ret = -1; |
74 | | |
75 | 3.07k | if ((it->itype != ASN1_ITYPE_SEQUENCE) |
76 | 3.07k | && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE)) |
77 | 0 | return 0; |
78 | 3.07k | aux = it->funcs; |
79 | 3.07k | if (aux == NULL || (aux->flags & ASN1_AFLG_REFCOUNT) == 0) |
80 | 3.07k | return 0; |
81 | 0 | lck = offset2ptr(*pval, aux->ref_offset); |
82 | 0 | lock = offset2ptr(*pval, aux->ref_lock); |
83 | |
|
84 | 0 | switch (op) { |
85 | 0 | case 0: |
86 | 0 | *lck = ret = 1; |
87 | 0 | *lock = CRYPTO_THREAD_lock_new(); |
88 | 0 | if (*lock == NULL) { |
89 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB); |
90 | 0 | return -1; |
91 | 0 | } |
92 | 0 | break; |
93 | 0 | case 1: |
94 | 0 | if (!CRYPTO_UP_REF(lck, &ret, *lock)) |
95 | 0 | return -1; |
96 | 0 | break; |
97 | 0 | case -1: |
98 | 0 | if (!CRYPTO_DOWN_REF(lck, &ret, *lock)) |
99 | 0 | return -1; /* failed */ |
100 | 0 | REF_PRINT_EX(it->sname, ret, (void *)it); |
101 | 0 | REF_ASSERT_ISNT(ret < 0); |
102 | 0 | if (ret == 0) { |
103 | 0 | CRYPTO_THREAD_lock_free(*lock); |
104 | 0 | *lock = NULL; |
105 | 0 | } |
106 | 0 | break; |
107 | 0 | } |
108 | | |
109 | 0 | return ret; |
110 | 0 | } |
111 | | |
112 | | static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) |
113 | 4.37k | { |
114 | 4.37k | const ASN1_AUX *aux; |
115 | | |
116 | 4.37k | if (pval == NULL || *pval == NULL) |
117 | 0 | return NULL; |
118 | 4.37k | aux = it->funcs; |
119 | 4.37k | if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0) |
120 | 4.37k | return NULL; |
121 | 0 | return offset2ptr(*pval, aux->enc_offset); |
122 | 4.37k | } |
123 | | |
124 | | static const ASN1_ENCODING *asn1_get_const_enc_ptr(const ASN1_VALUE **pval, |
125 | | const ASN1_ITEM *it) |
126 | 0 | { |
127 | 0 | const ASN1_AUX *aux; |
128 | |
|
129 | 0 | if (pval == NULL || *pval == NULL) |
130 | 0 | return NULL; |
131 | 0 | aux = it->funcs; |
132 | 0 | if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0) |
133 | 0 | return NULL; |
134 | 0 | return offset2ptr(*pval, aux->enc_offset); |
135 | 0 | } |
136 | | |
137 | | void ossl_asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) |
138 | 0 | { |
139 | 0 | ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it); |
140 | |
|
141 | 0 | if (enc != NULL) { |
142 | 0 | enc->enc = NULL; |
143 | 0 | enc->len = 0; |
144 | 0 | enc->modified = 1; |
145 | 0 | } |
146 | 0 | } |
147 | | |
148 | | void ossl_asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
149 | 0 | { |
150 | 0 | ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it); |
151 | |
|
152 | 0 | if (enc != NULL) { |
153 | 0 | OPENSSL_free(enc->enc); |
154 | 0 | enc->enc = NULL; |
155 | 0 | enc->len = 0; |
156 | 0 | enc->modified = 1; |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | | int ossl_asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, |
161 | | const ASN1_ITEM *it) |
162 | 4.37k | { |
163 | 4.37k | ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it); |
164 | | |
165 | 4.37k | if (enc == NULL) |
166 | 4.37k | return 1; |
167 | | |
168 | 0 | OPENSSL_free(enc->enc); |
169 | 0 | if (inlen <= 0) |
170 | 0 | return 0; |
171 | 0 | if ((enc->enc = OPENSSL_malloc(inlen)) == NULL) |
172 | 0 | return 0; |
173 | 0 | memcpy(enc->enc, in, inlen); |
174 | 0 | enc->len = inlen; |
175 | 0 | enc->modified = 0; |
176 | |
|
177 | 0 | return 1; |
178 | 0 | } |
179 | | |
180 | | int ossl_asn1_enc_restore(int *len, unsigned char **out, const ASN1_VALUE **pval, |
181 | | const ASN1_ITEM *it) |
182 | 0 | { |
183 | 0 | const ASN1_ENCODING *enc = asn1_get_const_enc_ptr(pval, it); |
184 | |
|
185 | 0 | if (enc == NULL || enc->modified) |
186 | 0 | return 0; |
187 | 0 | if (out) { |
188 | 0 | memcpy(*out, enc->enc, enc->len); |
189 | 0 | *out += enc->len; |
190 | 0 | } |
191 | 0 | if (len != NULL) |
192 | 0 | *len = enc->len; |
193 | 0 | return 1; |
194 | 0 | } |
195 | | |
196 | | /* Given an ASN1_TEMPLATE get a pointer to a field */ |
197 | | ASN1_VALUE **ossl_asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
198 | 12.8k | { |
199 | 12.8k | ASN1_VALUE **pvaltmp = offset2ptr(*pval, tt->offset); |
200 | | |
201 | | /* |
202 | | * NOTE for BOOLEAN types the field is just a plain int so we can't |
203 | | * return int **, so settle for (int *). |
204 | | */ |
205 | 12.8k | return pvaltmp; |
206 | 12.8k | } |
207 | | |
208 | | /* Given an ASN1_TEMPLATE get a const pointer to a field */ |
209 | | const ASN1_VALUE **ossl_asn1_get_const_field_ptr(const ASN1_VALUE **pval, |
210 | | const ASN1_TEMPLATE *tt) |
211 | 0 | { |
212 | 0 | return offset2ptr(*pval, tt->offset); |
213 | 0 | } |
214 | | |
215 | | /* |
216 | | * Handle ANY DEFINED BY template, find the selector, look up the relevant |
217 | | * ASN1_TEMPLATE in the table and return it. |
218 | | */ |
219 | | |
220 | | const ASN1_TEMPLATE *ossl_asn1_do_adb(const ASN1_VALUE *val, |
221 | | const ASN1_TEMPLATE *tt, |
222 | | int nullerr) |
223 | 13.5k | { |
224 | 13.5k | const ASN1_ADB *adb; |
225 | 13.5k | const ASN1_ADB_TABLE *atbl; |
226 | 13.5k | long selector; |
227 | 13.5k | const ASN1_VALUE **sfld; |
228 | 13.5k | int i; |
229 | | |
230 | 13.5k | if ((tt->flags & ASN1_TFLG_ADB_MASK) == 0) |
231 | 13.5k | return tt; |
232 | | |
233 | | /* Else ANY DEFINED BY ... get the table */ |
234 | 0 | adb = ASN1_ADB_ptr(tt->item); |
235 | | |
236 | | /* Get the selector field */ |
237 | 0 | sfld = offset2ptr(val, adb->offset); |
238 | | |
239 | | /* Check if NULL */ |
240 | 0 | if (*sfld == NULL) { |
241 | 0 | if (adb->null_tt == NULL) |
242 | 0 | goto err; |
243 | 0 | return adb->null_tt; |
244 | 0 | } |
245 | | |
246 | | /* |
247 | | * Convert type to a long: NB: don't check for NID_undef here because it |
248 | | * might be a legitimate value in the table |
249 | | */ |
250 | 0 | if ((tt->flags & ASN1_TFLG_ADB_OID) != 0) |
251 | 0 | selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); |
252 | 0 | else |
253 | 0 | selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); |
254 | | |
255 | | /* Let application callback translate value */ |
256 | 0 | if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) { |
257 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); |
258 | 0 | return NULL; |
259 | 0 | } |
260 | | |
261 | | /* |
262 | | * Try to find matching entry in table Maybe should check application |
263 | | * types first to allow application override? Might also be useful to |
264 | | * have a flag which indicates table is sorted and we can do a binary |
265 | | * search. For now stick to a linear search. |
266 | | */ |
267 | | |
268 | 0 | for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) |
269 | 0 | if (atbl->value == selector) |
270 | 0 | return &atbl->tt; |
271 | | |
272 | | /* FIXME: need to search application table too */ |
273 | | |
274 | | /* No match, return default type */ |
275 | 0 | if (!adb->default_tt) |
276 | 0 | goto err; |
277 | 0 | return adb->default_tt; |
278 | | |
279 | 0 | err: |
280 | | /* FIXME: should log the value or OID of unsupported type */ |
281 | 0 | if (nullerr) |
282 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); |
283 | 0 | return NULL; |
284 | 0 | } |