]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/smime.c
CMS sign digest
[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_COMMON,
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 EVP_CIPHER *cipher = NULL;
144 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;
149 char *subject = NULL, *digestname = NULL, *ciphername = NULL;
150 OPTION_CHOICE o;
151 int noCApath = 0, noCAfile = 0, noCAstore = 0;
152 int flags = PKCS7_DETACHED, operation = 0, ret = 0, indef = 0;
153 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform =
154 FORMAT_UNDEF;
155 int vpmtouched = 0, rv = 0;
156 ENGINE *e = NULL;
157 const char *mime_eol = "\n";
158 OSSL_LIB_CTX *libctx = app_get0_libctx();
159
160 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
161 return 1;
162
163 opt_set_unknown_name("cipher");
164 prog = opt_init(argc, argv, smime_options);
165 while ((o = opt_next()) != OPT_EOF) {
166 switch (o) {
167 case OPT_EOF:
168 case OPT_ERR:
169 opthelp:
170 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
171 goto end;
172 case OPT_HELP:
173 opt_help(smime_options);
174 ret = 0;
175 goto end;
176 case OPT_INFORM:
177 if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))
178 goto opthelp;
179 break;
180 case OPT_IN:
181 infile = opt_arg();
182 break;
183 case OPT_OUTFORM:
184 if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))
185 goto opthelp;
186 break;
187 case OPT_OUT:
188 outfile = opt_arg();
189 break;
190 case OPT_ENCRYPT:
191 operation = SMIME_ENCRYPT;
192 break;
193 case OPT_DECRYPT:
194 operation = SMIME_DECRYPT;
195 break;
196 case OPT_SIGN:
197 operation = SMIME_SIGN;
198 break;
199 case OPT_RESIGN:
200 operation = SMIME_RESIGN;
201 break;
202 case OPT_VERIFY:
203 operation = SMIME_VERIFY;
204 break;
205 case OPT_PK7OUT:
206 operation = SMIME_PK7OUT;
207 break;
208 case OPT_TEXT:
209 flags |= PKCS7_TEXT;
210 break;
211 case OPT_NOINTERN:
212 flags |= PKCS7_NOINTERN;
213 break;
214 case OPT_NOVERIFY:
215 flags |= PKCS7_NOVERIFY;
216 break;
217 case OPT_NOCHAIN:
218 flags |= PKCS7_NOCHAIN;
219 break;
220 case OPT_NOCERTS:
221 flags |= PKCS7_NOCERTS;
222 break;
223 case OPT_NOATTR:
224 flags |= PKCS7_NOATTR;
225 break;
226 case OPT_NODETACH:
227 flags &= ~PKCS7_DETACHED;
228 break;
229 case OPT_NOSMIMECAP:
230 flags |= PKCS7_NOSMIMECAP;
231 break;
232 case OPT_BINARY:
233 flags |= PKCS7_BINARY;
234 break;
235 case OPT_NOSIGS:
236 flags |= PKCS7_NOSIGS;
237 break;
238 case OPT_STREAM:
239 case OPT_INDEF:
240 indef = 1;
241 break;
242 case OPT_NOINDEF:
243 indef = 0;
244 break;
245 case OPT_CRLFEOL:
246 flags |= PKCS7_CRLFEOL;
247 mime_eol = "\r\n";
248 break;
249 case OPT_R_CASES:
250 if (!opt_rand(o))
251 goto end;
252 break;
253 case OPT_PROV_CASES:
254 if (!opt_provider(o))
255 goto end;
256 break;
257 case OPT_CONFIG:
258 conf = app_load_config_modules(opt_arg());
259 if (conf == NULL)
260 goto end;
261 break;
262 case OPT_ENGINE:
263 e = setup_engine(opt_arg(), 0);
264 break;
265 case OPT_PASSIN:
266 passinarg = opt_arg();
267 break;
268 case OPT_TO:
269 to = opt_arg();
270 break;
271 case OPT_FROM:
272 from = opt_arg();
273 break;
274 case OPT_SUBJECT:
275 subject = opt_arg();
276 break;
277 case OPT_SIGNER:
278 /* If previous -signer argument add signer to list */
279 if (signerfile != NULL) {
280 if (sksigners == NULL
281 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
282 goto end;
283 sk_OPENSSL_STRING_push(sksigners, signerfile);
284 if (keyfile == NULL)
285 keyfile = signerfile;
286 if (skkeys == NULL
287 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
288 goto end;
289 sk_OPENSSL_STRING_push(skkeys, keyfile);
290 keyfile = NULL;
291 }
292 signerfile = opt_arg();
293 break;
294 case OPT_RECIP:
295 recipfile = opt_arg();
296 break;
297 case OPT_MD:
298 digestname = opt_arg();
299 break;
300 case OPT_CIPHER:
301 ciphername = opt_unknown();
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 if (!app_RAND_load())
364 goto end;
365
366 if (digestname != NULL) {
367 if (!opt_md(digestname, &sign_md))
368 goto opthelp;
369 }
370 if (!opt_cipher_any(ciphername, &cipher))
371 goto opthelp;
372 if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
373 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
374 goto opthelp;
375 }
376 if (!operation) {
377 BIO_puts(bio_err,
378 "No operation (-encrypt|-sign|...) specified\n");
379 goto opthelp;
380 }
381
382 if (operation & SMIME_SIGNERS) {
383 /* Check to see if any final signer needs to be appended */
384 if (keyfile && !signerfile) {
385 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
386 goto opthelp;
387 }
388 if (signerfile != NULL) {
389 if (sksigners == NULL
390 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
391 goto end;
392 sk_OPENSSL_STRING_push(sksigners, signerfile);
393 if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
394 goto end;
395 if (!keyfile)
396 keyfile = signerfile;
397 sk_OPENSSL_STRING_push(skkeys, keyfile);
398 }
399 if (sksigners == NULL) {
400 BIO_printf(bio_err, "No signer certificate specified\n");
401 goto opthelp;
402 }
403 signerfile = NULL;
404 keyfile = NULL;
405 } else if (operation == SMIME_DECRYPT) {
406 if (recipfile == NULL && keyfile == NULL) {
407 BIO_printf(bio_err,
408 "No recipient certificate or key specified\n");
409 goto opthelp;
410 }
411 } else if (operation == SMIME_ENCRYPT) {
412 if (argc == 0) {
413 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
414 goto opthelp;
415 }
416 }
417
418 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
419 BIO_printf(bio_err, "Error getting password\n");
420 goto end;
421 }
422
423 ret = 2;
424
425 if (!(operation & SMIME_SIGNERS))
426 flags &= ~PKCS7_DETACHED;
427
428 if (!(operation & SMIME_OP)) {
429 if (flags & PKCS7_BINARY)
430 outformat = FORMAT_BINARY;
431 }
432
433 if (!(operation & SMIME_IP)) {
434 if (flags & PKCS7_BINARY)
435 informat = FORMAT_BINARY;
436 }
437
438 if (operation == SMIME_ENCRYPT) {
439 if (cipher == NULL) {
440 #ifndef OPENSSL_NO_DES
441 cipher = (EVP_CIPHER *)EVP_des_ede3_cbc();
442 #else
443 BIO_printf(bio_err, "No cipher selected\n");
444 goto end;
445 #endif
446 }
447 encerts = sk_X509_new_null();
448 if (encerts == NULL)
449 goto end;
450 while (*argv != NULL) {
451 cert = load_cert(*argv, FORMAT_UNDEF,
452 "recipient certificate file");
453 if (cert == NULL)
454 goto end;
455 sk_X509_push(encerts, cert);
456 cert = NULL;
457 argv++;
458 }
459 }
460
461 if (certfile != NULL) {
462 if (!load_certs(certfile, 0, &other, NULL, "certificates")) {
463 ERR_print_errors(bio_err);
464 goto end;
465 }
466 }
467
468 if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
469 if ((recip = load_cert(recipfile, FORMAT_UNDEF,
470 "recipient certificate file")) == NULL) {
471 ERR_print_errors(bio_err);
472 goto end;
473 }
474 }
475
476 if (operation == SMIME_DECRYPT) {
477 if (keyfile == NULL)
478 keyfile = recipfile;
479 } else if (operation == SMIME_SIGN) {
480 if (keyfile == NULL)
481 keyfile = signerfile;
482 } else {
483 keyfile = NULL;
484 }
485
486 if (keyfile != NULL) {
487 key = load_key(keyfile, keyform, 0, passin, e, "signing key");
488 if (key == NULL)
489 goto end;
490 }
491
492 in = bio_open_default(infile, 'r', informat);
493 if (in == NULL)
494 goto end;
495
496 if (operation & SMIME_IP) {
497 PKCS7 *p7_in = NULL;
498
499 p7 = PKCS7_new_ex(libctx, app_get0_propq());
500 if (p7 == NULL) {
501 BIO_printf(bio_err, "Error allocating PKCS7 object\n");
502 goto end;
503 }
504 if (informat == FORMAT_SMIME) {
505 p7_in = SMIME_read_PKCS7_ex(in, &indata, &p7);
506 } else if (informat == FORMAT_PEM) {
507 p7_in = PEM_read_bio_PKCS7(in, &p7, NULL, NULL);
508 } else if (informat == FORMAT_ASN1) {
509 p7_in = d2i_PKCS7_bio(in, &p7);
510 } else {
511 BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
512 goto end;
513 }
514
515 if (p7_in == NULL) {
516 BIO_printf(bio_err, "Error reading S/MIME message\n");
517 goto end;
518 }
519 if (contfile != NULL) {
520 BIO_free(indata);
521 if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
522 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
523 goto end;
524 }
525 }
526 }
527
528 out = bio_open_default(outfile, 'w', outformat);
529 if (out == NULL)
530 goto end;
531
532 if (operation == SMIME_VERIFY) {
533 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
534 CAstore, noCAstore)) == NULL)
535 goto end;
536 X509_STORE_set_verify_cb(store, smime_cb);
537 if (vpmtouched)
538 X509_STORE_set1_param(store, vpm);
539 }
540
541 ret = 3;
542
543 if (operation == SMIME_ENCRYPT) {
544 if (indef)
545 flags |= PKCS7_STREAM;
546 p7 = PKCS7_encrypt_ex(encerts, in, cipher, flags, libctx, app_get0_propq());
547 } else if (operation & SMIME_SIGNERS) {
548 int i;
549 /*
550 * If detached data content we only enable streaming if S/MIME output
551 * format.
552 */
553 if (operation == SMIME_SIGN) {
554 if (flags & PKCS7_DETACHED) {
555 if (outformat == FORMAT_SMIME)
556 flags |= PKCS7_STREAM;
557 } else if (indef) {
558 flags |= PKCS7_STREAM;
559 }
560 flags |= PKCS7_PARTIAL;
561 p7 = PKCS7_sign_ex(NULL, NULL, other, in, flags, libctx, app_get0_propq());
562 if (p7 == NULL)
563 goto end;
564 if (flags & PKCS7_NOCERTS) {
565 for (i = 0; i < sk_X509_num(other); i++) {
566 X509 *x = sk_X509_value(other, i);
567 PKCS7_add_certificate(p7, x);
568 }
569 }
570 } else {
571 flags |= PKCS7_REUSE_DIGEST;
572 }
573 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
574 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
575 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
576 signer = load_cert(signerfile, FORMAT_UNDEF, "signer certificate");
577 if (signer == NULL)
578 goto end;
579 key = load_key(keyfile, keyform, 0, passin, e, "signing key");
580 if (key == NULL)
581 goto end;
582
583 if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))
584 goto end;
585 X509_free(signer);
586 signer = NULL;
587 EVP_PKEY_free(key);
588 key = NULL;
589 }
590 /* If not streaming or resigning finalize structure */
591 if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
592 if (!PKCS7_final(p7, in, flags))
593 goto end;
594 }
595 }
596
597 if (p7 == NULL) {
598 BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
599 goto end;
600 }
601
602 ret = 4;
603 if (operation == SMIME_DECRYPT) {
604 if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
605 BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
606 goto end;
607 }
608 } else if (operation == SMIME_VERIFY) {
609 STACK_OF(X509) *signers;
610 if (PKCS7_verify(p7, other, store, indata, out, flags))
611 BIO_printf(bio_err, "Verification successful\n");
612 else {
613 BIO_printf(bio_err, "Verification failure\n");
614 goto end;
615 }
616 signers = PKCS7_get0_signers(p7, other, flags);
617 if (!save_certs(signerfile, signers)) {
618 BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
619 ret = 5;
620 goto end;
621 }
622 sk_X509_free(signers);
623 } else if (operation == SMIME_PK7OUT) {
624 PEM_write_bio_PKCS7(out, p7);
625 } else {
626 if (to)
627 BIO_printf(out, "To: %s%s", to, mime_eol);
628 if (from)
629 BIO_printf(out, "From: %s%s", from, mime_eol);
630 if (subject)
631 BIO_printf(out, "Subject: %s%s", subject, mime_eol);
632 if (outformat == FORMAT_SMIME) {
633 if (operation == SMIME_RESIGN)
634 rv = SMIME_write_PKCS7(out, p7, indata, flags);
635 else
636 rv = SMIME_write_PKCS7(out, p7, in, flags);
637 } else if (outformat == FORMAT_PEM) {
638 rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags);
639 } else if (outformat == FORMAT_ASN1) {
640 rv = i2d_PKCS7_bio_stream(out, p7, in, flags);
641 } else {
642 BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
643 goto end;
644 }
645 if (rv == 0) {
646 BIO_printf(bio_err, "Error writing output\n");
647 ret = 3;
648 goto end;
649 }
650 }
651 ret = 0;
652 end:
653 if (ret)
654 ERR_print_errors(bio_err);
655 OSSL_STACK_OF_X509_free(encerts);
656 OSSL_STACK_OF_X509_free(other);
657 X509_VERIFY_PARAM_free(vpm);
658 sk_OPENSSL_STRING_free(sksigners);
659 sk_OPENSSL_STRING_free(skkeys);
660 X509_STORE_free(store);
661 X509_free(cert);
662 X509_free(recip);
663 X509_free(signer);
664 EVP_PKEY_free(key);
665 EVP_MD_free(sign_md);
666 EVP_CIPHER_free(cipher);
667 PKCS7_free(p7);
668 release_engine(e);
669 BIO_free(in);
670 BIO_free(indata);
671 BIO_free_all(out);
672 OPENSSL_free(passin);
673 NCONF_free(conf);
674 return ret;
675 }
676
677 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
678 {
679 int i;
680 BIO *tmp;
681
682 if (signerfile == NULL)
683 return 1;
684 tmp = BIO_new_file(signerfile, "w");
685 if (tmp == NULL)
686 return 0;
687 for (i = 0; i < sk_X509_num(signers); i++)
688 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
689 BIO_free(tmp);
690 return 1;
691 }
692
693 /* Minimal callback just to output policy info (if any) */
694
695 static int smime_cb(int ok, X509_STORE_CTX *ctx)
696 {
697 int error;
698
699 error = X509_STORE_CTX_get_error(ctx);
700
701 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
702 && ((error != X509_V_OK) || (ok != 2)))
703 return ok;
704
705 policies_print(ctx);
706
707 return ok;
708 }