Locking issues.
[openssl.git] / apps / pkcs12.c
index 261139565dce59c78d337716a91c116c75ce07b7..365a8ada937d10a199894f350a8a425726d55533 100644 (file)
@@ -66,6 +66,7 @@
 #include <openssl/err.h>
 #include <openssl/pem.h>
 #include <openssl/pkcs12.h>
+#include <openssl/engine.h>
 
 #define PROG pkcs12_main
 
@@ -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();
 
@@ -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;
@@ -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;
@@ -350,8 +373,15 @@ int MAIN(int argc, char **argv)
     CRYPTO_push_info("write files");
 #endif
 
-    if (!outfile) out = BIO_new_fp(stdout, BIO_NOCLOSE);
-    else out = BIO_new_file(outfile, "wb");
+    if (!outfile) {
+       out = BIO_new_fp(stdout, BIO_NOCLOSE);
+#ifdef VMS
+       {
+           BIO *tmpbio = BIO_new(BIO_f_linebuffer());
+           out = BIO_push(tmpbio, out);
+       }
+#endif
+    } else out = BIO_new_file(outfile, "wb");
     if (!out) {
        BIO_printf(bio_err, "Error opening output file %s\n",
                                                outfile ? outfile : "<stdout>");
@@ -657,7 +687,7 @@ int MAIN(int argc, char **argv)
     CRYPTO_remove_all_info();
 #endif
     BIO_free(in);
-    BIO_free(out);
+    BIO_free_all(out);
     if (canames) sk_free(canames);
     if(passin) OPENSSL_free(passin);
     if(passout) OPENSSL_free(passout);
@@ -880,14 +910,14 @@ int print_attribs (BIO *out, STACK_OF(X509_ATTRIBUTE) *attrlst, char *name)
                                break;
 
                                case V_ASN1_OCTET_STRING:
-                               hex_prin(out, av->value.bit_string->data,
-                                       av->value.bit_string->length);
+                               hex_prin(out, av->value.octet_string->data,
+                                       av->value.octet_string->length);
                                BIO_printf(out, "\n");  
                                break;
 
                                case V_ASN1_BIT_STRING:
-                               hex_prin(out, av->value.octet_string->data,
-                                       av->value.octet_string->length);
+                               hex_prin(out, av->value.bit_string->data,
+                                       av->value.bit_string->length);
                                BIO_printf(out, "\n");  
                                break;