Revert the size_t modifications from HEAD that had led to more
[openssl.git] / crypto / asn1 / asn1t.h
index 5e6efe2cccccc6ff192aece536c0d82a40d39051..d230e4bf70032450a659c6dfff6449e934d50db1 100644 (file)
@@ -1,9 +1,9 @@
 /* asn1t.h */
-/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
+/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  * project 2000.
  */
 /* ====================================================================
- * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 2000-2005 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 extern "C" {
 #endif
 
+
+#ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
+
+/* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
+#define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
+
+
+/* Macros for start and end of ASN1_ITEM definition */
+
+#define ASN1_ITEM_start(itname) \
+       OPENSSL_GLOBAL const ASN1_ITEM itname##_it = {
+
+#define ASN1_ITEM_end(itname) \
+               };
+
+#else
+
+/* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
+#define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr()))
+
+
+/* Macros for start and end of ASN1_ITEM definition */
+
+#define ASN1_ITEM_start(itname) \
+       const ASN1_ITEM * itname##_it(void) \
+       { \
+               static const ASN1_ITEM local_it = { 
+
+#define ASN1_ITEM_end(itname) \
+               }; \
+       return &local_it; \
+       }
+
+#endif
+
+
 /* Macros to aid ASN1 template writing */
 
 #define ASN1_ITEM_TEMPLATE(tname) \
-       const static ASN1_TEMPLATE tname##_item_tt 
+       static const ASN1_TEMPLATE tname##_item_tt 
 
 #define ASN1_ITEM_TEMPLATE_END(tname) \
        ;\
-       OPENSSL_GLOBAL const ASN1_ITEM tname##_it = { \
+       ASN1_ITEM_start(tname) \
                ASN1_ITYPE_PRIMITIVE,\
                -1,\
                &tname##_item_tt,\
@@ -88,7 +124,7 @@ extern "C" {
                NULL,\
                0,\
                #tname \
-       }
+       ASN1_ITEM_end(tname)
 
 
 /* This is a ASN1 type which just embeds a template */
@@ -97,7 +133,7 @@ extern "C" {
  *
  *     ASN1_SEQUENCE(stname) = {
  *             ... SEQUENCE components ...
- *     } ASN1_SEQUENCE_END(stname);
+ *     } ASN1_SEQUENCE_END(stname)
  *
  *     This will produce an ASN1_ITEM called stname_it
  *     for a structure called stname.
@@ -107,20 +143,20 @@ extern "C" {
  *
  *     ASN1_SEQUENCE(itname) = {
  *             ... SEQUENCE components ...
- *     } ASN1_SEQUENCE_END_name(stname, itname);
+ *     } ASN1_SEQUENCE_END_name(stname, itname)
  *
  *     This will create an item called itname_it using
  *     a structure called stname.
  */
 
 #define ASN1_SEQUENCE(tname) \
-       const static ASN1_TEMPLATE tname##_seq_tt[] 
+       static const ASN1_TEMPLATE tname##_seq_tt[] 
 
 #define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
 
 #define ASN1_SEQUENCE_END_name(stname, tname) \
        ;\
-       OPENSSL_GLOBAL const ASN1_ITEM tname##_it = { \
+       ASN1_ITEM_start(tname) \
                ASN1_ITYPE_SEQUENCE,\
                V_ASN1_SEQUENCE,\
                tname##_seq_tt,\
@@ -128,24 +164,42 @@ extern "C" {
                NULL,\
                sizeof(stname),\
                #stname \
-       }
+       ASN1_ITEM_end(tname)
+
+#define ASN1_NDEF_SEQUENCE(tname) \
+       ASN1_SEQUENCE(tname)
+
+#define ASN1_NDEF_SEQUENCE_cb(tname, cb) \
+       ASN1_SEQUENCE_cb(tname, cb)
 
 #define ASN1_SEQUENCE_cb(tname, cb) \
