smime.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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_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. if (flags & PKCS7_NOCERTS) {
  604. for (i = 0; i < sk_X509_num(other); i++) {
  605. X509 *x = sk_X509_value(other, i);
  606. PKCS7_add_certificate(p7, x);
  607. }
  608. }
  609. } else
  610. flags |= PKCS7_REUSE_DIGEST;
  611. for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
  612. signerfile = sk_OPENSSL_STRING_value(sksigners, i);
  613. keyfile = sk_OPENSSL_STRING_value(skkeys, i);
  614. signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
  615. e, "signer certificate");
  616. if (!signer)
  617. goto end;
  618. key = load_key(bio_err, keyfile, keyform, 0, passin, e,
  619. "signing key file");
  620. if (!key)
  621. goto end;
  622. if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))
  623. goto end;
  624. X509_free(signer);
  625. signer = NULL;
  626. EVP_PKEY_free(key);
  627. key = NULL;
  628. }
  629. /* If not streaming or resigning finalize structure */
  630. if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
  631. if (!PKCS7_final(p7, in, flags))
  632. goto end;
  633. }
  634. }
  635. if (!p7) {
  636. BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
  637. goto end;
  638. }
  639. ret = 4;
  640. if (operation == SMIME_DECRYPT) {
  641. if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
  642. BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
  643. goto end;
  644. }
  645. } else if (operation == SMIME_VERIFY) {
  646. STACK_OF(X509) *signers;
  647. if (PKCS7_verify(p7, other, store, indata, out, flags))
  648. BIO_printf(bio_err, "Verification successful\n");
  649. else {
  650. BIO_printf(bio_err, "Verification failure\n");
  651. goto end;
  652. }
  653. signers = PKCS7_get0_signers(p7, other, flags);
  654. if (!save_certs(signerfile, signers)) {
  655. BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
  656. ret = 5;
  657. goto end;
  658. }
  659. sk_X509_free(signers);
  660. } else if (operation == SMIME_PK7OUT)
  661. PEM_write_bio_PKCS7(out, p7);
  662. else {
  663. if (to)
  664. BIO_printf(out, "To: %s\n", to);
  665. if (from)
  666. BIO_printf(out, "From: %s\n", from);
  667. if (subject)
  668. BIO_printf(out, "Subject: %s\n", subject);
  669. if (outformat == FORMAT_SMIME) {
  670. if (operation == SMIME_RESIGN)
  671. SMIME_write_PKCS7(out, p7, indata, flags);
  672. else
  673. SMIME_write_PKCS7(out, p7, in, flags);
  674. } else if (outformat == FORMAT_PEM)
  675. PEM_write_bio_PKCS7_stream(out, p7, in, flags);
  676. else if (outformat == FORMAT_ASN1)
  677. i2d_PKCS7_bio_stream(out, p7, in, flags);
  678. else {
  679. BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
  680. goto end;
  681. }
  682. }
  683. ret = 0;
  684. end:
  685. if (need_rand)
  686. app_RAND_write_file(NULL, bio_err);
  687. if (ret)
  688. ERR_print_errors(bio_err);
  689. sk_X509_pop_free(encerts, X509_free);
  690. sk_X509_pop_free(other, X509_free);
  691. if (vpm)
  692. X509_VERIFY_PARAM_free(vpm);
  693. if (sksigners)
  694. sk_OPENSSL_STRING_free(sksigners);
  695. if (skkeys)
  696. sk_OPENSSL_STRING_free(skkeys);
  697. X509_STORE_free(store);
  698. X509_free(cert);
  699. X509_free(recip);
  700. X509_free(signer);
  701. EVP_PKEY_free(key);
  702. PKCS7_free(p7);
  703. BIO_free(in);
  704. BIO_free(indata);
  705. BIO_free_all(out);
  706. if (passin)
  707. OPENSSL_free(passin);
  708. return (ret);
  709. }
  710. static int save_certs(char *signerfile, STACK_OF(X509) *signers)
  711. {
  712. int i;
  713. BIO *tmp;
  714. if (!signerfile)
  715. return 1;
  716. tmp = BIO_new_file(signerfile, "w");
  717. if (!tmp)
  718. return 0;
  719. for (i = 0; i < sk_X509_num(signers); i++)
  720. PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
  721. BIO_free(tmp);
  722. return 1;
  723. }
  724. /* Minimal callback just to output policy info (if any) */
  725. static int smime_cb(int ok, X509_STORE_CTX *ctx)
  726. {
  727. int error;
  728. error = X509_STORE_CTX_get_error(ctx);
  729. if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
  730. && ((error != X509_V_OK) || (ok != 2)))
  731. return ok;
  732. policies_print(NULL, ctx);
  733. return ok;
  734. }