openssl fipsinstall: fix cosmetic wart
[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 /*-
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
490 struct 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
503 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
504 typedef struct ASN1_ADB_st ASN1_ADB;
505
506 struct 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
516 struct 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
602 struct 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
620 struct 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 */
630 typedef 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
634 typedef int ASN1_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
635                         const ASN1_ITEM *it, int tag, int aclass);
636 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
637 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
638
639 typedef int ASN1_ex_print_func(BIO *out, const ASN1_VALUE **pval,
640                                int indent, const char *fname,
641                                const ASN1_PCTX *pctx);
642
643 typedef int ASN1_primitive_i2c(const ASN1_VALUE **pval, unsigned char *cont,
644                                int *putype, const ASN1_ITEM *it);
645 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont,
646                                int len, int utype, char *free_cont,
647                                const ASN1_ITEM *it);
648 typedef int ASN1_primitive_print(BIO *out, const ASN1_VALUE **pval,
649                                  const ASN1_ITEM *it, int indent,
650                                  const ASN1_PCTX *pctx);
651
652 typedef struct ASN1_EXTERN_FUNCS_st {
653     void *app_data;
654     ASN1_ex_new_func *asn1_ex_new;
655     ASN1_ex_free_func *asn1_ex_free;
656     ASN1_ex_free_func *asn1_ex_clear;
657     ASN1_ex_d2i *asn1_ex_d2i;
658     ASN1_ex_i2d *asn1_ex_i2d;
659     ASN1_ex_print_func *asn1_ex_print;
660 } ASN1_EXTERN_FUNCS;
661
662 typedef struct ASN1_PRIMITIVE_FUNCS_st {
663     void *app_data;
664     unsigned long flags;
665     ASN1_ex_new_func *prim_new;
666     ASN1_ex_free_func *prim_free;
667     ASN1_ex_free_func *prim_clear;
668     ASN1_primitive_c2i *prim_c2i;
669     ASN1_primitive_i2c *prim_i2c;
670     ASN1_primitive_print *prim_print;
671 } ASN1_PRIMITIVE_FUNCS;
672
673 /*
674  * This is the ASN1_AUX structure: it handles various miscellaneous
675  * requirements. For example the use of reference counts and an informational
676  * callback. The "informational callback" is called at various points during
677  * the ASN1 encoding and decoding. It can be used to provide minor
678  * customisation of the structures used. This is most useful where the
679  * supplied routines *almost* do the right thing but need some extra help at
680  * a few points. If the callback returns zero then it is assumed a fatal
681  * error has occurred and the main operation should be abandoned. If major
682  * changes in the default behaviour are required then an external type is
683  * more appropriate.
684  * For the operations ASN1_OP_I2D_PRE, ASN1_OP_I2D_POST, ASN1_OP_PRINT_PRE, and
685  * ASN1_OP_PRINT_POST, meanwhile a variant of the callback with const parameter
686  * 'in' is provided to make clear statically that its input is not modified. If
687  * and only if this variant is in use the flag ASN1_AFLG_CONST_CB must be set.
688  */
689
690 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
691                         void *exarg);
692 typedef int ASN1_aux_const_cb(int operation, const ASN1_VALUE **in,
693                               const ASN1_ITEM *it, void *exarg);
694
695 typedef struct ASN1_AUX_st {
696     void *app_data;
697     int flags;
698     int ref_offset;             /* Offset of reference value */
699     int ref_lock;               /* Lock type to use */
700     ASN1_aux_cb *asn1_cb;
701     int enc_offset;             /* Offset of ASN1_ENCODING structure */
702     ASN1_aux_const_cb *asn1_const_cb; /* for ASN1_OP_I2D_ and ASN1_OP_PRINT_ */
703 } ASN1_AUX;
704
705 /* For print related callbacks exarg points to this structure */
706 typedef struct ASN1_PRINT_ARG_st {
707     BIO *out;
708     int indent;
709     const ASN1_PCTX *pctx;
710 } ASN1_PRINT_ARG;
711
712 /* For streaming related callbacks exarg points to this structure */
713 typedef struct ASN1_STREAM_ARG_st {
714     /* BIO to stream through */
715     BIO *out;
716     /* BIO with filters appended */
717     BIO *ndef_bio;
718     /* Streaming I/O boundary */
719     unsigned char **boundary;
720 } ASN1_STREAM_ARG;
721
722 /* Flags in ASN1_AUX */
723
724 /* Use a reference count */
725 # define ASN1_AFLG_REFCOUNT      1
726 /* Save the encoding of structure (useful for signatures) */
727 # define ASN1_AFLG_ENCODING      2
728 /* The Sequence length is invalid */
729 # define ASN1_AFLG_BROKEN        4
730 /* Use the new asn1_const_cb */
731 # define ASN1_AFLG_CONST_CB      8
732
733 /* operation values for asn1_cb */
734
735 # define ASN1_OP_NEW_PRE         0
736 # define ASN1_OP_NEW_POST        1
737 # define ASN1_OP_FREE_PRE        2
738 # define ASN1_OP_FREE_POST       3
739 # define ASN1_OP_D2I_PRE         4
740 # define ASN1_OP_D2I_POST        5
741 # define ASN1_OP_I2D_PRE         6
742 # define ASN1_OP_I2D_POST        7
743 # define ASN1_OP_PRINT_PRE       8
744 # define ASN1_OP_PRINT_POST      9
745 # define ASN1_OP_STREAM_PRE      10
746 # define ASN1_OP_STREAM_POST     11
747 # define ASN1_OP_DETACHED_PRE    12
748 # define ASN1_OP_DETACHED_POST   13
749 # define ASN1_OP_DUP_PRE         14
750 # define ASN1_OP_DUP_POST        15
751
752 /* Macro to implement a primitive type */
753 # define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
754 # define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
755                                 ASN1_ITEM_start(itname) \
756                                         ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
757                                 ASN1_ITEM_end(itname)
758
759 /* Macro to implement a multi string type */
760 # define IMPLEMENT_ASN1_MSTRING(itname, mask) \
761                                 ASN1_ITEM_start(itname) \
762                                         ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
763                                 ASN1_ITEM_end(itname)
764
765 # define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
766         ASN1_ITEM_start(sname) \
767                 ASN1_ITYPE_EXTERN, \
768                 tag, \
769                 NULL, \
770                 0, \
771                 &fptrs, \
772                 0, \
773                 #sname \
774         ASN1_ITEM_end(sname)
775
776 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
777
778 # define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
779
780 # define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
781
782 # define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
783                         IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
784
785 # define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
786                 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
787
788 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
789                 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
790
791 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
792         pre stname *fname##_new(void) \
793         { \
794                 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
795         } \
796         pre void fname##_free(stname *a) \
797         { \
798                 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
799         }
800
801 # define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
802         stname *fname##_new(void) \
803         { \
804                 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
805         } \
806         void fname##_free(stname *a) \
807         { \
808                 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
809         }
810
811 # define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
812         IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
813         IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
814
815 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
816         stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
817         { \
818                 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
819         } \
820         int i2d_##fname(const stname *a, unsigned char **out) \
821         { \
822                 return ASN1_item_i2d((const ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
823         }
824
825 # define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
826         int i2d_##stname##_NDEF(const stname *a, unsigned char **out) \
827         { \
828                 return ASN1_item_ndef_i2d((const ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
829         }
830
831 # define IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(stname) \
832         static stname *d2i_##stname(stname **a, \
833                                    const unsigned char **in, long len) \
834         { \
835                 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, \
836                                                ASN1_ITEM_rptr(stname)); \
837         } \
838         static int i2d_##stname(const stname *a, unsigned char **out) \
839         { \
840                 return ASN1_item_i2d((const ASN1_VALUE *)a, out, \
841                                      ASN1_ITEM_rptr(stname)); \
842         }
843
844 # define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
845         stname * stname##_dup(const stname *x) \
846         { \
847         return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
848         }
849
850 # define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \
851         IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname)
852
853 # define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \
854         int fname##_print_ctx(BIO *out, const stname *x, int indent, \
855                                                 const ASN1_PCTX *pctx) \
856         { \
857                 return ASN1_item_print(out, (const ASN1_VALUE *)x, indent, \
858                         ASN1_ITEM_rptr(itname), pctx); \
859         }
860
861 /* external definitions for primitive types */
862
863 DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
864 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
865 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
866 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
867 DECLARE_ASN1_ITEM(CBIGNUM)
868 DECLARE_ASN1_ITEM(BIGNUM)
869 DECLARE_ASN1_ITEM(INT32)
870 DECLARE_ASN1_ITEM(ZINT32)
871 DECLARE_ASN1_ITEM(UINT32)
872 DECLARE_ASN1_ITEM(ZUINT32)
873 DECLARE_ASN1_ITEM(INT64)
874 DECLARE_ASN1_ITEM(ZINT64)
875 DECLARE_ASN1_ITEM(UINT64)
876 DECLARE_ASN1_ITEM(ZUINT64)
877
878 # ifndef OPENSSL_NO_DEPRECATED_3_0
879 /*
880  * LONG and ZLONG are strongly discouraged for use as stored data, as the
881  * underlying C type (long) differs in size depending on the architecture.
882  * They are designed with 32-bit longs in mind.
883  */
884 DECLARE_ASN1_ITEM(LONG)
885 DECLARE_ASN1_ITEM(ZLONG)
886 # endif
887
888 {-
889     generate_stack_macros("ASN1_VALUE");
890 -}
891
892
893 /* Functions used internally by the ASN1 code */
894
895 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
896 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
897
898 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
899                      const ASN1_ITEM *it, int tag, int aclass, char opt,
900                      ASN1_TLC *ctx);
901
902 int ASN1_item_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
903                      const ASN1_ITEM *it, int tag, int aclass);
904
905 /* Legacy compatibility */
906 # define IMPLEMENT_ASN1_FUNCTIONS_const(name) IMPLEMENT_ASN1_FUNCTIONS(name)
907 # define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
908          IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname)
909
910 #ifdef  __cplusplus
911 }
912 #endif
913 #endif