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