1109998Smarkm/* crypto/engine/eng_ctrl.c */
2109998Smarkm/* ====================================================================
3109998Smarkm * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
4109998Smarkm *
5109998Smarkm * Redistribution and use in source and binary forms, with or without
6109998Smarkm * modification, are permitted provided that the following conditions
7109998Smarkm * are met:
8109998Smarkm *
9109998Smarkm * 1. Redistributions of source code must retain the above copyright
10296341Sdelphij *    notice, this list of conditions and the following disclaimer.
11109998Smarkm *
12109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
13109998Smarkm *    notice, this list of conditions and the following disclaimer in
14109998Smarkm *    the documentation and/or other materials provided with the
15109998Smarkm *    distribution.
16109998Smarkm *
17109998Smarkm * 3. All advertising materials mentioning features or use of this
18109998Smarkm *    software must display the following acknowledgment:
19109998Smarkm *    "This product includes software developed by the OpenSSL Project
20109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21109998Smarkm *
22109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23109998Smarkm *    endorse or promote products derived from this software without
24109998Smarkm *    prior written permission. For written permission, please contact
25109998Smarkm *    licensing@OpenSSL.org.
26109998Smarkm *
27109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
28109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
29109998Smarkm *    permission of the OpenSSL Project.
30109998Smarkm *
31109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
32109998Smarkm *    acknowledgment:
33109998Smarkm *    "This product includes software developed by the OpenSSL Project
34109998Smarkm *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35109998Smarkm *
36109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
48109998Smarkm * ====================================================================
49109998Smarkm *
50109998Smarkm * This product includes cryptographic software written by Eric Young
51109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
52109998Smarkm * Hudson (tjh@cryptsoft.com).
53109998Smarkm *
54109998Smarkm */
55109998Smarkm
56109998Smarkm#include "eng_int.h"
57109998Smarkm
58296341Sdelphij/*
59296341Sdelphij * When querying a ENGINE-specific control command's 'description', this
60296341Sdelphij * string is used if the ENGINE_CMD_DEFN has cmd_desc set to NULL.
61296341Sdelphij */
62109998Smarkmstatic const char *int_no_description = "";
63109998Smarkm
64296341Sdelphij/*
65296341Sdelphij * These internal functions handle 'CMD'-related control commands when the
66109998Smarkm * ENGINE in question has asked us to take care of it (ie. the ENGINE did not
67296341Sdelphij * set the ENGINE_FLAGS_MANUAL_CMD_CTRL flag.
68296341Sdelphij */
69109998Smarkm
70109998Smarkmstatic int int_ctrl_cmd_is_null(const ENGINE_CMD_DEFN *defn)
71296341Sdelphij{
72296341Sdelphij    if ((defn->cmd_num == 0) || (defn->cmd_name == NULL))
73296341Sdelphij        return 1;
74296341Sdelphij    return 0;
75296341Sdelphij}
76109998Smarkm
77109998Smarkmstatic int int_ctrl_cmd_by_name(const ENGINE_CMD_DEFN *defn, const char *s)
78296341Sdelphij{
79296341Sdelphij    int idx = 0;
80296341Sdelphij    while (!int_ctrl_cmd_is_null(defn) && (strcmp(defn->cmd_name, s) != 0)) {
81296341Sdelphij        idx++;
82296341Sdelphij        defn++;
83296341Sdelphij    }
84296341Sdelphij    if (int_ctrl_cmd_is_null(defn))
85296341Sdelphij        /* The given name wasn't found */
86296341Sdelphij        return -1;
87296341Sdelphij    return idx;
88296341Sdelphij}
89109998Smarkm
90109998Smarkmstatic int int_ctrl_cmd_by_num(const ENGINE_CMD_DEFN *defn, unsigned int num)
91296341Sdelphij{
92296341Sdelphij    int idx = 0;
93296341Sdelphij    /*
94296341Sdelphij     * NB: It is stipulated that 'cmd_defn' lists are ordered by cmd_num. So
95296341Sdelphij     * our searches don't need to take any longer than necessary.
96296341Sdelphij     */
97296341Sdelphij    while (!int_ctrl_cmd_is_null(defn) && (defn->cmd_num < num)) {
98296341Sdelphij        idx++;
99296341Sdelphij        defn++;
100296341Sdelphij    }
101296341Sdelphij    if (defn->cmd_num == num)
102296341Sdelphij        return idx;
103296341Sdelphij    /* The given cmd_num wasn't found */
104296341Sdelphij    return -1;
105296341Sdelphij}
106109998Smarkm
107160814Ssimonstatic int int_ctrl_helper(ENGINE *e, int cmd, long i, void *p,
108296341Sdelphij                           void (*f) (void))
109296341Sdelphij{
110296341Sdelphij    int idx;
111296341Sdelphij    char *s = (char *)p;
112296341Sdelphij    /* Take care of the easy one first (eg. it requires no searches) */
113296341Sdelphij    if (cmd == ENGINE_CTRL_GET_FIRST_CMD_TYPE) {
114296341Sdelphij        if ((e->cmd_defns == NULL) || int_ctrl_cmd_is_null(e->cmd_defns))
115296341Sdelphij            return 0;
116296341Sdelphij        return e->cmd_defns->cmd_num;
117296341Sdelphij    }
118296341Sdelphij    /* One or two commands require that "p" be a valid string buffer */
119296341Sdelphij    if ((cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) ||
120296341Sdelphij        (cmd == ENGINE_CTRL_GET_NAME_FROM_CMD) ||
121296341Sdelphij        (cmd == ENGINE_CTRL_GET_DESC_FROM_CMD)) {
122296341Sdelphij        if (s == NULL) {
123296341Sdelphij            ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ERR_R_PASSED_NULL_PARAMETER);
124296341Sdelphij            return -1;
125296341Sdelphij        }
126296341Sdelphij    }
127296341Sdelphij    /* Now handle cmd_name -> cmd_num conversion */
128296341Sdelphij    if (cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) {
129296341Sdelphij        if ((e->cmd_defns == NULL)
130296341Sdelphij            || ((idx = int_ctrl_cmd_by_name(e->cmd_defns, s)) < 0)) {
131296341Sdelphij            ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INVALID_CMD_NAME);
132296341Sdelphij            return -1;
133296341Sdelphij        }
134296341Sdelphij        return e->cmd_defns[idx].cmd_num;
135296341Sdelphij    }
136296341Sdelphij    /*
137296341Sdelphij     * For the rest of the commands, the 'long' argument must specify a valie
138296341Sdelphij     * command number - so we need to conduct a search.
139296341Sdelphij     */
140296341Sdelphij    if ((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_num(e->cmd_defns,
141296341Sdelphij                                                              (unsigned int)
142296341Sdelphij                                                              i)) < 0)) {
143296341Sdelphij        ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INVALID_CMD_NUMBER);
144296341Sdelphij        return -1;
145296341Sdelphij    }
146296341Sdelphij    /* Now the logic splits depending on command type */
147296341Sdelphij    switch (cmd) {
148296341Sdelphij    case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
149296341Sdelphij        idx++;
150296341Sdelphij        if (int_ctrl_cmd_is_null(e->cmd_defns + idx))
151296341Sdelphij            /* end-of-list */
152296341Sdelphij            return 0;
153296341Sdelphij        else
154296341Sdelphij            return e->cmd_defns[idx].cmd_num;
155296341Sdelphij    case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
156296341Sdelphij        return strlen(e->cmd_defns[idx].cmd_name);
157296341Sdelphij    case ENGINE_CTRL_GET_NAME_FROM_CMD:
158296341Sdelphij        return BIO_snprintf(s, strlen(e->cmd_defns[idx].cmd_name) + 1,
159296341Sdelphij                            "%s", e->cmd_defns[idx].cmd_name);
160296341Sdelphij    case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
161296341Sdelphij        if (e->cmd_defns[idx].cmd_desc)
162296341Sdelphij            return strlen(e->cmd_defns[idx].cmd_desc);
163296341Sdelphij        return strlen(int_no_description);
164296341Sdelphij    case ENGINE_CTRL_GET_DESC_FROM_CMD:
165296341Sdelphij        if (e->cmd_defns[idx].cmd_desc)
166296341Sdelphij            return BIO_snprintf(s,
167296341Sdelphij                                strlen(e->cmd_defns[idx].cmd_desc) + 1,
168296341Sdelphij                                "%s", e->cmd_defns[idx].cmd_desc);
169296341Sdelphij        return BIO_snprintf(s, strlen(int_no_description) + 1, "%s",
170296341Sdelphij                            int_no_description);
171296341Sdelphij    case ENGINE_CTRL_GET_CMD_FLAGS:
172296341Sdelphij        return e->cmd_defns[idx].cmd_flags;
173296341Sdelphij    }
174296341Sdelphij    /* Shouldn't really be here ... */
175296341Sdelphij    ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INTERNAL_LIST_ERROR);
176296341Sdelphij    return -1;
177296341Sdelphij}
178109998Smarkm
179296341Sdelphijint ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
180296341Sdelphij{
181296341Sdelphij    int ctrl_exists, ref_exists;
182296341Sdelphij    if (e == NULL) {
183296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_CTRL, ERR_R_PASSED_NULL_PARAMETER);
184296341Sdelphij        return 0;
185296341Sdelphij    }
186296341Sdelphij    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
187296341Sdelphij    ref_exists = ((e->struct_ref > 0) ? 1 : 0);
188296341Sdelphij    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
189296341Sdelphij    ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
190296341Sdelphij    if (!ref_exists) {
191296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_REFERENCE);
192296341Sdelphij        return 0;
193296341Sdelphij    }
194296341Sdelphij    /*
195296341Sdelphij     * Intercept any "root-level" commands before trying to hand them on to
196296341Sdelphij     * ctrl() handlers.
197296341Sdelphij     */
198296341Sdelphij    switch (cmd) {
199296341Sdelphij    case ENGINE_CTRL_HAS_CTRL_FUNCTION:
200296341Sdelphij        return ctrl_exists;
201296341Sdelphij    case ENGINE_CTRL_GET_FIRST_CMD_TYPE:
202296341Sdelphij    case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
203296341Sdelphij    case ENGINE_CTRL_GET_CMD_FROM_NAME:
204296341Sdelphij    case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
205296341Sdelphij    case ENGINE_CTRL_GET_NAME_FROM_CMD:
206296341Sdelphij    case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
207296341Sdelphij    case ENGINE_CTRL_GET_DESC_FROM_CMD:
208296341Sdelphij    case ENGINE_CTRL_GET_CMD_FLAGS:
209296341Sdelphij        if (ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL))
210296341Sdelphij            return int_ctrl_helper(e, cmd, i, p, f);
211296341Sdelphij        if (!ctrl_exists) {
212296341Sdelphij            ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION);
213296341Sdelphij            /*
214296341Sdelphij             * For these cmd-related functions, failure is indicated by a -1
215296341Sdelphij             * return value (because 0 is used as a valid return in some
216296341Sdelphij             * places).
217296341Sdelphij             */
218296341Sdelphij            return -1;
219296341Sdelphij        }
220296341Sdelphij    default:
221296341Sdelphij        break;
222296341Sdelphij    }
223296341Sdelphij    /* Anything else requires a ctrl() handler to exist. */
224296341Sdelphij    if (!ctrl_exists) {
225296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION);
226296341Sdelphij        return 0;
227296341Sdelphij    }
228296341Sdelphij    return e->ctrl(e, cmd, i, p, f);
229296341Sdelphij}
230109998Smarkm
231109998Smarkmint ENGINE_cmd_is_executable(ENGINE *e, int cmd)
232296341Sdelphij{
233296341Sdelphij    int flags;
234296341Sdelphij    if ((flags =
235296341Sdelphij         ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0) {
236296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_CMD_IS_EXECUTABLE,
237296341Sdelphij                  ENGINE_R_INVALID_CMD_NUMBER);
238296341Sdelphij        return 0;
239296341Sdelphij    }
240296341Sdelphij    if (!(flags & ENGINE_CMD_FLAG_NO_INPUT) &&
241296341Sdelphij        !(flags & ENGINE_CMD_FLAG_NUMERIC) &&
242296341Sdelphij        !(flags & ENGINE_CMD_FLAG_STRING))
243296341Sdelphij        return 0;
244296341Sdelphij    return 1;
245296341Sdelphij}
246109998Smarkm
247109998Smarkmint ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
248296341Sdelphij                    long i, void *p, void (*f) (void), int cmd_optional)
249296341Sdelphij{
250296341Sdelphij    int num;
251109998Smarkm
252296341Sdelphij    if ((e == NULL) || (cmd_name == NULL)) {
253296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, ERR_R_PASSED_NULL_PARAMETER);
254109998Smarkm        return 0;
255296341Sdelphij    }
256296341Sdelphij    if ((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
257296341Sdelphij                                                 ENGINE_CTRL_GET_CMD_FROM_NAME,
258296341Sdelphij                                                 0, (void *)cmd_name,
259296341Sdelphij                                                 NULL)) <= 0)) {
260296341Sdelphij        /*
261296341Sdelphij         * If the command didn't *have* to be supported, we fake success.
262296341Sdelphij         * This allows certain settings to be specified for multiple ENGINEs
263296341Sdelphij         * and only require a change of ENGINE id (without having to
264296341Sdelphij         * selectively apply settings). Eg. changing from a hardware device
265296341Sdelphij         * back to the regular software ENGINE without editing the config
266296341Sdelphij         * file, etc.
267296341Sdelphij         */
268296341Sdelphij        if (cmd_optional) {
269296341Sdelphij            ERR_clear_error();
270296341Sdelphij            return 1;
271109998Smarkm        }
272296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, ENGINE_R_INVALID_CMD_NAME);
273296341Sdelphij        return 0;
274296341Sdelphij    }
275296341Sdelphij    /*
276296341Sdelphij     * Force the result of the control command to 0 or 1, for the reasons
277296341Sdelphij     * mentioned before.
278296341Sdelphij     */
279296341Sdelphij    if (ENGINE_ctrl(e, num, i, p, f) > 0)
280296341Sdelphij        return 1;
281296341Sdelphij    return 0;
282296341Sdelphij}
283109998Smarkm
284109998Smarkmint ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
285296341Sdelphij                           int cmd_optional)
286296341Sdelphij{
287296341Sdelphij    int num, flags;
288296341Sdelphij    long l;
289296341Sdelphij    char *ptr;
290296341Sdelphij    if ((e == NULL) || (cmd_name == NULL)) {
291296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
292296341Sdelphij                  ERR_R_PASSED_NULL_PARAMETER);
293296341Sdelphij        return 0;
294296341Sdelphij    }
295296341Sdelphij    if ((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
296296341Sdelphij                                                 ENGINE_CTRL_GET_CMD_FROM_NAME,
297296341Sdelphij                                                 0, (void *)cmd_name,
298296341Sdelphij                                                 NULL)) <= 0)) {
299296341Sdelphij        /*
300296341Sdelphij         * If the command didn't *have* to be supported, we fake success.
301296341Sdelphij         * This allows certain settings to be specified for multiple ENGINEs
302296341Sdelphij         * and only require a change of ENGINE id (without having to
303296341Sdelphij         * selectively apply settings). Eg. changing from a hardware device
304296341Sdelphij         * back to the regular software ENGINE without editing the config
305296341Sdelphij         * file, etc.
306296341Sdelphij         */
307296341Sdelphij        if (cmd_optional) {
308296341Sdelphij            ERR_clear_error();
309296341Sdelphij            return 1;
310296341Sdelphij        }
311296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, ENGINE_R_INVALID_CMD_NAME);
312296341Sdelphij        return 0;
313296341Sdelphij    }
314296341Sdelphij    if (!ENGINE_cmd_is_executable(e, num)) {
315296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
316296341Sdelphij                  ENGINE_R_CMD_NOT_EXECUTABLE);
317296341Sdelphij        return 0;
318296341Sdelphij    }
319296341Sdelphij    if ((flags =
320296341Sdelphij         ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL)) < 0) {
321296341Sdelphij        /*
322296341Sdelphij         * Shouldn't happen, given that ENGINE_cmd_is_executable() returned
323296341Sdelphij         * success.
324296341Sdelphij         */
325296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
326296341Sdelphij                  ENGINE_R_INTERNAL_LIST_ERROR);
327296341Sdelphij        return 0;
328296341Sdelphij    }
329296341Sdelphij    /*
330296341Sdelphij     * If the command takes no input, there must be no input. And vice versa.
331296341Sdelphij     */
332296341Sdelphij    if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
333296341Sdelphij        if (arg != NULL) {
334296341Sdelphij            ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
335296341Sdelphij                      ENGINE_R_COMMAND_TAKES_NO_INPUT);
336296341Sdelphij            return 0;
337296341Sdelphij        }
338296341Sdelphij        /*
339296341Sdelphij         * We deliberately force the result of ENGINE_ctrl() to 0 or 1 rather
340296341Sdelphij         * than returning it as "return data". This is to ensure usage of
341296341Sdelphij         * these commands is consistent across applications and that certain
342296341Sdelphij         * applications don't understand it one way, and others another.
343296341Sdelphij         */
344296341Sdelphij        if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
345296341Sdelphij            return 1;
346296341Sdelphij        return 0;
347296341Sdelphij    }
348296341Sdelphij    /* So, we require input */
349296341Sdelphij    if (arg == NULL) {
350296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
351296341Sdelphij                  ENGINE_R_COMMAND_TAKES_INPUT);
352296341Sdelphij        return 0;
353296341Sdelphij    }
354296341Sdelphij    /* If it takes string input, that's easy */
355296341Sdelphij    if (flags & ENGINE_CMD_FLAG_STRING) {
356296341Sdelphij        /* Same explanation as above */
357296341Sdelphij        if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
358296341Sdelphij            return 1;
359296341Sdelphij        return 0;
360296341Sdelphij    }
361296341Sdelphij    /*
362296341Sdelphij     * If it doesn't take numeric either, then it is unsupported for use in a
363296341Sdelphij     * config-setting situation, which is what this function is for. This
364296341Sdelphij     * should never happen though, because ENGINE_cmd_is_executable() was
365296341Sdelphij     * used.
366296341Sdelphij     */
367296341Sdelphij    if (!(flags & ENGINE_CMD_FLAG_NUMERIC)) {
368296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
369296341Sdelphij                  ENGINE_R_INTERNAL_LIST_ERROR);
370296341Sdelphij        return 0;
371296341Sdelphij    }
372296341Sdelphij    l = strtol(arg, &ptr, 10);
373296341Sdelphij    if ((arg == ptr) || (*ptr != '\0')) {
374296341Sdelphij        ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
375296341Sdelphij                  ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER);
376296341Sdelphij        return 0;
377296341Sdelphij    }
378296341Sdelphij    /*
379296341Sdelphij     * Force the result of the control command to 0 or 1, for the reasons
380296341Sdelphij     * mentioned before.
381296341Sdelphij     */
382296341Sdelphij    if (ENGINE_ctrl(e, num, l, NULL, NULL) > 0)
383296341Sdelphij        return 1;
384296341Sdelphij    return 0;
385296341Sdelphij}
386