1 /* smime.c */ 2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 3 * project. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * licensing@OpenSSL.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 59 /* 60 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 61 * Use is subject to license terms. 62 */ 63 64 #pragma ident "%Z%%M% %I% %E% SMI" 65 66 /* S/MIME utility function */ 67 68 #include <stdio.h> 69 #include <string.h> 70 #include "apps.h" 71 #include <openssl/crypto.h> 72 #include <openssl/pem.h> 73 #include <openssl/err.h> 74 #include <openssl/x509_vfy.h> 75 #include <openssl/x509v3.h> 76 77 #ifdef SOLARIS_OPENSSL 78 extern int SUNWcry_installed; 79 #endif 80 81 #undef PROG 82 #define PROG smime_main 83 static int save_certs(char *signerfile, STACK_OF(X509) *signers); 84 static int smime_cb(int ok, X509_STORE_CTX *ctx); 85 86 #define SMIME_OP 0x10 87 #define SMIME_ENCRYPT (1 | SMIME_OP) 88 #define SMIME_DECRYPT 2 89 #define SMIME_SIGN (3 | SMIME_OP) 90 #define SMIME_VERIFY 4 91 #define SMIME_PK7OUT 5 92 93 int MAIN(int, char **); 94 95 int MAIN(int argc, char **argv) 96 { 97 ENGINE *e = NULL; 98 int operation = 0; 99 int ret = 0; 100 char **args; 101 const char *inmode = "r", *outmode = "w"; 102 char *infile = NULL, *outfile = NULL; 103 char *signerfile = NULL, *recipfile = NULL; 104 char *certfile = NULL, *keyfile = NULL, *contfile=NULL; 105 const EVP_CIPHER *cipher = NULL; 106 PKCS7 *p7 = NULL; 107 X509_STORE *store = NULL; 108 X509 *cert = NULL, *recip = NULL, *signer = NULL; 109 EVP_PKEY *key = NULL; 110 STACK_OF(X509) *encerts = NULL, *other = NULL; 111 BIO *in = NULL, *out = NULL, *indata = NULL; 112 int badarg = 0; 113 int flags = PKCS7_DETACHED; 114 char *to = NULL, *from = NULL, *subject = NULL; 115 char *CAfile = NULL, *CApath = NULL; 116 char *passargin = NULL, *passin = NULL; 117 char *inrand = NULL; 118 int need_rand = 0; 119 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME; 120 int keyform = FORMAT_PEM; 121 #ifndef OPENSSL_NO_ENGINE 122 char *engine=NULL; 123 #endif 124 125 X509_VERIFY_PARAM *vpm = NULL; 126 127 args = argv + 1; 128 ret = 1; 129 130 apps_startup(); 131 132 if (bio_err == NULL) 133 { 134 if ((bio_err = BIO_new(BIO_s_file())) != NULL) 135 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT); 136 } 137 138 if (!load_config(bio_err, NULL)) 139 goto end; 140 141 while (!badarg && *args && *args[0] == '-') 142 { 143 if (!strcmp (*args, "-encrypt")) 144 operation = SMIME_ENCRYPT; 145 else if (!strcmp (*args, "-decrypt")) 146 operation = SMIME_DECRYPT; 147 else if (!strcmp (*args, "-sign")) 148 operation = SMIME_SIGN; 149 else if (!strcmp (*args, "-verify")) 150 operation = SMIME_VERIFY; 151 else if (!strcmp (*args, "-pk7out")) 152 operation = SMIME_PK7OUT; 153 #ifndef OPENSSL_NO_DES 154 else if (!strcmp (*args, "-des3")) 155 cipher = EVP_des_ede3_cbc(); 156 else if (!strcmp (*args, "-des")) 157 cipher = EVP_des_cbc(); 158 #endif 159 #ifndef OPENSSL_NO_RC2 160 else if (!strcmp (*args, "-rc2-40")) 161 cipher = EVP_rc2_40_cbc(); 162 else if (!strcmp (*args, "-rc2-128")) 163 cipher = EVP_rc2_cbc(); 164 else if (!strcmp (*args, "-rc2-64")) 165 cipher = EVP_rc2_64_cbc(); 166 #endif 167 #ifndef OPENSSL_NO_AES 168 else if (!strcmp(*args,"-aes128")) 169 cipher = EVP_aes_128_cbc(); 170 #ifdef SOLARIS_OPENSSL 171 else if (strcmp(*argv,"-aes192") == 0) 172 { 173 if (!SUNWcry_installed) 174 { 175 BIO_printf(bio_err,"SUNWcry not installed.\n"); 176 goto end; 177 } 178 } 179 else if (strcmp(*argv,"-aes256") == 0) 180 { 181 if (!SUNWcry_installed) 182 { 183 BIO_printf(bio_err,"SUNWcry not installed.\n"); 184 goto end; 185 } 186 } 187 #else 188 else if (!strcmp(*args,"-aes192")) 189 cipher = EVP_aes_192_cbc(); 190 else if (!strcmp(*args,"-aes256")) 191 cipher = EVP_aes_256_cbc(); 192 #endif 193 #endif 194 else if (!strcmp (*args, "-text")) 195 flags |= PKCS7_TEXT; 196 else if (!strcmp (*args, "-nointern")) 197 flags |= PKCS7_NOINTERN; 198 else if (!strcmp (*args, "-noverify")) 199 flags |= PKCS7_NOVERIFY; 200 else if (!strcmp (*args, "-nochain")) 201 flags |= PKCS7_NOCHAIN; 202 else if (!strcmp (*args, "-nocerts")) 203 flags |= PKCS7_NOCERTS; 204 else if (!strcmp (*args, "-noattr")) 205 flags |= PKCS7_NOATTR; 206 else if (!strcmp (*args, "-nodetach")) 207 flags &= ~PKCS7_DETACHED; 208 else if (!strcmp (*args, "-nosmimecap")) 209 flags |= PKCS7_NOSMIMECAP; 210 else if (!strcmp (*args, "-binary")) 211 flags |= PKCS7_BINARY; 212 else if (!strcmp (*args, "-nosigs")) 213 flags |= PKCS7_NOSIGS; 214 else if (!strcmp (*args, "-nooldmime")) 215 flags |= PKCS7_NOOLDMIMETYPE; 216 else if (!strcmp (*args, "-crlfeol")) 217 flags |= PKCS7_CRLFEOL; 218 else if (!strcmp(*args,"-rand")) 219 { 220 if (args[1]) 221 { 222 args++; 223 inrand = *args; 224 } 225 else 226 badarg = 1; 227 need_rand = 1; 228 } 229 #ifndef OPENSSL_NO_ENGINE 230 else if (!strcmp(*args,"-engine")) 231 { 232 if (args[1]) 233 { 234 args++; 235 engine = *args; 236 } 237 else badarg = 1; 238 } 239 #endif 240 else if (!strcmp(*args,"-passin")) 241 { 242 if (args[1]) 243 { 244 args++; 245 passargin = *args; 246 } 247 else 248 badarg = 1; 249 } 250 else if (!strcmp (*args, "-to")) 251 { 252 if (args[1]) 253 { 254 args++; 255 to = *args; 256 } 257 else 258 badarg = 1; 259 } 260 else if (!strcmp (*args, "-from")) 261 { 262 if (args[1]) 263 { 264 args++; 265 from = *args; 266 } 267 else badarg = 1; 268 } 269 else if (!strcmp (*args, "-subject")) 270 { 271 if (args[1]) 272 { 273 args++; 274 subject = *args; 275 } 276 else 277 badarg = 1; 278 } 279 else if (!strcmp (*args, "-signer")) 280 { 281 if (args[1]) 282 { 283 args++; 284 signerfile = *args; 285 } 286 else 287 badarg = 1; 288 } 289 else if (!strcmp (*args, "-recip")) 290 { 291 if (args[1]) 292 { 293 args++; 294 recipfile = *args; 295 } 296 else badarg = 1; 297 } 298 else if (!strcmp (*args, "-inkey")) 299 { 300 if (args[1]) 301 { 302 args++; 303 keyfile = *args; 304 } 305 else 306 badarg = 1; 307 } 308 else if (!strcmp (*args, "-keyform")) 309 { 310 if (args[1]) 311 { 312 args++; 313 keyform = str2fmt(*args); 314 } 315 else 316 badarg = 1; 317 } 318 else if (!strcmp (*args, "-certfile")) 319 { 320 if (args[1]) 321 { 322 args++; 323 certfile = *args; 324 } 325 else 326 badarg = 1; 327 } 328 else if (!strcmp (*args, "-CAfile")) 329 { 330 if (args[1]) 331 { 332 args++; 333 CAfile = *args; 334 } 335 else 336 badarg = 1; 337 } 338 else if (!strcmp (*args, "-CApath")) 339 { 340 if (args[1]) 341 { 342 args++; 343 CApath = *args; 344 } 345 else 346 badarg = 1; 347 } 348 else if (!strcmp (*args, "-in")) 349 { 350 if (args[1]) 351 { 352 args++; 353 infile = *args; 354 } 355 else 356 badarg = 1; 357 } 358 else if (!strcmp (*args, "-inform")) 359 { 360 if (args[1]) 361 { 362 args++; 363 informat = str2fmt(*args); 364 } 365 else 366 badarg = 1; 367 } 368 else if (!strcmp (*args, "-outform")) 369 { 370 if (args[1]) 371 { 372 args++; 373 outformat = str2fmt(*args); 374 } 375 else 376 badarg = 1; 377 } 378 else if (!strcmp (*args, "-out")) 379 { 380 if (args[1]) 381 { 382 args++; 383 outfile = *args; 384 } 385 else 386 badarg = 1; 387 } 388 else if (!strcmp (*args, "-content")) 389 { 390 if (args[1]) 391 { 392 args++; 393 contfile = *args; 394 } 395 else 396 badarg = 1; 397 } 398 else if (args_verify(&args, NULL, &badarg, bio_err, &vpm)) 399 continue; 400 else 401 badarg = 1; 402 args++; 403 } 404 405 406 if (operation == SMIME_SIGN) 407 { 408 if (!signerfile) 409 { 410 BIO_printf(bio_err, "No signer certificate specified\n"); 411 badarg = 1; 412 } 413 need_rand = 1; 414 } 415 else if (operation == SMIME_DECRYPT) 416 { 417 if (!recipfile && !keyfile) 418 { 419 BIO_printf(bio_err, "No recipient certificate or key specified\n"); 420 badarg = 1; 421 } 422 } 423 else if (operation == SMIME_ENCRYPT) 424 { 425 if (!*args) 426 { 427 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n"); 428 badarg = 1; 429 } 430 need_rand = 1; 431 } 432 else if (!operation) 433 badarg = 1; 434 435 if (badarg) 436 { 437 BIO_printf (bio_err, "Usage smime [options] cert.pem ...\n"); 438 BIO_printf (bio_err, "where options are\n"); 439 BIO_printf (bio_err, "-encrypt encrypt message\n"); 440 BIO_printf (bio_err, "-decrypt decrypt encrypted message\n"); 441 BIO_printf (bio_err, "-sign sign message\n"); 442 BIO_printf (bio_err, "-verify verify signed message\n"); 443 BIO_printf (bio_err, "-pk7out output PKCS#7 structure\n"); 444 #ifndef OPENSSL_NO_DES 445 BIO_printf (bio_err, "-des3 encrypt with triple DES\n"); 446 BIO_printf (bio_err, "-des encrypt with DES\n"); 447 #endif 448 #ifndef OPENSSL_NO_RC2 449 BIO_printf (bio_err, "-rc2-40 encrypt with RC2-40 (default)\n"); 450 BIO_printf (bio_err, "-rc2-64 encrypt with RC2-64\n"); 451 BIO_printf (bio_err, "-rc2-128 encrypt with RC2-128\n"); 452 #endif 453 #ifndef OPENSSL_NO_AES 454 BIO_printf (bio_err, "-aes128, -aes192, -aes256\n"); 455 BIO_printf (bio_err, " encrypt PEM output with cbc aes\n"); 456 #endif 457 BIO_printf (bio_err, "-nointern don't search certificates in message for signer\n"); 458 BIO_printf (bio_err, "-nosigs don't verify message signature\n"); 459 BIO_printf (bio_err, "-noverify don't verify signers certificate\n"); 460 BIO_printf (bio_err, "-nocerts don't include signers certificate when signing\n"); 461 BIO_printf (bio_err, "-nodetach use opaque signing\n"); 462 BIO_printf (bio_err, "-noattr don't include any signed attributes\n"); 463 BIO_printf (bio_err, "-binary don't translate message to text\n"); 464 BIO_printf (bio_err, "-certfile file other certificates file\n"); 465 BIO_printf (bio_err, "-signer file signer certificate file\n"); 466 BIO_printf (bio_err, "-recip file recipient certificate file for decryption\n"); 467 BIO_printf (bio_err, "-in file input file\n"); 468 BIO_printf (bio_err, "-inform arg input format SMIME (default), PEM or DER\n"); 469 BIO_printf (bio_err, "-inkey file input private key (if not signer or recipient)\n"); 470 BIO_printf (bio_err, "-keyform arg input private key format (PEM or ENGINE)\n"); 471 BIO_printf (bio_err, "-out file output file\n"); 472 BIO_printf (bio_err, "-outform arg output format SMIME (default), PEM or DER\n"); 473 BIO_printf (bio_err, "-content file supply or override content for detached signature\n"); 474 BIO_printf (bio_err, "-to addr to address\n"); 475 BIO_printf (bio_err, "-from ad from address\n"); 476 BIO_printf (bio_err, "-subject s subject\n"); 477 BIO_printf (bio_err, "-text include or delete text MIME headers\n"); 478 BIO_printf (bio_err, "-CApath dir trusted certificates directory\n"); 479 BIO_printf (bio_err, "-CAfile file trusted certificates file\n"); 480 BIO_printf (bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n"); 481 BIO_printf (bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n"); 482 #ifndef OPENSSL_NO_ENGINE 483 BIO_printf (bio_err, "-engine e use engine e, possibly a hardware device.\n"); 484 #endif 485 BIO_printf (bio_err, "-passin arg input file pass phrase source\n"); 486 BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); 487 BIO_printf(bio_err, " load the file (or the files in the directory) into\n"); 488 BIO_printf(bio_err, " the random number generator\n"); 489 BIO_printf (bio_err, "cert.pem recipient certificate(s) for encryption\n"); 490 goto end; 491 } 492 493 #ifndef OPENSSL_NO_ENGINE 494 e = setup_engine(bio_err, engine, 0); 495 #endif 496 497 if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) 498 { 499 BIO_printf(bio_err, "Error getting password\n"); 500 goto end; 501 } 502 503 if (need_rand) 504 { 505 app_RAND_load_file(NULL, bio_err, (inrand != NULL)); 506 if (inrand != NULL) 507 BIO_printf(bio_err,"%ld semi-random bytes loaded\n", 508 app_RAND_load_files(inrand)); 509 } 510 511 ret = 2; 512 513 if (operation != SMIME_SIGN) 514 flags &= ~PKCS7_DETACHED; 515 516 if (operation & SMIME_OP) 517 { 518 if (flags & PKCS7_BINARY) 519 inmode = "rb"; 520 if (outformat == FORMAT_ASN1) 521 outmode = "wb"; 522 } 523 else 524 { 525 if (flags & PKCS7_BINARY) 526 outmode = "wb"; 527 if (informat == FORMAT_ASN1) 528 inmode = "rb"; 529 } 530 531 if (operation == SMIME_ENCRYPT) 532 { 533 if (!cipher) 534 { 535 #ifndef OPENSSL_NO_RC2 536 cipher = EVP_rc2_40_cbc(); 537 #else 538 BIO_printf(bio_err, "No cipher selected\n"); 539 goto end; 540 #endif 541 } 542 encerts = sk_X509_new_null(); 543 while (*args) 544 { 545 if (!(cert = load_cert(bio_err,*args,FORMAT_PEM, 546 NULL, e, "recipient certificate file"))) 547 { 548 #if 0 /* An appropriate message is already printed */ 549 BIO_printf(bio_err, "Can't read recipient certificate file %s\n", *args); 550 #endif 551 goto end; 552 } 553 sk_X509_push(encerts, cert); 554 cert = NULL; 555 args++; 556 } 557 } 558 559 if (signerfile && (operation == SMIME_SIGN)) 560 { 561 if (!(signer = load_cert(bio_err,signerfile,FORMAT_PEM, NULL, 562 e, "signer certificate"))) 563 { 564 #if 0 /* An appropri message has already been printed */ 565 BIO_printf(bio_err, "Can't read signer certificate file %s\n", signerfile); 566 #endif 567 goto end; 568 } 569 } 570 571 if (certfile) 572 { 573 if (!(other = load_certs(bio_err,certfile,FORMAT_PEM, NULL, 574 e, "certificate file"))) 575 { 576 #if 0 /* An appropriate message has already been printed */ 577 BIO_printf(bio_err, "Can't read certificate file %s\n", certfile); 578 #endif 579 ERR_print_errors(bio_err); 580 goto end; 581 } 582 } 583 584 if (recipfile && (operation == SMIME_DECRYPT)) 585 { 586 if (!(recip = load_cert(bio_err,recipfile,FORMAT_PEM,NULL, 587 e, "recipient certificate file"))) 588 { 589 #if 0 /* An appropriate message has alrady been printed */ 590 BIO_printf(bio_err, "Can't read recipient certificate file %s\n", recipfile); 591 #endif 592 ERR_print_errors(bio_err); 593 goto end; 594 } 595 } 596 597 if (operation == SMIME_DECRYPT) 598 { 599 if (!keyfile) 600 keyfile = recipfile; 601 } 602 else if (operation == SMIME_SIGN) 603 { 604 if (!keyfile) 605 keyfile = signerfile; 606 } 607 else keyfile = NULL; 608 609 if (keyfile) 610 { 611 key = load_key(bio_err, keyfile, keyform, 0, passin, e, 612 "signing key file"); 613 if (!key) 614 goto end; 615 } 616 617 if (infile) 618 { 619 if (!(in = BIO_new_file(infile, inmode))) 620 { 621 BIO_printf (bio_err, 622 "Can't open input file %s\n", infile); 623 goto end; 624 } 625 } 626 else 627 in = BIO_new_fp(stdin, BIO_NOCLOSE); 628 629 if (outfile) 630 { 631 if (!(out = BIO_new_file(outfile, outmode))) 632 { 633 BIO_printf (bio_err, 634 "Can't open output file %s\n", outfile); 635 goto end; 636 } 637 } 638 else 639 { 640 out = BIO_new_fp(stdout, BIO_NOCLOSE); 641 #ifdef OPENSSL_SYS_VMS 642 { 643 BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 644 out = BIO_push(tmpbio, out); 645 } 646 #endif 647 } 648 649 if (operation == SMIME_VERIFY) 650 { 651 if (!(store = setup_verify(bio_err, CAfile, CApath))) 652 goto end; 653 X509_STORE_set_verify_cb_func(store, smime_cb); 654 if (vpm) 655 X509_STORE_set1_param(store, vpm); 656 } 657 658 659 ret = 3; 660 661 if (operation == SMIME_ENCRYPT) 662 p7 = PKCS7_encrypt(encerts, in, cipher, flags); 663 else if (operation == SMIME_SIGN) 664 { 665 /* If detached data and SMIME output enable partial 666 * signing. 667 */ 668 if ((flags & PKCS7_DETACHED) && (outformat == FORMAT_SMIME)) 669 flags |= PKCS7_STREAM; 670 p7 = PKCS7_sign(signer, key, other, in, flags); 671 /* Don't need to rewind for partial signing */ 672 if (!(flags & PKCS7_STREAM) && (BIO_reset(in) != 0)) 673 { 674 BIO_printf(bio_err, "Can't rewind input file\n"); 675 goto end; 676 } 677 } 678 else 679 { 680 if (informat == FORMAT_SMIME) 681 p7 = SMIME_read_PKCS7(in, &indata); 682 else if (informat == FORMAT_PEM) 683 p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL); 684 else if (informat == FORMAT_ASN1) 685 p7 = d2i_PKCS7_bio(in, NULL); 686 else 687 { 688 BIO_printf(bio_err, "Bad input format for PKCS#7 file\n"); 689 goto end; 690 } 691 692 if (!p7) 693 { 694 BIO_printf(bio_err, "Error reading S/MIME message\n"); 695 goto end; 696 } 697 if (contfile) 698 { 699 BIO_free(indata); 700 if (!(indata = BIO_new_file(contfile, "rb"))) 701 { 702 BIO_printf(bio_err, "Can't read content file %s\n", contfile); 703 goto end; 704 } 705 } 706 } 707 708 if (!p7) 709 { 710 BIO_printf(bio_err, "Error creating PKCS#7 structure\n"); 711 goto end; 712 } 713 714 ret = 4; 715 if (operation == SMIME_DECRYPT) 716 { 717 if (!PKCS7_decrypt(p7, key, recip, out, flags)) 718 { 719 BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n"); 720 goto end; 721 } 722 } 723 else if (operation == SMIME_VERIFY) 724 { 725 STACK_OF(X509) *signers; 726 if (PKCS7_verify(p7, other, store, indata, out, flags)) 727 BIO_printf(bio_err, "Verification successful\n"); 728 else 729 { 730 BIO_printf(bio_err, "Verification failure\n"); 731 goto end; 732 } 733 signers = PKCS7_get0_signers(p7, other, flags); 734 if (!save_certs(signerfile, signers)) 735 { 736 BIO_printf(bio_err, "Error writing signers to %s\n", 737 signerfile); 738 ret = 5; 739 goto end; 740 } 741 sk_X509_free(signers); 742 } 743 else if (operation == SMIME_PK7OUT) 744 PEM_write_bio_PKCS7(out, p7); 745 else 746 { 747 if (to) 748 BIO_printf(out, "To: %s\n", to); 749 if (from) 750 BIO_printf(out, "From: %s\n", from); 751 if (subject) 752 BIO_printf(out, "Subject: %s\n", subject); 753 if (outformat == FORMAT_SMIME) 754 SMIME_write_PKCS7(out, p7, in, flags); 755 else if (outformat == FORMAT_PEM) 756 PEM_write_bio_PKCS7(out,p7); 757 else if (outformat == FORMAT_ASN1) 758 i2d_PKCS7_bio(out,p7); 759 else 760 { 761 BIO_printf(bio_err, "Bad output format for PKCS#7 file\n"); 762 goto end; 763 } 764 } 765 ret = 0; 766 end: 767 if (need_rand) 768 app_RAND_write_file(NULL, bio_err); 769 if (ret) ERR_print_errors(bio_err); 770 sk_X509_pop_free(encerts, X509_free); 771 sk_X509_pop_free(other, X509_free); 772 if (vpm) 773 X509_VERIFY_PARAM_free(vpm); 774 X509_STORE_free(store); 775 X509_free(cert); 776 X509_free(recip); 777 X509_free(signer); 778 EVP_PKEY_free(key); 779 PKCS7_free(p7); 780 BIO_free(in); 781 BIO_free(indata); 782 BIO_free_all(out); 783 if (passin) OPENSSL_free(passin); 784 return (ret); 785 } 786 787 static int save_certs(char *signerfile, STACK_OF(X509) *signers) 788 { 789 int i; 790 BIO *tmp; 791 if (!signerfile) 792 return 1; 793 tmp = BIO_new_file(signerfile, "w"); 794 if (!tmp) return 0; 795 for(i = 0; i < sk_X509_num(signers); i++) 796 PEM_write_bio_X509(tmp, sk_X509_value(signers, i)); 797 BIO_free(tmp); 798 return 1; 799 } 800 801 802 /* Minimal callback just to output policy info (if any) */ 803 804 static int smime_cb(int ok, X509_STORE_CTX *ctx) 805 { 806 int error; 807 808 error = X509_STORE_CTX_get_error(ctx); 809 810 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY) 811 && ((error != X509_V_OK) || (ok != 2))) 812 return ok; 813 814 policies_print(NULL, ctx); 815 816 return ok; 817 818 } 819