|
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 © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. |
|
57 */ |
|
58 #include "eng_int.h" |
|
59 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) |
|
60 #include "libcrypto_wsd_macros.h" |
|
61 #include "libcrypto_wsd.h" |
|
62 #endif |
|
63 |
|
64 /* When querying a ENGINE-specific control command's 'description', this string |
|
65 * is used if the ENGINE_CMD_DEFN has cmd_desc set to NULL. */ |
|
66 #ifndef EMULATOR |
|
67 static const char *int_no_description = ""; |
|
68 #else |
|
69 static const char *const int_no_description = ""; |
|
70 #endif |
|
71 |
|
72 /* These internal functions handle 'CMD'-related control commands when the |
|
73 * ENGINE in question has asked us to take care of it (ie. the ENGINE did not |
|
74 * set the ENGINE_FLAGS_MANUAL_CMD_CTRL flag. */ |
|
75 |
|
76 static int int_ctrl_cmd_is_null(const ENGINE_CMD_DEFN *defn) |
|
77 { |
|
78 if((defn->cmd_num == 0) || (defn->cmd_name == NULL)) |
|
79 return 1; |
|
80 return 0; |
|
81 } |
|
82 |
|
83 static int int_ctrl_cmd_by_name(const ENGINE_CMD_DEFN *defn, const char *s) |
|
84 { |
|
85 int idx = 0; |
|
86 while(!int_ctrl_cmd_is_null(defn) && (strcmp(defn->cmd_name, s) != 0)) |
|
87 { |
|
88 idx++; |
|
89 defn++; |
|
90 } |
|
91 if(int_ctrl_cmd_is_null(defn)) |
|
92 /* The given name wasn't found */ |
|
93 return -1; |
|
94 return idx; |
|
95 } |
|
96 |
|
97 static int int_ctrl_cmd_by_num(const ENGINE_CMD_DEFN *defn, unsigned int num) |
|
98 { |
|
99 int idx = 0; |
|
100 /* NB: It is stipulated that 'cmd_defn' lists are ordered by cmd_num. So |
|
101 * our searches don't need to take any longer than necessary. */ |
|
102 while(!int_ctrl_cmd_is_null(defn) && (defn->cmd_num < num)) |
|
103 { |
|
104 idx++; |
|
105 defn++; |
|
106 } |
|
107 if(defn->cmd_num == num) |
|
108 return idx; |
|
109 /* The given cmd_num wasn't found */ |
|
110 return -1; |
|
111 } |
|
112 |
|
113 static int int_ctrl_helper(ENGINE *e, int cmd, long i, void *p, |
|
114 void (*f)(void)) |
|
115 { |
|
116 int idx; |
|
117 char *s = (char *)p; |
|
118 /* Take care of the easy one first (eg. it requires no searches) */ |
|
119 if(cmd == ENGINE_CTRL_GET_FIRST_CMD_TYPE) |
|
120 { |
|
121 if((e->cmd_defns == NULL) || int_ctrl_cmd_is_null(e->cmd_defns)) |
|
122 return 0; |
|
123 return e->cmd_defns->cmd_num; |
|
124 } |
|
125 /* One or two commands require that "p" be a valid string buffer */ |
|
126 if((cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) || |
|
127 (cmd == ENGINE_CTRL_GET_NAME_FROM_CMD) || |
|
128 (cmd == ENGINE_CTRL_GET_DESC_FROM_CMD)) |
|
129 { |
|
130 if(s == NULL) |
|
131 { |
|
132 ENGINEerr(ENGINE_F_INT_CTRL_HELPER, |
|
133 ERR_R_PASSED_NULL_PARAMETER); |
|
134 return -1; |
|
135 } |
|
136 } |
|
137 /* Now handle cmd_name -> cmd_num conversion */ |
|
138 if(cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) |
|
139 { |
|
140 if((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_name( |
|
141 e->cmd_defns, s)) < 0)) |
|
142 { |
|
143 ENGINEerr(ENGINE_F_INT_CTRL_HELPER, |
|
144 ENGINE_R_INVALID_CMD_NAME); |
|
145 return -1; |
|
146 } |
|
147 return e->cmd_defns[idx].cmd_num; |
|
148 } |
|
149 /* For the rest of the commands, the 'long' argument must specify a |
|
150 * valie command number - so we need to conduct a search. */ |
|
151 if((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_num(e->cmd_defns, |
|
152 (unsigned int)i)) < 0)) |
|
153 { |
|
154 ENGINEerr(ENGINE_F_INT_CTRL_HELPER, |
|
155 ENGINE_R_INVALID_CMD_NUMBER); |
|
156 return -1; |
|
157 } |
|
158 /* Now the logic splits depending on command type */ |
|
159 switch(cmd) |
|
160 { |
|
161 case ENGINE_CTRL_GET_NEXT_CMD_TYPE: |
|
162 idx++; |
|
163 if(int_ctrl_cmd_is_null(e->cmd_defns + idx)) |
|
164 /* end-of-list */ |
|
165 return 0; |
|
166 else |
|
167 return e->cmd_defns[idx].cmd_num; |
|
168 case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD: |
|
169 return strlen(e->cmd_defns[idx].cmd_name); |
|
170 case ENGINE_CTRL_GET_NAME_FROM_CMD: |
|
171 return BIO_snprintf(s,strlen(e->cmd_defns[idx].cmd_name) + 1, |
|
172 "%s", e->cmd_defns[idx].cmd_name); |
|
173 case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD: |
|
174 if(e->cmd_defns[idx].cmd_desc) |
|
175 return strlen(e->cmd_defns[idx].cmd_desc); |
|
176 return strlen(int_no_description); |
|
177 case ENGINE_CTRL_GET_DESC_FROM_CMD: |
|
178 if(e->cmd_defns[idx].cmd_desc) |
|
179 return BIO_snprintf(s, |
|
180 strlen(e->cmd_defns[idx].cmd_desc) + 1, |
|
181 "%s", e->cmd_defns[idx].cmd_desc); |
|
182 return BIO_snprintf(s, strlen(int_no_description) + 1,"%s", |
|
183 int_no_description); |
|
184 case ENGINE_CTRL_GET_CMD_FLAGS: |
|
185 return e->cmd_defns[idx].cmd_flags; |
|
186 } |
|
187 /* Shouldn't really be here ... */ |
|
188 ENGINEerr(ENGINE_F_INT_CTRL_HELPER,ENGINE_R_INTERNAL_LIST_ERROR); |
|
189 return -1; |
|
190 } |
|
191 |
|
192 EXPORT_C int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void)) |
|
193 { |
|
194 int ctrl_exists, ref_exists; |
|
195 if(e == NULL) |
|
196 { |
|
197 ENGINEerr(ENGINE_F_ENGINE_CTRL,ERR_R_PASSED_NULL_PARAMETER); |
|
198 return 0; |
|
199 } |
|
200 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); |
|
201 ref_exists = ((e->struct_ref > 0) ? 1 : 0); |
|
202 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); |
|
203 ctrl_exists = ((e->ctrl == NULL) ? 0 : 1); |
|
204 if(!ref_exists) |
|
205 { |
|
206 ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_REFERENCE); |
|
207 return 0; |
|
208 } |
|
209 /* Intercept any "root-level" commands before trying to hand them on to |
|
210 * ctrl() handlers. */ |
|
211 switch(cmd) |
|
212 { |
|
213 case ENGINE_CTRL_HAS_CTRL_FUNCTION: |
|
214 return ctrl_exists; |
|
215 case ENGINE_CTRL_GET_FIRST_CMD_TYPE: |
|
216 case ENGINE_CTRL_GET_NEXT_CMD_TYPE: |
|
217 case ENGINE_CTRL_GET_CMD_FROM_NAME: |
|
218 case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD: |
|
219 case ENGINE_CTRL_GET_NAME_FROM_CMD: |
|
220 case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD: |
|
221 case ENGINE_CTRL_GET_DESC_FROM_CMD: |
|
222 case ENGINE_CTRL_GET_CMD_FLAGS: |
|
223 if(ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL)) |
|
224 return int_ctrl_helper(e,cmd,i,p,f); |
|
225 if(!ctrl_exists) |
|
226 { |
|
227 ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_CONTROL_FUNCTION); |
|
228 /* For these cmd-related functions, failure is indicated |
|
229 * by a -1 return value (because 0 is used as a valid |
|
230 * return in some places). */ |
|
231 return -1; |
|
232 } |
|
233 default: |
|
234 break; |
|
235 } |
|
236 /* Anything else requires a ctrl() handler to exist. */ |
|
237 if(!ctrl_exists) |
|
238 { |
|
239 ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_CONTROL_FUNCTION); |
|
240 return 0; |
|
241 } |
|
242 return e->ctrl(e, cmd, i, p, f); |
|
243 } |
|
244 |
|
245 EXPORT_C int ENGINE_cmd_is_executable(ENGINE *e, int cmd) |
|
246 { |
|
247 int flags; |
|
248 if((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0) |
|
249 { |
|
250 ENGINEerr(ENGINE_F_ENGINE_CMD_IS_EXECUTABLE, |
|
251 ENGINE_R_INVALID_CMD_NUMBER); |
|
252 return 0; |
|
253 } |
|
254 if(!(flags & ENGINE_CMD_FLAG_NO_INPUT) && |
|
255 !(flags & ENGINE_CMD_FLAG_NUMERIC) && |
|
256 !(flags & ENGINE_CMD_FLAG_STRING)) |
|
257 return 0; |
|
258 return 1; |
|
259 } |
|
260 |
|
261 EXPORT_C int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name, |
|
262 long i, void *p, void (*f)(void), int cmd_optional) |
|
263 { |
|
264 int num; |
|
265 |
|
266 if((e == NULL) || (cmd_name == NULL)) |
|
267 { |
|
268 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, |
|
269 ERR_R_PASSED_NULL_PARAMETER); |
|
270 return 0; |
|
271 } |
|
272 if((e->ctrl == NULL) || ((num = ENGINE_ctrl(e, |
|
273 ENGINE_CTRL_GET_CMD_FROM_NAME, |
|
274 0, (void *)cmd_name, NULL)) <= 0)) |
|
275 { |
|
276 /* If the command didn't *have* to be supported, we fake |
|
277 * success. This allows certain settings to be specified for |
|
278 * multiple ENGINEs and only require a change of ENGINE id |
|
279 * (without having to selectively apply settings). Eg. changing |
|
280 * from a hardware device back to the regular software ENGINE |
|
281 * without editing the config file, etc. */ |
|
282 if(cmd_optional) |
|
283 { |
|
284 ERR_clear_error(); |
|
285 return 1; |
|
286 } |
|
287 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, |
|
288 ENGINE_R_INVALID_CMD_NAME); |
|
289 return 0; |
|
290 } |
|
291 /* Force the result of the control command to 0 or 1, for the reasons |
|
292 * mentioned before. */ |
|
293 if (ENGINE_ctrl(e, num, i, p, f)) |
|
294 return 1; |
|
295 return 0; |
|
296 } |
|
297 |
|
298 EXPORT_C int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg, |
|
299 int cmd_optional) |
|
300 { |
|
301 int num, flags; |
|
302 long l; |
|
303 char *ptr; |
|
304 if((e == NULL) || (cmd_name == NULL)) |
|
305 { |
|
306 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, |
|
307 ERR_R_PASSED_NULL_PARAMETER); |
|
308 return 0; |
|
309 } |
|
310 if((e->ctrl == NULL) || ((num = ENGINE_ctrl(e, |
|
311 ENGINE_CTRL_GET_CMD_FROM_NAME, |
|
312 0, (void *)cmd_name, NULL)) <= 0)) |
|
313 { |
|
314 /* If the command didn't *have* to be supported, we fake |
|
315 * success. This allows certain settings to be specified for |
|
316 * multiple ENGINEs and only require a change of ENGINE id |
|
317 * (without having to selectively apply settings). Eg. changing |
|
318 * from a hardware device back to the regular software ENGINE |
|
319 * without editing the config file, etc. */ |
|
320 if(cmd_optional) |
|
321 { |
|
322 ERR_clear_error(); |
|
323 return 1; |
|
324 } |
|
325 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, |
|
326 ENGINE_R_INVALID_CMD_NAME); |
|
327 return 0; |
|
328 } |
|
329 if(!ENGINE_cmd_is_executable(e, num)) |
|
330 { |
|
331 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, |
|
332 ENGINE_R_CMD_NOT_EXECUTABLE); |
|
333 return 0; |
|
334 } |
|
335 if((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL)) < 0) |
|
336 { |
|
337 /* Shouldn't happen, given that ENGINE_cmd_is_executable() |
|
338 * returned success. */ |
|
339 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, |
|
340 ENGINE_R_INTERNAL_LIST_ERROR); |
|
341 return 0; |
|
342 } |
|
343 /* If the command takes no input, there must be no input. And vice |
|
344 * versa. */ |
|
345 if(flags & ENGINE_CMD_FLAG_NO_INPUT) |
|
346 { |
|
347 if(arg != NULL) |
|
348 { |
|
349 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, |
|
350 ENGINE_R_COMMAND_TAKES_NO_INPUT); |
|
351 return 0; |
|
352 } |
|
353 /* We deliberately force the result of ENGINE_ctrl() to 0 or 1 |
|
354 * rather than returning it as "return data". This is to ensure |
|
355 * usage of these commands is consistent across applications and |
|
356 * that certain applications don't understand it one way, and |
|
357 * others another. */ |
|
358 if(ENGINE_ctrl(e, num, 0, (void *)arg, NULL)) |
|
359 return 1; |
|
360 return 0; |
|
361 } |
|
362 /* So, we require input */ |
|
363 if(arg == NULL) |
|
364 { |
|
365 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, |
|
366 ENGINE_R_COMMAND_TAKES_INPUT); |
|
367 return 0; |
|
368 } |
|
369 /* If it takes string input, that's easy */ |
|
370 if(flags & ENGINE_CMD_FLAG_STRING) |
|
371 { |
|
372 /* Same explanation as above */ |
|
373 if(ENGINE_ctrl(e, num, 0, (void *)arg, NULL)) |
|
374 return 1; |
|
375 return 0; |
|
376 } |
|
377 /* If it doesn't take numeric either, then it is unsupported for use in |
|
378 * a config-setting situation, which is what this function is for. This |
|
379 * should never happen though, because ENGINE_cmd_is_executable() was |
|
380 * used. */ |
|
381 if(!(flags & ENGINE_CMD_FLAG_NUMERIC)) |
|
382 { |
|
383 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, |
|
384 ENGINE_R_INTERNAL_LIST_ERROR); |
|
385 return 0; |
|
386 } |
|
387 l = strtol(arg, &ptr, 10); |
|
388 if((arg == ptr) || (*ptr != '\0')) |
|
389 { |
|
390 ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, |
|
391 ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER); |
|
392 return 0; |
|
393 } |
|
394 /* Force the result of the control command to 0 or 1, for the reasons |
|
395 * mentioned before. */ |
|
396 if(ENGINE_ctrl(e, num, l, NULL, NULL)) |
|
397 return 1; |
|
398 return 0; |
|
399 } |