eng_cnf.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright 2002-2016 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 "eng_local.h"
  10. #include <openssl/conf.h>
  11. #include <openssl/trace.h>
  12. /* ENGINE config module */
  13. static const char *skip_dot(const char *name)
  14. {
  15. const char *p = strchr(name, '.');
  16. if (p != NULL)
  17. return p + 1;
  18. return name;
  19. }
  20. static STACK_OF(ENGINE) *initialized_engines = NULL;
  21. static int int_engine_init(ENGINE *e)
  22. {
  23. if (!ENGINE_init(e))
  24. return 0;
  25. if (!initialized_engines)
  26. initialized_engines = sk_ENGINE_new_null();
  27. if (!initialized_engines || !sk_ENGINE_push(initialized_engines, e)) {
  28. ENGINE_finish(e);
  29. return 0;
  30. }
  31. return 1;
  32. }
  33. static int int_engine_configure(const char *name, const char *value, const CONF *cnf)
  34. {
  35. int i;
  36. int ret = 0;
  37. long do_init = -1;
  38. STACK_OF(CONF_VALUE) *ecmds;
  39. CONF_VALUE *ecmd = NULL;
  40. const char *ctrlname, *ctrlvalue;
  41. ENGINE *e = NULL;
  42. int soft = 0;
  43. name = skip_dot(name);
  44. OSSL_TRACE1(CONF, "Configuring engine %s\n", name);
  45. /* Value is a section containing ENGINE commands */
  46. ecmds = NCONF_get_section(cnf, value);
  47. if (!ecmds) {
  48. ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE,
  49. ENGINE_R_ENGINE_SECTION_ERROR);
  50. return 0;
  51. }
  52. for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) {
  53. ecmd = sk_CONF_VALUE_value(ecmds, i);
  54. ctrlname = skip_dot(ecmd->name);
  55. ctrlvalue = ecmd->value;
  56. OSSL_TRACE2(CONF, "ENGINE: doing ctrl(%s,%s)\n",
  57. ctrlname, ctrlvalue);
  58. /* First handle some special pseudo ctrls */
  59. /* Override engine name to use */
  60. if (strcmp(ctrlname, "engine_id") == 0)
  61. name = ctrlvalue;
  62. else if (strcmp(ctrlname, "soft_load") == 0)
  63. soft = 1;
  64. /* Load a dynamic ENGINE */
  65. else if (strcmp(ctrlname, "dynamic_path") == 0) {
  66. e = ENGINE_by_id("dynamic");
  67. if (!e)
  68. goto err;
  69. if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", ctrlvalue, 0))
  70. goto err;
  71. if (!ENGINE_ctrl_cmd_string(e, "LIST_ADD", "2", 0))
  72. goto err;
  73. if (!ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
  74. goto err;
  75. }
  76. /* ... add other pseudos here ... */
  77. else {
  78. /*
  79. * At this point we need an ENGINE structural reference if we
  80. * don't already have one.
  81. */
  82. if (!e) {
  83. e = ENGINE_by_id(name);
  84. if (!e && soft) {
  85. ERR_clear_error();
  86. return 1;
  87. }
  88. if (!e)
  89. goto err;
  90. }
  91. /*
  92. * Allow "EMPTY" to mean no value: this allows a valid "value" to
  93. * be passed to ctrls of type NO_INPUT
  94. */
  95. if (strcmp(ctrlvalue, "EMPTY") == 0)
  96. ctrlvalue = NULL;
  97. if (strcmp(ctrlname, "init") == 0) {
  98. if (!NCONF_get_number_e(cnf, value, "init", &do_init))
  99. goto err;
  100. if (do_init == 1) {
  101. if (!int_engine_init(e))
  102. goto err;
  103. } else if (do_init != 0) {
  104. ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE,
  105. ENGINE_R_INVALID_INIT_VALUE);
  106. goto err;
  107. }
  108. } else if (strcmp(ctrlname, "default_algorithms") == 0) {
  109. if (!ENGINE_set_default_string(e, ctrlvalue))
  110. goto err;
  111. } else if (!ENGINE_ctrl_cmd_string(e, ctrlname, ctrlvalue, 0))
  112. goto err;
  113. }
  114. }
  115. if (e && (do_init == -1) && !int_engine_init(e)) {
  116. ecmd = NULL;
  117. goto err;
  118. }
  119. ret = 1;
  120. err:
  121. if (ret != 1) {
  122. ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE,
  123. ENGINE_R_ENGINE_CONFIGURATION_ERROR);
  124. if (ecmd)
  125. ERR_add_error_data(6, "section=", ecmd->section,
  126. ", name=", ecmd->name,
  127. ", value=", ecmd->value);
  128. }
  129. ENGINE_free(e);
  130. return ret;
  131. }
  132. static int int_engine_module_init(CONF_IMODULE *md, const CONF *cnf)
  133. {
  134. STACK_OF(CONF_VALUE) *elist;
  135. CONF_VALUE *cval;
  136. int i;
  137. OSSL_TRACE2(CONF, "Called engine module: name %s, value %s\n",
  138. CONF_imodule_get_name(md), CONF_imodule_get_value(md));
  139. /* Value is a section containing ENGINEs to configure */
  140. elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
  141. if (!elist) {
  142. ENGINEerr(ENGINE_F_INT_ENGINE_MODULE_INIT,
  143. ENGINE_R_ENGINES_SECTION_ERROR);
  144. return 0;
  145. }
  146. for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
  147. cval = sk_CONF_VALUE_value(elist, i);
  148. if (!int_engine_configure(cval->name, cval->value, cnf))
  149. return 0;
  150. }
  151. return 1;
  152. }
  153. static void int_engine_module_finish(CONF_IMODULE *md)
  154. {
  155. ENGINE *e;
  156. while ((e = sk_ENGINE_pop(initialized_engines)))
  157. ENGINE_finish(e);
  158. sk_ENGINE_free(initialized_engines);
  159. initialized_engines = NULL;
  160. }
  161. void ENGINE_add_conf_module(void)
  162. {
  163. CONF_module_add("engines",
  164. int_engine_module_init, int_engine_module_finish);
  165. }