cms.c 41 KB

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