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