2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/conf.h>
13 /* #define ENGINE_CONF_DEBUG */
15 /* ENGINE config module */
17 static const char *skip_dot(const char *name)
19 const char *p = strchr(name, '.');
26 static STACK_OF(ENGINE) *initialized_engines = NULL;
28 static int int_engine_init(ENGINE *e)
32 if (!initialized_engines)
33 initialized_engines = sk_ENGINE_new_null();
34 if (!initialized_engines || !sk_ENGINE_push(initialized_engines, e)) {
41 static int int_engine_configure(const char *name, const char *value, const CONF *cnf)
46 STACK_OF(CONF_VALUE) *ecmds;
47 CONF_VALUE *ecmd = NULL;
48 const char *ctrlname, *ctrlvalue;
52 name = skip_dot(name);
53 #ifdef ENGINE_CONF_DEBUG
54 fprintf(stderr, "Configuring engine %s\n", name);
56 /* Value is a section containing ENGINE commands */
57 ecmds = NCONF_get_section(cnf, value);
60 ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE,
61 ENGINE_R_ENGINE_SECTION_ERROR);
65 for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) {
66 ecmd = sk_CONF_VALUE_value(ecmds, i);
67 ctrlname = skip_dot(ecmd->name);
68 ctrlvalue = ecmd->value;
69 #ifdef ENGINE_CONF_DEBUG
70 fprintf(stderr, "ENGINE conf: doing ctrl(%s,%s)\n", ctrlname,
74 /* First handle some special pseudo ctrls */
76 /* Override engine name to use */
77 if (strcmp(ctrlname, "engine_id") == 0)
79 else if (strcmp(ctrlname, "soft_load") == 0)
81 /* Load a dynamic ENGINE */
82 else if (strcmp(ctrlname, "dynamic_path") == 0) {
83 e = ENGINE_by_id("dynamic");
86 if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", ctrlvalue, 0))
88 if (!ENGINE_ctrl_cmd_string(e, "LIST_ADD", "2", 0))
90 if (!ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
93 /* ... add other pseudos here ... */
96 * At this point we need an ENGINE structural reference if we
97 * don't already have one.
100 e = ENGINE_by_id(name);
109 * Allow "EMPTY" to mean no value: this allows a valid "value" to
110 * be passed to ctrls of type NO_INPUT
112 if (strcmp(ctrlvalue, "EMPTY") == 0)
114 if (strcmp(ctrlname, "init") == 0) {
115 if (!NCONF_get_number_e(cnf, value, "init", &do_init))
118 if (!int_engine_init(e))
120 } else if (do_init != 0) {
121 ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE,
122 ENGINE_R_INVALID_INIT_VALUE);
125 } else if (strcmp(ctrlname, "default_algorithms") == 0) {
126 if (!ENGINE_set_default_string(e, ctrlvalue))
128 } else if (!ENGINE_ctrl_cmd_string(e, ctrlname, ctrlvalue, 0))
133 if (e && (do_init == -1) && !int_engine_init(e)) {
140 ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE,
141 ENGINE_R_ENGINE_CONFIGURATION_ERROR);
143 ERR_add_error_data(6, "section=", ecmd->section,
144 ", name=", ecmd->name,
145 ", value=", ecmd->value);
151 static int int_engine_module_init(CONF_IMODULE *md, const CONF *cnf)
153 STACK_OF(CONF_VALUE) *elist;
156 #ifdef ENGINE_CONF_DEBUG
157 fprintf(stderr, "Called engine module: name %s, value %s\n",
158 CONF_imodule_get_name(md), CONF_imodule_get_value(md));
160 /* Value is a section containing ENGINEs to configure */
161 elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
164 ENGINEerr(ENGINE_F_INT_ENGINE_MODULE_INIT,
165 ENGINE_R_ENGINES_SECTION_ERROR);
169 for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
170 cval = sk_CONF_VALUE_value(elist, i);
171 if (!int_engine_configure(cval->name, cval->value, cnf))
178 static void int_engine_module_finish(CONF_IMODULE *md)
182 while ((e = sk_ENGINE_pop(initialized_engines)))
184 sk_ENGINE_free(initialized_engines);
185 initialized_engines = NULL;
188 void ENGINE_add_conf_module(void)
190 CONF_module_add("engines",
191 int_engine_module_init, int_engine_module_finish);