-       const static ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
+       static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
        ASN1_SEQUENCE(tname)
 
 #define ASN1_BROKEN_SEQUENCE(tname) \
-       const static ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
+       static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
        ASN1_SEQUENCE(tname)
 
 #define ASN1_SEQUENCE_ref(tname, cb, lck) \
-       const static ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
+       static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
        ASN1_SEQUENCE(tname)
 
 #define ASN1_SEQUENCE_enc(tname, enc, cb) \
-       const static ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
+       static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
        ASN1_SEQUENCE(tname)
 
+#define ASN1_NDEF_SEQUENCE_END(tname) \
+       ;\
+       ASN1_ITEM_start(tname) \
+               ASN1_ITYPE_NDEF_SEQUENCE,\
+               V_ASN1_SEQUENCE,\
+               tname##_seq_tt,\
+               sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
+               NULL,\
+               sizeof(tname),\
+               #tname \
+       ASN1_ITEM_end(tname)
+
 #define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
 
 #define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
@@ -154,7 +208,7 @@ extern "C" {
 
 #define ASN1_SEQUENCE_END_ref(stname, tname) \
        ;\
-       OPENSSL_GLOBAL const ASN1_ITEM tname##_it = { \
+       ASN1_ITEM_start(tname) \
                ASN1_ITYPE_SEQUENCE,\
                V_ASN1_SEQUENCE,\
                tname##_seq_tt,\
@@ -162,14 +216,26 @@ extern "C" {
                &tname##_aux,\
                sizeof(stname),\
                #stname \
-       }
+       ASN1_ITEM_end(tname)
+
+#define ASN1_NDEF_SEQUENCE_END_cb(stname, tname) \
+       ;\
+       ASN1_ITEM_start(tname) \
+               ASN1_ITYPE_NDEF_SEQUENCE,\
+               V_ASN1_SEQUENCE,\
+               tname##_seq_tt,\
+               sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
+               &tname##_aux,\
+               sizeof(stname),\
+               #stname \
+       ASN1_ITEM_end(tname)
 
 
 /* This pair helps declare a CHOICE type. We can do:
  *
  *     ASN1_CHOICE(chname) = {
  *             ... CHOICE options ...
- *     ASN1_CHOICE_END(chname);
+ *     ASN1_CHOICE_END(chname)
  *
  *     This will produce an ASN1_ITEM called chname_it
  *     for a structure called chname. The structure
@@ -188,10 +254,10 @@ extern "C" {
  */
 
 #define ASN1_CHOICE(tname) \
-       const static ASN1_TEMPLATE tname##_ch_tt[] 
+       static const ASN1_TEMPLATE tname##_ch_tt[] 
 
 #define ASN1_CHOICE_cb(tname, cb) \
-       const static ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
+       static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
        ASN1_CHOICE(tname)
 
 #define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
@@ -200,7 +266,7 @@ extern "C" {
 
 #define ASN1_CHOICE_END_selector(stname, tname, selname) \
        ;\
-       OPENSSL_GLOBAL const ASN1_ITEM tname##_it = { \
+       ASN1_ITEM_start(tname) \
                ASN1_ITYPE_CHOICE,\
                offsetof(stname,selname) ,\
                tname##_ch_tt,\
@@ -208,11 +274,11 @@ extern "C" {
                NULL,\
                sizeof(stname),\
                #stname \
-       }
+       ASN1_ITEM_end(tname)
 
 #define ASN1_CHOICE_END_cb(stname, tname, selname) \
        ;\
-       OPENSSL_GLOBAL const ASN1_ITEM tname##_it = { \
+       ASN1_ITEM_start(tname) \
                ASN1_ITYPE_CHOICE,\
                offsetof(stname,selname) ,\
                tname##_ch_tt,\
@@ -220,13 +286,13 @@ extern "C" {
                &tname##_aux,\
                sizeof(stname),\
                #stname \
-       }
+       ASN1_ITEM_end(tname)
 
 /* This helps with the template wrapper form of ASN1_ITEM */
 
 #define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
        (flags), (tag), 0,\
-       #name, &(type##_it) }
+       #name, ASN1_ITEM_ref(type) }
 
 /* These help with SEQUENCE or CHOICE components */
 
@@ -234,12 +300,12 @@ extern "C" {
 
 #define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
        (flags), (tag), offsetof(stname, field),\
-       #field, &(type##_it) }
+       #field, ASN1_ITEM_ref(type) }
 
 /* used when the structure is combined with the parent */
 
 #define ASN1_EX_COMBINE(flags, tag, type) { \
-       (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, &(type##_it) }
+       (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) }
 
 /* implicit and explicit helper macros */
 
@@ -251,9 +317,13 @@ extern "C" {
 
 /* Any defined by macros: the field used is in the table itself */
 
-#define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, &(tblname##_adb) }
-#define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, &(tblname##_adb) }
-
+#ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
+#define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
+#define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
+#else
+#define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
+#define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
+#endif
 /* Plain simple type */
 #define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
 
@@ -313,14 +383,24 @@ extern "C" {
 #define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
                        ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
 
+/* EXPLICIT using indefinite length constructed form */
+#define ASN1_NDEF_EXP(stname, field, type, tag) \
+                       ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF)
+
+/* EXPLICIT OPTIONAL using indefinite length constructed form */
+#define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
+                       ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
+
 /* Macros for the ASN1_ADB structure */
 
 #define ASN1_ADB(name) \
-       const static ASN1_ADB_TABLE name##_adbtbl[] 
+       static const ASN1_ADB_TABLE name##_adbtbl[] 
+
+#ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
 
 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \
        ;\
-       const static ASN1_ADB name##_adb = {\
+       static const ASN1_ADB name##_adb = {\
                flags,\
                offsetof(name, field),\
                app_table,\
@@ -330,10 +410,32 @@ extern "C" {
                none\
        }
 
+#else
+
+#define ASN1_ADB_END(name, flags, field, app_table, def, none) \
+       ;\
+       static const ASN1_ITEM *name##_adb(void) \
+       { \
+       static const ASN1_ADB internal_adb = \
+               {\
+               flags,\
+               offsetof(name, field),\
+               app_table,\
+               name##_adbtbl,\
+               sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
+               def,\
+               none\
+               }; \
+               return (const ASN1_ITEM *) &internal_adb; \
+       } \
+       void dummy_function(void)
+
+#endif
+
 #define ADB_ENTRY(val, template) {val, template}
 
 #define ASN1_ADB_TEMPLATE(name) \
-       const static ASN1_TEMPLATE name##_tt 
+       static const ASN1_TEMPLATE name##_tt 
 
 /* This is the ASN1 template structure that defines
  * a wrapper round the actual type. It determines the
@@ -346,11 +448,15 @@ unsigned long flags;              /* Various flags */
 long tag;                      /* tag, not used if no tagging */
 unsigned long offset;          /* Offset of this field in structure */
 #ifndef NO_ASN1_FIELD_NAMES
-char *field_name;              /* Field name */
+const char *field_name;                /* Field name */
 #endif
-const void *item;              /* Relevant ASN1_ITEM or ASN1_ADB */
+ASN1_ITEM_EXP *item;           /* Relevant ASN1_ITEM or ASN1_ADB */
 };
 
+/* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
+
+#define ASN1_TEMPLATE_item(t) (t->item_ptr)
+#define ASN1_TEMPLATE_adb(t) (t->item_ptr)
 
 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
 typedef struct ASN1_ADB_st ASN1_ADB;
@@ -450,6 +556,13 @@ struct ASN1_ADB_TABLE_st {
 
 #define ASN1_TFLG_COMBINE      (0x1<<10)
 
+/* This flag when present in a SEQUENCE OF, SET OF
+ * or EXPLICIT causes indefinite length constructed
+ * encoding to be used if required.
+ */
+
+#define ASN1_TFLG_NDEF         (0x1<<11)
+
 /* This is the actual ASN1 item itself */
 
 struct ASN1_ITEM_st {
@@ -502,19 +615,25 @@ const char *sname;                /* Structure name */
  * has a special meaning, it is used as a mask
  * of acceptable types using the B_ASN1 constants.
  *
+ * NDEF_SEQUENCE is the same as SEQUENCE except
+ * that it will use indefinite length constructed
+ * encoding if requested.
+ *
  */
 
-#define ASN1_ITYPE_PRIMITIVE   0x0
+#define ASN1_ITYPE_PRIMITIVE           0x0
+
+#define ASN1_ITYPE_SEQUENCE            0x1
 
-#define ASN1_ITYPE_SEQUENCE    0x1
+#define ASN1_ITYPE_CHOICE              0x2
 
-#define ASN1_ITYPE_CHOICE      0x2
+#define ASN1_ITYPE_COMPAT              0x3
 
-#define ASN1_ITYPE_COMPAT      0x3
+#define ASN1_ITYPE_EXTERN              0x4
 
-#define ASN1_ITYPE_EXTERN      0x4
+#define ASN1_ITYPE_MSTRING             0x5
 
-#define ASN1_ITYPE_MSTRING     0x5
+#define ASN1_ITYPE_NDEF_SEQUENCE       0x6
 
 /* Cache for ASN1 tag and length, so we
  * don't keep re-reading it for things
@@ -534,18 +653,23 @@ struct ASN1_TLC_st{
 
 typedef ASN1_VALUE * ASN1_new_func(void);
 typedef void ASN1_free_func(ASN1_VALUE *a);
-typedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, unsigned char ** in, long length);
+typedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, const unsigned char ** in, long length);
 typedef int ASN1_i2d_func(ASN1_VALUE * a, unsigned char **in);
 
-typedef int ASN1_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it,
+typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
                                        int tag, int aclass, char opt, ASN1_TLC *ctx);
 
 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
 
+typedef int ASN1_ex_print_func(BIO *out, ASN1_VALUE **pval, 
+                                               int indent, const char *fname, 
+                                               const ASN1_PCTX *pctx);
+
 typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
-typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
+typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
+typedef int ASN1_primitive_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it, int indent, const ASN1_PCTX *pctx);
 
 typedef struct ASN1_COMPAT_FUNCS_st {
        ASN1_new_func *asn1_new;
@@ -561,6 +685,7 @@ typedef struct ASN1_EXTERN_FUNCS_st {
        ASN1_ex_free_func *asn1_ex_clear;
        ASN1_ex_d2i *asn1_ex_d2i;
        ASN1_ex_i2d *asn1_ex_i2d;
+       ASN1_ex_print_func *asn1_ex_print;
 } ASN1_EXTERN_FUNCS;
 
 typedef struct ASN1_PRIMITIVE_FUNCS_st {
@@ -571,6 +696,7 @@ typedef struct ASN1_PRIMITIVE_FUNCS_st {
        ASN1_ex_free_func *prim_clear;
        ASN1_primitive_c2i *prim_c2i;
        ASN1_primitive_i2c *prim_i2c;
+       ASN1_primitive_print *prim_print;
 } ASN1_PRIMITIVE_FUNCS;
 
 /* This is the ASN1_AUX structure: it handles various
@@ -590,7 +716,8 @@ typedef struct ASN1_PRIMITIVE_FUNCS_st {
  * then an external type is more appropriate.
  */
 
-typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it);
+typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it,
+                               void *exarg);
 
 typedef struct ASN1_AUX_st {
        void *app_data;
@@ -601,6 +728,23 @@ typedef struct ASN1_AUX_st {
        int enc_offset;         /* Offset of ASN1_ENCODING structure */
 } ASN1_AUX;
 
+/* For print related callbacks exarg points to this structure */
+typedef struct ASN1_PRINT_ARG_st {
+       BIO *out;
+       int indent;
+       const ASN1_PCTX *pctx;
+} ASN1_PRINT_ARG;
+
+/* For streaming related callbacks exarg points to this structure */
+typedef struct ASN1_STREAM_ARG_st {
+       /* BIO to stream through */
+       BIO *out;
+       /* BIO with filters appended */
+       BIO *ndef_bio;
+       /* Streaming I/O boundary */
+       unsigned char **boundary;
+} ASN1_STREAM_ARG;
+
 /* Flags in ASN1_AUX */
 
 /* Use a reference count */
@@ -620,15 +764,25 @@ typedef struct ASN1_AUX_st {
 #define ASN1_OP_D2I_POST       5
 #define ASN1_OP_I2D_PRE                6
 #define ASN1_OP_I2D_POST       7
+#define ASN1_OP_PRINT_PRE      8
+#define ASN1_OP_PRINT_POST     9
+#define ASN1_OP_STREAM_PRE     10
+#define ASN1_OP_STREAM_POST    11
+#define ASN1_OP_DETACHED_PRE   12
+#define ASN1_OP_DETACHED_POST  13
 
 /* Macro to implement a primitive type */
 #define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
-#define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) const ASN1_ITEM itname##_it = \
-                               { ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname};
+#define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
+                               ASN1_ITEM_start(itname) \
+                                       ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
+                               ASN1_ITEM_end(itname)
 
 /* Macro to implement a multi string type */
-#define IMPLEMENT_ASN1_MSTRING(itname, mask) const ASN1_ITEM itname##_it = \
-                               { ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname};
+#define IMPLEMENT_ASN1_MSTRING(itname, mask) \
+                               ASN1_ITEM_start(itname) \
+                                       ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
+                               ASN1_ITEM_end(itname)
 
 /* Macro to implement an ASN1_ITEM in terms of old style funcs */
 
@@ -641,7 +795,7 @@ typedef struct ASN1_AUX_st {
                (ASN1_d2i_func *)d2i_##sname, \
                (ASN1_i2d_func *)i2d_##sname, \
        }; \
-       OPENSSL_GLOBAL ASN1_ITEM const sname##_it = { \
+       ASN1_ITEM_start(sname) \
                ASN1_ITYPE_COMPAT, \
                tag, \
                NULL, \
@@ -649,10 +803,10 @@ typedef struct ASN1_AUX_st {
                &sname##_ff, \
                0, \
                #sname \
-       }
+       ASN1_ITEM_end(sname)
 
 #define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
-       OPENSSL_GLOBAL const ASN1_ITEM sname##_it = { \
+       ASN1_ITEM_start(sname) \
                ASN1_ITYPE_EXTERN, \
                tag, \
                NULL, \
@@ -660,7 +814,7 @@ typedef struct ASN1_AUX_st {
                &fptrs, \
                0, \
                #sname \
-       };
+       ASN1_ITEM_end(sname)
 
 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
 
@@ -671,14 +825,30 @@ typedef struct ASN1_AUX_st {
 #define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
                        IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
 
+#define IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(stname) \
+               IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(static, stname, stname, stname)
+
+#define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
+               IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
+
+#define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_pfname(pre, stname, itname, fname) \
+       pre stname *fname##_new(void) \
+       { \
+               return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
+       } \
+       pre void fname##_free(stname *a) \
+       { \
+               ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
+       }
+
 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
        stname *fname##_new(void) \
        { \
-               return (stname *)ASN1_item_new(&itname##_it); \
+               return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
        } \
        void fname##_free(stname *a) \
        { \
-               ASN1_item_free((ASN1_VALUE *)a, &itname##_it); \
+               ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
        }
 
 #define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
@@ -686,13 +856,19 @@ typedef struct ASN1_AUX_st {
        IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
 
 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
-       stname *d2i_##fname(stname **a, unsigned char **in, long len) \
+       stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
        { \
-               return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, &itname##_it);\
+               return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
        } \
        int i2d_##fname(stname *a, unsigned char **out) \
        { \
-               return ASN1_item_i2d((ASN1_VALUE *)a, out, &itname##_it);\
+               return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
+       } 
+
+#define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
+       int i2d_##stname##_NDEF(stname *a, unsigned char **out) \
+       { \
+               return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
        } 
 
 /* This includes evil casts to remove const: they will go away when full
@@ -701,11 +877,28 @@ typedef struct ASN1_AUX_st {
 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
        stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
        { \
-               return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, (unsigned char **)in, len, &itname##_it);\
+               return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
        } \
        int i2d_##fname(const stname *a, unsigned char **out) \
        { \
-               return ASN1_item_i2d((ASN1_VALUE *)a, out, &itname##_it);\
+               return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
+       } 
+
+#define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
+       stname * stname##_dup(stname *x) \
+        { \
+        return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
+        }
+
+#define IMPLEMENT_ASN1_PRINT_FUNCTION(stname) \
+       IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, stname, stname)
+
+#define IMPLEMENT_ASN1_PRINT_FUNCTION_fname(stname, itname, fname) \
+       int fname##_print_ctx(BIO *out, stname *x, int indent, \
+                                               const ASN1_PCTX *pctx) \
+       { \
+               return ASN1_item_print(out, (ASN1_VALUE *)x, indent, \
+                       ASN1_ITEM_rptr(itname), pctx); \
        } 
 
 #define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
@@ -717,16 +910,14 @@ typedef struct ASN1_AUX_st {
 
 /* external definitions for primitive types */
 
-OPENSSL_EXTERN const ASN1_ITEM ASN1_BOOLEAN_it;
-OPENSSL_EXTERN const ASN1_ITEM ASN1_TBOOLEAN_it;
-OPENSSL_EXTERN const ASN1_ITEM ASN1_FBOOLEAN_it;
-OPENSSL_EXTERN const ASN1_ITEM ASN1_OBJECT_it;
-OPENSSL_EXTERN const ASN1_ITEM ASN1_ANY_it;
-OPENSSL_EXTERN const ASN1_ITEM ASN1_SEQUENCE_it;
-OPENSSL_EXTERN const ASN1_ITEM CBIGNUM_it;
-OPENSSL_EXTERN const ASN1_ITEM BIGNUM_it;
-OPENSSL_EXTERN const ASN1_ITEM LONG_it;
-OPENSSL_EXTERN const ASN1_ITEM ZLONG_it;
+DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
+DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
+DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
+DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
+DECLARE_ASN1_ITEM(CBIGNUM)
+DECLARE_ASN1_ITEM(BIGNUM)
+DECLARE_ASN1_ITEM(LONG)
+DECLARE_ASN1_ITEM(ZLONG)
 
 DECLARE_STACK_OF(ASN1_VALUE)
 
@@ -738,8 +929,8 @@ int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
 int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
 
 void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
-int ASN1_template_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_TEMPLATE *tt);
-int ASN1_item_ex_d2i(ASN1_VALUE **pval, unsigned char **in, long len, const ASN1_ITEM *it,
+int ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_TEMPLATE *tt);
+int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
                                int tag, int aclass, char opt, ASN1_TLC *ctx);
 
 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
@@ -747,7 +938,7 @@ int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_TEMPLAT
 void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
 
 int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
-int asn1_ex_c2i(ASN1_VALUE **pval, unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
+int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
 
 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
 int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
@@ -761,7 +952,7 @@ int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it);
 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it);
-int asn1_enc_save(ASN1_VALUE **pval, unsigned char *in, int inlen, const ASN1_ITEM *it);
+int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, const ASN1_ITEM *it);
 
 #ifdef  __cplusplus
 }