1 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2 * project 2000. 3 */ 4 /* ==================================================================== 5 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. All advertising materials mentioning features or use of this 20 * software must display the following acknowledgment: 21 * "This product includes software developed by the OpenSSL Project 22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 23 * 24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 25 * endorse or promote products derived from this software without 26 * prior written permission. For written permission, please contact 27 * licensing@OpenSSL.org. 28 * 29 * 5. Products derived from this software may not be called "OpenSSL" 30 * nor may "OpenSSL" appear in their names without prior written 31 * permission of the OpenSSL Project. 32 * 33 * 6. Redistributions of any form whatsoever must retain the following 34 * acknowledgment: 35 * "This product includes software developed by the OpenSSL Project 36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 37 * 38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 49 * OF THE POSSIBILITY OF SUCH DAMAGE. 50 * ==================================================================== 51 * 52 * This product includes cryptographic software written by Eric Young 53 * (eay@cryptsoft.com). This product includes software written by Tim 54 * Hudson (tjh@cryptsoft.com). 55 * 56 */ 57 #ifndef HEADER_ASN1T_H 58 #define HEADER_ASN1T_H 59 60 #include <openssl/base.h> 61 #include <openssl/asn1.h> 62 63 #ifdef __cplusplus 64 extern "C" { 65 #endif 66 67 68 /* Legacy ASN.1 library template definitions. 69 * 70 * This header is used to define new types in OpenSSL's ASN.1 implementation. It 71 * is deprecated and will be unexported from the library. Use the new |CBS| and 72 * |CBB| library in <openssl/bytestring.h> instead. */ 73 74 75 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */ 76 #define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr)) 77 78 79 /* Macros for start and end of ASN1_ITEM definition */ 80 81 #define ASN1_ITEM_start(itname) \ 82 const ASN1_ITEM itname##_it = { 83 84 #define ASN1_ITEM_end(itname) \ 85 }; 86 87 /* Macros to aid ASN1 template writing */ 88 89 #define ASN1_ITEM_TEMPLATE(tname) \ 90 static const ASN1_TEMPLATE tname##_item_tt 91 92 #define ASN1_ITEM_TEMPLATE_END(tname) \ 93 ;\ 94 ASN1_ITEM_start(tname) \ 95 ASN1_ITYPE_PRIMITIVE,\ 96 -1,\ 97 &tname##_item_tt,\ 98 0,\ 99 NULL,\ 100 0,\ 101 #tname \ 102 ASN1_ITEM_end(tname) 103 104 105 /* This is a ASN1 type which just embeds a template */ 106 107 /* This pair helps declare a SEQUENCE. We can do: 108 * 109 * ASN1_SEQUENCE(stname) = { 110 * ... SEQUENCE components ... 111 * } ASN1_SEQUENCE_END(stname) 112 * 113 * This will produce an ASN1_ITEM called stname_it 114 * for a structure called stname. 115 * 116 * If you want the same structure but a different 117 * name then use: 118 * 119 * ASN1_SEQUENCE(itname) = { 120 * ... SEQUENCE components ... 121 * } ASN1_SEQUENCE_END_name(stname, itname) 122 * 123 * This will create an item called itname_it using 124 * a structure called stname. 125 */ 126 127 #define ASN1_SEQUENCE(tname) \ 128 static const ASN1_TEMPLATE tname##_seq_tt[] 129 130 #define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname) 131 132 #define ASN1_SEQUENCE_END_name(stname, tname) \ 133 ;\ 134 ASN1_ITEM_start(tname) \ 135 ASN1_ITYPE_SEQUENCE,\ 136 V_ASN1_SEQUENCE,\ 137 tname##_seq_tt,\ 138 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\ 139 NULL,\ 140 sizeof(stname),\ 141 #stname \ 142 ASN1_ITEM_end(tname) 143 144 #define ASN1_SEQUENCE_cb(tname, cb) \ 145 static const ASN1_AUX tname##_aux = {NULL, 0, 0, cb, 0}; \ 146 ASN1_SEQUENCE(tname) 147 148 #define ASN1_SEQUENCE_ref(tname, cb) \ 149 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), cb, 0}; \ 150 ASN1_SEQUENCE(tname) 151 152 #define ASN1_SEQUENCE_enc(tname, enc, cb) \ 153 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, cb, offsetof(tname, enc)}; \ 154 ASN1_SEQUENCE(tname) 155 156 #define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname) 157 158 #define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname) 159 160 #define ASN1_SEQUENCE_END_ref(stname, tname) \ 161 ;\ 162 ASN1_ITEM_start(tname) \ 163 ASN1_ITYPE_SEQUENCE,\ 164 V_ASN1_SEQUENCE,\ 165 tname##_seq_tt,\ 166 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\ 167 &tname##_aux,\ 168 sizeof(stname),\ 169 #stname \ 170 ASN1_ITEM_end(tname) 171 172 173 /* This pair helps declare a CHOICE type. We can do: 174 * 175 * ASN1_CHOICE(chname) = { 176 * ... CHOICE options ... 177 * ASN1_CHOICE_END(chname) 178 * 179 * This will produce an ASN1_ITEM called chname_it 180 * for a structure called chname. The structure 181 * definition must look like this: 182 * typedef struct { 183 * int type; 184 * union { 185 * ASN1_SOMETHING *opt1; 186 * ASN1_SOMEOTHER *opt2; 187 * } value; 188 * } chname; 189 * 190 * the name of the selector must be 'type'. 191 * to use an alternative selector name use the 192 * ASN1_CHOICE_END_selector() version. 193 */ 194 195 #define ASN1_CHOICE(tname) \ 196 static const ASN1_TEMPLATE tname##_ch_tt[] 197 198 #define ASN1_CHOICE_cb(tname, cb) \ 199 static const ASN1_AUX tname##_aux = {NULL, 0, 0, cb, 0}; \ 200 ASN1_CHOICE(tname) 201 202 #define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname) 203 204 #define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type) 205 206 #define ASN1_CHOICE_END_selector(stname, tname, selname) \ 207 ;\ 208 ASN1_ITEM_start(tname) \ 209 ASN1_ITYPE_CHOICE,\ 210 offsetof(stname,selname) ,\ 211 tname##_ch_tt,\ 212 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\ 213 NULL,\ 214 sizeof(stname),\ 215 #stname \ 216 ASN1_ITEM_end(tname) 217 218 #define ASN1_CHOICE_END_cb(stname, tname, selname) \ 219 ;\ 220 ASN1_ITEM_start(tname) \ 221 ASN1_ITYPE_CHOICE,\ 222 offsetof(stname,selname) ,\ 223 tname##_ch_tt,\ 224 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\ 225 &tname##_aux,\ 226 sizeof(stname),\ 227 #stname \ 228 ASN1_ITEM_end(tname) 229 230 /* This helps with the template wrapper form of ASN1_ITEM */ 231 232 #define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \ 233 (flags), (tag), 0,\ 234 #name, ASN1_ITEM_ref(type) } 235 236 /* These help with SEQUENCE or CHOICE components */ 237 238 /* used to declare other types */ 239 240 #define ASN1_EX_TYPE(flags, tag, stname, field, type) { \ 241 (flags), (tag), offsetof(stname, field),\ 242 #field, ASN1_ITEM_ref(type) } 243 244 /* used when the structure is combined with the parent */ 245 246 #define ASN1_EX_COMBINE(flags, tag, type) { \ 247 (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) } 248 249 /* implicit and explicit helper macros */ 250 251 #define ASN1_IMP_EX(stname, field, type, tag, ex) \ 252 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type) 253 254 #define ASN1_EXP_EX(stname, field, type, tag, ex) \ 255 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type) 256 257 /* Any defined by macros: the field used is in the table itself */ 258 259 #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) } 260 #define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) } 261 /* Plain simple type */ 262 #define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type) 263 264 /* OPTIONAL simple type */ 265 #define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type) 266 267 /* IMPLICIT tagged simple type */ 268 #define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0) 269 270 /* IMPLICIT tagged OPTIONAL simple type */ 271 #define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL) 272 273 /* Same as above but EXPLICIT */ 274 275 #define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0) 276 #define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL) 277 278 /* SEQUENCE OF type */ 279 #define ASN1_SEQUENCE_OF(stname, field, type) \ 280 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type) 281 282 /* OPTIONAL SEQUENCE OF */ 283 #define ASN1_SEQUENCE_OF_OPT(stname, field, type) \ 284 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type) 285 286 /* Same as above but for SET OF */ 287 288 #define ASN1_SET_OF(stname, field, type) \ 289 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type) 290 291 #define ASN1_SET_OF_OPT(stname, field, type) \ 292 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type) 293 294 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */ 295 296 #define ASN1_IMP_SET_OF(stname, field, type, tag) \ 297 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF) 298 299 #define ASN1_EXP_SET_OF(stname, field, type, tag) \ 300 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF) 301 302 #define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \ 303 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL) 304 305 #define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \ 306 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL) 307 308 #define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \ 309 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF) 310 311 #define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \ 312 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL) 313 314 #define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \ 315 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF) 316 317 #define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \ 318 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL) 319 320 /* Macros for the ASN1_ADB structure */ 321 322 #define ASN1_ADB(name) \ 323 static const ASN1_ADB_TABLE name##_adbtbl[] 324 325 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \ 326 ;\ 327 static const ASN1_ADB name##_adb = {\ 328 flags,\ 329 offsetof(name, field),\ 330 app_table,\ 331 name##_adbtbl,\ 332 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\ 333 def,\ 334 none\ 335 } 336 337 #define ADB_ENTRY(val, template) {val, template} 338 339 #define ASN1_ADB_TEMPLATE(name) \ 340 static const ASN1_TEMPLATE name##_tt 341 342 /* This is the ASN1 template structure that defines 343 * a wrapper round the actual type. It determines the 344 * actual position of the field in the value structure, 345 * various flags such as OPTIONAL and the field name. 346 */ 347 348 struct ASN1_TEMPLATE_st { 349 unsigned long flags; /* Various flags */ 350 long tag; /* tag, not used if no tagging */ 351 unsigned long offset; /* Offset of this field in structure */ 352 const char *field_name; /* Field name */ 353 ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */ 354 }; 355 356 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */ 357 358 #define ASN1_TEMPLATE_item(t) (t->item_ptr) 359 #define ASN1_TEMPLATE_adb(t) (t->item_ptr) 360 361 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE; 362 typedef struct ASN1_ADB_st ASN1_ADB; 363 364 typedef struct asn1_must_be_null_st ASN1_MUST_BE_NULL; 365 366 struct ASN1_ADB_st { 367 unsigned long flags; /* Various flags */ 368 unsigned long offset; /* Offset of selector field */ 369 ASN1_MUST_BE_NULL *unused; 370 const ASN1_ADB_TABLE *tbl; /* Table of possible types */ 371 long tblcount; /* Number of entries in tbl */ 372 const ASN1_TEMPLATE *default_tt; /* Type to use if no match */ 373 const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */ 374 }; 375 376 struct ASN1_ADB_TABLE_st { 377 long value; /* NID for an object or value for an int */ 378 const ASN1_TEMPLATE tt; /* item for this value */ 379 }; 380 381 /* template flags */ 382 383 /* Field is optional */ 384 #define ASN1_TFLG_OPTIONAL (0x1) 385 386 /* Field is a SET OF */ 387 #define ASN1_TFLG_SET_OF (0x1 << 1) 388 389 /* Field is a SEQUENCE OF */ 390 #define ASN1_TFLG_SEQUENCE_OF (0x2 << 1) 391 392 /* Mask for SET OF or SEQUENCE OF */ 393 #define ASN1_TFLG_SK_MASK (0x3 << 1) 394 395 /* These flags mean the tag should be taken from the 396 * tag field. If EXPLICIT then the underlying type 397 * is used for the inner tag. 398 */ 399 400 /* IMPLICIT tagging */ 401 #define ASN1_TFLG_IMPTAG (0x1 << 3) 402 403 404 /* EXPLICIT tagging, inner tag from underlying type */ 405 #define ASN1_TFLG_EXPTAG (0x2 << 3) 406 407 #define ASN1_TFLG_TAG_MASK (0x3 << 3) 408 409 /* context specific IMPLICIT */ 410 #define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT 411 412 /* context specific EXPLICIT */ 413 #define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT 414 415 /* If tagging is in force these determine the 416 * type of tag to use. Otherwise the tag is 417 * determined by the underlying type. These 418 * values reflect the actual octet format. 419 */ 420 421 /* Universal tag */ 422 #define ASN1_TFLG_UNIVERSAL (0x0<<6) 423 /* Application tag */ 424 #define ASN1_TFLG_APPLICATION (0x1<<6) 425 /* Context specific tag */ 426 #define ASN1_TFLG_CONTEXT (0x2<<6) 427 /* Private tag */ 428 #define ASN1_TFLG_PRIVATE (0x3<<6) 429 430 #define ASN1_TFLG_TAG_CLASS (0x3<<6) 431 432 /* These are for ANY DEFINED BY type. In this case 433 * the 'item' field points to an ASN1_ADB structure 434 * which contains a table of values to decode the 435 * relevant type 436 */ 437 438 #define ASN1_TFLG_ADB_MASK (0x3<<8) 439 440 #define ASN1_TFLG_ADB_OID (0x1<<8) 441 442 #define ASN1_TFLG_ADB_INT (0x1<<9) 443 444 /* This flag means a parent structure is passed 445 * instead of the field: this is useful is a 446 * SEQUENCE is being combined with a CHOICE for 447 * example. Since this means the structure and 448 * item name will differ we need to use the 449 * ASN1_CHOICE_END_name() macro for example. 450 */ 451 452 #define ASN1_TFLG_COMBINE (0x1<<10) 453 454 /* This is the actual ASN1 item itself */ 455 456 struct ASN1_ITEM_st { 457 char itype; /* The item type, primitive, SEQUENCE, CHOICE or extern */ 458 long utype; /* underlying type */ 459 const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains the contents */ 460 long tcount; /* Number of templates if SEQUENCE or CHOICE */ 461 const void *funcs; /* functions that handle this type */ 462 long size; /* Structure size (usually)*/ 463 const char *sname; /* Structure name */ 464 }; 465 466 /* These are values for the itype field and 467 * determine how the type is interpreted. 468 * 469 * For PRIMITIVE types the underlying type 470 * determines the behaviour if items is NULL. 471 * 472 * Otherwise templates must contain a single 473 * template and the type is treated in the 474 * same way as the type specified in the template. 475 * 476 * For SEQUENCE types the templates field points 477 * to the members, the size field is the 478 * structure size. 479 * 480 * For CHOICE types the templates field points 481 * to each possible member (typically a union) 482 * and the 'size' field is the offset of the 483 * selector. 484 * 485 * The 'funcs' field is used for application 486 * specific functions. 487 * 488 * The EXTERN type uses a new style d2i/i2d. 489 * The new style should be used where possible 490 * because it avoids things like the d2i IMPLICIT 491 * hack. 492 * 493 * MSTRING is a multiple string type, it is used 494 * for a CHOICE of character strings where the 495 * actual strings all occupy an ASN1_STRING 496 * structure. In this case the 'utype' field 497 * has a special meaning, it is used as a mask 498 * of acceptable types using the B_ASN1 constants. 499 * 500 */ 501 502 #define ASN1_ITYPE_PRIMITIVE 0x0 503 504 #define ASN1_ITYPE_SEQUENCE 0x1 505 506 #define ASN1_ITYPE_CHOICE 0x2 507 508 #define ASN1_ITYPE_EXTERN 0x4 509 510 #define ASN1_ITYPE_MSTRING 0x5 511 512 /* Cache for ASN1 tag and length, so we 513 * don't keep re-reading it for things 514 * like CHOICE 515 */ 516 517 struct ASN1_TLC_st{ 518 char valid; /* Values below are valid */ 519 int ret; /* return value */ 520 long plen; /* length */ 521 int ptag; /* class value */ 522 int pclass; /* class value */ 523 int hdrlen; /* header length */ 524 }; 525 526 /* Typedefs for ASN1 function pointers */ 527 528 typedef ASN1_VALUE * ASN1_new_func(void); 529 typedef void ASN1_free_func(ASN1_VALUE *a); 530 typedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, const unsigned char ** in, long length); 531 typedef int ASN1_i2d_func(ASN1_VALUE * a, unsigned char **in); 532 533 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it, 534 int tag, int aclass, char opt, ASN1_TLC *ctx); 535 536 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass); 537 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it); 538 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it); 539 540 typedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval, 541 int indent, const char *fname, 542 const ASN1_PCTX *pctx); 543 544 typedef struct ASN1_EXTERN_FUNCS_st { 545 void *app_data; 546 ASN1_ex_new_func *asn1_ex_new; 547 ASN1_ex_free_func *asn1_ex_free; 548 ASN1_ex_free_func *asn1_ex_clear; 549 ASN1_ex_d2i *asn1_ex_d2i; 550 ASN1_ex_i2d *asn1_ex_i2d; 551 /* asn1_ex_print is unused. */ 552 ASN1_ex_print_func *asn1_ex_print; 553 } ASN1_EXTERN_FUNCS; 554 555 /* This is the ASN1_AUX structure: it handles various 556 * miscellaneous requirements. For example the use of 557 * reference counts and an informational callback. 558 * 559 * The "informational callback" is called at various 560 * points during the ASN1 encoding and decoding. It can 561 * be used to provide minor customisation of the structures 562 * used. This is most useful where the supplied routines 563 * *almost* do the right thing but need some extra help 564 * at a few points. If the callback returns zero then 565 * it is assumed a fatal error has occurred and the 566 * main operation should be abandoned. 567 * 568 * If major changes in the default behaviour are required 569 * then an external type is more appropriate. 570 */ 571 572 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it, 573 void *exarg); 574 575 typedef struct ASN1_AUX_st { 576 void *app_data; 577 int flags; 578 int ref_offset; /* Offset of reference value */ 579 ASN1_aux_cb *asn1_cb; 580 int enc_offset; /* Offset of ASN1_ENCODING structure */ 581 } ASN1_AUX; 582 583 /* Flags in ASN1_AUX */ 584 585 /* Use a reference count */ 586 #define ASN1_AFLG_REFCOUNT 1 587 /* Save the encoding of structure (useful for signatures) */ 588 #define ASN1_AFLG_ENCODING 2 589 590 /* operation values for asn1_cb */ 591 592 #define ASN1_OP_NEW_PRE 0 593 #define ASN1_OP_NEW_POST 1 594 #define ASN1_OP_FREE_PRE 2 595 #define ASN1_OP_FREE_POST 3 596 #define ASN1_OP_D2I_PRE 4 597 #define ASN1_OP_D2I_POST 5 598 #define ASN1_OP_I2D_PRE 6 599 #define ASN1_OP_I2D_POST 7 600 #define ASN1_OP_PRINT_PRE 8 601 #define ASN1_OP_PRINT_POST 9 602 #define ASN1_OP_STREAM_PRE 10 603 #define ASN1_OP_STREAM_POST 11 604 #define ASN1_OP_DETACHED_PRE 12 605 #define ASN1_OP_DETACHED_POST 13 606 607 /* Macro to implement a primitive type */ 608 #define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0) 609 #define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \ 610 ASN1_ITEM_start(itname) \ 611 ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \ 612 ASN1_ITEM_end(itname) 613 614 /* Macro to implement a multi string type */ 615 #define IMPLEMENT_ASN1_MSTRING(itname, mask) \ 616 ASN1_ITEM_start(itname) \ 617 ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \ 618 ASN1_ITEM_end(itname) 619 620 #define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \ 621 ASN1_ITEM_start(sname) \ 622 ASN1_ITYPE_EXTERN, \ 623 tag, \ 624 NULL, \ 625 0, \ 626 &fptrs, \ 627 0, \ 628 #sname \ 629 ASN1_ITEM_end(sname) 630 631 /* Macro to implement standard functions in terms of ASN1_ITEM structures */ 632 633 #define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname) 634 635 #define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname) 636 637 #define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \ 638 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname) 639 640 #define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \ 641 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname) 642 643 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \ 644 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname) 645 646 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \ 647 pre stname *fname##_new(void) \ 648 { \ 649 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \ 650 } \ 651 pre void fname##_free(stname *a) \ 652 { \ 653 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \ 654 } 655 656 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \ 657 stname *fname##_new(void) \ 658 { \ 659 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \ 660 } \ 661 void fname##_free(stname *a) \ 662 { \ 663 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \ 664 } 665 666 #define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \ 667 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \ 668 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) 669 670 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \ 671 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \ 672 { \ 673 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\ 674 } \ 675 int i2d_##fname(stname *a, unsigned char **out) \ 676 { \ 677 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\ 678 } 679 680 /* This includes evil casts to remove const: they will go away when full 681 * ASN1 constification is done. 682 */ 683 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \ 684 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \ 685 { \ 686 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\ 687 } \ 688 int i2d_##fname(const stname *a, unsigned char **out) \ 689 { \ 690 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\ 691 } 692 693 #define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \ 694 stname * stname##_dup(stname *x) \ 695 { \ 696 return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \ 697 } 698 699 #define IMPLEMENT_ASN1_FUNCTIONS_const(name) \ 700 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name) 701 702 #define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \ 703 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \ 704 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) 705 706 /* external definitions for primitive types */ 707 708 DECLARE_ASN1_ITEM(ASN1_BOOLEAN) 709 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN) 710 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN) 711 DECLARE_ASN1_ITEM(ASN1_SEQUENCE) 712 713 DEFINE_STACK_OF(ASN1_VALUE) 714 715 #ifdef __cplusplus 716 } 717 #endif 718 #endif 719