ts_conf.c 14 KB

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