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