conf_ssl.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright 2015-2018 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. #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_local.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. if (ssl_names == NULL)
  73. goto err;
  74. ssl_names_count = cnt;
  75. for (i = 0; i < ssl_names_count; i++) {
  76. struct ssl_conf_name_st *ssl_name = ssl_names + i;
  77. CONF_VALUE *sect = sk_CONF_VALUE_value(cmd_lists, (int)i);
  78. STACK_OF(CONF_VALUE) *cmds = NCONF_get_section(cnf, sect->value);
  79. if (sk_CONF_VALUE_num(cmds) <= 0) {
  80. if (cmds == NULL)
  81. CONFerr(CONF_F_SSL_MODULE_INIT,
  82. CONF_R_SSL_COMMAND_SECTION_NOT_FOUND);
  83. else
  84. CONFerr(CONF_F_SSL_MODULE_INIT,
  85. CONF_R_SSL_COMMAND_SECTION_EMPTY);
  86. ERR_add_error_data(4, "name=", sect->name, ", value=", sect->value);
  87. goto err;
  88. }
  89. ssl_name->name = OPENSSL_strdup(sect->name);
  90. if (ssl_name->name == NULL)
  91. goto err;
  92. cnt = sk_CONF_VALUE_num(cmds);
  93. ssl_name->cmds = OPENSSL_zalloc(cnt * sizeof(struct ssl_conf_cmd_st));
  94. if (ssl_name->cmds == NULL)
  95. goto err;
  96. ssl_name->cmd_count = cnt;
  97. for (j = 0; j < cnt; j++) {
  98. const char *name;
  99. CONF_VALUE *cmd_conf = sk_CONF_VALUE_value(cmds, (int)j);
  100. struct ssl_conf_cmd_st *cmd = ssl_name->cmds + j;
  101. /* Skip any initial dot in name */
  102. name = strchr(cmd_conf->name, '.');
  103. if (name != NULL)
  104. name++;
  105. else
  106. name = cmd_conf->name;
  107. cmd->cmd = OPENSSL_strdup(name);
  108. cmd->arg = OPENSSL_strdup(cmd_conf->value);
  109. if (cmd->cmd == NULL || cmd->arg == NULL)
  110. goto err;
  111. }
  112. }
  113. rv = 1;
  114. err:
  115. if (rv == 0)
  116. ssl_module_free(md);
  117. return rv;
  118. }
  119. /*
  120. * Returns the set of commands with index |idx| previously searched for via
  121. * conf_ssl_name_find. Also stores the name of the set of commands in |*name|
  122. * and the number of commands in the set in |*cnt|.
  123. */
  124. const SSL_CONF_CMD *conf_ssl_get(size_t idx, const char **name, size_t *cnt)
  125. {
  126. *name = ssl_names[idx].name;
  127. *cnt = ssl_names[idx].cmd_count;
  128. return ssl_names[idx].cmds;
  129. }
  130. /*
  131. * Search for the named set of commands given in |name|. On success return the
  132. * index for the command set in |*idx|.
  133. * Returns 1 on success or 0 on failure.
  134. */
  135. int conf_ssl_name_find(const char *name, size_t *idx)
  136. {
  137. size_t i;
  138. const struct ssl_conf_name_st *nm;
  139. if (name == NULL)
  140. return 0;
  141. for (i = 0, nm = ssl_names; i < ssl_names_count; i++, nm++) {
  142. if (strcmp(nm->name, name) == 0) {
  143. *idx = i;
  144. return 1;
  145. }
  146. }
  147. return 0;
  148. }
  149. /*
  150. * Given a command set |cmd|, return details on the command at index |idx| which
  151. * must be less than the number of commands in the set (as returned by
  152. * conf_ssl_get). The name of the command will be returned in |*cmdstr| and the
  153. * argument is returned in |*arg|.
  154. */
  155. void conf_ssl_get_cmd(const SSL_CONF_CMD *cmd, size_t idx, char **cmdstr,
  156. char **arg)
  157. {
  158. *cmdstr = cmd[idx].cmd;
  159. *arg = cmd[idx].arg;
  160. }
  161. void conf_add_ssl_module(void)
  162. {
  163. CONF_module_add("ssl_conf", ssl_module_init, ssl_module_free);
  164. }