ts.c 30 KB

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