Cleanse memory using the new OPENSSL_cleanse() function.
[oweals/openssl.git] / apps / req.c
index 790aa90eb6cb8bf9056d87374d30f69f44d7717c..c2bada07c8f649c2ad33b899a82b278d121bb459 100644 (file)
@@ -73,6 +73,7 @@
 #include <openssl/x509v3.h>
 #include <openssl/objects.h>
 #include <openssl/pem.h>
+#include "../crypto/cryptlib.h"
 
 #define SECTION                "req"
 
@@ -151,7 +152,7 @@ int MAIN(int argc, char **argv)
 #ifndef OPENSSL_NO_DSA
        DSA *dsa_params=NULL;
 #endif
-       unsigned long nmflag = 0;
+       unsigned long nmflag = 0, reqflag = 0;
        int ex=1,x509=0,days=30;
        X509 *x509ss=NULL;
        X509_REQ *req=NULL;
@@ -176,7 +177,7 @@ int MAIN(int argc, char **argv)
        const EVP_MD *md_alg=NULL,*digest=EVP_md5();
        unsigned long chtype = MBSTRING_ASC;
 #ifndef MONOLITH
-       MS_STATIC char config_name[256];
+       char *to_free;
        long errline;
 #endif
 
@@ -356,6 +357,11 @@ int MAIN(int argc, char **argv)
                        if (--argc < 1) goto bad;
                        if (!set_name_ex(&nmflag, *(++argv))) goto bad;
                        }
+               else if (strcmp(*argv,"-reqopt") == 0)
+                       {
+                       if (--argc < 1) goto bad;
+                       if (!set_cert_ex(&reqflag, *(++argv))) goto bad;
+                       }
                else if (strcmp(*argv,"-subject") == 0)
                        subject=1;
                else if (strcmp(*argv,"-text") == 0)
@@ -448,7 +454,8 @@ bad:
                BIO_printf(bio_err," -extensions .. specify certificate extension section (override value in config file)\n");
                BIO_printf(bio_err," -reqexts ..    specify request extension section (override value in config file)\n");
                BIO_printf(bio_err," -utf8          input characters are UTF8 (default ASCII)\n");
-               BIO_printf(bio_err," -nameopt arg    - various certificate name options\n");
+               BIO_printf(bio_err," -nameopt arg   - various certificate name options\n");
+               BIO_printf(bio_err," -reqopt arg    - various request text options\n\n");
                goto end;
                }
 
@@ -464,14 +471,7 @@ bad:
        if (p == NULL)
                p=getenv("SSLEAY_CONF");
        if (p == NULL)
-               {
-               strcpy(config_name,X509_get_default_cert_area());
-#ifndef OPENSSL_SYS_VMS
-               strcat(config_name,"/");
-#endif
-               strcat(config_name,OPENSSL_CONF);
-               p=config_name;
-               }
+               p=to_free=make_config_name();
        default_config_file=p;
        config=NCONF_new(NULL);
        i=NCONF_load(config, p, &errline);
