PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/crypto/ts/ts_conf.c

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