1109998Smarkm/* asn1t.h */ 2280304Sjkim/* 3280304Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 4280304Sjkim * 2000. 5109998Smarkm */ 6109998Smarkm/* ==================================================================== 7238405Sjkim * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved. 8109998Smarkm * 9109998Smarkm * Redistribution and use in source and binary forms, with or without 10109998Smarkm * modification, are permitted provided that the following conditions 11109998Smarkm * are met: 12109998Smarkm * 13109998Smarkm * 1. Redistributions of source code must retain the above copyright 14280304Sjkim * notice, this list of conditions and the following disclaimer. 15109998Smarkm * 16109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright 17109998Smarkm * notice, this list of conditions and the following disclaimer in 18109998Smarkm * the documentation and/or other materials provided with the 19109998Smarkm * distribution. 20109998Smarkm * 21109998Smarkm * 3. All advertising materials mentioning features or use of this 22109998Smarkm * software must display the following acknowledgment: 23109998Smarkm * "This product includes software developed by the OpenSSL Project 24109998Smarkm * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25109998Smarkm * 26109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27109998Smarkm * endorse or promote products derived from this software without 28109998Smarkm * prior written permission. For written permission, please contact 29109998Smarkm * licensing@OpenSSL.org. 30109998Smarkm * 31109998Smarkm * 5. Products derived from this software may not be called "OpenSSL" 32109998Smarkm * nor may "OpenSSL" appear in their names without prior written 33109998Smarkm * permission of the OpenSSL Project. 34109998Smarkm * 35109998Smarkm * 6. Redistributions of any form whatsoever must retain the following 36109998Smarkm * acknowledgment: 37109998Smarkm * "This product includes software developed by the OpenSSL Project 38109998Smarkm * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39109998Smarkm * 40109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43109998Smarkm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE. 52109998Smarkm * ==================================================================== 53109998Smarkm * 54109998Smarkm * This product includes cryptographic software written by Eric Young 55109998Smarkm * (eay@cryptsoft.com). This product includes software written by Tim 56109998Smarkm * Hudson (tjh@cryptsoft.com). 57109998Smarkm * 58109998Smarkm */ 59109998Smarkm#ifndef HEADER_ASN1T_H 60280304Sjkim# define HEADER_ASN1T_H 61109998Smarkm 62280304Sjkim# include <stddef.h> 63280304Sjkim# include <openssl/e_os2.h> 64280304Sjkim# include <openssl/asn1.h> 65109998Smarkm 66280304Sjkim# ifdef OPENSSL_BUILD_SHLIBCRYPTO 67280304Sjkim# undef OPENSSL_EXTERN 68280304Sjkim# define OPENSSL_EXTERN OPENSSL_EXPORT 69280304Sjkim# endif 70109998Smarkm 71109998Smarkm/* ASN1 template defines, structures and functions */ 72109998Smarkm 73109998Smarkm#ifdef __cplusplus 74109998Smarkmextern "C" { 75109998Smarkm#endif 76109998Smarkm 77280304Sjkim# ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION 78109998Smarkm 79109998Smarkm/* Macro to obtain ASN1_ADB pointer from a type (only used internally) */ 80280304Sjkim# define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr)) 81109998Smarkm 82109998Smarkm/* Macros for start and end of ASN1_ITEM definition */ 83109998Smarkm 84280304Sjkim# define ASN1_ITEM_start(itname) \ 85280304Sjkim OPENSSL_GLOBAL const ASN1_ITEM itname##_it = { 86109998Smarkm 87280304Sjkim# define ASN1_ITEM_end(itname) \ 88280304Sjkim }; 89109998Smarkm 90280304Sjkim# else 91109998Smarkm 92109998Smarkm/* Macro to obtain ASN1_ADB pointer from a type (only used internally) */ 93280304Sjkim# define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr())) 94109998Smarkm 95109998Smarkm/* Macros for start and end of ASN1_ITEM definition */ 96109998Smarkm 97280304Sjkim# define ASN1_ITEM_start(itname) \ 98280304Sjkim const ASN1_ITEM * itname##_it(void) \ 99280304Sjkim { \ 100280304Sjkim static const ASN1_ITEM local_it = { 101109998Smarkm 102280304Sjkim# define ASN1_ITEM_end(itname) \ 103280304Sjkim }; \ 104280304Sjkim return &local_it; \ 105280304Sjkim } 106109998Smarkm 107280304Sjkim# endif 108109998Smarkm 109109998Smarkm/* Macros to aid ASN1 template writing */ 110109998Smarkm 111280304Sjkim# define ASN1_ITEM_TEMPLATE(tname) \ 112280304Sjkim static const ASN1_TEMPLATE tname##_item_tt 113109998Smarkm 114280304Sjkim# define ASN1_ITEM_TEMPLATE_END(tname) \ 115280304Sjkim ;\ 116280304Sjkim ASN1_ITEM_start(tname) \ 117280304Sjkim ASN1_ITYPE_PRIMITIVE,\ 118280304Sjkim -1,\ 119280304Sjkim &tname##_item_tt,\ 120280304Sjkim 0,\ 121280304Sjkim NULL,\ 122280304Sjkim 0,\ 123280304Sjkim #tname \ 124280304Sjkim ASN1_ITEM_end(tname) 125109998Smarkm 126280304Sjkim/* This is a ASN1 type which just embeds a template */ 127109998Smarkm 128280304Sjkim/*- 129280304Sjkim * This pair helps declare a SEQUENCE. We can do: 130109998Smarkm * 131280304Sjkim * ASN1_SEQUENCE(stname) = { 132280304Sjkim * ... SEQUENCE components ... 133280304Sjkim * } ASN1_SEQUENCE_END(stname) 134109998Smarkm * 135280304Sjkim * This will produce an ASN1_ITEM called stname_it 136280304Sjkim * for a structure called stname. 137109998Smarkm * 138280304Sjkim * If you want the same structure but a different 139280304Sjkim * name then use: 140109998Smarkm * 141280304Sjkim * ASN1_SEQUENCE(itname) = { 142280304Sjkim * ... SEQUENCE components ... 143280304Sjkim * } ASN1_SEQUENCE_END_name(stname, itname) 144109998Smarkm * 145280304Sjkim * This will create an item called itname_it using 146280304Sjkim * a structure called stname. 147109998Smarkm */ 148109998Smarkm 149280304Sjkim# define ASN1_SEQUENCE(tname) \ 150280304Sjkim static const ASN1_TEMPLATE tname##_seq_tt[] 151109998Smarkm 152280304Sjkim# define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname) 153109998Smarkm 154280304Sjkim# define ASN1_SEQUENCE_END_name(stname, tname) \ 155280304Sjkim ;\ 156280304Sjkim ASN1_ITEM_start(tname) \ 157280304Sjkim ASN1_ITYPE_SEQUENCE,\ 158280304Sjkim V_ASN1_SEQUENCE,\ 159280304Sjkim tname##_seq_tt,\ 160280304Sjkim sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\ 161280304Sjkim NULL,\ 162280304Sjkim sizeof(stname),\ 163280304Sjkim #stname \ 164280304Sjkim ASN1_ITEM_end(tname) 165109998Smarkm 166280304Sjkim# define ASN1_NDEF_SEQUENCE(tname) \ 167280304Sjkim ASN1_SEQUENCE(tname) 168160814Ssimon 169280304Sjkim# define ASN1_NDEF_SEQUENCE_cb(tname, cb) \ 170280304Sjkim ASN1_SEQUENCE_cb(tname, cb) 171194206Ssimon 172280304Sjkim# define ASN1_SEQUENCE_cb(tname, cb) \ 173280304Sjkim static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \ 174280304Sjkim ASN1_SEQUENCE(tname) 175109998Smarkm 176280304Sjkim# define ASN1_BROKEN_SEQUENCE(tname) \ 177280304Sjkim static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \ 178280304Sjkim ASN1_SEQUENCE(tname) 179109998Smarkm 180280304Sjkim# define ASN1_SEQUENCE_ref(tname, cb, lck) \ 181280304Sjkim static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \ 182280304Sjkim ASN1_SEQUENCE(tname) 183109998Smarkm 184280304Sjkim# define ASN1_SEQUENCE_enc(tname, enc, cb) \ 185280304Sjkim static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \ 186280304Sjkim ASN1_SEQUENCE(tname) 187109998Smarkm 188280304Sjkim# define ASN1_NDEF_SEQUENCE_END(tname) \ 189280304Sjkim ;\ 190280304Sjkim ASN1_ITEM_start(tname) \ 191280304Sjkim ASN1_ITYPE_NDEF_SEQUENCE,\ 192280304Sjkim V_ASN1_SEQUENCE,\ 193280304Sjkim tname##_seq_tt,\ 194280304Sjkim sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\ 195280304Sjkim NULL,\ 196280304Sjkim sizeof(tname),\ 197280304Sjkim #tname \ 198280304Sjkim ASN1_ITEM_end(tname) 199160814Ssimon 200280304Sjkim# define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname) 201109998Smarkm 202280304Sjkim# define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname) 203109998Smarkm 204280304Sjkim# define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname) 205109998Smarkm 206280304Sjkim# define ASN1_SEQUENCE_END_ref(stname, tname) \ 207280304Sjkim ;\ 208280304Sjkim ASN1_ITEM_start(tname) \ 209280304Sjkim ASN1_ITYPE_SEQUENCE,\ 210280304Sjkim V_ASN1_SEQUENCE,\ 211280304Sjkim tname##_seq_tt,\ 212280304Sjkim sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\ 213280304Sjkim &tname##_aux,\ 214280304Sjkim sizeof(stname),\ 215280304Sjkim #stname \ 216280304Sjkim ASN1_ITEM_end(tname) 217109998Smarkm 218280304Sjkim# define ASN1_NDEF_SEQUENCE_END_cb(stname, tname) \ 219280304Sjkim ;\ 220280304Sjkim ASN1_ITEM_start(tname) \ 221280304Sjkim ASN1_ITYPE_NDEF_SEQUENCE,\ 222280304Sjkim V_ASN1_SEQUENCE,\ 223280304Sjkim tname##_seq_tt,\ 224280304Sjkim sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\ 225280304Sjkim &tname##_aux,\ 226280304Sjkim sizeof(stname),\ 227280304Sjkim #stname \ 228280304Sjkim ASN1_ITEM_end(tname) 229109998Smarkm 230280304Sjkim/*- 231280304Sjkim * This pair helps declare a CHOICE type. We can do: 232109998Smarkm * 233280304Sjkim * ASN1_CHOICE(chname) = { 234280304Sjkim * ... CHOICE options ... 235280304Sjkim * ASN1_CHOICE_END(chname) 236109998Smarkm * 237280304Sjkim * This will produce an ASN1_ITEM called chname_it 238280304Sjkim * for a structure called chname. The structure 239280304Sjkim * definition must look like this: 240280304Sjkim * typedef struct { 241280304Sjkim * int type; 242280304Sjkim * union { 243280304Sjkim * ASN1_SOMETHING *opt1; 244280304Sjkim * ASN1_SOMEOTHER *opt2; 245280304Sjkim * } value; 246280304Sjkim * } chname; 247280304Sjkim * 248280304Sjkim * the name of the selector must be 'type'. 249280304Sjkim * to use an alternative selector name use the 250109998Smarkm * ASN1_CHOICE_END_selector() version. 251109998Smarkm */ 252109998Smarkm 253280304Sjkim# define ASN1_CHOICE(tname) \ 254280304Sjkim static const ASN1_TEMPLATE tname##_ch_tt[] 255109998Smarkm 256280304Sjkim# define ASN1_CHOICE_cb(tname, cb) \ 257280304Sjkim static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \ 258280304Sjkim ASN1_CHOICE(tname) 259109998Smarkm 260280304Sjkim# define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname) 261109998Smarkm 262280304Sjkim# define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type) 263109998Smarkm 264280304Sjkim# define ASN1_CHOICE_END_selector(stname, tname, selname) \ 265280304Sjkim ;\ 266280304Sjkim ASN1_ITEM_start(tname) \ 267280304Sjkim ASN1_ITYPE_CHOICE,\ 268280304Sjkim offsetof(stname,selname) ,\ 269280304Sjkim tname##_ch_tt,\ 270280304Sjkim sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\ 271280304Sjkim NULL,\ 272280304Sjkim sizeof(stname),\ 273280304Sjkim #stname \ 274280304Sjkim ASN1_ITEM_end(tname) 275109998Smarkm 276280304Sjkim# define ASN1_CHOICE_END_cb(stname, tname, selname) \ 277280304Sjkim ;\ 278280304Sjkim ASN1_ITEM_start(tname) \ 279280304Sjkim ASN1_ITYPE_CHOICE,\ 280280304Sjkim offsetof(stname,selname) ,\ 281280304Sjkim tname##_ch_tt,\ 282280304Sjkim sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\ 283280304Sjkim &tname##_aux,\ 284280304Sjkim sizeof(stname),\ 285280304Sjkim #stname \ 286280304Sjkim ASN1_ITEM_end(tname) 287109998Smarkm 288109998Smarkm/* This helps with the template wrapper form of ASN1_ITEM */ 289109998Smarkm 290280304Sjkim# define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \ 291280304Sjkim (flags), (tag), 0,\ 292280304Sjkim #name, ASN1_ITEM_ref(type) } 293109998Smarkm 294109998Smarkm/* These help with SEQUENCE or CHOICE components */ 295109998Smarkm 296109998Smarkm/* used to declare other types */ 297109998Smarkm 298280304Sjkim# define ASN1_EX_TYPE(flags, tag, stname, field, type) { \ 299280304Sjkim (flags), (tag), offsetof(stname, field),\ 300280304Sjkim #field, ASN1_ITEM_ref(type) } 301109998Smarkm 302109998Smarkm/* used when the structure is combined with the parent */ 303109998Smarkm 304280304Sjkim# define ASN1_EX_COMBINE(flags, tag, type) { \ 305280304Sjkim (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) } 306109998Smarkm 307109998Smarkm/* implicit and explicit helper macros */ 308109998Smarkm 309280304Sjkim# define ASN1_IMP_EX(stname, field, type, tag, ex) \ 310280304Sjkim ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type) 311109998Smarkm 312280304Sjkim# define ASN1_EXP_EX(stname, field, type, tag, ex) \ 313280304Sjkim ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type) 314109998Smarkm 315109998Smarkm/* Any defined by macros: the field used is in the table itself */ 316109998Smarkm 317280304Sjkim# ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION 318280304Sjkim# define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) } 319280304Sjkim# define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) } 320280304Sjkim# else 321280304Sjkim# define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb } 322280304Sjkim# define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb } 323280304Sjkim# endif 324109998Smarkm/* Plain simple type */ 325280304Sjkim# define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type) 326109998Smarkm 327109998Smarkm/* OPTIONAL simple type */ 328280304Sjkim# define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type) 329109998Smarkm 330109998Smarkm/* IMPLICIT tagged simple type */ 331280304Sjkim# define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0) 332109998Smarkm 333109998Smarkm/* IMPLICIT tagged OPTIONAL simple type */ 334280304Sjkim# define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL) 335109998Smarkm 336109998Smarkm/* Same as above but EXPLICIT */ 337109998Smarkm 338280304Sjkim# define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0) 339280304Sjkim# define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL) 340109998Smarkm 341109998Smarkm/* SEQUENCE OF type */ 342280304Sjkim# define ASN1_SEQUENCE_OF(stname, field, type) \ 343280304Sjkim ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type) 344109998Smarkm 345109998Smarkm/* OPTIONAL SEQUENCE OF */ 346280304Sjkim# define ASN1_SEQUENCE_OF_OPT(stname, field, type) \ 347280304Sjkim ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type) 348109998Smarkm 349109998Smarkm/* Same as above but for SET OF */ 350109998Smarkm 351280304Sjkim# define ASN1_SET_OF(stname, field, type) \ 352280304Sjkim ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type) 353109998Smarkm 354280304Sjkim# define ASN1_SET_OF_OPT(stname, field, type) \ 355280304Sjkim ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type) 356109998Smarkm 357109998Smarkm/* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */ 358109998Smarkm 359280304Sjkim# define ASN1_IMP_SET_OF(stname, field, type, tag) \ 360280304Sjkim ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF) 361109998Smarkm 362280304Sjkim# define ASN1_EXP_SET_OF(stname, field, type, tag) \ 363280304Sjkim ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF) 364109998Smarkm 365280304Sjkim# define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \ 366280304Sjkim ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL) 367109998Smarkm 368280304Sjkim# define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \ 369280304Sjkim ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL) 370109998Smarkm 371280304Sjkim# define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \ 372280304Sjkim ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF) 373109998Smarkm 374280304Sjkim# define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \ 375280304Sjkim ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL) 376109998Smarkm 377280304Sjkim# define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \ 378280304Sjkim ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF) 379109998Smarkm 380280304Sjkim# define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \ 381280304Sjkim ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL) 382109998Smarkm 383194206Ssimon/* EXPLICIT using indefinite length constructed form */ 384280304Sjkim# define ASN1_NDEF_EXP(stname, field, type, tag) \ 385280304Sjkim ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF) 386194206Ssimon 387160814Ssimon/* EXPLICIT OPTIONAL using indefinite length constructed form */ 388280304Sjkim# define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \ 389280304Sjkim ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF) 390160814Ssimon 391109998Smarkm/* Macros for the ASN1_ADB structure */ 392109998Smarkm 393280304Sjkim# define ASN1_ADB(name) \ 394280304Sjkim static const ASN1_ADB_TABLE name##_adbtbl[] 395109998Smarkm 396280304Sjkim# ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION 397109998Smarkm 398280304Sjkim# define ASN1_ADB_END(name, flags, field, app_table, def, none) \ 399280304Sjkim ;\ 400280304Sjkim static const ASN1_ADB name##_adb = {\ 401280304Sjkim flags,\ 402280304Sjkim offsetof(name, field),\ 403280304Sjkim app_table,\ 404280304Sjkim name##_adbtbl,\ 405280304Sjkim sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\ 406280304Sjkim def,\ 407280304Sjkim none\ 408280304Sjkim } 409109998Smarkm 410280304Sjkim# else 411109998Smarkm 412280304Sjkim# define ASN1_ADB_END(name, flags, field, app_table, def, none) \ 413280304Sjkim ;\ 414280304Sjkim static const ASN1_ITEM *name##_adb(void) \ 415280304Sjkim { \ 416280304Sjkim static const ASN1_ADB internal_adb = \ 417280304Sjkim {\ 418280304Sjkim flags,\ 419280304Sjkim offsetof(name, field),\ 420280304Sjkim app_table,\ 421280304Sjkim name##_adbtbl,\ 422280304Sjkim sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\ 423280304Sjkim def,\ 424280304Sjkim none\ 425280304Sjkim }; \ 426280304Sjkim return (const ASN1_ITEM *) &internal_adb; \ 427280304Sjkim } \ 428280304Sjkim void dummy_function(void) 429109998Smarkm 430280304Sjkim# endif 431109998Smarkm 432280304Sjkim# define ADB_ENTRY(val, template) {val, template} 433109998Smarkm 434280304Sjkim# define ASN1_ADB_TEMPLATE(name) \ 435280304Sjkim static const ASN1_TEMPLATE name##_tt 436109998Smarkm 437280304Sjkim/* 438280304Sjkim * This is the ASN1 template structure that defines a wrapper round the 439280304Sjkim * actual type. It determines the actual position of the field in the value 440280304Sjkim * structure, various flags such as OPTIONAL and the field name. 441109998Smarkm */ 442109998Smarkm 443109998Smarkmstruct ASN1_TEMPLATE_st { 444280304Sjkim unsigned long flags; /* Various flags */ 445280304Sjkim long tag; /* tag, not used if no tagging */ 446280304Sjkim unsigned long offset; /* Offset of this field in structure */ 447280304Sjkim# ifndef NO_ASN1_FIELD_NAMES 448280304Sjkim const char *field_name; /* Field name */ 449280304Sjkim# endif 450280304Sjkim ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */ 451109998Smarkm}; 452109998Smarkm 453109998Smarkm/* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */ 454109998Smarkm 455280304Sjkim# define ASN1_TEMPLATE_item(t) (t->item_ptr) 456280304Sjkim# define ASN1_TEMPLATE_adb(t) (t->item_ptr) 457109998Smarkm 458109998Smarkmtypedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE; 459109998Smarkmtypedef struct ASN1_ADB_st ASN1_ADB; 460109998Smarkm 461109998Smarkmstruct ASN1_ADB_st { 462280304Sjkim unsigned long flags; /* Various flags */ 463280304Sjkim unsigned long offset; /* Offset of selector field */ 464280304Sjkim STACK_OF(ASN1_ADB_TABLE) **app_items; /* Application defined items */ 465280304Sjkim const ASN1_ADB_TABLE *tbl; /* Table of possible types */ 466280304Sjkim long tblcount; /* Number of entries in tbl */ 467280304Sjkim const ASN1_TEMPLATE *default_tt; /* Type to use if no match */ 468280304Sjkim const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */ 469109998Smarkm}; 470109998Smarkm 471109998Smarkmstruct ASN1_ADB_TABLE_st { 472280304Sjkim long value; /* NID for an object or value for an int */ 473280304Sjkim const ASN1_TEMPLATE tt; /* item for this value */ 474109998Smarkm}; 475109998Smarkm 476109998Smarkm/* template flags */ 477109998Smarkm 478109998Smarkm/* Field is optional */ 479280304Sjkim# define ASN1_TFLG_OPTIONAL (0x1) 480109998Smarkm 481109998Smarkm/* Field is a SET OF */ 482280304Sjkim# define ASN1_TFLG_SET_OF (0x1 << 1) 483109998Smarkm 484109998Smarkm/* Field is a SEQUENCE OF */ 485280304Sjkim# define ASN1_TFLG_SEQUENCE_OF (0x2 << 1) 486109998Smarkm 487280304Sjkim/* 488280304Sjkim * Special case: this refers to a SET OF that will be sorted into DER order 489280304Sjkim * when encoded *and* the corresponding STACK will be modified to match the 490280304Sjkim * new order. 491109998Smarkm */ 492280304Sjkim# define ASN1_TFLG_SET_ORDER (0x3 << 1) 493109998Smarkm 494109998Smarkm/* Mask for SET OF or SEQUENCE OF */ 495280304Sjkim# define ASN1_TFLG_SK_MASK (0x3 << 1) 496109998Smarkm 497280304Sjkim/* 498280304Sjkim * These flags mean the tag should be taken from the tag field. If EXPLICIT 499280304Sjkim * then the underlying type is used for the inner tag. 500109998Smarkm */ 501109998Smarkm 502109998Smarkm/* IMPLICIT tagging */ 503280304Sjkim# define ASN1_TFLG_IMPTAG (0x1 << 3) 504109998Smarkm 505109998Smarkm/* EXPLICIT tagging, inner tag from underlying type */ 506280304Sjkim# define ASN1_TFLG_EXPTAG (0x2 << 3) 507109998Smarkm 508280304Sjkim# define ASN1_TFLG_TAG_MASK (0x3 << 3) 509109998Smarkm 510109998Smarkm/* context specific IMPLICIT */ 511280304Sjkim# define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT 512109998Smarkm 513109998Smarkm/* context specific EXPLICIT */ 514280304Sjkim# define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT 515109998Smarkm 516280304Sjkim/* 517280304Sjkim * If tagging is in force these determine the type of tag to use. Otherwise 518280304Sjkim * the tag is determined by the underlying type. These values reflect the 519280304Sjkim * actual octet format. 520109998Smarkm */ 521109998Smarkm 522280304Sjkim/* Universal tag */ 523280304Sjkim# define ASN1_TFLG_UNIVERSAL (0x0<<6) 524280304Sjkim/* Application tag */ 525280304Sjkim# define ASN1_TFLG_APPLICATION (0x1<<6) 526280304Sjkim/* Context specific tag */ 527280304Sjkim# define ASN1_TFLG_CONTEXT (0x2<<6) 528280304Sjkim/* Private tag */ 529280304Sjkim# define ASN1_TFLG_PRIVATE (0x3<<6) 530109998Smarkm 531280304Sjkim# define ASN1_TFLG_TAG_CLASS (0x3<<6) 532109998Smarkm 533280304Sjkim/* 534280304Sjkim * These are for ANY DEFINED BY type. In this case the 'item' field points to 535280304Sjkim * an ASN1_ADB structure which contains a table of values to decode the 536109998Smarkm * relevant type 537109998Smarkm */ 538109998Smarkm 539280304Sjkim# define ASN1_TFLG_ADB_MASK (0x3<<8) 540109998Smarkm 541280304Sjkim# define ASN1_TFLG_ADB_OID (0x1<<8) 542109998Smarkm 543280304Sjkim# define ASN1_TFLG_ADB_INT (0x1<<9) 544109998Smarkm 545280304Sjkim/* 546280304Sjkim * This flag means a parent structure is passed instead of the field: this is 547280304Sjkim * useful is a SEQUENCE is being combined with a CHOICE for example. Since 548280304Sjkim * this means the structure and item name will differ we need to use the 549109998Smarkm * ASN1_CHOICE_END_name() macro for example. 550109998Smarkm */ 551109998Smarkm 552280304Sjkim# define ASN1_TFLG_COMBINE (0x1<<10) 553109998Smarkm 554280304Sjkim/* 555280304Sjkim * This flag when present in a SEQUENCE OF, SET OF or EXPLICIT causes 556280304Sjkim * indefinite length constructed encoding to be used if required. 557160814Ssimon */ 558160814Ssimon 559280304Sjkim# define ASN1_TFLG_NDEF (0x1<<11) 560160814Ssimon 561109998Smarkm/* This is the actual ASN1 item itself */ 562109998Smarkm 563109998Smarkmstruct ASN1_ITEM_st { 564280304Sjkim char itype; /* The item type, primitive, SEQUENCE, CHOICE 565280304Sjkim * or extern */ 566280304Sjkim long utype; /* underlying type */ 567280304Sjkim const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains 568280304Sjkim * the contents */ 569280304Sjkim long tcount; /* Number of templates if SEQUENCE or CHOICE */ 570280304Sjkim const void *funcs; /* functions that handle this type */ 571280304Sjkim long size; /* Structure size (usually) */ 572280304Sjkim# ifndef NO_ASN1_FIELD_NAMES 573280304Sjkim const char *sname; /* Structure name */ 574280304Sjkim# endif 575109998Smarkm}; 576109998Smarkm 577280304Sjkim/*- 578280304Sjkim * These are values for the itype field and 579109998Smarkm * determine how the type is interpreted. 580109998Smarkm * 581109998Smarkm * For PRIMITIVE types the underlying type 582109998Smarkm * determines the behaviour if items is NULL. 583109998Smarkm * 584280304Sjkim * Otherwise templates must contain a single 585109998Smarkm * template and the type is treated in the 586109998Smarkm * same way as the type specified in the template. 587109998Smarkm * 588109998Smarkm * For SEQUENCE types the templates field points 589109998Smarkm * to the members, the size field is the 590109998Smarkm * structure size. 591109998Smarkm * 592109998Smarkm * For CHOICE types the templates field points 593109998Smarkm * to each possible member (typically a union) 594109998Smarkm * and the 'size' field is the offset of the 595109998Smarkm * selector. 596109998Smarkm * 597109998Smarkm * The 'funcs' field is used for application 598280304Sjkim * specific functions. 599109998Smarkm * 600109998Smarkm * For COMPAT types the funcs field gives a 601109998Smarkm * set of functions that handle this type, this 602109998Smarkm * supports the old d2i, i2d convention. 603109998Smarkm * 604109998Smarkm * The EXTERN type uses a new style d2i/i2d. 605109998Smarkm * The new style should be used where possible 606109998Smarkm * because it avoids things like the d2i IMPLICIT 607109998Smarkm * hack. 608109998Smarkm * 609109998Smarkm * MSTRING is a multiple string type, it is used 610109998Smarkm * for a CHOICE of character strings where the 611109998Smarkm * actual strings all occupy an ASN1_STRING 612109998Smarkm * structure. In this case the 'utype' field 613109998Smarkm * has a special meaning, it is used as a mask 614109998Smarkm * of acceptable types using the B_ASN1 constants. 615109998Smarkm * 616160814Ssimon * NDEF_SEQUENCE is the same as SEQUENCE except 617160814Ssimon * that it will use indefinite length constructed 618160814Ssimon * encoding if requested. 619160814Ssimon * 620109998Smarkm */ 621109998Smarkm 622280304Sjkim# define ASN1_ITYPE_PRIMITIVE 0x0 623109998Smarkm 624280304Sjkim# define ASN1_ITYPE_SEQUENCE 0x1 625109998Smarkm 626280304Sjkim# define ASN1_ITYPE_CHOICE 0x2 627109998Smarkm 628280304Sjkim# define ASN1_ITYPE_COMPAT 0x3 629109998Smarkm 630280304Sjkim# define ASN1_ITYPE_EXTERN 0x4 631109998Smarkm 632280304Sjkim# define ASN1_ITYPE_MSTRING 0x5 633109998Smarkm 634280304Sjkim# define ASN1_ITYPE_NDEF_SEQUENCE 0x6 635160814Ssimon 636280304Sjkim/* 637280304Sjkim * Cache for ASN1 tag and length, so we don't keep re-reading it for things 638109998Smarkm * like CHOICE 639109998Smarkm */ 640109998Smarkm 641280304Sjkimstruct ASN1_TLC_st { 642280304Sjkim char valid; /* Values below are valid */ 643280304Sjkim int ret; /* return value */ 644280304Sjkim long plen; /* length */ 645280304Sjkim int ptag; /* class value */ 646280304Sjkim int pclass; /* class value */ 647280304Sjkim int hdrlen; /* header length */ 648109998Smarkm}; 649109998Smarkm 650109998Smarkm/* Typedefs for ASN1 function pointers */ 651109998Smarkm 652280304Sjkimtypedef ASN1_VALUE *ASN1_new_func(void); 653109998Smarkmtypedef void ASN1_free_func(ASN1_VALUE *a); 654280304Sjkimtypedef ASN1_VALUE *ASN1_d2i_func(ASN1_VALUE **a, const unsigned char **in, 655280304Sjkim long length); 656280304Sjkimtypedef int ASN1_i2d_func(ASN1_VALUE *a, unsigned char **in); 657109998Smarkm 658280304Sjkimtypedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, 659280304Sjkim const ASN1_ITEM *it, int tag, int aclass, char opt, 660280304Sjkim ASN1_TLC *ctx); 661109998Smarkm 662280304Sjkimtypedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, 663280304Sjkim const ASN1_ITEM *it, int tag, int aclass); 664109998Smarkmtypedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it); 665109998Smarkmtypedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it); 666109998Smarkm 667280304Sjkimtypedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval, 668280304Sjkim int indent, const char *fname, 669280304Sjkim const ASN1_PCTX *pctx); 670238405Sjkim 671280304Sjkimtypedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont, 672280304Sjkim int *putype, const ASN1_ITEM *it); 673280304Sjkimtypedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont, 674280304Sjkim int len, int utype, char *free_cont, 675280304Sjkim const ASN1_ITEM *it); 676280304Sjkimtypedef int ASN1_primitive_print(BIO *out, ASN1_VALUE **pval, 677280304Sjkim const ASN1_ITEM *it, int indent, 678280304Sjkim const ASN1_PCTX *pctx); 679109998Smarkm 680109998Smarkmtypedef struct ASN1_COMPAT_FUNCS_st { 681280304Sjkim ASN1_new_func *asn1_new; 682280304Sjkim ASN1_free_func *asn1_free; 683280304Sjkim ASN1_d2i_func *asn1_d2i; 684280304Sjkim ASN1_i2d_func *asn1_i2d; 685109998Smarkm} ASN1_COMPAT_FUNCS; 686109998Smarkm 687109998Smarkmtypedef struct ASN1_EXTERN_FUNCS_st { 688280304Sjkim void *app_data; 689280304Sjkim ASN1_ex_new_func *asn1_ex_new; 690280304Sjkim ASN1_ex_free_func *asn1_ex_free; 691280304Sjkim ASN1_ex_free_func *asn1_ex_clear; 692280304Sjkim ASN1_ex_d2i *asn1_ex_d2i; 693280304Sjkim ASN1_ex_i2d *asn1_ex_i2d; 694280304Sjkim ASN1_ex_print_func *asn1_ex_print; 695109998Smarkm} ASN1_EXTERN_FUNCS; 696109998Smarkm 697109998Smarkmtypedef struct ASN1_PRIMITIVE_FUNCS_st { 698280304Sjkim void *app_data; 699280304Sjkim unsigned long flags; 700280304Sjkim ASN1_ex_new_func *prim_new; 701280304Sjkim ASN1_ex_free_func *prim_free; 702280304Sjkim ASN1_ex_free_func *prim_clear; 703280304Sjkim ASN1_primitive_c2i *prim_c2i; 704280304Sjkim ASN1_primitive_i2c *prim_i2c; 705280304Sjkim ASN1_primitive_print *prim_print; 706109998Smarkm} ASN1_PRIMITIVE_FUNCS; 707109998Smarkm 708280304Sjkim/* 709280304Sjkim * This is the ASN1_AUX structure: it handles various miscellaneous 710280304Sjkim * requirements. For example the use of reference counts and an informational 711280304Sjkim * callback. The "informational callback" is called at various points during 712280304Sjkim * the ASN1 encoding and decoding. It can be used to provide minor 713280304Sjkim * customisation of the structures used. This is most useful where the 714280304Sjkim * supplied routines *almost* do the right thing but need some extra help at 715280304Sjkim * a few points. If the callback returns zero then it is assumed a fatal 716280304Sjkim * error has occurred and the main operation should be abandoned. If major 717280304Sjkim * changes in the default behaviour are required then an external type is 718280304Sjkim * more appropriate. 719109998Smarkm */ 720109998Smarkm 721238405Sjkimtypedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it, 722280304Sjkim void *exarg); 723109998Smarkm 724109998Smarkmtypedef struct ASN1_AUX_st { 725280304Sjkim void *app_data; 726280304Sjkim int flags; 727280304Sjkim int ref_offset; /* Offset of reference value */ 728280304Sjkim int ref_lock; /* Lock type to use */ 729280304Sjkim ASN1_aux_cb *asn1_cb; 730280304Sjkim int enc_offset; /* Offset of ASN1_ENCODING structure */ 731109998Smarkm} ASN1_AUX; 732109998Smarkm 733238405Sjkim/* For print related callbacks exarg points to this structure */ 734238405Sjkimtypedef struct ASN1_PRINT_ARG_st { 735280304Sjkim BIO *out; 736280304Sjkim int indent; 737280304Sjkim const ASN1_PCTX *pctx; 738238405Sjkim} ASN1_PRINT_ARG; 739238405Sjkim 740238405Sjkim/* For streaming related callbacks exarg points to this structure */ 741238405Sjkimtypedef struct ASN1_STREAM_ARG_st { 742280304Sjkim /* BIO to stream through */ 743280304Sjkim BIO *out; 744280304Sjkim /* BIO with filters appended */ 745280304Sjkim BIO *ndef_bio; 746280304Sjkim /* Streaming I/O boundary */ 747280304Sjkim unsigned char **boundary; 748238405Sjkim} ASN1_STREAM_ARG; 749238405Sjkim 750109998Smarkm/* Flags in ASN1_AUX */ 751109998Smarkm 752109998Smarkm/* Use a reference count */ 753280304Sjkim# define ASN1_AFLG_REFCOUNT 1 754109998Smarkm/* Save the encoding of structure (useful for signatures) */ 755280304Sjkim# define ASN1_AFLG_ENCODING 2 756109998Smarkm/* The Sequence length is invalid */ 757280304Sjkim# define ASN1_AFLG_BROKEN 4 758109998Smarkm 759109998Smarkm/* operation values for asn1_cb */ 760109998Smarkm 761280304Sjkim# define ASN1_OP_NEW_PRE 0 762280304Sjkim# define ASN1_OP_NEW_POST 1 763280304Sjkim# define ASN1_OP_FREE_PRE 2 764280304Sjkim# define ASN1_OP_FREE_POST 3 765280304Sjkim# define ASN1_OP_D2I_PRE 4 766280304Sjkim# define ASN1_OP_D2I_POST 5 767280304Sjkim# define ASN1_OP_I2D_PRE 6 768280304Sjkim# define ASN1_OP_I2D_POST 7 769280304Sjkim# define ASN1_OP_PRINT_PRE 8 770280304Sjkim# define ASN1_OP_PRINT_POST 9 771280304Sjkim# define ASN1_OP_STREAM_PRE 10 772280304Sjkim# define ASN1_OP_STREAM_POST 11 773280304Sjkim# define ASN1_OP_DETACHED_PRE 12 774280304Sjkim# define ASN1_OP_DETACHED_POST 13 775109998Smarkm 776109998Smarkm/* Macro to implement a primitive type */ 777280304Sjkim# define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0) 778280304Sjkim# define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \ 779280304Sjkim ASN1_ITEM_start(itname) \ 780280304Sjkim ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \ 781280304Sjkim ASN1_ITEM_end(itname) 782109998Smarkm 783109998Smarkm/* Macro to implement a multi string type */ 784280304Sjkim# define IMPLEMENT_ASN1_MSTRING(itname, mask) \ 785280304Sjkim ASN1_ITEM_start(itname) \ 786280304Sjkim ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \ 787280304Sjkim ASN1_ITEM_end(itname) 788109998Smarkm 789109998Smarkm/* Macro to implement an ASN1_ITEM in terms of old style funcs */ 790109998Smarkm 791280304Sjkim# define IMPLEMENT_COMPAT_ASN1(sname) IMPLEMENT_COMPAT_ASN1_type(sname, V_ASN1_SEQUENCE) 792109998Smarkm 793280304Sjkim# define IMPLEMENT_COMPAT_ASN1_type(sname, tag) \ 794280304Sjkim static const ASN1_COMPAT_FUNCS sname##_ff = { \ 795280304Sjkim (ASN1_new_func *)sname##_new, \ 796280304Sjkim (ASN1_free_func *)sname##_free, \ 797280304Sjkim (ASN1_d2i_func *)d2i_##sname, \ 798280304Sjkim (ASN1_i2d_func *)i2d_##sname, \ 799280304Sjkim }; \ 800280304Sjkim ASN1_ITEM_start(sname) \ 801280304Sjkim ASN1_ITYPE_COMPAT, \ 802280304Sjkim tag, \ 803280304Sjkim NULL, \ 804280304Sjkim 0, \ 805280304Sjkim &sname##_ff, \ 806280304Sjkim 0, \ 807280304Sjkim #sname \ 808280304Sjkim ASN1_ITEM_end(sname) 809109998Smarkm 810280304Sjkim# define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \ 811280304Sjkim ASN1_ITEM_start(sname) \ 812280304Sjkim ASN1_ITYPE_EXTERN, \ 813280304Sjkim tag, \ 814280304Sjkim NULL, \ 815280304Sjkim 0, \ 816280304Sjkim &fptrs, \ 817280304Sjkim 0, \ 818280304Sjkim #sname \ 819280304Sjkim ASN1_ITEM_end(sname) 820109998Smarkm 821109998Smarkm/* Macro to implement standard functions in terms of ASN1_ITEM structures */ 822109998Smarkm 823280304Sjkim# define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname) 824109998Smarkm 825280304Sjkim# define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname) 826109998Smarkm 827280304Sjkim# define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \ 828280304Sjkim IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname) 829109998Smarkm 830280304Sjkim# define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \ 831280304Sjkim IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname) 832238405Sjkim 833280304Sjkim# define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \ 834280304Sjkim IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname) 835160814Ssimon 836280304Sjkim# define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \ 837280304Sjkim pre stname *fname##_new(void) \ 838280304Sjkim { \ 839280304Sjkim return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \ 840280304Sjkim } \ 841280304Sjkim pre void fname##_free(stname *a) \ 842280304Sjkim { \ 843280304Sjkim ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \ 844280304Sjkim } 845238405Sjkim 846280304Sjkim# define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \ 847280304Sjkim stname *fname##_new(void) \ 848280304Sjkim { \ 849280304Sjkim return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \ 850280304Sjkim } \ 851280304Sjkim void fname##_free(stname *a) \ 852280304Sjkim { \ 853280304Sjkim ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \ 854280304Sjkim } 855109998Smarkm 856280304Sjkim# define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \ 857280304Sjkim IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \ 858280304Sjkim IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) 859109998Smarkm 860280304Sjkim# define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \ 861280304Sjkim stname *d2i_##fname(stname **a, const unsigned char **in, long len) \ 862280304Sjkim { \ 863280304Sjkim return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\ 864280304Sjkim } \ 865280304Sjkim int i2d_##fname(stname *a, unsigned char **out) \ 866280304Sjkim { \ 867280304Sjkim return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\ 868280304Sjkim } 869109998Smarkm 870280304Sjkim# define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \ 871280304Sjkim int i2d_##stname##_NDEF(stname *a, unsigned char **out) \ 872280304Sjkim { \ 873280304Sjkim return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\ 874280304Sjkim } 875160814Ssimon 876280304Sjkim/* 877280304Sjkim * This includes evil casts to remove const: they will go away when full ASN1 878280304Sjkim * constification is done. 879109998Smarkm */ 880280304Sjkim# define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \ 881280304Sjkim stname *d2i_##fname(stname **a, const unsigned char **in, long len) \ 882280304Sjkim { \ 883280304Sjkim return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\ 884280304Sjkim } \ 885280304Sjkim int i2d_##fname(const stname *a, unsigned char **out) \ 886280304Sjkim { \ 887280304Sjkim return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\ 888280304Sjkim } 889109998Smarkm 890280304Sjkim# define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \ 891280304Sjkim stname * stname##_dup(stname *x) \ 892109998Smarkm { \ 893109998Smarkm return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \ 894109998Smarkm } 895109998Smarkm 896280304Sjkim# define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \ 897280304Sjkim IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname) 898238405Sjkim 899280304Sjkim# define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \ 900280304Sjkim int fname##_print_ctx(BIO *out, stname *x, int indent, \ 901280304Sjkim const ASN1_PCTX *pctx) \ 902280304Sjkim { \ 903280304Sjkim return ASN1_item_print(out, (ASN1_VALUE *)x, indent, \ 904280304Sjkim ASN1_ITEM_rptr(itname), pctx); \ 905280304Sjkim } 906238405Sjkim 907280304Sjkim# define IMPLEMENT_ASN1_FUNCTIONS_const(name) \ 908280304Sjkim IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name) 909109998Smarkm 910280304Sjkim# define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \ 911280304Sjkim IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \ 912280304Sjkim IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) 913109998Smarkm 914109998Smarkm/* external definitions for primitive types */ 915109998Smarkm 916109998SmarkmDECLARE_ASN1_ITEM(ASN1_BOOLEAN) 917109998SmarkmDECLARE_ASN1_ITEM(ASN1_TBOOLEAN) 918109998SmarkmDECLARE_ASN1_ITEM(ASN1_FBOOLEAN) 919109998SmarkmDECLARE_ASN1_ITEM(ASN1_SEQUENCE) 920109998SmarkmDECLARE_ASN1_ITEM(CBIGNUM) 921109998SmarkmDECLARE_ASN1_ITEM(BIGNUM) 922109998SmarkmDECLARE_ASN1_ITEM(LONG) 923109998SmarkmDECLARE_ASN1_ITEM(ZLONG) 924109998Smarkm 925109998SmarkmDECLARE_STACK_OF(ASN1_VALUE) 926109998Smarkm 927109998Smarkm/* Functions used internally by the ASN1 code */ 928109998Smarkm 929109998Smarkmint ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it); 930109998Smarkmvoid ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it); 931109998Smarkmint ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); 932109998Smarkmint ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it); 933109998Smarkm 934109998Smarkmvoid ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); 935280304Sjkimint ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, 936280304Sjkim const ASN1_TEMPLATE *tt); 937280304Sjkimint ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, 938280304Sjkim const ASN1_ITEM *it, int tag, int aclass, char opt, 939280304Sjkim ASN1_TLC *ctx); 940109998Smarkm 941280304Sjkimint ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, 942280304Sjkim const ASN1_ITEM *it, int tag, int aclass); 943280304Sjkimint ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, 944280304Sjkim const ASN1_TEMPLATE *tt); 945109998Smarkmvoid ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it); 946109998Smarkm 947280304Sjkimint asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, 948280304Sjkim const ASN1_ITEM *it); 949280304Sjkimint asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, 950280304Sjkim int utype, char *free_cont, const ASN1_ITEM *it); 951109998Smarkm 952109998Smarkmint asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it); 953280304Sjkimint asn1_set_choice_selector(ASN1_VALUE **pval, int value, 954280304Sjkim const ASN1_ITEM *it); 955109998Smarkm 956280304SjkimASN1_VALUE **asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); 957109998Smarkm 958280304Sjkimconst ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, 959280304Sjkim int nullerr); 960109998Smarkm 961109998Smarkmint asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it); 962109998Smarkm 963109998Smarkmvoid asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it); 964109998Smarkmvoid asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it); 965280304Sjkimint asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, 966280304Sjkim const ASN1_ITEM *it); 967280304Sjkimint asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, 968280304Sjkim const ASN1_ITEM *it); 969109998Smarkm 970109998Smarkm#ifdef __cplusplus 971109998Smarkm} 972109998Smarkm#endif 973109998Smarkm#endif 974