conf_ssl.c 5.2 KB

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