Tolerate a SEQUENCE in DN components.
[openssl.git] / crypto / asn1 / asn1_par.c
index 5a628a4bc244c3bd6e9708249f79411f0027eb33..501b62a4b1993b4a957098f11ac54a262f746ccc 100644 (file)
@@ -64,8 +64,8 @@
 
 static int asn1_print_info(BIO *bp, int tag, int xclass,int constructed,
        int indent);
-static int asn1_parse2(BIO *bp, unsigned char **pp, long length,
-       int offset, int depth, int indent);
+static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
+       int offset, int depth, int indent, int dump);
 static int asn1_print_info(BIO *bp, int tag, int xclass, int constructed,
             int indent)
        {
@@ -79,21 +79,19 @@ static int asn1_print_info(BIO *bp, int tag, int xclass, int constructed,
        else
                p="prim: ";
        if (BIO_write(bp,p,6) < 6) goto err;
-       if (indent)
-               {
-               if (indent > 128) indent=128;
-               memset(str,' ',indent);
-               if (BIO_write(bp,str,indent) < indent) goto err;
-               }
+       BIO_indent(bp,indent,128);
 
        p=str;
        if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
-               sprintf(str,"priv [ %d ] ",tag);
+               BIO_snprintf(str,sizeof str,"priv [ %d ] ",tag);
        else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
-               sprintf(str,"cont [ %d ]",tag);
+               BIO_snprintf(str,sizeof str,"cont [ %d ]",tag);
        else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
-               sprintf(str,"appl [ %d ]",tag);
-       else p = ASN1_tag2str(tag);
+               BIO_snprintf(str,sizeof str,"appl [ %d ]",tag);
+       else if (tag > 30)
+               BIO_snprintf(str,sizeof str,"<ASN1 %d>",tag);
+       else
+               p = ASN1_tag2str(tag);
 
        if (p2 != NULL)
                {
@@ -108,22 +106,33 @@ err:
        return(0);
        }
 
-int ASN1_parse(BIO *bp, unsigned char *pp, long len, int indent)
+int ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent)
        {
-       return(asn1_parse2(bp,&pp,len,0,0,indent));
+       return(asn1_parse2(bp,&pp,len,0,0,indent,0));
        }
 
-static int asn1_parse2(BIO *bp, unsigned char **pp, long length, int offset,
-            int depth, int indent)
+int ASN1_parse_dump(BIO *bp, const unsigned char *pp, long len, int indent, int dump)
        {
-       unsigned char *p,*ep,*tot,*op,*opp;
+       return(asn1_parse2(bp,&pp,len,0,0,indent,dump));
+       }
+
+static int asn1_parse2(BIO *bp, const unsigned char **pp, long length, int offset,
+            int depth, int indent, int dump)
+       {
+       const unsigned char *p,*ep,*tot,*op,*opp;
        long len;
        int tag,xclass,ret=0;
        int nl,hl,j,r;
        ASN1_OBJECT *o=NULL;
        ASN1_OCTET_STRING *os=NULL;
        /* ASN1_BMPSTRING *bmp=NULL;*/
+       int dump_indent;
 
+#if 0
+       dump_indent = indent;
+#else
+       dump_indent = 6;        /* Because we know BIO_dump_indent() */
+#endif
        p= *pp;
        tot=p+length;
        op=p-1;
@@ -178,7 +187,7 @@ static int asn1_parse2(BIO *bp, unsigned char **pp, long length, int offset,
                                        {
                                        r=asn1_parse2(bp,&p,(long)(tot-p),
                                                offset+(p - *pp),depth+1,
-                                               indent);
+                                               indent,dump);
                                        if (r == 0) { ret=0; goto end; }
                                        if ((r == 2) || (p >= tot)) break;
                                        }
@@ -188,7 +197,7 @@ static int asn1_parse2(BIO *bp, unsigned char **pp, long length, int offset,
                                        {
                                        r=asn1_parse2(bp,&p,(long)len,
                                                offset+(p - *pp),depth+1,
-                                               indent);
+                                               indent,dump);
                                        if (r == 0) { ret=0; goto end; }
                                        }
                        }
@@ -209,7 +218,7 @@ static int asn1_parse2(BIO *bp, unsigned char **pp, long length, int offset,
                                {
                                if (BIO_write(bp,":",1) <= 0) goto end;
                                if ((len > 0) &&
-                                       BIO_write(bp,(char *)p,(int)len)
+                                       BIO_write(bp,(const char *)p,(int)len)
                                        != (int)len)
                                        goto end;
                                }
@@ -250,9 +259,11 @@ static int asn1_parse2(BIO *bp, unsigned char **pp, long length, int offset,
 
                                opp=op;
                                os=d2i_ASN1_OCTET_STRING(NULL,&opp,len+hl);
-                               if (os != NULL)
+                               if (os != NULL && os->length > 0)
                                        {
-                                       opp=os->data;
+                                       opp = os->data;
+                                       /* testing whether the octet string is
+                                        * printable */
                                        for (i=0; i<os->length; i++)
                                                {
                                                if ((   (opp[i] < ' ') &&
@@ -265,15 +276,48 @@ static int asn1_parse2(BIO *bp, unsigned char **pp, long length, int offset,
                                                        break;
                                                        }
                                                }
-                                       if (printable && (os->length > 0))
+                                       if (printable)
+                                       /* printable string */
                                                {
                                                if (BIO_write(bp,":",1) <= 0)
                                                        goto end;
-                                               if (BIO_write(bp,(char *)opp,
+                                               if (BIO_write(bp,(const char *)opp,
                                                        os->length) <= 0)
                                                        goto end;
                                                }
-                                       ASN1_OCTET_STRING_free(os);
+                                       else if (!dump)
+                                       /* not printable => print octet string
+                                        * as hex dump */
+                                               {
+                                               if (BIO_write(bp,"[HEX DUMP]:",11) <= 0)
+                                                       goto end;
+                                               for (i=0; i<os->length; i++)
+                                                       {
+                                                       if (BIO_printf(bp,"%02X"
+                                                               , opp[i]) <= 0)
+                                                               goto end;
+                                                       }
+                                               }
+                                       else
+                                       /* print the normal dump */
+                                               {
+                                               if (!nl) 
+                                                       {
+                                                       if (BIO_write(bp,"\n",1) <= 0)
+                                                               goto end;
+                                                       }
+                                               if (BIO_dump_indent(bp,
+                                                       (const char *)opp,
+                                                       ((dump == -1 || dump > 
+                                                       os->length)?os->length:dump),
+                                                       dump_indent) <= 0)
+                                                       goto end;
+                                               nl=1;
+                                               }
+                                       }
+                               if (os != NULL)
+                                       {
+                                       M_ASN1_OCTET_STRING_free(os);
                                        os=NULL;
                                        }
                                }
@@ -307,7 +351,7 @@ static int asn1_parse2(BIO *bp, unsigned char **pp, long length, int offset,
                                        if (BIO_write(bp,"BAD INTEGER",11) <= 0)
                                                goto end;
                                        }
-                               ASN1_INTEGER_free(bs);
+                               M_ASN1_INTEGER_free(bs);
                                }
                        else if (tag == V_ASN1_ENUMERATED)
                                {
@@ -339,7 +383,20 @@ static int asn1_parse2(BIO *bp, unsigned char **pp, long length, int offset,
                                        if (BIO_write(bp,"BAD ENUMERATED",11) <= 0)
                                                goto end;
                                        }
-                               ASN1_ENUMERATED_free(bs);
+                               M_ASN1_ENUMERATED_free(bs);
+                               }
+                       else if (len > 0 && dump)
+                               {
+                               if (!nl) 
+                                       {
+                                       if (BIO_write(bp,"\n",1) <= 0)
+                                               goto end;
+                                       }
+                               if (BIO_dump_indent(bp,(const char *)p,
+                                       ((dump == -1 || dump > len)?len:dump),
+                                       dump_indent) <= 0)
+                                       goto end;
+                               nl=1;
                                }
 
                        if (!nl) 
@@ -358,14 +415,14 @@ static int asn1_parse2(BIO *bp, unsigned char **pp, long length, int offset,
        ret=1;
 end:
        if (o != NULL) ASN1_OBJECT_free(o);
-       if (os != NULL) ASN1_OCTET_STRING_free(os);
+       if (os != NULL) M_ASN1_OCTET_STRING_free(os);
        *pp=p;
        return(ret);
        }
 
 const char *ASN1_tag2str(int tag)
 {
-       const static char *tag2str[] = {
+       static const char *tag2str[] = {
         "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING", /* 0-4 */
         "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL", /* 5-9 */
         "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>",      /* 10-13 */