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