Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/engine/eng_ctrl.c
Line
Count
Source (jump to first uncovered line)
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) ||
79
0
        (cmd == ENGINE_CTRL_GET_NAME_FROM_CMD) ||
80
0
        (cmd == ENGINE_CTRL_GET_DESC_FROM_CMD)) {
81
0
        if (s == NULL) {
82
0
            ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
83
0
            return -1;
84
0
        }
85
0
    }
86
    /* Now handle cmd_name -> cmd_num conversion */
87
0
    if (cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) {
88
0
        if ((e->cmd_defns == NULL)
89
0
            || ((idx = int_ctrl_cmd_by_name(e->cmd_defns, s)) < 0)) {
90
0
            ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NAME);
91
0
            return -1;
92
0
        }
93
0
        return e->cmd_defns[idx].cmd_num;
94
0
    }
95
    /*
96
     * For the rest of the commands, the 'long' argument must specify a valid
97
     * command number - so we need to conduct a search.
98
     */
99
0
    if ((e->cmd_defns == NULL)
100
0
        || ((idx = int_ctrl_cmd_by_num(e->cmd_defns, (unsigned int)i)) < 0)) {
101
0
        ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NUMBER);
102
0
        return -1;
103
0
    }
104
    /* Now the logic splits depending on command type */
105
0
    cdp = &e->cmd_defns[idx];
106
0
    switch (cmd) {
107
0
    case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
108
0
        cdp++;
109
0
        return int_ctrl_cmd_is_null(cdp) ? 0 : cdp->cmd_num;
110
0
    case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
111
0
        return strlen(cdp->cmd_name);
112
0
    case ENGINE_CTRL_GET_NAME_FROM_CMD:
113
0
        return strlen(strcpy(s, cdp->cmd_name));
114
0
    case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
115
0
        return strlen(cdp->cmd_desc == NULL ? int_no_description
116
0
                                            : cdp->cmd_desc);
117
0
    case ENGINE_CTRL_GET_DESC_FROM_CMD:
118
0
        return strlen(strcpy(s, cdp->cmd_desc == NULL ? int_no_description
119
0
                                                      : cdp->cmd_desc));
120
0
    case ENGINE_CTRL_GET_CMD_FLAGS:
121
0
        return cdp->cmd_flags;
122
0
    }
123
    /* Shouldn't really be here ... */
124
0
    ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
125
0
    return -1;
126
0
}
127
128
int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
129
0
{
130
0
    int ctrl_exists, ref_exists;
131
0
    if (e == NULL) {
132
0
        ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
133
0
        return 0;
134
0
    }
135
0
    if (!CRYPTO_THREAD_write_lock(global_engine_lock))
136
0
        return 0;
137
0
    ref_exists = ((e->struct_ref > 0) ? 1 : 0);
138
0
    CRYPTO_THREAD_unlock(global_engine_lock);
139
0
    ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
140
0
    if (!ref_exists) {
141
0
        ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_REFERENCE);
142
0
        return 0;
143
0
    }
144
    /*
145
     * Intercept any "root-level" commands before trying to hand them on to
146
     * ctrl() handlers.
147
     */
148
0
    switch (cmd) {
149
0
    case ENGINE_CTRL_HAS_CTRL_FUNCTION:
150
0
        return ctrl_exists;
151
0
    case ENGINE_CTRL_GET_FIRST_CMD_TYPE:
152
0
    case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
153
0
    case ENGINE_CTRL_GET_CMD_FROM_NAME:
154
0
    case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
155
0
    case ENGINE_CTRL_GET_NAME_FROM_CMD:
156
0
    case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
157
0
    case ENGINE_CTRL_GET_DESC_FROM_CMD:
158
0
    case ENGINE_CTRL_GET_CMD_FLAGS:
159
0
        if (ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL))
160
0
            return int_ctrl_helper(e, cmd, i, p, f);
161
0
        if (!ctrl_exists) {
162
0
            ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_CONTROL_FUNCTION);
163
            /*
164
             * For these cmd-related functions, failure is indicated by a -1
165
             * return value (because 0 is used as a valid return in some
166
             * places).
167
             */
168
0
            return -1;
169
0
        }
170
0
    default:
171
0
        break;
172
0
    }
173
    /* Anything else requires a ctrl() handler to exist. */
174
0
    if (!ctrl_exists) {
175
0
        ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_CONTROL_FUNCTION);
176
0
        return 0;
177
0
    }
178
0
    return e->ctrl(e, cmd, i, p, f);
179
0
}
180
181
int ENGINE_cmd_is_executable(ENGINE *e, int cmd)
182
0
{
183
0
    int flags;
184
0
    if ((flags =
185
0
         ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0) {
186
0
        ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NUMBER);
187
0
        return 0;
188
0
    }
189
0
    if (!(flags & ENGINE_CMD_FLAG_NO_INPUT) &&
190
0
        !(flags & ENGINE_CMD_FLAG_NUMERIC) &&
191
0
        !(flags & ENGINE_CMD_FLAG_STRING))
192
0
        return 0;
193
0
    return 1;
194
0
}
195
196
int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
197
                    long i, void *p, void (*f) (void), int cmd_optional)
