1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/asn1.h>
58
59 #include <assert.h>
60 #include <string.h>
61
62 #include <openssl/asn1t.h>
63 #include <openssl/mem.h>
64 #include <openssl/obj.h>
65 #include <openssl/err.h>
66 #include <openssl/thread.h>
67
68 #include "../internal.h"
69
70
71 /* Utility functions for manipulating fields and offsets */
72
73 /* Add 'offset' to 'addr' */
74 #define offset2ptr(addr, offset) (void *)(((char *)(addr)) + (offset))
75
76 /* Given an ASN1_ITEM CHOICE type return the selector value */
asn1_get_choice_selector(ASN1_VALUE ** pval,const ASN1_ITEM * it)77 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) {
78 int *sel = offset2ptr(*pval, it->utype);
79 return *sel;
80 }
81
82 /* Given an ASN1_ITEM CHOICE type set the selector value, return old value. */
asn1_set_choice_selector(ASN1_VALUE ** pval,int value,const ASN1_ITEM * it)83 int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
84 const ASN1_ITEM *it) {
85 int *sel, ret;
86 sel = offset2ptr(*pval, it->utype);
87 ret = *sel;
88 *sel = value;
89 return ret;
90 }
91
asn1_get_references(ASN1_VALUE ** pval,const ASN1_ITEM * it)92 static CRYPTO_refcount_t *asn1_get_references(ASN1_VALUE **pval,
93 const ASN1_ITEM *it) {
94 if (it->itype != ASN1_ITYPE_SEQUENCE &&
95 it->itype != ASN1_ITYPE_NDEF_SEQUENCE) {
96 return NULL;
97 }
98 const ASN1_AUX *aux = it->funcs;
99 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) {
100 return NULL;
101 }
102 return offset2ptr(*pval, aux->ref_offset);
103 }
104
asn1_refcount_set_one(ASN1_VALUE ** pval,const ASN1_ITEM * it)105 void asn1_refcount_set_one(ASN1_VALUE **pval, const ASN1_ITEM *it) {
106 CRYPTO_refcount_t *references = asn1_get_references(pval, it);
107 if (references != NULL) {
108 *references = 1;
109 }
110 }
111
asn1_refcount_dec_and_test_zero(ASN1_VALUE ** pval,const ASN1_ITEM * it)112 int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it) {
113 CRYPTO_refcount_t *references = asn1_get_references(pval, it);
114 if (references != NULL) {
115 return CRYPTO_refcount_dec_and_test_zero(references);
116 }
117 return 1;
118 }
119
asn1_get_enc_ptr(ASN1_VALUE ** pval,const ASN1_ITEM * it)120 static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) {
121 const ASN1_AUX *aux;
122 if (!pval || !*pval) {
123 return NULL;
124 }
125 aux = it->funcs;
126 if (!aux || !(aux->flags & ASN1_AFLG_ENCODING)) {
127 return NULL;
128 }
129 return offset2ptr(*pval, aux->enc_offset);
130 }
131
asn1_enc_init(ASN1_VALUE ** pval,const ASN1_ITEM * it)132 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) {
133 ASN1_ENCODING *enc;
134 enc = asn1_get_enc_ptr(pval, it);
135 if (enc) {
136 enc->enc = NULL;
137 enc->len = 0;
138 enc->alias_only = 0;
139 enc->alias_only_on_next_parse = 0;
140 enc->modified = 1;
141 }
142 }
143
asn1_enc_free(ASN1_VALUE ** pval,const ASN1_ITEM * it)144 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
145 ASN1_ENCODING *enc;
146 enc = asn1_get_enc_ptr(pval, it);
147 if (enc) {
148 if (enc->enc && !enc->alias_only) {
149 OPENSSL_free(enc->enc);
150 }
151 enc->enc = NULL;
152 enc->len = 0;
153 enc->alias_only = 0;
154 enc->alias_only_on_next_parse = 0;
155 enc->modified = 1;
156 }
157 }
158
asn1_enc_save(ASN1_VALUE ** pval,const unsigned char * in,int inlen,const ASN1_ITEM * it)159 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
160 const ASN1_ITEM *it) {
161 ASN1_ENCODING *enc;
162 enc = asn1_get_enc_ptr(pval, it);
163 if (!enc) {
164 return 1;
165 }
166
167 if (!enc->alias_only) {
168 OPENSSL_free(enc->enc);
169 }
170
171 enc->alias_only = enc->alias_only_on_next_parse;
172 enc->alias_only_on_next_parse = 0;
173
174 if (enc->alias_only) {
175 enc->enc = (uint8_t *) in;
176 } else {
177 enc->enc = OPENSSL_malloc(inlen);
178 if (!enc->enc) {
179 return 0;
180 }
181 OPENSSL_memcpy(enc->enc, in, inlen);
182 }
183
184 enc->len = inlen;
185 enc->modified = 0;
186
187 return 1;
188 }
189
asn1_enc_restore(int * len,unsigned char ** out,ASN1_VALUE ** pval,const ASN1_ITEM * it)190 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
191 const ASN1_ITEM *it) {
192 ASN1_ENCODING *enc;
193 enc = asn1_get_enc_ptr(pval, it);
194 if (!enc || enc->modified) {
195 return 0;
196 }
197 if (out) {
198 OPENSSL_memcpy(*out, enc->enc, enc->len);
199 *out += enc->len;
200 }
201 if (len) {
202 *len = enc->len;
203 }
204 return 1;
205 }
206
207 /* Given an ASN1_TEMPLATE get a pointer to a field */
asn1_get_field_ptr(ASN1_VALUE ** pval,const ASN1_TEMPLATE * tt)208 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
209 ASN1_VALUE **pvaltmp;
210 if (tt->flags & ASN1_TFLG_COMBINE) {
211 return pval;
212 }
213 pvaltmp = offset2ptr(*pval, tt->offset);
214 /* NOTE for BOOLEAN types the field is just a plain int so we can't return
215 * int **, so settle for (int *). */
216 return pvaltmp;
217 }
218
219 /* Handle ANY DEFINED BY template, find the selector, look up the relevant
220 * ASN1_TEMPLATE in the table and return it. */
asn1_do_adb(ASN1_VALUE ** pval,const ASN1_TEMPLATE * tt,int nullerr)221 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
222 int nullerr) {
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
232 /* Else ANY DEFINED BY ... get the table */
233 adb = ASN1_ADB_ptr(tt->item);
234
235 /* Get the selector field */
236 sfld = offset2ptr(*pval, adb->offset);
237
238 /* Check if NULL */
239 if (*sfld == NULL) {
240 if (!adb->null_tt) {
241 goto err;
242 }
243 return adb->null_tt;
244 }
245
246 /* Convert type to a long:
247 * NB: don't check for NID_undef here because it
248 * might be a legitimate value in the table */
249 if (tt->flags & ASN1_TFLG_ADB_OID) {
250 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
251 } else {
252 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
253 }
254
255 /* Try to find matching entry in table Maybe should check application types
256 * first to allow application override? Might also be useful to have a flag
257 * which indicates table is sorted and we can do a binary search. For now
258 * stick to a linear search. */
259
260 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) {
261 if (atbl->value == selector) {
262 return &atbl->tt;
263 }
264 }
265
266 /* FIXME: need to search application table too */
267
268 /* No match, return default type */
269 if (!adb->default_tt) {
270 goto err;
271 }
272 return adb->default_tt;
273
274 err:
275 /* FIXME: should log the value or OID of unsupported type */
276 if (nullerr) {
277 OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
278 }
279 return NULL;
280 }
281