2
0

conf_api.c 5.4 KB

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