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