smime.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /*
  2. * Copyright 1999-2025 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_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
  28. #define SMIME_VERIFY (4 | SMIME_IP)
  29. #define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP)
  30. typedef enum OPTION_choice {
  31. OPT_COMMON,
  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. {"pk7out", OPT_PK7OUT, '-', "Output PKCS#7 structure"},
  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. {"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 LF 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. static const char *operation_name(int operation)
  117. {
  118. switch (operation) {
  119. case SMIME_ENCRYPT:
  120. return "encrypt";
  121. case SMIME_DECRYPT:
  122. return "decrypt";
  123. case SMIME_SIGN:
  124. return "sign";
  125. case SMIME_RESIGN:
  126. return "resign";
  127. case SMIME_VERIFY:
  128. return "verify";
  129. case SMIME_PK7OUT:
  130. return "pk7out";
  131. default:
  132. return "(invalid operation)";
  133. }
  134. }
  135. #define SET_OPERATION(op) \
  136. ((operation != 0 && (operation != (op))) \
  137. ? 0 * BIO_printf(bio_err, "%s: Cannot use -%s together with -%s\n", \
  138. prog, operation_name(op), operation_name(operation)) \
  139. : (operation = (op)))
  140. int smime_main(int argc, char **argv)
  141. {
  142. CONF *conf = NULL;
  143. BIO *in = NULL, *out = NULL, *indata = NULL;
  144. EVP_PKEY *key = NULL;
  145. PKCS7 *p7 = NULL;
  146. STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
  147. STACK_OF(X509) *encerts = NULL, *other = NULL;
  148. X509 *cert = NULL, *recip = NULL, *signer = NULL;
  149. X509_STORE *store = NULL;
  150. X509_VERIFY_PARAM *vpm = NULL;
  151. EVP_CIPHER *cipher = NULL;
  152. EVP_MD *sign_md = NULL;
  153. const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL, *prog = NULL;
  154. char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
  155. char *infile = NULL, *outfile = NULL, *signerfile = NULL, *recipfile = NULL;
  156. char *passinarg = NULL, *passin = NULL, *to = NULL, *from = NULL;
  157. char *subject = NULL, *digestname = NULL, *ciphername = NULL;
  158. OPTION_CHOICE o;
  159. int noCApath = 0, noCAfile = 0, noCAstore = 0;
  160. int flags = PKCS7_DETACHED, operation = 0, ret = 0, indef = 0;
  161. int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform =
  162. FORMAT_UNDEF;
  163. int vpmtouched = 0, rv = 0;
  164. ENGINE *e = NULL;
  165. const char *mime_eol = "\n";
  166. OSSL_LIB_CTX *libctx = app_get0_libctx();
  167. if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
  168. return 1;
  169. opt_set_unknown_name("cipher");
  170. prog = opt_init(argc, argv, smime_options);
  171. while ((o = opt_next()) != OPT_EOF) {
  172. switch (o) {
  173. case OPT_EOF:
  174. case OPT_ERR:
  175. opthelp:
  176. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  177. goto end;
  178. case OPT_HELP:
  179. opt_help(smime_options);
  180. ret = 0;
  181. goto end;
  182. case OPT_INFORM:
  183. if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))
  184. goto opthelp;
  185. break;
  186. case OPT_IN:
  187. infile = opt_arg();
  188. break;
  189. case OPT_OUTFORM:
  190. if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))
  191. goto opthelp;
  192. break;
  193. case OPT_OUT:
  194. outfile = opt_arg();
  195. break;
  196. case OPT_ENCRYPT:
  197. if (!SET_OPERATION(SMIME_ENCRYPT))
  198. goto end;
  199. break;
  200. case OPT_DECRYPT:
  201. if (!SET_OPERATION(SMIME_DECRYPT))
  202. goto end;
  203. break;
  204. case OPT_SIGN:
  205. if (!SET_OPERATION(SMIME_SIGN))
  206. goto end;
  207. break;
  208. case OPT_RESIGN:
  209. if (!SET_OPERATION(SMIME_RESIGN))
  210. goto end;
  211. break;
  212. case OPT_VERIFY:
  213. if (!SET_OPERATION(SMIME_VERIFY))
  214. goto end;
  215. break;
  216. case OPT_PK7OUT:
  217. if (!SET_OPERATION(SMIME_PK7OUT))
  218. goto end;
  219. break;
  220. case OPT_TEXT:
  221. flags |= PKCS7_TEXT;
  222. break;
  223. case OPT_NOINTERN:
  224. flags |= PKCS7_NOINTERN;
  225. break;
  226. case OPT_NOVERIFY:
  227. flags |= PKCS7_NOVERIFY;
  228. break;
  229. case OPT_NOCHAIN:
  230. flags |= PKCS7_NOCHAIN;
  231. break;
  232. case OPT_NOCERTS:
  233. flags |= PKCS7_NOCERTS;
  234. break;
  235. case OPT_NOATTR:
  236. flags |= PKCS7_NOATTR;
  237. break;
  238. case OPT_NODETACH:
  239. flags &= ~PKCS7_DETACHED;
  240. break;
  241. case OPT_NOSMIMECAP:
  242. flags |= PKCS7_NOSMIMECAP;
  243. break;
  244. case OPT_BINARY:
  245. flags |= PKCS7_BINARY;
  246. break;
  247. case OPT_NOSIGS:
  248. flags |= PKCS7_NOSIGS;
  249. break;
  250. case OPT_STREAM:
  251. case OPT_INDEF:
  252. indef = 1;
  253. break;
  254. case OPT_NOINDEF:
  255. indef = 0;
  256. break;
  257. case OPT_CRLFEOL:
  258. flags |= PKCS7_CRLFEOL;
  259. mime_eol = "\r\n";
  260. break;
  261. case OPT_R_CASES:
  262. if (!opt_rand(o))
  263. goto end;
  264. break;
  265. case OPT_PROV_CASES:
  266. if (!opt_provider(o))
  267. goto end;
  268. break;
  269. case OPT_CONFIG:
  270. conf = app_load_config_modules(opt_arg());
  271. if (conf == NULL)
  272. goto end;
  273. break;
  274. case OPT_ENGINE:
  275. e = setup_engine(opt_arg(), 0);
  276. break;
  277. case OPT_PASSIN:
  278. passinarg = opt_arg();
  279. break;
  280. case OPT_TO:
  281. to = opt_arg();
  282. break;
  283. case OPT_FROM:
  284. from = opt_arg();
  285. break;
  286. case OPT_SUBJECT:
  287. subject = opt_arg();
  288. break;
  289. case OPT_SIGNER:
  290. /* If previous -signer argument add signer to list */
  291. if (signerfile != NULL) {
  292. if (sksigners == NULL
  293. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  294. goto end;
  295. if (sk_OPENSSL_STRING_push(sksigners, signerfile) <= 0)
  296. goto end;
  297. if (keyfile == NULL)
  298. keyfile = signerfile;
  299. if (skkeys == NULL
  300. && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  301. goto end;
  302. if (sk_OPENSSL_STRING_push(skkeys, keyfile) <= 0)
  303. goto end;
  304. keyfile = NULL;
  305. }
  306. signerfile = opt_arg();
  307. break;
  308. case OPT_RECIP:
  309. recipfile = opt_arg();
  310. break;
  311. case OPT_MD:
  312. digestname = opt_arg();
  313. break;
  314. case OPT_CIPHER:
  315. ciphername = opt_unknown();
  316. break;
  317. case OPT_INKEY:
  318. /* If previous -inkey argument add signer to list */
  319. if (keyfile != NULL) {
  320. if (signerfile == NULL) {
  321. BIO_printf(bio_err,
  322. "%s: Must have -signer before -inkey\n", prog);
  323. goto opthelp;
  324. }
  325. if (sksigners == NULL
  326. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  327. goto end;
  328. if (sk_OPENSSL_STRING_push(sksigners, signerfile) <= 0)
  329. goto end;
  330. signerfile = NULL;
  331. if (skkeys == NULL
  332. && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  333. goto end;
  334. if (sk_OPENSSL_STRING_push(skkeys, keyfile) <= 0)
  335. goto end;
  336. }
  337. keyfile = opt_arg();
  338. break;
  339. case OPT_KEYFORM:
  340. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
  341. goto opthelp;
  342. break;
  343. case OPT_CERTFILE:
  344. certfile = opt_arg();
  345. break;
  346. case OPT_CAFILE:
  347. CAfile = opt_arg();
  348. break;
  349. case OPT_CAPATH:
  350. CApath = opt_arg();
  351. break;
  352. case OPT_CASTORE:
  353. CAstore = opt_arg();
  354. break;
  355. case OPT_NOCAFILE:
  356. noCAfile = 1;
  357. break;
  358. case OPT_NOCAPATH:
  359. noCApath = 1;
  360. break;
  361. case OPT_NOCASTORE:
  362. noCAstore = 1;
  363. break;
  364. case OPT_CONTENT:
  365. contfile = opt_arg();
  366. break;
  367. case OPT_V_CASES:
  368. if (!opt_verify(o, vpm))
  369. goto opthelp;
  370. vpmtouched++;
  371. break;
  372. }
  373. }
  374. /* Extra arguments are files with recipient keys. */
  375. argc = opt_num_rest();
  376. argv = opt_rest();
  377. if (!app_RAND_load())
  378. goto end;
  379. if (digestname != NULL) {
  380. if (!opt_md(digestname, &sign_md))
  381. goto opthelp;
  382. }
  383. if (!opt_cipher_any(ciphername, &cipher))
  384. goto opthelp;
  385. if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
  386. BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
  387. goto opthelp;
  388. }
  389. if (!operation) {
  390. BIO_puts(bio_err,
  391. "No operation (-encrypt|-sign|...) specified\n");
  392. goto opthelp;
  393. }
  394. if (operation & SMIME_SIGNERS) {
  395. /* Check to see if any final signer needs to be appended */
  396. if (keyfile && !signerfile) {
  397. BIO_puts(bio_err, "Illegal -inkey without -signer\n");
  398. goto opthelp;
  399. }
  400. if (signerfile != NULL) {
  401. if (sksigners == NULL
  402. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  403. goto end;
  404. if (sk_OPENSSL_STRING_push(sksigners, signerfile) <= 0)
  405. goto end;
  406. if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  407. goto end;
  408. if (!keyfile)
  409. keyfile = signerfile;
  410. if (sk_OPENSSL_STRING_push(skkeys, keyfile) <= 0)
  411. goto end;
  412. }
  413. if (sksigners == NULL) {
  414. BIO_printf(bio_err, "No signer certificate specified\n");
  415. goto opthelp;
  416. }
  417. signerfile = NULL;
  418. keyfile = NULL;
  419. } else if (operation == SMIME_DECRYPT) {
  420. if (recipfile == NULL && keyfile == NULL) {
  421. BIO_printf(bio_err,
  422. "No recipient certificate or key specified\n");
  423. goto opthelp;
  424. }
  425. } else if (operation == SMIME_ENCRYPT) {
  426. if (argc == 0) {
  427. BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
  428. goto opthelp;
  429. }
  430. }
  431. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  432. BIO_printf(bio_err, "Error getting password\n");
  433. goto end;
  434. }
  435. ret = 2;
  436. if (!(operation & SMIME_SIGNERS))
  437. flags &= ~PKCS7_DETACHED;
  438. if (!(operation & SMIME_OP)) {
  439. if (flags & PKCS7_BINARY)
  440. outformat = FORMAT_BINARY;
  441. }
  442. if (!(operation & SMIME_IP)) {
  443. if (flags & PKCS7_BINARY)
  444. informat = FORMAT_BINARY;
  445. }
  446. if (operation == SMIME_ENCRYPT) {
  447. if (cipher == NULL) {
  448. #ifndef OPENSSL_NO_DES
  449. cipher = (EVP_CIPHER *)EVP_des_ede3_cbc();
  450. #else
  451. BIO_printf(bio_err, "No cipher selected\n");
  452. goto end;
  453. #endif
  454. }
  455. encerts = sk_X509_new_null();
  456. if (encerts == NULL)
  457. goto end;
  458. while (*argv != NULL) {
  459. cert = load_cert(*argv, FORMAT_UNDEF,
  460. "recipient certificate file");
  461. if (cert == NULL)
  462. goto end;
  463. if (!sk_X509_push(encerts, cert))
  464. goto end;
  465. cert = NULL;
  466. argv++;
  467. }
  468. }
  469. if (certfile != NULL) {
  470. if (!load_certs(certfile, 0, &other, NULL, "certificates")) {
  471. ERR_print_errors(bio_err);
  472. goto end;
  473. }
  474. }
  475. if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
  476. if ((recip = load_cert(recipfile, FORMAT_UNDEF,
  477. "recipient certificate file")) == NULL) {
  478. ERR_print_errors(bio_err);
  479. goto end;
  480. }
  481. }
  482. if (operation == SMIME_DECRYPT) {
  483. if (keyfile == NULL)
  484. keyfile = recipfile;
  485. } else if (operation == SMIME_SIGN) {
  486. if (keyfile == NULL)
  487. keyfile = signerfile;
  488. } else {
  489. keyfile = NULL;
  490. }
  491. if (keyfile != NULL) {
  492. key = load_key(keyfile, keyform, 0, passin, e, "signing key");
  493. if (key == NULL)
  494. goto end;
  495. }
  496. in = bio_open_default(infile, 'r', informat);
  497. if (in == NULL)
  498. goto end;
  499. if (operation & SMIME_IP) {
  500. PKCS7 *p7_in = NULL;
  501. p7 = PKCS7_new_ex(libctx, app_get0_propq());
  502. if (p7 == NULL) {
  503. BIO_printf(bio_err, "Error allocating PKCS7 object\n");
  504. goto end;
  505. }
  506. if (informat == FORMAT_SMIME) {
  507. p7_in = SMIME_read_PKCS7_ex(in, &indata, &p7);
  508. } else if (informat == FORMAT_PEM) {
  509. p7_in = PEM_read_bio_PKCS7(in, &p7, NULL, NULL);
  510. } else if (informat == FORMAT_ASN1) {
  511. p7_in = d2i_PKCS7_bio(in, &p7);
  512. } else {
  513. BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
  514. goto end;
  515. }
  516. if (p7_in == NULL) {
  517. BIO_printf(bio_err, "Error reading S/MIME message\n");
  518. goto end;
  519. }
  520. if (contfile != NULL) {
  521. BIO_free(indata);
  522. if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
  523. BIO_printf(bio_err, "Can't read content file %s\n", contfile);
  524. goto end;
  525. }
  526. }
  527. }
  528. out = bio_open_default(outfile, 'w', outformat);
  529. if (out == NULL)
  530. goto end;
  531. if (operation == SMIME_VERIFY) {
  532. if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  533. CAstore, noCAstore)) == NULL)
  534. goto end;
  535. X509_STORE_set_verify_cb(store, smime_cb);
  536. if (vpmtouched)
  537. X509_STORE_set1_param(store, vpm);
  538. }
  539. ret = 3;
  540. if (operation == SMIME_ENCRYPT) {
  541. if (indef)
  542. flags |= PKCS7_STREAM;
  543. p7 = PKCS7_encrypt_ex(encerts, in, cipher, flags, libctx, app_get0_propq());
  544. } else if (operation & SMIME_SIGNERS) {
  545. int i;
  546. /*
  547. * If detached data content we only enable streaming if S/MIME output
  548. * format.
  549. */
  550. if (operation == SMIME_SIGN) {
  551. if (flags & PKCS7_DETACHED) {
  552. if (outformat == FORMAT_SMIME)
  553. flags |= PKCS7_STREAM;
  554. } else if (indef) {
  555. flags |= PKCS7_STREAM;
  556. }
  557. flags |= PKCS7_PARTIAL;
  558. p7 = PKCS7_sign_ex(NULL, NULL, other, in, flags, libctx, app_get0_propq());
  559. if (p7 == NULL)
  560. goto end;
  561. if (flags & PKCS7_NOCERTS) {
  562. for (i = 0; i < sk_X509_num(other); i++) {
  563. X509 *x = sk_X509_value(other, i);
  564. PKCS7_add_certificate(p7, x);
  565. }
  566. }
  567. } else {
  568. flags |= PKCS7_REUSE_DIGEST;
  569. }
  570. for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
  571. signerfile = sk_OPENSSL_STRING_value(sksigners, i);
  572. keyfile = sk_OPENSSL_STRING_value(skkeys, i);
  573. signer = load_cert(signerfile, FORMAT_UNDEF, "signer certificate");
  574. if (signer == NULL)
  575. goto end;
  576. key = load_key(keyfile, keyform, 0, passin, e, "signing key");
  577. if (key == NULL)
  578. goto end;
  579. if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))
  580. goto end;
  581. X509_free(signer);
  582. signer = NULL;
  583. EVP_PKEY_free(key);
  584. key = NULL;
  585. }
  586. /* If not streaming or resigning finalize structure */
  587. if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
  588. if (!PKCS7_final(p7, in, flags))
  589. goto end;
  590. }
  591. }
  592. if (p7 == NULL) {
  593. BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
  594. goto end;
  595. }
  596. ret = 4;
  597. if (operation == SMIME_DECRYPT) {
  598. if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
  599. BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
  600. goto end;
  601. }
  602. } else if (operation == SMIME_VERIFY) {
  603. STACK_OF(X509) *signers;
  604. if (PKCS7_verify(p7, other, store, indata, out, flags))
  605. BIO_printf(bio_err, "Verification successful\n");
  606. else {
  607. BIO_printf(bio_err, "Verification failure\n");
  608. goto end;
  609. }
  610. signers = PKCS7_get0_signers(p7, other, flags);
  611. if (!save_certs(signerfile, signers)) {
  612. BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
  613. ret = 5;
  614. goto end;
  615. }
  616. sk_X509_free(signers);
  617. } else if (operation == SMIME_PK7OUT) {
  618. PEM_write_bio_PKCS7(out, p7);
  619. } else {
  620. if (to)
  621. BIO_printf(out, "To: %s%s", to, mime_eol);
  622. if (from)
  623. BIO_printf(out, "From: %s%s", from, mime_eol);
  624. if (subject)
  625. BIO_printf(out, "Subject: %s%s", subject, mime_eol);
  626. if (outformat == FORMAT_SMIME) {
  627. if (operation == SMIME_RESIGN)
  628. rv = SMIME_write_PKCS7(out, p7, indata, flags);
  629. else
  630. rv = SMIME_write_PKCS7(out, p7, in, flags);
  631. } else if (outformat == FORMAT_PEM) {
  632. rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags);
  633. } else if (outformat == FORMAT_ASN1) {
  634. rv = i2d_PKCS7_bio_stream(out, p7, in, flags);
  635. } else {
  636. BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
  637. goto end;
  638. }
  639. if (rv == 0) {
  640. BIO_printf(bio_err, "Error writing output\n");
  641. ret = 3;
  642. goto end;
  643. }
  644. }
  645. ret = 0;
  646. end:
  647. if (ret)
  648. ERR_print_errors(bio_err);
  649. OSSL_STACK_OF_X509_free(encerts);
  650. OSSL_STACK_OF_X509_free(other);
  651. X509_VERIFY_PARAM_free(vpm);
  652. sk_OPENSSL_STRING_free(sksigners);
  653. sk_OPENSSL_STRING_free(skkeys);
  654. X509_STORE_free(store);
  655. X509_free(cert);
  656. X509_free(recip);
  657. X509_free(signer);
  658. EVP_PKEY_free(key);
  659. EVP_MD_free(sign_md);
  660. EVP_CIPHER_free(cipher);
  661. PKCS7_free(p7);
  662. release_engine(e);
  663. BIO_free(in);
  664. BIO_free(indata);
  665. BIO_free_all(out);
  666. OPENSSL_free(passin);
  667. NCONF_free(conf);
  668. return ret;
  669. }
  670. static int save_certs(char *signerfile, STACK_OF(X509) *signers)
  671. {
  672. int i;
  673. BIO *tmp;
  674. if (signerfile == NULL)
  675. return 1;
  676. tmp = BIO_new_file(signerfile, "w");
  677. if (tmp == NULL)
  678. return 0;
  679. for (i = 0; i < sk_X509_num(signers); i++)
  680. PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
  681. BIO_free(tmp);
  682. return 1;
  683. }
  684. /* Minimal callback just to output policy info (if any) */
  685. static int smime_cb(int ok, X509_STORE_CTX *ctx)
  686. {
  687. int error;
  688. error = X509_STORE_CTX_get_error(ctx);
  689. if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
  690. && ((error != X509_V_OK) || (ok != 2)))
  691. return ok;
  692. policies_print(ctx);
  693. return ok;
  694. }