conf_api.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright 1995-2021 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. /* Part of the code in here was originally in conf.c, which is now removed */
  10. #include "e_os.h"
  11. #include "internal/cryptlib.h"
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <openssl/conf.h>
  15. #include <openssl/conf_api.h>
  16. #include "conf_local.h"
  17. static void value_free_hash(const CONF_VALUE *a, LHASH_OF(CONF_VALUE) *conf);
  18. static void value_free_stack_doall(CONF_VALUE *a);
  19. CONF_VALUE *_CONF_get_section(const CONF *conf, const char *section)
  20. {
  21. CONF_VALUE vv;
  22. if (conf == NULL || section == NULL)
  23. return NULL;
  24. vv.name = NULL;
  25. vv.section = (char *)section;
  26. return conf->data != NULL ? lh_CONF_VALUE_retrieve(conf->data, &vv) : NULL;
  27. }
  28. STACK_OF(CONF_VALUE) *_CONF_get_section_values(const CONF *conf,
  29. const char *section)
  30. {
  31. CONF_VALUE *v;
  32. v = _CONF_get_section(conf, section);
  33. if (v == NULL)
  34. return NULL;
  35. return ((STACK_OF(CONF_VALUE) *)v->value);
  36. }
  37. int _CONF_add_string(CONF *conf, CONF_VALUE *section, CONF_VALUE *value)
  38. {
  39. CONF_VALUE *v = NULL;
  40. STACK_OF(CONF_VALUE) *ts;
  41. ts = (STACK_OF(CONF_VALUE) *)section->value;
  42. value->section = section->section;
  43. if (!sk_CONF_VALUE_push(ts, value))
  44. return 0;
  45. v = lh_CONF_VALUE_insert(conf->data, value);
  46. if (v != NULL) {
  47. (void)sk_CONF_VALUE_delete_ptr(ts, v);
  48. OPENSSL_free(v->name);
  49. OPENSSL_free(v->value);
  50. OPENSSL_free(v);
  51. }
  52. return 1;
  53. }
  54. char *_CONF_get_string(const CONF *conf, const char *section,
  55. const char *name)
  56. {
  57. CONF_VALUE *v, vv;
  58. char *p;
  59. if (name == NULL)
  60. return NULL;
  61. if (conf == NULL)
  62. return ossl_safe_getenv(name);
  63. if (conf->data == NULL)
  64. return NULL;
  65. if (section != NULL) {
  66. vv.name = (char *)name;
  67. vv.section = (char *)section;
  68. v = lh_CONF_VALUE_retrieve(conf->data, &vv);
  69. if (v != NULL)
  70. return v->value;
  71. if (strcmp(section, "ENV") == 0) {
  72. p = ossl_safe_getenv(name);
  73. if (p != NULL)
  74. return p;
  75. }
  76. }
  77. vv.section = "default";
  78. vv.name = (char *)name;
  79. v = lh_CONF_VALUE_retrieve(conf->data, &vv);
  80. if (v == NULL)
  81. return NULL;
  82. return v->value;
  83. }
  84. static unsigned long conf_value_hash(const CONF_VALUE *v)
  85. {
  86. return (OPENSSL_LH_strhash(v->section) << 2) ^ OPENSSL_LH_strhash(v->name);
  87. }
  88. static int conf_value_cmp(const CONF_VALUE *a, const CONF_VALUE *b)
  89. {
  90. int i;
  91. if (a->section != b->section) {
  92. i = strcmp(a->section, b->section);
  93. if (i != 0)
  94. return i;
  95. }
  96. if (a->name != NULL && b->name != NULL)
  97. return strcmp(a->name, b->name);
  98. if (a->name == b->name)
  99. return 0;
  100. return (a->name == NULL) ? -1 : 1;
  101. }
  102. int _CONF_new_data(CONF *conf)
  103. {
  104. if (conf == NULL)
  105. return 0;
  106. if (conf->data == NULL) {
  107. conf->data = lh_CONF_VALUE_new(conf_value_hash, conf_value_cmp);
  108. if (conf->data == NULL)
  109. return 0;
  110. }
  111. return 1;
  112. }
  113. typedef LHASH_OF(CONF_VALUE) LH_CONF_VALUE;
  114. IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, LH_CONF_VALUE);
  115. void _CONF_free_data(CONF *conf)
  116. {
  117. if (conf == NULL || conf->data == NULL)
  118. return;
  119. /* evil thing to make sure the 'OPENSSL_free()' works as expected */
  120. lh_CONF_VALUE_set_down_load(conf->data, 0);
  121. lh_CONF_VALUE_doall_LH_CONF_VALUE(conf->data, value_free_hash, conf->data);
  122. /*
  123. * We now have only 'section' entries in the hash table. Due to problems
  124. * with
  125. */
  126. OPENSSL_free(conf->includedir);
  127. lh_CONF_VALUE_doall(conf->data, value_free_stack_doall);
  128. lh_CONF_VALUE_free(conf->data);
  129. }
  130. static void value_free_hash(const CONF_VALUE *a, LHASH_OF(CONF_VALUE) *conf)
  131. {
  132. if (a->name != NULL)
  133. (void)lh_CONF_VALUE_delete(conf, a);
  134. }
  135. static void value_free_stack_doall(CONF_VALUE *a)
  136. {
  137. CONF_VALUE *vv;
  138. STACK_OF(CONF_VALUE) *sk;
  139. int i;
  140. if (a->name != NULL)
  141. return;
  142. sk = (STACK_OF(CONF_VALUE) *)a->value;
  143. for (i = sk_CONF_VALUE_num(sk) - 1; i >= 0; i--) {
  144. vv = sk_CONF_VALUE_value(sk, i);
  145. OPENSSL_free(vv->value);
  146. OPENSSL_free(vv->name);
  147. OPENSSL_free(vv);
  148. }
  149. sk_CONF_VALUE_free(sk);
  150. OPENSSL_free(a->section);
  151. OPENSSL_free(a);
  152. }
  153. CONF_VALUE *_CONF_new_section(CONF *conf, const char *section)
  154. {
  155. STACK_OF(CONF_VALUE) *sk = NULL;
  156. int i;
  157. CONF_VALUE *v = NULL, *vv;
  158. if ((sk = sk_CONF_VALUE_new_null()) == NULL)
  159. goto err;
  160. if ((v = OPENSSL_malloc(sizeof(*v))) == NULL)
  161. goto err;
  162. i = strlen(section) + 1;
  163. if ((v->section = OPENSSL_malloc(i)) == NULL)
  164. goto err;
  165. memcpy(v->section, section, i);
  166. v->name = NULL;
  167. v->value = (char *)sk;
  168. vv = lh_CONF_VALUE_insert(conf->data, v);
  169. if (vv != NULL || lh_CONF_VALUE_error(conf->data) > 0)
  170. goto err;
  171. return v;
  172. err:
  173. sk_CONF_VALUE_free(sk);
  174. if (v != NULL)
  175. OPENSSL_free(v->section);
  176. OPENSSL_free(v);
  177. return NULL;
  178. }