More code for X509_print_ex() support.
[openssl.git] / apps / req.c
index 79a7394088b414642b54d8657f8e771923e43037..4d707e83ed7387394ee108ed820dcec2ef4dd0a5 100644 (file)
  * -config file        - Load configuration file.
  * -key file   - make a request using key in file (or use it for verification).
  * -keyform    - key file format.
+ * -rand file(s) - load the file(s) into the PRNG.
  * -newkey     - make a key and a request.
  * -modulus    - print RSA modulus.
  * -x509       - output a self signed X509 structure instead.
@@ -125,7 +126,6 @@ static void MS_CALLBACK req_cb(int p,int n,void *arg);
 #endif
 static int req_check_len(int len,int min,int max);
 static int check_end(char *str, char *end);
-static int add_oid_section(LHASH *conf);
 #ifndef MONOLITH
 static char *default_config_file=NULL;
 static LHASH *config=NULL;
@@ -143,6 +143,7 @@ int MAIN(int argc, char **argv)
 #ifndef NO_DSA
        DSA *dsa_params=NULL;
 #endif
+       unsigned long nmflag = 0;
        int ex=1,x509=0,days=30;
        X509 *x509ss=NULL;
        X509_REQ *req=NULL;
@@ -150,12 +151,13 @@ int MAIN(int argc, char **argv)
        int i,badops=0,newreq=0,newkey= -1,pkey_type=0;
        BIO *in=NULL,*out=NULL;
        int informat,outformat,verify=0,noout=0,text=0,keyform=FORMAT_PEM;
-       int nodes=0,kludge=0,newhdr=0;
+       int nodes=0,kludge=0,newhdr=0,subject=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 *inrand=NULL;
        char *passargin = NULL, *passargout = NULL;
        char *passin = NULL, *passout = NULL;
        char *p;
@@ -239,6 +241,11 @@ int MAIN(int argc, char **argv)
                        if (--argc < 1) goto bad;
                        passargout= *(++argv);
                        }
+               else if (strcmp(*argv,"-rand") == 0)
+                       {
+                       if (--argc < 1) goto bad;
+                       inrand= *(++argv);
+                       }
                else if (strcmp(*argv,"-newkey") == 0)
                        {
                        int is_numeric;
@@ -316,6 +323,13 @@ int MAIN(int argc, char **argv)
                        nodes=1;
                else if (strcmp(*argv,"-noout") == 0)
                        noout=1;
+               else if (strcmp(*argv,"-nameopt") == 0)
+                       {
+                       if (--argc < 1) goto bad;
+                       if (!set_name_ex(&nmflag, *(++argv))) goto bad;
+                       }
+               else if (strcmp(*argv,"-subject") == 0)
+                       subject=1;
                else if (strcmp(*argv,"-text") == 0)
                        text=1;
                else if (strcmp(*argv,"-x509") == 0)
@@ -372,6 +386,9 @@ bad:
                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");
+               BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
+               BIO_printf(bio_err,"                load the file (or the files in the directory) into\n");
+               BIO_printf(bio_err,"                the random number generator\n");
                BIO_printf(bio_err," -newkey rsa:bits generate a new RSA key of 'bits' in size\n");
                BIO_printf(bio_err," -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n");
 
@@ -457,7 +474,7 @@ bad:
                                }
                        }
                }
-               if(!add_oid_section(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))
@@ -538,12 +555,19 @@ bad:
                        BIO_printf(bio_err,"unable to load Private key\n");
                        goto end;
                        }
