smime.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /* smime.c */
  2. /*
  3. * Written by Dr Stephen N Henson ([email protected]) 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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  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_IP 0x20
  74. #define SMIME_SIGNERS 0x40
  75. #define SMIME_ENCRYPT (1 | SMIME_OP)
  76. #define SMIME_DECRYPT (2 | SMIME_IP)
  77. #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
  78. #define SMIME_VERIFY (4 | SMIME_IP)
  79. #define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP)
  80. #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
  81. int MAIN(int, char **);
  82. int MAIN(int argc, char **argv)
  83. {
  84. ENGINE *e = NULL;
  85. int operation = 0;
  86. int ret = 0;
  87. char **args;
  88. const char *inmode = "r", *outmode = "w";
  89. char *infile = NULL, *outfile = NULL;
  90. char *signerfile = NULL, *recipfile = NULL;
  91. STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
  92. char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
  93. const EVP_CIPHER *cipher = NULL;
  94. PKCS7 *p7 = NULL;
  95. X509_STORE *store = NULL;
  96. X509 *cert = NULL, *recip = NULL, *signer = NULL;
  97. EVP_PKEY *key = NULL;
  98. STACK_OF(X509) *encerts = NULL, *other = NULL;
  99. BIO *in = NULL, *out = NULL, *indata = NULL;
  100. int badarg = 0;
  101. int flags = PKCS7_DETACHED;
  102. char *to = NULL, *from = NULL, *subject = NULL;
  103. char *CAfile = NULL, *CApath = NULL;
  104. char *passargin = NULL, *passin = NULL;
  105. char *inrand = NULL;
  106. int need_rand = 0;
  107. int indef = 0;
  108. const EVP_MD *sign_md = NULL;
  109. int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
  110. int keyform = FORMAT_PEM;
  111. #ifndef OPENSSL_NO_ENGINE
  112. char *engine = NULL;
  113. #endif
  114. X509_VERIFY_PARAM *vpm = NULL;
  115. args = argv + 1;
  116. ret = 1;
  117. apps_startup();
  118. if (bio_err == NULL) {
  119. if ((bio_err = BIO_new(BIO_s_file())) != NULL)
  120. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  121. }
  122. if (!load_config(bio_err, NULL))
  123. goto end;
  124. while (!badarg && *args && *args[0] == '-') {
  125. if (!strcmp(*args, "-encrypt"))
  126. operation = SMIME_ENCRYPT;
  127. else if (!strcmp(*args, "-decrypt"))
  128. operation = SMIME_DECRYPT;
  129. else if (!strcmp(*args, "-sign"))
  130. operation = SMIME_SIGN;
  131. else if (!strcmp(*args, "-resign"))
  132. operation = SMIME_RESIGN;
  133. else if (!strcmp(*args, "-verify"))
  134. operation = SMIME_VERIFY;
  135. else if (!strcmp(*args, "-pk7out"))
  136. operation = SMIME_PK7OUT;
  137. #ifndef OPENSSL_NO_DES
  138. else if (!strcmp(*args, "-des3"))
  139. cipher = EVP_des_ede3_cbc();
  140. else if (!strcmp(*args, "-des"))
  141. cipher = EVP_des_cbc();
  142. #endif
  143. #ifndef OPENSSL_NO_SEED
  144. else if (!strcmp(*args, "-seed"))
  145. cipher = EVP_seed_cbc();
  146. #endif
  147. #ifndef OPENSSL_NO_RC2
  148. else if (!strcmp(*args, "-rc2-40"))
  149. cipher = EVP_rc2_40_cbc();
  150. else if (!strcmp(*args, "-rc2-128"))
  151. cipher = EVP_rc2_cbc();
  152. else if (!strcmp(*args, "-rc2-64"))
  153. cipher = EVP_rc2_64_cbc();
  154. #endif
  155. #ifndef OPENSSL_NO_AES
  156. else if (!strcmp(*args, "-aes128"))
  157. cipher = EVP_aes_128_cbc();
  158. else if (!strcmp(*args, "-aes192"))
  159. cipher = EVP_aes_192_cbc();
  160. else if (!strcmp(*args, "-aes256"))
  161. cipher = EVP_aes_256_cbc();
  162. #endif
  163. #ifndef OPENSSL_NO_CAMELLIA
  164. else if (!strcmp(*args, "-camellia128"))
  165. cipher = EVP_camellia_128_cbc();
  166. else if (!strcmp(*args, "-camellia192"))
  167. cipher = EVP_camellia_192_cbc();
  168. else if (!strcmp(*args, "-camellia256"))
  169. cipher = EVP_camellia_256_cbc();
  170. #endif
  171. else if (!strcmp(*args, "-text"))
  172. flags |= PKCS7_TEXT;
  173. else if (!strcmp(*args, "-nointern"))
  174. flags |= PKCS7_NOINTERN;
  175. else if (!strcmp(*args, "-noverify"))
  176. flags |= PKCS7_NOVERIFY;
  177. else if (!strcmp(*args, "-nochain"))
  178. flags |= PKCS7_NOCHAIN;
  179. else if (!strcmp(*args, "-nocerts"))
  180. flags |= PKCS7_NOCERTS;
  181. else if (!strcmp(*args, "-noattr"))
  182. flags |= PKCS7_NOATTR;
  183. else if (!strcmp(*args, "-nodetach"))
  184. flags &= ~PKCS7_DETACHED;
  185. else if (!strcmp(*args, "-nosmimecap"))
  186. flags |= PKCS7_NOSMIMECAP;
  187. else if (!strcmp(*args, "-binary"))
  188. flags |= PKCS7_BINARY;
  189. else if (!strcmp(*args, "-nosigs"))
  190. flags |= PKCS7_NOSIGS;
  191. else if (!strcmp(*args, "-stream"))
  192. indef = 1;
  193. else if (!strcmp(*args, "-indef"))
  194. indef = 1;
  195. else if (!strcmp(*args, "-noindef"))
  196. indef = 0;
  197. else if (!strcmp(*args, "-nooldmime"))
  198. flags |= PKCS7_NOOLDMIMETYPE;
  199. else if (!strcmp(*args, "-crlfeol"))
  200. flags |= PKCS7_CRLFEOL;
  201. else if (!strcmp(*args, "-rand")) {
  202. if (!args[1])
  203. goto argerr;
  204. args++;
  205. inrand = *args;
  206. need_rand = 1;
  207. }
  208. #ifndef OPENSSL_NO_ENGINE
  209. else if (!strcmp(*args, "-engine")) {
  210. if (!args[1])
  211. goto argerr;
  212. engine = *++args;
  213. }
  214. #endif
  215. else if (!strcmp(*args, "-passin")) {
  216. if (!args[1])
  217. goto argerr;
  218. passargin = *++args;
  219. } else if (!strcmp(*args, "-to")) {
  220. if (!args[1])
  221. goto argerr;
  222. to = *++args;
  223. } else if (!strcmp(*args, "-from")) {
  224. if (!args[1])
  225. goto argerr;
  226. from = *++args;
  227. } else if (!strcmp(*args, "-subject")) {
  228. if (!args[1])
  229. goto argerr;
  230. subject = *++args;
  231. } else if (!strcmp(*args, "-signer")) {
  232. if (!args[1])
  233. goto argerr;
  234. /* If previous -signer argument add signer to list */
  235. if (signerfile) {
  236. if (!sksigners)
  237. sksigners = sk_OPENSSL_STRING_new_null();
  238. sk_OPENSSL_STRING_push(sksigners, signerfile);
  239. if (!keyfile)
  240. keyfile = signerfile;
  241. if (!skkeys)
  242. skkeys = sk_OPENSSL_STRING_new_null();
  243. sk_OPENSSL_STRING_push(skkeys, keyfile);
  244. keyfile = NULL;
  245. }
  246. signerfile = *++args;
  247. } else if (!strcmp(*args, "-recip")) {
  248. if (!args[1])
  249. goto argerr;
  250. recipfile = *++args;
  251. } else if (!strcmp(*args, "-md")) {
  252. if (!args[1])
  253. goto argerr;
  254. sign_md = EVP_get_digestbyname(*++args);
  255. if (sign_md == NULL) {
  256. BIO_printf(bio_err, "Unknown digest %s\n", *args);
  257. goto argerr;
  258. }
  259. } else if (!strcmp(*args, "-inkey")) {
  260. if (!args[1])
  261. goto argerr;
  262. /* If previous -inkey arument add signer to list */
  263. if (keyfile) {
  264. if (!signerfile) {
  265. BIO_puts(bio_err, "Illegal -inkey without -signer\n");
  266. goto argerr;
  267. }
  268. if (!sksigners)
  269. sksigners = sk_OPENSSL_STRING_new_null();
  270. sk_OPENSSL_STRING_push(sksigners, signerfile);
  271. signerfile = NULL;
  272. if (!skkeys)
  273. skkeys = sk_OPENSSL_STRING_new_null();
  274. sk_OPENSSL_STRING_push(skkeys, keyfile);
  275. }
  276. keyfile = *++args;
  277. } else if (!strcmp(*args, "-keyform")) {
  278. if (!args[1])
  279. goto argerr;
  280. keyform = str2fmt(*++args);
  281. } else if (!strcmp(*args, "-certfile")) {
  282. if (!args[1])
  283. goto argerr;
  284. certfile = *++args;
  285. } else if (!strcmp(*args, "-CAfile")) {
  286. if (!args[1])
  287. goto argerr;
  288. CAfile = *++args;
  289. } else if (!strcmp(*args, "-CApath")) {
  290. if (!args[1])
  291. goto argerr;
  292. CApath = *++args;
  293. } else if (!strcmp(*args, "-in")) {
  294. if (!args[1])
  295. goto argerr;
  296. infile = *++args;
  297. } else if (!strcmp(*args, "-inform")) {
  298. if (!args[1])
  299. goto argerr;
  300. informat = str2fmt(*++args);
  301. } else if (!strcmp(*args, "-outform")) {
  302. if (!args[1])
  303. goto argerr;
  304. outformat = str2fmt(*++args);
  305. } else if (!strcmp(*args, "-out")) {
  306. if (!args[1])
  307. goto argerr;
  308. outfile = *++args;
  309. } else if (!strcmp(*args, "-content")) {
  310. if (!args[1])
  311. goto argerr;
  312. contfile = *++args;
  313. } else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
  314. continue;
  315. else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
  316. badarg = 1;
  317. args++;
  318. }
  319. if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
  320. BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
  321. goto argerr;
  322. }
  323. if (operation & SMIME_SIGNERS) {
  324. /* Check to see if any final signer needs to be appended */
  325. if (keyfile && !signerfile) {
  326. BIO_puts(bio_err, "Illegal -inkey without -signer\n");
  327. goto argerr;
  328. }
  329. if (signerfile) {
  330. if (!sksigners)
  331. sksigners = sk_OPENSSL_STRING_new_null();
  332. sk_OPENSSL_STRING_push(sksigners, signerfile);
  333. if (!skkeys)
  334. skkeys = sk_OPENSSL_STRING_new_null();
  335. if (!keyfile)
  336. keyfile = signerfile;
  337. sk_OPENSSL_STRING_push(skkeys, keyfile);
  338. }
  339. if (!sksigners) {
  340. BIO_printf(bio_err, "No signer certificate specified\n");
  341. badarg = 1;
  342. }
  343. signerfile = NULL;
  344. keyfile = NULL;
  345. need_rand = 1;
  346. } else if (operation == SMIME_DECRYPT) {
  347. if (!recipfile && !keyfile) {
  348. BIO_printf(bio_err,
  349. "No recipient certificate or key specified\n");
  350. badarg = 1;
  351. }
  352. } else if (operation == SMIME_ENCRYPT) {
  353. if (!*args) {
  354. BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
  355. badarg = 1;
  356. }
  357. need_rand = 1;
  358. } else if (!operation)
  359. badarg = 1;
  360. if (badarg) {
  361. argerr:
  362. BIO_printf(bio_err, "Usage smime [options] cert.pem ...\n");
  363. BIO_printf(bio_err, "where options are\n");
  364. BIO_printf(bio_err, "-encrypt encrypt message\n");
  365. BIO_printf(bio_err, "-decrypt decrypt encrypted message\n");
  366. BIO_printf(bio_err, "-sign sign message\n");
  367. BIO_printf(bio_err, "-verify verify signed message\n");
  368. BIO_printf(bio_err, "-pk7out output PKCS#7 structure\n");
  369. #ifndef OPENSSL_NO_DES
  370. BIO_printf(bio_err, "-des3 encrypt with triple DES\n");
  371. BIO_printf(bio_err, "-des encrypt with DES\n");
  372. #endif
  373. #ifndef OPENSSL_NO_SEED
  374. BIO_printf(bio_err, "-seed encrypt with SEED\n");
  375. #endif
  376. #ifndef OPENSSL_NO_RC2
  377. BIO_printf(bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
  378. BIO_printf(bio_err, "-rc2-64 encrypt with RC2-64\n");
  379. BIO_printf(bio_err, "-rc2-128 encrypt with RC2-128\n");
  380. #endif
  381. #ifndef OPENSSL_NO_AES
  382. BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
  383. BIO_printf(bio_err,
  384. " encrypt PEM output with cbc aes\n");
  385. #endif
  386. #ifndef OPENSSL_NO_CAMELLIA
  387. BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
  388. BIO_printf(bio_err,
  389. " encrypt PEM output with cbc camellia\n");
  390. #endif
  391. BIO_printf(bio_err,
  392. "-nointern don't search certificates in message for signer\n");
  393. BIO_printf(bio_err,
  394. "-nosigs don't verify message signature\n");
  395. BIO_printf(bio_err,
  396. "-noverify don't verify signers certificate\n");
  397. BIO_printf(bio_err,
  398. "-nocerts don't include signers certificate when signing\n");
  399. BIO_printf(bio_err, "-nodetach use opaque signing\n");
  400. BIO_printf(bio_err,
  401. "-noattr don't include any signed attributes\n");
  402. BIO_printf(bio_err,
  403. "-binary don't translate message to text\n");
  404. BIO_printf(bio_err, "-certfile file other certificates file\n");
  405. BIO_printf(bio_err, "-signer file signer certificate file\n");
  406. BIO_printf(bio_err,
  407. "-recip file recipient certificate file for decryption\n");
  408. BIO_printf(bio_err, "-in file input file\n");
  409. BIO_printf(bio_err,
  410. "-inform arg input format SMIME (default), PEM or DER\n");
  411. BIO_printf(bio_err,
  412. "-inkey file input private key (if not signer or recipient)\n");
  413. BIO_printf(bio_err,
  414. "-keyform arg input private key format (PEM or ENGINE)\n");
  415. BIO_printf(bio_err, "-out file output file\n");
  416. BIO_printf(bio_err,
  417. "-outform arg output format SMIME (default), PEM or DER\n");
  418. BIO_printf(bio_err,
  419. "-content file supply or override content for detached signature\n");
  420. BIO_printf(bio_err, "-to addr to address\n");
  421. BIO_printf(bio_err, "-from ad from address\n");
  422. BIO_printf(bio_err, "-subject s subject\n");
  423. BIO_printf(bio_err,
  424. "-text include or delete text MIME headers\n");
  425. BIO_printf(bio_err,
  426. "-CApath dir trusted certificates directory\n");
  427. BIO_printf(bio_err, "-CAfile file trusted certificates file\n");
  428. BIO_printf(bio_err,
  429. "-no_alt_chains only ever use the first certificate chain found\n");
  430. BIO_printf(bio_err,
  431. "-crl_check check revocation status of signer's certificate using CRLs\n");
  432. BIO_printf(bio_err,
  433. "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
  434. #ifndef OPENSSL_NO_ENGINE
  435. BIO_printf(bio_err,
  436. "-engine e use engine e, possibly a hardware device.\n");
  437. #endif
  438. BIO_printf(bio_err, "-passin arg input file pass phrase source\n");
  439. BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
  440. LIST_SEPARATOR_CHAR);
  441. BIO_printf(bio_err,
  442. " load the file (or the files in the directory) into\n");
  443. BIO_printf(bio_err, " the random number generator\n");
  444. BIO_printf(bio_err,
  445. "cert.pem recipient certificate(s) for encryption\n");
  446. goto end;
  447. }
  448. #ifndef OPENSSL_NO_ENGINE
  449. e = setup_engine(bio_err, engine, 0);
  450. #endif
  451. if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
  452. BIO_printf(bio_err, "Error getting password\n");
  453. goto end;
  454. }
  455. if (need_rand) {
  456. app_RAND_load_file(NULL, bio_err, (inrand != NULL));
  457. if (inrand != NULL)
  458. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  459. app_RAND_load_files(inrand));
  460. }
  461. ret = 2;
  462. if (!(operation & SMIME_SIGNERS))
  463. flags &= ~PKCS7_DETACHED;
  464. if (operation & SMIME_OP) {
  465. if (outformat == FORMAT_ASN1)
  466. outmode = "wb";
  467. } else {
  468. if (flags & PKCS7_BINARY)
  469. outmode = "wb";
  470. }
  471. if (operation & SMIME_IP) {
  472. if (informat == FORMAT_ASN1)
  473. inmode = "rb";
  474. } else {
  475. if (flags & PKCS7_BINARY)
  476. inmode = "rb";
  477. }
  478. if (operation == SMIME_ENCRYPT) {
  479. if (!cipher) {
  480. #ifndef OPENSSL_NO_DES
  481. cipher = EVP_des_ede3_cbc();
  482. #else
  483. BIO_printf(bio_err, "No cipher selected\n");
  484. goto end;
  485. #endif
  486. }
  487. encerts = sk_X509_new_null();
  488. while (*args) {
  489. if (!(cert = load_cert(bio_err, *args, FORMAT_PEM,
  490. NULL, e, "recipient certificate file"))) {
  491. #if 0 /* An appropriate message is already printed */
  492. BIO_printf(bio_err,
  493. "Can't read recipient certificate file %s\n",
  494. *args);
  495. #endif
  496. goto end;
  497. }
  498. sk_X509_push(encerts, cert);
  499. cert = NULL;
  500. args++;
  501. }
  502. }
  503. if (certfile) {
  504. if (!(other = load_certs(bio_err, certfile, FORMAT_PEM, NULL,
  505. e, "certificate file"))) {
  506. ERR_print_errors(bio_err);
  507. goto end;
  508. }
  509. }
  510. if (recipfile && (operation == SMIME_DECRYPT)) {
  511. if (!(recip = load_cert(bio_err, recipfile, FORMAT_PEM, NULL,
  512. e, "recipient certificate file"))) {
  513. ERR_print_errors(bio_err);
  514. goto end;
  515. }
  516. }
  517. if (operation == SMIME_DECRYPT) {
  518. if (!keyfile)
  519. keyfile = recipfile;
  520. } else if (operation == SMIME_SIGN) {
  521. if (!keyfile)
  522. keyfile = signerfile;
  523. } else
  524. keyfile = NULL;
  525. if (keyfile) {
  526. key = load_key(bio_err, keyfile, keyform, 0, passin, e,
  527. "signing key file");
  528. if (!key)
  529. goto end;
  530. }
  531. if (infile) {
  532. if (!(in = BIO_new_file(infile, inmode))) {
  533. BIO_printf(bio_err, "Can't open input file %s\n", infile);
  534. goto end;
  535. }
  536. } else
  537. in = BIO_new_fp(stdin, BIO_NOCLOSE);
  538. if (operation & SMIME_IP) {
  539. if (informat == FORMAT_SMIME)
  540. p7 = SMIME_read_PKCS7(in, &indata);
  541. else if (informat == FORMAT_PEM)
  542. p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
  543. else if (informat == FORMAT_ASN1)
  544. p7 = d2i_PKCS7_bio(in, NULL);
  545. else {
  546. BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
  547. goto end;
  548. }
  549. if (!p7) {
  550. BIO_printf(bio_err, "Error reading S/MIME message\n");
  551. goto end;
  552. }
  553. if (contfile) {
  554. BIO_free(indata);
  555. if (!(indata = BIO_new_file(contfile, "rb"))) {
  556. BIO_printf(bio_err, "Can't read content file %s\n", contfile);
  557. goto end;
  558. }
  559. }
  560. }
  561. if (outfile) {
  562. if (!(out = BIO_new_file(outfile, outmode))) {
  563. BIO_printf(bio_err, "Can't open output file %s\n", outfile);
  564. goto end;
  565. }
  566. } else {
  567. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  568. #ifdef OPENSSL_SYS_VMS
  569. {
  570. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  571. out = BIO_push(tmpbio, out);
  572. }
  573. #endif
  574. }
  575. if (operation == SMIME_VERIFY) {
  576. if (!(store = setup_verify(bio_err, CAfile, CApath)))
  577. goto end;
  578. X509_STORE_set_verify_cb(store, smime_cb);
  579. if (vpm)
  580. X509_STORE_set1_param(store, vpm);
  581. }
  582. ret = 3;
  583. if (operation == SMIME_ENCRYPT) {
  584. if (indef)
  585. flags |= PKCS7_STREAM;
  586. p7 = PKCS7_encrypt(encerts, in, cipher, flags);
  587. } else if (operation & SMIME_SIGNERS) {
  588. int i;
  589. /*
  590. * If detached data content we only enable streaming if S/MIME output
  591. * format.
  592. */
  593. if (operation == SMIME_SIGN) {
  594. if (flags & PKCS7_DETACHED) {
  595. if (outformat == FORMAT_SMIME)
  596. flags |= PKCS7_STREAM;
  597. } else if (indef)
  598. flags |= PKCS7_STREAM;
  599. flags |= PKCS7_PARTIAL;
  600. p7 = PKCS7_sign(NULL, NULL, other, in, flags);
  601. if (!p7)
  602. goto end;
  603. } else
  604. flags |= PKCS7_REUSE_DIGEST;
  605. for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
  606. signerfile = sk_OPENSSL_STRING_value(sksigners, i);
  607. keyfile = sk_OPENSSL_STRING_value(skkeys, i);
  608. signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
  609. e, "signer certificate");
  610. if (!signer)
  611. goto end;
  612. key = load_key(bio_err, keyfile, keyform, 0, passin, e,
  613. "signing key file");
  614. if (!key)
  615. goto end;
  616. if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))
  617. goto end;
  618. X509_free(signer);
  619. signer = NULL;
  620. EVP_PKEY_free(key);
  621. key = NULL;
  622. }
  623. /* If not streaming or resigning finalize structure */
  624. if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
  625. if (!PKCS7_final(p7, in, flags))
  626. goto end;
  627. }
  628. }
  629. if (!p7) {
  630. BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
  631. goto end;
  632. }
  633. ret = 4;
  634. if (operation == SMIME_DECRYPT) {
  635. if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
  636. BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
  637. goto end;
  638. }
  639. } else if (operation == SMIME_VERIFY) {
  640. STACK_OF(X509) *signers;
  641. if (PKCS7_verify(p7, other, store, indata, out, flags))
  642. BIO_printf(bio_err, "Verification successful\n");
  643. else {
  644. BIO_printf(bio_err, "Verification failure\n");
  645. goto end;
  646. }
  647. signers = PKCS7_get0_signers(p7, other, flags);
  648. if (!save_certs(signerfile, signers)) {
  649. BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
  650. ret = 5;
  651. goto end;
  652. }
  653. sk_X509_free(signers);
  654. } else if (operation == SMIME_PK7OUT)
  655. PEM_write_bio_PKCS7(out, p7);
  656. else {
  657. if (to)
  658. BIO_printf(out, "To: %s\n", to);
  659. if (from)
  660. BIO_printf(out, "From: %s\n", from);
  661. if (subject)
  662. BIO_printf(out, "Subject: %s\n", subject);
  663. if (outformat == FORMAT_SMIME) {
  664. if (operation == SMIME_RESIGN)
  665. SMIME_write_PKCS7(out, p7, indata, flags);
  666. else
  667. SMIME_write_PKCS7(out, p7, in, flags);
  668. } else if (outformat == FORMAT_PEM)
  669. PEM_write_bio_PKCS7_stream(out, p7, in, flags);
  670. else if (outformat == FORMAT_ASN1)
  671. i2d_PKCS7_bio_stream(out, p7, in, flags);
  672. else {
  673. BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
  674. goto end;
  675. }
  676. }
  677. ret = 0;
  678. end:
  679. if (need_rand)
  680. app_RAND_write_file(NULL, bio_err);
  681. if (ret)
  682. ERR_print_errors(bio_err);
  683. sk_X509_pop_free(encerts, X509_free);
  684. sk_X509_pop_free(other, X509_free);
  685. if (vpm)
  686. X509_VERIFY_PARAM_free(vpm);
  687. if (sksigners)
  688. sk_OPENSSL_STRING_free(sksigners);
  689. if (skkeys)
  690. sk_OPENSSL_STRING_free(skkeys);
  691. X509_STORE_free(store);
  692. X509_free(cert);
  693. X509_free(recip);
  694. X509_free(signer);
  695. EVP_PKEY_free(key);
  696. PKCS7_free(p7);
  697. BIO_free(in);
  698. BIO_free(indata);
  699. BIO_free_all(out);
  700. if (passin)
  701. OPENSSL_free(passin);
  702. return (ret);
  703. }
  704. static int save_certs(char *signerfile, STACK_OF(X509) *signers)
  705. {
  706. int i;
  707. BIO *tmp;
  708. if (!signerfile)
  709. return 1;
  710. tmp = BIO_new_file(signerfile, "w");
  711. if (!tmp)
  712. return 0;
  713. for (i = 0; i < sk_X509_num(signers); i++)
  714. PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
  715. BIO_free(tmp);
  716. return 1;
  717. }
  718. /* Minimal callback just to output policy info (if any) */
  719. static int smime_cb(int ok, X509_STORE_CTX *ctx)
  720. {
  721. int error;
  722. error = X509_STORE_CTX_get_error(ctx);
  723. if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
  724. && ((error != X509_V_OK) || (ok != 2)))
  725. return ok;
  726. policies_print(NULL, ctx);
  727. return ok;
  728. }