@@ -479,7 +479,7 @@ bad:
 
        if (template != NULL)
                {
-               long errline;
+               long errline = -1;
 
                if( verbose )
                        BIO_printf(bio_err,"Using configuration from %s\n",template);
@@ -622,7 +622,7 @@ bad:
 
        if (keyfile != NULL)
                {
-               pkey = load_key(bio_err, keyfile, keyform, passin, e,
+               pkey = load_key(bio_err, keyfile, keyform, 0, passin, e,
                        "Private Key");
                if (!pkey)
                        {
@@ -981,9 +981,9 @@ loop:
        if (text)
                {
                if (x509)
-                       X509_print(out,x509ss);
+                       X509_print_ex(out, x509ss, nmflag, reqflag);
                else    
-                       X509_REQ_print(out,req);
+                       X509_REQ_print_ex(out, req, nmflag, reqflag);
                }
 
        if(subject) 
@@ -1053,6 +1053,10 @@ loop:
                }
        ex=0;
 end:
+#ifndef MONOLITH
+       if(to_free)
+               OPENSSL_free(to_free);
+#endif
        if (ex)
                {
                ERR_print_errors(bio_err);
@@ -1144,120 +1148,18 @@ err:
  */
 static int build_subject(X509_REQ *req, char *subject, unsigned long chtype)
        {
-       size_t buflen = strlen (subject)+1; /* to copy the types and values into. due to escaping, the copy can only become shorter */
-       char *buf = malloc (buflen);
-       size_t max_ne = buflen / 2 + 1; /* maximum number of name elements */
-       char **ne_types = malloc (max_ne * sizeof (char *));
-       char **ne_values = malloc (max_ne * sizeof (char *));
-
-       char *sp = subject, *bp = buf;
-       int i, ne_num = 0;
-
-       X509_NAME *n = NULL;
-       int nid;
-
-       if (!buf || !ne_types || !ne_values)
-       {
-               BIO_printf(bio_err, "malloc error\n");
-               goto error0;
-       }
-
-       if (*subject != '/')
-       {
-               BIO_printf(bio_err, "Subject does not start with '/'.\n");
-               goto error0;
-       }
-       sp++; /* skip leading / */
-
-       while (*sp)
-       {
-               /* collect type */
-               ne_types[ne_num] = bp;
-               while (*sp)
-               {
-                       if (*sp == '\\') /* is there anything to escape in the type...? */
-                               if (*++sp)
-                                       *bp++ = *sp++;
-                               else
-                               {
-                                       BIO_printf(bio_err, "escape character at end of string\n");
-                                       goto error0;
-                               }
-                       else if (*sp == '=')
-                       {
-                               sp++;
-                               *bp++ = '\0';
-                               break;
-                       }
-                       else
-                               *bp++ = *sp++;
-               }
-               if (!*sp)
-               {
-                       BIO_printf(bio_err, "end of string encountered while processing type of subject name element #%d\n", ne_num);
-                       goto error0;
-               }
-               ne_values[ne_num] = bp;
-               while (*sp)
-               {
-                       if (*sp == '\\')
-                               if (*++sp)
-                                       *bp++ = *sp++;
-                               else
-                               {
-                                       BIO_printf(bio_err, "escape character at end of string\n");
-                                       goto error0;
-                               }
-                       else if (*sp == '/')
-                       {
-                               sp++;
-                               *bp++ = '\0';
-                               break;
-                       }
-                       else
-                               *bp++ = *sp++;
-               }
-               *bp++ = '\0';
-               ne_num++;
-       }
+       X509_NAME *n;
 
-       if (!(n = X509_NAME_new()))
-               goto error0;
+       if (!(n = do_subject(subject, chtype)))
+               return 0;
 
-       for(i = 0; i < ne_num; i++)
+       if (!X509_REQ_set_subject_name(req, n))
                {
-               if ((nid=OBJ_txt2nid(ne_types[i])) == NID_undef)
-                       {
-                       BIO_printf(bio_err, "Subject Attribute %s has no known NID, skipped\n", ne_types[i]);
-                       continue;
-                       }
-
-               if (!*ne_values[i])
-                       {
-                       BIO_printf(bio_err, "No value provided for Subject Attribute %s, skipped\n", ne_types[i]);
-                       continue;
-                       }
-
-               if (!X509_NAME_add_entry_by_NID(n, nid, chtype, (unsigned char*)ne_values[i], -1,-1,0))
-                       goto error1;
-
+               X509_NAME_free(n);
+               return 0;
                }
-
-       if (!X509_REQ_set_subject_name(req, n))
-               goto error1;
        X509_NAME_free(n);
-       free (ne_values);
-       free (ne_types);
-       free (buf);
        return 1;
-
-error1:
-       X509_NAME_free(n);
-error0:
-       free (ne_values);
-       free (ne_types);
-       free (buf);
-       return 0;
 }
 
 
@@ -1314,13 +1216,19 @@ start:          for (;;)
                                }
                        /* If OBJ not recognised ignore it */
                        if ((nid=OBJ_txt2nid(type)) == NID_undef) goto start;
+
+                       if(strlen(v->name) > sizeof buf-9)
+                          {
+                          BIO_printf(bio_err,"Name '%s' too long\n",v->name);
+                          return 0;
+                          }
+
                        sprintf(buf,"%s_default",v->name);
                        if ((def=NCONF_get_string(req_conf,dn_sect,buf)) == NULL)
                                {
                                ERR_clear_error();
                                def="";
                                }
-                               
                        sprintf(buf,"%s_value",v->name);
                        if ((value=NCONF_get_string(req_conf,dn_sect,buf)) == NULL)
                                {
@@ -1367,6 +1275,12 @@ start2:                  for (;;)
                                if ((nid=OBJ_txt2nid(type)) == NID_undef)
                                        goto start2;
 
+                               if(strlen(v->name) > sizeof buf-9)
+                                  {
+                                  BIO_printf(bio_err,"Name '%s' too long\n",v->name);
+                                  return 0;
+                                  }
+
                                sprintf(buf,"%s_default",type);
                                if ((def=NCONF_get_string(req_conf,attr_sect,buf))
                                        == NULL)
@@ -1470,6 +1384,7 @@ start:
        (void)BIO_flush(bio_err);
        if(value != NULL)
                {
+               OPENSSL_assert(strlen(value) < sizeof buf-2);
                strcpy(buf,value);
                strcat(buf,"\n");
                BIO_printf(bio_err,"%s\n",value);
@@ -1479,7 +1394,7 @@ start:
                buf[0]='\0';
                if (!batch)
                        {
-                       fgets(buf,1024,stdin);
+                       fgets(buf,sizeof buf,stdin);
                        }
                else
                        {
@@ -1528,6 +1443,7 @@ start:
        (void)BIO_flush(bio_err);
        if (value != NULL)
                {
+               OPENSSL_assert(strlen(value) < sizeof buf-2);
                strcpy(buf,value);
                strcat(buf,"\n");
                BIO_printf(bio_err,"%s\n",value);
@@ -1537,7 +1453,7 @@ start:
                buf[0]='\0';
                if (!batch)
                        {
-                       fgets(buf,1024,stdin);
+                       fgets(buf,sizeof buf,stdin);
                        }
                else
                        {