Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/libressl/src/crypto/asn1/tasn_utl.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* $OpenBSD: tasn_utl.c,v 1.12 2017/01/29 17:49:22 beck Exp $ */ | - | ||||||||||||
2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | - | ||||||||||||
3 | * project 2000. | - | ||||||||||||
4 | */ | - | ||||||||||||
5 | /* ==================================================================== | - | ||||||||||||
6 | * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved. | - | ||||||||||||
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 | - | ||||||||||||
13 | * notice, this list of conditions and the following disclaimer. | - | ||||||||||||
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 | - | |||||||||||||
59 | #include <stddef.h> | - | ||||||||||||
60 | #include <string.h> | - | ||||||||||||
61 | #include <openssl/asn1.h> | - | ||||||||||||
62 | #include <openssl/asn1t.h> | - | ||||||||||||
63 | #include <openssl/objects.h> | - | ||||||||||||
64 | #include <openssl/err.h> | - | ||||||||||||
65 | - | |||||||||||||
66 | /* Utility functions for manipulating fields and offsets */ | - | ||||||||||||
67 | - | |||||||||||||
68 | /* Add 'offset' to 'addr' */ | - | ||||||||||||
69 | #define offset2ptr(addr, offset) (void *)(((char *) addr) + offset) | - | ||||||||||||
70 | - | |||||||||||||
71 | /* Given an ASN1_ITEM CHOICE type return | - | ||||||||||||
72 | * the selector value | - | ||||||||||||
73 | */ | - | ||||||||||||
74 | - | |||||||||||||
75 | int | - | ||||||||||||
76 | asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) | - | ||||||||||||
77 | { | - | ||||||||||||
78 | int *sel = offset2ptr(*pval, it->utype); | - | ||||||||||||
79 | return *sel; executed 276 times by 3 tests: return *sel; Executed by:
| 276 | ||||||||||||
80 | } | - | ||||||||||||
81 | - | |||||||||||||
82 | /* Given an ASN1_ITEM CHOICE type set | - | ||||||||||||
83 | * the selector value, return old value. | - | ||||||||||||
84 | */ | - | ||||||||||||
85 | - | |||||||||||||
86 | int | - | ||||||||||||
87 | asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it) | - | ||||||||||||
88 | { | - | ||||||||||||
89 | int *sel, ret; | - | ||||||||||||
90 | sel = offset2ptr(*pval, it->utype); | - | ||||||||||||
91 | ret = *sel; | - | ||||||||||||
92 | *sel = value; | - | ||||||||||||
93 | return ret; executed 96 times by 2 tests: return ret; Executed by:
| 96 | ||||||||||||
94 | } | - | ||||||||||||
95 | - | |||||||||||||
96 | /* Do reference counting. The value 'op' decides what to do. | - | ||||||||||||
97 | * if it is +1 then the count is incremented. If op is 0 count is | - | ||||||||||||
98 | * set to 1. If op is -1 count is decremented and the return value | - | ||||||||||||
99 | * is the current refrence count or 0 if no reference count exists. | - | ||||||||||||
100 | */ | - | ||||||||||||
101 | - | |||||||||||||
102 | int | - | ||||||||||||
103 | asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) | - | ||||||||||||
104 | { | - | ||||||||||||
105 | const ASN1_AUX *aux; | - | ||||||||||||
106 | int *lck, ret; | - | ||||||||||||
107 | - | |||||||||||||
108 | if ((it->itype != ASN1_ITYPE_SEQUENCE) &&
| 31-14777 | ||||||||||||
109 | (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
| 0-31 | ||||||||||||
110 | return 0; never executed: return 0; | 0 | ||||||||||||
111 | aux = it->funcs; | - | ||||||||||||
112 | if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
| 1032-12729 | ||||||||||||
113 | return 0; executed 13761 times by 10 tests: return 0; Executed by:
| 13761 | ||||||||||||
114 | lck = offset2ptr(*pval, aux->ref_offset); | - | ||||||||||||
115 | if (op == 0) {
| 251-796 | ||||||||||||
116 | *lck = 1; | - | ||||||||||||
117 | return 1; executed 251 times by 8 tests: return 1; Executed by:
| 251 | ||||||||||||
118 | } | - | ||||||||||||
119 | ret = CRYPTO_add(lck, op, aux->ref_lock); | - | ||||||||||||
120 | return ret; executed 796 times by 8 tests: return ret; Executed by:
| 796 | ||||||||||||
121 | } | - | ||||||||||||
122 | - | |||||||||||||
123 | static ASN1_ENCODING * | - | ||||||||||||
124 | asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) | - | ||||||||||||
125 | { | - | ||||||||||||
126 | const ASN1_AUX *aux; | - | ||||||||||||
127 | - | |||||||||||||
128 | if (!pval || !*pval)
| 0-30336 | ||||||||||||
129 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
130 | aux = it->funcs; | - | ||||||||||||
131 | if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
| 1830-25635 | ||||||||||||
132 | return NULL; executed 28506 times by 11 tests: return ((void *)0) ; Executed by:
| 28506 | ||||||||||||
133 | return offset2ptr(*pval, aux->enc_offset); executed 1830 times by 8 tests: return (void *)(((char *) *pval) + aux->enc_offset); Executed by:
| 1830 | ||||||||||||
134 | } | - | ||||||||||||
135 | - | |||||||||||||
136 | void | - | ||||||||||||
137 | asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) | - | ||||||||||||
138 | { | - | ||||||||||||
139 | ASN1_ENCODING *enc; | - | ||||||||||||
140 | - | |||||||||||||
141 | enc = asn1_get_enc_ptr(pval, it); | - | ||||||||||||
142 | if (enc) {
| 251-6942 | ||||||||||||
143 | enc->enc = NULL; | - | ||||||||||||
144 | enc->len = 0; | - | ||||||||||||
145 | enc->modified = 1; | - | ||||||||||||
146 | } executed 251 times by 8 tests: end of block Executed by:
| 251 | ||||||||||||
147 | } executed 7193 times by 10 tests: end of block Executed by:
| 7193 | ||||||||||||
148 | - | |||||||||||||
149 | void | - | ||||||||||||
150 | asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | - | ||||||||||||
151 | { | - | ||||||||||||
152 | ASN1_ENCODING *enc; | - | ||||||||||||
153 | - | |||||||||||||
154 | enc = asn1_get_enc_ptr(pval, it); | - | ||||||||||||
155 | if (enc) {
| 246-6819 | ||||||||||||
156 | free(enc->enc); | - | ||||||||||||
157 | enc->enc = NULL; | - | ||||||||||||
158 | enc->len = 0; | - | ||||||||||||
159 | enc->modified = 1; | - | ||||||||||||
160 | } executed 246 times by 7 tests: end of block Executed by:
| 246 | ||||||||||||
161 | } executed 7065 times by 10 tests: end of block Executed by:
| 7065 | ||||||||||||
162 | - | |||||||||||||
163 | int | - | ||||||||||||
164 | asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, | - | ||||||||||||
165 | const ASN1_ITEM *it) | - | ||||||||||||
166 | { | - | ||||||||||||
167 | ASN1_ENCODING *enc; | - | ||||||||||||
168 | - | |||||||||||||
169 | enc = asn1_get_enc_ptr(pval, it); | - | ||||||||||||
170 | if (!enc)
| 219-5047 | ||||||||||||
171 | return 1; executed 5047 times by 11 tests: return 1; Executed by:
| 5047 | ||||||||||||
172 | - | |||||||||||||
173 | free(enc->enc); | - | ||||||||||||
174 | enc->enc = malloc(inlen); | - | ||||||||||||
175 | if (!enc->enc)
| 0-219 | ||||||||||||
176 | return 0; never executed: return 0; | 0 | ||||||||||||
177 | memcpy(enc->enc, in, inlen); | - | ||||||||||||
178 | enc->len = inlen; | - | ||||||||||||
179 | enc->modified = 0; | - | ||||||||||||
180 | - | |||||||||||||
181 | return 1; executed 219 times by 7 tests: return 1; Executed by:
| 219 | ||||||||||||
182 | } | - | ||||||||||||
183 | - | |||||||||||||
184 | int | - | ||||||||||||
185 | asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, | - | ||||||||||||
186 | const ASN1_ITEM *it) | - | ||||||||||||
187 | { | - | ||||||||||||
188 | ASN1_ENCODING *enc; | - | ||||||||||||
189 | - | |||||||||||||
190 | enc = asn1_get_enc_ptr(pval, it); | - | ||||||||||||
191 | if (!enc || enc->modified)
| 10-9698 | ||||||||||||
192 | return 0; executed 9708 times by 11 tests: return 0; Executed by:
| 9708 | ||||||||||||
193 | if (out) {
| 388-716 | ||||||||||||
194 | memcpy(*out, enc->enc, enc->len); | - | ||||||||||||
195 | *out += enc->len; | - | ||||||||||||
196 | } executed 388 times by 6 tests: end of block Executed by:
| 388 | ||||||||||||
197 | if (len)
| 0-1104 | ||||||||||||
198 | *len = enc->len; executed 1104 times by 6 tests: *len = enc->len; Executed by:
| 1104 | ||||||||||||
199 | return 1; executed 1104 times by 6 tests: return 1; Executed by:
| 1104 | ||||||||||||
200 | } | - | ||||||||||||
201 | - | |||||||||||||
202 | /* Given an ASN1_TEMPLATE get a pointer to a field */ | - | ||||||||||||
203 | ASN1_VALUE ** | - | ||||||||||||
204 | asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) | - | ||||||||||||
205 | { | - | ||||||||||||
206 | ASN1_VALUE **pvaltmp; | - | ||||||||||||
207 | - | |||||||||||||
208 | if (tt->flags & ASN1_TFLG_COMBINE)
| 168-77642 | ||||||||||||
209 | return pval; executed 168 times by 1 test: return pval; Executed by:
| 168 | ||||||||||||
210 | pvaltmp = offset2ptr(*pval, tt->offset); | - | ||||||||||||
211 | /* NOTE for BOOLEAN types the field is just a plain | - | ||||||||||||
212 | * int so we can't return int **, so settle for | - | ||||||||||||
213 | * (int *). | - | ||||||||||||
214 | */ | - | ||||||||||||
215 | return pvaltmp; executed 77642 times by 11 tests: return pvaltmp; Executed by:
| 77642 | ||||||||||||
216 | } | - | ||||||||||||
217 | - | |||||||||||||
218 | /* Handle ANY DEFINED BY template, find the selector, look up | - | ||||||||||||
219 | * the relevant ASN1_TEMPLATE in the table and return it. | - | ||||||||||||
220 | */ | - | ||||||||||||
221 | - | |||||||||||||
222 | const ASN1_TEMPLATE * | - | ||||||||||||
223 | asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr) | - | ||||||||||||
224 | { | - | ||||||||||||
225 | const ASN1_ADB *adb; | - | ||||||||||||
226 | const ASN1_ADB_TABLE *atbl; | - | ||||||||||||
227 | long selector; | - | ||||||||||||
228 | ASN1_VALUE **sfld; | - | ||||||||||||
229 | int i; | - | ||||||||||||
230 | - | |||||||||||||
231 | if (!(tt->flags & ASN1_TFLG_ADB_MASK))
| 39-59911 | ||||||||||||
232 | return tt; executed 59911 times by 11 tests: return tt; Executed by:
| 59911 | ||||||||||||
233 | - | |||||||||||||
234 | /* Else ANY DEFINED BY ... get the table */ | - | ||||||||||||
235 | adb = (const ASN1_ADB *)tt->item; | - | ||||||||||||
236 | - | |||||||||||||
237 | /* Get the selector field */ | - | ||||||||||||
238 | sfld = offset2ptr(*pval, adb->offset); | - | ||||||||||||
239 | - | |||||||||||||
240 | /* Check if NULL */ | - | ||||||||||||
241 | if (!sfld) {
| 0-39 | ||||||||||||
242 | if (!adb->null_tt)
| 0 | ||||||||||||
243 | goto err; never executed: goto err; | 0 | ||||||||||||
244 | return adb->null_tt; never executed: return adb->null_tt; | 0 | ||||||||||||
245 | } | - | ||||||||||||
246 | - | |||||||||||||
247 | /* Convert type to a long: | - | ||||||||||||
248 | * NB: don't check for NID_undef here because it | - | ||||||||||||
249 | * might be a legitimate value in the table | - | ||||||||||||
250 | */ | - | ||||||||||||
251 | if (tt->flags & ASN1_TFLG_ADB_OID)
| 0-39 | ||||||||||||
252 | selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); executed 39 times by 1 test: selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); Executed by:
| 39 | ||||||||||||
253 | else | - | ||||||||||||
254 | selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); never executed: selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); | 0 | ||||||||||||
255 | - | |||||||||||||
256 | /* Try to find matching entry in table | - | ||||||||||||
257 | * Maybe should check application types first to | - | ||||||||||||
258 | * allow application override? Might also be useful | - | ||||||||||||
259 | * to have a flag which indicates table is sorted and | - | ||||||||||||
260 | * we can do a binary search. For now stick to a | - | ||||||||||||
261 | * linear search. | - | ||||||||||||
262 | */ | - | ||||||||||||
263 | - | |||||||||||||
264 | for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
| 7-94 | ||||||||||||
265 | if (atbl->value == selector)
| 32-62 | ||||||||||||
266 | return &atbl->tt; executed 32 times by 1 test: return &atbl->tt; Executed by:
| 32 | ||||||||||||
267 | - | |||||||||||||
268 | /* FIXME: need to search application table too */ | - | ||||||||||||
269 | - | |||||||||||||
270 | /* No match, return default type */ | - | ||||||||||||
271 | if (!adb->default_tt)
| 0-7 | ||||||||||||
272 | goto err; never executed: goto err; | 0 | ||||||||||||
273 | return adb->default_tt; executed 7 times by 1 test: return adb->default_tt; Executed by:
| 7 | ||||||||||||
274 | - | |||||||||||||
275 | err: | - | ||||||||||||
276 | /* FIXME: should log the value or OID of unsupported type */ | - | ||||||||||||
277 | if (nullerr)
| 0 | ||||||||||||
278 | ASN1error(ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); never executed: ERR_put_error(13,(0xfff),(164),__FILE__,278); | 0 | ||||||||||||
279 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
280 | } | - | ||||||||||||
Source code | Switch to Preprocessed file |