+                if (EVP_PKEY_type(pkey->type) == EVP_PKEY_DSA)
+                       {
+                       char *randfile = CONF_get_string(req_conf,SECTION,"RANDFILE");
+                       app_RAND_load_file(randfile, bio_err, 0);
+                       }
                }
 
        if (newreq && (pkey == NULL))
                {
                char *randfile = CONF_get_string(req_conf,SECTION,"RANDFILE");
                app_RAND_load_file(randfile, bio_err, 0);
+               if (inrand)
+                       app_RAND_load_files(inrand);
        
                if (newkey <= 0)
                        {
@@ -593,6 +617,12 @@ bad:
                        {
                        BIO_printf(bio_err,"writing new private key to stdout\n");
                        BIO_set_fp(out,stdout,BIO_NOCLOSE);
+#ifdef VMS
+                       {
+                       BIO *tmpbio = BIO_new(BIO_f_linebuffer());
+                       out = BIO_push(tmpbio, out);
+                       }
+#endif
                        }
                else
                        {
@@ -781,14 +811,22 @@ loop:
                        BIO_printf(bio_err,"verify OK\n");
                }
 
-       if (noout && !text && !modulus)
+       if (noout && !text && !modulus && !subject)
                {
                ex=0;
                goto end;
                }
 
        if (outfile == NULL)
+               {
                BIO_set_fp(out,stdout,BIO_NOCLOSE);
+#ifdef VMS
+               {
+               BIO *tmpbio = BIO_new(BIO_f_linebuffer());
+               out = BIO_push(tmpbio, out);
+               }
+#endif
+               }
        else
                {
                if ((keyout != NULL) && (strcmp(outfile,keyout) == 0))
@@ -810,6 +848,14 @@ loop:
                        X509_REQ_print(out,req);
                }
 
+       if(subject) 
+               {
+               if(x509)
+                       print_name(out, "subject=", X509_get_subject_name(x509ss), nmflag);
+               else
+                       print_name(out, "subject=", X509_REQ_get_subject_name(req), nmflag);
+               }
+
        if (modulus)
                {
                EVP_PKEY *pubkey;
@@ -874,12 +920,12 @@ end:
                }
        if ((req_conf != NULL) && (req_conf != config)) CONF_free(req_conf);
        BIO_free(in);
-       BIO_free(out);
+       BIO_free_all(out);
        EVP_PKEY_free(pkey);
        X509_REQ_free(req);
        X509_free(x509ss);
-       if(passin) Free(passin);
-       if(passout) Free(passout);
+       if(passargin && passin) OPENSSL_free(passin);
+       if(passargout && passout) OPENSSL_free(passout);
        OBJ_cleanup();
 #ifndef NO_DSA
        if (dsa_params != NULL) DSA_free(dsa_params);
@@ -1083,7 +1129,11 @@ static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk,
                 * multiple instances 
                 */
                for(p = v->name; *p ; p++) 
+#ifndef CHARSET_EBCDIC
                        if ((*p == ':') || (*p == ',') || (*p == '.')) {
+#else
+                       if ((*p == os_toascii[':']) || (*p == os_toascii[',']) || (*p == os_toascii['.'])) {
+#endif
                                p++;
                                if(*p) type = p;
                                break;
@@ -1103,7 +1153,7 @@ static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk,
                        for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++)
                                {
                                v=sk_CONF_VALUE_value(attr_sk,i);
-                               if(!X509_REQ_radd_attr_by_txt(req, v->name, MBSTRING_ASC,
+                               if(!X509_REQ_add1_attr_by_txt(req, v->name, MBSTRING_ASC,
                                        (unsigned char *)v->value, -1)) return 0;
                                }
                        }
@@ -1199,9 +1249,12 @@ start:
                return(0);
                }
        buf[--i]='\0';
+#ifdef CHARSET_EBCDIC
+       ebcdic2ascii(buf, buf, i);
+#endif
        if(!req_check_len(i, min, max)) goto start;
 
-       if(!X509_REQ_radd_attr_by_NID(req, nid, MBSTRING_ASC,
+       if(!X509_REQ_add1_attr_by_NID(req, nid, MBSTRING_ASC,
                                        (unsigned char *)buf, -1)) {
                BIO_printf(bio_err, "Error adding attribute\n");
                ERR_print_errors(bio_err);
@@ -1256,25 +1309,3 @@ static int check_end(char *str, char *end)
        tmp = str + slen - elen;
        return strcmp(tmp, end);
 }
-
-static int add_oid_section(LHASH *conf)
-{      
-       char *p;
-       STACK_OF(CONF_VALUE) *sktmp;
-       CONF_VALUE *cnf;
-       int i;
-       if(!(p=CONF_get_string(conf,NULL,"oid_section"))) return 1;
-       if(!(sktmp = CONF_get_section(conf, p))) {
-               BIO_printf(bio_err, "problem loading oid section %s\n", p);
-               return 0;
-       }
-       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);
-                       return 0;
-               }
-       }
-       return 1;
-}