Various randomness handling bugfixes and improvements --
[openssl.git] / apps / req.c
index b7342f071d373834ba8ff684b5533b49e8e1177a..a945610f92b938fbde87e69902798e7b8445b1a1 100644 (file)
@@ -66,7 +66,6 @@
 #include "apps.h"
 #include <openssl/bio.h>
 #include <openssl/evp.h>
-#include <openssl/rand.h>
 #include <openssl/conf.h>
 #include <openssl/err.h>
 #include <openssl/asn1.h>
@@ -82,6 +81,7 @@
 #define DISTINGUISHED_NAME     "distinguished_name"
 #define ATTRIBUTES     "attributes"
 #define V3_EXTENSIONS  "x509_extensions"
+#define REQ_EXTENSIONS "req_extensions"
 
 #define DEFAULT_KEY_LENGTH     512
 #define MIN_KEY_LENGTH         384
  */
 
 static int make_REQ(X509_REQ *req,EVP_PKEY *pkey,int attribs);
-static int add_attribute_object(STACK *n, char *text, char *def, 
-       char *value, int nid,int min,int max);
+static int add_attribute_object(STACK_OF(X509_ATTRIBUTE) *n, char *text,
+                               char *def, char *value, int nid, int min,
+                               int max);
 static int add_DN_object(X509_NAME *n, char *text, char *def, char *value,
        int nid,int min,int max);
-static void MS_CALLBACK req_cb(int p,int n,char *arg);
+static void MS_CALLBACK req_cb(int p,int n,void *arg);
 static int req_fix_data(int nid,int *type,int len,int min,int max);
 static int check_end(char *str, char *end);
 static int add_oid_section(LHASH *conf);
@@ -141,6 +142,7 @@ int MAIN(int argc, char **argv)
        int nodes=0,kludge=0;
        char *infile,*outfile,*prog,*keyfile=NULL,*template=NULL,*keyout=NULL;
        char *extensions = NULL;
+       char *req_exts = NULL;
        EVP_CIPHER *cipher=NULL;
        int modulus=0;
        char *p;
@@ -149,6 +151,7 @@ int MAIN(int argc, char **argv)
        MS_STATIC char config_name[256];
 #endif
 
+       req_conf = NULL;
 #ifndef NO_DES
        cipher=EVP_des_ede3_cbc();
 #endif
@@ -241,11 +244,11 @@ int MAIN(int argc, char **argv)
                                        perror(p);
                                        goto end;
                                        }
-                               if ((dsa_params=PEM_read_bio_DSAparams(in,NULL,NULL)) == NULL)
+                               if ((dsa_params=PEM_read_bio_DSAparams(in,NULL,NULL,NULL)) == NULL)
                                        {
                                        ERR_clear_error();
-                                       BIO_reset(in);
-                                       if ((xtmp=PEM_read_bio_X509(in,NULL,NULL)) == NULL)
+                                       (void)BIO_reset(in);
+                                       if ((xtmp=PEM_read_bio_X509(in,NULL,NULL,NULL)) == NULL)
                                                {
                                                BIO_printf(bio_err,"unable to load DSA parameters from file\n");
                                                goto end;
@@ -307,8 +310,17 @@ int MAIN(int argc, char **argv)
                        /* ok */
                        digest=md_alg;
                        }
+               else if (strcmp(*argv,"-extensions") == 0)
+                       {
+                       if (--argc < 1) goto bad;
+                       extensions = *(++argv);
+                       }
+               else if (strcmp(*argv,"-reqexts") == 0)
+                       {
+                       if (--argc < 1) goto bad;
+                       req_exts = *(++argv);
+                       }
                else
-
                        {
                        BIO_printf(bio_err,"unknown option %s\n",*argv);
                        badops=1;
@@ -346,6 +358,8 @@ bad:
                BIO_printf(bio_err," -asn1-kludge   Output the 'request' in a format that is wrong but some CA's\n");
                BIO_printf(bio_err,"                have been reported as requiring\n");
                BIO_printf(bio_err,"                [ It is now always turned on but can be turned off with -no-asn1-kludge ]\n");
+               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");
                goto end;
                }
 
@@ -360,7 +374,9 @@ bad:
        if (p == NULL)
                {
                strcpy(config_name,X509_get_default_cert_area());
-               strcat(config_name,"/lib/");
+#ifndef VMS
+               strcat(config_name,"/");
+#endif
                strcat(config_name,OPENSSL_CONF);
                p=config_name;
                }
@@ -422,7 +438,8 @@ bad:
                        digest=md_alg;
                }
 
