Stop spurious "unable to load config info" errors in req
[openssl.git] / apps / pkcs12.c
index f277956cd96423fedbf2935f090afad259519693..cd9c02808ac292947d24f9b591501b2dced0e307 100644 (file)
@@ -66,7 +66,6 @@
 #include <openssl/err.h>
 #include <openssl/pem.h>
 #include <openssl/pkcs12.h>
-#include <openssl/engine.h>
 
 #define PROG pkcs12_main
 
@@ -96,7 +95,7 @@ int MAIN(int argc, char **argv)
     ENGINE *e = NULL;
     char *infile=NULL, *outfile=NULL, *keyname = NULL; 
     char *certfile=NULL;
-    BIO *in=NULL, *out = NULL, *inkey = NULL, *certsin = NULL;
+    BIO *in=NULL, *out = NULL;
     char **args;
     char *name = NULL;
     char *csp_name = NULL;
@@ -300,19 +299,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);
 
     if(passarg) {
        if(export_cert) passargout = passarg;
@@ -358,6 +345,7 @@ int MAIN(int argc, char **argv)
            goto end;
    }
 
+#if 0
    if (certfile) {
        if(!(certsin = BIO_new_file(certfile, "r"))) {
            BIO_printf(bio_err, "Can't open certificate file %s\n", certfile);
@@ -373,6 +361,7 @@ int MAIN(int argc, char **argv)
            goto end;
        }
      }
+#endif
 
 #ifdef CRYPTO_MDEBUG
     CRYPTO_pop_info();
@@ -426,12 +415,9 @@ int MAIN(int argc, char **argv)
        CRYPTO_push_info("process -export_cert");
        CRYPTO_push_info("reading private key");
 #endif
-       key = PEM_read_bio_PrivateKey(inkey ? inkey : in, NULL, NULL, passin);
-       if (!inkey) (void) BIO_reset(in);
-       else BIO_free(inkey);
+       key = load_key(bio_err, keyname ? keyname : infile, FORMAT_PEM,
+               passin, e, "private key");
        if (!key) {
-               BIO_printf (bio_err, "Error loading private key\n");
-               ERR_print_errors(bio_err);
                goto export_end;
        }
 
@@ -440,12 +426,9 @@ int MAIN(int argc, char **argv)
        CRYPTO_push_info("reading certs from input");
 #endif
 
-       certs = sk_X509_new_null();
-
        /* Load in all certs in input file */
-       if(!cert_load(in, certs)) {
-               BIO_printf(bio_err, "Error loading certificates from input\n");
-               ERR_print_errors(bio_err);
+       if(!(certs = load_certs(bio_err, infile, FORMAT_PEM, NULL, e,
+               "certificates"))) {
                goto export_end;
        }
 
@@ -475,13 +458,11 @@ int MAIN(int argc, char **argv)
        bags = sk_PKCS12_SAFEBAG_new_null ();
 
        /* Add any more certificates asked for */
-       if (certsin) {
-               if(!cert_load(certsin, certs)) {
-                       BIO_printf(bio_err, "Error loading certificates from certfile\n");
-                       ERR_print_errors(bio_err);
+       if (certfile) {
+               if(!(certs = load_certs(bio_err, certfile, FORMAT_PEM, NULL, e,
+                       "certificates from certfile"))) {
                        goto export_end;
                }
-               BIO_free(certsin);
        }
 
 #ifdef CRYPTO_MDEBUG
@@ -698,6 +679,7 @@ int MAIN(int argc, char **argv)
     if (canames) sk_free(canames);
     if(passin) OPENSSL_free(passin);
     if(passout) OPENSSL_free(passout);
+    apps_shutdown();
     EXIT(ret);
 }
 
@@ -827,6 +809,9 @@ int get_cert_chain (X509 *cert, X509_STORE *store, STACK_OF(X509) **chain)
        STACK_OF(X509) *chn;
        int i;
 
+       /* FIXME: Should really check the return status of X509_STORE_CTX_init
+        * for an error, but how that fits into the return value of this
+        * function is less obvious. */
        X509_STORE_CTX_init(&store_ctx, store, cert, NULL);
        if (X509_verify_cert(&store_ctx) <= 0) {
                i = X509_STORE_CTX_get_error (&store_ctx);