Add X509_CHECK_FLAG_NEVER_CHECK_SUBJECT flag
[openssl.git] / crypto / engine / eng_ctrl.c
1 /* ====================================================================
2  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
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
14  *    distribution.
15  *
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/)"
20  *
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.
25  *
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.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the OpenSSL Project
33  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34  *
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  * ====================================================================
48  *
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).
52  *
53  */
54
55 #include "eng_int.h"
56
57 /*
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.
60  */
61 static const char *int_no_description = "";
62
63 /*
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.
67  */
68
69 static int int_ctrl_cmd_is_null(const ENGINE_CMD_DEFN *defn)
70 {
71     if ((defn->cmd_num == 0) || (defn->cmd_name == NULL))
72         return 1;
73     return 0;
74 }
75
76 static int int_ctrl_cmd_by_name(const ENGINE_CMD_DEFN *defn, const char *s)
77 {
78     int idx = 0;
79     while (!int_ctrl_cmd_is_null(defn) && (strcmp(defn->cmd_name, s) != 0)) {
80         idx++;
81         defn++;
82     }
83     if (int_ctrl_cmd_is_null(defn))
84         /* The given name wasn't found */
85         return -1;
86     return idx;
87 }
88
89 static int int_ctrl_cmd_by_num(const ENGINE_CMD_DEFN *defn, unsigned int num)
90 {
91     int idx = 0;
92     /*
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.
95      */
96     while (!int_ctrl_cmd_is_null(defn) && (defn->cmd_num < num)) {
97         idx++;
98         defn++;
99     }
100     if (defn->cmd_num == num)
101         return idx;
102     /* The given cmd_num wasn't found */
103     return -1;
104 }
105
106 static int int_ctrl_helper(ENGINE *e, int cmd, long i, void *p,
107                            void (*f) (void))
108 {
109     int idx;
110     char *s = (char *)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))
114             return 0;
115         return e->cmd_defns->cmd_num;
116     }
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)) {
121         if (s == NULL) {
122             ENGINEerr(ENGINE_F_INT_CTRL_HELPER, 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         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);
131             return -1;
132         }
133         return e->cmd_defns[idx].cmd_num;
134     }
135     /*
136      * For the rest of the commands, the 'long' argument must specify a valid
137      * command number - so we need to conduct a search.
138      */
139     if ((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_num(e->cmd_defns,
140                                                               (unsigned int)
141                                                               i)) < 0)) {
142         ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INVALID_CMD_NUMBER);
143         return -1;
144     }
145     /* Now the logic splits depending on command type */
146     switch (cmd) {
147     case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
148         idx++;
149         if (int_ctrl_cmd_is_null(e->cmd_defns + idx))
150             /* end-of-list */
151             return 0;
152         else
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",
169                             int_no_description);
170     case ENGINE_CTRL_GET_CMD_FLAGS:
171         return e->cmd_defns[idx].cmd_flags;
172     }
173     /* Shouldn't really be here ... */
174     ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INTERNAL_LIST_ERROR);
175     return -1;
176 }
177
178 int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
179 {
180     int ctrl_exists, ref_exists;
181     if (e == NULL) {
182         ENGINEerr(ENGINE_F_ENGINE_CTRL, ERR_R_PASSED_NULL_PARAMETER);
183         return 0;
184     }
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);
189     if (!ref_exists) {
190         ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_REFERENCE);
191         return 0;
192     }
193     /*
194      * Intercept any "root-level" commands before trying to hand them on to
195      * ctrl() handlers.
196      */
197     switch (cmd) {
198     case ENGINE_CTRL_HAS_CTRL_FUNCTION:
199         return ctrl_exists;
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);
210         if (!ctrl_exists) {
211             ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION);
212             /*
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
215              * places).
216              */
217             return -1;
218         }
219     default:
220         break;
221     }
222     /* Anything else requires a ctrl() handler to exist. */
223     if (!ctrl_exists) {
224         ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION);
225         return 0;
226     }
227     return e->ctrl(e, cmd, i, p, f);
228 }
229
230 int ENGINE_cmd_is_executable(ENGINE *e, int cmd)
231 {
232     int flags;
233     if ((flags =
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);
237         return 0;
238     }
239     if (!(flags & ENGINE_CMD_FLAG_NO_INPUT) &&
240         !(flags & ENGINE_CMD_FLAG_NUMERIC) &&
241         !(flags & ENGINE_CMD_FLAG_STRING))
242         return 0;
243     return 1;
244 }
245
246 int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
247                     long i, void *p, void (*f) (void), int cmd_optional)
248 {
249     int num;
250
251     if ((e == NULL) || (cmd_name == NULL)) {
252         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, ERR_R_PASSED_NULL_PARAMETER);
253         return 0;
254     }
255     if ((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
256                                                  ENGINE_CTRL_GET_CMD_FROM_NAME,
257                                                  0, (void *)cmd_name,
258                                                  NULL)) <= 0)) {
259         /*
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
265          * file, etc.
266          */
267         if (cmd_optional) {
268             ERR_clear_error();
269             return 1;
270         }
271         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, ENGINE_R_INVALID_CMD_NAME);
272         return 0;
273     }
274     /*
275      * Force the result of the control command to 0 or 1, for the reasons
276      * mentioned before.
277      */
278     if (ENGINE_ctrl(e, num, i, p, f) > 0)
279         return 1;
280     return 0;
281 }
282
283 int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
284                            int cmd_optional)
285 {
286     int num, flags;
287     long l;
288     char *ptr;
289     if ((e == NULL) || (cmd_name == NULL)) {
290         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
291                   ERR_R_PASSED_NULL_PARAMETER);
292         return 0;
293     }
294     if ((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
295                                                  ENGINE_CTRL_GET_CMD_FROM_NAME,
296                                                  0, (void *)cmd_name,
297                                                  NULL)) <= 0)) {
298         /*
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
304          * file, etc.
305          */
306         if (cmd_optional) {
307             ERR_clear_error();
308             return 1;
309         }
310         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, ENGINE_R_INVALID_CMD_NAME);
311         return 0;
312     }
313     if (!ENGINE_cmd_is_executable(e, num)) {
314         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
315                   ENGINE_R_CMD_NOT_EXECUTABLE);
316         return 0;
317     }
318     if ((flags =
319          ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL)) < 0) {
320         /*
321          * Shouldn't happen, given that ENGINE_cmd_is_executable() returned
322          * success.
323          */
324         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
325                   ENGINE_R_INTERNAL_LIST_ERROR);
326         return 0;
327     }
328     /*
329      * If the command takes no input, there must be no input. And vice versa.
330      */
331     if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
332         if (arg != NULL) {
333             ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
334                       ENGINE_R_COMMAND_TAKES_NO_INPUT);
335             return 0;
336         }
337         /*
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.
342          */
343         if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
344             return 1;
345         return 0;
346     }
347     /* So, we require input */
348     if (arg == NULL) {
349         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
350                   ENGINE_R_COMMAND_TAKES_INPUT);
351         return 0;
352     }
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)
357             return 1;
358         return 0;
359     }
360     /*
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
364      * used.
365      */
366     if (!(flags & ENGINE_CMD_FLAG_NUMERIC)) {
367         ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
368                   ENGINE_R_INTERNAL_LIST_ERROR);
369         return 0;
370     }
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);
375         return 0;
376     }
377     /*
378      * Force the result of the control command to 0 or 1, for the reasons
379      * mentioned before.
380      */
381     if (ENGINE_ctrl(e, num, l, NULL, NULL) > 0)
382         return 1;
383     return 0;
384 }