/src/boringssl/crypto/asn1/tasn_utl.c
Line | Count | Source (jump to first uncovered line) |
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/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 | 0 | #define offset2ptr(addr, offset) (void *)(((char *)(addr)) + (offset)) |
77 | | |
78 | | // Given an ASN1_ITEM CHOICE type return the selector value |
79 | 0 | int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) { |
80 | 0 | int *sel = offset2ptr(*pval, it->utype); |
81 | 0 | return *sel; |
82 | 0 | } |
83 | | |
84 | | // Given an ASN1_ITEM CHOICE type set the selector value, return old value. |
85 | | int asn1_set_choice_selector(ASN1_VALUE **pval, int value, |
86 | 0 | const ASN1_ITEM *it) { |
87 | 0 | int *sel, ret; |
88 | 0 | sel = offset2ptr(*pval, it->utype); |
89 | 0 | ret = *sel; |
90 | 0 | *sel = value; |
91 | 0 | return ret; |
92 | 0 | } |
93 | | |
94 | | static CRYPTO_refcount_t *asn1_get_references(ASN1_VALUE **pval, |
95 | 0 | const ASN1_ITEM *it) { |
96 | 0 | if (it->itype != ASN1_ITYPE_SEQUENCE) { |
97 | 0 | return NULL; |
98 | 0 | } |
99 | 0 | const ASN1_AUX *aux = it->funcs; |
100 | 0 | if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) { |
101 | 0 | return NULL; |
102 | 0 | } |
103 | 0 | return offset2ptr(*pval, aux->ref_offset); |
104 | 0 | } |
105 | | |
106 | 0 | void asn1_refcount_set_one(ASN1_VALUE **pval, const ASN1_ITEM *it) { |
107 | 0 | CRYPTO_refcount_t *references = asn1_get_references(pval, it); |
108 | 0 | if (references != NULL) { |
109 | 0 | *references = 1; |
110 | 0 | } |
111 | 0 | } |
112 | | |
113 | 0 | int asn1_refcount_dec_and_test_zero(ASN1_VALUE **pval, const ASN1_ITEM *it) { |
114 | 0 | CRYPTO_refcount_t *references = asn1_get_references(pval, it); |
115 | 0 | if (references != NULL) { |
116 | 0 | return CRYPTO_refcount_dec_and_test_zero(references); |
117 | 0 | } |
118 | 0 | return 1; |
119 | 0 | } |
120 | | |
121 | 0 | static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) { |
122 | 0 | assert(it->itype == ASN1_ITYPE_SEQUENCE); |
123 | 0 | const ASN1_AUX *aux; |
124 | 0 | if (!pval || !*pval) { |
125 | 0 | return NULL; |
126 | 0 | } |
127 | 0 | aux = it->funcs; |
128 | 0 | if (!aux || !(aux->flags & ASN1_AFLG_ENCODING)) { |
129 | 0 | return NULL; |
130 | 0 | } |
131 | 0 | return offset2ptr(*pval, aux->enc_offset); |
132 | 0 | } |
133 | | |
134 | 0 | void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) { |
135 | 0 | ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it); |
136 | 0 | if (enc) { |
137 | 0 | enc->enc = NULL; |
138 | 0 | enc->len = 0; |
139 | 0 | enc->buf = NULL; |
140 | 0 | } |
141 | 0 | } |
142 | | |
143 | 0 | void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) { |
144 | 0 | ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it); |
145 | 0 | if (enc) { |
146 | 0 | asn1_encoding_clear(enc); |
147 | 0 | } |
148 | 0 | } |
149 | | |
150 | | int asn1_enc_save(ASN1_VALUE **pval, const uint8_t *in, size_t in_len, |
151 | 0 | const ASN1_ITEM *it, CRYPTO_BUFFER *buf) { |
152 | 0 | ASN1_ENCODING *enc; |
153 | 0 | enc = asn1_get_enc_ptr(pval, it); |
154 | 0 | if (!enc) { |
155 | 0 | return 1; |
156 | 0 | } |
157 | | |
158 | 0 | asn1_encoding_clear(enc); |
159 | 0 | if (buf != NULL) { |
160 | 0 | assert(CRYPTO_BUFFER_data(buf) <= in && |
161 | 0 | in + in_len <= CRYPTO_BUFFER_data(buf) + CRYPTO_BUFFER_len(buf)); |
162 | 0 | CRYPTO_BUFFER_up_ref(buf); |
163 | 0 | enc->buf = buf; |
164 | 0 | enc->enc = (uint8_t *)in; |
165 | 0 | } else { |
166 | 0 | enc->enc = OPENSSL_memdup(in, in_len); |
167 | 0 | if (!enc->enc) { |
168 | 0 | return 0; |
169 | 0 | } |
170 | 0 | } |
171 | | |
172 | 0 | enc->len = in_len; |
173 | 0 | return 1; |
174 | 0 | } |
175 | | |
176 | 0 | void asn1_encoding_clear(ASN1_ENCODING *enc) { |
177 | 0 | if (enc->buf != NULL) { |
178 | 0 | CRYPTO_BUFFER_free(enc->buf); |
179 | 0 | } else { |
180 | 0 | OPENSSL_free(enc->enc); |
181 | 0 | } |
182 | 0 | enc->enc = NULL; |
183 | 0 | enc->len = 0; |
184 | 0 | enc->buf = NULL; |
185 | 0 | } |
186 | | |
187 | | int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, |
188 | 0 | const ASN1_ITEM *it) { |
189 | 0 | ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it); |
190 | 0 | if (!enc || enc->len == 0) { |
191 | 0 | return 0; |
192 | 0 | } |
193 | 0 | if (out) { |
194 | 0 | OPENSSL_memcpy(*out, enc->enc, enc->len); |
195 | 0 | *out += enc->len; |
196 | 0 | } |
197 | 0 | if (len) { |
198 | 0 | *len = enc->len; |
199 | 0 | } |
200 | 0 | return 1; |
201 | 0 | } |
202 | | |
203 | | // Given an ASN1_TEMPLATE get a pointer to a field |
204 | 0 | ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) { |
205 | 0 | 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 | 0 | return pvaltmp; |
209 | 0 | } |
210 | | |
211 | | // Handle ANY DEFINED BY template, find the selector, look up the relevant |
212 | | // ASN1_TEMPLATE in the table and return it. |
213 | | const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, |
214 | 0 | int nullerr) { |
215 | 0 | const ASN1_ADB *adb; |
216 | 0 | const ASN1_ADB_TABLE *atbl; |
217 | 0 | ASN1_VALUE **sfld; |
218 | 0 | int i; |
219 | 0 | if (!(tt->flags & ASN1_TFLG_ADB_MASK)) { |
220 | 0 | return tt; |
221 | 0 | } |
222 | | |
223 | | // Else ANY DEFINED BY ... get the table |
224 | 0 | adb = ASN1_ADB_ptr(tt->item); |
225 | | |
226 | | // Get the selector field |
227 | 0 | sfld = offset2ptr(*pval, adb->offset); |
228 | | |
229 | | // Check if NULL |
230 | 0 | if (*sfld == NULL) { |
231 | 0 | if (!adb->null_tt) { |
232 | 0 | goto err; |
233 | 0 | } |
234 | 0 | return adb->null_tt; |
235 | 0 | } |
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 | 0 | assert(tt->flags & ASN1_TFLG_ADB_OID); |
241 | 0 | 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 | 0 | for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) { |
249 | 0 | if (atbl->value == selector) { |
250 | 0 | return &atbl->tt; |
251 | 0 | } |
252 | 0 | } |
253 | | |
254 | | // FIXME: need to search application table too |
255 | | |
256 | | // No match, return default type |
257 | 0 | if (!adb->default_tt) { |
258 | 0 | goto err; |
259 | 0 | } |
260 | 0 | return adb->default_tt; |
261 | | |
262 | 0 | err: |
263 | | // FIXME: should log the value or OID of unsupported type |
264 | 0 | if (nullerr) { |
265 | 0 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); |
266 | 0 | } |
267 | 0 | return NULL; |
268 | 0 | } |