os/ossrv/ssl/tsrc/topenssl/src/smime.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* smime.c */
sl@0
     2
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
sl@0
     3
 * project.
sl@0
     4
 */
sl@0
     5
/* ====================================================================
sl@0
     6
 * Copyright (c) 1999-2004 The OpenSSL Project.  All rights reserved.
sl@0
     7
 *
sl@0
     8
 * Redistribution and use in source and binary forms, with or without
sl@0
     9
 * modification, are permitted provided that the following conditions
sl@0
    10
 * are met:
sl@0
    11
 *
sl@0
    12
 * 1. Redistributions of source code must retain the above copyright
sl@0
    13
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    14
 *
sl@0
    15
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    16
 *    notice, this list of conditions and the following disclaimer in
sl@0
    17
 *    the documentation and/or other materials provided with the
sl@0
    18
 *    distribution.
sl@0
    19
 *
sl@0
    20
 * 3. All advertising materials mentioning features or use of this
sl@0
    21
 *    software must display the following acknowledgment:
sl@0
    22
 *    "This product includes software developed by the OpenSSL Project
sl@0
    23
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
sl@0
    24
 *
sl@0
    25
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    26
 *    endorse or promote products derived from this software without
sl@0
    27
 *    prior written permission. For written permission, please contact
sl@0
    28
 *    licensing@OpenSSL.org.
sl@0
    29
 *
sl@0
    30
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    31
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    32
 *    permission of the OpenSSL Project.
sl@0
    33
 *
sl@0
    34
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    35
 *    acknowledgment:
sl@0
    36
 *    "This product includes software developed by the OpenSSL Project
sl@0
    37
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
sl@0
    38
 *
sl@0
    39
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    40
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    42
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    43
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    48
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
    49
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
    50
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
    51
 * ====================================================================
sl@0
    52
 *
sl@0
    53
 * This product includes cryptographic software written by Eric Young
sl@0
    54
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
    55
 * Hudson (tjh@cryptsoft.com).
sl@0
    56
 *
sl@0
    57
 */
sl@0
    58
sl@0
    59
/* S/MIME utility function */
sl@0
    60
sl@0
    61
#include <stdio.h>
sl@0
    62
#include <string.h>
sl@0
    63
#include "apps.h"
sl@0
    64
#include <openssl/crypto.h>
sl@0
    65
#include <openssl/pem.h>
sl@0
    66
#include <openssl/err.h>
sl@0
    67
#include <openssl/x509_vfy.h>
sl@0
    68
#include <openssl/x509v3.h>
sl@0
    69
sl@0
    70
#undef PROG
sl@0
    71
#define PROG smime_main
sl@0
    72
static int save_certs(char *signerfile, STACK_OF(X509) *signers);
sl@0
    73
static int smime_cb(int ok, X509_STORE_CTX *ctx);
sl@0
    74
sl@0
    75
#define SMIME_OP	0x10
sl@0
    76
#define SMIME_ENCRYPT	(1 | SMIME_OP)
sl@0
    77
#define SMIME_DECRYPT	2
sl@0
    78
#define SMIME_SIGN	(3 | SMIME_OP)
sl@0
    79
#define SMIME_VERIFY	4
sl@0
    80
#define SMIME_PK7OUT	5
sl@0
    81
sl@0
    82
sl@0
    83
int MAIN(int, char **);
sl@0
    84
sl@0
    85
