]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
3c2bdd7d | 2 | * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved. |
9d6b1ce6 | 3 | * |
365a2d99 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
2039c421 RS |
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 | |
9d6b1ce6 DSH |
8 | */ |
9 | ||
9d6b1ce6 | 10 | #include <stddef.h> |
3ebac273 | 11 | #include <string.h> |
5f8dd0f8 | 12 | #include "internal/cryptlib.h" |
cd420b0b | 13 | #include "internal/refcount.h" |
9d6b1ce6 DSH |
14 | #include <openssl/asn1.h> |
15 | #include <openssl/asn1t.h> | |
16 | #include <openssl/objects.h> | |
17 | #include <openssl/err.h> | |
706457b7 | 18 | #include "asn1_local.h" |
9d6b1ce6 DSH |
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 | ||
0f113f3e MC |
25 | /* |
26 | * Given an ASN1_ITEM CHOICE type return the selector value | |
9d6b1ce6 DSH |
27 | */ |
28 | ||
adf7e6d1 | 29 | int ossl_asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) |
0f113f3e MC |
30 | { |
31 | int *sel = offset2ptr(*pval, it->utype); | |
9fdcc21f DO |
32 | |
33 | return *sel; | |
34 | } | |
35 | ||
adf7e6d1 SL |
36 | int ossl_asn1_get_choice_selector_const(const ASN1_VALUE **pval, |
37 | const ASN1_ITEM *it) | |
9fdcc21f DO |
38 | { |
39 | int *sel = offset2ptr(*pval, it->utype); | |
40 | ||
0f113f3e MC |
41 | return *sel; |
42 | } | |
9d6b1ce6 | 43 | |
0f113f3e MC |
44 | /* |
45 | * Given an ASN1_ITEM CHOICE type set the selector value, return old value. | |
9d6b1ce6 DSH |
46 | */ |
47 | ||
adf7e6d1 SL |
48 | int ossl_asn1_set_choice_selector(ASN1_VALUE **pval, int value, |
49 | const ASN1_ITEM *it) | |
0f113f3e MC |
50 | { |
51 | int *sel, ret; | |
9fdcc21f | 52 | |
0f113f3e MC |
53 | sel = offset2ptr(*pval, it->utype); |
54 | ret = *sel; | |
55 | *sel = value; | |
56 | return ret; | |
57 | } | |
9d6b1ce6 | 58 | |
0f113f3e | 59 | /* |
687b4868 F |
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 | |
9d6b1ce6 | 67 | */ |
adf7e6d1 | 68 | int ossl_asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) |
0f113f3e MC |
69 | { |
70 | const ASN1_AUX *aux; | |
680b9d45 | 71 | CRYPTO_REF_COUNT *lck; |
c001ce33 | 72 | CRYPTO_RWLOCK **lock; |
680b9d45 AP |
73 | int ret = -1; |
74 | ||
0f113f3e MC |
75 | if ((it->itype != ASN1_ITYPE_SEQUENCE) |
76 | && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE)) | |
77 | return 0; | |
78 | aux = it->funcs; | |
9fdcc21f | 79 | if (aux == NULL || (aux->flags & ASN1_AFLG_REFCOUNT) == 0) |
0f113f3e MC |
80 | return 0; |
81 | lck = offset2ptr(*pval, aux->ref_offset); | |
c001ce33 | 82 | lock = offset2ptr(*pval, aux->ref_lock); |
680b9d45 AP |
83 | |
84 | switch (op) { | |
85 | case 0: | |
86 | *lck = ret = 1; | |
c001ce33 | 87 | *lock = CRYPTO_THREAD_lock_new(); |
b2b361f6 | 88 | if (*lock == NULL) { |
9311d0c4 | 89 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
687b4868 | 90 | return -1; |
b2b361f6 | 91 | } |
680b9d45 AP |
92 | break; |
93 | case 1: | |
94 | if (!CRYPTO_UP_REF(lck, &ret, *lock)) | |
95 | return -1; | |
96 | break; | |
97 | case -1: | |
98 | if (!CRYPTO_DOWN_REF(lck, &ret, *lock)) | |
99 | return -1; /* failed */ | |
9d6b1ce6 | 100 | #ifdef REF_PRINT |
fce56f5b | 101 | fprintf(stderr, "%p:%4d:%s\n", (void*)it, ret, it->sname); |
9d6b1ce6 | 102 | #endif |
680b9d45 AP |
103 | REF_ASSERT_ISNT(ret < 0); |
104 | if (ret == 0) { | |
105 | CRYPTO_THREAD_lock_free(*lock); | |
106 | *lock = NULL; | |
107 | } | |
108 | break; | |
687b4868 | 109 | } |
680b9d45 | 110 | |
0f113f3e MC |
111 | return ret; |
112 | } | |
9d6b1ce6 DSH |
113 | |
114 | static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
0f113f3e MC |
115 | { |
116 | const ASN1_AUX *aux; | |
9fdcc21f DO |
117 | |
118 | if (pval == NULL || *pval == NULL) | |
0f113f3e MC |
119 | return NULL; |
120 | aux = it->funcs; | |
9fdcc21f DO |
121 | if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0) |
122 | return NULL; | |
123 | return offset2ptr(*pval, aux->enc_offset); | |
124 | } | |
125 | ||
126 | static const ASN1_ENCODING *asn1_get_const_enc_ptr(const ASN1_VALUE **pval, | |
127 | const ASN1_ITEM *it) | |
128 | { | |
129 | const ASN1_AUX *aux; | |
130 | ||
131 | if (pval == NULL || *pval == NULL) | |
132 | return NULL; | |
133 | aux = it->funcs; | |
134 | if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0) | |
0f113f3e MC |
135 | return NULL; |
136 | return offset2ptr(*pval, aux->enc_offset); | |
137 | } | |
9d6b1ce6 | 138 | |
adf7e6d1 | 139 | void ossl_asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) |
0f113f3e | 140 | { |
9fdcc21f DO |
141 | ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it); |
142 | ||
143 | if (enc != NULL) { | |
0f113f3e MC |
144 | enc->enc = NULL; |
145 | enc->len = 0; | |
146 | enc->modified = 1; | |
147 | } | |
148 | } | |
9d6b1ce6 | 149 | |
adf7e6d1 | 150 | void ossl_asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
0f113f3e | 151 | { |
9fdcc21f DO |
152 | ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it); |
153 | ||
154 | if (enc != NULL) { | |
b548a1f1 | 155 | OPENSSL_free(enc->enc); |
0f113f3e MC |
156 | enc->enc = NULL; |
157 | enc->len = 0; | |
158 | enc->modified = 1; | |
159 | } | |
160 | } | |
9d6b1ce6 | 161 | |
adf7e6d1 SL |
162 | int ossl_asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, |
163 | const ASN1_ITEM *it) | |
0f113f3e | 164 | { |
9fdcc21f DO |
165 | ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it); |
166 | ||
167 | if (enc == NULL) | |
0f113f3e MC |
168 | return 1; |
169 | ||
b548a1f1 | 170 | OPENSSL_free(enc->enc); |
42e7d2f1 SL |
171 | if (inlen <= 0) |
172 | return 0; | |
cdb10bae | 173 | if ((enc->enc = OPENSSL_malloc(inlen)) == NULL) { |
9311d0c4 | 174 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
0f113f3e | 175 | return 0; |
cdb10bae | 176 | } |
0f113f3e MC |
177 | memcpy(enc->enc, in, inlen); |
178 | enc->len = inlen; | |
179 | enc->modified = 0; | |
9d6b1ce6 | 180 | |
0f113f3e MC |
181 | return 1; |
182 | } | |
9d6b1ce6 | 183 | |
adf7e6d1 SL |
184 | int ossl_asn1_enc_restore(int *len, unsigned char **out, const ASN1_VALUE **pval, |
185 | const ASN1_ITEM *it) | |
0f113f3e | 186 | { |
9fdcc21f DO |
187 | const ASN1_ENCODING *enc = asn1_get_const_enc_ptr(pval, it); |
188 | ||
189 | if (enc == NULL || enc->modified) | |
0f113f3e MC |
190 | return 0; |
191 | if (out) { | |
192 | memcpy(*out, enc->enc, enc->len); | |
193 | *out += enc->len; | |
194 | } | |
9fdcc21f | 195 | if (len != NULL) |
0f113f3e MC |
196 | *len = enc->len; |
197 | return 1; | |
198 | } | |
9d6b1ce6 DSH |
199 | |
200 | /* Given an ASN1_TEMPLATE get a pointer to a field */ | |
adf7e6d1 | 201 | ASN1_VALUE **ossl_asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
0f113f3e | 202 | { |
9fdcc21f DO |
203 | ASN1_VALUE **pvaltmp = offset2ptr(*pval, tt->offset); |
204 | ||
0f113f3e MC |
205 | /* |
206 | * NOTE for BOOLEAN types the field is just a plain int so we can't | |
207 | * return int **, so settle for (int *). | |
208 | */ | |
209 | return pvaltmp; | |
210 | } | |
9d6b1ce6 | 211 | |
9fdcc21f | 212 | /* Given an ASN1_TEMPLATE get a const pointer to a field */ |
adf7e6d1 SL |
213 | const ASN1_VALUE **ossl_asn1_get_const_field_ptr(const ASN1_VALUE **pval, |
214 | const ASN1_TEMPLATE *tt) | |
9fdcc21f DO |
215 | { |
216 | return offset2ptr(*pval, tt->offset); | |
217 | } | |
218 | ||
0f113f3e MC |
219 | /* |
220 | * Handle ANY DEFINED BY template, find the selector, look up the relevant | |
221 | * ASN1_TEMPLATE in the table and return it. | |
9d6b1ce6 DSH |
222 | */ |
223 | ||
adf7e6d1 SL |
224 | const ASN1_TEMPLATE *ossl_asn1_do_adb(const ASN1_VALUE *val, |
225 | const ASN1_TEMPLATE *tt, | |
226 | int nullerr) | |
0f113f3e MC |
227 | { |
228 | const ASN1_ADB *adb; | |
229 | const ASN1_ADB_TABLE *atbl; | |
230 | long selector; | |
9fdcc21f | 231 | const ASN1_VALUE **sfld; |
0f113f3e | 232 | int i; |
9fdcc21f DO |
233 | |
234 | if ((tt->flags & ASN1_TFLG_ADB_MASK) == 0) | |
0f113f3e MC |
235 | return tt; |
236 | ||
237 | /* Else ANY DEFINED BY ... get the table */ | |
238 | adb = ASN1_ADB_ptr(tt->item); | |
9d6b1ce6 | 239 | |
0f113f3e | 240 | /* Get the selector field */ |
9fdcc21f | 241 | sfld = offset2ptr(val, adb->offset); |
9d6b1ce6 | 242 | |
0f113f3e | 243 | /* Check if NULL */ |
7c46746b | 244 | if (*sfld == NULL) { |
9fdcc21f | 245 | if (adb->null_tt == NULL) |
0f113f3e MC |
246 | goto err; |
247 | return adb->null_tt; | |
248 | } | |
9d6b1ce6 | 249 | |
0f113f3e MC |
250 | /* |
251 | * Convert type to a long: NB: don't check for NID_undef here because it | |
252 | * might be a legitimate value in the table | |
253 | */ | |
9fdcc21f | 254 | if ((tt->flags & ASN1_TFLG_ADB_OID) != 0) |
0f113f3e MC |
255 | selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); |
256 | else | |
257 | selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); | |
9d6b1ce6 | 258 | |
5b70372d DSH |
259 | /* Let application callback translate value */ |
260 | if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) { | |
9311d0c4 | 261 | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); |
5b70372d DSH |
262 | return NULL; |
263 | } | |
264 | ||
0f113f3e MC |
265 | /* |
266 | * Try to find matching entry in table Maybe should check application | |
267 | * types first to allow application override? Might also be useful to | |
268 | * have a flag which indicates table is sorted and we can do a binary | |
269 | * search. For now stick to a linear search. | |
270 | */ | |
9d6b1ce6 | 271 | |
0f113f3e MC |
272 | for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) |
273 | if (atbl->value == selector) | |
274 | return &atbl->tt; | |
9d6b1ce6 | 275 | |
0f113f3e | 276 | /* FIXME: need to search application table too */ |
9d6b1ce6 | 277 | |
0f113f3e MC |
278 | /* No match, return default type */ |
279 | if (!adb->default_tt) | |
280 | goto err; | |
281 | return adb->default_tt; | |
9d6b1ce6 | 282 | |
0f113f3e MC |
283 | err: |
284 | /* FIXME: should log the value or OID of unsupported type */ | |
285 | if (nullerr) | |
9311d0c4 | 286 | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); |
0f113f3e MC |
287 | return NULL; |
288 | } |