cms.c 51 KB

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