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