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