ts.c 29 KB

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