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