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