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