Anchor AES and SHA-256/-512 assembler from C.
[openssl.git] / crypto / engine / eng_ctrl.c
1 /* crypto/engine/eng_ctrl.c */
2 /* ====================================================================
3  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer. 
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. All advertising materials mentioning features or use of this
18  *    software must display the following acknowledgment:
19  *    "This product includes software developed by the OpenSSL Project
20  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21  *
22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23  *    endorse or promote products derived from this software without
24  *    prior written permission. For written permission, please contact
25  *    licensing@OpenSSL.org.
26  *
27  * 5. Products derived from this software may not be called "OpenSSL"
28  *    nor may "OpenSSL" appear in their names without prior written
29  *    permission of the OpenSSL Project.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This product includes cryptographic software written by Eric Young
51  * (eay@cryptsoft.com).  This product includes software written by Tim
52  * Hudson (tjh@cryptsoft.com).
53  *
54  */
55
56 #include "eng_int.h"
57
58 /* When querying a ENGINE-specific control command's 'description', this string
59  * is used if the ENGINE_CMD_DEFN has cmd_desc set to NULL. */
60 static const char *int_no_description = "";
61
62 /* These internal functions handle 'CMD'-related control commands when the
63  * ENGINE in question has asked us to take care of it (ie. the ENGINE did not
64  * set the ENGINE_FLAGS_MANUAL_CMD_CTRL flag. */
65
66 static int int_ctrl_cmd_is_null(const ENGINE_CMD_DEFN *defn)
67         {
68         if((defn->cmd_num == 0) || (defn->cmd_name == NULL))
69                 return 1;
70         return 0;
71         }
72
73 static int int_ctrl_cmd_by_name(const ENGINE_CMD_DEFN *defn, const char *s)
74         {
75         int idx = 0;
76         while(!int_ctrl_cmd_is_null(defn) && (strcmp(defn->cmd_name, s) != 0))
77                 {
78                 idx++;
79                 defn++;
80                 }
81         if(int_ctrl_cmd_is_null(defn))
82                 /* The given name wasn't found */
83                 return -1;
84         return idx;
85         }
86
87 static int int_ctrl_cmd_by_num(const ENGINE_CMD_DEFN *defn, unsigned int num)
88         {
89         int idx = 0;
90         /* NB: It is stipulated that 'cmd_defn' lists are ordered by cmd_num. So
91          * our searches don't need to take any longer than necessary. */
92         while(!int_ctrl_cmd_is_null(defn) && (defn->cmd_num < num))
93                 {
94                 idx++;
95                 defn++;
96                 }
97         if(defn->cmd_num == num)
98                 return idx;
99         /* The given cmd_num wasn't found */
100         return -1;
101         }
102
103 static int int_ctrl_helper(ENGINE *e, int cmd, long i, void *p, void (*f)())
104         {
105         int idx;
106         char *s = (char *)p;
107         /* Take care of the easy one first (eg. it requires no searches) */
108         if(cmd == ENGINE_CTRL_GET_FIRST_CMD_TYPE)
109                 {
110                 if((e->cmd_defns == NULL) || int_ctrl_cmd_is_null(e->cmd_defns))
111                         return 0;
112                 return e->cmd_defns->cmd_num;
113                 }
114         /* One or two commands require that "p" be a valid string buffer */
115         if((cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) ||
116                         (cmd == ENGINE_CTRL_GET_NAME_FROM_CMD) ||
117                         (cmd == ENGINE_CTRL_GET_DESC_FROM_CMD))
118                 {
119                 if(s == NULL)
120                         {
121                         ENGINEerr(ENGINE_F_INT_CTRL_HELPER,
122                                 ERR_R_PASSED_NULL_PARAMETER);
123                         return -1;
124                         }
125                 }
126         /* Now handle cmd_name -> cmd_num conversion */
127         if(cmd == ENGINE_CTRL_GET_CMD_FROM_NAME)
128                 {
129                 if((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_name(
130                                                 e->cmd_defns, s)) < 0))
131                         {
132                         ENGINEerr(ENGINE_F_INT_CTRL_HELPER,
133                                 ENGINE_R_INVALID_CMD_NAME);
134                         return -1;
135                         }
136                 return e->cmd_defns[idx].cmd_num;
137                 }
138         /* For the rest of the commands, the 'long' argument must specify a
139          * valie command number - so we need to conduct a search. */
140         if((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_num(e->cmd_defns,
141                                         (unsigned int)i)) < 0))
142                 {
143                 ENGINEerr(ENGINE_F_INT_CTRL_HELPER,
144                         ENGINE_R_INVALID_CMD_NUMBER);
145                 return -1;
146                 }
147         /* Now the logic splits depending on command type */
148         switch(cmd)
149                 {
150         case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
151                 idx++;
152                 if(int_ctrl_cmd_is_null(e->cmd_defns + idx))
153                         /* end-of-list */
154                         return 0;
155                 else
156                         return e->cmd_defns[idx].cmd_num;
157         case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
158                 return strlen(e->cmd_defns[idx].cmd_name);
159         case ENGINE_CTRL_GET_NAME_FROM_CMD:
160                 return BIO_snprintf(s,strlen(e->cmd_defns[idx].cmd_name) + 1,
161                                     "%s", e->cmd_defns[idx].cmd_name);
162         case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
163                 if(e->cmd_defns[idx].cmd_desc)
164                         return strlen(e->cmd_defns[idx].cmd_desc);
165                 return strlen(int_no_description);
166         case ENGINE_CTRL_GET_DESC_FROM_CMD:
167                 if(e->cmd_defns[idx].cmd_desc)
168                         return BIO_snprintf(s,
169                                             strlen(e->cmd_defns[idx].cmd_desc) + 1,
170                                             "%s", e->cmd_defns[idx].cmd_desc);
171                 return BIO_snprintf(s, strlen(int_no_description) + 1,"%s",
172                                     int_no_description);
173         case ENGINE_CTRL_GET_CMD_FLAGS:
174                 return e->cmd_defns[idx].cmd_flags;
175                 }
176         /* Shouldn't really be here ... */
177         ENGINEerr(ENGINE_F_INT_CTRL_HELPER,ENGINE_R_INTERNAL_LIST_ERROR);
178         return -1;
179         }
180
181 int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
182         {
183         int ctrl_exists, ref_exists;
184         if(e == NULL)
185                 {
186                 ENGINEerr(ENGINE_F_ENGINE_CTRL,ERR_R_PASSED_NULL_PARAMETER);
187                 return 0;
188                 }
189         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
190         ref_exists = ((e->struct_ref > 0) ? 1 : 0);
191         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
192         ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
193         if(!ref_exists)
194                 {
195                 ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_REFERENCE);
196                 return 0;
197                 }
198         /* Intercept any "root-level" commands before trying to hand them on to
199          * ctrl() handlers. */
200         switch(cmd)
201                 {
202         case ENGINE_CTRL_HAS_CTRL_FUNCTION:
203                 return ctrl_exists;
204         case ENGINE_CTRL_GET_FIRST_CMD_TYPE:
205         case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
206         case ENGINE_CTRL_GET_CMD_FROM_NAME:
207         case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
208         case ENGINE_CTRL_GET_NAME_FROM_CMD:
209         case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
210         case ENGINE_CTRL_GET_DESC_FROM_CMD:
211         case ENGINE_CTRL_GET_CMD_FLAGS:
212                 if(ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL))
213                         return int_ctrl_helper(e,cmd,i,p,f);
214                 if(!ctrl_exists)
215                         {
216                         ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_CONTROL_FUNCTION);
217                         /* For these cmd-related functions, failure is indicated
218                          * by a -1 return value (because 0 is used as a valid
219                          * return in some places). */
220                         return -1;
221                         }
222         default:
223                 break;
224                 }
225         /* Anything else requires a ctrl() handler to exist. */
226         if(!ctrl_exists)
227                 {
228                 ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_CONTROL_FUNCTION);
229                 return 0;
230                 }
231         return e->ctrl(e, cmd, i, p, f);
232         }
233
234 int ENGINE_cmd_is_executable(ENGINE *e, int cmd)
235         {
236         int flags;
237         if((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0)
238                 {
239                 ENGINEerr(ENGINE_F_ENGINE_CMD_IS_EXECUTABLE,
240                         ENGINE_R_INVALID_CMD_NUMBER);
241                 return 0;
242                 }
243         if(!(flags & ENGINE_CMD_FLAG_NO_INPUT) &&
244                         !(flags & ENGINE_CMD_FLAG_NUMERIC) &&
245                         !(flags & ENGINE_CMD_FLAG_STRING))
246                 return 0;
247         return 1;
248         }
249
250 int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
251         long i, void *p, void (*f)(void), int cmd_optional)
252         {
253         int num;
254
255         if((e == NULL) || (cmd_name == NULL))
256                 {
257                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
258                         ERR_R_PASSED_NULL_PARAMETER);
259                 return 0;
260                 }
261         if((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
262                                         ENGINE_CTRL_GET_CMD_FROM_NAME,
263                                         0, (void *)cmd_name, NULL)) <= 0))
264                 {
265                 /* If the command didn't *have* to be supported, we fake
266                  * success. This allows certain settings to be specified for
267                  * multiple ENGINEs and only require a change of ENGINE id
268                  * (without having to selectively apply settings). Eg. changing
269                  * from a hardware device back to the regular software ENGINE
270                  * without editing the config file, etc. */
271                 if(cmd_optional)
272                         {
273                         ERR_clear_error();
274                         return 1;
275                         }
276                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD,
277                         ENGINE_R_INVALID_CMD_NAME);
278                 return 0;
279                 }
280         /* Force the result of the control command to 0 or 1, for the reasons
281          * mentioned before. */
282         if (ENGINE_ctrl(e, num, i, p, f))
283                 return 1;
284         return 0;
285         }
286
287 int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
288                                 int cmd_optional)
289         {
290         int num, flags;
291         long l;
292         char *ptr;
293         if((e == NULL) || (cmd_name == NULL))
294                 {
295                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
296                         ERR_R_PASSED_NULL_PARAMETER);
297                 return 0;
298                 }
299         if((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
300                                         ENGINE_CTRL_GET_CMD_FROM_NAME,
301                                         0, (void *)cmd_name, NULL)) <= 0))
302                 {
303                 /* If the command didn't *have* to be supported, we fake
304                  * success. This allows certain settings to be specified for
305                  * multiple ENGINEs and only require a change of ENGINE id
306                  * (without having to selectively apply settings). Eg. changing
307                  * from a hardware device back to the regular software ENGINE
308                  * without editing the config file, etc. */
309                 if(cmd_optional)
310                         {
311                         ERR_clear_error();
312                         return 1;
313                         }
314                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
315                         ENGINE_R_INVALID_CMD_NAME);
316                 return 0;
317                 }
318         if(!ENGINE_cmd_is_executable(e, num))
319                 {
320                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
321                         ENGINE_R_CMD_NOT_EXECUTABLE);
322                 return 0;
323                 }
324         if((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL)) < 0)
325                 {
326                 /* Shouldn't happen, given that ENGINE_cmd_is_executable()
327                  * returned success. */
328                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
329                         ENGINE_R_INTERNAL_LIST_ERROR);
330                 return 0;
331                 }
332         /* If the command takes no input, there must be no input. And vice
333          * versa. */
334         if(flags & ENGINE_CMD_FLAG_NO_INPUT)
335                 {
336                 if(arg != NULL)
337                         {
338                         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
339                                 ENGINE_R_COMMAND_TAKES_NO_INPUT);
340                         return 0;
341                         }
342                 /* We deliberately force the result of ENGINE_ctrl() to 0 or 1
343                  * rather than returning it as "return data". This is to ensure
344                  * usage of these commands is consistent across applications and
345                  * that certain applications don't understand it one way, and
346                  * others another. */
347                 if(ENGINE_ctrl(e, num, 0, (void *)arg, NULL))
348                         return 1;
349                 return 0;
350                 }
351         /* So, we require input */
352         if(arg == NULL)
353                 {
354                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
355                         ENGINE_R_COMMAND_TAKES_INPUT);
356                 return 0;
357                 }
358         /* If it takes string input, that's easy */
359         if(flags & ENGINE_CMD_FLAG_STRING)
360                 {
361                 /* Same explanation as above */
362                 if(ENGINE_ctrl(e, num, 0, (void *)arg, NULL))
363                         return 1;
364                 return 0;
365                 }
366         /* If it doesn't take numeric either, then it is unsupported for use in
367          * a config-setting situation, which is what this function is for. This
368          * should never happen though, because ENGINE_cmd_is_executable() was
369          * used. */
370         if(!(flags & ENGINE_CMD_FLAG_NUMERIC))
371                 {
372                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
373                         ENGINE_R_INTERNAL_LIST_ERROR);
374                 return 0;
375                 }
376         l = strtol(arg, &ptr, 10);
377         if((arg == ptr) || (*ptr != '\0'))
378                 {
379                 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
380                         ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER);
381                 return 0;
382                 }
383         /* Force the result of the control command to 0 or 1, for the reasons
384          * mentioned before. */
385         if(ENGINE_ctrl(e, num, l, NULL, NULL))
386                 return 1;
387         return 0;
388         }