cms.c 51 KB

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