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