Keep up with Unix code. It's beginning to be time to rethink the VMS
[openssl.git] / apps / req.c
index 4d707e83ed7387394ee108ed820dcec2ef4dd0a5..292bc5910f3bf5d1c28f792ca5f61e53fe0c1a23 100644 (file)
@@ -73,6 +73,7 @@
 #include <openssl/x509v3.h>
 #include <openssl/objects.h>
 #include <openssl/pem.h>
+#include <openssl/engine.h>
 
 #define SECTION                "req"
 
  * -nodes      - no des encryption
  * -config file        - Load configuration file.
  * -key file   - make a request using key in file (or use it for verification).
- * -keyform    - key file format.
+ * -keyform arg        - key file format.
  * -rand file(s) - load the file(s) into the PRNG.
  * -newkey     - make a key and a request.
  * -modulus    - print RSA modulus.
@@ -140,6 +141,7 @@ int MAIN(int, char **);
 
 int MAIN(int argc, char **argv)
        {
+       ENGINE *e = NULL;
 #ifndef NO_DSA
        DSA *dsa_params=NULL;
 #endif
@@ -148,11 +150,12 @@ int MAIN(int argc, char **argv)
        X509 *x509ss=NULL;
        X509_REQ *req=NULL;
        EVP_PKEY *pkey=NULL;
-       int i,badops=0,newreq=0,newkey= -1,pkey_type=0;
+       int i,badops=0,newreq=0,newkey= -1,pkey_type=TYPE_RSA;
        BIO *in=NULL,*out=NULL;
        int informat,outformat,verify=0,noout=0,text=0,keyform=FORMAT_PEM;
        int nodes=0,kludge=0,newhdr=0,subject=0;
        char *infile,*outfile,*prog,*keyfile=NULL,*template=NULL,*keyout=NULL;
+       char *engine=NULL;
        char *extensions = NULL;
        char *req_exts = NULL;
        EVP_CIPHER *cipher=NULL;
@@ -196,6 +199,11 @@ int MAIN(int argc, char **argv)
                        if (--argc < 1) goto bad;
                        outformat=str2fmt(*(++argv));
                        }
+               else if (strcmp(*argv,"-engine") == 0)
+                       {
+                       if (--argc < 1) goto bad;
+                       engine= *(++argv);
+                       }
                else if (strcmp(*argv,"-key") == 0)
                        {
                        if (--argc < 1) goto bad;
@@ -203,7 +211,6 @@ int MAIN(int argc, char **argv)
                        }
                else if (strcmp(*argv,"-new") == 0)
                        {
-                       pkey_type=TYPE_RSA;
                        newreq=1;
                        }
                else if (strcmp(*argv,"-config") == 0)
@@ -383,6 +390,7 @@ bad:
                BIO_printf(bio_err," -verify        verify signature on REQ\n");
                BIO_printf(bio_err," -modulus       RSA modulus\n");
                BIO_printf(bio_err," -nodes         don't encrypt the output key\n");
+               BIO_printf(bio_err," -engine e      use engine e, possibly a hardware device.\n");
                BIO_printf(bio_err," -key file  use the private key contained in file\n");
                BIO_printf(bio_err," -keyform arg   key file format\n");
                BIO_printf(bio_err," -keyout arg    file to send the key to\n");
@@ -455,6 +463,8 @@ bad:
        if (req_conf != NULL)
                {
                p=CONF_get_string(req_conf,NULL,"oid_file");
+               if (p == NULL)
+                       ERR_clear_error();
                if (p != NULL)
                        {
                        BIO *oid_bio;
@@ -474,18 +484,27 @@ bad:
                                }
                        }
                }
-               if(!add_oid_section(bio_err, req_conf)) goto end;
+       if(!add_oid_section(bio_err, req_conf)) goto end;
 
