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.
53 * FIXME: return and manage any error from inside this method
56 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
61 if ((it->itype != ASN1_ITYPE_SEQUENCE)
62 && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
65 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
67 lck = offset2ptr(*pval, aux->ref_offset);
68 lock = offset2ptr(*pval, aux->ref_lock);
71 *lock = CRYPTO_THREAD_lock_new();
73 /* FIXME: should report an error (-1) at this point */
78 CRYPTO_atomic_add(lck, op, &ret, *lock);
80 fprintf(stderr, "%p:%4d:%s\n", it, *lck, it->sname);
82 REF_ASSERT_ISNT(ret < 0);
84 CRYPTO_THREAD_lock_free(*lock);
88 static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
94 if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
96 return offset2ptr(*pval, aux->enc_offset);
99 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
102 enc = asn1_get_enc_ptr(pval, it);
110 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
113 enc = asn1_get_enc_ptr(pval, it);
115 OPENSSL_free(enc->enc);
122 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
126 enc = asn1_get_enc_ptr(pval, it);
130 OPENSSL_free(enc->enc);
131 enc->enc = OPENSSL_malloc(inlen);
132 if (enc->enc == NULL)
134 memcpy(enc->enc, in, inlen);
141 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
145 enc = asn1_get_enc_ptr(pval, it);
146 if (!enc || enc->modified)
149 memcpy(*out, enc->enc, enc->len);
157 /* Given an ASN1_TEMPLATE get a pointer to a field */
158 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
160 ASN1_VALUE **pvaltmp;
161 pvaltmp = offset2ptr(*pval, tt->offset);
163 * NOTE for BOOLEAN types the field is just a plain int so we can't
164 * return int **, so settle for (int *).
170 * Handle ANY DEFINED BY template, find the selector, look up the relevant
171 * ASN1_TEMPLATE in the table and return it.
174 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
178 const ASN1_ADB_TABLE *atbl;
182 if (!(tt->flags & ASN1_TFLG_ADB_MASK))
185 /* Else ANY DEFINED BY ... get the table */
186 adb = ASN1_ADB_ptr(tt->item);
188 /* Get the selector field */
189 sfld = offset2ptr(*pval, adb->offset);
199 * Convert type to a long: NB: don't check for NID_undef here because it
200 * might be a legitimate value in the table
202 if (tt->flags & ASN1_TFLG_ADB_OID)
203 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
205 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
207 /* Let application callback translate value */
208 if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
209 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
214 * Try to find matching entry in table Maybe should check application
215 * types first to allow application override? Might also be useful to
216 * have a flag which indicates table is sorted and we can do a binary
217 * search. For now stick to a linear search.
220 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
221 if (atbl->value == selector)
224 /* FIXME: need to search application table too */
226 /* No match, return default type */
227 if (!adb->default_tt)
229 return adb->default_tt;
232 /* FIXME: should log the value or OID of unsupported type */
234 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);