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