]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/smime.c
6cf6ab3a452d864708f360ac1f8abe82eecb565c
[thirdparty/openssl.git] / apps / smime.c
1 /*
2 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 /* S/MIME utility function */
11
12 #include <stdio.h>
13 #include <string.h>
14 #include "apps.h"
15 #include "progs.h"
16 #include <openssl/crypto.h>
17 #include <openssl/pem.h>
18 #include <openssl/err.h>
19 #include <openssl/x509_vfy.h>
20 #include <openssl/x509v3.h>
21
22 static int save_certs(char *signerfile, STACK_OF(X509) *signers);
23 static int smime_cb(int ok, X509_STORE_CTX *ctx);
24
25 #define SMIME_OP 0x10
26 #define SMIME_IP 0x20
27 #define SMIME_SIGNERS 0x40
28 #define SMIME_ENCRYPT (1 | SMIME_OP)
29 #define SMIME_DECRYPT (2 | SMIME_IP)
30 #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
31 #define SMIME_VERIFY (4 | SMIME_IP)
32 #define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP)
33 #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
34
35 typedef enum OPTION_choice {
36 OPT_COMMON,
37 OPT_ENCRYPT, OPT_DECRYPT, OPT_SIGN, OPT_RESIGN, OPT_VERIFY,
38 OPT_PK7OUT, OPT_TEXT, OPT_NOINTERN, OPT_NOVERIFY, OPT_NOCHAIN,
39 OPT_NOCERTS, OPT_NOATTR, OPT_NODETACH, OPT_NOSMIMECAP,
40 OPT_BINARY, OPT_NOSIGS, OPT_STREAM, OPT_INDEF, OPT_NOINDEF,
41 OPT_CRLFEOL, OPT_ENGINE, OPT_PASSIN,
42 OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP, OPT_MD,
43 OPT_CIPHER, OPT_INKEY, OPT_KEYFORM, OPT_CERTFILE, OPT_CAFILE,
44 OPT_CAPATH, OPT_CASTORE, OPT_NOCAFILE, OPT_NOCAPATH, OPT_NOCASTORE,
45 OPT_R_ENUM, OPT_PROV_ENUM, OPT_CONFIG,
46 OPT_V_ENUM,
47 OPT_IN, OPT_INFORM, OPT_OUT,
48 OPT_OUTFORM, OPT_CONTENT
49 } OPTION_CHOICE;
50
51 const OPTIONS smime_options[] = {
52 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert...]\n"},
53
54 OPT_SECTION("General"),
55 {"help", OPT_HELP, '-', "Display this summary"},
56 {"in", OPT_IN, '<', "Input file"},
57 {"inform", OPT_INFORM, 'c', "Input format SMIME (default), PEM or DER"},
58 {"out", OPT_OUT, '>', "Output file"},
59 {"outform", OPT_OUTFORM, 'c',
60 "Output format SMIME (default), PEM or DER"},
61 {"inkey", OPT_INKEY, 's',
62 "Input private key (if not signer or recipient)"},
63 {"keyform", OPT_KEYFORM, 'f', "Input private key format (ENGINE, other values ignored)"},
64 #ifndef OPENSSL_NO_ENGINE
65 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
66 #endif
67 {"stream", OPT_STREAM, '-', "Enable CMS streaming" },
68 {"indef", OPT_INDEF, '-', "Same as -stream" },
69 {"noindef", OPT_NOINDEF, '-', "Disable CMS streaming"},
70 OPT_CONFIG_OPTION,
71
72 OPT_SECTION("Action"),
73 {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
74 {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
75 {"sign", OPT_SIGN, '-', "Sign message"},
76 {"resign", OPT_RESIGN, '-', "Resign a signed message"},
77 {"verify", OPT_VERIFY, '-', "Verify signed message"},
78
79 OPT_SECTION("Signing/Encryption"),
80 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
81 {"md", OPT_MD, 's', "Digest algorithm to use when signing or resigning"},
82 {"", OPT_CIPHER, '-', "Any supported cipher"},
83 {"pk7out", OPT_PK7OUT, '-', "Output PKCS#7 structure"},
84 {"nointern", OPT_NOINTERN, '-',
85 "Don't search certificates in message for signer"},
86 {"nodetach", OPT_NODETACH, '-', "Use opaque signing"},
87 {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"},
88 {"binary", OPT_BINARY, '-', "Don't translate message to text"},
89 {"signer", OPT_SIGNER, 's', "Signer certificate file"},
90 {"content", OPT_CONTENT, '<',
91 "Supply or override content for detached signature"},
92 {"nocerts", OPT_NOCERTS, '-',
93 "Don't include signers certificate when signing"},
94
95 OPT_SECTION("Verification/Decryption"),
96 {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"},
97 {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"},
98
99 {"certfile", OPT_CERTFILE, '<', "Other certificates file"},
100 {"recip", OPT_RECIP, '<', "Recipient certificate file for decryption"},
101
102 OPT_SECTION("Email"),
103 {"to", OPT_TO, 's', "To address"},
104 {"from", OPT_FROM, 's', "From address"},
105 {"subject", OPT_SUBJECT, 's', "Subject"},
106 {"text", OPT_TEXT, '-', "Include or delete text MIME headers"},
107 {"nosmimecap", OPT_NOSMIMECAP, '-', "Omit the SMIMECapabilities attribute"},
108
109 OPT_SECTION("Certificate chain"),
110 {"CApath", OPT_CAPATH, '/', "Trusted certificates directory"},
111 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
112 {"CAstore", OPT_CASTORE, ':', "Trusted certificates store URI"},
113 {"no-CAfile", OPT_NOCAFILE, '-',
114 "Do not load the default certificates file"},
115 {"no-CApath", OPT_NOCAPATH, '-',
116 "Do not load certificates from the default certificates directory"},
117 {"no-CAstore", OPT_NOCASTORE, '-',
118 "Do not load certificates from the default certificates store"},
119 {"nochain", OPT_NOCHAIN, '-',
120 "set PKCS7_NOCHAIN so certificates contained in the message are not used as untrusted CAs" },
121 {"crlfeol", OPT_CRLFEOL, '-', "Use CRLF as EOL termination instead of CR only"},
122
123 OPT_R_OPTIONS,
124 OPT_V_OPTIONS,
125 OPT_PROV_OPTIONS,
126
127 OPT_PARAMETERS(),
128 {"cert", 0, 0, "Recipient certs, used when encrypting"},
129 {NULL}
130 };
131
132 int smime_main(int argc, char **argv)
133 {
134 CONF *conf = NULL;
135 BIO *in = NULL, *out = NULL, *indata = NULL;
136 EVP_PKEY *key = NULL;
137 PKCS7 *p7 = NULL;
138 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
139 STACK_OF(X509) *encerts = NULL, *other = NULL;
140 X509 *cert = NULL, *recip = NULL, *signer = NULL;
141 X509_STORE *store = NULL;
142 X509_VERIFY_PARAM *vpm = NULL;
143 EVP_CIPHER *cipher = NULL;
144 EVP_MD *sign_md = NULL;
145 const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL, *prog = NULL;
146 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
147 char *infile = NULL, *outfile = NULL, *signerfile = NULL, *recipfile = NULL;
148 char *passinarg = NULL, *passin = NULL, *to = NULL, *from = NULL;
149 char *subject = NULL, *digestname = NULL, *ciphername = NULL;
150 OPTION_CHOICE o;
151 int noCApath = 0, noCAfile = 0, noCAstore = 0;
152 int flags = PKCS7_DETACHED, operation = 0, ret = 0, indef = 0;
153 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform =
154 FORMAT_UNDEF;
155 int vpmtouched = 0, rv = 0;
156 ENGINE *e = NULL;
157 const char *mime_eol = "\n";
158 OSSL_LIB_CTX *libctx = app_get0_libctx();
159
160 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
161 return 1;
162
163 prog = opt_init(argc, argv, smime_options);
164 while ((o = opt_next()) != OPT_EOF) {
165 switch (o) {
166 case OPT_EOF:
167 case OPT_ERR:
168 opthelp:
169 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
170 goto end;
171 case OPT_HELP:
172 opt_help(smime_options);
173 ret = 0;
174 goto end;
175 case OPT_INFORM:
176 if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))
177 goto opthelp;
178 break;
179 case OPT_IN:
180 infile = opt_arg();
181 break;
182 case OPT_OUTFORM:
183 if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))
184 goto opthelp;
185 break;
186 case OPT_OUT:
187 outfile = opt_arg();
188 break;
189 case OPT_ENCRYPT:
190 operation = SMIME_ENCRYPT;
191 break;
192 case OPT_DECRYPT:
193 operation = SMIME_DECRYPT;
194 break;
195 case OPT_SIGN:
196 operation = SMIME_SIGN;
197 break;
198 case OPT_RESIGN:
199 operation = SMIME_RESIGN;
200 break;
201 case OPT_VERIFY:
202 operation = SMIME_VERIFY;
203 break;
204 case OPT_PK7OUT:
205 operation = SMIME_PK7OUT;
206 break;
207 case OPT_TEXT:
208 flags |= PKCS7_TEXT;
209 break;
210 case OPT_NOINTERN:
211 flags |= PKCS7_NOINTERN;
212 break;
213 case OPT_NOVERIFY:
214 flags |= PKCS7_NOVERIFY;
215 break;
216 case OPT_NOCHAIN:
217 flags |= PKCS7_NOCHAIN;
218 break;
219 case OPT_NOCERTS:
220 flags |= PKCS7_NOCERTS;
221 break;
222 case OPT_NOATTR:
223 flags |= PKCS7_NOATTR;
224 break;
225 case OPT_NODETACH:
226 flags &= ~PKCS7_DETACHED;
227 break;
228 case OPT_NOSMIMECAP:
229 flags |= PKCS7_NOSMIMECAP;
230 break;
231 case OPT_BINARY:
232 flags |= PKCS7_BINARY;
233 break;
234 case OPT_NOSIGS:
235 flags |= PKCS7_NOSIGS;
236 break;
237 case OPT_STREAM:
238 case OPT_INDEF:
239 indef = 1;
240 break;
241 case OPT_NOINDEF:
242 indef = 0;
243 break;
244 case OPT_CRLFEOL:
245 flags |= PKCS7_CRLFEOL;
246 mime_eol = "\r\n";
247 break;
248 case OPT_R_CASES:
249 if (!opt_rand(o))
250 goto end;
251 break;
252 case OPT_PROV_CASES:
253 if (!opt_provider(o))
254 goto end;
255 break;
256 case OPT_CONFIG:
257 conf = app_load_config_modules(opt_arg());
258 if (conf == NULL)
259 goto end;
260 break;
261 case OPT_ENGINE:
262 e = setup_engine(opt_arg(), 0);
263 break;
264 case OPT_PASSIN:
265 passinarg = opt_arg();
266 break;
267 case OPT_TO:
268 to = opt_arg();
269 break;
270 case OPT_FROM:
271 from = opt_arg();
272 break;
273 case OPT_SUBJECT:
274 subject = opt_arg();
275 break;
276 case OPT_SIGNER:
277 /* If previous -signer argument add signer to list */
278 if (signerfile != NULL) {
279 if (sksigners == NULL
280 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
281 goto end;
282 sk_OPENSSL_STRING_push(sksigners, signerfile);
283 if (keyfile == NULL)
284 keyfile = signerfile;
285 if (skkeys == NULL
286 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
287 goto end;
288 sk_OPENSSL_STRING_push(skkeys, keyfile);
289 keyfile = NULL;
290 }
291 signerfile = opt_arg();
292 break;
293 case OPT_RECIP:
294 recipfile = opt_arg();
295 break;
296 case OPT_MD:
297 digestname = opt_arg();
298 break;
299 case OPT_CIPHER:
300 ciphername = opt_unknown();
301 break;
302 case OPT_INKEY:
303 /* If previous -inkey argument add signer to list */
304 if (keyfile != NULL) {
305 if (signerfile == NULL) {
306 BIO_printf(bio_err,
307 "%s: Must have -signer before -inkey\n", prog);
308 goto opthelp;
309 }
310 if (sksigners == NULL
311 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
312 goto end;
313 sk_OPENSSL_STRING_push(sksigners, signerfile);
314 signerfile = NULL;
315 if (skkeys == NULL
316 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
317 goto end;
318 sk_OPENSSL_STRING_push(skkeys, keyfile);
319 }
320 keyfile = opt_arg();
321 break;
322 case OPT_KEYFORM:
323 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
324 goto opthelp;
325 break;
326 case OPT_CERTFILE:
327 certfile = opt_arg();
328 break;
329 case OPT_CAFILE:
330 CAfile = opt_arg();
331 break;
332 case OPT_CAPATH:
333 CApath = opt_arg();
334 break;
335 case OPT_CASTORE:
336 CAstore = opt_arg();
337 break;
338 case OPT_NOCAFILE:
339 noCAfile = 1;
340 break;
341 case OPT_NOCAPATH:
342 noCApath = 1;
343 break;
344 case OPT_NOCASTORE:
345 noCAstore = 1;
346 break;
347 case OPT_CONTENT:
348 contfile = opt_arg();
349 break;
350 case OPT_V_CASES:
351 if (!opt_verify(o, vpm))
352 goto opthelp;
353 vpmtouched++;
354 break;
355 }
356 }
357
358 /* Extra arguments are files with recipient keys. */
359 argc = opt_num_rest();
360 argv = opt_rest();
361
362 if (!app_RAND_load())
363 goto end;
364
365 if (digestname != NULL) {
366 if (!opt_md(digestname, &sign_md))
367 goto opthelp;
368 }
369 if (!opt_cipher_any(ciphername, &cipher))
370 goto opthelp;
371 if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
372 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
373 goto opthelp;
374 }
375 if (!operation) {
376 BIO_puts(bio_err,
377 "No operation (-encrypt|-sign|...) specified\n");
378 goto opthelp;
379 }
380
381 if (operation & SMIME_SIGNERS) {
382 /* Check to see if any final signer needs to be appended */
383 if (keyfile && !signerfile) {
384 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
385 goto opthelp;
386 }
387 if (signerfile != NULL) {
388 if (sksigners == NULL
389 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
390 goto end;
391 sk_OPENSSL_STRING_push(sksigners, signerfile);
392 if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
393 goto end;
394 if (!keyfile)
395 keyfile = signerfile;
396 sk_OPENSSL_STRING_push(skkeys, keyfile);
397 }
398 if (sksigners == NULL) {
399 BIO_printf(bio_err, "No signer certificate specified\n");
400 goto opthelp;
401 }
402 signerfile = NULL;
403 keyfile = NULL;
404 } else if (operation == SMIME_DECRYPT) {
405 if (recipfile == NULL && keyfile == NULL) {
406 BIO_printf(bio_err,
407 "No recipient certificate or key specified\n");
408 goto opthelp;
409 }
410 } else if (operation == SMIME_ENCRYPT) {
411 if (argc == 0) {
412 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
413 goto opthelp;
414 }
415 }
416
417 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
418 BIO_printf(bio_err, "Error getting password\n");
419 goto end;
420 }
421
422 ret = 2;
423
424 if (!(operation & SMIME_SIGNERS))
425 flags &= ~PKCS7_DETACHED;
426
427 if (!(operation & SMIME_OP)) {
428 if (flags & PKCS7_BINARY)
429 outformat = FORMAT_BINARY;
430 }
431
432 if (!(operation & SMIME_IP)) {
433 if (flags & PKCS7_BINARY)
434 informat = FORMAT_BINARY;
435 }
436
437 if (operation == SMIME_ENCRYPT) {
438 if (cipher == NULL) {
439 #ifndef OPENSSL_NO_DES
440 cipher = (EVP_CIPHER *)EVP_des_ede3_cbc();
441 #else
442 BIO_printf(bio_err, "No cipher selected\n");
443 goto end;
444 #endif
445 }
446 encerts = sk_X509_new_null();
447 if (encerts == NULL)
448 goto end;
449 while (*argv != NULL) {
450 cert = load_cert(*argv, FORMAT_UNDEF,
451 "recipient certificate file");
452 if (cert == NULL)
453 goto end;
454 sk_X509_push(encerts, cert);
455 cert = NULL;
456 argv++;
457 }
458 }
459
460 if (certfile != NULL) {
461 if (!load_certs(certfile, 0, &other, NULL, "certificates")) {
462 ERR_print_errors(bio_err);
463 goto end;
464 }
465 }
466
467 if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
468 if ((recip = load_cert(recipfile, FORMAT_UNDEF,
469 "recipient certificate file")) == NULL) {
470 ERR_print_errors(bio_err);
471 goto end;
472 }
473 }
474
475 if (operation == SMIME_DECRYPT) {
476 if (keyfile == NULL)
477 keyfile = recipfile;
478 } else if (operation == SMIME_SIGN) {
479 if (keyfile == NULL)
480 keyfile = signerfile;
481 } else {
482 keyfile = NULL;
483 }
484
485 if (keyfile != NULL) {
486 key = load_key(keyfile, keyform, 0, passin, e, "signing key");
487 if (key == NULL)
488 goto end;
489 }
490
491 in = bio_open_default(infile, 'r', informat);
492 if (in == NULL)
493 goto end;
494
495 if (operation & SMIME_IP) {
496 PKCS7 *p7_in = NULL;
497
498 p7 = PKCS7_new_ex(libctx, app_get0_propq());
499 if (p7 == NULL) {
500 BIO_printf(bio_err, "Error allocating PKCS7 object\n");
501 goto end;
502 }
503 if (informat == FORMAT_SMIME) {
504 p7_in = SMIME_read_PKCS7_ex(in, &indata, &p7);
505 } else if (informat == FORMAT_PEM) {
506 p7_in = PEM_read_bio_PKCS7(in, &p7, NULL, NULL);
507 } else if (informat == FORMAT_ASN1) {
508 p7_in = d2i_PKCS7_bio(in, &p7);
509 } else {
510 BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
511 goto end;
512 }
513
514 if (p7_in == NULL) {
515 BIO_printf(bio_err, "Error reading S/MIME message\n");
516 goto end;
517 }
518 if (contfile != NULL) {
519 BIO_free(indata);
520 if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
521 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
522 goto end;
523 }
524 }
525 }
526
527 out = bio_open_default(outfile, 'w', outformat);
528 if (out == NULL)
529 goto end;
530
531 if (operation == SMIME_VERIFY) {
532 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
533 CAstore, noCAstore)) == NULL)
534 goto end;
535 X509_STORE_set_verify_cb(store, smime_cb);
536 if (vpmtouched)
537 X509_STORE_set1_param(store, vpm);
538 }
539
540 ret = 3;
541
542 if (operation == SMIME_ENCRYPT) {
543 if (indef)
544 flags |= PKCS7_STREAM;
545 p7 = PKCS7_encrypt_ex(encerts, in, cipher, flags, libctx, app_get0_propq());
546 } else if (operation & SMIME_SIGNERS) {
547 int i;
548 /*
549 * If detached data content we only enable streaming if S/MIME output
550 * format.
551 */
552 if (operation == SMIME_SIGN) {
553 if (flags & PKCS7_DETACHED) {
554 if (outformat == FORMAT_SMIME)
555 flags |= PKCS7_STREAM;
556 } else if (indef) {
557 flags |= PKCS7_STREAM;
558 }
559 flags |= PKCS7_PARTIAL;
560 p7 = PKCS7_sign_ex(NULL, NULL, other, in, flags, libctx, app_get0_propq());
561 if (p7 == NULL)
562 goto end;
563 if (flags & PKCS7_NOCERTS) {
564 for (i = 0; i < sk_X509_num(other); i++) {
565 X509 *x = sk_X509_value(other, i);
566 PKCS7_add_certificate(p7, x);
567 }
568 }
569 } else {
570 flags |= PKCS7_REUSE_DIGEST;
571 }
572 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
573 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
574 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
575 signer = load_cert(signerfile, FORMAT_UNDEF, "signer certificate");
576 if (signer == NULL)
577 goto end;
578 key = load_key(keyfile, keyform, 0, passin, e, "signing key");
579 if (key == NULL)
580 goto end;
581
582 if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))
583 goto end;
584 X509_free(signer);
585 signer = NULL;
586 EVP_PKEY_free(key);
587 key = NULL;
588 }
589 /* If not streaming or resigning finalize structure */
590 if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
591 if (!PKCS7_final(p7, in, flags))
592 goto end;
593 }
594 }
595
596 if (p7 == NULL) {
597 BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
598 goto end;
599 }
600
601 ret = 4;
602 if (operation == SMIME_DECRYPT) {
603 if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
604 BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
605 goto end;
606 }
607 } else if (operation == SMIME_VERIFY) {
608 STACK_OF(X509) *signers;
609 if (PKCS7_verify(p7, other, store, indata, out, flags))
610 BIO_printf(bio_err, "Verification successful\n");
611 else {
612 BIO_printf(bio_err, "Verification failure\n");
613 goto end;
614 }
615 signers = PKCS7_get0_signers(p7, other, flags);
616 if (!save_certs(signerfile, signers)) {
617 BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
618 ret = 5;
619 goto end;
620 }
621 sk_X509_free(signers);
622 } else if (operation == SMIME_PK7OUT) {
623 PEM_write_bio_PKCS7(out, p7);
624 } else {
625 if (to)
626 BIO_printf(out, "To: %s%s", to, mime_eol);
627 if (from)
628 BIO_printf(out, "From: %s%s", from, mime_eol);
629 if (subject)
630 BIO_printf(out, "Subject: %s%s", subject, mime_eol);
631 if (outformat == FORMAT_SMIME) {
632 if (operation == SMIME_RESIGN)
633 rv = SMIME_write_PKCS7(out, p7, indata, flags);
634 else
635 rv = SMIME_write_PKCS7(out, p7, in, flags);
636 } else if (outformat == FORMAT_PEM) {
637 rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags);
638 } else if (outformat == FORMAT_ASN1) {
639 rv = i2d_PKCS7_bio_stream(out, p7, in, flags);
640 } else {
641 BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
642 goto end;
643 }
644 if (rv == 0) {
645 BIO_printf(bio_err, "Error writing output\n");
646 ret = 3;
647 goto end;
648 }
649 }
650 ret = 0;
651 end:
652 if (ret)
653 ERR_print_errors(bio_err);
654 sk_X509_pop_free(encerts, X509_free);
655 sk_X509_pop_free(other, X509_free);
656 X509_VERIFY_PARAM_free(vpm);
657 sk_OPENSSL_STRING_free(sksigners);
658 sk_OPENSSL_STRING_free(skkeys);
659 X509_STORE_free(store);
660 X509_free(cert);
661 X509_free(recip);
662 X509_free(signer);
663 EVP_PKEY_free(key);
664 EVP_MD_free(sign_md);
665 EVP_CIPHER_free(cipher);
666 PKCS7_free(p7);
667 release_engine(e);
668 BIO_free(in);
669 BIO_free(indata);
670 BIO_free_all(out);
671 OPENSSL_free(passin);
672 NCONF_free(conf);
673 return ret;
674 }
675
676 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
677 {
678 int i;
679 BIO *tmp;
680
681 if (signerfile == NULL)
682 return 1;
683 tmp = BIO_new_file(signerfile, "w");
684 if (tmp == NULL)
685 return 0;
686 for (i = 0; i < sk_X509_num(signers); i++)
687 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
688 BIO_free(tmp);
689 return 1;
690 }
691
692 /* Minimal callback just to output policy info (if any) */
693
694 static int smime_cb(int ok, X509_STORE_CTX *ctx)
695 {
696 int error;
697
698 error = X509_STORE_CTX_get_error(ctx);
699
700 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
701 && ((error != X509_V_OK) || (ok != 2)))
702 return ok;
703
704 policies_print(ctx);
705
706 return ok;
707 }