-       extensions = CONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
+       if(!extensions)
+               extensions = CONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
        if(extensions) {
                /* Check syntax of file */
                X509V3_CTX ctx;
@@ -435,6 +452,21 @@ bad:
                }
        }
 
+       if(!req_exts)
+               req_exts = CONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
+       if(req_exts) {
+               /* Check syntax of file */
+               X509V3_CTX ctx;
+               X509V3_set_ctx_test(&ctx);
+               X509V3_set_conf_lhash(&ctx, req_conf);
+               if(!X509V3_EXT_add_conf(req_conf, &ctx, req_exts, NULL)) {
+                       BIO_printf(bio_err,
+                        "Error Loading request extension section %s\n",
+                                                               req_exts);
+                       goto end;
+               }
+       }
+
        in=BIO_new(BIO_s_file());
        out=BIO_new(BIO_s_file());
        if ((in == NULL) || (out == NULL))
@@ -452,7 +484,7 @@ bad:
                        rsa=d2i_RSAPrivateKey_bio(in,NULL);
                else */
                if (keyform == FORMAT_PEM)
-                       pkey=PEM_read_bio_PrivateKey(in,NULL,NULL);
+                       pkey=PEM_read_bio_PrivateKey(in,NULL,NULL,NULL);
                else
                        {
                        BIO_printf(bio_err,"bad input format specified for X509 request\n");
@@ -468,25 +500,9 @@ bad:
 
        if (newreq && (pkey == NULL))
                {
-               char *randfile;
-               char buffer[200];
-
-               if ((randfile=CONF_get_string(req_conf,SECTION,"RANDFILE")) == NULL)
-                       randfile=RAND_file_name(buffer,200);
-#ifdef WINDOWS
-               BIO_printf(bio_err,"Loading 'screen' into random state -");
-               BIO_flush(bio_err);
-               RAND_screen();
-               BIO_printf(bio_err," done\n");
-#endif
-               if ((randfile == NULL) || !RAND_load_file(randfile,1024L*1024L))
-                       {
-                       BIO_printf(bio_err,"unable to load 'random state'\n");
-                       BIO_printf(bio_err,"What this means is that the random number generator has not been seeded\n");
-                       BIO_printf(bio_err,"with much random data.\n");
-                       BIO_printf(bio_err,"Consider setting the RANDFILE environment variable to point at a file that\n");
-                       BIO_printf(bio_err,"'random' data can be kept in.\n");
-                       }
+               char *randfile = CONF_get_string(req_conf,SECTION,"RANDFILE");
+               app_RAND_load_file(randfile, bio_err, 0);
+       
                if (newkey <= 0)
                        {
                        newkey=(int)CONF_get_number(req_conf,SECTION,BITS);
@@ -510,7 +526,7 @@ bad:
                        {
                        if (!EVP_PKEY_assign_RSA(pkey,
                                RSA_generate_key(newkey,0x10001,
-                                       req_cb,(char *)bio_err)))
+                                       req_cb,bio_err)))
                                goto end;
                        }
                else
@@ -524,8 +540,7 @@ bad:
                        }
 #endif
 
-               if ((randfile == NULL) || (RAND_write_file(randfile) == 0))
-                       BIO_printf(bio_err,"unable to write 'random state'\n");
+               app_RAND_write_file(randfile, bio_err);
 
                if (pkey == NULL) goto end;
 
