ts.c 32 KB

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