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