ts_conf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /* crypto/ts/ts_conf.c */
  2. /*
  3. * Written by Zoltan Glozik (zglozik@stones.com) for the OpenSSL project
  4. * 2002.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <string.h>
  60. #include <openssl/crypto.h>
  61. #include "cryptlib.h"
  62. #include <openssl/pem.h>
  63. #ifndef OPENSSL_NO_ENGINE
  64. # include <openssl/engine.h>
  65. #endif
  66. #include <openssl/ts.h>
  67. /* Macro definitions for the configuration file. */
  68. #define BASE_SECTION "tsa"
  69. #define ENV_DEFAULT_TSA "default_tsa"
  70. #define ENV_SERIAL "serial"
  71. #define ENV_CRYPTO_DEVICE "crypto_device"
  72. #define ENV_SIGNER_CERT "signer_cert"
  73. #define ENV_CERTS "certs"
  74. #define ENV_SIGNER_KEY "signer_key"
  75. #define ENV_DEFAULT_POLICY "default_policy"
  76. #define ENV_OTHER_POLICIES "other_policies"
  77. #define ENV_DIGESTS "digests"
  78. #define ENV_ACCURACY "accuracy"
  79. #define ENV_ORDERING "ordering"
  80. #define ENV_TSA_NAME "tsa_name"
  81. #define ENV_ESS_CERT_ID_CHAIN "ess_cert_id_chain"
  82. #define ENV_VALUE_SECS "secs"
  83. #define ENV_VALUE_MILLISECS "millisecs"
  84. #define ENV_VALUE_MICROSECS "microsecs"
  85. #define ENV_CLOCK_PRECISION_DIGITS "clock_precision_digits"
  86. #define ENV_VALUE_YES "yes"
  87. #define ENV_VALUE_NO "no"
  88. /* Function definitions for certificate and key loading. */
  89. X509 *TS_CONF_load_cert(const char *file)
  90. {
  91. BIO *cert = NULL;
  92. X509 *x = NULL;
  93. if ((cert = BIO_new_file(file, "r")) == NULL)
  94. goto end;
  95. x = PEM_read_bio_X509_AUX(cert, NULL, NULL, NULL);
  96. end:
  97. if (x == NULL)
  98. fprintf(stderr, "unable to load certificate: %s\n", file);
  99. BIO_free(cert);
  100. return x;
  101. }
  102. STACK_OF(X509) *TS_CONF_load_certs(const char *file)
  103. {
  104. BIO *certs = NULL;
  105. STACK_OF(X509) *othercerts = NULL;
  106. STACK_OF(X509_INFO) *allcerts = NULL;
  107. int i;
  108. if (!(certs = BIO_new_file(file, "r")))
  109. goto end;
  110. if (!(othercerts = sk_X509_new_null()))
  111. goto end;
  112. allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL);
  113. for (i = 0; i < sk_X509_INFO_num(allcerts); i++) {
  114. X509_INFO *xi = sk_X509_INFO_value(allcerts, i);
  115. if (xi->x509) {
  116. sk_X509_push(othercerts, xi->x509);
  117. xi->x509 = NULL;
  118. }
  119. }
  120. end:
  121. if (othercerts == NULL)
  122. fprintf(stderr, "unable to load certificates: %s\n", file);
  123. sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
  124. BIO_free(certs);
  125. return othercerts;
  126. }
  127. EVP_PKEY *TS_CONF_load_key(const char *file, const char *pass)
  128. {
  129. BIO *key = NULL;
  130. EVP_PKEY *pkey = NULL;
  131. if (!(key = BIO_new_file(file, "r")))
  132. goto end;
  133. pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, (char *)pass);
  134. end:
  135. if (pkey == NULL)
  136. fprintf(stderr, "unable to load private key: %s\n", file);
  137. BIO_free(key);
  138. return pkey;
  139. }
  140. /* Function definitions for handling configuration options. */
  141. static void TS_CONF_lookup_fail(const char *name, const char *tag)
  142. {
  143. fprintf(stderr, "variable lookup failed for %s::%s\n", name, tag);
  144. }
  145. static void TS_CONF_invalid(const char *name, const char *tag)
  146. {
  147. fprintf(stderr, "invalid variable value for %s::%s\n", name, tag);
  148. }
  149. const char *TS_CONF_get_tsa_section(CONF *conf, const char *section)
  150. {
  151. if (!section) {
  152. section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_TSA);
  153. if (!section)
  154. TS_CONF_lookup_fail(BASE_SECTION, ENV_DEFAULT_TSA);
  155. }
  156. return section;
  157. }
  158. int TS_CONF_set_serial(CONF *conf, const char *section, TS_serial_cb cb,
  159. TS_RESP_CTX *ctx)
  160. {
  161. int ret = 0;
  162. char *serial = NCONF_get_string(conf, section, ENV_SERIAL);
  163. if (!serial) {
  164. TS_CONF_lookup_fail(section, ENV_SERIAL);
  165. goto err;
  166. }
  167. TS_RESP_CTX_set_serial_cb(ctx, cb, serial);
  168. ret = 1;
  169. err:
  170. return ret;
  171. }
  172. #ifndef OPENSSL_NO_ENGINE
  173. int TS_CONF_set_crypto_device(CONF *conf, const char *section,
  174. const char *device)
  175. {
  176. int ret = 0;
  177. if (!device)
  178. device = NCONF_get_string(conf, section, ENV_CRYPTO_DEVICE);
  179. if (device && !TS_CONF_set_default_engine(device)) {
  180. TS_CONF_invalid(section, ENV_CRYPTO_DEVICE);
  181. goto err;
  182. }
  183. ret = 1;
  184. err:
  185. return ret;
  186. }
  187. int TS_CONF_set_default_engine(const char *name)
  188. {
  189. ENGINE *e = NULL;
  190. int ret = 0;
  191. /* Leave the default if builtin specified. */
  192. if (strcmp(name, "builtin") == 0)
  193. return 1;
  194. if (!(e = ENGINE_by_id(name)))
  195. goto err;
  196. /* Enable the use of the NCipher HSM for forked children. */
  197. if (strcmp(name, "chil") == 0)
  198. ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
  199. /* All the operations are going to be carried out by the engine. */
  200. if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
  201. goto err;
  202. ret = 1;
  203. err:
  204. if (!ret) {
  205. TSerr(TS_F_TS_CONF_SET_DEFAULT_ENGINE, TS_R_COULD_NOT_SET_ENGINE);
  206. ERR_add_error_data(2, "engine:", name);
  207. }
  208. if (e)
  209. ENGINE_free(e);
  210. return ret;
  211. }
  212. #endif
  213. int TS_CONF_set_signer_cert(CONF *conf, const char *section,
  214. const char *cert, TS_RESP_CTX *ctx)
  215. {
  216. int ret = 0;
  217. X509 *cert_obj = NULL;
  218. if (!cert)
  219. cert = NCONF_get_string(conf, section, ENV_SIGNER_CERT);
  220. if (!cert) {
  221. TS_CONF_lookup_fail(section, ENV_SIGNER_CERT);
  222. goto err;
  223. }
  224. if (!(cert_obj = TS_CONF_load_cert(cert)))
  225. goto err;
  226. if (!TS_RESP_CTX_set_signer_cert(ctx, cert_obj))
  227. goto err;
  228. ret = 1;
  229. err:
  230. X509_free(cert_obj);
  231. return ret;
  232. }
  233. int TS_CONF_set_certs(CONF *conf, const char *section, const char *certs,
  234. TS_RESP_CTX *ctx)
  235. {
  236. int ret = 0;
  237. STACK_OF(X509) *certs_obj = NULL;
  238. if (!certs)
  239. certs = NCONF_get_string(conf, section, ENV_CERTS);
  240. /* Certificate chain is optional. */
  241. if (!certs)
  242. goto end;
  243. if (!(certs_obj = TS_CONF_load_certs(certs)))
  244. goto err;
  245. if (!TS_RESP_CTX_set_certs(ctx, certs_obj))
  246. goto err;
  247. end:
  248. ret = 1;
  249. err:
  250. sk_X509_pop_free(certs_obj, X509_free);
  251. return ret;
  252. }
  253. int TS_CONF_set_signer_key(CONF *conf, const char *section,
  254. const char *key, const char *pass,
  255. TS_RESP_CTX *ctx)
  256. {
  257. int ret = 0;
  258. EVP_PKEY *key_obj = NULL;
  259. if (!key)
  260. key = NCONF_get_string(conf, section, ENV_SIGNER_KEY);
  261. if (!key) {
  262. TS_CONF_lookup_fail(section, ENV_SIGNER_KEY);
  263. goto err;
  264. }
  265. if (!(key_obj = TS_CONF_load_key(key, pass)))
  266. goto err;
  267. if (!TS_RESP_CTX_set_signer_key(ctx, key_obj))
  268. goto err;
  269. ret = 1;
  270. err:
  271. EVP_PKEY_free(key_obj);
  272. return ret;
  273. }
  274. int TS_CONF_set_def_policy(CONF *conf, const char *section,
  275. const char *policy, TS_RESP_CTX *ctx)
  276. {
  277. int ret = 0;
  278. ASN1_OBJECT *policy_obj = NULL;
  279. if (!policy)
  280. policy = NCONF_get_string(conf, section, ENV_DEFAULT_POLICY);
  281. if (!policy) {
  282. TS_CONF_lookup_fail(section, ENV_DEFAULT_POLICY);
  283. goto err;
  284. }
  285. if (!(policy_obj = OBJ_txt2obj(policy, 0))) {
  286. TS_CONF_invalid(section, ENV_DEFAULT_POLICY);
  287. goto err;
  288. }
  289. if (!TS_RESP_CTX_set_def_policy(ctx, policy_obj))
  290. goto err;
  291. ret = 1;
  292. err:
  293. ASN1_OBJECT_free(policy_obj);
  294. return ret;
  295. }
  296. int TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx)
  297. {
  298. int ret = 0;
  299. int i;
  300. STACK_OF(CONF_VALUE) *list = NULL;
  301. char *policies = NCONF_get_string(conf, section,
  302. ENV_OTHER_POLICIES);
  303. /* If no other policy is specified, that's fine. */
  304. if (policies && !(list = X509V3_parse_list(policies))) {
  305. TS_CONF_invalid(section, ENV_OTHER_POLICIES);
  306. goto err;
  307. }
  308. for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
  309. CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
  310. const char *extval = val->value ? val->value : val->name;
  311. ASN1_OBJECT *objtmp;
  312. if (!(objtmp = OBJ_txt2obj(extval, 0))) {
  313. TS_CONF_invalid(section, ENV_OTHER_POLICIES);
  314. goto err;
  315. }
  316. if (!TS_RESP_CTX_add_policy(ctx, objtmp))
  317. goto err;
  318. ASN1_OBJECT_free(objtmp);
  319. }
  320. ret = 1;
  321. err:
  322. sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
  323. return ret;
  324. }
  325. int TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx)
  326. {
  327. int ret = 0;
  328. int i;
  329. STACK_OF(CONF_VALUE) *list = NULL;
  330. char *digests = NCONF_get_string(conf, section, ENV_DIGESTS);
  331. if (!digests) {
  332. TS_CONF_lookup_fail(section, ENV_DIGESTS);
  333. goto err;
  334. }
  335. if (!(list = X509V3_parse_list(digests))) {
  336. TS_CONF_invalid(section, ENV_DIGESTS);
  337. goto err;
  338. }
  339. if (sk_CONF_VALUE_num(list) == 0) {
  340. TS_CONF_invalid(section, ENV_DIGESTS);
  341. goto err;
  342. }
  343. for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
  344. CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
  345. const char *extval = val->value ? val->value : val->name;
  346. const EVP_MD *md;
  347. if (!(md = EVP_get_digestbyname(extval))) {
  348. TS_CONF_invalid(section, ENV_DIGESTS);
  349. goto err;
  350. }
  351. if (!TS_RESP_CTX_add_md(ctx, md))
  352. goto err;
  353. }
  354. ret = 1;
  355. err:
  356. sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
  357. return ret;
  358. }
  359. int TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx)
  360. {
  361. int ret = 0;
  362. int i;
  363. int secs = 0, millis = 0, micros = 0;
  364. STACK_OF(CONF_VALUE) *list = NULL;
  365. char *accuracy = NCONF_get_string(conf, section, ENV_ACCURACY);
  366. if (accuracy && !(list = X509V3_parse_list(accuracy))) {
  367. TS_CONF_invalid(section, ENV_ACCURACY);
  368. goto err;
  369. }
  370. for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
  371. CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
  372. if (strcmp(val->name, ENV_VALUE_SECS) == 0) {
  373. if (val->value)
  374. secs = atoi(val->value);
  375. } else if (strcmp(val->name, ENV_VALUE_MILLISECS) == 0) {
  376. if (val->value)
  377. millis = atoi(val->value);
  378. } else if (strcmp(val->name, ENV_VALUE_MICROSECS) == 0) {
  379. if (val->value)
  380. micros = atoi(val->value);
  381. } else {
  382. TS_CONF_invalid(section, ENV_ACCURACY);
  383. goto err;
  384. }
  385. }
  386. if (!TS_RESP_CTX_set_accuracy(ctx, secs, millis, micros))
  387. goto err;
  388. ret = 1;
  389. err:
  390. sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
  391. return ret;
  392. }
  393. int TS_CONF_set_clock_precision_digits(CONF *conf, const char *section,
  394. TS_RESP_CTX *ctx)
  395. {
  396. int ret = 0;
  397. long digits = 0;
  398. /*
  399. * If not specified, set the default value to 0, i.e. sec precision
  400. */
  401. if (!NCONF_get_number_e(conf, section, ENV_CLOCK_PRECISION_DIGITS,
  402. &digits))
  403. digits = 0;
  404. if (digits < 0 || digits > TS_MAX_CLOCK_PRECISION_DIGITS) {
  405. TS_CONF_invalid(section, ENV_CLOCK_PRECISION_DIGITS);
  406. goto err;
  407. }
  408. if (!TS_RESP_CTX_set_clock_precision_digits(ctx, digits))
  409. goto err;
  410. return 1;
  411. err:
  412. return ret;
  413. }
  414. static int TS_CONF_add_flag(CONF *conf, const char *section,
  415. const char *field, int flag, TS_RESP_CTX *ctx)
  416. {
  417. /* Default is false. */
  418. const char *value = NCONF_get_string(conf, section, field);
  419. if (value) {
  420. if (strcmp(value, ENV_VALUE_YES) == 0)
  421. TS_RESP_CTX_add_flags(ctx, flag);
  422. else if (strcmp(value, ENV_VALUE_NO) != 0) {
  423. TS_CONF_invalid(section, field);
  424. return 0;
  425. }
  426. }
  427. return 1;
  428. }
  429. int TS_CONF_set_ordering(CONF *conf, const char *section, TS_RESP_CTX *ctx)
  430. {
  431. return TS_CONF_add_flag(conf, section, ENV_ORDERING, TS_ORDERING, ctx);
  432. }
  433. int TS_CONF_set_tsa_name(CONF *conf, const char *section, TS_RESP_CTX *ctx)
  434. {
  435. return TS_CONF_add_flag(conf, section, ENV_TSA_NAME, TS_TSA_NAME, ctx);
  436. }
  437. int TS_CONF_set_ess_cert_id_chain(CONF *conf, const char *section,
  438. TS_RESP_CTX *ctx)
  439. {
  440. return TS_CONF_add_flag(conf, section, ENV_ESS_CERT_ID_CHAIN,
  441. TS_ESS_CERT_ID_CHAIN, ctx);
  442. }