198
0
{
199
0
    int num;
200
201
0
    if (e == NULL || cmd_name == NULL) {
202
0
        ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
203
0
        return 0;
204
0
    }
205
0
    if (e->ctrl == NULL
206
0
        || (num = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FROM_NAME,
207
0
                              0, (void *)cmd_name, NULL)) <= 0) {
208
        /*
209
         * If the command didn't *have* to be supported, we fake success.
210
         * This allows certain settings to be specified for multiple ENGINEs
211
         * and only require a change of ENGINE id (without having to
212
         * selectively apply settings). Eg. changing from a hardware device
213
         * back to the regular software ENGINE without editing the config
214
         * file, etc.
215
         */
216
0
        if (cmd_optional) {
217
0
            ERR_clear_error();
218
0
            return 1;
219
0
        }
220
0
        ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NAME);
221
0
        return 0;
222
0
    }
223
    /*
224
     * Force the result of the control command to 0 or 1, for the reasons
225
     * mentioned before.
226
     */
227
0
    if (ENGINE_ctrl(e, num, i, p, f) > 0)
228
0
        return 1;
229
0
    return 0;
230
0
}
231
232
int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
233
                           int cmd_optional)
234
0
{
235
0
    int num, flags;
236
0
    long l;
237
0
    char *ptr;
238
239
0
    if (e == NULL || cmd_name == NULL) {
240
0
        ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
241
0
        return 0;
242
0
    }
243
0
    if (e->ctrl == NULL
244
0
        || (num = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FROM_NAME,
245
0
                              0, (void *)cmd_name, NULL)) <= 0) {
246
        /*
247
         * If the command didn't *have* to be supported, we fake success.
248
         * This allows certain settings to be specified for multiple ENGINEs
249
         * and only require a change of ENGINE id (without having to
250
         * selectively apply settings). Eg. changing from a hardware device
251
         * back to the regular software ENGINE without editing the config
252
         * file, etc.
253
         */
254
0
        if (cmd_optional) {
255
0
            ERR_clear_error();
256
0
            return 1;
257
0
        }
258
0
        ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_CMD_NAME);
259
0
        return 0;
260
0
    }
261
0
    if (!ENGINE_cmd_is_executable(e, num)) {
262
0
        ERR_raise(ERR_LIB_ENGINE, ENGINE_R_CMD_NOT_EXECUTABLE);
263
0
        return 0;
264
0
    }
265
266
0
    flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL);
267
0
    if (flags < 0) {
268
        /*
269
         * Shouldn't happen, given that ENGINE_cmd_is_executable() returned
270
         * success.
271
         */
272
0
        ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
273
0
        return 0;
274
0
    }
275
    /*
276
     * If the command takes no input, there must be no input. And vice versa.
277
     */
278
0
    if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
279
0
        if (arg != NULL) {
280
0
            ERR_raise(ERR_LIB_ENGINE, ENGINE_R_COMMAND_TAKES_NO_INPUT);
281
0
            return 0;
282
0
        }
283
        /*
284
         * We deliberately force the result of ENGINE_ctrl() to 0 or 1 rather
285
         * than returning it as "return data". This is to ensure usage of
286
         * these commands is consistent across applications and that certain
287
         * applications don't understand it one way, and others another.
288
         */
289
0
        if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
290
0
            return 1;
291
0
        return 0;
292
0
    }
293
    /* So, we require input */
294
0
    if (arg == NULL) {
295
0
        ERR_raise(ERR_LIB_ENGINE, ENGINE_R_COMMAND_TAKES_INPUT);
296
0
        return 0;
297
0
    }
298
    /* If it takes string input, that's easy */
299
0
    if (flags & ENGINE_CMD_FLAG_STRING) {
300
        /* Same explanation as above */
301
0
        if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0)
302
0
            return 1;
303
0
        return 0;
304
0
    }
305
    /*
306
     * If it doesn't take numeric either, then it is unsupported for use in a
307
     * config-setting situation, which is what this function is for. This
308
     * should never happen though, because ENGINE_cmd_is_executable() was
309
     * used.
310
     */
311
0
    if (!(flags & ENGINE_CMD_FLAG_NUMERIC)) {
312
0
        ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INTERNAL_LIST_ERROR);
313
0
        return 0;
314
0
    }
315
0
    l = strtol(arg, &ptr, 10);
316
0
    if ((arg == ptr) || (*ptr != '\0')) {
317
0
        ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER);
318
0
        return 0;
319
0
    }
320
    /*
321
     * Force the result of the control command to 0 or 1, for the reasons
322
     * mentioned before.
323
     */
324
0
    if (ENGINE_ctrl(e, num, l, NULL, NULL) > 0)
325
0
        return 1;
326
0
    return 0;
327
0
}