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