]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project | |
3 | * 2000. | |
9d6b1ce6 DSH |
4 | */ |
5 | /* ==================================================================== | |
f3f52d7f | 6 | * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved. |
9d6b1ce6 DSH |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
9d6b1ce6 DSH |
14 | * |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in | |
17 | * the documentation and/or other materials provided with the | |
18 | * distribution. | |
19 | * | |
20 | * 3. All advertising materials mentioning features or use of this | |
21 | * software must display the following acknowledgment: | |
22 | * "This product includes software developed by the OpenSSL Project | |
23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
24 | * | |
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
26 | * endorse or promote products derived from this software without | |
27 | * prior written permission. For written permission, please contact | |
28 | * licensing@OpenSSL.org. | |
29 | * | |
30 | * 5. Products derived from this software may not be called "OpenSSL" | |
31 | * nor may "OpenSSL" appear in their names without prior written | |
32 | * permission of the OpenSSL Project. | |
33 | * | |
34 | * 6. Redistributions of any form whatsoever must retain the following | |
35 | * acknowledgment: | |
36 | * "This product includes software developed by the OpenSSL Project | |
37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
38 | * | |
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
51 | * ==================================================================== | |
52 | * | |
53 | * This product includes cryptographic software written by Eric Young | |
54 | * (eay@cryptsoft.com). This product includes software written by Tim | |
55 | * Hudson (tjh@cryptsoft.com). | |
56 | * | |
57 | */ | |
58 | ||
9d6b1ce6 | 59 | #include <stddef.h> |
3ebac273 | 60 | #include <string.h> |
984d6c60 | 61 | #include <internal/cryptlib.h> |
9d6b1ce6 DSH |
62 | #include <openssl/asn1.h> |
63 | #include <openssl/asn1t.h> | |
64 | #include <openssl/objects.h> | |
65 | #include <openssl/err.h> | |
c1ee50aa | 66 | #include "asn1_locl.h" |
9d6b1ce6 DSH |
67 | |
68 | /* Utility functions for manipulating fields and offsets */ | |
69 | ||
70 | /* Add 'offset' to 'addr' */ | |
71 | #define offset2ptr(addr, offset) (void *)(((char *) addr) + offset) | |
72 | ||
0f113f3e MC |
73 | /* |
74 | * Given an ASN1_ITEM CHOICE type return the selector value | |
9d6b1ce6 DSH |
75 | */ |
76 | ||
77 | int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
0f113f3e MC |
78 | { |
79 | int *sel = offset2ptr(*pval, it->utype); | |
80 | return *sel; | |
81 | } | |
9d6b1ce6 | 82 | |
0f113f3e MC |
83 | /* |
84 | * Given an ASN1_ITEM CHOICE type set the selector value, return old value. | |
9d6b1ce6 DSH |
85 | */ |
86 | ||
0f113f3e MC |
87 | int asn1_set_choice_selector(ASN1_VALUE **pval, int value, |
88 | const ASN1_ITEM *it) | |
89 | { | |
90 | int *sel, ret; | |
91 | sel = offset2ptr(*pval, it->utype); | |
92 | ret = *sel; | |
93 | *sel = value; | |
94 | return ret; | |
95 | } | |
9d6b1ce6 | 96 | |
0f113f3e MC |
97 | /* |
98 | * Do reference counting. The value 'op' decides what to do. if it is +1 | |
99 | * then the count is incremented. If op is 0 count is set to 1. If op is -1 | |
100 | * count is decremented and the return value is the current reference count | |
101 | * or 0 if no reference count exists. | |
9d6b1ce6 DSH |
102 | */ |
103 | ||
104 | int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) | |
0f113f3e MC |
105 | { |
106 | const ASN1_AUX *aux; | |
107 | int *lck, ret; | |
c001ce33 | 108 | CRYPTO_RWLOCK **lock; |
0f113f3e MC |
109 | if ((it->itype != ASN1_ITYPE_SEQUENCE) |
110 | && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE)) | |
111 | return 0; | |
112 | aux = it->funcs; | |
113 | if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) | |
114 | return 0; | |
115 | lck = offset2ptr(*pval, aux->ref_offset); | |
c001ce33 | 116 | lock = offset2ptr(*pval, aux->ref_lock); |
0f113f3e MC |
117 | if (op == 0) { |
118 | *lck = 1; | |
c001ce33 AG |
119 | *lock = CRYPTO_THREAD_lock_new(); |
120 | if (*lock == NULL) | |
121 | return 0; | |
0f113f3e MC |
122 | return 1; |
123 | } | |
c001ce33 | 124 | CRYPTO_atomic_add(lck, op, &ret, *lock); |
9d6b1ce6 | 125 | #ifdef REF_PRINT |
f3f1cf84 | 126 | fprintf(stderr, "%p:%4d:%s\n", it, *lck, it->sname); |
9d6b1ce6 | 127 | #endif |
f3f1cf84 | 128 | REF_ASSERT_ISNT(ret < 0); |
c001ce33 AG |
129 | if (ret == 0) |
130 | CRYPTO_THREAD_lock_free(*lock); | |
0f113f3e MC |
131 | return ret; |
132 | } | |
9d6b1ce6 DSH |
133 | |
134 | static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
0f113f3e MC |
135 | { |
136 | const ASN1_AUX *aux; | |
137 | if (!pval || !*pval) | |
138 | return NULL; | |
139 | aux = it->funcs; | |
140 | if (!aux || !(aux->flags & ASN1_AFLG_ENCODING)) | |
141 | return NULL; | |
142 | return offset2ptr(*pval, aux->enc_offset); | |
143 | } | |
9d6b1ce6 DSH |
144 | |
145 | void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
0f113f3e MC |
146 | { |
147 | ASN1_ENCODING *enc; | |
148 | enc = asn1_get_enc_ptr(pval, it); | |
149 | if (enc) { | |
150 | enc->enc = NULL; | |
151 | enc->len = 0; | |
152 | enc->modified = 1; | |
153 | } | |
154 | } | |
9d6b1ce6 DSH |
155 | |
156 | void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
0f113f3e MC |
157 | { |
158 | ASN1_ENCODING *enc; | |
159 | enc = asn1_get_enc_ptr(pval, it); | |
160 | if (enc) { | |
b548a1f1 | 161 | OPENSSL_free(enc->enc); |
0f113f3e MC |
162 | enc->enc = NULL; |
163 | enc->len = 0; | |
164 | enc->modified = 1; | |
165 | } | |
166 | } | |
9d6b1ce6 | 167 | |
f3f52d7f | 168 | int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, |
0f113f3e MC |
169 | const ASN1_ITEM *it) |
170 | { | |
171 | ASN1_ENCODING *enc; | |
172 | enc = asn1_get_enc_ptr(pval, it); | |
173 | if (!enc) | |
174 | return 1; | |
175 | ||
b548a1f1 | 176 | OPENSSL_free(enc->enc); |
0f113f3e | 177 | enc->enc = OPENSSL_malloc(inlen); |
90945fa3 | 178 | if (enc->enc == NULL) |
0f113f3e MC |
179 | return 0; |
180 | memcpy(enc->enc, in, inlen); | |
181 | enc->len = inlen; | |
182 | enc->modified = 0; | |
9d6b1ce6 | 183 | |
0f113f3e MC |
184 | return 1; |
185 | } | |
9d6b1ce6 | 186 | |
f3f52d7f | 187 | int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, |
0f113f3e MC |
188 | const ASN1_ITEM *it) |
189 | { | |
190 | ASN1_ENCODING *enc; | |
191 | enc = asn1_get_enc_ptr(pval, it); | |
192 | if (!enc || enc->modified) | |
193 | return 0; | |
194 | if (out) { | |
195 | memcpy(*out, enc->enc, enc->len); | |
196 | *out += enc->len; | |
197 | } | |
198 | if (len) | |
199 | *len = enc->len; | |
200 | return 1; | |
201 | } | |
9d6b1ce6 DSH |
202 | |
203 | /* Given an ASN1_TEMPLATE get a pointer to a field */ | |
0f113f3e MC |
204 | ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
205 | { | |
206 | ASN1_VALUE **pvaltmp; | |
0f113f3e MC |
207 | pvaltmp = offset2ptr(*pval, tt->offset); |
208 | /* | |
209 | * NOTE for BOOLEAN types the field is just a plain int so we can't | |
210 | * return int **, so settle for (int *). | |
211 | */ | |
212 | return pvaltmp; | |
213 | } | |
9d6b1ce6 | 214 | |
0f113f3e MC |
215 | /* |
216 | * Handle ANY DEFINED BY template, find the selector, look up the relevant | |
217 | * ASN1_TEMPLATE in the table and return it. | |
9d6b1ce6 DSH |
218 | */ |
219 | ||
f3f52d7f | 220 | const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, |
0f113f3e MC |
221 | int nullerr) |
222 | { | |
223 | const ASN1_ADB *adb; | |
224 | const ASN1_ADB_TABLE *atbl; | |
225 | long selector; | |
226 | ASN1_VALUE **sfld; | |
227 | int i; | |
228 | if (!(tt->flags & ASN1_TFLG_ADB_MASK)) | |
229 | return tt; | |
230 | ||
231 | /* Else ANY DEFINED BY ... get the table */ | |
232 | adb = ASN1_ADB_ptr(tt->item); | |
9d6b1ce6 | 233 | |
0f113f3e MC |
234 | /* Get the selector field */ |
235 | sfld = offset2ptr(*pval, adb->offset); | |
9d6b1ce6 | 236 | |
0f113f3e MC |
237 | /* Check if NULL */ |
238 | if (!sfld) { | |
239 | if (!adb->null_tt) | |
240 | goto err; | |
241 | return adb->null_tt; | |
242 | } | |
9d6b1ce6 | 243 | |
0f113f3e MC |
244 | /* |
245 | * Convert type to a long: NB: don't check for NID_undef here because it | |
246 | * might be a legitimate value in the table | |
247 | */ | |
248 | if (tt->flags & ASN1_TFLG_ADB_OID) | |
249 | selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); | |
250 | else | |
251 | selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); | |
9d6b1ce6 | 252 | |
5b70372d DSH |
253 | /* Let application callback translate value */ |
254 | if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) { | |
255 | ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); | |
256 | return NULL; | |
257 | } | |
258 | ||
0f113f3e MC |
259 | /* |
260 | * Try to find matching entry in table Maybe should check application | |
261 | * types first to allow application override? Might also be useful to | |
262 | * have a flag which indicates table is sorted and we can do a binary | |
263 | * search. For now stick to a linear search. | |
264 | */ | |
9d6b1ce6 | 265 | |
0f113f3e MC |
266 | for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) |
267 | if (atbl->value == selector) | |
268 | return &atbl->tt; | |
9d6b1ce6 | 269 | |
0f113f3e | 270 | /* FIXME: need to search application table too */ |
9d6b1ce6 | 271 | |
0f113f3e MC |
272 | /* No match, return default type */ |
273 | if (!adb->default_tt) | |
274 | goto err; | |
275 | return adb->default_tt; | |
9d6b1ce6 | 276 | |
0f113f3e MC |
277 | err: |
278 | /* FIXME: should log the value or OID of unsupported type */ | |
279 | if (nullerr) | |
280 | ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); | |
281 | return NULL; | |
282 | } |