2 * Copyright 2000-2021 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 ossl_asn1_get_choice_selector(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
31 int *sel
= offset2ptr(*pval
, it
->utype
);
36 int ossl_asn1_get_choice_selector_const(const ASN1_VALUE
**pval
,
39 int *sel
= offset2ptr(*pval
, it
->utype
);
45 * Given an ASN1_ITEM CHOICE type set the selector value, return old value.
48 int ossl_asn1_set_choice_selector(ASN1_VALUE
**pval
, int value
,
53 sel
= offset2ptr(*pval
, it
->utype
);
60 * Do atomic reference counting. The value 'op' decides what to do.
61 * If it is +1 then the count is incremented.
62 * If |op| is 0, lock is initialised and count is set to 1.
63 * If |op| is -1, count is decremented and the return value is the current
64 * reference count or 0 if no reference count is active.
65 * It returns -1 on initialisation error.
66 * Used by ASN1_SEQUENCE construct of X509, X509_REQ, X509_CRL objects
68 int ossl_asn1_do_lock(ASN1_VALUE
**pval
, int op
, const ASN1_ITEM
*it
)
71 CRYPTO_REF_COUNT
*lck
;
75 if ((it
->itype
!= ASN1_ITYPE_SEQUENCE
)
76 && (it
->itype
!= ASN1_ITYPE_NDEF_SEQUENCE
))
79 if (aux
== NULL
|| (aux
->flags
& ASN1_AFLG_REFCOUNT
) == 0)
81 lck
= offset2ptr(*pval
, aux
->ref_offset
);
82 lock
= offset2ptr(*pval
, aux
->ref_lock
);
87 *lock
= CRYPTO_THREAD_lock_new();
89 ERR_raise(ERR_LIB_ASN1
, ERR_R_MALLOC_FAILURE
);
94 if (!CRYPTO_UP_REF(lck
, &ret
, *lock
))
98 if (!CRYPTO_DOWN_REF(lck
, &ret
, *lock
))
99 return -1; /* failed */
101 fprintf(stderr
, "%p:%4d:%s\n", (void*)it
, ret
, it
->sname
);
103 REF_ASSERT_ISNT(ret
< 0);
105 CRYPTO_THREAD_lock_free(*lock
);
114 static ASN1_ENCODING
*asn1_get_enc_ptr(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
118 if (pval
== NULL
|| *pval
== NULL
)
121 if (aux
== NULL
|| (aux
->flags
& ASN1_AFLG_ENCODING
) == 0)
123 return offset2ptr(*pval
, aux
->enc_offset
);
126 static const ASN1_ENCODING
*asn1_get_const_enc_ptr(const ASN1_VALUE
**pval
,
131 if (pval
== NULL
|| *pval
== NULL
)
134 if (aux
== NULL
|| (aux
->flags
& ASN1_AFLG_ENCODING
) == 0)
136 return offset2ptr(*pval
, aux
->enc_offset
);
139 void ossl_asn1_enc_init(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
141 ASN1_ENCODING
*enc
= asn1_get_enc_ptr(pval
, it
);
150 void ossl_asn1_enc_free(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
)
152 ASN1_ENCODING
*enc
= asn1_get_enc_ptr(pval
, it
);
155 OPENSSL_free(enc
->enc
);
162 int ossl_asn1_enc_save(ASN1_VALUE
**pval
, const unsigned char *in
, int inlen
,
165 ASN1_ENCODING
*enc
= asn1_get_enc_ptr(pval
, it
);
170 OPENSSL_free(enc
->enc
);
171 if ((enc
->enc
= OPENSSL_malloc(inlen
)) == NULL
) {
172 ERR_raise(ERR_LIB_ASN1
, ERR_R_MALLOC_FAILURE
);
175 memcpy(enc
->enc
, in
, inlen
);
182 int ossl_asn1_enc_restore(int *len
, unsigned char **out
, const ASN1_VALUE
**pval
,
185 const ASN1_ENCODING
*enc
= asn1_get_const_enc_ptr(pval
, it
);
187 if (enc
== NULL
|| enc
->modified
)
190 memcpy(*out
, enc
->enc
, enc
->len
);
198 /* Given an ASN1_TEMPLATE get a pointer to a field */
199 ASN1_VALUE
**ossl_asn1_get_field_ptr(ASN1_VALUE
**pval
, const ASN1_TEMPLATE
*tt
)
201 ASN1_VALUE
**pvaltmp
= offset2ptr(*pval
, tt
->offset
);
204 * NOTE for BOOLEAN types the field is just a plain int so we can't
205 * return int **, so settle for (int *).
210 /* Given an ASN1_TEMPLATE get a const pointer to a field */
211 const ASN1_VALUE
**ossl_asn1_get_const_field_ptr(const ASN1_VALUE
**pval
,
212 const ASN1_TEMPLATE
*tt
)
214 return offset2ptr(*pval
, tt
->offset
);
218 * Handle ANY DEFINED BY template, find the selector, look up the relevant
219 * ASN1_TEMPLATE in the table and return it.
222 const ASN1_TEMPLATE
*ossl_asn1_do_adb(const ASN1_VALUE
*val
,
223 const ASN1_TEMPLATE
*tt
,
227 const ASN1_ADB_TABLE
*atbl
;
229 const ASN1_VALUE
**sfld
;
232 if ((tt
->flags
& ASN1_TFLG_ADB_MASK
) == 0)
235 /* Else ANY DEFINED BY ... get the table */
236 adb
= ASN1_ADB_ptr(tt
->item
);
238 /* Get the selector field */
239 sfld
= offset2ptr(val
, adb
->offset
);
243 if (adb
->null_tt
== NULL
)
249 * Convert type to a long: NB: don't check for NID_undef here because it
250 * might be a legitimate value in the table
252 if ((tt
->flags
& ASN1_TFLG_ADB_OID
) != 0)
253 selector
= OBJ_obj2nid((ASN1_OBJECT
*)*sfld
);
255 selector
= ASN1_INTEGER_get((ASN1_INTEGER
*)*sfld
);
257 /* Let application callback translate value */
258 if (adb
->adb_cb
!= NULL
&& adb
->adb_cb(&selector
) == 0) {
259 ERR_raise(ERR_LIB_ASN1
, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE
);
264 * Try to find matching entry in table Maybe should check application
265 * types first to allow application override? Might also be useful to
266 * have a flag which indicates table is sorted and we can do a binary
267 * search. For now stick to a linear search.
270 for (atbl
= adb
->tbl
, i
= 0; i
< adb
->tblcount
; i
++, atbl
++)
271 if (atbl
->value
== selector
)
274 /* FIXME: need to search application table too */
276 /* No match, return default type */
277 if (!adb
->default_tt
)
279 return adb
->default_tt
;
282 /* FIXME: should log the value or OID of unsupported type */
284 ERR_raise(ERR_LIB_ASN1
, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE
);