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 10280304Sjkim * 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 58280304Sjkim/* 59280304Sjkim * When querying a ENGINE-specific control command's 'description', this 60280304Sjkim * string is used if the ENGINE_CMD_DEFN has cmd_desc set to NULL. 61280304Sjkim */ 62109998Smarkmstatic const char *int_no_description = ""; 63109998Smarkm 64280304Sjkim/* 65280304Sjkim * 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 67280304Sjkim * set the ENGINE_FLAGS_MANUAL_CMD_CTRL flag. 68280304Sjkim */ 69109998Smarkm 70109998Smarkmstatic int int_ctrl_cmd_is_null(const ENGINE_CMD_DEFN *defn) 71280304Sjkim{ 72280304Sjkim if ((defn->cmd_num == 0) || (defn->cmd_name == NULL)) 73280304Sjkim return 1; 74280304Sjkim return 0; 75280304Sjkim} 76109998Smarkm 77109998Smarkmstatic int int_ctrl_cmd_by_name(const ENGINE_CMD_DEFN *defn, const char *s) 78280304Sjkim{ 79280304Sjkim int idx = 0; 80280304Sjkim while (!int_ctrl_cmd_is_null(defn) && (strcmp(defn->cmd_name, s) != 0)) { 81280304Sjkim idx++; 82280304Sjkim defn++; 83280304Sjkim } 84280304Sjkim if (int_ctrl_cmd_is_null(defn)) 85280304Sjkim /* The given name wasn't found */ 86280304Sjkim return -1; 87280304Sjkim return idx; 88280304Sjkim} 89109998Smarkm 90109998Smarkmstatic int int_ctrl_cmd_by_num(const ENGINE_CMD_DEFN *defn, unsigned int num) 91280304Sjkim{ 92280304Sjkim int idx = 0; 93280304Sjkim /* 94280304Sjkim * NB: It is stipulated that 'cmd_defn' lists are ordered by cmd_num. So 95280304Sjkim * our searches don't need to take any longer than necessary. 96280304Sjkim */ 97280304Sjkim while (!int_ctrl_cmd_is_null(defn) && (defn->cmd_num < num)) { 98280304Sjkim idx++; 99280304Sjkim defn++; 100280304Sjkim } 101280304Sjkim if (defn->cmd_num == num) 102280304Sjkim return idx; 103280304Sjkim /* The given cmd_num wasn't found */ 104280304Sjkim return -1; 105280304Sjkim} 106109998Smarkm 107160814Ssimonstatic int int_ctrl_helper(ENGINE *e, int cmd, long i, void *p, 108280304Sjkim void (*f) (void)) 109280304Sjkim{ 110280304Sjkim int idx; 111280304Sjkim char *s = (char *)p; 112280304Sjkim /* Take care of the easy one first (eg. it requires no searches) */ 113280304Sjkim if (cmd == ENGINE_CTRL_GET_FIRST_CMD_TYPE) { 114280304Sjkim if ((e->cmd_defns == NULL) || int_ctrl_cmd_is_null(e->cmd_defns)) 115280304Sjkim return 0; 116280304Sjkim return e->cmd_defns->cmd_num; 117280304Sjkim } 118280304Sjkim /* One or two commands require that "p" be a valid string buffer */ 119280304Sjkim if ((cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) || 120280304Sjkim (cmd == ENGINE_CTRL_GET_NAME_FROM_CMD) || 121280304Sjkim (cmd == ENGINE_CTRL_GET_DESC_FROM_CMD)) { 122280304Sjkim if (s == NULL) { 123280304Sjkim ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ERR_R_PASSED_NULL_PARAMETER); 124280304Sjkim return -1; 125280304Sjkim } 126280304Sjkim } 127280304Sjkim /* Now handle cmd_name -> cmd_num conversion */ 128280304Sjkim if (cmd == ENGINE_CTRL_GET_CMD_FROM_NAME) { 129280304Sjkim if ((e->cmd_defns == NULL) 130280304Sjkim || ((idx = int_ctrl_cmd_by_name(e->cmd_defns, s)) < 0)) { 131280304Sjkim ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INVALID_CMD_NAME); 132280304Sjkim return -1; 133280304Sjkim } 134280304Sjkim return e->cmd_defns[idx].cmd_num; 135280304Sjkim } 136280304Sjkim /* 137280304Sjkim * For the rest of the commands, the 'long' argument must specify a valie 138280304Sjkim * command number - so we need to conduct a search. 139280304Sjkim */ 140280304Sjkim if ((e->cmd_defns == NULL) || ((idx = int_ctrl_cmd_by_num(e->cmd_defns, 141280304Sjkim (unsigned int) 142280304Sjkim i)) < 0)) { 143280304Sjkim ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INVALID_CMD_NUMBER); 144280304Sjkim return -1; 145280304Sjkim } 146280304Sjkim /* Now the logic splits depending on command type */ 147280304Sjkim switch (cmd) { 148280304Sjkim case ENGINE_CTRL_GET_NEXT_CMD_TYPE: 149280304Sjkim idx++; 150280304Sjkim if (int_ctrl_cmd_is_null(e->cmd_defns + idx)) 151280304Sjkim /* end-of-list */ 152280304Sjkim return 0; 153280304Sjkim else 154280304Sjkim return e->cmd_defns[idx].cmd_num; 155280304Sjkim case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD: 156280304Sjkim return strlen(e->cmd_defns[idx].cmd_name); 157280304Sjkim case ENGINE_CTRL_GET_NAME_FROM_CMD: 158280304Sjkim return BIO_snprintf(s, strlen(e->cmd_defns[idx].cmd_name) + 1, 159280304Sjkim "%s", e->cmd_defns[idx].cmd_name); 160280304Sjkim case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD: 161280304Sjkim if (e->cmd_defns[idx].cmd_desc) 162280304Sjkim return strlen(e->cmd_defns[idx].cmd_desc); 163280304Sjkim return strlen(int_no_description); 164280304Sjkim case ENGINE_CTRL_GET_DESC_FROM_CMD: 165280304Sjkim if (e->cmd_defns[idx].cmd_desc) 166280304Sjkim return BIO_snprintf(s, 167280304Sjkim strlen(e->cmd_defns[idx].cmd_desc) + 1, 168280304Sjkim "%s", e->cmd_defns[idx].cmd_desc); 169280304Sjkim return BIO_snprintf(s, strlen(int_no_description) + 1, "%s", 170280304Sjkim int_no_description); 171280304Sjkim case ENGINE_CTRL_GET_CMD_FLAGS: 172280304Sjkim return e->cmd_defns[idx].cmd_flags; 173280304Sjkim } 174280304Sjkim /* Shouldn't really be here ... */ 175280304Sjkim ENGINEerr(ENGINE_F_INT_CTRL_HELPER, ENGINE_R_INTERNAL_LIST_ERROR); 176280304Sjkim return -1; 177280304Sjkim} 178109998Smarkm 179280304Sjkimint ENGINE_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)) 180280304Sjkim{ 181280304Sjkim int ctrl_exists, ref_exists; 182280304Sjkim if (e == NULL) { 183280304Sjkim ENGINEerr(ENGINE_F_ENGINE_CTRL, ERR_R_PASSED_NULL_PARAMETER); 184280304Sjkim return 0; 185280304Sjkim } 186280304Sjkim CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 187280304Sjkim ref_exists = ((e->struct_ref > 0) ? 1 : 0); 188280304Sjkim CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 189280304Sjkim ctrl_exists = ((e->ctrl == NULL) ? 0 : 1); 190280304Sjkim if (!ref_exists) { 191280304Sjkim ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_REFERENCE); 192280304Sjkim return 0; 193280304Sjkim } 194280304Sjkim /* 195280304Sjkim * Intercept any "root-level" commands before trying to hand them on to 196280304Sjkim * ctrl() handlers. 197280304Sjkim */ 198280304Sjkim switch (cmd) { 199280304Sjkim case ENGINE_CTRL_HAS_CTRL_FUNCTION: 200280304Sjkim return ctrl_exists; 201280304Sjkim case ENGINE_CTRL_GET_FIRST_CMD_TYPE: 202280304Sjkim case ENGINE_CTRL_GET_NEXT_CMD_TYPE: 203280304Sjkim case ENGINE_CTRL_GET_CMD_FROM_NAME: 204280304Sjkim case ENGINE_CTRL_GET_NAME_LEN_FROM_CMD: 205280304Sjkim case ENGINE_CTRL_GET_NAME_FROM_CMD: 206280304Sjkim case ENGINE_CTRL_GET_DESC_LEN_FROM_CMD: 207280304Sjkim case ENGINE_CTRL_GET_DESC_FROM_CMD: 208280304Sjkim case ENGINE_CTRL_GET_CMD_FLAGS: 209280304Sjkim if (ctrl_exists && !(e->flags & ENGINE_FLAGS_MANUAL_CMD_CTRL)) 210280304Sjkim return int_ctrl_helper(e, cmd, i, p, f); 211280304Sjkim if (!ctrl_exists) { 212280304Sjkim ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION); 213280304Sjkim /* 214280304Sjkim * For these cmd-related functions, failure is indicated by a -1 215280304Sjkim * return value (because 0 is used as a valid return in some 216280304Sjkim * places). 217280304Sjkim */ 218280304Sjkim return -1; 219280304Sjkim } 220280304Sjkim default: 221280304Sjkim break; 222280304Sjkim } 223280304Sjkim /* Anything else requires a ctrl() handler to exist. */ 224280304Sjkim if (!ctrl_exists) { 225280304Sjkim ENGINEerr(ENGINE_F_ENGINE_CTRL, ENGINE_R_NO_CONTROL_FUNCTION); 226280304Sjkim return 0; 227280304Sjkim } 228280304Sjkim return e->ctrl(e, cmd, i, p, f); 229280304Sjkim} 230109998Smarkm 231109998Smarkmint ENGINE_cmd_is_executable(ENGINE *e, int cmd) 232280304Sjkim{ 233280304Sjkim int flags; 234280304Sjkim if ((flags = 235280304Sjkim ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, cmd, NULL, NULL)) < 0) { 236280304Sjkim ENGINEerr(ENGINE_F_ENGINE_CMD_IS_EXECUTABLE, 237280304Sjkim ENGINE_R_INVALID_CMD_NUMBER); 238280304Sjkim return 0; 239280304Sjkim } 240280304Sjkim if (!(flags & ENGINE_CMD_FLAG_NO_INPUT) && 241280304Sjkim !(flags & ENGINE_CMD_FLAG_NUMERIC) && 242280304Sjkim !(flags & ENGINE_CMD_FLAG_STRING)) 243280304Sjkim return 0; 244280304Sjkim return 1; 245280304Sjkim} 246109998Smarkm 247109998Smarkmint ENGINE_ctrl_cmd(ENGINE *e, const char *cmd_name, 248280304Sjkim long i, void *p, void (*f) (void), int cmd_optional) 249280304Sjkim{ 250280304Sjkim int num; 251109998Smarkm 252280304Sjkim if ((e == NULL) || (cmd_name == NULL)) { 253280304Sjkim ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, ERR_R_PASSED_NULL_PARAMETER); 254109998Smarkm return 0; 255280304Sjkim } 256280304Sjkim if ((e->ctrl == NULL) || ((num = ENGINE_ctrl(e, 257280304Sjkim ENGINE_CTRL_GET_CMD_FROM_NAME, 258280304Sjkim 0, (void *)cmd_name, 259280304Sjkim NULL)) <= 0)) { 260280304Sjkim /* 261280304Sjkim * If the command didn't *have* to be supported, we fake success. 262280304Sjkim * This allows certain settings to be specified for multiple ENGINEs 263280304Sjkim * and only require a change of ENGINE id (without having to 264280304Sjkim * selectively apply settings). Eg. changing from a hardware device 265280304Sjkim * back to the regular software ENGINE without editing the config 266280304Sjkim * file, etc. 267280304Sjkim */ 268280304Sjkim if (cmd_optional) { 269280304Sjkim ERR_clear_error(); 270280304Sjkim return 1; 271109998Smarkm } 272280304Sjkim ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD, ENGINE_R_INVALID_CMD_NAME); 273280304Sjkim return 0; 274280304Sjkim } 275280304Sjkim /* 276280304Sjkim * Force the result of the control command to 0 or 1, for the reasons 277280304Sjkim * mentioned before. 278280304Sjkim */ 279280304Sjkim if (ENGINE_ctrl(e, num, i, p, f) > 0) 280280304Sjkim return 1; 281280304Sjkim return 0; 282280304Sjkim} 283109998Smarkm 284109998Smarkmint ENGINE_ctrl_cmd_string(ENGINE *e, const char *cmd_name, const char *arg, 285280304Sjkim int cmd_optional) 286280304Sjkim{ 287280304Sjkim int num, flags; 288280304Sjkim long l; 289280304Sjkim char *ptr; 290280304Sjkim if ((e == NULL) || (cmd_name == NULL)) { 291280304Sjkim ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, 292280304Sjkim ERR_R_PASSED_NULL_PARAMETER); 293280304Sjkim return 0; 294280304Sjkim } 295280304Sjkim if ((e->ctrl == NULL) || ((num = ENGINE_ctrl(e, 296280304Sjkim ENGINE_CTRL_GET_CMD_FROM_NAME, 297280304Sjkim 0, (void *)cmd_name, 298280304Sjkim NULL)) <= 0)) { 299280304Sjkim /* 300280304Sjkim * If the command didn't *have* to be supported, we fake success. 301280304Sjkim * This allows certain settings to be specified for multiple ENGINEs 302280304Sjkim * and only require a change of ENGINE id (without having to 303280304Sjkim * selectively apply settings). Eg. changing from a hardware device 304280304Sjkim * back to the regular software ENGINE without editing the config 305280304Sjkim * file, etc. 306280304Sjkim */ 307280304Sjkim if (cmd_optional) { 308280304Sjkim ERR_clear_error(); 309280304Sjkim return 1; 310280304Sjkim } 311280304Sjkim ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, ENGINE_R_INVALID_CMD_NAME); 312280304Sjkim return 0; 313280304Sjkim } 314280304Sjkim if (!ENGINE_cmd_is_executable(e, num)) { 315280304Sjkim ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, 316280304Sjkim ENGINE_R_CMD_NOT_EXECUTABLE); 317280304Sjkim return 0; 318280304Sjkim } 319280304Sjkim if ((flags = 320280304Sjkim ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, NULL, NULL)) < 0) { 321280304Sjkim /* 322280304Sjkim * Shouldn't happen, given that ENGINE_cmd_is_executable() returned 323280304Sjkim * success. 324280304Sjkim */ 325280304Sjkim ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, 326280304Sjkim ENGINE_R_INTERNAL_LIST_ERROR); 327280304Sjkim return 0; 328280304Sjkim } 329280304Sjkim /* 330280304Sjkim * If the command takes no input, there must be no input. And vice versa. 331280304Sjkim */ 332280304Sjkim if (flags & ENGINE_CMD_FLAG_NO_INPUT) { 333280304Sjkim if (arg != NULL) { 334280304Sjkim ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, 335280304Sjkim ENGINE_R_COMMAND_TAKES_NO_INPUT); 336280304Sjkim return 0; 337280304Sjkim } 338280304Sjkim /* 339280304Sjkim * We deliberately force the result of ENGINE_ctrl() to 0 or 1 rather 340280304Sjkim * than returning it as "return data". This is to ensure usage of 341280304Sjkim * these commands is consistent across applications and that certain 342280304Sjkim * applications don't understand it one way, and others another. 343280304Sjkim */ 344280304Sjkim if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0) 345280304Sjkim return 1; 346280304Sjkim return 0; 347280304Sjkim } 348280304Sjkim /* So, we require input */ 349280304Sjkim if (arg == NULL) { 350280304Sjkim ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, 351280304Sjkim ENGINE_R_COMMAND_TAKES_INPUT); 352280304Sjkim return 0; 353280304Sjkim } 354280304Sjkim /* If it takes string input, that's easy */ 355280304Sjkim if (flags & ENGINE_CMD_FLAG_STRING) { 356280304Sjkim /* Same explanation as above */ 357280304Sjkim if (ENGINE_ctrl(e, num, 0, (void *)arg, NULL) > 0) 358280304Sjkim return 1; 359280304Sjkim return 0; 360280304Sjkim } 361280304Sjkim /* 362280304Sjkim * If it doesn't take numeric either, then it is unsupported for use in a 363280304Sjkim * config-setting situation, which is what this function is for. This 364280304Sjkim * should never happen though, because ENGINE_cmd_is_executable() was 365280304Sjkim * used. 366280304Sjkim */ 367280304Sjkim if (!(flags & ENGINE_CMD_FLAG_NUMERIC)) { 368280304Sjkim ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, 369280304Sjkim ENGINE_R_INTERNAL_LIST_ERROR); 370280304Sjkim return 0; 371280304Sjkim } 372280304Sjkim l = strtol(arg, &ptr, 10); 373280304Sjkim if ((arg == ptr) || (*ptr != '\0')) { 374280304Sjkim ENGINEerr(ENGINE_F_ENGINE_CTRL_CMD_STRING, 375280304Sjkim ENGINE_R_ARGUMENT_IS_NOT_A_NUMBER); 376280304Sjkim return 0; 377280304Sjkim } 378280304Sjkim /* 379280304Sjkim * Force the result of the control command to 0 or 1, for the reasons 380280304Sjkim * mentioned before. 381280304Sjkim */ 382280304Sjkim if (ENGINE_ctrl(e, num, l, NULL, NULL) > 0) 383280304Sjkim return 1; 384280304Sjkim return 0; 385280304Sjkim} 386