2
0

conf_ssl.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2015-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. #include <stdio.h>
  10. #include <string.h>
  11. #include <openssl/conf.h>
  12. #include <openssl/err.h>
  13. #include "internal/sslconf.h"
  14. #include "conf_lcl.h"
  15. /*
  16. * SSL library configuration module placeholder. We load it here but defer
  17. * all decisions about its contents to libssl.
  18. */
  19. struct ssl_conf_name_st {
  20. /* Name of this set of commands */
  21. char *name;
  22. /* List of commands */
  23. SSL_CONF_CMD *cmds;
  24. /* Number of commands */
  25. size_t cmd_count;
  26. };
  27. struct ssl_conf_cmd_st {
  28. /* Command */
  29. char *cmd;
  30. /* Argument */
  31. char *arg;
  32. };
  33. static struct ssl_conf_name_st *ssl_names;
  34. static size_t ssl_names_count;
  35. static void ssl_module_free(CONF_IMODULE *md)
  36. {
  37. size_t i, j;
  38. if (ssl_names == NULL)
  39. return;
  40. for (i = 0; i < ssl_names_count; i++) {
  41. struct ssl_conf_name_st *tname = ssl_names + i;
  42. OPENSSL_free(tname->name);
  43. for (j = 0; j < tname->cmd_count; j++) {
  44. OPENSSL_free(tname->cmds[j].cmd);
  45. OPENSSL_free(tname->cmds[j].arg);
  46. }
  47. OPENSSL_free(tname->cmds);
  48. }
  49. OPENSSL_free(ssl_names);
  50. ssl_names = NULL;
  51. ssl_names_count = 0;
  52. }
  53. static int ssl_module_init(CONF_IMODULE *md, const CONF *cnf)
  54. {
  55. size_t i, j, cnt;
  56. int rv = 0;
  57. const char *ssl_conf_section;
  58. STACK_OF(CONF_VALUE) *cmd_lists;
  59. ssl_conf_section = CONF_imodule_get_value(md);
  60. cmd_lists = NCONF_get_section(cnf, ssl_conf_section);
  61. if (sk_CONF_VALUE_num(cmd_lists) <= 0) {
  62. if (cmd_lists == NULL)
  63. CONFerr(CONF_F_SSL_MODULE_INIT, CONF_R_SSL_SECTION_NOT_FOUND);
  64. else
  65. CONFerr(CONF_F_SSL_MODULE_INIT, CONF_R_SSL_SECTION_EMPTY);
  66. ERR_add_error_data(2, "section=", ssl_conf_section);
  67. goto err;
  68. }
  69. cnt = sk_CONF_VALUE_num(cmd_lists);
  70. ssl_module_free(md);
  71. ssl_names = OPENSSL_zalloc(sizeof(*ssl_names) * cnt);
  72. ssl_names_count = cnt;
  73. for (i = 0; i < ssl_names_count; i++) {
  74. struct ssl_conf_name_st *ssl_name = ssl_names + i;
  75. CONF_VALUE *sect = sk_CONF_VALUE_value(cmd_lists, (int)i);
  76. STACK_OF(CONF_VALUE) *cmds = NCONF_get_section(cnf, sect->value);
  77. if (sk_CONF_VALUE_num(cmds) <= 0) {
  78. if (cmds == NULL)
  79. CONFerr(CONF_F_SSL_MODULE_INIT,
  80. CONF_R_SSL_COMMAND_SECTION_NOT_FOUND);
  81. else
  82. CONFerr(CONF_F_SSL_MODULE_INIT,
  83. CONF_R_SSL_COMMAND_SECTION_EMPTY);
  84. ERR_add_error_data(4, "name=", sect->name, ", value=", sect->value);
  85. goto err;
  86. }
  87. ssl_name->name = OPENSSL_strdup(sect->name);
  88. if (ssl_name->name == NULL)
  89. goto err;
  90. cnt = sk_CONF_VALUE_num(cmds);
  91. ssl_name->cmds = OPENSSL_zalloc(cnt * sizeof(struct ssl_conf_cmd_st));
  92. if (ssl_name->cmds == NULL)
  93. goto err;
  94. ssl_name->cmd_count = cnt;
  95. for (j = 0; j < cnt; j++) {
  96. const char *name;
  97. CONF_VALUE *cmd_conf = sk_CONF_VALUE_value(cmds, (int)j);
  98. struct ssl_conf_cmd_st *cmd = ssl_name->cmds + j;
  99. /* Skip any initial dot in name */
  100. name = strchr(cmd_conf->name, '.');
  101. if (name != NULL)
  102. name++;
  103. else
  104. name = cmd_conf->name;
  105. cmd->cmd = OPENSSL_strdup(name);
  106. cmd->arg = OPENSSL_strdup(cmd_conf->value);
  107. if (cmd->cmd == NULL || cmd->arg == NULL)
  108. goto err;
  109. }
  110. }
  111. rv = 1;
  112. err:
  113. if (rv == 0)
  114. ssl_module_free(md);
  115. return rv;
  116. }
  117. /*
  118. * Returns the set of commands with index |idx| previously searched for via
  119. * conf_ssl_name_find. Also stores the name of the set of commands in |*name|
  120. * and the number of commands in the set in |*cnt|.
  121. */
  122. const SSL_CONF_CMD *conf_ssl_get(size_t idx, const char **name, size_t *cnt)
  123. {
  124. *name = ssl_names[idx].name;
  125. *cnt = ssl_names[idx].cmd_count;
  126. return ssl_names[idx].cmds;
  127. }
  128. /*
  129. * Search for the named set of commands given in |name|. On success return the
  130. * index for the command set in |*idx|.
  131. * Returns 1 on success or 0 on failure.
  132. */
  133. int conf_ssl_name_find(const char *name, size_t *idx)
  134. {
  135. size_t i;
  136. const struct ssl_conf_name_st *nm;
  137. if (name == NULL)
  138. return 0;
  139. for (i = 0, nm = ssl_names; i < ssl_names_count; i++, nm++) {
  140. if (strcmp(nm->name, name) == 0) {
  141. *idx = i;
  142. return 1;
  143. }
  144. }
  145. return 0;
  146. }
  147. /*
  148. * Given a command set |cmd|, return details on the command at index |idx| which
  149. * must be less than the number of commands in the set (as returned by
  150. * conf_ssl_get). The name of the command will be returned in |*cmdstr| and the
  151. * argument is returned in |*arg|.
  152. */
  153. void conf_ssl_get_cmd(const SSL_CONF_CMD *cmd, size_t idx, char **cmdstr,
  154. char **arg)
  155. {
  156. *cmdstr = cmd[idx].cmd;
  157. *arg = cmd[idx].arg;
  158. }
  159. void conf_add_ssl_module(void)
  160. {
  161. CONF_module_add("ssl_conf", ssl_module_init, ssl_module_free);
  162. }