1109998Smarkm/* eng_cnf.c */ 2296341Sdelphij/* 3296341Sdelphij * Written by Stephen Henson (steve@openssl.org) for the OpenSSL project 4296341Sdelphij * 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 14296341Sdelphij * 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) 68296341Sdelphij{ 69296341Sdelphij char *p; 70296341Sdelphij p = strchr(name, '.'); 71296341Sdelphij if (p) 72296341Sdelphij return p + 1; 73296341Sdelphij return name; 74296341Sdelphij} 75109998Smarkm 76109998Smarkmstatic STACK_OF(ENGINE) *initialized_engines = NULL; 77109998Smarkm 78109998Smarkmstatic int int_engine_init(ENGINE *e) 79296341Sdelphij{ 80296341Sdelphij if (!ENGINE_init(e)) 81296341Sdelphij return 0; 82296341Sdelphij if (!initialized_engines) 83296341Sdelphij initialized_engines = sk_ENGINE_new_null(); 84296341Sdelphij if (!initialized_engines || !sk_ENGINE_push(initialized_engines, e)) { 85296341Sdelphij ENGINE_finish(e); 86296341Sdelphij return 0; 87296341Sdelphij } 88296341Sdelphij return 1; 89296341Sdelphij} 90109998Smarkm 91109998Smarkmstatic int int_engine_configure(char *name, char *value, const CONF *cnf) 92296341Sdelphij{ 93296341Sdelphij int i; 94296341Sdelphij int ret = 0; 95296341Sdelphij long do_init = -1; 96296341Sdelphij STACK_OF(CONF_VALUE) *ecmds; 97296341Sdelphij CONF_VALUE *ecmd = NULL; 98296341Sdelphij char *ctrlname, *ctrlvalue; 99296341Sdelphij ENGINE *e = NULL; 100296341Sdelphij int soft = 0; 101194206Ssimon 102296341Sdelphij name = skip_dot(name); 103109998Smarkm#ifdef ENGINE_CONF_DEBUG 104296341Sdelphij fprintf(stderr, "Configuring engine %s\n", name); 105109998Smarkm#endif 106296341Sdelphij /* Value is a section containing ENGINE commands */ 107296341Sdelphij ecmds = NCONF_get_section(cnf, value); 108109998Smarkm 109296341Sdelphij if (!ecmds) { 110296341Sdelphij ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE, 111296341Sdelphij ENGINE_R_ENGINE_SECTION_ERROR); 112296341Sdelphij return 0; 113296341Sdelphij } 114109998Smarkm 115296341Sdelphij for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) { 116296341Sdelphij ecmd = sk_CONF_VALUE_value(ecmds, i); 117296341Sdelphij ctrlname = skip_dot(ecmd->name); 118296341Sdelphij ctrlvalue = ecmd->value; 119109998Smarkm#ifdef ENGINE_CONF_DEBUG 120296341Sdelphij fprintf(stderr, "ENGINE conf: doing ctrl(%s,%s)\n", ctrlname, 121296341Sdelphij ctrlvalue); 122109998Smarkm#endif 123109998Smarkm 124296341Sdelphij /* First handle some special pseudo ctrls */ 125109998Smarkm 126296341Sdelphij /* Override engine name to use */ 127296341Sdelphij if (!strcmp(ctrlname, "engine_id")) 128296341Sdelphij name = ctrlvalue; 129296341Sdelphij else if (!strcmp(ctrlname, "soft_load")) 130296341Sdelphij soft = 1; 131296341Sdelphij /* Load a dynamic ENGINE */ 132296341Sdelphij else if (!strcmp(ctrlname, "dynamic_path")) { 133296341Sdelphij e = ENGINE_by_id("dynamic"); 134296341Sdelphij if (!e) 135296341Sdelphij goto err; 136296341Sdelphij if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", ctrlvalue, 0)) 137296341Sdelphij goto err; 138296341Sdelphij if (!ENGINE_ctrl_cmd_string(e, "LIST_ADD", "2", 0)) 139296341Sdelphij goto err; 140296341Sdelphij if (!ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0)) 141296341Sdelphij goto err; 142296341Sdelphij } 143296341Sdelphij /* ... add other pseudos here ... */ 144296341Sdelphij else { 145296341Sdelphij /* 146296341Sdelphij * At this point we need an ENGINE structural reference if we 147296341Sdelphij * don't already have one. 148296341Sdelphij */ 149296341Sdelphij if (!e) { 150296341Sdelphij e = ENGINE_by_id(name); 151296341Sdelphij if (!e && soft) { 152296341Sdelphij ERR_clear_error(); 153296341Sdelphij return 1; 154296341Sdelphij } 155296341Sdelphij if (!e) 156296341Sdelphij goto err; 157296341Sdelphij } 158296341Sdelphij /* 159296341Sdelphij * Allow "EMPTY" to mean no value: this allows a valid "value" to 160296341Sdelphij * be passed to ctrls of type NO_INPUT 161296341Sdelphij */ 162296341Sdelphij if (!strcmp(ctrlvalue, "EMPTY")) 163296341Sdelphij ctrlvalue = NULL; 164296341Sdelphij if (!strcmp(ctrlname, "init")) { 165296341Sdelphij if (!NCONF_get_number_e(cnf, value, "init", &do_init)) 166296341Sdelphij goto err; 167296341Sdelphij if (do_init == 1) { 168296341Sdelphij if (!int_engine_init(e)) 169296341Sdelphij goto err; 170296341Sdelphij } else if (do_init != 0) { 171296341Sdelphij ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE, 172296341Sdelphij ENGINE_R_INVALID_INIT_VALUE); 173296341Sdelphij goto err; 174296341Sdelphij } 175296341Sdelphij } else if (!strcmp(ctrlname, "default_algorithms")) { 176296341Sdelphij if (!ENGINE_set_default_string(e, ctrlvalue)) 177296341Sdelphij goto err; 178296341Sdelphij } else if (!ENGINE_ctrl_cmd_string(e, ctrlname, ctrlvalue, 0)) 179296341Sdelphij goto err; 180296341Sdelphij } 181109998Smarkm 182296341Sdelphij } 183296341Sdelphij if (e && (do_init == -1) && !int_engine_init(e)) { 184296341Sdelphij ecmd = NULL; 185296341Sdelphij goto err; 186296341Sdelphij } 187296341Sdelphij ret = 1; 188296341Sdelphij err: 189296341Sdelphij if (ret != 1) { 190296341Sdelphij ENGINEerr(ENGINE_F_INT_ENGINE_CONFIGURE, 191296341Sdelphij ENGINE_R_ENGINE_CONFIGURATION_ERROR); 192296341Sdelphij if (ecmd) 193296341Sdelphij ERR_add_error_data(6, "section=", ecmd->section, 194296341Sdelphij ", name=", ecmd->name, 195296341Sdelphij ", value=", ecmd->value); 196296341Sdelphij } 197296341Sdelphij if (e) 198296341Sdelphij ENGINE_free(e); 199296341Sdelphij return ret; 200296341Sdelphij} 201109998Smarkm 202109998Smarkmstatic int int_engine_module_init(CONF_IMODULE *md, const CONF *cnf) 203296341Sdelphij{ 204296341Sdelphij STACK_OF(CONF_VALUE) *elist; 205296341Sdelphij CONF_VALUE *cval; 206296341Sdelphij int i; 207109998Smarkm#ifdef ENGINE_CONF_DEBUG 208296341Sdelphij fprintf(stderr, "Called engine module: name %s, value %s\n", 209296341Sdelphij CONF_imodule_get_name(md), CONF_imodule_get_value(md)); 210109998Smarkm#endif 211296341Sdelphij /* Value is a section containing ENGINEs to configure */ 212296341Sdelphij elist = NCONF_get_section(cnf, CONF_imodule_get_value(md)); 213109998Smarkm 214296341Sdelphij if (!elist) { 215296341Sdelphij ENGINEerr(ENGINE_F_INT_ENGINE_MODULE_INIT, 216296341Sdelphij ENGINE_R_ENGINES_SECTION_ERROR); 217296341Sdelphij return 0; 218296341Sdelphij } 219109998Smarkm 220296341Sdelphij for (i = 0; i < sk_CONF_VALUE_num(elist); i++) { 221296341Sdelphij cval = sk_CONF_VALUE_value(elist, i); 222296341Sdelphij if (!int_engine_configure(cval->name, cval->value, cnf)) 223296341Sdelphij return 0; 224296341Sdelphij } 225109998Smarkm 226296341Sdelphij return 1; 227296341Sdelphij} 228109998Smarkm 229109998Smarkmstatic void int_engine_module_finish(CONF_IMODULE *md) 230296341Sdelphij{ 231296341Sdelphij ENGINE *e; 232296341Sdelphij while ((e = sk_ENGINE_pop(initialized_engines))) 233296341Sdelphij ENGINE_finish(e); 234296341Sdelphij sk_ENGINE_free(initialized_engines); 235296341Sdelphij initialized_engines = NULL; 236296341Sdelphij} 237109998Smarkm 238109998Smarkmvoid ENGINE_add_conf_module(void) 239296341Sdelphij{ 240296341Sdelphij CONF_module_add("engines", 241296341Sdelphij int_engine_module_init, int_engine_module_finish); 242296341Sdelphij} 243