cms.c 50 KB

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