cms.c 44 KB

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