cms.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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(), FORMAT_UNDEF,
  595. "recipient certificate file");
  596. if (cert == NULL)
  597. goto end;
  598. sk_X509_push(encerts, cert);
  599. cert = NULL;
  600. } else {
  601. recipfile = opt_arg();
  602. }
  603. break;
  604. case OPT_CIPHER:
  605. if (!opt_cipher(opt_unknown(), &cipher))
  606. goto end;
  607. break;
  608. case OPT_KEYOPT:
  609. keyidx = -1;
  610. if (operation == SMIME_ENCRYPT) {
  611. if (encerts != NULL)
  612. keyidx += sk_X509_num(encerts);
  613. } else {
  614. if (keyfile != NULL || signerfile != NULL)
  615. keyidx++;
  616. if (skkeys != NULL)
  617. keyidx += sk_OPENSSL_STRING_num(skkeys);
  618. }
  619. if (keyidx < 0) {
  620. BIO_printf(bio_err, "No key specified\n");
  621. goto opthelp;
  622. }
  623. if (key_param == NULL || key_param->idx != keyidx) {
  624. cms_key_param *nparam;
  625. nparam = app_malloc(sizeof(*nparam), "key param buffer");
  626. if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL) {
  627. OPENSSL_free(nparam);
  628. goto end;
  629. }
  630. nparam->idx = keyidx;
  631. nparam->next = NULL;
  632. if (key_first == NULL)
  633. key_first = nparam;
  634. else
  635. key_param->next = nparam;
  636. key_param = nparam;
  637. }
  638. sk_OPENSSL_STRING_push(key_param->param, opt_arg());
  639. break;
  640. case OPT_V_CASES:
  641. if (!opt_verify(o, vpm))
  642. goto end;
  643. vpmtouched++;
  644. break;
  645. case OPT_R_CASES:
  646. if (!opt_rand(o))
  647. goto end;
  648. break;
  649. case OPT_PROV_CASES:
  650. if (!opt_provider(o))
  651. goto end;
  652. break;
  653. case OPT_CONFIG:
  654. conf = app_load_config_modules(opt_arg());
  655. if (conf == NULL)
  656. goto end;
  657. break;
  658. case OPT_3DES_WRAP:
  659. # ifndef OPENSSL_NO_DES
  660. wrap_cipher = EVP_des_ede3_wrap();
  661. # endif
  662. break;
  663. case OPT_AES128_WRAP:
  664. wrap_cipher = EVP_aes_128_wrap();
  665. break;
  666. case OPT_AES192_WRAP:
  667. wrap_cipher = EVP_aes_192_wrap();
  668. break;
  669. case OPT_AES256_WRAP:
  670. wrap_cipher = EVP_aes_256_wrap();
  671. break;
  672. case OPT_WRAP:
  673. if (!opt_cipher(opt_unknown(), &wrap_cipher))
  674. goto end;
  675. break;
  676. }
  677. }
  678. argc = opt_num_rest();
  679. argv = opt_rest();
  680. if ((rr_allorfirst != -1 || rr_from != NULL) && rr_to == NULL) {
  681. BIO_puts(bio_err, "No Signed Receipts Recipients\n");
  682. goto opthelp;
  683. }
  684. if (!(operation & SMIME_SIGNERS) && (rr_to != NULL || rr_from != NULL)) {
  685. BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
  686. goto opthelp;
  687. }
  688. if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
  689. BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
  690. goto opthelp;
  691. }
  692. if ((flags & CMS_CADES) != 0) {
  693. if ((flags & CMS_NOATTR) != 0) {
  694. BIO_puts(bio_err, "Incompatible options: "
  695. "CAdES required signed attributes\n");
  696. goto opthelp;
  697. }
  698. if (operation == SMIME_VERIFY
  699. && (flags & (CMS_NO_SIGNER_CERT_VERIFY | CMS_NO_ATTR_VERIFY)) != 0) {
  700. BIO_puts(bio_err, "Incompatible options: CAdES validation require"
  701. " certs and signed attributes validations\n");
  702. goto opthelp;
  703. }
  704. }
  705. if (operation & SMIME_SIGNERS) {
  706. if (keyfile != NULL && signerfile == NULL) {
  707. BIO_puts(bio_err, "Illegal -inkey without -signer\n");
  708. goto opthelp;
  709. }
  710. /* Check to see if any final signer needs to be appended */
  711. if (signerfile != NULL) {
  712. if (sksigners == NULL
  713. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  714. goto end;
  715. sk_OPENSSL_STRING_push(sksigners, signerfile);
  716. if (skkeys == NULL && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  717. goto end;
  718. if (keyfile == NULL)
  719. keyfile = signerfile;
  720. sk_OPENSSL_STRING_push(skkeys, keyfile);
  721. }
  722. if (sksigners == NULL) {
  723. BIO_printf(bio_err, "No signer certificate specified\n");
  724. goto opthelp;
  725. }
  726. signerfile = NULL;
  727. keyfile = NULL;
  728. } else if (operation == SMIME_DECRYPT) {
  729. if (recipfile == NULL && keyfile == NULL
  730. && secret_key == NULL && pwri_pass == NULL) {
  731. BIO_printf(bio_err,
  732. "No recipient certificate or key specified\n");
  733. goto opthelp;
  734. }
  735. } else if (operation == SMIME_ENCRYPT) {
  736. if (*argv == NULL && secret_key == NULL
  737. && pwri_pass == NULL && encerts == NULL) {
  738. BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
  739. goto opthelp;
  740. }
  741. } else if (!operation) {
  742. BIO_printf(bio_err, "No operation option (-encrypt|-decrypt|-sign|-verify|...) specified.\n");
  743. goto opthelp;
  744. }
  745. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  746. BIO_printf(bio_err, "Error getting password\n");
  747. goto end;
  748. }
  749. ret = 2;
  750. if (!(operation & SMIME_SIGNERS))
  751. flags &= ~CMS_DETACHED;
  752. if (!(operation & SMIME_OP))
  753. if (flags & CMS_BINARY)
  754. outformat = FORMAT_BINARY;
  755. if (!(operation & SMIME_IP))
  756. if (flags & CMS_BINARY)
  757. informat = FORMAT_BINARY;
  758. if (operation == SMIME_ENCRYPT) {
  759. if (!cipher) {
  760. # ifndef OPENSSL_NO_DES
  761. cipher = EVP_des_ede3_cbc();
  762. # else
  763. BIO_printf(bio_err, "No cipher selected\n");
  764. goto end;
  765. # endif
  766. }
  767. if (secret_key && !secret_keyid) {
  768. BIO_printf(bio_err, "No secret key id\n");
  769. goto end;
  770. }
  771. if (*argv && encerts == NULL)
  772. if ((encerts = sk_X509_new_null()) == NULL)
  773. goto end;
  774. while (*argv) {
  775. if ((cert = load_cert(*argv, FORMAT_UNDEF,
  776. "recipient certificate file")) == NULL)
  777. goto end;
  778. sk_X509_push(encerts, cert);
  779. cert = NULL;
  780. argv++;
  781. }
  782. }
  783. if (certfile != NULL) {
  784. if (!load_certs(certfile, &other, NULL, "certificate file")) {
  785. ERR_print_errors(bio_err);
  786. goto end;
  787. }
  788. }
  789. if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
  790. if ((recip = load_cert(recipfile, FORMAT_UNDEF,
  791. "recipient certificate file")) == NULL) {
  792. ERR_print_errors(bio_err);
  793. goto end;
  794. }
  795. }
  796. if (originatorfile != NULL) {
  797. if ((originator = load_cert(originatorfile, FORMAT_UNDEF,
  798. "originator certificate file")) == NULL) {
  799. ERR_print_errors(bio_err);
  800. goto end;
  801. }
  802. }
  803. if (operation == SMIME_SIGN_RECEIPT) {
  804. if ((signer = load_cert(signerfile, FORMAT_UNDEF,
  805. "receipt signer certificate file")) == NULL) {
  806. ERR_print_errors(bio_err);
  807. goto end;
  808. }
  809. }
  810. if ((operation == SMIME_DECRYPT) || (operation == SMIME_ENCRYPT)) {
  811. if (keyfile == NULL)
  812. keyfile = recipfile;
  813. } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
  814. if (keyfile == NULL)
  815. keyfile = signerfile;
  816. } else {
  817. keyfile = NULL;
  818. }
  819. if (keyfile != NULL) {
  820. key = load_key(keyfile, keyform, 0, passin, e, "signing key");
  821. if (key == NULL)
  822. goto end;
  823. }
  824. in = bio_open_default(infile, 'r', informat);
  825. if (in == NULL)
  826. goto end;
  827. if (operation & SMIME_IP) {
  828. cms = load_content_info(informat, in, &indata, "SMIME", libctx, propq);
  829. if (cms == NULL)
  830. goto end;
  831. if (contfile != NULL) {
  832. BIO_free(indata);
  833. if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
  834. BIO_printf(bio_err, "Can't read content file %s\n", contfile);
  835. goto end;
  836. }
  837. }
  838. if (certsoutfile != NULL) {
  839. STACK_OF(X509) *allcerts;
  840. allcerts = CMS_get1_certs(cms);
  841. if (!save_certs(certsoutfile, allcerts)) {
  842. BIO_printf(bio_err,
  843. "Error writing certs to %s\n", certsoutfile);
  844. ret = 5;
  845. goto end;
  846. }
  847. sk_X509_pop_free(allcerts, X509_free);
  848. }
  849. }
  850. if (rctfile != NULL) {
  851. char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
  852. if ((rctin = BIO_new_file(rctfile, rctmode)) == NULL) {
  853. BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
  854. goto end;
  855. }
  856. rcms = load_content_info(rctformat, rctin, NULL, "recipient", libctx,
  857. propq);
  858. if (rcms == NULL)
  859. goto end;
  860. }
  861. out = bio_open_default(outfile, 'w', outformat);
  862. if (out == NULL)
  863. goto end;
  864. if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
  865. if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  866. CAstore, noCAstore)) == NULL)
  867. goto end;
  868. X509_STORE_set_verify_cb(store, cms_cb);
  869. if (vpmtouched)
  870. X509_STORE_set1_param(store, vpm);
  871. }
  872. ret = 3;
  873. if (operation == SMIME_DATA_CREATE) {
  874. cms = CMS_data_create_ex(in, flags, libctx, propq);
  875. } else if (operation == SMIME_DIGEST_CREATE) {
  876. cms = CMS_digest_create_ex(in, sign_md, flags, libctx, propq);
  877. } else if (operation == SMIME_COMPRESS) {
  878. cms = CMS_compress(in, -1, flags);
  879. } else if (operation == SMIME_ENCRYPT) {
  880. int i;
  881. flags |= CMS_PARTIAL;
  882. cms = CMS_encrypt_ex(NULL, in, cipher, flags, libctx, propq);
  883. if (cms == NULL)
  884. goto end;
  885. for (i = 0; i < sk_X509_num(encerts); i++) {
  886. CMS_RecipientInfo *ri;
  887. cms_key_param *kparam;
  888. int tflags = flags | CMS_KEY_PARAM; /* This flag enforces allocating the EVP_PKEY_CTX for the recipient here */
  889. EVP_PKEY_CTX *pctx;
  890. X509 *x = sk_X509_value(encerts, i);
  891. int res;
  892. for (kparam = key_first; kparam; kparam = kparam->next) {
  893. if (kparam->idx == i) {
  894. break;
  895. }
  896. }
  897. ri = CMS_add1_recipient(cms, x, key, originator, tflags);
  898. if (ri == NULL)
  899. goto end;
  900. pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  901. if (kparam != NULL) {
  902. if (!cms_set_pkey_param(pctx, kparam->param))
  903. goto end;
  904. }
  905. res = EVP_PKEY_CTX_ctrl(pctx, -1, -1,
  906. EVP_PKEY_CTRL_CIPHER,
  907. EVP_CIPHER_nid(cipher), NULL);
  908. if (res <= 0 && res != -2)
  909. goto end;
  910. if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE
  911. && wrap_cipher) {
  912. EVP_CIPHER_CTX *wctx;
  913. wctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  914. EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL);
  915. }
  916. }
  917. if (secret_key != NULL) {
  918. if (!CMS_add0_recipient_key(cms, NID_undef,
  919. secret_key, secret_keylen,
  920. secret_keyid, secret_keyidlen,
  921. NULL, NULL, NULL))
  922. goto end;
  923. /* NULL these because call absorbs them */
  924. secret_key = NULL;
  925. secret_keyid = NULL;
  926. }
  927. if (pwri_pass != NULL) {
  928. pwri_tmp = (unsigned char *)OPENSSL_strdup((char *)pwri_pass);
  929. if (pwri_tmp == NULL)
  930. goto end;
  931. if (CMS_add0_recipient_password(cms,
  932. -1, NID_undef, NID_undef,
  933. pwri_tmp, -1, NULL) == NULL)
  934. goto end;
  935. pwri_tmp = NULL;
  936. }
  937. if (!(flags & CMS_STREAM)) {
  938. if (!CMS_final(cms, in, NULL, flags))
  939. goto end;
  940. }
  941. } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
  942. cms = CMS_EncryptedData_encrypt_ex(in, cipher, secret_key,
  943. secret_keylen, flags, libctx, propq);
  944. } else if (operation == SMIME_SIGN_RECEIPT) {
  945. CMS_ContentInfo *srcms = NULL;
  946. STACK_OF(CMS_SignerInfo) *sis;
  947. CMS_SignerInfo *si;
  948. sis = CMS_get0_SignerInfos(cms);
  949. if (sis == NULL)
  950. goto end;
  951. si = sk_CMS_SignerInfo_value(sis, 0);
  952. srcms = CMS_sign_receipt(si, signer, key, other, flags);
  953. if (srcms == NULL)
  954. goto end;
  955. CMS_ContentInfo_free(cms);
  956. cms = srcms;
  957. } else if (operation & SMIME_SIGNERS) {
  958. int i;
  959. /*
  960. * If detached data content we enable streaming if S/MIME output
  961. * format.
  962. */
  963. if (operation == SMIME_SIGN) {
  964. if (flags & CMS_DETACHED) {
  965. if (outformat == FORMAT_SMIME)
  966. flags |= CMS_STREAM;
  967. }
  968. flags |= CMS_PARTIAL;
  969. cms = CMS_sign_ex(NULL, NULL, other, in, flags, libctx, propq);
  970. if (cms == NULL)
  971. goto end;
  972. if (econtent_type != NULL)
  973. CMS_set1_eContentType(cms, econtent_type);
  974. if (rr_to != NULL) {
  975. rr = make_receipt_request(rr_to, rr_allorfirst, rr_from, libctx,
  976. propq);
  977. if (rr == NULL) {
  978. BIO_puts(bio_err,
  979. "Signed Receipt Request Creation Error\n");
  980. goto end;
  981. }
  982. }
  983. } else {
  984. flags |= CMS_REUSE_DIGEST;
  985. }
  986. for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
  987. CMS_SignerInfo *si;
  988. cms_key_param *kparam;
  989. int tflags = flags;
  990. signerfile = sk_OPENSSL_STRING_value(sksigners, i);
  991. keyfile = sk_OPENSSL_STRING_value(skkeys, i);
  992. signer = load_cert(signerfile, FORMAT_UNDEF,
  993. "signer certificate");
  994. if (signer == NULL) {
  995. ret = 2;
  996. goto end;
  997. }
  998. key = load_key(keyfile, keyform, 0, passin, e, "signing key");
  999. if (key == NULL) {
  1000. ret = 2;
  1001. goto end;
  1002. }
  1003. for (kparam = key_first; kparam; kparam = kparam->next) {
  1004. if (kparam->idx == i) {
  1005. tflags |= CMS_KEY_PARAM;
  1006. break;
  1007. }
  1008. }
  1009. si = CMS_add1_signer(cms, signer, key, sign_md, tflags);
  1010. if (si == NULL)
  1011. goto end;
  1012. if (kparam != NULL) {
  1013. EVP_PKEY_CTX *pctx;
  1014. pctx = CMS_SignerInfo_get0_pkey_ctx(si);
  1015. if (!cms_set_pkey_param(pctx, kparam->param))
  1016. goto end;
  1017. }
  1018. if (rr != NULL && !CMS_add1_ReceiptRequest(si, rr))
  1019. goto end;
  1020. X509_free(signer);
  1021. signer = NULL;
  1022. EVP_PKEY_free(key);
  1023. key = NULL;
  1024. }
  1025. /* If not streaming or resigning finalize structure */
  1026. if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {
  1027. if (!CMS_final(cms, in, NULL, flags))
  1028. goto end;
  1029. }
  1030. }
  1031. if (cms == NULL) {
  1032. BIO_printf(bio_err, "Error creating CMS structure\n");
  1033. goto end;
  1034. }
  1035. ret = 4;
  1036. if (operation == SMIME_DECRYPT) {
  1037. if (flags & CMS_DEBUG_DECRYPT)
  1038. CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
  1039. if (secret_key != NULL) {
  1040. if (!CMS_decrypt_set1_key(cms,
  1041. secret_key, secret_keylen,
  1042. secret_keyid, secret_keyidlen)) {
  1043. BIO_puts(bio_err, "Error decrypting CMS using secret key\n");
  1044. goto end;
  1045. }
  1046. }
  1047. if (key != NULL) {
  1048. if (!CMS_decrypt_set1_pkey_and_peer(cms, key, recip, originator)) {
  1049. BIO_puts(bio_err, "Error decrypting CMS using private key\n");
  1050. goto end;
  1051. }
  1052. }
  1053. if (pwri_pass != NULL) {
  1054. if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
  1055. BIO_puts(bio_err, "Error decrypting CMS using password\n");
  1056. goto end;
  1057. }
  1058. }
  1059. if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
  1060. BIO_printf(bio_err, "Error decrypting CMS structure\n");
  1061. goto end;
  1062. }
  1063. } else if (operation == SMIME_DATAOUT) {
  1064. if (!CMS_data(cms, out, flags))
  1065. goto end;
  1066. } else if (operation == SMIME_UNCOMPRESS) {
  1067. if (!CMS_uncompress(cms, indata, out, flags))
  1068. goto end;
  1069. } else if (operation == SMIME_DIGEST_VERIFY) {
  1070. if (CMS_digest_verify(cms, indata, out, flags) > 0) {
  1071. BIO_printf(bio_err, "Verification successful\n");
  1072. } else {
  1073. BIO_printf(bio_err, "Verification failure\n");
  1074. goto end;
  1075. }
  1076. } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
  1077. if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
  1078. indata, out, flags))
  1079. goto end;
  1080. } else if (operation == SMIME_VERIFY) {
  1081. if (CMS_verify(cms, other, store, indata, out, flags) > 0) {
  1082. BIO_printf(bio_err, "%s Verification successful\n",
  1083. (flags & CMS_CADES) ? "CAdES" : "CMS");
  1084. } else {
  1085. BIO_printf(bio_err, "Verification failure\n");
  1086. if (verify_retcode)
  1087. ret = verify_err + 32;
  1088. goto end;
  1089. }
  1090. if (signerfile != NULL) {
  1091. STACK_OF(X509) *signers;
  1092. signers = CMS_get0_signers(cms);
  1093. if (!save_certs(signerfile, signers)) {
  1094. BIO_printf(bio_err,
  1095. "Error writing signers to %s\n", signerfile);
  1096. ret = 5;
  1097. goto end;
  1098. }
  1099. sk_X509_free(signers);
  1100. }
  1101. if (rr_print)
  1102. receipt_request_print(cms);
  1103. } else if (operation == SMIME_VERIFY_RECEIPT) {
  1104. if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0) {
  1105. BIO_printf(bio_err, "Verification successful\n");
  1106. } else {
  1107. BIO_printf(bio_err, "Verification failure\n");
  1108. goto end;
  1109. }
  1110. } else {
  1111. if (noout) {
  1112. if (print) {
  1113. ASN1_PCTX *pctx = NULL;
  1114. if (get_nameopt() != XN_FLAG_ONELINE) {
  1115. pctx = ASN1_PCTX_new();
  1116. if (pctx != NULL) { /* Print anyway if malloc failed */
  1117. ASN1_PCTX_set_flags(pctx, ASN1_PCTX_FLAGS_SHOW_ABSENT);
  1118. ASN1_PCTX_set_str_flags(pctx, get_nameopt());
  1119. ASN1_PCTX_set_nm_flags(pctx, get_nameopt());
  1120. }
  1121. }
  1122. CMS_ContentInfo_print_ctx(out, cms, 0, pctx);
  1123. ASN1_PCTX_free(pctx);
  1124. }
  1125. } else if (outformat == FORMAT_SMIME) {
  1126. if (to)
  1127. BIO_printf(out, "To: %s%s", to, mime_eol);
  1128. if (from)
  1129. BIO_printf(out, "From: %s%s", from, mime_eol);
  1130. if (subject)
  1131. BIO_printf(out, "Subject: %s%s", subject, mime_eol);
  1132. if (operation == SMIME_RESIGN)
  1133. ret = SMIME_write_CMS(out, cms, indata, flags);
  1134. else
  1135. ret = SMIME_write_CMS(out, cms, in, flags);
  1136. } else if (outformat == FORMAT_PEM) {
  1137. ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
  1138. } else if (outformat == FORMAT_ASN1) {
  1139. ret = i2d_CMS_bio_stream(out, cms, in, flags);
  1140. } else {
  1141. BIO_printf(bio_err, "Bad output format for CMS file\n");
  1142. goto end;
  1143. }
  1144. if (ret <= 0) {
  1145. ret = 6;
  1146. goto end;
  1147. }
  1148. }
  1149. ret = 0;
  1150. end:
  1151. if (ret)
  1152. ERR_print_errors(bio_err);
  1153. sk_X509_pop_free(encerts, X509_free);
  1154. sk_X509_pop_free(other, X509_free);
  1155. X509_VERIFY_PARAM_free(vpm);
  1156. sk_OPENSSL_STRING_free(sksigners);
  1157. sk_OPENSSL_STRING_free(skkeys);
  1158. OPENSSL_free(secret_key);
  1159. OPENSSL_free(secret_keyid);
  1160. OPENSSL_free(pwri_tmp);
  1161. ASN1_OBJECT_free(econtent_type);
  1162. CMS_ReceiptRequest_free(rr);
  1163. sk_OPENSSL_STRING_free(rr_to);
  1164. sk_OPENSSL_STRING_free(rr_from);
  1165. for (key_param = key_first; key_param;) {
  1166. cms_key_param *tparam;
  1167. sk_OPENSSL_STRING_free(key_param->param);
  1168. tparam = key_param->next;
  1169. OPENSSL_free(key_param);
  1170. key_param = tparam;
  1171. }
  1172. X509_STORE_free(store);
  1173. X509_free(cert);
  1174. X509_free(recip);
  1175. X509_free(signer);
  1176. EVP_PKEY_free(key);
  1177. CMS_ContentInfo_free(cms);
  1178. CMS_ContentInfo_free(rcms);
  1179. release_engine(e);
  1180. BIO_free(rctin);
  1181. BIO_free(in);
  1182. BIO_free(indata);
  1183. BIO_free_all(out);
  1184. OPENSSL_free(passin);
  1185. NCONF_free(conf);
  1186. return ret;
  1187. }
  1188. static int save_certs(char *signerfile, STACK_OF(X509) *signers)
  1189. {
  1190. int i;
  1191. BIO *tmp;
  1192. if (signerfile == NULL)
  1193. return 1;
  1194. tmp = BIO_new_file(signerfile, "w");
  1195. if (tmp == NULL)
  1196. return 0;
  1197. for (i = 0; i < sk_X509_num(signers); i++)
  1198. PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
  1199. BIO_free(tmp);
  1200. return 1;
  1201. }
  1202. /* Minimal callback just to output policy info (if any) */
  1203. static int cms_cb(int ok, X509_STORE_CTX *ctx)
  1204. {
  1205. int error;
  1206. error = X509_STORE_CTX_get_error(ctx);
  1207. verify_err = error;
  1208. if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
  1209. && ((error != X509_V_OK) || (ok != 2)))
  1210. return ok;
  1211. policies_print(ctx);
  1212. return ok;
  1213. }
  1214. static void gnames_stack_print(STACK_OF(GENERAL_NAMES) *gns)
  1215. {
  1216. STACK_OF(GENERAL_NAME) *gens;
  1217. GENERAL_NAME *gen;
  1218. int i, j;
  1219. for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
  1220. gens = sk_GENERAL_NAMES_value(gns, i);
  1221. for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
  1222. gen = sk_GENERAL_NAME_value(gens, j);
  1223. BIO_puts(bio_err, " ");
  1224. GENERAL_NAME_print(bio_err, gen);
  1225. BIO_puts(bio_err, "\n");
  1226. }
  1227. }
  1228. return;
  1229. }
  1230. static void receipt_request_print(CMS_ContentInfo *cms)
  1231. {
  1232. STACK_OF(CMS_SignerInfo) *sis;
  1233. CMS_SignerInfo *si;
  1234. CMS_ReceiptRequest *rr;
  1235. int allorfirst;
  1236. STACK_OF(GENERAL_NAMES) *rto, *rlist;
  1237. ASN1_STRING *scid;
  1238. int i, rv;
  1239. sis = CMS_get0_SignerInfos(cms);
  1240. for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
  1241. si = sk_CMS_SignerInfo_value(sis, i);
  1242. rv = CMS_get1_ReceiptRequest(si, &rr);
  1243. BIO_printf(bio_err, "Signer %d:\n", i + 1);
  1244. if (rv == 0) {
  1245. BIO_puts(bio_err, " No Receipt Request\n");
  1246. } else if (rv < 0) {
  1247. BIO_puts(bio_err, " Receipt Request Parse Error\n");
  1248. ERR_print_errors(bio_err);
  1249. } else {
  1250. const char *id;
  1251. int idlen;
  1252. CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
  1253. &rlist, &rto);
  1254. BIO_puts(bio_err, " Signed Content ID:\n");
  1255. idlen = ASN1_STRING_length(scid);
  1256. id = (const char *)ASN1_STRING_get0_data(scid);
  1257. BIO_dump_indent(bio_err, id, idlen, 4);
  1258. BIO_puts(bio_err, " Receipts From");
  1259. if (rlist != NULL) {
  1260. BIO_puts(bio_err, " List:\n");
  1261. gnames_stack_print(rlist);
  1262. } else if (allorfirst == 1) {
  1263. BIO_puts(bio_err, ": First Tier\n");
  1264. } else if (allorfirst == 0) {
  1265. BIO_puts(bio_err, ": All\n");
  1266. } else {
  1267. BIO_printf(bio_err, " Unknown (%d)\n", allorfirst);
  1268. }
  1269. BIO_puts(bio_err, " Receipts To:\n");
  1270. gnames_stack_print(rto);
  1271. }
  1272. CMS_ReceiptRequest_free(rr);
  1273. }
  1274. }
  1275. static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
  1276. {
  1277. int i;
  1278. STACK_OF(GENERAL_NAMES) *ret;
  1279. GENERAL_NAMES *gens = NULL;
  1280. GENERAL_NAME *gen = NULL;
  1281. ret = sk_GENERAL_NAMES_new_null();
  1282. if (ret == NULL)
  1283. goto err;
  1284. for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
  1285. char *str = sk_OPENSSL_STRING_value(ns, i);
  1286. gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
  1287. if (gen == NULL)
  1288. goto err;
  1289. gens = GENERAL_NAMES_new();
  1290. if (gens == NULL)
  1291. goto err;
  1292. if (!sk_GENERAL_NAME_push(gens, gen))
  1293. goto err;
  1294. gen = NULL;
  1295. if (!sk_GENERAL_NAMES_push(ret, gens))
  1296. goto err;
  1297. gens = NULL;
  1298. }
  1299. return ret;
  1300. err:
  1301. sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
  1302. GENERAL_NAMES_free(gens);
  1303. GENERAL_NAME_free(gen);
  1304. return NULL;
  1305. }
  1306. static CMS_ReceiptRequest *make_receipt_request(
  1307. STACK_OF(OPENSSL_STRING) *rr_to, int rr_allorfirst,
  1308. STACK_OF(OPENSSL_STRING) *rr_from,
  1309. OSSL_LIB_CTX *libctx, const char *propq)
  1310. {
  1311. STACK_OF(GENERAL_NAMES) *rct_to = NULL, *rct_from = NULL;
  1312. CMS_ReceiptRequest *rr;
  1313. rct_to = make_names_stack(rr_to);
  1314. if (rct_to == NULL)
  1315. goto err;
  1316. if (rr_from != NULL) {
  1317. rct_from = make_names_stack(rr_from);
  1318. if (rct_from == NULL)
  1319. goto err;
  1320. } else {
  1321. rct_from = NULL;
  1322. }
  1323. rr = CMS_ReceiptRequest_create0_ex(NULL, -1, rr_allorfirst, rct_from,
  1324. rct_to, libctx, propq);
  1325. return rr;
  1326. err:
  1327. sk_GENERAL_NAMES_pop_free(rct_to, GENERAL_NAMES_free);
  1328. return NULL;
  1329. }
  1330. static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
  1331. STACK_OF(OPENSSL_STRING) *param)
  1332. {
  1333. char *keyopt;
  1334. int i;
  1335. if (sk_OPENSSL_STRING_num(param) <= 0)
  1336. return 1;
  1337. for (i = 0; i < sk_OPENSSL_STRING_num(param); i++) {
  1338. keyopt = sk_OPENSSL_STRING_value(param, i);
  1339. if (pkey_ctrl_string(pctx, keyopt) <= 0) {
  1340. BIO_printf(bio_err, "parameter error \"%s\"\n", keyopt);
  1341. ERR_print_errors(bio_err);
  1342. return 0;
  1343. }
  1344. }
  1345. return 1;
  1346. }
  1347. #endif