smime.c 21 KB

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