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