Use apps_shutdown() in all applications, in case someone decides not
[oweals/openssl.git] / apps / dgst.c
index 8500f6c7e67501ba1f0c78b2adc8baca16f0987e..e3edf302c3ac9c953e61a02ca3bfb6ae64896884 100644 (file)
@@ -74,7 +74,7 @@
 #undef PROG
 #define PROG   dgst_main
 
-void do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, char binout,
+void do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
                EVP_PKEY *key, unsigned char *sigin, int siglen);
 
 int MAIN(int, char **);
@@ -96,7 +96,7 @@ int MAIN(int argc, char **argv)
        int keyform=FORMAT_PEM;
        const char *outfile = NULL, *keyfile = NULL;
        const char *sigfile = NULL, *randfile = NULL;
-       char out_bin = -1, want_pub = 0, do_verify = 0;
+       int out_bin = -1, want_pub = 0, do_verify = 0;
        EVP_PKEY *sigkey = NULL;
        unsigned char *sigbuf = NULL;
        int siglen = 0;
@@ -225,23 +225,7 @@ int MAIN(int argc, char **argv)
                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);
-               }
+        e = setup_engine(bio_err, engine, 0);
 
        in=BIO_new(BIO_s_file());
        bmd=BIO_new(BIO_f_md());
@@ -272,7 +256,7 @@ int MAIN(int argc, char **argv)
                else    out = BIO_new_file(outfile, "w");
        } else {
                out = BIO_new_fp(stdout, BIO_NOCLOSE);
-#ifdef VMS
+#ifdef OPENSSL_SYS_VMS
                {
                BIO *tmpbio = BIO_new(BIO_f_linebuffer());
                out = BIO_push(tmpbio, out);
@@ -289,52 +273,19 @@ int MAIN(int argc, char **argv)
 
        if(keyfile)
                {
-               if (keyform == FORMAT_PEM)
-                       {
-                       BIO *keybio;
-                       keybio = BIO_new_file(keyfile, "r");
-                       if(!keybio)
-                               {
-                               BIO_printf(bio_err,
-                                       "Error opening key file %s\n",
-                                       keyfile);
-                               ERR_print_errors(bio_err);
-                               goto end;
-                               }
-                       if(want_pub) 
-                               sigkey = PEM_read_bio_PUBKEY(keybio,
-                                       NULL, NULL, NULL);
-                       else
-                               sigkey = PEM_read_bio_PrivateKey(keybio,
-                                       NULL, NULL, NULL);
-                       BIO_free(keybio);
-                       }
-               else if (keyform == FORMAT_ENGINE)
-                       {
-                       if (!e)
-                               {
-                               BIO_printf(bio_err,"no engine specified\n");
-                               goto end;
-                               }
-                       if (want_pub)
-                               sigkey = ENGINE_load_public_key(e, keyfile, NULL);
-                       else
-                               sigkey = ENGINE_load_private_key(e, keyfile, NULL);
-                       }
+               if (want_pub)
+                       sigkey = load_pubkey(bio_err, keyfile, keyform, NULL,
+                               e, "key file");
                else
+                       sigkey = load_key(bio_err, keyfile, keyform, NULL,
+                               e, "key file");
+               if (!sigkey)
                        {
-                       BIO_printf(bio_err,
-                               "bad input format specified for key file\n");
+                       /* load_[pub]key() has already printed an appropriate
+                          message */
                        goto end;
                        }
-               
-               if(!sigkey) {
-                       BIO_printf(bio_err, "Error reading key file %s\n",
-                                                               keyfile);
-                       ERR_print_errors(bio_err);
-                       goto end;
                }
-       }
 
        if(sigfile && sigkey) {
                BIO *sigbio;
@@ -396,10 +347,11 @@ end:
        EVP_PKEY_free(sigkey);
        if(sigbuf) OPENSSL_free(sigbuf);
        if (bmd != NULL) BIO_free(bmd);
+       apps_shutdown();
        EXIT(err);
        }
 
-void do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, char binout,
+void do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
                        EVP_PKEY *key, unsigned char *sigin, int siglen)
        {
        int len;