-       if ((md_alg == NULL) &&
-               ((p=CONF_get_string(req_conf,SECTION,"default_md")) != NULL))
+       if (md_alg == NULL)
                {
-               if ((md_alg=EVP_get_digestbyname(p)) != NULL)
-                       digest=md_alg;
+               p=CONF_get_string(req_conf,SECTION,"default_md");
+               if (p == NULL)
+                       ERR_clear_error();
+               if (p != NULL)
+                       {
+                       if ((md_alg=EVP_get_digestbyname(p)) != NULL)
+                               digest=md_alg;
+                       }
                }
 
-       if(!extensions)
+       if (!extensions)
+               {
                extensions = CONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
-       if(extensions) {
+               if (!extensions)
+                       ERR_clear_error();
+               }
+       if (extensions) {
                /* Check syntax of file */
                X509V3_CTX ctx;
                X509V3_set_ctx_test(&ctx);
@@ -498,12 +517,22 @@ bad:
        }
 
        if(!passin)
+               {
                passin = CONF_get_string(req_conf, SECTION, "input_password");
-
+               if (!passin)
+                       ERR_clear_error();
+               }
+       
        if(!passout)
+               {
                passout = CONF_get_string(req_conf, SECTION, "output_password");
+               if (!passout)
+                       ERR_clear_error();
+               }
 
        p = CONF_get_string(req_conf, SECTION, STRING_MASK);
+       if (!p)
+               ERR_clear_error();
 
        if(p && !ASN1_STRING_set_default_mask_asc(p)) {
                BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
@@ -511,7 +540,11 @@ bad:
        }
 
        if(!req_exts)
+               {
                req_exts = CONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
+               if (!req_exts)
+                       ERR_clear_error();
+               }
        if(req_exts) {
                /* Check syntax of file */
                X509V3_CTX ctx;
@@ -530,24 +563,55 @@ bad:
        if ((in == NULL) || (out == NULL))
                goto end;
 
-       if (keyfile != NULL)
+       if (engine != NULL)
                {
-               if (BIO_read_filename(in,keyfile) <= 0)
+               if((e = ENGINE_by_id(engine)) == NULL)
                        {
-                       perror(keyfile);
+                       BIO_printf(bio_err,"invalid engine \"%s\"\n",
+                               engine);
                        goto end;
                        }
+               if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
+                       {
+                       BIO_printf(bio_err,"can't use that engine\n");
+                       goto end;
+                       }
+               BIO_printf(bio_err,"engine \"%s\" set.\n", engine);
+               /* Free our "structural" reference. */
+               ENGINE_free(e);
+               }
 
-               if (keyform == FORMAT_ASN1)
-                       pkey=d2i_PrivateKey_bio(in,NULL);
-               else if (keyform == FORMAT_PEM)
+       if (keyfile != NULL)
+               {
+               if (keyform == FORMAT_ENGINE)
                        {
-                       pkey=PEM_read_bio_PrivateKey(in,NULL,NULL,passin);
+                       if (!e)
+                               {
+                               BIO_printf(bio_err,"no engine specified\n");
+                               goto end;
+                               }
+                       pkey = ENGINE_load_private_key(e, keyfile, NULL);
                        }
                else
                        {
-                       BIO_printf(bio_err,"bad input format specified for X509 request\n");
-                       goto end;
+                       if (BIO_read_filename(in,keyfile) <= 0)
+                               {
+                               perror(keyfile);
+                               goto end;
+                               }
+
+                       if (keyform == FORMAT_ASN1)
+                               pkey=d2i_PrivateKey_bio(in,NULL);
+                       else if (keyform == FORMAT_PEM)
+                               {
+                               pkey=PEM_read_bio_PrivateKey(in,NULL,NULL,
+                                       passin);
+                               }
+                       else
+                               {
+                               BIO_printf(bio_err,"bad input format specified for X509 request\n");
+                               goto end;
+                               }
                        }
 
                if (pkey == NULL)
@@ -558,6 +622,8 @@ bad:
                 if (EVP_PKEY_type(pkey->type) == EVP_PKEY_DSA)
                        {
                        char *randfile = CONF_get_string(req_conf,SECTION,"RANDFILE");
+                       if (randfile == NULL)
+                               ERR_clear_error();
                        app_RAND_load_file(randfile, bio_err, 0);
                        }
                }
@@ -565,6 +631,8 @@ bad:
        if (newreq && (pkey == NULL))
                {
                char *randfile = CONF_get_string(req_conf,SECTION,"RANDFILE");
+               if (randfile == NULL)
+                       ERR_clear_error();
                app_RAND_load_file(randfile, bio_err, 0);
                if (inrand)
                        app_RAND_load_files(inrand);
@@ -611,8 +679,12 @@ bad:
                if (pkey == NULL) goto end;
 
                if (keyout == NULL)
+                       {
                        keyout=CONF_get_string(req_conf,SECTION,KEYFILE);
-
+                       if (keyout == NULL)
+                               ERR_clear_error();
+                       }
+               
                if (keyout == NULL)
                        {
                        BIO_printf(bio_err,"writing new private key to stdout\n");
@@ -636,7 +708,12 @@ bad:
 
                p=CONF_get_string(req_conf,SECTION,"encrypt_rsa_key");
                if (p == NULL)
+                       {
+                       ERR_clear_error();
                        p=CONF_get_string(req_conf,SECTION,"encrypt_key");
+                       if (p == NULL)
+                               ERR_clear_error();
+                       }
                if ((p != NULL) && (strcmp(p,"no") == 0))
                        cipher=NULL;
                if (nodes) cipher=NULL;
@@ -712,8 +789,11 @@ loop:
                                }
 
                        i=make_REQ(req,pkey,!x509);
-                       if (kludge >= 0)
-                               req->req_info->req_kludge=kludge;
+                       if ((kludge > 0) && !sk_X509_ATTRIBUTE_num(req->req_info->attributes))
+                               {
+                               sk_X509_ATTRIBUTE_free(req->req_info->attributes);
+                               req->req_info->attributes = NULL;
+                               }
                        if (!i)
                                {
                                BIO_printf(bio_err,"problems making Certificate Request\n");
@@ -941,6 +1021,8 @@ static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, int attribs)
        char *tmp, *dn_sect,*attr_sect;
 
        tmp=CONF_get_string(req_conf,SECTION,PROMPT);
+       if (tmp == NULL)
+               ERR_clear_error();
        if((tmp != NULL) && !strcmp(tmp, "no")) no_prompt = 1;
 
        dn_sect=CONF_get_string(req_conf,SECTION,DISTINGUISHED_NAME);
@@ -959,7 +1041,10 @@ static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, int attribs)
 
        attr_sect=CONF_get_string(req_conf,SECTION,ATTRIBUTES);
        if (attr_sect == NULL)
+               {
+               ERR_clear_error();              
                attr_sk=NULL;
+               }
        else
                {
                attr_sk=CONF_get_section(req_conf,attr_sect);
@@ -1034,11 +1119,17 @@ start:          for (;;)
                        if ((nid=OBJ_txt2nid(type)) == NID_undef) goto start;
                        sprintf(buf,"%s_default",v->name);
                        if ((def=CONF_get_string(req_conf,dn_sect,buf)) == NULL)
+                               {
+                               ERR_clear_error();
                                def="";
+                               }
                                
                        sprintf(buf,"%s_value",v->name);
                        if ((value=CONF_get_string(req_conf,dn_sect,buf)) == NULL)
+                               {
+                               ERR_clear_error();
                                value=NULL;
+                               }
 
                        sprintf(buf,"%s_min",v->name);
                        min=(int)CONF_get_number(req_conf,dn_sect,buf);
@@ -1080,12 +1171,19 @@ start2:                 for (;;)
                                sprintf(buf,"%s_default",type);
                                if ((def=CONF_get_string(req_conf,attr_sect,buf))
                                        == NULL)
+                                       {
+                                       ERR_clear_error();
                                        def="";
+                                       }
+                               
                                
                                sprintf(buf,"%s_value",type);
                                if ((value=CONF_get_string(req_conf,attr_sect,buf))
                                        == NULL)
+                                       {
+                                       ERR_clear_error();
                                        value=NULL;
+                                       }
 
                                sprintf(buf,"%s_min",type);
                                min=(int)CONF_get_number(req_conf,attr_sect,buf);