2
0

conf_api.c 5.5 KB

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