cms.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. /* apps/cms.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2008 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. /* CMS utility function */
  54. #include <stdio.h>
  55. #include <string.h>
  56. #include "apps.h"
  57. #ifndef OPENSSL_NO_CMS
  58. #include <openssl/crypto.h>
  59. #include <openssl/pem.h>
  60. #include <openssl/err.h>
  61. #include <openssl/x509_vfy.h>
  62. #include <openssl/x509v3.h>
  63. #include <openssl/cms.h>
  64. #undef PROG
  65. #define PROG cms_main
  66. static int save_certs(char *signerfile, STACK_OF(X509) *signers);
  67. static int cms_cb(int ok, X509_STORE_CTX *ctx);
  68. static void receipt_request_print(BIO *out, CMS_ContentInfo *cms);
  69. static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING) *rr_to,
  70. int rr_allorfirst,
  71. STACK_OF(OPENSSL_STRING) *rr_from);
  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_CMSOUT (5 | SMIME_IP | SMIME_OP)
  80. #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
  81. #define SMIME_DATAOUT (7 | SMIME_IP)
  82. #define SMIME_DATA_CREATE (8 | SMIME_OP)
  83. #define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
  84. #define SMIME_DIGEST_CREATE (10 | SMIME_OP)
  85. #define SMIME_UNCOMPRESS (11 | SMIME_IP)
  86. #define SMIME_COMPRESS (12 | SMIME_OP)
  87. #define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
  88. #define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
  89. #define SMIME_SIGN_RECEIPT (15 | SMIME_IP | SMIME_OP)
  90. #define SMIME_VERIFY_RECEIPT (16 | SMIME_IP)
  91. int verify_err = 0;
  92. int MAIN(int, char **);
  93. int MAIN(int argc, char **argv)
  94. {
  95. ENGINE *e = NULL;
  96. int operation = 0;
  97. int ret = 0;
  98. char **args;
  99. const char *inmode = "r", *outmode = "w";
  100. char *infile = NULL, *outfile = NULL, *rctfile = NULL;
  101. char *signerfile = NULL, *recipfile = NULL;
  102. STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
  103. char *certfile = NULL, *keyfile = NULL, *contfile=NULL;
  104. char *certsoutfile = NULL;
  105. const EVP_CIPHER *cipher = NULL;
  106. CMS_ContentInfo *cms = NULL, *rcms = NULL;
  107. X509_STORE *store = NULL;
  108. X509 *cert = NULL, *recip = NULL, *signer = NULL;
  109. EVP_PKEY *key = NULL;
  110. STACK_OF(X509) *encerts = NULL, *other = NULL;
  111. BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
  112. int badarg = 0;
  113. int flags = CMS_DETACHED, noout = 0, print = 0;
  114. int verify_retcode = 0;
  115. int rr_print = 0, rr_allorfirst = -1;
  116. STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL;
  117. CMS_ReceiptRequest *rr = NULL;
  118. char *to = NULL, *from = NULL, *subject = NULL;
  119. char *CAfile = NULL, *CApath = NULL;
  120. char *passargin = NULL, *passin = NULL;
  121. char *inrand = NULL;
  122. int need_rand = 0;
  123. const EVP_MD *sign_md = NULL;
  124. int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
  125. int rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
  126. #ifndef OPENSSL_NO_ENGINE
  127. char *engine=NULL;
  128. #endif
  129. unsigned char *secret_key = NULL, *secret_keyid = NULL;
  130. unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;
  131. size_t secret_keylen = 0, secret_keyidlen = 0;
  132. ASN1_OBJECT *econtent_type = NULL;
  133. X509_VERIFY_PARAM *vpm = NULL;
  134. args = argv + 1;
  135. ret = 1;
  136. apps_startup();
  137. if (bio_err == NULL)
  138. {
  139. if ((bio_err = BIO_new(BIO_s_file())) != NULL)
  140. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
  141. }
  142. if (!load_config(bio_err, NULL))
  143. goto end;
  144. while (!badarg && *args && *args[0] == '-')
  145. {
  146. if (!strcmp (*args, "-encrypt"))
  147. operation = SMIME_ENCRYPT;
  148. else if (!strcmp (*args, "-decrypt"))
  149. operation = SMIME_DECRYPT;
  150. else if (!strcmp (*args, "-sign"))
  151. operation = SMIME_SIGN;
  152. else if (!strcmp (*args, "-sign_receipt"))
  153. operation = SMIME_SIGN_RECEIPT;
  154. else if (!strcmp (*args, "-resign"))
  155. operation = SMIME_RESIGN;
  156. else if (!strcmp (*args, "-verify"))
  157. operation = SMIME_VERIFY;
  158. else if (!strcmp (*args, "-verify_retcode"))
  159. verify_retcode = 1;
  160. else if (!strcmp(*args,"-verify_receipt"))
  161. {
  162. operation = SMIME_VERIFY_RECEIPT;
  163. if (!args[1])
  164. goto argerr;
  165. args++;
  166. rctfile = *args;
  167. }
  168. else if (!strcmp (*args, "-cmsout"))
  169. operation = SMIME_CMSOUT;
  170. else if (!strcmp (*args, "-data_out"))
  171. operation = SMIME_DATAOUT;
  172. else if (!strcmp (*args, "-data_create"))
  173. operation = SMIME_DATA_CREATE;
  174. else if (!strcmp (*args, "-digest_verify"))
  175. operation = SMIME_DIGEST_VERIFY;
  176. else if (!strcmp (*args, "-digest_create"))
  177. operation = SMIME_DIGEST_CREATE;
  178. else if (!strcmp (*args, "-compress"))
  179. operation = SMIME_COMPRESS;
  180. else if (!strcmp (*args, "-uncompress"))
  181. operation = SMIME_UNCOMPRESS;
  182. else if (!strcmp (*args, "-EncryptedData_decrypt"))
  183. operation = SMIME_ENCRYPTED_DECRYPT;
  184. else if (!strcmp (*args, "-EncryptedData_encrypt"))
  185. operation = SMIME_ENCRYPTED_ENCRYPT;
  186. #ifndef OPENSSL_NO_DES
  187. else if (!strcmp (*args, "-des3"))
  188. cipher = EVP_des_ede3_cbc();
  189. else if (!strcmp (*args, "-des"))
  190. cipher = EVP_des_cbc();
  191. #endif
  192. #ifndef OPENSSL_NO_SEED
  193. else if (!strcmp (*args, "-seed"))
  194. cipher = EVP_seed_cbc();
  195. #endif
  196. #ifndef OPENSSL_NO_RC2
  197. else if (!strcmp (*args, "-rc2-40"))
  198. cipher = EVP_rc2_40_cbc();
  199. else if (!strcmp (*args, "-rc2-128"))
  200. cipher = EVP_rc2_cbc();
  201. else if (!strcmp (*args, "-rc2-64"))
  202. cipher = EVP_rc2_64_cbc();
  203. #endif
  204. #ifndef OPENSSL_NO_AES
  205. else if (!strcmp(*args,"-aes128"))
  206. cipher = EVP_aes_128_cbc();
  207. else if (!strcmp(*args,"-aes192"))
  208. cipher = EVP_aes_192_cbc();
  209. else if (!strcmp(*args,"-aes256"))
  210. cipher = EVP_aes_256_cbc();
  211. #endif
  212. #ifndef OPENSSL_NO_CAMELLIA
  213. else if (!strcmp(*args,"-camellia128"))
  214. cipher = EVP_camellia_128_cbc();
  215. else if (!strcmp(*args,"-camellia192"))
  216. cipher = EVP_camellia_192_cbc();
  217. else if (!strcmp(*args,"-camellia256"))
  218. cipher = EVP_camellia_256_cbc();
  219. #endif
  220. else if (!strcmp (*args, "-text"))
  221. flags |= CMS_TEXT;
  222. else if (!strcmp (*args, "-nointern"))
  223. flags |= CMS_NOINTERN;
  224. else if (!strcmp (*args, "-noverify")
  225. || !strcmp (*args, "-no_signer_cert_verify"))
  226. flags |= CMS_NO_SIGNER_CERT_VERIFY;
  227. else if (!strcmp (*args, "-nocerts"))
  228. flags |= CMS_NOCERTS;
  229. else if (!strcmp (*args, "-noattr"))
  230. flags |= CMS_NOATTR;
  231. else if (!strcmp (*args, "-nodetach"))
  232. flags &= ~CMS_DETACHED;
  233. else if (!strcmp (*args, "-nosmimecap"))
  234. flags |= CMS_NOSMIMECAP;
  235. else if (!strcmp (*args, "-binary"))
  236. flags |= CMS_BINARY;
  237. else if (!strcmp (*args, "-keyid"))
  238. flags |= CMS_USE_KEYID;
  239. else if (!strcmp (*args, "-nosigs"))
  240. flags |= CMS_NOSIGS;
  241. else if (!strcmp (*args, "-no_content_verify"))
  242. flags |= CMS_NO_CONTENT_VERIFY;
  243. else if (!strcmp (*args, "-no_attr_verify"))
  244. flags |= CMS_NO_ATTR_VERIFY;
  245. else if (!strcmp (*args, "-stream"))
  246. flags |= CMS_STREAM;
  247. else if (!strcmp (*args, "-indef"))
  248. flags |= CMS_STREAM;
  249. else if (!strcmp (*args, "-noindef"))
  250. flags &= ~CMS_STREAM;
  251. else if (!strcmp (*args, "-nooldmime"))
  252. flags |= CMS_NOOLDMIMETYPE;
  253. else if (!strcmp (*args, "-crlfeol"))
  254. flags |= CMS_CRLFEOL;
  255. else if (!strcmp (*args, "-noout"))
  256. noout = 1;
  257. else if (!strcmp (*args, "-receipt_request_print"))
  258. rr_print = 1;
  259. else if (!strcmp (*args, "-receipt_request_all"))
  260. rr_allorfirst = 0;
  261. else if (!strcmp (*args, "-receipt_request_first"))
  262. rr_allorfirst = 1;
  263. else if (!strcmp(*args,"-receipt_request_from"))
  264. {
  265. if (!args[1])
  266. goto argerr;
  267. args++;
  268. if (!rr_from)
  269. rr_from = sk_OPENSSL_STRING_new_null();
  270. sk_OPENSSL_STRING_push(rr_from, *args);
  271. }
  272. else if (!strcmp(*args,"-receipt_request_to"))
  273. {
  274. if (!args[1])
  275. goto argerr;
  276. args++;
  277. if (!rr_to)
  278. rr_to = sk_OPENSSL_STRING_new_null();
  279. sk_OPENSSL_STRING_push(rr_to, *args);
  280. }
  281. else if (!strcmp (*args, "-print"))
  282. {
  283. noout = 1;
  284. print = 1;
  285. }
  286. else if (!strcmp(*args,"-secretkey"))
  287. {
  288. long ltmp;
  289. if (!args[1])
  290. goto argerr;
  291. args++;
  292. secret_key = string_to_hex(*args, &ltmp);
  293. if (!secret_key)
  294. {
  295. BIO_printf(bio_err, "Invalid key %s\n", *args);
  296. goto argerr;
  297. }
  298. secret_keylen = (size_t)ltmp;
  299. }
  300. else if (!strcmp(*args,"-secretkeyid"))
  301. {
  302. long ltmp;
  303. if (!args[1])
  304. goto argerr;
  305. args++;
  306. secret_keyid = string_to_hex(*args, &ltmp);
  307. if (!secret_keyid)
  308. {
  309. BIO_printf(bio_err, "Invalid id %s\n", *args);
  310. goto argerr;
  311. }
  312. secret_keyidlen = (size_t)ltmp;
  313. }
  314. else if (!strcmp(*args,"-pwri_password"))
  315. {
  316. if (!args[1])
  317. goto argerr;
  318. args++;
  319. pwri_pass = (unsigned char *)*args;
  320. }
  321. else if (!strcmp(*args,"-econtent_type"))
  322. {
  323. if (!args[1])
  324. goto argerr;
  325. args++;
  326. econtent_type = OBJ_txt2obj(*args, 0);
  327. if (!econtent_type)
  328. {
  329. BIO_printf(bio_err, "Invalid OID %s\n", *args);
  330. goto argerr;
  331. }
  332. }
  333. else if (!strcmp(*args,"-rand"))
  334. {
  335. if (!args[1])
  336. goto argerr;
  337. args++;
  338. inrand = *args;
  339. need_rand = 1;
  340. }
  341. #ifndef OPENSSL_NO_ENGINE
  342. else if (!strcmp(*args,"-engine"))
  343. {
  344. if (!args[1])
  345. goto argerr;
  346. engine = *++args;
  347. }
  348. #endif
  349. else if (!strcmp(*args,"-passin"))
  350. {
  351. if (!args[1])
  352. goto argerr;
  353. passargin = *++args;
  354. }
  355. else if (!strcmp (*args, "-to"))
  356. {
  357. if (!args[1])
  358. goto argerr;
  359. to = *++args;
  360. }
  361. else if (!strcmp (*args, "-from"))
  362. {
  363. if (!args[1])
  364. goto argerr;
  365. from = *++args;
  366. }
  367. else if (!strcmp (*args, "-subject"))
  368. {
  369. if (!args[1])
  370. goto argerr;
  371. subject = *++args;
  372. }
  373. else if (!strcmp (*args, "-signer"))
  374. {
  375. if (!args[1])
  376. goto argerr;
  377. /* If previous -signer argument add signer to list */
  378. if (signerfile)
  379. {
  380. if (!sksigners)
  381. sksigners = sk_OPENSSL_STRING_new_null();
  382. sk_OPENSSL_STRING_push(sksigners, signerfile);
  383. if (!keyfile)
  384. keyfile = signerfile;
  385. if (!skkeys)
  386. skkeys = sk_OPENSSL_STRING_new_null();
  387. sk_OPENSSL_STRING_push(skkeys, keyfile);
  388. keyfile = NULL;
  389. }
  390. signerfile = *++args;
  391. }
  392. else if (!strcmp (*args, "-recip"))
  393. {
  394. if (!args[1])
  395. goto argerr;
  396. recipfile = *++args;
  397. }
  398. else if (!strcmp (*args, "-certsout"))
  399. {
  400. if (!args[1])
  401. goto argerr;
  402. certsoutfile = *++args;
  403. }
  404. else if (!strcmp (*args, "-md"))
  405. {
  406. if (!args[1])
  407. goto argerr;
  408. sign_md = EVP_get_digestbyname(*++args);
  409. if (sign_md == NULL)
  410. {
  411. BIO_printf(bio_err, "Unknown digest %s\n",
  412. *args);
  413. goto argerr;
  414. }
  415. }
  416. else if (!strcmp (*args, "-inkey"))
  417. {
  418. if (!args[1])
  419. goto argerr;
  420. /* If previous -inkey arument add signer to list */
  421. if (keyfile)
  422. {
  423. if (!signerfile)
  424. {
  425. BIO_puts(bio_err, "Illegal -inkey without -signer\n");
  426. goto argerr;
  427. }
  428. if (!sksigners)
  429. sksigners = sk_OPENSSL_STRING_new_null();
  430. sk_OPENSSL_STRING_push(sksigners, signerfile);
  431. signerfile = NULL;
  432. if (!skkeys)
  433. skkeys = sk_OPENSSL_STRING_new_null();
  434. sk_OPENSSL_STRING_push(skkeys, keyfile);
  435. }
  436. keyfile = *++args;
  437. }
  438. else if (!strcmp (*args, "-keyform"))
  439. {
  440. if (!args[1])
  441. goto argerr;
  442. keyform = str2fmt(*++args);
  443. }
  444. else if (!strcmp (*args, "-rctform"))
  445. {
  446. if (!args[1])
  447. goto argerr;
  448. rctformat = str2fmt(*++args);
  449. }
  450. else if (!strcmp (*args, "-certfile"))
  451. {
  452. if (!args[1])
  453. goto argerr;
  454. certfile = *++args;
  455. }
  456. else if (!strcmp (*args, "-CAfile"))
  457. {
  458. if (!args[1])
  459. goto argerr;
  460. CAfile = *++args;
  461. }
  462. else if (!strcmp (*args, "-CApath"))
  463. {
  464. if (!args[1])
  465. goto argerr;
  466. CApath = *++args;
  467. }
  468. else if (!strcmp (*args, "-in"))
  469. {
  470. if (!args[1])
  471. goto argerr;
  472. infile = *++args;
  473. }
  474. else if (!strcmp (*args, "-inform"))
  475. {
  476. if (!args[1])
  477. goto argerr;
  478. informat = str2fmt(*++args);
  479. }
  480. else if (!strcmp (*args, "-outform"))
  481. {
  482. if (!args[1])
  483. goto argerr;
  484. outformat = str2fmt(*++args);
  485. }
  486. else if (!strcmp (*args, "-out"))
  487. {
  488. if (!args[1])
  489. goto argerr;
  490. outfile = *++args;
  491. }
  492. else if (!strcmp (*args, "-content"))
  493. {
  494. if (!args[1])
  495. goto argerr;
  496. contfile = *++args;
  497. }
  498. else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
  499. continue;
  500. else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
  501. badarg = 1;
  502. args++;
  503. }
  504. if (((rr_allorfirst != -1) || rr_from) && !rr_to)
  505. {
  506. BIO_puts(bio_err, "No Signed Receipts Recipients\n");
  507. goto argerr;
  508. }
  509. if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from))
  510. {
  511. BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
  512. goto argerr;
  513. }
  514. if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners))
  515. {
  516. BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
  517. goto argerr;
  518. }
  519. if (operation & SMIME_SIGNERS)
  520. {
  521. if (keyfile && !signerfile)
  522. {
  523. BIO_puts(bio_err, "Illegal -inkey without -signer\n");
  524. goto argerr;
  525. }
  526. /* Check to see if any final signer needs to be appended */
  527. if (signerfile)
  528. {
  529. if (!sksigners)
  530. sksigners = sk_OPENSSL_STRING_new_null();
  531. sk_OPENSSL_STRING_push(sksigners, signerfile);
  532. if (!skkeys)
  533. skkeys = sk_OPENSSL_STRING_new_null();
  534. if (!keyfile)
  535. keyfile = signerfile;
  536. sk_OPENSSL_STRING_push(skkeys, keyfile);
  537. }
  538. if (!sksigners)
  539. {
  540. BIO_printf(bio_err, "No signer certificate specified\n");
  541. badarg = 1;
  542. }
  543. signerfile = NULL;
  544. keyfile = NULL;
  545. need_rand = 1;
  546. }
  547. else if (operation == SMIME_DECRYPT)
  548. {
  549. if (!recipfile && !keyfile && !secret_key && !pwri_pass)
  550. {
  551. BIO_printf(bio_err, "No recipient certificate or key specified\n");
  552. badarg = 1;
  553. }
  554. }
  555. else if (operation == SMIME_ENCRYPT)
  556. {
  557. if (!*args && !secret_key && !pwri_pass)
  558. {
  559. BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
  560. badarg = 1;
  561. }
  562. need_rand = 1;
  563. }
  564. else if (!operation)
  565. badarg = 1;
  566. if (badarg)
  567. {
  568. argerr:
  569. BIO_printf (bio_err, "Usage cms [options] cert.pem ...\n");
  570. BIO_printf (bio_err, "where options are\n");
  571. BIO_printf (bio_err, "-encrypt encrypt message\n");
  572. BIO_printf (bio_err, "-decrypt decrypt encrypted message\n");
  573. BIO_printf (bio_err, "-sign sign message\n");
  574. BIO_printf (bio_err, "-verify verify signed message\n");
  575. BIO_printf (bio_err, "-cmsout output CMS structure\n");
  576. #ifndef OPENSSL_NO_DES
  577. BIO_printf (bio_err, "-des3 encrypt with triple DES\n");
  578. BIO_printf (bio_err, "-des encrypt with DES\n");
  579. #endif
  580. #ifndef OPENSSL_NO_SEED
  581. BIO_printf (bio_err, "-seed encrypt with SEED\n");
  582. #endif
  583. #ifndef OPENSSL_NO_RC2
  584. BIO_printf (bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
  585. BIO_printf (bio_err, "-rc2-64 encrypt with RC2-64\n");
  586. BIO_printf (bio_err, "-rc2-128 encrypt with RC2-128\n");
  587. #endif
  588. #ifndef OPENSSL_NO_AES
  589. BIO_printf (bio_err, "-aes128, -aes192, -aes256\n");
  590. BIO_printf (bio_err, " encrypt PEM output with cbc aes\n");
  591. #endif
  592. #ifndef OPENSSL_NO_CAMELLIA
  593. BIO_printf (bio_err, "-camellia128, -camellia192, -camellia256\n");
  594. BIO_printf (bio_err, " encrypt PEM output with cbc camellia\n");
  595. #endif
  596. BIO_printf (bio_err, "-nointern don't search certificates in message for signer\n");
  597. BIO_printf (bio_err, "-nosigs don't verify message signature\n");
  598. BIO_printf (bio_err, "-noverify don't verify signers certificate\n");
  599. BIO_printf (bio_err, "-nocerts don't include signers certificate when signing\n");
  600. BIO_printf (bio_err, "-nodetach use opaque signing\n");
  601. BIO_printf (bio_err, "-noattr don't include any signed attributes\n");
  602. BIO_printf (bio_err, "-binary don't translate message to text\n");
  603. BIO_printf (bio_err, "-certfile file other certificates file\n");
  604. BIO_printf (bio_err, "-certsout file certificate output file\n");
  605. BIO_printf (bio_err, "-signer file signer certificate file\n");
  606. BIO_printf (bio_err, "-recip file recipient certificate file for decryption\n");
  607. BIO_printf (bio_err, "-keyid use subject key identifier\n");
  608. BIO_printf (bio_err, "-in file input file\n");
  609. BIO_printf (bio_err, "-inform arg input format SMIME (default), PEM or DER\n");
  610. BIO_printf (bio_err, "-inkey file input private key (if not signer or recipient)\n");
  611. BIO_printf (bio_err, "-keyform arg input private key format (PEM or ENGINE)\n");
  612. BIO_printf (bio_err, "-out file output file\n");
  613. BIO_printf (bio_err, "-outform arg output format SMIME (default), PEM or DER\n");
  614. BIO_printf (bio_err, "-content file supply or override content for detached signature\n");
  615. BIO_printf (bio_err, "-to addr to address\n");
  616. BIO_printf (bio_err, "-from ad from address\n");
  617. BIO_printf (bio_err, "-subject s subject\n");
  618. BIO_printf (bio_err, "-text include or delete text MIME headers\n");
  619. BIO_printf (bio_err, "-CApath dir trusted certificates directory\n");
  620. BIO_printf (bio_err, "-CAfile file trusted certificates file\n");
  621. BIO_printf (bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n");
  622. BIO_printf (bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
  623. #ifndef OPENSSL_NO_ENGINE
  624. BIO_printf (bio_err, "-engine e use engine e, possibly a hardware device.\n");
  625. #endif
  626. BIO_printf (bio_err, "-passin arg input file pass phrase source\n");
  627. BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
  628. BIO_printf(bio_err, " load the file (or the files in the directory) into\n");
  629. BIO_printf(bio_err, " the random number generator\n");
  630. BIO_printf (bio_err, "cert.pem recipient certificate(s) for encryption\n");
  631. goto end;
  632. }
  633. #ifndef OPENSSL_NO_ENGINE
  634. e = setup_engine(bio_err, engine, 0);
  635. #endif
  636. if (!app_passwd(bio_err, passargin, NULL, &passin, NULL))
  637. {
  638. BIO_printf(bio_err, "Error getting password\n");
  639. goto end;
  640. }
  641. if (need_rand)
  642. {
  643. app_RAND_load_file(NULL, bio_err, (inrand != NULL));
  644. if (inrand != NULL)
  645. BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
  646. app_RAND_load_files(inrand));
  647. }
  648. ret = 2;
  649. if (!(operation & SMIME_SIGNERS))
  650. flags &= ~CMS_DETACHED;
  651. if (operation & SMIME_OP)
  652. {
  653. if (outformat == FORMAT_ASN1)
  654. outmode = "wb";
  655. }
  656. else
  657. {
  658. if (flags & CMS_BINARY)
  659. outmode = "wb";
  660. }
  661. if (operation & SMIME_IP)
  662. {
  663. if (informat == FORMAT_ASN1)
  664. inmode = "rb";
  665. }
  666. else
  667. {
  668. if (flags & CMS_BINARY)
  669. inmode = "rb";
  670. }
  671. if (operation == SMIME_ENCRYPT)
  672. {
  673. if (!cipher)
  674. {
  675. #ifndef OPENSSL_NO_DES
  676. cipher = EVP_des_ede3_cbc();
  677. #else
  678. BIO_printf(bio_err, "No cipher selected\n");
  679. goto end;
  680. #endif
  681. }
  682. if (secret_key && !secret_keyid)
  683. {
  684. BIO_printf(bio_err, "No secret key id\n");
  685. goto end;
  686. }
  687. if (*args)
  688. encerts = sk_X509_new_null();
  689. while (*args)
  690. {
  691. if (!(cert = load_cert(bio_err,*args,FORMAT_PEM,
  692. NULL, e, "recipient certificate file")))
  693. goto end;
  694. sk_X509_push(encerts, cert);
  695. cert = NULL;
  696. args++;
  697. }
  698. }
  699. if (certfile)
  700. {
  701. if (!(other = load_certs(bio_err,certfile,FORMAT_PEM, NULL,
  702. e, "certificate file")))
  703. {
  704. ERR_print_errors(bio_err);
  705. goto end;
  706. }
  707. }
  708. if (recipfile && (operation == SMIME_DECRYPT))
  709. {
  710. if (!(recip = load_cert(bio_err,recipfile,FORMAT_PEM,NULL,
  711. e, "recipient certificate file")))
  712. {
  713. ERR_print_errors(bio_err);
  714. goto end;
  715. }
  716. }
  717. if (operation == SMIME_SIGN_RECEIPT)
  718. {
  719. if (!(signer = load_cert(bio_err,signerfile,FORMAT_PEM,NULL,
  720. e, "receipt signer certificate file")))
  721. {
  722. ERR_print_errors(bio_err);
  723. goto end;
  724. }
  725. }
  726. if (operation == SMIME_DECRYPT)
  727. {
  728. if (!keyfile)
  729. keyfile = recipfile;
  730. }
  731. else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT))
  732. {
  733. if (!keyfile)
  734. keyfile = signerfile;
  735. }
  736. else keyfile = NULL;
  737. if (keyfile)
  738. {
  739. key = load_key(bio_err, keyfile, keyform, 0, passin, e,
  740. "signing key file");
  741. if (!key)
  742. goto end;
  743. }
  744. if (infile)
  745. {
  746. if (!(in = BIO_new_file(infile, inmode)))
  747. {
  748. BIO_printf (bio_err,
  749. "Can't open input file %s\n", infile);
  750. goto end;
  751. }
  752. }
  753. else
  754. in = BIO_new_fp(stdin, BIO_NOCLOSE);
  755. if (operation & SMIME_IP)
  756. {
  757. if (informat == FORMAT_SMIME)
  758. cms = SMIME_read_CMS(in, &indata);
  759. else if (informat == FORMAT_PEM)
  760. cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
  761. else if (informat == FORMAT_ASN1)
  762. cms = d2i_CMS_bio(in, NULL);
  763. else
  764. {
  765. BIO_printf(bio_err, "Bad input format for CMS file\n");
  766. goto end;
  767. }
  768. if (!cms)
  769. {
  770. BIO_printf(bio_err, "Error reading S/MIME message\n");
  771. goto end;
  772. }
  773. if (contfile)
  774. {
  775. BIO_free(indata);
  776. if (!(indata = BIO_new_file(contfile, "rb")))
  777. {
  778. BIO_printf(bio_err, "Can't read content file %s\n", contfile);
  779. goto end;
  780. }
  781. }
  782. if (certsoutfile)
  783. {
  784. STACK_OF(X509) *allcerts;
  785. allcerts = CMS_get1_certs(cms);
  786. if (!save_certs(certsoutfile, allcerts))
  787. {
  788. BIO_printf(bio_err,
  789. "Error writing certs to %s\n",
  790. certsoutfile);
  791. ret = 5;
  792. goto end;
  793. }
  794. sk_X509_pop_free(allcerts, X509_free);
  795. }
  796. }
  797. if (rctfile)
  798. {
  799. char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
  800. if (!(rctin = BIO_new_file(rctfile, rctmode)))
  801. {
  802. BIO_printf (bio_err,
  803. "Can't open receipt file %s\n", rctfile);
  804. goto end;
  805. }
  806. if (rctformat == FORMAT_SMIME)
  807. rcms = SMIME_read_CMS(rctin, NULL);
  808. else if (rctformat == FORMAT_PEM)
  809. rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
  810. else if (rctformat == FORMAT_ASN1)
  811. rcms = d2i_CMS_bio(rctin, NULL);
  812. else
  813. {
  814. BIO_printf(bio_err, "Bad input format for receipt\n");
  815. goto end;
  816. }
  817. if (!rcms)
  818. {
  819. BIO_printf(bio_err, "Error reading receipt\n");
  820. goto end;
  821. }
  822. }
  823. if (outfile)
  824. {
  825. if (!(out = BIO_new_file(outfile, outmode)))
  826. {
  827. BIO_printf (bio_err,
  828. "Can't open output file %s\n", outfile);
  829. goto end;
  830. }
  831. }
  832. else
  833. {
  834. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  835. #ifdef OPENSSL_SYS_VMS
  836. {
  837. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  838. out = BIO_push(tmpbio, out);
  839. }
  840. #endif
  841. }
  842. if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT))
  843. {
  844. if (!(store = setup_verify(bio_err, CAfile, CApath)))
  845. goto end;
  846. X509_STORE_set_verify_cb(store, cms_cb);
  847. if (vpm)
  848. X509_STORE_set1_param(store, vpm);
  849. }
  850. ret = 3;
  851. if (operation == SMIME_DATA_CREATE)
  852. {
  853. cms = CMS_data_create(in, flags);
  854. }
  855. else if (operation == SMIME_DIGEST_CREATE)
  856. {
  857. cms = CMS_digest_create(in, sign_md, flags);
  858. }
  859. else if (operation == SMIME_COMPRESS)
  860. {
  861. cms = CMS_compress(in, -1, flags);
  862. }
  863. else if (operation == SMIME_ENCRYPT)
  864. {
  865. flags |= CMS_PARTIAL;
  866. cms = CMS_encrypt(encerts, in, cipher, flags);
  867. if (!cms)
  868. goto end;
  869. if (secret_key)
  870. {
  871. if (!CMS_add0_recipient_key(cms, NID_undef,
  872. secret_key, secret_keylen,
  873. secret_keyid, secret_keyidlen,
  874. NULL, NULL, NULL))
  875. goto end;
  876. /* NULL these because call absorbs them */
  877. secret_key = NULL;
  878. secret_keyid = NULL;
  879. }
  880. if (pwri_pass)
  881. {
  882. pwri_tmp = (unsigned char *)BUF_strdup((char *)pwri_pass);
  883. if (!pwri_tmp)
  884. goto end;
  885. if (!CMS_add0_recipient_password(cms,
  886. -1, NID_undef, NID_undef,
  887. pwri_tmp, -1, NULL))
  888. goto end;
  889. pwri_tmp = NULL;
  890. }
  891. if (!(flags & CMS_STREAM))
  892. {
  893. if (!CMS_final(cms, in, NULL, flags))
  894. goto end;
  895. }
  896. }
  897. else if (operation == SMIME_ENCRYPTED_ENCRYPT)
  898. {
  899. cms = CMS_EncryptedData_encrypt(in, cipher,
  900. secret_key, secret_keylen,
  901. flags);
  902. }
  903. else if (operation == SMIME_SIGN_RECEIPT)
  904. {
  905. CMS_ContentInfo *srcms = NULL;
  906. STACK_OF(CMS_SignerInfo) *sis;
  907. CMS_SignerInfo *si;
  908. sis = CMS_get0_SignerInfos(cms);
  909. if (!sis)
  910. goto end;
  911. si = sk_CMS_SignerInfo_value(sis, 0);
  912. srcms = CMS_sign_receipt(si, signer, key, other, flags);
  913. if (!srcms)
  914. goto end;
  915. CMS_ContentInfo_free(cms);
  916. cms = srcms;
  917. }
  918. else if (operation & SMIME_SIGNERS)
  919. {
  920. int i;
  921. /* If detached data content we enable streaming if
  922. * S/MIME output format.
  923. */
  924. if (operation == SMIME_SIGN)
  925. {
  926. if (flags & CMS_DETACHED)
  927. {
  928. if (outformat == FORMAT_SMIME)
  929. flags |= CMS_STREAM;
  930. }
  931. flags |= CMS_PARTIAL;
  932. cms = CMS_sign(NULL, NULL, other, in, flags);
  933. if (!cms)
  934. goto end;
  935. if (econtent_type)
  936. CMS_set1_eContentType(cms, econtent_type);
  937. if (rr_to)
  938. {
  939. rr = make_receipt_request(rr_to, rr_allorfirst,
  940. rr_from);
  941. if (!rr)
  942. {
  943. BIO_puts(bio_err,
  944. "Signed Receipt Request Creation Error\n");
  945. goto end;
  946. }
  947. }
  948. }
  949. else
  950. flags |= CMS_REUSE_DIGEST;
  951. for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++)
  952. {
  953. CMS_SignerInfo *si;
  954. signerfile = sk_OPENSSL_STRING_value(sksigners, i);
  955. keyfile = sk_OPENSSL_STRING_value(skkeys, i);
  956. signer = load_cert(bio_err, signerfile,FORMAT_PEM, NULL,
  957. e, "signer certificate");
  958. if (!signer)
  959. goto end;
  960. key = load_key(bio_err, keyfile, keyform, 0, passin, e,
  961. "signing key file");
  962. if (!key)
  963. goto end;
  964. si = CMS_add1_signer(cms, signer, key, sign_md, flags);
  965. if (!si)
  966. goto end;
  967. if (rr && !CMS_add1_ReceiptRequest(si, rr))
  968. goto end;
  969. X509_free(signer);
  970. signer = NULL;
  971. EVP_PKEY_free(key);
  972. key = NULL;
  973. }
  974. /* If not streaming or resigning finalize structure */
  975. if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM))
  976. {
  977. if (!CMS_final(cms, in, NULL, flags))
  978. goto end;
  979. }
  980. }
  981. if (!cms)
  982. {
  983. BIO_printf(bio_err, "Error creating CMS structure\n");
  984. goto end;
  985. }
  986. ret = 4;
  987. if (operation == SMIME_DECRYPT)
  988. {
  989. if (secret_key)
  990. {
  991. if (!CMS_decrypt_set1_key(cms,
  992. secret_key, secret_keylen,
  993. secret_keyid, secret_keyidlen))
  994. {
  995. BIO_puts(bio_err,
  996. "Error decrypting CMS using secret key\n");
  997. goto end;
  998. }
  999. }
  1000. if (key)
  1001. {
  1002. if (!CMS_decrypt_set1_pkey(cms, key, recip))
  1003. {
  1004. BIO_puts(bio_err,
  1005. "Error decrypting CMS using private key\n");
  1006. goto end;
  1007. }
  1008. }
  1009. if (pwri_pass)
  1010. {
  1011. if (!CMS_decrypt_set1_password(cms, pwri_pass, -1))
  1012. {
  1013. BIO_puts(bio_err,
  1014. "Error decrypting CMS using password\n");
  1015. goto end;
  1016. }
  1017. }
  1018. if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags))
  1019. {
  1020. BIO_printf(bio_err, "Error decrypting CMS structure\n");
  1021. goto end;
  1022. }
  1023. }
  1024. else if (operation == SMIME_DATAOUT)
  1025. {
  1026. if (!CMS_data(cms, out, flags))
  1027. goto end;
  1028. }
  1029. else if (operation == SMIME_UNCOMPRESS)
  1030. {
  1031. if (!CMS_uncompress(cms, indata, out, flags))
  1032. goto end;
  1033. }
  1034. else if (operation == SMIME_DIGEST_VERIFY)
  1035. {
  1036. if (CMS_digest_verify(cms, indata, out, flags) > 0)
  1037. BIO_printf(bio_err, "Verification successful\n");
  1038. else
  1039. {
  1040. BIO_printf(bio_err, "Verification failure\n");
  1041. goto end;
  1042. }
  1043. }
  1044. else if (operation == SMIME_ENCRYPTED_DECRYPT)
  1045. {
  1046. if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
  1047. indata, out, flags))
  1048. goto end;
  1049. }
  1050. else if (operation == SMIME_VERIFY)
  1051. {
  1052. if (CMS_verify(cms, other, store, indata, out, flags) > 0)
  1053. BIO_printf(bio_err, "Verification successful\n");
  1054. else
  1055. {
  1056. BIO_printf(bio_err, "Verification failure\n");
  1057. if (verify_retcode)
  1058. ret = verify_err + 32;
  1059. goto end;
  1060. }
  1061. if (signerfile)
  1062. {
  1063. STACK_OF(X509) *signers;
  1064. signers = CMS_get0_signers(cms);
  1065. if (!save_certs(signerfile, signers))
  1066. {
  1067. BIO_printf(bio_err,
  1068. "Error writing signers to %s\n",
  1069. signerfile);
  1070. ret = 5;
  1071. goto end;
  1072. }
  1073. sk_X509_free(signers);
  1074. }
  1075. if (rr_print)
  1076. receipt_request_print(bio_err, cms);
  1077. }
  1078. else if (operation == SMIME_VERIFY_RECEIPT)
  1079. {
  1080. if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0)
  1081. BIO_printf(bio_err, "Verification successful\n");
  1082. else
  1083. {
  1084. BIO_printf(bio_err, "Verification failure\n");
  1085. goto end;
  1086. }
  1087. }
  1088. else
  1089. {
  1090. if (noout)
  1091. {
  1092. if (print)
  1093. CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
  1094. }
  1095. else if (outformat == FORMAT_SMIME)
  1096. {
  1097. if (to)
  1098. BIO_printf(out, "To: %s\n", to);
  1099. if (from)
  1100. BIO_printf(out, "From: %s\n", from);
  1101. if (subject)
  1102. BIO_printf(out, "Subject: %s\n", subject);
  1103. if (operation == SMIME_RESIGN)
  1104. ret = SMIME_write_CMS(out, cms, indata, flags);
  1105. else
  1106. ret = SMIME_write_CMS(out, cms, in, flags);
  1107. }
  1108. else if (outformat == FORMAT_PEM)
  1109. ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
  1110. else if (outformat == FORMAT_ASN1)
  1111. ret = i2d_CMS_bio_stream(out,cms, in, flags);
  1112. else
  1113. {
  1114. BIO_printf(bio_err, "Bad output format for CMS file\n");
  1115. goto end;
  1116. }
  1117. if (ret <= 0)
  1118. {
  1119. ret = 6;
  1120. goto end;
  1121. }
  1122. }
  1123. ret = 0;
  1124. end:
  1125. if (ret)
  1126. ERR_print_errors(bio_err);
  1127. if (need_rand)
  1128. app_RAND_write_file(NULL, bio_err);
  1129. sk_X509_pop_free(encerts, X509_free);
  1130. sk_X509_pop_free(other, X509_free);
  1131. if (vpm)
  1132. X509_VERIFY_PARAM_free(vpm);
  1133. if (sksigners)
  1134. sk_OPENSSL_STRING_free(sksigners);
  1135. if (skkeys)
  1136. sk_OPENSSL_STRING_free(skkeys);
  1137. if (secret_key)
  1138. OPENSSL_free(secret_key);
  1139. if (secret_keyid)
  1140. OPENSSL_free(secret_keyid);
  1141. if (pwri_tmp)
  1142. OPENSSL_free(pwri_tmp);
  1143. if (econtent_type)
  1144. ASN1_OBJECT_free(econtent_type);
  1145. if (rr)
  1146. CMS_ReceiptRequest_free(rr);
  1147. if (rr_to)
  1148. sk_OPENSSL_STRING_free(rr_to);
  1149. if (rr_from)
  1150. sk_OPENSSL_STRING_free(rr_from);
  1151. X509_STORE_free(store);
  1152. X509_free(cert);
  1153. X509_free(recip);
  1154. X509_free(signer);
  1155. EVP_PKEY_free(key);
  1156. CMS_ContentInfo_free(cms);
  1157. CMS_ContentInfo_free(rcms);
  1158. BIO_free(rctin);
  1159. BIO_free(in);
  1160. BIO_free(indata);
  1161. BIO_free_all(out);
  1162. if (passin) OPENSSL_free(passin);
  1163. return (ret);
  1164. }
  1165. static int save_certs(char *signerfile, STACK_OF(X509) *signers)
  1166. {
  1167. int i;
  1168. BIO *tmp;
  1169. if (!signerfile)
  1170. return 1;
  1171. tmp = BIO_new_file(signerfile, "w");
  1172. if (!tmp) return 0;
  1173. for(i = 0; i < sk_X509_num(signers); i++)
  1174. PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
  1175. BIO_free(tmp);
  1176. return 1;
  1177. }
  1178. /* Minimal callback just to output policy info (if any) */
  1179. static int cms_cb(int ok, X509_STORE_CTX *ctx)
  1180. {
  1181. int error;
  1182. error = X509_STORE_CTX_get_error(ctx);
  1183. verify_err = error;
  1184. if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
  1185. && ((error != X509_V_OK) || (ok != 2)))
  1186. return ok;
  1187. policies_print(NULL, ctx);
  1188. return ok;
  1189. }
  1190. static void gnames_stack_print(BIO *out, STACK_OF(GENERAL_NAMES) *gns)
  1191. {
  1192. STACK_OF(GENERAL_NAME) *gens;
  1193. GENERAL_NAME *gen;
  1194. int i, j;
  1195. for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++)
  1196. {
  1197. gens = sk_GENERAL_NAMES_value(gns, i);
  1198. for (j = 0; j < sk_GENERAL_NAME_num(gens); j++)
  1199. {
  1200. gen = sk_GENERAL_NAME_value(gens, j);
  1201. BIO_puts(out, " ");
  1202. GENERAL_NAME_print(out, gen);
  1203. BIO_puts(out, "\n");
  1204. }
  1205. }
  1206. return;
  1207. }
  1208. static void receipt_request_print(BIO *out, CMS_ContentInfo *cms)
  1209. {
  1210. STACK_OF(CMS_SignerInfo) *sis;
  1211. CMS_SignerInfo *si;
  1212. CMS_ReceiptRequest *rr;
  1213. int allorfirst;
  1214. STACK_OF(GENERAL_NAMES) *rto, *rlist;
  1215. ASN1_STRING *scid;
  1216. int i, rv;
  1217. sis = CMS_get0_SignerInfos(cms);
  1218. for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++)
  1219. {
  1220. si = sk_CMS_SignerInfo_value(sis, i);
  1221. rv = CMS_get1_ReceiptRequest(si, &rr);
  1222. BIO_printf(bio_err, "Signer %d:\n", i + 1);
  1223. if (rv == 0)
  1224. BIO_puts(bio_err, " No Receipt Request\n");
  1225. else if (rv < 0)
  1226. {
  1227. BIO_puts(bio_err, " Receipt Request Parse Error\n");
  1228. ERR_print_errors(bio_err);
  1229. }
  1230. else
  1231. {
  1232. char *id;
  1233. int idlen;
  1234. CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
  1235. &rlist, &rto);
  1236. BIO_puts(out, " Signed Content ID:\n");
  1237. idlen = ASN1_STRING_length(scid);
  1238. id = (char *)ASN1_STRING_data(scid);
  1239. BIO_dump_indent(out, id, idlen, 4);
  1240. BIO_puts(out, " Receipts From");
  1241. if (rlist)
  1242. {
  1243. BIO_puts(out, " List:\n");
  1244. gnames_stack_print(out, rlist);
  1245. }
  1246. else if (allorfirst == 1)
  1247. BIO_puts(out, ": First Tier\n");
  1248. else if (allorfirst == 0)
  1249. BIO_puts(out, ": All\n");
  1250. else
  1251. BIO_printf(out, " Unknown (%d)\n", allorfirst);
  1252. BIO_puts(out, " Receipts To:\n");
  1253. gnames_stack_print(out, rto);
  1254. }
  1255. if (rr)
  1256. CMS_ReceiptRequest_free(rr);
  1257. }
  1258. }
  1259. static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
  1260. {
  1261. int i;
  1262. STACK_OF(GENERAL_NAMES) *ret;
  1263. GENERAL_NAMES *gens = NULL;
  1264. GENERAL_NAME *gen = NULL;
  1265. ret = sk_GENERAL_NAMES_new_null();
  1266. if (!ret)
  1267. goto err;
  1268. for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++)
  1269. {
  1270. char *str = sk_OPENSSL_STRING_value(ns, i);
  1271. gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
  1272. if (!gen)
  1273. goto err;
  1274. gens = GENERAL_NAMES_new();
  1275. if (!gens)
  1276. goto err;
  1277. if (!sk_GENERAL_NAME_push(gens, gen))
  1278. goto err;
  1279. gen = NULL;
  1280. if (!sk_GENERAL_NAMES_push(ret, gens))
  1281. goto err;
  1282. gens = NULL;
  1283. }
  1284. return ret;
  1285. err:
  1286. if (ret)
  1287. sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
  1288. if (gens)
  1289. GENERAL_NAMES_free(gens);
  1290. if (gen)
  1291. GENERAL_NAME_free(gen);
  1292. return NULL;
  1293. }
  1294. static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING) *rr_to,
  1295. int rr_allorfirst,
  1296. STACK_OF(OPENSSL_STRING) *rr_from)
  1297. {
  1298. STACK_OF(GENERAL_NAMES) *rct_to, *rct_from;
  1299. CMS_ReceiptRequest *rr;
  1300. rct_to = make_names_stack(rr_to);
  1301. if (!rct_to)
  1302. goto err;
  1303. if (rr_from)
  1304. {
  1305. rct_from = make_names_stack(rr_from);
  1306. if (!rct_from)
  1307. goto err;
  1308. }
  1309. else
  1310. rct_from = NULL;
  1311. rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,
  1312. rct_to);
  1313. return rr;
  1314. err:
  1315. return NULL;
  1316. }
  1317. #endif