2
0

cms.c 50 KB

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