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