smime.c 23 KB

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