Changed -strictpem to use PEM_read_bio
authorMatt Caswell <matt@openssl.org>
Mon, 26 May 2014 22:22:08 +0000 (23:22 +0100)
committerMatt Caswell <matt@openssl.org>
Mon, 26 May 2014 22:31:37 +0000 (23:31 +0100)
apps/asn1pars.c
doc/apps/asn1parse.pod

index dcdf628d404d79a915d6c004206e049ce351ed5d..42f37d7819a36b4811aeaf560e4bd69abb181a9c 100644 (file)
@@ -80,8 +80,6 @@
 #undef PROG
 #define PROG   asn1parse_main
 
 #undef PROG
 #define PROG   asn1parse_main
 
-/* Minimum buffer size to be used */
-#define MIN_BUFFER     256
 
 int MAIN(int, char **);
 
 
 int MAIN(int, char **);
 
@@ -94,7 +92,7 @@ int MAIN(int argc, char **argv)
        long num,tmplen;
        BIO *in=NULL,*out=NULL,*b64=NULL, *derout = NULL;
        int informat,indent=0, noout = 0, dump = 0, strictpem = 0;
        long num,tmplen;
        BIO *in=NULL,*out=NULL,*b64=NULL, *derout = NULL;
        int informat,indent=0, noout = 0, dump = 0, strictpem = 0;
-       char *infile=NULL,*str=NULL,*prog,*oidfile=NULL, *derfile=NULL;
+       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;
        char *genstr=NULL, *genconf=NULL;
        unsigned char *tmpbuf;
        const unsigned char *ctmpbuf;
@@ -271,76 +269,58 @@ bad:
                }
        }
 
                }
        }
 
-       if ((buf=BUF_MEM_new()) == NULL) goto end;
-       if (!BUF_MEM_grow(buf,(BUFSIZ*8)<MIN_BUFFER?MIN_BUFFER:(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;
                        }
                }
                        ERR_print_errors(bio_err);
                        goto end;
                        }
                }
-
        else
                {
 
        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(strictpem)
+               if (genstr || genconf)
+                       {
+                       num = do_generate(bio_err, genstr, genconf, buf);
+                       if (num < 0)
                                {
                                {
-                               for (;;)
-                                       {
-                                       /* Read a line */
-                                       i=BIO_gets(in,buf->data,MIN_BUFFER-1);
-
-                                       if (i <= 0)
-                                               {
-                                               BIO_printf(bio_err, "Error: Cannot find start line\n");
-                                               goto end;
-                                               }
-
-                                       /* Strip trailing spaces etc */
-                                       do
-                                               i--;
-                                       while ((i >= 0) && (buf->data[i]  <= ' '));
-
-                                       buf->data[++i]='\0';
-
-                                       /* Check if we have a PEM BEGIN marker */
-                                       if (strncmp(buf->data,"-----BEGIN ",11) == 0)
-                                               {
-                                               if (strncmp(&(buf->data[i-5]),"-----",5) != 0)
-                                                       continue;
-                                               break;
-                                               }
-                                       }
+                               ERR_print_errors(bio_err);
+                               goto end;
                                }
                                }
+                       }
 
 
+               else
+                       {
 
 
+                       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;
+                               }
 
 
-                       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;
 
 
-               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;
 
        /* If any structs to parse go through in sequence */
 
 
        /* If any structs to parse go through in sequence */
 
@@ -419,6 +399,9 @@ end:
        if (ret != 0)
                ERR_print_errors(bio_err);
        if (buf != NULL) BUF_MEM_free(buf);
        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_OPENSSL_STRING_free(osk);
        OBJ_cleanup();
        if (at != NULL) ASN1_TYPE_free(at);
        if (osk != NULL) sk_OPENSSL_STRING_free(osk);
        OBJ_cleanup();
index 577d997630d782669d68b6641eb688b5ba80aa37..5acea8cfc5df3031dbf7e29ba5eec3332d809bc2 100644 (file)
@@ -82,9 +82,10 @@ file using the B<out> option.
 =item B<-strictpem>
 
 If this option is used then B<-inform> will be ignored. Without this option any
 =item B<-strictpem>
 
 If this option is used then B<-inform> will be ignored. Without this option any
-data in a PEM format input file will be treated as base64 encoded and processed
-whether it has the normal PEM BEGIN and END markers or not. This option will
-ignore any data prior to the start of the BEGIN marker in a PEM file.
+data in a PEM format input file will be treated as being base64 encoded and
+processed whether it has the normal PEM BEGIN and END markers or not. This
+option will ignore any data prior to the start of the BEGIN marker, or after an
+END marker in a PEM file.
 
 =back
 
 
 =back