2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
58 #ifndef HEADER_ASN1T_H
59 #define HEADER_ASN1T_H
62 #include <openssl/asn1.h>
64 /* ASN1 template defines, structures and functions */
70 /* Macros to aid ASN1 template writing */
72 #define ASN1_ITEM_TEMPLATE(tname) \
73 const static ASN1_TEMPLATE tname##_item_tt
75 #define ASN1_ITEM_TEMPLATE_END(tname) \
77 const ASN1_ITEM tname##_it = { \
78 ASN1_ITYPE_PRIMITIVE,\
88 /* This is a ASN1 type which just embeds a template */
90 /* This pair helps declare a SEQUENCE. We can do:
92 * ASN1_SEQUENCE(stname) = {
93 * ... SEQUENCE components ...
94 * } ASN1_SEQUENCE_END(stname);
96 * This will produce an ASN1_ITEM called stname_it
97 * for a structure called stname.
99 * If you want the same structure but a different
102 * ASN1_SEQUENCE(itname) = {
103 * ... SEQUENCE components ...
104 * } ASN1_SEQUENCE_END_name(stname, itname);
106 * This will create an item called itname_it using
107 * a structure called stname.
110 #define ASN1_SEQUENCE(tname) \
111 const static ASN1_TEMPLATE tname##_seq_tt[]
113 #define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
115 #define ASN1_SEQUENCE_END_name(stname, tname) \
117 const ASN1_ITEM tname##_it = { \
118 ASN1_ITYPE_SEQUENCE,\
121 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
127 #define ASN1_SEQUENCE_cb(tname, cb) \
128 const static ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
131 #define ASN1_BROKEN_SEQUENCE(tname) \
132 const static ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
135 #define ASN1_SEQUENCE_ref(tname, cb, lck) \
136 const static ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
139 #define ASN1_SEQUENCE_enc(tname, enc, cb) \
140 const static ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
143 #define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
145 #define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
147 #define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
149 #define ASN1_SEQUENCE_END_ref(stname, tname) \
151 const ASN1_ITEM tname##_it = { \
152 ASN1_ITYPE_SEQUENCE,\
155 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
162 /* This pair helps declare a CHOICE type. We can do:
164 * ASN1_CHOICE(chname) = {
165 * ... CHOICE options ...
166 * ASN1_CHOICE_END(chname);
168 * This will produce an ASN1_ITEM called chname_it
169 * for a structure called chname. The structure
170 * definition must look like this:
174 * ASN1_SOMETHING *opt1;
175 * ASN1_SOMEOTHER *opt2;
179 * the name of the selector must be 'type'.
180 * to use an alternative selector name use the
181 * ASN1_CHOICE_END_selector() version.
184 #define ASN1_CHOICE(tname) \
185 const static ASN1_TEMPLATE tname##_ch_tt[]
187 #define ASN1_CHOICE_cb(tname, cb) \
188 const static ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
191 #define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
193 #define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
195 #define ASN1_CHOICE_END_selector(stname, tname, selname) \
197 const ASN1_ITEM tname##_it = { \
199 offsetof(stname,selname) ,\
201 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
207 #define ASN1_CHOICE_END_cb(stname, tname, selname) \
209 const ASN1_ITEM tname##_it = { \
211 offsetof(stname,selname) ,\
213 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
219 /* This helps with the template wrapper form of ASN1_ITEM */
221 #define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
223 #name, &(type##_it) }
225 /* These help with SEQUENCE or CHOICE components */
227 /* used to declare other types */
229 #define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
230 (flags), (tag), offsetof(stname, field),\
231 #field, &(type##_it) }
233 /* used when the structure is combined with the parent */
235 #define ASN1_EX_COMBINE(flags, tag, type) { \
236 (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, &(type##_it) }
238 /* implicit and explicit helper macros */
240 #define ASN1_IMP_EX(stname, field, type, tag, ex) \
241 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
243 #define ASN1_EXP_EX(stname, field, type, tag, ex) \
244 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
246 /* Any defined by macros: the field used is in the table itself */
248 #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, &(tblname##_adb) }
249 #define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, &(tblname##_adb) }
251 /* Plain simple type */
252 #define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
254 /* OPTIONAL simple type */
255 #define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
257 /* IMPLICIT tagged simple type */
258 #define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
260 /* IMPLICIT tagged OPTIONAL simple type */
261 #define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
263 /* Same as above but EXPLICIT */
265 #define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
266 #define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
268 /* SEQUENCE OF type */
269 #define ASN1_SEQUENCE_OF(stname, field, type) \
270 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
272 /* OPTIONAL SEQUENCE OF */
273 #define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
274 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
276 /* Same as above but for SET OF */
278 #define ASN1_SET_OF(stname, field, type) \
279 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
281 #define ASN1_SET_OF_OPT(stname, field, type) \
282 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
284 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
286 #define ASN1_IMP_SET_OF(stname, field, type, tag) \
287 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
289 #define ASN1_EXP_SET_OF(stname, field, type, tag) \
290 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
292 #define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
293 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
295 #define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
296 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
298 #define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
299 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
301 #define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
302 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
304 #define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
305 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
307 #define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
308 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
310 /* Macros for the ASN1_ADB structure */
312 #define ASN1_ADB(name) \
313 const static ASN1_ADB_TABLE name##_adbtbl[]
315 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \
317 const static ASN1_ADB name##_adb = {\
319 offsetof(name, field),\
322 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
327 #define ADB_ENTRY(val, template) {val, template}
329 #define ASN1_ADB_TEMPLATE(name) \
330 const static ASN1_TEMPLATE name##_tt
332 /* This is the ASN1 template structure that defines
333 * a wrapper round the actual type. It determines the
334 * actual position of the field in the value structure,
335 * various flags such as OPTIONAL and the field name.
338 struct ASN1_TEMPLATE_st
{
339 unsigned long flags
; /* Various flags */
340 long tag
; /* tag, not used if no tagging */
341 unsigned long offset
; /* Offset of this field in structure */
342 #ifndef NO_ASN1_FIELD_NAMES
343 char *field_name
; /* Field name */
345 const void *item
; /* Relevant ASN1_ITEM or ASN1_ADB */
349 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE
;
350 typedef struct ASN1_ADB_st ASN1_ADB
;
353 unsigned long flags
; /* Various flags */
354 unsigned long offset
; /* Offset of selector field */
355 STACK_OF(ASN1_ADB_TABLE
) **app_items
; /* Application defined items */
356 const ASN1_ADB_TABLE
*tbl
; /* Table of possible types */
357 long tblcount
; /* Number of entries in tbl */
358 const ASN1_TEMPLATE
*default_tt
; /* Type to use if no match */
359 const ASN1_TEMPLATE
*null_tt
; /* Type to use if selector is NULL */
362 struct ASN1_ADB_TABLE_st
{
363 long value
; /* NID for an object or value for an int */
364 const ASN1_TEMPLATE tt
; /* item for this value */
369 /* Field is optional */
370 #define ASN1_TFLG_OPTIONAL (0x1)
372 /* Field is a SET OF */
373 #define ASN1_TFLG_SET_OF (0x1 << 1)
375 /* Field is a SEQUENCE OF */
376 #define ASN1_TFLG_SEQUENCE_OF (0x2 << 1)
378 #define ASN1_TFLG_SK_MASK (0x3 << 1)
380 /* These flags mean the tag should be taken from the
381 * tag field. If EXPLICIT then the underlying type
382 * is used for the inner tag.
385 /* IMPLICIT tagging */
386 #define ASN1_TFLG_IMPTAG (0x1 << 3)
389 /* EXPLICIT tagging, inner tag from underlying type */
390 #define ASN1_TFLG_EXPTAG (0x2 << 3)
392 #define ASN1_TFLG_TAG_MASK (0x3 << 3)
394 /* context specific IMPLICIT */
395 #define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
397 /* context specific EXPLICIT */
398 #define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
400 /* If tagging is in force these determine the
401 * type of tag to use. Otherwise the tag is
402 * determined by the underlying type. These
403 * values reflect the actual octet format.
407 #define ASN1_TFLG_UNIVERSAL (0x0<<6)
408 /* Application tag */
409 #define ASN1_TFLG_APPLICATION (0x1<<6)
410 /* Context specific tag */
411 #define ASN1_TFLG_CONTEXT (0x2<<6)
413 #define ASN1_TFLG_PRIVATE (0x3<<6)
415 #define ASN1_TFLG_TAG_CLASS (0x3<<6)
417 /* These are for ANY DEFINED BY type. In this case
418 * the 'item' field points to an ASN1_ADB structure
419 * which contains a table of values to decode the
423 #define ASN1_TFLG_ADB_MASK (0x3<<8)
425 #define ASN1_TFLG_ADB_OID (0x1<<8)
427 #define ASN1_TFLG_ADB_INT (0x1<<9)
429 /* This flag means a parent structure is passed
430 * instead of the field: this is useful is a
431 * SEQUENCE is being combined with a CHOICE for
432 * example. Since this means the structure and
433 * item name will differ we need to use the
434 * ASN1_CHOICE_END_name() macro for example.
437 #define ASN1_TFLG_COMBINE (0x1<<10)
439 /* This is the actual ASN1 item itself */
441 struct ASN1_ITEM_st
{
442 char itype
; /* The item type, primitive, SEQUENCE, CHOICE or extern */
443 long 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 #ifndef NO_ASN1_FIELD_NAMES
449 const char *sname
; /* Structure name */
453 /* These are values for the itype field and
454 * determine how the type is interpreted.
456 * For PRIMITIVE types the underlying type
457 * determines the behaviour if items is NULL.
459 * Otherwise templates must contain a single
460 * template and the type is treated in the
461 * same way as the type specified in the template.
463 * For SEQUENCE types the templates field points
464 * to the members, the size field is the
467 * For CHOICE types the templates field points
468 * to each possible member (typically a union)
469 * and the 'size' field is the offset of the
472 * The 'funcs' field is used for application
473 * specific functions.
475 * For COMPAT types the funcs field gives a
476 * set of functions that handle this type, this
477 * supports the old d2i, i2d convention.
479 * The EXTERN type uses a new style d2i/i2d.
480 * The new style should be used where possible
481 * because it avoids things like the d2i IMPLICIT
484 * MSTRING is a multiple string type, it is used
485 * for a CHOICE of character strings where the
486 * actual strings all occupy an ASN1_STRING
487 * structure. In this case the 'utype' field
488 * has a special meaning, it is used as a mask
489 * of acceptable types using the B_ASN1 constants.
493 #define ASN1_ITYPE_PRIMITIVE 0x0
495 #define ASN1_ITYPE_SEQUENCE 0x1
497 #define ASN1_ITYPE_CHOICE 0x2
499 #define ASN1_ITYPE_COMPAT 0x3
501 #define ASN1_ITYPE_EXTERN 0x4
503 #define ASN1_ITYPE_MSTRING 0x5
505 /* Cache for ASN1 tag and length, so we
506 * don't keep re-reading it for things
511 char valid
; /* Values below are valid */
512 int ret
; /* return value */
513 long plen
; /* length */
514 int ptag
; /* class value */
515 int pclass
; /* class value */
516 int hdrlen
; /* header length */
519 /* Typedefs for ASN1 function pointers */
521 typedef ASN1_VALUE
* ASN1_new_func(void);
522 typedef void ASN1_free_func(ASN1_VALUE
*a
);
523 typedef ASN1_VALUE
* ASN1_d2i_func(ASN1_VALUE
**a
, unsigned char ** in
, long length
);
524 typedef int ASN1_i2d_func(ASN1_VALUE
* a
, unsigned char **in
);
526 typedef int ASN1_ex_d2i(ASN1_VALUE
**pval
, unsigned char **in
, long len
, const ASN1_ITEM
*it
,
527 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
);
529 typedef int ASN1_ex_i2d(ASN1_VALUE
**pval
, unsigned char **out
, const ASN1_ITEM
*it
, int tag
, int aclass
);
530 typedef int ASN1_ex_new_func(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
);
531 typedef void ASN1_ex_free_func(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
);
533 typedef int ASN1_primitive_i2c(ASN1_VALUE
**pval
, unsigned char *cont
, int *putype
, const ASN1_ITEM
*it
);
534 typedef int ASN1_primitive_c2i(ASN1_VALUE
**pval
, unsigned char *cont
, int len
, int utype
, char *free_cont
, const ASN1_ITEM
*it
);
536 typedef struct ASN1_COMPAT_FUNCS_st
{
537 ASN1_new_func
*asn1_new
;
538 ASN1_free_func
*asn1_free
;
539 ASN1_d2i_func
*asn1_d2i
;
540 ASN1_i2d_func
*asn1_i2d
;
543 typedef struct ASN1_EXTERN_FUNCS_st
{
545 ASN1_ex_new_func
*asn1_ex_new
;
546 ASN1_ex_free_func
*asn1_ex_free
;
547 ASN1_ex_free_func
*asn1_ex_clear
;
548 ASN1_ex_d2i
*asn1_ex_d2i
;
549 ASN1_ex_i2d
*asn1_ex_i2d
;
552 typedef struct ASN1_PRIMITIVE_FUNCS_st
{
555 ASN1_ex_new_func
*prim_new
;
556 ASN1_ex_free_func
*prim_free
;
557 ASN1_ex_free_func
*prim_clear
;
558 ASN1_primitive_c2i
*prim_c2i
;
559 ASN1_primitive_i2c
*prim_i2c
;
560 } ASN1_PRIMITIVE_FUNCS
;
562 /* This is the ASN1_AUX structure: it handles various
563 * miscellaneous requirements. For example the use of
564 * reference counts and an informational callback.
566 * The "informational callback" is called at various
567 * points during the ASN1 encoding and decoding. It can
568 * be used to provide minor customisation of the structures
569 * used. This is most useful where the supplied routines
570 * *almost* do the right thing but need some extra help
571 * at a few points. If the callback returns zero then
572 * it is assumed a fatal error has occurred and the
573 * main operation should be abandoned.
575 * If major changes in the default behaviour are required
576 * then an external type is more appropriate.
579 typedef int ASN1_aux_cb(int operation
, ASN1_VALUE
**in
, const ASN1_ITEM
*it
);
581 typedef struct ASN1_AUX_st
{
584 int ref_offset
; /* Offset of reference value */
585 int ref_lock
; /* Lock type to use */
586 ASN1_aux_cb
*asn1_cb
;
587 int enc_offset
; /* Offset of ASN1_ENCODING structure */
590 /* Flags in ASN1_AUX */
592 /* Use a reference count */
593 #define ASN1_AFLG_REFCOUNT 1
594 /* Save the encoding of structure (useful for signatures) */
595 #define ASN1_AFLG_ENCODING 2
596 /* The Sequence length is invalid */
597 #define ASN1_AFLG_BROKEN 4
599 /* operation values for asn1_cb */
601 #define ASN1_OP_NEW_PRE 0
602 #define ASN1_OP_NEW_POST 1
603 #define ASN1_OP_FREE_PRE 2
604 #define ASN1_OP_FREE_POST 3
605 #define ASN1_OP_D2I_PRE 4
606 #define ASN1_OP_D2I_POST 5
607 #define ASN1_OP_I2D_PRE 6
608 #define ASN1_OP_I2D_POST 7
610 /* Macro to implement a primitive type */
611 #define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
612 #define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) const ASN1_ITEM itname##_it = \
613 { ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname};
615 /* Macro to implement a multi string type */
616 #define IMPLEMENT_ASN1_MSTRING(itname, mask) const ASN1_ITEM itname##_it = \
617 { ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname};
619 /* Macro to implement an ASN1_ITEM in terms of old style funcs */
621 #define IMPLEMENT_COMPAT_ASN1(sname) IMPLEMENT_COMPAT_ASN1_type(sname, V_ASN1_SEQUENCE)
623 #define IMPLEMENT_COMPAT_ASN1_type(sname, tag) \
624 static const ASN1_COMPAT_FUNCS sname##_ff = { \
625 (ASN1_new_func *)sname##_new, \
626 (ASN1_free_func *)sname##_free, \
627 (ASN1_d2i_func *)d2i_##sname, \
628 (ASN1_i2d_func *)i2d_##sname, \
630 ASN1_ITEM const sname##_it = { \
640 #define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
641 const ASN1_ITEM sname##_it = { \
651 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
653 #define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
655 #define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
657 #define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
658 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
660 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
661 stname *fname##_new(void) \
663 return (stname *)ASN1_item_new(&itname##_it); \
665 void fname##_free(stname *a) \
667 ASN1_item_free((ASN1_VALUE *)a, &itname##_it); \
670 #define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
671 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
672 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
674 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
675 stname *d2i_##fname(stname **a, unsigned char **in, long len) \
677 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, &itname##_it);\
679 int i2d_##fname(stname *a, unsigned char **out) \
681 return ASN1_item_i2d((ASN1_VALUE *)a, out, &itname##_it);\
684 /* This includes evil casts to remove const: they will go away when full
685 * ASN1 constification is done.
687 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
688 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
690 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, (unsigned char **)in, len, &itname##_it);\
692 int i2d_##fname(const stname *a, unsigned char **out) \
694 return ASN1_item_i2d((ASN1_VALUE *)a, out, &itname##_it);\
697 #define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
698 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
700 #define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
701 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
702 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
704 /* external definitions for primitive types */
706 extern const ASN1_ITEM ASN1_BOOLEAN_it
;
707 extern const ASN1_ITEM ASN1_TBOOLEAN_it
;
708 extern const ASN1_ITEM ASN1_FBOOLEAN_it
;
709 extern const ASN1_ITEM ASN1_OBJECT_it
;
710 extern const ASN1_ITEM ASN1_ANY_it
;
711 extern const ASN1_ITEM ASN1_SEQUENCE_it
;
712 extern const ASN1_ITEM CBIGNUM_it
;
713 extern const ASN1_ITEM BIGNUM_it
;
714 extern const ASN1_ITEM LONG_it
;
715 extern const ASN1_ITEM ZLONG_it
;
717 DECLARE_STACK_OF(ASN1_VALUE
)
719 /* Functions used internally by the ASN1 code */
721 int ASN1_item_ex_new(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
);
722 void ASN1_item_ex_free(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
);
723 int ASN1_template_new(ASN1_VALUE
**pval
, const ASN1_TEMPLATE
*tt
);
724 int ASN1_primitive_new(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
);
726 void ASN1_template_free(ASN1_VALUE
**pval
, const ASN1_TEMPLATE
*tt
);
727 int ASN1_template_d2i(ASN1_VALUE
**pval
, unsigned char **in
, long len
, const ASN1_TEMPLATE
*tt
);
728 int ASN1_item_ex_d2i(ASN1_VALUE
**pval
, unsigned char **in
, long len
, const ASN1_ITEM
*it
,
729 int tag
, int aclass
, char opt
, ASN1_TLC
*ctx
);
731 int ASN1_item_ex_i2d(ASN1_VALUE
**pval
, unsigned char **out
, const ASN1_ITEM
*it
, int tag
, int aclass
);
732 int ASN1_template_i2d(ASN1_VALUE
**pval
, unsigned char **out
, const ASN1_TEMPLATE
*tt
);
733 void ASN1_primitive_free(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
);
735 int asn1_ex_i2c(ASN1_VALUE
**pval
, unsigned char *cont
, int *putype
, const ASN1_ITEM
*it
);
736 int asn1_ex_c2i(ASN1_VALUE
**pval
, unsigned char *cont
, int len
, int utype
, char *free_cont
, const ASN1_ITEM
*it
);
738 int asn1_get_choice_selector(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
);
739 int asn1_set_choice_selector(ASN1_VALUE
**pval
, int value
, const ASN1_ITEM
*it
);
741 ASN1_VALUE
** asn1_get_field_ptr(ASN1_VALUE
**pval
, const ASN1_TEMPLATE
*tt
);
743 const ASN1_TEMPLATE
*asn1_do_adb(ASN1_VALUE
**pval
, const ASN1_TEMPLATE
*tt
, int nullerr
);
745 int asn1_do_lock(ASN1_VALUE
**pval
, int op
, const ASN1_ITEM
*it
);
747 void asn1_enc_init(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
);
748 void asn1_enc_free(ASN1_VALUE
**pval
, const ASN1_ITEM
*it
);
749 int asn1_enc_restore(int *len
, unsigned char **out
, ASN1_VALUE
**pval
, const ASN1_ITEM
*it
);
750 int asn1_enc_save(ASN1_VALUE
**pval
, unsigned char *in
, int inlen
, const ASN1_ITEM
*it
);