cms.c 42 KB

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