conf_api.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright 1995-2022 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 "internal/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)
  118. return;
  119. OPENSSL_free(conf->includedir);
  120. if (conf->data == NULL)
  121. return;
  122. /* evil thing to make sure the 'OPENSSL_free()' works as expected */
  123. lh_CONF_VALUE_set_down_load(conf->data, 0);
  124. lh_CONF_VALUE_doall_LH_CONF_VALUE(conf->data, value_free_hash, conf->data);
  125. /*
  126. * We now have only 'section' entries in the hash table. Due to problems
  127. * with
  128. */
  129. lh_CONF_VALUE_doall(conf->data, value_free_stack_doall);
  130. lh_CONF_VALUE_free(conf->data);
  131. }
  132. static void value_free_hash(const CONF_VALUE *a, LHASH_OF(CONF_VALUE) *conf)
  133. {
  134. if (a->name != NULL)
  135. (void)lh_CONF_VALUE_delete(conf, a);
  136. }
  137. static void value_free_stack_doall(CONF_VALUE *a)
  138. {
  139. CONF_VALUE *vv;
  140. STACK_OF(CONF_VALUE) *sk;
  141. int i;
  142. if (a->name != NULL)
  143. return;
  144. sk = (STACK_OF(CONF_VALUE) *)a->value;
  145. for (i = sk_CONF_VALUE_num(sk) - 1; i >= 0; i--) {
  146. vv = sk_CONF_VALUE_value(sk, i);
  147. OPENSSL_free(vv->value);
  148. OPENSSL_free(vv->name);
  149. OPENSSL_free(vv);
  150. }
  151. sk_CONF_VALUE_free(sk);
  152. OPENSSL_free(a->section);
  153. OPENSSL_free(a);
  154. }
  155. CONF_VALUE *_CONF_new_section(CONF *conf, const char *section)
  156. {
  157. STACK_OF(CONF_VALUE) *sk = NULL;
  158. int i;
  159. CONF_VALUE *v = NULL, *vv;
  160. if ((sk = sk_CONF_VALUE_new_null()) == NULL)
  161. goto err;
  162. if ((v = OPENSSL_malloc(sizeof(*v))) == NULL)
  163. goto err;
  164. i = strlen(section) + 1;
  165. if ((v->section = OPENSSL_malloc(i)) == NULL)
  166. goto err;
  167. memcpy(v->section, section, i);
  168. v->name = NULL;
  169. v->value = (char *)sk;
  170. vv = lh_CONF_VALUE_insert(conf->data, v);
  171. if (vv != NULL || lh_CONF_VALUE_error(conf->data) > 0)
  172. goto err;
  173. return v;
  174. err:
  175. sk_CONF_VALUE_free(sk);
  176. if (v != NULL)
  177. OPENSSL_free(v->section);
  178. OPENSSL_free(v);
  179. return NULL;
  180. }