cms.c 51 KB

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