Print out zero length string properly.
[openssl.git] / crypto / asn1 / tasn_prn.c
index 65c623647a65047a13f0355b7efb8d7e72ac57af..cf4c4ab3740f93b36a9fc8f9524e42b452d9df5d 100644 (file)
@@ -58,6 +58,7 @@
 
 
 #include <stddef.h>
+#include "cryptlib.h"
 #include <openssl/asn1.h>
 #include <openssl/asn1t.h>
 #include <openssl/objects.h>
 #include <openssl/x509v3.h>
 #include "asn1_locl.h"
 
-#include <string.h>
-/* Print flags */
-
-/* Indicate missing OPTIONAL fields */
-#define ASN1_PCTX_FLAGS_SHOW_ABSENT            0x001   
-/* Mark start and end of SEQUENCE */
-#define ASN1_PCTX_FLAGS_SHOW_SEQUENCE          0x002
-/* Mark start and end of SEQUENCE/SET OF */
-#define ASN1_PCTX_FLAGS_SHOW_SSOF              0x004
-/* Show the ASN1 type of primitives */
-#define ASN1_PCTX_FLAGS_SHOW_TYPE              0x008
-/* Don't show ASN1 type of ANY */
-#define ASN1_PCTX_FLAGS_NO_ANY_TYPE            0x010
-/* Don't show ASN1 type of MSTRINGs */
-#define ASN1_PCTX_FLAGS_NO_MSTRING_TYPE                0x020
-/* Don't show field names in SEQUENCE */
-#define ASN1_PCTX_FLAGS_NO_FIELD_NAME          0x040
-/* Show structure names of each SEQUENCE field */
-#define ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME 0x080
-/* Don't show structure name even at top level */
-#define ASN1_PCTX_FLAGS_NO_STRUCT_NAME         0x100
-
 /* Print routines.
  */
 
@@ -181,12 +160,15 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
                                const ASN1_ITEM *it,
                                const char *fname, const char *sname,
                                int nohdr, const ASN1_PCTX *pctx);
+
 int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
                                const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
+
 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
                                const ASN1_ITEM *it, int indent,
                                const char *fname, const char *sname,
                                const ASN1_PCTX *pctx);
+
 static int asn1_print_fsname(BIO *out, int indent,
                        const char *fname, const char *sname,
                        const ASN1_PCTX *pctx);
@@ -213,12 +195,25 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
        const ASN1_TEMPLATE *tt;
        const ASN1_EXTERN_FUNCS *ef;
        ASN1_VALUE **tmpfld;
+       const ASN1_AUX *aux = it->funcs;
+       ASN1_aux_cb *asn1_cb;
+       ASN1_PRINT_ARG parg;
        int i;
+       if (aux && aux->asn1_cb)
+               {
+               parg.out = out;
+               parg.indent = indent;
+               parg.pctx = pctx;
+               asn1_cb = aux->asn1_cb;
+               }
+       else asn1_cb = 0;
+
        if(*fld == NULL)
                {
                if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT)
                        {
-                       if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
+                       if (!nohdr && !asn1_print_fsname(out, indent,
+                                                       fname, sname, pctx))
                                return 0;
                        if (BIO_puts(out, "<ABSENT>\n") <= 0)
                                return 0;
@@ -300,6 +295,15 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
                                }
                        }
 
+               if (asn1_cb)
+                       {
+                       i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
+                       if (i == 0)
+                               return 0;
+                       if (i == 2)
+                               return 1;
+                       }
+
                /* Print each field entry */
                for(i = 0, tt = it->templates; i < it->tcount; i++, tt++)
                        {
@@ -315,6 +319,13 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
                        if (BIO_printf(out, "%*s}\n", indent, "") < 0)
                                return 0;
                        }
+
+               if (asn1_cb)
+                       {
+                       i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
+                       if (i == 0)
+                               return 0;
+                       }
                break;
 
                default:
@@ -332,7 +343,7 @@ int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
        const char *sname, *fname;
        flags = tt->flags;
        if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
-               sname = tt->item->sname;
+               sname = ASN1_ITEM_ptr(tt->item)->sname;
        else
                sname = NULL;
        if(pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
@@ -367,7 +378,7 @@ int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
 
                        skitem = (ASN1_VALUE *)sk_value((STACK *)*fld, i);
                        if (!asn1_item_print_ctx(out, &skitem, indent + 2,
-                                       tt->item, NULL, NULL, 1, pctx))
+                               ASN1_ITEM_ptr(tt->item), NULL, NULL, 1, pctx))
                                return 0;
                        }
                if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
@@ -379,7 +390,7 @@ int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
                        }
                return 1;
                }
-       return asn1_item_print_ctx(out, fld, indent, tt->item,
+       return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
                                                        fname, sname, 0, pctx);
        }
 
@@ -495,7 +506,8 @@ static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
                }
        else if (BIO_puts(out, "\n") <= 0)
                return 0;
-       if (BIO_dump_indent(out, (char *)str->data, str->length,
+       if ((str->length > 0)
+               && BIO_dump_indent(out, (char *)str->data, str->length,
                                indent + 2) <= 0)
                return 0;
        return 1;