ts.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /* apps/ts.c */
  2. /*
  3. * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
  4. * 2002.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include "apps.h"
  63. #include <openssl/bio.h>
  64. #include <openssl/err.h>
  65. #include <openssl/pem.h>
  66. #include <openssl/rand.h>
  67. #include <openssl/ts.h>
  68. #include <openssl/bn.h>
  69. #undef PROG
  70. #define PROG ts_main
  71. /* Length of the nonce of the request in bits (must be a multiple of 8). */
  72. #define NONCE_LENGTH 64
  73. /* Macro definitions for the configuration file. */
  74. #define ENV_OID_FILE "oid_file"
  75. /* Local function declarations. */
  76. static ASN1_OBJECT *txt2obj(const char *oid);
  77. static CONF *load_config_file(const char *configfile);
  78. /* Query related functions. */
  79. static int query_command(const char *data, char *digest,
  80. const EVP_MD *md, const char *policy, int no_nonce,
  81. int cert, const char *in, const char *out, int text);
  82. static BIO *BIO_open_with_default(const char *file, const char *mode,
  83. FILE *default_fp);
  84. static TS_REQ *create_query(BIO *data_bio, char *digest, const EVP_MD *md,
  85. const char *policy, int no_nonce, int cert);
  86. static int create_digest(BIO *input, char *digest,
  87. const EVP_MD *md, unsigned char **md_value);
  88. static ASN1_INTEGER *create_nonce(int bits);
  89. /* Reply related functions. */
  90. static int reply_command(CONF *conf, char *section, char *engine,
  91. char *queryfile, char *passin, char *inkey,
  92. char *signer, char *chain, const char *policy,
  93. char *in, int token_in, char *out, int token_out,
  94. int text);
  95. static TS_RESP *read_PKCS7(BIO *in_bio);
  96. static TS_RESP *create_response(CONF *conf, const char *section, char *engine,
  97. char *queryfile, char *passin, char *inkey,
  98. char *signer, char *chain,
  99. const char *policy);
  100. static ASN1_INTEGER *MS_CALLBACK serial_cb(TS_RESP_CTX *ctx, void *data);
  101. static ASN1_INTEGER *next_serial(const char *serialfile);
  102. static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial);
  103. /* Verify related functions. */
  104. static int verify_command(char *data, char *digest, char *queryfile,
  105. char *in, int token_in,
  106. char *ca_path, char *ca_file, char *untrusted);
  107. static TS_VERIFY_CTX *create_verify_ctx(char *data, char *digest,
  108. char *queryfile,
  109. char *ca_path, char *ca_file,
  110. char *untrusted);
  111. static X509_STORE *create_cert_store(char *ca_path, char *ca_file);
  112. static int MS_CALLBACK verify_cb(int ok, X509_STORE_CTX *ctx);
  113. /* Main function definition. */
  114. int MAIN(int, char **);
  115. int MAIN(int argc, char **argv)
  116. {
  117. int ret = 1;
  118. char *configfile = NULL;
  119. char *section = NULL;
  120. CONF *conf = NULL;
  121. enum mode {
  122. CMD_NONE, CMD_QUERY, CMD_REPLY, CMD_VERIFY
  123. } mode = CMD_NONE;
  124. char *data = NULL;
  125. char *digest = NULL;
  126. const EVP_MD *md = NULL;
  127. char *rnd = NULL;
  128. char *policy = NULL;
  129. int no_nonce = 0;
  130. int cert = 0;
  131. char *in = NULL;
  132. char *out = NULL;
  133. int text = 0;
  134. char *queryfile = NULL;
  135. char *passin = NULL; /* Password source. */
  136. char *password = NULL; /* Password itself. */
  137. char *inkey = NULL;
  138. char *signer = NULL;
  139. char *chain = NULL;
  140. char *ca_path = NULL;
  141. char *ca_file = NULL;
  142. char *untrusted = NULL;
  143. char *engine = NULL;
  144. /* Input is ContentInfo instead of TimeStampResp. */
  145. int token_in = 0;
  146. /* Output is ContentInfo instead of TimeStampResp. */
  147. int token_out = 0;
  148. int free_bio_err = 0;
  149. ERR_load_crypto_strings();
  150. apps_startup();
  151. if (bio_err == NULL && (bio_err = BIO_new(BIO_s_file())) != NULL) {
  152. free_bio_err = 1;
  153. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  154. }
  155. if (!load_config(bio_err, NULL))
  156. goto cleanup;
  157. for (argc--, argv++; argc > 0; argc--, argv++) {
  158. if (strcmp(*argv, "-config") == 0) {
  159. if (argc-- < 1)
  160. goto usage;
  161. configfile = *++argv;
  162. } else if (strcmp(*argv, "-section") == 0) {
  163. if (argc-- < 1)
  164. goto usage;
  165. section = *++argv;
  166. } else if (strcmp(*argv, "-query") == 0) {
  167. if (mode != CMD_NONE)
  168. goto usage;
  169. mode = CMD_QUERY;
  170. } else if (strcmp(*argv, "-data") == 0) {
  171. if (argc-- < 1)
  172. goto usage;
  173. data = *++argv;
  174. } else if (strcmp(*argv, "-digest") == 0) {
  175. if (argc-- < 1)
  176. goto usage;
  177. digest = *++argv;
  178. } else if (strcmp(*argv, "-rand") == 0) {
  179. if (argc-- < 1)
  180. goto usage;
  181. rnd = *++argv;
  182. } else if (strcmp(*argv, "-policy") == 0) {
  183. if (argc-- < 1)
  184. goto usage;
  185. policy = *++argv;
  186. } else if (strcmp(*argv, "-no_nonce") == 0) {
  187. no_nonce = 1;
  188. } else if (strcmp(*argv, "-cert") == 0) {
  189. cert = 1;
  190. } else if (strcmp(*argv, "-in") == 0) {
  191. if (argc-- < 1)
  192. goto usage;
  193. in = *++argv;
  194. } else if (strcmp(*argv, "-token_in") == 0) {
  195. token_in = 1;
  196. } else if (strcmp(*argv, "-out") == 0) {
  197. if (argc-- < 1)
  198. goto usage;
  199. out = *++argv;
  200. } else if (strcmp(*argv, "-token_out") == 0) {
  201. token_out = 1;
  202. } else if (strcmp(*argv, "-text") == 0) {
  203. text = 1;
  204. } else if (strcmp(*argv, "-reply") == 0) {
  205. if (mode != CMD_NONE)
  206. goto usage;
  207. mode = CMD_REPLY;
  208. } else if (strcmp(*argv, "-queryfile") == 0) {
  209. if (argc-- < 1)
  210. goto usage;
  211. queryfile = *++argv;
  212. } else if (strcmp(*argv, "-passin") == 0) {
  213. if (argc-- < 1)
  214. goto usage;
  215. passin = *++argv;
  216. } else if (strcmp(*argv, "-inkey") == 0) {
  217. if (argc-- < 1)
  218. goto usage;
  219. inkey = *++argv;
  220. } else if (strcmp(*argv, "-signer") == 0) {
  221. if (argc-- < 1)
  222. goto usage;
  223. signer = *++argv;
  224. } else if (strcmp(*argv, "-chain") == 0) {
  225. if (argc-- < 1)
  226. goto usage;
  227. chain = *++argv;
  228. } else if (strcmp(*argv, "-verify") == 0) {
  229. if (mode != CMD_NONE)
  230. goto usage;
  231. mode = CMD_VERIFY;
  232. } else if (strcmp(*argv, "-CApath") == 0) {
  233. if (argc-- < 1)
  234. goto usage;
  235. ca_path = *++argv;
  236. } else if (strcmp(*argv, "-CAfile") == 0) {
  237. if (argc-- < 1)
  238. goto usage;
  239. ca_file = *++argv;
  240. } else if (strcmp(*argv, "-untrusted") == 0) {
  241. if (argc-- < 1)
  242. goto usage;
  243. untrusted = *++argv;
  244. } else if (strcmp(*argv, "-engine") == 0) {
  245. if (argc-- < 1)
  246. goto usage;
  247. engine = *++argv;
  248. } else if ((md = EVP_get_digestbyname(*argv + 1)) != NULL) {
  249. /* empty. */
  250. } else
  251. goto usage;
  252. }
  253. /* Seed the random number generator if it is going to be used. */
  254. if (mode == CMD_QUERY && !no_nonce) {
  255. if (!app_RAND_load_file(NULL, bio_err, 1) && rnd == NULL)
  256. BIO_printf(bio_err, "warning, not much extra random "
  257. "data, consider using the -rand option\n");
  258. if (rnd != NULL)
  259. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  260. app_RAND_load_files(rnd));
  261. }
  262. /* Get the password if required. */
  263. if (mode == CMD_REPLY && passin &&
  264. !app_passwd(bio_err, passin, NULL, &password, NULL)) {
  265. BIO_printf(bio_err, "Error getting password.\n");
  266. goto cleanup;
  267. }
  268. /*
  269. * Check consistency of parameters and execute the appropriate function.
  270. */
  271. switch (mode) {
  272. case CMD_NONE:
  273. goto usage;
  274. case CMD_QUERY:
  275. /*
  276. * Data file and message imprint cannot be specified at the same
  277. * time.
  278. */
  279. ret = data != NULL && digest != NULL;
  280. if (ret)
  281. goto usage;
  282. /* Load the config file for possible policy OIDs. */
  283. conf = load_config_file(configfile);
  284. ret = !query_command(data, digest, md, policy, no_nonce, cert,
  285. in, out, text);
  286. break;
  287. case CMD_REPLY:
  288. conf = load_config_file(configfile);
  289. if (in == NULL) {
  290. ret = !(queryfile != NULL && conf != NULL && !token_in);
  291. if (ret)
  292. goto usage;
  293. } else {
  294. /* 'in' and 'queryfile' are exclusive. */
  295. ret = !(queryfile == NULL);
  296. if (ret)
  297. goto usage;
  298. }
  299. ret = !reply_command(conf, section, engine, queryfile,
  300. password, inkey, signer, chain, policy,
  301. in, token_in, out, token_out, text);
  302. break;
  303. case CMD_VERIFY:
  304. ret = !(((queryfile && !data && !digest)
  305. || (!queryfile && data && !digest)
  306. || (!queryfile && !data && digest))
  307. && in != NULL);
  308. if (ret)
  309. goto usage;
  310. ret = !verify_command(data, digest, queryfile, in, token_in,
  311. ca_path, ca_file, untrusted);
  312. }
  313. goto cleanup;
  314. usage:
  315. BIO_printf(bio_err, "usage:\n"
  316. "ts -query [-rand file%cfile%c...] [-config configfile] "
  317. "[-data file_to_hash] [-digest digest_bytes]"
  318. "[-md2|-md4|-md5|-sha|-sha1|-mdc2|-ripemd160] "
  319. "[-policy object_id] [-no_nonce] [-cert] "
  320. "[-in request.tsq] [-out request.tsq] [-text]\n",
  321. LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
  322. BIO_printf(bio_err, "or\n"
  323. "ts -reply [-config configfile] [-section tsa_section] "
  324. "[-queryfile request.tsq] [-passin password] "
  325. "[-signer tsa_cert.pem] [-inkey private_key.pem] "
  326. "[-chain certs_file.pem] [-policy object_id] "
  327. "[-in response.tsr] [-token_in] "
  328. "[-out response.tsr] [-token_out] [-text] [-engine id]\n");
  329. BIO_printf(bio_err, "or\n"
  330. "ts -verify [-data file_to_hash] [-digest digest_bytes] "
  331. "[-queryfile request.tsq] "
  332. "-in response.tsr [-token_in] "
  333. "-CApath ca_path -CAfile ca_file.pem "
  334. "-untrusted cert_file.pem\n");
  335. cleanup:
  336. /* Clean up. */
  337. app_RAND_write_file(NULL, bio_err);
  338. NCONF_free(conf);
  339. OPENSSL_free(password);
  340. OBJ_cleanup();
  341. if (free_bio_err) {
  342. BIO_free_all(bio_err);
  343. bio_err = NULL;
  344. }
  345. OPENSSL_EXIT(ret);
  346. }
  347. /*
  348. * Configuration file-related function definitions.
  349. */
  350. static ASN1_OBJECT *txt2obj(const char *oid)
  351. {
  352. ASN1_OBJECT *oid_obj = NULL;
  353. if (!(oid_obj = OBJ_txt2obj(oid, 0)))
  354. BIO_printf(bio_err, "cannot convert %s to OID\n", oid);
  355. return oid_obj;
  356. }
  357. static CONF *load_config_file(const char *configfile)
  358. {
  359. CONF *conf = NULL;
  360. long errorline = -1;
  361. if (!configfile)
  362. configfile = getenv("OPENSSL_CONF");
  363. if (!configfile)
  364. configfile = getenv("SSLEAY_CONF");
  365. if (configfile &&
  366. (!(conf = NCONF_new(NULL)) ||
  367. NCONF_load(conf, configfile, &errorline) <= 0)) {
  368. if (errorline <= 0)
  369. BIO_printf(bio_err, "error loading the config file "
  370. "'%s'\n", configfile);
  371. else
  372. BIO_printf(bio_err, "error on line %ld of config file "
  373. "'%s'\n", errorline, configfile);
  374. }
  375. if (conf != NULL) {
  376. const char *p;
  377. BIO_printf(bio_err, "Using configuration from %s\n", configfile);
  378. p = NCONF_get_string(conf, NULL, ENV_OID_FILE);
  379. if (p != NULL) {
  380. BIO *oid_bio = BIO_new_file(p, "r");
  381. if (!oid_bio)
  382. ERR_print_errors(bio_err);
  383. else {
  384. OBJ_create_objects(oid_bio);
  385. BIO_free_all(oid_bio);
  386. }
  387. } else
  388. ERR_clear_error();
  389. if (!add_oid_section(bio_err, conf))
  390. ERR_print_errors(bio_err);
  391. }
  392. return conf;
  393. }
  394. /*
  395. * Query-related method definitions.
  396. */
  397. static int query_command(const char *data, char *digest, const EVP_MD *md,
  398. const char *policy, int no_nonce,
  399. int cert, const char *in, const char *out, int text)
  400. {
  401. int ret = 0;
  402. TS_REQ *query = NULL;
  403. BIO *in_bio = NULL;
  404. BIO *data_bio = NULL;
  405. BIO *out_bio = NULL;
  406. /* Build query object either from file or from scratch. */
  407. if (in != NULL) {
  408. if ((in_bio = BIO_new_file(in, "rb")) == NULL)
  409. goto end;
  410. query = d2i_TS_REQ_bio(in_bio, NULL);
  411. } else {
  412. /*
  413. * Open the file if no explicit digest bytes were specified.
  414. */
  415. if (!digest && !(data_bio = BIO_open_with_default(data, "rb", stdin)))
  416. goto end;
  417. /* Creating the query object. */
  418. query = create_query(data_bio, digest, md, policy, no_nonce, cert);
  419. /* Saving the random number generator state. */
  420. }
  421. if (query == NULL)
  422. goto end;
  423. /* Write query either in ASN.1 or in text format. */
  424. if ((out_bio = BIO_open_with_default(out, "wb", stdout)) == NULL)
  425. goto end;
  426. if (text) {
  427. /* Text output. */
  428. if (!TS_REQ_print_bio(out_bio, query))
  429. goto end;
  430. } else {
  431. /* ASN.1 output. */
  432. if (!i2d_TS_REQ_bio(out_bio, query))
  433. goto end;
  434. }
  435. ret = 1;
  436. end:
  437. ERR_print_errors(bio_err);
  438. /* Clean up. */
  439. BIO_free_all(in_bio);
  440. BIO_free_all(data_bio);
  441. BIO_free_all(out_bio);
  442. TS_REQ_free(query);
  443. return ret;
  444. }
  445. static BIO *BIO_open_with_default(const char *file, const char *mode,
  446. FILE *default_fp)
  447. {
  448. return file == NULL ? BIO_new_fp(default_fp, BIO_NOCLOSE)
  449. : BIO_new_file(file, mode);
  450. }
  451. static TS_REQ *create_query(BIO *data_bio, char *digest, const EVP_MD *md,
  452. const char *policy, int no_nonce, int cert)
  453. {
  454. int ret = 0;
  455. TS_REQ *ts_req = NULL;
  456. int len;
  457. TS_MSG_IMPRINT *msg_imprint = NULL;
  458. X509_ALGOR *algo = NULL;
  459. unsigned char *data = NULL;
  460. ASN1_OBJECT *policy_obj = NULL;
  461. ASN1_INTEGER *nonce_asn1 = NULL;
  462. /* Setting default message digest. */
  463. if (!md && !(md = EVP_get_digestbyname("sha1")))
  464. goto err;
  465. /* Creating request object. */
  466. if (!(ts_req = TS_REQ_new()))
  467. goto err;
  468. /* Setting version. */
  469. if (!TS_REQ_set_version(ts_req, 1))
  470. goto err;
  471. /* Creating and adding MSG_IMPRINT object. */
  472. if (!(msg_imprint = TS_MSG_IMPRINT_new()))
  473. goto err;
  474. /* Adding algorithm. */
  475. if (!(algo = X509_ALGOR_new()))
  476. goto err;
  477. if (!(algo->algorithm = OBJ_nid2obj(EVP_MD_type(md))))
  478. goto err;
  479. if (!(algo->parameter = ASN1_TYPE_new()))
  480. goto err;
  481. algo->parameter->type = V_ASN1_NULL;
  482. if (!TS_MSG_IMPRINT_set_algo(msg_imprint, algo))
  483. goto err;
  484. /* Adding message digest. */
  485. if ((len = create_digest(data_bio, digest, md, &data)) == 0)
  486. goto err;
  487. if (!TS_MSG_IMPRINT_set_msg(msg_imprint, data, len))
  488. goto err;
  489. if (!TS_REQ_set_msg_imprint(ts_req, msg_imprint))
  490. goto err;
  491. /* Setting policy if requested. */
  492. if (policy && !(policy_obj = txt2obj(policy)))
  493. goto err;
  494. if (policy_obj && !TS_REQ_set_policy_id(ts_req, policy_obj))
  495. goto err;
  496. /* Setting nonce if requested. */
  497. if (!no_nonce && !(nonce_asn1 = create_nonce(NONCE_LENGTH)))
  498. goto err;
  499. if (nonce_asn1 && !TS_REQ_set_nonce(ts_req, nonce_asn1))
  500. goto err;
  501. /* Setting certificate request flag if requested. */
  502. if (!TS_REQ_set_cert_req(ts_req, cert))
  503. goto err;
  504. ret = 1;
  505. err:
  506. if (!ret) {
  507. TS_REQ_free(ts_req);
  508. ts_req = NULL;
  509. BIO_printf(bio_err, "could not create query\n");
  510. }
  511. TS_MSG_IMPRINT_free(msg_imprint);
  512. X509_ALGOR_free(algo);
  513. OPENSSL_free(data);
  514. ASN1_OBJECT_free(policy_obj);
  515. ASN1_INTEGER_free(nonce_asn1);
  516. return ts_req;
  517. }
  518. static int create_digest(BIO *input, char *digest, const EVP_MD *md,
  519. unsigned char **md_value)
  520. {
  521. int md_value_len;
  522. md_value_len = EVP_MD_size(md);
  523. if (md_value_len < 0)
  524. goto err;
  525. if (input) {
  526. /* Digest must be computed from an input file. */
  527. EVP_MD_CTX md_ctx;
  528. unsigned char buffer[4096];
  529. int length;
  530. *md_value = OPENSSL_malloc(md_value_len);
  531. if (*md_value == 0)
  532. goto err;
  533. EVP_DigestInit(&md_ctx, md);
  534. while ((length = BIO_read(input, buffer, sizeof(buffer))) > 0) {
  535. EVP_DigestUpdate(&md_ctx, buffer, length);
  536. }
  537. EVP_DigestFinal(&md_ctx, *md_value, NULL);
  538. } else {
  539. /* Digest bytes are specified with digest. */
  540. long digest_len;
  541. *md_value = string_to_hex(digest, &digest_len);
  542. if (!*md_value || md_value_len != digest_len) {
  543. OPENSSL_free(*md_value);
  544. *md_value = NULL;
  545. BIO_printf(bio_err, "bad digest, %d bytes "
  546. "must be specified\n", md_value_len);
  547. goto err;
  548. }
  549. }
  550. return md_value_len;
  551. err:
  552. return 0;
  553. }
  554. static ASN1_INTEGER *create_nonce(int bits)
  555. {
  556. unsigned char buf[20];
  557. ASN1_INTEGER *nonce = NULL;
  558. int len = (bits - 1) / 8 + 1;
  559. int i;
  560. /* Generating random byte sequence. */
  561. if (len > (int)sizeof(buf))
  562. goto err;
  563. if (RAND_bytes(buf, len) <= 0)
  564. goto err;
  565. /* Find the first non-zero byte and creating ASN1_INTEGER object. */
  566. for (i = 0; i < len && !buf[i]; ++i) ;
  567. if (!(nonce = ASN1_INTEGER_new()))
  568. goto err;
  569. OPENSSL_free(nonce->data);
  570. /* Allocate at least one byte. */
  571. nonce->length = len - i;
  572. if (!(nonce->data = OPENSSL_malloc(nonce->length + 1)))
  573. goto err;
  574. memcpy(nonce->data, buf + i, nonce->length);
  575. return nonce;
  576. err:
  577. BIO_printf(bio_err, "could not create nonce\n");
  578. ASN1_INTEGER_free(nonce);
  579. return NULL;
  580. }
  581. /*
  582. * Reply-related method definitions.
  583. */
  584. static int reply_command(CONF *conf, char *section, char *engine,
  585. char *queryfile, char *passin, char *inkey,
  586. char *signer, char *chain, const char *policy,
  587. char *in, int token_in,
  588. char *out, int token_out, int text)
  589. {
  590. int ret = 0;
  591. TS_RESP *response = NULL;
  592. BIO *in_bio = NULL;
  593. BIO *query_bio = NULL;
  594. BIO *inkey_bio = NULL;
  595. BIO *signer_bio = NULL;
  596. BIO *out_bio = NULL;
  597. /* Build response object either from response or query. */
  598. if (in != NULL) {
  599. if ((in_bio = BIO_new_file(in, "rb")) == NULL)
  600. goto end;
  601. if (token_in) {
  602. /*
  603. * We have a ContentInfo (PKCS7) object, add 'granted' status
  604. * info around it.
  605. */
  606. response = read_PKCS7(in_bio);
  607. } else {
  608. /* We have a ready-made TS_RESP object. */
  609. response = d2i_TS_RESP_bio(in_bio, NULL);
  610. }
  611. } else {
  612. response = create_response(conf, section, engine, queryfile,
  613. passin, inkey, signer, chain, policy);
  614. if (response)
  615. BIO_printf(bio_err, "Response has been generated.\n");
  616. else
  617. BIO_printf(bio_err, "Response is not generated.\n");
  618. }
  619. if (response == NULL)
  620. goto end;
  621. /* Write response either in ASN.1 or text format. */
  622. if ((out_bio = BIO_open_with_default(out, "wb", stdout)) == NULL)
  623. goto end;
  624. if (text) {
  625. /* Text output. */
  626. if (token_out) {
  627. TS_TST_INFO *tst_info = TS_RESP_get_tst_info(response);
  628. if (!TS_TST_INFO_print_bio(out_bio, tst_info))
  629. goto end;
  630. } else {
  631. if (!TS_RESP_print_bio(out_bio, response))
  632. goto end;
  633. }
  634. } else {
  635. /* ASN.1 DER output. */
  636. if (token_out) {
  637. PKCS7 *token = TS_RESP_get_token(response);
  638. if (!i2d_PKCS7_bio(out_bio, token))
  639. goto end;
  640. } else {
  641. if (!i2d_TS_RESP_bio(out_bio, response))
  642. goto end;
  643. }
  644. }
  645. ret = 1;
  646. end:
  647. ERR_print_errors(bio_err);
  648. /* Clean up. */
  649. BIO_free_all(in_bio);
  650. BIO_free_all(query_bio);
  651. BIO_free_all(inkey_bio);
  652. BIO_free_all(signer_bio);
  653. BIO_free_all(out_bio);
  654. TS_RESP_free(response);
  655. return ret;
  656. }
  657. /* Reads a PKCS7 token and adds default 'granted' status info to it. */
  658. static TS_RESP *read_PKCS7(BIO *in_bio)
  659. {
  660. int ret = 0;
  661. PKCS7 *token = NULL;
  662. TS_TST_INFO *tst_info = NULL;
  663. TS_RESP *resp = NULL;
  664. TS_STATUS_INFO *si = NULL;
  665. /* Read PKCS7 object and extract the signed time stamp info. */
  666. if (!(token = d2i_PKCS7_bio(in_bio, NULL)))
  667. goto end;
  668. if (!(tst_info = PKCS7_to_TS_TST_INFO(token)))
  669. goto end;
  670. /* Creating response object. */
  671. if (!(resp = TS_RESP_new()))
  672. goto end;
  673. /* Create granted status info. */
  674. if (!(si = TS_STATUS_INFO_new()))
  675. goto end;
  676. if (!(ASN1_INTEGER_set(si->status, TS_STATUS_GRANTED)))
  677. goto end;
  678. if (!TS_RESP_set_status_info(resp, si))
  679. goto end;
  680. /* Setting encapsulated token. */
  681. TS_RESP_set_tst_info(resp, token, tst_info);
  682. token = NULL; /* Ownership is lost. */
  683. tst_info = NULL; /* Ownership is lost. */
  684. ret = 1;
  685. end:
  686. PKCS7_free(token);
  687. TS_TST_INFO_free(tst_info);
  688. if (!ret) {
  689. TS_RESP_free(resp);
  690. resp = NULL;
  691. }
  692. TS_STATUS_INFO_free(si);
  693. return resp;
  694. }
  695. static TS_RESP *create_response(CONF *conf, const char *section, char *engine,
  696. char *queryfile, char *passin, char *inkey,
  697. char *signer, char *chain, const char *policy)
  698. {
  699. int ret = 0;
  700. TS_RESP *response = NULL;
  701. BIO *query_bio = NULL;
  702. TS_RESP_CTX *resp_ctx = NULL;
  703. if (!(query_bio = BIO_new_file(queryfile, "rb")))
  704. goto end;
  705. /* Getting TSA configuration section. */
  706. if (!(section = TS_CONF_get_tsa_section(conf, section)))
  707. goto end;
  708. /* Setting up response generation context. */
  709. if (!(resp_ctx = TS_RESP_CTX_new()))
  710. goto end;
  711. /* Setting serial number provider callback. */
  712. if (!TS_CONF_set_serial(conf, section, serial_cb, resp_ctx))
  713. goto end;
  714. #ifndef OPENSSL_NO_ENGINE
  715. /* Setting default OpenSSL engine. */
  716. if (!TS_CONF_set_crypto_device(conf, section, engine))
  717. goto end;
  718. #endif
  719. /* Setting TSA signer certificate. */
  720. if (!TS_CONF_set_signer_cert(conf, section, signer, resp_ctx))
  721. goto end;
  722. /* Setting TSA signer certificate chain. */
  723. if (!TS_CONF_set_certs(conf, section, chain, resp_ctx))
  724. goto end;
  725. /* Setting TSA signer private key. */
  726. if (!TS_CONF_set_signer_key(conf, section, inkey, passin, resp_ctx))
  727. goto end;
  728. /* Setting default policy OID. */
  729. if (!TS_CONF_set_def_policy(conf, section, policy, resp_ctx))
  730. goto end;
  731. /* Setting acceptable policy OIDs. */
  732. if (!TS_CONF_set_policies(conf, section, resp_ctx))
  733. goto end;
  734. /* Setting the acceptable one-way hash algorithms. */
  735. if (!TS_CONF_set_digests(conf, section, resp_ctx))
  736. goto end;
  737. /* Setting guaranteed time stamp accuracy. */
  738. if (!TS_CONF_set_accuracy(conf, section, resp_ctx))
  739. goto end;
  740. /* Setting the precision of the time. */
  741. if (!TS_CONF_set_clock_precision_digits(conf, section, resp_ctx))
  742. goto end;
  743. /* Setting the ordering flaf if requested. */
  744. if (!TS_CONF_set_ordering(conf, section, resp_ctx))
  745. goto end;
  746. /* Setting the TSA name required flag if requested. */
  747. if (!TS_CONF_set_tsa_name(conf, section, resp_ctx))
  748. goto end;
  749. /* Setting the ESS cert id chain flag if requested. */
  750. if (!TS_CONF_set_ess_cert_id_chain(conf, section, resp_ctx))
  751. goto end;
  752. /* Creating the response. */
  753. if (!(response = TS_RESP_create_response(resp_ctx, query_bio)))
  754. goto end;
  755. ret = 1;
  756. end:
  757. if (!ret) {
  758. TS_RESP_free(response);
  759. response = NULL;
  760. }
  761. TS_RESP_CTX_free(resp_ctx);
  762. BIO_free_all(query_bio);
  763. return response;
  764. }
  765. static ASN1_INTEGER *MS_CALLBACK serial_cb(TS_RESP_CTX *ctx, void *data)
  766. {
  767. const char *serial_file = (const char *)data;
  768. ASN1_INTEGER *serial = next_serial(serial_file);
  769. if (!serial) {
  770. TS_RESP_CTX_set_status_info(ctx, TS_STATUS_REJECTION,
  771. "Error during serial number "
  772. "generation.");
  773. TS_RESP_CTX_add_failure_info(ctx, TS_INFO_ADD_INFO_NOT_AVAILABLE);
  774. } else
  775. save_ts_serial(serial_file, serial);
  776. return serial;
  777. }
  778. static ASN1_INTEGER *next_serial(const char *serialfile)
  779. {
  780. int ret = 0;
  781. BIO *in = NULL;
  782. ASN1_INTEGER *serial = NULL;
  783. BIGNUM *bn = NULL;
  784. if (!(serial = ASN1_INTEGER_new()))
  785. goto err;
  786. if (!(in = BIO_new_file(serialfile, "r"))) {
  787. ERR_clear_error();
  788. BIO_printf(bio_err, "Warning: could not open file %s for "
  789. "reading, using serial number: 1\n", serialfile);
  790. if (!ASN1_INTEGER_set(serial, 1))
  791. goto err;
  792. } else {
  793. char buf[1024];
  794. if (!a2i_ASN1_INTEGER(in, serial, buf, sizeof(buf))) {
  795. BIO_printf(bio_err, "unable to load number from %s\n",
  796. serialfile);
  797. goto err;
  798. }
  799. if (!(bn = ASN1_INTEGER_to_BN(serial, NULL)))
  800. goto err;
  801. ASN1_INTEGER_free(serial);
  802. serial = NULL;
  803. if (!BN_add_word(bn, 1))
  804. goto err;
  805. if (!(serial = BN_to_ASN1_INTEGER(bn, NULL)))
  806. goto err;
  807. }
  808. ret = 1;
  809. err:
  810. if (!ret) {
  811. ASN1_INTEGER_free(serial);
  812. serial = NULL;
  813. }
  814. BIO_free_all(in);
  815. BN_free(bn);
  816. return serial;
  817. }
  818. static int save_ts_serial(const char *serialfile, ASN1_INTEGER *serial)
  819. {
  820. int ret = 0;
  821. BIO *out = NULL;
  822. if (!(out = BIO_new_file(serialfile, "w")))
  823. goto err;
  824. if (i2a_ASN1_INTEGER(out, serial) <= 0)
  825. goto err;
  826. if (BIO_puts(out, "\n") <= 0)
  827. goto err;
  828. ret = 1;
  829. err:
  830. if (!ret)
  831. BIO_printf(bio_err, "could not save serial number to %s\n",
  832. serialfile);
  833. BIO_free_all(out);
  834. return ret;
  835. }
  836. /*
  837. * Verify-related method definitions.
  838. */
  839. static int verify_command(char *data, char *digest, char *queryfile,
  840. char *in, int token_in,
  841. char *ca_path, char *ca_file, char *untrusted)
  842. {
  843. BIO *in_bio = NULL;
  844. PKCS7 *token = NULL;
  845. TS_RESP *response = NULL;
  846. TS_VERIFY_CTX *verify_ctx = NULL;
  847. int ret = 0;
  848. /* Decode the token (PKCS7) or response (TS_RESP) files. */
  849. if (!(in_bio = BIO_new_file(in, "rb")))
  850. goto end;
  851. if (token_in) {
  852. if (!(token = d2i_PKCS7_bio(in_bio, NULL)))
  853. goto end;
  854. } else {
  855. if (!(response = d2i_TS_RESP_bio(in_bio, NULL)))
  856. goto end;
  857. }
  858. if (!(verify_ctx = create_verify_ctx(data, digest, queryfile,
  859. ca_path, ca_file, untrusted)))
  860. goto end;
  861. /* Checking the token or response against the request. */
  862. ret = token_in ?
  863. TS_RESP_verify_token(verify_ctx, token) :
  864. TS_RESP_verify_response(verify_ctx, response);
  865. end:
  866. printf("Verification: ");
  867. if (ret)
  868. printf("OK\n");
  869. else {
  870. printf("FAILED\n");
  871. /* Print errors, if there are any. */
  872. ERR_print_errors(bio_err);
  873. }
  874. /* Clean up. */
  875. BIO_free_all(in_bio);
  876. PKCS7_free(token);
  877. TS_RESP_free(response);
  878. TS_VERIFY_CTX_free(verify_ctx);
  879. return ret;
  880. }
  881. static TS_VERIFY_CTX *create_verify_ctx(char *data, char *digest,
  882. char *queryfile,
  883. char *ca_path, char *ca_file,
  884. char *untrusted)
  885. {
  886. TS_VERIFY_CTX *ctx = NULL;
  887. BIO *input = NULL;
  888. TS_REQ *request = NULL;
  889. int ret = 0;
  890. if (data != NULL || digest != NULL) {
  891. if (!(ctx = TS_VERIFY_CTX_new()))
  892. goto err;
  893. ctx->flags = TS_VFY_VERSION | TS_VFY_SIGNER;
  894. if (data != NULL) {
  895. ctx->flags |= TS_VFY_DATA;
  896. if (!(ctx->data = BIO_new_file(data, "rb")))
  897. goto err;
  898. } else if (digest != NULL) {
  899. long imprint_len;
  900. ctx->flags |= TS_VFY_IMPRINT;
  901. if (!(ctx->imprint = string_to_hex(digest, &imprint_len))) {
  902. BIO_printf(bio_err, "invalid digest string\n");
  903. goto err;
  904. }
  905. ctx->imprint_len = imprint_len;
  906. }
  907. } else if (queryfile != NULL) {
  908. /*
  909. * The request has just to be read, decoded and converted to a verify
  910. * context object.
  911. */
  912. if (!(input = BIO_new_file(queryfile, "rb")))
  913. goto err;
  914. if (!(request = d2i_TS_REQ_bio(input, NULL)))
  915. goto err;
  916. if (!(ctx = TS_REQ_to_TS_VERIFY_CTX(request, NULL)))
  917. goto err;
  918. } else
  919. return NULL;
  920. /* Add the signature verification flag and arguments. */
  921. ctx->flags |= TS_VFY_SIGNATURE;
  922. /* Initialising the X509_STORE object. */
  923. if (!(ctx->store = create_cert_store(ca_path, ca_file)))
  924. goto err;
  925. /* Loading untrusted certificates. */
  926. if (untrusted && !(ctx->certs = TS_CONF_load_certs(untrusted)))
  927. goto err;
  928. ret = 1;
  929. err:
  930. if (!ret) {
  931. TS_VERIFY_CTX_free(ctx);
  932. ctx = NULL;
  933. }
  934. BIO_free_all(input);
  935. TS_REQ_free(request);
  936. return ctx;
  937. }
  938. static X509_STORE *create_cert_store(char *ca_path, char *ca_file)
  939. {
  940. X509_STORE *cert_ctx = NULL;
  941. X509_LOOKUP *lookup = NULL;
  942. int i;
  943. /* Creating the X509_STORE object. */
  944. cert_ctx = X509_STORE_new();
  945. /* Setting the callback for certificate chain verification. */
  946. X509_STORE_set_verify_cb(cert_ctx, verify_cb);
  947. /* Adding a trusted certificate directory source. */
  948. if (ca_path) {
  949. lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir());
  950. if (lookup == NULL) {
  951. BIO_printf(bio_err, "memory allocation failure\n");
  952. goto err;
  953. }
  954. i = X509_LOOKUP_add_dir(lookup, ca_path, X509_FILETYPE_PEM);
  955. if (!i) {
  956. BIO_printf(bio_err, "Error loading directory %s\n", ca_path);
  957. goto err;
  958. }
  959. }
  960. /* Adding a trusted certificate file source. */
  961. if (ca_file) {
  962. lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
  963. if (lookup == NULL) {
  964. BIO_printf(bio_err, "memory allocation failure\n");
  965. goto err;
  966. }
  967. i = X509_LOOKUP_load_file(lookup, ca_file, X509_FILETYPE_PEM);
  968. if (!i) {
  969. BIO_printf(bio_err, "Error loading file %s\n", ca_file);
  970. goto err;
  971. }
  972. }
  973. return cert_ctx;
  974. err:
  975. X509_STORE_free(cert_ctx);
  976. return NULL;
  977. }
  978. static int MS_CALLBACK verify_cb(int ok, X509_STORE_CTX *ctx)
  979. {
  980. /*-
  981. char buf[256];
  982. if (!ok)
  983. {
  984. X509_NAME_oneline(X509_get_subject_name(ctx->current_cert),
  985. buf, sizeof(buf));
  986. printf("%s\n", buf);
  987. printf("error %d at %d depth lookup: %s\n",
  988. ctx->error, ctx->error_depth,
  989. X509_verify_cert_error_string(ctx->error));
  990. }
  991. */
  992. return ok;
  993. }