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