2 * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 "internal/refcount.h"
14 #include <openssl/asn1.h>
15 #include <openssl/asn1t.h>
16 #include <openssl/objects.h>
17 #include <openssl/err.h>
18 #include "asn1_local.h"
20 /* Utility functions for manipulating fields and offsets */
22 /* Add 'offset' to 'addr' */
23 #define offset2ptr(addr, offset) (void *)(((char *) addr) + offset)
26 * Given an ASN1_ITEM CHOICE type return the selector value
29 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
31 int *sel = offset2ptr(*pval, it->utype);
36 int asn1_get_choice_selector_const(const ASN1_VALUE **pval, const ASN1_ITEM *it)
38 int *sel = offset2ptr(*pval, it->utype);
44 * Given an ASN1_ITEM CHOICE type set the selector value, return old value.
47 int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
52 sel = offset2ptr(*pval, it->utype);
59 * Do atomic reference counting. The value 'op' decides what to do.
60 * If it is +1 then the count is incremented.
61 * If |op| is 0, lock is initialised and count is set to 1.
62 * If |op| is -1, count is decremented and the return value is the current
63 * reference count or 0 if no reference count is active.
64 * It returns -1 on initialisation error.
65 * Used by ASN1_SEQUENCE construct of X509, X509_REQ, X509_CRL objects
67 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
70 CRYPTO_REF_COUNT *lck;
74 if ((it->itype != ASN1_ITYPE_SEQUENCE)
75 && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
78 if (aux == NULL || (aux->flags & ASN1_AFLG_REFCOUNT) == 0)
80 lck = offset2ptr(*pval, aux->ref_offset);
81 lock = offset2ptr(*pval, aux->ref_lock);
86 *lock = CRYPTO_THREAD_lock_new();
88 ASN1err(ASN1_F_ASN1_DO_LOCK, ERR_R_MALLOC_FAILURE);
93 if (!CRYPTO_UP_REF(lck, &ret, *lock))
97 if (!CRYPTO_DOWN_REF(lck, &ret, *lock))
98 return -1; /* failed */
100 fprintf(stderr, "%p:%4d:%s\n", it, ret, it->sname);
102 REF_ASSERT_ISNT(ret < 0);
104 CRYPTO_THREAD_lock_free(*lock);
113 static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
117 if (pval == NULL || *pval == NULL)
120 if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
122 return offset2ptr(*pval, aux->enc_offset);
125 static const ASN1_ENCODING *asn1_get_const_enc_ptr(const ASN1_VALUE **pval,
130 if (pval == NULL || *pval == NULL)
133 if (aux == NULL || (aux->flags & ASN1_AFLG_ENCODING) == 0)
135 return offset2ptr(*pval, aux->enc_offset);
138 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
140 ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
149 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
151 ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
154 OPENSSL_free(enc->enc);
161 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
164 ASN1_ENCODING *enc = asn1_get_enc_ptr(pval, it);
169 OPENSSL_free(enc->enc);
170 if ((enc->enc = OPENSSL_malloc(inlen)) == NULL) {
171 ASN1err(ASN1_F_ASN1_ENC_SAVE, ERR_R_MALLOC_FAILURE);
174 memcpy(enc->enc, in, inlen);
181 int asn1_enc_restore(int *len, unsigned char **out, const ASN1_VALUE **pval,
184 const ASN1_ENCODING *enc = asn1_get_const_enc_ptr(pval, it);
186 if (enc == NULL || enc->modified)
189 memcpy(*out, enc->enc, enc->len);
197 /* Given an ASN1_TEMPLATE get a pointer to a field */
198 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
200 ASN1_VALUE **pvaltmp = offset2ptr(*pval, tt->offset);
203 * NOTE for BOOLEAN types the field is just a plain int so we can't
204 * return int **, so settle for (int *).
209 /* Given an ASN1_TEMPLATE get a const pointer to a field */
210 const ASN1_VALUE **asn1_get_const_field_ptr(const ASN1_VALUE **pval,
211 const ASN1_TEMPLATE *tt)
213 return offset2ptr(*pval, tt->offset);
217 * Handle ANY DEFINED BY template, find the selector, look up the relevant
218 * ASN1_TEMPLATE in the table and return it.
221 const ASN1_TEMPLATE *asn1_do_adb(const ASN1_VALUE *val,
222 const ASN1_TEMPLATE *tt,
226 const ASN1_ADB_TABLE *atbl;
228 const ASN1_VALUE **sfld;
231 if ((tt->flags & ASN1_TFLG_ADB_MASK) == 0)
234 /* Else ANY DEFINED BY ... get the table */
235 adb = ASN1_ADB_ptr(tt->item);
237 /* Get the selector field */
238 sfld = offset2ptr(val, adb->offset);
242 if (adb->null_tt == NULL)
248 * Convert type to a long: NB: don't check for NID_undef here because it
249 * might be a legitimate value in the table
251 if ((tt->flags & ASN1_TFLG_ADB_OID) != 0)
252 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
254 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
256 /* Let application callback translate value */
257 if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
258 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
263 * Try to find matching entry in table Maybe should check application
264 * types first to allow application override? Might also be useful to
265 * have a flag which indicates table is sorted and we can do a binary
266 * search. For now stick to a linear search.
269 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
270 if (atbl->value == selector)
273 /* FIXME: need to search application table too */
275 /* No match, return default type */
276 if (!adb->default_tt)
278 return adb->default_tt;
281 /* FIXME: should log the value or OID of unsupported type */
283 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);