| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #ifndef OPENSSL_HEADER_ASN1T_H |
| 16 | #define OPENSSL_HEADER_ASN1T_H |
| 17 |
| 18 | #include <openssl/asn1.h> |
| 19 | #include <openssl/base.h> // IWYU pragma: export |
| 20 |
| 21 | #if defined(__cplusplus) |
| 22 | extern "C" { |
| 23 | #endif |
| 24 |
| 25 |
| 26 | /* Legacy ASN.1 library template definitions. |
| 27 | * |
| 28 | * This header is used to define new types in OpenSSL's ASN.1 implementation. It |
| 29 | * is deprecated and will be unexported from the library. Use the new |CBS| and |
| 30 | * |CBB| library in <openssl/bytestring.h> instead. */ |
| 31 |
| 32 |
| 33 | typedef struct ASN1_TEMPLATE_st ASN1_TEMPLATE; |
| 34 | typedef struct ASN1_TLC_st ASN1_TLC; |
| 35 |
| 36 | /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */ |
| 37 | #define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr)) |
| 38 |
| 39 |
| 40 | /* Macros for start and end of ASN1_ITEM definition */ |
| 41 |
| 42 | #define ASN1_ITEM_start(itname) const ASN1_ITEM itname##_it = { |
| 43 | #define ASN1_ITEM_end(itname) \ |
| 44 | } \ |
| 45 | ; |
| 46 |
| 47 | /* Macros to aid ASN1 template writing */ |
| 48 |
| 49 | #define ASN1_ITEM_TEMPLATE(tname) static const ASN1_TEMPLATE tname##_item_tt |
| 50 |
| 51 | #define ASN1_ITEM_TEMPLATE_END(tname) \ |
| 52 | ; \ |
| 53 | ASN1_ITEM_start(tname) ASN1_ITYPE_PRIMITIVE, -1, &tname##_item_tt, 0, NULL, \ |
| 54 | 0, #tname ASN1_ITEM_end(tname) |
| 55 |
| 56 |
| 57 | /* This is a ASN1 type which just embeds a template */ |
| 58 |
| 59 | /* This pair helps declare a SEQUENCE. We can do: |
| 60 | * |
| 61 | * ASN1_SEQUENCE(stname) = { |
| 62 | * ... SEQUENCE components ... |
| 63 | * } ASN1_SEQUENCE_END(stname) |
| 64 | * |
| 65 | * This will produce an ASN1_ITEM called stname_it |
| 66 | * for a structure called stname. |
| 67 | * |
| 68 | * If you want the same structure but a different |
| 69 | * name then use: |
| 70 | * |
| 71 | * ASN1_SEQUENCE(itname) = { |
| 72 | * ... SEQUENCE components ... |
| 73 | * } ASN1_SEQUENCE_END_name(stname, itname) |
| 74 | * |
| 75 | * This will create an item called itname_it using |
| 76 | * a structure called stname. |
| 77 | */ |
| 78 |
| 79 | #define ASN1_SEQUENCE(tname) static const ASN1_TEMPLATE tname##_seq_tt[] |
| 80 |
| 81 | #define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname) |
| 82 |
| 83 | #define ASN1_SEQUENCE_END_name(stname, tname) \ |
| 84 | ; \ |
| 85 | ASN1_ITEM_start(tname) ASN1_ITYPE_SEQUENCE, V_ASN1_SEQUENCE, tname##_seq_tt, \ |
| 86 | sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE), NULL, sizeof(stname), \ |
| 87 | #stname ASN1_ITEM_end(tname) |
| 88 |
| 89 | #define ASN1_SEQUENCE_cb(tname, cb) \ |
| 90 | static const ASN1_AUX tname##_aux = {NULL, 0, 0, cb, 0}; \ |
| 91 | ASN1_SEQUENCE(tname) |
| 92 |
| 93 | #define ASN1_SEQUENCE_ref(tname, cb) \ |
| 94 | static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, \ |
| 95 | offsetof(tname, references), cb, 0}; \ |
| 96 | ASN1_SEQUENCE(tname) |
| 97 |
| 98 | #define ASN1_SEQUENCE_enc(tname, enc, cb) \ |
| 99 | static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, cb, \ |
| 100 | offsetof(tname, enc)}; \ |
| 101 | ASN1_SEQUENCE(tname) |
| 102 |
| 103 | #define ASN1_SEQUENCE_END_enc(stname, tname) \ |
| 104 | ASN1_SEQUENCE_END_ref(stname, tname) |
| 105 |
| 106 | #define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname) |
| 107 |
| 108 | #define ASN1_SEQUENCE_END_ref(stname, tname) \ |
| 109 | ; \ |
| 110 | ASN1_ITEM_start(tname) ASN1_ITYPE_SEQUENCE, V_ASN1_SEQUENCE, tname##_seq_tt, \ |
| 111 | sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE), &tname##_aux, \ |
| 112 | sizeof(stname), #stname ASN1_ITEM_end(tname) |
| 113 |
| 114 |
| 115 | /* This pair helps declare a CHOICE type. We can do: |
| 116 | * |
| 117 | * ASN1_CHOICE(chname) = { |
| 118 | * ... CHOICE options ... |
| 119 | * ASN1_CHOICE_END(chname) |
| 120 | * |
| 121 | * This will produce an ASN1_ITEM called chname_it |
| 122 | * for a structure called chname. The structure |
| 123 | * definition must look like this: |
| 124 | * typedef struct { |
| 125 | * int type; |
| 126 | * union { |
| 127 | * ASN1_SOMETHING *opt1; |
| 128 | * ASN1_SOMEOTHER *opt2; |
| 129 | * } value; |
| 130 | * } chname; |
| 131 | * |
| 132 | * the name of the selector must be 'type'. |
| 133 | * to use an alternative selector name use the |
| 134 | * ASN1_CHOICE_END_selector() version. |
| 135 | */ |
| 136 |
| 137 | #define ASN1_CHOICE(tname) static const ASN1_TEMPLATE tname##_ch_tt[] |
| 138 |
| 139 | #define ASN1_CHOICE_cb(tname, cb) \ |
| 140 | static const ASN1_AUX tname##_aux = {NULL, 0, 0, cb, 0}; \ |
| 141 | ASN1_CHOICE(tname) |
| 142 |
| 143 | #define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname) |
| 144 |
| 145 | #define ASN1_CHOICE_END_name(stname, tname) \ |
| 146 | ASN1_CHOICE_END_selector(stname, tname, type) |
| 147 |
| 148 | #define ASN1_CHOICE_END_selector(stname, tname, selname) \ |
| 149 | ; \ |
| 150 | ASN1_ITEM_start(tname) ASN1_ITYPE_CHOICE, offsetof(stname, selname), \ |
| 151 | tname##_ch_tt, sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE), NULL, \ |
| 152 | sizeof(stname), #stname ASN1_ITEM_end(tname) |
| 153 |
| 154 | #define ASN1_CHOICE_END_cb(stname, tname, selname) \ |
| 155 | ; \ |
| 156 | ASN1_ITEM_start(tname) ASN1_ITYPE_CHOICE, offsetof(stname, selname), \ |
| 157 | tname##_ch_tt, sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE), \ |
| 158 | &tname##_aux, sizeof(stname), #stname ASN1_ITEM_end(tname) |
| 159 |
| 160 | /* This helps with the template wrapper form of ASN1_ITEM */ |
| 161 |
| 162 | #define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) \ |
| 163 | { (flags), (tag), 0, #name, ASN1_ITEM_ref(type) } |
| 164 |
| 165 | /* These help with SEQUENCE or CHOICE components */ |
| 166 |
| 167 | /* used to declare other types */ |
| 168 |
| 169 | #define ASN1_EX_TYPE(flags, tag, stname, field, type) \ |
| 170 | { (flags), (tag), offsetof(stname, field), #field, ASN1_ITEM_ref(type) } |
| 171 |
| 172 | /* implicit and explicit helper macros */ |
| 173 |
| 174 | #define ASN1_IMP_EX(stname, field, type, tag, ex) \ |
| 175 | ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type) |
| 176 |
| 177 | #define ASN1_EXP_EX(stname, field, type, tag, ex) \ |
| 178 | ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type) |
| 179 |
| 180 | /* Any defined by macros: the field used is in the table itself */ |
| 181 |
| 182 | #define ASN1_ADB_OBJECT(tblname) \ |
| 183 | { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) } |
| 184 | /* Plain simple type */ |
| 185 | #define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0, 0, stname, field, type) |
| 186 |
| 187 | /* OPTIONAL simple type */ |
| 188 | #define ASN1_OPT(stname, field, type) \ |
| 189 | ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type) |
| 190 |
| 191 | /* IMPLICIT tagged simple type */ |
| 192 | #define ASN1_IMP(stname, field, type, tag) \ |
| 193 | ASN1_IMP_EX(stname, field, type, tag, 0) |
| 194 |
| 195 | /* IMPLICIT tagged OPTIONAL simple type */ |
| 196 | #define ASN1_IMP_OPT(stname, field, type, tag) \ |
| 197 | ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL) |
| 198 |
| 199 | /* Same as above but EXPLICIT */ |
| 200 |
| 201 | #define ASN1_EXP(stname, field, type, tag) \ |
| 202 | ASN1_EXP_EX(stname, field, type, tag, 0) |
| 203 | #define ASN1_EXP_OPT(stname, field, type, tag) \ |
| 204 | ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL) |
| 205 |
| 206 | /* SEQUENCE OF type */ |
| 207 | #define ASN1_SEQUENCE_OF(stname, field, type) \ |
| 208 | ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type) |
| 209 |
| 210 | /* OPTIONAL SEQUENCE OF */ |
| 211 | #define ASN1_SEQUENCE_OF_OPT(stname, field, type) \ |
| 212 | ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_OPTIONAL, 0, stname, field, \ |
| 213 | type) |
| 214 |
| 215 | /* Same as above but for SET OF */ |
| 216 |
| 217 | #define ASN1_SET_OF(stname, field, type) \ |
| 218 | ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type) |
| 219 |
| 220 | #define ASN1_SET_OF_OPT(stname, field, type) \ |
| 221 | ASN1_EX_TYPE(ASN1_TFLG_SET_OF | ASN1_TFLG_OPTIONAL, 0, stname, field, type) |
| 222 |
| 223 | /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */ |
| 224 |
| 225 | #define ASN1_IMP_SET_OF(stname, field, type, tag) \ |
| 226 | ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF) |
| 227 |
| 228 | #define ASN1_EXP_SET_OF(stname, field, type, tag) \ |
| 229 | ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF) |
| 230 |
| 231 | #define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \ |
| 232 | ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF | ASN1_TFLG_OPTIONAL) |
| 233 |
| 234 | #define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \ |
| 235 | ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF | ASN1_TFLG_OPTIONAL) |
| 236 |
| 237 | #define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \ |
| 238 | ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF) |
| 239 |
| 240 | #define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \ |
| 241 | ASN1_IMP_EX(stname, field, type, tag, \ |
| 242 | ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_OPTIONAL) |
| 243 |
| 244 | #define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \ |
| 245 | ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF) |
| 246 |
| 247 | #define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \ |
| 248 | ASN1_EXP_EX(stname, field, type, tag, \ |
| 249 | ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_OPTIONAL) |
| 250 |
| 251 | /* Macros for the ASN1_ADB structure */ |
| 252 |
| 253 | #define ASN1_ADB(name) static const ASN1_ADB_TABLE name##_adbtbl[] |
| 254 |
| 255 | #define ASN1_ADB_END(name, flags, field, app_table, def, none) \ |
| 256 | ; \ |
| 257 | static const ASN1_ADB name##_adb = { \ |
| 258 | flags, \ |
| 259 | offsetof(name, field), \ |
| 260 | app_table, \ |
| 261 | name##_adbtbl, \ |
| 262 | sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE), \ |
| 263 | def, \ |
| 264 | none} |
| 265 |
| 266 | #define ADB_ENTRY(val, template) \ |
| 267 | { val, template } |
| 268 |
| 269 | #define ASN1_ADB_TEMPLATE(name) static const ASN1_TEMPLATE name##_tt |
| 270 |
| 271 | /* This is the ASN1 template structure that defines |
| 272 | * a wrapper round the actual type. It determines the |
| 273 | * actual position of the field in the value structure, |
| 274 | * various flags such as OPTIONAL and the field name. |
| 275 | */ |
| 276 |
| 277 | struct ASN1_TEMPLATE_st { |
| 278 | uint32_t flags; /* Various flags */ |
| 279 | int tag; /* tag, not used if no tagging */ |
| 280 | unsigned long offset; /* Offset of this field in structure */ |
| 281 | const char *field_name; /* Field name */ |
| 282 | ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */ |
| 283 | }; |
| 284 |
| 285 | /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */ |
| 286 |
| 287 | #define ASN1_TEMPLATE_item(t) (t->item_ptr) |
| 288 | #define ASN1_TEMPLATE_adb(t) (t->item_ptr) |
| 289 |
| 290 | typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE; |
| 291 | typedef struct ASN1_ADB_st ASN1_ADB; |
| 292 |
| 293 | typedef struct asn1_must_be_null_st ASN1_MUST_BE_NULL; |
| 294 |
| 295 | struct ASN1_ADB_st { |
| 296 | uint32_t flags; /* Various flags */ |
| 297 | unsigned long offset; /* Offset of selector field */ |
| 298 | ASN1_MUST_BE_NULL *unused; |
| 299 | const ASN1_ADB_TABLE *tbl; /* Table of possible types */ |
| 300 | long tblcount; /* Number of entries in tbl */ |
| 301 | const ASN1_TEMPLATE *default_tt; /* Type to use if no match */ |
| 302 | const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */ |
| 303 | }; |
| 304 |
| 305 | struct ASN1_ADB_TABLE_st { |
| 306 | int value; /* NID for an object */ |
| 307 | const ASN1_TEMPLATE tt; /* item for this value */ |
| 308 | }; |
| 309 |
| 310 | /* template flags */ |
| 311 |
| 312 | /* Field is optional */ |
| 313 | #define ASN1_TFLG_OPTIONAL (0x1) |
| 314 |
| 315 | /* Field is a SET OF */ |
| 316 | #define ASN1_TFLG_SET_OF (0x1 << 1) |
| 317 |
| 318 | /* Field is a SEQUENCE OF */ |
| 319 | #define ASN1_TFLG_SEQUENCE_OF (0x2 << 1) |
| 320 |
| 321 | /* Mask for SET OF or SEQUENCE OF */ |
| 322 | #define ASN1_TFLG_SK_MASK (0x3 << 1) |
| 323 |
| 324 | /* These flags mean the tag should be taken from the |
| 325 | * tag field. If EXPLICIT then the underlying type |
| 326 | * is used for the inner tag. |
| 327 | */ |
| 328 |
| 329 | /* IMPLICIT tagging */ |
| 330 | #define ASN1_TFLG_IMPTAG (0x1 << 3) |
| 331 |
| 332 |
| 333 | /* EXPLICIT tagging, inner tag from underlying type */ |
| 334 | #define ASN1_TFLG_EXPTAG (0x2 << 3) |
| 335 |
| 336 | #define ASN1_TFLG_TAG_MASK (0x3 << 3) |
| 337 |
| 338 | /* context specific IMPLICIT */ |
| 339 | #define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG | ASN1_TFLG_CONTEXT |
| 340 |
| 341 | /* context specific EXPLICIT */ |
| 342 | #define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG | ASN1_TFLG_CONTEXT |
| 343 |
| 344 | /* If tagging is in force these determine the |
| 345 | * type of tag to use. Otherwise the tag is |
| 346 | * determined by the underlying type. These |
| 347 | * values reflect the actual octet format. |
| 348 | */ |
| 349 |
| 350 | /* Universal tag */ |
| 351 | #define ASN1_TFLG_UNIVERSAL (0x0 << 6) |
| 352 | /* Application tag */ |
| 353 | #define ASN1_TFLG_APPLICATION (0x1 << 6) |
| 354 | /* Context specific tag */ |
| 355 | #define ASN1_TFLG_CONTEXT (0x2 << 6) |
| 356 | /* Private tag */ |
| 357 | #define ASN1_TFLG_PRIVATE (0x3 << 6) |
| 358 |
| 359 | #define ASN1_TFLG_TAG_CLASS (0x3 << 6) |
| 360 |
| 361 | /* These are for ANY DEFINED BY type. In this case |
| 362 | * the 'item' field points to an ASN1_ADB structure |
| 363 | * which contains a table of values to decode the |
| 364 | * relevant type |
| 365 | */ |
| 366 |
| 367 | #define ASN1_TFLG_ADB_MASK (0x3 << 8) |
| 368 |
| 369 | #define ASN1_TFLG_ADB_OID (0x1 << 8) |
| 370 |
| 371 | /* This is the actual ASN1 item itself */ |
| 372 |
| 373 | struct ASN1_ITEM_st { |
| 374 | char itype; /* The item type, primitive, SEQUENCE, CHOICE or extern */ |
| 375 | int utype; /* underlying type */ |
| 376 | const ASN1_TEMPLATE |
| 377 | *templates; /* If SEQUENCE or CHOICE this contains the contents */ |
| 378 | long tcount; /* Number of templates if SEQUENCE or CHOICE */ |
| 379 | const void *funcs; /* functions that handle this type */ |
| 380 | long size; /* Structure size (usually)*/ |
| 381 | const char *sname; /* Structure name */ |
| 382 | }; |
| 383 |
| 384 | /* These are values for the itype field and |
| 385 | * determine how the type is interpreted. |
| 386 | * |
| 387 | * For PRIMITIVE types the underlying type |
| 388 | * determines the behaviour if items is NULL. |
| 389 | * |
| 390 | * Otherwise templates must contain a single |
| 391 | * template and the type is treated in the |
| 392 | * same way as the type specified in the template. |
| 393 | * |
| 394 | * For SEQUENCE types the templates field points |
| 395 | * to the members, the size field is the |
| 396 | * structure size. |
| 397 | * |
| 398 | * For CHOICE types the templates field points |
| 399 | * to each possible member (typically a union) |
| 400 | * and the 'size' field is the offset of the |
| 401 | * selector. |
| 402 | * |
| 403 | * The 'funcs' field is used for application |
| 404 | * specific functions. |
| 405 | * |
| 406 | * The EXTERN type uses a new style d2i/i2d. |
| 407 | * The new style should be used where possible |
| 408 | * because it avoids things like the d2i IMPLICIT |
| 409 | * hack. |
| 410 | * |
| 411 | * MSTRING is a multiple string type, it is used |
| 412 | * for a CHOICE of character strings where the |
| 413 | * actual strings all occupy an ASN1_STRING |
| 414 | * structure. In this case the 'utype' field |
| 415 | * has a special meaning, it is used as a mask |
| 416 | * of acceptable types using the B_ASN1 constants. |
| 417 | * |
| 418 | */ |
| 419 |
| 420 | #define ASN1_ITYPE_PRIMITIVE 0x0 |
| 421 |
| 422 | #define ASN1_ITYPE_SEQUENCE 0x1 |
| 423 |
| 424 | #define ASN1_ITYPE_CHOICE 0x2 |
| 425 |
| 426 | #define ASN1_ITYPE_EXTERN 0x4 |
| 427 |
| 428 | #define ASN1_ITYPE_MSTRING 0x5 |
| 429 |
| 430 | /* Deprecated tag and length cache */ |
| 431 | struct ASN1_TLC_st; |
| 432 |
| 433 | /* This is the ASN1_AUX structure: it handles various |
| 434 | * miscellaneous requirements. For example the use of |
| 435 | * reference counts and an informational callback. |
| 436 | * |
| 437 | * The "informational callback" is called at various |
| 438 | * points during the ASN1 encoding and decoding. It can |
| 439 | * be used to provide minor customisation of the structures |
| 440 | * used. This is most useful where the supplied routines |
| 441 | * *almost* do the right thing but need some extra help |
| 442 | * at a few points. If the callback returns zero then |
| 443 | * it is assumed a fatal error has occurred and the |
| 444 | * main operation should be abandoned. |
| 445 | * |
| 446 | * If major changes in the default behaviour are required |
| 447 | * then an external type is more appropriate. |
| 448 | */ |
| 449 |
| 450 | typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it, |
| 451 | void *exarg); |
| 452 |
| 453 | typedef struct ASN1_AUX_st { |
| 454 | void *app_data; |
| 455 | uint32_t flags; |
| 456 | int ref_offset; /* Offset of reference value */ |
| 457 | ASN1_aux_cb *asn1_cb; |
| 458 | int enc_offset; /* Offset of ASN1_ENCODING structure */ |
| 459 | } ASN1_AUX; |
| 460 |
| 461 | /* Flags in ASN1_AUX */ |
| 462 |
| 463 | /* Use a reference count */ |
| 464 | #define ASN1_AFLG_REFCOUNT 1 |
| 465 | /* Save the encoding of structure (useful for signatures) */ |
| 466 | #define ASN1_AFLG_ENCODING 2 |
| 467 |
| 468 | /* operation values for asn1_cb */ |
| 469 |
| 470 | #define ASN1_OP_NEW_PRE 0 |
| 471 | #define ASN1_OP_NEW_POST 1 |
| 472 | #define ASN1_OP_FREE_PRE 2 |
| 473 | #define ASN1_OP_FREE_POST 3 |
| 474 | #define ASN1_OP_D2I_PRE 4 |
| 475 | #define ASN1_OP_D2I_POST 5 |
| 476 | /* ASN1_OP_I2D_PRE and ASN1_OP_I2D_POST are not supported. We leave the |
| 477 | * constants undefined so code relying on them does not accidentally compile. */ |
| 478 | #define ASN1_OP_PRINT_PRE 8 |
| 479 | #define ASN1_OP_PRINT_POST 9 |
| 480 | #define ASN1_OP_STREAM_PRE 10 |
| 481 | #define ASN1_OP_STREAM_POST 11 |
| 482 | #define ASN1_OP_DETACHED_PRE 12 |
| 483 | #define ASN1_OP_DETACHED_POST 13 |
| 484 |
| 485 | /* Macro to implement a primitive type */ |
| 486 | #define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0) |
| 487 | #define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \ |
| 488 | ASN1_ITEM_start(itname) ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, \ |
| 489 | #itname ASN1_ITEM_end(itname) |
| 490 |
| 491 | /* Macro to implement a multi string type */ |
| 492 | #define IMPLEMENT_ASN1_MSTRING(itname, mask) \ |
| 493 | ASN1_ITEM_start(itname) ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, \ |
| 494 | sizeof(ASN1_STRING), #itname ASN1_ITEM_end(itname) |
| 495 |
| 496 | #define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \ |
| 497 | ASN1_ITEM_start(sname) ASN1_ITYPE_EXTERN, tag, NULL, 0, &fptrs, 0, \ |
| 498 | #sname ASN1_ITEM_end(sname) |
| 499 |
| 500 | /* Macro to implement standard functions in terms of ASN1_ITEM structures */ |
| 501 |
| 502 | #define IMPLEMENT_ASN1_FUNCTIONS(stname) \ |
| 503 | IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname) |
| 504 |
| 505 | #define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) \ |
| 506 | IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname) |
| 507 |
| 508 | #define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \ |
| 509 | IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname) |
| 510 |
| 511 | #define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \ |
| 512 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname) |
| 513 |
| 514 | #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \ |
| 515 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname) |
| 516 |
| 517 | #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \ |
| 518 | pre stname *fname##_new(void) { \ |
| 519 | return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \ |
| 520 | } \ |
| 521 | pre void fname##_free(stname *a) { \ |
| 522 | ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \ |
| 523 | } |
| 524 |
| 525 | #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \ |
| 526 | stname *fname##_new(void) { \ |
| 527 | return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \ |
| 528 | } \ |
| 529 | void fname##_free(stname *a) { \ |
| 530 | ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \ |
| 531 | } |
| 532 |
| 533 | #define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \ |
| 534 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \ |
| 535 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) |
| 536 |
| 537 | #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \ |
| 538 | stname *d2i_##fname(stname **a, const unsigned char **in, long len) { \ |
| 539 | return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, \ |
| 540 | ASN1_ITEM_rptr(itname)); \ |
| 541 | } \ |
| 542 | int i2d_##fname(stname *a, unsigned char **out) { \ |
| 543 | return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname)); \ |
| 544 | } |
| 545 |
| 546 | /* This includes evil casts to remove const: they will go away when full |
| 547 | * ASN1 constification is done. |
| 548 | */ |
| 549 | #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \ |
| 550 | stname *d2i_##fname(stname **a, const unsigned char **in, long len) { \ |
| 551 | return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, \ |
| 552 | ASN1_ITEM_rptr(itname)); \ |
| 553 | } \ |
| 554 | int i2d_##fname(const stname *a, unsigned char **out) { \ |
| 555 | return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname)); \ |
| 556 | } |
| 557 |
| 558 | #define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \ |
| 559 | stname *stname##_dup(stname *x) { \ |
| 560 | return (stname *)ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \ |
| 561 | } |
| 562 |
| 563 | #define IMPLEMENT_ASN1_DUP_FUNCTION_const(stname) \ |
| 564 | stname *stname##_dup(const stname *x) { \ |
| 565 | return (stname *)ASN1_item_dup(ASN1_ITEM_rptr(stname), (void *)x); \ |
| 566 | } |
| 567 |
| 568 | #define IMPLEMENT_ASN1_FUNCTIONS_const(name) \ |
| 569 | IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name) |
| 570 |
| 571 | #define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \ |
| 572 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \ |
| 573 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) |
| 574 |
| 575 | /* external definitions for primitive types */ |
| 576 |
| 577 | DECLARE_ASN1_ITEM(ASN1_SEQUENCE) |
| 578 |
| 579 | DEFINE_STACK_OF(ASN1_VALUE) |
| 580 |
| 581 |
| 582 | #if defined(__cplusplus) |
| 583 | } // extern "C" |
| 584 | #endif |
| 585 |
| 586 | #endif // OPENSSL_HEADER_ASN1T_H |
| 587 |