cms.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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 (operation & SMIME_SIGNERS) {
  590. if (keyfile != NULL && signerfile == NULL) {
  591. BIO_puts(bio_err, "Illegal -inkey without -signer\n");
  592. goto opthelp;
  593. }
  594. /* Check to see if any final signer needs to be appended */
  595. if (signerfile != NULL) {
  596. if (sksigners == NULL
  597. && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
  598. goto end;
  599. sk_OPENSSL_STRING_push(sksigners, signerfile);
  600. if (skkeys == NULL && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
  601. goto end;
  602. if (keyfile == NULL)
  603. keyfile = signerfile;
  604. sk_OPENSSL_STRING_push(skkeys, keyfile);
  605. }
  606. if (sksigners == NULL) {
  607. BIO_printf(bio_err, "No signer certificate specified\n");
  608. goto opthelp;
  609. }
  610. signerfile = NULL;
  611. keyfile = NULL;
  612. } else if (operation == SMIME_DECRYPT) {
  613. if (recipfile == NULL && keyfile == NULL
  614. && secret_key == NULL && pwri_pass == NULL) {
  615. BIO_printf(bio_err,
  616. "No recipient certificate or key specified\n");
  617. goto opthelp;
  618. }
  619. } else if (operation == SMIME_ENCRYPT) {
  620. if (*argv == NULL && secret_key == NULL
  621. && pwri_pass == NULL && encerts == NULL) {
  622. BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
  623. goto opthelp;
  624. }
  625. } else if (!operation) {
  626. goto opthelp;
  627. }
  628. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  629. BIO_printf(bio_err, "Error getting password\n");
  630. goto end;
  631. }
  632. ret = 2;
  633. if (!(operation & SMIME_SIGNERS))
  634. flags &= ~CMS_DETACHED;
  635. if (!(operation & SMIME_OP))
  636. if (flags & CMS_BINARY)
  637. outformat = FORMAT_BINARY;
  638. if (!(operation & SMIME_IP))
  639. if (flags & CMS_BINARY)
  640. informat = FORMAT_BINARY;
  641. if (operation == SMIME_ENCRYPT) {
  642. if (!cipher) {
  643. # ifndef OPENSSL_NO_DES
  644. cipher = EVP_des_ede3_cbc();
  645. # else
  646. BIO_printf(bio_err, "No cipher selected\n");
  647. goto end;
  648. # endif
  649. }
  650. if (secret_key && !secret_keyid) {
  651. BIO_printf(bio_err, "No secret key id\n");
  652. goto end;
  653. }
  654. if (*argv && encerts == NULL)
  655. if ((encerts = sk_X509_new_null()) == NULL)
  656. goto end;
  657. while (*argv) {
  658. if ((cert = load_cert(*argv, FORMAT_PEM,
  659. "recipient certificate file")) == NULL)
  660. goto end;
  661. sk_X509_push(encerts, cert);
  662. cert = NULL;
  663. argv++;
  664. }
  665. }
  666. if (certfile != NULL) {
  667. if (!load_certs(certfile, &other, FORMAT_PEM, NULL,
  668. "certificate file")) {
  669. ERR_print_errors(bio_err);
  670. goto end;
  671. }
  672. }
  673. if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
  674. if ((recip = load_cert(recipfile, FORMAT_PEM,
  675. "recipient certificate file")) == NULL) {
  676. ERR_print_errors(bio_err);
  677. goto end;
  678. }
  679. }
  680. if (operation == SMIME_SIGN_RECEIPT) {
  681. if ((signer = load_cert(signerfile, FORMAT_PEM,
  682. "receipt signer certificate file")) == NULL) {
  683. ERR_print_errors(bio_err);
  684. goto end;
  685. }
  686. }
  687. if (operation == SMIME_DECRYPT) {
  688. if (keyfile == NULL)
  689. keyfile = recipfile;
  690. } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
  691. if (keyfile == NULL)
  692. keyfile = signerfile;
  693. } else {
  694. keyfile = NULL;
  695. }
  696. if (keyfile != NULL) {
  697. key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
  698. if (key == NULL)
  699. goto end;
  700. }
  701. in = bio_open_default(infile, 'r', informat);
  702. if (in == NULL)
  703. goto end;
  704. if (operation & SMIME_IP) {
  705. if (informat == FORMAT_SMIME) {
  706. cms = SMIME_read_CMS(in, &indata);
  707. } else if (informat == FORMAT_PEM) {
  708. cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
  709. } else if (informat == FORMAT_ASN1) {
  710. cms = d2i_CMS_bio(in, NULL);
  711. } else {
  712. BIO_printf(bio_err, "Bad input format for CMS file\n");
  713. goto end;
  714. }
  715. if (cms == NULL) {
  716. BIO_printf(bio_err, "Error reading S/MIME message\n");
  717. goto end;
  718. }
  719. if (contfile != NULL) {
  720. BIO_free(indata);
  721. if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
  722. BIO_printf(bio_err, "Can't read content file %s\n", contfile);
  723. goto end;
  724. }
  725. }
  726. if (certsoutfile != NULL) {
  727. STACK_OF(X509) *allcerts;
  728. allcerts = CMS_get1_certs(cms);
  729. if (!save_certs(certsoutfile, allcerts)) {
  730. BIO_printf(bio_err,
  731. "Error writing certs to %s\n", certsoutfile);
  732. ret = 5;
  733. goto end;
  734. }
  735. sk_X509_pop_free(allcerts, X509_free);
  736. }
  737. }
  738. if (rctfile != NULL) {
  739. char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
  740. if ((rctin = BIO_new_file(rctfile, rctmode)) == NULL) {
  741. BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
  742. goto end;
  743. }
  744. if (rctformat == FORMAT_SMIME) {
  745. rcms = SMIME_read_CMS(rctin, NULL);
  746. } else if (rctformat == FORMAT_PEM) {
  747. rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
  748. } else if (rctformat == FORMAT_ASN1) {
  749. rcms = d2i_CMS_bio(rctin, NULL);
  750. } else {
  751. BIO_printf(bio_err, "Bad input format for receipt\n");
  752. goto end;
  753. }
  754. if (rcms == NULL) {
  755. BIO_printf(bio_err, "Error reading receipt\n");
  756. goto end;
  757. }
  758. }
  759. out = bio_open_default(outfile, 'w', outformat);
  760. if (out == NULL)
  761. goto end;
  762. if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
  763. if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
  764. goto end;
  765. X509_STORE_set_verify_cb(store, cms_cb);
  766. if (vpmtouched)
  767. X509_STORE_set1_param(store, vpm);
  768. }
  769. ret = 3;
  770. if (operation == SMIME_DATA_CREATE) {
  771. cms = CMS_data_create(in, flags);
  772. } else if (operation == SMIME_DIGEST_CREATE) {
  773. cms = CMS_digest_create(in, sign_md, flags);
  774. } else if (operation == SMIME_COMPRESS) {
  775. cms = CMS_compress(in, -1, flags);
  776. } else if (operation == SMIME_ENCRYPT) {
  777. int i;
  778. flags |= CMS_PARTIAL;
  779. cms = CMS_encrypt(NULL, in, cipher, flags);
  780. if (cms == NULL)
  781. goto end;
  782. for (i = 0; i < sk_X509_num(encerts); i++) {
  783. CMS_RecipientInfo *ri;
  784. cms_key_param *kparam;
  785. int tflags = flags;
  786. X509 *x = sk_X509_value(encerts, i);
  787. for (kparam = key_first; kparam; kparam = kparam->next) {
  788. if (kparam->idx == i) {
  789. tflags |= CMS_KEY_PARAM;
  790. break;
  791. }
  792. }
  793. ri = CMS_add1_recipient_cert(cms, x, tflags);
  794. if (ri == NULL)
  795. goto end;
  796. if (kparam != NULL) {
  797. EVP_PKEY_CTX *pctx;
  798. pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
  799. if (!cms_set_pkey_param(pctx, kparam->param))
  800. goto end;
  801. }
  802. if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE
  803. && wrap_cipher) {
  804. EVP_CIPHER_CTX *wctx;
  805. wctx = CMS_RecipientInfo_kari_get0_ctx(ri);
  806. EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL);
  807. }
  808. }
  809. if (secret_key != NULL) {
  810. if (!CMS_add0_recipient_key(cms, NID_undef,
  811. secret_key, secret_keylen,
  812. secret_keyid, secret_keyidlen,
  813. NULL, NULL, NULL))
  814. goto end;
  815. /* NULL these because call absorbs them */
  816. secret_key = NULL;
  817. secret_keyid = NULL;
  818. }
  819. if (pwri_pass != NULL) {
  820. pwri_tmp = (unsigned char *)OPENSSL_strdup((char *)pwri_pass);
  821. if (pwri_tmp == NULL)
  822. goto end;
  823. if (CMS_add0_recipient_password(cms,
  824. -1, NID_undef, NID_undef,
  825. pwri_tmp, -1, NULL) == NULL)
  826. goto end;
  827. pwri_tmp = NULL;
  828. }
  829. if (!(flags & CMS_STREAM)) {
  830. if (!CMS_final(cms, in, NULL, flags))
  831. goto end;
  832. }
  833. } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
  834. cms = CMS_EncryptedData_encrypt(in, cipher,
  835. secret_key, secret_keylen, flags);
  836. } else if (operation == SMIME_SIGN_RECEIPT) {
  837. CMS_ContentInfo *srcms = NULL;
  838. STACK_OF(CMS_SignerInfo) *sis;
  839. CMS_SignerInfo *si;
  840. sis = CMS_get0_SignerInfos(cms);
  841. if (sis == NULL)
  842. goto end;
  843. si = sk_CMS_SignerInfo_value(sis, 0);
  844. srcms = CMS_sign_receipt(si, signer, key, other, flags);
  845. if (srcms == NULL)
  846. goto end;
  847. CMS_ContentInfo_free(cms);
  848. cms = srcms;
  849. } else if (operation & SMIME_SIGNERS) {
  850. int i;
  851. /*
  852. * If detached data content we enable streaming if S/MIME output
  853. * format.
  854. */
  855. if (operation == SMIME_SIGN) {
  856. if (flags & CMS_DETACHED) {
  857. if (outformat == FORMAT_SMIME)
  858. flags |= CMS_STREAM;
  859. }
  860. flags |= CMS_PARTIAL;
  861. cms = CMS_sign(NULL, NULL, other, in, flags);
  862. if (cms == NULL)
  863. goto end;
  864. if (econtent_type != NULL)
  865. CMS_set1_eContentType(cms, econtent_type);
  866. if (rr_to != NULL) {
  867. rr = make_receipt_request(rr_to, rr_allorfirst, rr_from);
  868. if (rr == NULL) {
  869. BIO_puts(bio_err,
  870. "Signed Receipt Request Creation Error\n");
  871. goto end;
  872. }
  873. }
  874. } else {
  875. flags |= CMS_REUSE_DIGEST;
  876. }
  877. for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
  878. CMS_SignerInfo *si;
  879. cms_key_param *kparam;
  880. int tflags = flags;
  881. signerfile = sk_OPENSSL_STRING_value(sksigners, i);
  882. keyfile = sk_OPENSSL_STRING_value(skkeys, i);
  883. signer = load_cert(signerfile, FORMAT_PEM, "signer certificate");
  884. if (signer == NULL) {
  885. ret = 2;
  886. goto end;
  887. }
  888. key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
  889. if (key == NULL) {
  890. ret = 2;
  891. goto end;
  892. }
  893. for (kparam = key_first; kparam; kparam = kparam->next) {
  894. if (kparam->idx == i) {
  895. tflags |= CMS_KEY_PARAM;
  896. break;
  897. }
  898. }
  899. si = CMS_add1_signer(cms, signer, key, sign_md, tflags);
  900. if (si == NULL)
  901. goto end;
  902. if (kparam != NULL) {
  903. EVP_PKEY_CTX *pctx;
  904. pctx = CMS_SignerInfo_get0_pkey_ctx(si);
  905. if (!cms_set_pkey_param(pctx, kparam->param))
  906. goto end;
  907. }
  908. if (rr != NULL && !CMS_add1_ReceiptRequest(si, rr))
  909. goto end;
  910. X509_free(signer);
  911. signer = NULL;
  912. EVP_PKEY_free(key);
  913. key = NULL;
  914. }
  915. /* If not streaming or resigning finalize structure */
  916. if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {
  917. if (!CMS_final(cms, in, NULL, flags))
  918. goto end;
  919. }
  920. }
  921. if (cms == NULL) {
  922. BIO_printf(bio_err, "Error creating CMS structure\n");
  923. goto end;
  924. }
  925. ret = 4;
  926. if (operation == SMIME_DECRYPT) {
  927. if (flags & CMS_DEBUG_DECRYPT)
  928. CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
  929. if (secret_key != NULL) {
  930. if (!CMS_decrypt_set1_key(cms,
  931. secret_key, secret_keylen,
  932. secret_keyid, secret_keyidlen)) {
  933. BIO_puts(bio_err, "Error decrypting CMS using secret key\n");
  934. goto end;
  935. }
  936. }
  937. if (key != NULL) {
  938. if (!CMS_decrypt_set1_pkey(cms, key, recip)) {
  939. BIO_puts(bio_err, "Error decrypting CMS using private key\n");
  940. goto end;
  941. }
  942. }
  943. if (pwri_pass != NULL) {
  944. if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
  945. BIO_puts(bio_err, "Error decrypting CMS using password\n");
  946. goto end;
  947. }
  948. }
  949. if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
  950. BIO_printf(bio_err, "Error decrypting CMS structure\n");
  951. goto end;
  952. }
  953. } else if (operation == SMIME_DATAOUT) {
  954. if (!CMS_data(cms, out, flags))
  955. goto end;
  956. } else if (operation == SMIME_UNCOMPRESS) {
  957. if (!CMS_uncompress(cms, indata, out, flags))
  958. goto end;
  959. } else if (operation == SMIME_DIGEST_VERIFY) {
  960. if (CMS_digest_verify(cms, indata, out, flags) > 0) {
  961. BIO_printf(bio_err, "Verification successful\n");
  962. } else {
  963. BIO_printf(bio_err, "Verification failure\n");
  964. goto end;
  965. }
  966. } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
  967. if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
  968. indata, out, flags))
  969. goto end;
  970. } else if (operation == SMIME_VERIFY) {
  971. if (CMS_verify(cms, other, store, indata, out, flags) > 0) {
  972. BIO_printf(bio_err, "Verification successful\n");
  973. } else {
  974. BIO_printf(bio_err, "Verification failure\n");
  975. if (verify_retcode)
  976. ret = verify_err + 32;
  977. goto end;
  978. }
  979. if (signerfile != NULL) {
  980. STACK_OF(X509) *signers;
  981. signers = CMS_get0_signers(cms);
  982. if (!save_certs(signerfile, signers)) {
  983. BIO_printf(bio_err,
  984. "Error writing signers to %s\n", signerfile);
  985. ret = 5;
  986. goto end;
  987. }
  988. sk_X509_free(signers);
  989. }
  990. if (rr_print)
  991. receipt_request_print(cms);
  992. } else if (operation == SMIME_VERIFY_RECEIPT) {
  993. if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0) {
  994. BIO_printf(bio_err, "Verification successful\n");
  995. } else {
  996. BIO_printf(bio_err, "Verification failure\n");
  997. goto end;
  998. }
  999. } else {
  1000. if (noout) {
  1001. if (print)
  1002. CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
  1003. } else if (outformat == FORMAT_SMIME) {
  1004. if (to)
  1005. BIO_printf(out, "To: %s%s", to, mime_eol);
  1006. if (from)
  1007. BIO_printf(out, "From: %s%s", from, mime_eol);
  1008. if (subject)
  1009. BIO_printf(out, "Subject: %s%s", subject, mime_eol);
  1010. if (operation == SMIME_RESIGN)
  1011. ret = SMIME_write_CMS(out, cms, indata, flags);
  1012. else
  1013. ret = SMIME_write_CMS(out, cms, in, flags);
  1014. } else if (outformat == FORMAT_PEM) {
  1015. ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
  1016. } else if (outformat == FORMAT_ASN1) {
  1017. ret = i2d_CMS_bio_stream(out, cms, in, flags);
  1018. } else {
  1019. BIO_printf(bio_err, "Bad output format for CMS file\n");
  1020. goto end;
  1021. }
  1022. if (ret <= 0) {
  1023. ret = 6;
  1024. goto end;
  1025. }
  1026. }
  1027. ret = 0;
  1028. end:
  1029. if (ret)
  1030. ERR_print_errors(bio_err);
  1031. sk_X509_pop_free(encerts, X509_free);
  1032. sk_X509_pop_free(other, X509_free);
  1033. X509_VERIFY_PARAM_free(vpm);
  1034. sk_OPENSSL_STRING_free(sksigners);
  1035. sk_OPENSSL_STRING_free(skkeys);
  1036. OPENSSL_free(secret_key);
  1037. OPENSSL_free(secret_keyid);
  1038. OPENSSL_free(pwri_tmp);
  1039. ASN1_OBJECT_free(econtent_type);
  1040. CMS_ReceiptRequest_free(rr);
  1041. sk_OPENSSL_STRING_free(rr_to);
  1042. sk_OPENSSL_STRING_free(rr_from);
  1043. for (key_param = key_first; key_param;) {
  1044. cms_key_param *tparam;
  1045. sk_OPENSSL_STRING_free(key_param->param);
  1046. tparam = key_param->next;
  1047. OPENSSL_free(key_param);
  1048. key_param = tparam;
  1049. }
  1050. X509_STORE_free(store);
  1051. X509_free(cert);
  1052. X509_free(recip);
  1053. X509_free(signer);
  1054. EVP_PKEY_free(key);
  1055. CMS_ContentInfo_free(cms);
  1056. CMS_ContentInfo_free(rcms);
  1057. release_engine(e);
  1058. BIO_free(rctin);
  1059. BIO_free(in);
  1060. BIO_free(indata);
  1061. BIO_free_all(out);
  1062. OPENSSL_free(passin);
  1063. return ret;
  1064. }
  1065. static int save_certs(char *signerfile, STACK_OF(X509) *signers)
  1066. {
  1067. int i;
  1068. BIO *tmp;
  1069. if (signerfile == NULL)
  1070. return 1;
  1071. tmp = BIO_new_file(signerfile, "w");
  1072. if (tmp == NULL)
  1073. return 0;
  1074. for (i = 0; i < sk_X509_num(signers); i++)
  1075. PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
  1076. BIO_free(tmp);
  1077. return 1;
  1078. }
  1079. /* Minimal callback just to output policy info (if any) */
  1080. static int cms_cb(int ok, X509_STORE_CTX *ctx)
  1081. {
  1082. int error;
  1083. error = X509_STORE_CTX_get_error(ctx);
  1084. verify_err = error;
  1085. if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
  1086. && ((error != X509_V_OK) || (ok != 2)))
  1087. return ok;
  1088. policies_print(ctx);
  1089. return ok;
  1090. }
  1091. static void gnames_stack_print(STACK_OF(GENERAL_NAMES) *gns)
  1092. {
  1093. STACK_OF(GENERAL_NAME) *gens;
  1094. GENERAL_NAME *gen;
  1095. int i, j;
  1096. for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
  1097. gens = sk_GENERAL_NAMES_value(gns, i);
  1098. for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
  1099. gen = sk_GENERAL_NAME_value(gens, j);
  1100. BIO_puts(bio_err, " ");
  1101. GENERAL_NAME_print(bio_err, gen);
  1102. BIO_puts(bio_err, "\n");
  1103. }
  1104. }
  1105. return;
  1106. }
  1107. static void receipt_request_print(CMS_ContentInfo *cms)
  1108. {
  1109. STACK_OF(CMS_SignerInfo) *sis;
  1110. CMS_SignerInfo *si;
  1111. CMS_ReceiptRequest *rr;
  1112. int allorfirst;
  1113. STACK_OF(GENERAL_NAMES) *rto, *rlist;
  1114. ASN1_STRING *scid;
  1115. int i, rv;
  1116. sis = CMS_get0_SignerInfos(cms);
  1117. for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
  1118. si = sk_CMS_SignerInfo_value(sis, i);
  1119. rv = CMS_get1_ReceiptRequest(si, &rr);
  1120. BIO_printf(bio_err, "Signer %d:\n", i + 1);
  1121. if (rv == 0) {
  1122. BIO_puts(bio_err, " No Receipt Request\n");
  1123. } else if (rv < 0) {
  1124. BIO_puts(bio_err, " Receipt Request Parse Error\n");
  1125. ERR_print_errors(bio_err);
  1126. } else {
  1127. const char *id;
  1128. int idlen;
  1129. CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
  1130. &rlist, &rto);
  1131. BIO_puts(bio_err, " Signed Content ID:\n");
  1132. idlen = ASN1_STRING_length(scid);
  1133. id = (const char *)ASN1_STRING_get0_data(scid);
  1134. BIO_dump_indent(bio_err, id, idlen, 4);
  1135. BIO_puts(bio_err, " Receipts From");
  1136. if (rlist != NULL) {
  1137. BIO_puts(bio_err, " List:\n");
  1138. gnames_stack_print(rlist);
  1139. } else if (allorfirst == 1) {
  1140. BIO_puts(bio_err, ": First Tier\n");
  1141. } else if (allorfirst == 0) {
  1142. BIO_puts(bio_err, ": All\n");
  1143. } else {
  1144. BIO_printf(bio_err, " Unknown (%d)\n", allorfirst);
  1145. }
  1146. BIO_puts(bio_err, " Receipts To:\n");
  1147. gnames_stack_print(rto);
  1148. }
  1149. CMS_ReceiptRequest_free(rr);
  1150. }
  1151. }
  1152. static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
  1153. {
  1154. int i;
  1155. STACK_OF(GENERAL_NAMES) *ret;
  1156. GENERAL_NAMES *gens = NULL;
  1157. GENERAL_NAME *gen = NULL;
  1158. ret = sk_GENERAL_NAMES_new_null();
  1159. if (ret == NULL)
  1160. goto err;
  1161. for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
  1162. char *str = sk_OPENSSL_STRING_value(ns, i);
  1163. gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
  1164. if (gen == NULL)
  1165. goto err;
  1166. gens = GENERAL_NAMES_new();
  1167. if (gens == NULL)
  1168. goto err;
  1169. if (!sk_GENERAL_NAME_push(gens, gen))
  1170. goto err;
  1171. gen = NULL;
  1172. if (!sk_GENERAL_NAMES_push(ret, gens))
  1173. goto err;
  1174. gens = NULL;
  1175. }
  1176. return ret;
  1177. err:
  1178. sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
  1179. GENERAL_NAMES_free(gens);
  1180. GENERAL_NAME_free(gen);
  1181. return NULL;
  1182. }
  1183. static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING)
  1184. *rr_to, int rr_allorfirst, STACK_OF(OPENSSL_STRING)
  1185. *rr_from)
  1186. {
  1187. STACK_OF(GENERAL_NAMES) *rct_to = NULL, *rct_from = NULL;
  1188. CMS_ReceiptRequest *rr;
  1189. rct_to = make_names_stack(rr_to);
  1190. if (rct_to == NULL)
  1191. goto err;
  1192. if (rr_from != NULL) {
  1193. rct_from = make_names_stack(rr_from);
  1194. if (rct_from == NULL)
  1195. goto err;
  1196. } else {
  1197. rct_from = NULL;
  1198. }
  1199. rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,
  1200. rct_to);
  1201. return rr;
  1202. err:
  1203. sk_GENERAL_NAMES_pop_free(rct_to, GENERAL_NAMES_free);
  1204. return NULL;
  1205. }
  1206. static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
  1207. STACK_OF(OPENSSL_STRING) *param)
  1208. {
  1209. char *keyopt;
  1210. int i;
  1211. if (sk_OPENSSL_STRING_num(param) <= 0)
  1212. return 1;
  1213. for (i = 0; i < sk_OPENSSL_STRING_num(param); i++) {
  1214. keyopt = sk_OPENSSL_STRING_value(param, i);
  1215. if (pkey_ctrl_string(pctx, keyopt) <= 0) {
  1216. BIO_printf(bio_err, "parameter error \"%s\"\n", keyopt);
  1217. ERR_print_errors(bio_err);
  1218. return 0;
  1219. }
  1220. }
  1221. return 1;
  1222. }
  1223. #endif