1 /* ====================================================================
2 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * licensing@OpenSSL.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This product includes cryptographic software written by Eric Young
50 * (eay@cryptsoft.com). This product includes software written by Tim
51 * Hudson (tjh@cryptsoft.com).
58 * When querying a ENGINE-specific control command's 'description', this
59 * string is used if the ENGINE_CMD_DEFN has cmd_desc set to NULL.
61 static const char *int_no_description = "";
64 * These internal functions handle 'CMD'-related control commands when the
65 * ENGINE in question has asked us to take care of it (ie. the ENGINE did not
66 * set the ENGINE_FLAGS_MANUAL_CMD_CTRL flag.
69 static int int_ctrl_cmd_is_null(const ENGINE_CMD_DEFN *defn)
71 if ((defn->cmd_num == 0) || (defn->cmd_name == NULL))
76 static int int_ctrl_cmd_by_name(const ENGINE_CMD_DEFN *defn, const char *s)
79 while (!int_ctrl_cmd_is_null(defn) && (strcmp(defn->cmd_name, s) != 0)) {
83 if (int_ctrl_cmd_is_null(defn))
84 /* The given name wasn't found */
89 static int int_ctrl_cmd_by_num(const ENGINE_CMD_DEFN *defn, unsigned int num)
93 * NB: It is stipulated that 'cmd_defn' lists are ordered by cmd_num. So
94 * our searches don't need to take any longer than necessary.
96 while (!int_ctrl_cmd_is_null(defn) && (defn->cmd_num < num)) {
100 if (defn->cmd_num == num)
102 /* The given cmd_num wasn't found */
106 static int int_ctrl_helper(ENGINE *e, int cmd, long i, void *p,
111 /* Take care of the easy one first (eg. it requires no searches) */
112 if (cmd == ENGINE_CTRL_GET_FIRST_CMD_TYPE) {
113 if ((e->cmd_defns == NULL) || int_ctrl_cmd_is_null(e->cmd_defns))
115 return e->cmd_defns->cmd_num;
117 /* One or two commands require that "p" be a valid string buffer */
118 if ((cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) ||
119 (cmd == ENGINE_CTRL_GET_NAME_FROM_CMD) ||
120 (cmd == ENGINE_CTRL_GET_DESC_FROM_CMD)) {
122 ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ERR_R_PASSED_NULL_PARAMETER);
126 /* Now handle cmd_name -> cmd_num conversion */
127 if (cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) {
128 if ((e->cmd_defns == NULL)
129 || ((idx = int_ctrl_cmd_by_name(e->cmd_defns, s)) < 0)) {
130 ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INVALID_CMD_NAME);
133 return e->cmd_defns[idx].cmd_num;
136 * For the rest of the commands, the 'long' argument must specify a valid
137 * command number - so we need to conduct a search.
139 if ((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_num(e->cmd_defns,
142 ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INVALID_CMD_NUMBER);
145 /* Now the logic splits depending on command type */
147 case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
149 if (int_ctrl_cmd_is_null(e->cmd_defns + idx))
153 return e->cmd_defns[idx].cmd_num;
154 case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
155 return strlen(e->cmd_defns[idx].cmd_name);
156 case ENGINE_CTRL_GET_NAME_FROM_CMD:
157 return BIO_snprintf(s, strlen(e->cmd_defns[idx].cmd_name) + 1,
158 "%s", e->cmd_defns[idx].cmd_name);
159 case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
160 if (e->cmd_defns[idx].cmd_desc)
161 return strlen(e->cmd_defns[idx].cmd_desc);
162 return strlen(int_no_description);
163 case ENGINE_CTRL_GET_DESC_FROM_CMD:
164 if (e->cmd_defns[idx].cmd_desc)
165 return BIO_snprintf(s,
166 strlen(e->cmd_defns[idx].cmd_desc) + 1,
167 "%s", e->cmd_defns[idx].cmd_desc);
168 return BIO_snprintf(s, strlen(int_no_description) + 1, "%s",
170 case ENGINE_CTRL_GET_CMD_FLAGS:
171 return e->cmd_defns[idx].cmd_flags;
173 /* Shouldn't really be here ... */
174 ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INTERNAL_LIST_ERROR);
178 int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
180 int ctrl_exists, ref_exists;
182 ENGINEerr(ENGINE_F_ENGINE_CTRL, ERR_R_PASSED_NULL_PARAMETER);
185 CRYPTO_THREAD_write_lock(global_engine_lock);
186 ref_exists = ((e->struct_ref > 0) ? 1 : 0);
187 CRYPTO_THREAD_unlock(global_engine_lock);
188 ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
190 ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_REFERENCE);
194 * Intercept any "root-level" commands before trying to hand them on to
198 case ENGINE_CTRL_HAS_CTRL_FUNCTION:
200 case ENGINE_CTRL_GET_FIRST_CMD_TYPE:
201 case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
202 case ENGINE_CTRL_GET_CMD_FROM_NAME:
203 case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
204 case ENGINE_CTRL_GET_NAME_FROM_CMD:
205 case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
206 case ENGINE_CTRL_GET_DESC_FROM_CMD:
207 case ENGINE_CTRL_GET_CMD_FLAGS:
208 if (ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL))
209 return int_ctrl_helper(e, cmd, i, p, f);
211 ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION);
213 * For these cmd-related functions, failure is indicated by a -1
214 * return value (because 0 is used as a valid return in some
222 /* Anything else requires a ctrl() handler to exist. */
224 ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION);
227 return e->ctrl(e, cmd, i, p, f);
230 int ENGINE_cmd_is_executable(ENGINE *e, int cmd)
234 ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0) {
235 ENGINEerr(ENGINE_F_ENGINE_CMD_IS_EXECUTABLE,
236 ENGINE_R_INVALID_CMD_NUMBER);
239 if (!(flags & ENGINE_CMD_FLAG_NO_INPUT) &&
240 !(flags & ENGINE_CMD_FLAG_NUMERIC) &&
241 !(flags & ENGINE_CMD_FLAG_STRING))
246 int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
247 long i, void *p, void (*f) (void), int cmd_optional)
251 if ((e == NULL) || (cmd_name == NULL)) {
252 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, ERR_R_PASSED_NULL_PARAMETER);
255 if ((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
256 ENGINE_CTRL_GET_CMD_FROM_NAME,
260 * If the command didn't *have* to be supported, we fake success.
261 * This allows certain settings to be specified for multiple ENGINEs
262 * and only require a change of ENGINE id (without having to
263 * selectively apply settings). Eg. changing from a hardware device
264 * back to the regular software ENGINE without editing the config
271 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, ENGINE_R_INVALID_CMD_NAME);
275 * Force the result of the control command to 0 or 1, for the reasons
278 if (ENGINE_ctrl(e, num, i, p, f) > 0)
283 int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
289 if ((e == NULL) || (cmd_name == NULL)) {
290 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
291 ERR_R_PASSED_NULL_PARAMETER);
294 if ((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
295 ENGINE_CTRL_GET_CMD_FROM_NAME,
299 * If the command didn't *have* to be supported, we fake success.
300 * This allows certain settings to be specified for multiple ENGINEs
301 * and only require a change of ENGINE id (without having to
302 * selectively apply settings). Eg. changing from a hardware device
303 * back to the regular software ENGINE without editing the config
310 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, ENGINE_R_INVALID_CMD_NAME);
313 if (!ENGINE_cmd_is_executable(e, num)) {
314 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
315 ENGINE_R_CMD_NOT_EXECUTABLE);
319 ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL)) < 0) {
321 * Shouldn't happen, given that ENGINE_cmd_is_executable() returned
324 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
325 ENGINE_R_INTERNAL_LIST_ERROR);
329 * If the command takes no input, there must be no input. And vice versa.
331 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
333 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
334 ENGINE_R_COMMAND_TAKES_NO_INPUT);
338 * We deliberately force the result of ENGINE_ctrl() to 0 or 1 rather
339 * than returning it as "return data". This is to ensure usage of
340 * these commands is consistent across applications and that certain
341 * applications don't understand it one way, and others another.
343 if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
347 /* So, we require input */
349 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
350 ENGINE_R_COMMAND_TAKES_INPUT);
353 /* If it takes string input, that's easy */
354 if (flags & ENGINE_CMD_FLAG_STRING) {
355 /* Same explanation as above */
356 if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
361 * If it doesn't take numeric either, then it is unsupported for use in a
362 * config-setting situation, which is what this function is for. This
363 * should never happen though, because ENGINE_cmd_is_executable() was
366 if (!(flags & ENGINE_CMD_FLAG_NUMERIC)) {
367 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
368 ENGINE_R_INTERNAL_LIST_ERROR);
371 l = strtol(arg, &ptr, 10);
372 if ((arg == ptr) || (*ptr != '\0')) {
373 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
374 ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER);
378 * Force the result of the control command to 0 or 1, for the reasons
381 if (ENGINE_ctrl(e, num, l, NULL, NULL) > 0)