cms.c 49 KB

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