ts.c 31 KB

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