New function X509_CTX_rget_chain(), make SSL_SESSION_print() display return code.
[openssl.git] / apps / smime.c
index 882838c66fa3750a90b6ee19b1f23fea15ec98bf..d5461f1a48ac68956a9e5918bae584ec602fac6c 100644 (file)
 
 #include <stdio.h>
 #include <string.h>
+#include "apps.h"
 #include <openssl/pem.h>
 #include <openssl/err.h>
-#include "apps.h"
 
 #undef PROG
 #define PROG smime_main
 static X509 *load_cert(char *file);
-static EVP_PKEY *load_key(char *file);
+static EVP_PKEY *load_key(char *file, char *pass);
 static STACK_OF(X509) *load_certs(char *file);
 static X509_STORE *setup_verify(char *CAfile, char *CApath);
 static int save_certs(char *signerfile, STACK_OF(X509) *signers);
@@ -98,7 +98,7 @@ int MAIN(int argc, char **argv)
        int badarg = 0;
        int flags = PKCS7_DETACHED;
        char *to = NULL, *from = NULL, *subject = NULL;
-       char *CAfile = NULL, *CApath = NULL;
+       char *CAfile = NULL, *CApath = NULL, *passin = NULL;
 
        args = argv + 1;
 
@@ -110,16 +110,20 @@ int MAIN(int argc, char **argv)
                else if (!strcmp (*args, "-sign")) operation = SMIME_SIGN;
                else if (!strcmp (*args, "-verify")) operation = SMIME_VERIFY;
                else if (!strcmp (*args, "-pk7out")) operation = SMIME_PK7OUT;
+#ifndef NO_DES
                else if (!strcmp (*args, "-des3")) 
                                cipher = EVP_des_ede3_cbc();
                else if (!strcmp (*args, "-des")) 
                                cipher = EVP_des_cbc();
+#endif
+#ifndef NO_RC2
                else if (!strcmp (*args, "-rc2-40")) 
                                cipher = EVP_rc2_40_cbc();
                else if (!strcmp (*args, "-rc2-128")) 
                                cipher = EVP_rc2_cbc();
                else if (!strcmp (*args, "-rc2-64")) 
                                cipher = EVP_rc2_64_cbc();
+#endif
                else if (!strcmp (*args, "-text")) 
                                flags |= PKCS7_TEXT;
                else if (!strcmp (*args, "-nointern")) 
@@ -138,7 +142,18 @@ int MAIN(int argc, char **argv)
                                flags |= PKCS7_BINARY;
                else if (!strcmp (*args, "-nosigs"))
                                flags |= PKCS7_NOSIGS;
-               else if (!strcmp (*args, "-to")) {
+               else if (!strcmp(*argv,"-passin")) {
+                       if (--argc < 1) badarg = 1;
+                       else passin= *(++argv);
+               } else if (!strcmp(*argv,"-envpassin")) {
+                       if (--argc < 1) badarg = 1;
+                       else if(!(passin= getenv(*(++argv)))) {
+                               BIO_printf(bio_err,
+                                "Can't read environment variable %s\n",
+                                                               *argv);
+                               badarg = 1;
+                       }
+               } else if (!strcmp (*args, "-to")) {
                        if (args[1]) {
                                args++;
                                to = *args;
@@ -222,10 +237,15 @@ int MAIN(int argc, char **argv)
                BIO_printf (bio_err, "-sign          sign message\n");
                BIO_printf (bio_err, "-verify        verify signed message\n");
                BIO_printf (bio_err, "-pk7out        output PKCS#7 structure\n");
+#ifndef NO_DES
                BIO_printf (bio_err, "-des3          encrypt with triple DES\n");
-               BIO_printf (bio_err, "-rc2-40        encrypt with RC2-40\n");
+               BIO_printf (bio_err, "-des           encrypt with DES\n");
+#endif
+#ifndef NO_RC2
+               BIO_printf (bio_err, "-rc2-40        encrypt with RC2-40 (default)\n");
                BIO_printf (bio_err, "-rc2-64        encrypt with RC2-64\n");
                BIO_printf (bio_err, "-rc2-128       encrypt with RC2-128\n");
+#endif
                BIO_printf (bio_err, "-nointern      don't search certificates in message for signer\n");
                BIO_printf (bio_err, "-nosigs        don't verify message signature\n");
                BIO_printf (bio_err, "-noverify      don't verify signers certificate\n");
@@ -260,7 +280,14 @@ int MAIN(int argc, char **argv)
        }
 
        if(operation == SMIME_ENCRYPT) {
-               if (!cipher) cipher = EVP_rc2_40_cbc();
+               if (!cipher) {
+#ifndef NO_RC2                 
+                       cipher = EVP_rc2_40_cbc();
+#else
+                       BIO_printf(bio_err, "No cipher selected\n");
+                       goto end;
+#endif
+               }
                encerts = sk_X509_new_null();
                while (*args) {
                        if(!(cert = load_cert(*args))) {
@@ -303,7 +330,7 @@ int MAIN(int argc, char **argv)
        } else keyfile = NULL;
 
        if(keyfile) {
-               if(!(key = load_key(keyfile))) {
+               if(!(key = load_key(keyfile, passin))) {
                        BIO_printf(bio_err, "Can't read recipient certificate file %s\n", keyfile);
                        ERR_print_errors(bio_err);
                        goto end;
@@ -405,12 +432,12 @@ static X509 *load_cert(char *file)
        return cert;
 }
 
-static EVP_PKEY *load_key(char *file)
+static EVP_PKEY *load_key(char *file, char *pass)
 {
        BIO *in;
        EVP_PKEY *key;
        if(!(in = BIO_new_file(file, "r"))) return NULL;
-       key = PEM_read_bio_PrivateKey(in, NULL, NULL,NULL);
+       key = PEM_read_bio_PrivateKey(in, NULL,PEM_cb,pass);
        BIO_free(in);
        return key;
 }