]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/asn1/tasn_utl.c
Reorganize local header files
[thirdparty/openssl.git] / crypto / asn1 / tasn_utl.c
1 /*
2 * Copyright 2000-2018 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 #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 asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
30 {
31 int *sel = offset2ptr(*pval, it->utype);
32
33 return *sel;
34 }
35
36 int asn1_get_choice_selector_const(const ASN1_VALUE **pval, const ASN1_ITEM *it)
37 {
38 int *sel = offset2ptr(*pval, it->utype);
39
40 return *sel;
41 }
42
43 /*
44 * Given an ASN1_ITEM CHOICE type set the selector value, return old value.
45 */
46
47 int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
48 const ASN1_ITEM *it)
49 {
50 int *sel, ret;
51
52 sel = offset2ptr(*pval, it->utype);
53 ret = *sel;
54 *sel = value;
55 return ret;
56 }
57
58 /*
59 * Do atomic reference counting. The value 'op' decides what to do.
60 * If it is +1 then the count is incremented.
61 * If |op| is 0, lock is initialised and count is set to 1.
62 * If |op| is -1, count is decremented and the return value is the current
63 * reference count or 0 if no reference count is active.
64 * It returns -1 on initialisation error.
65 * Used by ASN1_SEQUENCE construct of X509, X509_REQ, X509_CRL objects
66 */
67 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
68 {
69 const ASN1_AUX *aux;
70 CRYPTO_REF_COUNT *lck;
71 CRYPTO_RWLOCK **lock;
72 int ret = -1;
73
74 if ((it->itype != ASN1_ITYPE_SEQUENCE)
75 && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
76 return 0;
77 aux = it->funcs;
78 if (aux == NULL || (aux->flags & ASN1_AFLG_REFCOUNT) == 0)
79 return 0;
80 lck = offset2ptr(*pval, aux->ref_offset);
81 lock = offset2ptr(*pval, aux->ref_lock);
82
83 switch (op) {
84 case 0:
85 *lck = ret = 1;
86 *lock = CRYPTO_THREAD_lock_new();
87 if (*lock == NULL) {
88 ASN1err(ASN1_F_ASN1_DO_LOCK, ERR_R_MALLOC_FAILURE);
89 return -1;
90 }
91 break;
92 case 1:
93 if (!CRYPTO_UP_REF(lck, &ret, *lock))
94 return -1;
95 break;
96 case -1:
97 if (!CRYPTO_DOWN_REF(lck, &ret, *lock))
98 return -1; /* failed */
99 #ifdef REF_PRINT
100 fprintf(stderr, "%p:%4d:%s\n", it, ret, it->sname);
101 #endif
102 REF_ASSERT_ISNT(ret < 0);
103 if (ret == 0) {
104 CRYPTO_THREAD_lock_free(*lock);
105 *lock = NULL;
106 }
107 break;
108 }
109
110 return ret;
111 }
112
113 static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
114 {
115 const ASN1_AUX *aux;
116
117 if (pval == NULL || *pval == NULL)
118 return NULL;
119 aux = it->funcs;
120 if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
121 return NULL;
122 return offset2ptr(*pval, aux->enc_offset);
123 }
124
125 static const ASN1_ENCODING *asn1_get_const_enc_ptr(const ASN1_VALUE **pval,
126 const ASN1_ITEM *it)
127 {
128 const ASN1_AUX *aux;
129
130 if (pval == NULL || *pval == NULL)
131 return NULL;
132 aux = it->funcs;
133 if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
134 return NULL;
135 return offset2ptr(*pval, aux->enc_offset);
136 }
137
138 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
139 {
140 ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
141
142 if (enc != NULL) {
143 enc->enc = NULL;
144 enc->len = 0;
145 enc->modified = 1;
146 }
147 }
148
149 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
150 {
151 ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
152
153 if (enc != NULL) {
154 OPENSSL_free(enc->enc);
155 enc->enc = NULL;
156 enc->len = 0;
157 enc->modified = 1;
158 }
159 }
160
161 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
162 const ASN1_ITEM *it)
163 {
164 ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
165
166 if (enc == NULL)
167 return 1;
168
169 OPENSSL_free(enc->enc);
170 if ((enc->enc = OPENSSL_malloc(inlen)) == NULL) {
171 ASN1err(ASN1_F_ASN1_ENC_SAVE, ERR_R_MALLOC_FAILURE);
172 return 0;
173 }
174 memcpy(enc->enc, in, inlen);
175 enc->len = inlen;
176 enc->modified = 0;
177
178 return 1;
179 }
180
181 int asn1_enc_restore(int *len, unsigned char **out, const ASN1_VALUE **pval,
182 const ASN1_ITEM *it)
183 {
184 const ASN1_ENCODING *enc = asn1_get_const_enc_ptr(pval, it);
185
186 if (enc == NULL || enc->modified)
187 return 0;
188 if (out) {
189 memcpy(*out, enc->enc, enc->len);
190 *out += enc->len;
191 }
192 if (len != NULL)
193 *len = enc->len;
194 return 1;
195 }
196
197 /* Given an ASN1_TEMPLATE get a pointer to a field */
198 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
199 {
200 ASN1_VALUE **pvaltmp = offset2ptr(*pval, tt->offset);
201
202 /*
203 * NOTE for BOOLEAN types the field is just a plain int so we can't
204 * return int **, so settle for (int *).
205 */
206 return pvaltmp;
207 }
208
209 /* Given an ASN1_TEMPLATE get a const pointer to a field */
210 const ASN1_VALUE **asn1_get_const_field_ptr(const ASN1_VALUE **pval,
211 const ASN1_TEMPLATE *tt)
212 {
213 return offset2ptr(*pval, tt->offset);
214 }
215
216 /*
217 * Handle ANY DEFINED BY template, find the selector, look up the relevant
218 * ASN1_TEMPLATE in the table and return it.
219 */
220
221 const ASN1_TEMPLATE *asn1_do_adb(const ASN1_VALUE *val,
222 const ASN1_TEMPLATE *tt,
223 int nullerr)
224 {
225 const ASN1_ADB *adb;
226 const ASN1_ADB_TABLE *atbl;
227 long selector;
228 const ASN1_VALUE **sfld;
229 int i;
230
231 if ((tt->flags & ASN1_TFLG_ADB_MASK) == 0)
232 return tt;
233
234 /* Else ANY DEFINED BY ... get the table */
235 adb = ASN1_ADB_ptr(tt->item);
236
237 /* Get the selector field */
238 sfld = offset2ptr(val, adb->offset);
239
240 /* Check if NULL */
241 if (*sfld == NULL) {
242 if (adb->null_tt == NULL)
243 goto err;
244 return adb->null_tt;
245 }
246
247 /*
248 * Convert type to a long: NB: don't check for NID_undef here because it
249 * might be a legitimate value in the table
250 */
251 if ((tt->flags & ASN1_TFLG_ADB_OID) != 0)
252 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
253 else
254 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
255
256 /* Let application callback translate value */
257 if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
258 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
259 return NULL;
260 }
261
262 /*
263 * Try to find matching entry in table Maybe should check application
264 * types first to allow application override? Might also be useful to
265 * have a flag which indicates table is sorted and we can do a binary
266 * search. For now stick to a linear search.
267 */
268
269 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
270 if (atbl->value == selector)
271 return &atbl->tt;
272
273 /* FIXME: need to search application table too */
274
275 /* No match, return default type */
276 if (!adb->default_tt)
277 goto err;
278 return adb->default_tt;
279
280 err:
281 /* FIXME: should log the value or OID of unsupported type */
282 if (nullerr)
283 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
284 return NULL;
285 }