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