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