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