1109998Smarkm/* eng_cnf.c */ 2280297Sjkim/* 3280297Sjkim * Written by Stephen Henson (steve@openssl.org) for the OpenSSL project 4280297Sjkim * 2001. 5109998Smarkm */ 6109998Smarkm/* ==================================================================== 7109998Smarkm * Copyright (c) 2001 The OpenSSL Project. All rights reserved. 8109998Smarkm * 9109998Smarkm * Redistribution and use in source and binary forms, with or without 10109998Smarkm * modification, are permitted provided that the following conditions 11109998Smarkm * are met: 12109998Smarkm * 13109998Smarkm * 1. Redistributions of source code must retain the above copyright 14280297Sjkim * notice, this list of conditions and the following disclaimer. 15109998Smarkm * 16109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright 17109998Smarkm * notice, this list of conditions and the following disclaimer in 18109998Smarkm * the documentation and/or other materials provided with the 19109998Smarkm * distribution. 20109998Smarkm * 21109998Smarkm * 3. All advertising materials mentioning features or use of this 22109998Smarkm * software must display the following acknowledgment: 23109998Smarkm * "This product includes software developed by the OpenSSL Project 24109998Smarkm * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25109998Smarkm * 26109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27109998Smarkm * endorse or promote products derived from this software without 28109998Smarkm * prior written permission. For written permission, please contact 29109998Smarkm * licensing@OpenSSL.org. 30109998Smarkm * 31109998Smarkm * 5. Products derived from this software may not be called "OpenSSL" 32109998Smarkm * nor may "OpenSSL" appear in their names without prior written 33109998Smarkm * permission of the OpenSSL Project. 34109998Smarkm * 35109998Smarkm * 6. Redistributions of any form whatsoever must retain the following 36109998Smarkm * acknowledgment: 37109998Smarkm * "This product includes software developed by the OpenSSL Project 38109998Smarkm * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39109998Smarkm * 40109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43109998Smarkm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE. 52109998Smarkm * ==================================================================== 53109998Smarkm * 54109998Smarkm * This product includes cryptographic software written by Eric Young 55109998Smarkm * (eay@cryptsoft.com). This product includes software written by Tim 56109998Smarkm * Hudson (tjh@cryptsoft.com). 57109998Smarkm * 58109998Smarkm */ 59109998Smarkm 60160814Ssimon#include "eng_int.h" 61109998Smarkm#include <openssl/conf.h> 62109998Smarkm 63109998Smarkm/* #define ENGINE_CONF_DEBUG */ 64109998Smarkm 65109998Smarkm/* ENGINE config module */ 66109998Smarkm 67109998Smarkmstatic char *skip_dot(char *name) 68280297Sjkim{ 69280297Sjkim char *p; 70280297Sjkim p = strchr(name, '.'); 71280297Sjkim if (p) 72280297Sjkim return p + 1; 73280297Sjkim return name; 74280297Sjkim} 75109998Smarkm 76109998Smarkmstatic STACK_OF(ENGINE) *initialized_engines = NULL; 77109998Smarkm 78109998Smarkmstatic int int_engine_init(ENGINE *e) 79280297Sjkim{ 80280297Sjkim if (!ENGINE_init(e)) 81280297Sjkim return 0; 82280297Sjkim if (!initialized_engines) 83280297Sjkim initialized_engines = sk_ENGINE_new_null(); 84280297Sjkim if (!initialized_engines || !sk_ENGINE_push(initialized_engines, e)) { 85280297Sjkim ENGINE_finish(e); 86280297Sjkim return 0; 87280297Sjkim } 88280297Sjkim return 1; 89280297Sjkim} 90109998Smarkm 91109998Smarkmstatic int int_engine_configure(char *name, char *value, const CONF *cnf) 92280297Sjkim{ 93280297Sjkim int i; 94280297Sjkim int ret = 0; 95280297Sjkim long do_init = -1; 96280297Sjkim STACK_OF(CONF_VALUE) *ecmds; 97280297Sjkim CONF_VALUE *ecmd = NULL; 98280297Sjkim char *ctrlname, *ctrlvalue; 99280297Sjkim ENGINE *e = NULL; 100280297Sjkim int soft = 0; 101194206Ssimon 102280297Sjkim name = skip_dot(name); 103109998Smarkm#ifdef ENGINE_CONF_DEBUG 104280297Sjkim fprintf(stderr, "Configuring engine %s\n", name); 105109998Smarkm#endif 106280297Sjkim /* Value is a section containing ENGINE commands */ 107280297Sjkim ecmds = NCONF_get_section(cnf, value); 108109998Smarkm 109280297Sjkim if (!ecmds) { 110280297Sjkim ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE, 111280297Sjkim ENGINE_R_ENGINE_SECTION_ERROR); 112280297Sjkim return 0; 113280297Sjkim } 114109998Smarkm 115280297Sjkim for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) { 116280297Sjkim ecmd = sk_CONF_VALUE_value(ecmds, i); 117280297Sjkim ctrlname = skip_dot(ecmd->name); 118280297Sjkim ctrlvalue = ecmd->value; 119109998Smarkm#ifdef ENGINE_CONF_DEBUG 120280297Sjkim fprintf(stderr, "ENGINE conf: doing ctrl(%s,%s)\n", ctrlname, 121280297Sjkim ctrlvalue); 122109998Smarkm#endif 123109998Smarkm 124280297Sjkim /* First handle some special pseudo ctrls */ 125109998Smarkm 126280297Sjkim /* Override engine name to use */ 127280297Sjkim if (!strcmp(ctrlname, "engine_id")) 128280297Sjkim name = ctrlvalue; 129280297Sjkim else if (!strcmp(ctrlname, "soft_load")) 130280297Sjkim soft = 1; 131280297Sjkim /* Load a dynamic ENGINE */ 132280297Sjkim else if (!strcmp(ctrlname, "dynamic_path")) { 133280297Sjkim e = ENGINE_by_id("dynamic"); 134280297Sjkim if (!e) 135280297Sjkim goto err; 136280297Sjkim if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", ctrlvalue, 0)) 137280297Sjkim goto err; 138280297Sjkim if (!ENGINE_ctrl_cmd_string(e, "LIST_ADD", "2", 0)) 139280297Sjkim goto err; 140280297Sjkim if (!ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0)) 141280297Sjkim goto err; 142280297Sjkim } 143280297Sjkim /* ... add other pseudos here ... */ 144280297Sjkim else { 145280297Sjkim /* 146280297Sjkim * At this point we need an ENGINE structural reference if we 147280297Sjkim * don't already have one. 148280297Sjkim */ 149280297Sjkim if (!e) { 150280297Sjkim e = ENGINE_by_id(name); 151280297Sjkim if (!e && soft) { 152280297Sjkim ERR_clear_error(); 153280297Sjkim return 1; 154280297Sjkim } 155280297Sjkim if (!e) 156280297Sjkim goto err; 157280297Sjkim } 158280297Sjkim /* 159280297Sjkim * Allow "EMPTY" to mean no value: this allows a valid "value" to 160280297Sjkim * be passed to ctrls of type NO_INPUT 161280297Sjkim */ 162280297Sjkim if (!strcmp(ctrlvalue, "EMPTY")) 163280297Sjkim ctrlvalue = NULL; 164280297Sjkim if (!strcmp(ctrlname, "init")) { 165280297Sjkim if (!NCONF_get_number_e(cnf, value, "init", &do_init)) 166280297Sjkim goto err; 167280297Sjkim if (do_init == 1) { 168280297Sjkim if (!int_engine_init(e)) 169280297Sjkim goto err; 170280297Sjkim } else if (do_init != 0) { 171280297Sjkim ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE, 172280297Sjkim ENGINE_R_INVALID_INIT_VALUE); 173280297Sjkim goto err; 174280297Sjkim } 175280297Sjkim } else if (!strcmp(ctrlname, "default_algorithms")) { 176280297Sjkim if (!ENGINE_set_default_string(e, ctrlvalue)) 177280297Sjkim goto err; 178280297Sjkim } else if (!ENGINE_ctrl_cmd_string(e, ctrlname, ctrlvalue, 0)) 179280297Sjkim goto err; 180280297Sjkim } 181109998Smarkm 182280297Sjkim } 183280297Sjkim if (e && (do_init == -1) && !int_engine_init(e)) { 184280297Sjkim ecmd = NULL; 185280297Sjkim goto err; 186280297Sjkim } 187280297Sjkim ret = 1; 188280297Sjkim err: 189280297Sjkim if (ret != 1) { 190280297Sjkim ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE, 191280297Sjkim ENGINE_R_ENGINE_CONFIGURATION_ERROR); 192280297Sjkim if (ecmd) 193280297Sjkim ERR_add_error_data(6, "section=", ecmd->section, 194280297Sjkim ", name=", ecmd->name, 195280297Sjkim ", value=", ecmd->value); 196280297Sjkim } 197280297Sjkim if (e) 198280297Sjkim ENGINE_free(e); 199280297Sjkim return ret; 200280297Sjkim} 201109998Smarkm 202109998Smarkmstatic int int_engine_module_init(CONF_IMODULE *md, const CONF *cnf) 203280297Sjkim{ 204280297Sjkim STACK_OF(CONF_VALUE) *elist; 205280297Sjkim CONF_VALUE *cval; 206280297Sjkim int i; 207109998Smarkm#ifdef ENGINE_CONF_DEBUG 208280297Sjkim fprintf(stderr, "Called engine module: name %s, value %s\n", 209280297Sjkim CONF_imodule_get_name(md), CONF_imodule_get_value(md)); 210109998Smarkm#endif 211280297Sjkim /* Value is a section containing ENGINEs to configure */ 212280297Sjkim elist = NCONF_get_section(cnf, CONF_imodule_get_value(md)); 213109998Smarkm 214280297Sjkim if (!elist) { 215280297Sjkim ENGINEerr(ENGINE_F_INT_ENGINE_MODULE_INIT, 216280297Sjkim ENGINE_R_ENGINES_SECTION_ERROR); 217280297Sjkim return 0; 218280297Sjkim } 219109998Smarkm 220280297Sjkim for (i = 0; i < sk_CONF_VALUE_num(elist); i++) { 221280297Sjkim cval = sk_CONF_VALUE_value(elist, i); 222280297Sjkim if (!int_engine_configure(cval->name, cval->value, cnf)) 223280297Sjkim return 0; 224280297Sjkim } 225109998Smarkm 226280297Sjkim return 1; 227280297Sjkim} 228109998Smarkm 229109998Smarkmstatic void int_engine_module_finish(CONF_IMODULE *md) 230280297Sjkim{ 231280297Sjkim ENGINE *e; 232280297Sjkim while ((e = sk_ENGINE_pop(initialized_engines))) 233280297Sjkim ENGINE_finish(e); 234280297Sjkim sk_ENGINE_free(initialized_engines); 235280297Sjkim initialized_engines = NULL; 236280297Sjkim} 237109998Smarkm 238109998Smarkmvoid ENGINE_add_conf_module(void) 239280297Sjkim{ 240280297Sjkim CONF_module_add("engines", 241280297Sjkim int_engine_module_init, int_engine_module_finish); 242280297Sjkim} 243