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