cms.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. /* apps/cms.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. */
  54. /* CMS utility function */
  55. #include <stdio.h>
  56. #include <string.h>
  57. #include "apps.h"
  58. #ifndef OPENSSL_NO_CMS
  59. # include <openssl/crypto.h>
  60. # include <openssl/pem.h>
  61. # include <openssl/err.h>
  62. # include <openssl/x509_vfy.h>
  63. # include <openssl/x509v3.h>
  64. # include <openssl/cms.h>
  65. # undef PROG
  66. # define PROG cms_main
  67. static int save_certs(char *signerfile, STACK_OF(X509) *signers);
  68. static int cms_cb(int ok, X509_STORE_CTX *ctx);
  69. static void receipt_request_print(BIO *out, CMS_ContentInfo *cms);
  70. static CMS_ReceiptRequest *make_receipt_request(STACK * rr_to,
  71. int rr_allorfirst,
  72. STACK * rr_from);
  73. # define SMIME_OP 0x10
  74. # define SMIME_IP 0x20
  75. # define SMIME_SIGNERS 0x40
  76. # define SMIME_ENCRYPT (1 | SMIME_OP)
  77. # define SMIME_DECRYPT (2 | SMIME_IP)
  78. # define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
  79. # define SMIME_VERIFY (4 | SMIME_IP)
  80. # define SMIME_CMSOUT (5 | SMIME_IP | SMIME_OP)
  81. # define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
  82. # define SMIME_DATAOUT (7 | SMIME_IP)
  83. # define SMIME_DATA_CREATE (8 | SMIME_OP)
  84. # define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
  85. # define SMIME_DIGEST_CREATE (10 | SMIME_OP)
  86. # define SMIME_UNCOMPRESS (11 | SMIME_IP)
  87. # define SMIME_COMPRESS (12 | SMIME_OP)
  88. # define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
  89. # define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
  90. # define SMIME_SIGN_RECEIPT (15 | SMIME_IP | SMIME_OP)
  91. # define SMIME_VERIFY_RECEIPT (16 | SMIME_IP)
  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 *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;
  114. int rr_print = 0, rr_allorfirst = -1;
  115. STACK *rr_to = NULL, *rr_from = NULL;
  116. CMS_ReceiptRequest *rr = NULL;
  117. char *to = NULL, *from = NULL, *subject = NULL;
  118. char *CAfile = NULL, *CApath = NULL;
  119. char *passargin = NULL, *passin = NULL;
  120. char *inrand = NULL;
  121. int need_rand = 0;
  122. const EVP_MD *sign_md = NULL;
  123. int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
  124. int rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
  125. # ifndef OPENSSL_NO_ENGINE
  126. char *engine = NULL;
  127. # endif
  128. unsigned char *secret_key = NULL, *secret_keyid = NULL;
  129. size_t secret_keylen = 0, secret_keyidlen = 0;
  130. ASN1_OBJECT *econtent_type = NULL;
  131. X509_VERIFY_PARAM *vpm = NULL;
  132. args = argv + 1;
  133. ret = 1;
  134. apps_startup();
  135. if (bio_err == NULL) {
  136. if ((bio_err = BIO_new(BIO_s_file())) != NULL)
  137. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  138. }
  139. if (!load_config(bio_err, NULL))
  140. goto end;
  141. while (!badarg && *args && *args[0] == '-') {
  142. if (!strcmp(*args, "-encrypt"))
  143. operation = SMIME_ENCRYPT;
  144. else if (!strcmp(*args, "-decrypt"))
  145. operation = SMIME_DECRYPT;
  146. else if (!strcmp(*args, "-sign"))
  147. operation = SMIME_SIGN;
  148. else if (!strcmp(*args, "-sign_receipt"))
  149. operation = SMIME_SIGN_RECEIPT;
  150. else if (!strcmp(*args, "-resign"))
  151. operation = SMIME_RESIGN;
  152. else if (!strcmp(*args, "-verify"))
  153. operation = SMIME_VERIFY;
  154. else if (!strcmp(*args, "-verify_receipt")) {
  155. operation = SMIME_VERIFY_RECEIPT;
  156. if (!args[1])
  157. goto argerr;
  158. args++;
  159. rctfile = *args;
  160. } else if (!strcmp(*args, "-cmsout"))
  161. operation = SMIME_CMSOUT;
  162. else if (!strcmp(*args, "-data_out"))
  163. operation = SMIME_DATAOUT;
  164. else if (!strcmp(*args, "-data_create"))
  165. operation = SMIME_DATA_CREATE;
  166. else if (!strcmp(*args, "-digest_verify"))
  167. operation = SMIME_DIGEST_VERIFY;
  168. else if (!strcmp(*args, "-digest_create"))
  169. operation = SMIME_DIGEST_CREATE;
  170. else if (!strcmp(*args, "-compress"))
  171. operation = SMIME_COMPRESS;
  172. else if (!strcmp(*args, "-uncompress"))
  173. operation = SMIME_UNCOMPRESS;
  174. else if (!strcmp(*args, "-EncryptedData_decrypt"))
  175. operation = SMIME_ENCRYPTED_DECRYPT;
  176. else if (!strcmp(*args, "-EncryptedData_encrypt"))
  177. operation = SMIME_ENCRYPTED_ENCRYPT;
  178. # ifndef OPENSSL_NO_DES
  179. else if (!strcmp(*args, "-des3"))
  180. cipher = EVP_des_ede3_cbc();
  181. else if (!strcmp(*args, "-des"))
  182. cipher = EVP_des_cbc();
  183. # endif
  184. # ifndef OPENSSL_NO_SEED
  185. else if (!strcmp(*args, "-seed"))
  186. cipher = EVP_seed_cbc();
  187. # endif
  188. # ifndef OPENSSL_NO_RC2
  189. else if (!strcmp(*args, "-rc2-40"))
  190. cipher = EVP_rc2_40_cbc();
  191. else if (!strcmp(*args, "-rc2-128"))
  192. cipher = EVP_rc2_cbc();
  193. else if (!strcmp(*args, "-rc2-64"))
  194. cipher = EVP_rc2_64_cbc();
  195. # endif
  196. # ifndef OPENSSL_NO_AES
  197. else if (!strcmp(*args, "-aes128"))
  198. cipher = EVP_aes_128_cbc();
  199. else if (!strcmp(*args, "-aes192"))
  200. cipher = EVP_aes_192_cbc();
  201. else if (!strcmp(*args, "-aes256"))
  202. cipher = EVP_aes_256_cbc();
  203. # endif
  204. # ifndef OPENSSL_NO_CAMELLIA
  205. else if (!strcmp(*args, "-camellia128"))
  206. cipher = EVP_camellia_128_cbc();
  207. else if (!strcmp(*args, "-camellia192"))
  208. cipher = EVP_camellia_192_cbc();
  209. else if (!strcmp(*args, "-camellia256"))
  210. cipher = EVP_camellia_256_cbc();
  211. # endif
  212. else if (!strcmp(*args, "-debug_decrypt"))
  213. flags |= CMS_DEBUG_DECRYPT;
  214. else if (!strcmp(*args, "-text"))
  215. flags |= CMS_TEXT;
  216. else if (!strcmp(*args, "-nointern"))
  217. flags |= CMS_NOINTERN;
  218. else if (!strcmp(*args, "-noverify")
  219. || !strcmp(*args, "-no_signer_cert_verify"))
  220. flags |= CMS_NO_SIGNER_CERT_VERIFY;
  221. else if (!strcmp(*args, "-nocerts"))
  222. flags |= CMS_NOCERTS;
  223. else if (!strcmp(*args, "-noattr"))
  224. flags |= CMS_NOATTR;
  225. else if (!strcmp(*args, "-nodetach"))
  226. flags &= ~CMS_DETACHED;
  227. else if (!strcmp(*args, "-nosmimecap"))
  228. flags |= CMS_NOSMIMECAP;
  229. else if (!strcmp(*args, "-binary"))
  230. flags |= CMS_BINARY;
  231. else if (!strcmp(*args, "-keyid"))
  232. flags |= CMS_USE_KEYID;
  233. else if (!strcmp(*args, "-nosigs"))
  234. flags |= CMS_NOSIGS;
  235. else if (!strcmp(*args, "-no_content_verify"))
  236. flags |= CMS_NO_CONTENT_VERIFY;
  237. else if (!strcmp(*args, "-no_attr_verify"))
  238. flags |= CMS_NO_ATTR_VERIFY;
  239. else if (!strcmp(*args, "-stream")) {
  240. args++;
  241. continue;
  242. } else if (!strcmp(*args, "-indef")) {
  243. args++;
  244. continue;
  245. } else if (!strcmp(*args, "-noindef"))
  246. flags &= ~CMS_STREAM;
  247. else if (!strcmp(*args, "-nooldmime"))
  248. flags |= CMS_NOOLDMIMETYPE;
  249. else if (!strcmp(*args, "-crlfeol"))
  250. flags |= CMS_CRLFEOL;
  251. else if (!strcmp(*args, "-receipt_request_print"))
  252. rr_print = 1;
  253. else if (!strcmp(*args, "-receipt_request_all"))
  254. rr_allorfirst = 0;
  255. else if (!strcmp(*args, "-receipt_request_first"))
  256. rr_allorfirst = 1;
  257. else if (!strcmp(*args, "-receipt_request_from")) {
  258. if (!args[1])
  259. goto argerr;
  260. args++;
  261. if (!rr_from)
  262. rr_from = sk_new_null();
  263. sk_push(rr_from, *args);
  264. } else if (!strcmp(*args, "-receipt_request_to")) {
  265. if (!args[1])
  266. goto argerr;
  267. args++;
  268. if (!rr_to)
  269. rr_to = sk_new_null();
  270. sk_push(rr_to, *args);
  271. } else if (!strcmp(*args, "-secretkey")) {
  272. long ltmp;
  273. if (!args[1])
  274. goto argerr;
  275. args++;
  276. secret_key = string_to_hex(*args, &ltmp);
  277. if (!secret_key) {
  278. BIO_printf(bio_err, "Invalid key %s\n", *args);
  279. goto argerr;
  280. }
  281. secret_keylen = (size_t)ltmp;
  282. } else if (!strcmp(*args, "-secretkeyid")) {
  283. long ltmp;
  284. if (!args[1])
  285. goto argerr;
  286. args++;
  287. secret_keyid = string_to_hex(*args, &ltmp);
  288. if (!secret_keyid) {
  289. BIO_printf(bio_err, "Invalid id %s\n", *args);
  290. goto argerr;
  291. }
  292. secret_keyidlen = (size_t)ltmp;
  293. } else if (!strcmp(*args, "-econtent_type")) {
  294. if (!args[1])
  295. goto argerr;
  296. args++;
  297. econtent_type = OBJ_txt2obj(*args, 0);
  298. if (!econtent_type) {
  299. BIO_printf(bio_err, "Invalid OID %s\n", *args);
  300. goto argerr;
  301. }
  302. } else if (!strcmp(*args, "-rand")) {
  303. if (!args[1])
  304. goto argerr;
  305. args++;
  306. inrand = *args;
  307. need_rand = 1;
  308. }
  309. # ifndef OPENSSL_NO_ENGINE
  310. else if (!strcmp(*args, "-engine")) {
  311. if (!args[1])
  312. goto argerr;
  313. engine = *++args;
  314. }
  315. # endif
  316. else if (!strcmp(*args, "-passin")) {
  317. if (!args[1])
  318. goto argerr;
  319. passargin = *++args;
  320. } else if (!strcmp(*args, "-to")) {
  321. if (!args[1])
  322. goto argerr;
  323. to = *++args;
  324. } else if (!strcmp(*args, "-from")) {
  325. if (!args[1])
  326. goto argerr;
  327. from = *++args;
  328. } else if (!strcmp(*args, "-subject")) {
  329. if (!args[1])
  330. goto argerr;
  331. subject = *++args;
  332. } else if (!strcmp(*args, "-signer")) {
  333. if (!args[1])
  334. goto argerr;
  335. /* If previous -signer argument add signer to list */
  336. if (signerfile) {
  337. if (!sksigners)
  338. sksigners = sk_new_null();
  339. sk_push(sksigners, signerfile);
  340. if (!keyfile)
  341. keyfile = signerfile;
  342. if (!skkeys)
  343. skkeys = sk_new_null();
  344. sk_push(skkeys, keyfile);
  345. keyfile = NULL;
  346. }
  347. signerfile = *++args;
  348. } else if (!strcmp(*args, "-recip")) {
  349. if (!args[1])
  350. goto argerr;
  351. recipfile = *++args;
  352. } else if (!strcmp(*args, "-certsout")) {
  353. if (!args[1])
  354. goto argerr;
  355. certsoutfile = *++args;
  356. } else if (!strcmp(*args, "-md")) {
  357. if (!args[1])
  358. goto argerr;
  359. sign_md = EVP_get_digestbyname(*++args);
  360. if (sign_md == NULL) {
  361. BIO_printf(bio_err, "Unknown digest %s\n", *args);
  362. goto argerr;
  363. }
  364. } else if (!strcmp(*args, "-inkey")) {
  365. if (!args[1])
  366. goto argerr;
  367. /* If previous -inkey arument add signer to list */
  368. if (keyfile) {
  369. if (!signerfile) {
  370. BIO_puts(bio_err, "Illegal -inkey without -signer\n");
  371. goto argerr;
  372. }
  373. if (!sksigners)
  374. sksigners = sk_new_null();
  375. sk_push(sksigners, signerfile);
  376. signerfile = NULL;
  377. if (!skkeys)
  378. skkeys = sk_new_null();
  379. sk_push(skkeys, keyfile);
  380. }
  381. keyfile = *++args;
  382. } else if (!strcmp(*args, "-keyform")) {
  383. if (!args[1])
  384. goto argerr;
  385. keyform = str2fmt(*++args);
  386. } else if (!strcmp(*args, "-rctform")) {
  387. if (!args[1])
  388. goto argerr;
  389. rctformat = str2fmt(*++args);
  390. } else if (!strcmp(*args, "-certfile")) {
  391. if (!args[1])
  392. goto argerr;
  393. certfile = *++args;
  394. } else if (!strcmp(*args, "-CAfile")) {
  395. if (!args[1])
  396. goto argerr;
  397. CAfile = *++args;
  398. } else if (!strcmp(*args, "-CApath")) {
  399. if (!args[1])
  400. goto argerr;
  401. CApath = *++args;
  402. } else if (!strcmp(*args, "-in")) {
  403. if (!args[1])
  404. goto argerr;
  405. infile = *++args;
  406. } else if (!strcmp(*args, "-inform")) {
  407. if (!args[1])
  408. goto argerr;
  409. informat = str2fmt(*++args);
  410. } else if (!strcmp(*args, "-outform")) {
  411. if (!args[1])
  412. goto argerr;
  413. outformat = str2fmt(*++args);
  414. } else if (!strcmp(*args, "-out")) {
  415. if (!args[1])
  416. goto argerr;
  417. outfile = *++args;
  418. } else if (!strcmp(*args, "-content")) {
  419. if (!args[1])
  420. goto argerr;
  421. contfile = *++args;
  422. } else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
  423. continue;
  424. else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
  425. badarg = 1;
  426. args++;
  427. }
  428. if (((rr_allorfirst != -1) || rr_from) && !rr_to) {
  429. BIO_puts(bio_err, "No Signed Receipts Recipients\n");
  430. goto argerr;
  431. }
  432. if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from)) {
  433. BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
  434. goto argerr;
  435. }
  436. if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
  437. BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
  438. goto argerr;
  439. }
  440. if (operation & SMIME_SIGNERS) {
  441. if (keyfile && !signerfile) {
  442. BIO_puts(bio_err, "Illegal -inkey without -signer\n");
  443. goto argerr;
  444. }
  445. /* Check to see if any final signer needs to be appended */
  446. if (signerfile) {
  447. if (!sksigners)
  448. sksigners = sk_new_null();
  449. sk_push(sksigners, signerfile);
  450. if (!skkeys)
  451. skkeys = sk_new_null();
  452. if (!keyfile)
  453. keyfile = signerfile;
  454. sk_push(skkeys, keyfile);
  455. }
  456. if (!sksigners) {
  457. BIO_printf(bio_err, "No signer certificate specified\n");
  458. badarg = 1;
  459. }
  460. signerfile = NULL;
  461. keyfile = NULL;
  462. need_rand = 1;
  463. }
  464. else if (operation == SMIME_DECRYPT) {
  465. if (!recipfile && !keyfile && !secret_key) {
  466. BIO_printf(bio_err,
  467. "No recipient certificate or key specified\n");
  468. badarg = 1;
  469. }
  470. } else if (operation == SMIME_ENCRYPT) {
  471. if (!*args && !secret_key) {
  472. BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
  473. badarg = 1;
  474. }
  475. need_rand = 1;
  476. } else if (!operation)
  477. badarg = 1;
  478. if (badarg) {
  479. argerr:
  480. BIO_printf(bio_err, "Usage cms [options] cert.pem ...\n");
  481. BIO_printf(bio_err, "where options are\n");
  482. BIO_printf(bio_err, "-encrypt encrypt message\n");
  483. BIO_printf(bio_err, "-decrypt decrypt encrypted message\n");
  484. BIO_printf(bio_err, "-sign sign message\n");
  485. BIO_printf(bio_err, "-verify verify signed message\n");
  486. BIO_printf(bio_err, "-cmsout output CMS structure\n");
  487. # ifndef OPENSSL_NO_DES
  488. BIO_printf(bio_err, "-des3 encrypt with triple DES\n");
  489. BIO_printf(bio_err, "-des encrypt with DES\n");
  490. # endif
  491. # ifndef OPENSSL_NO_SEED
  492. BIO_printf(bio_err, "-seed encrypt with SEED\n");
  493. # endif
  494. # ifndef OPENSSL_NO_RC2
  495. BIO_printf(bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
  496. BIO_printf(bio_err, "-rc2-64 encrypt with RC2-64\n");
  497. BIO_printf(bio_err, "-rc2-128 encrypt with RC2-128\n");
  498. # endif
  499. # ifndef OPENSSL_NO_AES
  500. BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
  501. BIO_printf(bio_err,
  502. " encrypt PEM output with cbc aes\n");
  503. # endif
  504. # ifndef OPENSSL_NO_CAMELLIA
  505. BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
  506. BIO_printf(bio_err,
  507. " encrypt PEM output with cbc camellia\n");
  508. # endif
  509. BIO_printf(bio_err,
  510. "-nointern don't search certificates in message for signer\n");
  511. BIO_printf(bio_err,
  512. "-nosigs don't verify message signature\n");
  513. BIO_printf(bio_err,
  514. "-noverify don't verify signers certificate\n");
  515. BIO_printf(bio_err,
  516. "-nocerts don't include signers certificate when signing\n");
  517. BIO_printf(bio_err, "-nodetach use opaque signing\n");
  518. BIO_printf(bio_err,
  519. "-noattr don't include any signed attributes\n");
  520. BIO_printf(bio_err,
  521. "-binary don't translate message to text\n");
  522. BIO_printf(bio_err, "-certfile file other certificates file\n");
  523. BIO_printf(bio_err, "-certsout file certificate output file\n");
  524. BIO_printf(bio_err, "-signer file signer certificate file\n");
  525. BIO_printf(bio_err,
  526. "-recip file recipient certificate file for decryption\n");
  527. BIO_printf(bio_err, "-keyid use subject key identifier\n");
  528. BIO_printf(bio_err, "-in file input file\n");
  529. BIO_printf(bio_err,
  530. "-inform arg input format SMIME (default), PEM or DER\n");
  531. BIO_printf(bio_err,
  532. "-inkey file input private key (if not signer or recipient)\n");
  533. BIO_printf(bio_err,
  534. "-keyform arg input private key format (PEM or ENGINE)\n");
  535. BIO_printf(bio_err, "-out file output file\n");
  536. BIO_printf(bio_err,
  537. "-outform arg output format SMIME (default), PEM or DER\n");
  538. BIO_printf(bio_err,
  539. "-content file supply or override content for detached signature\n");
  540. BIO_printf(bio_err, "-to addr to address\n");
  541. BIO_printf(bio_err, "-from ad from address\n");
  542. BIO_printf(bio_err, "-subject s subject\n");
  543. BIO_printf(bio_err,
  544. "-text include or delete text MIME headers\n");
  545. BIO_printf(bio_err,
  546. "-CApath dir trusted certificates directory\n");
  547. BIO_printf(bio_err, "-CAfile file trusted certificates file\n");
  548. BIO_printf(bio_err,
  549. "-crl_check check revocation status of signer's certificate using CRLs\n");
  550. BIO_printf(bio_err,
  551. "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
  552. # ifndef OPENSSL_NO_ENGINE
  553. BIO_printf(bio_err,
  554. "-engine e use engine e, possibly a hardware device.\n");
  555. # endif
  556. BIO_printf(bio_err, "-passin arg input file pass phrase source\n");
  557. BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
  558. LIST_SEPARATOR_CHAR);
  559. BIO_printf(bio_err,
  560. " load the file (or the files in the directory) into\n");
  561. BIO_printf(bio_err, " the random number generator\n");
  562. BIO_printf(bio_err,
  563. "cert.pem recipient certificate(s) for encryption\n");
  564. goto end;
  565. }
  566. # ifndef OPENSSL_NO_ENGINE
  567. e = setup_engine(bio_err, engine, 0);
  568. # endif
  569. if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
  570. BIO_printf(bio_err, "Error getting password\n");
  571. goto end;
  572. }
  573. if (need_rand) {
  574. app_RAND_load_file(NULL, bio_err, (inrand != NULL));
  575. if (inrand != NULL)
  576. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  577. app_RAND_load_files(inrand));
  578. }
  579. ret = 2;
  580. if (!(operation & SMIME_SIGNERS))
  581. flags &= ~CMS_DETACHED;
  582. if (operation & SMIME_OP) {
  583. if (outformat == FORMAT_ASN1)
  584. outmode = "wb";
  585. } else {
  586. if (flags & CMS_BINARY)
  587. outmode = "wb";
  588. }
  589. if (operation & SMIME_IP) {
  590. if (informat == FORMAT_ASN1)
  591. inmode = "rb";
  592. } else {
  593. if (flags & CMS_BINARY)
  594. inmode = "rb";
  595. }
  596. if (operation == SMIME_ENCRYPT) {
  597. if (!cipher) {
  598. # ifndef OPENSSL_NO_DES
  599. cipher = EVP_des_ede3_cbc();
  600. # else
  601. BIO_printf(bio_err, "No cipher selected\n");
  602. goto end;
  603. # endif
  604. }
  605. if (secret_key && !secret_keyid) {
  606. BIO_printf(bio_err, "No sectre key id\n");
  607. goto end;
  608. }
  609. if (*args)
  610. encerts = sk_X509_new_null();
  611. while (*args) {
  612. if (!(cert = load_cert(bio_err, *args, FORMAT_PEM,
  613. NULL, e, "recipient certificate file")))
  614. goto end;
  615. sk_X509_push(encerts, cert);
  616. cert = NULL;
  617. args++;
  618. }
  619. }
  620. if (certfile) {
  621. if (!(other = load_certs(bio_err, certfile, FORMAT_PEM, NULL,
  622. e, "certificate file"))) {
  623. ERR_print_errors(bio_err);
  624. goto end;
  625. }
  626. }
  627. if (recipfile && (operation == SMIME_DECRYPT)) {
  628. if (!(recip = load_cert(bio_err, recipfile, FORMAT_PEM, NULL,
  629. e, "recipient certificate file"))) {
  630. ERR_print_errors(bio_err);
  631. goto end;
  632. }
  633. }
  634. if (operation == SMIME_SIGN_RECEIPT) {
  635. if (!(signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
  636. e, "receipt signer certificate file"))) {
  637. ERR_print_errors(bio_err);
  638. goto end;
  639. }
  640. }
  641. if (operation == SMIME_DECRYPT) {
  642. if (!keyfile)
  643. keyfile = recipfile;
  644. } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
  645. if (!keyfile)
  646. keyfile = signerfile;
  647. } else
  648. keyfile = NULL;
  649. if (keyfile) {
  650. key = load_key(bio_err, keyfile, keyform, 0, passin, e,
  651. "signing key file");
  652. if (!key)
  653. goto end;
  654. }
  655. if (infile) {
  656. if (!(in = BIO_new_file(infile, inmode))) {
  657. BIO_printf(bio_err, "Can't open input file %s\n", infile);
  658. goto end;
  659. }
  660. } else
  661. in = BIO_new_fp(stdin, BIO_NOCLOSE);
  662. if (operation & SMIME_IP) {
  663. if (informat == FORMAT_SMIME)
  664. cms = SMIME_read_CMS(in, &indata);
  665. else if (informat == FORMAT_PEM)
  666. cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
  667. else if (informat == FORMAT_ASN1)
  668. cms = d2i_CMS_bio(in, NULL);
  669. else {
  670. BIO_printf(bio_err, "Bad input format for CMS file\n");
  671. goto end;
  672. }
  673. if (!cms) {
  674. BIO_printf(bio_err, "Error reading S/MIME message\n");
  675. goto end;
  676. }
  677. if (contfile) {
  678. BIO_free(indata);
  679. if (!(indata = BIO_new_file(contfile, "rb"))) {
  680. BIO_printf(bio_err, "Can't read content file %s\n", contfile);
  681. goto end;
  682. }
  683. }
  684. if (certsoutfile) {
  685. STACK_OF(X509) *allcerts;
  686. allcerts = CMS_get1_certs(cms);
  687. if (!save_certs(certsoutfile, allcerts)) {
  688. BIO_printf(bio_err,
  689. "Error writing certs to %s\n", certsoutfile);
  690. ret = 5;
  691. goto end;
  692. }
  693. sk_X509_pop_free(allcerts, X509_free);
  694. }
  695. }
  696. if (rctfile) {
  697. char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
  698. if (!(rctin = BIO_new_file(rctfile, rctmode))) {
  699. BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
  700. goto end;
  701. }
  702. if (rctformat == FORMAT_SMIME)
  703. rcms = SMIME_read_CMS(rctin, NULL);
  704. else if (rctformat == FORMAT_PEM)
  705. rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
  706. else if (rctformat == FORMAT_ASN1)
  707. rcms = d2i_CMS_bio(rctin, NULL);
  708. else {
  709. BIO_printf(bio_err, "Bad input format for receipt\n");
  710. goto end;
  711. }
  712. if (!rcms) {
  713. BIO_printf(bio_err, "Error reading receipt\n");
  714. goto end;
  715. }
  716. }
  717. if (outfile) {
  718. if (!(out = BIO_new_file(outfile, outmode))) {
  719. BIO_printf(bio_err, "Can't open output file %s\n", outfile);
  720. goto end;
  721. }
  722. } else {
  723. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  724. # ifdef OPENSSL_SYS_VMS
  725. {
  726. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  727. out = BIO_push(tmpbio, out);
  728. }
  729. # endif
  730. }
  731. if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
  732. if (!(store = setup_verify(bio_err, CAfile, CApath)))
  733. goto end;
  734. X509_STORE_set_verify_cb_func(store, cms_cb);
  735. if (vpm)
  736. X509_STORE_set1_param(store, vpm);
  737. }
  738. ret = 3;
  739. if (operation == SMIME_DATA_CREATE) {
  740. cms = CMS_data_create(in, flags);
  741. } else if (operation == SMIME_DIGEST_CREATE) {
  742. cms = CMS_digest_create(in, sign_md, flags);
  743. } else if (operation == SMIME_COMPRESS) {
  744. cms = CMS_compress(in, -1, flags);
  745. } else if (operation == SMIME_ENCRYPT) {
  746. flags |= CMS_PARTIAL;
  747. cms = CMS_encrypt(encerts, in, cipher, flags);
  748. if (!cms)
  749. goto end;
  750. if (secret_key) {
  751. if (!CMS_add0_recipient_key(cms, NID_undef,
  752. secret_key, secret_keylen,
  753. secret_keyid, secret_keyidlen,
  754. NULL, NULL, NULL))
  755. goto end;
  756. /* NULL these because call absorbs them */
  757. secret_key = NULL;
  758. secret_keyid = NULL;
  759. }
  760. if (!(flags & CMS_STREAM)) {
  761. if (!CMS_final(cms, in, NULL, flags))
  762. goto end;
  763. }
  764. } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
  765. cms = CMS_EncryptedData_encrypt(in, cipher,
  766. secret_key, secret_keylen, flags);
  767. } else if (operation == SMIME_SIGN_RECEIPT) {
  768. CMS_ContentInfo *srcms = NULL;
  769. STACK_OF(CMS_SignerInfo) *sis;
  770. CMS_SignerInfo *si;
  771. sis = CMS_get0_SignerInfos(cms);
  772. if (!sis)
  773. goto end;
  774. si = sk_CMS_SignerInfo_value(sis, 0);
  775. srcms = CMS_sign_receipt(si, signer, key, other, flags);
  776. if (!srcms)
  777. goto end;
  778. CMS_ContentInfo_free(cms);
  779. cms = srcms;
  780. } else if (operation & SMIME_SIGNERS) {
  781. int i;
  782. /*
  783. * If detached data content we enable streaming if S/MIME output
  784. * format.
  785. */
  786. if (operation == SMIME_SIGN) {
  787. if (flags & CMS_DETACHED) {
  788. if (outformat == FORMAT_SMIME)
  789. flags |= CMS_STREAM;
  790. }
  791. flags |= CMS_PARTIAL;
  792. cms = CMS_sign(NULL, NULL, other, in, flags);
  793. if (!cms)
  794. goto end;
  795. if (econtent_type)
  796. CMS_set1_eContentType(cms, econtent_type);
  797. if (rr_to) {
  798. rr = make_receipt_request(rr_to, rr_allorfirst, rr_from);
  799. if (!rr) {
  800. BIO_puts(bio_err,
  801. "Signed Receipt Request Creation Error\n");
  802. goto end;
  803. }
  804. }
  805. } else
  806. flags |= CMS_REUSE_DIGEST;
  807. for (i = 0; i < sk_num(sksigners); i++) {
  808. CMS_SignerInfo *si;
  809. signerfile = sk_value(sksigners, i);
  810. keyfile = sk_value(skkeys, i);
  811. signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
  812. e, "signer certificate");
  813. if (!signer)
  814. goto end;
  815. key = load_key(bio_err, keyfile, keyform, 0, passin, e,
  816. "signing key file");
  817. if (!key)
  818. goto end;
  819. si = CMS_add1_signer(cms, signer, key, sign_md, flags);
  820. if (!si)
  821. goto end;
  822. if (rr && !CMS_add1_ReceiptRequest(si, rr))
  823. goto end;
  824. X509_free(signer);
  825. signer = NULL;
  826. EVP_PKEY_free(key);
  827. key = NULL;
  828. }
  829. /* If not streaming or resigning finalize structure */
  830. if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {
  831. if (!CMS_final(cms, in, NULL, flags))
  832. goto end;
  833. }
  834. }
  835. if (!cms) {
  836. BIO_printf(bio_err, "Error creating CMS structure\n");
  837. goto end;
  838. }
  839. ret = 4;
  840. if (operation == SMIME_DECRYPT) {
  841. if (flags & CMS_DEBUG_DECRYPT)
  842. CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
  843. if (secret_key) {
  844. if (!CMS_decrypt_set1_key(cms,
  845. secret_key, secret_keylen,
  846. secret_keyid, secret_keyidlen)) {
  847. BIO_puts(bio_err, "Error decrypting CMS using secret key\n");
  848. goto end;
  849. }
  850. }
  851. if (key) {
  852. if (!CMS_decrypt_set1_pkey(cms, key, recip)) {
  853. BIO_puts(bio_err, "Error decrypting CMS using private key\n");
  854. goto end;
  855. }
  856. }
  857. if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
  858. BIO_printf(bio_err, "Error decrypting CMS structure\n");
  859. goto end;
  860. }
  861. } else if (operation == SMIME_DATAOUT) {
  862. if (!CMS_data(cms, out, flags))
  863. goto end;
  864. } else if (operation == SMIME_UNCOMPRESS) {
  865. if (!CMS_uncompress(cms, indata, out, flags))
  866. goto end;
  867. } else if (operation == SMIME_DIGEST_VERIFY) {
  868. if (CMS_digest_verify(cms, indata, out, flags) > 0)
  869. BIO_printf(bio_err, "Verification successful\n");
  870. else {
  871. BIO_printf(bio_err, "Verification failure\n");
  872. goto end;
  873. }
  874. } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
  875. if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
  876. indata, out, flags))
  877. goto end;
  878. } else if (operation == SMIME_VERIFY) {
  879. if (CMS_verify(cms, other, store, indata, out, flags) > 0)
  880. BIO_printf(bio_err, "Verification successful\n");
  881. else {
  882. BIO_printf(bio_err, "Verification failure\n");
  883. goto end;
  884. }
  885. if (signerfile) {
  886. STACK_OF(X509) *signers;
  887. signers = CMS_get0_signers(cms);
  888. if (!save_certs(signerfile, signers)) {
  889. BIO_printf(bio_err,
  890. "Error writing signers to %s\n", signerfile);
  891. ret = 5;
  892. goto end;
  893. }
  894. sk_X509_free(signers);
  895. }
  896. if (rr_print)
  897. receipt_request_print(bio_err, cms);
  898. } else if (operation == SMIME_VERIFY_RECEIPT) {
  899. if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0)
  900. BIO_printf(bio_err, "Verification successful\n");
  901. else {
  902. BIO_printf(bio_err, "Verification failure\n");
  903. goto end;
  904. }
  905. } else {
  906. if (outformat == FORMAT_SMIME) {
  907. if (to)
  908. BIO_printf(out, "To: %s\n", to);
  909. if (from)
  910. BIO_printf(out, "From: %s\n", from);
  911. if (subject)
  912. BIO_printf(out, "Subject: %s\n", subject);
  913. if (operation == SMIME_RESIGN)
  914. ret = SMIME_write_CMS(out, cms, indata, flags);
  915. else
  916. ret = SMIME_write_CMS(out, cms, in, flags);
  917. } else if (outformat == FORMAT_PEM)
  918. ret = PEM_write_bio_CMS(out, cms);
  919. else if (outformat == FORMAT_ASN1)
  920. ret = i2d_CMS_bio(out, cms);
  921. else {
  922. BIO_printf(bio_err, "Bad output format for CMS file\n");
  923. goto end;
  924. }
  925. if (ret <= 0) {
  926. ret = 6;
  927. goto end;
  928. }
  929. }
  930. ret = 0;
  931. end:
  932. if (ret)
  933. ERR_print_errors(bio_err);
  934. if (need_rand)
  935. app_RAND_write_file(NULL, bio_err);
  936. sk_X509_pop_free(encerts, X509_free);
  937. sk_X509_pop_free(other, X509_free);
  938. if (vpm)
  939. X509_VERIFY_PARAM_free(vpm);
  940. if (sksigners)
  941. sk_free(sksigners);
  942. if (skkeys)
  943. sk_free(skkeys);
  944. if (secret_key)
  945. OPENSSL_free(secret_key);
  946. if (secret_keyid)
  947. OPENSSL_free(secret_keyid);
  948. if (econtent_type)
  949. ASN1_OBJECT_free(econtent_type);
  950. if (rr)
  951. CMS_ReceiptRequest_free(rr);
  952. if (rr_to)
  953. sk_free(rr_to);
  954. if (rr_from)
  955. sk_free(rr_from);
  956. X509_STORE_free(store);
  957. X509_free(cert);
  958. X509_free(recip);
  959. X509_free(signer);
  960. EVP_PKEY_free(key);
  961. CMS_ContentInfo_free(cms);
  962. CMS_ContentInfo_free(rcms);
  963. BIO_free(rctin);
  964. BIO_free(in);
  965. BIO_free(indata);
  966. BIO_free_all(out);
  967. if (passin)
  968. OPENSSL_free(passin);
  969. return (ret);
  970. }
  971. static int save_certs(char *signerfile, STACK_OF(X509) *signers)
  972. {
  973. int i;
  974. BIO *tmp;
  975. if (!signerfile)
  976. return 1;
  977. tmp = BIO_new_file(signerfile, "w");
  978. if (!tmp)
  979. return 0;
  980. for (i = 0; i < sk_X509_num(signers); i++)
  981. PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
  982. BIO_free(tmp);
  983. return 1;
  984. }
  985. /* Minimal callback just to output policy info (if any) */
  986. static int cms_cb(int ok, X509_STORE_CTX *ctx)
  987. {
  988. int error;
  989. error = X509_STORE_CTX_get_error(ctx);
  990. if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
  991. && ((error != X509_V_OK) || (ok != 2)))
  992. return ok;
  993. policies_print(NULL, ctx);
  994. return ok;
  995. }
  996. static void gnames_stack_print(BIO *out, STACK_OF(GENERAL_NAMES) *gns)
  997. {
  998. STACK_OF(GENERAL_NAME) *gens;
  999. GENERAL_NAME *gen;
  1000. int i, j;
  1001. for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
  1002. gens = sk_GENERAL_NAMES_value(gns, i);
  1003. for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
  1004. gen = sk_GENERAL_NAME_value(gens, j);
  1005. BIO_puts(out, " ");
  1006. GENERAL_NAME_print(out, gen);
  1007. BIO_puts(out, "\n");
  1008. }
  1009. }
  1010. return;
  1011. }
  1012. static void receipt_request_print(BIO *out, CMS_ContentInfo *cms)
  1013. {
  1014. STACK_OF(CMS_SignerInfo) *sis;
  1015. CMS_SignerInfo *si;
  1016. CMS_ReceiptRequest *rr;
  1017. int allorfirst;
  1018. STACK_OF(GENERAL_NAMES) *rto, *rlist;
  1019. ASN1_STRING *scid;
  1020. int i, rv;
  1021. sis = CMS_get0_SignerInfos(cms);
  1022. for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
  1023. si = sk_CMS_SignerInfo_value(sis, i);
  1024. rv = CMS_get1_ReceiptRequest(si, &rr);
  1025. BIO_printf(bio_err, "Signer %d:\n", i + 1);
  1026. if (rv == 0)
  1027. BIO_puts(bio_err, " No Receipt Request\n");
  1028. else if (rv < 0) {
  1029. BIO_puts(bio_err, " Receipt Request Parse Error\n");
  1030. ERR_print_errors(bio_err);
  1031. } else {
  1032. char *id;
  1033. int idlen;
  1034. CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
  1035. &rlist, &rto);
  1036. BIO_puts(out, " Signed Content ID:\n");
  1037. idlen = ASN1_STRING_length(scid);
  1038. id = (char *)ASN1_STRING_data(scid);
  1039. BIO_dump_indent(out, id, idlen, 4);
  1040. BIO_puts(out, " Receipts From");
  1041. if (rlist) {
  1042. BIO_puts(out, " List:\n");
  1043. gnames_stack_print(out, rlist);
  1044. } else if (allorfirst == 1)
  1045. BIO_puts(out, ": First Tier\n");
  1046. else if (allorfirst == 0)
  1047. BIO_puts(out, ": All\n");
  1048. else
  1049. BIO_printf(out, " Unknown (%d)\n", allorfirst);
  1050. BIO_puts(out, " Receipts To:\n");
  1051. gnames_stack_print(out, rto);
  1052. }
  1053. if (rr)
  1054. CMS_ReceiptRequest_free(rr);
  1055. }
  1056. }
  1057. static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK * ns)
  1058. {
  1059. int i;
  1060. STACK_OF(GENERAL_NAMES) *ret;
  1061. GENERAL_NAMES *gens = NULL;
  1062. GENERAL_NAME *gen = NULL;
  1063. ret = sk_GENERAL_NAMES_new_null();
  1064. if (!ret)
  1065. goto err;
  1066. for (i = 0; i < sk_num(ns); i++) {
  1067. CONF_VALUE cnf;
  1068. cnf.name = "email";
  1069. cnf.value = sk_value(ns, i);
  1070. gen = v2i_GENERAL_NAME(NULL, NULL, &cnf);
  1071. if (!gen)
  1072. goto err;
  1073. gens = GENERAL_NAMES_new();
  1074. if (!gens)
  1075. goto err;
  1076. if (!sk_GENERAL_NAME_push(gens, gen))
  1077. goto err;
  1078. gen = NULL;
  1079. if (!sk_GENERAL_NAMES_push(ret, gens))
  1080. goto err;
  1081. gens = NULL;
  1082. }
  1083. return ret;
  1084. err:
  1085. if (ret)
  1086. sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
  1087. if (gens)
  1088. GENERAL_NAMES_free(gens);
  1089. if (gen)
  1090. GENERAL_NAME_free(gen);
  1091. return NULL;
  1092. }
  1093. static CMS_ReceiptRequest *make_receipt_request(STACK * rr_to,
  1094. int rr_allorfirst,
  1095. STACK * rr_from)
  1096. {
  1097. STACK_OF(GENERAL_NAMES) *rct_to, *rct_from;
  1098. CMS_ReceiptRequest *rr;
  1099. rct_to = make_names_stack(rr_to);
  1100. if (!rct_to)
  1101. goto err;
  1102. if (rr_from) {
  1103. rct_from = make_names_stack(rr_from);
  1104. if (!rct_from)
  1105. goto err;
  1106. } else
  1107. rct_from = NULL;
  1108. rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,
  1109. rct_to);
  1110. return rr;
  1111. err:
  1112. return NULL;
  1113. }
  1114. #endif