smime.c 21 KB

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