smime.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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. OSSL_LIB_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, "recipient certificate file");
  413. if (cert == NULL)
  414. goto end;
  415. sk_X509_push(encerts, cert);
  416. cert = NULL;
  417. argv++;
  418. }
  419. }
  420. if (certfile != NULL) {
  421. if (!load_certs(certfile, &other, NULL, "certificates")) {
  422. ERR_print_errors(bio_err);
  423. goto end;
  424. }
  425. }
  426. if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
  427. if ((recip = load_cert(recipfile,
  428. "recipient certificate file")) == NULL) {
  429. ERR_print_errors(bio_err);
  430. goto end;
  431. }
  432. }
  433. if (operation == SMIME_DECRYPT) {
  434. if (keyfile == NULL)
  435. keyfile = recipfile;
  436. } else if (operation == SMIME_SIGN) {
  437. if (keyfile == NULL)
  438. keyfile = signerfile;
  439. } else {
  440. keyfile = NULL;
  441. }
  442. if (keyfile != NULL) {
  443. key = load_key(keyfile, keyform, 0, passin, e, "signing key");
  444. if (key == NULL)
  445. goto end;
  446. /*
  447. * TODO: Remove this when CMS has full support for provider-native
  448. * EVP_PKEYs
  449. */
  450. if (EVP_PKEY_get0(key) == NULL)
  451. goto end;
  452. }
  453. in = bio_open_default(infile, 'r', informat);
  454. if (in == NULL)
  455. goto end;
  456. if (operation & SMIME_IP) {
  457. PKCS7 *p7_in = NULL;
  458. p7 = PKCS7_new_ex(libctx, propq);
  459. if (p7 == NULL) {
  460. BIO_printf(bio_err, "Error allocating PKCS7 object\n");
  461. goto end;
  462. }
  463. if (informat == FORMAT_SMIME) {
  464. p7_in = SMIME_read_PKCS7_ex(in, &indata, &p7);
  465. } else if (informat == FORMAT_PEM) {
  466. p7_in = PEM_read_bio_PKCS7(in, &p7, NULL, NULL);
  467. } else if (informat == FORMAT_ASN1) {
  468. p7_in = d2i_PKCS7_bio(in, &p7);
  469. } else {
  470. BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
  471. goto end;
  472. }
  473. if (p7_in == NULL) {
  474. BIO_printf(bio_err, "Error reading S/MIME message\n");
  475. goto end;
  476. }
  477. if (contfile != NULL) {
  478. BIO_free(indata);
  479. if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
  480. BIO_printf(bio_err, "Can't read content file %s\n", contfile);
  481. goto end;
  482. }
  483. }
  484. }
  485. out = bio_open_default(outfile, 'w', outformat);
  486. if (out == NULL)
  487. goto end;
  488. if (operation == SMIME_VERIFY) {
  489. if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  490. CAstore, noCAstore)) == NULL)
  491. goto end;
  492. X509_STORE_set_verify_cb(store, smime_cb);
  493. if (vpmtouched)
  494. X509_STORE_set1_param(store, vpm);
  495. }
  496. ret = 3;
  497. if (operation == SMIME_ENCRYPT) {
  498. if (indef)
  499. flags |= PKCS7_STREAM;
  500. p7 = PKCS7_encrypt_ex(encerts, in, cipher, flags, libctx, propq);
  501. } else if (operation & SMIME_SIGNERS) {
  502. int i;
  503. /*
  504. * If detached data content we only enable streaming if S/MIME output
  505. * format.
  506. */
  507. if (operation == SMIME_SIGN) {
  508. if (flags & PKCS7_DETACHED) {
  509. if (outformat == FORMAT_SMIME)
  510. flags |= PKCS7_STREAM;
  511. } else if (indef) {
  512. flags |= PKCS7_STREAM;
  513. }
  514. flags |= PKCS7_PARTIAL;
  515. p7 = PKCS7_sign_ex(NULL, NULL, other, in, flags, libctx, propq);
  516. if (p7 == NULL)
  517. goto end;
  518. if (flags & PKCS7_NOCERTS) {
  519. for (i = 0; i < sk_X509_num(other); i++) {
  520. X509 *x = sk_X509_value(other, i);
  521. PKCS7_add_certificate(p7, x);
  522. }
  523. }
  524. } else {
  525. flags |= PKCS7_REUSE_DIGEST;
  526. }
  527. for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
  528. signerfile = sk_OPENSSL_STRING_value(sksigners, i);
  529. keyfile = sk_OPENSSL_STRING_value(skkeys, i);
  530. signer = load_cert(signerfile, "signer certificate");
  531. if (signer == NULL)
  532. goto end;
  533. key = load_key(keyfile, keyform, 0, passin, e, "signing key");
  534. if (key == NULL)
  535. goto end;
  536. /*
  537. * TODO: Remove this when CMS has full support for provider-native
  538. * EVP_PKEYs
  539. */
  540. if (EVP_PKEY_get0(key) == NULL)
  541. goto end;
  542. if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))
  543. goto end;
  544. X509_free(signer);
  545. signer = NULL;
  546. EVP_PKEY_free(key);
  547. key = NULL;
  548. }
  549. /* If not streaming or resigning finalize structure */
  550. if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
  551. if (!PKCS7_final(p7, in, flags))
  552. goto end;
  553. }
  554. }
  555. if (p7 == NULL) {
  556. BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
  557. goto end;
  558. }
  559. ret = 4;
  560. if (operation == SMIME_DECRYPT) {
  561. if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
  562. BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
  563. goto end;
  564. }
  565. } else if (operation == SMIME_VERIFY) {
  566. STACK_OF(X509) *signers;
  567. if (PKCS7_verify(p7, other, store, indata, out, flags))
  568. BIO_printf(bio_err, "Verification successful\n");
  569. else {
  570. BIO_printf(bio_err, "Verification failure\n");
  571. goto end;
  572. }
  573. signers = PKCS7_get0_signers(p7, other, flags);
  574. if (!save_certs(signerfile, signers)) {
  575. BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
  576. ret = 5;
  577. goto end;
  578. }
  579. sk_X509_free(signers);
  580. } else if (operation == SMIME_PK7OUT) {
  581. PEM_write_bio_PKCS7(out, p7);
  582. } else {
  583. if (to)
  584. BIO_printf(out, "To: %s%s", to, mime_eol);
  585. if (from)
  586. BIO_printf(out, "From: %s%s", from, mime_eol);
  587. if (subject)
  588. BIO_printf(out, "Subject: %s%s", subject, mime_eol);
  589. if (outformat == FORMAT_SMIME) {
  590. if (operation == SMIME_RESIGN)
  591. rv = SMIME_write_PKCS7(out, p7, indata, flags);
  592. else
  593. rv = SMIME_write_PKCS7(out, p7, in, flags);
  594. } else if (outformat == FORMAT_PEM) {
  595. rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags);
  596. } else if (outformat == FORMAT_ASN1) {
  597. rv = i2d_PKCS7_bio_stream(out, p7, in, flags);
  598. } else {
  599. BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
  600. goto end;
  601. }
  602. if (rv == 0) {
  603. BIO_printf(bio_err, "Error writing output\n");
  604. ret = 3;
  605. goto end;
  606. }
  607. }
  608. ret = 0;
  609. end:
  610. if (ret)
  611. ERR_print_errors(bio_err);
  612. sk_X509_pop_free(encerts, X509_free);
  613. sk_X509_pop_free(other, X509_free);
  614. X509_VERIFY_PARAM_free(vpm);
  615. sk_OPENSSL_STRING_free(sksigners);
  616. sk_OPENSSL_STRING_free(skkeys);
  617. X509_STORE_free(store);
  618. X509_free(cert);
  619. X509_free(recip);
  620. X509_free(signer);
  621. EVP_PKEY_free(key);
  622. PKCS7_free(p7);
  623. release_engine(e);
  624. BIO_free(in);
  625. BIO_free(indata);
  626. BIO_free_all(out);
  627. OPENSSL_free(passin);
  628. NCONF_free(conf);
  629. return ret;
  630. }
  631. static int save_certs(char *signerfile, STACK_OF(X509) *signers)
  632. {
  633. int i;
  634. BIO *tmp;
  635. if (signerfile == NULL)
  636. return 1;
  637. tmp = BIO_new_file(signerfile, "w");
  638. if (tmp == NULL)
  639. return 0;
  640. for (i = 0; i < sk_X509_num(signers); i++)
  641. PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
  642. BIO_free(tmp);
  643. return 1;
  644. }
  645. /* Minimal callback just to output policy info (if any) */
  646. static int smime_cb(int ok, X509_STORE_CTX *ctx)
  647. {
  648. int error;
  649. error = X509_STORE_CTX_get_error(ctx);
  650. if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
  651. && ((error != X509_V_OK) || (ok != 2)))
  652. return ok;
  653. policies_print(ctx);
  654. return ok;
  655. }