DSO_load() should also work when it is passed a NULL - a new DSO is created
[openssl.git] / apps / gendsa.c
index b1a1c4fcfae9fdfb3c1aa1435b1566066be4659d..1c0ec371d272941d03ea8ab3c046f8f4d8fa4b6f 100644 (file)
@@ -68,6 +68,7 @@
 #include <openssl/dsa.h>
 #include <openssl/x509.h>
 #include <openssl/pem.h>
+#include <openssl/engine.h>
 
 #define DEFBITS        512
 #undef PROG
@@ -77,6 +78,7 @@ int MAIN(int, char **);
 
 int MAIN(int argc, char **argv)
        {
+       ENGINE *e = NULL;
        DSA *dsa=NULL;
        int ret=1;
        char *outfile=NULL;
@@ -84,6 +86,7 @@ int MAIN(int argc, char **argv)
        char *passargout = NULL, *passout = NULL;
        BIO *out=NULL,*in=NULL;
        EVP_CIPHER *enc=NULL;
+       char *engine=NULL;
 
        apps_startup();
 
@@ -106,6 +109,11 @@ int MAIN(int argc, char **argv)
                        if (--argc < 1) goto bad;
                        passargout= *(++argv);
                        }
+               else if (strcmp(*argv,"-engine") == 0)
+                       {
+                       if (--argc < 1) goto bad;
+                       engine= *(++argv);
+                       }
                else if (strcmp(*argv,"-rand") == 0)
                        {
                        if (--argc < 1) goto bad;
@@ -145,6 +153,7 @@ bad:
 #ifndef NO_IDEA
                BIO_printf(bio_err," -idea     - encrypt the generated key with IDEA in cbc mode\n");
 #endif
+               BIO_printf(bio_err," -engine e - use engine e, possibly a hardware device.\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");
@@ -153,6 +162,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(!app_passwd(bio_err, NULL, passargout, NULL, &passout)) {
                BIO_printf(bio_err, "Error getting password\n");
                goto end;
@@ -178,7 +205,15 @@ bad:
        if (out == NULL) 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)
@@ -209,9 +244,9 @@ end:
        if (ret != 0)
                ERR_print_errors(bio_err);
        if (in != NULL) BIO_free(in);
-       if (out != NULL) BIO_free(out);
+       if (out != NULL) BIO_free_all(out);
        if (dsa != NULL) DSA_free(dsa);
-       if(passout) Free(passout);
+       if(passout) OPENSSL_free(passout);
        EXIT(ret);
        }
 #endif