os/ossrv/ssl/tsrc/BC/libcrypto/topenssl/src/smime.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/tsrc/BC/libcrypto/topenssl/src/smime.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,792 @@
     1.4 +/* smime.c */
     1.5 +/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
     1.6 + * project.
     1.7 + */
     1.8 +/* ====================================================================
     1.9 + * Copyright (c) 1999-2004 The OpenSSL Project.  All rights reserved.
    1.10 + *
    1.11 + * Redistribution and use in source and binary forms, with or without
    1.12 + * modification, are permitted provided that the following conditions
    1.13 + * are met:
    1.14 + *
    1.15 + * 1. Redistributions of source code must retain the above copyright
    1.16 + *    notice, this list of conditions and the following disclaimer. 
    1.17 + *
    1.18 + * 2. Redistributions in binary form must reproduce the above copyright
    1.19 + *    notice, this list of conditions and the following disclaimer in
    1.20 + *    the documentation and/or other materials provided with the
    1.21 + *    distribution.
    1.22 + *
    1.23 + * 3. All advertising materials mentioning features or use of this
    1.24 + *    software must display the following acknowledgment:
    1.25 + *    "This product includes software developed by the OpenSSL Project
    1.26 + *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
    1.27 + *
    1.28 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
    1.29 + *    endorse or promote products derived from this software without
    1.30 + *    prior written permission. For written permission, please contact
    1.31 + *    licensing@OpenSSL.org.
    1.32 + *
    1.33 + * 5. Products derived from this software may not be called "OpenSSL"
    1.34 + *    nor may "OpenSSL" appear in their names without prior written
    1.35 + *    permission of the OpenSSL Project.
    1.36 + *
    1.37 + * 6. Redistributions of any form whatsoever must retain the following
    1.38 + *    acknowledgment:
    1.39 + *    "This product includes software developed by the OpenSSL Project
    1.40 + *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
    1.41 + *
    1.42 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
    1.43 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.44 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.45 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
    1.46 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.47 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.48 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    1.49 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.50 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.51 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.52 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    1.53 + * OF THE POSSIBILITY OF SUCH DAMAGE.
    1.54 + * ====================================================================
    1.55 + *
    1.56 + * This product includes cryptographic software written by Eric Young
    1.57 + * (eay@cryptsoft.com).  This product includes software written by Tim
    1.58 + * Hudson (tjh@cryptsoft.com).
    1.59 + *
    1.60 + */
    1.61 +
    1.62 +/* S/MIME utility function */
    1.63 +
    1.64 +#include <stdio.h>
    1.65 +#include <string.h>
    1.66 +#include "apps.h"
    1.67 +#include <openssl/crypto.h>
    1.68 +#include <openssl/pem.h>
    1.69 +#include <openssl/err.h>
    1.70 +#include <openssl/x509_vfy.h>
    1.71 +#include <openssl/x509v3.h>
    1.72 +
    1.73 +#undef PROG
    1.74 +#define PROG smime_main
    1.75 +static int save_certs(char *signerfile, STACK_OF(X509) *signers);
    1.76 +static int smime_cb(int ok, X509_STORE_CTX *ctx);
    1.77 +
    1.78 +#define SMIME_OP	0x10
    1.79 +#define SMIME_ENCRYPT	(1 | SMIME_OP)
    1.80 +#define SMIME_DECRYPT	2
    1.81 +#define SMIME_SIGN	(3 | SMIME_OP)
    1.82 +#define SMIME_VERIFY	4
    1.83 +#define SMIME_PK7OUT	5
    1.84 +
    1.85 +
    1.86 +int MAIN(int, char **);
    1.87 +
    1.88 +int MAIN(int argc, char **argv)
    1.89 +	{
    1.90 +	ENGINE *e = NULL;
    1.91 +	int operation = 0;
    1.92 +	int ret = 0;
    1.93 +	char **args;
    1.94 +	const char *inmode = "r", *outmode = "w";
    1.95 +	char *infile = NULL, *outfile = NULL;
    1.96 +	char *signerfile = NULL, *recipfile = NULL;
    1.97 +	char *certfile = NULL, *keyfile = NULL, *contfile=NULL;
    1.98 +	const EVP_CIPHER *cipher = NULL;
    1.99 +	PKCS7 *p7 = NULL;
   1.100 +	X509_STORE *store = NULL;
   1.101 +	X509 *cert = NULL, *recip = NULL, *signer = NULL;
   1.102 +	EVP_PKEY *key = NULL;
   1.103 +	STACK_OF(X509) *encerts = NULL, *other = NULL;
   1.104 +	BIO *in = NULL, *out = NULL, *indata = NULL;
   1.105 +	int badarg = 0;
   1.106 +	int flags = PKCS7_DETACHED;
   1.107 +	char *to = NULL, *from = NULL, *subject = NULL;
   1.108 +	char *CAfile = NULL, *CApath = NULL;
   1.109 +	char *passargin = NULL, *passin = NULL;
   1.110 +	char *inrand = NULL;
   1.111 +	int need_rand = 0;
   1.112 +	int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
   1.113 +        int keyform = FORMAT_PEM;
   1.114 +#ifndef OPENSSL_NO_ENGINE
   1.115 +	char *engine=NULL;
   1.116 +#endif
   1.117 +
   1.118 +	X509_VERIFY_PARAM *vpm = NULL;
   1.119 +
   1.120 +	args = argv + 1;
   1.121 +	ret = 1;
   1.122 +
   1.123 +	apps_startup();
   1.124 +
   1.125 +	if (bio_err == NULL)
   1.126 +		{
   1.127 +		if ((bio_err = BIO_new(BIO_s_file())) != NULL)
   1.128 +			BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
   1.129 +
   1.130 +		}
   1.131 +
   1.132 +	if (!load_config(bio_err, NULL))
   1.133 +		goto end;
   1.134 +
   1.135 +	while (!badarg && *args && *args[0] == '-')
   1.136 +		{
   1.137 +		if (!strcmp (*args, "-encrypt"))
   1.138 +			operation = SMIME_ENCRYPT;
   1.139 +		else if (!strcmp (*args, "-decrypt"))
   1.140 +			operation = SMIME_DECRYPT;
   1.141 +		else if (!strcmp (*args, "-sign"))
   1.142 +			operation = SMIME_SIGN;
   1.143 +		else if (!strcmp (*args, "-verify"))
   1.144 +			operation = SMIME_VERIFY;
   1.145 +		else if (!strcmp (*args, "-pk7out"))
   1.146 +			operation = SMIME_PK7OUT;
   1.147 +#ifndef OPENSSL_NO_DES
   1.148 +		else if (!strcmp (*args, "-des3")) 
   1.149 +				cipher = EVP_des_ede3_cbc();
   1.150 +		else if (!strcmp (*args, "-des")) 
   1.151 +				cipher = EVP_des_cbc();
   1.152 +#endif
   1.153 +#ifndef OPENSSL_NO_RC2
   1.154 +		else if (!strcmp (*args, "-rc2-40")) 
   1.155 +				cipher = EVP_rc2_40_cbc();
   1.156 +		else if (!strcmp (*args, "-rc2-128")) 
   1.157 +				cipher = EVP_rc2_cbc();
   1.158 +		else if (!strcmp (*args, "-rc2-64")) 
   1.159 +				cipher = EVP_rc2_64_cbc();
   1.160 +#endif
   1.161 +#ifndef OPENSSL_NO_AES
   1.162 +		else if (!strcmp(*args,"-aes128"))
   1.163 +				cipher = EVP_aes_128_cbc();
   1.164 +		else if (!strcmp(*args,"-aes192"))
   1.165 +				cipher = EVP_aes_192_cbc();
   1.166 +		else if (!strcmp(*args,"-aes256"))
   1.167 +				cipher = EVP_aes_256_cbc();
   1.168 +#endif
   1.169 +		else if (!strcmp (*args, "-text")) 
   1.170 +				flags |= PKCS7_TEXT;
   1.171 +		else if (!strcmp (*args, "-nointern")) 
   1.172 +				flags |= PKCS7_NOINTERN;
   1.173 +		else if (!strcmp (*args, "-noverify")) 
   1.174 +				flags |= PKCS7_NOVERIFY;
   1.175 +		else if (!strcmp (*args, "-nochain")) 
   1.176 +				flags |= PKCS7_NOCHAIN;
   1.177 +		else if (!strcmp (*args, "-nocerts")) 
   1.178 +				flags |= PKCS7_NOCERTS;
   1.179 +		else if (!strcmp (*args, "-noattr")) 
   1.180 +				flags |= PKCS7_NOATTR;
   1.181 +		else if (!strcmp (*args, "-nodetach")) 
   1.182 +				flags &= ~PKCS7_DETACHED;
   1.183 +		else if (!strcmp (*args, "-nosmimecap"))
   1.184 +				flags |= PKCS7_NOSMIMECAP;
   1.185 +		else if (!strcmp (*args, "-binary"))
   1.186 +				flags |= PKCS7_BINARY;
   1.187 +		else if (!strcmp (*args, "-nosigs"))
   1.188 +				flags |= PKCS7_NOSIGS;
   1.189 +		else if (!strcmp (*args, "-nooldmime"))
   1.190 +				flags |= PKCS7_NOOLDMIMETYPE;
   1.191 +		else if (!strcmp (*args, "-crlfeol"))
   1.192 +				flags |= PKCS7_CRLFEOL;
   1.193 +		else if (!strcmp(*args,"-rand"))
   1.194 +			{
   1.195 +			if (args[1])
   1.196 +				{
   1.197 +				args++;
   1.198 +				inrand = *args;
   1.199 +				}
   1.200 +			else
   1.201 +				badarg = 1;
   1.202 +			need_rand = 1;
   1.203 +			}
   1.204 +#ifndef OPENSSL_NO_ENGINE
   1.205 +		else if (!strcmp(*args,"-engine"))
   1.206 +			{
   1.207 +			if (args[1])
   1.208 +				{
   1.209 +				args++;
   1.210 +				engine = *args;
   1.211 +				}
   1.212 +			else badarg = 1;
   1.213 +			}
   1.214 +#endif
   1.215 +		else if (!strcmp(*args,"-passin"))
   1.216 +			{
   1.217 +			if (args[1])
   1.218 +				{
   1.219 +				args++;
   1.220 +				passargin = *args;
   1.221 +				}
   1.222 +			else
   1.223 +				badarg = 1;
   1.224 +			}
   1.225 +		else if (!strcmp (*args, "-to"))
   1.226 +			{
   1.227 +			if (args[1])
   1.228 +				{
   1.229 +				args++;
   1.230 +				to = *args;
   1.231 +				}
   1.232 +			else
   1.233 +				badarg = 1;
   1.234 +			}
   1.235 +		else if (!strcmp (*args, "-from"))
   1.236 +			{
   1.237 +			if (args[1])
   1.238 +				{
   1.239 +				args++;
   1.240 +				from = *args;
   1.241 +				}
   1.242 +			else badarg = 1;
   1.243 +			}
   1.244 +		else if (!strcmp (*args, "-subject"))
   1.245 +			{
   1.246 +			if (args[1])
   1.247 +				{
   1.248 +				args++;
   1.249 +				subject = *args;
   1.250 +				}
   1.251 +			else
   1.252 +				badarg = 1;
   1.253 +			}
   1.254 +		else if (!strcmp (*args, "-signer"))
   1.255 +			{
   1.256 +			if (args[1])
   1.257 +				{
   1.258 +				args++;
   1.259 +				signerfile = *args;
   1.260 +				}
   1.261 +			else
   1.262 +				badarg = 1;
   1.263 +			}
   1.264 +		else if (!strcmp (*args, "-recip"))
   1.265 +			{
   1.266 +			if (args[1])
   1.267 +				{
   1.268 +				args++;
   1.269 +				recipfile = *args;
   1.270 +				}
   1.271 +			else badarg = 1;
   1.272 +			}
   1.273 +		else if (!strcmp (*args, "-inkey"))
   1.274 +			{
   1.275 +			if (args[1])
   1.276 +				{
   1.277 +				args++;
   1.278 +				keyfile = *args;
   1.279 +				}
   1.280 +			else
   1.281 +				badarg = 1;
   1.282 +		}
   1.283 +		else if (!strcmp (*args, "-keyform"))
   1.284 +			{
   1.285 +			if (args[1])
   1.286 +				{
   1.287 +				args++;
   1.288 +				keyform = str2fmt(*args);
   1.289 +				}
   1.290 +			else
   1.291 +				badarg = 1;
   1.292 +			}
   1.293 +		else if (!strcmp (*args, "-certfile"))
   1.294 +			{
   1.295 +			if (args[1])
   1.296 +				{
   1.297 +				args++;
   1.298 +				certfile = *args;
   1.299 +				}
   1.300 +			else
   1.301 +				badarg = 1;
   1.302 +			}
   1.303 +		else if (!strcmp (*args, "-CAfile"))
   1.304 +			{
   1.305 +			if (args[1])
   1.306 +				{
   1.307 +				args++;
   1.308 +				CAfile = *args;
   1.309 +				}
   1.310 +			else
   1.311 +				badarg = 1;
   1.312 +			}
   1.313 +		else if (!strcmp (*args, "-CApath"))
   1.314 +			{
   1.315 +			if (args[1])
   1.316 +				{
   1.317 +				args++;
   1.318 +				CApath = *args;
   1.319 +				}
   1.320 +			else
   1.321 +				badarg = 1;
   1.322 +			}
   1.323 +		else if (!strcmp (*args, "-in"))
   1.324 +			{
   1.325 +			if (args[1])
   1.326 +				{
   1.327 +				args++;
   1.328 +				infile = *args;
   1.329 +				}
   1.330 +			else
   1.331 +				badarg = 1;
   1.332 +			}
   1.333 +		else if (!strcmp (*args, "-inform"))
   1.334 +			{
   1.335 +			if (args[1])
   1.336 +				{
   1.337 +				args++;
   1.338 +				informat = str2fmt(*args);
   1.339 +				}
   1.340 +			else
   1.341 +				badarg = 1;
   1.342 +			}
   1.343 +		else if (!strcmp (*args, "-outform"))
   1.344 +			{
   1.345 +			if (args[1])
   1.346 +				{
   1.347 +				args++;
   1.348 +				outformat = str2fmt(*args);
   1.349 +				}
   1.350 +			else
   1.351 +				badarg = 1;
   1.352 +			}
   1.353 +		else if (!strcmp (*args, "-out"))
   1.354 +			{
   1.355 +			if (args[1])
   1.356 +				{
   1.357 +				args++;
   1.358 +				outfile = *args;
   1.359 +				}
   1.360 +			else
   1.361 +				badarg = 1;
   1.362 +			}
   1.363 +		else if (!strcmp (*args, "-content"))
   1.364 +			{
   1.365 +			if (args[1])
   1.366 +				{
   1.367 +				args++;
   1.368 +				contfile = *args;
   1.369 +				}
   1.370 +			else
   1.371 +				badarg = 1;
   1.372 +			}
   1.373 +		else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
   1.374 +			continue;
   1.375 +		else
   1.376 +			badarg = 1;
   1.377 +		args++;
   1.378 +		}
   1.379 +
   1.380 +
   1.381 +	if (operation == SMIME_SIGN)
   1.382 +		{
   1.383 +		if (!signerfile)
   1.384 +			{
   1.385 +			BIO_printf(bio_err, "No signer certificate specified\n");
   1.386 +			badarg = 1;
   1.387 +			}
   1.388 +		need_rand = 1;
   1.389 +		}
   1.390 +	else if (operation == SMIME_DECRYPT)
   1.391 +		{
   1.392 +		if (!recipfile && !keyfile)
   1.393 +			{
   1.394 +			BIO_printf(bio_err, "No recipient certificate or key specified\n");
   1.395 +			badarg = 1;
   1.396 +			}
   1.397 +		}
   1.398 +	else if (operation == SMIME_ENCRYPT)
   1.399 +		{
   1.400 +		if (!*args)
   1.401 +			{
   1.402 +			BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
   1.403 +			badarg = 1;
   1.404 +			}
   1.405 +		need_rand = 1;
   1.406 +		}
   1.407 +	else if (!operation)
   1.408 +		badarg = 1;
   1.409 +
   1.410 +	if (badarg)
   1.411 +		{
   1.412 +		BIO_printf (bio_err, "Usage smime [options] cert.pem ...\n");
   1.413 +		BIO_printf (bio_err, "where options are\n");
   1.414 +		BIO_printf (bio_err, "-encrypt       encrypt message\n");
   1.415 +		BIO_printf (bio_err, "-decrypt       decrypt encrypted message\n");
   1.416 +		BIO_printf (bio_err, "-sign          sign message\n");
   1.417 +		BIO_printf (bio_err, "-verify        verify signed message\n");
   1.418 +		BIO_printf (bio_err, "-pk7out        output PKCS#7 structure\n");
   1.419 +#ifndef OPENSSL_NO_DES
   1.420 +		BIO_printf (bio_err, "-des3          encrypt with triple DES\n");
   1.421 +		BIO_printf (bio_err, "-des           encrypt with DES\n");
   1.422 +#endif
   1.423 +#ifndef OPENSSL_NO_RC2
   1.424 +		BIO_printf (bio_err, "-rc2-40        encrypt with RC2-40 (default)\n");
   1.425 +		BIO_printf (bio_err, "-rc2-64        encrypt with RC2-64\n");
   1.426 +		BIO_printf (bio_err, "-rc2-128       encrypt with RC2-128\n");
   1.427 +#endif
   1.428 +#ifndef OPENSSL_NO_AES
   1.429 +		BIO_printf (bio_err, "-aes128, -aes192, -aes256\n");
   1.430 +		BIO_printf (bio_err, "               encrypt PEM output with cbc aes\n");
   1.431 +#endif
   1.432 +		BIO_printf (bio_err, "-nointern      don't search certificates in message for signer\n");
   1.433 +		BIO_printf (bio_err, "-nosigs        don't verify message signature\n");
   1.434 +		BIO_printf (bio_err, "-noverify      don't verify signers certificate\n");
   1.435 +		BIO_printf (bio_err, "-nocerts       don't include signers certificate when signing\n");
   1.436 +		BIO_printf (bio_err, "-nodetach      use opaque signing\n");
   1.437 +		BIO_printf (bio_err, "-noattr        don't include any signed attributes\n");
   1.438 +		BIO_printf (bio_err, "-binary        don't translate message to text\n");
   1.439 +		BIO_printf (bio_err, "-certfile file other certificates file\n");
   1.440 +		BIO_printf (bio_err, "-signer file   signer certificate file\n");
   1.441 +		BIO_printf (bio_err, "-recip  file   recipient certificate file for decryption\n");
   1.442 +		BIO_printf (bio_err, "-in file       input file\n");
   1.443 +		BIO_printf (bio_err, "-inform arg    input format SMIME (default), PEM or DER\n");
   1.444 +		BIO_printf (bio_err, "-inkey file    input private key (if not signer or recipient)\n");
   1.445 +		BIO_printf (bio_err, "-keyform arg   input private key format (PEM or ENGINE)\n");
   1.446 +		BIO_printf (bio_err, "-out file      output file\n");
   1.447 +		BIO_printf (bio_err, "-outform arg   output format SMIME (default), PEM or DER\n");
   1.448 +		BIO_printf (bio_err, "-content file  supply or override content for detached signature\n");
   1.449 +		BIO_printf (bio_err, "-to addr       to address\n");
   1.450 +		BIO_printf (bio_err, "-from ad       from address\n");
   1.451 +		BIO_printf (bio_err, "-subject s     subject\n");
   1.452 +		BIO_printf (bio_err, "-text          include or delete text MIME headers\n");
   1.453 +		BIO_printf (bio_err, "-CApath dir    trusted certificates directory\n");
   1.454 +		BIO_printf (bio_err, "-CAfile file   trusted certificates file\n");
   1.455 +		BIO_printf (bio_err, "-crl_check     check revocation status of signer's certificate using CRLs\n");
   1.456 +		BIO_printf (bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
   1.457 +#ifndef OPENSSL_NO_ENGINE
   1.458 +		BIO_printf (bio_err, "-engine e      use engine e, possibly a hardware device.\n");
   1.459 +#endif
   1.460 +		BIO_printf (bio_err, "-passin arg    input file pass phrase source\n");
   1.461 +		BIO_printf(bio_err,  "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
   1.462 +		BIO_printf(bio_err,  "               load the file (or the files in the directory) into\n");
   1.463 +		BIO_printf(bio_err,  "               the random number generator\n");
   1.464 +		BIO_printf (bio_err, "cert.pem       recipient certificate(s) for encryption\n");
   1.465 +		goto end;
   1.466 +		}
   1.467 +
   1.468 +#ifndef OPENSSL_NO_ENGINE
   1.469 +        e = setup_engine(bio_err, engine, 0);
   1.470 +#endif
   1.471 +
   1.472 +	if (!app_passwd(bio_err, passargin, NULL, &passin, NULL))
   1.473 +		{
   1.474 +		BIO_printf(bio_err, "Error getting password\n");
   1.475 +		goto end;
   1.476 +		}
   1.477 +
   1.478 +	if (need_rand)
   1.479 +		{
   1.480 +		app_RAND_load_file(NULL, bio_err, (inrand != NULL));
   1.481 +		if (inrand != NULL)
   1.482 +			BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
   1.483 +				app_RAND_load_files(inrand));
   1.484 +		}
   1.485 +
   1.486 +	ret = 2;
   1.487 +
   1.488 +	if (operation != SMIME_SIGN)
   1.489 +		flags &= ~PKCS7_DETACHED;
   1.490 +
   1.491 +	if (operation & SMIME_OP)
   1.492 +		{
   1.493 +		if (flags & PKCS7_BINARY)
   1.494 +			inmode = "rb";
   1.495 +		if (outformat == FORMAT_ASN1)
   1.496 +			outmode = "wb";
   1.497 +		}
   1.498 +	else
   1.499 +		{
   1.500 +		if (flags & PKCS7_BINARY)
   1.501 +			outmode = "wb";
   1.502 +		if (informat == FORMAT_ASN1)
   1.503 +			inmode = "rb";
   1.504 +		}
   1.505 +
   1.506 +	if (operation == SMIME_ENCRYPT)
   1.507 +		{
   1.508 +		if (!cipher)
   1.509 +			{
   1.510 +#ifndef OPENSSL_NO_RC2			
   1.511 +			cipher = EVP_rc2_40_cbc();
   1.512 +#else
   1.513 +			BIO_printf(bio_err, "No cipher selected\n");
   1.514 +			goto end;
   1.515 +#endif
   1.516 +			}
   1.517 +		encerts = sk_X509_new_null();
   1.518 +		while (*args)
   1.519 +			{
   1.520 +			if (!(cert = load_cert(bio_err,*args,FORMAT_PEM,
   1.521 +				NULL, e, "recipient certificate file")))
   1.522 +				{
   1.523 +#if 0				/* An appropriate message is already printed */
   1.524 +				BIO_printf(bio_err, "Can't read recipient certificate file %s\n", *args);
   1.525 +#endif
   1.526 +				goto end;
   1.527 +				}
   1.528 +			sk_X509_push(encerts, cert);
   1.529 +			cert = NULL;
   1.530 +			args++;
   1.531 +			}
   1.532 +		}
   1.533 +
   1.534 +	if (signerfile && (operation == SMIME_SIGN))
   1.535 +		{
   1.536 +		if (!(signer = load_cert(bio_err,signerfile,FORMAT_PEM, NULL,
   1.537 +			e, "signer certificate")))
   1.538 +			{
   1.539 +#if 0			/* An appropri message has already been printed */
   1.540 +			BIO_printf(bio_err, "Can't read signer certificate file %s\n", signerfile);
   1.541 +#endif
   1.542 +			goto end;
   1.543 +			}
   1.544 +		}
   1.545 +
   1.546 +	if (certfile)
   1.547 +		{
   1.548 +		if (!(other = load_certs(bio_err,certfile,FORMAT_PEM, NULL,
   1.549 +			e, "certificate file")))
   1.550 +			{
   1.551 +#if 0			/* An appropriate message has already been printed */
   1.552 +			BIO_printf(bio_err, "Can't read certificate file %s\n", certfile);
   1.553 +#endif
   1.554 +			ERR_print_errors(bio_err);
   1.555 +			goto end;
   1.556 +			}
   1.557 +		}
   1.558 +
   1.559 +	if (recipfile && (operation == SMIME_DECRYPT))
   1.560 +		{
   1.561 +		if (!(recip = load_cert(bio_err,recipfile,FORMAT_PEM,NULL,
   1.562 +			e, "recipient certificate file")))
   1.563 +			{
   1.564 +#if 0			/* An appropriate message has alrady been printed */
   1.565 +			BIO_printf(bio_err, "Can't read recipient certificate file %s\n", recipfile);
   1.566 +#endif
   1.567 +			ERR_print_errors(bio_err);
   1.568 +			goto end;
   1.569 +			}
   1.570 +		}
   1.571 +
   1.572 +	if (operation == SMIME_DECRYPT)
   1.573 +		{
   1.574 +		if (!keyfile)
   1.575 +			keyfile = recipfile;
   1.576 +		}
   1.577 +	else if (operation == SMIME_SIGN)
   1.578 +		{
   1.579 +		if (!keyfile)
   1.580 +			keyfile = signerfile;
   1.581 +		}
   1.582 +	else keyfile = NULL;
   1.583 +
   1.584 +	if (keyfile)
   1.585 +		{
   1.586 +		key = load_key(bio_err, keyfile, keyform, 0, passin, e,
   1.587 +			       "signing key file");
   1.588 +		if (!key)
   1.589 +			goto end;
   1.590 +		}
   1.591 +
   1.592 +	if (infile)
   1.593 +		{
   1.594 +		if (!(in = BIO_new_file(infile, inmode)))
   1.595 +			{
   1.596 +			BIO_printf (bio_err,
   1.597 +				 "Can't open input file %s\n", infile);
   1.598 +			goto end;
   1.599 +			}
   1.600 +		}
   1.601 +	else
   1.602 +		in = BIO_new_fp(stdin, BIO_NOCLOSE);
   1.603 +	
   1.604 +
   1.605 +	if (outfile)
   1.606 +		{
   1.607 +		if (!(out = BIO_new_file(outfile, outmode)))
   1.608 +			{
   1.609 +			BIO_printf (bio_err,
   1.610 +				 "Can't open output file %s\n", outfile);
   1.611 +			goto end;
   1.612 +			}
   1.613 +		}
   1.614 +	else
   1.615 +		{
   1.616 +		out = BIO_new_fp(stdout, BIO_NOCLOSE);
   1.617 +
   1.618 +#ifdef OPENSSL_SYS_VMS
   1.619 +		{
   1.620 +		    BIO *tmpbio = BIO_new(BIO_f_linebuffer());
   1.621 +		    out = BIO_push(tmpbio, out);
   1.622 +		}
   1.623 +#endif
   1.624 +		}
   1.625 +
   1.626 +	if (operation == SMIME_VERIFY)
   1.627 +		{
   1.628 +		if (!(store = setup_verify(bio_err, CAfile, CApath)))
   1.629 +			goto end;
   1.630 +		X509_STORE_set_verify_cb_func(store, smime_cb);
   1.631 +		if (vpm)
   1.632 +			X509_STORE_set1_param(store, vpm);
   1.633 +		}
   1.634 +
   1.635 +
   1.636 +	ret = 3;
   1.637 +
   1.638 +	if (operation == SMIME_ENCRYPT)
   1.639 +		p7 = PKCS7_encrypt(encerts, in, cipher, flags);
   1.640 +	else if (operation == SMIME_SIGN)
   1.641 +		{
   1.642 +		/* If detached data and SMIME output enable partial
   1.643 +		 * signing.
   1.644 +		 */
   1.645 +		if ((flags & PKCS7_DETACHED) && (outformat == FORMAT_SMIME))
   1.646 +			flags |= PKCS7_STREAM;
   1.647 +		p7 = PKCS7_sign(signer, key, other, in, flags);
   1.648 +		/* Don't need to rewind for partial signing */
   1.649 +		if (!(flags & PKCS7_STREAM) && (BIO_reset(in) != 0))
   1.650 +			{
   1.651 +			BIO_printf(bio_err, "Can't rewind input file\n");
   1.652 +			goto end;
   1.653 +			}
   1.654 +		}
   1.655 +	else
   1.656 +		{
   1.657 +		if (informat == FORMAT_SMIME) 
   1.658 +			p7 = SMIME_read_PKCS7(in, &indata);
   1.659 +		else if (informat == FORMAT_PEM) 
   1.660 +			p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
   1.661 +		else if (informat == FORMAT_ASN1) 
   1.662 +			p7 = d2i_PKCS7_bio(in, NULL);
   1.663 +		else
   1.664 +			{
   1.665 +			BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
   1.666 +			goto end;
   1.667 +			}
   1.668 +
   1.669 +		if (!p7)
   1.670 +			{
   1.671 +			BIO_printf(bio_err, "Error reading S/MIME message\n");
   1.672 +			goto end;
   1.673 +			}
   1.674 +		if (contfile)
   1.675 +			{
   1.676 +			BIO_free(indata);
   1.677 +			if (!(indata = BIO_new_file(contfile, "rb")))
   1.678 +				{
   1.679 +				BIO_printf(bio_err, "Can't read content file %s\n", contfile);
   1.680 +				goto end;
   1.681 +				}
   1.682 +			}
   1.683 +		}
   1.684 +
   1.685 +	if (!p7)
   1.686 +		{
   1.687 +		BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
   1.688 +		goto end;
   1.689 +		}
   1.690 +
   1.691 +	ret = 4;
   1.692 +	if (operation == SMIME_DECRYPT)
   1.693 +		{
   1.694 +		if (!PKCS7_decrypt(p7, key, recip, out, flags))
   1.695 +			{
   1.696 +			BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
   1.697 +			goto end;
   1.698 +			}
   1.699 +		}
   1.700 +	else if (operation == SMIME_VERIFY)
   1.701 +		{
   1.702 +		STACK_OF(X509) *signers;
   1.703 +		if (PKCS7_verify(p7, other, store, indata, out, flags))
   1.704 +			BIO_printf(bio_err, "Verification successful\n");
   1.705 +		else
   1.706 +			{
   1.707 +			BIO_printf(bio_err, "Verification failure\n");
   1.708 +			goto end;
   1.709 +			}
   1.710 +		signers = PKCS7_get0_signers(p7, other, flags);
   1.711 +		if (!save_certs(signerfile, signers))
   1.712 +			{
   1.713 +			BIO_printf(bio_err, "Error writing signers to %s\n",
   1.714 +								signerfile);
   1.715 +			ret = 5;
   1.716 +			goto end;
   1.717 +			}
   1.718 +		sk_X509_free(signers);
   1.719 +		}
   1.720 +	else if (operation == SMIME_PK7OUT)
   1.721 +		PEM_write_bio_PKCS7(out, p7);
   1.722 +	else
   1.723 +		{
   1.724 +		if (to)
   1.725 +			BIO_printf(out, "To: %s\n", to);
   1.726 +		if (from)
   1.727 +			BIO_printf(out, "From: %s\n", from);
   1.728 +		if (subject)
   1.729 +			BIO_printf(out, "Subject: %s\n", subject);
   1.730 +		if (outformat == FORMAT_SMIME) 
   1.731 +			SMIME_write_PKCS7(out, p7, in, flags);
   1.732 +		else if (outformat == FORMAT_PEM) 
   1.733 +			PEM_write_bio_PKCS7(out,p7);
   1.734 +		else if (outformat == FORMAT_ASN1) 
   1.735 +			i2d_PKCS7_bio(out,p7);
   1.736 +		else
   1.737 +			{
   1.738 +			BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
   1.739 +			goto end;
   1.740 +			}
   1.741 +		}
   1.742 +	ret = 0;
   1.743 +end:
   1.744 +	if (need_rand)
   1.745 +		app_RAND_write_file(NULL, bio_err);
   1.746 +	if (ret) ERR_print_errors(bio_err);
   1.747 +	sk_X509_pop_free(encerts, X509_free);
   1.748 +	sk_X509_pop_free(other, X509_free);
   1.749 +	if (vpm)
   1.750 +		X509_VERIFY_PARAM_free(vpm);
   1.751 +	X509_STORE_free(store);
   1.752 +	X509_free(cert);
   1.753 +	X509_free(recip);
   1.754 +	X509_free(signer);
   1.755 +	EVP_PKEY_free(key);
   1.756 +	PKCS7_free(p7);
   1.757 +	BIO_free(in);
   1.758 +	BIO_free(indata);
   1.759 +	BIO_free_all(out);
   1.760 +	if (passin) OPENSSL_free(passin);
   1.761 +	return (ret);
   1.762 +}
   1.763 +
   1.764 +static int save_certs(char *signerfile, STACK_OF(X509) *signers)
   1.765 +	{
   1.766 +	int i;
   1.767 +	BIO *tmp;
   1.768 +	if (!signerfile)
   1.769 +		return 1;
   1.770 +	tmp = BIO_new_file(signerfile, "w");
   1.771 +	if (!tmp) return 0;
   1.772 +	for(i = 0; i < sk_X509_num(signers); i++)
   1.773 +		PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
   1.774 +	BIO_free(tmp);
   1.775 +	return 1;
   1.776 +	}
   1.777 +	
   1.778 +
   1.779 +/* Minimal callback just to output policy info (if any) */
   1.780 +
   1.781 +static int smime_cb(int ok, X509_STORE_CTX *ctx)
   1.782 +	{
   1.783 +	int error;
   1.784 +
   1.785 +	error = X509_STORE_CTX_get_error(ctx);
   1.786 +
   1.787 +	if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
   1.788 +		&& ((error != X509_V_OK) || (ok != 2)))
   1.789 +		return ok;
   1.790 +
   1.791 +	policies_print(NULL, ctx);
   1.792 +
   1.793 +	return ok;
   1.794 +
   1.795 +	}