2 * Copyright 2000-2018 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 "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_locl.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 * Given an ASN1_ITEM CHOICE type set the selector value, return old value.
39 int asn1_set_choice_selector(ASN1_VALUE **pval, int value,
43 sel = offset2ptr(*pval, it->utype);
50 * Do atomic reference counting. The value 'op' decides what to do.
51 * If it is +1 then the count is incremented.
52 * If |op| is 0, lock is initialised and count is set to 1.
53 * If |op| is -1, count is decremented and the return value is the current
54 * reference count or 0 if no reference count is active.
55 * It returns -1 on initialisation error.
56 * Used by ASN1_SEQUENCE construct of X509, X509_REQ, X509_CRL objects
58 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
61 CRYPTO_REF_COUNT *lck;
65 if ((it->itype != ASN1_ITYPE_SEQUENCE)
66 && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
69 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
71 lck = offset2ptr(*pval, aux->ref_offset);
72 lock = offset2ptr(*pval, aux->ref_lock);
77 *lock = CRYPTO_THREAD_lock_new();
79 ASN1err(ASN1_F_ASN1_DO_LOCK, ERR_R_MALLOC_FAILURE);
84 if (!CRYPTO_UP_REF(lck, &ret, *lock))
88 if (!CRYPTO_DOWN_REF(lck, &ret, *lock))
89 return -1; /* failed */
91 fprintf(stderr, "%p:%4d:%s\n", it, ret, it->sname);
93 REF_ASSERT_ISNT(ret < 0);
95 CRYPTO_THREAD_lock_free(*lock);
104 static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
110 if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
112 return offset2ptr(*pval, aux->enc_offset);
115 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
118 enc = asn1_get_enc_ptr(pval, it);
126 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
129 enc = asn1_get_enc_ptr(pval, it);
131 OPENSSL_free(enc->enc);
138 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
142 enc = asn1_get_enc_ptr(pval, it);
146 OPENSSL_free(enc->enc);
147 if ((enc->enc = OPENSSL_malloc(inlen)) == NULL) {
148 ASN1err(ASN1_F_ASN1_ENC_SAVE, ERR_R_MALLOC_FAILURE);
151 memcpy(enc->enc, in, inlen);
158 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
162 enc = asn1_get_enc_ptr(pval, it);
163 if (!enc || enc->modified)
166 memcpy(*out, enc->enc, enc->len);
174 /* Given an ASN1_TEMPLATE get a pointer to a field */
175 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
177 ASN1_VALUE **pvaltmp;
178 pvaltmp = offset2ptr(*pval, tt->offset);
180 * NOTE for BOOLEAN types the field is just a plain int so we can't
181 * return int **, so settle for (int *).
187 * Handle ANY DEFINED BY template, find the selector, look up the relevant
188 * ASN1_TEMPLATE in the table and return it.
191 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
195 const ASN1_ADB_TABLE *atbl;
199 if (!(tt->flags & ASN1_TFLG_ADB_MASK))
202 /* Else ANY DEFINED BY ... get the table */
203 adb = ASN1_ADB_ptr(tt->item);
205 /* Get the selector field */
206 sfld = offset2ptr(*pval, adb->offset);
216 * Convert type to a long: NB: don't check for NID_undef here because it
217 * might be a legitimate value in the table
219 if (tt->flags & ASN1_TFLG_ADB_OID)
220 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
222 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
224 /* Let application callback translate value */
225 if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
226 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
231 * Try to find matching entry in table Maybe should check application
232 * types first to allow application override? Might also be useful to
233 * have a flag which indicates table is sorted and we can do a binary
234 * search. For now stick to a linear search.
237 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
238 if (atbl->value == selector)
241 /* FIXME: need to search application table too */
243 /* No match, return default type */
244 if (!adb->default_tt)
246 return adb->default_tt;
249 /* FIXME: should log the value or OID of unsupported type */
251 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);