ts.c 31 KB

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