smime.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. * Copyright 1999-2023 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 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. 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. sk_OPENSSL_STRING_push(sksigners, signerfile);
  296. if (keyfile == NULL)
  297. keyfile = signerfile;
  298. if (skkeys == NULL
  299. && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  300. goto end;
  301. sk_OPENSSL_STRING_push(skkeys, keyfile);
  302. keyfile = NULL;
  303. }
  304. signerfile = opt_arg();
  305. break;
  306. case OPT_RECIP:
  307. recipfile = opt_arg();
  308. break;
  309. case OPT_MD:
  310. digestname = opt_arg();
  311. break;
  312. case OPT_CIPHER:
  313. ciphername = opt_unknown();
  314. break;
  315. case OPT_INKEY:
  316. /* If previous -inkey argument add signer to list */
  317. if (keyfile != NULL) {
  318. if (signerfile == NULL) {
  319. BIO_printf(bio_err,
  320. "%s: Must have -signer before -inkey\n", prog);
  321. goto opthelp;
  322. }
  323. if (sksigners == NULL
  324. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  325. goto end;
  326. sk_OPENSSL_STRING_push(sksigners, signerfile);
  327. signerfile = NULL;
  328. if (skkeys == NULL
  329. && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  330. goto end;
  331. sk_OPENSSL_STRING_push(skkeys, keyfile);
  332. }
  333. keyfile = opt_arg();
  334. break;
  335. case OPT_KEYFORM:
  336. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
  337. goto opthelp;
  338. break;
  339. case OPT_CERTFILE:
  340. certfile = opt_arg();
  341. break;
  342. case OPT_CAFILE:
  343. CAfile = opt_arg();
  344. break;
  345. case OPT_CAPATH:
  346. CApath = opt_arg();
  347. break;
  348. case OPT_CASTORE:
  349. CAstore = opt_arg();
  350. break;
  351. case OPT_NOCAFILE:
  352. noCAfile = 1;
  353. break;
  354. case OPT_NOCAPATH:
  355. noCApath = 1;
  356. break;
  357. case OPT_NOCASTORE:
  358. noCAstore = 1;
  359. break;
  360. case OPT_CONTENT:
  361. contfile = opt_arg();
  362. break;
  363. case OPT_V_CASES:
  364. if (!opt_verify(o, vpm))
  365. goto opthelp;
  366. vpmtouched++;
  367. break;
  368. }
  369. }
  370. /* Extra arguments are files with recipient keys. */
  371. argc = opt_num_rest();
  372. argv = opt_rest();
  373. if (!app_RAND_load())
  374. goto end;
  375. if (digestname != NULL) {
  376. if (!opt_md(digestname, &sign_md))
  377. goto opthelp;
  378. }
  379. if (!opt_cipher_any(ciphername, &cipher))
  380. goto opthelp;
  381. if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
  382. BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
  383. goto opthelp;
  384. }
  385. if (!operation) {
  386. BIO_puts(bio_err,
  387. "No operation (-encrypt|-sign|...) specified\n");
  388. goto opthelp;
  389. }
  390. if (operation & SMIME_SIGNERS) {
  391. /* Check to see if any final signer needs to be appended */
  392. if (keyfile && !signerfile) {
  393. BIO_puts(bio_err, "Illegal -inkey without -signer\n");
  394. goto opthelp;
  395. }
  396. if (signerfile != NULL) {
  397. if (sksigners == NULL
  398. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  399. goto end;
  400. sk_OPENSSL_STRING_push(sksigners, signerfile);
  401. if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  402. goto end;
  403. if (!keyfile)
  404. keyfile = signerfile;
  405. sk_OPENSSL_STRING_push(skkeys, keyfile);
  406. }
  407. if (sksigners == NULL) {
  408. BIO_printf(bio_err, "No signer certificate specified\n");
  409. goto opthelp;
  410. }
  411. signerfile = NULL;
  412. keyfile = NULL;
  413. } else if (operation == SMIME_DECRYPT) {
  414. if (recipfile == NULL && keyfile == NULL) {
  415. BIO_printf(bio_err,
  416. "No recipient certificate or key specified\n");
  417. goto opthelp;
  418. }
  419. } else if (operation == SMIME_ENCRYPT) {
  420. if (argc == 0) {
  421. BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
  422. goto opthelp;
  423. }
  424. }
  425. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  426. BIO_printf(bio_err, "Error getting password\n");
  427. goto end;
  428. }
  429. ret = 2;
  430. if (!(operation & SMIME_SIGNERS))
  431. flags &= ~PKCS7_DETACHED;
  432. if (!(operation & SMIME_OP)) {
  433. if (flags & PKCS7_BINARY)
  434. outformat = FORMAT_BINARY;
  435. }
  436. if (!(operation & SMIME_IP)) {
  437. if (flags & PKCS7_BINARY)
  438. informat = FORMAT_BINARY;
  439. }
  440. if (operation == SMIME_ENCRYPT) {
  441. if (cipher == NULL) {
  442. #ifndef OPENSSL_NO_DES
  443. cipher = (EVP_CIPHER *)EVP_des_ede3_cbc();
  444. #else
  445. BIO_printf(bio_err, "No cipher selected\n");
  446. goto end;
  447. #endif
  448. }
  449. encerts = sk_X509_new_null();
  450. if (encerts == NULL)
  451. goto end;
  452. while (*argv != NULL) {
  453. cert = load_cert(*argv, FORMAT_UNDEF,
  454. "recipient certificate file");
  455. if (cert == NULL)
  456. goto end;
  457. if (!sk_X509_push(encerts, cert))
  458. goto end;
  459. cert = NULL;
  460. argv++;
  461. }
  462. }
  463. if (certfile != NULL) {
  464. if (!load_certs(certfile, 0, &other, NULL, "certificates")) {
  465. ERR_print_errors(bio_err);
  466. goto end;
  467. }
  468. }
  469. if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
  470. if ((recip = load_cert(recipfile, FORMAT_UNDEF,
  471. "recipient certificate file")) == NULL) {
  472. ERR_print_errors(bio_err);
  473. goto end;
  474. }
  475. }
  476. if (operation == SMIME_DECRYPT) {
  477. if (keyfile == NULL)
  478. keyfile = recipfile;
  479. } else if (operation == SMIME_SIGN) {
  480. if (keyfile == NULL)
  481. keyfile = signerfile;
  482. } else {
  483. keyfile = NULL;
  484. }
  485. if (keyfile != NULL) {
  486. key = load_key(keyfile, keyform, 0, passin, e, "signing key");
  487. if (key == NULL)
  488. goto end;
  489. }
  490. in = bio_open_default(infile, 'r', informat);
  491. if (in == NULL)
  492. goto end;
  493. if (operation & SMIME_IP) {
  494. PKCS7 *p7_in = NULL;
  495. p7 = PKCS7_new_ex(libctx, app_get0_propq());
  496. if (p7 == NULL) {
  497. BIO_printf(bio_err, "Error allocating PKCS7 object\n");
  498. goto end;
  499. }
  500. if (informat == FORMAT_SMIME) {
  501. p7_in = SMIME_read_PKCS7_ex(in, &indata, &p7);
  502. } else if (informat == FORMAT_PEM) {
  503. p7_in = PEM_read_bio_PKCS7(in, &p7, NULL, NULL);
  504. } else if (informat == FORMAT_ASN1) {
  505. p7_in = d2i_PKCS7_bio(in, &p7);
  506. } else {
  507. BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
  508. goto end;
  509. }
  510. if (p7_in == NULL) {
  511. BIO_printf(bio_err, "Error reading S/MIME message\n");
  512. goto end;
  513. }
  514. if (contfile != NULL) {
  515. BIO_free(indata);
  516. if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
  517. BIO_printf(bio_err, "Can't read content file %s\n", contfile);
  518. goto end;
  519. }
  520. }
  521. }
  522. out = bio_open_default(outfile, 'w', outformat);
  523. if (out == NULL)
  524. goto end;
  525. if (operation == SMIME_VERIFY) {
  526. if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  527. CAstore, noCAstore)) == NULL)
  528. goto end;
  529. X509_STORE_set_verify_cb(store, smime_cb);
  530. if (vpmtouched)
  531. X509_STORE_set1_param(store, vpm);
  532. }
  533. ret = 3;
  534. if (operation == SMIME_ENCRYPT) {
  535. if (indef)
  536. flags |= PKCS7_STREAM;
  537. p7 = PKCS7_encrypt_ex(encerts, in, cipher, flags, libctx, app_get0_propq());
  538. } else if (operation & SMIME_SIGNERS) {
  539. int i;
  540. /*
  541. * If detached data content we only enable streaming if S/MIME output
  542. * format.
  543. */
  544. if (operation == SMIME_SIGN) {
  545. if (flags & PKCS7_DETACHED) {
  546. if (outformat == FORMAT_SMIME)
  547. flags |= PKCS7_STREAM;
  548. } else if (indef) {
  549. flags |= PKCS7_STREAM;
  550. }
  551. flags |= PKCS7_PARTIAL;
  552. p7 = PKCS7_sign_ex(NULL, NULL, other, in, flags, libctx, app_get0_propq());
  553. if (p7 == NULL)
  554. goto end;
  555. if (flags & PKCS7_NOCERTS) {
  556. for (i = 0; i < sk_X509_num(other); i++) {
  557. X509 *x = sk_X509_value(other, i);
  558. PKCS7_add_certificate(p7, x);
  559. }
  560. }
  561. } else {
  562. flags |= PKCS7_REUSE_DIGEST;
  563. }
  564. for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
  565. signerfile = sk_OPENSSL_STRING_value(sksigners, i);
  566. keyfile = sk_OPENSSL_STRING_value(skkeys, i);
  567. signer = load_cert(signerfile, FORMAT_UNDEF, "signer certificate");
  568. if (signer == NULL)
  569. goto end;
  570. key = load_key(keyfile, keyform, 0, passin, e, "signing key");
  571. if (key == NULL)
  572. goto end;
  573. if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))
  574. goto end;
  575. X509_free(signer);
  576. signer = NULL;
  577. EVP_PKEY_free(key);
  578. key = NULL;
  579. }
  580. /* If not streaming or resigning finalize structure */
  581. if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
  582. if (!PKCS7_final(p7, in, flags))
  583. goto end;
  584. }
  585. }
  586. if (p7 == NULL) {
  587. BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
  588. goto end;
  589. }
  590. ret = 4;
  591. if (operation == SMIME_DECRYPT) {
  592. if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
  593. BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
  594. goto end;
  595. }
  596. } else if (operation == SMIME_VERIFY) {
  597. STACK_OF(X509) *signers;
  598. if (PKCS7_verify(p7, other, store, indata, out, flags))
  599. BIO_printf(bio_err, "Verification successful\n");
  600. else {
  601. BIO_printf(bio_err, "Verification failure\n");
  602. goto end;
  603. }
  604. signers = PKCS7_get0_signers(p7, other, flags);
  605. if (!save_certs(signerfile, signers)) {
  606. BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
  607. ret = 5;
  608. goto end;
  609. }
  610. sk_X509_free(signers);
  611. } else if (operation == SMIME_PK7OUT) {
  612. PEM_write_bio_PKCS7(out, p7);
  613. } else {
  614. if (to)
  615. BIO_printf(out, "To: %s%s", to, mime_eol);
  616. if (from)
  617. BIO_printf(out, "From: %s%s", from, mime_eol);
  618. if (subject)
  619. BIO_printf(out, "Subject: %s%s", subject, mime_eol);
  620. if (outformat == FORMAT_SMIME) {
  621. if (operation == SMIME_RESIGN)
  622. rv = SMIME_write_PKCS7(out, p7, indata, flags);
  623. else
  624. rv = SMIME_write_PKCS7(out, p7, in, flags);
  625. } else if (outformat == FORMAT_PEM) {
  626. rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags);
  627. } else if (outformat == FORMAT_ASN1) {
  628. rv = i2d_PKCS7_bio_stream(out, p7, in, flags);
  629. } else {
  630. BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
  631. goto end;
  632. }
  633. if (rv == 0) {
  634. BIO_printf(bio_err, "Error writing output\n");
  635. ret = 3;
  636. goto end;
  637. }
  638. }
  639. ret = 0;
  640. end:
  641. if (ret)
  642. ERR_print_errors(bio_err);
  643. OSSL_STACK_OF_X509_free(encerts);
  644. OSSL_STACK_OF_X509_free(other);
  645. X509_VERIFY_PARAM_free(vpm);
  646. sk_OPENSSL_STRING_free(sksigners);
  647. sk_OPENSSL_STRING_free(skkeys);
  648. X509_STORE_free(store);
  649. X509_free(cert);
  650. X509_free(recip);
  651. X509_free(signer);
  652. EVP_PKEY_free(key);
  653. EVP_MD_free(sign_md);
  654. EVP_CIPHER_free(cipher);
  655. PKCS7_free(p7);
  656. release_engine(e);
  657. BIO_free(in);
  658. BIO_free(indata);
  659. BIO_free_all(out);
  660. OPENSSL_free(passin);
  661. NCONF_free(conf);
  662. return ret;
  663. }
  664. static int save_certs(char *signerfile, STACK_OF(X509) *signers)
  665. {
  666. int i;
  667. BIO *tmp;
  668. if (signerfile == NULL)
  669. return 1;
  670. tmp = BIO_new_file(signerfile, "w");
  671. if (tmp == NULL)
  672. return 0;
  673. for (i = 0; i < sk_X509_num(signers); i++)
  674. PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
  675. BIO_free(tmp);
  676. return 1;
  677. }
  678. /* Minimal callback just to output policy info (if any) */
  679. static int smime_cb(int ok, X509_STORE_CTX *ctx)
  680. {
  681. int error;
  682. error = X509_STORE_CTX_get_error(ctx);
  683. if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
  684. && ((error != X509_V_OK) || (ok != 2)))
  685. return ok;
  686. policies_print(ctx);
  687. return ok;
  688. }