int MAIN(int argc, char **argv)
sl@0
    86
	{
sl@0
    87
	ENGINE *e = NULL;
sl@0
    88
	int operation = 0;
sl@0
    89
	int ret = 0;
sl@0
    90
	char **args;
sl@0
    91
	const char *inmode = "r", *outmode = "w";
sl@0
    92
	char *infile = NULL, *outfile = NULL;
sl@0
    93
	char *signerfile = NULL, *recipfile = NULL;
sl@0
    94
	char *certfile = NULL, *keyfile = NULL, *contfile=NULL;
sl@0
    95
	const EVP_CIPHER *cipher = NULL;
sl@0
    96
	PKCS7 *p7 = NULL;
sl@0
    97
	X509_STORE *store = NULL;
sl@0
    98
	X509 *cert = NULL, *recip = NULL, *signer = NULL;
sl@0
    99
	EVP_PKEY *key = NULL;
sl@0
   100
	STACK_OF(X509) *encerts = NULL, *other = NULL;
sl@0
   101
	BIO *in = NULL, *out = NULL, *indata = NULL;
sl@0
   102
	int badarg = 0;
sl@0
   103
	int flags = PKCS7_DETACHED;
sl@0
   104
	char *to = NULL, *from = NULL, *subject = NULL;
sl@0
   105
	char *CAfile = NULL, *CApath = NULL;
sl@0
   106
	char *passargin = NULL, *passin = NULL;
sl@0
   107
	char *inrand = NULL;
sl@0
   108
	int need_rand = 0;
sl@0
   109
	int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
sl@0
   110
        int keyform = FORMAT_PEM;
sl@0
   111
#ifndef OPENSSL_NO_ENGINE
sl@0
   112
	char *engine=NULL;
sl@0
   113
#endif
sl@0
   114
sl@0
   115
	X509_VERIFY_PARAM *vpm = NULL;
sl@0
   116
sl@0
   117
	args = argv + 1;
sl@0
   118
	ret = 1;
sl@0
   119
sl@0
   120
	apps_startup();
sl@0
   121
sl@0
   122
	if (bio_err == NULL)
sl@0
   123
		{
sl@0
   124
		if ((bio_err = BIO_new(BIO_s_file())) != NULL)
sl@0
   125
			BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
sl@0
   126
sl@0
   127
		}
sl@0
   128
sl@0
   129
	if (!load_config(bio_err, NULL))
sl@0
   130
		goto end;
sl@0
   131
sl@0
   132
	while (!badarg && *args && *args[0] == '-')
sl@0
   133
		{
sl@0
   134
		if (!strcmp (*args, "-encrypt"))
sl@0
   135
			operation = SMIME_ENCRYPT;
sl@0
   136
		else if (!strcmp (*args, "-decrypt"))
sl@0
   137
			operation = SMIME_DECRYPT;
sl@0
   138
		else if (!strcmp (*args, "-sign"))
sl@0
   139
			operation = SMIME_SIGN;
sl@0
   140
		else if (!strcmp (*args, "-verify"))
sl@0
   141
			operation = SMIME_VERIFY;
sl@0
   142
		else if (!strcmp (*args, "-pk7out"))
sl@0
   143
			operation = SMIME_PK7OUT;
sl@0
   144
#ifndef OPENSSL_NO_DES
sl@0
   145
		else if (!strcmp (*args, "-des3")) 
sl@0
   146
				cipher = EVP_des_ede3_cbc();
sl@0
   147
		else if (!strcmp (*args, "-des")) 
sl@0
   148
				cipher = EVP_des_cbc();
sl@0
   149
#endif
sl@0
   150
#ifndef OPENSSL_NO_RC2
sl@0
   151
		else if (!strcmp (*args, "-rc2-40")) 
sl@0
   152
				cipher = EVP_rc2_40_cbc();
sl@0
   153
		else if (!strcmp (*args, "-rc2-128")) 
sl@0
   154
				cipher = EVP_rc2_cbc();
sl@0
   155
		else if (!strcmp (*args, "-rc2-64")) 
sl@0
   156
				cipher = EVP_rc2_64_cbc();
sl@0
   157
#endif
sl@0
   158
#ifndef OPENSSL_NO_AES
sl@0
   159
		else if (!strcmp(*args,"-aes128"))
sl@0
   160
				cipher = EVP_aes_128_cbc();
sl@0
   161
		else if (!strcmp(*args,"-aes192"))
sl@0
   162
				cipher = EVP_aes_192_cbc();
sl@0
   163
		else if (!strcmp(*args,"-aes256"))
sl@0
   164
				cipher = EVP_aes_256_cbc();
sl@0
   165
#endif
sl@0
   166
		else if (!strcmp (*args, "-text")) 
sl@0
   167
				flags |= PKCS7_TEXT;
sl@0
   168
		else if (!strcmp (*args, "-nointern")) 
sl@0
   169
				flags |= PKCS7_NOINTERN;
sl@0
   170
		else if (!strcmp (*args, "-noverify")) 
sl@0
   171
				flags |= PKCS7_NOVERIFY;
sl@0
   172
		else if (!strcmp (*args, "-nochain")) 
sl@0
   173
				flags |= PKCS7_NOCHAIN;
sl@0
   174
		else if (!strcmp (*args, "-nocerts")) 
sl@0
   175
				flags |= PKCS7_NOCERTS;
sl@0
   176
		else if (!strcmp (*args, "-noattr")) 
sl@0
   177
				flags |= PKCS7_NOATTR;
sl@0
   178
		else if (!strcmp (*args, "-nodetach")) 
sl@0
   179
				flags &= ~PKCS7_DETACHED;
sl@0
   180
		else if (!strcmp (*args, "-nosmimecap"))
sl@0
   181
				flags |= PKCS7_NOSMIMECAP;
sl@0
   182
		else if (!strcmp (*args, "-binary"))
sl@0
   183
				flags |= PKCS7_BINARY;
sl@0
   184
		else if (!strcmp (*args, "-nosigs"))
sl@0
   185
				flags |= PKCS7_NOSIGS;
sl@0
   186
		else if (!strcmp (*args, "-nooldmime"))
sl@0
   187
				flags |= PKCS7_NOOLDMIMETYPE;
sl@0
   188
		else if (!strcmp (*args, "-crlfeol"))
sl@0
   189
				flags |= PKCS7_CRLFEOL;
sl@0
   190
		else if (!strcmp(*args,"-rand"))
sl@0
   191
			{
sl@0
   192
			if (args[1])
sl@0
   193
				{
sl@0
   194
				args++;
sl@0
   195
				inrand = *args;
sl@0
   196
				}
sl@0
   197
			else
sl@0
   198
				badarg = 1;
sl@0
   199
			need_rand = 1;
sl@0
   200
			}
sl@0
   201
#ifndef OPENSSL_NO_ENGINE
sl@0
   202
		else if (!strcmp(*args,"-engine"))
sl@0
   203
			{
sl@0
   204
			if (args[1])
sl@0
   205
				{
sl@0
   206
				args++;
sl@0
   207
				engine = *args;
sl@0
   208
				}
sl@0
   209
			else badarg = 1;
sl@0
   210
			}
sl@0
   211
#endif
sl@0
   212
		else if (!strcmp(*args,"-passin"))
sl@0
   213
			{
sl@0
   214
			if (args[1])
sl@0
   215
				{
sl@0
   216
				args++;
sl@0
   217
				passargin = *args;
sl@0
   218
				}
sl@0
   219
			else
sl@0
   220
				badarg = 1;
sl@0
   221
			}
sl@0
   222
		else if (!strcmp (*args, "-to"))
sl@0
   223
			{
sl@0
   224
			if (args[1])
sl@0
   225
				{
sl@0
   226
				args++;
sl@0
   227
				to = *args;
sl@0
   228
				}
sl@0
   229
			else
sl@0
   230
				badarg = 1;
sl@0
   231
			}
sl@0
   232
		else if (!strcmp (*args, "-from"))
sl@0
   233
			{
sl@0
   234
			if (args[1])
sl@0
   235
				{
sl@0
   236
				args++;
sl@0
   237
				from = *args;
sl@0
   238
				}
sl@0
   239
			else badarg = 1;
sl@0
   240
			}
sl@0
   241
		else if (!strcmp (*args, "-subject"))
sl@0
   242
			{
sl@0
   243
			if (args[1])
sl@0
   244
				{
sl@0
   245
				args++;
sl@0
   246
				subject = *args;
sl@0
   247
				}
sl@0
   248
			else
sl@0
   249
				badarg = 1;
sl@0
   250
			}
sl@0
   251
		else if (!strcmp (*args, "-signer"))
sl@0
   252
			{
sl@0
   253
			if (args[1])
sl@0
   254
				{
sl@0
   255
				args++;
sl@0
   256
				signerfile = *args;
sl@0
   257
				}
sl@0
   258
			else
sl@0
   259
				badarg = 1;
sl@0
   260
			}
sl@0
   261
		else if (!strcmp (*args, "-recip"))
sl@0
   262
			{
sl@0
   263
			if (args[1])
sl@0
   264
				{
sl@0
   265
				args++;
sl@0
   266
				recipfile = *args;
sl@0
   267
				}
sl@0
   268
			else badarg = 1;
sl@0
   269
			}
sl@0
   270
		else if (!strcmp (*args, "-inkey"))
sl@0
   271
			{
sl@0
   272
			if (args[1])
sl@0
   273
				{
sl@0
   274
				args++;
sl@0
   275
				keyfile = *args;
sl@0
   276
				}
sl@0
   277
			else
sl@0
   278
				badarg = 1;
sl@0
   279
		}
sl@0
   280
		else if (!strcmp (*args, "-keyform"))
sl@0
   281
			{
sl@0
   282
			if (args[1])
sl@0
   283
				{
sl@0
   284
				args++;
sl@0
   285
				keyform = str2fmt(*args);
sl@0
   286
				}
sl@0
   287
			else
sl@0
   288
				badarg = 1;
sl@0
   289
			}
sl@0
   290
		else if (!strcmp (*args, "-certfile"))
sl@0
   291
			{
sl@0
   292
			if (args[1])
sl@0
   293
				{
sl@0
   294
				args++;
sl@0
   295
				certfile = *args;
sl@0
   296
				}
sl@0
   297
			else
sl@0
   298
				badarg = 1;
sl@0
   299
			}
sl@0
   300
		else if (!strcmp (*args, "-CAfile"))
sl@0
   301
			{
sl@0
   302
			if (args[1])
sl@0
   303
				{
sl@0
   304
				args++;
sl@0
   305
				CAfile = *args;
sl@0
   306
				}
sl@0
   307
			else
sl@0
   308
				badarg = 1;
sl@0
   309
			}
sl@0
   310
		else if (!strcmp (*args, "-CApath"))
sl@0
   311
			{
sl@0
   312
			if (args[1])
sl@0
   313
				{
sl@0
   314
				args++;
sl@0
   315
				CApath = *args;
sl@0
   316
				}
sl@0
   317
			else
sl@0
   318
				badarg = 1;
sl@0
   319
			}
sl@0
   320
		else if (!strcmp (*args, "-in"))
sl@0
   321
			{
sl@0
   322
			if (args[1])
sl@0
   323
				{
sl@0
   324
				args++;
sl@0
   325
				infile = *args;
sl@0
   326
				}
sl@0
   327
			else
sl@0
   328
				badarg = 1;
sl@0
   329
			}
sl@0
   330
		else if (!strcmp (*args, "-inform"))
sl@0
   331
			{
sl@0
   332
			if (args[1])
sl@0
   333
				{
sl@0
   334
				args++;
sl@0
   335
				informat = str2fmt(*args);
sl@0
   336
				}
sl@0
   337
			else
sl@0
   338
				badarg = 1;
sl@0
   339
			}
sl@0
   340
		else if (!strcmp (*args, "-outform"))
sl@0
   341
			{
sl@0
   342
			if (args[1])
sl@0
   343
				{
sl@0
   344
				args++;
sl@0
   345
				outformat = str2fmt(*args);
sl@0
   346
				}
sl@0
   347
			else
sl@0
   348
				badarg = 1;
sl@0
   349
			}
sl@0
   350
		else if (!strcmp (*args, "-out"))
sl@0
   351
			{
sl@0
   352
			if (args[1])
sl@0
   353
				{
sl@0
   354
				args++;
sl@0
   355
				outfile = *args;
sl@0
   356
				}
sl@0
   357
			else
sl@0
   358
				badarg = 1;
sl@0
   359
			}
sl@0
   360
		else if (!strcmp (*args, "-content"))
sl@0
   361
			{
sl@0
   362
			if (args[1])
sl@0
   363
				{
sl@0
   364
				args++;
sl@0
   365
				contfile = *args;
sl@0
   366
				}
sl@0
   367
			else
sl@0
   368
				badarg = 1;
sl@0
   369
			}
sl@0
   370
		else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
sl@0
   371
			continue;
sl@0
   372
		else
sl@0
   373
			badarg = 1;
sl@0
   374
		args++;
sl@0
   375
		}
sl@0
   376
sl@0
   377
sl@0
   378
	if (operation == SMIME_SIGN)
sl@0
   379
		{
sl@0
   380
		if (!signerfile)
sl@0
   381
			{
sl@0
   382
			BIO_printf(bio_err, "No signer certificate specified\n");
sl@0
   383
			badarg = 1;
sl@0
   384
			}
sl@0
   385
		need_rand = 1;
sl@0
   386
		}
sl@0
   387
	else if (operation == SMIME_DECRYPT)
sl@0
   388
		{
sl@0
   389
		if (!recipfile && !keyfile)
sl@0
   390
			{
sl@0
   391
			BIO_printf(bio_err, "No recipient certificate or key specified\n");
sl@0
   392
			badarg = 1;
sl@0
   393
			}
sl@0
   394
		}
sl@0
   395
	else if (operation == SMIME_ENCRYPT)
sl@0
   396
		{
sl@0
   397
		if (!*args)
sl@0
   398
			{
sl@0
   399
			BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
sl@0
   400
			badarg = 1;
sl@0
   401
			}
sl@0
   402
		need_rand = 1;
sl@0
   403
		}
sl@0
   404
	else if (!operation)
sl@0
   405
		badarg = 1;
sl@0
   406
sl@0
   407
	if (badarg)
sl@0
   408
		{
sl@0
   409
		BIO_printf (bio_err, "Usage smime [options] cert.pem ...\n");
sl@0
   410
		BIO_printf (bio_err, "where options are\n");
sl@0
   411
		BIO_printf (bio_err, "-encrypt       encrypt message\n");
sl@0
   412
		BIO_printf (bio_err, "-decrypt       decrypt encrypted message\n");
sl@0
   413
		BIO_printf (bio_err, "-sign          sign message\n");
sl@0
   414
		BIO_printf (bio_err, "-verify        verify signed message\n");
sl@0
   415
		BIO_printf (bio_err, "-pk7out        output PKCS#7 structure\n");
sl@0
   416
#ifndef OPENSSL_NO_DES
sl@0
   417
		BIO_printf (bio_err, "-des3          encrypt with triple DES\n");
sl@0
   418
		BIO_printf (bio_err, "-des           encrypt with DES\n");
sl@0
   419
#endif
sl@0
   420
#ifndef OPENSSL_NO_RC2
sl@0
   421
		BIO_printf (bio_err, "-rc2-40        encrypt with RC2-40 (default)\n");
sl@0
   422
		BIO_printf (bio_err, "-rc2-64        encrypt with RC2-64\n");
sl@0
   423
		BIO_printf (bio_err, "-rc2-128       encrypt with RC2-128\n");
sl@0
   424
#endif
sl@0
   425
#ifndef OPENSSL_NO_AES
sl@0
   426
		BIO_printf (bio_err, "-aes128, -aes192, -aes256\n");
sl@0
   427
		BIO_printf (bio_err, "               encrypt PEM output with cbc aes\n");
sl@0
   428
#endif
sl@0
   429
		BIO_printf (bio_err, "-nointern      don't search certificates in message for signer\n");
sl@0
   430
		BIO_printf (bio_err, "-nosigs        don't verify message signature\n");
sl@0
   431
		BIO_printf (bio_err, "-noverify      don't verify signers certificate\n");
sl@0
   432
		BIO_printf (bio_err, "-nocerts       don't include signers certificate when signing\n");
sl@0
   433
		BIO_printf (bio_err, "-nodetach      use opaque signing\n");
sl@0
   434
		BIO_printf (bio_err, "-noattr        don't include any signed attributes\n");
sl@0
   435
		BIO_printf (bio_err, "-binary        don't translate message to text\n");
sl@0
   436
		BIO_printf (bio_err, "-certfile file other certificates file\n");
sl@0
   437
		BIO_printf (bio_err, "-signer file   signer certificate file\n");
sl@0
   438
		BIO_printf (bio_err, "-recip  file   recipient certificate file for decryption\n");
sl@0
   439
		BIO_printf (bio_err, "-in file       input file\n");
sl@0
   440
		BIO_printf (bio_err, "-inform arg    input format SMIME (default), PEM or DER\n");
sl@0
   441
		BIO_printf (bio_err, "-inkey file    input private key (if not signer or recipient)\n");
sl@0
   442
		BIO_printf (bio_err, "-keyform arg   input private key format (PEM or ENGINE)\n");
sl@0
   443
		BIO_printf (bio_err, "-out file      output file\n");
sl@0
   444
		BIO_printf (bio_err, "-outform arg   output format SMIME (default), PEM or DER\n");
sl@0
   445
		BIO_printf (bio_err, "-content file  supply or override content for detached signature\n");
sl@0
   446
		BIO_printf (bio_err, "-to addr       to address\n");
sl@0
   447
		BIO_printf (bio_err, "-from ad       from address\n");
sl@0
   448
		BIO_printf (bio_err, "-subject s     subject\n");
sl@0
   449
		BIO_printf (bio_err, "-text          include or delete text MIME headers\n");
sl@0
   450
		BIO_printf (bio_err, "-CApath dir    trusted certificates directory\n");
sl@0
   451
		BIO_printf (bio_err, "-CAfile file   trusted certificates file\n");
sl@0
   452
		BIO_printf (bio_err, "-crl_check     check revocation status of signer's certificate using CRLs\n");
sl@0
   453
		BIO_printf (bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
sl@0
   454
#ifndef OPENSSL_NO_ENGINE
sl@0
   455
		BIO_printf (bio_err, "-engine e      use engine e, possibly a hardware device.\n");
sl@0
   456
#endif
sl@0
   457
		BIO_printf (bio_err, "-passin arg    input file pass phrase source\n");
sl@0
   458
		BIO_printf(bio_err,  "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
sl@0
   459
		BIO_printf(bio_err,  "               load the file (or the files in the directory) into\n");
sl@0
   460
		BIO_printf(bio_err,  "               the random number generator\n");
sl@0
   461
		BIO_printf (bio_err, "cert.pem       recipient certificate(s) for encryption\n");
sl@0
   462
		goto end;
sl@0
   463
		}
sl@0
   464
sl@0
   465
#ifndef OPENSSL_NO_ENGINE
sl@0
   466
        e = setup_engine(bio_err, engine, 0);
sl@0
   467
#endif
sl@0
   468
sl@0
   469
	if (!app_passwd(bio_err, passargin, NULL, &passin, NULL))
sl@0
   470
		{
sl@0
   471
		BIO_printf(bio_err, "Error getting password\n");
sl@0
   472
		goto end;
sl@0
   473
		}
sl@0
   474
sl@0
   475
	if (need_rand)
sl@0
   476
		{
sl@0
   477
		app_RAND_load_file(NULL, bio_err, (inrand != NULL));
sl@0
   478
		if (inrand != NULL)
sl@0
   479
			BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
sl@0
   480
				app_RAND_load_files(inrand));
sl@0
   481
		}
sl@0
   482
sl@0
   483
	ret = 2;
sl@0
   484
sl@0
   485
	if (operation != SMIME_SIGN)
sl@0
   486
		flags &= ~PKCS7_DETACHED;
sl@0
   487
sl@0
   488
	if (operation & SMIME_OP)
sl@0
   489
		{
sl@0
   490
		if (flags & PKCS7_BINARY)
sl@0
   491
			inmode = "rb";
sl@0
   492
		if (outformat == FORMAT_ASN1)
sl@0
   493
			outmode = "wb";
sl@0
   494
		}
sl@0
   495
	else
sl@0
   496
		{
sl@0
   497
		if (flags & PKCS7_BINARY)
sl@0
   498
			outmode = "wb";
sl@0
   499
		if (informat == FORMAT_ASN1)
sl@0
   500
			inmode = "rb";
sl@0
   501
		}
sl@0
   502
sl@0
   503
	if (operation == SMIME_ENCRYPT)
sl@0
   504
		{
sl@0
   505
		if (!cipher)
sl@0
   506
			{
sl@0
   507
#ifndef OPENSSL_NO_RC2			
sl@0
   508
			cipher = EVP_rc2_40_cbc();
sl@0
   509
#else
sl@0
   510
			BIO_printf(bio_err, "No cipher selected\n");
sl@0
   511
			goto end;
sl@0
   512
#endif
sl@0
   513
			}
sl@0
   514
		encerts = sk_X509_new_null();
sl@0
   515
		while (*args)
sl@0
   516
			{
sl@0
   517
			if (!(cert = load_cert(bio_err,*args,FORMAT_PEM,
sl@0
   518
				NULL, e, "recipient certificate file")))
sl@0
   519
				{
sl@0
   520
#if 0				/* An appropriate message is already printed */
sl@0
   521
				BIO_printf(bio_err, "Can't read recipient certificate file %s\n", *args);
sl@0
   522
#endif
sl@0
   523
				goto end;
sl@0
   524
				}
sl@0
   525
			sk_X509_push(encerts, cert);
sl@0
   526
			cert = NULL;
sl@0
   527
			args++;
sl@0
   528
			}
sl@0
   529
		}
sl@0
   530
sl@0
   531
	if (signerfile && (operation == SMIME_SIGN))
sl@0
   532
		{
sl@0
   533
		if (!(signer = load_cert(bio_err,signerfile,FORMAT_PEM, NULL,
sl@0
   534
			e, "signer certificate")))
sl@0
   535
			{
sl@0
   536
#if 0			/* An appropri message has already been printed */
sl@0
   537
			BIO_printf(bio_err, "Can't read signer certificate file %s\n", signerfile);
sl@0
   538
#endif
sl@0
   539
			goto end;
sl@0
   540
			}
sl@0
   541
		}
sl@0
   542
sl@0
   543
	if (certfile)
sl@0
   544
		{
sl@0
   545
		if (!(other = load_certs(bio_err,certfile,FORMAT_PEM, NULL,
sl@0
   546
			e, "certificate file")))
sl@0
   547
			{
sl@0
   548
#if 0			/* An appropriate message has already been printed */
sl@0
   549
			BIO_printf(bio_err, "Can't read certificate file %s\n", certfile);
sl@0
   550
#endif
sl@0
   551
			ERR_print_errors(bio_err);
sl@0
   552
			goto end;
sl@0
   553
			}
sl@0
   554
		}
sl@0
   555
sl@0
   556
	if (recipfile && (operation == SMIME_DECRYPT))
sl@0
   557
		{
sl@0
   558
		if (!(recip = load_cert(bio_err,recipfile,FORMAT_PEM,NULL,
sl@0
   559
			e, "recipient certificate file")))
sl@0
   560
			{
sl@0
   561
#if 0			/* An appropriate message has alrady been printed */
sl@0
   562
			BIO_printf(bio_err, "Can't read recipient certificate file %s\n", recipfile);
sl@0
   563
#endif
sl@0
   564
			ERR_print_errors(bio_err);
sl@0
   565
			goto end;
sl@0
   566
			}
sl@0
   567
		}
sl@0
   568
sl@0
   569
	if (operation == SMIME_DECRYPT)
sl@0
   570
		{
sl@0
   571
		if (!keyfile)
sl@0
   572
			keyfile = recipfile;
sl@0
   573
		}
sl@0
   574
	else if (operation == SMIME_SIGN)
sl@0
   575
		{
sl@0
   576
		if (!keyfile)
sl@0
   577
			keyfile = signerfile;
sl@0
   578
		}
sl@0
   579
	else keyfile = NULL;
sl@0
   580
sl@0
   581
	if (keyfile)
sl@0
   582
		{
sl@0
   583
		key = load_key(bio_err, keyfile, keyform, 0, passin, e,
sl@0
   584
			       "signing key file");
sl@0
   585
		if (!key)
sl@0
   586
			goto end;
sl@0
   587
		}
sl@0
   588
sl@0
   589
	if (infile)
sl@0
   590
		{
sl@0
   591
		if (!(in = BIO_new_file(infile, inmode)))
sl@0
   592
			{
sl@0
   593
			BIO_printf (bio_err,
sl@0
   594
				 "Can't open input file %s\n", infile);
sl@0
   595
			goto end;
sl@0
   596
			}
sl@0
   597
		}
sl@0
   598
	else
sl@0
   599
		in = BIO_new_fp(stdin, BIO_NOCLOSE);
sl@0
   600
	
sl@0
   601
sl@0
   602
	if (outfile)
sl@0
   603
		{
sl@0
   604
		if (!(out = BIO_new_file(outfile, outmode)))
sl@0
   605
			{
sl@0
   606
			BIO_printf (bio_err,
sl@0
   607
				 "Can't open output file %s\n", outfile);
sl@0
   608
			goto end;
sl@0
   609
			}
sl@0
   610
		}
sl@0
   611
	else
sl@0
   612
		{
sl@0
   613
		out = BIO_new_fp(stdout, BIO_NOCLOSE);
sl@0
   614
sl@0
   615
#ifdef OPENSSL_SYS_VMS
sl@0
   616
		{
sl@0
   617
		    BIO *tmpbio = BIO_new(BIO_f_linebuffer());
sl@0
   618
		    out = BIO_push(tmpbio, out);
sl@0
   619
		}
sl@0
   620
#endif
sl@0
   621
		}
sl@0
   622
sl@0
   623
	if (operation == SMIME_VERIFY)
sl@0
   624
		{
sl@0
   625
		if (!(store = setup_verify(bio_err, CAfile, CApath)))
sl@0
   626
			goto end;
sl@0
   627
		X509_STORE_set_verify_cb_func(store, smime_cb);
sl@0
   628
		if (vpm)
sl@0
   629
			X509_STORE_set1_param(store, vpm);
sl@0
   630
		}
sl@0
   631
sl@0
   632
sl@0
   633
	ret = 3;
sl@0
   634
sl@0
   635
	if (operation == SMIME_ENCRYPT)
sl@0
   636
		p7 = PKCS7_encrypt(encerts, in, cipher, flags);
sl@0
   637
	else if (operation == SMIME_SIGN)
sl@0
   638
		{
sl@0
   639
		/* If detached data and SMIME output enable partial
sl@0
   640
		 * signing.
sl@0
   641
		 */
sl@0
   642
		if ((flags & PKCS7_DETACHED) && (outformat == FORMAT_SMIME))
sl@0
   643
			flags |= PKCS7_STREAM;
sl@0
   644
		p7 = PKCS7_sign(signer, key, other, in, flags);
sl@0
   645
		}
sl@0
   646
	else
sl@0
   647
		{
sl@0
   648
		if (informat == FORMAT_SMIME) 
sl@0
   649
			p7 = SMIME_read_PKCS7(in, &indata);
sl@0
   650
		else if (informat == FORMAT_PEM) 
sl@0
   651
			p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
sl@0
   652
		else if (informat == FORMAT_ASN1) 
sl@0
   653
			p7 = d2i_PKCS7_bio(in, NULL);
sl@0
   654
		else
sl@0
   655
			{
sl@0
   656
			BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
sl@0
   657
			goto end;
sl@0
   658
			}
sl@0
   659
sl@0
   660
		if (!p7)
sl@0
   661
			{
sl@0
   662
			BIO_printf(bio_err, "Error reading S/MIME message\n");
sl@0
   663
			goto end;
sl@0
   664
			}
sl@0
   665
		if (contfile)
sl@0
   666
			{
sl@0
   667
			BIO_free(indata);
sl@0
   668
			if (!(indata = BIO_new_file(contfile, "rb")))
sl@0
   669
				{
sl@0
   670
				BIO_printf(bio_err, "Can't read content file %s\n", contfile);
sl@0
   671
				goto end;
sl@0
   672
				}
sl@0
   673
			}
sl@0
   674
		}
sl@0
   675
sl@0
   676
	if (!p7)
sl@0
   677
		{
sl@0
   678
		BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
sl@0
   679
		goto end;
sl@0
   680
		}
sl@0
   681
sl@0
   682
	ret = 4;
sl@0
   683
	if (operation == SMIME_DECRYPT)
sl@0
   684
		{
sl@0
   685
		if (!PKCS7_decrypt(p7, key, recip, out, flags))
sl@0
   686
			{
sl@0
   687
			BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
sl@0
   688
			goto end;
sl@0
   689
			}
sl@0
   690
		}
sl@0
   691
	else if (operation == SMIME_VERIFY)
sl@0
   692
		{
sl@0
   693
		STACK_OF(X509) *signers;
sl@0
   694
		if (PKCS7_verify(p7, other, store, indata, out, flags))
sl@0
   695
			BIO_printf(bio_err, "Verification successful\n");
sl@0
   696
		else
sl@0
   697
			{
sl@0
   698
			BIO_printf(bio_err, "Verification failure\n");
sl@0
   699
			goto end;
sl@0
   700
			}
sl@0
   701
		signers = PKCS7_get0_signers(p7, other, flags);
sl@0
   702
		if (!save_certs(signerfile, signers))
sl@0
   703
			{
sl@0
   704
			BIO_printf(bio_err, "Error writing signers to %s\n",
sl@0
   705
								signerfile);
sl@0
   706
			ret = 5;
sl@0
   707
			goto end;
sl@0
   708
			}
sl@0
   709
		sk_X509_free(signers);
sl@0
   710
		}
sl@0
   711
	else if (operation == SMIME_PK7OUT)
sl@0
   712
		PEM_write_bio_PKCS7(out, p7);
sl@0
   713
	else
sl@0
   714
		{
sl@0
   715
		if (to)
sl@0
   716
			BIO_printf(out, "To: %s\n", to);
sl@0
   717
		if (from)
sl@0
   718
			BIO_printf(out, "From: %s\n", from);
sl@0
   719
		if (subject)
sl@0
   720
			BIO_printf(out, "Subject: %s\n", subject);
sl@0
   721
		if (outformat == FORMAT_SMIME) 
sl@0
   722
			SMIME_write_PKCS7(out, p7, in, flags);
sl@0
   723
		else if (outformat == FORMAT_PEM) 
sl@0
   724
			PEM_write_bio_PKCS7(out,p7);
sl@0
   725
		else if (outformat == FORMAT_ASN1) 
sl@0
   726
			i2d_PKCS7_bio(out,p7);
sl@0
   727
		else
sl@0
   728
			{
sl@0
   729
			BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
sl@0
   730
			goto end;
sl@0
   731
			}
sl@0
   732
		}
sl@0
   733
	ret = 0;
sl@0
   734
end:
sl@0
   735
	if (need_rand)
sl@0
   736
		app_RAND_write_file(NULL, bio_err);
sl@0
   737
	if (ret) ERR_print_errors(bio_err);
sl@0
   738
	sk_X509_pop_free(encerts, X509_free);
sl@0
   739
	sk_X509_pop_free(other, X509_free);
sl@0
   740
	if (vpm)
sl@0
   741
		X509_VERIFY_PARAM_free(vpm);
sl@0
   742
	X509_STORE_free(store);
sl@0
   743
	X509_free(cert);
sl@0
   744
	X509_free(recip);
sl@0
   745
	X509_free(signer);
sl@0
   746
	EVP_PKEY_free(key);
sl@0
   747
	PKCS7_free(p7);
sl@0
   748
	BIO_free(in);
sl@0
   749
	BIO_free(indata);
sl@0
   750
	BIO_free_all(out);
sl@0
   751
	if (passin) OPENSSL_free(passin);
sl@0
   752
	return (ret);
sl@0
   753
}
sl@0
   754
sl@0
   755
static int save_certs(char *signerfile, STACK_OF(X509) *signers)
sl@0
   756
	{
sl@0
   757
	int i;
sl@0
   758
	BIO *tmp;
sl@0
   759
	if (!signerfile)
sl@0
   760
		return 1;
sl@0
   761
	tmp = BIO_new_file(signerfile, "w");
sl@0
   762
	if (!tmp) return 0;
sl@0
   763
	for(i = 0; i < sk_X509_num(signers); i++)
sl@0
   764
		PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
sl@0
   765
	BIO_free(tmp);
sl@0
   766
	return 1;
sl@0
   767
	}
sl@0
   768
	
sl@0
   769
sl@0
   770
/* Minimal callback just to output policy info (if any) */
sl@0
   771
sl@0
   772
static int smime_cb(int ok, X509_STORE_CTX *ctx)
sl@0
   773
	{
sl@0
   774
	int error;
sl@0
   775
sl@0
   776
	error = X509_STORE_CTX_get_error(ctx);
sl@0
   777
sl@0
   778
	if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
sl@0
   779
		&& ((error != X509_V_OK) || (ok != 2)))
sl@0
   780
		return ok;
sl@0
   781
sl@0
   782
	policies_print(NULL, ctx);
sl@0
   783
sl@0
   784
	return ok;
sl@0
   785
sl@0
   786
	}