cms.c 49 KB

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