ts.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*
  2. * Copyright 2006-2023 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 = app_conf_try_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. }
  357. if (!add_oid_section(conf))
  358. ERR_print_errors(bio_err);
  359. }
  360. return conf;
  361. }
  362. /*
  363. * Query-related method definitions.
  364. */
  365. static int query_command(const char *data, const char *digest, const EVP_MD *md,
  366. const char *policy, int no_nonce,
  367. int cert, const char *in, const char *out, int text)
  368. {
  369. int ret = 0;
  370. TS_REQ *query = NULL;
  371. BIO *in_bio = NULL;
  372. BIO *data_bio = NULL;
  373. BIO *out_bio = NULL;
  374. /* Build query object. */
  375. if (in != NULL) {
  376. if ((in_bio = bio_open_default(in, 'r', FORMAT_ASN1)) == NULL)
  377. goto end;
  378. query = d2i_TS_REQ_bio(in_bio, NULL);
  379. } else {
  380. if (digest == NULL
  381. && (data_bio = bio_open_default(data, 'r', FORMAT_ASN1)) == NULL)
  382. goto end;
  383. query = create_query(data_bio, digest, md, policy, no_nonce, cert);
  384. }
  385. if (query == NULL)
  386. goto end;
  387. if (text) {
  388. if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
  389. goto end;
  390. if (!TS_REQ_print_bio(out_bio, query))
  391. goto end;
  392. } else {
  393. if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
  394. goto end;
  395. if (!i2d_TS_REQ_bio(out_bio, query))
  396. goto end;
  397. }
  398. ret = 1;
  399. end:
  400. ERR_print_errors(bio_err);
  401. BIO_free_all(in_bio);
  402. BIO_free_all(data_bio);
  403. BIO_free_all(out_bio);
  404. TS_REQ_free(query);
  405. return ret;
  406. }
  407. static TS_REQ *create_query(BIO *data_bio, const char *digest, const EVP_MD *md,
  408. const char *policy, int no_nonce, int cert)
  409. {
  410. int ret = 0;
  411. TS_REQ *ts_req = NULL;
  412. int len;
  413. TS_MSG_IMPRINT *msg_imprint = NULL;
  414. X509_ALGOR *algo = NULL;
  415. unsigned char *data = NULL;
  416. ASN1_OBJECT *policy_obj = NULL;
  417. ASN1_INTEGER *nonce_asn1 = NULL;
  418. if (md == NULL && (md = EVP_get_digestbyname("sha256")) == NULL)
  419. goto err;
  420. if ((ts_req = TS_REQ_new()) == NULL)
  421. goto err;
  422. if (!TS_REQ_set_version(ts_req, 1))
  423. goto err;
  424. if ((msg_imprint = TS_MSG_IMPRINT_new()) == NULL)
  425. goto err;
  426. if ((algo = X509_ALGOR_new()) == NULL)
  427. goto err;
  428. if ((algo->algorithm = OBJ_nid2obj(EVP_MD_get_type(md))) == NULL)
  429. goto err;
  430. if ((algo->parameter = ASN1_TYPE_new()) == NULL)
  431. goto err;
  432. algo->parameter->type = V_ASN1_NULL;
  433. if (!TS_MSG_IMPRINT_set_algo(msg_imprint, algo))
  434. goto err;
  435. if ((len = create_digest(data_bio, digest, md, &data)) == 0)
  436. goto err;
  437. if (!TS_MSG_IMPRINT_set_msg(msg_imprint, data, len))
  438. goto err;
  439. if (!TS_REQ_set_msg_imprint(ts_req, msg_imprint))
  440. goto err;
  441. if (policy && (policy_obj = txt2obj(policy)) == NULL)
  442. goto err;
  443. if (policy_obj && !TS_REQ_set_policy_id(ts_req, policy_obj))
  444. goto err;
  445. /* Setting nonce if requested. */
  446. if (!no_nonce && (nonce_asn1 = create_nonce(NONCE_LENGTH)) == NULL)
  447. goto err;
  448. if (nonce_asn1 && !TS_REQ_set_nonce(ts_req, nonce_asn1))
  449. goto err;
  450. if (!TS_REQ_set_cert_req(ts_req, cert))
  451. goto err;
  452. ret = 1;
  453. err:
  454. if (!ret) {
  455. TS_REQ_free(ts_req);
  456. ts_req = NULL;
  457. BIO_printf(bio_err, "could not create query\n");
  458. ERR_print_errors(bio_err);
  459. }
  460. TS_MSG_IMPRINT_free(msg_imprint);
  461. X509_ALGOR_free(algo);
  462. OPENSSL_free(data);
  463. ASN1_OBJECT_free(policy_obj);
  464. ASN1_INTEGER_free(nonce_asn1);
  465. return ts_req;
  466. }
  467. static int create_digest(BIO *input, const char *digest, const EVP_MD *md,
  468. unsigned char **md_value)
  469. {
  470. int md_value_len;
  471. int rv = 0;
  472. EVP_MD_CTX *md_ctx = NULL;
  473. md_value_len = EVP_MD_get_size(md);
  474. if (md_value_len < 0)
  475. return 0;
  476. if (input != NULL) {
  477. unsigned char buffer[4096];
  478. int length;
  479. md_ctx = EVP_MD_CTX_new();
  480. if (md_ctx == NULL)
  481. return 0;
  482. *md_value = app_malloc(md_value_len, "digest buffer");
  483. if (!EVP_DigestInit(md_ctx, md))
  484. goto err;
  485. while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) {
  486. if (!EVP_DigestUpdate(md_ctx, buffer, length))
  487. goto err;
  488. }
  489. if (!EVP_DigestFinal(md_ctx, *md_value, NULL))
  490. goto err;
  491. md_value_len = EVP_MD_get_size(md);
  492. } else {
  493. long digest_len;
  494. *md_value = OPENSSL_hexstr2buf(digest, &digest_len);
  495. if (*md_value == NULL || md_value_len != digest_len) {
  496. BIO_printf(bio_err, "bad digest, %d bytes "
  497. "must be specified\n", md_value_len);
  498. goto err;
  499. }
  500. }
  501. rv = md_value_len;
  502. err:
  503. if (rv <= 0) {
  504. OPENSSL_free(*md_value);
  505. *md_value = NULL;
  506. rv = 0;
  507. }
  508. EVP_MD_CTX_free(md_ctx);
  509. return rv;
  510. }
  511. static ASN1_INTEGER *create_nonce(int bits)
  512. {
  513. unsigned char buf[20];
  514. ASN1_INTEGER *nonce = NULL;
  515. int len = (bits - 1) / 8 + 1;
  516. int i;
  517. if (len > (int)sizeof(buf))
  518. goto err;
  519. if (RAND_bytes(buf, len) <= 0)
  520. goto err;
  521. /* Find the first non-zero byte and creating ASN1_INTEGER object. */
  522. for (i = 0; i < len && !buf[i]; ++i)
  523. continue;
  524. if ((nonce = ASN1_INTEGER_new()) == NULL)
  525. goto err;
  526. OPENSSL_free(nonce->data);
  527. nonce->length = len - i;
  528. nonce->data = app_malloc(nonce->length + 1, "nonce buffer");
  529. memcpy(nonce->data, buf + i, nonce->length);
  530. return nonce;
  531. err:
  532. BIO_printf(bio_err, "could not create nonce\n");
  533. ASN1_INTEGER_free(nonce);
  534. return NULL;
  535. }
  536. /*
  537. * Reply-related method definitions.
  538. */
  539. static int reply_command(CONF *conf, const char *section, const char *engine,
  540. const char *queryfile, const char *passin, const char *inkey,
  541. const EVP_MD *md, const char *signer, const char *chain,
  542. const char *policy, const char *in, int token_in,
  543. const char *out, int token_out, int text)
  544. {
  545. int ret = 0;
  546. TS_RESP *response = NULL;
  547. BIO *in_bio = NULL;
  548. BIO *query_bio = NULL;
  549. BIO *inkey_bio = NULL;
  550. BIO *signer_bio = NULL;
  551. BIO *out_bio = NULL;
  552. if (in != NULL) {
  553. if ((in_bio = BIO_new_file(in, "rb")) == NULL)
  554. goto end;
  555. if (token_in) {
  556. response = read_PKCS7(in_bio);
  557. } else {
  558. response = d2i_TS_RESP_bio(in_bio, NULL);
  559. }
  560. } else {
  561. response = create_response(conf, section, engine, queryfile,
  562. passin, inkey, md, signer, chain, policy);
  563. if (response != NULL)
  564. BIO_printf(bio_err, "Response has been generated.\n");
  565. else
  566. BIO_printf(bio_err, "Response is not generated.\n");
  567. }
  568. if (response == NULL)
  569. goto end;
  570. /* Write response. */
  571. if (text) {
  572. if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
  573. goto end;
  574. if (token_out) {
  575. TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
  576. if (!TS_TST_INFO_print_bio(out_bio, tst_info))
  577. goto end;
  578. } else {
  579. if (!TS_RESP_print_bio(out_bio, response))
  580. goto end;
  581. }
  582. } else {
  583. if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
  584. goto end;
  585. if (token_out) {
  586. PKCS7 *token = TS_RESP_get_token(response);
  587. if (!i2d_PKCS7_bio(out_bio, token))
  588. goto end;
  589. } else {
  590. if (!i2d_TS_RESP_bio(out_bio, response))
  591. goto end;
  592. }
  593. }
  594. ret = 1;
  595. end:
  596. ERR_print_errors(bio_err);
  597. BIO_free_all(in_bio);
  598. BIO_free_all(query_bio);
  599. BIO_free_all(inkey_bio);
  600. BIO_free_all(signer_bio);
  601. BIO_free_all(out_bio);
  602. TS_RESP_free(response);
  603. return ret;
  604. }
  605. /* Reads a PKCS7 token and adds default 'granted' status info to it. */
  606. static TS_RESP *read_PKCS7(BIO *in_bio)
  607. {
  608. int ret = 0;
  609. PKCS7 *token = NULL;
  610. TS_TST_INFO *tst_info = NULL;
  611. TS_RESP *resp = NULL;
  612. TS_STATUS_INFO *si = NULL;
  613. if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
  614. goto end;
  615. if ((tst_info = PKCS7_to_TS_TST_INFO(token)) == NULL)
  616. goto end;
  617. if ((resp = TS_RESP_new()) == NULL)
  618. goto end;
  619. if ((si = TS_STATUS_INFO_new()) == NULL)
  620. goto end;
  621. if (!TS_STATUS_INFO_set_status(si, TS_STATUS_GRANTED))
  622. goto end;
  623. if (!TS_RESP_set_status_info(resp, si))
  624. goto end;
  625. TS_RESP_set_tst_info(resp, token, tst_info);
  626. token = NULL; /* Ownership is lost. */
  627. tst_info = NULL; /* Ownership is lost. */
  628. ret = 1;
  629. end:
  630. PKCS7_free(token);
  631. TS_TST_INFO_free(tst_info);
  632. if (!ret) {
  633. TS_RESP_free(resp);
  634. resp = NULL;
  635. }
  636. TS_STATUS_INFO_free(si);
  637. return resp;
  638. }
  639. static TS_RESP *create_response(CONF *conf, const char *section, const char *engine,
  640. const char *queryfile, const char *passin,
  641. const char *inkey, const EVP_MD *md, const char *signer,
  642. const char *chain, const char *policy)
  643. {
  644. int ret = 0;
  645. TS_RESP *response = NULL;
  646. BIO *query_bio = NULL;
  647. TS_RESP_CTX *resp_ctx = NULL;
  648. if ((query_bio = BIO_new_file(queryfile, "rb")) == NULL)
  649. goto end;
  650. if ((section = TS_CONF_get_tsa_section(conf, section)) == NULL)
  651. goto end;
  652. if ((resp_ctx = TS_RESP_CTX_new()) == NULL)
  653. goto end;
  654. if (!TS_CONF_set_serial(conf, section, serial_cb, resp_ctx))
  655. goto end;
  656. #ifndef OPENSSL_NO_ENGINE
  657. if (!TS_CONF_set_crypto_device(conf, section, engine))
  658. goto end;
  659. #endif
  660. if (!TS_CONF_set_signer_cert(conf, section, signer, resp_ctx))
  661. goto end;
  662. if (!TS_CONF_set_certs(conf, section, chain, resp_ctx))
  663. goto end;
  664. if (!TS_CONF_set_signer_key(conf, section, inkey, passin, resp_ctx))
  665. goto end;
  666. if (md) {
  667. if (!TS_RESP_CTX_set_signer_digest(resp_ctx, md))
  668. goto end;
  669. } else if (!TS_CONF_set_signer_digest(conf, section, NULL, resp_ctx)) {
  670. goto end;
  671. }
  672. if (!TS_CONF_set_ess_cert_id_digest(conf, section, resp_ctx))
  673. goto end;
  674. if (!TS_CONF_set_def_policy(conf, section, policy, resp_ctx))
  675. goto end;
  676. if (!TS_CONF_set_policies(conf, section, resp_ctx))
  677. goto end;
  678. if (!TS_CONF_set_digests(conf, section, resp_ctx))
  679. goto end;
  680. if (!TS_CONF_set_accuracy(conf, section, resp_ctx))
  681. goto end;
  682. if (!TS_CONF_set_clock_precision_digits(conf, section, resp_ctx))
  683. goto end;
  684. if (!TS_CONF_set_ordering(conf, section, resp_ctx))
  685. goto end;
  686. if (!TS_CONF_set_tsa_name(conf, section, resp_ctx))
  687. goto end;
  688. if (!TS_CONF_set_ess_cert_id_chain(conf, section, resp_ctx))
  689. goto end;
  690. if ((response = TS_RESP_create_response(resp_ctx, query_bio)) == NULL)
  691. goto end;
  692. ret = 1;
  693. end:
  694. if (!ret) {
  695. TS_RESP_free(response);
  696. response = NULL;
  697. }
  698. TS_RESP_CTX_free(resp_ctx);
  699. BIO_free_all(query_bio);
  700. return response;
  701. }
  702. static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data)
  703. {
  704. const char *serial_file = (const char *)data;
  705. ASN1_INTEGER *serial = next_serial(serial_file);
  706. if (serial == NULL) {
  707. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  708. "Error during serial number "
  709. "generation.");
  710. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_ADD_INFO_NOT_AVAILABLE);
  711. } else {
  712. save_ts_serial(serial_file, serial);
  713. }
  714. return serial;
  715. }
  716. static ASN1_INTEGER *next_serial(const char *serialfile)
  717. {
  718. int ret = 0;
  719. BIO *in = NULL;
  720. ASN1_INTEGER *serial = NULL;
  721. BIGNUM *bn = NULL;
  722. if ((serial = ASN1_INTEGER_new()) == NULL)
  723. goto err;
  724. if ((in = BIO_new_file(serialfile, "r")) == NULL) {
  725. ERR_clear_error();
  726. BIO_printf(bio_err, "Warning: could not open file %s for "
  727. "reading, using serial number: 1\n", serialfile);
  728. if (!ASN1_INTEGER_set(serial, 1))
  729. goto err;
  730. } else {
  731. char buf[1024];
  732. if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {
  733. BIO_printf(bio_err, "unable to load number from %s\n",
  734. serialfile);
  735. goto err;
  736. }
  737. if ((bn = ASN1_INTEGER_to_BN(serial, NULL)) == NULL)
  738. goto err;
  739. ASN1_INTEGER_free(serial);
  740. serial = NULL;
  741. if (!BN_add_word(bn, 1))
  742. goto err;
  743. if ((serial = BN_to_ASN1_INTEGER(bn, NULL)) == NULL)
  744. goto err;
  745. }
  746. ret = 1;
  747. err:
  748. if (!ret) {
  749. ASN1_INTEGER_free(serial);
  750. serial = NULL;
  751. }
  752. BIO_free_all(in);
  753. BN_free(bn);
  754. return serial;
  755. }
  756. static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial)
  757. {
  758. int ret = 0;
  759. BIO *out = NULL;
  760. if ((out = BIO_new_file(serialfile, "w")) == NULL)
  761. goto err;
  762. if (i2a_ASN1_INTEGER(out, serial) <= 0)
  763. goto err;
  764. if (BIO_puts(out, "\n") <= 0)
  765. goto err;
  766. ret = 1;
  767. err:
  768. if (!ret)
  769. BIO_printf(bio_err, "could not save serial number to %s\n",
  770. serialfile);
  771. BIO_free_all(out);
  772. return ret;
  773. }
  774. /*
  775. * Verify-related method definitions.
  776. */
  777. static int verify_command(const char *data, const char *digest, const char *queryfile,
  778. const char *in, int token_in,
  779. const char *CApath, const char *CAfile,
  780. const char *CAstore, char *untrusted,
  781. X509_VERIFY_PARAM *vpm)
  782. {
  783. BIO *in_bio = NULL;
  784. PKCS7 *token = NULL;
  785. TS_RESP *response = NULL;
  786. TS_VERIFY_CTX *verify_ctx = NULL;
  787. int ret = 0;
  788. if ((in_bio = BIO_new_file(in, "rb")) == NULL)
  789. goto end;
  790. if (token_in) {
  791. if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
  792. goto end;
  793. } else {
  794. if ((response = d2i_TS_RESP_bio(in_bio, NULL)) == NULL)
  795. goto end;
  796. }
  797. if ((verify_ctx = create_verify_ctx(data, digest, queryfile,
  798. CApath, CAfile, CAstore, untrusted,
  799. vpm)) == NULL)
  800. goto end;
  801. ret = token_in
  802. ? TS_RESP_verify_token(verify_ctx, token)
  803. : TS_RESP_verify_response(verify_ctx, response);
  804. end:
  805. printf("Verification: ");
  806. if (ret)
  807. printf("OK\n");
  808. else {
  809. printf("FAILED\n");
  810. ERR_print_errors(bio_err);
  811. }
  812. BIO_free_all(in_bio);
  813. PKCS7_free(token);
  814. TS_RESP_free(response);
  815. TS_VERIFY_CTX_free(verify_ctx);
  816. return ret;
  817. }
  818. static TS_VERIFY_CTX *create_verify_ctx(const char *data, const char *digest,
  819. const char *queryfile,
  820. const char *CApath, const char *CAfile,
  821. const char *CAstore,
  822. char *untrusted,
  823. X509_VERIFY_PARAM *vpm)
  824. {
  825. TS_VERIFY_CTX *ctx = NULL;
  826. STACK_OF(X509) *certs;
  827. BIO *input = NULL;
  828. TS_REQ *request = NULL;
  829. int ret = 0;
  830. int f = 0;
  831. if (data != NULL || digest != NULL) {
  832. if ((ctx = TS_VERIFY_CTX_new()) == NULL)
  833. goto err;
  834. f = TS_VFY_VERSION | TS_VFY_SIGNER;
  835. if (data != NULL) {
  836. BIO *out = NULL;
  837. f |= TS_VFY_DATA;
  838. if ((out = BIO_new_file(data, "rb")) == NULL)
  839. goto err;
  840. if (TS_VERIFY_CTX_set_data(ctx, out) == NULL) {
  841. BIO_free_all(out);
  842. goto err;
  843. }
  844. } else if (digest != NULL) {
  845. long imprint_len;
  846. unsigned char *hexstr = OPENSSL_hexstr2buf(digest, &imprint_len);
  847. f |= TS_VFY_IMPRINT;
  848. if (TS_VERIFY_CTX_set_imprint(ctx, hexstr, imprint_len) == NULL) {
  849. BIO_printf(bio_err, "invalid digest string\n");
  850. goto err;
  851. }
  852. }
  853. } else if (queryfile != NULL) {
  854. if ((input = BIO_new_file(queryfile, "rb")) == NULL)
  855. goto err;
  856. if ((request = d2i_TS_REQ_bio(input, NULL)) == NULL)
  857. goto err;
  858. if ((ctx = TS_REQ_to_TS_VERIFY_CTX(request, NULL)) == NULL)
  859. goto err;
  860. } else {
  861. return NULL;
  862. }
  863. /* Add the signature verification flag and arguments. */
  864. TS_VERIFY_CTX_add_flags(ctx, f | TS_VFY_SIGNATURE);
  865. /* Initialising the X509_STORE object. */
  866. if (TS_VERIFY_CTX_set_store(ctx,
  867. create_cert_store(CApath, CAfile, CAstore, vpm))
  868. == NULL)
  869. goto err;
  870. /* Loading any extra untrusted certificates. */
  871. if (untrusted != NULL) {
  872. certs = load_certs_multifile(untrusted, NULL, "extra untrusted certs",
  873. vpm);
  874. if (certs == NULL || TS_VERIFY_CTX_set_certs(ctx, certs) == NULL)
  875. goto err;
  876. }
  877. ret = 1;
  878. err:
  879. if (!ret) {
  880. TS_VERIFY_CTX_free(ctx);
  881. ctx = NULL;
  882. }
  883. BIO_free_all(input);
  884. TS_REQ_free(request);
  885. return ctx;
  886. }
  887. static X509_STORE *create_cert_store(const char *CApath, const char *CAfile,
  888. const char *CAstore, X509_VERIFY_PARAM *vpm)
  889. {
  890. X509_STORE *cert_ctx = NULL;
  891. X509_LOOKUP *lookup = NULL;
  892. OSSL_LIB_CTX *libctx = app_get0_libctx();
  893. const char *propq = app_get0_propq();
  894. cert_ctx = X509_STORE_new();
  895. if (cert_ctx == NULL) {
  896. BIO_printf(bio_err, "memory allocation failure\n");
  897. return NULL;
  898. }
  899. X509_STORE_set_verify_cb(cert_ctx, verify_cb);
  900. if (CApath != NULL) {
  901. lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir());
  902. if (lookup == NULL) {
  903. BIO_printf(bio_err, "memory allocation failure\n");
  904. goto err;
  905. }
  906. if (X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM) <= 0) {
  907. BIO_printf(bio_err, "Error loading directory %s\n", CApath);
  908. goto err;
  909. }
  910. }
  911. if (CAfile != NULL) {
  912. lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
  913. if (lookup == NULL) {
  914. BIO_printf(bio_err, "memory allocation failure\n");
  915. goto err;
  916. }
  917. if (X509_LOOKUP_load_file_ex(lookup, CAfile, X509_FILETYPE_PEM, libctx,
  918. propq) <= 0) {
  919. BIO_printf(bio_err, "Error loading file %s\n", CAfile);
  920. goto err;
  921. }
  922. }
  923. if (CAstore != NULL) {
  924. lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_store());
  925. if (lookup == NULL) {
  926. BIO_printf(bio_err, "memory allocation failure\n");
  927. goto err;
  928. }
  929. if (X509_LOOKUP_load_store_ex(lookup, CAstore, libctx, propq) <= 0) {
  930. BIO_printf(bio_err, "Error loading store URI %s\n", CAstore);
  931. goto err;
  932. }
  933. }
  934. if (vpm != NULL)
  935. X509_STORE_set1_param(cert_ctx, vpm);
  936. return cert_ctx;
  937. err:
  938. X509_STORE_free(cert_ctx);
  939. return NULL;
  940. }
  941. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  942. {
  943. return ok;
  944. }