ts.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /*
  2. * Copyright 2006-2022 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. #include <openssl/opensslconf.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "apps.h"
  14. #include "progs.h"
  15. #include <openssl/bio.h>
  16. #include <openssl/err.h>
  17. #include <openssl/pem.h>
  18. #include <openssl/rand.h>
  19. #include <openssl/ts.h>
  20. #include <openssl/bn.h>
  21. /* Request nonce length, in bits (must be a multiple of 8). */
  22. #define NONCE_LENGTH 64
  23. /* Name of config entry that defines the OID file. */
  24. #define ENV_OID_FILE "oid_file"
  25. /* Is |EXACTLY_ONE| of three pointers set? */
  26. #define EXACTLY_ONE(a, b, c) \
  27. (( a && !b && !c) || \
  28. ( b && !a && !c) || \
  29. ( c && !a && !b))
  30. static ASN1_OBJECT *txt2obj(const char *oid);
  31. static CONF *load_config_file(const char *configfile);
  32. /* Query related functions. */
  33. static int query_command(const char *data, const char *digest,
  34. const EVP_MD *md, const char *policy, int no_nonce,
  35. int cert, const char *in, const char *out, int text);
  36. static TS_REQ *create_query(BIO *data_bio, const char *digest, const EVP_MD *md,
  37. const char *policy, int no_nonce, int cert);
  38. static int create_digest(BIO *input, const char *digest,
  39. const EVP_MD *md, unsigned char **md_value);
  40. static ASN1_INTEGER *create_nonce(int bits);
  41. /* Reply related functions. */
  42. static int reply_command(CONF *conf, const char *section, const char *engine,
  43. const char *queryfile, const char *passin, const char *inkey,
  44. const EVP_MD *md, const char *signer, const char *chain,
  45. const char *policy, const char *in, int token_in,
  46. const char *out, int token_out, int text);
  47. static TS_RESP *read_PKCS7(BIO *in_bio);
  48. static TS_RESP *create_response(CONF *conf, const char *section, const char *engine,
  49. const char *queryfile, const char *passin,
  50. const char *inkey, const EVP_MD *md, const char *signer,
  51. const char *chain, const char *policy);
  52. static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data);
  53. static ASN1_INTEGER *next_serial(const char *serialfile);
  54. static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial);
  55. /* Verify related functions. */
  56. static int verify_command(const char *data, const char *digest, const char *queryfile,
  57. const char *in, int token_in,
  58. const char *CApath, const char *CAfile,
  59. const char *CAstore,
  60. char *untrusted, X509_VERIFY_PARAM *vpm);
  61. static TS_VERIFY_CTX *create_verify_ctx(const char *data, const char *digest,
  62. const char *queryfile,
  63. const char *CApath, const char *CAfile,
  64. const char *CAstore,
  65. char *untrusted,
  66. X509_VERIFY_PARAM *vpm);
  67. static X509_STORE *create_cert_store(const char *CApath, const char *CAfile,
  68. const char *CAstore, X509_VERIFY_PARAM *vpm);
  69. static int verify_cb(int ok, X509_STORE_CTX *ctx);
  70. typedef enum OPTION_choice {
  71. OPT_COMMON,
  72. OPT_ENGINE, OPT_CONFIG, OPT_SECTION, OPT_QUERY, OPT_DATA,
  73. OPT_DIGEST, OPT_TSPOLICY, OPT_NO_NONCE, OPT_CERT,
  74. OPT_IN, OPT_TOKEN_IN, OPT_OUT, OPT_TOKEN_OUT, OPT_TEXT,
  75. OPT_REPLY, OPT_QUERYFILE, OPT_PASSIN, OPT_INKEY, OPT_SIGNER,
  76. OPT_CHAIN, OPT_VERIFY, OPT_CAPATH, OPT_CAFILE, OPT_CASTORE, OPT_UNTRUSTED,
  77. OPT_MD, OPT_V_ENUM, OPT_R_ENUM, OPT_PROV_ENUM
  78. } OPTION_CHOICE;
  79. const OPTIONS ts_options[] = {
  80. OPT_SECTION("General"),
  81. {"help", OPT_HELP, '-', "Display this summary"},
  82. {"config", OPT_CONFIG, '<', "Configuration file"},
  83. {"section", OPT_SECTION, 's', "Section to use within config file"},
  84. #ifndef OPENSSL_NO_ENGINE
  85. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  86. #endif
  87. {"inkey", OPT_INKEY, 's', "File with private key for reply"},
  88. {"signer", OPT_SIGNER, 's', "Signer certificate file"},
  89. {"chain", OPT_CHAIN, '<', "File with signer CA chain"},
  90. {"CAfile", OPT_CAFILE, '<', "File with trusted CA certs"},
  91. {"CApath", OPT_CAPATH, '/', "Path to trusted CA files"},
  92. {"CAstore", OPT_CASTORE, ':', "URI to trusted CA store"},
  93. {"untrusted", OPT_UNTRUSTED, '<', "Extra untrusted certs"},
  94. {"token_in", OPT_TOKEN_IN, '-', "Input is a PKCS#7 file"},
  95. {"token_out", OPT_TOKEN_OUT, '-', "Output is a PKCS#7 file"},
  96. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  97. {"", OPT_MD, '-', "Any supported digest"},
  98. OPT_SECTION("Query"),
  99. {"query", OPT_QUERY, '-', "Generate a TS query"},
  100. {"data", OPT_DATA, '<', "File to hash"},
  101. {"digest", OPT_DIGEST, 's', "Digest (as a hex string)"},
  102. {"queryfile", OPT_QUERYFILE, '<', "File containing a TS query"},
  103. {"cert", OPT_CERT, '-', "Put cert request into query"},
  104. {"in", OPT_IN, '<', "Input file"},
  105. OPT_SECTION("Verify"),
  106. {"verify", OPT_VERIFY, '-', "Verify a TS response"},
  107. {"reply", OPT_REPLY, '-', "Generate a TS reply"},
  108. {"tspolicy", OPT_TSPOLICY, 's', "Policy OID to use"},
  109. {"no_nonce", OPT_NO_NONCE, '-', "Do not include a nonce"},
  110. {"out", OPT_OUT, '>', "Output file"},
  111. {"text", OPT_TEXT, '-', "Output text (not DER)"},
  112. OPT_R_OPTIONS,
  113. OPT_V_OPTIONS,
  114. OPT_PROV_OPTIONS,
  115. {NULL}
  116. };
  117. /*
  118. * This command is so complex, special help is needed.
  119. */
  120. static char* opt_helplist[] = {
  121. "",
  122. "Typical uses:",
  123. " openssl ts -query [-rand file...] [-config file] [-data file]",
  124. " [-digest hexstring] [-tspolicy oid] [-no_nonce] [-cert]",
  125. " [-in file] [-out file] [-text]",
  126. "",
  127. " openssl ts -reply [-config file] [-section tsa_section]",
  128. " [-queryfile file] [-passin password]",
  129. " [-signer tsa_cert.pem] [-inkey private_key.pem]",
  130. " [-chain certs_file.pem] [-tspolicy oid]",
  131. " [-in file] [-token_in] [-out file] [-token_out]",
  132. #ifndef OPENSSL_NO_ENGINE
  133. " [-text] [-engine id]",
  134. #else
  135. " [-text]",
  136. #endif
  137. "",
  138. " openssl ts -verify -CApath dir -CAfile root-cert.pem -CAstore uri",
  139. " -untrusted extra-certs.pem [-data file] [-digest hexstring]",
  140. " [-queryfile request.tsq] -in response.tsr [-token_in] ...",
  141. NULL,
  142. };
  143. int ts_main(int argc, char **argv)
  144. {
  145. CONF *conf = NULL;
  146. const char *CAfile = NULL, *prog;
  147. char *untrusted = NULL;
  148. const char *configfile = default_config_file, *engine = NULL;
  149. const char *section = NULL, *digestname = NULL;
  150. char **helpp;
  151. char *password = NULL;
  152. char *data = NULL, *digest = NULL, *policy = NULL;
  153. char *in = NULL, *out = NULL, *queryfile = NULL, *passin = NULL;
  154. char *inkey = NULL, *signer = NULL, *chain = NULL, *CApath = NULL;
  155. char *CAstore = NULL;
  156. EVP_MD *md = NULL;
  157. OPTION_CHOICE o, mode = OPT_ERR;
  158. int ret = 1, no_nonce = 0, cert = 0, text = 0;
  159. int vpmtouched = 0;
  160. X509_VERIFY_PARAM *vpm = NULL;
  161. /* Input is ContentInfo instead of TimeStampResp. */
  162. int token_in = 0;
  163. /* Output is ContentInfo instead of TimeStampResp. */
  164. int token_out = 0;
  165. if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
  166. goto end;
  167. opt_set_unknown_name("digest");
  168. prog = opt_init(argc, argv, ts_options);
  169. while ((o = opt_next()) != OPT_EOF) {
  170. switch (o) {
  171. case OPT_EOF:
  172. case OPT_ERR:
  173. opthelp:
  174. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  175. goto end;
  176. case OPT_HELP:
  177. opt_help(ts_options);
  178. for (helpp = opt_helplist; *helpp; ++helpp)
  179. BIO_printf(bio_err, "%s\n", *helpp);
  180. ret = 0;
  181. goto end;
  182. case OPT_CONFIG:
  183. configfile = opt_arg();
  184. break;
  185. case OPT_SECTION:
  186. section = opt_arg();
  187. break;
  188. case OPT_QUERY:
  189. case OPT_REPLY:
  190. case OPT_VERIFY:
  191. if (mode != OPT_ERR) {
  192. BIO_printf(bio_err, "%s: Must give only one of -query, -reply, or -verify\n", prog);
  193. goto opthelp;
  194. }
  195. mode = o;
  196. break;
  197. case OPT_DATA:
  198. data = opt_arg();
  199. break;
  200. case OPT_DIGEST:
  201. digest = opt_arg();
  202. break;
  203. case OPT_R_CASES:
  204. if (!opt_rand(o))
  205. goto end;
  206. break;
  207. case OPT_PROV_CASES:
  208. if (!opt_provider(o))
  209. goto end;
  210. break;
  211. case OPT_TSPOLICY:
  212. policy = opt_arg();
  213. break;
  214. case OPT_NO_NONCE:
  215. no_nonce = 1;
  216. break;
  217. case OPT_CERT:
  218. cert = 1;
  219. break;
  220. case OPT_IN:
  221. in = opt_arg();
  222. break;
  223. case OPT_TOKEN_IN:
  224. token_in = 1;
  225. break;
  226. case OPT_OUT:
  227. out = opt_arg();
  228. break;
  229. case OPT_TOKEN_OUT:
  230. token_out = 1;
  231. break;
  232. case OPT_TEXT:
  233. text = 1;
  234. break;
  235. case OPT_QUERYFILE:
  236. queryfile = opt_arg();
  237. break;
  238. case OPT_PASSIN:
  239. passin = opt_arg();
  240. break;
  241. case OPT_INKEY:
  242. inkey = opt_arg();
  243. break;
  244. case OPT_SIGNER:
  245. signer = opt_arg();
  246. break;
  247. case OPT_CHAIN:
  248. chain = opt_arg();
  249. break;
  250. case OPT_CAPATH:
  251. CApath = opt_arg();
  252. break;
  253. case OPT_CAFILE:
  254. CAfile = opt_arg();
  255. break;
  256. case OPT_CASTORE:
  257. CAstore = opt_arg();
  258. break;
  259. case OPT_UNTRUSTED:
  260. untrusted = opt_arg();
  261. break;
  262. case OPT_ENGINE:
  263. engine = opt_arg();
  264. break;
  265. case OPT_MD:
  266. digestname = opt_unknown();
  267. break;
  268. case OPT_V_CASES:
  269. if (!opt_verify(o, vpm))
  270. goto end;
  271. vpmtouched++;
  272. break;
  273. }
  274. }
  275. /* No extra arguments. */
  276. if (!opt_check_rest_arg(NULL))
  277. goto opthelp;
  278. if (mode == OPT_ERR) {
  279. BIO_printf(bio_err, "%s: Must give one of -query, -reply, or -verify\n", prog);
  280. goto opthelp;
  281. }
  282. if (!app_RAND_load())
  283. goto end;
  284. if (!opt_md(digestname, &md))
  285. goto opthelp;
  286. if (mode == OPT_REPLY && passin &&
  287. !app_passwd(passin, NULL, &password, NULL)) {
  288. BIO_printf(bio_err, "Error getting password.\n");
  289. goto end;
  290. }
  291. if ((conf = load_config_file(configfile)) == NULL)
  292. goto end;
  293. if (configfile != default_config_file && !app_load_modules(conf))
  294. goto end;
  295. /* Check parameter consistency and execute the appropriate function. */
  296. if (mode == OPT_QUERY) {
  297. if (vpmtouched)
  298. goto opthelp;
  299. if ((data != NULL) && (digest != NULL))
  300. goto opthelp;
  301. ret = !query_command(data, digest, md, policy, no_nonce, cert,
  302. in, out, text);
  303. } else if (mode == OPT_REPLY) {
  304. if (vpmtouched)
  305. goto opthelp;
  306. if ((in != NULL) && (queryfile != NULL))
  307. goto opthelp;
  308. if (in == NULL) {
  309. if ((conf == NULL) || (token_in != 0))
  310. goto opthelp;
  311. }
  312. ret = !reply_command(conf, section, engine, queryfile,
  313. password, inkey, md, signer, chain, policy,
  314. in, token_in, out, token_out, text);
  315. } else if (mode == OPT_VERIFY) {
  316. if ((in == NULL) || !EXACTLY_ONE(queryfile, data, digest))
  317. goto opthelp;
  318. ret = !verify_command(data, digest, queryfile, in, token_in,
  319. CApath, CAfile, CAstore, untrusted,
  320. vpmtouched ? vpm : NULL);
  321. } else {
  322. goto opthelp;
  323. }
  324. end:
  325. X509_VERIFY_PARAM_free(vpm);
  326. EVP_MD_free(md);
  327. NCONF_free(conf);
  328. OPENSSL_free(password);
  329. return ret;
  330. }
  331. /*
  332. * Configuration file-related function definitions.
  333. */
  334. static ASN1_OBJECT *txt2obj(const char *oid)
  335. {
  336. ASN1_OBJECT *oid_obj = NULL;
  337. if ((oid_obj = OBJ_txt2obj(oid, 0)) == NULL)
  338. BIO_printf(bio_err, "cannot convert %s to OID\n", oid);
  339. return oid_obj;
  340. }
  341. static CONF *load_config_file(const char *configfile)
  342. {
  343. CONF *conf = app_load_config(configfile);
  344. if (conf != NULL) {
  345. const char *p;
  346. BIO_printf(bio_err, "Using configuration from %s\n", configfile);
  347. p = NCONF_get_string(conf, NULL, ENV_OID_FILE);
  348. if (p != NULL) {
  349. BIO *oid_bio = BIO_new_file(p, "r");
  350. if (!oid_bio)
  351. ERR_print_errors(bio_err);
  352. else {
  353. OBJ_create_objects(oid_bio);
  354. BIO_free_all(oid_bio);
  355. }
  356. } else
  357. ERR_clear_error();
  358. if (!add_oid_section(conf))
  359. ERR_print_errors(bio_err);
  360. }
  361. return conf;
  362. }
  363. /*
  364. * Query-related method definitions.
  365. */
  366. static int query_command(const char *data, const char *digest, const EVP_MD *md,
  367. const char *policy, int no_nonce,
  368. int cert, const char *in, const char *out, int text)
  369. {
  370. int ret = 0;
  371. TS_REQ *query = NULL;
  372. BIO *in_bio = NULL;
  373. BIO *data_bio = NULL;
  374. BIO *out_bio = NULL;
  375. /* Build query object. */
  376. if (in != NULL) {
  377. if ((in_bio = bio_open_default(in, 'r', FORMAT_ASN1)) == NULL)
  378. goto end;
  379. query = d2i_TS_REQ_bio(in_bio, NULL);
  380. } else {
  381. if (digest == NULL
  382. && (data_bio = bio_open_default(data, 'r', FORMAT_ASN1)) == NULL)
  383. goto end;
  384. query = create_query(data_bio, digest, md, policy, no_nonce, cert);
  385. }
  386. if (query == NULL)
  387. goto end;
  388. if (text) {
  389. if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
  390. goto end;
  391. if (!TS_REQ_print_bio(out_bio, query))
  392. goto end;
  393. } else {
  394. if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
  395. goto end;
  396. if (!i2d_TS_REQ_bio(out_bio, query))
  397. goto end;
  398. }
  399. ret = 1;
  400. end:
  401. ERR_print_errors(bio_err);
  402. BIO_free_all(in_bio);
  403. BIO_free_all(data_bio);
  404. BIO_free_all(out_bio);
  405. TS_REQ_free(query);
  406. return ret;
  407. }
  408. static TS_REQ *create_query(BIO *data_bio, const char *digest, const EVP_MD *md,
  409. const char *policy, int no_nonce, int cert)
  410. {
  411. int ret = 0;
  412. TS_REQ *ts_req = NULL;
  413. int len;
  414. TS_MSG_IMPRINT *msg_imprint = NULL;
  415. X509_ALGOR *algo = NULL;
  416. unsigned char *data = NULL;
  417. ASN1_OBJECT *policy_obj = NULL;
  418. ASN1_INTEGER *nonce_asn1 = NULL;
  419. if (md == NULL && (md = EVP_get_digestbyname("sha256")) == NULL)
  420. goto err;
  421. if ((ts_req = TS_REQ_new()) == NULL)
  422. goto err;
  423. if (!TS_REQ_set_version(ts_req, 1))
  424. goto err;
  425. if ((msg_imprint = TS_MSG_IMPRINT_new()) == NULL)
  426. goto err;
  427. if ((algo = X509_ALGOR_new()) == NULL)
  428. goto err;
  429. if ((algo->algorithm = OBJ_nid2obj(EVP_MD_get_type(md))) == NULL)
  430. goto err;
  431. if ((algo->parameter = ASN1_TYPE_new()) == NULL)
  432. goto err;
  433. algo->parameter->type = V_ASN1_NULL;
  434. if (!TS_MSG_IMPRINT_set_algo(msg_imprint, algo))
  435. goto err;
  436. if ((len = create_digest(data_bio, digest, md, &data)) == 0)
  437. goto err;
  438. if (!TS_MSG_IMPRINT_set_msg(msg_imprint, data, len))
  439. goto err;
  440. if (!TS_REQ_set_msg_imprint(ts_req, msg_imprint))
  441. goto err;
  442. if (policy && (policy_obj = txt2obj(policy)) == NULL)
  443. goto err;
  444. if (policy_obj && !TS_REQ_set_policy_id(ts_req, policy_obj))
  445. goto err;
  446. /* Setting nonce if requested. */
  447. if (!no_nonce && (nonce_asn1 = create_nonce(NONCE_LENGTH)) == NULL)
  448. goto err;
  449. if (nonce_asn1 && !TS_REQ_set_nonce(ts_req, nonce_asn1))
  450. goto err;
  451. if (!TS_REQ_set_cert_req(ts_req, cert))
  452. goto err;
  453. ret = 1;
  454. err:
  455. if (!ret) {
  456. TS_REQ_free(ts_req);
  457. ts_req = NULL;
  458. BIO_printf(bio_err, "could not create query\n");
  459. ERR_print_errors(bio_err);
  460. }
  461. TS_MSG_IMPRINT_free(msg_imprint);
  462. X509_ALGOR_free(algo);
  463. OPENSSL_free(data);
  464. ASN1_OBJECT_free(policy_obj);
  465. ASN1_INTEGER_free(nonce_asn1);
  466. return ts_req;
  467. }
  468. static int create_digest(BIO *input, const char *digest, const EVP_MD *md,
  469. unsigned char **md_value)
  470. {
  471. int md_value_len;
  472. int rv = 0;
  473. EVP_MD_CTX *md_ctx = NULL;
  474. md_value_len = EVP_MD_get_size(md);
  475. if (md_value_len < 0)
  476. return 0;
  477. if (input != NULL) {
  478. unsigned char buffer[4096];
  479. int length;
  480. md_ctx = EVP_MD_CTX_new();
  481. if (md_ctx == NULL)
  482. return 0;
  483. *md_value = app_malloc(md_value_len, "digest buffer");
  484. if (!EVP_DigestInit(md_ctx, md))
  485. goto err;
  486. while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) {
  487. if (!EVP_DigestUpdate(md_ctx, buffer, length))
  488. goto err;
  489. }
  490. if (!EVP_DigestFinal(md_ctx, *md_value, NULL))
  491. goto err;
  492. md_value_len = EVP_MD_get_size(md);
  493. } else {
  494. long digest_len;
  495. *md_value = OPENSSL_hexstr2buf(digest, &digest_len);
  496. if (*md_value == NULL || md_value_len != digest_len) {
  497. OPENSSL_free(*md_value);
  498. *md_value = NULL;
  499. BIO_printf(bio_err, "bad digest, %d bytes "
  500. "must be specified\n", md_value_len);
  501. return 0;
  502. }
  503. }
  504. rv = md_value_len;
  505. err:
  506. EVP_MD_CTX_free(md_ctx);
  507. return rv;
  508. }
  509. static ASN1_INTEGER *create_nonce(int bits)
  510. {
  511. unsigned char buf[20];
  512. ASN1_INTEGER *nonce = NULL;
  513. int len = (bits - 1) / 8 + 1;
  514. int i;
  515. if (len > (int)sizeof(buf))
  516. goto err;
  517. if (RAND_bytes(buf, len) <= 0)
  518. goto err;
  519. /* Find the first non-zero byte and creating ASN1_INTEGER object. */
  520. for (i = 0; i < len && !buf[i]; ++i)
  521. continue;
  522. if ((nonce = ASN1_INTEGER_new()) == NULL)
  523. goto err;
  524. OPENSSL_free(nonce->data);
  525. nonce->length = len - i;
  526. nonce->data = app_malloc(nonce->length + 1, "nonce buffer");
  527. memcpy(nonce->data, buf + i, nonce->length);
  528. return nonce;
  529. err:
  530. BIO_printf(bio_err, "could not create nonce\n");
  531. ASN1_INTEGER_free(nonce);
  532. return NULL;
  533. }
  534. /*
  535. * Reply-related method definitions.
  536. */
  537. static int reply_command(CONF *conf, const char *section, const char *engine,
  538. const char *queryfile, const char *passin, const char *inkey,
  539. const EVP_MD *md, const char *signer, const char *chain,
  540. const char *policy, const char *in, int token_in,
  541. const char *out, int token_out, int text)
  542. {
  543. int ret = 0;
  544. TS_RESP *response = NULL;
  545. BIO *in_bio = NULL;
  546. BIO *query_bio = NULL;
  547. BIO *inkey_bio = NULL;
  548. BIO *signer_bio = NULL;
  549. BIO *out_bio = NULL;
  550. if (in != NULL) {
  551. if ((in_bio = BIO_new_file(in, "rb")) == NULL)
  552. goto end;
  553. if (token_in) {
  554. response = read_PKCS7(in_bio);
  555. } else {
  556. response = d2i_TS_RESP_bio(in_bio, NULL);
  557. }
  558. } else {
  559. response = create_response(conf, section, engine, queryfile,
  560. passin, inkey, md, signer, chain, policy);
  561. if (response != NULL)
  562. BIO_printf(bio_err, "Response has been generated.\n");
  563. else
  564. BIO_printf(bio_err, "Response is not generated.\n");
  565. }
  566. if (response == NULL)
  567. goto end;
  568. /* Write response. */
  569. if (text) {
  570. if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
  571. goto end;
  572. if (token_out) {
  573. TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
  574. if (!TS_TST_INFO_print_bio(out_bio, tst_info))
  575. goto end;
  576. } else {
  577. if (!TS_RESP_print_bio(out_bio, response))
  578. goto end;
  579. }
  580. } else {
  581. if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
  582. goto end;
  583. if (token_out) {
  584. PKCS7 *token = TS_RESP_get_token(response);
  585. if (!i2d_PKCS7_bio(out_bio, token))
  586. goto end;
  587. } else {
  588. if (!i2d_TS_RESP_bio(out_bio, response))
  589. goto end;
  590. }
  591. }
  592. ret = 1;
  593. end:
  594. ERR_print_errors(bio_err);
  595. BIO_free_all(in_bio);
  596. BIO_free_all(query_bio);
  597. BIO_free_all(inkey_bio);
  598. BIO_free_all(signer_bio);
  599. BIO_free_all(out_bio);
  600. TS_RESP_free(response);
  601. return ret;
  602. }
  603. /* Reads a PKCS7 token and adds default 'granted' status info to it. */
  604. static TS_RESP *read_PKCS7(BIO *in_bio)
  605. {
  606. int ret = 0;
  607. PKCS7 *token = NULL;
  608. TS_TST_INFO *tst_info = NULL;
  609. TS_RESP *resp = NULL;
  610. TS_STATUS_INFO *si = NULL;
  611. if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
  612. goto end;
  613. if ((tst_info = PKCS7_to_TS_TST_INFO(token)) == NULL)
  614. goto end;
  615. if ((resp = TS_RESP_new()) == NULL)
  616. goto end;
  617. if ((si = TS_STATUS_INFO_new()) == NULL)
  618. goto end;
  619. if (!TS_STATUS_INFO_set_status(si, TS_STATUS_GRANTED))
  620. goto end;
  621. if (!TS_RESP_set_status_info(resp, si))
  622. goto end;
  623. TS_RESP_set_tst_info(resp, token, tst_info);
  624. token = NULL; /* Ownership is lost. */
  625. tst_info = NULL; /* Ownership is lost. */
  626. ret = 1;
  627. end:
  628. PKCS7_free(token);
  629. TS_TST_INFO_free(tst_info);
  630. if (!ret) {
  631. TS_RESP_free(resp);
  632. resp = NULL;
  633. }
  634. TS_STATUS_INFO_free(si);
  635. return resp;
  636. }
  637. static TS_RESP *create_response(CONF *conf, const char *section, const char *engine,
  638. const char *queryfile, const char *passin,
  639. const char *inkey, const EVP_MD *md, const char *signer,
  640. const char *chain, const char *policy)
  641. {
  642. int ret = 0;
  643. TS_RESP *response = NULL;
  644. BIO *query_bio = NULL;
  645. TS_RESP_CTX *resp_ctx = NULL;
  646. if ((query_bio = BIO_new_file(queryfile, "rb")) == NULL)
  647. goto end;
  648. if ((section = TS_CONF_get_tsa_section(conf, section)) == NULL)
  649. goto end;
  650. if ((resp_ctx = TS_RESP_CTX_new()) == NULL)
  651. goto end;
  652. if (!TS_CONF_set_serial(conf, section, serial_cb, resp_ctx))
  653. goto end;
  654. #ifndef OPENSSL_NO_ENGINE
  655. if (!TS_CONF_set_crypto_device(conf, section, engine))
  656. goto end;
  657. #endif
  658. if (!TS_CONF_set_signer_cert(conf, section, signer, resp_ctx))
  659. goto end;
  660. if (!TS_CONF_set_certs(conf, section, chain, resp_ctx))
  661. goto end;
  662. if (!TS_CONF_set_signer_key(conf, section, inkey, passin, resp_ctx))
  663. goto end;
  664. if (md) {
  665. if (!TS_RESP_CTX_set_signer_digest(resp_ctx, md))
  666. goto end;
  667. } else if (!TS_CONF_set_signer_digest(conf, section, NULL, resp_ctx)) {
  668. goto end;
  669. }
  670. if (!TS_CONF_set_ess_cert_id_digest(conf, section, resp_ctx))
  671. goto end;
  672. if (!TS_CONF_set_def_policy(conf, section, policy, resp_ctx))
  673. goto end;
  674. if (!TS_CONF_set_policies(conf, section, resp_ctx))
  675. goto end;
  676. if (!TS_CONF_set_digests(conf, section, resp_ctx))
  677. goto end;
  678. if (!TS_CONF_set_accuracy(conf, section, resp_ctx))
  679. goto end;
  680. if (!TS_CONF_set_clock_precision_digits(conf, section, resp_ctx))
  681. goto end;
  682. if (!TS_CONF_set_ordering(conf, section, resp_ctx))
  683. goto end;
  684. if (!TS_CONF_set_tsa_name(conf, section, resp_ctx))
  685. goto end;
  686. if (!TS_CONF_set_ess_cert_id_chain(conf, section, resp_ctx))
  687. goto end;
  688. if ((response = TS_RESP_create_response(resp_ctx, query_bio)) == NULL)
  689. goto end;
  690. ret = 1;
  691. end:
  692. if (!ret) {
  693. TS_RESP_free(response);
  694. response = NULL;
  695. }
  696. TS_RESP_CTX_free(resp_ctx);
  697. BIO_free_all(query_bio);
  698. return response;
  699. }
  700. static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data)
  701. {
  702. const char *serial_file = (const char *)data;
  703. ASN1_INTEGER *serial = next_serial(serial_file);
  704. if (serial == NULL) {
  705. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  706. "Error during serial number "
  707. "generation.");
  708. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_ADD_INFO_NOT_AVAILABLE);
  709. } else {
  710. save_ts_serial(serial_file, serial);
  711. }
  712. return serial;
  713. }
  714. static ASN1_INTEGER *next_serial(const char *serialfile)
  715. {
  716. int ret = 0;
  717. BIO *in = NULL;
  718. ASN1_INTEGER *serial = NULL;
  719. BIGNUM *bn = NULL;
  720. if ((serial = ASN1_INTEGER_new()) == NULL)
  721. goto err;
  722. if ((in = BIO_new_file(serialfile, "r")) == NULL) {
  723. ERR_clear_error();
  724. BIO_printf(bio_err, "Warning: could not open file %s for "
  725. "reading, using serial number: 1\n", serialfile);
  726. if (!ASN1_INTEGER_set(serial, 1))
  727. goto err;
  728. } else {
  729. char buf[1024];
  730. if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {
  731. BIO_printf(bio_err, "unable to load number from %s\n",
  732. serialfile);
  733. goto err;
  734. }
  735. if ((bn = ASN1_INTEGER_to_BN(serial, NULL)) == NULL)
  736. goto err;
  737. ASN1_INTEGER_free(serial);
  738. serial = NULL;
  739. if (!BN_add_word(bn, 1))
  740. goto err;
  741. if ((serial = BN_to_ASN1_INTEGER(bn, NULL)) == NULL)
  742. goto err;
  743. }
  744. ret = 1;
  745. err:
  746. if (!ret) {
  747. ASN1_INTEGER_free(serial);
  748. serial = NULL;
  749. }
  750. BIO_free_all(in);
  751. BN_free(bn);
  752. return serial;
  753. }
  754. static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial)
  755. {
  756. int ret = 0;
  757. BIO *out = NULL;
  758. if ((out = BIO_new_file(serialfile, "w")) == NULL)
  759. goto err;
  760. if (i2a_ASN1_INTEGER(out, serial) <= 0)
  761. goto err;
  762. if (BIO_puts(out, "\n") <= 0)
  763. goto err;
  764. ret = 1;
  765. err:
  766. if (!ret)
  767. BIO_printf(bio_err, "could not save serial number to %s\n",
  768. serialfile);
  769. BIO_free_all(out);
  770. return ret;
  771. }
  772. /*
  773. * Verify-related method definitions.
  774. */
  775. static int verify_command(const char *data, const char *digest, const char *queryfile,
  776. const char *in, int token_in,
  777. const char *CApath, const char *CAfile,
  778. const char *CAstore, char *untrusted,
  779. X509_VERIFY_PARAM *vpm)
  780. {
  781. BIO *in_bio = NULL;
  782. PKCS7 *token = NULL;
  783. TS_RESP *response = NULL;
  784. TS_VERIFY_CTX *verify_ctx = NULL;
  785. int ret = 0;
  786. if ((in_bio = BIO_new_file(in, "rb")) == NULL)
  787. goto end;
  788. if (token_in) {
  789. if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
  790. goto end;
  791. } else {
  792. if ((response = d2i_TS_RESP_bio(in_bio, NULL)) == NULL)
  793. goto end;
  794. }
  795. if ((verify_ctx = create_verify_ctx(data, digest, queryfile,
  796. CApath, CAfile, CAstore, untrusted,
  797. vpm)) == NULL)
  798. goto end;
  799. ret = token_in
  800. ? TS_RESP_verify_token(verify_ctx, token)
  801. : TS_RESP_verify_response(verify_ctx, response);
  802. end:
  803. printf("Verification: ");
  804. if (ret)
  805. printf("OK\n");
  806. else {
  807. printf("FAILED\n");
  808. ERR_print_errors(bio_err);
  809. }
  810. BIO_free_all(in_bio);
  811. PKCS7_free(token);
  812. TS_RESP_free(response);
  813. TS_VERIFY_CTX_free(verify_ctx);
  814. return ret;
  815. }
  816. static TS_VERIFY_CTX *create_verify_ctx(const char *data, const char *digest,
  817. const char *queryfile,
  818. const char *CApath, const char *CAfile,
  819. const char *CAstore,
  820. char *untrusted,
  821. X509_VERIFY_PARAM *vpm)
  822. {
  823. TS_VERIFY_CTX *ctx = NULL;
  824. STACK_OF(X509) *certs;
  825. BIO *input = NULL;
  826. TS_REQ *request = NULL;
  827. int ret = 0;
  828. int f = 0;
  829. if (data != NULL || digest != NULL) {
  830. if ((ctx = TS_VERIFY_CTX_new()) == NULL)
  831. goto err;
  832. f = TS_VFY_VERSION | TS_VFY_SIGNER;
  833. if (data != NULL) {
  834. BIO *out = NULL;
  835. f |= TS_VFY_DATA;
  836. if ((out = BIO_new_file(data, "rb")) == NULL)
  837. goto err;
  838. if (TS_VERIFY_CTX_set_data(ctx, out) == NULL) {
  839. BIO_free_all(out);
  840. goto err;
  841. }
  842. } else if (digest != NULL) {
  843. long imprint_len;
  844. unsigned char *hexstr = OPENSSL_hexstr2buf(digest, &imprint_len);
  845. f |= TS_VFY_IMPRINT;
  846. if (TS_VERIFY_CTX_set_imprint(ctx, hexstr, imprint_len) == NULL) {
  847. BIO_printf(bio_err, "invalid digest string\n");
  848. goto err;
  849. }
  850. }
  851. } else if (queryfile != NULL) {
  852. if ((input = BIO_new_file(queryfile, "rb")) == NULL)
  853. goto err;
  854. if ((request = d2i_TS_REQ_bio(input, NULL)) == NULL)
  855. goto err;
  856. if ((ctx = TS_REQ_to_TS_VERIFY_CTX(request, NULL)) == NULL)
  857. goto err;
  858. } else {
  859. return NULL;
  860. }
  861. /* Add the signature verification flag and arguments. */
  862. TS_VERIFY_CTX_add_flags(ctx, f | TS_VFY_SIGNATURE);
  863. /* Initialising the X509_STORE object. */
  864. if (TS_VERIFY_CTX_set_store(ctx,
  865. create_cert_store(CApath, CAfile, CAstore, vpm))
  866. == NULL)
  867. goto err;
  868. /* Loading any extra untrusted certificates. */
  869. if (untrusted != NULL) {
  870. certs = load_certs_multifile(untrusted, NULL, "extra untrusted certs",
  871. vpm);
  872. if (certs == NULL || TS_VERIFY_CTX_set_certs(ctx, certs) == NULL)
  873. goto err;
  874. }
  875. ret = 1;
  876. err:
  877. if (!ret) {
  878. TS_VERIFY_CTX_free(ctx);
  879. ctx = NULL;
  880. }
  881. BIO_free_all(input);
  882. TS_REQ_free(request);
  883. return ctx;
  884. }
  885. static X509_STORE *create_cert_store(const char *CApath, const char *CAfile,
  886. const char *CAstore, X509_VERIFY_PARAM *vpm)
  887. {
  888. X509_STORE *cert_ctx = NULL;
  889. X509_LOOKUP *lookup = NULL;
  890. OSSL_LIB_CTX *libctx = app_get0_libctx();
  891. const char *propq = app_get0_propq();
  892. cert_ctx = X509_STORE_new();
  893. if (cert_ctx == NULL) {
  894. BIO_printf(bio_err, "memory allocation failure\n");
  895. return NULL;
  896. }
  897. X509_STORE_set_verify_cb(cert_ctx, verify_cb);
  898. if (CApath != NULL) {
  899. lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir());
  900. if (lookup == NULL) {
  901. BIO_printf(bio_err, "memory allocation failure\n");
  902. goto err;
  903. }
  904. if (X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM) <= 0) {
  905. BIO_printf(bio_err, "Error loading directory %s\n", CApath);
  906. goto err;
  907. }
  908. }
  909. if (CAfile != NULL) {
  910. lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
  911. if (lookup == NULL) {
  912. BIO_printf(bio_err, "memory allocation failure\n");
  913. goto err;
  914. }
  915. if (X509_LOOKUP_load_file_ex(lookup, CAfile, X509_FILETYPE_PEM, libctx,
  916. propq) <= 0) {
  917. BIO_printf(bio_err, "Error loading file %s\n", CAfile);
  918. goto err;
  919. }
  920. }
  921. if (CAstore != NULL) {
  922. lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_store());
  923. if (lookup == NULL) {
  924. BIO_printf(bio_err, "memory allocation failure\n");
  925. goto err;
  926. }
  927. if (X509_LOOKUP_load_store_ex(lookup, CAstore, libctx, propq) <= 0) {
  928. BIO_printf(bio_err, "Error loading store URI %s\n", CAstore);
  929. goto err;
  930. }
  931. }
  932. if (vpm != NULL)
  933. X509_STORE_set1_param(cert_ctx, vpm);
  934. return cert_ctx;
  935. err:
  936. X509_STORE_free(cert_ctx);
  937. return NULL;
  938. }
  939. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  940. {
  941. return ok;
  942. }