1 /* $OpenBSD: smime.c,v 1.2 2015/02/08 10:22:45 doug Exp $ */ 2 /* Written by Dr Stephen N Henson (steve@openssl.org) 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 /* S/MIME utility function */ 60 61 #include <stdio.h> 62 #include <string.h> 63 64 #include "apps.h" 65 66 #include <openssl/crypto.h> 67 #include <openssl/err.h> 68 #include <openssl/pem.h> 69 #include <openssl/x509_vfy.h> 70 #include <openssl/x509v3.h> 71 72 static int save_certs(char *signerfile, STACK_OF(X509) * signers); 73 static int smime_cb(int ok, X509_STORE_CTX * ctx); 74 75 #define SMIME_OP 0x10 76 #define SMIME_IP 0x20 77 #define SMIME_SIGNERS 0x40 78 #define SMIME_ENCRYPT (1 | SMIME_OP) 79 #define SMIME_DECRYPT (2 | SMIME_IP) 80 #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS) 81 #define SMIME_VERIFY (4 | SMIME_IP) 82 #define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP) 83 #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS) 84 85 int smime_main(int, char **); 86 87 int 88 smime_main(int argc, char **argv) 89 { 90 ENGINE *e = NULL; 91 int operation = 0; 92 int ret = 0; 93 char **args; 94 const char *inmode = "r", *outmode = "w"; 95 char *infile = NULL, *outfile = NULL; 96 char *signerfile = NULL, *recipfile = NULL; 97 STACK_OF(OPENSSL_STRING) * sksigners = NULL, *skkeys = NULL; 98 char *certfile = NULL, *keyfile = NULL, *contfile = NULL; 99 const EVP_CIPHER *cipher = NULL; 100 PKCS7 *p7 = NULL; 101 X509_STORE *store = NULL; 102 X509 *cert = NULL, *recip = NULL, *signer = NULL; 103 EVP_PKEY *key = NULL; 104 STACK_OF(X509) * encerts = NULL, *other = NULL; 105 BIO *in = NULL, *out = NULL, *indata = NULL; 106 int badarg = 0; 107 int flags = PKCS7_DETACHED; 108 char *to = NULL, *from = NULL, *subject = NULL; 109 char *CAfile = NULL, *CApath = NULL; 110 char *passargin = NULL, *passin = NULL; 111 int indef = 0; 112 const EVP_MD *sign_md = NULL; 113 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME; 114 int keyform = FORMAT_PEM; 115 #ifndef OPENSSL_NO_ENGINE 116 char *engine = NULL; 117 #endif 118 119 X509_VERIFY_PARAM *vpm = NULL; 120 121 args = argv + 1; 122 ret = 1; 123 124 while (!badarg && *args && *args[0] == '-') { 125 if (!strcmp(*args, "-encrypt")) 126 operation = SMIME_ENCRYPT; 127 else if (!strcmp(*args, "-decrypt")) 128 operation = SMIME_DECRYPT; 129 else if (!strcmp(*args, "-sign")) 130 operation = SMIME_SIGN; 131 else if (!strcmp(*args, "-resign")) 132 operation = SMIME_RESIGN; 133 else if (!strcmp(*args, "-verify")) 134 operation = SMIME_VERIFY; 135 else if (!strcmp(*args, "-pk7out")) 136 operation = SMIME_PK7OUT; 137 #ifndef OPENSSL_NO_DES 138 else if (!strcmp(*args, "-des3")) 139 cipher = EVP_des_ede3_cbc(); 140 else if (!strcmp(*args, "-des")) 141 cipher = EVP_des_cbc(); 142 #endif 143 #ifndef OPENSSL_NO_RC2 144 else if (!strcmp(*args, "-rc2-40")) 145 cipher = EVP_rc2_40_cbc(); 146 else if (!strcmp(*args, "-rc2-128")) 147 cipher = EVP_rc2_cbc(); 148 else if (!strcmp(*args, "-rc2-64")) 149 cipher = EVP_rc2_64_cbc(); 150 #endif 151 #ifndef OPENSSL_NO_AES 152 else if (!strcmp(*args, "-aes128")) 153 cipher = EVP_aes_128_cbc(); 154 else if (!strcmp(*args, "-aes192")) 155 cipher = EVP_aes_192_cbc(); 156 else if (!strcmp(*args, "-aes256")) 157 cipher = EVP_aes_256_cbc(); 158 #endif 159 #ifndef OPENSSL_NO_CAMELLIA 160 else if (!strcmp(*args, "-camellia128")) 161 cipher = EVP_camellia_128_cbc(); 162 else if (!strcmp(*args, "-camellia192")) 163 cipher = EVP_camellia_192_cbc(); 164 else if (!strcmp(*args, "-camellia256")) 165 cipher = EVP_camellia_256_cbc(); 166 #endif 167 else if (!strcmp(*args, "-text")) 168 flags |= PKCS7_TEXT; 169 else if (!strcmp(*args, "-nointern")) 170 flags |= PKCS7_NOINTERN; 171 else if (!strcmp(*args, "-noverify")) 172 flags |= PKCS7_NOVERIFY; 173 else if (!strcmp(*args, "-nochain")) 174 flags |= PKCS7_NOCHAIN; 175 else if (!strcmp(*args, "-nocerts")) 176 flags |= PKCS7_NOCERTS; 177 else if (!strcmp(*args, "-noattr")) 178 flags |= PKCS7_NOATTR; 179 else if (!strcmp(*args, "-nodetach")) 180 flags &= ~PKCS7_DETACHED; 181 else if (!strcmp(*args, "-nosmimecap")) 182 flags |= PKCS7_NOSMIMECAP; 183 else if (!strcmp(*args, "-binary")) 184 flags |= PKCS7_BINARY; 185 else if (!strcmp(*args, "-nosigs")) 186 flags |= PKCS7_NOSIGS; 187 else if (!strcmp(*args, "-stream")) 188 indef = 1; 189 else if (!strcmp(*args, "-indef")) 190 indef = 1; 191 else if (!strcmp(*args, "-noindef")) 192 indef = 0; 193 else if (!strcmp(*args, "-nooldmime")) 194 flags |= PKCS7_NOOLDMIMETYPE; 195 else if (!strcmp(*args, "-crlfeol")) 196 flags |= PKCS7_CRLFEOL; 197 #ifndef OPENSSL_NO_ENGINE 198 else if (!strcmp(*args, "-engine")) { 199 if (!args[1]) 200 goto argerr; 201 engine = *++args; 202 } 203 #endif 204 else if (!strcmp(*args, "-passin")) { 205 if (!args[1]) 206 goto argerr; 207 passargin = *++args; 208 } else if (!strcmp(*args, "-to")) { 209 if (!args[1]) 210 goto argerr; 211 to = *++args; 212 } else if (!strcmp(*args, "-from")) { 213 if (!args[1]) 214 goto argerr; 215 from = *++args; 216 } else if (!strcmp(*args, "-subject")) { 217 if (!args[1]) 218 goto argerr; 219 subject = *++args; 220 } else if (!strcmp(*args, "-signer")) { 221 if (!args[1]) 222 goto argerr; 223 /* If previous -signer argument add signer to list */ 224 225 if (signerfile) { 226 if (!sksigners) 227 sksigners = sk_OPENSSL_STRING_new_null(); 228 sk_OPENSSL_STRING_push(sksigners, signerfile); 229 if (!keyfile) 230 keyfile = signerfile; 231 if (!skkeys) 232 skkeys = sk_OPENSSL_STRING_new_null(); 233 sk_OPENSSL_STRING_push(skkeys, keyfile); 234 keyfile = NULL; 235 } 236 signerfile = *++args; 237 } else if (!strcmp(*args, "-recip")) { 238 if (!args[1]) 239 goto argerr; 240 recipfile = *++args; 241 } else if (!strcmp(*args, "-md")) { 242 if (!args[1]) 243 goto argerr; 244 sign_md = EVP_get_digestbyname(*++args); 245 if (sign_md == NULL) { 246 BIO_printf(bio_err, "Unknown digest %s\n", 247 *args); 248 goto argerr; 249 } 250 } else if (!strcmp(*args, "-inkey")) { 251 if (!args[1]) 252 goto argerr; 253 /* If previous -inkey arument add signer to list */ 254 if (keyfile) { 255 if (!signerfile) { 256 BIO_puts(bio_err, "Illegal -inkey without -signer\n"); 257 goto argerr; 258 } 259 if (!sksigners) 260 sksigners = sk_OPENSSL_STRING_new_null(); 261 sk_OPENSSL_STRING_push(sksigners, signerfile); 262 signerfile = NULL; 263 if (!skkeys) 264 skkeys = sk_OPENSSL_STRING_new_null(); 265 sk_OPENSSL_STRING_push(skkeys, keyfile); 266 } 267 keyfile = *++args; 268 } else if (!strcmp(*args, "-keyform")) { 269 if (!args[1]) 270 goto argerr; 271 keyform = str2fmt(*++args); 272 } else if (!strcmp(*args, "-certfile")) { 273 if (!args[1]) 274 goto argerr; 275 certfile = *++args; 276 } else if (!strcmp(*args, "-CAfile")) { 277 if (!args[1]) 278 goto argerr; 279 CAfile = *++args; 280 } else if (!strcmp(*args, "-CApath")) { 281 if (!args[1]) 282 goto argerr; 283 CApath = *++args; 284 } else if (!strcmp(*args, "-in")) { 285 if (!args[1]) 286 goto argerr; 287 infile = *++args; 288 } else if (!strcmp(*args, "-inform")) { 289 if (!args[1]) 290 goto argerr; 291 informat = str2fmt(*++args); 292 } else if (!strcmp(*args, "-outform")) { 293 if (!args[1]) 294 goto argerr; 295 outformat = str2fmt(*++args); 296 } else if (!strcmp(*args, "-out")) { 297 if (!args[1]) 298 goto argerr; 299 outfile = *++args; 300 } else if (!strcmp(*args, "-content")) { 301 if (!args[1]) 302 goto argerr; 303 contfile = *++args; 304 } else if (args_verify(&args, NULL, &badarg, bio_err, &vpm)) 305 continue; 306 else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL) 307 badarg = 1; 308 args++; 309 } 310 311 if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) { 312 BIO_puts(bio_err, "Multiple signers or keys not allowed\n"); 313 goto argerr; 314 } 315 if (operation & SMIME_SIGNERS) { 316 /* Check to see if any final signer needs to be appended */ 317 if (keyfile && !signerfile) { 318 BIO_puts(bio_err, "Illegal -inkey without -signer\n"); 319 goto argerr; 320 } 321 if (signerfile) { 322 if (!sksigners) 323 sksigners = sk_OPENSSL_STRING_new_null(); 324 sk_OPENSSL_STRING_push(sksigners, signerfile); 325 if (!skkeys) 326 skkeys = sk_OPENSSL_STRING_new_null(); 327 if (!keyfile) 328 keyfile = signerfile; 329 sk_OPENSSL_STRING_push(skkeys, keyfile); 330 } 331 if (!sksigners) { 332 BIO_printf(bio_err, "No signer certificate specified\n"); 333 badarg = 1; 334 } 335 signerfile = NULL; 336 keyfile = NULL; 337 } else if (operation == SMIME_DECRYPT) { 338 if (!recipfile && !keyfile) { 339 BIO_printf(bio_err, "No recipient certificate or key specified\n"); 340 badarg = 1; 341 } 342 } else if (operation == SMIME_ENCRYPT) { 343 if (!*args) { 344 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n"); 345 badarg = 1; 346 } 347 } else if (!operation) 348 badarg = 1; 349 350 if (badarg) { 351 argerr: 352 BIO_printf(bio_err, "Usage smime [options] cert.pem ...\n"); 353 BIO_printf(bio_err, "where options are\n"); 354 BIO_printf(bio_err, "-encrypt encrypt message\n"); 355 BIO_printf(bio_err, "-decrypt decrypt encrypted message\n"); 356 BIO_printf(bio_err, "-sign sign message\n"); 357 BIO_printf(bio_err, "-verify verify signed message\n"); 358 BIO_printf(bio_err, "-pk7out output PKCS#7 structure\n"); 359 #ifndef OPENSSL_NO_DES 360 BIO_printf(bio_err, "-des3 encrypt with triple DES\n"); 361 BIO_printf(bio_err, "-des encrypt with DES\n"); 362 #endif 363 #ifndef OPENSSL_NO_RC2 364 BIO_printf(bio_err, "-rc2-40 encrypt with RC2-40 (default)\n"); 365 BIO_printf(bio_err, "-rc2-64 encrypt with RC2-64\n"); 366 BIO_printf(bio_err, "-rc2-128 encrypt with RC2-128\n"); 367 #endif 368 #ifndef OPENSSL_NO_AES 369 BIO_printf(bio_err, "-aes128, -aes192, -aes256\n"); 370 BIO_printf(bio_err, " encrypt PEM output with cbc aes\n"); 371 #endif 372 #ifndef OPENSSL_NO_CAMELLIA 373 BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n"); 374 BIO_printf(bio_err, " encrypt PEM output with cbc camellia\n"); 375 #endif 376 BIO_printf(bio_err, "-nointern don't search certificates in message for signer\n"); 377 BIO_printf(bio_err, "-nosigs don't verify message signature\n"); 378 BIO_printf(bio_err, "-noverify don't verify signers certificate\n"); 379 BIO_printf(bio_err, "-nocerts don't include signers certificate when signing\n"); 380 BIO_printf(bio_err, "-nodetach use opaque signing\n"); 381 BIO_printf(bio_err, "-noattr don't include any signed attributes\n"); 382 BIO_printf(bio_err, "-binary don't translate message to text\n"); 383 BIO_printf(bio_err, "-certfile file other certificates file\n"); 384 BIO_printf(bio_err, "-signer file signer certificate file\n"); 385 BIO_printf(bio_err, "-recip file recipient certificate file for decryption\n"); 386 BIO_printf(bio_err, "-in file input file\n"); 387 BIO_printf(bio_err, "-inform arg input format SMIME (default), PEM or DER\n"); 388 BIO_printf(bio_err, "-inkey file input private key (if not signer or recipient)\n"); 389 BIO_printf(bio_err, "-keyform arg input private key format (PEM or ENGINE)\n"); 390 BIO_printf(bio_err, "-out file output file\n"); 391 BIO_printf(bio_err, "-outform arg output format SMIME (default), PEM or DER\n"); 392 BIO_printf(bio_err, "-content file supply or override content for detached signature\n"); 393 BIO_printf(bio_err, "-to addr to address\n"); 394 BIO_printf(bio_err, "-from ad from address\n"); 395 BIO_printf(bio_err, "-subject s subject\n"); 396 BIO_printf(bio_err, "-text include or delete text MIME headers\n"); 397 BIO_printf(bio_err, "-CApath dir trusted certificates directory\n"); 398 BIO_printf(bio_err, "-CAfile file trusted certificates file\n"); 399 BIO_printf(bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n"); 400 BIO_printf(bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n"); 401 #ifndef OPENSSL_NO_ENGINE 402 BIO_printf(bio_err, "-engine e use engine e, possibly a hardware device.\n"); 403 #endif 404 BIO_printf(bio_err, "-passin arg input file pass phrase source\n"); 405 BIO_printf(bio_err, "cert.pem recipient certificate(s) for encryption\n"); 406 goto end; 407 } 408 #ifndef OPENSSL_NO_ENGINE 409 e = setup_engine(bio_err, engine, 0); 410 #endif 411 412 if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) { 413 BIO_printf(bio_err, "Error getting password\n"); 414 goto end; 415 } 416 ret = 2; 417 418 if (!(operation & SMIME_SIGNERS)) 419 flags &= ~PKCS7_DETACHED; 420 421 if (operation & SMIME_OP) { 422 if (outformat == FORMAT_ASN1) 423 outmode = "wb"; 424 } else { 425 if (flags & PKCS7_BINARY) 426 outmode = "wb"; 427 } 428 429 if (operation & SMIME_IP) { 430 if (informat == FORMAT_ASN1) 431 inmode = "rb"; 432 } else { 433 if (flags & PKCS7_BINARY) 434 inmode = "rb"; 435 } 436 437 if (operation == SMIME_ENCRYPT) { 438 if (!cipher) { 439 #ifndef OPENSSL_NO_RC2 440 cipher = EVP_rc2_40_cbc(); 441 #else 442 BIO_printf(bio_err, "No cipher selected\n"); 443 goto end; 444 #endif 445 } 446 encerts = sk_X509_new_null(); 447 while (*args) { 448 if (!(cert = load_cert(bio_err, *args, FORMAT_PEM, 449 NULL, e, "recipient certificate file"))) { 450 goto end; 451 } 452 sk_X509_push(encerts, cert); 453 cert = NULL; 454 args++; 455 } 456 } 457 if (certfile) { 458 if (!(other = load_certs(bio_err, certfile, FORMAT_PEM, NULL, 459 e, "certificate file"))) { 460 ERR_print_errors(bio_err); 461 goto end; 462 } 463 } 464 if (recipfile && (operation == SMIME_DECRYPT)) { 465 if (!(recip = load_cert(bio_err, recipfile, FORMAT_PEM, NULL, 466 e, "recipient certificate file"))) { 467 ERR_print_errors(bio_err); 468 goto end; 469 } 470 } 471 if (operation == SMIME_DECRYPT) { 472 if (!keyfile) 473 keyfile = recipfile; 474 } else if (operation == SMIME_SIGN) { 475 if (!keyfile) 476 keyfile = signerfile; 477 } else 478 keyfile = NULL; 479 480 if (keyfile) { 481 key = load_key(bio_err, keyfile, keyform, 0, passin, e, 482 "signing key file"); 483 if (!key) 484 goto end; 485 } 486 if (infile) { 487 if (!(in = BIO_new_file(infile, inmode))) { 488 BIO_printf(bio_err, 489 "Can't open input file %s\n", infile); 490 goto end; 491 } 492 } else 493 in = BIO_new_fp(stdin, BIO_NOCLOSE); 494 495 if (operation & SMIME_IP) { 496 if (informat == FORMAT_SMIME) 497 p7 = SMIME_read_PKCS7(in, &indata); 498 else if (informat == FORMAT_PEM) 499 p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL); 500 else if (informat == FORMAT_ASN1) 501 p7 = d2i_PKCS7_bio(in, NULL); 502 else { 503 BIO_printf(bio_err, "Bad input format for PKCS#7 file\n"); 504 goto end; 505 } 506 507 if (!p7) { 508 BIO_printf(bio_err, "Error reading S/MIME message\n"); 509 goto end; 510 } 511 if (contfile) { 512 BIO_free(indata); 513 if (!(indata = BIO_new_file(contfile, "rb"))) { 514 BIO_printf(bio_err, "Can't read content file %s\n", contfile); 515 goto end; 516 } 517 } 518 } 519 if (outfile) { 520 if (!(out = BIO_new_file(outfile, outmode))) { 521 BIO_printf(bio_err, 522 "Can't open output file %s\n", outfile); 523 goto end; 524 } 525 } else { 526 out = BIO_new_fp(stdout, BIO_NOCLOSE); 527 } 528 529 if (operation == SMIME_VERIFY) { 530 if (!(store = setup_verify(bio_err, CAfile, CApath))) 531 goto end; 532 X509_STORE_set_verify_cb(store, smime_cb); 533 if (vpm) 534 X509_STORE_set1_param(store, vpm); 535 } 536 ret = 3; 537 538 if (operation == SMIME_ENCRYPT) { 539 if (indef) 540 flags |= PKCS7_STREAM; 541 p7 = PKCS7_encrypt(encerts, in, cipher, flags); 542 } else if (operation & SMIME_SIGNERS) { 543 int i; 544 /* 545 * If detached data content we only enable streaming if 546 * S/MIME output format. 547 */ 548 if (operation == SMIME_SIGN) { 549 if (flags & PKCS7_DETACHED) { 550 if (outformat == FORMAT_SMIME) 551 flags |= PKCS7_STREAM; 552 } else if (indef) 553 flags |= PKCS7_STREAM; 554 flags |= PKCS7_PARTIAL; 555 p7 = PKCS7_sign(NULL, NULL, other, in, flags); 556 if (!p7) 557 goto end; 558 } else 559 flags |= PKCS7_REUSE_DIGEST; 560 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) { 561 signerfile = sk_OPENSSL_STRING_value(sksigners, i); 562 keyfile = sk_OPENSSL_STRING_value(skkeys, i); 563 signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL, 564 e, "signer certificate"); 565 if (!signer) 566 goto end; 567 key = load_key(bio_err, keyfile, keyform, 0, passin, e, 568 "signing key file"); 569 if (!key) 570 goto end; 571 if (!PKCS7_sign_add_signer(p7, signer, key, 572 sign_md, flags)) 573 goto end; 574 X509_free(signer); 575 signer = NULL; 576 EVP_PKEY_free(key); 577 key = NULL; 578 } 579 /* If not streaming or resigning finalize structure */ 580 if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) { 581 if (!PKCS7_final(p7, in, flags)) 582 goto end; 583 } 584 } 585 if (!p7) { 586 BIO_printf(bio_err, "Error creating PKCS#7 structure\n"); 587 goto end; 588 } 589 ret = 4; 590 if (operation == SMIME_DECRYPT) { 591 if (!PKCS7_decrypt(p7, key, recip, out, flags)) { 592 BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n"); 593 goto end; 594 } 595 } else if (operation == SMIME_VERIFY) { 596 STACK_OF(X509) * signers; 597 if (PKCS7_verify(p7, other, store, indata, out, flags)) 598 BIO_printf(bio_err, "Verification successful\n"); 599 else { 600 BIO_printf(bio_err, "Verification failure\n"); 601 goto end; 602 } 603 signers = PKCS7_get0_signers(p7, other, flags); 604 if (!save_certs(signerfile, signers)) { 605 BIO_printf(bio_err, "Error writing signers to %s\n", 606 signerfile); 607 ret = 5; 608 goto end; 609 } 610 sk_X509_free(signers); 611 } else if (operation == SMIME_PK7OUT) 612 PEM_write_bio_PKCS7(out, p7); 613 else { 614 if (to) 615 BIO_printf(out, "To: %s\n", to); 616 if (from) 617 BIO_printf(out, "From: %s\n", from); 618 if (subject) 619 BIO_printf(out, "Subject: %s\n", subject); 620 if (outformat == FORMAT_SMIME) { 621 if (operation == SMIME_RESIGN) 622 SMIME_write_PKCS7(out, p7, indata, flags); 623 else 624 SMIME_write_PKCS7(out, p7, in, flags); 625 } else if (outformat == FORMAT_PEM) 626 PEM_write_bio_PKCS7_stream(out, p7, in, flags); 627 else if (outformat == FORMAT_ASN1) 628 i2d_PKCS7_bio_stream(out, p7, in, flags); 629 else { 630 BIO_printf(bio_err, "Bad output format for PKCS#7 file\n"); 631 goto end; 632 } 633 } 634 ret = 0; 635 end: 636 if (ret) 637 ERR_print_errors(bio_err); 638 sk_X509_pop_free(encerts, X509_free); 639 sk_X509_pop_free(other, X509_free); 640 if (vpm) 641 X509_VERIFY_PARAM_free(vpm); 642 if (sksigners) 643 sk_OPENSSL_STRING_free(sksigners); 644 if (skkeys) 645 sk_OPENSSL_STRING_free(skkeys); 646 X509_STORE_free(store); 647 X509_free(cert); 648 X509_free(recip); 649 X509_free(signer); 650 EVP_PKEY_free(key); 651 PKCS7_free(p7); 652 BIO_free(in); 653 BIO_free(indata); 654 BIO_free_all(out); 655 free(passin); 656 657 return (ret); 658 } 659 660 static int 661 save_certs(char *signerfile, STACK_OF(X509) * signers) 662 { 663 int i; 664 BIO *tmp; 665 if (!signerfile) 666 return 1; 667 tmp = BIO_new_file(signerfile, "w"); 668 if (!tmp) 669 return 0; 670 for (i = 0; i < sk_X509_num(signers); i++) 671 PEM_write_bio_X509(tmp, sk_X509_value(signers, i)); 672 BIO_free(tmp); 673 return 1; 674 } 675 676 677 /* Minimal callback just to output policy info (if any) */ 678 679 static int 680 smime_cb(int ok, X509_STORE_CTX * ctx) 681 { 682 int error; 683 684 error = X509_STORE_CTX_get_error(ctx); 685 686 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY) 687 && ((error != X509_V_OK) || (ok != 2))) 688 return ok; 689 690 policies_print(NULL, ctx); 691 692 return ok; 693 694 } 695