1/* $OpenBSD: tasn_utl.c,v 1.18 2022/12/26 07:18:51 jmc 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 <limits.h>
60#include <stddef.h>
61#include <string.h>
62
63#include <openssl/asn1.h>
64#include <openssl/asn1t.h>
65#include <openssl/objects.h>
66#include <openssl/err.h>
67
68#include "bytestring.h"
69
70/* Utility functions for manipulating fields and offsets */
71
72/* Add 'offset' to 'addr' */
73#define offset2ptr(addr, offset) (void *)(((char *) addr) + offset)
74
75/* Given an ASN1_ITEM CHOICE type return
76 * the selector value
77 */
78
79int
80asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
81{
82	int *sel = offset2ptr(*pval, it->utype);
83	return *sel;
84}
85
86/* Given an ASN1_ITEM CHOICE type set
87 * the selector value, return old value.
88 */
89
90int
91asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it)
92{
93	int *sel, ret;
94	sel = offset2ptr(*pval, it->utype);
95	ret = *sel;
96	*sel = value;
97	return ret;
98}
99
100/* Do reference counting. The value 'op' decides what to do.
101 * if it is +1 then the count is incremented. If op is 0 count is
102 * set to 1. If op is -1 count is decremented and the return value
103 * is the current reference count or 0 if no reference count exists.
104 */
105
106int
107asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
108{
109	const ASN1_AUX *aux;
110	int *lck, ret;
111
112	if ((it->itype != ASN1_ITYPE_SEQUENCE) &&
113	    (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
114		return 0;
115	aux = it->funcs;
116	if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
117		return 0;
118	lck = offset2ptr(*pval, aux->ref_offset);
119	if (op == 0) {
120		*lck = 1;
121		return 1;
122	}
123	ret = CRYPTO_add(lck, op, aux->ref_lock);
124	return ret;
125}
126
127static ASN1_ENCODING *
128asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
129{
130	const ASN1_AUX *aux = it->funcs;
131
132	if (pval == NULL || *pval == NULL)
133		return NULL;
134
135	if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
136		return NULL;
137
138	return offset2ptr(*pval, aux->enc_offset);
139}
140
141void
142asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
143{
144	ASN1_ENCODING *aenc;
145
146	if ((aenc = asn1_get_enc_ptr(pval, it)) == NULL)
147		return;
148
149	aenc->enc = NULL;
150	aenc->len = 0;
151	aenc->modified = 1;
152}
153
154static void
155asn1_enc_clear(ASN1_ENCODING *aenc)
156{
157	freezero(aenc->enc, aenc->len);
158	aenc->enc = NULL;
159	aenc->len = 0;
160	aenc->modified = 1;
161}
162
163void
164asn1_enc_cleanup(ASN1_VALUE **pval, const ASN1_ITEM *it)
165{
166	ASN1_ENCODING *aenc;
167
168	if ((aenc = asn1_get_enc_ptr(pval, it)) == NULL)
169		return;
170
171	asn1_enc_clear(aenc);
172}
173
174int
175asn1_enc_save(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it)
176{
177	ASN1_ENCODING *aenc;
178	uint8_t *data = NULL;
179	size_t data_len = 0;
180
181	if ((aenc = asn1_get_enc_ptr(pval, it)) == NULL)
182		return 1;
183
184	asn1_enc_clear(aenc);
185
186	if (!CBS_stow(cbs, &data, &data_len))
187		return 0;
188	if (data_len > LONG_MAX) {
189		freezero(data, data_len);
190		return 0;
191	}
192
193	aenc->enc = data;
194	aenc->len = (long)data_len;
195	aenc->modified = 0;
196
197	return 1;
198}
199
200int
201asn1_enc_restore(int *out_len, unsigned char **out, ASN1_VALUE **pval,
202    const ASN1_ITEM *it)
203{
204	ASN1_ENCODING *aenc;
205
206	if ((aenc = asn1_get_enc_ptr(pval, it)) == NULL)
207		return 0;
208
209	if (aenc->modified)
210		return 0;
211
212	if (out != NULL) {
213		memcpy(*out, aenc->enc, aenc->len);
214		*out += aenc->len;
215	}
216
217	if (out_len != NULL)
218		*out_len = aenc->len;
219
220	return 1;
221}
222
223/* Given an ASN1_TEMPLATE get a pointer to a field */
224ASN1_VALUE **
225asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
226{
227	ASN1_VALUE **pvaltmp;
228
229	pvaltmp = offset2ptr(*pval, tt->offset);
230	/* NOTE for BOOLEAN types the field is just a plain
231 	 * int so we can't return int **, so settle for
232	 * (int *).
233	 */
234	return pvaltmp;
235}
236
237/* Handle ANY DEFINED BY template, find the selector, look up
238 * the relevant ASN1_TEMPLATE in the table and return it.
239 */
240
241const ASN1_TEMPLATE *
242asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr)
243{
244	const ASN1_ADB *adb;
245	const ASN1_ADB_TABLE *atbl;
246	long selector;
247	ASN1_VALUE **sfld;
248	int i;
249
250	if (!(tt->flags & ASN1_TFLG_ADB_MASK))
251		return tt;
252
253	/* Else ANY DEFINED BY ... get the table */
254	adb = (const ASN1_ADB *)tt->item;
255
256	/* Get the selector field */
257	sfld = offset2ptr(*pval, adb->offset);
258
259	/* Check if NULL */
260	if (!sfld) {
261		if (!adb->null_tt)
262			goto err;
263		return adb->null_tt;
264	}
265
266	/* Convert type to a long:
267	 * NB: don't check for NID_undef here because it
268	 * might be a legitimate value in the table
269	 */
270	if (tt->flags & ASN1_TFLG_ADB_OID)
271		selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
272	else
273		selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
274
275	/* Try to find matching entry in table
276	 * Maybe should check application types first to
277	 * allow application override? Might also be useful
278	 * to have a flag which indicates table is sorted and
279	 * we can do a binary search. For now stick to a
280	 * linear search.
281	 */
282
283	for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
284		if (atbl->value == selector)
285			return &atbl->tt;
286
287	/* FIXME: need to search application table too */
288
289	/* No match, return default type */
290	if (!adb->default_tt)
291		goto err;
292	return adb->default_tt;
293
294 err:
295	/* FIXME: should log the value or OID of unsupported type */
296	if (nullerr)
297		ASN1error(ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
298	return NULL;
299}
300