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