2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
5 /* ====================================================================
6 * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
61 #include <internal/cryptlib.h>
62 #include <openssl/asn1.h>
63 #include <openssl/asn1t.h>
64 #include <openssl/objects.h>
65 #include <openssl/err.h>
66 #include "asn1_locl.h"
68 /* Utility functions for manipulating fields and offsets */
70 /* Add 'offset' to 'addr' */
71 #define offset2ptr(addr, offset) (void *)(((char *) addr) + offset)
74 * Given an ASN1_ITEM CHOICE type return the selector value
77 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
79 int *sel = offset2ptr(*pval, it->utype);
84 * Given an ASN1_ITEM CHOICE type set the selector value, return old value.
87 int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
91 sel = offset2ptr(*pval, it->utype);
98 * Do reference counting. The value 'op' decides what to do. if it is +1
99 * then the count is incremented. If op is 0 count is set to 1. If op is -1
100 * count is decremented and the return value is the current reference count
101 * or 0 if no reference count exists.
104 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
108 if ((it->itype != ASN1_ITYPE_SEQUENCE)
109 && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
112 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
114 lck = offset2ptr(*pval, aux->ref_offset);
119 ret = CRYPTO_add(lck, op, aux->ref_lock);
121 fprintf(stderr, "%s: Reference Count: %d\n", it->sname, *lck);
125 fprintf(stderr, "%s, bad reference count\n", it->sname);
130 static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
136 if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
138 return offset2ptr(*pval, aux->enc_offset);
141 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
144 enc = asn1_get_enc_ptr(pval, it);
152 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
155 enc = asn1_get_enc_ptr(pval, it);
157 OPENSSL_free(enc->enc);
164 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
168 enc = asn1_get_enc_ptr(pval, it);
172 OPENSSL_free(enc->enc);
173 enc->enc = OPENSSL_malloc(inlen);
174 if (enc->enc == NULL)
176 memcpy(enc->enc, in, inlen);
183 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
187 enc = asn1_get_enc_ptr(pval, it);
188 if (!enc || enc->modified)
191 memcpy(*out, enc->enc, enc->len);
199 /* Given an ASN1_TEMPLATE get a pointer to a field */
200 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
202 ASN1_VALUE **pvaltmp;
203 pvaltmp = offset2ptr(*pval, tt->offset);
205 * NOTE for BOOLEAN types the field is just a plain int so we can't
206 * return int **, so settle for (int *).
212 * Handle ANY DEFINED BY template, find the selector, look up the relevant
213 * ASN1_TEMPLATE in the table and return it.
216 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
220 const ASN1_ADB_TABLE *atbl;
224 if (!(tt->flags & ASN1_TFLG_ADB_MASK))
227 /* Else ANY DEFINED BY ... get the table */
228 adb = ASN1_ADB_ptr(tt->item);
230 /* Get the selector field */
231 sfld = offset2ptr(*pval, adb->offset);
241 * Convert type to a long: NB: don't check for NID_undef here because it
242 * might be a legitimate value in the table
244 if (tt->flags & ASN1_TFLG_ADB_OID)
245 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
247 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
250 * Try to find matching entry in table Maybe should check application
251 * types first to allow application override? Might also be useful to
252 * have a flag which indicates table is sorted and we can do a binary
253 * search. For now stick to a linear search.
256 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
257 if (atbl->value == selector)
260 /* FIXME: need to search application table too */
262 /* No match, return default type */
263 if (!adb->default_tt)
265 return adb->default_tt;
268 /* FIXME: should log the value or OID of unsupported type */
270 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);