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