2 * Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "eng_local.h"
11 #include <openssl/conf.h>
12 #include <openssl/trace.h>
14 DEFINE_STACK_OF(CONF_VALUE)
16 /* ENGINE config module */
18 static const char *skip_dot(const char *name)
20 const char *p = strchr(name, '.');
27 static STACK_OF(ENGINE) *initialized_engines = NULL;
29 static int int_engine_init(ENGINE *e)
33 if (!initialized_engines)
34 initialized_engines = sk_ENGINE_new_null();
35 if (!initialized_engines || !sk_ENGINE_push(initialized_engines, e)) {
42 static int int_engine_configure(const char *name, const char *value, const CONF *cnf)
47 STACK_OF(CONF_VALUE) *ecmds;
48 CONF_VALUE *ecmd = NULL;
49 const char *ctrlname, *ctrlvalue;
53 name = skip_dot(name);
54 OSSL_TRACE1(CONF, "Configuring engine %s\n", name);
55 /* Value is a section containing ENGINE commands */
56 ecmds = NCONF_get_section(cnf, value);
59 ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE,
60 ENGINE_R_ENGINE_SECTION_ERROR);
64 for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) {
65 ecmd = sk_CONF_VALUE_value(ecmds, i);
66 ctrlname = skip_dot(ecmd->name);
67 ctrlvalue = ecmd->value;
68 OSSL_TRACE2(CONF, "ENGINE: doing ctrl(%s,%s)\n",
71 /* First handle some special pseudo ctrls */
73 /* Override engine name to use */
74 if (strcmp(ctrlname, "engine_id") == 0)
76 else if (strcmp(ctrlname, "soft_load") == 0)
78 /* Load a dynamic ENGINE */
79 else if (strcmp(ctrlname, "dynamic_path") == 0) {
80 e = ENGINE_by_id("dynamic");
83 if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", ctrlvalue, 0))
85 if (!ENGINE_ctrl_cmd_string(e, "LIST_ADD", "2", 0))
87 if (!ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
90 /* ... add other pseudos here ... */
93 * At this point we need an ENGINE structural reference if we
94 * don't already have one.
97 e = ENGINE_by_id(name);
106 * Allow "EMPTY" to mean no value: this allows a valid "value" to
107 * be passed to ctrls of type NO_INPUT
109 if (strcmp(ctrlvalue, "EMPTY") == 0)
111 if (strcmp(ctrlname, "init") == 0) {
112 if (!NCONF_get_number_e(cnf, value, "init", &do_init))
115 if (!int_engine_init(e))
117 } else if (do_init != 0) {
118 ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE,
119 ENGINE_R_INVALID_INIT_VALUE);
122 } else if (strcmp(ctrlname, "default_algorithms") == 0) {
123 if (!ENGINE_set_default_string(e, ctrlvalue))
125 } else if (!ENGINE_ctrl_cmd_string(e, ctrlname, ctrlvalue, 0))
130 if (e && (do_init == -1) && !int_engine_init(e)) {
137 ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE,
138 ENGINE_R_ENGINE_CONFIGURATION_ERROR);
140 ERR_add_error_data(6, "section=", ecmd->section,
141 ", name=", ecmd->name,
142 ", value=", ecmd->value);
148 static int int_engine_module_init(CONF_IMODULE *md, const CONF *cnf)
150 STACK_OF(CONF_VALUE) *elist;
153 OSSL_TRACE2(CONF, "Called engine module: name %s, value %s\n",
154 CONF_imodule_get_name(md), CONF_imodule_get_value(md));
155 /* Value is a section containing ENGINEs to configure */
156 elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
159 ENGINEerr(ENGINE_F_INT_ENGINE_MODULE_INIT,
160 ENGINE_R_ENGINES_SECTION_ERROR);
164 for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
165 cval = sk_CONF_VALUE_value(elist, i);
166 if (!int_engine_configure(cval->name, cval->value, cnf))
173 static void int_engine_module_finish(CONF_IMODULE *md)
177 while ((e = sk_ENGINE_pop(initialized_engines)))
179 sk_ENGINE_free(initialized_engines);
180 initialized_engines = NULL;
183 void ENGINE_add_conf_module(void)
185 CONF_module_add("engines",
186 int_engine_module_init, int_engine_module_finish);