Coverage Report

Created: 2026-02-14 07:20

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