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