2
0

smime.c 22 KB

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