| 1 |
/* |
| 2 |
* Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
/* We need to use some engine deprecated APIs */ |
| 11 |
#define OPENSSL_SUPPRESS_DEPRECATED |
| 12 |
|
| 13 |
#include "eng_local.h" |
| 14 |
#include <openssl/conf.h> |
| 15 |
#include <openssl/trace.h> |
| 16 |
|
| 17 |
/* ENGINE config module */ |
| 18 |
|
| 19 |
static const char *skip_dot(const char *name) |
| 20 |
{ |
| 21 |
const char *p = strchr(name, '.'); |
| 22 |
|
| 23 |
if (p != NULL) |
| 24 |
return p + 1; |
| 25 |
return name; |
| 26 |
} |
| 27 |
|
| 28 |
static STACK_OF(ENGINE) *initialized_engines = NULL; |
| 29 |
|
| 30 |
static int int_engine_init(ENGINE *e) |
| 31 |
{ |
| 32 |
if (!ENGINE_init(e)) |
| 33 |
return 0; |
| 34 |
if (!initialized_engines) |
| 35 |
initialized_engines = sk_ENGINE_new_null(); |
| 36 |
if (!initialized_engines || !sk_ENGINE_push(initialized_engines, e)) { |
| 37 |
ENGINE_finish(e); |
| 38 |
return 0; |
| 39 |
} |
| 40 |
return 1; |
| 41 |
} |
| 42 |
|
| 43 |
static int int_engine_configure(const char *name, const char *value, const CONF *cnf) |
| 44 |
{ |
| 45 |
int i; |
| 46 |
int ret = 0; |
| 47 |
long do_init = -1; |
| 48 |
STACK_OF(CONF_VALUE) *ecmds; |
| 49 |
CONF_VALUE *ecmd = NULL; |
| 50 |
const char *ctrlname, *ctrlvalue; |
| 51 |
ENGINE *e = NULL; |
| 52 |
int soft = 0; |
| 53 |
|
| 54 |
name = skip_dot(name); |
| 55 |
OSSL_TRACE1(CONF, "Configuring engine %s\n", name); |
| 56 |
/* Value is a section containing ENGINE commands */ |
| 57 |
ecmds = NCONF_get_section(cnf, value); |
| 58 |
|
| 59 |
if (!ecmds) { |
| 60 |
ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ENGINE_SECTION_ERROR); |
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) { |
| 65 |
ecmd = sk_CONF_VALUE_value(ecmds, i); |
| 66 |
ctrlname = skip_dot(ecmd->name); |
| 67 |
ctrlvalue = ecmd->value; |
| 68 |
OSSL_TRACE2(CONF, "ENGINE: doing ctrl(%s,%s)\n", |
| 69 |
ctrlname, ctrlvalue); |
| 70 |
|
| 71 |
/* First handle some special pseudo ctrls */ |
| 72 |
|
| 73 |
/* Override engine name to use */ |
| 74 |
if (strcmp(ctrlname, "engine_id") == 0) |
| 75 |
name = ctrlvalue; |
| 76 |
else if (strcmp(ctrlname, "soft_load") == 0) |
| 77 |
soft = 1; |
| 78 |
/* Load a dynamic ENGINE */ |
| 79 |
else if (strcmp(ctrlname, "dynamic_path") == 0) { |
| 80 |
e = ENGINE_by_id("dynamic"); |
| 81 |
if (!e) |
| 82 |
goto err; |
| 83 |
if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", ctrlvalue, 0)) |
| 84 |
goto err; |
| 85 |
if (!ENGINE_ctrl_cmd_string(e, "LIST_ADD", "2", 0)) |
| 86 |
goto err; |
| 87 |
if (!ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0)) |
| 88 |
goto err; |
| 89 |
} |
| 90 |
/* ... add other pseudos here ... */ |
| 91 |
else { |
| 92 |
/* |
| 93 |
* At this point we need an ENGINE structural reference if we |
| 94 |
* don't already have one. |
| 95 |
*/ |
| 96 |
if (!e) { |
| 97 |
e = ENGINE_by_id(name); |
| 98 |
if (!e && soft) { |
| 99 |
ERR_clear_error(); |
| 100 |
return 1; |
| 101 |
} |
| 102 |
if (!e) |
| 103 |
goto err; |
| 104 |
} |
| 105 |
/* |
| 106 |
* Allow "EMPTY" to mean no value: this allows a valid "value" to |
| 107 |
* be passed to ctrls of type NO_INPUT |
| 108 |
*/ |
| 109 |
if (strcmp(ctrlvalue, "EMPTY") == 0) |
| 110 |
ctrlvalue = NULL; |
| 111 |
if (strcmp(ctrlname, "init") == 0) { |
| 112 |
if (!NCONF_get_number_e(cnf, value, "init", &do_init)) |
| 113 |
goto err; |
| 114 |
if (do_init == 1) { |
| 115 |
if (!int_engine_init(e)) |
| 116 |
goto err; |
| 117 |
} else if (do_init != 0) { |
| 118 |
ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INVALID_INIT_VALUE); |
| 119 |
goto err; |
| 120 |
} |
| 121 |
} else if (strcmp(ctrlname, "default_algorithms") == 0) { |
| 122 |
if (!ENGINE_set_default_string(e, ctrlvalue)) |
| 123 |
goto err; |
| 124 |
} else if (!ENGINE_ctrl_cmd_string(e, ctrlname, ctrlvalue, 0)) |
| 125 |
goto err; |
| 126 |
} |
| 127 |
} |
| 128 |
if (e && (do_init == -1) && !int_engine_init(e)) { |
| 129 |
ecmd = NULL; |
| 130 |
goto err; |
| 131 |
} |
| 132 |
ret = 1; |
| 133 |
err: |
| 134 |
if (ret != 1) { |
| 135 |
if (ecmd == NULL) |
| 136 |
ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ENGINE_CONFIGURATION_ERROR); |
| 137 |
else |
| 138 |
ERR_raise_data(ERR_LIB_ENGINE, ENGINE_R_ENGINE_CONFIGURATION_ERROR, |
| 139 |
"section=%s, name=%s, value=%s", |
| 140 |
ecmd->section, ecmd->name, ecmd->value); |
| 141 |
} |
| 142 |
ENGINE_free(e); |
| 143 |
return ret; |
| 144 |
} |
| 145 |
|
| 146 |
static int int_engine_module_init(CONF_IMODULE *md, const CONF *cnf) |
| 147 |
{ |
| 148 |
STACK_OF(CONF_VALUE) *elist; |
| 149 |
CONF_VALUE *cval; |
| 150 |
int i; |
| 151 |
OSSL_TRACE2(CONF, "Called engine module: name %s, value %s\n", |
| 152 |
CONF_imodule_get_name(md), CONF_imodule_get_value(md)); |
| 153 |
/* Value is a section containing ENGINEs to configure */ |
| 154 |
elist = NCONF_get_section(cnf, CONF_imodule_get_value(md)); |
| 155 |
|
| 156 |
if (!elist) { |
| 157 |
ERR_raise(ERR_LIB_ENGINE, ENGINE_R_ENGINES_SECTION_ERROR); |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
for (i = 0; i < sk_CONF_VALUE_num(elist); i++) { |
| 162 |
cval = sk_CONF_VALUE_value(elist, i); |
| 163 |
if (!int_engine_configure(cval->name, cval->value, cnf)) |
| 164 |
return 0; |
| 165 |
} |
| 166 |
|
| 167 |
return 1; |
| 168 |
} |
| 169 |
|
| 170 |
static void int_engine_module_finish(CONF_IMODULE *md) |
| 171 |
{ |
| 172 |
ENGINE *e; |
| 173 |
|
| 174 |
while ((e = sk_ENGINE_pop(initialized_engines))) |
| 175 |
ENGINE_finish(e); |
| 176 |
sk_ENGINE_free(initialized_engines); |
| 177 |
initialized_engines = NULL; |
| 178 |
} |
| 179 |
|
| 180 |
void ENGINE_add_conf_module(void) |
| 181 |
{ |
| 182 |
CONF_module_add("engines", |
| 183 |
int_engine_module_init, int_engine_module_finish); |
| 184 |
} |