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