]> git.ipfire.org Git - thirdparty/openssl.git/blame - include/openssl/asn1t.h.in
Move ossl_asn1_string_to_time_t() to libtestutil
[thirdparty/openssl.git] / include / openssl / asn1t.h.in
CommitLineData
0f113f3e 1/*
0b282540
MC
2 * {- join("\n * ", @autowarntext) -}
3 *
38fc02a7 4 * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
9d6b1ce6 5 *
48f4ad77 6 * Licensed under the Apache License 2.0 (the "License"). You may not use
21dcbebc
RS
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
9d6b1ce6 10 */
21dcbebc 11
0b282540
MC
12{-
13use OpenSSL::stackhash qw(generate_stack_macros);
14-}
15
ae4186b0
DMSP
16#ifndef OPENSSL_ASN1T_H
17# define OPENSSL_ASN1T_H
d86167ec
DMSP
18# pragma once
19
20# include <openssl/macros.h>
936c2b9e 21# ifndef OPENSSL_NO_DEPRECATED_3_0
d86167ec
DMSP
22# define HEADER_ASN1T_H
23# endif
9d6b1ce6 24
0f113f3e
MC
25# include <stddef.h>
26# include <openssl/e_os2.h>
27# include <openssl/asn1.h>
9d6b1ce6 28
0f113f3e
MC
29# ifdef OPENSSL_BUILD_SHLIBCRYPTO
30# undef OPENSSL_EXTERN
31# define OPENSSL_EXTERN OPENSSL_EXPORT
32# endif
26da3e65 33
9d6b1ce6
DSH
34/* ASN1 template defines, structures and functions */
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
4f7e08c8
DDO
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
bb5ea36b 89/* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
211da00b 90# define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)((iptr)()))
bb5ea36b
DSH
91
92/* Macros for start and end of ASN1_ITEM definition */
93
211da00b 94# define ASN1_ITEM_start(itname) \
0f113f3e
MC
95 const ASN1_ITEM * itname##_it(void) \
96 { \
97 static const ASN1_ITEM local_it = {
bb5ea36b 98
211da00b 99# define static_ASN1_ITEM_start(itname) \
03cbd3b8 100 static ASN1_ITEM_start(itname)
05e97f1d 101
211da00b 102# define ASN1_ITEM_end(itname) \
0f113f3e
MC
103 }; \
104 return &local_it; \
105 }
bb5ea36b 106
9d6b1ce6
DSH
107/* Macros to aid ASN1 template writing */
108
0f113f3e
MC
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)
df2ee0e2
BL
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)
9d6b1ce6
DSH
134
135/* This is a ASN1 type which just embeds a template */
0f113f3e
MC
136
137/*-
1d97c843 138 * This pair helps declare a SEQUENCE. We can do:
9d6b1ce6 139 *
0f113f3e
MC
140 * ASN1_SEQUENCE(stname) = {
141 * ... SEQUENCE components ...
142 * } ASN1_SEQUENCE_END(stname)
9d6b1ce6 143 *
0f113f3e
MC
144 * This will produce an ASN1_ITEM called stname_it
145 * for a structure called stname.
9d6b1ce6 146 *
0f113f3e
MC
147 * If you want the same structure but a different
148 * name then use:
9d6b1ce6 149 *
0f113f3e
MC
150 * ASN1_SEQUENCE(itname) = {
151 * ... SEQUENCE components ...
152 * } ASN1_SEQUENCE_END_name(stname, itname)
9d6b1ce6 153 *
0f113f3e
MC
154 * This will create an item called itname_it using
155 * a structure called stname.
9d6b1ce6
DSH
156 */
157
0f113f3e
MC
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
df2ee0e2
BL
163# define static_ASN1_SEQUENCE_END(stname) static_ASN1_SEQUENCE_END_name(stname, stname)
164
0f113f3e
MC
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),\
2171a071 174 #tname \
0f113f3e
MC
175 ASN1_ITEM_end(tname)
176
df2ee0e2
BL
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
0f113f3e
MC
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) \
9fdcc21f
DO
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}; \
0f113f3e
MC
207 ASN1_SEQUENCE(tname)
208
c001ce33 209# define ASN1_SEQUENCE_ref(tname, cb) \
9fdcc21f 210 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), offsetof(tname, lock), cb, 0, NULL}; \
0f113f3e
MC
211 ASN1_SEQUENCE(tname)
212
213# define ASN1_SEQUENCE_enc(tname, enc, cb) \
9fdcc21f 214 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc), NULL}; \
0f113f3e
MC
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)
df2ee0e2
BL
228# define static_ASN1_NDEF_SEQUENCE_END(tname) \
229 ;\
dccd20d1 230 static_ASN1_ITEM_start(tname) \
df2ee0e2
BL
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)
0f113f3e 239
0f113f3e
MC
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)
df2ee0e2 244# define static_ASN1_SEQUENCE_END_cb(stname, tname) static_ASN1_SEQUENCE_END_ref(stname, tname)
0f113f3e
MC
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),\
2171a071 255 #tname \
0f113f3e 256 ASN1_ITEM_end(tname)
df2ee0e2
BL
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)
0f113f3e
MC
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)
9d6b1ce6 280
1d97c843
TH
281/*-
282 * This pair helps declare a CHOICE type. We can do:
9d6b1ce6 283 *
0f113f3e
MC
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;
9d6b1ce6 298 *
0f113f3e
MC
299 * the name of the selector must be 'type'.
300 * to use an alternative selector name use the
9d6b1ce6
DSH
301 * ASN1_CHOICE_END_selector() version.
302 */
303
0f113f3e
MC
304# define ASN1_CHOICE(tname) \
305 static const ASN1_TEMPLATE tname##_ch_tt[]
306
307# define ASN1_CHOICE_cb(tname, cb) \
9fdcc21f 308 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0, NULL}; \
0f113f3e
MC
309 ASN1_CHOICE(tname)
310
311# define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
312
df2ee0e2
BL
313# define static_ASN1_CHOICE_END(stname) static_ASN1_CHOICE_END_name(stname, stname)
314
0f113f3e
MC
315# define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
316
df2ee0e2
BL
317# define static_ASN1_CHOICE_END_name(stname, tname) static_ASN1_CHOICE_END_selector(stname, tname, type)
318
0f113f3e
MC
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
df2ee0e2
BL
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
0f113f3e
MC
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)
9d6b1ce6
DSH
354
355/* This helps with the template wrapper form of ASN1_ITEM */
356
0f113f3e
MC
357# define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
358 (flags), (tag), 0,\
359 #name, ASN1_ITEM_ref(type) }
9d6b1ce6
DSH
360
361/* These help with SEQUENCE or CHOICE components */
362
363/* used to declare other types */
364
0f113f3e
MC
365# define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
366 (flags), (tag), offsetof(stname, field),\
367 #field, ASN1_ITEM_ref(type) }
9d6b1ce6 368
9d6b1ce6
DSH
369/* implicit and explicit helper macros */
370
0f113f3e 371# define ASN1_IMP_EX(stname, field, type, tag, ex) \
37659ea4 372 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | (ex), tag, stname, field, type)
9d6b1ce6 373
0f113f3e 374# define ASN1_EXP_EX(stname, field, type, tag, ex) \
37659ea4 375 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | (ex), tag, stname, field, type)
9d6b1ce6
DSH
376
377/* Any defined by macros: the field used is in the table itself */
378
211da00b
RS
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
9d6b1ce6 382/* Plain simple type */
0f113f3e 383# define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
de17bd5d
DSH
384/* Embedded simple type */
385# define ASN1_EMBED(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_EMBED,0, stname, field, type)
9d6b1ce6
DSH
386
387/* OPTIONAL simple type */
0f113f3e 388# define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
49005bb8 389# define ASN1_OPT_EMBED(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL|ASN1_TFLG_EMBED, 0, stname, field, type)
9d6b1ce6
DSH
390
391/* IMPLICIT tagged simple type */
0f113f3e 392# define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
49005bb8 393# define ASN1_IMP_EMBED(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_EMBED)
9d6b1ce6
DSH
394
395/* IMPLICIT tagged OPTIONAL simple type */
0f113f3e 396# define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
49005bb8 397# define ASN1_IMP_OPT_EMBED(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_EMBED)
9d6b1ce6
DSH
398
399/* Same as above but EXPLICIT */
400
0f113f3e 401# define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
49005bb8 402# define ASN1_EXP_EMBED(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_EMBED)
0f113f3e 403# define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
49005bb8 404# define ASN1_EXP_OPT_EMBED(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_EMBED)
9d6b1ce6
DSH
405
406/* SEQUENCE OF type */
0f113f3e
MC
407# define ASN1_SEQUENCE_OF(stname, field, type) \
408 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
9d6b1ce6
DSH
409
410/* OPTIONAL SEQUENCE OF */
0f113f3e
MC
411# define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
412 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
9d6b1ce6
DSH
413
414/* Same as above but for SET OF */
415
0f113f3e
MC
416# define ASN1_SET_OF(stname, field, type) \
417 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
9d6b1ce6 418
0f113f3e
MC
419# define ASN1_SET_OF_OPT(stname, field, type) \
420 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
9d6b1ce6
DSH
421
422/* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
423
0f113f3e
MC
424# define ASN1_IMP_SET_OF(stname, field, type, tag) \
425 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
9d6b1ce6 426
0f113f3e
MC
427# define ASN1_EXP_SET_OF(stname, field, type, tag) \
428 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
9d6b1ce6 429
0f113f3e
MC
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)
9d6b1ce6 432
0f113f3e
MC
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)
9d6b1ce6 435
0f113f3e
MC
436# define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
437 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
9d6b1ce6 438
0f113f3e
MC
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)
9d6b1ce6 441
0f113f3e
MC
442# define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
443 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
9d6b1ce6 444
0f113f3e
MC
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)
9d6b1ce6 447
f8492ffe 448/* EXPLICIT using indefinite length constructed form */
0f113f3e
MC
449# define ASN1_NDEF_EXP(stname, field, type, tag) \
450 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF)
f8492ffe 451
230fd6b7 452/* EXPLICIT OPTIONAL using indefinite length constructed form */
0f113f3e
MC
453# define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
454 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
230fd6b7 455
9d6b1ce6
DSH
456/* Macros for the ASN1_ADB structure */
457
0f113f3e
MC
458# define ASN1_ADB(name) \
459 static const ASN1_ADB_TABLE name##_adbtbl[]
460
211da00b 461# define ASN1_ADB_END(name, flags, field, adb_cb, def, none) \
0f113f3e
MC
462 ;\
463 static const ASN1_ITEM *name##_adb(void) \
464 { \
465 static const ASN1_ADB internal_adb = \
466 {\
467 flags,\
468 offsetof(name, field),\
5b70372d 469 adb_cb,\
0f113f3e
MC
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
0f113f3e
MC
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.
9d6b1ce6
DSH
488 */
489
490struct ASN1_TEMPLATE_st {
0f113f3e
MC
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 */
0f113f3e 494 const char *field_name; /* Field name */
0f113f3e 495 ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */
9d6b1ce6
DSH
496};
497
bb5ea36b
DSH
498/* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
499
0f113f3e
MC
500# define ASN1_TEMPLATE_item(t) (t->item_ptr)
501# define ASN1_TEMPLATE_adb(t) (t->item_ptr)
9d6b1ce6
DSH
502
503typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
504typedef struct ASN1_ADB_st ASN1_ADB;
505
506struct ASN1_ADB_st {
0f113f3e
MC
507 unsigned long flags; /* Various flags */
508 unsigned long offset; /* Offset of selector field */
5b70372d 509 int (*adb_cb)(long *psel); /* Application callback */
0f113f3e
MC
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 */
9d6b1ce6
DSH
514};
515
516struct ASN1_ADB_TABLE_st {
0f113f3e
MC
517 long value; /* NID for an object or value for an int */
518 const ASN1_TEMPLATE tt; /* item for this value */
9d6b1ce6
DSH
519};
520
521/* template flags */
522
523/* Field is optional */
0f113f3e 524# define ASN1_TFLG_OPTIONAL (0x1)
9d6b1ce6
DSH
525
526/* Field is a SET OF */
0f113f3e 527# define ASN1_TFLG_SET_OF (0x1 << 1)
9d6b1ce6
DSH
528
529/* Field is a SEQUENCE OF */
0f113f3e 530# define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
9d6b1ce6 531
0f113f3e
MC
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.
06db4253 536 */
0f113f3e 537# define ASN1_TFLG_SET_ORDER (0x3 << 1)
06db4253
DSH
538
539/* Mask for SET OF or SEQUENCE OF */
0f113f3e 540# define ASN1_TFLG_SK_MASK (0x3 << 1)
9d6b1ce6 541
0f113f3e
MC
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.
9d6b1ce6
DSH
545 */
546
547/* IMPLICIT tagging */
0f113f3e 548# define ASN1_TFLG_IMPTAG (0x1 << 3)
9d6b1ce6
DSH
549
550/* EXPLICIT tagging, inner tag from underlying type */
0f113f3e 551# define ASN1_TFLG_EXPTAG (0x2 << 3)
9d6b1ce6 552
0f113f3e 553# define ASN1_TFLG_TAG_MASK (0x3 << 3)
9d6b1ce6
DSH
554
555/* context specific IMPLICIT */
37659ea4 556# define ASN1_TFLG_IMPLICIT (ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT)
9d6b1ce6
DSH
557
558/* context specific EXPLICIT */
37659ea4 559# define ASN1_TFLG_EXPLICIT (ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT)
9d6b1ce6 560
0f113f3e
MC
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.
9d6b1ce6
DSH
565 */
566
0f113f3e
MC
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)
9d6b1ce6 575
0f113f3e 576# define ASN1_TFLG_TAG_CLASS (0x3<<6)
9d6b1ce6 577
0f113f3e
MC
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
9d6b1ce6
DSH
581 * relevant type
582 */
583
0f113f3e 584# define ASN1_TFLG_ADB_MASK (0x3<<8)
9d6b1ce6 585
0f113f3e 586# define ASN1_TFLG_ADB_OID (0x1<<8)
9d6b1ce6 587
0f113f3e 588# define ASN1_TFLG_ADB_INT (0x1<<9)
9d6b1ce6 589
0f113f3e
MC
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.
230fd6b7
DSH
593 */
594
0f113f3e 595# define ASN1_TFLG_NDEF (0x1<<11)
230fd6b7 596
de17bd5d
DSH
597/* Field is embedded and not a pointer */
598# define ASN1_TFLG_EMBED (0x1 << 12)
599
9d6b1ce6
DSH
600/* This is the actual ASN1 item itself */
601
602struct ASN1_ITEM_st {
0f113f3e
MC
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 */
4f7e08c8
DDO
609 const void *funcs; /* further data and type-specific functions */
610 /* funcs can be ASN1_PRIMITIVE_FUNCS*, ASN1_EXTERN_FUNCS*, or ASN1_AUX* */
0f113f3e 611 long size; /* Structure size (usually) */
0f113f3e 612 const char *sname; /* Structure name */
9d6b1ce6
DSH
613};
614
0f113f3e
MC
615/*
616 * Cache for ASN1 tag and length, so we don't keep re-reading it for things
9d6b1ce6
DSH
617 * like CHOICE
618 */
619
0f113f3e
MC
620struct 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 */
9d6b1ce6
DSH
627};
628
629/* Typedefs for ASN1 function pointers */
0f113f3e
MC
630typedef 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);
9d6b1ce6 633
dea2878f
MC
634typedef int ASN1_ex_d2i_ex(ASN1_VALUE **pval, const unsigned char **in, long len,
635 const ASN1_ITEM *it, int tag, int aclass, char opt,
636 ASN1_TLC *ctx, OSSL_LIB_CTX *libctx,
637 const char *propq);
9fdcc21f 638typedef int ASN1_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
0f113f3e 639 const ASN1_ITEM *it, int tag, int aclass);
9d6b1ce6 640typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
c8a9af97
MC
641typedef int ASN1_ex_new_ex_func(ASN1_VALUE **pval, const ASN1_ITEM *it,
642 OSSL_LIB_CTX *libctx, const char *propq);
9d6b1ce6
DSH
643typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
644
9fdcc21f 645typedef int ASN1_ex_print_func(BIO *out, const ASN1_VALUE **pval,
0f113f3e
MC
646 int indent, const char *fname,
647 const ASN1_PCTX *pctx);
1ef7acfe 648
9fdcc21f 649typedef int ASN1_primitive_i2c(const ASN1_VALUE **pval, unsigned char *cont,
0f113f3e
MC
650 int *putype, const ASN1_ITEM *it);
651typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont,
652 int len, int utype, char *free_cont,
653 const ASN1_ITEM *it);
9fdcc21f 654typedef int ASN1_primitive_print(BIO *out, const ASN1_VALUE **pval,
0f113f3e
MC
655 const ASN1_ITEM *it, int indent,
656 const ASN1_PCTX *pctx);
9d6b1ce6 657
9d6b1ce6 658typedef struct ASN1_EXTERN_FUNCS_st {
0f113f3e
MC
659 void *app_data;
660 ASN1_ex_new_func *asn1_ex_new;
661 ASN1_ex_free_func *asn1_ex_free;
662 ASN1_ex_free_func *asn1_ex_clear;
663 ASN1_ex_d2i *asn1_ex_d2i;
664 ASN1_ex_i2d *asn1_ex_i2d;
665 ASN1_ex_print_func *asn1_ex_print;
c8a9af97 666 ASN1_ex_new_ex_func *asn1_ex_new_ex;
dea2878f 667 ASN1_ex_d2i_ex *asn1_ex_d2i_ex;
9d6b1ce6
DSH
668} ASN1_EXTERN_FUNCS;
669
670typedef struct ASN1_PRIMITIVE_FUNCS_st {
0f113f3e
MC
671 void *app_data;
672 unsigned long flags;
673 ASN1_ex_new_func *prim_new;
674 ASN1_ex_free_func *prim_free;
675 ASN1_ex_free_func *prim_clear;
676 ASN1_primitive_c2i *prim_c2i;
677 ASN1_primitive_i2c *prim_i2c;
678 ASN1_primitive_print *prim_print;
9d6b1ce6
DSH
679} ASN1_PRIMITIVE_FUNCS;
680
0f113f3e
MC
681/*
682 * This is the ASN1_AUX structure: it handles various miscellaneous
683 * requirements. For example the use of reference counts and an informational
684 * callback. The "informational callback" is called at various points during
685 * the ASN1 encoding and decoding. It can be used to provide minor
686 * customisation of the structures used. This is most useful where the
687 * supplied routines *almost* do the right thing but need some extra help at
688 * a few points. If the callback returns zero then it is assumed a fatal
689 * error has occurred and the main operation should be abandoned. If major
690 * changes in the default behaviour are required then an external type is
691 * more appropriate.
9fdcc21f
DO
692 * For the operations ASN1_OP_I2D_PRE, ASN1_OP_I2D_POST, ASN1_OP_PRINT_PRE, and
693 * ASN1_OP_PRINT_POST, meanwhile a variant of the callback with const parameter
694 * 'in' is provided to make clear statically that its input is not modified. If
695 * and only if this variant is in use the flag ASN1_AFLG_CONST_CB must be set.
9d6b1ce6
DSH
696 */
697
24484759 698typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
0f113f3e 699 void *exarg);
9fdcc21f
DO
700typedef int ASN1_aux_const_cb(int operation, const ASN1_VALUE **in,
701 const ASN1_ITEM *it, void *exarg);
9d6b1ce6
DSH
702
703typedef struct ASN1_AUX_st {
0f113f3e
MC
704 void *app_data;
705 int flags;
706 int ref_offset; /* Offset of reference value */
3d9d1ce5 707 int ref_lock; /* Offset of lock value */
0f113f3e
MC
708 ASN1_aux_cb *asn1_cb;
709 int enc_offset; /* Offset of ASN1_ENCODING structure */
9fdcc21f 710 ASN1_aux_const_cb *asn1_const_cb; /* for ASN1_OP_I2D_ and ASN1_OP_PRINT_ */
9d6b1ce6
DSH
711} ASN1_AUX;
712
24484759
DSH
713/* For print related callbacks exarg points to this structure */
714typedef struct ASN1_PRINT_ARG_st {
0f113f3e
MC
715 BIO *out;
716 int indent;
717 const ASN1_PCTX *pctx;
24484759
DSH
718} ASN1_PRINT_ARG;
719
8931b30d
DSH
720/* For streaming related callbacks exarg points to this structure */
721typedef struct ASN1_STREAM_ARG_st {
0f113f3e
MC
722 /* BIO to stream through */
723 BIO *out;
724 /* BIO with filters appended */
725 BIO *ndef_bio;
726 /* Streaming I/O boundary */
727 unsigned char **boundary;
8931b30d
DSH
728} ASN1_STREAM_ARG;
729
9d6b1ce6
DSH
730/* Flags in ASN1_AUX */
731
732/* Use a reference count */
0f113f3e 733# define ASN1_AFLG_REFCOUNT 1
9d6b1ce6 734/* Save the encoding of structure (useful for signatures) */
0f113f3e 735# define ASN1_AFLG_ENCODING 2
9d6b1ce6 736/* The Sequence length is invalid */
0f113f3e 737# define ASN1_AFLG_BROKEN 4
9fdcc21f
DO
738/* Use the new asn1_const_cb */
739# define ASN1_AFLG_CONST_CB 8
9d6b1ce6
DSH
740
741/* operation values for asn1_cb */
742
0f113f3e
MC
743# define ASN1_OP_NEW_PRE 0
744# define ASN1_OP_NEW_POST 1
745# define ASN1_OP_FREE_PRE 2
746# define ASN1_OP_FREE_POST 3
747# define ASN1_OP_D2I_PRE 4
748# define ASN1_OP_D2I_POST 5
749# define ASN1_OP_I2D_PRE 6
750# define ASN1_OP_I2D_POST 7
751# define ASN1_OP_PRINT_PRE 8
752# define ASN1_OP_PRINT_POST 9
753# define ASN1_OP_STREAM_PRE 10
754# define ASN1_OP_STREAM_POST 11
755# define ASN1_OP_DETACHED_PRE 12
756# define ASN1_OP_DETACHED_POST 13
ee46dfbf
DDO
757# define ASN1_OP_DUP_PRE 14
758# define ASN1_OP_DUP_POST 15
7be04a3a
MC
759# define ASN1_OP_GET0_LIBCTX 16
760# define ASN1_OP_GET0_PROPQ 17
9d6b1ce6
DSH
761
762/* Macro to implement a primitive type */
0f113f3e
MC
763# define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
764# define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
765 ASN1_ITEM_start(itname) \
766 ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
767 ASN1_ITEM_end(itname)
9d6b1ce6
DSH
768
769/* Macro to implement a multi string type */
0f113f3e
MC
770# define IMPLEMENT_ASN1_MSTRING(itname, mask) \
771 ASN1_ITEM_start(itname) \
772 ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
773 ASN1_ITEM_end(itname)
9d6b1ce6 774
0f113f3e
MC
775# define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
776 ASN1_ITEM_start(sname) \
777 ASN1_ITYPE_EXTERN, \
778 tag, \
779 NULL, \
780 0, \
781 &fptrs, \
782 0, \
783 #sname \
784 ASN1_ITEM_end(sname)
9d6b1ce6
DSH
785
786/* Macro to implement standard functions in terms of ASN1_ITEM structures */
787
0f113f3e
MC
788# define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
789
790# define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
791
792# define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
793 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
794
795# define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
796 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
797
798# define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
799 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
800
801# define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
802 pre stname *fname##_new(void) \
803 { \
804 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
805 } \
806 pre void fname##_free(stname *a) \
807 { \
808 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
809 }
810
811# define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
812 stname *fname##_new(void) \
813 { \
814 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
815 } \
816 void fname##_free(stname *a) \
817 { \
818 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
819 }
820
821# define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
822 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
823 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
824
825# define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
826 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
827 { \
828 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
829 } \
9fdcc21f 830 int i2d_##fname(const stname *a, unsigned char **out) \
0f113f3e 831 { \
15e5b969 832 return ASN1_item_i2d((const ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
0f113f3e
MC
833 }
834
835# define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
9fdcc21f 836 int i2d_##stname##_NDEF(const stname *a, unsigned char **out) \
0f113f3e 837 { \
15e5b969 838 return ASN1_item_ndef_i2d((const ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
0f113f3e
MC
839 }
840
4fb6b0de
DSH
841# define IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(stname) \
842 static stname *d2i_##stname(stname **a, \
843 const unsigned char **in, long len) \
844 { \
845 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, \
846 ASN1_ITEM_rptr(stname)); \
847 } \
9fdcc21f 848 static int i2d_##stname(const stname *a, unsigned char **out) \
4fb6b0de 849 { \
15e5b969 850 return ASN1_item_i2d((const ASN1_VALUE *)a, out, \
4fb6b0de
DSH
851 ASN1_ITEM_rptr(stname)); \
852 }
853
0f113f3e 854# define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
9fdcc21f 855 stname * stname##_dup(const stname *x) \
1241126a
DSH
856 { \
857 return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
858 }
859
0f113f3e
MC
860# define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \
861 IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname)
9194296d 862
0f113f3e 863# define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \
9fdcc21f 864 int fname##_print_ctx(BIO *out, const stname *x, int indent, \
0f113f3e
MC
865 const ASN1_PCTX *pctx) \
866 { \
15e5b969 867 return ASN1_item_print(out, (const ASN1_VALUE *)x, indent, \
0f113f3e
MC
868 ASN1_ITEM_rptr(itname), pctx); \
869 }
9194296d 870
9d6b1ce6
DSH
871/* external definitions for primitive types */
872
bb5ea36b
DSH
873DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
874DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
875DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
bb5ea36b
DSH
876DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
877DECLARE_ASN1_ITEM(CBIGNUM)
878DECLARE_ASN1_ITEM(BIGNUM)
64f11ee8
RL
879DECLARE_ASN1_ITEM(INT32)
880DECLARE_ASN1_ITEM(ZINT32)
881DECLARE_ASN1_ITEM(UINT32)
882DECLARE_ASN1_ITEM(ZUINT32)
883DECLARE_ASN1_ITEM(INT64)
884DECLARE_ASN1_ITEM(ZINT64)
885DECLARE_ASN1_ITEM(UINT64)
886DECLARE_ASN1_ITEM(ZUINT64)
7eb4c1eb 887
936c2b9e 888# ifndef OPENSSL_NO_DEPRECATED_3_0
7eb4c1eb
RL
889/*
890 * LONG and ZLONG are strongly discouraged for use as stored data, as the
891 * underlying C type (long) differs in size depending on the architecture.
892 * They are designed with 32-bit longs in mind.
893 */
bb5ea36b
DSH
894DECLARE_ASN1_ITEM(LONG)
895DECLARE_ASN1_ITEM(ZLONG)
7eb4c1eb 896# endif
9d6b1ce6 897
0b282540
MC
898{-
899 generate_stack_macros("ASN1_VALUE");
900-}
901
9d6b1ce6
DSH
902
903/* Functions used internally by the ASN1 code */
904
905int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
906void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
9d6b1ce6 907
0f113f3e
MC
908int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
909 const ASN1_ITEM *it, int tag, int aclass, char opt,
910 ASN1_TLC *ctx);
911
9fdcc21f 912int ASN1_item_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
0f113f3e 913 const ASN1_ITEM *it, int tag, int aclass);
9d6b1ce6 914
9fdcc21f
DO
915/* Legacy compatibility */
916# define IMPLEMENT_ASN1_FUNCTIONS_const(name) IMPLEMENT_ASN1_FUNCTIONS(name)
917# define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
918 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname)
919
9d6b1ce6
DSH
920#ifdef __cplusplus
921}
922#endif
923#endif