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