cms.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. */
  53. /* CMS utility function */
  54. #include <stdio.h>
  55. #include <string.h>
  56. #include "apps.h"
  57. #ifndef OPENSSL_NO_CMS
  58. # include <openssl/crypto.h>
  59. # include <openssl/pem.h>
  60. # include <openssl/err.h>
  61. # include <openssl/x509_vfy.h>
  62. # include <openssl/x509v3.h>
  63. # include <openssl/cms.h>
  64. static int save_certs(char *signerfile, STACK_OF(X509) *signers);
  65. static int cms_cb(int ok, X509_STORE_CTX *ctx);
  66. static void receipt_request_print(CMS_ContentInfo *cms);
  67. static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING)
  68. *rr_to, int rr_allorfirst, STACK_OF(OPENSSL_STRING)
  69. *rr_from);
  70. static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
  71. STACK_OF(OPENSSL_STRING) *param);
  72. # define SMIME_OP 0x10
  73. # define SMIME_IP 0x20
  74. # define SMIME_SIGNERS 0x40
  75. # define SMIME_ENCRYPT (1 | SMIME_OP)
  76. # define SMIME_DECRYPT (2 | SMIME_IP)
  77. # define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
  78. # define SMIME_VERIFY (4 | SMIME_IP)
  79. # define SMIME_CMSOUT (5 | SMIME_IP | SMIME_OP)
  80. # define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
  81. # define SMIME_DATAOUT (7 | SMIME_IP)
  82. # define SMIME_DATA_CREATE (8 | SMIME_OP)
  83. # define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
  84. # define SMIME_DIGEST_CREATE (10 | SMIME_OP)
  85. # define SMIME_UNCOMPRESS (11 | SMIME_IP)
  86. # define SMIME_COMPRESS (12 | SMIME_OP)
  87. # define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
  88. # define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
  89. # define SMIME_SIGN_RECEIPT (15 | SMIME_IP | SMIME_OP)
  90. # define SMIME_VERIFY_RECEIPT (16 | SMIME_IP)
  91. static int verify_err = 0;
  92. typedef struct cms_key_param_st cms_key_param;
  93. struct cms_key_param_st {
  94. int idx;
  95. STACK_OF(OPENSSL_STRING) *param;
  96. cms_key_param *next;
  97. };
  98. typedef enum OPTION_choice {
  99. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  100. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_ENCRYPT,
  101. OPT_DECRYPT, OPT_SIGN, OPT_SIGN_RECEIPT, OPT_RESIGN,
  102. OPT_VERIFY, OPT_VERIFY_RETCODE, OPT_VERIFY_RECEIPT,
  103. OPT_CMSOUT, OPT_DATA_OUT, OPT_DATA_CREATE, OPT_DIGEST_VERIFY,
  104. OPT_DIGEST_CREATE, OPT_COMPRESS, OPT_UNCOMPRESS,
  105. OPT_ED_DECRYPT, OPT_ED_ENCRYPT, OPT_DEBUG_DECRYPT, OPT_TEXT,
  106. OPT_ASCIICRLF, OPT_NOINTERN, OPT_NOVERIFY, OPT_NOCERTS,
  107. OPT_NOATTR, OPT_NODETACH, OPT_NOSMIMECAP, OPT_BINARY, OPT_KEYID,
  108. OPT_NOSIGS, OPT_NO_CONTENT_VERIFY, OPT_NO_ATTR_VERIFY, OPT_INDEF,
  109. OPT_NOINDEF, OPT_NOOLDMIME, OPT_CRLFEOL, OPT_NOOUT, OPT_RR_PRINT,
  110. OPT_RR_ALL, OPT_RR_FIRST, OPT_RCTFORM, OPT_CERTFILE, OPT_CAFILE,
  111. OPT_CAPATH, OPT_NOCAPATH, OPT_NOCAFILE,OPT_CONTENT, OPT_PRINT,
  112. OPT_SECRETKEY, OPT_SECRETKEYID, OPT_PWRI_PASSWORD, OPT_ECONTENT_TYPE,
  113. OPT_RAND, OPT_PASSIN, OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP,
  114. OPT_CERTSOUT, OPT_MD, OPT_INKEY, OPT_KEYFORM, OPT_KEYOPT, OPT_RR_FROM,
  115. OPT_RR_TO, OPT_AES128_WRAP, OPT_AES192_WRAP, OPT_AES256_WRAP,
  116. OPT_3DES_WRAP, OPT_ENGINE,
  117. OPT_V_ENUM,
  118. OPT_CIPHER
  119. } OPTION_CHOICE;
  120. OPTIONS cms_options[] = {
  121. {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"},
  122. {OPT_HELP_STR, 1, '-',
  123. " cert.pem... recipient certs for encryption\n"},
  124. {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
  125. {"help", OPT_HELP, '-', "Display this summary"},
  126. {"inform", OPT_INFORM, 'F', "Input format SMIME (default), PEM or DER"},
  127. {"outform", OPT_OUTFORM, 'F',
  128. "Output format SMIME (default), PEM or DER"},
  129. {"in", OPT_IN, '<', "Input file"},
  130. {"out", OPT_OUT, '>', "Output file"},
  131. {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
  132. {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
  133. {"sign", OPT_SIGN, '-', "Sign message"},
  134. {"sign_receipt", OPT_SIGN_RECEIPT, '-'},
  135. {"resign", OPT_RESIGN, '-'},
  136. {"verify", OPT_VERIFY, '-', "Verify signed message"},
  137. {"verify_retcode", OPT_VERIFY_RETCODE, '-'},
  138. {"verify_receipt", OPT_VERIFY_RECEIPT, '<'},
  139. {"cmsout", OPT_CMSOUT, '-', "Output CMS structure"},
  140. {"data_out", OPT_DATA_OUT, '-'},
  141. {"data_create", OPT_DATA_CREATE, '-'},
  142. {"digest_verify", OPT_DIGEST_VERIFY, '-'},
  143. {"digest_create", OPT_DIGEST_CREATE, '-'},
  144. {"compress", OPT_COMPRESS, '-'},
  145. {"uncompress", OPT_UNCOMPRESS, '-'},
  146. {"EncryptedData_decrypt", OPT_ED_DECRYPT, '-'},
  147. {"EncryptedData_encrypt", OPT_ED_ENCRYPT, '-'},
  148. {"debug_decrypt", OPT_DEBUG_DECRYPT, '-'},
  149. {"text", OPT_TEXT, '-', "Include or delete text MIME headers"},
  150. {"asciicrlf", OPT_ASCIICRLF, '-'},
  151. {"nointern", OPT_NOINTERN, '-',
  152. "Don't search certificates in message for signer"},
  153. {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"},
  154. {"nocerts", OPT_NOCERTS, '-',
  155. "Don't include signers certificate when signing"},
  156. {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"},
  157. {"nodetach", OPT_NODETACH, '-', "Use opaque signing"},
  158. {"nosmimecap", OPT_NOSMIMECAP, '-'},
  159. {"binary", OPT_BINARY, '-', "Don't translate message to text"},
  160. {"keyid", OPT_KEYID, '-', "Use subject key identifier"},
  161. {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"},
  162. {"no_content_verify", OPT_NO_CONTENT_VERIFY, '-'},
  163. {"no_attr_verify", OPT_NO_ATTR_VERIFY, '-'},
  164. {"stream", OPT_INDEF, '-'},
  165. {"indef", OPT_INDEF, '-'},
  166. {"noindef", OPT_NOINDEF, '-'},
  167. {"nooldmime", OPT_NOOLDMIME, '-'},
  168. {"crlfeol", OPT_CRLFEOL, '-'},
  169. {"noout", OPT_NOOUT, '-'},
  170. {"receipt_request_print", OPT_RR_PRINT, '-'},
  171. {"receipt_request_all", OPT_RR_ALL, '-'},
  172. {"receipt_request_first", OPT_RR_FIRST, '-'},
  173. {"rctform", OPT_RCTFORM, 'F'},
  174. {"certfile", OPT_CERTFILE, '<', "Other certificates file"},
  175. {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
  176. {"CApath", OPT_CAPATH, '/', "trusted certificates directory"},
  177. {"no-CAfile", OPT_NOCAFILE, '-',
  178. "Do not load the default certificates file"},
  179. {"no-CApath", OPT_NOCAPATH, '-',
  180. "Do not load certificates from the default certificates directory"},
  181. {"content", OPT_CONTENT, '<',
  182. "Supply or override content for detached signature"},
  183. {"print", OPT_PRINT, '-'},
  184. {"secretkey", OPT_SECRETKEY, 's'},
  185. {"secretkeyid", OPT_SECRETKEYID, 's'},
  186. {"pwri_password", OPT_PWRI_PASSWORD, 's'},
  187. {"econtent_type", OPT_ECONTENT_TYPE, 's'},
  188. {"rand", OPT_RAND, 's',
  189. "Load the file(s) into the random number generator"},
  190. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  191. {"to", OPT_TO, 's', "To address"},
  192. {"from", OPT_FROM, 's', "From address"},
  193. {"subject", OPT_SUBJECT, 's', "Subject"},
  194. {"signer", OPT_SIGNER, 's', "Signer certificate file"},
  195. {"recip", OPT_RECIP, '<', "Recipient cert file for decryption"},
  196. {"certsout", OPT_CERTSOUT, '>', "Certificate output file"},
  197. {"md", OPT_MD, 's'},
  198. {"inkey", OPT_INKEY, 's',
  199. "Input private key (if not signer or recipient)"},
  200. {"keyform", OPT_KEYFORM, 'f', "Input private key format (PEM or ENGINE)"},
  201. {"keyopt", OPT_KEYOPT, 's', "Set public key parameters as n:v pairs"},
  202. {"receipt_request_from", OPT_RR_FROM, 's'},
  203. {"receipt_request_to", OPT_RR_TO, 's'},
  204. {"", OPT_CIPHER, '-', "Any supported cipher"},
  205. OPT_V_OPTIONS,
  206. # ifndef OPENSSL_NO_AES
  207. {"aes128-wrap", OPT_AES128_WRAP, '-', "Use AES128 to wrap key"},
  208. {"aes192-wrap", OPT_AES192_WRAP, '-', "Use AES192 to wrap key"},
  209. {"aes256-wrap", OPT_AES256_WRAP, '-', "Use AES256 to wrap key"},
  210. # endif
  211. # ifndef OPENSSL_NO_DES
  212. {"des3-wrap", OPT_3DES_WRAP, '-', "Use 3DES-EDE to wrap key"},
  213. # endif
  214. # ifndef OPENSSL_NO_ENGINE
  215. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  216. # endif
  217. {NULL}
  218. };
  219. int cms_main(int argc, char **argv)
  220. {
  221. ASN1_OBJECT *econtent_type = NULL;
  222. BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
  223. CMS_ContentInfo *cms = NULL, *rcms = NULL;
  224. CMS_ReceiptRequest *rr = NULL;
  225. ENGINE *e = NULL;
  226. EVP_PKEY *key = NULL;
  227. const EVP_CIPHER *cipher = NULL, *wrap_cipher = NULL;
  228. const EVP_MD *sign_md = NULL;
  229. STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL;
  230. STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
  231. STACK_OF(X509) *encerts = NULL, *other = NULL;
  232. X509 *cert = NULL, *recip = NULL, *signer = NULL;
  233. X509_STORE *store = NULL;
  234. X509_VERIFY_PARAM *vpm = NULL;
  235. char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
  236. char *CAfile = NULL, *CApath = NULL, *certsoutfile = NULL;
  237. int noCAfile = 0, noCApath = 0;
  238. char *infile = NULL, *outfile = NULL, *rctfile = NULL, *inrand = NULL;
  239. char *passinarg = NULL, *passin = NULL, *signerfile = NULL, *recipfile =
  240. NULL;
  241. char *to = NULL, *from = NULL, *subject = NULL, *prog;
  242. cms_key_param *key_first = NULL, *key_param = NULL;
  243. int flags = CMS_DETACHED, noout = 0, print = 0, keyidx = -1, vpmtouched =
  244. 0;
  245. int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
  246. int need_rand = 0, operation = 0, ret = 1, rr_print = 0, rr_allorfirst =
  247. -1;
  248. int verify_retcode = 0, rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
  249. size_t secret_keylen = 0, secret_keyidlen = 0;
  250. unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;
  251. unsigned char *secret_key = NULL, *secret_keyid = NULL;
  252. long ltmp;
  253. OPTION_CHOICE o;
  254. if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
  255. return 1;
  256. prog = opt_init(argc, argv, cms_options);
  257. while ((o = opt_next()) != OPT_EOF) {
  258. switch (o) {
  259. case OPT_EOF:
  260. case OPT_ERR:
  261. opthelp:
  262. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  263. goto end;
  264. case OPT_HELP:
  265. opt_help(cms_options);
  266. ret = 0;
  267. goto end;
  268. case OPT_INFORM:
  269. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  270. goto opthelp;
  271. break;
  272. case OPT_OUTFORM:
  273. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  274. goto opthelp;
  275. break;
  276. case OPT_OUT:
  277. outfile = opt_arg();
  278. break;
  279. case OPT_ENCRYPT:
  280. operation = SMIME_ENCRYPT;
  281. break;
  282. case OPT_DECRYPT:
  283. operation = SMIME_DECRYPT;
  284. break;
  285. case OPT_SIGN:
  286. operation = SMIME_SIGN;
  287. break;
  288. case OPT_SIGN_RECEIPT:
  289. operation = SMIME_SIGN_RECEIPT;
  290. break;
  291. case OPT_RESIGN:
  292. operation = SMIME_RESIGN;
  293. break;
  294. case OPT_VERIFY:
  295. operation = SMIME_VERIFY;
  296. break;
  297. case OPT_VERIFY_RETCODE:
  298. verify_retcode = 1;
  299. break;
  300. case OPT_VERIFY_RECEIPT:
  301. operation = SMIME_VERIFY_RECEIPT;
  302. rctfile = opt_arg();
  303. break;
  304. case OPT_CMSOUT:
  305. operation = SMIME_CMSOUT;
  306. break;
  307. case OPT_DATA_OUT:
  308. operation = SMIME_DATAOUT;
  309. break;
  310. case OPT_DATA_CREATE:
  311. operation = SMIME_DATA_CREATE;
  312. break;
  313. case OPT_DIGEST_VERIFY:
  314. operation = SMIME_DIGEST_VERIFY;
  315. break;
  316. case OPT_DIGEST_CREATE:
  317. operation = SMIME_DIGEST_CREATE;
  318. break;
  319. case OPT_COMPRESS:
  320. operation = SMIME_COMPRESS;
  321. break;
  322. case OPT_UNCOMPRESS:
  323. operation = SMIME_UNCOMPRESS;
  324. break;
  325. case OPT_ED_DECRYPT:
  326. operation = SMIME_ENCRYPTED_DECRYPT;
  327. break;
  328. case OPT_ED_ENCRYPT:
  329. operation = SMIME_ENCRYPTED_ENCRYPT;
  330. break;
  331. case OPT_DEBUG_DECRYPT:
  332. flags |= CMS_DEBUG_DECRYPT;
  333. break;
  334. case OPT_TEXT:
  335. flags |= CMS_TEXT;
  336. break;
  337. case OPT_ASCIICRLF:
  338. flags |= CMS_ASCIICRLF;
  339. break;
  340. case OPT_NOINTERN:
  341. flags |= CMS_NOINTERN;
  342. break;
  343. case OPT_NOVERIFY:
  344. flags |= CMS_NO_SIGNER_CERT_VERIFY;
  345. break;
  346. case OPT_NOCERTS:
  347. flags |= CMS_NOCERTS;
  348. break;
  349. case OPT_NOATTR:
  350. flags |= CMS_NOATTR;
  351. break;
  352. case OPT_NODETACH:
  353. flags &= ~CMS_DETACHED;
  354. break;
  355. case OPT_NOSMIMECAP:
  356. flags |= CMS_NOSMIMECAP;
  357. break;
  358. case OPT_BINARY:
  359. flags |= CMS_BINARY;
  360. break;
  361. case OPT_KEYID:
  362. flags |= CMS_USE_KEYID;
  363. break;
  364. case OPT_NOSIGS:
  365. flags |= CMS_NOSIGS;
  366. break;
  367. case OPT_NO_CONTENT_VERIFY:
  368. flags |= CMS_NO_CONTENT_VERIFY;
  369. break;
  370. case OPT_NO_ATTR_VERIFY:
  371. flags |= CMS_NO_ATTR_VERIFY;
  372. break;
  373. case OPT_INDEF:
  374. flags |= CMS_STREAM;
  375. break;
  376. case OPT_NOINDEF:
  377. flags &= ~CMS_STREAM;
  378. break;
  379. case OPT_NOOLDMIME:
  380. flags |= CMS_NOOLDMIMETYPE;
  381. break;
  382. case OPT_CRLFEOL:
  383. flags |= CMS_CRLFEOL;
  384. break;
  385. case OPT_NOOUT:
  386. noout = 1;
  387. break;
  388. case OPT_RR_PRINT:
  389. rr_print = 1;
  390. break;
  391. case OPT_RR_ALL:
  392. rr_allorfirst = 0;
  393. break;
  394. case OPT_RR_FIRST:
  395. rr_allorfirst = 1;
  396. break;
  397. case OPT_RCTFORM:
  398. if (rctformat == FORMAT_SMIME)
  399. rcms = SMIME_read_CMS(rctin, NULL);
  400. else if (rctformat == FORMAT_PEM)
  401. rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
  402. else if (rctformat == FORMAT_ASN1)
  403. if (!opt_format(opt_arg(),
  404. OPT_FMT_PEMDER | OPT_FMT_SMIME, &rctformat))
  405. goto opthelp;
  406. break;
  407. case OPT_CERTFILE:
  408. certfile = opt_arg();
  409. break;
  410. case OPT_CAFILE:
  411. CAfile = opt_arg();
  412. break;
  413. case OPT_CAPATH:
  414. CApath = opt_arg();
  415. break;
  416. case OPT_NOCAFILE:
  417. noCAfile = 1;
  418. break;
  419. case OPT_NOCAPATH:
  420. noCApath = 1;
  421. break;
  422. case OPT_IN:
  423. infile = opt_arg();
  424. break;
  425. case OPT_CONTENT:
  426. contfile = opt_arg();
  427. break;
  428. case OPT_RR_FROM:
  429. if (rr_from == NULL
  430. && (rr_from = sk_OPENSSL_STRING_new_null()) == NULL)
  431. goto end;
  432. sk_OPENSSL_STRING_push(rr_from, opt_arg());
  433. break;
  434. case OPT_RR_TO:
  435. if (rr_to == NULL
  436. && (rr_to = sk_OPENSSL_STRING_new_null()) == NULL)
  437. goto end;
  438. sk_OPENSSL_STRING_push(rr_to, opt_arg());
  439. break;
  440. case OPT_PRINT:
  441. noout = print = 1;
  442. break;
  443. case OPT_SECRETKEY:
  444. secret_key = string_to_hex(opt_arg(), &ltmp);
  445. if (secret_key == NULL) {
  446. BIO_printf(bio_err, "Invalid key %s\n", opt_arg());
  447. goto end;
  448. }
  449. secret_keylen = (size_t)ltmp;
  450. break;
  451. case OPT_SECRETKEYID:
  452. secret_keyid = string_to_hex(opt_arg(), &ltmp);
  453. if (secret_keyid == NULL) {
  454. BIO_printf(bio_err, "Invalid id %s\n", opt_arg());
  455. goto opthelp;
  456. }
  457. secret_keyidlen = (size_t)ltmp;
  458. break;
  459. case OPT_PWRI_PASSWORD:
  460. pwri_pass = (unsigned char *)opt_arg();
  461. break;
  462. case OPT_ECONTENT_TYPE:
  463. econtent_type = OBJ_txt2obj(opt_arg(), 0);
  464. if (econtent_type == NULL) {
  465. BIO_printf(bio_err, "Invalid OID %s\n", opt_arg());
  466. goto opthelp;
  467. }
  468. break;
  469. case OPT_RAND:
  470. inrand = opt_arg();
  471. need_rand = 1;
  472. break;
  473. case OPT_ENGINE:
  474. e = setup_engine(opt_arg(), 0);
  475. break;
  476. case OPT_PASSIN:
  477. passinarg = opt_arg();
  478. break;
  479. case OPT_TO:
  480. to = opt_arg();
  481. break;
  482. case OPT_FROM:
  483. from = opt_arg();
  484. break;
  485. case OPT_SUBJECT:
  486. subject = opt_arg();
  487. break;
  488. case OPT_CERTSOUT:
  489. certsoutfile = opt_arg();
  490. break;
  491. case OPT_MD:
  492. if (!opt_md(opt_arg(), &sign_md))
  493. goto end;
  494. break;
  495. case OPT_SIGNER:
  496. /* If previous -signer argument add signer to list */
  497. if (signerfile) {
  498. if (sksigners == NULL
  499. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  500. goto end;
  501. sk_OPENSSL_STRING_push(sksigners, signerfile);
  502. if (keyfile == NULL)
  503. keyfile = signerfile;
  504. if (skkeys == NULL
  505. && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  506. goto end;
  507. sk_OPENSSL_STRING_push(skkeys, keyfile);
  508. keyfile = NULL;
  509. }
  510. signerfile = opt_arg();
  511. break;
  512. case OPT_INKEY:
  513. /* If previous -inkey arument add signer to list */
  514. if (keyfile) {
  515. if (signerfile == NULL) {
  516. BIO_puts(bio_err, "Illegal -inkey without -signer\n");
  517. goto end;
  518. }
  519. if (sksigners == NULL
  520. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  521. goto end;
  522. sk_OPENSSL_STRING_push(sksigners, signerfile);
  523. signerfile = NULL;
  524. if (skkeys == NULL
  525. && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  526. goto end;
  527. sk_OPENSSL_STRING_push(skkeys, keyfile);
  528. }
  529. keyfile = opt_arg();
  530. break;
  531. case OPT_KEYFORM:
  532. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
  533. goto opthelp;
  534. break;
  535. case OPT_RECIP:
  536. if (operation == SMIME_ENCRYPT) {
  537. if (encerts == NULL && (encerts = sk_X509_new_null()) == NULL)
  538. goto end;
  539. cert = load_cert(opt_arg(), FORMAT_PEM, NULL, e,
  540. "recipient certificate file");
  541. if (cert == NULL)
  542. goto end;
  543. sk_X509_push(encerts, cert);
  544. cert = NULL;
  545. } else
  546. recipfile = opt_arg();
  547. break;
  548. case OPT_CIPHER:
  549. if (!opt_cipher(opt_unknown(), &cipher))
  550. goto end;
  551. break;
  552. case OPT_KEYOPT:
  553. keyidx = -1;
  554. if (operation == SMIME_ENCRYPT) {
  555. if (encerts)
  556. keyidx += sk_X509_num(encerts);
  557. } else {
  558. if (keyfile || signerfile)
  559. keyidx++;
  560. if (skkeys)
  561. keyidx += sk_OPENSSL_STRING_num(skkeys);
  562. }
  563. if (keyidx < 0) {
  564. BIO_printf(bio_err, "No key specified\n");
  565. goto opthelp;
  566. }
  567. if (key_param == NULL || key_param->idx != keyidx) {
  568. cms_key_param *nparam;
  569. nparam = app_malloc(sizeof(*nparam), "key param buffer");
  570. nparam->idx = keyidx;
  571. if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL)
  572. goto end;
  573. nparam->next = NULL;
  574. if (key_first == NULL)
  575. key_first = nparam;
  576. else
  577. key_param->next = nparam;
  578. key_param = nparam;
  579. }
  580. sk_OPENSSL_STRING_push(key_param->param, opt_arg());
  581. break;
  582. case OPT_V_CASES:
  583. if (!opt_verify(o, vpm))
  584. goto end;
  585. vpmtouched++;
  586. break;
  587. case OPT_3DES_WRAP:
  588. # ifndef OPENSSL_NO_DES
  589. wrap_cipher = EVP_des_ede3_wrap();
  590. # endif
  591. break;
  592. # ifndef OPENSSL_NO_AES
  593. case OPT_AES128_WRAP:
  594. wrap_cipher = EVP_aes_128_wrap();
  595. break;
  596. case OPT_AES192_WRAP:
  597. wrap_cipher = EVP_aes_192_wrap();
  598. break;
  599. case OPT_AES256_WRAP:
  600. wrap_cipher = EVP_aes_256_wrap();
  601. break;
  602. # else
  603. case OPT_AES128_WRAP:
  604. case OPT_AES192_WRAP:
  605. case OPT_AES256_WRAP:
  606. break;
  607. # endif
  608. }
  609. }
  610. argc = opt_num_rest();
  611. argv = opt_rest();
  612. if (((rr_allorfirst != -1) || rr_from) && !rr_to) {
  613. BIO_puts(bio_err, "No Signed Receipts Recipients\n");
  614. goto opthelp;
  615. }
  616. if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from)) {
  617. BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
  618. goto opthelp;
  619. }
  620. if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
  621. BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
  622. goto opthelp;
  623. }
  624. if (operation & SMIME_SIGNERS) {
  625. if (keyfile && !signerfile) {
  626. BIO_puts(bio_err, "Illegal -inkey without -signer\n");
  627. goto opthelp;
  628. }
  629. /* Check to see if any final signer needs to be appended */
  630. if (signerfile) {
  631. if (!sksigners
  632. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  633. goto end;
  634. sk_OPENSSL_STRING_push(sksigners, signerfile);
  635. if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  636. goto end;
  637. if (!keyfile)
  638. keyfile = signerfile;
  639. sk_OPENSSL_STRING_push(skkeys, keyfile);
  640. }
  641. if (!sksigners) {
  642. BIO_printf(bio_err, "No signer certificate specified\n");
  643. goto opthelp;
  644. }
  645. signerfile = NULL;
  646. keyfile = NULL;
  647. need_rand = 1;
  648. }
  649. else if (operation == SMIME_DECRYPT) {
  650. if (!recipfile && !keyfile && !secret_key && !pwri_pass) {
  651. BIO_printf(bio_err,
  652. "No recipient certificate or key specified\n");
  653. goto opthelp;
  654. }
  655. } else if (operation == SMIME_ENCRYPT) {
  656. if (*argv == NULL && !secret_key && !pwri_pass && !encerts) {
  657. BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
  658. goto opthelp;
  659. }
  660. need_rand = 1;
  661. } else if (!operation)
  662. goto opthelp;
  663. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  664. BIO_printf(bio_err, "Error getting password\n");
  665. goto end;
  666. }
  667. if (need_rand) {
  668. app_RAND_load_file(NULL, (inrand != NULL));
  669. if (inrand != NULL)
  670. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  671. app_RAND_load_files(inrand));
  672. }
  673. ret = 2;
  674. if (!(operation & SMIME_SIGNERS))
  675. flags &= ~CMS_DETACHED;
  676. if (!(operation & SMIME_OP)) {
  677. if (flags & CMS_BINARY)
  678. outformat = FORMAT_BINARY;
  679. }
  680. if (!(operation & SMIME_IP)) {
  681. if (flags & CMS_BINARY)
  682. informat = FORMAT_BINARY;
  683. }
  684. if (operation == SMIME_ENCRYPT) {
  685. if (!cipher) {
  686. # ifndef OPENSSL_NO_DES
  687. cipher = EVP_des_ede3_cbc();
  688. # else
  689. BIO_printf(bio_err, "No cipher selected\n");
  690. goto end;
  691. # endif
  692. }
  693. if (secret_key && !secret_keyid) {
  694. BIO_printf(bio_err, "No secret key id\n");
  695. goto end;
  696. }
  697. if (*argv && !encerts)
  698. if ((encerts = sk_X509_new_null()) == NULL)
  699. goto end;
  700. while (*argv) {
  701. if ((cert = load_cert(*argv, FORMAT_PEM, NULL, e,
  702. "recipient certificate file")) == NULL)
  703. goto end;
  704. sk_X509_push(encerts, cert);
  705. cert = NULL;
  706. argv++;
  707. }
  708. }
  709. if (certfile) {
  710. if (!load_certs(certfile, &other, FORMAT_PEM, NULL, e,
  711. "certificate file")) {
  712. ERR_print_errors(bio_err);
  713. goto end;
  714. }
  715. }
  716. if (recipfile && (operation == SMIME_DECRYPT)) {
  717. if ((recip = load_cert(recipfile, FORMAT_PEM, NULL, e,
  718. "recipient certificate file")) == NULL) {
  719. ERR_print_errors(bio_err);
  720. goto end;
  721. }
  722. }
  723. if (operation == SMIME_SIGN_RECEIPT) {
  724. if ((signer = load_cert(signerfile, FORMAT_PEM, NULL, e,
  725. "receipt signer certificate file")) == NULL) {
  726. ERR_print_errors(bio_err);
  727. goto end;
  728. }
  729. }
  730. if (operation == SMIME_DECRYPT) {
  731. if (!keyfile)
  732. keyfile = recipfile;
  733. } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
  734. if (!keyfile)
  735. keyfile = signerfile;
  736. } else
  737. keyfile = NULL;
  738. if (keyfile) {
  739. key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
  740. if (!key)
  741. goto end;
  742. }
  743. in = bio_open_default(infile, 'r', informat);
  744. if (in == NULL)
  745. goto end;
  746. if (operation & SMIME_IP) {
  747. if (informat == FORMAT_SMIME)
  748. cms = SMIME_read_CMS(in, &indata);
  749. else if (informat == FORMAT_PEM)
  750. cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
  751. else if (informat == FORMAT_ASN1)
  752. cms = d2i_CMS_bio(in, NULL);
  753. else {
  754. BIO_printf(bio_err, "Bad input format for CMS file\n");
  755. goto end;
  756. }
  757. if (!cms) {
  758. BIO_printf(bio_err, "Error reading S/MIME message\n");
  759. goto end;
  760. }
  761. if (contfile) {
  762. BIO_free(indata);
  763. if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
  764. BIO_printf(bio_err, "Can't read content file %s\n", contfile);
  765. goto end;
  766. }
  767. }
  768. if (certsoutfile) {
  769. STACK_OF(X509) *allcerts;
  770. allcerts = CMS_get1_certs(cms);
  771. if (!save_certs(certsoutfile, allcerts)) {
  772. BIO_printf(bio_err,
  773. "Error writing certs to %s\n", certsoutfile);
  774. ret = 5;
  775. goto end;
  776. }
  777. sk_X509_pop_free(allcerts, X509_free);
  778. }
  779. }
  780. if (rctfile) {
  781. char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
  782. if ((rctin = BIO_new_file(rctfile, rctmode)) == NULL) {
  783. BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
  784. goto end;
  785. }
  786. if (rctformat == FORMAT_SMIME)
  787. rcms = SMIME_read_CMS(rctin, NULL);
  788. else if (rctformat == FORMAT_PEM)
  789. rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
  790. else if (rctformat == FORMAT_ASN1)
  791. rcms = d2i_CMS_bio(rctin, NULL);
  792. else {
  793. BIO_printf(bio_err, "Bad input format for receipt\n");
  794. goto end;
  795. }
  796. if (!rcms) {
  797. BIO_printf(bio_err, "Error reading receipt\n");
  798. goto end;
  799. }
  800. }
  801. out = bio_open_default(outfile, 'w', outformat);
  802. if (out == NULL)
  803. goto end;
  804. if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
  805. if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
  806. goto end;
  807. X509_STORE_set_verify_cb(store, cms_cb);
  808. if (vpmtouched)
  809. X509_STORE_set1_param(store, vpm);
  810. }
  811. ret = 3;
  812. if (operation == SMIME_DATA_CREATE) {
  813. cms = CMS_data_create(in, flags);
  814. } else if (operation == SMIME_DIGEST_CREATE) {
  815. cms = CMS_digest_create(in, sign_md, flags);
  816. } else if (operation == SMIME_COMPRESS) {
  817. cms = CMS_compress(in, -1, flags);
  818. } else if (operation == SMIME_ENCRYPT) {
  819. int i;
  820. flags |= CMS_PARTIAL;
  821. cms = CMS_encrypt(NULL, in, cipher, flags);
  822. if (!cms)
  823. goto end;
  824. for (i = 0; i < sk_X509_num(encerts); i++) {
  825. CMS_RecipientInfo *ri;
  826. cms_key_param *kparam;
  827. int tflags = flags;
  828. X509 *x = sk_X509_value(encerts, i);
  829. for (kparam = key_first; kparam; kparam = kparam->next) {
  830. if (kparam->idx == i) {
  831. tflags |= CMS_KEY_PARAM;
  832. break;
  833. }
  834. }
  835. ri = CMS_add1_recipient_cert(cms, x, tflags);
  836. if (!ri)
  837. goto end;
  838. if (kparam) {
  839. EVP_PKEY_CTX *pctx;
  840. pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  841. if (!cms_set_pkey_param(pctx, kparam->param))
  842. goto end;
  843. }
  844. if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE
  845. && wrap_cipher) {
  846. EVP_CIPHER_CTX *wctx;
  847. wctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  848. EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL);
  849. }
  850. }
  851. if (secret_key) {
  852. if (!CMS_add0_recipient_key(cms, NID_undef,
  853. secret_key, secret_keylen,
  854. secret_keyid, secret_keyidlen,
  855. NULL, NULL, NULL))
  856. goto end;
  857. /* NULL these because call absorbs them */
  858. secret_key = NULL;
  859. secret_keyid = NULL;
  860. }
  861. if (pwri_pass) {
  862. pwri_tmp = (unsigned char *)OPENSSL_strdup((char *)pwri_pass);
  863. if (!pwri_tmp)
  864. goto end;
  865. if (!CMS_add0_recipient_password(cms,
  866. -1, NID_undef, NID_undef,
  867. pwri_tmp, -1, NULL))
  868. goto end;
  869. pwri_tmp = NULL;
  870. }
  871. if (!(flags & CMS_STREAM)) {
  872. if (!CMS_final(cms, in, NULL, flags))
  873. goto end;
  874. }
  875. } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
  876. cms = CMS_EncryptedData_encrypt(in, cipher,
  877. secret_key, secret_keylen, flags);
  878. } else if (operation == SMIME_SIGN_RECEIPT) {
  879. CMS_ContentInfo *srcms = NULL;
  880. STACK_OF(CMS_SignerInfo) *sis;
  881. CMS_SignerInfo *si;
  882. sis = CMS_get0_SignerInfos(cms);
  883. if (!sis)
  884. goto end;
  885. si = sk_CMS_SignerInfo_value(sis, 0);
  886. srcms = CMS_sign_receipt(si, signer, key, other, flags);
  887. if (!srcms)
  888. goto end;
  889. CMS_ContentInfo_free(cms);
  890. cms = srcms;
  891. } else if (operation & SMIME_SIGNERS) {
  892. int i;
  893. /*
  894. * If detached data content we enable streaming if S/MIME output
  895. * format.
  896. */
  897. if (operation == SMIME_SIGN) {
  898. if (flags & CMS_DETACHED) {
  899. if (outformat == FORMAT_SMIME)
  900. flags |= CMS_STREAM;
  901. }
  902. flags |= CMS_PARTIAL;
  903. cms = CMS_sign(NULL, NULL, other, in, flags);
  904. if (!cms)
  905. goto end;
  906. if (econtent_type)
  907. CMS_set1_eContentType(cms, econtent_type);
  908. if (rr_to) {
  909. rr = make_receipt_request(rr_to, rr_allorfirst, rr_from);
  910. if (!rr) {
  911. BIO_puts(bio_err,
  912. "Signed Receipt Request Creation Error\n");
  913. goto end;
  914. }
  915. }
  916. } else
  917. flags |= CMS_REUSE_DIGEST;
  918. for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
  919. CMS_SignerInfo *si;
  920. cms_key_param *kparam;
  921. int tflags = flags;
  922. signerfile = sk_OPENSSL_STRING_value(sksigners, i);
  923. keyfile = sk_OPENSSL_STRING_value(skkeys, i);
  924. signer = load_cert(signerfile, FORMAT_PEM, NULL,
  925. e, "signer certificate");
  926. if (!signer)
  927. goto end;
  928. key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
  929. if (!key)
  930. goto end;
  931. for (kparam = key_first; kparam; kparam = kparam->next) {
  932. if (kparam->idx == i) {
  933. tflags |= CMS_KEY_PARAM;
  934. break;
  935. }
  936. }
  937. si = CMS_add1_signer(cms, signer, key, sign_md, tflags);
  938. if (!si)
  939. goto end;
  940. if (kparam) {
  941. EVP_PKEY_CTX *pctx;
  942. pctx = CMS_SignerInfo_get0_pkey_ctx(si);
  943. if (!cms_set_pkey_param(pctx, kparam->param))
  944. goto end;
  945. }
  946. if (rr && !CMS_add1_ReceiptRequest(si, rr))
  947. goto end;
  948. X509_free(signer);
  949. signer = NULL;
  950. EVP_PKEY_free(key);
  951. key = NULL;
  952. }
  953. /* If not streaming or resigning finalize structure */
  954. if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {
  955. if (!CMS_final(cms, in, NULL, flags))
  956. goto end;
  957. }
  958. }
  959. if (!cms) {
  960. BIO_printf(bio_err, "Error creating CMS structure\n");
  961. goto end;
  962. }
  963. ret = 4;
  964. if (operation == SMIME_DECRYPT) {
  965. if (flags & CMS_DEBUG_DECRYPT)
  966. CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
  967. if (secret_key) {
  968. if (!CMS_decrypt_set1_key(cms,
  969. secret_key, secret_keylen,
  970. secret_keyid, secret_keyidlen)) {
  971. BIO_puts(bio_err, "Error decrypting CMS using secret key\n");
  972. goto end;
  973. }
  974. }
  975. if (key) {
  976. if (!CMS_decrypt_set1_pkey(cms, key, recip)) {
  977. BIO_puts(bio_err, "Error decrypting CMS using private key\n");
  978. goto end;
  979. }
  980. }
  981. if (pwri_pass) {
  982. if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
  983. BIO_puts(bio_err, "Error decrypting CMS using password\n");
  984. goto end;
  985. }
  986. }
  987. if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
  988. BIO_printf(bio_err, "Error decrypting CMS structure\n");
  989. goto end;
  990. }
  991. } else if (operation == SMIME_DATAOUT) {
  992. if (!CMS_data(cms, out, flags))
  993. goto end;
  994. } else if (operation == SMIME_UNCOMPRESS) {
  995. if (!CMS_uncompress(cms, indata, out, flags))
  996. goto end;
  997. } else if (operation == SMIME_DIGEST_VERIFY) {
  998. if (CMS_digest_verify(cms, indata, out, flags) > 0)
  999. BIO_printf(bio_err, "Verification successful\n");
  1000. else {
  1001. BIO_printf(bio_err, "Verification failure\n");
  1002. goto end;
  1003. }
  1004. } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
  1005. if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
  1006. indata, out, flags))
  1007. goto end;
  1008. } else if (operation == SMIME_VERIFY) {
  1009. if (CMS_verify(cms, other, store, indata, out, flags) > 0)
  1010. BIO_printf(bio_err, "Verification successful\n");
  1011. else {
  1012. BIO_printf(bio_err, "Verification failure\n");
  1013. if (verify_retcode)
  1014. ret = verify_err + 32;
  1015. goto end;
  1016. }
  1017. if (signerfile) {
  1018. STACK_OF(X509) *signers;
  1019. signers = CMS_get0_signers(cms);
  1020. if (!save_certs(signerfile, signers)) {
  1021. BIO_printf(bio_err,
  1022. "Error writing signers to %s\n", signerfile);
  1023. ret = 5;
  1024. goto end;
  1025. }
  1026. sk_X509_free(signers);
  1027. }
  1028. if (rr_print)
  1029. receipt_request_print(cms);
  1030. } else if (operation == SMIME_VERIFY_RECEIPT) {
  1031. if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0)
  1032. BIO_printf(bio_err, "Verification successful\n");
  1033. else {
  1034. BIO_printf(bio_err, "Verification failure\n");
  1035. goto end;
  1036. }
  1037. } else {
  1038. if (noout) {
  1039. if (print)
  1040. CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
  1041. } else if (outformat == FORMAT_SMIME) {
  1042. if (to)
  1043. BIO_printf(out, "To: %s\n", to);
  1044. if (from)
  1045. BIO_printf(out, "From: %s\n", from);
  1046. if (subject)
  1047. BIO_printf(out, "Subject: %s\n", subject);
  1048. if (operation == SMIME_RESIGN)
  1049. ret = SMIME_write_CMS(out, cms, indata, flags);
  1050. else
  1051. ret = SMIME_write_CMS(out, cms, in, flags);
  1052. } else if (outformat == FORMAT_PEM)
  1053. ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
  1054. else if (outformat == FORMAT_ASN1)
  1055. ret = i2d_CMS_bio_stream(out, cms, in, flags);
  1056. else {
  1057. BIO_printf(bio_err, "Bad output format for CMS file\n");
  1058. goto end;
  1059. }
  1060. if (ret <= 0) {
  1061. ret = 6;
  1062. goto end;
  1063. }
  1064. }
  1065. ret = 0;
  1066. end:
  1067. if (ret)
  1068. ERR_print_errors(bio_err);
  1069. if (need_rand)
  1070. app_RAND_write_file(NULL);
  1071. sk_X509_pop_free(encerts, X509_free);
  1072. sk_X509_pop_free(other, X509_free);
  1073. X509_VERIFY_PARAM_free(vpm);
  1074. sk_OPENSSL_STRING_free(sksigners);
  1075. sk_OPENSSL_STRING_free(skkeys);
  1076. OPENSSL_free(secret_key);
  1077. OPENSSL_free(secret_keyid);
  1078. OPENSSL_free(pwri_tmp);
  1079. ASN1_OBJECT_free(econtent_type);
  1080. CMS_ReceiptRequest_free(rr);
  1081. sk_OPENSSL_STRING_free(rr_to);
  1082. sk_OPENSSL_STRING_free(rr_from);
  1083. for (key_param = key_first; key_param;) {
  1084. cms_key_param *tparam;
  1085. sk_OPENSSL_STRING_free(key_param->param);
  1086. tparam = key_param->next;
  1087. OPENSSL_free(key_param);
  1088. key_param = tparam;
  1089. }
  1090. X509_STORE_free(store);
  1091. X509_free(cert);
  1092. X509_free(recip);
  1093. X509_free(signer);
  1094. EVP_PKEY_free(key);
  1095. CMS_ContentInfo_free(cms);
  1096. CMS_ContentInfo_free(rcms);
  1097. BIO_free(rctin);
  1098. BIO_free(in);
  1099. BIO_free(indata);
  1100. BIO_free_all(out);
  1101. OPENSSL_free(passin);
  1102. return (ret);
  1103. }
  1104. static int save_certs(char *signerfile, STACK_OF(X509) *signers)
  1105. {
  1106. int i;
  1107. BIO *tmp;
  1108. if (!signerfile)
  1109. return 1;
  1110. tmp = BIO_new_file(signerfile, "w");
  1111. if (!tmp)
  1112. return 0;
  1113. for (i = 0; i < sk_X509_num(signers); i++)
  1114. PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
  1115. BIO_free(tmp);
  1116. return 1;
  1117. }
  1118. /* Minimal callback just to output policy info (if any) */
  1119. static int cms_cb(int ok, X509_STORE_CTX *ctx)
  1120. {
  1121. int error;
  1122. error = X509_STORE_CTX_get_error(ctx);
  1123. verify_err = error;
  1124. if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
  1125. && ((error != X509_V_OK) || (ok != 2)))
  1126. return ok;
  1127. policies_print(ctx);
  1128. return ok;
  1129. }
  1130. static void gnames_stack_print(STACK_OF(GENERAL_NAMES) *gns)
  1131. {
  1132. STACK_OF(GENERAL_NAME) *gens;
  1133. GENERAL_NAME *gen;
  1134. int i, j;
  1135. for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
  1136. gens = sk_GENERAL_NAMES_value(gns, i);
  1137. for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
  1138. gen = sk_GENERAL_NAME_value(gens, j);
  1139. BIO_puts(bio_err, " ");
  1140. GENERAL_NAME_print(bio_err, gen);
  1141. BIO_puts(bio_err, "\n");
  1142. }
  1143. }
  1144. return;
  1145. }
  1146. static void receipt_request_print(CMS_ContentInfo *cms)
  1147. {
  1148. STACK_OF(CMS_SignerInfo) *sis;
  1149. CMS_SignerInfo *si;
  1150. CMS_ReceiptRequest *rr;
  1151. int allorfirst;
  1152. STACK_OF(GENERAL_NAMES) *rto, *rlist;
  1153. ASN1_STRING *scid;
  1154. int i, rv;
  1155. sis = CMS_get0_SignerInfos(cms);
  1156. for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
  1157. si = sk_CMS_SignerInfo_value(sis, i);
  1158. rv = CMS_get1_ReceiptRequest(si, &rr);
  1159. BIO_printf(bio_err, "Signer %d:\n", i + 1);
  1160. if (rv == 0)
  1161. BIO_puts(bio_err, " No Receipt Request\n");
  1162. else if (rv < 0) {
  1163. BIO_puts(bio_err, " Receipt Request Parse Error\n");
  1164. ERR_print_errors(bio_err);
  1165. } else {
  1166. char *id;
  1167. int idlen;
  1168. CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
  1169. &rlist, &rto);
  1170. BIO_puts(bio_err, " Signed Content ID:\n");
  1171. idlen = ASN1_STRING_length(scid);
  1172. id = (char *)ASN1_STRING_data(scid);
  1173. BIO_dump_indent(bio_err, id, idlen, 4);
  1174. BIO_puts(bio_err, " Receipts From");
  1175. if (rlist) {
  1176. BIO_puts(bio_err, " List:\n");
  1177. gnames_stack_print(rlist);
  1178. } else if (allorfirst == 1)
  1179. BIO_puts(bio_err, ": First Tier\n");
  1180. else if (allorfirst == 0)
  1181. BIO_puts(bio_err, ": All\n");
  1182. else
  1183. BIO_printf(bio_err, " Unknown (%d)\n", allorfirst);
  1184. BIO_puts(bio_err, " Receipts To:\n");
  1185. gnames_stack_print(rto);
  1186. }
  1187. CMS_ReceiptRequest_free(rr);
  1188. }
  1189. }
  1190. static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
  1191. {
  1192. int i;
  1193. STACK_OF(GENERAL_NAMES) *ret;
  1194. GENERAL_NAMES *gens = NULL;
  1195. GENERAL_NAME *gen = NULL;
  1196. ret = sk_GENERAL_NAMES_new_null();
  1197. if (!ret)
  1198. goto err;
  1199. for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
  1200. char *str = sk_OPENSSL_STRING_value(ns, i);
  1201. gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
  1202. if (!gen)
  1203. goto err;
  1204. gens = GENERAL_NAMES_new();
  1205. if (gens == NULL)
  1206. goto err;
  1207. if (!sk_GENERAL_NAME_push(gens, gen))
  1208. goto err;
  1209. gen = NULL;
  1210. if (!sk_GENERAL_NAMES_push(ret, gens))
  1211. goto err;
  1212. gens = NULL;
  1213. }
  1214. return ret;
  1215. err:
  1216. sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
  1217. GENERAL_NAMES_free(gens);
  1218. GENERAL_NAME_free(gen);
  1219. return NULL;
  1220. }
  1221. static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING)
  1222. *rr_to, int rr_allorfirst, STACK_OF(OPENSSL_STRING)
  1223. *rr_from)
  1224. {
  1225. STACK_OF(GENERAL_NAMES) *rct_to, *rct_from;
  1226. CMS_ReceiptRequest *rr;
  1227. rct_to = make_names_stack(rr_to);
  1228. if (!rct_to)
  1229. goto err;
  1230. if (rr_from) {
  1231. rct_from = make_names_stack(rr_from);
  1232. if (!rct_from)
  1233. goto err;
  1234. } else
  1235. rct_from = NULL;
  1236. rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,
  1237. rct_to);
  1238. return rr;
  1239. err:
  1240. return NULL;
  1241. }
  1242. static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
  1243. STACK_OF(OPENSSL_STRING) *param)
  1244. {
  1245. char *keyopt;
  1246. int i;
  1247. if (sk_OPENSSL_STRING_num(param) <= 0)
  1248. return 1;
  1249. for (i = 0; i < sk_OPENSSL_STRING_num(param); i++) {
  1250. keyopt = sk_OPENSSL_STRING_value(param, i);
  1251. if (pkey_ctrl_string(pctx, keyopt) <= 0) {
  1252. BIO_printf(bio_err, "parameter error \"%s\"\n", keyopt);
  1253. ERR_print_errors(bio_err);
  1254. return 0;
  1255. }
  1256. }
  1257. return 1;
  1258. }
  1259. #endif