2
0

ts.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. OPENSSL_free(*md_value);
  497. *md_value = NULL;
  498. BIO_printf(bio_err, "bad digest, %d bytes "
  499. "must be specified\n", md_value_len);
  500. return 0;
  501. }
  502. }
  503. rv = md_value_len;
  504. err:
  505. EVP_MD_CTX_free(md_ctx);
  506. return rv;
  507. }
  508. static ASN1_INTEGER *create_nonce(int bits)
  509. {
  510. unsigned char buf[20];
  511. ASN1_INTEGER *nonce = NULL;
  512. int len = (bits - 1) / 8 + 1;
  513. int i;
  514. if (len > (int)sizeof(buf))
  515. goto err;
  516. if (RAND_bytes(buf, len) <= 0)
  517. goto err;
  518. /* Find the first non-zero byte and creating ASN1_INTEGER object. */
  519. for (i = 0; i < len && !buf[i]; ++i)
  520. continue;
  521. if ((nonce = ASN1_INTEGER_new()) == NULL)
  522. goto err;
  523. OPENSSL_free(nonce->data);
  524. nonce->length = len - i;
  525. nonce->data = app_malloc(nonce->length + 1, "nonce buffer");
  526. memcpy(nonce->data, buf + i, nonce->length);
  527. return nonce;
  528. err:
  529. BIO_printf(bio_err, "could not create nonce\n");
  530. ASN1_INTEGER_free(nonce);
  531. return NULL;
  532. }
  533. /*
  534. * Reply-related method definitions.
  535. */
  536. static int reply_command(CONF *conf, const char *section, const char *engine,
  537. const char *queryfile, const char *passin, const char *inkey,
  538. const EVP_MD *md, const char *signer, const char *chain,
  539. const char *policy, const char *in, int token_in,
  540. const char *out, int token_out, int text)
  541. {
  542. int ret = 0;
  543. TS_RESP *response = NULL;
  544. BIO *in_bio = NULL;
  545. BIO *query_bio = NULL;
  546. BIO *inkey_bio = NULL;
  547. BIO *signer_bio = NULL;
  548. BIO *out_bio = NULL;
  549. if (in != NULL) {
  550. if ((in_bio = BIO_new_file(in, "rb")) == NULL)
  551. goto end;
  552. if (token_in) {
  553. response = read_PKCS7(in_bio);
  554. } else {
  555. response = d2i_TS_RESP_bio(in_bio, NULL);
  556. }
  557. } else {
  558. response = create_response(conf, section, engine, queryfile,
  559. passin, inkey, md, signer, chain, policy);
  560. if (response != NULL)
  561. BIO_printf(bio_err, "Response has been generated.\n");
  562. else
  563. BIO_printf(bio_err, "Response is not generated.\n");
  564. }
  565. if (response == NULL)
  566. goto end;
  567. /* Write response. */
  568. if (text) {
  569. if ((out_bio = bio_open_default(out, 'w', FORMAT_TEXT)) == NULL)
  570. goto end;
  571. if (token_out) {
  572. TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
  573. if (!TS_TST_INFO_print_bio(out_bio, tst_info))
  574. goto end;
  575. } else {
  576. if (!TS_RESP_print_bio(out_bio, response))
  577. goto end;
  578. }
  579. } else {
  580. if ((out_bio = bio_open_default(out, 'w', FORMAT_ASN1)) == NULL)
  581. goto end;
  582. if (token_out) {
  583. PKCS7 *token = TS_RESP_get_token(response);
  584. if (!i2d_PKCS7_bio(out_bio, token))
  585. goto end;
  586. } else {
  587. if (!i2d_TS_RESP_bio(out_bio, response))
  588. goto end;
  589. }
  590. }
  591. ret = 1;
  592. end:
  593. ERR_print_errors(bio_err);
  594. BIO_free_all(in_bio);
  595. BIO_free_all(query_bio);
  596. BIO_free_all(inkey_bio);
  597. BIO_free_all(signer_bio);
  598. BIO_free_all(out_bio);
  599. TS_RESP_free(response);
  600. return ret;
  601. }
  602. /* Reads a PKCS7 token and adds default 'granted' status info to it. */
  603. static TS_RESP *read_PKCS7(BIO *in_bio)
  604. {
  605. int ret = 0;
  606. PKCS7 *token = NULL;
  607. TS_TST_INFO *tst_info = NULL;
  608. TS_RESP *resp = NULL;
  609. TS_STATUS_INFO *si = NULL;
  610. if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
  611. goto end;
  612. if ((tst_info = PKCS7_to_TS_TST_INFO(token)) == NULL)
  613. goto end;
  614. if ((resp = TS_RESP_new()) == NULL)
  615. goto end;
  616. if ((si = TS_STATUS_INFO_new()) == NULL)
  617. goto end;
  618. if (!TS_STATUS_INFO_set_status(si, TS_STATUS_GRANTED))
  619. goto end;
  620. if (!TS_RESP_set_status_info(resp, si))
  621. goto end;
  622. TS_RESP_set_tst_info(resp, token, tst_info);
  623. token = NULL; /* Ownership is lost. */
  624. tst_info = NULL; /* Ownership is lost. */
  625. ret = 1;
  626. end:
  627. PKCS7_free(token);
  628. TS_TST_INFO_free(tst_info);
  629. if (!ret) {
  630. TS_RESP_free(resp);
  631. resp = NULL;
  632. }
  633. TS_STATUS_INFO_free(si);
  634. return resp;
  635. }
  636. static TS_RESP *create_response(CONF *conf, const char *section, const char *engine,
  637. const char *queryfile, const char *passin,
  638. const char *inkey, const EVP_MD *md, const char *signer,
  639. const char *chain, const char *policy)
  640. {
  641. int ret = 0;
  642. TS_RESP *response = NULL;
  643. BIO *query_bio = NULL;
  644. TS_RESP_CTX *resp_ctx = NULL;
  645. if ((query_bio = BIO_new_file(queryfile, "rb")) == NULL)
  646. goto end;
  647. if ((section = TS_CONF_get_tsa_section(conf, section)) == NULL)
  648. goto end;
  649. if ((resp_ctx = TS_RESP_CTX_new()) == NULL)
  650. goto end;
  651. if (!TS_CONF_set_serial(conf, section, serial_cb, resp_ctx))
  652. goto end;
  653. #ifndef OPENSSL_NO_ENGINE
  654. if (!TS_CONF_set_crypto_device(conf, section, engine))
  655. goto end;
  656. #endif
  657. if (!TS_CONF_set_signer_cert(conf, section, signer, resp_ctx))
  658. goto end;
  659. if (!TS_CONF_set_certs(conf, section, chain, resp_ctx))
  660. goto end;
  661. if (!TS_CONF_set_signer_key(conf, section, inkey, passin, resp_ctx))
  662. goto end;
  663. if (md) {
  664. if (!TS_RESP_CTX_set_signer_digest(resp_ctx, md))
  665. goto end;
  666. } else if (!TS_CONF_set_signer_digest(conf, section, NULL, resp_ctx)) {
  667. goto end;
  668. }
  669. if (!TS_CONF_set_ess_cert_id_digest(conf, section, resp_ctx))
  670. goto end;
  671. if (!TS_CONF_set_def_policy(conf, section, policy, resp_ctx))
  672. goto end;
  673. if (!TS_CONF_set_policies(conf, section, resp_ctx))
  674. goto end;
  675. if (!TS_CONF_set_digests(conf, section, resp_ctx))
  676. goto end;
  677. if (!TS_CONF_set_accuracy(conf, section, resp_ctx))
  678. goto end;
  679. if (!TS_CONF_set_clock_precision_digits(conf, section, resp_ctx))
  680. goto end;
  681. if (!TS_CONF_set_ordering(conf, section, resp_ctx))
  682. goto end;
  683. if (!TS_CONF_set_tsa_name(conf, section, resp_ctx))
  684. goto end;
  685. if (!TS_CONF_set_ess_cert_id_chain(conf, section, resp_ctx))
  686. goto end;
  687. if ((response = TS_RESP_create_response(resp_ctx, query_bio)) == NULL)
  688. goto end;
  689. ret = 1;
  690. end:
  691. if (!ret) {
  692. TS_RESP_free(response);
  693. response = NULL;
  694. }
  695. TS_RESP_CTX_free(resp_ctx);
  696. BIO_free_all(query_bio);
  697. return response;
  698. }
  699. static ASN1_INTEGER *serial_cb(TS_RESP_CTX *ctx, void *data)
  700. {
  701. const char *serial_file = (const char *)data;
  702. ASN1_INTEGER *serial = next_serial(serial_file);
  703. if (serial == NULL) {
  704. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  705. "Error during serial number "
  706. "generation.");
  707. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_ADD_INFO_NOT_AVAILABLE);
  708. } else {
  709. save_ts_serial(serial_file, serial);
  710. }
  711. return serial;
  712. }
  713. static ASN1_INTEGER *next_serial(const char *serialfile)
  714. {
  715. int ret = 0;
  716. BIO *in = NULL;
  717. ASN1_INTEGER *serial = NULL;
  718. BIGNUM *bn = NULL;
  719. if ((serial = ASN1_INTEGER_new()) == NULL)
  720. goto err;
  721. if ((in = BIO_new_file(serialfile, "r")) == NULL) {
  722. ERR_clear_error();
  723. BIO_printf(bio_err, "Warning: could not open file %s for "
  724. "reading, using serial number: 1\n", serialfile);
  725. if (!ASN1_INTEGER_set(serial, 1))
  726. goto err;
  727. } else {
  728. char buf[1024];
  729. if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {
  730. BIO_printf(bio_err, "unable to load number from %s\n",
  731. serialfile);
  732. goto err;
  733. }
  734. if ((bn = ASN1_INTEGER_to_BN(serial, NULL)) == NULL)
  735. goto err;
  736. ASN1_INTEGER_free(serial);
  737. serial = NULL;
  738. if (!BN_add_word(bn, 1))
  739. goto err;
  740. if ((serial = BN_to_ASN1_INTEGER(bn, NULL)) == NULL)
  741. goto err;
  742. }
  743. ret = 1;
  744. err:
  745. if (!ret) {
  746. ASN1_INTEGER_free(serial);
  747. serial = NULL;
  748. }
  749. BIO_free_all(in);
  750. BN_free(bn);
  751. return serial;
  752. }
  753. static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial)
  754. {
  755. int ret = 0;
  756. BIO *out = NULL;
  757. if ((out = BIO_new_file(serialfile, "w")) == NULL)
  758. goto err;
  759. if (i2a_ASN1_INTEGER(out, serial) <= 0)
  760. goto err;
  761. if (BIO_puts(out, "\n") <= 0)
  762. goto err;
  763. ret = 1;
  764. err:
  765. if (!ret)
  766. BIO_printf(bio_err, "could not save serial number to %s\n",
  767. serialfile);
  768. BIO_free_all(out);
  769. return ret;
  770. }
  771. /*
  772. * Verify-related method definitions.
  773. */
  774. static int verify_command(const char *data, const char *digest, const char *queryfile,
  775. const char *in, int token_in,
  776. const char *CApath, const char *CAfile,
  777. const char *CAstore, char *untrusted,
  778. X509_VERIFY_PARAM *vpm)
  779. {
  780. BIO *in_bio = NULL;
  781. PKCS7 *token = NULL;
  782. TS_RESP *response = NULL;
  783. TS_VERIFY_CTX *verify_ctx = NULL;
  784. int ret = 0;
  785. if ((in_bio = BIO_new_file(in, "rb")) == NULL)
  786. goto end;
  787. if (token_in) {
  788. if ((token = d2i_PKCS7_bio(in_bio, NULL)) == NULL)
  789. goto end;
  790. } else {
  791. if ((response = d2i_TS_RESP_bio(in_bio, NULL)) == NULL)
  792. goto end;
  793. }
  794. if ((verify_ctx = create_verify_ctx(data, digest, queryfile,
  795. CApath, CAfile, CAstore, untrusted,
  796. vpm)) == NULL)
  797. goto end;
  798. ret = token_in
  799. ? TS_RESP_verify_token(verify_ctx, token)
  800. : TS_RESP_verify_response(verify_ctx, response);
  801. end:
  802. printf("Verification: ");
  803. if (ret)
  804. printf("OK\n");
  805. else {
  806. printf("FAILED\n");
  807. ERR_print_errors(bio_err);
  808. }
  809. BIO_free_all(in_bio);
  810. PKCS7_free(token);
  811. TS_RESP_free(response);
  812. TS_VERIFY_CTX_free(verify_ctx);
  813. return ret;
  814. }
  815. static TS_VERIFY_CTX *create_verify_ctx(const char *data, const char *digest,
  816. const char *queryfile,
  817. const char *CApath, const char *CAfile,
  818. const char *CAstore,
  819. char *untrusted,
  820. X509_VERIFY_PARAM *vpm)
  821. {
  822. TS_VERIFY_CTX *ctx = NULL;
  823. STACK_OF(X509) *certs;
  824. BIO *input = NULL;
  825. TS_REQ *request = NULL;
  826. int ret = 0;
  827. int f = 0;
  828. if (data != NULL || digest != NULL) {
  829. if ((ctx = TS_VERIFY_CTX_new()) == NULL)
  830. goto err;
  831. f = TS_VFY_VERSION | TS_VFY_SIGNER;
  832. if (data != NULL) {
  833. BIO *out = NULL;
  834. f |= TS_VFY_DATA;
  835. if ((out = BIO_new_file(data, "rb")) == NULL)
  836. goto err;
  837. if (TS_VERIFY_CTX_set_data(ctx, out) == NULL) {
  838. BIO_free_all(out);
  839. goto err;
  840. }
  841. } else if (digest != NULL) {
  842. long imprint_len;
  843. unsigned char *hexstr = OPENSSL_hexstr2buf(digest, &imprint_len);
  844. f |= TS_VFY_IMPRINT;
  845. if (TS_VERIFY_CTX_set_imprint(ctx, hexstr, imprint_len) == NULL) {
  846. BIO_printf(bio_err, "invalid digest string\n");
  847. goto err;
  848. }
  849. }
  850. } else if (queryfile != NULL) {
  851. if ((input = BIO_new_file(queryfile, "rb")) == NULL)
  852. goto err;
  853. if ((request = d2i_TS_REQ_bio(input, NULL)) == NULL)
  854. goto err;
  855. if ((ctx = TS_REQ_to_TS_VERIFY_CTX(request, NULL)) == NULL)
  856. goto err;
  857. } else {
  858. return NULL;
  859. }
  860. /* Add the signature verification flag and arguments. */
  861. TS_VERIFY_CTX_add_flags(ctx, f | TS_VFY_SIGNATURE);
  862. /* Initialising the X509_STORE object. */
  863. if (TS_VERIFY_CTX_set_store(ctx,
  864. create_cert_store(CApath, CAfile, CAstore, vpm))
  865. == NULL)
  866. goto err;
  867. /* Loading any extra untrusted certificates. */
  868. if (untrusted != NULL) {
  869. certs = load_certs_multifile(untrusted, NULL, "extra untrusted certs",
  870. vpm);
  871. if (certs == NULL || TS_VERIFY_CTX_set_certs(ctx, certs) == NULL)
  872. goto err;
  873. }
  874. ret = 1;
  875. err:
  876. if (!ret) {
  877. TS_VERIFY_CTX_free(ctx);
  878. ctx = NULL;
  879. }
  880. BIO_free_all(input);
  881. TS_REQ_free(request);
  882. return ctx;
  883. }
  884. static X509_STORE *create_cert_store(const char *CApath, const char *CAfile,
  885. const char *CAstore, X509_VERIFY_PARAM *vpm)
  886. {
  887. X509_STORE *cert_ctx = NULL;
  888. X509_LOOKUP *lookup = NULL;
  889. OSSL_LIB_CTX *libctx = app_get0_libctx();
  890. const char *propq = app_get0_propq();
  891. cert_ctx = X509_STORE_new();
  892. if (cert_ctx == NULL) {
  893. BIO_printf(bio_err, "memory allocation failure\n");
  894. return NULL;
  895. }
  896. X509_STORE_set_verify_cb(cert_ctx, verify_cb);
  897. if (CApath != NULL) {
  898. lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir());
  899. if (lookup == NULL) {
  900. BIO_printf(bio_err, "memory allocation failure\n");
  901. goto err;
  902. }
  903. if (X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM) <= 0) {
  904. BIO_printf(bio_err, "Error loading directory %s\n", CApath);
  905. goto err;
  906. }
  907. }
  908. if (CAfile != NULL) {
  909. lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
  910. if (lookup == NULL) {
  911. BIO_printf(bio_err, "memory allocation failure\n");
  912. goto err;
  913. }
  914. if (X509_LOOKUP_load_file_ex(lookup, CAfile, X509_FILETYPE_PEM, libctx,
  915. propq) <= 0) {
  916. BIO_printf(bio_err, "Error loading file %s\n", CAfile);
  917. goto err;
  918. }
  919. }
  920. if (CAstore != NULL) {
  921. lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_store());
  922. if (lookup == NULL) {
  923. BIO_printf(bio_err, "memory allocation failure\n");
  924. goto err;
  925. }
  926. if (X509_LOOKUP_load_store_ex(lookup, CAstore, libctx, propq) <= 0) {
  927. BIO_printf(bio_err, "Error loading store URI %s\n", CAstore);
  928. goto err;
  929. }
  930. }
  931. if (vpm != NULL)
  932. X509_STORE_set1_param(cert_ctx, vpm);
  933. return cert_ctx;
  934. err:
  935. X509_STORE_free(cert_ctx);
  936. return NULL;
  937. }
  938. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  939. {
  940. return ok;
  941. }