smime.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /* smime.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. /* S/MIME utility function */
  60. #include <stdio.h>
  61. #include <string.h>
  62. #include "apps.h"
  63. #include <openssl/crypto.h>
  64. #include <openssl/pem.h>
  65. #include <openssl/err.h>
  66. #include <openssl/x509_vfy.h>
  67. #include <openssl/x509v3.h>
  68. #undef PROG
  69. #define PROG smime_main
  70. static int save_certs(char *signerfile, STACK_OF(X509) *signers);
  71. static int smime_cb(int ok, X509_STORE_CTX *ctx);
  72. #define SMIME_OP 0x10
  73. #define SMIME_ENCRYPT (1 | SMIME_OP)
  74. #define SMIME_DECRYPT 2
  75. #define SMIME_SIGN (3 | SMIME_OP)
  76. #define SMIME_VERIFY 4
  77. #define SMIME_PK7OUT 5
  78. int MAIN(int, char **);
  79. int MAIN(int argc, char **argv)
  80. {
  81. ENGINE *e = NULL;
  82. int operation = 0;
  83. int ret = 0;
  84. char **args;
  85. const char *inmode = "r", *outmode = "w";
  86. char *infile = NULL, *outfile = NULL;
  87. char *signerfile = NULL, *recipfile = NULL;
  88. char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
  89. const EVP_CIPHER *cipher = NULL;
  90. PKCS7 *p7 = NULL;
  91. X509_STORE *store = NULL;
  92. X509 *cert = NULL, *recip = NULL, *signer = NULL;
  93. EVP_PKEY *key = NULL;
  94. STACK_OF(X509) *encerts = NULL, *other = NULL;
  95. BIO *in = NULL, *out = NULL, *indata = NULL;
  96. int badarg = 0;
  97. int flags = PKCS7_DETACHED;
  98. char *to = NULL, *from = NULL, *subject = NULL;
  99. char *CAfile = NULL, *CApath = NULL;
  100. char *passargin = NULL, *passin = NULL;
  101. char *inrand = NULL;
  102. int need_rand = 0;
  103. int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
  104. int keyform = FORMAT_PEM;
  105. #ifndef OPENSSL_NO_ENGINE
  106. char *engine = NULL;
  107. #endif
  108. X509_VERIFY_PARAM *vpm = NULL;
  109. args = argv + 1;
  110. ret = 1;
  111. apps_startup();
  112. if (bio_err == NULL) {
  113. if ((bio_err = BIO_new(BIO_s_file())) != NULL)
  114. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  115. }
  116. if (!load_config(bio_err, NULL))
  117. goto end;
  118. while (!badarg && *args && *args[0] == '-') {
  119. if (!strcmp(*args, "-encrypt"))
  120. operation = SMIME_ENCRYPT;
  121. else if (!strcmp(*args, "-decrypt"))
  122. operation = SMIME_DECRYPT;
  123. else if (!strcmp(*args, "-sign"))
  124. operation = SMIME_SIGN;
  125. else if (!strcmp(*args, "-verify"))
  126. operation = SMIME_VERIFY;
  127. else if (!strcmp(*args, "-pk7out"))
  128. operation = SMIME_PK7OUT;
  129. #ifndef OPENSSL_NO_DES
  130. else if (!strcmp(*args, "-des3"))
  131. cipher = EVP_des_ede3_cbc();
  132. else if (!strcmp(*args, "-des"))
  133. cipher = EVP_des_cbc();
  134. #endif
  135. #ifndef OPENSSL_NO_SEED
  136. else if (!strcmp(*args, "-seed"))
  137. cipher = EVP_seed_cbc();
  138. #endif
  139. #ifndef OPENSSL_NO_RC2
  140. else if (!strcmp(*args, "-rc2-40"))
  141. cipher = EVP_rc2_40_cbc();
  142. else if (!strcmp(*args, "-rc2-128"))
  143. cipher = EVP_rc2_cbc();
  144. else if (!strcmp(*args, "-rc2-64"))
  145. cipher = EVP_rc2_64_cbc();
  146. #endif
  147. #ifndef OPENSSL_NO_AES
  148. else if (!strcmp(*args, "-aes128"))
  149. cipher = EVP_aes_128_cbc();
  150. else if (!strcmp(*args, "-aes192"))
  151. cipher = EVP_aes_192_cbc();
  152. else if (!strcmp(*args, "-aes256"))
  153. cipher = EVP_aes_256_cbc();
  154. #endif
  155. #ifndef OPENSSL_NO_CAMELLIA
  156. else if (!strcmp(*args, "-camellia128"))
  157. cipher = EVP_camellia_128_cbc();
  158. else if (!strcmp(*args, "-camellia192"))
  159. cipher = EVP_camellia_192_cbc();
  160. else if (!strcmp(*args, "-camellia256"))
  161. cipher = EVP_camellia_256_cbc();
  162. #endif
  163. else if (!strcmp(*args, "-text"))
  164. flags |= PKCS7_TEXT;
  165. else if (!strcmp(*args, "-nointern"))
  166. flags |= PKCS7_NOINTERN;
  167. else if (!strcmp(*args, "-noverify"))
  168. flags |= PKCS7_NOVERIFY;
  169. else if (!strcmp(*args, "-nochain"))
  170. flags |= PKCS7_NOCHAIN;
  171. else if (!strcmp(*args, "-nocerts"))
  172. flags |= PKCS7_NOCERTS;
  173. else if (!strcmp(*args, "-noattr"))
  174. flags |= PKCS7_NOATTR;
  175. else if (!strcmp(*args, "-nodetach"))
  176. flags &= ~PKCS7_DETACHED;
  177. else if (!strcmp(*args, "-nosmimecap"))
  178. flags |= PKCS7_NOSMIMECAP;
  179. else if (!strcmp(*args, "-binary"))
  180. flags |= PKCS7_BINARY;
  181. else if (!strcmp(*args, "-nosigs"))
  182. flags |= PKCS7_NOSIGS;
  183. else if (!strcmp(*args, "-nooldmime"))
  184. flags |= PKCS7_NOOLDMIMETYPE;
  185. else if (!strcmp(*args, "-crlfeol"))
  186. flags |= PKCS7_CRLFEOL;
  187. else if (!strcmp(*args, "-rand")) {
  188. if (args[1]) {
  189. args++;
  190. inrand = *args;
  191. } else
  192. badarg = 1;
  193. need_rand = 1;
  194. }
  195. #ifndef OPENSSL_NO_ENGINE
  196. else if (!strcmp(*args, "-engine")) {
  197. if (args[1]) {
  198. args++;
  199. engine = *args;
  200. } else
  201. badarg = 1;
  202. }
  203. #endif
  204. else if (!strcmp(*args, "-passin")) {
  205. if (args[1]) {
  206. args++;
  207. passargin = *args;
  208. } else
  209. badarg = 1;
  210. } else if (!strcmp(*args, "-to")) {
  211. if (args[1]) {
  212. args++;
  213. to = *args;
  214. } else
  215. badarg = 1;
  216. } else if (!strcmp(*args, "-from")) {
  217. if (args[1]) {
  218. args++;
  219. from = *args;
  220. } else
  221. badarg = 1;
  222. } else if (!strcmp(*args, "-subject")) {
  223. if (args[1]) {
  224. args++;
  225. subject = *args;
  226. } else
  227. badarg = 1;
  228. } else if (!strcmp(*args, "-signer")) {
  229. if (args[1]) {
  230. args++;
  231. signerfile = *args;
  232. } else
  233. badarg = 1;
  234. } else if (!strcmp(*args, "-recip")) {
  235. if (args[1]) {
  236. args++;
  237. recipfile = *args;
  238. } else
  239. badarg = 1;
  240. } else if (!strcmp(*args, "-inkey")) {
  241. if (args[1]) {
  242. args++;
  243. keyfile = *args;
  244. } else
  245. badarg = 1;
  246. } else if (!strcmp(*args, "-keyform")) {
  247. if (args[1]) {
  248. args++;
  249. keyform = str2fmt(*args);
  250. } else
  251. badarg = 1;
  252. } else if (!strcmp(*args, "-certfile")) {
  253. if (args[1]) {
  254. args++;
  255. certfile = *args;
  256. } else
  257. badarg = 1;
  258. } else if (!strcmp(*args, "-CAfile")) {
  259. if (args[1]) {
  260. args++;
  261. CAfile = *args;
  262. } else
  263. badarg = 1;
  264. } else if (!strcmp(*args, "-CApath")) {
  265. if (args[1]) {
  266. args++;
  267. CApath = *args;
  268. } else
  269. badarg = 1;
  270. } else if (!strcmp(*args, "-in")) {
  271. if (args[1]) {
  272. args++;
  273. infile = *args;
  274. } else
  275. badarg = 1;
  276. } else if (!strcmp(*args, "-inform")) {
  277. if (args[1]) {
  278. args++;
  279. informat = str2fmt(*args);
  280. } else
  281. badarg = 1;
  282. } else if (!strcmp(*args, "-outform")) {
  283. if (args[1]) {
  284. args++;
  285. outformat = str2fmt(*args);
  286. } else
  287. badarg = 1;
  288. } else if (!strcmp(*args, "-out")) {
  289. if (args[1]) {
  290. args++;
  291. outfile = *args;
  292. } else
  293. badarg = 1;
  294. } else if (!strcmp(*args, "-content")) {
  295. if (args[1]) {
  296. args++;
  297. contfile = *args;
  298. } else
  299. badarg = 1;
  300. } else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
  301. continue;
  302. else
  303. badarg = 1;
  304. args++;
  305. }
  306. if (operation == SMIME_SIGN) {
  307. if (!signerfile) {
  308. BIO_printf(bio_err, "No signer certificate specified\n");
  309. badarg = 1;
  310. }
  311. need_rand = 1;
  312. } else if (operation == SMIME_DECRYPT) {
  313. if (!recipfile && !keyfile) {
  314. BIO_printf(bio_err,
  315. "No recipient certificate or key specified\n");
  316. badarg = 1;
  317. }
  318. } else if (operation == SMIME_ENCRYPT) {
  319. if (!*args) {
  320. BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
  321. badarg = 1;
  322. }
  323. need_rand = 1;
  324. } else if (!operation)
  325. badarg = 1;
  326. if (badarg) {
  327. BIO_printf(bio_err, "Usage smime [options] cert.pem ...\n");
  328. BIO_printf(bio_err, "where options are\n");
  329. BIO_printf(bio_err, "-encrypt encrypt message\n");
  330. BIO_printf(bio_err, "-decrypt decrypt encrypted message\n");
  331. BIO_printf(bio_err, "-sign sign message\n");
  332. BIO_printf(bio_err, "-verify verify signed message\n");
  333. BIO_printf(bio_err, "-pk7out output PKCS#7 structure\n");
  334. #ifndef OPENSSL_NO_DES
  335. BIO_printf(bio_err, "-des3 encrypt with triple DES\n");
  336. BIO_printf(bio_err, "-des encrypt with DES\n");
  337. #endif
  338. #ifndef OPENSSL_NO_SEED
  339. BIO_printf(bio_err, "-seed encrypt with SEED\n");
  340. #endif
  341. #ifndef OPENSSL_NO_RC2
  342. BIO_printf(bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
  343. BIO_printf(bio_err, "-rc2-64 encrypt with RC2-64\n");
  344. BIO_printf(bio_err, "-rc2-128 encrypt with RC2-128\n");
  345. #endif
  346. #ifndef OPENSSL_NO_AES
  347. BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
  348. BIO_printf(bio_err,
  349. " encrypt PEM output with cbc aes\n");
  350. #endif
  351. #ifndef OPENSSL_NO_CAMELLIA
  352. BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
  353. BIO_printf(bio_err,
  354. " encrypt PEM output with cbc camellia\n");
  355. #endif
  356. BIO_printf(bio_err,
  357. "-nointern don't search certificates in message for signer\n");
  358. BIO_printf(bio_err,
  359. "-nosigs don't verify message signature\n");
  360. BIO_printf(bio_err,
  361. "-noverify don't verify signers certificate\n");
  362. BIO_printf(bio_err,
  363. "-nocerts don't include signers certificate when signing\n");
  364. BIO_printf(bio_err, "-nodetach use opaque signing\n");
  365. BIO_printf(bio_err,
  366. "-noattr don't include any signed attributes\n");
  367. BIO_printf(bio_err,
  368. "-binary don't translate message to text\n");
  369. BIO_printf(bio_err, "-certfile file other certificates file\n");
  370. BIO_printf(bio_err, "-signer file signer certificate file\n");
  371. BIO_printf(bio_err,
  372. "-recip file recipient certificate file for decryption\n");
  373. BIO_printf(bio_err, "-in file input file\n");
  374. BIO_printf(bio_err,
  375. "-inform arg input format SMIME (default), PEM or DER\n");
  376. BIO_printf(bio_err,
  377. "-inkey file input private key (if not signer or recipient)\n");
  378. BIO_printf(bio_err,
  379. "-keyform arg input private key format (PEM or ENGINE)\n");
  380. BIO_printf(bio_err, "-out file output file\n");
  381. BIO_printf(bio_err,
  382. "-outform arg output format SMIME (default), PEM or DER\n");
  383. BIO_printf(bio_err,
  384. "-content file supply or override content for detached signature\n");
  385. BIO_printf(bio_err, "-to addr to address\n");
  386. BIO_printf(bio_err, "-from ad from address\n");
  387. BIO_printf(bio_err, "-subject s subject\n");
  388. BIO_printf(bio_err,
  389. "-text include or delete text MIME headers\n");
  390. BIO_printf(bio_err,
  391. "-CApath dir trusted certificates directory\n");
  392. BIO_printf(bio_err, "-CAfile file trusted certificates file\n");
  393. BIO_printf(bio_err,
  394. "-crl_check check revocation status of signer's certificate using CRLs\n");
  395. BIO_printf(bio_err,
  396. "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
  397. #ifndef OPENSSL_NO_ENGINE
  398. BIO_printf(bio_err,
  399. "-engine e use engine e, possibly a hardware device.\n");
  400. #endif
  401. BIO_printf(bio_err, "-passin arg input file pass phrase source\n");
  402. BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
  403. LIST_SEPARATOR_CHAR);
  404. BIO_printf(bio_err,
  405. " load the file (or the files in the directory) into\n");
  406. BIO_printf(bio_err, " the random number generator\n");
  407. BIO_printf(bio_err,
  408. "cert.pem recipient certificate(s) for encryption\n");
  409. goto end;
  410. }
  411. #ifndef OPENSSL_NO_ENGINE
  412. e = setup_engine(bio_err, engine, 0);
  413. #endif
  414. if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
  415. BIO_printf(bio_err, "Error getting password\n");
  416. goto end;
  417. }
  418. if (need_rand) {
  419. app_RAND_load_file(NULL, bio_err, (inrand != NULL));
  420. if (inrand != NULL)
  421. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  422. app_RAND_load_files(inrand));
  423. }
  424. ret = 2;
  425. if (operation != SMIME_SIGN)
  426. flags &= ~PKCS7_DETACHED;
  427. if (operation & SMIME_OP) {
  428. if (flags & PKCS7_BINARY)
  429. inmode = "rb";
  430. if (outformat == FORMAT_ASN1)
  431. outmode = "wb";
  432. } else {
  433. if (flags & PKCS7_BINARY)
  434. outmode = "wb";
  435. if (informat == FORMAT_ASN1)
  436. inmode = "rb";
  437. }
  438. if (operation == SMIME_ENCRYPT) {
  439. if (!cipher) {
  440. #ifndef OPENSSL_NO_DES
  441. cipher = EVP_des_ede3_cbc();
  442. #else
  443. BIO_printf(bio_err, "No cipher selected\n");
  444. goto end;
  445. #endif
  446. }
  447. encerts = sk_X509_new_null();
  448. while (*args) {
  449. if (!(cert = load_cert(bio_err, *args, FORMAT_PEM,
  450. NULL, e, "recipient certificate file"))) {
  451. #if 0 /* An appropriate message is already printed */
  452. BIO_printf(bio_err,
  453. "Can't read recipient certificate file %s\n",
  454. *args);
  455. #endif
  456. goto end;
  457. }
  458. sk_X509_push(encerts, cert);
  459. cert = NULL;
  460. args++;
  461. }
  462. }
  463. if (signerfile && (operation == SMIME_SIGN)) {
  464. if (!(signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
  465. e, "signer certificate"))) {
  466. #if 0 /* An appropri message has already been
  467. * printed */
  468. BIO_printf(bio_err, "Can't read signer certificate file %s\n",
  469. signerfile);
  470. #endif
  471. goto end;
  472. }
  473. }
  474. if (certfile) {
  475. if (!(other = load_certs(bio_err, certfile, FORMAT_PEM, NULL,
  476. e, "certificate file"))) {
  477. #if 0 /* An appropriate message has already been
  478. * printed */
  479. BIO_printf(bio_err, "Can't read certificate file %s\n", certfile);
  480. #endif
  481. ERR_print_errors(bio_err);
  482. goto end;
  483. }
  484. }
  485. if (recipfile && (operation == SMIME_DECRYPT)) {
  486. if (!(recip = load_cert(bio_err, recipfile, FORMAT_PEM, NULL,
  487. e, "recipient certificate file"))) {
  488. #if 0 /* An appropriate message has alrady been
  489. * printed */
  490. BIO_printf(bio_err, "Can't read recipient certificate file %s\n",
  491. recipfile);
  492. #endif
  493. ERR_print_errors(bio_err);
  494. goto end;
  495. }
  496. }
  497. if (operation == SMIME_DECRYPT) {
  498. if (!keyfile)
  499. keyfile = recipfile;
  500. } else if (operation == SMIME_SIGN) {
  501. if (!keyfile)
  502. keyfile = signerfile;
  503. } else
  504. keyfile = NULL;
  505. if (keyfile) {
  506. key = load_key(bio_err, keyfile, keyform, 0, passin, e,
  507. "signing key file");
  508. if (!key)
  509. goto end;
  510. }
  511. if (infile) {
  512. if (!(in = BIO_new_file(infile, inmode))) {
  513. BIO_printf(bio_err, "Can't open input file %s\n", infile);
  514. goto end;
  515. }
  516. } else
  517. in = BIO_new_fp(stdin, BIO_NOCLOSE);
  518. if (outfile) {
  519. if (!(out = BIO_new_file(outfile, outmode))) {
  520. BIO_printf(bio_err, "Can't open output file %s\n", outfile);
  521. goto end;
  522. }
  523. } else {
  524. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  525. #ifdef OPENSSL_SYS_VMS
  526. {
  527. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  528. out = BIO_push(tmpbio, out);
  529. }
  530. #endif
  531. }
  532. if (operation == SMIME_VERIFY) {
  533. if (!(store = setup_verify(bio_err, CAfile, CApath)))
  534. goto end;
  535. X509_STORE_set_verify_cb_func(store, smime_cb);
  536. if (vpm)
  537. X509_STORE_set1_param(store, vpm);
  538. }
  539. ret = 3;
  540. if (operation == SMIME_ENCRYPT)
  541. p7 = PKCS7_encrypt(encerts, in, cipher, flags);
  542. else if (operation == SMIME_SIGN) {
  543. /*
  544. * If detached data and SMIME output enable partial signing.
  545. */
  546. if ((flags & PKCS7_DETACHED) && (outformat == FORMAT_SMIME))
  547. flags |= PKCS7_STREAM;
  548. p7 = PKCS7_sign(signer, key, other, in, flags);
  549. } else {
  550. if (informat == FORMAT_SMIME)
  551. p7 = SMIME_read_PKCS7(in, &indata);
  552. else if (informat == FORMAT_PEM)
  553. p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
  554. else if (informat == FORMAT_ASN1)
  555. p7 = d2i_PKCS7_bio(in, NULL);
  556. else {
  557. BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
  558. goto end;
  559. }
  560. if (!p7) {
  561. BIO_printf(bio_err, "Error reading S/MIME message\n");
  562. goto end;
  563. }
  564. if (contfile) {
  565. BIO_free(indata);
  566. if (!(indata = BIO_new_file(contfile, "rb"))) {
  567. BIO_printf(bio_err, "Can't read content file %s\n", contfile);
  568. goto end;
  569. }
  570. }
  571. }
  572. if (!p7) {
  573. BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
  574. goto end;
  575. }
  576. ret = 4;
  577. if (operation == SMIME_DECRYPT) {
  578. if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
  579. BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
  580. goto end;
  581. }
  582. } else if (operation == SMIME_VERIFY) {
  583. STACK_OF(X509) *signers;
  584. if (PKCS7_verify(p7, other, store, indata, out, flags))
  585. BIO_printf(bio_err, "Verification successful\n");
  586. else {
  587. BIO_printf(bio_err, "Verification failure\n");
  588. goto end;
  589. }
  590. signers = PKCS7_get0_signers(p7, other, flags);
  591. if (!save_certs(signerfile, signers)) {
  592. BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
  593. ret = 5;
  594. goto end;
  595. }
  596. sk_X509_free(signers);
  597. } else if (operation == SMIME_PK7OUT)
  598. PEM_write_bio_PKCS7(out, p7);
  599. else {
  600. if (to)
  601. BIO_printf(out, "To: %s\n", to);
  602. if (from)
  603. BIO_printf(out, "From: %s\n", from);
  604. if (subject)
  605. BIO_printf(out, "Subject: %s\n", subject);
  606. if (outformat == FORMAT_SMIME)
  607. SMIME_write_PKCS7(out, p7, in, flags);
  608. else if (outformat == FORMAT_PEM)
  609. PEM_write_bio_PKCS7(out, p7);
  610. else if (outformat == FORMAT_ASN1)
  611. i2d_PKCS7_bio(out, p7);
  612. else {
  613. BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
  614. goto end;
  615. }
  616. }
  617. ret = 0;
  618. end:
  619. if (need_rand)
  620. app_RAND_write_file(NULL, bio_err);
  621. if (ret)
  622. ERR_print_errors(bio_err);
  623. sk_X509_pop_free(encerts, X509_free);
  624. sk_X509_pop_free(other, X509_free);
  625. if (vpm)
  626. X509_VERIFY_PARAM_free(vpm);
  627. X509_STORE_free(store);
  628. X509_free(cert);
  629. X509_free(recip);
  630. X509_free(signer);
  631. EVP_PKEY_free(key);
  632. PKCS7_free(p7);
  633. BIO_free(in);
  634. BIO_free(indata);
  635. BIO_free_all(out);
  636. if (passin)
  637. OPENSSL_free(passin);
  638. return (ret);
  639. }
  640. static int save_certs(char *signerfile, STACK_OF(X509) *signers)
  641. {
  642. int i;
  643. BIO *tmp;
  644. if (!signerfile)
  645. return 1;
  646. tmp = BIO_new_file(signerfile, "w");
  647. if (!tmp)
  648. return 0;
  649. for (i = 0; i < sk_X509_num(signers); i++)
  650. PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
  651. BIO_free(tmp);
  652. return 1;
  653. }
  654. /* Minimal callback just to output policy info (if any) */
  655. static int smime_cb(int ok, X509_STORE_CTX *ctx)
  656. {
  657. int error;
  658. error = X509_STORE_CTX_get_error(ctx);
  659. if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
  660. && ((error != X509_V_OK) || (ok != 2)))
  661. return ok;
  662. policies_print(NULL, ctx);
  663. return ok;
  664. }