Extend all the loading functions to take an engine pointer, a pass
[openssl.git] / apps / pkcs12.c
index 6789169bdb48dc7d6c31ebad1c09abee42f66f9e..90abbb84d2a35c409b4039a25a9575b81f8b3505 100644 (file)
@@ -1,5 +1,5 @@
 /* pkcs12.c */
-#if !defined(NO_DES) && !defined(NO_SHA1)
+#if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA1)
 
 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  * project 1999.
 #include <openssl/err.h>
 #include <openssl/pem.h>
 #include <openssl/pkcs12.h>
+#include <openssl/engine.h>
 
 #define PROG pkcs12_main
 
-EVP_CIPHER *enc;
+const EVP_CIPHER *enc;
 
 
 #define NOKEYS         0x1
@@ -92,6 +93,7 @@ int MAIN(int, char **);
 
 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;
@@ -118,6 +120,7 @@ int MAIN(int argc, char **argv)
     char *passin = NULL, *passout = NULL;
     char *inrand = NULL;
     char *CApath = NULL, *CAfile = NULL;
+    char *engine=NULL;
 
     apps_startup();
 
@@ -144,7 +147,7 @@ int MAIN(int argc, char **argv)
                        cert_pbe = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
                else if (!strcmp (*args, "-export")) export_cert = 1;
                else if (!strcmp (*args, "-des")) enc=EVP_des_cbc();
-#ifndef NO_IDEA
+#ifndef OPENSSL_NO_IDEA
                else if (!strcmp (*args, "-idea")) enc=EVP_idea_cbc();
 #endif
                else if (!strcmp (*args, "-des3")) enc = EVP_des_ede3_cbc();
@@ -236,6 +239,11 @@ int MAIN(int argc, char **argv)
                        args++; 
                        CAfile = *args;
                    } else badarg = 1;
+               } else if (!strcmp(*args,"-engine")) {
+                   if (args[1]) {
+                       args++; 
+                       engine = *args;
+                   } else badarg = 1;
                } else badarg = 1;
 
        } else badarg = 1;
@@ -264,7 +272,7 @@ int MAIN(int argc, char **argv)
        BIO_printf (bio_err, "-info         give info about PKCS#12 structure.\n");
        BIO_printf (bio_err, "-des          encrypt private keys with DES\n");
        BIO_printf (bio_err, "-des3         encrypt private keys with triple DES (default)\n");
-#ifndef NO_IDEA
+#ifndef OPENSSL_NO_IDEA
        BIO_printf (bio_err, "-idea         encrypt private keys with idea\n");
 #endif
        BIO_printf (bio_err, "-nodes        don't encrypt private keys\n");
@@ -279,12 +287,27 @@ int MAIN(int argc, char **argv)
        BIO_printf (bio_err, "-password p   set import/export password source\n");
        BIO_printf (bio_err, "-passin p     input file pass phrase source\n");
        BIO_printf (bio_err, "-passout p    output file pass phrase source\n");
+       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");
        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(passarg) {
        if(export_cert) passargout = passarg;
        else passargin = passarg;
@@ -352,7 +375,7 @@ int MAIN(int argc, char **argv)
 
     if (!outfile) {
        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);
@@ -498,7 +521,7 @@ int MAIN(int argc, char **argv)
        for(i = 0; i < sk_X509_num(certs); i++) {
                X509 *cert = NULL;
                cert = sk_X509_value(certs, i);
-               bag = M_PKCS12_x5092certbag(cert);
+               bag = PKCS12_x5092certbag(cert);
                /* If it matches private key set id */
                if(cert == ucert) {
                        if(name) PKCS12_add_friendlyname(bag, name, -1);
@@ -569,9 +592,9 @@ int MAIN(int argc, char **argv)
        CRYPTO_push_info("building pkcs12");
 #endif
 
-       p12 = PKCS12_init (NID_pkcs7_data);
+       p12 = PKCS12_init(NID_pkcs7_data);
 
-       M_PKCS12_pack_authsafes (p12, safes);
+       PKCS12_pack_authsafes(p12, safes);
 
        sk_PKCS7_pop_free(safes, PKCS7_free);
        safes = NULL;
@@ -679,20 +702,20 @@ int dump_certs_keys_p12 (BIO *out, PKCS12 *p12, char *pass,
        int i, bagnid;
        PKCS7 *p7;
 
-       if (!( asafes = M_PKCS12_unpack_authsafes (p12))) return 0;
+       if (!( asafes = PKCS12_unpack_authsafes(p12))) return 0;
        for (i = 0; i < sk_PKCS7_num (asafes); i++) {
                p7 = sk_PKCS7_value (asafes, i);
                bagnid = OBJ_obj2nid (p7->type);
                if (bagnid == NID_pkcs7_data) {
-                       bags = M_PKCS12_unpack_p7data (p7);
+                       bags = PKCS12_unpack_p7data(p7);
                        if (options & INFO) BIO_printf (bio_err, "PKCS7 Data\n");
                } else if (bagnid == NID_pkcs7_encrypted) {
                        if (options & INFO) {
-                               BIO_printf (bio_err, "PKCS7 Encrypted data: ");
-                               alg_print (bio_err, 
+                               BIO_printf(bio_err, "PKCS7 Encrypted data: ");
+                               alg_print(bio_err, 
                                        p7->d.encrypted->enc_data->algorithm);
                        }
-                       bags = M_PKCS12_unpack_p7encdata (p7, pass, passlen);
+                       bags = PKCS12_unpack_p7encdata(p7, pass, passlen);
                } else continue;
                if (!bags) return 0;
                if (!dump_certs_pkeys_bags (out, bags, pass, passlen, 
@@ -747,7 +770,7 @@ int dump_certs_pkeys_bag (BIO *out, PKCS12_SAFEBAG *bag, char *pass,
                }
                if (options & NOKEYS) return 1;
                print_attribs (out, bag->attrib, "Bag Attributes");
-               if (!(p8 = M_PKCS12_decrypt_skey (bag, pass, passlen)))
+               if (!(p8 = PKCS12_decrypt_skey(bag, pass, passlen)))
                                return 0;
                if (!(pkey = EVP_PKCS82PKEY (p8))) return 0;
                print_attribs (out, p8->attributes, "Key Attributes");
@@ -765,7 +788,7 @@ int dump_certs_pkeys_bag (BIO *out, PKCS12_SAFEBAG *bag, char *pass,
                print_attribs (out, bag->attrib, "Bag Attributes");
                if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate )
                                                                 return 1;
-               if (!(x509 = M_PKCS12_certbag2x509(bag))) return 0;
+               if (!(x509 = PKCS12_certbag2x509(bag))) return 0;
                dump_cert_text (out, x509);
                PEM_write_bio_X509 (out, x509);
                X509_free(x509);