@@ -557,7 +572,7 @@ bad:
                i=0;
 loop:
                if (!PEM_write_bio_PrivateKey(out,pkey,cipher,
-                       NULL,0,NULL))
+                       NULL,0,NULL,NULL))
                        {
                        if ((ERR_GET_REASON(ERR_peek_error()) ==
                                PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3))
@@ -591,7 +606,7 @@ loop:
                if      (informat == FORMAT_ASN1)
                        req=d2i_X509_REQ_bio(in,NULL);
                else if (informat == FORMAT_PEM)
-                       req=PEM_read_bio_X509_REQ(in,NULL,NULL);
+                       req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL);
                else
                        {
                        BIO_printf(bio_err,"bad input format specified for X509 request\n");
@@ -674,6 +689,22 @@ loop:
                        }
                else
                        {
+                       X509V3_CTX ext_ctx;
+
+                       /* Set up V3 context struct */
+
+                       X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0);
+                       X509V3_set_conf_lhash(&ext_ctx, req_conf);
+
+                       /* Add extensions */
+                       if(req_exts && !X509V3_EXT_REQ_add_conf(req_conf, 
+                                       &ext_ctx, req_exts, req))
+                           {
+                           BIO_printf(bio_err,
+                                      "Error Loading extension section %s\n",
+                                      req_exts);
+                           goto end;
+                           }
                        if (!(i=X509_REQ_sign(req,pkey,digest)))
                                goto end;
                        }
@@ -820,7 +851,7 @@ static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, int attribs)
        char buf[100];
        int nid,min,max;
        char *type,*def,*tmp,*value,*tmp_attr;
-       STACK *sk,*attr=NULL;
+       STACK_OF(CONF_VALUE) *sk, *attr=NULL;
        CONF_VALUE *v;
        
        tmp=CONF_get_string(req_conf,SECTION,DISTINGUISHED_NAME);
@@ -863,15 +894,15 @@ static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, int attribs)
        /* setup version number */
        if (!ASN1_INTEGER_set(ri->version,0L)) goto err; /* version 1 */
 
