Remove FIPSCANISTERINTERNAL reference.
[openssl.git] / apps / asn1pars.c
index c28b5ec194dcfb5e55292cf0f24c595f1876e465..42f37d7819a36b4811aeaf560e4bd69abb181a9c 100644 (file)
@@ -56,7 +56,7 @@
  * [including the GNU Public Licence.]
  */
 
-/* A nice addition from Dr Stephen Henson <shenson@bigfoot.com> to 
+/* A nice addition from Dr Stephen Henson <steve@openssl.org> to 
  * add the -strparse option which parses nested binary structures
  */
 
@@ -80,6 +80,7 @@
 #undef PROG
 #define PROG   asn1parse_main
 
+
 int MAIN(int, char **);
 
 static int do_generate(BIO *bio, char *genstr, char *genconf, BUF_MEM *buf);
@@ -90,12 +91,13 @@ int MAIN(int argc, char **argv)
        unsigned int length=0;
        long num,tmplen;
        BIO *in=NULL,*out=NULL,*b64=NULL, *derout = NULL;
-       int informat,indent=0, noout = 0, dump = 0;
-       char *infile=NULL,*str=NULL,*prog,*oidfile=NULL, *derfile=NULL;
+       int informat,indent=0, noout = 0, dump = 0, strictpem = 0;
+       char *infile=NULL,*str=NULL,*prog,*oidfile=NULL, *derfile=NULL, *name=NULL, *header=NULL;
        char *genstr=NULL, *genconf=NULL;
        unsigned char *tmpbuf;
+       const unsigned char *ctmpbuf;
        BUF_MEM *buf=NULL;
-       STACK *osk=NULL;
+       STACK_OF(OPENSSL_STRING) *osk=NULL;
        ASN1_TYPE *at=NULL;
 
        informat=FORMAT_PEM;
@@ -112,7 +114,7 @@ int MAIN(int argc, char **argv)
        prog=argv[0];
        argc--;
        argv++;
-       if ((osk=sk_new_null()) == NULL)
+       if ((osk=sk_OPENSSL_STRING_new_null()) == NULL)
                {
                BIO_printf(bio_err,"Memory allocation failure\n");
                goto end;
@@ -168,7 +170,7 @@ int MAIN(int argc, char **argv)
                else if (strcmp(*argv,"-strparse") == 0)
                        {
                        if (--argc < 1) goto bad;
-                       sk_push(osk,*(++argv));
+                       sk_OPENSSL_STRING_push(osk,*(++argv));
                        }
                else if (strcmp(*argv,"-genstr") == 0)
                        {
@@ -180,6 +182,11 @@ int MAIN(int argc, char **argv)
                        if (--argc < 1) goto bad;
                        genconf= *(++argv);
                        }
+               else if (strcmp(*argv,"-strictpem") == 0)
+                       {
+                       strictpem = 1;
+                       informat = FORMAT_PEM;
+                       }
                else
                        {
                        BIO_printf(bio_err,"unknown option %s\n",*argv);
@@ -195,7 +202,7 @@ int MAIN(int argc, char **argv)
 bad:
                BIO_printf(bio_err,"%s [options] <infile\n",prog);
                BIO_printf(bio_err,"where options are\n");
-               BIO_printf(bio_err," -inform arg   input format - one of DER TXT PEM\n");
+               BIO_printf(bio_err," -inform arg   input format - one of DER PEM\n");
                BIO_printf(bio_err," -in arg       input file\n");
                BIO_printf(bio_err," -out arg      output file (output format is always DER\n");
                BIO_printf(bio_err," -noout arg    don't produce any output\n");
@@ -210,6 +217,8 @@ bad:
                BIO_printf(bio_err,"               ASN1 blob wrappings\n");
                BIO_printf(bio_err," -genstr str   string to generate ASN1 structure from\n");
                BIO_printf(bio_err," -genconf file file to generate ASN1 structure from\n");
+               BIO_printf(bio_err," -strictpem    do not attempt base64 decode outside PEM markers (-inform \n");
+               BIO_printf(bio_err,"               will be ignored)\n");
                goto end;
                }
 
@@ -260,64 +269,80 @@ bad:
                }
        }
 
-       if ((buf=BUF_MEM_new()) == NULL) goto end;
-       if (!BUF_MEM_grow(buf,BUFSIZ*8)) goto end; /* Pre-allocate :-) */
-
-       if (genstr || genconf)
+       if(strictpem)
                {
-               num = do_generate(bio_err, genstr, genconf, buf);
-               if (num < 0)
+               if(PEM_read_bio(in, &name, &header, (unsigned char **)&str, &num) != 1)
                        {
+                       BIO_printf(bio_err,"Error reading PEM file\n");
                        ERR_print_errors(bio_err);
                        goto end;
                        }
                }
