smime.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*
  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. /* S/MIME utility function */
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include "apps.h"
  62. #include <openssl/crypto.h>
  63. #include <openssl/pem.h>
  64. #include <openssl/err.h>
  65. #include <openssl/x509_vfy.h>
  66. #include <openssl/x509v3.h>
  67. static int save_certs(char *signerfile, STACK_OF(X509) *signers);
  68. static int smime_cb(int ok, X509_STORE_CTX *ctx);
  69. #define SMIME_OP 0x10
  70. #define SMIME_IP 0x20
  71. #define SMIME_SIGNERS 0x40
  72. #define SMIME_ENCRYPT (1 | SMIME_OP)
  73. #define SMIME_DECRYPT (2 | SMIME_IP)
  74. #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
  75. #define SMIME_VERIFY (4 | SMIME_IP)
  76. #define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP)
  77. #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
  78. typedef enum OPTION_choice {
  79. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  80. OPT_ENCRYPT, OPT_DECRYPT, OPT_SIGN, OPT_RESIGN, OPT_VERIFY,
  81. OPT_PK7OUT, OPT_TEXT, OPT_NOINTERN, OPT_NOVERIFY, OPT_NOCHAIN,
  82. OPT_NOCERTS, OPT_NOATTR, OPT_NODETACH, OPT_NOSMIMECAP,
  83. OPT_BINARY, OPT_NOSIGS, OPT_STREAM, OPT_INDEF, OPT_NOINDEF,
  84. OPT_NOOLDMIME, OPT_CRLFEOL, OPT_RAND, OPT_ENGINE, OPT_PASSIN,
  85. OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP, OPT_MD,
  86. OPT_CIPHER, OPT_INKEY, OPT_KEYFORM, OPT_CERTFILE, OPT_CAFILE,
  87. OPT_V_ENUM,
  88. OPT_CAPATH, OPT_NOCAFILE, OPT_NOCAPATH, OPT_IN, OPT_INFORM, OPT_OUT,
  89. OPT_OUTFORM, OPT_CONTENT
  90. } OPTION_CHOICE;
  91. OPTIONS smime_options[] = {
  92. {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"},
  93. {OPT_HELP_STR, 1, '-',
  94. " cert.pem... recipient certs for encryption\n"},
  95. {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
  96. {"help", OPT_HELP, '-', "Display this summary"},
  97. {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
  98. {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
  99. {"sign", OPT_SIGN, '-', "Sign message"},
  100. {"verify", OPT_VERIFY, '-', "Verify signed message"},
  101. {"pk7out", OPT_PK7OUT, '-', "Output PKCS#7 structure"},
  102. {"nointern", OPT_NOINTERN, '-',
  103. "Don't search certificates in message for signer"},
  104. {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"},
  105. {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"},
  106. {"nocerts", OPT_NOCERTS, '-',
  107. "Don't include signers certificate when signing"},
  108. {"nodetach", OPT_NODETACH, '-', "Use opaque signing"},
  109. {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"},
  110. {"binary", OPT_BINARY, '-', "Don't translate message to text"},
  111. {"certfile", OPT_CERTFILE, '<', "Other certificates file"},
  112. {"signer", OPT_SIGNER, '<', "Signer certificate file"},
  113. {"recip", OPT_RECIP, '<', "Recipient certificate file for decryption"},
  114. {"in", OPT_IN, '<', "Input file"},
  115. {"inform", OPT_INFORM, 'F', "Input format SMIME (default), PEM or DER"},
  116. {"inkey", OPT_INKEY, '<',
  117. "Input private key (if not signer or recipient)"},
  118. {"keyform", OPT_KEYFORM, 'f', "Input private key format (PEM or ENGINE)"},
  119. {"out", OPT_OUT, '>', "Output file"},
  120. {"outform", OPT_OUTFORM, 'F',
  121. "Output format SMIME (default), PEM or DER"},
  122. {"content", OPT_CONTENT, '<',
  123. "Supply or override content for detached signature"},
  124. {"to", OPT_TO, 's', "To address"},
  125. {"from", OPT_FROM, 's', "From address"},
  126. {"subject", OPT_SUBJECT, 's', "Subject"},
  127. {"text", OPT_TEXT, '-', "Include or delete text MIME headers"},
  128. {"CApath", OPT_CAPATH, '/', "Trusted certificates directory"},
  129. {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
  130. {"no-CAfile", OPT_NOCAFILE, '-',
  131. "Do not load the default certificates file"},
  132. {"no-CApath", OPT_NOCAPATH, '-',
  133. "Do not load certificates from the default certificates directory"},
  134. {"resign", OPT_RESIGN, '-'},
  135. {"nochain", OPT_NOCHAIN, '-'},
  136. {"nosmimecap", OPT_NOSMIMECAP, '-'},
  137. {"stream", OPT_STREAM, '-'},
  138. {"indef", OPT_INDEF, '-'},
  139. {"noindef", OPT_NOINDEF, '-'},
  140. {"nooldmime", OPT_NOOLDMIME, '-'},
  141. {"crlfeol", OPT_CRLFEOL, '-'},
  142. {"rand", OPT_RAND, 's',
  143. "Load the file(s) into the random number generator"},
  144. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  145. {"md", OPT_MD, 's'},
  146. {"", OPT_CIPHER, '-', "Any supported cipher"},
  147. OPT_V_OPTIONS,
  148. #ifndef OPENSSL_NO_ENGINE
  149. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  150. #endif
  151. {NULL}
  152. };
  153. int smime_main(int argc, char **argv)
  154. {
  155. BIO *in = NULL, *out = NULL, *indata = NULL;
  156. EVP_PKEY *key = NULL;
  157. PKCS7 *p7 = NULL;
  158. STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
  159. STACK_OF(X509) *encerts = NULL, *other = NULL;
  160. X509 *cert = NULL, *recip = NULL, *signer = NULL;
  161. X509_STORE *store = NULL;
  162. X509_VERIFY_PARAM *vpm = NULL;
  163. const EVP_CIPHER *cipher = NULL;
  164. const EVP_MD *sign_md = NULL;
  165. char *CAfile = NULL, *CApath = NULL, *inrand = NULL;
  166. char *certfile = NULL, *keyfile = NULL, *contfile = NULL, *prog;
  167. char *infile = NULL, *outfile = NULL, *signerfile = NULL, *recipfile =
  168. NULL;
  169. char *passinarg = NULL, *passin = NULL, *to = NULL, *from =
  170. NULL, *subject = NULL;
  171. OPTION_CHOICE o;
  172. int noCApath = 0, noCAfile = 0;
  173. int flags = PKCS7_DETACHED, operation = 0, ret = 0, need_rand = 0, indef =
  174. 0;
  175. int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform =
  176. FORMAT_PEM;
  177. int vpmtouched = 0, rv = 0;
  178. ENGINE *e = NULL;
  179. if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
  180. return 1;
  181. prog = opt_init(argc, argv, smime_options);
  182. while ((o = opt_next()) != OPT_EOF) {
  183. switch (o) {
  184. case OPT_EOF:
  185. case OPT_ERR:
  186. opthelp:
  187. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  188. goto end;
  189. case OPT_HELP:
  190. opt_help(smime_options);
  191. ret = 0;
  192. goto end;
  193. case OPT_INFORM:
  194. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  195. goto opthelp;
  196. break;
  197. case OPT_IN:
  198. infile = opt_arg();
  199. break;
  200. case OPT_OUTFORM:
  201. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  202. goto opthelp;
  203. break;
  204. case OPT_OUT:
  205. outfile = opt_arg();
  206. break;
  207. case OPT_ENCRYPT:
  208. operation = SMIME_ENCRYPT;
  209. break;
  210. case OPT_DECRYPT:
  211. operation = SMIME_DECRYPT;
  212. break;
  213. case OPT_SIGN:
  214. operation = SMIME_SIGN;
  215. break;
  216. case OPT_RESIGN:
  217. operation = SMIME_RESIGN;
  218. break;
  219. case OPT_VERIFY:
  220. operation = SMIME_VERIFY;
  221. break;
  222. case OPT_PK7OUT:
  223. operation = SMIME_PK7OUT;
  224. break;
  225. case OPT_TEXT:
  226. flags |= PKCS7_TEXT;
  227. break;
  228. case OPT_NOINTERN:
  229. flags |= PKCS7_NOINTERN;
  230. break;
  231. case OPT_NOVERIFY:
  232. flags |= PKCS7_NOVERIFY;
  233. break;
  234. case OPT_NOCHAIN:
  235. flags |= PKCS7_NOCHAIN;
  236. break;
  237. case OPT_NOCERTS:
  238. flags |= PKCS7_NOCERTS;
  239. break;
  240. case OPT_NOATTR:
  241. flags |= PKCS7_NOATTR;
  242. break;
  243. case OPT_NODETACH:
  244. flags &= ~PKCS7_DETACHED;
  245. break;
  246. case OPT_NOSMIMECAP:
  247. flags |= PKCS7_NOSMIMECAP;
  248. break;
  249. case OPT_BINARY:
  250. flags |= PKCS7_BINARY;
  251. break;
  252. case OPT_NOSIGS:
  253. flags |= PKCS7_NOSIGS;
  254. break;
  255. case OPT_STREAM:
  256. case OPT_INDEF:
  257. indef = 1;
  258. break;
  259. case OPT_NOINDEF:
  260. indef = 0;
  261. break;
  262. case OPT_NOOLDMIME:
  263. flags |= PKCS7_NOOLDMIMETYPE;
  264. break;
  265. case OPT_CRLFEOL:
  266. flags |= PKCS7_CRLFEOL;
  267. break;
  268. case OPT_RAND:
  269. inrand = opt_arg();
  270. need_rand = 1;
  271. break;
  272. case OPT_ENGINE:
  273. e = setup_engine(opt_arg(), 0);
  274. break;
  275. case OPT_PASSIN:
  276. passinarg = opt_arg();
  277. break;
  278. case OPT_TO:
  279. to = opt_arg();
  280. break;
  281. case OPT_FROM:
  282. from = opt_arg();
  283. break;
  284. case OPT_SUBJECT:
  285. subject = opt_arg();
  286. break;
  287. case OPT_SIGNER:
  288. /* If previous -signer argument add signer to list */
  289. if (signerfile) {
  290. if (sksigners == NULL
  291. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  292. goto end;
  293. sk_OPENSSL_STRING_push(sksigners, signerfile);
  294. if (keyfile == NULL)
  295. keyfile = signerfile;
  296. if (skkeys == NULL
  297. && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  298. goto end;
  299. sk_OPENSSL_STRING_push(skkeys, keyfile);
  300. keyfile = NULL;
  301. }
  302. signerfile = opt_arg();
  303. break;
  304. case OPT_RECIP:
  305. recipfile = opt_arg();
  306. break;
  307. case OPT_MD:
  308. if (!opt_md(opt_arg(), &sign_md))
  309. goto opthelp;
  310. break;
  311. case OPT_CIPHER:
  312. if (!opt_cipher(opt_unknown(), &cipher))
  313. goto opthelp;
  314. break;
  315. case OPT_INKEY:
  316. /* If previous -inkey arument add signer to list */
  317. if (keyfile) {
  318. if (signerfile == NULL) {
  319. BIO_printf(bio_err,
  320. "%s: Must have -signer before -inkey\n", prog);
  321. goto opthelp;
  322. }
  323. if (sksigners == NULL
  324. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  325. goto end;
  326. sk_OPENSSL_STRING_push(sksigners, signerfile);
  327. signerfile = NULL;
  328. if (skkeys == NULL
  329. && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  330. goto end;
  331. sk_OPENSSL_STRING_push(skkeys, keyfile);
  332. }
  333. keyfile = opt_arg();
  334. break;
  335. case OPT_KEYFORM:
  336. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
  337. goto opthelp;
  338. break;
  339. case OPT_CERTFILE:
  340. certfile = opt_arg();
  341. break;
  342. case OPT_CAFILE:
  343. CAfile = opt_arg();
  344. break;
  345. case OPT_CAPATH:
  346. CApath = opt_arg();
  347. break;
  348. case OPT_NOCAFILE:
  349. noCAfile = 1;
  350. break;
  351. case OPT_NOCAPATH:
  352. noCApath = 1;
  353. break;
  354. case OPT_CONTENT:
  355. contfile = opt_arg();
  356. break;
  357. case OPT_V_CASES:
  358. if (!opt_verify(o, vpm))
  359. goto opthelp;
  360. vpmtouched++;
  361. break;
  362. }
  363. }
  364. argc = opt_num_rest();
  365. argv = opt_rest();
  366. if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
  367. BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
  368. goto opthelp;
  369. }
  370. if (operation & SMIME_SIGNERS) {
  371. /* Check to see if any final signer needs to be appended */
  372. if (keyfile && !signerfile) {
  373. BIO_puts(bio_err, "Illegal -inkey without -signer\n");
  374. goto opthelp;
  375. }
  376. if (signerfile) {
  377. if (!sksigners
  378. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  379. goto end;
  380. sk_OPENSSL_STRING_push(sksigners, signerfile);
  381. if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  382. goto end;
  383. if (!keyfile)
  384. keyfile = signerfile;
  385. sk_OPENSSL_STRING_push(skkeys, keyfile);
  386. }
  387. if (!sksigners) {
  388. BIO_printf(bio_err, "No signer certificate specified\n");
  389. goto opthelp;
  390. }
  391. signerfile = NULL;
  392. keyfile = NULL;
  393. need_rand = 1;
  394. } else if (operation == SMIME_DECRYPT) {
  395. if (!recipfile && !keyfile) {
  396. BIO_printf(bio_err,
  397. "No recipient certificate or key specified\n");
  398. goto opthelp;
  399. }
  400. } else if (operation == SMIME_ENCRYPT) {
  401. if (argc == 0) {
  402. BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
  403. goto opthelp;
  404. }
  405. need_rand = 1;
  406. } else if (!operation)
  407. goto opthelp;
  408. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  409. BIO_printf(bio_err, "Error getting password\n");
  410. goto end;
  411. }
  412. if (need_rand) {
  413. app_RAND_load_file(NULL, (inrand != NULL));
  414. if (inrand != NULL)
  415. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  416. app_RAND_load_files(inrand));
  417. }
  418. ret = 2;
  419. if (!(operation & SMIME_SIGNERS))
  420. flags &= ~PKCS7_DETACHED;
  421. if (!(operation & SMIME_OP)) {
  422. if (flags & PKCS7_BINARY)
  423. outformat = FORMAT_BINARY;
  424. }
  425. if (!(operation & SMIME_IP)) {
  426. if (flags & PKCS7_BINARY)
  427. informat = FORMAT_BINARY;
  428. }
  429. if (operation == SMIME_ENCRYPT) {
  430. if (!cipher) {
  431. #ifndef OPENSSL_NO_DES
  432. cipher = EVP_des_ede3_cbc();
  433. #else
  434. BIO_printf(bio_err, "No cipher selected\n");
  435. goto end;
  436. #endif
  437. }
  438. encerts = sk_X509_new_null();
  439. if (!encerts)
  440. goto end;
  441. while (*argv) {
  442. cert = load_cert(*argv, FORMAT_PEM,
  443. NULL, e, "recipient certificate file");
  444. if (cert == NULL)
  445. goto end;
  446. sk_X509_push(encerts, cert);
  447. cert = NULL;
  448. argv++;
  449. }
  450. }
  451. if (certfile) {
  452. if (!load_certs(certfile, &other, FORMAT_PEM, NULL, e,
  453. "certificate file")) {
  454. ERR_print_errors(bio_err);
  455. goto end;
  456. }
  457. }
  458. if (recipfile && (operation == SMIME_DECRYPT)) {
  459. if ((recip = load_cert(recipfile, FORMAT_PEM, NULL,
  460. e, "recipient certificate file")) == NULL) {
  461. ERR_print_errors(bio_err);
  462. goto end;
  463. }
  464. }
  465. if (operation == SMIME_DECRYPT) {
  466. if (!keyfile)
  467. keyfile = recipfile;
  468. } else if (operation == SMIME_SIGN) {
  469. if (!keyfile)
  470. keyfile = signerfile;
  471. } else
  472. keyfile = NULL;
  473. if (keyfile) {
  474. key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
  475. if (!key)
  476. goto end;
  477. }
  478. in = bio_open_default(infile, 'r', informat);
  479. if (in == NULL)
  480. goto end;
  481. if (operation & SMIME_IP) {
  482. if (informat == FORMAT_SMIME)
  483. p7 = SMIME_read_PKCS7(in, &indata);
  484. else if (informat == FORMAT_PEM)
  485. p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
  486. else if (informat == FORMAT_ASN1)
  487. p7 = d2i_PKCS7_bio(in, NULL);
  488. else {
  489. BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
  490. goto end;
  491. }
  492. if (!p7) {
  493. BIO_printf(bio_err, "Error reading S/MIME message\n");
  494. goto end;
  495. }
  496. if (contfile) {
  497. BIO_free(indata);
  498. if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
  499. BIO_printf(bio_err, "Can't read content file %s\n", contfile);
  500. goto end;
  501. }
  502. }
  503. }
  504. out = bio_open_default(outfile, 'w', outformat);
  505. if (out == NULL)
  506. goto end;
  507. if (operation == SMIME_VERIFY) {
  508. if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
  509. goto end;
  510. X509_STORE_set_verify_cb(store, smime_cb);
  511. if (vpmtouched)
  512. X509_STORE_set1_param(store, vpm);
  513. }
  514. ret = 3;
  515. if (operation == SMIME_ENCRYPT) {
  516. if (indef)
  517. flags |= PKCS7_STREAM;
  518. p7 = PKCS7_encrypt(encerts, in, cipher, flags);
  519. } else if (operation & SMIME_SIGNERS) {
  520. int i;
  521. /*
  522. * If detached data content we only enable streaming if S/MIME output
  523. * format.
  524. */
  525. if (operation == SMIME_SIGN) {
  526. if (flags & PKCS7_DETACHED) {
  527. if (outformat == FORMAT_SMIME)
  528. flags |= PKCS7_STREAM;
  529. } else if (indef)
  530. flags |= PKCS7_STREAM;
  531. flags |= PKCS7_PARTIAL;
  532. p7 = PKCS7_sign(NULL, NULL, other, in, flags);
  533. if (!p7)
  534. goto end;
  535. if (flags & PKCS7_NOCERTS) {
  536. for (i = 0; i < sk_X509_num(other); i++) {
  537. X509 *x = sk_X509_value(other, i);
  538. PKCS7_add_certificate(p7, x);
  539. }
  540. }
  541. } else
  542. flags |= PKCS7_REUSE_DIGEST;
  543. for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
  544. signerfile = sk_OPENSSL_STRING_value(sksigners, i);
  545. keyfile = sk_OPENSSL_STRING_value(skkeys, i);
  546. signer = load_cert(signerfile, FORMAT_PEM, NULL,
  547. e, "signer certificate");
  548. if (!signer)
  549. goto end;
  550. key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
  551. if (!key)
  552. goto end;
  553. if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))
  554. goto end;
  555. X509_free(signer);
  556. signer = NULL;
  557. EVP_PKEY_free(key);
  558. key = NULL;
  559. }
  560. /* If not streaming or resigning finalize structure */
  561. if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
  562. if (!PKCS7_final(p7, in, flags))
  563. goto end;
  564. }
  565. }
  566. if (!p7) {
  567. BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
  568. goto end;
  569. }
  570. ret = 4;
  571. if (operation == SMIME_DECRYPT) {
  572. if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
  573. BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
  574. goto end;
  575. }
  576. } else if (operation == SMIME_VERIFY) {
  577. STACK_OF(X509) *signers;
  578. if (PKCS7_verify(p7, other, store, indata, out, flags))
  579. BIO_printf(bio_err, "Verification successful\n");
  580. else {
  581. BIO_printf(bio_err, "Verification failure\n");
  582. goto end;
  583. }
  584. signers = PKCS7_get0_signers(p7, other, flags);
  585. if (!save_certs(signerfile, signers)) {
  586. BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
  587. ret = 5;
  588. goto end;
  589. }
  590. sk_X509_free(signers);
  591. } else if (operation == SMIME_PK7OUT)
  592. PEM_write_bio_PKCS7(out, p7);
  593. else {
  594. if (to)
  595. BIO_printf(out, "To: %s\n", to);
  596. if (from)
  597. BIO_printf(out, "From: %s\n", from);
  598. if (subject)
  599. BIO_printf(out, "Subject: %s\n", subject);
  600. if (outformat == FORMAT_SMIME) {
  601. if (operation == SMIME_RESIGN)
  602. rv = SMIME_write_PKCS7(out, p7, indata, flags);
  603. else
  604. rv = SMIME_write_PKCS7(out, p7, in, flags);
  605. } else if (outformat == FORMAT_PEM)
  606. rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags);
  607. else if (outformat == FORMAT_ASN1)
  608. rv = i2d_PKCS7_bio_stream(out, p7, in, flags);
  609. else {
  610. BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
  611. goto end;
  612. }
  613. if (rv == 0) {
  614. BIO_printf(bio_err, "Error writing output\n");
  615. ret = 3;
  616. goto end;
  617. }
  618. }
  619. ret = 0;
  620. end:
  621. if (need_rand)
  622. app_RAND_write_file(NULL);
  623. if (ret)
  624. ERR_print_errors(bio_err);
  625. sk_X509_pop_free(encerts, X509_free);
  626. sk_X509_pop_free(other, X509_free);
  627. X509_VERIFY_PARAM_free(vpm);
  628. sk_OPENSSL_STRING_free(sksigners);
  629. sk_OPENSSL_STRING_free(skkeys);
  630. X509_STORE_free(store);
  631. X509_free(cert);
  632. X509_free(recip);
  633. X509_free(signer);
  634. EVP_PKEY_free(key);
  635. PKCS7_free(p7);
  636. BIO_free(in);
  637. BIO_free(indata);
  638. BIO_free_all(out);
  639. OPENSSL_free(passin);
  640. return (ret);
  641. }
  642. static int save_certs(char *signerfile, STACK_OF(X509) *signers)
  643. {
  644. int i;
  645. BIO *tmp;
  646. if (!signerfile)
  647. return 1;
  648. tmp = BIO_new_file(signerfile, "w");
  649. if (!tmp)
  650. return 0;
  651. for (i = 0; i < sk_X509_num(signers); i++)
  652. PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
  653. BIO_free(tmp);
  654. return 1;
  655. }
  656. /* Minimal callback just to output policy info (if any) */
  657. static int smime_cb(int ok, X509_STORE_CTX *ctx)
  658. {
  659. int error;
  660. error = X509_STORE_CTX_get_error(ctx);
  661. if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
  662. && ((error != X509_V_OK) || (ok != 2)))
  663. return ok;
  664. policies_print(ctx);
  665. return ok;
  666. }