2 * Copyright 2000-2017 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)
63 if ((it->itype != ASN1_ITYPE_SEQUENCE)
64 && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
67 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
69 lck = offset2ptr(*pval, aux->ref_offset);
70 lock = offset2ptr(*pval, aux->ref_lock);
73 *lock = CRYPTO_THREAD_lock_new();
75 ASN1err(ASN1_F_ASN1_DO_LOCK, ERR_R_MALLOC_FAILURE);
80 if (CRYPTO_atomic_add(lck, op, &ret, *lock) < 0)
81 return -1; /* failed */
83 fprintf(stderr, "%p:%4d:%s\n", it, *lck, it->sname);
85 REF_ASSERT_ISNT(ret < 0);
87 CRYPTO_THREAD_lock_free(*lock);
93 static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
99 if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
101 return offset2ptr(*pval, aux->enc_offset);
104 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
107 enc = asn1_get_enc_ptr(pval, it);
115 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
118 enc = asn1_get_enc_ptr(pval, it);
120 OPENSSL_free(enc->enc);
127 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
131 enc = asn1_get_enc_ptr(pval, it);
135 OPENSSL_free(enc->enc);
136 if ((enc->enc = OPENSSL_malloc(inlen)) == NULL) {
137 ASN1err(ASN1_F_ASN1_ENC_SAVE, ERR_R_MALLOC_FAILURE);
140 memcpy(enc->enc, in, inlen);
147 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
151 enc = asn1_get_enc_ptr(pval, it);
152 if (!enc || enc->modified)
155 memcpy(*out, enc->enc, enc->len);
163 /* Given an ASN1_TEMPLATE get a pointer to a field */
164 ASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
166 ASN1_VALUE **pvaltmp;
167 pvaltmp = offset2ptr(*pval, tt->offset);
169 * NOTE for BOOLEAN types the field is just a plain int so we can't
170 * return int **, so settle for (int *).
176 * Handle ANY DEFINED BY template, find the selector, look up the relevant
177 * ASN1_TEMPLATE in the table and return it.
180 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
184 const ASN1_ADB_TABLE *atbl;
188 if (!(tt->flags & ASN1_TFLG_ADB_MASK))
191 /* Else ANY DEFINED BY ... get the table */
192 adb = ASN1_ADB_ptr(tt->item);
194 /* Get the selector field */
195 sfld = offset2ptr(*pval, adb->offset);
205 * Convert type to a long: NB: don't check for NID_undef here because it
206 * might be a legitimate value in the table
208 if (tt->flags & ASN1_TFLG_ADB_OID)
209 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
211 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
213 /* Let application callback translate value */
214 if (adb->adb_cb != NULL && adb->adb_cb(&selector) == 0) {
215 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
220 * Try to find matching entry in table Maybe should check application
221 * types first to allow application override? Might also be useful to
222 * have a flag which indicates table is sorted and we can do a binary
223 * search. For now stick to a linear search.
226 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
227 if (atbl->value == selector)
230 /* FIXME: need to search application table too */
232 /* No match, return default type */
233 if (!adb->default_tt)
235 return adb->default_tt;
238 /* FIXME: should log the value or OID of unsupported type */
240 ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);