1109998Smarkm/* asn1t.h */
2194206Ssimon/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3109998Smarkm * project 2000.
4109998Smarkm */
5109998Smarkm/* ====================================================================
6238405Sjkim * Copyright (c) 2000-2005 The OpenSSL Project.  All rights reserved.
7109998Smarkm *
8109998Smarkm * Redistribution and use in source and binary forms, with or without
9109998Smarkm * modification, are permitted provided that the following conditions
10109998Smarkm * are met:
11109998Smarkm *
12109998Smarkm * 1. Redistributions of source code must retain the above copyright
13109998Smarkm *    notice, this list of conditions and the following disclaimer.
14109998Smarkm *
15109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
16109998Smarkm *    notice, this list of conditions and the following disclaimer in
17109998Smarkm *    the documentation and/or other materials provided with the
18109998Smarkm *    distribution.
19109998Smarkm *
20109998Smarkm * 3. All advertising materials mentioning features or use of this
21109998Smarkm *    software must display the following acknowledgment:
22109998Smarkm *    "This product includes software developed by the OpenSSL Project
23109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24109998Smarkm *
25109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26109998Smarkm *    endorse or promote products derived from this software without
27109998Smarkm *    prior written permission. For written permission, please contact
28109998Smarkm *    licensing@OpenSSL.org.
29109998Smarkm *
30109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
31109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
32109998Smarkm *    permission of the OpenSSL Project.
33109998Smarkm *
34109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
35109998Smarkm *    acknowledgment:
36109998Smarkm *    "This product includes software developed by the OpenSSL Project
37109998Smarkm *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38109998Smarkm *
39109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
51109998Smarkm * ====================================================================
52109998Smarkm *
53109998Smarkm * This product includes cryptographic software written by Eric Young
54109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
55109998Smarkm * Hudson (tjh@cryptsoft.com).
56109998Smarkm *
57109998Smarkm */
58109998Smarkm#ifndef HEADER_ASN1T_H
59109998Smarkm#define HEADER_ASN1T_H
60109998Smarkm
61109998Smarkm#include <stddef.h>
62109998Smarkm#include <openssl/e_os2.h>
63109998Smarkm#include <openssl/asn1.h>
64109998Smarkm
65109998Smarkm#ifdef OPENSSL_BUILD_SHLIBCRYPTO
66109998Smarkm# undef OPENSSL_EXTERN
67109998Smarkm# define OPENSSL_EXTERN OPENSSL_EXPORT
68109998Smarkm#endif
69109998Smarkm
70109998Smarkm/* ASN1 template defines, structures and functions */
71109998Smarkm
72109998Smarkm#ifdef  __cplusplus
73109998Smarkmextern "C" {
74109998Smarkm#endif
75109998Smarkm
76109998Smarkm
77109998Smarkm#ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
78109998Smarkm
79109998Smarkm/* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
80109998Smarkm#define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
81109998Smarkm
82109998Smarkm
83109998Smarkm/* Macros for start and end of ASN1_ITEM definition */
84109998Smarkm
85109998Smarkm#define ASN1_ITEM_start(itname) \
86109998Smarkm	OPENSSL_GLOBAL const ASN1_ITEM itname##_it = {
87109998Smarkm
88109998Smarkm#define ASN1_ITEM_end(itname) \
89109998Smarkm		};
90109998Smarkm
91109998Smarkm#else
92109998Smarkm
93109998Smarkm/* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
94109998Smarkm#define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr()))
95109998Smarkm
96109998Smarkm
97109998Smarkm/* Macros for start and end of ASN1_ITEM definition */
98109998Smarkm
99109998Smarkm#define ASN1_ITEM_start(itname) \
100109998Smarkm	const ASN1_ITEM * itname##_it(void) \
101109998Smarkm	{ \
102167612Ssimon		static const ASN1_ITEM local_it = {
103109998Smarkm
104109998Smarkm#define ASN1_ITEM_end(itname) \
105109998Smarkm		}; \
106109998Smarkm	return &local_it; \
107109998Smarkm	}
108109998Smarkm
109109998Smarkm#endif
110109998Smarkm
111109998Smarkm
112109998Smarkm/* Macros to aid ASN1 template writing */
113109998Smarkm
114109998Smarkm#define ASN1_ITEM_TEMPLATE(tname) \
115160814Ssimon	static const ASN1_TEMPLATE tname##_item_tt
116109998Smarkm
117109998Smarkm#define ASN1_ITEM_TEMPLATE_END(tname) \
118109998Smarkm	;\
119109998Smarkm	ASN1_ITEM_start(tname) \
120109998Smarkm		ASN1_ITYPE_PRIMITIVE,\
121109998Smarkm		-1,\
122109998Smarkm		&tname##_item_tt,\
123109998Smarkm		0,\
124109998Smarkm		NULL,\
125109998Smarkm		0,\
126109998Smarkm		#tname \
127109998Smarkm	ASN1_ITEM_end(tname)
128109998Smarkm
129109998Smarkm
130109998Smarkm/* This is a ASN1 type which just embeds a template */
131109998Smarkm
132109998Smarkm/* This pair helps declare a SEQUENCE. We can do:
133109998Smarkm *
134109998Smarkm * 	ASN1_SEQUENCE(stname) = {
135109998Smarkm * 		... SEQUENCE components ...
136109998Smarkm * 	} ASN1_SEQUENCE_END(stname)
137109998Smarkm *
138109998Smarkm * 	This will produce an ASN1_ITEM called stname_it
139109998Smarkm *	for a structure called stname.
140109998Smarkm *
141109998Smarkm * 	If you want the same structure but a different
142109998Smarkm *	name then use:
143109998Smarkm *
144109998Smarkm * 	ASN1_SEQUENCE(itname) = {
145109998Smarkm * 		... SEQUENCE components ...
146109998Smarkm * 	} ASN1_SEQUENCE_END_name(stname, itname)
147109998Smarkm *
148109998Smarkm *	This will create an item called itname_it using
149109998Smarkm *	a structure called stname.
150109998Smarkm */
151109998Smarkm
152109998Smarkm#define ASN1_SEQUENCE(tname) \
153160814Ssimon	static const ASN1_TEMPLATE tname##_seq_tt[]
154109998Smarkm
155109998Smarkm#define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
156109998Smarkm
157109998Smarkm#define ASN1_SEQUENCE_END_name(stname, tname) \
158109998Smarkm	;\
159109998Smarkm	ASN1_ITEM_start(tname) \
160109998Smarkm		ASN1_ITYPE_SEQUENCE,\
161109998Smarkm		V_ASN1_SEQUENCE,\
162109998Smarkm		tname##_seq_tt,\
163109998Smarkm		sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
164109998Smarkm		NULL,\
165109998Smarkm		sizeof(stname),\
166109998Smarkm		#stname \
167109998Smarkm	ASN1_ITEM_end(tname)
168109998Smarkm
169160814Ssimon#define ASN1_NDEF_SEQUENCE(tname) \
170160814Ssimon	ASN1_SEQUENCE(tname)
171160814Ssimon
172194206Ssimon#define ASN1_NDEF_SEQUENCE_cb(tname, cb) \
173194206Ssimon	ASN1_SEQUENCE_cb(tname, cb)
174194206Ssimon
175109998Smarkm#define ASN1_SEQUENCE_cb(tname, cb) \
176160814Ssimon	static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
177109998Smarkm	ASN1_SEQUENCE(tname)
178109998Smarkm
179109998Smarkm#define ASN1_BROKEN_SEQUENCE(tname) \
180160814Ssimon	static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
181109998Smarkm	ASN1_SEQUENCE(tname)
182109998Smarkm
183109998Smarkm#define ASN1_SEQUENCE_ref(tname, cb, lck) \
184160814Ssimon	static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
185109998Smarkm	ASN1_SEQUENCE(tname)
186109998Smarkm
187109998Smarkm#define ASN1_SEQUENCE_enc(tname, enc, cb) \
188160814Ssimon	static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
189109998Smarkm	ASN1_SEQUENCE(tname)
190109998Smarkm
191160814Ssimon#define ASN1_NDEF_SEQUENCE_END(tname) \
192160814Ssimon	;\
193160814Ssimon	ASN1_ITEM_start(tname) \
194160814Ssimon		ASN1_ITYPE_NDEF_SEQUENCE,\
195160814Ssimon		V_ASN1_SEQUENCE,\
196160814Ssimon		tname##_seq_tt,\
197160814Ssimon		sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
198160814Ssimon		NULL,\
199160814Ssimon		sizeof(tname),\
200160814Ssimon		#tname \
201160814Ssimon	ASN1_ITEM_end(tname)
202160814Ssimon
203109998Smarkm#define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
204109998Smarkm
205109998Smarkm#define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
206109998Smarkm
207109998Smarkm#define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
208109998Smarkm
209109998Smarkm#define ASN1_SEQUENCE_END_ref(stname, tname) \
210109998Smarkm	;\
211109998Smarkm	ASN1_ITEM_start(tname) \
212109998Smarkm		ASN1_ITYPE_SEQUENCE,\
213109998Smarkm		V_ASN1_SEQUENCE,\
214109998Smarkm		tname##_seq_tt,\
215109998Smarkm		sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
216109998Smarkm		&tname##_aux,\
217109998Smarkm		sizeof(stname),\
218109998Smarkm		#stname \
219109998Smarkm	ASN1_ITEM_end(tname)
220109998Smarkm
221238405Sjkim#define ASN1_NDEF_SEQUENCE_END_cb(stname, tname) \
222238405Sjkim	;\
223238405Sjkim	ASN1_ITEM_start(tname) \
224238405Sjkim		ASN1_ITYPE_NDEF_SEQUENCE,\
225238405Sjkim		V_ASN1_SEQUENCE,\
226238405Sjkim		tname##_seq_tt,\
227238405Sjkim		sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
228238405Sjkim		&tname##_aux,\
229238405Sjkim		sizeof(stname),\
230238405Sjkim		#stname \
231238405Sjkim	ASN1_ITEM_end(tname)
232109998Smarkm
233238405Sjkim
234109998Smarkm/* This pair helps declare a CHOICE type. We can do:
235109998Smarkm *
236109998Smarkm * 	ASN1_CHOICE(chname) = {
237109998Smarkm * 		... CHOICE options ...
238109998Smarkm * 	ASN1_CHOICE_END(chname)
239109998Smarkm *
240109998Smarkm * 	This will produce an ASN1_ITEM called chname_it
241109998Smarkm *	for a structure called chname. The structure
242109998Smarkm *	definition must look like this:
243109998Smarkm *	typedef struct {
244109998Smarkm *		int type;
245109998Smarkm *		union {
246109998Smarkm *			ASN1_SOMETHING *opt1;
247109998Smarkm *			ASN1_SOMEOTHER *opt2;
248109998Smarkm *		} value;
249109998Smarkm *	} chname;
250109998Smarkm *
251109998Smarkm *	the name of the selector must be 'type'.
252109998Smarkm * 	to use an alternative selector name use the
253109998Smarkm *      ASN1_CHOICE_END_selector() version.
254109998Smarkm */
255109998Smarkm
256109998Smarkm#define ASN1_CHOICE(tname) \
257160814Ssimon	static const ASN1_TEMPLATE tname##_ch_tt[]
258109998Smarkm
259109998Smarkm#define ASN1_CHOICE_cb(tname, cb) \
260160814Ssimon	static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
261109998Smarkm	ASN1_CHOICE(tname)
262109998Smarkm
263109998Smarkm#define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
264109998Smarkm
265109998Smarkm#define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
266109998Smarkm
267109998Smarkm#define ASN1_CHOICE_END_selector(stname, tname, selname) \
268109998Smarkm	;\
269109998Smarkm	ASN1_ITEM_start(tname) \
270109998Smarkm		ASN1_ITYPE_CHOICE,\
271109998Smarkm		offsetof(stname,selname) ,\
272109998Smarkm		tname##_ch_tt,\
273109998Smarkm		sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
274109998Smarkm		NULL,\
275109998Smarkm		sizeof(stname),\
276109998Smarkm		#stname \
277109998Smarkm	ASN1_ITEM_end(tname)
278109998Smarkm
279109998Smarkm#define ASN1_CHOICE_END_cb(stname, tname, selname) \
280109998Smarkm	;\
281109998Smarkm	ASN1_ITEM_start(tname) \
282109998Smarkm		ASN1_ITYPE_CHOICE,\
283109998Smarkm		offsetof(stname,selname) ,\
284109998Smarkm		tname##_ch_tt,\
285109998Smarkm		sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
286109998Smarkm		&tname##_aux,\
287109998Smarkm		sizeof(stname),\
288109998Smarkm		#stname \
289109998Smarkm	ASN1_ITEM_end(tname)
290109998Smarkm
291109998Smarkm/* This helps with the template wrapper form of ASN1_ITEM */
292109998Smarkm
293109998Smarkm#define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
294109998Smarkm	(flags), (tag), 0,\
295109998Smarkm	#name, ASN1_ITEM_ref(type) }
296109998Smarkm
297109998Smarkm/* These help with SEQUENCE or CHOICE components */
298109998Smarkm
299109998Smarkm/* used to declare other types */
300109998Smarkm
301109998Smarkm#define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
302109998Smarkm	(flags), (tag), offsetof(stname, field),\
303109998Smarkm	#field, ASN1_ITEM_ref(type) }
304109998Smarkm
305109998Smarkm/* used when the structure is combined with the parent */
306109998Smarkm
307109998Smarkm#define ASN1_EX_COMBINE(flags, tag, type) { \
308109998Smarkm	(flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) }
309109998Smarkm
310109998Smarkm/* implicit and explicit helper macros */
311109998Smarkm
312109998Smarkm#define ASN1_IMP_EX(stname, field, type, tag, ex) \
313109998Smarkm		ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
314109998Smarkm
315109998Smarkm#define ASN1_EXP_EX(stname, field, type, tag, ex) \
316109998Smarkm		ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
317109998Smarkm
318109998Smarkm/* Any defined by macros: the field used is in the table itself */
319109998Smarkm
320109998Smarkm#ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
321109998Smarkm#define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
322109998Smarkm#define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
323109998Smarkm#else
324109998Smarkm#define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
325109998Smarkm#define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
326109998Smarkm#endif
327109998Smarkm/* Plain simple type */
328109998Smarkm#define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
329109998Smarkm
330109998Smarkm/* OPTIONAL simple type */
331109998Smarkm#define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
332109998Smarkm
333109998Smarkm/* IMPLICIT tagged simple type */
334109998Smarkm#define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
335109998Smarkm
336109998Smarkm/* IMPLICIT tagged OPTIONAL simple type */
337109998Smarkm#define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
338109998Smarkm
339109998Smarkm/* Same as above but EXPLICIT */
340109998Smarkm
341109998Smarkm#define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
342109998Smarkm#define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
343109998Smarkm
344109998Smarkm/* SEQUENCE OF type */
345109998Smarkm#define ASN1_SEQUENCE_OF(stname, field, type) \
346109998Smarkm		ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
347109998Smarkm
348109998Smarkm/* OPTIONAL SEQUENCE OF */
349109998Smarkm#define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
350109998Smarkm		ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
351109998Smarkm
352109998Smarkm/* Same as above but for SET OF */
353109998Smarkm
354109998Smarkm#define ASN1_SET_OF(stname, field, type) \
355109998Smarkm		ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
356109998Smarkm
357109998Smarkm#define ASN1_SET_OF_OPT(stname, field, type) \
358109998Smarkm		ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
359109998Smarkm
360109998Smarkm/* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
361109998Smarkm
362109998Smarkm#define ASN1_IMP_SET_OF(stname, field, type, tag) \
363109998Smarkm			ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
364109998Smarkm
365109998Smarkm#define ASN1_EXP_SET_OF(stname, field, type, tag) \
366109998Smarkm			ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
367109998Smarkm
368109998Smarkm#define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
369109998Smarkm			ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
370109998Smarkm
371109998Smarkm#define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
372109998Smarkm			ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
373109998Smarkm
374109998Smarkm#define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
375109998Smarkm			ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
376109998Smarkm
377109998Smarkm#define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
378109998Smarkm			ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
379109998Smarkm
380109998Smarkm#define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
381109998Smarkm			ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
382109998Smarkm
383109998Smarkm#define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
384109998Smarkm			ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
385109998Smarkm
386194206Ssimon/* EXPLICIT using indefinite length constructed form */
387194206Ssimon#define ASN1_NDEF_EXP(stname, field, type, tag) \
388194206Ssimon			ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF)
389194206Ssimon
390160814Ssimon/* EXPLICIT OPTIONAL using indefinite length constructed form */
391160814Ssimon#define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
392160814Ssimon			ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
393160814Ssimon
394109998Smarkm/* Macros for the ASN1_ADB structure */
395109998Smarkm
396109998Smarkm#define ASN1_ADB(name) \
397160814Ssimon	static const ASN1_ADB_TABLE name##_adbtbl[]
398109998Smarkm
399109998Smarkm#ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
400109998Smarkm
401109998Smarkm#define ASN1_ADB_END(name, flags, field, app_table, def, none) \
402109998Smarkm	;\
403160814Ssimon	static const ASN1_ADB name##_adb = {\
404109998Smarkm		flags,\
405109998Smarkm		offsetof(name, field),\
406109998Smarkm		app_table,\
407109998Smarkm		name##_adbtbl,\
408109998Smarkm		sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
409109998Smarkm		def,\
410109998Smarkm		none\
411109998Smarkm	}
412109998Smarkm
413109998Smarkm#else
414109998Smarkm
415109998Smarkm#define ASN1_ADB_END(name, flags, field, app_table, def, none) \
416109998Smarkm	;\
417160814Ssimon	static const ASN1_ITEM *name##_adb(void) \
418109998Smarkm	{ \
419160814Ssimon	static const ASN1_ADB internal_adb = \
420109998Smarkm		{\
421109998Smarkm		flags,\
422109998Smarkm		offsetof(name, field),\
423109998Smarkm		app_table,\
424109998Smarkm		name##_adbtbl,\
425109998Smarkm		sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
426109998Smarkm		def,\
427109998Smarkm		none\
428109998Smarkm		}; \
429109998Smarkm		return (const ASN1_ITEM *) &internal_adb; \
430109998Smarkm	} \
431109998Smarkm	void dummy_function(void)
432109998Smarkm
433109998Smarkm#endif
434109998Smarkm
435109998Smarkm#define ADB_ENTRY(val, template) {val, template}
436109998Smarkm
437109998Smarkm#define ASN1_ADB_TEMPLATE(name) \
438160814Ssimon	static const ASN1_TEMPLATE name##_tt
439109998Smarkm
440109998Smarkm/* This is the ASN1 template structure that defines
441109998Smarkm * a wrapper round the actual type. It determines the
442109998Smarkm * actual position of the field in the value structure,
443109998Smarkm * various flags such as OPTIONAL and the field name.
444109998Smarkm */
445109998Smarkm
446109998Smarkmstruct ASN1_TEMPLATE_st {
447109998Smarkmunsigned long flags;		/* Various flags */
448109998Smarkmlong tag;			/* tag, not used if no tagging */
449109998Smarkmunsigned long offset;		/* Offset of this field in structure */
450109998Smarkm#ifndef NO_ASN1_FIELD_NAMES
451160814Ssimonconst char *field_name;		/* Field name */
452109998Smarkm#endif
453109998SmarkmASN1_ITEM_EXP *item;		/* Relevant ASN1_ITEM or ASN1_ADB */
454109998Smarkm};
455109998Smarkm
456109998Smarkm/* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
457109998Smarkm
458109998Smarkm#define ASN1_TEMPLATE_item(t) (t->item_ptr)
459109998Smarkm#define ASN1_TEMPLATE_adb(t) (t->item_ptr)
460109998Smarkm
461109998Smarkmtypedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
462109998Smarkmtypedef struct ASN1_ADB_st ASN1_ADB;
463109998Smarkm
464109998Smarkmstruct ASN1_ADB_st {
465109998Smarkm	unsigned long flags;	/* Various flags */
466109998Smarkm	unsigned long offset;	/* Offset of selector field */
467109998Smarkm	STACK_OF(ASN1_ADB_TABLE) **app_items; /* Application defined items */
468109998Smarkm	const ASN1_ADB_TABLE *tbl;	/* Table of possible types */
469109998Smarkm	long tblcount;		/* Number of entries in tbl */
470109998Smarkm	const ASN1_TEMPLATE *default_tt;  /* Type to use if no match */
471109998Smarkm	const ASN1_TEMPLATE *null_tt;  /* Type to use if selector is NULL */
472109998Smarkm};
473109998Smarkm
474109998Smarkmstruct ASN1_ADB_TABLE_st {
475109998Smarkm	long value;		/* NID for an object or value for an int */
476109998Smarkm	const ASN1_TEMPLATE tt;		/* item for this value */
477109998Smarkm};
478109998Smarkm
479109998Smarkm/* template flags */
480109998Smarkm
481109998Smarkm/* Field is optional */
482109998Smarkm#define ASN1_TFLG_OPTIONAL	(0x1)
483109998Smarkm
484109998Smarkm/* Field is a SET OF */
485109998Smarkm#define ASN1_TFLG_SET_OF	(0x1 << 1)
486109998Smarkm
487109998Smarkm/* Field is a SEQUENCE OF */
488109998Smarkm#define ASN1_TFLG_SEQUENCE_OF	(0x2 << 1)
489109998Smarkm
490109998Smarkm/* Special case: this refers to a SET OF that
491109998Smarkm * will be sorted into DER order when encoded *and*
492109998Smarkm * the corresponding STACK will be modified to match
493109998Smarkm * the new order.
494109998Smarkm */
495109998Smarkm#define ASN1_TFLG_SET_ORDER	(0x3 << 1)
496109998Smarkm
497109998Smarkm/* Mask for SET OF or SEQUENCE OF */
498109998Smarkm#define ASN1_TFLG_SK_MASK	(0x3 << 1)
499109998Smarkm
500109998Smarkm/* These flags mean the tag should be taken from the
501109998Smarkm * tag field. If EXPLICIT then the underlying type
502109998Smarkm * is used for the inner tag.
503109998Smarkm */
504109998Smarkm
505109998Smarkm/* IMPLICIT tagging */
506109998Smarkm#define ASN1_TFLG_IMPTAG	(0x1 << 3)
507109998Smarkm
508109998Smarkm
509109998Smarkm/* EXPLICIT tagging, inner tag from underlying type */
510109998Smarkm#define ASN1_TFLG_EXPTAG	(0x2 << 3)
511109998Smarkm
512109998Smarkm#define ASN1_TFLG_TAG_MASK	(0x3 << 3)
513109998Smarkm
514109998Smarkm/* context specific IMPLICIT */
515109998Smarkm#define ASN1_TFLG_IMPLICIT	ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
516109998Smarkm
517109998Smarkm/* context specific EXPLICIT */
518109998Smarkm#define ASN1_TFLG_EXPLICIT	ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
519109998Smarkm
520109998Smarkm/* If tagging is in force these determine the
521109998Smarkm * type of tag to use. Otherwise the tag is
522109998Smarkm * determined by the underlying type. These
523109998Smarkm * values reflect the actual octet format.
524109998Smarkm */
525109998Smarkm
526109998Smarkm/* Universal tag */
527109998Smarkm#define ASN1_TFLG_UNIVERSAL	(0x0<<6)
528109998Smarkm/* Application tag */
529109998Smarkm#define ASN1_TFLG_APPLICATION	(0x1<<6)
530109998Smarkm/* Context specific tag */
531109998Smarkm#define ASN1_TFLG_CONTEXT	(0x2<<6)
532109998Smarkm/* Private tag */
533109998Smarkm#define ASN1_TFLG_PRIVATE	(0x3<<6)
534109998Smarkm
535109998Smarkm#define ASN1_TFLG_TAG_CLASS	(0x3<<6)
536109998Smarkm
537109998Smarkm/* These are for ANY DEFINED BY type. In this case
538109998Smarkm * the 'item' field points to an ASN1_ADB structure
539109998Smarkm * which contains a table of values to decode the
540109998Smarkm * relevant type
541109998Smarkm */
542109998Smarkm
543109998Smarkm#define ASN1_TFLG_ADB_MASK	(0x3<<8)
544109998Smarkm
545109998Smarkm#define ASN1_TFLG_ADB_OID	(0x1<<8)
546109998Smarkm
547109998Smarkm#define ASN1_TFLG_ADB_INT	(0x1<<9)
548109998Smarkm
549109998Smarkm/* This flag means a parent structure is passed
550109998Smarkm * instead of the field: this is useful is a
551109998Smarkm * SEQUENCE is being combined with a CHOICE for
552109998Smarkm * example. Since this means the structure and
553109998Smarkm * item name will differ we need to use the
554109998Smarkm * ASN1_CHOICE_END_name() macro for example.
555109998Smarkm */
556109998Smarkm
557109998Smarkm#define ASN1_TFLG_COMBINE	(0x1<<10)
558109998Smarkm
559160814Ssimon/* This flag when present in a SEQUENCE OF, SET OF
560160814Ssimon * or EXPLICIT causes indefinite length constructed
561160814Ssimon * encoding to be used if required.
562160814Ssimon */
563160814Ssimon
564160814Ssimon#define ASN1_TFLG_NDEF		(0x1<<11)
565160814Ssimon
566109998Smarkm/* This is the actual ASN1 item itself */
567109998Smarkm
568109998Smarkmstruct ASN1_ITEM_st {
569109998Smarkmchar itype;			/* The item type, primitive, SEQUENCE, CHOICE or extern */
570109998Smarkmlong utype;			/* underlying type */
571109998Smarkmconst ASN1_TEMPLATE *templates;	/* If SEQUENCE or CHOICE this contains the contents */
572109998Smarkmlong tcount;			/* Number of templates if SEQUENCE or CHOICE */
573109998Smarkmconst void *funcs;		/* functions that handle this type */
574109998Smarkmlong size;			/* Structure size (usually)*/
575109998Smarkm#ifndef NO_ASN1_FIELD_NAMES
576109998Smarkmconst char *sname;		/* Structure name */
577109998Smarkm#endif
578109998Smarkm};
579109998Smarkm
580109998Smarkm/* These are values for the itype field and
581109998Smarkm * determine how the type is interpreted.
582109998Smarkm *
583109998Smarkm * For PRIMITIVE types the underlying type
584109998Smarkm * determines the behaviour if items is NULL.
585109998Smarkm *
586109998Smarkm * Otherwise templates must contain a single
587109998Smarkm * template and the type is treated in the
588109998Smarkm * same way as the type specified in the template.
589109998Smarkm *
590109998Smarkm * For SEQUENCE types the templates field points
591109998Smarkm * to the members, the size field is the
592109998Smarkm * structure size.
593109998Smarkm *
594109998Smarkm * For CHOICE types the templates field points
595109998Smarkm * to each possible member (typically a union)
596109998Smarkm * and the 'size' field is the offset of the
597109998Smarkm * selector.
598109998Smarkm *
599109998Smarkm * The 'funcs' field is used for application
600109998Smarkm * specific functions.
601109998Smarkm *
602109998Smarkm * For COMPAT types the funcs field gives a
603109998Smarkm * set of functions that handle this type, this
604109998Smarkm * supports the old d2i, i2d convention.
605109998Smarkm *
606109998Smarkm * The EXTERN type uses a new style d2i/i2d.
607109998Smarkm * The new style should be used where possible
608109998Smarkm * because it avoids things like the d2i IMPLICIT
609109998Smarkm * hack.
610109998Smarkm *
611109998Smarkm * MSTRING is a multiple string type, it is used
612109998Smarkm * for a CHOICE of character strings where the
613109998Smarkm * actual strings all occupy an ASN1_STRING
614109998Smarkm * structure. In this case the 'utype' field
615109998Smarkm * has a special meaning, it is used as a mask
616109998Smarkm * of acceptable types using the B_ASN1 constants.
617109998Smarkm *
618160814Ssimon * NDEF_SEQUENCE is the same as SEQUENCE except
619160814Ssimon * that it will use indefinite length constructed
620160814Ssimon * encoding if requested.
621160814Ssimon *
622109998Smarkm */
623109998Smarkm
624160814Ssimon#define ASN1_ITYPE_PRIMITIVE		0x0
625109998Smarkm
626160814Ssimon#define ASN1_ITYPE_SEQUENCE		0x1
627109998Smarkm
628160814Ssimon#define ASN1_ITYPE_CHOICE		0x2
629109998Smarkm
630160814Ssimon#define ASN1_ITYPE_COMPAT		0x3
631109998Smarkm
632160814Ssimon#define ASN1_ITYPE_EXTERN		0x4
633109998Smarkm
634160814Ssimon#define ASN1_ITYPE_MSTRING		0x5
635109998Smarkm
636160814Ssimon#define ASN1_ITYPE_NDEF_SEQUENCE	0x6
637160814Ssimon
638109998Smarkm/* Cache for ASN1 tag and length, so we
639109998Smarkm * don't keep re-reading it for things
640109998Smarkm * like CHOICE
641109998Smarkm */
642109998Smarkm
643109998Smarkmstruct ASN1_TLC_st{
644109998Smarkm	char valid;	/* Values below are valid */
645109998Smarkm	int ret;	/* return value */
646109998Smarkm	long plen;	/* length */
647109998Smarkm	int ptag;	/* class value */
648109998Smarkm	int pclass;	/* class value */
649109998Smarkm	int hdrlen;	/* header length */
650109998Smarkm};
651109998Smarkm
652109998Smarkm/* Typedefs for ASN1 function pointers */
653109998Smarkm
654109998Smarkmtypedef ASN1_VALUE * ASN1_new_func(void);
655109998Smarkmtypedef void ASN1_free_func(ASN1_VALUE *a);
656160814Ssimontypedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, const unsigned char ** in, long length);
657109998Smarkmtypedef int ASN1_i2d_func(ASN1_VALUE * a, unsigned char **in);
658109998Smarkm
659160814Ssimontypedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
660109998Smarkm					int tag, int aclass, char opt, ASN1_TLC *ctx);
661109998Smarkm
662109998Smarkmtypedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
663109998Smarkmtypedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
664109998Smarkmtypedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
665109998Smarkm
666238405Sjkimtypedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval,
667238405Sjkim						int indent, const char *fname,
668238405Sjkim						const ASN1_PCTX *pctx);
669238405Sjkim
670109998Smarkmtypedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
671160814Ssimontypedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
672238405Sjkimtypedef int ASN1_primitive_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, int indent, const ASN1_PCTX *pctx);
673109998Smarkm
674109998Smarkmtypedef struct ASN1_COMPAT_FUNCS_st {
675109998Smarkm	ASN1_new_func *asn1_new;
676109998Smarkm	ASN1_free_func *asn1_free;
677109998Smarkm	ASN1_d2i_func *asn1_d2i;
678109998Smarkm	ASN1_i2d_func *asn1_i2d;
679109998Smarkm} ASN1_COMPAT_FUNCS;
680109998Smarkm
681109998Smarkmtypedef struct ASN1_EXTERN_FUNCS_st {
682109998Smarkm	void *app_data;
683109998Smarkm	ASN1_ex_new_func *asn1_ex_new;
684109998Smarkm	ASN1_ex_free_func *asn1_ex_free;
685109998Smarkm	ASN1_ex_free_func *asn1_ex_clear;
686109998Smarkm	ASN1_ex_d2i *asn1_ex_d2i;
687109998Smarkm	ASN1_ex_i2d *asn1_ex_i2d;
688238405Sjkim	ASN1_ex_print_func *asn1_ex_print;
689109998Smarkm} ASN1_EXTERN_FUNCS;
690109998Smarkm
691109998Smarkmtypedef struct ASN1_PRIMITIVE_FUNCS_st {
692109998Smarkm	void *app_data;
693109998Smarkm	unsigned long flags;
694109998Smarkm	ASN1_ex_new_func *prim_new;
695109998Smarkm	ASN1_ex_free_func *prim_free;
696109998Smarkm	ASN1_ex_free_func *prim_clear;
697109998Smarkm	ASN1_primitive_c2i *prim_c2i;
698109998Smarkm	ASN1_primitive_i2c *prim_i2c;
699238405Sjkim	ASN1_primitive_print *prim_print;
700109998Smarkm} ASN1_PRIMITIVE_FUNCS;
701109998Smarkm
702109998Smarkm/* This is the ASN1_AUX structure: it handles various
703109998Smarkm * miscellaneous requirements. For example the use of
704109998Smarkm * reference counts and an informational callback.
705109998Smarkm *
706109998Smarkm * The "informational callback" is called at various
707109998Smarkm * points during the ASN1 encoding and decoding. It can
708109998Smarkm * be used to provide minor customisation of the structures
709109998Smarkm * used. This is most useful where the supplied routines
710109998Smarkm * *almost* do the right thing but need some extra help
711109998Smarkm * at a few points. If the callback returns zero then
712109998Smarkm * it is assumed a fatal error has occurred and the
713109998Smarkm * main operation should be abandoned.
714109998Smarkm *
715109998Smarkm * If major changes in the default behaviour are required
716109998Smarkm * then an external type is more appropriate.
717109998Smarkm */
718109998Smarkm
719238405Sjkimtypedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
720238405Sjkim				void *exarg);
721109998Smarkm
722109998Smarkmtypedef struct ASN1_AUX_st {
723109998Smarkm	void *app_data;
724109998Smarkm	int flags;
725109998Smarkm	int ref_offset;		/* Offset of reference value */
726109998Smarkm	int ref_lock;		/* Lock type to use */
727109998Smarkm	ASN1_aux_cb *asn1_cb;
728109998Smarkm	int enc_offset;		/* Offset of ASN1_ENCODING structure */
729109998Smarkm} ASN1_AUX;
730109998Smarkm
731238405Sjkim/* For print related callbacks exarg points to this structure */
732238405Sjkimtypedef struct ASN1_PRINT_ARG_st {
733238405Sjkim	BIO *out;
734238405Sjkim	int indent;
735238405Sjkim	const ASN1_PCTX *pctx;
736238405Sjkim} ASN1_PRINT_ARG;
737238405Sjkim
738238405Sjkim/* For streaming related callbacks exarg points to this structure */
739238405Sjkimtypedef struct ASN1_STREAM_ARG_st {
740238405Sjkim	/* BIO to stream through */
741238405Sjkim	BIO *out;
742238405Sjkim	/* BIO with filters appended */
743238405Sjkim	BIO *ndef_bio;
744238405Sjkim	/* Streaming I/O boundary */
745238405Sjkim	unsigned char **boundary;
746238405Sjkim} ASN1_STREAM_ARG;
747238405Sjkim
748109998Smarkm/* Flags in ASN1_AUX */
749109998Smarkm
750109998Smarkm/* Use a reference count */
751109998Smarkm#define ASN1_AFLG_REFCOUNT	1
752109998Smarkm/* Save the encoding of structure (useful for signatures) */
753109998Smarkm#define ASN1_AFLG_ENCODING	2
754109998Smarkm/* The Sequence length is invalid */
755109998Smarkm#define ASN1_AFLG_BROKEN	4
756109998Smarkm
757109998Smarkm/* operation values for asn1_cb */
758109998Smarkm
759109998Smarkm#define ASN1_OP_NEW_PRE		0
760109998Smarkm#define ASN1_OP_NEW_POST	1
761109998Smarkm#define ASN1_OP_FREE_PRE	2
762109998Smarkm#define ASN1_OP_FREE_POST	3
763109998Smarkm#define ASN1_OP_D2I_PRE		4
764109998Smarkm#define ASN1_OP_D2I_POST	5
765109998Smarkm#define ASN1_OP_I2D_PRE		6
766109998Smarkm#define ASN1_OP_I2D_POST	7
767238405Sjkim#define ASN1_OP_PRINT_PRE	8
768238405Sjkim#define ASN1_OP_PRINT_POST	9
769238405Sjkim#define ASN1_OP_STREAM_PRE	10
770238405Sjkim#define ASN1_OP_STREAM_POST	11
771238405Sjkim#define ASN1_OP_DETACHED_PRE	12
772238405Sjkim#define ASN1_OP_DETACHED_POST	13
773109998Smarkm
774109998Smarkm/* Macro to implement a primitive type */
775109998Smarkm#define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
776109998Smarkm#define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
777109998Smarkm				ASN1_ITEM_start(itname) \
778109998Smarkm					ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
779109998Smarkm				ASN1_ITEM_end(itname)
780109998Smarkm
781109998Smarkm/* Macro to implement a multi string type */
782109998Smarkm#define IMPLEMENT_ASN1_MSTRING(itname, mask) \
783109998Smarkm				ASN1_ITEM_start(itname) \
784109998Smarkm					ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
785109998Smarkm				ASN1_ITEM_end(itname)
786109998Smarkm
787109998Smarkm/* Macro to implement an ASN1_ITEM in terms of old style funcs */
788109998Smarkm
789109998Smarkm#define IMPLEMENT_COMPAT_ASN1(sname) IMPLEMENT_COMPAT_ASN1_type(sname, V_ASN1_SEQUENCE)
790109998Smarkm
791109998Smarkm#define IMPLEMENT_COMPAT_ASN1_type(sname, tag) \
792109998Smarkm	static const ASN1_COMPAT_FUNCS sname##_ff = { \
793109998Smarkm		(ASN1_new_func *)sname##_new, \
794109998Smarkm		(ASN1_free_func *)sname##_free, \
795109998Smarkm		(ASN1_d2i_func *)d2i_##sname, \
796109998Smarkm		(ASN1_i2d_func *)i2d_##sname, \
797109998Smarkm	}; \
798109998Smarkm	ASN1_ITEM_start(sname) \
799109998Smarkm		ASN1_ITYPE_COMPAT, \
800109998Smarkm		tag, \
801109998Smarkm		NULL, \
802109998Smarkm		0, \
803109998Smarkm		&sname##_ff, \
804109998Smarkm		0, \
805109998Smarkm		#sname \
806109998Smarkm	ASN1_ITEM_end(sname)
807109998Smarkm
808109998Smarkm#define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
809109998Smarkm	ASN1_ITEM_start(sname) \
810109998Smarkm		ASN1_ITYPE_EXTERN, \
811109998Smarkm		tag, \
812109998Smarkm		NULL, \
813109998Smarkm		0, \
814109998Smarkm		&fptrs, \
815109998Smarkm		0, \
816109998Smarkm		#sname \
817109998Smarkm	ASN1_ITEM_end(sname)
818109998Smarkm
819109998Smarkm/* Macro to implement standard functions in terms of ASN1_ITEM structures */
820109998Smarkm
821109998Smarkm#define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
822109998Smarkm
823109998Smarkm#define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
824109998Smarkm
825109998Smarkm#define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
826109998Smarkm			IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
827109998Smarkm
828238405Sjkim#define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
829238405Sjkim		IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
830238405Sjkim
831160814Ssimon#define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
832160814Ssimon		IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
833160814Ssimon
834238405Sjkim#define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
835238405Sjkim	pre stname *fname##_new(void) \
836238405Sjkim	{ \
837238405Sjkim		return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
838238405Sjkim	} \
839238405Sjkim	pre void fname##_free(stname *a) \
840238405Sjkim	{ \
841238405Sjkim		ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
842238405Sjkim	}
843238405Sjkim
844109998Smarkm#define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
845109998Smarkm	stname *fname##_new(void) \
846109998Smarkm	{ \
847109998Smarkm		return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
848109998Smarkm	} \
849109998Smarkm	void fname##_free(stname *a) \
850109998Smarkm	{ \
851109998Smarkm		ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
852109998Smarkm	}
853109998Smarkm
854109998Smarkm#define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
855109998Smarkm	IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
856109998Smarkm	IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
857109998Smarkm
858109998Smarkm#define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
859160814Ssimon	stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
860109998Smarkm	{ \
861109998Smarkm		return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
862109998Smarkm	} \
863109998Smarkm	int i2d_##fname(stname *a, unsigned char **out) \
864109998Smarkm	{ \
865109998Smarkm		return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
866109998Smarkm	}
867109998Smarkm
868160814Ssimon#define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
869160814Ssimon	int i2d_##stname##_NDEF(stname *a, unsigned char **out) \
870160814Ssimon	{ \
871160814Ssimon		return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
872160814Ssimon	}
873160814Ssimon
874109998Smarkm/* This includes evil casts to remove const: they will go away when full
875109998Smarkm * ASN1 constification is done.
876109998Smarkm */
877109998Smarkm#define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
878109998Smarkm	stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
879109998Smarkm	{ \
880160814Ssimon		return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
881109998Smarkm	} \
882109998Smarkm	int i2d_##fname(const stname *a, unsigned char **out) \
883109998Smarkm	{ \
884109998Smarkm		return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
885109998Smarkm	}
886109998Smarkm
887109998Smarkm#define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
888109998Smarkm	stname * stname##_dup(stname *x) \
889109998Smarkm        { \
890109998Smarkm        return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
891109998Smarkm        }
892109998Smarkm
893238405Sjkim#define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \
894238405Sjkim	IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname)
895238405Sjkim
896238405Sjkim#define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \
897238405Sjkim	int fname##_print_ctx(BIO *out, stname *x, int indent, \
898238405Sjkim						const ASN1_PCTX *pctx) \
899238405Sjkim	{ \
900238405Sjkim		return ASN1_item_print(out, (ASN1_VALUE *)x, indent, \
901238405Sjkim			ASN1_ITEM_rptr(itname), pctx); \
902238405Sjkim	}
903238405Sjkim
904109998Smarkm#define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
905109998Smarkm		IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
906109998Smarkm
907109998Smarkm#define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
908109998Smarkm	IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
909109998Smarkm	IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
910109998Smarkm
911109998Smarkm/* external definitions for primitive types */
912109998Smarkm
913109998SmarkmDECLARE_ASN1_ITEM(ASN1_BOOLEAN)
914109998SmarkmDECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
915109998SmarkmDECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
916109998SmarkmDECLARE_ASN1_ITEM(ASN1_SEQUENCE)
917109998SmarkmDECLARE_ASN1_ITEM(CBIGNUM)
918109998SmarkmDECLARE_ASN1_ITEM(BIGNUM)
919109998SmarkmDECLARE_ASN1_ITEM(LONG)
920109998SmarkmDECLARE_ASN1_ITEM(ZLONG)
921109998Smarkm
922109998SmarkmDECLARE_STACK_OF(ASN1_VALUE)
923109998Smarkm
924109998Smarkm/* Functions used internally by the ASN1 code */
925109998Smarkm
926109998Smarkmint ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
927109998Smarkmvoid ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
928109998Smarkmint ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
929109998Smarkmint ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
930109998Smarkm
931109998Smarkmvoid ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
932160814Ssimonint ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_TEMPLATE *tt);
933160814Ssimonint ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
934109998Smarkm				int tag, int aclass, char opt, ASN1_TLC *ctx);
935109998Smarkm
936109998Smarkmint ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
937109998Smarkmint ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_TEMPLATE *tt);
938109998Smarkmvoid ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
939109998Smarkm
940109998Smarkmint asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
941160814Ssimonint asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
942109998Smarkm
943109998Smarkmint asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
944109998Smarkmint asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
945109998Smarkm
946109998SmarkmASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
947109998Smarkm
948109998Smarkmconst ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr);
949109998Smarkm
950109998Smarkmint asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it);
951109998Smarkm
952109998Smarkmvoid asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
953109998Smarkmvoid asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
954109998Smarkmint asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it);
955160814Ssimonint asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, const ASN1_ITEM *it);
956109998Smarkm
957109998Smarkm#ifdef  __cplusplus
958109998Smarkm}
959109998Smarkm#endif
960109998Smarkm#endif
961