smime.c 22 KB

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