2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <internal/cryptlib.h>
13 #include <openssl/asn1.h>
14 #include <openssl/asn1t.h>
15 #include <openssl/objects.h>
16 #include <openssl/err.h>
17 #include "asn1_locl.h"
19 /* Utility functions for manipulating fields and offsets */
21 /* Add 'offset' to 'addr' */
22 #define offset2ptr(addr, offset) (void *)(((char *) addr) + offset)
25 * Given an ASN1_ITEM CHOICE type return the selector value
28 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
30 int *sel = offset2ptr(*pval, it->utype);
35 * Given an ASN1_ITEM CHOICE type set the selector value, return old value.
38 int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
42 sel = offset2ptr(*pval, it->utype);
49 * Do reference counting. The value 'op' decides what to do. if it is +1
50 * then the count is incremented. If op is 0 count is set to 1. If op is -1
51 * count is decremented and the return value is the current reference count
52 * or 0 if no reference count exists.
55 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
60 if ((it->itype != ASN1_ITYPE_SEQUENCE)
61 && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
64 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
66 lck = offset2ptr(*pval, aux->ref_offset);
67 lock = offset2ptr(*pval, aux->ref_lock);
70 *lock = CRYPTO_THREAD_lock_new();
75 CRYPTO_atomic_add(lck, op, &ret, *lock);
77 fprintf(stderr, "%p:%4d:%s\n", it, *lck, it->sname);
79 REF_ASSERT_ISNT(ret < 0);
81 CRYPTO_THREAD_lock_free(*lock);
85 static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
91 if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
93 return offset2ptr(*pval, aux->enc_offset);
96 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
99 enc = asn1_get_enc_ptr(pval, it);
107 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
110 enc = asn1_get_enc_ptr(pval, it);
112 OPENSSL_free(enc->enc);
119 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
123 enc = asn1_get_enc_ptr(pval, it);
127 OPENSSL_free(enc->enc);
128 enc->enc = OPENSSL_malloc(inlen);
129 if (enc->enc == NULL)
131 memcpy(enc->enc, in, inlen);
138 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
142 enc = asn1_get_enc_ptr(pval, it);
143 if (!enc || enc->modified)
146 memcpy(*out, enc->enc, enc->len);
154 /* Given an ASN1_TEMPLATE get a pointer to a field */
155 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
157 ASN1_VALUE **pvaltmp;
158 pvaltmp = offset2ptr(*pval, tt->offset);
160 * NOTE for BOOLEAN types the field is just a plain int so we can't
161 * return int **, so settle for (int *).
167 * Handle ANY DEFINED BY template, find the selector, look up the relevant
168 * ASN1_TEMPLATE in the table and return it.
171 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
175 const ASN1_ADB_TABLE *atbl;
179 if (!(tt->flags & ASN1_TFLG_ADB_MASK))
182 /* Else ANY DEFINED BY ... get the table */
183 adb = ASN1_ADB_ptr(tt->item);
185 /* Get the selector field */
186 sfld = offset2ptr(*pval, adb->offset);
196 * Convert type to a long: NB: don't check for NID_undef here because it
197 * might be a legitimate value in the table
199 if (tt->flags & ASN1_TFLG_ADB_OID)
200 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
202 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
204 /* Let application callback translate value */
205 if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
206 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
211 * Try to find matching entry in table Maybe should check application
212 * types first to allow application override? Might also be useful to
213 * have a flag which indicates table is sorted and we can do a binary
214 * search. For now stick to a linear search.
217 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
218 if (atbl->value == selector)
221 /* FIXME: need to search application table too */
223 /* No match, return default type */
224 if (!adb->default_tt)
226 return adb->default_tt;
229 /* FIXME: should log the value or OID of unsupported type */
231 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);