ts_conf.c 14 KB

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