cms.c 52 KB

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