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