ts.c 29 KB

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