/* * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). * All rights reserved. * * Portions Copyright (c) 2009 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * 3. Neither the name of the Institute nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* asn1 templates */ #ifndef __TEMPLATE_H__ #define __TEMPLATE_H__ /* * TBD: * * - For OER also encode number of optional/default/extension elements into * header entry's ptr field, not just the number of entries that follow it * * - For JER/GSER/whatver, and probably for not-C-coded template interpreters, * we'll need to have an entry type for the names of structures and their * fields. * * - For auto open types we need a new opcode, let's call it * A1_OP_OPENTYPE_OBJSET, and we need to encode into its entry: * a) the index of the template entry for the type ID field, and * b) the index of the template entry for the open type field, * c) 1 bit to indicate whether the object set is sorted by type ID value, * d) a pointer to the object set's template. * With that we can then find the struct offsets of those, and also their * types (since we can find their template entries). * The object set entries should be encoded into two template entries each: * one pointing to the value of the type ID field for that object (unless * the value is an integer, in which case the ptr should be the integer * value directly), and the other pointing to the template for the type * identified by the type ID. These will need an opcode each... * A1_OP_OPENTYPE_ID and A1_OP_OPENTYPE. * We should also end the object set with an A1_OP_OPENTYPE_OBJSET entry so * that iterating backwards can be fast. Unless... unless we don't inline * the object set and its objects but point to the object set's template. * Also, for extensible object sets we can point to the object set's name, * and we can then have a function to get an object set template by name, * one to release that, and one to add an object to the object set (there's * no need to remove objects from object sets, which helps with thread- * safety). And then we don't need (c) either. * The decoder will then not see these entries until after decoding the type * ID and open type field (as its outer type, so OCTET STRING, BIT STRING, * or HEIM_ANY) and so it will be able to find those values in the struct at * their respective offsets. * The encoder and decoder both need to identify the relevant object in the * object set, either by linear search or binary search if they are sorted * by type ID value, then interpret the template for the identified type. * The encoder needs to place the encoding into the normal location for it * in the struct, then it can execute the normal template entry for it. */ /* header: * HF flags if not a BIT STRING type * HBF flags if a BIT STRING type * * ptr is count of elements * offset is size of struct */ /* tag: * 0..20 tag * 21 type * 22..23 class * 24..27 flags * 28..31 op * * ptr points to template for tagged type * offset is offset of struct field */ /* parse: * 0..11 type * 12..23 unused * 24..27 flags * 28..31 op * * ptr is NULL * offset is ... */ /* defval: (next template entry is defaulted) * * DV flags (ptr is or points to defval) * * ptr is default value or pointer to default value * offset is all ones */ /* name: first one is the name of the SET/SEQUENCE/CHOICE type * subsequent ones are the name of the nth field * * 0..23 unused * 24..27 flags A1_NM_* * 28..31 op * * ptr is const char * pointer to the name as C string * offset is all zeros */ /* objset: * 0..9 open type ID entry index * 10..19 open type entry index * 20..23 unused * 24..27 flags A1_OS_* * 28..31 op * * ptr points to object set template * offset is the offset of the choice struct */ /* opentypeid: offset is zero * ptr points to value if it is not an integer * ptr is the value if it is an integer * 0..23 unused * 24..27 flags A1_OTI_* * 28..31 op */ /* opentype: offset is sizeof C type for this open type choice * ptr points to template for type choice * 0..23 unused * 24..27 flags * 28..31 op */ #define A1_OP_MASK (0xf0000000) #define A1_OP_TYPE (0x10000000) #define A1_OP_TYPE_EXTERN (0x20000000) #define A1_OP_TAG (0x30000000) #define A1_OP_PARSE (0x40000000) #define A1_OP_SEQOF (0x50000000) #define A1_OP_SETOF (0x60000000) #define A1_OP_BMEMBER (0x70000000) #define A1_OP_CHOICE (0x80000000) #define A1_OP_DEFVAL (0x90000000) #define A1_OP_OPENTYPE_OBJSET (0xa0000000) #define A1_OP_OPENTYPE_ID (0xb0000000) #define A1_OP_OPENTYPE (0xc0000000) #define A1_OP_NAME (0xd0000000) #define A1_FLAG_MASK (0x0f000000) #define A1_FLAG_OPTIONAL (0x01000000) #define A1_FLAG_IMPLICIT (0x02000000) #define A1_FLAG_DEFAULT (0x04000000) #define A1_TAG_T(CLASS,TYPE,TAG) ((A1_OP_TAG) | (((CLASS) << 22) | ((TYPE) << 21) | (TAG))) #define A1_TAG_CLASS(x) (((x) >> 22) & 0x3) #define A1_TAG_TYPE(x) (((x) >> 21) & 0x1) #define A1_TAG_TAG(x) ((x) & 0x1fffff) #define A1_TAG_LEN(t) ((uintptr_t)(t)->ptr) #define A1_HEADER_LEN(t) ((uintptr_t)(t)->ptr) #define A1_PARSE_T(type) ((A1_OP_PARSE) | (type)) #define A1_PARSE_TYPE_MASK 0xfff #define A1_PARSE_TYPE(x) (A1_PARSE_TYPE_MASK & (x)) #define A1_PF_INDEFINTE 0x1 #define A1_PF_ALLOW_BER 0x2 #define A1_HF_PRESERVE 0x1 #define A1_HF_ELLIPSIS 0x2 #define A1_HBF_RFC1510 0x1 #define A1_DV_BOOLEAN 0x01 #define A1_DV_INTEGER 0x02 #define A1_DV_INTEGER32 0x04 #define A1_DV_INTEGER64 0x08 #define A1_DV_UTF8STRING 0x10 #define A1_OS_IS_SORTED (0x01000000) #define A1_OS_OT_IS_ARRAY (0x02000000) #define A1_OTI_IS_INTEGER (0x04000000) struct asn1_template { uint32_t tt; uint32_t offset; const void *ptr; }; typedef int (ASN1CALL *asn1_type_decode)(const unsigned char *, size_t, void *, size_t *); typedef int (ASN1CALL *asn1_type_encode)(unsigned char *, size_t, const void *, size_t *); typedef size_t (ASN1CALL *asn1_type_length)(const void *); typedef void (ASN1CALL *asn1_type_release)(void *); typedef int (ASN1CALL *asn1_type_copy)(const void *, void *); typedef char * (ASN1CALL *asn1_type_print)(const void *, int); struct asn1_type_func { asn1_type_encode encode; asn1_type_decode decode; asn1_type_length length; asn1_type_copy copy; asn1_type_release release; asn1_type_print print; size_t size; }; struct template_of { unsigned int len; void *val; }; enum template_types { A1T_IMEMBER = 0, A1T_HEIM_INTEGER, A1T_INTEGER, A1T_INTEGER64, A1T_UNSIGNED, A1T_UNSIGNED64, A1T_GENERAL_STRING, A1T_OCTET_STRING, A1T_OCTET_STRING_BER, A1T_IA5_STRING, A1T_BMP_STRING, A1T_UNIVERSAL_STRING, A1T_PRINTABLE_STRING, A1T_VISIBLE_STRING, A1T_UTF8_STRING, A1T_GENERALIZED_TIME, A1T_UTC_TIME, A1T_HEIM_BIT_STRING, A1T_BOOLEAN, A1T_OID, A1T_TELETEX_STRING, A1T_NUM_ENTRY }; extern struct asn1_type_func asn1_template_prim[A1T_NUM_ENTRY]; #define ABORT_ON_ERROR() abort() #define DPOC(data,offset) ((const void *)(((const unsigned char *)data) + offset)) #define DPO(data,offset) ((void *)(((unsigned char *)data) + offset)) /* * These functions are needed by the generated template stubs and are * really internal functions. Since they are part of der-private.h * that contains extra prototypes that really a private we included a * copy here. */ int _asn1_copy_top ( const struct asn1_template * /*t*/, const void * /*from*/, void * /*to*/); void _asn1_free_top(const struct asn1_template *, void *); char * _asn1_print_top(const struct asn1_template *, int, const void *); int _asn1_decode_top ( const struct asn1_template * /*t*/, unsigned /*flags*/, const unsigned char * /*p*/, size_t /*len*/, void * /*data*/, size_t * /*size*/); int _asn1_encode ( const struct asn1_template * /*t*/, unsigned char * /*p*/, size_t /*len*/, const void * /*data*/, size_t * /*size*/); int _asn1_encode_fuzzer ( const struct asn1_template * /*t*/, unsigned char * /*p*/, size_t /*len*/, const void * /*data*/, size_t * /*size*/); void _asn1_free ( const struct asn1_template * /*t*/, void * /*data*/); size_t _asn1_length ( const struct asn1_template * /*t*/, const void * /*data*/); size_t _asn1_length_fuzzer ( const struct asn1_template * /*t*/, const void * /*data*/); #endif