sl@0: /* smime.c */ sl@0: /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL sl@0: * project. sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * licensing@OpenSSL.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: /* S/MIME utility function */ sl@0: sl@0: #include sl@0: #include sl@0: #include "apps.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #undef PROG sl@0: #define PROG smime_main sl@0: static int save_certs(char *signerfile, STACK_OF(X509) *signers); sl@0: static int smime_cb(int ok, X509_STORE_CTX *ctx); sl@0: sl@0: #define SMIME_OP 0x10 sl@0: #define SMIME_ENCRYPT (1 | SMIME_OP) sl@0: #define SMIME_DECRYPT 2 sl@0: #define SMIME_SIGN (3 | SMIME_OP) sl@0: #define SMIME_VERIFY 4 sl@0: #define SMIME_PK7OUT 5 sl@0: sl@0: sl@0: int MAIN(int, char **); sl@0: sl@0: int MAIN(int argc, char **argv) sl@0: { sl@0: ENGINE *e = NULL; sl@0: int operation = 0; sl@0: int ret = 0; sl@0: char **args; sl@0: const char *inmode = "r", *outmode = "w"; sl@0: char *infile = NULL, *outfile = NULL; sl@0: char *signerfile = NULL, *recipfile = NULL; sl@0: char *certfile = NULL, *keyfile = NULL, *contfile=NULL; sl@0: const EVP_CIPHER *cipher = NULL; sl@0: PKCS7 *p7 = NULL; sl@0: X509_STORE *store = NULL; sl@0: X509 *cert = NULL, *recip = NULL, *signer = NULL; sl@0: EVP_PKEY *key = NULL; sl@0: STACK_OF(X509) *encerts = NULL, *other = NULL; sl@0: BIO *in = NULL, *out = NULL, *indata = NULL; sl@0: int badarg = 0; sl@0: int flags = PKCS7_DETACHED; sl@0: char *to = NULL, *from = NULL, *subject = NULL; sl@0: char *CAfile = NULL, *CApath = NULL; sl@0: char *passargin = NULL, *passin = NULL; sl@0: char *inrand = NULL; sl@0: int need_rand = 0; sl@0: int informat = FORMAT_SMIME, outformat = FORMAT_SMIME; sl@0: int keyform = FORMAT_PEM; sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: char *engine=NULL; sl@0: #endif sl@0: sl@0: X509_VERIFY_PARAM *vpm = NULL; sl@0: sl@0: args = argv + 1; sl@0: ret = 1; sl@0: sl@0: apps_startup(); sl@0: sl@0: if (bio_err == NULL) sl@0: { sl@0: if ((bio_err = BIO_new(BIO_s_file())) != NULL) sl@0: BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT); sl@0: sl@0: } sl@0: sl@0: if (!load_config(bio_err, NULL)) sl@0: goto end; sl@0: sl@0: while (!badarg && *args && *args[0] == '-') sl@0: { sl@0: if (!strcmp (*args, "-encrypt")) sl@0: operation = SMIME_ENCRYPT; sl@0: else if (!strcmp (*args, "-decrypt")) sl@0: operation = SMIME_DECRYPT; sl@0: else if (!strcmp (*args, "-sign")) sl@0: operation = SMIME_SIGN; sl@0: else if (!strcmp (*args, "-verify")) sl@0: operation = SMIME_VERIFY; sl@0: else if (!strcmp (*args, "-pk7out")) sl@0: operation = SMIME_PK7OUT; sl@0: #ifndef OPENSSL_NO_DES sl@0: else if (!strcmp (*args, "-des3")) sl@0: cipher = EVP_des_ede3_cbc(); sl@0: else if (!strcmp (*args, "-des")) sl@0: cipher = EVP_des_cbc(); sl@0: #endif sl@0: #ifndef OPENSSL_NO_RC2 sl@0: else if (!strcmp (*args, "-rc2-40")) sl@0: cipher = EVP_rc2_40_cbc(); sl@0: else if (!strcmp (*args, "-rc2-128")) sl@0: cipher = EVP_rc2_cbc(); sl@0: else if (!strcmp (*args, "-rc2-64")) sl@0: cipher = EVP_rc2_64_cbc(); sl@0: #endif sl@0: #ifndef OPENSSL_NO_AES sl@0: else if (!strcmp(*args,"-aes128")) sl@0: cipher = EVP_aes_128_cbc(); sl@0: else if (!strcmp(*args,"-aes192")) sl@0: cipher = EVP_aes_192_cbc(); sl@0: else if (!strcmp(*args,"-aes256")) sl@0: cipher = EVP_aes_256_cbc(); sl@0: #endif sl@0: else if (!strcmp (*args, "-text")) sl@0: flags |= PKCS7_TEXT; sl@0: else if (!strcmp (*args, "-nointern")) sl@0: flags |= PKCS7_NOINTERN; sl@0: else if (!strcmp (*args, "-noverify")) sl@0: flags |= PKCS7_NOVERIFY; sl@0: else if (!strcmp (*args, "-nochain")) sl@0: flags |= PKCS7_NOCHAIN; sl@0: else if (!strcmp (*args, "-nocerts")) sl@0: flags |= PKCS7_NOCERTS; sl@0: else if (!strcmp (*args, "-noattr")) sl@0: flags |= PKCS7_NOATTR; sl@0: else if (!strcmp (*args, "-nodetach")) sl@0: flags &= ~PKCS7_DETACHED; sl@0: else if (!strcmp (*args, "-nosmimecap")) sl@0: flags |= PKCS7_NOSMIMECAP; sl@0: else if (!strcmp (*args, "-binary")) sl@0: flags |= PKCS7_BINARY; sl@0: else if (!strcmp (*args, "-nosigs")) sl@0: flags |= PKCS7_NOSIGS; sl@0: else if (!strcmp (*args, "-nooldmime")) sl@0: flags |= PKCS7_NOOLDMIMETYPE; sl@0: else if (!strcmp (*args, "-crlfeol")) sl@0: flags |= PKCS7_CRLFEOL; sl@0: else if (!strcmp(*args,"-rand")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: inrand = *args; sl@0: } sl@0: else sl@0: badarg = 1; sl@0: need_rand = 1; sl@0: } sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: else if (!strcmp(*args,"-engine")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: engine = *args; sl@0: } sl@0: else badarg = 1; sl@0: } sl@0: #endif sl@0: else if (!strcmp(*args,"-passin")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: passargin = *args; sl@0: } sl@0: else sl@0: badarg = 1; sl@0: } sl@0: else if (!strcmp (*args, "-to")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: to = *args; sl@0: } sl@0: else sl@0: badarg = 1; sl@0: } sl@0: else if (!strcmp (*args, "-from")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: from = *args; sl@0: } sl@0: else badarg = 1; sl@0: } sl@0: else if (!strcmp (*args, "-subject")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: subject = *args; sl@0: } sl@0: else sl@0: badarg = 1; sl@0: } sl@0: else if (!strcmp (*args, "-signer")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: signerfile = *args; sl@0: } sl@0: else sl@0: badarg = 1; sl@0: } sl@0: else if (!strcmp (*args, "-recip")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: recipfile = *args; sl@0: } sl@0: else badarg = 1; sl@0: } sl@0: else if (!strcmp (*args, "-inkey")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: keyfile = *args; sl@0: } sl@0: else sl@0: badarg = 1; sl@0: } sl@0: else if (!strcmp (*args, "-keyform")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: keyform = str2fmt(*args); sl@0: } sl@0: else sl@0: badarg = 1; sl@0: } sl@0: else if (!strcmp (*args, "-certfile")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: certfile = *args; sl@0: } sl@0: else sl@0: badarg = 1; sl@0: } sl@0: else if (!strcmp (*args, "-CAfile")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: CAfile = *args; sl@0: } sl@0: else sl@0: badarg = 1; sl@0: } sl@0: else if (!strcmp (*args, "-CApath")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: CApath = *args; sl@0: } sl@0: else sl@0: badarg = 1; sl@0: } sl@0: else if (!strcmp (*args, "-in")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: infile = *args; sl@0: } sl@0: else sl@0: badarg = 1; sl@0: } sl@0: else if (!strcmp (*args, "-inform")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: informat = str2fmt(*args); sl@0: } sl@0: else sl@0: badarg = 1; sl@0: } sl@0: else if (!strcmp (*args, "-outform")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: outformat = str2fmt(*args); sl@0: } sl@0: else sl@0: badarg = 1; sl@0: } sl@0: else if (!strcmp (*args, "-out")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: outfile = *args; sl@0: } sl@0: else sl@0: badarg = 1; sl@0: } sl@0: else if (!strcmp (*args, "-content")) sl@0: { sl@0: if (args[1]) sl@0: { sl@0: args++; sl@0: contfile = *args; sl@0: } sl@0: else sl@0: badarg = 1; sl@0: } sl@0: else if (args_verify(&args, NULL, &badarg, bio_err, &vpm)) sl@0: continue; sl@0: else sl@0: badarg = 1; sl@0: args++; sl@0: } sl@0: sl@0: sl@0: if (operation == SMIME_SIGN) sl@0: { sl@0: if (!signerfile) sl@0: { sl@0: BIO_printf(bio_err, "No signer certificate specified\n"); sl@0: badarg = 1; sl@0: } sl@0: need_rand = 1; sl@0: } sl@0: else if (operation == SMIME_DECRYPT) sl@0: { sl@0: if (!recipfile && !keyfile) sl@0: { sl@0: BIO_printf(bio_err, "No recipient certificate or key specified\n"); sl@0: badarg = 1; sl@0: } sl@0: } sl@0: else if (operation == SMIME_ENCRYPT) sl@0: { sl@0: if (!*args) sl@0: { sl@0: BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n"); sl@0: badarg = 1; sl@0: } sl@0: need_rand = 1; sl@0: } sl@0: else if (!operation) sl@0: badarg = 1; sl@0: sl@0: if (badarg) sl@0: { sl@0: BIO_printf (bio_err, "Usage smime [options] cert.pem ...\n"); sl@0: BIO_printf (bio_err, "where options are\n"); sl@0: BIO_printf (bio_err, "-encrypt encrypt message\n"); sl@0: BIO_printf (bio_err, "-decrypt decrypt encrypted message\n"); sl@0: BIO_printf (bio_err, "-sign sign message\n"); sl@0: BIO_printf (bio_err, "-verify verify signed message\n"); sl@0: BIO_printf (bio_err, "-pk7out output PKCS#7 structure\n"); sl@0: #ifndef OPENSSL_NO_DES sl@0: BIO_printf (bio_err, "-des3 encrypt with triple DES\n"); sl@0: BIO_printf (bio_err, "-des encrypt with DES\n"); sl@0: #endif sl@0: #ifndef OPENSSL_NO_RC2 sl@0: BIO_printf (bio_err, "-rc2-40 encrypt with RC2-40 (default)\n"); sl@0: BIO_printf (bio_err, "-rc2-64 encrypt with RC2-64\n"); sl@0: BIO_printf (bio_err, "-rc2-128 encrypt with RC2-128\n"); sl@0: #endif sl@0: #ifndef OPENSSL_NO_AES sl@0: BIO_printf (bio_err, "-aes128, -aes192, -aes256\n"); sl@0: BIO_printf (bio_err, " encrypt PEM output with cbc aes\n"); sl@0: #endif sl@0: BIO_printf (bio_err, "-nointern don't search certificates in message for signer\n"); sl@0: BIO_printf (bio_err, "-nosigs don't verify message signature\n"); sl@0: BIO_printf (bio_err, "-noverify don't verify signers certificate\n"); sl@0: BIO_printf (bio_err, "-nocerts don't include signers certificate when signing\n"); sl@0: BIO_printf (bio_err, "-nodetach use opaque signing\n"); sl@0: BIO_printf (bio_err, "-noattr don't include any signed attributes\n"); sl@0: BIO_printf (bio_err, "-binary don't translate message to text\n"); sl@0: BIO_printf (bio_err, "-certfile file other certificates file\n"); sl@0: BIO_printf (bio_err, "-signer file signer certificate file\n"); sl@0: BIO_printf (bio_err, "-recip file recipient certificate file for decryption\n"); sl@0: BIO_printf (bio_err, "-in file input file\n"); sl@0: BIO_printf (bio_err, "-inform arg input format SMIME (default), PEM or DER\n"); sl@0: BIO_printf (bio_err, "-inkey file input private key (if not signer or recipient)\n"); sl@0: BIO_printf (bio_err, "-keyform arg input private key format (PEM or ENGINE)\n"); sl@0: BIO_printf (bio_err, "-out file output file\n"); sl@0: BIO_printf (bio_err, "-outform arg output format SMIME (default), PEM or DER\n"); sl@0: BIO_printf (bio_err, "-content file supply or override content for detached signature\n"); sl@0: BIO_printf (bio_err, "-to addr to address\n"); sl@0: BIO_printf (bio_err, "-from ad from address\n"); sl@0: BIO_printf (bio_err, "-subject s subject\n"); sl@0: BIO_printf (bio_err, "-text include or delete text MIME headers\n"); sl@0: BIO_printf (bio_err, "-CApath dir trusted certificates directory\n"); sl@0: BIO_printf (bio_err, "-CAfile file trusted certificates file\n"); sl@0: BIO_printf (bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n"); sl@0: BIO_printf (bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n"); sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: BIO_printf (bio_err, "-engine e use engine e, possibly a hardware device.\n"); sl@0: #endif sl@0: BIO_printf (bio_err, "-passin arg input file pass phrase source\n"); sl@0: BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); sl@0: BIO_printf(bio_err, " load the file (or the files in the directory) into\n"); sl@0: BIO_printf(bio_err, " the random number generator\n"); sl@0: BIO_printf (bio_err, "cert.pem recipient certificate(s) for encryption\n"); sl@0: goto end; sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: e = setup_engine(bio_err, engine, 0); sl@0: #endif sl@0: sl@0: if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) sl@0: { sl@0: BIO_printf(bio_err, "Error getting password\n"); sl@0: goto end; sl@0: } sl@0: sl@0: if (need_rand) sl@0: { sl@0: app_RAND_load_file(NULL, bio_err, (inrand != NULL)); sl@0: if (inrand != NULL) sl@0: BIO_printf(bio_err,"%ld semi-random bytes loaded\n", sl@0: app_RAND_load_files(inrand)); sl@0: } sl@0: sl@0: ret = 2; sl@0: sl@0: if (operation != SMIME_SIGN) sl@0: flags &= ~PKCS7_DETACHED; sl@0: sl@0: if (operation & SMIME_OP) sl@0: { sl@0: if (flags & PKCS7_BINARY) sl@0: inmode = "rb"; sl@0: if (outformat == FORMAT_ASN1) sl@0: outmode = "wb"; sl@0: } sl@0: else sl@0: { sl@0: if (flags & PKCS7_BINARY) sl@0: outmode = "wb"; sl@0: if (informat == FORMAT_ASN1) sl@0: inmode = "rb"; sl@0: } sl@0: sl@0: if (operation == SMIME_ENCRYPT) sl@0: { sl@0: if (!cipher) sl@0: { sl@0: #ifndef OPENSSL_NO_RC2 sl@0: cipher = EVP_rc2_40_cbc(); sl@0: #else sl@0: BIO_printf(bio_err, "No cipher selected\n"); sl@0: goto end; sl@0: #endif sl@0: } sl@0: encerts = sk_X509_new_null(); sl@0: while (*args) sl@0: { sl@0: if (!(cert = load_cert(bio_err,*args,FORMAT_PEM, sl@0: NULL, e, "recipient certificate file"))) sl@0: { sl@0: #if 0 /* An appropriate message is already printed */ sl@0: BIO_printf(bio_err, "Can't read recipient certificate file %s\n", *args); sl@0: #endif sl@0: goto end; sl@0: } sl@0: sk_X509_push(encerts, cert); sl@0: cert = NULL; sl@0: args++; sl@0: } sl@0: } sl@0: sl@0: if (signerfile && (operation == SMIME_SIGN)) sl@0: { sl@0: if (!(signer = load_cert(bio_err,signerfile,FORMAT_PEM, NULL, sl@0: e, "signer certificate"))) sl@0: { sl@0: #if 0 /* An appropri message has already been printed */ sl@0: BIO_printf(bio_err, "Can't read signer certificate file %s\n", signerfile); sl@0: #endif sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: if (certfile) sl@0: { sl@0: if (!(other = load_certs(bio_err,certfile,FORMAT_PEM, NULL, sl@0: e, "certificate file"))) sl@0: { sl@0: #if 0 /* An appropriate message has already been printed */ sl@0: BIO_printf(bio_err, "Can't read certificate file %s\n", certfile); sl@0: #endif sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: if (recipfile && (operation == SMIME_DECRYPT)) sl@0: { sl@0: if (!(recip = load_cert(bio_err,recipfile,FORMAT_PEM,NULL, sl@0: e, "recipient certificate file"))) sl@0: { sl@0: #if 0 /* An appropriate message has alrady been printed */ sl@0: BIO_printf(bio_err, "Can't read recipient certificate file %s\n", recipfile); sl@0: #endif sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: if (operation == SMIME_DECRYPT) sl@0: { sl@0: if (!keyfile) sl@0: keyfile = recipfile; sl@0: } sl@0: else if (operation == SMIME_SIGN) sl@0: { sl@0: if (!keyfile) sl@0: keyfile = signerfile; sl@0: } sl@0: else keyfile = NULL; sl@0: sl@0: if (keyfile) sl@0: { sl@0: key = load_key(bio_err, keyfile, keyform, 0, passin, e, sl@0: "signing key file"); sl@0: if (!key) sl@0: goto end; sl@0: } sl@0: sl@0: if (infile) sl@0: { sl@0: if (!(in = BIO_new_file(infile, inmode))) sl@0: { sl@0: BIO_printf (bio_err, sl@0: "Can't open input file %s\n", infile); sl@0: goto end; sl@0: } sl@0: } sl@0: else sl@0: in = BIO_new_fp(stdin, BIO_NOCLOSE); sl@0: sl@0: sl@0: if (outfile) sl@0: { sl@0: if (!(out = BIO_new_file(outfile, outmode))) sl@0: { sl@0: BIO_printf (bio_err, sl@0: "Can't open output file %s\n", outfile); sl@0: goto end; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: out = BIO_new_fp(stdout, BIO_NOCLOSE); sl@0: sl@0: #ifdef OPENSSL_SYS_VMS sl@0: { sl@0: BIO *tmpbio = BIO_new(BIO_f_linebuffer()); sl@0: out = BIO_push(tmpbio, out); sl@0: } sl@0: #endif sl@0: } sl@0: sl@0: if (operation == SMIME_VERIFY) sl@0: { sl@0: if (!(store = setup_verify(bio_err, CAfile, CApath))) sl@0: goto end; sl@0: X509_STORE_set_verify_cb_func(store, smime_cb); sl@0: if (vpm) sl@0: X509_STORE_set1_param(store, vpm); sl@0: } sl@0: sl@0: sl@0: ret = 3; sl@0: sl@0: if (operation == SMIME_ENCRYPT) sl@0: p7 = PKCS7_encrypt(encerts, in, cipher, flags); sl@0: else if (operation == SMIME_SIGN) sl@0: { sl@0: /* If detached data and SMIME output enable partial sl@0: * signing. sl@0: */ sl@0: if ((flags & PKCS7_DETACHED) && (outformat == FORMAT_SMIME)) sl@0: flags |= PKCS7_STREAM; sl@0: p7 = PKCS7_sign(signer, key, other, in, flags); sl@0: } sl@0: else sl@0: { sl@0: if (informat == FORMAT_SMIME) sl@0: p7 = SMIME_read_PKCS7(in, &indata); sl@0: else if (informat == FORMAT_PEM) sl@0: p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL); sl@0: else if (informat == FORMAT_ASN1) sl@0: p7 = d2i_PKCS7_bio(in, NULL); sl@0: else sl@0: { sl@0: BIO_printf(bio_err, "Bad input format for PKCS#7 file\n"); sl@0: goto end; sl@0: } sl@0: sl@0: if (!p7) sl@0: { sl@0: BIO_printf(bio_err, "Error reading S/MIME message\n"); sl@0: goto end; sl@0: } sl@0: if (contfile) sl@0: { sl@0: BIO_free(indata); sl@0: if (!(indata = BIO_new_file(contfile, "rb"))) sl@0: { sl@0: BIO_printf(bio_err, "Can't read content file %s\n", contfile); sl@0: goto end; sl@0: } sl@0: } sl@0: } sl@0: sl@0: if (!p7) sl@0: { sl@0: BIO_printf(bio_err, "Error creating PKCS#7 structure\n"); sl@0: goto end; sl@0: } sl@0: sl@0: ret = 4; sl@0: if (operation == SMIME_DECRYPT) sl@0: { sl@0: if (!PKCS7_decrypt(p7, key, recip, out, flags)) sl@0: { sl@0: BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n"); sl@0: goto end; sl@0: } sl@0: } sl@0: else if (operation == SMIME_VERIFY) sl@0: { sl@0: STACK_OF(X509) *signers; sl@0: if (PKCS7_verify(p7, other, store, indata, out, flags)) sl@0: BIO_printf(bio_err, "Verification successful\n"); sl@0: else sl@0: { sl@0: BIO_printf(bio_err, "Verification failure\n"); sl@0: goto end; sl@0: } sl@0: signers = PKCS7_get0_signers(p7, other, flags); sl@0: if (!save_certs(signerfile, signers)) sl@0: { sl@0: BIO_printf(bio_err, "Error writing signers to %s\n", sl@0: signerfile); sl@0: ret = 5; sl@0: goto end; sl@0: } sl@0: sk_X509_free(signers); sl@0: } sl@0: else if (operation == SMIME_PK7OUT) sl@0: PEM_write_bio_PKCS7(out, p7); sl@0: else sl@0: { sl@0: if (to) sl@0: BIO_printf(out, "To: %s\n", to); sl@0: if (from) sl@0: BIO_printf(out, "From: %s\n", from); sl@0: if (subject) sl@0: BIO_printf(out, "Subject: %s\n", subject); sl@0: if (outformat == FORMAT_SMIME) sl@0: SMIME_write_PKCS7(out, p7, in, flags); sl@0: else if (outformat == FORMAT_PEM) sl@0: PEM_write_bio_PKCS7(out,p7); sl@0: else if (outformat == FORMAT_ASN1) sl@0: i2d_PKCS7_bio(out,p7); sl@0: else sl@0: { sl@0: BIO_printf(bio_err, "Bad output format for PKCS#7 file\n"); sl@0: goto end; sl@0: } sl@0: } sl@0: ret = 0; sl@0: end: sl@0: if (need_rand) sl@0: app_RAND_write_file(NULL, bio_err); sl@0: if (ret) ERR_print_errors(bio_err); sl@0: sk_X509_pop_free(encerts, X509_free); sl@0: sk_X509_pop_free(other, X509_free); sl@0: if (vpm) sl@0: X509_VERIFY_PARAM_free(vpm); sl@0: X509_STORE_free(store); sl@0: X509_free(cert); sl@0: X509_free(recip); sl@0: X509_free(signer); sl@0: EVP_PKEY_free(key); sl@0: PKCS7_free(p7); sl@0: BIO_free(in); sl@0: BIO_free(indata); sl@0: BIO_free_all(out); sl@0: if (passin) OPENSSL_free(passin); sl@0: return (ret); sl@0: } sl@0: sl@0: static int save_certs(char *signerfile, STACK_OF(X509) *signers) sl@0: { sl@0: int i; sl@0: BIO *tmp; sl@0: if (!signerfile) sl@0: return 1; sl@0: tmp = BIO_new_file(signerfile, "w"); sl@0: if (!tmp) return 0; sl@0: for(i = 0; i < sk_X509_num(signers); i++) sl@0: PEM_write_bio_X509(tmp, sk_X509_value(signers, i)); sl@0: BIO_free(tmp); sl@0: return 1; sl@0: } sl@0: sl@0: sl@0: /* Minimal callback just to output policy info (if any) */ sl@0: sl@0: static int smime_cb(int ok, X509_STORE_CTX *ctx) sl@0: { sl@0: int error; sl@0: sl@0: error = X509_STORE_CTX_get_error(ctx); sl@0: sl@0: if ((error != X509_V_ERR_NO_EXPLICIT_POLICY) sl@0: && ((error != X509_V_OK) || (ok != 2))) sl@0: return ok; sl@0: sl@0: policies_print(NULL, ctx); sl@0: sl@0: return ok; sl@0: sl@0: }