2
0

smime.c 23 KB

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