The majority of the OCSP code from CertCo.
[openssl.git] / apps / pkcs12.c
index 0f3ac4977a235806c3a7f817ad5705905f03b480..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();
 
@@ -197,7 +200,7 @@ int MAIN(int argc, char **argv)
                } else if (!strcmp (*args, "-caname")) {
                    if (args[1]) {
                        args++; 
-                       if (!canames) canames = sk_new(NULL);
+                       if (!canames) canames = sk_new_null();
                        sk_push(canames, *args);
                    } else badarg = 1;
                } else if (!strcmp (*args, "-in")) {
@@ -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>");
@@ -404,7 +434,7 @@ int MAIN(int argc, char **argv)
        CRYPTO_push_info("reading certs from input");
 #endif
 
-       certs = sk_X509_new(NULL);
+       certs = sk_X509_new_null();
 
        /* Load in all certs in input file */
        if(!cert_load(in, certs)) {
@@ -436,7 +466,7 @@ int MAIN(int argc, char **argv)
        CRYPTO_push_info("reading certs from certfile");
 #endif
 
-       bags = sk_PKCS12_SAFEBAG_new (NULL);
+       bags = sk_PKCS12_SAFEBAG_new_null ();
 
        /* Add any more certificates asked for */
        if (certsin) {
@@ -527,7 +557,7 @@ int MAIN(int argc, char **argv)
                goto export_end;
        }
 
-       safes = sk_PKCS7_new (NULL);
+       safes = sk_PKCS7_new_null ();
        sk_PKCS7_push (safes, authsafe);
 
 #ifdef CRYPTO_MDEBUG
@@ -543,7 +573,7 @@ int MAIN(int argc, char **argv)
        p8 = NULL;
         if (name) PKCS12_add_friendlyname (bag, name, -1);
        PKCS12_add_localkeyid (bag, keyid, keyidlen);
-       bags = sk_PKCS12_SAFEBAG_new(NULL);
+       bags = sk_PKCS12_SAFEBAG_new_null();
        sk_PKCS12_SAFEBAG_push (bags, bag);
 
 #ifdef CRYPTO_MDEBUG
@@ -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;