]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/smime.c
Big apps cleanup (option-parsing, etc)
[thirdparty/openssl.git] / apps / smime.c
CommitLineData
0f113f3e
MC
1/*
2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
27068df7 3 * project.
5a9a4b29
DSH
4 */
5/* ====================================================================
5d7c222d 6 * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
5a9a4b29
DSH
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
0f113f3e 13 * notice, this list of conditions and the following disclaimer.
5a9a4b29
DSH
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59/* S/MIME utility function */
60
61#include <stdio.h>
62#include <string.h>
36217a94 63#include "apps.h"
11afb40c 64#include <openssl/crypto.h>
5a9a4b29
DSH
65#include <openssl/pem.h>
66#include <openssl/err.h>
5d7c222d
DSH
67#include <openssl/x509_vfy.h>
68#include <openssl/x509v3.h>
5a9a4b29 69
55ec5861 70static int save_certs(char *signerfile, STACK_OF(X509) *signers);
5d7c222d 71static int smime_cb(int ok, X509_STORE_CTX *ctx);
5a9a4b29 72
0f113f3e
MC
73#define SMIME_OP 0x10
74#define SMIME_IP 0x20
75#define SMIME_SIGNERS 0x40
76#define SMIME_ENCRYPT (1 | SMIME_OP)
77#define SMIME_DECRYPT (2 | SMIME_IP)
78#define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
79#define SMIME_VERIFY (4 | SMIME_IP)
80#define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP)
81#define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
5a9a4b29 82
7e1b7485
RS
83typedef enum OPTION_choice {
84 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
85 OPT_ENCRYPT, OPT_DECRYPT, OPT_SIGN, OPT_RESIGN, OPT_VERIFY,
86 OPT_PK7OUT, OPT_TEXT, OPT_NOINTERN, OPT_NOVERIFY, OPT_NOCHAIN,
87 OPT_NOCERTS, OPT_NOATTR, OPT_NODETACH, OPT_NOSMIMECAP,
88 OPT_BINARY, OPT_NOSIGS, OPT_STREAM, OPT_INDEF, OPT_NOINDEF,
89 OPT_NOOLDMIME, OPT_CRLFEOL, OPT_RAND, OPT_ENGINE, OPT_PASSIN,
90 OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP, OPT_MD,
91 OPT_CIPHER, OPT_INKEY, OPT_KEYFORM, OPT_CERTFILE, OPT_CAFILE,
92 OPT_V_ENUM,
93 OPT_CAPATH, OPT_IN, OPT_INFORM, OPT_OUT, OPT_OUTFORM, OPT_CONTENT
94} OPTION_CHOICE;
95
96OPTIONS smime_options[] = {
97 {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"},
98 {OPT_HELP_STR, 1, '-',
99 " cert.pem... recipient certs for encryption\n"},
100 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
101 {"help", OPT_HELP, '-', "Display this summary"},
102 {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
103 {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
104 {"sign", OPT_SIGN, '-', "Sign message"},
105 {"verify", OPT_VERIFY, '-', "Verify signed message"},
106 {"pk7out", OPT_PK7OUT, '-', "Output PKCS#7 structure"},
107 {"nointern", OPT_NOINTERN, '-',
108 "Don't search certificates in message for signer"},
109 {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"},
110 {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"},
111 {"nocerts", OPT_NOCERTS, '-',
112 "Don't include signers certificate when signing"},
113 {"nodetach", OPT_NODETACH, '-', "Use opaque signing"},
114 {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"},
115 {"binary", OPT_BINARY, '-', "Don't translate message to text"},
116 {"certfile", OPT_CERTFILE, '<', "Other certificates file"},
117 {"signer", OPT_SIGNER, '<', "Signer certificate file"},
118 {"recip", OPT_RECIP, '<', "Recipient certificate file for decryption"},
119 {"in", OPT_IN, '<', "Input file"},
120 {"inform", OPT_INFORM, 'F', "Input format SMIME (default), PEM or DER"},
121 {"inkey", OPT_INKEY, '<',
122 "Input private key (if not signer or recipient)"},
123 {"keyform", OPT_KEYFORM, 'f', "Input private key format (PEM or ENGINE)"},
124 {"out", OPT_OUT, '>', "Output file"},
125 {"outform", OPT_OUTFORM, 'F',
126 "Output format SMIME (default), PEM or DER"},
127 {"content", OPT_CONTENT, '<',
128 "Supply or override content for detached signature"},
129 {"to", OPT_TO, 's', "To address"},
130 {"from", OPT_FROM, 's', "From address"},
131 {"subject", OPT_SUBJECT, 's', "Subject"},
132 {"text", OPT_TEXT, '-', "Include or delete text MIME headers"},
133 {"CApath", OPT_CAPATH, '/', "Trusted certificates directory"},
134 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
135 {"resign", OPT_RESIGN, '-'},
136 {"nochain", OPT_NOCHAIN, '-'},
137 {"nosmimecap", OPT_NOSMIMECAP, '-'},
138 {"stream", OPT_STREAM, '-'},
139 {"indef", OPT_INDEF, '-'},
140 {"noindef", OPT_NOINDEF, '-'},
141 {"nooldmime", OPT_NOOLDMIME, '-'},
142 {"crlfeol", OPT_CRLFEOL, '-'},
143 {"rand", OPT_RAND, 's',
144 "Load the file(s) into the random number generator"},
145 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
146 {"md", OPT_MD, 's'},
147 {"", OPT_CIPHER, '-', "Any supported cipher"},
148 OPT_V_OPTIONS,
149#ifndef OPENSSL_NO_ENGINE
150 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
151#endif
152 {NULL}
153};
667ac4ec 154
7e1b7485 155int smime_main(int argc, char **argv)
0f113f3e 156{
7e1b7485 157 BIO *in = NULL, *out = NULL, *indata = NULL;
0f113f3e 158 EVP_PKEY *key = NULL;
7e1b7485
RS
159 PKCS7 *p7 = NULL;
160 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
0f113f3e 161 STACK_OF(X509) *encerts = NULL, *other = NULL;
7e1b7485
RS
162 X509 *cert = NULL, *recip = NULL, *signer = NULL;
163 X509_STORE *store = NULL;
164 X509_VERIFY_PARAM *vpm = NULL;
165 const EVP_CIPHER *cipher = NULL;
0f113f3e 166 const EVP_MD *sign_md = NULL;
7e1b7485
RS
167 char *CAfile = NULL, *CApath = NULL, *inrand = NULL, *engine = NULL;
168 char *certfile = NULL, *keyfile = NULL, *contfile = NULL, *prog;
169 char *infile = NULL, *outfile = NULL, *signerfile = NULL, *recipfile =
170 NULL;
171 char *passinarg = NULL, *passin = NULL, *to = NULL, *from =
172 NULL, *subject = NULL;
173 const char *inmode = "r", *outmode = "w";
174 OPTION_CHOICE o;
175 int flags = PKCS7_DETACHED, operation = 0, ret = 0, need_rand = 0, indef =
176 0;
177 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform =
178 FORMAT_PEM;
179 int vpmtouched = 0, rv = 0;
0b13e9f0 180#ifndef OPENSSL_NO_ENGINE
7e1b7485 181 ENGINE *e = NULL;
0b13e9f0 182#endif
5a9a4b29 183
7e1b7485
RS
184 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
185 return 1;
0f113f3e 186
7e1b7485
RS
187 prog = opt_init(argc, argv, smime_options);
188 while ((o = opt_next()) != OPT_EOF) {
189 switch (o) {
190 case OPT_EOF:
191 case OPT_ERR:
192 opthelp:
193 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
194 goto end;
195 case OPT_HELP:
196 opt_help(smime_options);
197 ret = 0;
198 goto end;
199 case OPT_INFORM:
200 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
201 goto opthelp;
202 break;
203 case OPT_IN:
204 infile = opt_arg();
205 break;
206 case OPT_OUTFORM:
207 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
208 goto opthelp;
209 break;
210 case OPT_OUT:
211 outfile = opt_arg();
212 break;
213 case OPT_ENCRYPT:
0f113f3e 214 operation = SMIME_ENCRYPT;
7e1b7485
RS
215 break;
216 case OPT_DECRYPT:
0f113f3e 217 operation = SMIME_DECRYPT;
7e1b7485
RS
218 break;
219 case OPT_SIGN:
0f113f3e 220 operation = SMIME_SIGN;
7e1b7485
RS
221 break;
222 case OPT_RESIGN:
0f113f3e 223 operation = SMIME_RESIGN;
7e1b7485
RS
224 break;
225 case OPT_VERIFY:
0f113f3e 226 operation = SMIME_VERIFY;
7e1b7485
RS
227 break;
228 case OPT_PK7OUT:
0f113f3e 229 operation = SMIME_PK7OUT;
7e1b7485
RS
230 break;
231 case OPT_TEXT:
0f113f3e 232 flags |= PKCS7_TEXT;
7e1b7485
RS
233 break;
234 case OPT_NOINTERN:
0f113f3e 235 flags |= PKCS7_NOINTERN;
7e1b7485
RS
236 break;
237 case OPT_NOVERIFY:
0f113f3e 238 flags |= PKCS7_NOVERIFY;
7e1b7485
RS
239 break;
240 case OPT_NOCHAIN:
0f113f3e 241 flags |= PKCS7_NOCHAIN;
7e1b7485
RS
242 break;
243 case OPT_NOCERTS:
0f113f3e 244 flags |= PKCS7_NOCERTS;
7e1b7485
RS
245 break;
246 case OPT_NOATTR:
0f113f3e 247 flags |= PKCS7_NOATTR;
7e1b7485
RS
248 break;
249 case OPT_NODETACH:
0f113f3e 250 flags &= ~PKCS7_DETACHED;
7e1b7485
RS
251 break;
252 case OPT_NOSMIMECAP:
0f113f3e 253 flags |= PKCS7_NOSMIMECAP;
7e1b7485
RS
254 break;
255 case OPT_BINARY:
0f113f3e 256 flags |= PKCS7_BINARY;
7e1b7485
RS
257 break;
258 case OPT_NOSIGS:
0f113f3e 259 flags |= PKCS7_NOSIGS;
7e1b7485
RS
260 break;
261 case OPT_STREAM:
262 case OPT_INDEF:
0f113f3e 263 indef = 1;
7e1b7485
RS
264 break;
265 case OPT_NOINDEF:
0f113f3e 266 indef = 0;
7e1b7485
RS
267 break;
268 case OPT_NOOLDMIME:
0f113f3e 269 flags |= PKCS7_NOOLDMIMETYPE;
7e1b7485
RS
270 break;
271 case OPT_CRLFEOL:
0f113f3e 272 flags |= PKCS7_CRLFEOL;
7e1b7485
RS
273 break;
274 case OPT_RAND:
275 inrand = opt_arg();
0f113f3e 276 need_rand = 1;
7e1b7485
RS
277 break;
278 case OPT_ENGINE:
279 engine = opt_arg();
280 break;
281 case OPT_PASSIN:
282 passinarg = opt_arg();
283 break;
284 case OPT_TO:
285 to = opt_arg();
286 break;
287 case OPT_FROM:
288 from = opt_arg();
289 break;
290 case OPT_SUBJECT:
291 subject = opt_arg();
292 break;
293 case OPT_SIGNER:
0f113f3e 294 /* If previous -signer argument add signer to list */
0f113f3e 295 if (signerfile) {
7e1b7485
RS
296 if (sksigners == NULL
297 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
298 goto end;
0f113f3e 299 sk_OPENSSL_STRING_push(sksigners, signerfile);
7e1b7485 300 if (keyfile == NULL)
0f113f3e 301 keyfile = signerfile;
7e1b7485
RS
302 if (skkeys == NULL
303 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
304 goto end;
0f113f3e
MC
305 sk_OPENSSL_STRING_push(skkeys, keyfile);
306 keyfile = NULL;
307 }
7e1b7485
RS
308 signerfile = opt_arg();
309 break;
310 case OPT_RECIP:
311 recipfile = opt_arg();
312 break;
313 case OPT_MD:
314 if (!opt_md(opt_arg(), &sign_md))
315 goto opthelp;
316 break;
317 case OPT_CIPHER:
318 if (!opt_cipher(opt_unknown(), &cipher))
319 goto opthelp;
320 break;
321 case OPT_INKEY:
0f113f3e
MC
322 /* If previous -inkey arument add signer to list */
323 if (keyfile) {
7e1b7485
RS
324 if (signerfile == NULL) {
325 BIO_printf(bio_err,
326 "%s: Must have -signer before -inkey\n", prog);
327 goto opthelp;
0f113f3e 328 }
7e1b7485
RS
329 if (sksigners == NULL
330 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
331 goto end;
0f113f3e
MC
332 sk_OPENSSL_STRING_push(sksigners, signerfile);
333 signerfile = NULL;
7e1b7485
RS
334 if (skkeys == NULL
335 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
336 goto end;
0f113f3e
MC
337 sk_OPENSSL_STRING_push(skkeys, keyfile);
338 }
7e1b7485
RS
339 keyfile = opt_arg();
340 break;
341 case OPT_KEYFORM:
342 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
343 goto opthelp;
344 break;
345 case OPT_CERTFILE:
346 certfile = opt_arg();
347 break;
348 case OPT_CAFILE:
349 CAfile = opt_arg();
350 break;
351 case OPT_CAPATH:
352 CApath = opt_arg();
353 break;
354 case OPT_CONTENT:
355 contfile = opt_arg();
356 break;
357 case OPT_V_CASES:
358 if (!opt_verify(o, vpm))
359 goto opthelp;
360 vpmtouched++;
361 break;
362 }
0f113f3e 363 }
7e1b7485
RS
364 argc = opt_num_rest();
365 argv = opt_rest();
0f113f3e
MC
366
367 if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
368 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
7e1b7485 369 goto opthelp;
0f113f3e
MC
370 }
371
372 if (operation & SMIME_SIGNERS) {
373 /* Check to see if any final signer needs to be appended */
374 if (keyfile && !signerfile) {
375 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
7e1b7485 376 goto opthelp;
0f113f3e
MC
377 }
378 if (signerfile) {
7e1b7485
RS
379 if (!sksigners
380 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
381 goto end;
0f113f3e 382 sk_OPENSSL_STRING_push(sksigners, signerfile);
7e1b7485
RS
383 if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
384 goto end;
0f113f3e
MC
385 if (!keyfile)
386 keyfile = signerfile;
387 sk_OPENSSL_STRING_push(skkeys, keyfile);
388 }
389 if (!sksigners) {
390 BIO_printf(bio_err, "No signer certificate specified\n");
7e1b7485 391 goto opthelp;
0f113f3e
MC
392 }
393 signerfile = NULL;
394 keyfile = NULL;
395 need_rand = 1;
396 } else if (operation == SMIME_DECRYPT) {
397 if (!recipfile && !keyfile) {
398 BIO_printf(bio_err,
399 "No recipient certificate or key specified\n");
7e1b7485 400 goto opthelp;
0f113f3e
MC
401 }
402 } else if (operation == SMIME_ENCRYPT) {
7e1b7485 403 if (argc == 0) {
0f113f3e 404 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
7e1b7485 405 goto opthelp;
0f113f3e
MC
406 }
407 need_rand = 1;
408 } else if (!operation)
7e1b7485
RS
409 goto opthelp;
410
0b13e9f0 411#ifndef OPENSSL_NO_ENGINE
7e1b7485 412 e = setup_engine(engine, 0);
0b13e9f0 413#endif
5270e702 414
7e1b7485 415 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
0f113f3e
MC
416 BIO_printf(bio_err, "Error getting password\n");
417 goto end;
418 }
419
420 if (need_rand) {
7e1b7485 421 app_RAND_load_file(NULL, (inrand != NULL));
0f113f3e
MC
422 if (inrand != NULL)
423 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
424 app_RAND_load_files(inrand));
425 }
426
427 ret = 2;
428
429 if (!(operation & SMIME_SIGNERS))
430 flags &= ~PKCS7_DETACHED;
431
432 if (operation & SMIME_OP) {
433 if (outformat == FORMAT_ASN1)
434 outmode = "wb";
435 } else {
436 if (flags & PKCS7_BINARY)
437 outmode = "wb";
438 }
439
440 if (operation & SMIME_IP) {
441 if (informat == FORMAT_ASN1)
442 inmode = "rb";
443 } else {
444 if (flags & PKCS7_BINARY)
445 inmode = "rb";
446 }
447
448 if (operation == SMIME_ENCRYPT) {
449 if (!cipher) {
450#ifndef OPENSSL_NO_DES
451 cipher = EVP_des_ede3_cbc();
63da21c0 452#else
0f113f3e
MC
453 BIO_printf(bio_err, "No cipher selected\n");
454 goto end;
63da21c0 455#endif
0f113f3e
MC
456 }
457 encerts = sk_X509_new_null();
7e1b7485
RS
458 if (!encerts)
459 goto end;
460 while (*argv) {
461 cert = load_cert(*argv, FORMAT_PEM,
462 NULL, e, "recipient certificate file");
463 if (cert == NULL)
0f113f3e 464 goto end;
0f113f3e
MC
465 sk_X509_push(encerts, cert);
466 cert = NULL;
7e1b7485 467 argv++;
0f113f3e
MC
468 }
469 }
470
471 if (certfile) {
7e1b7485 472 if (!(other = load_certs(certfile, FORMAT_PEM, NULL,
0f113f3e
MC
473 e, "certificate file"))) {
474 ERR_print_errors(bio_err);
475 goto end;
476 }
477 }
478
479 if (recipfile && (operation == SMIME_DECRYPT)) {
7e1b7485 480 if (!(recip = load_cert(recipfile, FORMAT_PEM, NULL,
0f113f3e
MC
481 e, "recipient certificate file"))) {
482 ERR_print_errors(bio_err);
483 goto end;
484 }
485 }
486
487 if (operation == SMIME_DECRYPT) {
488 if (!keyfile)
489 keyfile = recipfile;
490 } else if (operation == SMIME_SIGN) {
491 if (!keyfile)
492 keyfile = signerfile;
493 } else
494 keyfile = NULL;
495
496 if (keyfile) {
7e1b7485 497 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
0f113f3e
MC
498 if (!key)
499 goto end;
500 }
501
7e1b7485
RS
502 in = bio_open_default(infile, inmode);
503 if (in == NULL)
504 goto end;
0f113f3e
MC
505
506 if (operation & SMIME_IP) {
507 if (informat == FORMAT_SMIME)
508 p7 = SMIME_read_PKCS7(in, &indata);
509 else if (informat == FORMAT_PEM)
510 p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
511 else if (informat == FORMAT_ASN1)
512 p7 = d2i_PKCS7_bio(in, NULL);
513 else {
514 BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
515 goto end;
516 }
517
518 if (!p7) {
519 BIO_printf(bio_err, "Error reading S/MIME message\n");
520 goto end;
521 }
522 if (contfile) {
523 BIO_free(indata);
524 if (!(indata = BIO_new_file(contfile, "rb"))) {
525 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
526 goto end;
527 }
528 }
529 }
530
7e1b7485
RS
531 out = bio_open_default(outfile, outmode);
532 if (out == NULL)
533 goto end;
0f113f3e
MC
534
535 if (operation == SMIME_VERIFY) {
7e1b7485 536 if (!(store = setup_verify(CAfile, CApath)))
0f113f3e
MC
537 goto end;
538 X509_STORE_set_verify_cb(store, smime_cb);
7e1b7485 539 if (vpmtouched)
0f113f3e
MC
540 X509_STORE_set1_param(store, vpm);
541 }
542
543 ret = 3;
544
545 if (operation == SMIME_ENCRYPT) {
546 if (indef)
547 flags |= PKCS7_STREAM;
548 p7 = PKCS7_encrypt(encerts, in, cipher, flags);
549 } else if (operation & SMIME_SIGNERS) {
550 int i;
551 /*
552 * If detached data content we only enable streaming if S/MIME output
553 * format.
554 */
555 if (operation == SMIME_SIGN) {
556 if (flags & PKCS7_DETACHED) {
557 if (outformat == FORMAT_SMIME)
558 flags |= PKCS7_STREAM;
559 } else if (indef)
560 flags |= PKCS7_STREAM;
561 flags |= PKCS7_PARTIAL;
562 p7 = PKCS7_sign(NULL, NULL, other, in, flags);
563 if (!p7)
564 goto end;
565 if (flags & PKCS7_NOCERTS) {
566 for (i = 0; i < sk_X509_num(other); i++) {
567 X509 *x = sk_X509_value(other, i);
568 PKCS7_add_certificate(p7, x);
569 }
570 }
571 } else
572 flags |= PKCS7_REUSE_DIGEST;
573 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
574 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
575 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
7e1b7485 576 signer = load_cert(signerfile, FORMAT_PEM, NULL,
0f113f3e
MC
577 e, "signer certificate");
578 if (!signer)
579 goto end;
7e1b7485 580 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
0f113f3e
MC
581 if (!key)
582 goto end;
583 if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))
584 goto end;
585 X509_free(signer);
586 signer = NULL;
587 EVP_PKEY_free(key);
588 key = NULL;
589 }
590 /* If not streaming or resigning finalize structure */
591 if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
592 if (!PKCS7_final(p7, in, flags))
593 goto end;
594 }
595 }
596
597 if (!p7) {
598 BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
599 goto end;
600 }
601
602 ret = 4;
603 if (operation == SMIME_DECRYPT) {
604 if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
605 BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
606 goto end;
607 }
608 } else if (operation == SMIME_VERIFY) {
609 STACK_OF(X509) *signers;
610 if (PKCS7_verify(p7, other, store, indata, out, flags))
611 BIO_printf(bio_err, "Verification successful\n");
612 else {
613 BIO_printf(bio_err, "Verification failure\n");
614 goto end;
615 }
616 signers = PKCS7_get0_signers(p7, other, flags);
617 if (!save_certs(signerfile, signers)) {
618 BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
619 ret = 5;
620 goto end;
621 }
622 sk_X509_free(signers);
623 } else if (operation == SMIME_PK7OUT)
624 PEM_write_bio_PKCS7(out, p7);
625 else {
626 if (to)
627 BIO_printf(out, "To: %s\n", to);
628 if (from)
629 BIO_printf(out, "From: %s\n", from);
630 if (subject)
631 BIO_printf(out, "Subject: %s\n", subject);
632 if (outformat == FORMAT_SMIME) {
633 if (operation == SMIME_RESIGN)
7e1b7485 634 rv = SMIME_write_PKCS7(out, p7, indata, flags);
0f113f3e 635 else
7e1b7485 636 rv = SMIME_write_PKCS7(out, p7, in, flags);
0f113f3e 637 } else if (outformat == FORMAT_PEM)
7e1b7485 638 rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags);
0f113f3e 639 else if (outformat == FORMAT_ASN1)
7e1b7485 640 rv = i2d_PKCS7_bio_stream(out, p7, in, flags);
0f113f3e
MC
641 else {
642 BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
643 goto end;
644 }
7e1b7485
RS
645 if (rv == 0) {
646 BIO_printf(bio_err, "Error writing output\n");
647 ret = 3;
648 goto end;
649 }
0f113f3e
MC
650 }
651 ret = 0;
652 end:
653 if (need_rand)
7e1b7485 654 app_RAND_write_file(NULL);
0f113f3e
MC
655 if (ret)
656 ERR_print_errors(bio_err);
657 sk_X509_pop_free(encerts, X509_free);
658 sk_X509_pop_free(other, X509_free);
659 if (vpm)
660 X509_VERIFY_PARAM_free(vpm);
661 if (sksigners)
662 sk_OPENSSL_STRING_free(sksigners);
663 if (skkeys)
664 sk_OPENSSL_STRING_free(skkeys);
665 X509_STORE_free(store);
666 X509_free(cert);
667 X509_free(recip);
668 X509_free(signer);
669 EVP_PKEY_free(key);
670 PKCS7_free(p7);
671 BIO_free(in);
672 BIO_free(indata);
673 BIO_free_all(out);
674 if (passin)
675 OPENSSL_free(passin);
676 return (ret);
5a9a4b29
DSH
677}
678
a91451ef 679static int save_certs(char *signerfile, STACK_OF(X509) *signers)
0f113f3e
MC
680{
681 int i;
682 BIO *tmp;
683 if (!signerfile)
684 return 1;
685 tmp = BIO_new_file(signerfile, "w");
686 if (!tmp)
687 return 0;
688 for (i = 0; i < sk_X509_num(signers); i++)
689 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
690 BIO_free(tmp);
691 return 1;
692}
5d7c222d 693
5d7c222d
DSH
694/* Minimal callback just to output policy info (if any) */
695
696static int smime_cb(int ok, X509_STORE_CTX *ctx)
0f113f3e
MC
697{
698 int error;
5d7c222d 699
0f113f3e 700 error = X509_STORE_CTX_get_error(ctx);
5d7c222d 701
0f113f3e
MC
702 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
703 && ((error != X509_V_OK) || (ok != 2)))
704 return ok;
5d7c222d 705
7e1b7485 706 policies_print(bio_err, ctx);
5d7c222d 707
0f113f3e 708 return ok;
5d7c222d 709
0f113f3e 710}