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