-
        else
                {
 
-               if (informat == FORMAT_PEM)
-                       {
-                       BIO *tmp;
+               if ((buf=BUF_MEM_new()) == NULL) goto end;
+               if (!BUF_MEM_grow(buf,BUFSIZ*8)) goto end; /* Pre-allocate :-) */
 
-                       if ((b64=BIO_new(BIO_f_base64())) == NULL)
+               if (genstr || genconf)
+                       {
+                       num = do_generate(bio_err, genstr, genconf, buf);
+                       if (num < 0)
+                               {
+                               ERR_print_errors(bio_err);
                                goto end;
-                       BIO_push(b64,in);
-                       tmp=in;
-                       in=b64;
-                       b64=tmp;
+                               }
                        }
 
-               num=0;
-               for (;;)
+               else
                        {
-                       if (!BUF_MEM_grow(buf,(int)num+BUFSIZ)) goto end;
-                       i=BIO_read(in,&(buf->data[num]),BUFSIZ);
-                       if (i <= 0) break;
-                       num+=i;
+
+                       if (informat == FORMAT_PEM)
+                               {
+                               BIO *tmp;
+
+                               if ((b64=BIO_new(BIO_f_base64())) == NULL)
+                                       goto end;
+                               BIO_push(b64,in);
+                               tmp=in;
+                               in=b64;
+                               b64=tmp;
+                               }
+
+                       num=0;
+                       for (;;)
+                               {
+                               if (!BUF_MEM_grow(buf,(int)num+BUFSIZ)) goto end;
+                               i=BIO_read(in,&(buf->data[num]),BUFSIZ);
+                               if (i <= 0) break;
+                               num+=i;
+                               }
                        }
+               str=buf->data;
+
                }
-       str=buf->data;
 
        /* If any structs to parse go through in sequence */
 
-       if (sk_num(osk))
+       if (sk_OPENSSL_STRING_num(osk))
                {
                tmpbuf=(unsigned char *)str;
                tmplen=num;
-               for (i=0; i<sk_num(osk); i++)
+               for (i=0; i<sk_OPENSSL_STRING_num(osk); i++)
                        {
                        ASN1_TYPE *atmp;
-                       j=atoi(sk_value(osk,i));
+                       int typ;
+                       j=atoi(sk_OPENSSL_STRING_value(osk,i));
                        if (j == 0)
                                {
-                               BIO_printf(bio_err,"'%s' is an invalid number\n",sk_value(osk,i));
+                               BIO_printf(bio_err,"'%s' is an invalid number\n",sk_OPENSSL_STRING_value(osk,i));
                                continue;
                                }
                        tmpbuf+=j;
                        tmplen-=j;
                        atmp = at;
-                       at = d2i_ASN1_TYPE(NULL,&tmpbuf,tmplen);
+                       ctmpbuf = tmpbuf;
+                       at = d2i_ASN1_TYPE(NULL,&ctmpbuf,tmplen);
                        ASN1_TYPE_free(atmp);
                        if(!at)
                                {
@@ -325,6 +350,15 @@ bad:
                                ERR_print_errors(bio_err);
                                goto end;
                                }
+                       typ = ASN1_TYPE_get(at);
+                       if ((typ == V_ASN1_OBJECT)
+                               || (typ == V_ASN1_NULL))
+                               {
+                               BIO_printf(bio_err, "Can't parse %s type\n",
+                                       typ == V_ASN1_NULL ? "NULL" : "OBJECT");
+                               ERR_print_errors(bio_err);
+                               goto end;
+                               }
                        /* hmm... this is a little evil but it works */
                        tmpbuf=at->value.asn1_string->data;
                        tmplen=at->value.asn1_string->length;
@@ -365,8 +399,11 @@ end:
        if (ret != 0)
                ERR_print_errors(bio_err);
        if (buf != NULL) BUF_MEM_free(buf);
+       if (name != NULL) OPENSSL_free(name);
+       if (header != NULL) OPENSSL_free(header);
+       if (strictpem && str != NULL) OPENSSL_free(str);
        if (at != NULL) ASN1_TYPE_free(at);
-       if (osk != NULL) sk_free(osk);
+       if (osk != NULL) sk_OPENSSL_STRING_free(osk);
        OBJ_cleanup();
        apps_shutdown();
        OPENSSL_EXIT(ret);
@@ -396,6 +433,7 @@ static int do_generate(BIO *bio, char *genstr, char *genconf, BUF_MEM *buf)
 
        atyp = ASN1_generate_nconf(genstr, cnf);
        NCONF_free(cnf);
+       cnf = NULL;
 
        if (!atyp)
                return -1;