'char' argument to islower must be converted to 'unsigned char'
[openssl.git] / apps / x509.c
index b86352e2fbd8eaa923f35bbd1c7cfb7ddabbafb2..9422e1d15cdef004d915ad272eb5203b5fc3a599 100644 (file)
@@ -73,6 +73,7 @@
 #include <openssl/x509v3.h>
 #include <openssl/objects.h>
 #include <openssl/pem.h>
+#include <openssl/engine.h>
 
 #undef PROG
 #define PROG x509_main
@@ -105,6 +106,7 @@ static char *x509_usage[]={
 " -fingerprint    - print the certificate fingerprint\n",
 " -alias          - output certificate alias\n",
 " -noout          - no certificate output\n",
+" -ocspid         - print OCSP hash values for the subject name and public key\n",
 " -trustout       - output a \"trusted\" certificate\n",
 " -clrtrust       - clear all trusted purposes\n",
 " -clrreject      - clear all rejected purposes\n",
@@ -129,6 +131,8 @@ static char *x509_usage[]={
 " -extensions     - section from config file with X509V3 extensions to add\n",
 " -clrext         - delete extensions before signing and input certificate\n",
 " -nameopt arg    - various certificate name options\n",
+" -engine e       - use engine e, possibly a hardware device.\n",
+" -certopt arg    - various certificate text options\n",
 NULL
 };
 
@@ -145,6 +149,7 @@ int MAIN(int, char **);
 
 int MAIN(int argc, char **argv)
        {
+       ENGINE *e = NULL;
        int ret=1;
        X509_REQ *req=NULL;
        X509 *x=NULL,*xca=NULL;
@@ -159,6 +164,7 @@ int MAIN(int argc, char **argv)
        char *CAkeyfile=NULL,*CAserial=NULL;
        char *alias=NULL;
        int text=0,serial=0,hash=0,subject=0,issuer=0,startdate=0,enddate=0;
+       int ocspid=0;
        int noout=0,sign_flag=0,CA_flag=0,CA_createserial=0,email=0;
        int trustout=0,clrtrust=0,clrreject=0,aliasout=0,clrext=0;
        int C=0;
@@ -174,7 +180,8 @@ int MAIN(int argc, char **argv)
        char *extsect = NULL, *extfile = NULL, *passin = NULL, *passargin = NULL;
        int need_rand = 0;
        int checkend=0,checkoffset=0;
-       unsigned long nmflag = 0;
+       unsigned long nmflag = 0, certflag = 0;
+       char *engine=NULL;
 
        reqfile=0;
 
@@ -183,6 +190,12 @@ int MAIN(int argc, char **argv)
        if (bio_err == NULL)
                bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
        STDout=BIO_new_fp(stdout,BIO_NOCLOSE);
+#ifdef VMS
+       {
+       BIO *tmpbio = BIO_new(BIO_f_linebuffer());
+       STDout = BIO_push(tmpbio, STDout);
+       }
+#endif
 
        informat=FORMAT_PEM;
        outformat=FORMAT_PEM;
@@ -320,6 +333,11 @@ int MAIN(int argc, char **argv)
                        alias= *(++argv);
                        trustout = 1;
                        }
+               else if (strcmp(*argv,"-certopt") == 0)
+                       {
+                       if (--argc < 1) goto bad;
+                       if (!set_cert_ex(&certflag, *(++argv))) goto bad;
+                       }
                else if (strcmp(*argv,"-nameopt") == 0)
                        {
                        if (--argc < 1) goto bad;
@@ -331,6 +349,11 @@ int MAIN(int argc, char **argv)
                        alias= *(++argv);
                        trustout = 1;
                        }
+               else if (strcmp(*argv,"-engine") == 0)
+                       {
+                       if (--argc < 1) goto bad;
+                       engine= *(++argv);
+                       }
                else if (strcmp(*argv,"-C") == 0)
                        C= ++num;
                else if (strcmp(*argv,"-email") == 0)
@@ -391,6 +414,8 @@ int MAIN(int argc, char **argv)
                        clrext = 1;
                        }
 #endif
+               else if (strcmp(*argv,"-ocspid") == 0)
+                       ocspid= ++num;
                else if ((md_alg=EVP_get_digestbyname(*argv + 1)))
                        {
                        /* ok */
@@ -414,6 +439,24 @@ bad:
                goto end;
                }
 
+       if (engine != NULL)
+               {
+               if((e = ENGINE_by_id(engine)) == NULL)
+                       {
+                       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 (need_rand)
                app_RAND_load_file(NULL, bio_err, 0);
 
@@ -455,8 +498,15 @@ bad:
                                                        ,errorline,extfile);
                        goto end;
                        }
-               if (!extsect && !(extsect = CONF_get_string(extconf, "default",
-                                        "extensions"))) extsect = "default";
+               if (!extsect)
+                       {
+                       extsect = CONF_get_string(extconf, "default", "extensions");
+                       if (!extsect)
+                               {
+                               ERR_clear_error();
+                               extsect = "default";
+                               }
+                       }
                X509V3_set_ctx_test(&ctx2);
                X509V3_set_conf_lhash(&ctx2, extconf);
                if (!X509V3_EXT_add_conf(extconf, &ctx2, extsect, NULL))
@@ -576,7 +626,15 @@ bad:
                        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 (BIO_write_filename(out,outfile) <= 0)
@@ -760,7 +818,7 @@ bad:
                                }
                        else if (text == i)
                                {
-                               X509_print(out,x);
+                               X509_print_ex(out,x,nmflag, certflag);
                                }
                        else if (startdate == i)
                                {
@@ -802,7 +860,7 @@ bad:
                                if (Upkey == NULL)
                                        {
                                        Upkey=load_key(bio_err,
-                                               keyfile,keyformat, passin);
+                                               keyfile,keyformat, passin, e);
                                        if (Upkey == NULL) goto end;
                                        }
 #ifndef NO_DSA
@@ -820,7 +878,8 @@ bad:
                                if (CAkeyfile != NULL)
                                        {
                                        CApkey=load_key(bio_err,
-                                               CAkeyfile,CAkeyformat, passin);
+                                               CAkeyfile,CAkeyformat, passin,
+                                               e);
                                        if (CApkey == NULL) goto end;
                                        }
 #ifndef NO_DSA
@@ -847,7 +906,7 @@ bad:
                                else
                                        {
                                        pk=load_key(bio_err,
-                                               keyfile,FORMAT_PEM, passin);
+                                               keyfile,FORMAT_PEM, passin, e);
                                        if (pk == NULL) goto end;
                                        }
 
@@ -870,6 +929,10 @@ bad:
                                        }
                                noout=1;
                                }
+                       else if (ocspid == i)
+                               {
+                               X509_ocspid_print(out, x);
+                               }
                        }
                }
 
@@ -933,8 +996,8 @@ end:
                app_RAND_write_file(NULL, bio_err);
        OBJ_cleanup();
        CONF_free(extconf);
-       BIO_free(out);
-       BIO_free(STDout);
+       BIO_free_all(out);
+       BIO_free_all(STDout);
        X509_STORE_free(ctx);
        X509_REQ_free(req);
        X509_free(x);