2
0

ts.c 29 KB

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