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