cms.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  1. /*
  2. * Copyright 2008-2022 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 ((flags & CMS_BINARY) != 0) {
  769. if (!(operation & SMIME_OP))
  770. outformat = FORMAT_BINARY;
  771. if (!(operation & SMIME_IP))
  772. informat = FORMAT_BINARY;
  773. if ((operation & SMIME_SIGNERS) != 0 && (flags & CMS_DETACHED) != 0)
  774. binary_files = 1;
  775. if ((operation & SMIME_IP) != 0 && contfile == NULL)
  776. binary_files = 1;
  777. }
  778. if (operation == SMIME_ENCRYPT) {
  779. if (!cipher) {
  780. #ifndef OPENSSL_NO_DES
  781. cipher = (EVP_CIPHER *)EVP_des_ede3_cbc();
  782. #else
  783. BIO_printf(bio_err, "No cipher selected\n");
  784. goto end;
  785. #endif
  786. }
  787. if (secret_key && !secret_keyid) {
  788. BIO_printf(bio_err, "No secret key id\n");
  789. goto end;
  790. }
  791. if (*argv != NULL) {
  792. if (operation == SMIME_ENCRYPT) {
  793. for (; *argv != NULL; argv++) {
  794. cert = load_cert(*argv, FORMAT_UNDEF,
  795. "recipient certificate file");
  796. if (cert == NULL)
  797. goto end;
  798. sk_X509_push(encerts, cert);
  799. cert = NULL;
  800. }
  801. } else {
  802. BIO_printf(bio_err, "Warning: recipient certificate file parameters ignored for operation other than -encrypt\n");
  803. }
  804. }
  805. }
  806. if (certfile != NULL) {
  807. if (!load_certs(certfile, 0, &other, NULL, "certificate file")) {
  808. ERR_print_errors(bio_err);
  809. goto end;
  810. }
  811. }
  812. if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
  813. if ((recip = load_cert(recipfile, FORMAT_UNDEF,
  814. "recipient certificate file")) == NULL) {
  815. ERR_print_errors(bio_err);
  816. goto end;
  817. }
  818. }
  819. if (originatorfile != NULL) {
  820. if ((originator = load_cert(originatorfile, FORMAT_UNDEF,
  821. "originator certificate file")) == NULL) {
  822. ERR_print_errors(bio_err);
  823. goto end;
  824. }
  825. }
  826. if (operation == SMIME_SIGN_RECEIPT) {
  827. if ((signer = load_cert(signerfile, FORMAT_UNDEF,
  828. "receipt signer certificate file")) == NULL) {
  829. ERR_print_errors(bio_err);
  830. goto end;
  831. }
  832. }
  833. if ((operation == SMIME_DECRYPT) || (operation == SMIME_ENCRYPT)) {
  834. if (keyfile == NULL)
  835. keyfile = recipfile;
  836. } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
  837. if (keyfile == NULL)
  838. keyfile = signerfile;
  839. } else {
  840. keyfile = NULL;
  841. }
  842. if (keyfile != NULL) {
  843. key = load_key(keyfile, keyform, 0, passin, e, "signing key");
  844. if (key == NULL)
  845. goto end;
  846. }
  847. if (digesthex != NULL) {
  848. if (operation != SMIME_SIGN) {
  849. BIO_printf(bio_err,
  850. "Cannot use -digest for non-signing operation\n");
  851. goto end;
  852. }
  853. if (infile != NULL
  854. || (flags & CMS_DETACHED) == 0
  855. || (flags & CMS_STREAM) != 0) {
  856. BIO_printf(bio_err,
  857. "Cannot use -digest when -in, -nodetach or streaming is used\n");
  858. goto end;
  859. }
  860. digestbin = OPENSSL_hexstr2buf(digesthex, &digestlen);
  861. if (digestbin == NULL) {
  862. BIO_printf(bio_err,
  863. "Invalid hex value after -digest\n");
  864. goto end;
  865. }
  866. } else {
  867. in = bio_open_default(infile, 'r',
  868. binary_files ? FORMAT_BINARY : informat);
  869. if (in == NULL)
  870. goto end;
  871. }
  872. if (operation & SMIME_IP) {
  873. cms = load_content_info(informat, in, flags, &indata, "SMIME");
  874. if (cms == NULL)
  875. goto end;
  876. if (contfile != NULL) {
  877. BIO_free(indata);
  878. if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
  879. BIO_printf(bio_err, "Can't read content file %s\n", contfile);
  880. goto end;
  881. }
  882. }
  883. if (certsoutfile != NULL) {
  884. STACK_OF(X509) *allcerts;
  885. allcerts = CMS_get1_certs(cms);
  886. if (!save_certs(certsoutfile, allcerts)) {
  887. BIO_printf(bio_err,
  888. "Error writing certs to %s\n", certsoutfile);
  889. ret = 5;
  890. goto end;
  891. }
  892. OSSL_STACK_OF_X509_free(allcerts);
  893. }
  894. }
  895. if (rctfile != NULL) {
  896. char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
  897. if ((rctin = BIO_new_file(rctfile, rctmode)) == NULL) {
  898. BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
  899. goto end;
  900. }
  901. rcms = load_content_info(rctformat, rctin, 0, NULL, "receipt");
  902. if (rcms == NULL)
  903. goto end;
  904. }
  905. out = bio_open_default(outfile, 'w',
  906. binary_files ? FORMAT_BINARY : outformat);
  907. if (out == NULL)
  908. goto end;
  909. if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
  910. if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  911. CAstore, noCAstore)) == NULL)
  912. goto end;
  913. X509_STORE_set_verify_cb(store, cms_cb);
  914. if (vpmtouched)
  915. X509_STORE_set1_param(store, vpm);
  916. }
  917. ret = 3;
  918. if (operation == SMIME_DATA_CREATE) {
  919. cms = CMS_data_create_ex(in, flags, libctx, app_get0_propq());
  920. } else if (operation == SMIME_DIGEST_CREATE) {
  921. cms = CMS_digest_create_ex(in, sign_md, flags, libctx, app_get0_propq());
  922. } else if (operation == SMIME_COMPRESS) {
  923. cms = CMS_compress(in, -1, flags);
  924. } else if (operation == SMIME_ENCRYPT) {
  925. int i;
  926. flags |= CMS_PARTIAL;
  927. cms = CMS_encrypt_ex(NULL, in, cipher, flags, libctx, app_get0_propq());
  928. if (cms == NULL)
  929. goto end;
  930. for (i = 0; i < sk_X509_num(encerts); i++) {
  931. CMS_RecipientInfo *ri;
  932. cms_key_param *kparam;
  933. int tflags = flags | CMS_KEY_PARAM;
  934. /* This flag enforces allocating the EVP_PKEY_CTX for the recipient here */
  935. EVP_PKEY_CTX *pctx;
  936. X509 *x = sk_X509_value(encerts, i);
  937. int res;
  938. for (kparam = key_first; kparam; kparam = kparam->next) {
  939. if (kparam->idx == i) {
  940. break;
  941. }
  942. }
  943. ri = CMS_add1_recipient(cms, x, key, originator, tflags);
  944. if (ri == NULL)
  945. goto end;
  946. pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  947. if (kparam != NULL) {
  948. if (!cms_set_pkey_param(pctx, kparam->param))
  949. goto end;
  950. }
  951. res = EVP_PKEY_CTX_ctrl(pctx, -1, -1,
  952. EVP_PKEY_CTRL_CIPHER,
  953. EVP_CIPHER_get_nid(cipher), NULL);
  954. if (res <= 0 && res != -2)
  955. goto end;
  956. if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE
  957. && wrap_cipher != NULL) {
  958. EVP_CIPHER_CTX *wctx;
  959. wctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  960. EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL);
  961. }
  962. }
  963. if (secret_key != NULL) {
  964. if (!CMS_add0_recipient_key(cms, NID_undef,
  965. secret_key, secret_keylen,
  966. secret_keyid, secret_keyidlen,
  967. NULL, NULL, NULL))
  968. goto end;
  969. /* NULL these because call absorbs them */
  970. secret_key = NULL;
  971. secret_keyid = NULL;
  972. }
  973. if (pwri_pass != NULL) {
  974. pwri_tmp = (unsigned char *)OPENSSL_strdup((char *)pwri_pass);
  975. if (pwri_tmp == NULL)
  976. goto end;
  977. if (CMS_add0_recipient_password(cms,
  978. -1, NID_undef, NID_undef,
  979. pwri_tmp, -1, NULL) == NULL)
  980. goto end;
  981. pwri_tmp = NULL;
  982. }
  983. if (!(flags & CMS_STREAM)) {
  984. if (!CMS_final(cms, in, NULL, flags))
  985. goto end;
  986. }
  987. } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
  988. cms = CMS_EncryptedData_encrypt_ex(in, cipher, secret_key,
  989. secret_keylen, flags, libctx, app_get0_propq());
  990. } else if (operation == SMIME_SIGN_RECEIPT) {
  991. CMS_ContentInfo *srcms = NULL;
  992. STACK_OF(CMS_SignerInfo) *sis;
  993. CMS_SignerInfo *si;
  994. sis = CMS_get0_SignerInfos(cms);
  995. if (sis == NULL)
  996. goto end;
  997. si = sk_CMS_SignerInfo_value(sis, 0);
  998. srcms = CMS_sign_receipt(si, signer, key, other, flags);
  999. if (srcms == NULL)
  1000. goto end;
  1001. CMS_ContentInfo_free(cms);
  1002. cms = srcms;
  1003. } else if (operation & SMIME_SIGNERS) {
  1004. int i;
  1005. /*
  1006. * If detached data content and not signing pre-computed digest, we
  1007. * enable streaming if S/MIME output format.
  1008. */
  1009. if (operation == SMIME_SIGN) {
  1010. if ((flags & CMS_DETACHED) != 0 && digestbin == NULL) {
  1011. if (outformat == FORMAT_SMIME)
  1012. flags |= CMS_STREAM;
  1013. }
  1014. flags |= CMS_PARTIAL;
  1015. cms = CMS_sign_ex(NULL, NULL, other, in, flags, libctx, app_get0_propq());
  1016. if (cms == NULL)
  1017. goto end;
  1018. if (econtent_type != NULL)
  1019. CMS_set1_eContentType(cms, econtent_type);
  1020. if (rr_to != NULL
  1021. && ((rr = make_receipt_request(rr_to, rr_allorfirst, rr_from))
  1022. == NULL)) {
  1023. BIO_puts(bio_err, "Signed Receipt Request Creation Error\n");
  1024. goto end;
  1025. }
  1026. } else {
  1027. flags |= CMS_REUSE_DIGEST;
  1028. }
  1029. for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
  1030. CMS_SignerInfo *si;
  1031. cms_key_param *kparam;
  1032. int tflags = flags;
  1033. signerfile = sk_OPENSSL_STRING_value(sksigners, i);
  1034. keyfile = sk_OPENSSL_STRING_value(skkeys, i);
  1035. signer = load_cert(signerfile, FORMAT_UNDEF, "signer certificate");
  1036. if (signer == NULL) {
  1037. ret = 2;
  1038. goto end;
  1039. }
  1040. key = load_key(keyfile, keyform, 0, passin, e, "signing key");
  1041. if (key == NULL) {
  1042. ret = 2;
  1043. goto end;
  1044. }
  1045. for (kparam = key_first; kparam; kparam = kparam->next) {
  1046. if (kparam->idx == i) {
  1047. tflags |= CMS_KEY_PARAM;
  1048. break;
  1049. }
  1050. }
  1051. si = CMS_add1_signer(cms, signer, key, sign_md, tflags);
  1052. if (si == NULL)
  1053. goto end;
  1054. if (kparam != NULL) {
  1055. EVP_PKEY_CTX *pctx;
  1056. pctx = CMS_SignerInfo_get0_pkey_ctx(si);
  1057. if (!cms_set_pkey_param(pctx, kparam->param))
  1058. goto end;
  1059. }
  1060. if (rr != NULL && !CMS_add1_ReceiptRequest(si, rr))
  1061. goto end;
  1062. X509_free(signer);
  1063. signer = NULL;
  1064. EVP_PKEY_free(key);
  1065. key = NULL;
  1066. }
  1067. /* If not streaming or resigning finalize structure */
  1068. if (operation == SMIME_SIGN && digestbin != NULL
  1069. && (flags & CMS_STREAM) == 0) {
  1070. /* Use pre-computed digest instead of content */
  1071. if (!CMS_final_digest(cms, digestbin, digestlen, NULL, flags))
  1072. goto end;
  1073. } else if (operation == SMIME_SIGN && (flags & CMS_STREAM) == 0) {
  1074. if (!CMS_final(cms, in, NULL, flags))
  1075. goto end;
  1076. }
  1077. }
  1078. if (cms == NULL) {
  1079. BIO_printf(bio_err, "Error creating CMS structure\n");
  1080. goto end;
  1081. }
  1082. ret = 4;
  1083. if (operation == SMIME_DECRYPT) {
  1084. if (flags & CMS_DEBUG_DECRYPT)
  1085. CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
  1086. if (secret_key != NULL) {
  1087. if (!CMS_decrypt_set1_key(cms,
  1088. secret_key, secret_keylen,
  1089. secret_keyid, secret_keyidlen)) {
  1090. BIO_puts(bio_err, "Error decrypting CMS using secret key\n");
  1091. goto end;
  1092. }
  1093. }
  1094. if (key != NULL) {
  1095. if (!CMS_decrypt_set1_pkey_and_peer(cms, key, recip, originator)) {
  1096. BIO_puts(bio_err, "Error decrypting CMS using private key\n");
  1097. goto end;
  1098. }
  1099. }
  1100. if (pwri_pass != NULL) {
  1101. if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
  1102. BIO_puts(bio_err, "Error decrypting CMS using password\n");
  1103. goto end;
  1104. }
  1105. }
  1106. if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
  1107. BIO_printf(bio_err, "Error decrypting CMS structure\n");
  1108. goto end;
  1109. }
  1110. } else if (operation == SMIME_DATA_OUT) {
  1111. if (!CMS_data(cms, out, flags))
  1112. goto end;
  1113. } else if (operation == SMIME_UNCOMPRESS) {
  1114. if (!CMS_uncompress(cms, indata, out, flags))
  1115. goto end;
  1116. } else if (operation == SMIME_DIGEST_VERIFY) {
  1117. if (CMS_digest_verify(cms, indata, out, flags) > 0) {
  1118. BIO_printf(bio_err, "Verification successful\n");
  1119. } else {
  1120. BIO_printf(bio_err, "Verification failure\n");
  1121. goto end;
  1122. }
  1123. } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
  1124. if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
  1125. indata, out, flags))
  1126. goto end;
  1127. } else if (operation == SMIME_VERIFY) {
  1128. if (CMS_verify(cms, other, store, indata, out, flags) > 0) {
  1129. BIO_printf(bio_err, "%s Verification successful\n",
  1130. (flags & CMS_CADES) != 0 ? "CAdES" : "CMS");
  1131. } else {
  1132. BIO_printf(bio_err, "%s Verification failure\n",
  1133. (flags & CMS_CADES) != 0 ? "CAdES" : "CMS");
  1134. if (verify_retcode)
  1135. ret = verify_err + 32;
  1136. goto end;
  1137. }
  1138. if (signerfile != NULL) {
  1139. STACK_OF(X509) *signers = CMS_get0_signers(cms);
  1140. if (!save_certs(signerfile, signers)) {
  1141. BIO_printf(bio_err,
  1142. "Error writing signers to %s\n", signerfile);
  1143. ret = 5;
  1144. goto end;
  1145. }
  1146. sk_X509_free(signers);
  1147. }
  1148. if (rr_print)
  1149. receipt_request_print(cms);
  1150. } else if (operation == SMIME_VERIFY_RECEIPT) {
  1151. if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0) {
  1152. BIO_printf(bio_err, "Verification successful\n");
  1153. } else {
  1154. BIO_printf(bio_err, "Verification failure\n");
  1155. goto end;
  1156. }
  1157. } else {
  1158. if (noout) {
  1159. if (print) {
  1160. ASN1_PCTX *pctx = NULL;
  1161. if (get_nameopt() != XN_FLAG_ONELINE) {
  1162. pctx = ASN1_PCTX_new();
  1163. if (pctx != NULL) { /* Print anyway if malloc failed */
  1164. ASN1_PCTX_set_flags(pctx, ASN1_PCTX_FLAGS_SHOW_ABSENT);
  1165. ASN1_PCTX_set_str_flags(pctx, get_nameopt());
  1166. ASN1_PCTX_set_nm_flags(pctx, get_nameopt());
  1167. }
  1168. }
  1169. CMS_ContentInfo_print_ctx(out, cms, 0, pctx);
  1170. ASN1_PCTX_free(pctx);
  1171. }
  1172. } else if (outformat == FORMAT_SMIME) {
  1173. if (to)
  1174. BIO_printf(out, "To: %s%s", to, mime_eol);
  1175. if (from)
  1176. BIO_printf(out, "From: %s%s", from, mime_eol);
  1177. if (subject)
  1178. BIO_printf(out, "Subject: %s%s", subject, mime_eol);
  1179. if (operation == SMIME_RESIGN)
  1180. ret = SMIME_write_CMS(out, cms, indata, flags);
  1181. else
  1182. ret = SMIME_write_CMS(out, cms, in, flags);
  1183. } else if (outformat == FORMAT_PEM) {
  1184. ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
  1185. } else if (outformat == FORMAT_ASN1) {
  1186. ret = i2d_CMS_bio_stream(out, cms, in, flags);
  1187. } else {
  1188. BIO_printf(bio_err, "Bad output format for CMS file\n");
  1189. goto end;
  1190. }
  1191. if (ret <= 0) {
  1192. ret = 6;
  1193. goto end;
  1194. }
  1195. }
  1196. ret = 0;
  1197. end:
  1198. if (ret)
  1199. ERR_print_errors(bio_err);
  1200. OSSL_STACK_OF_X509_free(encerts);
  1201. OSSL_STACK_OF_X509_free(other);
  1202. X509_VERIFY_PARAM_free(vpm);
  1203. sk_OPENSSL_STRING_free(sksigners);
  1204. sk_OPENSSL_STRING_free(skkeys);
  1205. OPENSSL_free(secret_key);
  1206. OPENSSL_free(secret_keyid);
  1207. OPENSSL_free(pwri_tmp);
  1208. ASN1_OBJECT_free(econtent_type);
  1209. CMS_ReceiptRequest_free(rr);
  1210. sk_OPENSSL_STRING_free(rr_to);
  1211. sk_OPENSSL_STRING_free(rr_from);
  1212. for (key_param = key_first; key_param;) {
  1213. cms_key_param *tparam;
  1214. sk_OPENSSL_STRING_free(key_param->param);
  1215. tparam = key_param->next;
  1216. OPENSSL_free(key_param);
  1217. key_param = tparam;
  1218. }
  1219. X509_STORE_free(store);
  1220. X509_free(cert);
  1221. X509_free(recip);
  1222. X509_free(signer);
  1223. EVP_PKEY_free(key);
  1224. EVP_CIPHER_free(cipher);
  1225. EVP_CIPHER_free(wrap_cipher);
  1226. EVP_MD_free(sign_md);
  1227. CMS_ContentInfo_free(cms);
  1228. CMS_ContentInfo_free(rcms);
  1229. release_engine(e);
  1230. BIO_free(rctin);
  1231. BIO_free(in);
  1232. BIO_free(indata);
  1233. BIO_free_all(out);
  1234. OPENSSL_free(digestbin);
  1235. OPENSSL_free(passin);
  1236. NCONF_free(conf);
  1237. return ret;
  1238. }
  1239. static int save_certs(char *signerfile, STACK_OF(X509) *signers)
  1240. {
  1241. int i;
  1242. BIO *tmp;
  1243. if (signerfile == NULL)
  1244. return 1;
  1245. tmp = BIO_new_file(signerfile, "w");
  1246. if (tmp == NULL)
  1247. return 0;
  1248. for (i = 0; i < sk_X509_num(signers); i++)
  1249. PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
  1250. BIO_free(tmp);
  1251. return 1;
  1252. }
  1253. /* Minimal callback just to output policy info (if any) */
  1254. static int cms_cb(int ok, X509_STORE_CTX *ctx)
  1255. {
  1256. int error;
  1257. error = X509_STORE_CTX_get_error(ctx);
  1258. verify_err = error;
  1259. if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
  1260. && ((error != X509_V_OK) || (ok != 2)))
  1261. return ok;
  1262. policies_print(ctx);
  1263. return ok;
  1264. }
  1265. static void gnames_stack_print(STACK_OF(GENERAL_NAMES) *gns)
  1266. {
  1267. STACK_OF(GENERAL_NAME) *gens;
  1268. GENERAL_NAME *gen;
  1269. int i, j;
  1270. for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
  1271. gens = sk_GENERAL_NAMES_value(gns, i);
  1272. for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
  1273. gen = sk_GENERAL_NAME_value(gens, j);
  1274. BIO_puts(bio_err, " ");
  1275. GENERAL_NAME_print(bio_err, gen);
  1276. BIO_puts(bio_err, "\n");
  1277. }
  1278. }
  1279. return;
  1280. }
  1281. static void receipt_request_print(CMS_ContentInfo *cms)
  1282. {
  1283. STACK_OF(CMS_SignerInfo) *sis;
  1284. CMS_SignerInfo *si;
  1285. CMS_ReceiptRequest *rr;
  1286. int allorfirst;
  1287. STACK_OF(GENERAL_NAMES) *rto, *rlist;
  1288. ASN1_STRING *scid;
  1289. int i, rv;
  1290. sis = CMS_get0_SignerInfos(cms);
  1291. for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
  1292. si = sk_CMS_SignerInfo_value(sis, i);
  1293. rv = CMS_get1_ReceiptRequest(si, &rr);
  1294. BIO_printf(bio_err, "Signer %d:\n", i + 1);
  1295. if (rv == 0) {
  1296. BIO_puts(bio_err, " No Receipt Request\n");
  1297. } else if (rv < 0) {
  1298. BIO_puts(bio_err, " Receipt Request Parse Error\n");
  1299. ERR_print_errors(bio_err);
  1300. } else {
  1301. const char *id;
  1302. int idlen;
  1303. CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
  1304. &rlist, &rto);
  1305. BIO_puts(bio_err, " Signed Content ID:\n");
  1306. idlen = ASN1_STRING_length(scid);
  1307. id = (const char *)ASN1_STRING_get0_data(scid);
  1308. BIO_dump_indent(bio_err, id, idlen, 4);
  1309. BIO_puts(bio_err, " Receipts From");
  1310. if (rlist != NULL) {
  1311. BIO_puts(bio_err, " List:\n");
  1312. gnames_stack_print(rlist);
  1313. } else if (allorfirst == 1) {
  1314. BIO_puts(bio_err, ": First Tier\n");
  1315. } else if (allorfirst == 0) {
  1316. BIO_puts(bio_err, ": All\n");
  1317. } else {
  1318. BIO_printf(bio_err, " Unknown (%d)\n", allorfirst);
  1319. }
  1320. BIO_puts(bio_err, " Receipts To:\n");
  1321. gnames_stack_print(rto);
  1322. }
  1323. CMS_ReceiptRequest_free(rr);
  1324. }
  1325. }
  1326. static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
  1327. {
  1328. int i;
  1329. STACK_OF(GENERAL_NAMES) *ret;
  1330. GENERAL_NAMES *gens = NULL;
  1331. GENERAL_NAME *gen = NULL;
  1332. ret = sk_GENERAL_NAMES_new_null();
  1333. if (ret == NULL)
  1334. goto err;
  1335. for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
  1336. char *str = sk_OPENSSL_STRING_value(ns, i);
  1337. gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
  1338. if (gen == NULL)
  1339. goto err;
  1340. gens = GENERAL_NAMES_new();
  1341. if (gens == NULL)
  1342. goto err;
  1343. if (!sk_GENERAL_NAME_push(gens, gen))
  1344. goto err;
  1345. gen = NULL;
  1346. if (!sk_GENERAL_NAMES_push(ret, gens))
  1347. goto err;
  1348. gens = NULL;
  1349. }
  1350. return ret;
  1351. err:
  1352. sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
  1353. GENERAL_NAMES_free(gens);
  1354. GENERAL_NAME_free(gen);
  1355. return NULL;
  1356. }
  1357. static CMS_ReceiptRequest
  1358. *make_receipt_request(STACK_OF(OPENSSL_STRING) *rr_to, int rr_allorfirst,
  1359. STACK_OF(OPENSSL_STRING) *rr_from)
  1360. {
  1361. STACK_OF(GENERAL_NAMES) *rct_to = NULL, *rct_from = NULL;
  1362. rct_to = make_names_stack(rr_to);
  1363. if (rct_to == NULL)
  1364. goto err;
  1365. if (rr_from != NULL) {
  1366. rct_from = make_names_stack(rr_from);
  1367. if (rct_from == NULL)
  1368. goto err;
  1369. } else {
  1370. rct_from = NULL;
  1371. }
  1372. return CMS_ReceiptRequest_create0_ex(NULL, -1, rr_allorfirst, rct_from,
  1373. rct_to, app_get0_libctx());
  1374. err:
  1375. sk_GENERAL_NAMES_pop_free(rct_to, GENERAL_NAMES_free);
  1376. return NULL;
  1377. }
  1378. static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
  1379. STACK_OF(OPENSSL_STRING) *param)
  1380. {
  1381. char *keyopt;
  1382. int i;
  1383. if (sk_OPENSSL_STRING_num(param) <= 0)
  1384. return 1;
  1385. for (i = 0; i < sk_OPENSSL_STRING_num(param); i++) {
  1386. keyopt = sk_OPENSSL_STRING_value(param, i);
  1387. if (pkey_ctrl_string(pctx, keyopt) <= 0) {
  1388. BIO_printf(bio_err, "parameter error \"%s\"\n", keyopt);
  1389. ERR_print_errors(bio_err);
  1390. return 0;
  1391. }
  1392. }
  1393. return 1;
  1394. }