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 atomic reference counting. The value 'op' decides what to do.
50 * If it is +1 then the count is incremented.
51 * If |op| is 0, lock is initialised and count is set to 1.
52 * If |op| is -1, count is decremented and the return value is the current
53 * reference count or 0 if no reference count is active.
54 * It returns -1 on initialisation error.
55 * Used by ASN1_SEQUENCE construct of X509, X509_REQ, X509_CRL objects
57 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
62 if ((it->itype != ASN1_ITYPE_SEQUENCE)
63 && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
66 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
68 lck = offset2ptr(*pval, aux->ref_offset);
69 lock = offset2ptr(*pval, aux->ref_lock);
72 *lock = CRYPTO_THREAD_lock_new();
74 ASN1err(ASN1_F_ASN1_DO_LOCK, ERR_R_MALLOC_FAILURE);
79 if (CRYPTO_atomic_add(lck, op, &ret, *lock) < 0)
80 return -1; /* failed */
82 fprintf(stderr, "%p:%4d:%s\n", it, *lck, it->sname);
84 REF_ASSERT_ISNT(ret < 0);
86 CRYPTO_THREAD_lock_free(*lock);
92 static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
98 if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
100 return offset2ptr(*pval, aux->enc_offset);
103 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
106 enc = asn1_get_enc_ptr(pval, it);
114 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
117 enc = asn1_get_enc_ptr(pval, it);
119 OPENSSL_free(enc->enc);
126 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
130 enc = asn1_get_enc_ptr(pval, it);
134 OPENSSL_free(enc->enc);
135 enc->enc = OPENSSL_malloc(inlen);
136 if (enc->enc == NULL)
138 memcpy(enc->enc, in, inlen);
145 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
149 enc = asn1_get_enc_ptr(pval, it);
150 if (!enc || enc->modified)
153 memcpy(*out, enc->enc, enc->len);
161 /* Given an ASN1_TEMPLATE get a pointer to a field */
162 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
164 ASN1_VALUE **pvaltmp;
165 pvaltmp = offset2ptr(*pval, tt->offset);
167 * NOTE for BOOLEAN types the field is just a plain int so we can't
168 * return int **, so settle for (int *).
174 * Handle ANY DEFINED BY template, find the selector, look up the relevant
175 * ASN1_TEMPLATE in the table and return it.
178 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
182 const ASN1_ADB_TABLE *atbl;
186 if (!(tt->flags & ASN1_TFLG_ADB_MASK))
189 /* Else ANY DEFINED BY ... get the table */
190 adb = ASN1_ADB_ptr(tt->item);
192 /* Get the selector field */
193 sfld = offset2ptr(*pval, adb->offset);
203 * Convert type to a long: NB: don't check for NID_undef here because it
204 * might be a legitimate value in the table
206 if (tt->flags & ASN1_TFLG_ADB_OID)
207 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
209 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
211 /* Let application callback translate value */
212 if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
213 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
218 * Try to find matching entry in table Maybe should check application
219 * types first to allow application override? Might also be useful to
220 * have a flag which indicates table is sorted and we can do a binary
221 * search. For now stick to a linear search.
224 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
225 if (atbl->value == selector)
228 /* FIXME: need to search application table too */
230 /* No match, return default type */
231 if (!adb->default_tt)
233 return adb->default_tt;
236 /* FIXME: should log the value or OID of unsupported type */
238 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);