cms.c 44 KB

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