cms.c 48 KB

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