7589c21d66786bc64d320a061a64eff4b3c7c5f2
[openssl.git] / crypto / engine / eng_ctrl.c
1 /*
2  * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 #include "eng_int.h"
11
12 /*
13  * When querying a ENGINE-specific control command's 'description', this
14  * string is used if the ENGINE_CMD_DEFN has cmd_desc set to NULL.
15  */
16 static const char *int_no_description = "";
17
18 /*
19  * These internal functions handle 'CMD'-related control commands when the
20  * ENGINE in question has asked us to take care of it (ie. the ENGINE did not
21  * set the ENGINE_FLAGS_MANUAL_CMD_CTRL flag.
22  */
23
24 static int int_ctrl_cmd_is_null(const ENGINE_CMD_DEFN *defn)
25 {
26     if ((defn->cmd_num == 0) || (defn->cmd_name == NULL))
27         return 1;
28     return 0;
29 }
30
31 static int int_ctrl_cmd_by_name(const ENGINE_CMD_DEFN *defn, const char *s)
32 {
33     int idx = 0;
34     while (!int_ctrl_cmd_is_null(defn) && (strcmp(defn->cmd_name, s) != 0)) {
35         idx++;
36         defn++;
37     }
38     if (int_ctrl_cmd_is_null(defn))
39         /* The given name wasn't found */
40         return -1;
41     return idx;
42 }
43
44 static int int_ctrl_cmd_by_num(const ENGINE_CMD_DEFN *defn, unsigned int num)
45 {
46     int idx = 0;
47     /*
48      * NB: It is stipulated that 'cmd_defn' lists are ordered by cmd_num. So
49      * our searches don't need to take any longer than necessary.
50      */
51     while (!int_ctrl_cmd_is_null(defn) && (defn->cmd_num < num)) {
52         idx++;
53         defn++;
54     }
55     if (defn->cmd_num == num)
56         return idx;
57     /* The given cmd_num wasn't found */
58     return -1;
59 }
60
61 static int int_ctrl_helper(ENGINE *e, int cmd, long i, void *p,
62                            void (*f) (void))
63 {
64     int idx;
65     char *s = (char *)p;
66     /* Take care of the easy one first (eg. it requires no searches) */
67     if (cmd == ENGINE_CTRL_GET_FIRST_CMD_TYPE) {
68         if ((e->cmd_defns == NULL) || int_ctrl_cmd_is_null(e->cmd_defns))
69             return 0;
70         return e->cmd_defns->cmd_num;
71     }
72     /* One or two commands require that "p" be a valid string buffer */
73     if ((cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) ||
74         (cmd == ENGINE_CTRL_GET_NAME_FROM_CMD) ||
75         (cmd == ENGINE_CTRL_GET_DESC_FROM_CMD)) {
76         if (s == NULL) {
77             ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ERR_R_PASSED_NULL_PARAMETER);
78             return -1;
79         }
80     }
81     /* Now handle cmd_name -> cmd_num conversion */
82     if (cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) {
83         if ((e->cmd_defns == NULL)
84             || ((idx = int_ctrl_cmd_by_name(e->cmd_defns, s)) < 0)) {
85             ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INVALID_CMD_NAME);
86             return -1;
87         }
88         return e->cmd_defns[idx].cmd_num;
89     }
90     /*
91      * For the rest of the commands, the 'long' argument must specify a valid
92      * command number - so we need to conduct a search.
93      */
94     if ((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_num(e->cmd_defns,
95                                                               (unsigned int)
96                                                               i)) < 0)) {
97         ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INVALID_CMD_NUMBER);
98         return -1;
99     }
100     /* Now the logic splits depending on command type */
101     switch (cmd) {
102     case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
103         idx++;
104         if (int_ctrl_cmd_is_null(e->cmd_defns + idx))
105             /* end-of-list */
106             return 0;
107         else
108             return e->cmd_defns[idx].cmd_num;
109     case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
110         return strlen(e->cmd_defns[idx].cmd_name);
111     case ENGINE_CTRL_GET_NAME_FROM_CMD:
112         return BIO_snprintf(s, strlen(e->cmd_defns[idx].cmd_name) + 1,
113                             "%s", e->cmd_defns[idx].cmd_name);
114     case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
115         if (e->cmd_defns[idx].cmd_desc)
116             return strlen(e->cmd_defns[idx].cmd_desc);
117         return strlen(int_no_description);
118     case ENGINE_CTRL_GET_DESC_FROM_CMD:
119         if (e->cmd_defns[idx].cmd_desc)
120             return BIO_snprintf(s,
121                                 strlen(e->cmd_defns[idx].cmd_desc) + 1,
122                                 "%s", e->cmd_defns[idx].cmd_desc);
123         return BIO_snprintf(s, strlen(int_no_description) + 1, "%s",
124                             int_no_description);
125     case ENGINE_CTRL_GET_CMD_FLAGS:
126         return e->cmd_defns[idx].cmd_flags;
127     }
128     /* Shouldn't really be here ... */
129     ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INTERNAL_LIST_ERROR);
130     return -1;
131 }
132
133 int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
134 {
135     int ctrl_exists, ref_exists;
136     if (e == NULL) {
137         ENGINEerr(ENGINE_F_ENGINE_CTRL, ERR_R_PASSED_NULL_PARAMETER);
138         return 0;
139     }
140     CRYPTO_THREAD_write_lock(global_engine_lock);
141     ref_exists = ((e->struct_ref > 0) ? 1 : 0);
142     CRYPTO_THREAD_unlock(global_engine_lock);
143     ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
144     if (!ref_exists) {
145         ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_REFERENCE);
146         return 0;
147     }
148     /*
149      * Intercept any "root-level" commands before trying to hand them on to
150      * ctrl() handlers.
151      */
152     switch (cmd) {
153     case ENGINE_CTRL_HAS_CTRL_FUNCTION:
154         return ctrl_exists;
155     case ENGINE_CTRL_GET_FIRST_CMD_TYPE:
156     case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
157     case ENGINE_CTRL_GET_CMD_FROM_NAME:
158     case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
159     case ENGINE_CTRL_GET_NAME_FROM_CMD:
160     case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
161     case ENGINE_CTRL_GET_DESC_FROM_CMD:
162     case ENGINE_CTRL_GET_CMD_FLAGS:
163         if (ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL))
164             return int_ctrl_helper(e, cmd, i, p, f);
165         if (!ctrl_exists) {
166             ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION);
167             /*
168              * For these cmd-related functions, failure is indicated by a -1
169              * return value (because 0 is used as a valid return in some
170              * places).
171              */
172             return -1;
173         }
174     default:
175         break;
176     }
177     /* Anything else requires a ctrl() handler to exist. */
178     if (!ctrl_exists) {
179         ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION);
180         return 0;
181     }
182     return e->ctrl(e, cmd, i, p, f);
183 }
184
185 int ENGINE_cmd_is_executable(ENGINE *e, int cmd)
186 {
187     int flags;
188     if ((flags =
189          ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0) {
190         ENGINEerr(ENGINE_F_ENGINE_CMD_IS_EXECUTABLE,
191                   ENGINE_R_INVALID_CMD_NUMBER);
192         return 0;
193     }
194     if (!(flags & ENGINE_CMD_FLAG_NO_INPUT) &&
195         !(flags & ENGINE_CMD_FLAG_NUMERIC) &&
196         !(flags & ENGINE_CMD_FLAG_STRING))
197         return 0;
198     return 1;
199 }
200
201 int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
202                     long i, void *p, void (*f) (void), int cmd_optional)
203 {
204     int num;
205
206     if ((e == NULL) || (cmd_name == NULL)) {
207         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, ERR_R_PASSED_NULL_PARAMETER);
208         return 0;
209     }
210     if ((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
211                                                  ENGINE_CTRL_GET_CMD_FROM_NAME,
212                                                  0, (void *)cmd_name,
213                                                  NULL)) <= 0)) {
214         /*
215          * If the command didn't *have* to be supported, we fake success.
216          * This allows certain settings to be specified for multiple ENGINEs
217          * and only require a change of ENGINE id (without having to
218          * selectively apply settings). Eg. changing from a hardware device
219          * back to the regular software ENGINE without editing the config
220          * file, etc.
221          */
222         if (cmd_optional) {
223             ERR_clear_error();
224             return 1;
225         }
226         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, ENGINE_R_INVALID_CMD_NAME);
227         return 0;
228     }
229     /*
230      * Force the result of the control command to 0 or 1, for the reasons
231      * mentioned before.
232      */
233     if (ENGINE_ctrl(e, num, i, p, f) > 0)
234         return 1;
235     return 0;
236 }
237
238 int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
239                            int cmd_optional)
240 {
241     int num, flags;
242     long l;
243     char *ptr;
244     if ((e == NULL) || (cmd_name == NULL)) {
245         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
246                   ERR_R_PASSED_NULL_PARAMETER);
247         return 0;
248     }
249     if ((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
250                                                  ENGINE_CTRL_GET_CMD_FROM_NAME,
251                                                  0, (void *)cmd_name,
252                                                  NULL)) <= 0)) {
253         /*
254          * If the command didn't *have* to be supported, we fake success.
255          * This allows certain settings to be specified for multiple ENGINEs
256          * and only require a change of ENGINE id (without having to
257          * selectively apply settings). Eg. changing from a hardware device
258          * back to the regular software ENGINE without editing the config
259          * file, etc.
260          */
261         if (cmd_optional) {
262             ERR_clear_error();
263             return 1;
264         }
265         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, ENGINE_R_INVALID_CMD_NAME);
266         return 0;
267     }
268     if (!ENGINE_cmd_is_executable(e, num)) {
269         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
270                   ENGINE_R_CMD_NOT_EXECUTABLE);
271         return 0;
272     }
273     if ((flags =
274          ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL)) < 0) {
275         /*
276          * Shouldn't happen, given that ENGINE_cmd_is_executable() returned
277          * success.
278          */
279         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
280                   ENGINE_R_INTERNAL_LIST_ERROR);
281         return 0;
282     }
283     /*
284      * If the command takes no input, there must be no input. And vice versa.
285      */
286     if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
287         if (arg != NULL) {
288             ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
289                       ENGINE_R_COMMAND_TAKES_NO_INPUT);
290             return 0;
291         }
292         /*
293          * We deliberately force the result of ENGINE_ctrl() to 0 or 1 rather
294          * than returning it as "return data". This is to ensure usage of
295          * these commands is consistent across applications and that certain
296          * applications don't understand it one way, and others another.
297          */
298         if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
299             return 1;
300         return 0;
301     }
302     /* So, we require input */
303     if (arg == NULL) {
304         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
305                   ENGINE_R_COMMAND_TAKES_INPUT);
306         return 0;
307     }
308     /* If it takes string input, that's easy */
309     if (flags & ENGINE_CMD_FLAG_STRING) {
310         /* Same explanation as above */
311         if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
312             return 1;
313         return 0;
314     }
315     /*
316      * If it doesn't take numeric either, then it is unsupported for use in a
317      * config-setting situation, which is what this function is for. This
318      * should never happen though, because ENGINE_cmd_is_executable() was
319      * used.
320      */
321     if (!(flags & ENGINE_CMD_FLAG_NUMERIC)) {
322         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
323                   ENGINE_R_INTERNAL_LIST_ERROR);
324         return 0;
325     }
326     l = strtol(arg, &ptr, 10);
327     if ((arg == ptr) || (*ptr != '\0')) {
328         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
329                   ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER);
330         return 0;
331     }
332     /*
333      * Force the result of the control command to 0 or 1, for the reasons
334      * mentioned before.
335      */
336     if (ENGINE_ctrl(e, num, l, NULL, NULL) > 0)
337         return 1;
338     return 0;
339 }