1 /* smime.c */ 2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 3 * project 1999. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 1999 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 2005 Sun Microsystems, Inc. All rights reserved. 61 * Use is subject to license terms. 62 */ 63 #pragma ident "%Z%%M% %I% %E% SMI" 64 65 /* S/MIME utility function */ 66 67 #include <stdio.h> 68 #include <string.h> 69 #include "apps.h" 70 #include <openssl/crypto.h> 71 #include <openssl/pem.h> 72 #include <openssl/err.h> 73 74 #ifdef SOLARIS_OPENSSL 75 extern int SUNWcry_installed; 76 #endif 77 78 #undef PROG 79 #define PROG smime_main 80 static int save_certs(char *signerfile, STACK_OF(X509) *signers); 81 82 #define SMIME_OP 0x10 83 #define SMIME_ENCRYPT (1 | SMIME_OP) 84 #define SMIME_DECRYPT 2 85 #define SMIME_SIGN (3 | SMIME_OP) 86 #define SMIME_VERIFY 4 87 #define SMIME_PK7OUT 5 88 89 int MAIN(int, char **); 90 91 int MAIN(int argc, char **argv) 92 { 93 ENGINE *e = NULL; 94 int operation = 0; 95 int ret = 0; 96 char **args; 97 char *inmode = "r", *outmode = "w"; 98 char *infile = NULL, *outfile = NULL; 99 char *signerfile = NULL, *recipfile = NULL; 100 char *certfile = NULL, *keyfile = NULL, *contfile=NULL; 101 const EVP_CIPHER *cipher = NULL; 102 PKCS7 *p7 = NULL; 103 X509_STORE *store = NULL; 104 X509 *cert = NULL, *recip = NULL, *signer = NULL; 105 EVP_PKEY *key = NULL; 106 STACK_OF(X509) *encerts = NULL, *other = NULL; 107 BIO *in = NULL, *out = NULL, *indata = NULL; 108 int badarg = 0; 109 int flags = PKCS7_DETACHED, store_flags = 0; 110 char *to = NULL, *from = NULL, *subject = NULL; 111 char *CAfile = NULL, *CApath = NULL; 112 char *passargin = NULL, *passin = NULL; 113 char *inrand = NULL; 114 int need_rand = 0; 115 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME; 116 int keyform = FORMAT_PEM; 117 #ifndef OPENSSL_NO_ENGINE 118 char *engine=NULL; 119 #endif 120 121 args = argv + 1; 122 ret = 1; 123 124 apps_startup(); 125 126 if (bio_err == NULL) 127 if ((bio_err = BIO_new(BIO_s_file())) != NULL) 128 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT); 129 130 if (!load_config(bio_err, NULL)) 131 goto end; 132 133 while (!badarg && *args && *args[0] == '-') { 134 if (!strcmp (*args, "-encrypt")) operation = SMIME_ENCRYPT; 135 else if (!strcmp (*args, "-decrypt")) operation = SMIME_DECRYPT; 136 else if (!strcmp (*args, "-sign")) operation = SMIME_SIGN; 137 else if (!strcmp (*args, "-verify")) operation = SMIME_VERIFY; 138 else if (!strcmp (*args, "-pk7out")) operation = SMIME_PK7OUT; 139 #ifndef OPENSSL_NO_DES 140 else if (!strcmp (*args, "-des3")) 141 cipher = EVP_des_ede3_cbc(); 142 else if (!strcmp (*args, "-des")) 143 cipher = EVP_des_cbc(); 144 #endif 145 #ifndef OPENSSL_NO_RC2 146 else if (!strcmp (*args, "-rc2-40")) 147 cipher = EVP_rc2_40_cbc(); 148 else if (!strcmp (*args, "-rc2-128")) 149 cipher = EVP_rc2_cbc(); 150 else if (!strcmp (*args, "-rc2-64")) 151 cipher = EVP_rc2_64_cbc(); 152 #endif 153 #ifndef OPENSSL_NO_AES 154 else if (!strcmp(*args,"-aes128")) 155 cipher = EVP_aes_128_cbc(); 156 #ifdef SOLARIS_OPENSSL 157 else if (strcmp(*argv,"-aes192") == 0) 158 { 159 if (!SUNWcry_installed) 160 { 161 BIO_printf(bio_err,"SUNWcry not installed.\n"); 162 goto end; 163 } 164 } 165 else if (strcmp(*argv,"-aes256") == 0) 166 { 167 if (!SUNWcry_installed) 168 { 169 BIO_printf(bio_err,"SUNWcry not installed.\n"); 170 goto end; 171 } 172 } 173 #else 174 else if (!strcmp(*args,"-aes192")) 175 cipher = EVP_aes_192_cbc(); 176 else if (!strcmp(*args,"-aes256")) 177 cipher = EVP_aes_256_cbc(); 178 #endif 179 #endif 180 else if (!strcmp (*args, "-text")) 181 flags |= PKCS7_TEXT; 182 else if (!strcmp (*args, "-nointern")) 183 flags |= PKCS7_NOINTERN; 184 else if (!strcmp (*args, "-noverify")) 185 flags |= PKCS7_NOVERIFY; 186 else if (!strcmp (*args, "-nochain")) 187 flags |= PKCS7_NOCHAIN; 188 else if (!strcmp (*args, "-nocerts")) 189 flags |= PKCS7_NOCERTS; 190 else if (!strcmp (*args, "-noattr")) 191 flags |= PKCS7_NOATTR; 192 else if (!strcmp (*args, "-nodetach")) 193 flags &= ~PKCS7_DETACHED; 194 else if (!strcmp (*args, "-nosmimecap")) 195 flags |= PKCS7_NOSMIMECAP; 196 else if (!strcmp (*args, "-binary")) 197 flags |= PKCS7_BINARY; 198 else if (!strcmp (*args, "-nosigs")) 199 flags |= PKCS7_NOSIGS; 200 else if (!strcmp (*args, "-nooldmime")) 201 flags |= PKCS7_NOOLDMIMETYPE; 202 else if (!strcmp (*args, "-crlfeol")) 203 flags |= PKCS7_CRLFEOL; 204 else if (!strcmp (*args, "-crl_check")) 205 store_flags |= X509_V_FLAG_CRL_CHECK; 206 else if (!strcmp (*args, "-crl_check_all")) 207 store_flags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL; 208 else if (!strcmp(*args,"-rand")) { 209 if (args[1]) { 210 args++; 211 inrand = *args; 212 } else badarg = 1; 213 need_rand = 1; 214 #ifndef OPENSSL_NO_ENGINE 215 } else if (!strcmp(*args,"-engine")) { 216 if (args[1]) { 217 args++; 218 engine = *args; 219 } else badarg = 1; 220 #endif 221 } else if (!strcmp(*args,"-passin")) { 222 if (args[1]) { 223 args++; 224 passargin = *args; 225 } else badarg = 1; 226 } else if (!strcmp (*args, "-to")) { 227 if (args[1]) { 228 args++; 229 to = *args; 230 } else badarg = 1; 231 } else if (!strcmp (*args, "-from")) { 232 if (args[1]) { 233 args++; 234 from = *args; 235 } else badarg = 1; 236 } else if (!strcmp (*args, "-subject")) { 237 if (args[1]) { 238 args++; 239 subject = *args; 240 } else badarg = 1; 241 } else if (!strcmp (*args, "-signer")) { 242 if (args[1]) { 243 args++; 244 signerfile = *args; 245 } else badarg = 1; 246 } else if (!strcmp (*args, "-recip")) { 247 if (args[1]) { 248 args++; 249 recipfile = *args; 250 } else badarg = 1; 251 } else if (!strcmp (*args, "-inkey")) { 252 if (args[1]) { 253 args++; 254 keyfile = *args; 255 } else badarg = 1; 256 } else if (!strcmp (*args, "-keyform")) { 257 if (args[1]) { 258 args++; 259 keyform = str2fmt(*args); 260 } else badarg = 1; 261 } else if (!strcmp (*args, "-certfile")) { 262 if (args[1]) { 263 args++; 264 certfile = *args; 265 } else badarg = 1; 266 } else if (!strcmp (*args, "-CAfile")) { 267 if (args[1]) { 268 args++; 269 CAfile = *args; 270 } else badarg = 1; 271 } else if (!strcmp (*args, "-CApath")) { 272 if (args[1]) { 273 args++; 274 CApath = *args; 275 } else badarg = 1; 276 } else if (!strcmp (*args, "-in")) { 277 if (args[1]) { 278 args++; 279 infile = *args; 280 } else badarg = 1; 281 } else if (!strcmp (*args, "-inform")) { 282 if (args[1]) { 283 args++; 284 informat = str2fmt(*args); 285 } else badarg = 1; 286 } else if (!strcmp (*args, "-outform")) { 287 if (args[1]) { 288 args++; 289 outformat = str2fmt(*args); 290 } else badarg = 1; 291 } else if (!strcmp (*args, "-out")) { 292 if (args[1]) { 293 args++; 294 outfile = *args; 295 } else badarg = 1; 296 } else if (!strcmp (*args, "-content")) { 297 if (args[1]) { 298 args++; 299 contfile = *args; 300 } else badarg = 1; 301 } else badarg = 1; 302 args++; 303 } 304 305 if(operation == SMIME_SIGN) { 306 if(!signerfile) { 307 BIO_printf(bio_err, "No signer certificate specified\n"); 308 badarg = 1; 309 } 310 need_rand = 1; 311 } else if(operation == SMIME_DECRYPT) { 312 if(!recipfile) { 313 BIO_printf(bio_err, "No recipient certificate and key specified\n"); 314 badarg = 1; 315 } 316 } else if(operation == SMIME_ENCRYPT) { 317 if(!*args) { 318 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n"); 319 badarg = 1; 320 } 321 need_rand = 1; 322 } else if(!operation) badarg = 1; 323 324 if (badarg) { 325 BIO_printf (bio_err, "Usage smime [options] cert.pem ...\n"); 326 BIO_printf (bio_err, "where options are\n"); 327 BIO_printf (bio_err, "-encrypt encrypt message\n"); 328 BIO_printf (bio_err, "-decrypt decrypt encrypted message\n"); 329 BIO_printf (bio_err, "-sign sign message\n"); 330 BIO_printf (bio_err, "-verify verify signed message\n"); 331 BIO_printf (bio_err, "-pk7out output PKCS#7 structure\n"); 332 #ifndef OPENSSL_NO_DES 333 BIO_printf (bio_err, "-des3 encrypt with triple DES\n"); 334 BIO_printf (bio_err, "-des encrypt with DES\n"); 335 #endif 336 #ifndef OPENSSL_NO_RC2 337 BIO_printf (bio_err, "-rc2-40 encrypt with RC2-40 (default)\n"); 338 BIO_printf (bio_err, "-rc2-64 encrypt with RC2-64\n"); 339 BIO_printf (bio_err, "-rc2-128 encrypt with RC2-128\n"); 340 #endif 341 #ifndef OPENSSL_NO_AES 342 BIO_printf (bio_err, "-aes128, -aes192, -aes256\n"); 343 BIO_printf (bio_err, " encrypt PEM output with cbc aes\n"); 344 #endif 345 BIO_printf (bio_err, "-nointern don't search certificates in message for signer\n"); 346 BIO_printf (bio_err, "-nosigs don't verify message signature\n"); 347 BIO_printf (bio_err, "-noverify don't verify signers certificate\n"); 348 BIO_printf (bio_err, "-nocerts don't include signers certificate when signing\n"); 349 BIO_printf (bio_err, "-nodetach use opaque signing\n"); 350 BIO_printf (bio_err, "-noattr don't include any signed attributes\n"); 351 BIO_printf (bio_err, "-binary don't translate message to text\n"); 352 BIO_printf (bio_err, "-certfile file other certificates file\n"); 353 BIO_printf (bio_err, "-signer file signer certificate file\n"); 354 BIO_printf (bio_err, "-recip file recipient certificate file for decryption\n"); 355 BIO_printf (bio_err, "-in file input file\n"); 356 BIO_printf (bio_err, "-inform arg input format SMIME (default), PEM or DER\n"); 357 BIO_printf (bio_err, "-inkey file input private key (if not signer or recipient)\n"); 358 BIO_printf (bio_err, "-keyform arg input private key format (PEM or ENGINE)\n"); 359 BIO_printf (bio_err, "-out file output file\n"); 360 BIO_printf (bio_err, "-outform arg output format SMIME (default), PEM or DER\n"); 361 BIO_printf (bio_err, "-content file supply or override content for detached signature\n"); 362 BIO_printf (bio_err, "-to addr to address\n"); 363 BIO_printf (bio_err, "-from ad from address\n"); 364 BIO_printf (bio_err, "-subject s subject\n"); 365 BIO_printf (bio_err, "-text include or delete text MIME headers\n"); 366 BIO_printf (bio_err, "-CApath dir trusted certificates directory\n"); 367 BIO_printf (bio_err, "-CAfile file trusted certificates file\n"); 368 BIO_printf (bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n"); 369 BIO_printf (bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n"); 370 #ifndef OPENSSL_NO_ENGINE 371 BIO_printf (bio_err, "-engine e use engine e, possibly a hardware device.\n"); 372 #endif 373 BIO_printf (bio_err, "-passin arg input file pass phrase source\n"); 374 BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR); 375 BIO_printf(bio_err, " load the file (or the files in the directory) into\n"); 376 BIO_printf(bio_err, " the random number generator\n"); 377 BIO_printf (bio_err, "cert.pem recipient certificate(s) for encryption\n"); 378 goto end; 379 } 380 381 #ifndef OPENSSL_NO_ENGINE 382 e = setup_engine(bio_err, engine, 0); 383 #endif 384 385 if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) { 386 BIO_printf(bio_err, "Error getting password\n"); 387 goto end; 388 } 389 390 if (need_rand) { 391 app_RAND_load_file(NULL, bio_err, (inrand != NULL)); 392 if (inrand != NULL) 393 BIO_printf(bio_err,"%ld semi-random bytes loaded\n", 394 app_RAND_load_files(inrand)); 395 } 396 397 ret = 2; 398 399 if(operation != SMIME_SIGN) flags &= ~PKCS7_DETACHED; 400 401 if(operation & SMIME_OP) { 402 if(flags & PKCS7_BINARY) inmode = "rb"; 403 if(outformat == FORMAT_ASN1) outmode = "wb"; 404 } else { 405 if(flags & PKCS7_BINARY) outmode = "wb"; 406 if(informat == FORMAT_ASN1) inmode = "rb"; 407 } 408 409 if(operation == SMIME_ENCRYPT) { 410 if (!cipher) { 411 #ifndef OPENSSL_NO_RC2 412 cipher = EVP_rc2_40_cbc(); 413 #else 414 BIO_printf(bio_err, "No cipher selected\n"); 415 goto end; 416 #endif 417 } 418 encerts = sk_X509_new_null(); 419 while (*args) { 420 if(!(cert = load_cert(bio_err,*args,FORMAT_PEM, 421 NULL, e, "recipient certificate file"))) { 422 #if 0 /* An appropriate message is already printed */ 423 BIO_printf(bio_err, "Can't read recipient certificate file %s\n", *args); 424 #endif 425 goto end; 426 } 427 sk_X509_push(encerts, cert); 428 cert = NULL; 429 args++; 430 } 431 } 432 433 if(signerfile && (operation == SMIME_SIGN)) { 434 if(!(signer = load_cert(bio_err,signerfile,FORMAT_PEM, NULL, 435 e, "signer certificate"))) { 436 #if 0 /* An appropri message has already been printed */ 437 BIO_printf(bio_err, "Can't read signer certificate file %s\n", signerfile); 438 #endif 439 goto end; 440 } 441 } 442 443 if(certfile) { 444 if(!(other = load_certs(bio_err,certfile,FORMAT_PEM, NULL, 445 e, "certificate file"))) { 446 #if 0 /* An appropriate message has already been printed */ 447 BIO_printf(bio_err, "Can't read certificate file %s\n", certfile); 448 #endif 449 ERR_print_errors(bio_err); 450 goto end; 451 } 452 } 453 454 if(recipfile && (operation == SMIME_DECRYPT)) { 455 if(!(recip = load_cert(bio_err,recipfile,FORMAT_PEM,NULL, 456 e, "recipient certificate file"))) { 457 #if 0 /* An appropriate message has alrady been printed */ 458 BIO_printf(bio_err, "Can't read recipient certificate file %s\n", recipfile); 459 #endif 460 ERR_print_errors(bio_err); 461 goto end; 462 } 463 } 464 465 if(operation == SMIME_DECRYPT) { 466 if(!keyfile) keyfile = recipfile; 467 } else if(operation == SMIME_SIGN) { 468 if(!keyfile) keyfile = signerfile; 469 } else keyfile = NULL; 470 471 if(keyfile) { 472 key = load_key(bio_err, keyfile, keyform, 0, passin, e, 473 "signing key file"); 474 if (!key) { 475 goto end; 476 } 477 } 478 479 if (infile) { 480 if (!(in = BIO_new_file(infile, inmode))) { 481 BIO_printf (bio_err, 482 "Can't open input file %s\n", infile); 483 goto end; 484 } 485 } else in = BIO_new_fp(stdin, BIO_NOCLOSE); 486 487 if (outfile) { 488 if (!(out = BIO_new_file(outfile, outmode))) { 489 BIO_printf (bio_err, 490 "Can't open output file %s\n", outfile); 491 goto end; 492 } 493 } else { 494 out = BIO_new_fp(stdout, BIO_NOCLOSE); 495 #ifdef OPENSSL_SYS_VMS 496 { 497 BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 498 out = BIO_push(tmpbio, out); 499 } 500 #endif 501 } 502 503 if(operation == SMIME_VERIFY) { 504 if(!(store = setup_verify(bio_err, CAfile, CApath))) goto end; 505 X509_STORE_set_flags(store, store_flags); 506 } 507 508 509 ret = 3; 510 511 if(operation == SMIME_ENCRYPT) { 512 p7 = PKCS7_encrypt(encerts, in, cipher, flags); 513 } else if(operation == SMIME_SIGN) { 514 p7 = PKCS7_sign(signer, key, other, in, flags); 515 if (BIO_reset(in) != 0 && (flags & PKCS7_DETACHED)) { 516 BIO_printf(bio_err, "Can't rewind input file\n"); 517 goto end; 518 } 519 } else { 520 if(informat == FORMAT_SMIME) 521 p7 = SMIME_read_PKCS7(in, &indata); 522 else if(informat == FORMAT_PEM) 523 p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL); 524 else if(informat == FORMAT_ASN1) 525 p7 = d2i_PKCS7_bio(in, NULL); 526 else { 527 BIO_printf(bio_err, "Bad input format for PKCS#7 file\n"); 528 goto end; 529 } 530 531 if(!p7) { 532 BIO_printf(bio_err, "Error reading S/MIME message\n"); 533 goto end; 534 } 535 if(contfile) { 536 BIO_free(indata); 537 if(!(indata = BIO_new_file(contfile, "rb"))) { 538 BIO_printf(bio_err, "Can't read content file %s\n", contfile); 539 goto end; 540 } 541 } 542 } 543 544 if(!p7) { 545 BIO_printf(bio_err, "Error creating PKCS#7 structure\n"); 546 goto end; 547 } 548 549 ret = 4; 550 if(operation == SMIME_DECRYPT) { 551 if(!PKCS7_decrypt(p7, key, recip, out, flags)) { 552 BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n"); 553 goto end; 554 } 555 } else if(operation == SMIME_VERIFY) { 556 STACK_OF(X509) *signers; 557 if(PKCS7_verify(p7, other, store, indata, out, flags)) { 558 BIO_printf(bio_err, "Verification successful\n"); 559 } else { 560 BIO_printf(bio_err, "Verification failure\n"); 561 goto end; 562 } 563 signers = PKCS7_get0_signers(p7, other, flags); 564 if(!save_certs(signerfile, signers)) { 565 BIO_printf(bio_err, "Error writing signers to %s\n", 566 signerfile); 567 ret = 5; 568 goto end; 569 } 570 sk_X509_free(signers); 571 } else if(operation == SMIME_PK7OUT) { 572 PEM_write_bio_PKCS7(out, p7); 573 } else { 574 if(to) BIO_printf(out, "To: %s\n", to); 575 if(from) BIO_printf(out, "From: %s\n", from); 576 if(subject) BIO_printf(out, "Subject: %s\n", subject); 577 if(outformat == FORMAT_SMIME) 578 SMIME_write_PKCS7(out, p7, in, flags); 579 else if(outformat == FORMAT_PEM) 580 PEM_write_bio_PKCS7(out,p7); 581 else if(outformat == FORMAT_ASN1) 582 i2d_PKCS7_bio(out,p7); 583 else { 584 BIO_printf(bio_err, "Bad output format for PKCS#7 file\n"); 585 goto end; 586 } 587 } 588 ret = 0; 589 end: 590 if (need_rand) 591 app_RAND_write_file(NULL, bio_err); 592 if(ret) ERR_print_errors(bio_err); 593 sk_X509_pop_free(encerts, X509_free); 594 sk_X509_pop_free(other, X509_free); 595 X509_STORE_free(store); 596 X509_free(cert); 597 X509_free(recip); 598 X509_free(signer); 599 EVP_PKEY_free(key); 600 PKCS7_free(p7); 601 BIO_free(in); 602 BIO_free(indata); 603 BIO_free_all(out); 604 if(passin) OPENSSL_free(passin); 605 return (ret); 606 } 607 608 static int save_certs(char *signerfile, STACK_OF(X509) *signers) 609 { 610 int i; 611 BIO *tmp; 612 if(!signerfile) return 1; 613 tmp = BIO_new_file(signerfile, "w"); 614 if(!tmp) return 0; 615 for(i = 0; i < sk_X509_num(signers); i++) 616 PEM_write_bio_X509(tmp, sk_X509_value(signers, i)); 617 BIO_free(tmp); 618 return 1; 619 } 620 621