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