smime.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. * Copyright 1999-2022 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_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. 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. EVP_CIPHER *cipher = NULL;
  128. 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;
  133. char *subject = NULL, *digestname = NULL, *ciphername = NULL;
  134. OPTION_CHOICE o;
  135. int noCApath = 0, noCAfile = 0, noCAstore = 0;
  136. int flags = PKCS7_DETACHED, operation = 0, ret = 0, indef = 0;
  137. int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform =
  138. FORMAT_UNDEF;
  139. int vpmtouched = 0, rv = 0;
  140. ENGINE *e = NULL;
  141. const char *mime_eol = "\n";
  142. OSSL_LIB_CTX *libctx = app_get0_libctx();
  143. if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
  144. return 1;
  145. opt_set_unknown_name("cipher");
  146. prog = opt_init(argc, argv, smime_options);
  147. while ((o = opt_next()) != OPT_EOF) {
  148. switch (o) {
  149. case OPT_EOF:
  150. case OPT_ERR:
  151. opthelp:
  152. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  153. goto end;
  154. case OPT_HELP:
  155. opt_help(smime_options);
  156. ret = 0;
  157. goto end;
  158. case OPT_INFORM:
  159. if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))
  160. goto opthelp;
  161. break;
  162. case OPT_IN:
  163. infile = opt_arg();
  164. break;
  165. case OPT_OUTFORM:
  166. if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))
  167. goto opthelp;
  168. break;
  169. case OPT_OUT:
  170. outfile = opt_arg();
  171. break;
  172. case OPT_ENCRYPT:
  173. operation = SMIME_ENCRYPT;
  174. break;
  175. case OPT_DECRYPT:
  176. operation = SMIME_DECRYPT;
  177. break;
  178. case OPT_SIGN:
  179. operation = SMIME_SIGN;
  180. break;
  181. case OPT_RESIGN:
  182. operation = SMIME_RESIGN;
  183. break;
  184. case OPT_VERIFY:
  185. operation = SMIME_VERIFY;
  186. break;
  187. case OPT_PK7OUT:
  188. operation = SMIME_PK7OUT;
  189. break;
  190. case OPT_TEXT:
  191. flags |= PKCS7_TEXT;
  192. break;
  193. case OPT_NOINTERN:
  194. flags |= PKCS7_NOINTERN;
  195. break;
  196. case OPT_NOVERIFY:
  197. flags |= PKCS7_NOVERIFY;
  198. break;
  199. case OPT_NOCHAIN:
  200. flags |= PKCS7_NOCHAIN;
  201. break;
  202. case OPT_NOCERTS:
  203. flags |= PKCS7_NOCERTS;
  204. break;
  205. case OPT_NOATTR:
  206. flags |= PKCS7_NOATTR;
  207. break;
  208. case OPT_NODETACH:
  209. flags &= ~PKCS7_DETACHED;
  210. break;
  211. case OPT_NOSMIMECAP:
  212. flags |= PKCS7_NOSMIMECAP;
  213. break;
  214. case OPT_BINARY:
  215. flags |= PKCS7_BINARY;
  216. break;
  217. case OPT_NOSIGS:
  218. flags |= PKCS7_NOSIGS;
  219. break;
  220. case OPT_STREAM:
  221. case OPT_INDEF:
  222. indef = 1;
  223. break;
  224. case OPT_NOINDEF:
  225. indef = 0;
  226. break;
  227. case OPT_CRLFEOL:
  228. flags |= PKCS7_CRLFEOL;
  229. mime_eol = "\r\n";
  230. break;
  231. case OPT_R_CASES:
  232. if (!opt_rand(o))
  233. goto end;
  234. break;
  235. case OPT_PROV_CASES:
  236. if (!opt_provider(o))
  237. goto end;
  238. break;
  239. case OPT_CONFIG:
  240. conf = app_load_config_modules(opt_arg());
  241. if (conf == NULL)
  242. goto end;
  243. break;
  244. case OPT_ENGINE:
  245. e = setup_engine(opt_arg(), 0);
  246. break;
  247. case OPT_PASSIN:
  248. passinarg = opt_arg();
  249. break;
  250. case OPT_TO:
  251. to = opt_arg();
  252. break;
  253. case OPT_FROM:
  254. from = opt_arg();
  255. break;
  256. case OPT_SUBJECT:
  257. subject = opt_arg();
  258. break;
  259. case OPT_SIGNER:
  260. /* If previous -signer argument add signer to list */
  261. if (signerfile != NULL) {
  262. if (sksigners == NULL
  263. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  264. goto end;
  265. sk_OPENSSL_STRING_push(sksigners, signerfile);
  266. if (keyfile == NULL)
  267. keyfile = signerfile;
  268. if (skkeys == NULL
  269. && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  270. goto end;
  271. sk_OPENSSL_STRING_push(skkeys, keyfile);
  272. keyfile = NULL;
  273. }
  274. signerfile = opt_arg();
  275. break;
  276. case OPT_RECIP:
  277. recipfile = opt_arg();
  278. break;
  279. case OPT_MD:
  280. digestname = opt_arg();
  281. break;
  282. case OPT_CIPHER:
  283. ciphername = opt_unknown();
  284. break;
  285. case OPT_INKEY:
  286. /* If previous -inkey argument add signer to list */
  287. if (keyfile != NULL) {
  288. if (signerfile == NULL) {
  289. BIO_printf(bio_err,
  290. "%s: Must have -signer before -inkey\n", prog);
  291. goto opthelp;
  292. }
  293. if (sksigners == NULL
  294. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  295. goto end;
  296. sk_OPENSSL_STRING_push(sksigners, signerfile);
  297. signerfile = NULL;
  298. if (skkeys == NULL
  299. && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  300. goto end;
  301. sk_OPENSSL_STRING_push(skkeys, keyfile);
  302. }
  303. keyfile = opt_arg();
  304. break;
  305. case OPT_KEYFORM:
  306. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
  307. goto opthelp;
  308. break;
  309. case OPT_CERTFILE:
  310. certfile = opt_arg();
  311. break;
  312. case OPT_CAFILE:
  313. CAfile = opt_arg();
  314. break;
  315. case OPT_CAPATH:
  316. CApath = opt_arg();
  317. break;
  318. case OPT_CASTORE:
  319. CAstore = opt_arg();
  320. break;
  321. case OPT_NOCAFILE:
  322. noCAfile = 1;
  323. break;
  324. case OPT_NOCAPATH:
  325. noCApath = 1;
  326. break;
  327. case OPT_NOCASTORE:
  328. noCAstore = 1;
  329. break;
  330. case OPT_CONTENT:
  331. contfile = opt_arg();
  332. break;
  333. case OPT_V_CASES:
  334. if (!opt_verify(o, vpm))
  335. goto opthelp;
  336. vpmtouched++;
  337. break;
  338. }
  339. }
  340. /* Extra arguments are files with recipient keys. */
  341. argc = opt_num_rest();
  342. argv = opt_rest();
  343. if (!app_RAND_load())
  344. goto end;
  345. if (digestname != NULL) {
  346. if (!opt_md(digestname, &sign_md))
  347. goto opthelp;
  348. }
  349. if (!opt_cipher_any(ciphername, &cipher))
  350. goto opthelp;
  351. if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
  352. BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
  353. goto opthelp;
  354. }
  355. if (!operation) {
  356. BIO_puts(bio_err,
  357. "No operation (-encrypt|-sign|...) specified\n");
  358. goto opthelp;
  359. }
  360. if (operation & SMIME_SIGNERS) {
  361. /* Check to see if any final signer needs to be appended */
  362. if (keyfile && !signerfile) {
  363. BIO_puts(bio_err, "Illegal -inkey without -signer\n");
  364. goto opthelp;
  365. }
  366. if (signerfile != NULL) {
  367. if (sksigners == NULL
  368. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  369. goto end;
  370. sk_OPENSSL_STRING_push(sksigners, signerfile);
  371. if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  372. goto end;
  373. if (!keyfile)
  374. keyfile = signerfile;
  375. sk_OPENSSL_STRING_push(skkeys, keyfile);
  376. }
  377. if (sksigners == NULL) {
  378. BIO_printf(bio_err, "No signer certificate specified\n");
  379. goto opthelp;
  380. }
  381. signerfile = NULL;
  382. keyfile = NULL;
  383. } else if (operation == SMIME_DECRYPT) {
  384. if (recipfile == NULL && keyfile == NULL) {
  385. BIO_printf(bio_err,
  386. "No recipient certificate or key specified\n");
  387. goto opthelp;
  388. }
  389. } else if (operation == SMIME_ENCRYPT) {
  390. if (argc == 0) {
  391. BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
  392. goto opthelp;
  393. }
  394. }
  395. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  396. BIO_printf(bio_err, "Error getting password\n");
  397. goto end;
  398. }
  399. ret = 2;
  400. if (!(operation & SMIME_SIGNERS))
  401. flags &= ~PKCS7_DETACHED;
  402. if (!(operation & SMIME_OP)) {
  403. if (flags & PKCS7_BINARY)
  404. outformat = FORMAT_BINARY;
  405. }
  406. if (!(operation & SMIME_IP)) {
  407. if (flags & PKCS7_BINARY)
  408. informat = FORMAT_BINARY;
  409. }
  410. if (operation == SMIME_ENCRYPT) {
  411. if (cipher == NULL) {
  412. #ifndef OPENSSL_NO_DES
  413. cipher = (EVP_CIPHER *)EVP_des_ede3_cbc();
  414. #else
  415. BIO_printf(bio_err, "No cipher selected\n");
  416. goto end;
  417. #endif
  418. }
  419. encerts = sk_X509_new_null();
  420. if (encerts == NULL)
  421. goto end;
  422. while (*argv != NULL) {
  423. cert = load_cert(*argv, FORMAT_UNDEF,
  424. "recipient certificate file");
  425. if (cert == NULL)
  426. goto end;
  427. sk_X509_push(encerts, cert);
  428. cert = NULL;
  429. argv++;
  430. }
  431. }
  432. if (certfile != NULL) {
  433. if (!load_certs(certfile, 0, &other, NULL, "certificates")) {
  434. ERR_print_errors(bio_err);
  435. goto end;
  436. }
  437. }
  438. if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
  439. if ((recip = load_cert(recipfile, FORMAT_UNDEF,
  440. "recipient certificate file")) == NULL) {
  441. ERR_print_errors(bio_err);
  442. goto end;
  443. }
  444. }
  445. if (operation == SMIME_DECRYPT) {
  446. if (keyfile == NULL)
  447. keyfile = recipfile;
  448. } else if (operation == SMIME_SIGN) {
  449. if (keyfile == NULL)
  450. keyfile = signerfile;
  451. } else {
  452. keyfile = NULL;
  453. }
  454. if (keyfile != NULL) {
  455. key = load_key(keyfile, keyform, 0, passin, e, "signing key");
  456. if (key == NULL)
  457. goto end;
  458. }
  459. in = bio_open_default(infile, 'r', informat);
  460. if (in == NULL)
  461. goto end;
  462. if (operation & SMIME_IP) {
  463. PKCS7 *p7_in = NULL;
  464. p7 = PKCS7_new_ex(libctx, app_get0_propq());
  465. if (p7 == NULL) {
  466. BIO_printf(bio_err, "Error allocating PKCS7 object\n");
  467. goto end;
  468. }
  469. if (informat == FORMAT_SMIME) {
  470. p7_in = SMIME_read_PKCS7_ex(in, &indata, &p7);
  471. } else if (informat == FORMAT_PEM) {
  472. p7_in = PEM_read_bio_PKCS7(in, &p7, NULL, NULL);
  473. } else if (informat == FORMAT_ASN1) {
  474. p7_in = d2i_PKCS7_bio(in, &p7);
  475. } else {
  476. BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
  477. goto end;
  478. }
  479. if (p7_in == NULL) {
  480. BIO_printf(bio_err, "Error reading S/MIME message\n");
  481. goto end;
  482. }
  483. if (contfile != NULL) {
  484. BIO_free(indata);
  485. if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
  486. BIO_printf(bio_err, "Can't read content file %s\n", contfile);
  487. goto end;
  488. }
  489. }
  490. }
  491. out = bio_open_default(outfile, 'w', outformat);
  492. if (out == NULL)
  493. goto end;
  494. if (operation == SMIME_VERIFY) {
  495. if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  496. CAstore, noCAstore)) == NULL)
  497. goto end;
  498. X509_STORE_set_verify_cb(store, smime_cb);
  499. if (vpmtouched)
  500. X509_STORE_set1_param(store, vpm);
  501. }
  502. ret = 3;
  503. if (operation == SMIME_ENCRYPT) {
  504. if (indef)
  505. flags |= PKCS7_STREAM;
  506. p7 = PKCS7_encrypt_ex(encerts, in, cipher, flags, libctx, app_get0_propq());
  507. } else if (operation & SMIME_SIGNERS) {
  508. int i;
  509. /*
  510. * If detached data content we only enable streaming if S/MIME output
  511. * format.
  512. */
  513. if (operation == SMIME_SIGN) {
  514. if (flags & PKCS7_DETACHED) {
  515. if (outformat == FORMAT_SMIME)
  516. flags |= PKCS7_STREAM;
  517. } else if (indef) {
  518. flags |= PKCS7_STREAM;
  519. }
  520. flags |= PKCS7_PARTIAL;
  521. p7 = PKCS7_sign_ex(NULL, NULL, other, in, flags, libctx, app_get0_propq());
  522. if (p7 == NULL)
  523. goto end;
  524. if (flags & PKCS7_NOCERTS) {
  525. for (i = 0; i < sk_X509_num(other); i++) {
  526. X509 *x = sk_X509_value(other, i);
  527. PKCS7_add_certificate(p7, x);
  528. }
  529. }
  530. } else {
  531. flags |= PKCS7_REUSE_DIGEST;
  532. }
  533. for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
  534. signerfile = sk_OPENSSL_STRING_value(sksigners, i);
  535. keyfile = sk_OPENSSL_STRING_value(skkeys, i);
  536. signer = load_cert(signerfile, FORMAT_UNDEF, "signer certificate");
  537. if (signer == NULL)
  538. goto end;
  539. key = load_key(keyfile, keyform, 0, passin, e, "signing key");
  540. if (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. OSSL_STACK_OF_X509_free(encerts);
  613. OSSL_STACK_OF_X509_free(other);
  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. EVP_MD_free(sign_md);
  623. EVP_CIPHER_free(cipher);
  624. PKCS7_free(p7);
  625. release_engine(e);
  626. BIO_free(in);
  627. BIO_free(indata);
  628. BIO_free_all(out);
  629. OPENSSL_free(passin);
  630. NCONF_free(conf);
  631. return ret;
  632. }
  633. static int save_certs(char *signerfile, STACK_OF(X509) *signers)
  634. {
  635. int i;
  636. BIO *tmp;
  637. if (signerfile == NULL)
  638. return 1;
  639. tmp = BIO_new_file(signerfile, "w");
  640. if (tmp == NULL)
  641. return 0;
  642. for (i = 0; i < sk_X509_num(signers); i++)
  643. PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
  644. BIO_free(tmp);
  645. return 1;
  646. }
  647. /* Minimal callback just to output policy info (if any) */
  648. static int smime_cb(int ok, X509_STORE_CTX *ctx)
  649. {
  650. int error;
  651. error = X509_STORE_CTX_get_error(ctx);
  652. if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
  653. && ((error != X509_V_OK) || (ok != 2)))
  654. return ok;
  655. policies_print(ctx);
  656. return ok;
  657. }