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