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