1/* 2 * Copyright (c) 1997 - 2006 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * 3. Neither the name of the Institute nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36/* asn1 templates */ 37 38#ifndef __TEMPLATE_H__ 39#define __TEMPLATE_H__ 40 41#ifndef HEIMDAL_PRINTF_ATTRIBUTE 42#if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 ))) 43#define HEIMDAL_PRINTF_ATTRIBUTE(x) __attribute__((format x)) 44#else 45#define HEIMDAL_PRINTF_ATTRIBUTE(x) 46#endif 47#endif 48 49/* tag: 50 * 0..20 tag 51 * 21 type 52 * 22..23 class 53 * 24..27 flags 54 * 28..31 op 55 */ 56 57/* parse: 58 * 0..11 type 59 * 12..23 unused 60 * 24..27 flags 61 * 28..31 op 62 */ 63 64#define A1_OP_MASK (0xf0000000) 65#define A1_OP_TYPE (0x10000000) 66#define A1_OP_TYPE_EXTERN (0x20000000) 67#define A1_OP_TAG (0x30000000) 68#define A1_OP_PARSE (0x40000000) 69#define A1_OP_SEQOF (0x50000000) 70#define A1_OP_SETOF (0x60000000) 71#define A1_OP_BMEMBER (0x70000000) 72#define A1_OP_CHOICE (0x80000000) 73 74#define A1_FLAG_MASK (0x0f000000) 75#define A1_FLAG_OPTIONAL (0x01000000) 76#define A1_FLAG_IMPLICIT (0x02000000) 77 78#define A1_TAG_T(CLASS,TYPE,TAG) ((A1_OP_TAG) | (((CLASS) << 22) | ((TYPE) << 21) | (TAG))) 79#define A1_TAG_CLASS(x) (((x) >> 22) & 0x3) 80#define A1_TAG_TYPE(x) (((x) >> 21) & 0x1) 81#define A1_TAG_TAG(x) ((x) & 0x1fffff) 82 83#define A1_TAG_LEN(t) ((uintptr_t)(t)->ptr) 84#define A1_HEADER_LEN(t) ((uintptr_t)(t)->ptr) 85 86#define A1_PARSE_T(type) ((A1_OP_PARSE) | (type)) 87#define A1_PARSE_TYPE_MASK 0xfff 88#define A1_PARSE_TYPE(x) (A1_PARSE_TYPE_MASK & (x)) 89 90#define A1_PF_INDEFINTE 0x1 91#define A1_PF_ALLOW_BER 0x2 92 93#define A1_HF_PRESERVE 0x1 94#define A1_HF_ELLIPSIS 0x2 95 96#define A1_HBF_RFC1510 0x1 97 98 99struct asn1_template { 100 uint32_t tt; 101 uint32_t offset; 102 const void *ptr; 103}; 104 105typedef int (*asn1_type_decode)(const unsigned char *, size_t, void *, size_t *); 106typedef int (*asn1_type_encode)(unsigned char *, size_t, const void *, size_t *); 107typedef size_t (*asn1_type_length)(const void *); 108typedef void (*asn1_type_release)(void *); 109typedef int (*asn1_type_copy)(const void *, void *); 110 111struct asn1_type_func { 112 asn1_type_encode encode; 113 asn1_type_decode decode; 114 asn1_type_length length; 115 asn1_type_copy copy; 116 asn1_type_release release; 117 size_t size; 118}; 119 120struct template_of { 121 unsigned int len; 122 void *val; 123}; 124 125enum template_types { 126 A1T_IMEMBER = 0, 127 A1T_HEIM_INTEGER, 128 A1T_INTEGER, 129 A1T_UNSIGNED, 130 A1T_GENERAL_STRING, 131 A1T_OCTET_STRING, 132 A1T_OCTET_STRING_BER, 133 A1T_IA5_STRING, 134 A1T_BMP_STRING, 135 A1T_UNIVERSAL_STRING, 136 A1T_PRINTABLE_STRING, 137 A1T_VISIBLE_STRING, 138 A1T_UTF8_STRING, 139 A1T_GENERALIZED_TIME, 140 A1T_UTC_TIME, 141 A1T_HEIM_BIT_STRING, 142 A1T_BOOLEAN, 143 A1T_OID, 144 A1T_TELETEX_STRING, 145 A1T_NUM_ENTRY 146}; 147 148extern struct asn1_type_func asn1_template_prim[A1T_NUM_ENTRY]; 149 150#define ABORT_ON_ERROR(...) asn1_abort(__VA_ARGS__) 151 152#define DPOC(data,offset) ((const void *)(((const unsigned char *)data) + offset)) 153#define DPO(data,offset) ((void *)(((unsigned char *)data) + offset)) 154 155/* 156 * These functions are needed by the generated template stubs and are 157 * really internal functions. Since they are part of der-private.h 158 * that contains extra prototypes that really a private we included a 159 * copy here. 160 */ 161 162int 163_asn1_copy_top ( 164 const struct asn1_template */*t*/, 165 const void */*from*/, 166 void */*to*/); 167 168void 169_asn1_free_top(const struct asn1_template *t, 170 void *data); 171 172void 173_asn1_capture_data(const char *type, const unsigned char *p, size_t len); 174 175int 176_asn1_decode_top ( 177 const struct asn1_template */*t*/, 178 unsigned /*flags*/, 179 const unsigned char */*p*/, 180 size_t /*len*/, 181 void */*data*/, 182 size_t */*size*/); 183 184int 185_asn1_encode ( 186 const struct asn1_template */*t*/, 187 unsigned char */*p*/, 188 size_t /*len*/, 189 const void */*data*/, 190 size_t */*size*/); 191 192int 193_asn1_encode_fuzzer ( 194 const struct asn1_template */*t*/, 195 unsigned char */*p*/, 196 size_t /*len*/, 197 const void */*data*/, 198 size_t */*size*/); 199 200void 201_asn1_free ( 202 const struct asn1_template */*t*/, 203 void */*data*/); 204 205size_t 206_asn1_length ( 207 const struct asn1_template */*t*/, 208 const void */*data*/); 209 210size_t 211_asn1_length_fuzzer ( 212 const struct asn1_template */*t*/, 213 const void */*data*/); 214 215 216#endif 217