1 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
2  * project 2000.
3  */
4 /* ====================================================================
5  * Copyright (c) 2000-2005 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    licensing@OpenSSL.org.
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This product includes cryptographic software written by Eric Young
53  * (eay@cryptsoft.com).  This product includes software written by Tim
54  * Hudson (tjh@cryptsoft.com).
55  *
56  */
57 #ifndef HEADER_ASN1T_H
58 #define HEADER_ASN1T_H
59 
60 #include <openssl/base.h>
61 #include <openssl/asn1.h>
62 
63 #ifdef  __cplusplus
64 extern "C" {
65 #endif
66 
67 
68 /* Legacy ASN.1 library template definitions.
69  *
70  * This header is used to define new types in OpenSSL's ASN.1 implementation. It
71  * is deprecated and will be unexported from the library. Use the new |CBS| and
72  * |CBB| library in <openssl/bytestring.h> instead. */
73 
74 
75 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
76 #define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
77 
78 
79 /* Macros for start and end of ASN1_ITEM definition */
80 
81 #define ASN1_ITEM_start(itname) \
82 	const ASN1_ITEM itname##_it = {
83 
84 #define ASN1_ITEM_end(itname) \
85 		};
86 
87 /* Macros to aid ASN1 template writing */
88 
89 #define ASN1_ITEM_TEMPLATE(tname) \
90 	static const ASN1_TEMPLATE tname##_item_tt
91 
92 #define ASN1_ITEM_TEMPLATE_END(tname) \
93 	;\
94 	ASN1_ITEM_start(tname) \
95 		ASN1_ITYPE_PRIMITIVE,\
96 		-1,\
97 		&tname##_item_tt,\
98 		0,\
99 		NULL,\
100 		0,\
101 		#tname \
102 	ASN1_ITEM_end(tname)
103 
104 
105 /* This is a ASN1 type which just embeds a template */
106 
107 /* This pair helps declare a SEQUENCE. We can do:
108  *
109  * 	ASN1_SEQUENCE(stname) = {
110  * 		... SEQUENCE components ...
111  * 	} ASN1_SEQUENCE_END(stname)
112  *
113  * 	This will produce an ASN1_ITEM called stname_it
114  *	for a structure called stname.
115  *
116  * 	If you want the same structure but a different
117  *	name then use:
118  *
119  * 	ASN1_SEQUENCE(itname) = {
120  * 		... SEQUENCE components ...
121  * 	} ASN1_SEQUENCE_END_name(stname, itname)
122  *
123  *	This will create an item called itname_it using
124  *	a structure called stname.
125  */
126 
127 #define ASN1_SEQUENCE(tname) \
128 	static const ASN1_TEMPLATE tname##_seq_tt[]
129 
130 #define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
131 
132 #define ASN1_SEQUENCE_END_name(stname, tname) \
133 	;\
134 	ASN1_ITEM_start(tname) \
135 		ASN1_ITYPE_SEQUENCE,\
136 		V_ASN1_SEQUENCE,\
137 		tname##_seq_tt,\
138 		sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
139 		NULL,\
140 		sizeof(stname),\
141 		#stname \
142 	ASN1_ITEM_end(tname)
143 
144 #define ASN1_SEQUENCE_cb(tname, cb) \
145 	static const ASN1_AUX tname##_aux = {NULL, 0, 0, cb, 0}; \
146 	ASN1_SEQUENCE(tname)
147 
148 #define ASN1_SEQUENCE_ref(tname, cb) \
149 	static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), cb, 0}; \
150 	ASN1_SEQUENCE(tname)
151 
152 #define ASN1_SEQUENCE_enc(tname, enc, cb) \
153 	static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, cb, offsetof(tname, enc)}; \
154 	ASN1_SEQUENCE(tname)
155 
156 #define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
157 
158 #define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
159 
160 #define ASN1_SEQUENCE_END_ref(stname, tname) \
161 	;\
162 	ASN1_ITEM_start(tname) \
163 		ASN1_ITYPE_SEQUENCE,\
164 		V_ASN1_SEQUENCE,\
165 		tname##_seq_tt,\
166 		sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
167 		&tname##_aux,\
168 		sizeof(stname),\
169 		#stname \
170 	ASN1_ITEM_end(tname)
171 
172 
173 /* This pair helps declare a CHOICE type. We can do:
174  *
175  * 	ASN1_CHOICE(chname) = {
176  * 		... CHOICE options ...
177  * 	ASN1_CHOICE_END(chname)
178  *
179  * 	This will produce an ASN1_ITEM called chname_it
180  *	for a structure called chname. The structure
181  *	definition must look like this:
182  *	typedef struct {
183  *		int type;
184  *		union {
185  *			ASN1_SOMETHING *opt1;
186  *			ASN1_SOMEOTHER *opt2;
187  *		} value;
188  *	} chname;
189  *
190  *	the name of the selector must be 'type'.
191  * 	to use an alternative selector name use the
192  *      ASN1_CHOICE_END_selector() version.
193  */
194 
195 #define ASN1_CHOICE(tname) \
196 	static const ASN1_TEMPLATE tname##_ch_tt[]
197 
198 #define ASN1_CHOICE_cb(tname, cb) \
199 	static const ASN1_AUX tname##_aux = {NULL, 0, 0, cb, 0}; \
200 	ASN1_CHOICE(tname)
201 
202 #define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
203 
204 #define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
205 
206 #define ASN1_CHOICE_END_selector(stname, tname, selname) \
207 	;\
208 	ASN1_ITEM_start(tname) \
209 		ASN1_ITYPE_CHOICE,\
210 		offsetof(stname,selname) ,\
211 		tname##_ch_tt,\
212 		sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
213 		NULL,\
214 		sizeof(stname),\
215 		#stname \
216 	ASN1_ITEM_end(tname)
217 
218 #define ASN1_CHOICE_END_cb(stname, tname, selname) \
219 	;\
220 	ASN1_ITEM_start(tname) \
221 		ASN1_ITYPE_CHOICE,\
222 		offsetof(stname,selname) ,\
223 		tname##_ch_tt,\
224 		sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
225 		&tname##_aux,\
226 		sizeof(stname),\
227 		#stname \
228 	ASN1_ITEM_end(tname)
229 
230 /* This helps with the template wrapper form of ASN1_ITEM */
231 
232 #define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
233 	(flags), (tag), 0,\
234 	#name, ASN1_ITEM_ref(type) }
235 
236 /* These help with SEQUENCE or CHOICE components */
237 
238 /* used to declare other types */
239 
240 #define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
241 	(flags), (tag), offsetof(stname, field),\
242 	#field, ASN1_ITEM_ref(type) }
243 
244 /* used when the structure is combined with the parent */
245 
246 #define ASN1_EX_COMBINE(flags, tag, type) { \
247 	(flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) }
248 
249 /* implicit and explicit helper macros */
250 
251 #define ASN1_IMP_EX(stname, field, type, tag, ex) \
252 		ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
253 
254 #define ASN1_EXP_EX(stname, field, type, tag, ex) \
255 		ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
256 
257 /* Any defined by macros: the field used is in the table itself */
258 
259 #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
260 #define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
261 /* Plain simple type */
262 #define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
263 
264 /* OPTIONAL simple type */
265 #define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
266 
267 /* IMPLICIT tagged simple type */
268 #define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
269 
270 /* IMPLICIT tagged OPTIONAL simple type */
271 #define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
272 
273 /* Same as above but EXPLICIT */
274 
275 #define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
276 #define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
277 
278 /* SEQUENCE OF type */
279 #define ASN1_SEQUENCE_OF(stname, field, type) \
280 		ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
281 
282 /* OPTIONAL SEQUENCE OF */
283 #define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
284 		ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
285 
286 /* Same as above but for SET OF */
287 
288 #define ASN1_SET_OF(stname, field, type) \
289 		ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
290 
291 #define ASN1_SET_OF_OPT(stname, field, type) \
292 		ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
293 
294 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
295 
296 #define ASN1_IMP_SET_OF(stname, field, type, tag) \
297 			ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
298 
299 #define ASN1_EXP_SET_OF(stname, field, type, tag) \
300 			ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
301 
302 #define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
303 			ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
304 
305 #define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
306 			ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
307 
308 #define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
309 			ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
310 
311 #define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
312 			ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
313 
314 #define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
315 			ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
316 
317 #define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
318 			ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
319 
320 /* Macros for the ASN1_ADB structure */
321 
322 #define ASN1_ADB(name) \
323 	static const ASN1_ADB_TABLE name##_adbtbl[]
324 
325 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \
326 	;\
327 	static const ASN1_ADB name##_adb = {\
328 		flags,\
329 		offsetof(name, field),\
330 		app_table,\
331 		name##_adbtbl,\
332 		sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
333 		def,\
334 		none\
335 	}
336 
337 #define ADB_ENTRY(val, template) {val, template}
338 
339 #define ASN1_ADB_TEMPLATE(name) \
340 	static const ASN1_TEMPLATE name##_tt
341 
342 /* This is the ASN1 template structure that defines
343  * a wrapper round the actual type. It determines the
344  * actual position of the field in the value structure,
345  * various flags such as OPTIONAL and the field name.
346  */
347 
348 struct ASN1_TEMPLATE_st {
349 unsigned long flags;		/* Various flags */
350 long tag;			/* tag, not used if no tagging */
351 unsigned long offset;		/* Offset of this field in structure */
352 const char *field_name;		/* Field name */
353 ASN1_ITEM_EXP *item;		/* Relevant ASN1_ITEM or ASN1_ADB */
354 };
355 
356 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
357 
358 #define ASN1_TEMPLATE_item(t) (t->item_ptr)
359 #define ASN1_TEMPLATE_adb(t) (t->item_ptr)
360 
361 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
362 typedef struct ASN1_ADB_st ASN1_ADB;
363 
364 typedef struct asn1_must_be_null_st ASN1_MUST_BE_NULL;
365 
366 struct ASN1_ADB_st {
367 	unsigned long flags;	/* Various flags */
368 	unsigned long offset;	/* Offset of selector field */
369 	ASN1_MUST_BE_NULL *unused;
370 	const ASN1_ADB_TABLE *tbl;	/* Table of possible types */
371 	long tblcount;		/* Number of entries in tbl */
372 	const ASN1_TEMPLATE *default_tt;  /* Type to use if no match */
373 	const ASN1_TEMPLATE *null_tt;  /* Type to use if selector is NULL */
374 };
375 
376 struct ASN1_ADB_TABLE_st {
377 	long value;		/* NID for an object or value for an int */
378 	const ASN1_TEMPLATE tt;		/* item for this value */
379 };
380 
381 /* template flags */
382 
383 /* Field is optional */
384 #define ASN1_TFLG_OPTIONAL	(0x1)
385 
386 /* Field is a SET OF */
387 #define ASN1_TFLG_SET_OF	(0x1 << 1)
388 
389 /* Field is a SEQUENCE OF */
390 #define ASN1_TFLG_SEQUENCE_OF	(0x2 << 1)
391 
392 /* Special case: this refers to a SET OF that
393  * will be sorted into DER order when encoded *and*
394  * the corresponding STACK will be modified to match
395  * the new order.
396  */
397 #define ASN1_TFLG_SET_ORDER	(0x3 << 1)
398 
399 /* Mask for SET OF or SEQUENCE OF */
400 #define ASN1_TFLG_SK_MASK	(0x3 << 1)
401 
402 /* These flags mean the tag should be taken from the
403  * tag field. If EXPLICIT then the underlying type
404  * is used for the inner tag.
405  */
406 
407 /* IMPLICIT tagging */
408 #define ASN1_TFLG_IMPTAG	(0x1 << 3)
409 
410 
411 /* EXPLICIT tagging, inner tag from underlying type */
412 #define ASN1_TFLG_EXPTAG	(0x2 << 3)
413 
414 #define ASN1_TFLG_TAG_MASK	(0x3 << 3)
415 
416 /* context specific IMPLICIT */
417 #define ASN1_TFLG_IMPLICIT	ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
418 
419 /* context specific EXPLICIT */
420 #define ASN1_TFLG_EXPLICIT	ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
421 
422 /* If tagging is in force these determine the
423  * type of tag to use. Otherwise the tag is
424  * determined by the underlying type. These
425  * values reflect the actual octet format.
426  */
427 
428 /* Universal tag */
429 #define ASN1_TFLG_UNIVERSAL	(0x0<<6)
430 /* Application tag */
431 #define ASN1_TFLG_APPLICATION	(0x1<<6)
432 /* Context specific tag */
433 #define ASN1_TFLG_CONTEXT	(0x2<<6)
434 /* Private tag */
435 #define ASN1_TFLG_PRIVATE	(0x3<<6)
436 
437 #define ASN1_TFLG_TAG_CLASS	(0x3<<6)
438 
439 /* These are for ANY DEFINED BY type. In this case
440  * the 'item' field points to an ASN1_ADB structure
441  * which contains a table of values to decode the
442  * relevant type
443  */
444 
445 #define ASN1_TFLG_ADB_MASK	(0x3<<8)
446 
447 #define ASN1_TFLG_ADB_OID	(0x1<<8)
448 
449 #define ASN1_TFLG_ADB_INT	(0x1<<9)
450 
451 /* This flag means a parent structure is passed
452  * instead of the field: this is useful is a
453  * SEQUENCE is being combined with a CHOICE for
454  * example. Since this means the structure and
455  * item name will differ we need to use the
456  * ASN1_CHOICE_END_name() macro for example.
457  */
458 
459 #define ASN1_TFLG_COMBINE	(0x1<<10)
460 
461 /* This is the actual ASN1 item itself */
462 
463 struct ASN1_ITEM_st {
464 char itype;			/* The item type, primitive, SEQUENCE, CHOICE or extern */
465 long utype;			/* underlying type */
466 const ASN1_TEMPLATE *templates;	/* If SEQUENCE or CHOICE this contains the contents */
467 long tcount;			/* Number of templates if SEQUENCE or CHOICE */
468 const void *funcs;		/* functions that handle this type */
469 long size;			/* Structure size (usually)*/
470 const char *sname;		/* Structure name */
471 };
472 
473 /* These are values for the itype field and
474  * determine how the type is interpreted.
475  *
476  * For PRIMITIVE types the underlying type
477  * determines the behaviour if items is NULL.
478  *
479  * Otherwise templates must contain a single
480  * template and the type is treated in the
481  * same way as the type specified in the template.
482  *
483  * For SEQUENCE types the templates field points
484  * to the members, the size field is the
485  * structure size.
486  *
487  * For CHOICE types the templates field points
488  * to each possible member (typically a union)
489  * and the 'size' field is the offset of the
490  * selector.
491  *
492  * The 'funcs' field is used for application
493  * specific functions.
494  *
495  * The EXTERN type uses a new style d2i/i2d.
496  * The new style should be used where possible
497  * because it avoids things like the d2i IMPLICIT
498  * hack.
499  *
500  * MSTRING is a multiple string type, it is used
501  * for a CHOICE of character strings where the
502  * actual strings all occupy an ASN1_STRING
503  * structure. In this case the 'utype' field
504  * has a special meaning, it is used as a mask
505  * of acceptable types using the B_ASN1 constants.
506  *
507  */
508 
509 #define ASN1_ITYPE_PRIMITIVE		0x0
510 
511 #define ASN1_ITYPE_SEQUENCE		0x1
512 
513 #define ASN1_ITYPE_CHOICE		0x2
514 
515 #define ASN1_ITYPE_EXTERN		0x4
516 
517 #define ASN1_ITYPE_MSTRING		0x5
518 
519 /* Cache for ASN1 tag and length, so we
520  * don't keep re-reading it for things
521  * like CHOICE
522  */
523 
524 struct ASN1_TLC_st{
525 	char valid;	/* Values below are valid */
526 	int ret;	/* return value */
527 	long plen;	/* length */
528 	int ptag;	/* class value */
529 	int pclass;	/* class value */
530 	int hdrlen;	/* header length */
531 };
532 
533 /* Typedefs for ASN1 function pointers */
534 
535 typedef ASN1_VALUE * ASN1_new_func(void);
536 typedef void ASN1_free_func(ASN1_VALUE *a);
537 typedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, const unsigned char ** in, long length);
538 typedef int ASN1_i2d_func(ASN1_VALUE * a, unsigned char **in);
539 
540 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
541 					int tag, int aclass, char opt, ASN1_TLC *ctx);
542 
543 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
544 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
545 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
546 
547 typedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval,
548 						int indent, const char *fname,
549 						const ASN1_PCTX *pctx);
550 
551 typedef struct ASN1_EXTERN_FUNCS_st {
552 	void *app_data;
553 	ASN1_ex_new_func *asn1_ex_new;
554 	ASN1_ex_free_func *asn1_ex_free;
555 	ASN1_ex_free_func *asn1_ex_clear;
556 	ASN1_ex_d2i *asn1_ex_d2i;
557 	ASN1_ex_i2d *asn1_ex_i2d;
558 	/* asn1_ex_print is unused. */
559 	ASN1_ex_print_func *asn1_ex_print;
560 } ASN1_EXTERN_FUNCS;
561 
562 /* This is the ASN1_AUX structure: it handles various
563  * miscellaneous requirements. For example the use of
564  * reference counts and an informational callback.
565  *
566  * The "informational callback" is called at various
567  * points during the ASN1 encoding and decoding. It can
568  * be used to provide minor customisation of the structures
569  * used. This is most useful where the supplied routines
570  * *almost* do the right thing but need some extra help
571  * at a few points. If the callback returns zero then
572  * it is assumed a fatal error has occurred and the
573  * main operation should be abandoned.
574  *
575  * If major changes in the default behaviour are required
576  * then an external type is more appropriate.
577  */
578 
579 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
580 				void *exarg);
581 
582 typedef struct ASN1_AUX_st {
583 	void *app_data;
584 	int flags;
585 	int ref_offset;		/* Offset of reference value */
586 	ASN1_aux_cb *asn1_cb;
587 	int enc_offset;		/* Offset of ASN1_ENCODING structure */
588 } ASN1_AUX;
589 
590 /* Flags in ASN1_AUX */
591 
592 /* Use a reference count */
593 #define ASN1_AFLG_REFCOUNT	1
594 /* Save the encoding of structure (useful for signatures) */
595 #define ASN1_AFLG_ENCODING	2
596 
597 /* operation values for asn1_cb */
598 
599 #define ASN1_OP_NEW_PRE		0
600 #define ASN1_OP_NEW_POST	1
601 #define ASN1_OP_FREE_PRE	2
602 #define ASN1_OP_FREE_POST	3
603 #define ASN1_OP_D2I_PRE		4
604 #define ASN1_OP_D2I_POST	5
605 #define ASN1_OP_I2D_PRE		6
606 #define ASN1_OP_I2D_POST	7
607 #define ASN1_OP_PRINT_PRE	8
608 #define ASN1_OP_PRINT_POST	9
609 #define ASN1_OP_STREAM_PRE	10
610 #define ASN1_OP_STREAM_POST	11
611 #define ASN1_OP_DETACHED_PRE	12
612 #define ASN1_OP_DETACHED_POST	13
613 
614 /* Macro to implement a primitive type */
615 #define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
616 #define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
617 				ASN1_ITEM_start(itname) \
618 					ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
619 				ASN1_ITEM_end(itname)
620 
621 /* Macro to implement a multi string type */
622 #define IMPLEMENT_ASN1_MSTRING(itname, mask) \
623 				ASN1_ITEM_start(itname) \
624 					ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
625 				ASN1_ITEM_end(itname)
626 
627 #define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
628 	ASN1_ITEM_start(sname) \
629 		ASN1_ITYPE_EXTERN, \
630 		tag, \
631 		NULL, \
632 		0, \
633 		&fptrs, \
634 		0, \
635 		#sname \
636 	ASN1_ITEM_end(sname)
637 
638 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
639 
640 #define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
641 
642 #define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
643 
644 #define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
645 			IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
646 
647 #define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
648 		IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
649 
650 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
651 		IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
652 
653 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
654 	pre stname *fname##_new(void) \
655 	{ \
656 		return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
657 	} \
658 	pre void fname##_free(stname *a) \
659 	{ \
660 		ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
661 	}
662 
663 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
664 	stname *fname##_new(void) \
665 	{ \
666 		return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
667 	} \
668 	void fname##_free(stname *a) \
669 	{ \
670 		ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
671 	}
672 
673 #define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
674 	IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
675 	IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
676 
677 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
678 	stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
679 	{ \
680 		return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
681 	} \
682 	int i2d_##fname(stname *a, unsigned char **out) \
683 	{ \
684 		return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
685 	}
686 
687 /* This includes evil casts to remove const: they will go away when full
688  * ASN1 constification is done.
689  */
690 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
691 	stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
692 	{ \
693 		return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
694 	} \
695 	int i2d_##fname(const stname *a, unsigned char **out) \
696 	{ \
697 		return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
698 	}
699 
700 #define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
701 	stname * stname##_dup(stname *x) \
702         { \
703         return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
704         }
705 
706 #define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
707 		IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
708 
709 #define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
710 	IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
711 	IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
712 
713 /* external definitions for primitive types */
714 
715 DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
716 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
717 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
718 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
719 
720 DEFINE_STACK_OF(ASN1_VALUE)
721 
722 #ifdef  __cplusplus
723 }
724 #endif
725 #endif
726