#include <stdio.h>
#include <string.h>
#include "apps.h"
+#include <openssl/crypto.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#define SMIME_VERIFY 4
#define SMIME_PK7OUT 5
+int MAIN(int, char **);
+
int MAIN(int argc, char **argv)
{
int operation = 0;
int badarg = 0;
int flags = PKCS7_DETACHED;
char *to = NULL, *from = NULL, *subject = NULL;
- char *CAfile = NULL, *CApath = NULL, *passin = NULL;
-
+ char *CAfile = NULL, *CApath = NULL;
+ char *passargin = NULL, *passin = NULL;
+ char *inrand = NULL;
+ int need_rand = 0;
args = argv + 1;
ret = 1;
flags |= PKCS7_BINARY;
else if (!strcmp (*args, "-nosigs"))
flags |= PKCS7_NOSIGS;
- 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,"-rand")) {
+ if (args[1]) {
+ args++;
+ inrand = *args;
+ } else badarg = 1;
+ need_rand = 1;
+ } else if (!strcmp(*args,"-passin")) {
+ if (args[1]) {
+ args++;
+ passargin = *args;
+ } else badarg = 1;
} else if (!strcmp (*args, "-to")) {
if (args[1]) {
args++;
BIO_printf(bio_err, "No signer certificate specified\n");
badarg = 1;
}
+ need_rand = 1;
} else if(operation == SMIME_DECRYPT) {
if(!recipfile) {
BIO_printf(bio_err, "No recipient certificate and key specified\n");
BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
badarg = 1;
}
+ need_rand = 1;
} else if(!operation) badarg = 1;
if (badarg) {
BIO_printf (bio_err, "-text include or delete text MIME headers\n");
BIO_printf (bio_err, "-CApath dir trusted certificates directory\n");
BIO_printf (bio_err, "-CAfile file trusted certificates file\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");
BIO_printf (bio_err, "cert.pem recipient certificate(s) for encryption\n");
goto end;
}
+ if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
+ BIO_printf(bio_err, "Error getting password\n");
+ goto end;
+ }
+
+ if (need_rand) {
+ app_RAND_load_file(NULL, bio_err, (inrand != NULL));
+ if (inrand != NULL)
+ BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
+ app_RAND_load_files(inrand));
+ }
+
ret = 2;
if(operation != SMIME_SIGN) flags &= ~PKCS7_DETACHED;
encerts = sk_X509_new_null();
while (*args) {
if(!(cert = load_cert(*args))) {
- BIO_printf(bio_err, "Can't read recipent certificate file %s\n", *args);
+ BIO_printf(bio_err, "Can't read recipient certificate file %s\n", *args);
goto end;
}
sk_X509_push(encerts, cert);
}
} else out = BIO_new_fp(stdout, BIO_NOCLOSE);
- if(operation == SMIME_VERIFY)
+ if(operation == SMIME_VERIFY) {
if(!(store = setup_verify(CAfile, CApath))) goto end;
+ }
ret = 3;
BIO_printf(bio_err, "Verification Failure\n");
goto end;
}
- signers = PKCS7_iget_signers(p7, other, flags);
+ signers = PKCS7_get0_signers(p7, other, flags);
if(!save_certs(signerfile, signers)) {
BIO_printf(bio_err, "Error writing signers to %s\n",
signerfile);
}
ret = 0;
end:
+ if (need_rand)
+ app_RAND_write_file(NULL, bio_err);
if(ret) ERR_print_errors(bio_err);
sk_X509_pop_free(encerts, X509_free);
sk_X509_pop_free(other, X509_free);
BIO_free(in);
BIO_free(indata);
BIO_free(out);
+ if(passin) Free(passin);
return (ret);
}
BIO *in;
EVP_PKEY *key;
if(!(in = BIO_new_file(file, "r"))) return NULL;
- key = PEM_read_bio_PrivateKey(in, NULL,PEM_cb,pass);
+ key = PEM_read_bio_PrivateKey(in, NULL,NULL,pass);
BIO_free(in);
return key;
}
return NULL;
}
-int save_certs(char *signerfile, STACK_OF(X509) *signers)
+static int save_certs(char *signerfile, STACK_OF(X509) *signers)
{
int i;
BIO *tmp;