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