1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young ([email protected]).
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 ([email protected]).
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 ([email protected])"
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 ([email protected])"
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/err.h>
64 #include <openssl/mem.h>
65 #include <openssl/obj.h>
66 #include <openssl/pool.h>
67 #include <openssl/thread.h>
68
69 #include "../internal.h"
70 #include "internal.h"
71
72
73 // Utility functions for manipulating fields and offsets
74
75 // Add 'offset' to 'addr'
76 #define offset2ptr(addr, offset) (void *)(((char *)(addr)) + (offset))
77
78 // Given an ASN1_ITEM CHOICE type return the selector value
asn1_get_choice_selector(ASN1_VALUE ** pval,const ASN1_ITEM * it)79 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) {
80 int *sel = offset2ptr(*pval, it->utype);
81 return *sel;
82 }
83
84 // 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)85 int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
86 const ASN1_ITEM *it) {
87 int *sel, ret;
88 sel = offset2ptr(*pval, it->utype);
89 ret = *sel;
90 *sel = value;
91 return ret;
92 }
93
asn1_get_references(ASN1_VALUE ** pval,const ASN1_ITEM * it)94 static CRYPTO_refcount_t *asn1_get_references(ASN1_VALUE **pval,
95 const ASN1_ITEM *it) {
96 if (it->itype != ASN1_ITYPE_SEQUENCE) {
97 return NULL;
98 }
99 const ASN1_AUX *aux = it->funcs;
100 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) {
101 return NULL;
102 }
103 return offset2ptr(*pval, aux->ref_offset);
104 }
105
asn1_refcount_set_one(ASN1_VALUE ** pval,const ASN1_ITEM * it)106 void asn1_refcount_set_one(ASN1_VALUE **pval, const ASN1_ITEM *it) {
107 CRYPTO_refcount_t *references = asn1_get_references(pval, it);
108 if (references != NULL) {
109 *references = 1;
110 }
111 }
112
asn1_refcount_dec_and_test_zero(ASN1_VALUE ** pval,const ASN1_ITEM * it)113 int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it) {
114 CRYPTO_refcount_t *references = asn1_get_references(pval, it);
115 if (references != NULL) {
116 return CRYPTO_refcount_dec_and_test_zero(references);
117 }
118 return 1;
119 }
120
asn1_get_enc_ptr(ASN1_VALUE ** pval,const ASN1_ITEM * it)121 static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) {
122 assert(it->itype == ASN1_ITYPE_SEQUENCE);
123 const ASN1_AUX *aux;
124 if (!pval || !*pval) {
125 return NULL;
126 }
127 aux = it->funcs;
128 if (!aux || !(aux->flags & ASN1_AFLG_ENCODING)) {
129 return NULL;
130 }
131 return offset2ptr(*pval, aux->enc_offset);
132 }
133
asn1_enc_init(ASN1_VALUE ** pval,const ASN1_ITEM * it)134 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) {
135 ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
136 if (enc) {
137 enc->enc = NULL;
138 enc->len = 0;
139 enc->buf = NULL;
140 }
141 }
142
asn1_enc_free(ASN1_VALUE ** pval,const ASN1_ITEM * it)143 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) {
144 ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
145 if (enc) {
146 asn1_encoding_clear(enc);
147 }
148 }
149
asn1_enc_save(ASN1_VALUE ** pval,const uint8_t * in,size_t in_len,const ASN1_ITEM * it,CRYPTO_BUFFER * buf)150 int asn1_enc_save(ASN1_VALUE **pval, const uint8_t *in, size_t in_len,
151 const ASN1_ITEM *it, CRYPTO_BUFFER *buf) {
152 ASN1_ENCODING *enc;
153 enc = asn1_get_enc_ptr(pval, it);
154 if (!enc) {
155 return 1;
156 }
157
158 asn1_encoding_clear(enc);
159 if (buf != NULL) {
160 assert(CRYPTO_BUFFER_data(buf) <= in &&
161 in + in_len <= CRYPTO_BUFFER_data(buf) + CRYPTO_BUFFER_len(buf));
162 CRYPTO_BUFFER_up_ref(buf);
163 enc->buf = buf;
164 enc->enc = (uint8_t *)in;
165 } else {
166 enc->enc = OPENSSL_memdup(in, in_len);
167 if (!enc->enc) {
168 return 0;
169 }
170 }
171
172 enc->len = in_len;
173 return 1;
174 }
175
asn1_encoding_clear(ASN1_ENCODING * enc)176 void asn1_encoding_clear(ASN1_ENCODING *enc) {
177 if (enc->buf != NULL) {
178 CRYPTO_BUFFER_free(enc->buf);
179 } else {
180 OPENSSL_free(enc->enc);
181 }
182 enc->enc = NULL;
183 enc->len = 0;
184 enc->buf = NULL;
185 }
186
asn1_enc_restore(int * len,unsigned char ** out,ASN1_VALUE ** pval,const ASN1_ITEM * it)187 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
188 const ASN1_ITEM *it) {
189 ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
190 if (!enc || enc->len == 0) {
191 return 0;
192 }
193 if (out) {
194 OPENSSL_memcpy(*out, enc->enc, enc->len);
195 *out += enc->len;
196 }
197 if (len) {
198 *len = enc->len;
199 }
200 return 1;
201 }
202
203 // Given an ASN1_TEMPLATE get a pointer to a field
asn1_get_field_ptr(ASN1_VALUE ** pval,const ASN1_TEMPLATE * tt)204 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
205 ASN1_VALUE **pvaltmp = offset2ptr(*pval, tt->offset);
206 // NOTE for BOOLEAN types the field is just a plain int so we can't return
207 // int **, so settle for (int *).
208 return pvaltmp;
209 }
210
211 // Handle ANY DEFINED BY template, find the selector, look up the relevant
212 // ASN1_TEMPLATE in the table and return it.
asn1_do_adb(ASN1_VALUE ** pval,const ASN1_TEMPLATE * tt,int nullerr)213 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
214 int nullerr) {
215 const ASN1_ADB *adb;
216 const ASN1_ADB_TABLE *atbl;
217 ASN1_VALUE **sfld;
218 int i;
219 if (!(tt->flags & ASN1_TFLG_ADB_MASK)) {
220 return tt;
221 }
222
223 // Else ANY DEFINED BY ... get the table
224 adb = ASN1_ADB_ptr(tt->item);
225
226 // Get the selector field
227 sfld = offset2ptr(*pval, adb->offset);
228
229 // Check if NULL
230 if (*sfld == NULL) {
231 if (!adb->null_tt) {
232 goto err;
233 }
234 return adb->null_tt;
235 }
236
237 // Convert type to a NID:
238 // NB: don't check for NID_undef here because it
239 // might be a legitimate value in the table
240 assert(tt->flags & ASN1_TFLG_ADB_OID);
241 int selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
242
243 // Try to find matching entry in table Maybe should check application types
244 // first to allow application override? Might also be useful to have a flag
245 // which indicates table is sorted and we can do a binary search. For now
246 // stick to a linear search.
247
248 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) {
249 if (atbl->value == selector) {
250 return &atbl->tt;
251 }
252 }
253
254 // FIXME: need to search application table too
255
256 // No match, return default type
257 if (!adb->default_tt) {
258 goto err;
259 }
260 return adb->default_tt;
261
262 err:
263 // FIXME: should log the value or OID of unsupported type
264 if (nullerr) {
265 OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
266 }
267 return NULL;
268 }
269