-       if (sk_num(sk))
+       if (sk_CONF_VALUE_num(sk))
                {
                i= -1;
 start:         for (;;)
                        {
                        i++;
-                       if ((int)sk_num(sk) <= i) break;
+                       if (sk_CONF_VALUE_num(sk) <= i) break;
 
-                       v=(CONF_VALUE *)sk_value(sk,i);
+                       v=sk_CONF_VALUE_value(sk,i);
                        p=q=NULL;
                        type=v->name;
                        if(!check_end(type,"_min") || !check_end(type,"_max") ||
@@ -915,7 +946,7 @@ start:              for (;;)
 
                if (attribs)
                        {
-                       if ((attr != NULL) && (sk_num(attr) > 0))
+                       if ((attr != NULL) && (sk_CONF_VALUE_num(attr) > 0))
                                {
                                BIO_printf(bio_err,"\nPlease enter the following 'extra' attributes\n");
                                BIO_printf(bio_err,"to be sent with your certificate request\n");
@@ -925,10 +956,11 @@ start:            for (;;)
 start2:                        for (;;)
                                {
                                i++;
-                               if ((attr == NULL) || ((int)sk_num(attr) <= i))
+                               if ((attr == NULL) ||
+                                           (sk_CONF_VALUE_num(attr) <= i))
                                        break;
 
-                               v=(CONF_VALUE *)sk_value(attr,i);
+                               v=sk_CONF_VALUE_value(attr,i);
                                type=v->name;
                                if ((nid=OBJ_txt2nid(type)) == NID_undef)
                                        goto start2;
@@ -976,7 +1008,7 @@ static int add_DN_object(X509_NAME *n, char *text, char *def, char *value,
        MS_STATIC char buf[1024];
 
        BIO_printf(bio_err,"%s [%s]:",text,def);
-       BIO_flush(bio_err);
+       (void)BIO_flush(bio_err);
        if (value != NULL)
                {
                strcpy(buf,value);
@@ -1010,6 +1042,9 @@ static int add_DN_object(X509_NAME *n, char *text, char *def, char *value,
        j=ASN1_PRINTABLE_type((unsigned char *)buf,-1);
        if (req_fix_data(nid,&j,i,min,max) == 0)
                goto err;
+#ifdef CHARSET_EBCDIC
+       ebcdic2ascii(buf, buf, i);
+#endif
        if ((ne=X509_NAME_ENTRY_create_by_NID(NULL,nid,j,(unsigned char *)buf,
                strlen(buf)))
                == NULL) goto err;
@@ -1022,8 +1057,9 @@ err:
        return(ret);
        }
 
-static int add_attribute_object(STACK *n, char *text, char *def, char *value,
-            int nid, int min, int max)
+static int add_attribute_object(STACK_OF(X509_ATTRIBUTE) *n, char *text,
+                               char *def, char *value, int nid, int min,
+                               int max)
        {
        int i,z;
        X509_ATTRIBUTE *xa=NULL;
@@ -1033,7 +1069,7 @@ static int add_attribute_object(STACK *n, char *text, char *def, char *value,
 
 start:
        BIO_printf(bio_err,"%s [%s]:",text,def);
-       BIO_flush(bio_err);
+       (void)BIO_flush(bio_err);
        if (value != NULL)
                {
                strcpy(buf,value);
@@ -1067,7 +1103,7 @@ start:
        /* add object plus value */
        if ((xa=X509_ATTRIBUTE_new()) == NULL)
                goto err;
-       if ((xa->value.set=sk_new_null()) == NULL)
+       if ((xa->value.set=sk_ASN1_TYPE_new_null()) == NULL)
                goto err;
        xa->set=1;
 
@@ -1093,12 +1129,12 @@ start:
                { BIO_printf(bio_err,"Malloc failure\n"); goto err; }
 
        ASN1_TYPE_set(at,bs->type,(char *)bs);
-       sk_push(xa->value.set,(char *)at);
+       sk_ASN1_TYPE_push(xa->value.set,at);
        bs=NULL;
        at=NULL;
        /* only one item per attribute */
 
-       if (!sk_push(n,(char *)xa)) goto err;
+       if (!sk_X509_ATTRIBUTE_push(n,xa)) goto err;
        return(1);
 err:
        if (xa != NULL) X509_ATTRIBUTE_free(xa);
@@ -1107,7 +1143,7 @@ err:
        return(0);
        }
 
-static void MS_CALLBACK req_cb(int p, int n, char *arg)
+static void MS_CALLBACK req_cb(int p, int n, void *arg)
        {
        char c='*';
 
@@ -1116,7 +1152,7 @@ static void MS_CALLBACK req_cb(int p, int n, char *arg)
        if (p == 2) c='*';
        if (p == 3) c='\n';
        BIO_write((BIO *)arg,&c,1);
-       BIO_flush((BIO *)arg);
+       (void)BIO_flush((BIO *)arg);
 #ifdef LINT
        p=n;
 #endif
@@ -1169,7 +1205,7 @@ static int check_end(char *str, char *end)
 static int add_oid_section(LHASH *conf)
 {      
        char *p;
-       STACK *sktmp;
+       STACK_OF(CONF_VALUE) *sktmp;
        CONF_VALUE *cnf;
        int i;
        if(!(p=CONF_get_string(conf,NULL,"oid_section"))) return 1;
@@ -1177,8 +1213,8 @@ static int add_oid_section(LHASH *conf)
                BIO_printf(bio_err, "problem loading oid section %s\n", p);
                return 0;
        }
-       for(i = 0; i < sk_num(sktmp); i++) {
-               cnf = (CONF_VALUE *)sk_value(sktmp, i);
+       for(i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
+               cnf = sk_CONF_VALUE_value(sktmp, i);
                if(OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
                        BIO_printf(bio_err, "problem creating object %s=%s\n",
                                                         cnf->name, cnf->value);