1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/engine/eng_ctrl.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,399 @@
1.4 +/* crypto/engine/eng_ctrl.c */
1.5 +/* ====================================================================
1.6 + * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
1.7 + *
1.8 + * Redistribution and use in source and binary forms, with or without
1.9 + * modification, are permitted provided that the following conditions
1.10 + * are met:
1.11 + *
1.12 + * 1. Redistributions of source code must retain the above copyright
1.13 + * notice, this list of conditions and the following disclaimer.
1.14 + *
1.15 + * 2. Redistributions in binary form must reproduce the above copyright
1.16 + * notice, this list of conditions and the following disclaimer in
1.17 + * the documentation and/or other materials provided with the
1.18 + * distribution.
1.19 + *
1.20 + * 3. All advertising materials mentioning features or use of this
1.21 + * software must display the following acknowledgment:
1.22 + * "This product includes software developed by the OpenSSL Project
1.23 + * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
1.24 + *
1.25 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
1.26 + * endorse or promote products derived from this software without
1.27 + * prior written permission. For written permission, please contact
1.28 + * licensing@OpenSSL.org.
1.29 + *
1.30 + * 5. Products derived from this software may not be called "OpenSSL"
1.31 + * nor may "OpenSSL" appear in their names without prior written
1.32 + * permission of the OpenSSL Project.
1.33 + *
1.34 + * 6. Redistributions of any form whatsoever must retain the following
1.35 + * acknowledgment:
1.36 + * "This product includes software developed by the OpenSSL Project
1.37 + * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
1.38 + *
1.39 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
1.40 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.41 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.42 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
1.43 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1.44 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1.45 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1.46 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.47 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.48 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1.49 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1.50 + * OF THE POSSIBILITY OF SUCH DAMAGE.
1.51 + * ====================================================================
1.52 + *
1.53 + * This product includes cryptographic software written by Eric Young
1.54 + * (eay@cryptsoft.com). This product includes software written by Tim
1.55 + * Hudson (tjh@cryptsoft.com).
1.56 + *
1.57 + */
1.58 +/*
1.59 + © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
1.60 + */
1.61 +#include "eng_int.h"
1.62 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
1.63 +#include "libcrypto_wsd_macros.h"
1.64 +#include "libcrypto_wsd.h"
1.65 +#endif
1.66 +
1.67 +/* When querying a ENGINE-specific control command's 'description', this string
1.68 + * is used if the ENGINE_CMD_DEFN has cmd_desc set to NULL. */
1.69 +#ifndef EMULATOR
1.70 +static const char *int_no_description = "";
1.71 +#else
1.72 +static const char *const int_no_description = "";
1.73 +#endif
1.74 +
1.75 +/* These internal functions handle 'CMD'-related control commands when the
1.76 + * ENGINE in question has asked us to take care of it (ie. the ENGINE did not
1.77 + * set the ENGINE_FLAGS_MANUAL_CMD_CTRL flag. */
1.78 +
1.79 +static int int_ctrl_cmd_is_null(const ENGINE_CMD_DEFN *defn)
1.80 + {
1.81 + if((defn->cmd_num == 0) || (defn->cmd_name == NULL))
1.82 + return 1;
1.83 + return 0;
1.84 + }
1.85 +
1.86 +static int int_ctrl_cmd_by_name(const ENGINE_CMD_DEFN *defn, const char *s)
1.87 + {
1.88 + int idx = 0;
1.89 + while(!int_ctrl_cmd_is_null(defn) && (strcmp(defn->cmd_name, s) != 0))
1.90 + {
1.91 + idx++;
1.92 + defn++;
1.93 + }
1.94 + if(int_ctrl_cmd_is_null(defn))
1.95 + /* The given name wasn't found */
1.96 + return -1;
1.97 + return idx;
1.98 + }
1.99 +
1.100 +static int int_ctrl_cmd_by_num(const ENGINE_CMD_DEFN *defn, unsigned int num)
1.101 + {
1.102 + int idx = 0;
1.103 + /* NB: It is stipulated that 'cmd_defn' lists are ordered by cmd_num. So
1.104 + * our searches don't need to take any longer than necessary. */
1.105 + while(!int_ctrl_cmd_is_null(defn) && (defn->cmd_num < num))
1.106 + {
1.107 + idx++;
1.108 + defn++;
1.109 + }
1.110 + if(defn->cmd_num == num)
1.111 + return idx;
1.112 + /* The given cmd_num wasn't found */
1.113 + return -1;
1.114 + }
1.115 +
1.116 +static int int_ctrl_helper(ENGINE *e, int cmd, long i, void *p,
1.117 + void (*f)(void))
1.118 + {
1.119 + int idx;
1.120 + char *s = (char *)p;
1.121 + /* Take care of the easy one first (eg. it requires no searches) */
1.122 + if(cmd == ENGINE_CTRL_GET_FIRST_CMD_TYPE)
1.123 + {
1.124 + if((e->cmd_defns == NULL) || int_ctrl_cmd_is_null(e->cmd_defns))
1.125 + return 0;
1.126 + return e->cmd_defns->cmd_num;
1.127 + }
1.128 + /* One or two commands require that "p" be a valid string buffer */
1.129 + if((cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) ||
1.130 + (cmd == ENGINE_CTRL_GET_NAME_FROM_CMD) ||
1.131 + (cmd == ENGINE_CTRL_GET_DESC_FROM_CMD))
1.132 + {
1.133 + if(s == NULL)
1.134 + {
1.135 + ENGINEerr(ENGINE_F_INT_CTRL_HELPER,
1.136 + ERR_R_PASSED_NULL_PARAMETER);
1.137 + return -1;
1.138 + }
1.139 + }
1.140 + /* Now handle cmd_name -> cmd_num conversion */
1.141 + if(cmd == ENGINE_CTRL_GET_CMD_FROM_NAME)
1.142 + {
1.143 + if((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_name(
1.144 + e->cmd_defns, s)) < 0))
1.145 + {
1.146 + ENGINEerr(ENGINE_F_INT_CTRL_HELPER,
1.147 + ENGINE_R_INVALID_CMD_NAME);
1.148 + return -1;
1.149 + }
1.150 + return e->cmd_defns[idx].cmd_num;
1.151 + }
1.152 + /* For the rest of the commands, the 'long' argument must specify a
1.153 + * valie command number - so we need to conduct a search. */
1.154 + if((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_num(e->cmd_defns,
1.155 + (unsigned int)i)) < 0))
1.156 + {
1.157 + ENGINEerr(ENGINE_F_INT_CTRL_HELPER,
1.158 + ENGINE_R_INVALID_CMD_NUMBER);
1.159 + return -1;
1.160 + }
1.161 + /* Now the logic splits depending on command type */
1.162 + switch(cmd)
1.163 + {
1.164 + case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
1.165 + idx++;
1.166 + if(int_ctrl_cmd_is_null(e->cmd_defns + idx))
1.167 + /* end-of-list */
1.168 + return 0;
1.169 + else
1.170 + return e->cmd_defns[idx].cmd_num;
1.171 + case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
1.172 + return strlen(e->cmd_defns[idx].cmd_name);
1.173 + case ENGINE_CTRL_GET_NAME_FROM_CMD:
1.174 + return BIO_snprintf(s,strlen(e->cmd_defns[idx].cmd_name) + 1,
1.175 + "%s", e->cmd_defns[idx].cmd_name);
1.176 + case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
1.177 + if(e->cmd_defns[idx].cmd_desc)
1.178 + return strlen(e->cmd_defns[idx].cmd_desc);
1.179 + return strlen(int_no_description);
1.180 + case ENGINE_CTRL_GET_DESC_FROM_CMD:
1.181 + if(e->cmd_defns[idx].cmd_desc)
1.182 + return BIO_snprintf(s,
1.183 + strlen(e->cmd_defns[idx].cmd_desc) + 1,
1.184 + "%s", e->cmd_defns[idx].cmd_desc);
1.185 + return BIO_snprintf(s, strlen(int_no_description) + 1,"%s",
1.186 + int_no_description);
1.187 + case ENGINE_CTRL_GET_CMD_FLAGS:
1.188 + return e->cmd_defns[idx].cmd_flags;
1.189 + }
1.190 + /* Shouldn't really be here ... */
1.191 + ENGINEerr(ENGINE_F_INT_CTRL_HELPER,ENGINE_R_INTERNAL_LIST_ERROR);
1.192 + return -1;
1.193 + }
1.194 +
1.195 +EXPORT_C int ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
1.196 + {
1.197 + int ctrl_exists, ref_exists;
1.198 + if(e == NULL)
1.199 + {
1.200 + ENGINEerr(ENGINE_F_ENGINE_CTRL,ERR_R_PASSED_NULL_PARAMETER);
1.201 + return 0;
1.202 + }
1.203 + CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
1.204 + ref_exists = ((e->struct_ref > 0) ? 1 : 0);
1.205 + CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
1.206 + ctrl_exists = ((e->ctrl == NULL) ? 0 : 1);
1.207 + if(!ref_exists)
1.208 + {
1.209 + ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_REFERENCE);
1.210 + return 0;
1.211 + }
1.212 + /* Intercept any "root-level" commands before trying to hand them on to
1.213 + * ctrl() handlers. */
1.214 + switch(cmd)
1.215 + {
1.216 + case ENGINE_CTRL_HAS_CTRL_FUNCTION:
1.217 + return ctrl_exists;
1.218 + case ENGINE_CTRL_GET_FIRST_CMD_TYPE:
1.219 + case ENGINE_CTRL_GET_NEXT_CMD_TYPE:
1.220 + case ENGINE_CTRL_GET_CMD_FROM_NAME:
1.221 + case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD:
1.222 + case ENGINE_CTRL_GET_NAME_FROM_CMD:
1.223 + case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD:
1.224 + case ENGINE_CTRL_GET_DESC_FROM_CMD:
1.225 + case ENGINE_CTRL_GET_CMD_FLAGS:
1.226 + if(ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL))
1.227 + return int_ctrl_helper(e,cmd,i,p,f);
1.228 + if(!ctrl_exists)
1.229 + {
1.230 + ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_CONTROL_FUNCTION);
1.231 + /* For these cmd-related functions, failure is indicated
1.232 + * by a -1 return value (because 0 is used as a valid
1.233 + * return in some places). */
1.234 + return -1;
1.235 + }
1.236 + default:
1.237 + break;
1.238 + }
1.239 + /* Anything else requires a ctrl() handler to exist. */
1.240 + if(!ctrl_exists)
1.241 + {
1.242 + ENGINEerr(ENGINE_F_ENGINE_CTRL,ENGINE_R_NO_CONTROL_FUNCTION);
1.243 + return 0;
1.244 + }
1.245 + return e->ctrl(e, cmd, i, p, f);
1.246 + }
1.247 +
1.248 +EXPORT_C int ENGINE_cmd_is_executable(ENGINE *e, int cmd)
1.249 + {
1.250 + int flags;
1.251 + if((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0)
1.252 + {
1.253 + ENGINEerr(ENGINE_F_ENGINE_CMD_IS_EXECUTABLE,
1.254 + ENGINE_R_INVALID_CMD_NUMBER);
1.255 + return 0;
1.256 + }
1.257 + if(!(flags & ENGINE_CMD_FLAG_NO_INPUT) &&
1.258 + !(flags & ENGINE_CMD_FLAG_NUMERIC) &&
1.259 + !(flags & ENGINE_CMD_FLAG_STRING))
1.260 + return 0;
1.261 + return 1;
1.262 + }
1.263 +
1.264 +EXPORT_C int ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name,
1.265 + long i, void *p, void (*f)(void), int cmd_optional)
1.266 + {
1.267 + int num;
1.268 +
1.269 + if((e == NULL) || (cmd_name == NULL))
1.270 + {
1.271 + ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD,
1.272 + ERR_R_PASSED_NULL_PARAMETER);
1.273 + return 0;
1.274 + }
1.275 + if((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
1.276 + ENGINE_CTRL_GET_CMD_FROM_NAME,
1.277 + 0, (void *)cmd_name, NULL)) <= 0))
1.278 + {
1.279 + /* If the command didn't *have* to be supported, we fake
1.280 + * success. This allows certain settings to be specified for
1.281 + * multiple ENGINEs and only require a change of ENGINE id
1.282 + * (without having to selectively apply settings). Eg. changing
1.283 + * from a hardware device back to the regular software ENGINE
1.284 + * without editing the config file, etc. */
1.285 + if(cmd_optional)
1.286 + {
1.287 + ERR_clear_error();
1.288 + return 1;
1.289 + }
1.290 + ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD,
1.291 + ENGINE_R_INVALID_CMD_NAME);
1.292 + return 0;
1.293 + }
1.294 + /* Force the result of the control command to 0 or 1, for the reasons
1.295 + * mentioned before. */
1.296 + if (ENGINE_ctrl(e, num, i, p, f))
1.297 + return 1;
1.298 + return 0;
1.299 + }
1.300 +
1.301 +EXPORT_C int ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg,
1.302 + int cmd_optional)
1.303 + {
1.304 + int num, flags;
1.305 + long l;
1.306 + char *ptr;
1.307 + if((e == NULL) || (cmd_name == NULL))
1.308 + {
1.309 + ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
1.310 + ERR_R_PASSED_NULL_PARAMETER);
1.311 + return 0;
1.312 + }
1.313 + if((e->ctrl == NULL) || ((num = ENGINE_ctrl(e,
1.314 + ENGINE_CTRL_GET_CMD_FROM_NAME,
1.315 + 0, (void *)cmd_name, NULL)) <= 0))
1.316 + {
1.317 + /* If the command didn't *have* to be supported, we fake
1.318 + * success. This allows certain settings to be specified for
1.319 + * multiple ENGINEs and only require a change of ENGINE id
1.320 + * (without having to selectively apply settings). Eg. changing
1.321 + * from a hardware device back to the regular software ENGINE
1.322 + * without editing the config file, etc. */
1.323 + if(cmd_optional)
1.324 + {
1.325 + ERR_clear_error();
1.326 + return 1;
1.327 + }
1.328 + ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
1.329 + ENGINE_R_INVALID_CMD_NAME);
1.330 + return 0;
1.331 + }
1.332 + if(!ENGINE_cmd_is_executable(e, num))
1.333 + {
1.334 + ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
1.335 + ENGINE_R_CMD_NOT_EXECUTABLE);
1.336 + return 0;
1.337 + }
1.338 + if((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL)) < 0)
1.339 + {
1.340 + /* Shouldn't happen, given that ENGINE_cmd_is_executable()
1.341 + * returned success. */
1.342 + ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
1.343 + ENGINE_R_INTERNAL_LIST_ERROR);
1.344 + return 0;
1.345 + }
1.346 + /* If the command takes no input, there must be no input. And vice
1.347 + * versa. */
1.348 + if(flags & ENGINE_CMD_FLAG_NO_INPUT)
1.349 + {
1.350 + if(arg != NULL)
1.351 + {
1.352 + ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
1.353 + ENGINE_R_COMMAND_TAKES_NO_INPUT);
1.354 + return 0;
1.355 + }
1.356 + /* We deliberately force the result of ENGINE_ctrl() to 0 or 1
1.357 + * rather than returning it as "return data". This is to ensure
1.358 + * usage of these commands is consistent across applications and
1.359 + * that certain applications don't understand it one way, and
1.360 + * others another. */
1.361 + if(ENGINE_ctrl(e, num, 0, (void *)arg, NULL))
1.362 + return 1;
1.363 + return 0;
1.364 + }
1.365 + /* So, we require input */
1.366 + if(arg == NULL)
1.367 + {
1.368 + ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
1.369 + ENGINE_R_COMMAND_TAKES_INPUT);
1.370 + return 0;
1.371 + }
1.372 + /* If it takes string input, that's easy */
1.373 + if(flags & ENGINE_CMD_FLAG_STRING)
1.374 + {
1.375 + /* Same explanation as above */
1.376 + if(ENGINE_ctrl(e, num, 0, (void *)arg, NULL))
1.377 + return 1;
1.378 + return 0;
1.379 + }
1.380 + /* If it doesn't take numeric either, then it is unsupported for use in
1.381 + * a config-setting situation, which is what this function is for. This
1.382 + * should never happen though, because ENGINE_cmd_is_executable() was
1.383 + * used. */
1.384 + if(!(flags & ENGINE_CMD_FLAG_NUMERIC))
1.385 + {
1.386 + ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
1.387 + ENGINE_R_INTERNAL_LIST_ERROR);
1.388 + return 0;
1.389 + }
1.390 + l = strtol(arg, &ptr, 10);
1.391 + if((arg == ptr) || (*ptr != '\0'))
1.392 + {
1.393 + ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING,
1.394 + ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER);
1.395 + return 0;
1.396 + }
1.397 + /* Force the result of the control command to 0 or 1, for the reasons
1.398 + * mentioned before. */
1.399 + if(ENGINE_ctrl(e, num, l, NULL, NULL))
1.400 + return 1;
1.401 + return 0;
1.402 + }