ts_conf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/crypto.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/pem.h>
  13. #include <openssl/engine.h>
  14. #include <openssl/ts.h>
  15. /* Macro definitions for the configuration file. */
  16. #define BASE_SECTION "tsa"
  17. #define ENV_DEFAULT_TSA "default_tsa"
  18. #define ENV_SERIAL "serial"
  19. #define ENV_CRYPTO_DEVICE "crypto_device"
  20. #define ENV_SIGNER_CERT "signer_cert"
  21. #define ENV_CERTS "certs"
  22. #define ENV_SIGNER_KEY "signer_key"
  23. #define ENV_SIGNER_DIGEST "signer_digest"
  24. #define ENV_DEFAULT_POLICY "default_policy"
  25. #define ENV_OTHER_POLICIES "other_policies"
  26. #define ENV_DIGESTS "digests"
  27. #define ENV_ACCURACY "accuracy"
  28. #define ENV_ORDERING "ordering"
  29. #define ENV_TSA_NAME "tsa_name"
  30. #define ENV_ESS_CERT_ID_CHAIN "ess_cert_id_chain"
  31. #define ENV_VALUE_SECS "secs"
  32. #define ENV_VALUE_MILLISECS "millisecs"
  33. #define ENV_VALUE_MICROSECS "microsecs"
  34. #define ENV_CLOCK_PRECISION_DIGITS "clock_precision_digits"
  35. #define ENV_VALUE_YES "yes"
  36. #define ENV_VALUE_NO "no"
  37. #define ENV_ESS_CERT_ID_ALG "ess_cert_id_alg"
  38. /* Function definitions for certificate and key loading. */
  39. X509 *TS_CONF_load_cert(const char *file)
  40. {
  41. BIO *cert = NULL;
  42. X509 *x = NULL;
  43. if ((cert = BIO_new_file(file, "r")) == NULL)
  44. goto end;
  45. x = PEM_read_bio_X509_AUX(cert, NULL, NULL, NULL);
  46. end:
  47. if (x == NULL)
  48. TSerr(TS_F_TS_CONF_LOAD_CERT, TS_R_CANNOT_LOAD_CERT);
  49. BIO_free(cert);
  50. return x;
  51. }
  52. STACK_OF(X509) *TS_CONF_load_certs(const char *file)
  53. {
  54. BIO *certs = NULL;
  55. STACK_OF(X509) *othercerts = NULL;
  56. STACK_OF(X509_INFO) *allcerts = NULL;
  57. int i;
  58. if ((certs = BIO_new_file(file, "r")) == NULL)
  59. goto end;
  60. if ((othercerts = sk_X509_new_null()) == NULL)
  61. goto end;
  62. allcerts = PEM_X509_INFO_read_bio(certs, NULL, NULL, NULL);
  63. for (i = 0; i < sk_X509_INFO_num(allcerts); i++) {
  64. X509_INFO *xi = sk_X509_INFO_value(allcerts, i);
  65. if (xi->x509) {
  66. sk_X509_push(othercerts, xi->x509);
  67. xi->x509 = NULL;
  68. }
  69. }
  70. end:
  71. if (othercerts == NULL)
  72. TSerr(TS_F_TS_CONF_LOAD_CERTS, TS_R_CANNOT_LOAD_CERT);
  73. sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
  74. BIO_free(certs);
  75. return othercerts;
  76. }
  77. EVP_PKEY *TS_CONF_load_key(const char *file, const char *pass)
  78. {
  79. BIO *key = NULL;
  80. EVP_PKEY *pkey = NULL;
  81. if ((key = BIO_new_file(file, "r")) == NULL)
  82. goto end;
  83. pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, (char *)pass);
  84. end:
  85. if (pkey == NULL)
  86. TSerr(TS_F_TS_CONF_LOAD_KEY, TS_R_CANNOT_LOAD_KEY);
  87. BIO_free(key);
  88. return pkey;
  89. }
  90. /* Function definitions for handling configuration options. */
  91. static void ts_CONF_lookup_fail(const char *name, const char *tag)
  92. {
  93. TSerr(TS_F_TS_CONF_LOOKUP_FAIL, TS_R_VAR_LOOKUP_FAILURE);
  94. ERR_add_error_data(3, name, "::", tag);
  95. }
  96. static void ts_CONF_invalid(const char *name, const char *tag)
  97. {
  98. TSerr(TS_F_TS_CONF_INVALID, TS_R_VAR_BAD_VALUE);
  99. ERR_add_error_data(3, name, "::", tag);
  100. }
  101. const char *TS_CONF_get_tsa_section(CONF *conf, const char *section)
  102. {
  103. if (!section) {
  104. section = NCONF_get_string(conf, BASE_SECTION, ENV_DEFAULT_TSA);
  105. if (!section)
  106. ts_CONF_lookup_fail(BASE_SECTION, ENV_DEFAULT_TSA);
  107. }
  108. return section;
  109. }
  110. int TS_CONF_set_serial(CONF *conf, const char *section, TS_serial_cb cb,
  111. TS_RESP_CTX *ctx)
  112. {
  113. int ret = 0;
  114. char *serial = NCONF_get_string(conf, section, ENV_SERIAL);
  115. if (!serial) {
  116. ts_CONF_lookup_fail(section, ENV_SERIAL);
  117. goto err;
  118. }
  119. TS_RESP_CTX_set_serial_cb(ctx, cb, serial);
  120. ret = 1;
  121. err:
  122. return ret;
  123. }
  124. #ifndef OPENSSL_NO_ENGINE
  125. int TS_CONF_set_crypto_device(CONF *conf, const char *section,
  126. const char *device)
  127. {
  128. int ret = 0;
  129. if (device == NULL)
  130. device = NCONF_get_string(conf, section, ENV_CRYPTO_DEVICE);
  131. if (device && !TS_CONF_set_default_engine(device)) {
  132. ts_CONF_invalid(section, ENV_CRYPTO_DEVICE);
  133. goto err;
  134. }
  135. ret = 1;
  136. err:
  137. return ret;
  138. }
  139. int TS_CONF_set_default_engine(const char *name)
  140. {
  141. ENGINE *e = NULL;
  142. int ret = 0;
  143. if (strcmp(name, "builtin") == 0)
  144. return 1;
  145. if ((e = ENGINE_by_id(name)) == NULL)
  146. goto err;
  147. if (strcmp(name, "chil") == 0)
  148. ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
  149. if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
  150. goto err;
  151. ret = 1;
  152. err:
  153. if (!ret) {
  154. TSerr(TS_F_TS_CONF_SET_DEFAULT_ENGINE, TS_R_COULD_NOT_SET_ENGINE);
  155. ERR_add_error_data(2, "engine:", name);
  156. }
  157. ENGINE_free(e);
  158. return ret;
  159. }
  160. #endif
  161. int TS_CONF_set_signer_cert(CONF *conf, const char *section,
  162. const char *cert, TS_RESP_CTX *ctx)
  163. {
  164. int ret = 0;
  165. X509 *cert_obj = NULL;
  166. if (cert == NULL) {
  167. cert = NCONF_get_string(conf, section, ENV_SIGNER_CERT);
  168. if (cert == NULL) {
  169. ts_CONF_lookup_fail(section, ENV_SIGNER_CERT);
  170. goto err;
  171. }
  172. }
  173. if ((cert_obj = TS_CONF_load_cert(cert)) == NULL)
  174. goto err;
  175. if (!TS_RESP_CTX_set_signer_cert(ctx, cert_obj))
  176. goto err;
  177. ret = 1;
  178. err:
  179. X509_free(cert_obj);
  180. return ret;
  181. }
  182. int TS_CONF_set_certs(CONF *conf, const char *section, const char *certs,
  183. TS_RESP_CTX *ctx)
  184. {
  185. int ret = 0;
  186. STACK_OF(X509) *certs_obj = NULL;
  187. if (certs == NULL) {
  188. /* Certificate chain is optional. */
  189. if ((certs = NCONF_get_string(conf, section, ENV_CERTS)) == NULL)
  190. goto end;
  191. }
  192. if ((certs_obj = TS_CONF_load_certs(certs)) == NULL)
  193. goto err;
  194. if (!TS_RESP_CTX_set_certs(ctx, certs_obj))
  195. goto err;
  196. end:
  197. ret = 1;
  198. err:
  199. sk_X509_pop_free(certs_obj, X509_free);
  200. return ret;
  201. }
  202. int TS_CONF_set_signer_key(CONF *conf, const char *section,
  203. const char *key, const char *pass,
  204. TS_RESP_CTX *ctx)
  205. {
  206. int ret = 0;
  207. EVP_PKEY *key_obj = NULL;
  208. if (!key)
  209. key = NCONF_get_string(conf, section, ENV_SIGNER_KEY);
  210. if (!key) {
  211. ts_CONF_lookup_fail(section, ENV_SIGNER_KEY);
  212. goto err;
  213. }
  214. if ((key_obj = TS_CONF_load_key(key, pass)) == NULL)
  215. goto err;
  216. if (!TS_RESP_CTX_set_signer_key(ctx, key_obj))
  217. goto err;
  218. ret = 1;
  219. err:
  220. EVP_PKEY_free(key_obj);
  221. return ret;
  222. }
  223. int TS_CONF_set_signer_digest(CONF *conf, const char *section,
  224. const char *md, TS_RESP_CTX *ctx)
  225. {
  226. int ret = 0;
  227. const EVP_MD *sign_md = NULL;
  228. if (md == NULL)
  229. md = NCONF_get_string(conf, section, ENV_SIGNER_DIGEST);
  230. if (md == NULL) {
  231. ts_CONF_lookup_fail(section, ENV_SIGNER_DIGEST);
  232. goto err;
  233. }
  234. sign_md = EVP_get_digestbyname(md);
  235. if (sign_md == NULL) {
  236. ts_CONF_invalid(section, ENV_SIGNER_DIGEST);
  237. goto err;
  238. }
  239. if (!TS_RESP_CTX_set_signer_digest(ctx, sign_md))
  240. goto err;
  241. ret = 1;
  242. err:
  243. return ret;
  244. }
  245. int TS_CONF_set_def_policy(CONF *conf, const char *section,
  246. const char *policy, TS_RESP_CTX *ctx)
  247. {
  248. int ret = 0;
  249. ASN1_OBJECT *policy_obj = NULL;
  250. if (policy == NULL)
  251. policy = NCONF_get_string(conf, section, ENV_DEFAULT_POLICY);
  252. if (policy == NULL) {
  253. ts_CONF_lookup_fail(section, ENV_DEFAULT_POLICY);
  254. goto err;
  255. }
  256. if ((policy_obj = OBJ_txt2obj(policy, 0)) == NULL) {
  257. ts_CONF_invalid(section, ENV_DEFAULT_POLICY);
  258. goto err;
  259. }
  260. if (!TS_RESP_CTX_set_def_policy(ctx, policy_obj))
  261. goto err;
  262. ret = 1;
  263. err:
  264. ASN1_OBJECT_free(policy_obj);
  265. return ret;
  266. }
  267. int TS_CONF_set_policies(CONF *conf, const char *section, TS_RESP_CTX *ctx)
  268. {
  269. int ret = 0;
  270. int i;
  271. STACK_OF(CONF_VALUE) *list = NULL;
  272. char *policies = NCONF_get_string(conf, section, ENV_OTHER_POLICIES);
  273. /* If no other policy is specified, that's fine. */
  274. if (policies && (list = X509V3_parse_list(policies)) == NULL) {
  275. ts_CONF_invalid(section, ENV_OTHER_POLICIES);
  276. goto err;
  277. }
  278. for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
  279. CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
  280. const char *extval = val->value ? val->value : val->name;
  281. ASN1_OBJECT *objtmp;
  282. if ((objtmp = OBJ_txt2obj(extval, 0)) == NULL) {
  283. ts_CONF_invalid(section, ENV_OTHER_POLICIES);
  284. goto err;
  285. }
  286. if (!TS_RESP_CTX_add_policy(ctx, objtmp))
  287. goto err;
  288. ASN1_OBJECT_free(objtmp);
  289. }
  290. ret = 1;
  291. err:
  292. sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
  293. return ret;
  294. }
  295. int TS_CONF_set_digests(CONF *conf, const char *section, TS_RESP_CTX *ctx)
  296. {
  297. int ret = 0;
  298. int i;
  299. STACK_OF(CONF_VALUE) *list = NULL;
  300. char *digests = NCONF_get_string(conf, section, ENV_DIGESTS);
  301. if (digests == NULL) {
  302. ts_CONF_lookup_fail(section, ENV_DIGESTS);
  303. goto err;
  304. }
  305. if ((list = X509V3_parse_list(digests)) == NULL) {
  306. ts_CONF_invalid(section, ENV_DIGESTS);
  307. goto err;
  308. }
  309. if (sk_CONF_VALUE_num(list) == 0) {
  310. ts_CONF_invalid(section, ENV_DIGESTS);
  311. goto err;
  312. }
  313. for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
  314. CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
  315. const char *extval = val->value ? val->value : val->name;
  316. const EVP_MD *md;
  317. if ((md = EVP_get_digestbyname(extval)) == NULL) {
  318. ts_CONF_invalid(section, ENV_DIGESTS);
  319. goto err;
  320. }
  321. if (!TS_RESP_CTX_add_md(ctx, md))
  322. goto err;
  323. }
  324. ret = 1;
  325. err:
  326. sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
  327. return ret;
  328. }
  329. int TS_CONF_set_accuracy(CONF *conf, const char *section, TS_RESP_CTX *ctx)
  330. {
  331. int ret = 0;
  332. int i;
  333. int secs = 0, millis = 0, micros = 0;
  334. STACK_OF(CONF_VALUE) *list = NULL;
  335. char *accuracy = NCONF_get_string(conf, section, ENV_ACCURACY);
  336. if (accuracy && (list = X509V3_parse_list(accuracy)) == NULL) {
  337. ts_CONF_invalid(section, ENV_ACCURACY);
  338. goto err;
  339. }
  340. for (i = 0; i < sk_CONF_VALUE_num(list); ++i) {
  341. CONF_VALUE *val = sk_CONF_VALUE_value(list, i);
  342. if (strcmp(val->name, ENV_VALUE_SECS) == 0) {
  343. if (val->value)
  344. secs = atoi(val->value);
  345. } else if (strcmp(val->name, ENV_VALUE_MILLISECS) == 0) {
  346. if (val->value)
  347. millis = atoi(val->value);
  348. } else if (strcmp(val->name, ENV_VALUE_MICROSECS) == 0) {
  349. if (val->value)
  350. micros = atoi(val->value);
  351. } else {
  352. ts_CONF_invalid(section, ENV_ACCURACY);
  353. goto err;
  354. }
  355. }
  356. if (!TS_RESP_CTX_set_accuracy(ctx, secs, millis, micros))
  357. goto err;
  358. ret = 1;
  359. err:
  360. sk_CONF_VALUE_pop_free(list, X509V3_conf_free);
  361. return ret;
  362. }
  363. int TS_CONF_set_clock_precision_digits(CONF *conf, const char *section,
  364. TS_RESP_CTX *ctx)
  365. {
  366. int ret = 0;
  367. long digits = 0;
  368. /*
  369. * If not specified, set the default value to 0, i.e. sec precision
  370. */
  371. if (!NCONF_get_number_e(conf, section, ENV_CLOCK_PRECISION_DIGITS,
  372. &digits))
  373. digits = 0;
  374. if (digits < 0 || digits > TS_MAX_CLOCK_PRECISION_DIGITS) {
  375. ts_CONF_invalid(section, ENV_CLOCK_PRECISION_DIGITS);
  376. goto err;
  377. }
  378. if (!TS_RESP_CTX_set_clock_precision_digits(ctx, digits))
  379. goto err;
  380. return 1;
  381. err:
  382. return ret;
  383. }
  384. static int ts_CONF_add_flag(CONF *conf, const char *section,
  385. const char *field, int flag, TS_RESP_CTX *ctx)
  386. {
  387. const char *value = NCONF_get_string(conf, section, field);
  388. if (value) {
  389. if (strcmp(value, ENV_VALUE_YES) == 0)
  390. TS_RESP_CTX_add_flags(ctx, flag);
  391. else if (strcmp(value, ENV_VALUE_NO) != 0) {
  392. ts_CONF_invalid(section, field);
  393. return 0;
  394. }
  395. }
  396. return 1;
  397. }
  398. int TS_CONF_set_ordering(CONF *conf, const char *section, TS_RESP_CTX *ctx)
  399. {
  400. return ts_CONF_add_flag(conf, section, ENV_ORDERING, TS_ORDERING, ctx);
  401. }
  402. int TS_CONF_set_tsa_name(CONF *conf, const char *section, TS_RESP_CTX *ctx)
  403. {
  404. return ts_CONF_add_flag(conf, section, ENV_TSA_NAME, TS_TSA_NAME, ctx);
  405. }
  406. int TS_CONF_set_ess_cert_id_chain(CONF *conf, const char *section,
  407. TS_RESP_CTX *ctx)
  408. {
  409. return ts_CONF_add_flag(conf, section, ENV_ESS_CERT_ID_CHAIN,
  410. TS_ESS_CERT_ID_CHAIN, ctx);
  411. }
  412. int TS_CONF_set_ess_cert_id_digest(CONF *conf, const char *section,
  413. TS_RESP_CTX *ctx)
  414. {
  415. int ret = 0;
  416. const EVP_MD *cert_md = NULL;
  417. const char *md = NCONF_get_string(conf, section, ENV_ESS_CERT_ID_ALG);
  418. if (md == NULL)
  419. md = "sha1";
  420. cert_md = EVP_get_digestbyname(md);
  421. if (cert_md == NULL) {
  422. ts_CONF_invalid(section, ENV_ESS_CERT_ID_ALG);
  423. goto err;
  424. }
  425. if (!TS_RESP_CTX_set_ess_cert_id_digest(ctx, cert_md))
  426. goto err;
  427. ret = 1;
  428. err:
  429. return ret;
  430. }