xref: /aosp_15_r20/external/boringssl/src/include/openssl/asn1t.h (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Written by Dr Stephen N Henson ([email protected]) 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  *    [email protected].
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  * ([email protected]).  This product includes software written by Tim
54  * Hudson ([email protected]).
55  *
56  */
57 #ifndef OPENSSL_HEADER_ASN1T_H
58 #define OPENSSL_HEADER_ASN1T_H
59 
60 #include <openssl/base.h>
61 #include <openssl/asn1.h>
62 
63 #if defined(__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 typedef struct ASN1_TEMPLATE_st ASN1_TEMPLATE;
76 typedef struct ASN1_TLC_st ASN1_TLC;
77 
78 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
79 #define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
80 
81 
82 /* Macros for start and end of ASN1_ITEM definition */
83 
84 #define ASN1_ITEM_start(itname) \
85 	const ASN1_ITEM itname##_it = {
86 
87 #define ASN1_ITEM_end(itname) \
88 		};
89 
90 /* Macros to aid ASN1 template writing */
91 
92 #define ASN1_ITEM_TEMPLATE(tname) \
93 	static const ASN1_TEMPLATE tname##_item_tt
94 
95 #define ASN1_ITEM_TEMPLATE_END(tname) \
96 	;\
97 	ASN1_ITEM_start(tname) \
98 		ASN1_ITYPE_PRIMITIVE,\
99 		-1,\
100 		&tname##_item_tt,\
101 		0,\
102 		NULL,\
103 		0,\
104 		#tname \
105 	ASN1_ITEM_end(tname)
106 
107 
108 /* This is a ASN1 type which just embeds a template */
109 
110 /* This pair helps declare a SEQUENCE. We can do:
111  *
112  * 	ASN1_SEQUENCE(stname) = {
113  * 		... SEQUENCE components ...
114  * 	} ASN1_SEQUENCE_END(stname)
115  *
116  * 	This will produce an ASN1_ITEM called stname_it
117  *	for a structure called stname.
118  *
119  * 	If you want the same structure but a different
120  *	name then use:
121  *
122  * 	ASN1_SEQUENCE(itname) = {
123  * 		... SEQUENCE components ...
124  * 	} ASN1_SEQUENCE_END_name(stname, itname)
125  *
126  *	This will create an item called itname_it using
127  *	a structure called stname.
128  */
129 
130 #define ASN1_SEQUENCE(tname) \
131 	static const ASN1_TEMPLATE tname##_seq_tt[]
132 
133 #define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
134 
135 #define ASN1_SEQUENCE_END_name(stname, tname) \
136 	;\
137 	ASN1_ITEM_start(tname) \
138 		ASN1_ITYPE_SEQUENCE,\
139 		V_ASN1_SEQUENCE,\
140 		tname##_seq_tt,\
141 		sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
142 		NULL,\
143 		sizeof(stname),\
144 		#stname \
145 	ASN1_ITEM_end(tname)
146 
147 #define ASN1_SEQUENCE_cb(tname, cb) \
148 	static const ASN1_AUX tname##_aux = {NULL, 0, 0, cb, 0}; \
149 	ASN1_SEQUENCE(tname)
150 
151 #define ASN1_SEQUENCE_ref(tname, cb) \
152 	static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), cb, 0}; \
153 	ASN1_SEQUENCE(tname)
154 
155 #define ASN1_SEQUENCE_enc(tname, enc, cb) \
156 	static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, cb, offsetof(tname, enc)}; \
157 	ASN1_SEQUENCE(tname)
158 
159 #define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
160 
161 #define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
162 
163 #define ASN1_SEQUENCE_END_ref(stname, tname) \
164 	;\
165 	ASN1_ITEM_start(tname) \
166 		ASN1_ITYPE_SEQUENCE,\
167 		V_ASN1_SEQUENCE,\
168 		tname##_seq_tt,\
169 		sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
170 		&tname##_aux,\
171 		sizeof(stname),\
172 		#stname \
173 	ASN1_ITEM_end(tname)
174 
175 
176 /* This pair helps declare a CHOICE type. We can do:
177  *
178  * 	ASN1_CHOICE(chname) = {
179  * 		... CHOICE options ...
180  * 	ASN1_CHOICE_END(chname)
181  *
182  * 	This will produce an ASN1_ITEM called chname_it
183  *	for a structure called chname. The structure
184  *	definition must look like this:
185  *	typedef struct {
186  *		int type;
187  *		union {
188  *			ASN1_SOMETHING *opt1;
189  *			ASN1_SOMEOTHER *opt2;
190  *		} value;
191  *	} chname;
192  *
193  *	the name of the selector must be 'type'.
194  * 	to use an alternative selector name use the
195  *      ASN1_CHOICE_END_selector() version.
196  */
197 
198 #define ASN1_CHOICE(tname) \
199 	static const ASN1_TEMPLATE tname##_ch_tt[]
200 
201 #define ASN1_CHOICE_cb(tname, cb) \
202 	static const ASN1_AUX tname##_aux = {NULL, 0, 0, cb, 0}; \
203 	ASN1_CHOICE(tname)
204 
205 #define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
206 
207 #define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
208 
209 #define ASN1_CHOICE_END_selector(stname, tname, selname) \
210 	;\
211 	ASN1_ITEM_start(tname) \
212 		ASN1_ITYPE_CHOICE,\
213 		offsetof(stname,selname) ,\
214 		tname##_ch_tt,\
215 		sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
216 		NULL,\
217 		sizeof(stname),\
218 		#stname \
219 	ASN1_ITEM_end(tname)
220 
221 #define ASN1_CHOICE_END_cb(stname, tname, selname) \
222 	;\
223 	ASN1_ITEM_start(tname) \
224 		ASN1_ITYPE_CHOICE,\
225 		offsetof(stname,selname) ,\
226 		tname##_ch_tt,\
227 		sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
228 		&tname##_aux,\
229 		sizeof(stname),\
230 		#stname \
231 	ASN1_ITEM_end(tname)
232 
233 /* This helps with the template wrapper form of ASN1_ITEM */
234 
235 #define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
236 	(flags), (tag), 0,\
237 	#name, ASN1_ITEM_ref(type) }
238 
239 /* These help with SEQUENCE or CHOICE components */
240 
241 /* used to declare other types */
242 
243 #define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
244 	(flags), (tag), offsetof(stname, field),\
245 	#field, ASN1_ITEM_ref(type) }
246 
247 /* implicit and explicit helper macros */
248 
249 #define ASN1_IMP_EX(stname, field, type, tag, ex) \
250 		ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
251 
252 #define ASN1_EXP_EX(stname, field, type, tag, ex) \
253 		ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
254 
255 /* Any defined by macros: the field used is in the table itself */
256 
257 #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
258 /* Plain simple type */
259 #define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
260 
261 /* OPTIONAL simple type */
262 #define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
263 
264 /* IMPLICIT tagged simple type */
265 #define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
266 
267 /* IMPLICIT tagged OPTIONAL simple type */
268 #define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
269 
270 /* Same as above but EXPLICIT */
271 
272 #define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
273 #define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
274 
275 /* SEQUENCE OF type */
276 #define ASN1_SEQUENCE_OF(stname, field, type) \
277 		ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
278 
279 /* OPTIONAL SEQUENCE OF */
280 #define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
281 		ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
282 
283 /* Same as above but for SET OF */
284 
285 #define ASN1_SET_OF(stname, field, type) \
286 		ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
287 
288 #define ASN1_SET_OF_OPT(stname, field, type) \
289 		ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
290 
291 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
292 
293 #define ASN1_IMP_SET_OF(stname, field, type, tag) \
294 			ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
295 
296 #define ASN1_EXP_SET_OF(stname, field, type, tag) \
297 			ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
298 
299 #define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
300 			ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
301 
302 #define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
303 			ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
304 
305 #define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
306 			ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
307 
308 #define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
309 			ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
310 
311 #define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
312 			ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
313 
314 #define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
315 			ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
316 
317 /* Macros for the ASN1_ADB structure */
318 
319 #define ASN1_ADB(name) \
320 	static const ASN1_ADB_TABLE name##_adbtbl[]
321 
322 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \
323 	;\
324 	static const ASN1_ADB name##_adb = {\
325 		flags,\
326 		offsetof(name, field),\
327 		app_table,\
328 		name##_adbtbl,\
329 		sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
330 		def,\
331 		none\
332 	}
333 
334 #define ADB_ENTRY(val, template) {val, template}
335 
336 #define ASN1_ADB_TEMPLATE(name) \
337 	static const ASN1_TEMPLATE name##_tt
338 
339 /* This is the ASN1 template structure that defines
340  * a wrapper round the actual type. It determines the
341  * actual position of the field in the value structure,
342  * various flags such as OPTIONAL and the field name.
343  */
344 
345 struct ASN1_TEMPLATE_st {
346 uint32_t flags;		/* Various flags */
347 int tag;			/* tag, not used if no tagging */
348 unsigned long offset;		/* Offset of this field in structure */
349 const char *field_name;		/* Field name */
350 ASN1_ITEM_EXP *item;		/* Relevant ASN1_ITEM or ASN1_ADB */
351 };
352 
353 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
354 
355 #define ASN1_TEMPLATE_item(t) (t->item_ptr)
356 #define ASN1_TEMPLATE_adb(t) (t->item_ptr)
357 
358 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
359 typedef struct ASN1_ADB_st ASN1_ADB;
360 
361 typedef struct asn1_must_be_null_st ASN1_MUST_BE_NULL;
362 
363 struct ASN1_ADB_st {
364 	uint32_t flags;	/* Various flags */
365 	unsigned long offset;	/* Offset of selector field */
366 	ASN1_MUST_BE_NULL *unused;
367 	const ASN1_ADB_TABLE *tbl;	/* Table of possible types */
368 	long tblcount;		/* Number of entries in tbl */
369 	const ASN1_TEMPLATE *default_tt;  /* Type to use if no match */
370 	const ASN1_TEMPLATE *null_tt;  /* Type to use if selector is NULL */
371 };
372 
373 struct ASN1_ADB_TABLE_st {
374 	int value;		/* NID for an object */
375 	const ASN1_TEMPLATE tt;		/* item for this value */
376 };
377 
378 /* template flags */
379 
380 /* Field is optional */
381 #define ASN1_TFLG_OPTIONAL	(0x1)
382 
383 /* Field is a SET OF */
384 #define ASN1_TFLG_SET_OF	(0x1 << 1)
385 
386 /* Field is a SEQUENCE OF */
387 #define ASN1_TFLG_SEQUENCE_OF	(0x2 << 1)
388 
389 /* Mask for SET OF or SEQUENCE OF */
390 #define ASN1_TFLG_SK_MASK	(0x3 << 1)
391 
392 /* These flags mean the tag should be taken from the
393  * tag field. If EXPLICIT then the underlying type
394  * is used for the inner tag.
395  */
396 
397 /* IMPLICIT tagging */
398 #define ASN1_TFLG_IMPTAG	(0x1 << 3)
399 
400 
401 /* EXPLICIT tagging, inner tag from underlying type */
402 #define ASN1_TFLG_EXPTAG	(0x2 << 3)
403 
404 #define ASN1_TFLG_TAG_MASK	(0x3 << 3)
405 
406 /* context specific IMPLICIT */
407 #define ASN1_TFLG_IMPLICIT	ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
408 
409 /* context specific EXPLICIT */
410 #define ASN1_TFLG_EXPLICIT	ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
411 
412 /* If tagging is in force these determine the
413  * type of tag to use. Otherwise the tag is
414  * determined by the underlying type. These
415  * values reflect the actual octet format.
416  */
417 
418 /* Universal tag */
419 #define ASN1_TFLG_UNIVERSAL	(0x0<<6)
420 /* Application tag */
421 #define ASN1_TFLG_APPLICATION	(0x1<<6)
422 /* Context specific tag */
423 #define ASN1_TFLG_CONTEXT	(0x2<<6)
424 /* Private tag */
425 #define ASN1_TFLG_PRIVATE	(0x3<<6)
426 
427 #define ASN1_TFLG_TAG_CLASS	(0x3<<6)
428 
429 /* These are for ANY DEFINED BY type. In this case
430  * the 'item' field points to an ASN1_ADB structure
431  * which contains a table of values to decode the
432  * relevant type
433  */
434 
435 #define ASN1_TFLG_ADB_MASK	(0x3<<8)
436 
437 #define ASN1_TFLG_ADB_OID	(0x1<<8)
438 
439 /* This is the actual ASN1 item itself */
440 
441 struct ASN1_ITEM_st {
442 char itype;			/* The item type, primitive, SEQUENCE, CHOICE or extern */
443 int utype;			/* underlying type */
444 const ASN1_TEMPLATE *templates;	/* If SEQUENCE or CHOICE this contains the contents */
445 long tcount;			/* Number of templates if SEQUENCE or CHOICE */
446 const void *funcs;		/* functions that handle this type */
447 long size;			/* Structure size (usually)*/
448 const char *sname;		/* Structure name */
449 };
450 
451 /* These are values for the itype field and
452  * determine how the type is interpreted.
453  *
454  * For PRIMITIVE types the underlying type
455  * determines the behaviour if items is NULL.
456  *
457  * Otherwise templates must contain a single
458  * template and the type is treated in the
459  * same way as the type specified in the template.
460  *
461  * For SEQUENCE types the templates field points
462  * to the members, the size field is the
463  * structure size.
464  *
465  * For CHOICE types the templates field points
466  * to each possible member (typically a union)
467  * and the 'size' field is the offset of the
468  * selector.
469  *
470  * The 'funcs' field is used for application
471  * specific functions.
472  *
473  * The EXTERN type uses a new style d2i/i2d.
474  * The new style should be used where possible
475  * because it avoids things like the d2i IMPLICIT
476  * hack.
477  *
478  * MSTRING is a multiple string type, it is used
479  * for a CHOICE of character strings where the
480  * actual strings all occupy an ASN1_STRING
481  * structure. In this case the 'utype' field
482  * has a special meaning, it is used as a mask
483  * of acceptable types using the B_ASN1 constants.
484  *
485  */
486 
487 #define ASN1_ITYPE_PRIMITIVE		0x0
488 
489 #define ASN1_ITYPE_SEQUENCE		0x1
490 
491 #define ASN1_ITYPE_CHOICE		0x2
492 
493 #define ASN1_ITYPE_EXTERN		0x4
494 
495 #define ASN1_ITYPE_MSTRING		0x5
496 
497 /* Deprecated tag and length cache */
498 struct ASN1_TLC_st;
499 
500 /* This is the ASN1_AUX structure: it handles various
501  * miscellaneous requirements. For example the use of
502  * reference counts and an informational callback.
503  *
504  * The "informational callback" is called at various
505  * points during the ASN1 encoding and decoding. It can
506  * be used to provide minor customisation of the structures
507  * used. This is most useful where the supplied routines
508  * *almost* do the right thing but need some extra help
509  * at a few points. If the callback returns zero then
510  * it is assumed a fatal error has occurred and the
511  * main operation should be abandoned.
512  *
513  * If major changes in the default behaviour are required
514  * then an external type is more appropriate.
515  */
516 
517 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
518 				void *exarg);
519 
520 typedef struct ASN1_AUX_st {
521 	void *app_data;
522 	uint32_t flags;
523 	int ref_offset;		/* Offset of reference value */
524 	ASN1_aux_cb *asn1_cb;
525 	int enc_offset;		/* Offset of ASN1_ENCODING structure */
526 } ASN1_AUX;
527 
528 /* Flags in ASN1_AUX */
529 
530 /* Use a reference count */
531 #define ASN1_AFLG_REFCOUNT	1
532 /* Save the encoding of structure (useful for signatures) */
533 #define ASN1_AFLG_ENCODING	2
534 
535 /* operation values for asn1_cb */
536 
537 #define ASN1_OP_NEW_PRE		0
538 #define ASN1_OP_NEW_POST	1
539 #define ASN1_OP_FREE_PRE	2
540 #define ASN1_OP_FREE_POST	3
541 #define ASN1_OP_D2I_PRE		4
542 #define ASN1_OP_D2I_POST	5
543 /* ASN1_OP_I2D_PRE and ASN1_OP_I2D_POST are not supported. We leave the
544  * constants undefined so code relying on them does not accidentally compile. */
545 #define ASN1_OP_PRINT_PRE	8
546 #define ASN1_OP_PRINT_POST	9
547 #define ASN1_OP_STREAM_PRE	10
548 #define ASN1_OP_STREAM_POST	11
549 #define ASN1_OP_DETACHED_PRE	12
550 #define ASN1_OP_DETACHED_POST	13
551 
552 /* Macro to implement a primitive type */
553 #define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
554 #define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
555 				ASN1_ITEM_start(itname) \
556 					ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
557 				ASN1_ITEM_end(itname)
558 
559 /* Macro to implement a multi string type */
560 #define IMPLEMENT_ASN1_MSTRING(itname, mask) \
561 				ASN1_ITEM_start(itname) \
562 					ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
563 				ASN1_ITEM_end(itname)
564 
565 #define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
566 	ASN1_ITEM_start(sname) \
567 		ASN1_ITYPE_EXTERN, \
568 		tag, \
569 		NULL, \
570 		0, \
571 		&fptrs, \
572 		0, \
573 		#sname \
574 	ASN1_ITEM_end(sname)
575 
576 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
577 
578 #define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
579 
580 #define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
581 
582 #define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
583 			IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
584 
585 #define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
586 		IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
587 
588 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
589 		IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
590 
591 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
592 	pre stname *fname##_new(void) \
593 	{ \
594 		return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
595 	} \
596 	pre void fname##_free(stname *a) \
597 	{ \
598 		ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
599 	}
600 
601 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
602 	stname *fname##_new(void) \
603 	{ \
604 		return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
605 	} \
606 	void fname##_free(stname *a) \
607 	{ \
608 		ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
609 	}
610 
611 #define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
612 	IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
613 	IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
614 
615 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
616 	stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
617 	{ \
618 		return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
619 	} \
620 	int i2d_##fname(stname *a, unsigned char **out) \
621 	{ \
622 		return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
623 	}
624 
625 /* This includes evil casts to remove const: they will go away when full
626  * ASN1 constification is done.
627  */
628 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
629 	stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
630 	{ \
631 		return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
632 	} \
633 	int i2d_##fname(const stname *a, unsigned char **out) \
634 	{ \
635 		return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
636 	}
637 
638 #define IMPLEMENT_ASN1_DUP_FUNCTION(stname)          \
639   stname *stname##_dup(stname *x) {                  \
640     return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
641   }
642 
643 #define IMPLEMENT_ASN1_DUP_FUNCTION_const(stname)            \
644   stname *stname##_dup(const stname *x) {                    \
645     return ASN1_item_dup(ASN1_ITEM_rptr(stname), (void *)x); \
646   }
647 
648 #define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
649 		IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
650 
651 #define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
652 	IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
653 	IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
654 
655 /* external definitions for primitive types */
656 
657 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
658 
659 DEFINE_STACK_OF(ASN1_VALUE)
660 
661 
662 #if defined(__cplusplus)
663 }  // extern "C"
664 #endif
665 
666 #endif  // OPENSSL_HEADER_ASN1T_H
667