- unsigned long flags)
- {
- STACK_OF(CONF_VALUE) *values;
- CONF_VALUE *vl;
- char *vsection;
-
- int ret, i;
-
- if (!cnf)
- return 1;
-
- if (appname == NULL)
- appname = "openssl_conf";
-
- vsection = NCONF_get_string(cnf, NULL, appname);
-
- if (!vsection)
- {
- ERR_clear_error();
- return 1;
- }
-
- values = NCONF_get_section(cnf, vsection);
-
- if (!values)
- return 0;
-
- for (i = 0; i < sk_CONF_VALUE_num(values); i++)
- {
- vl = sk_CONF_VALUE_value(values, i);
- ret = module_run(cnf, vl->name, vl->value, flags);
- if (ret <= 0)
- if(!(flags & CONF_MFLAGS_IGNORE_ERRORS))
- return ret;
- }
-
- return 1;
-
- }
-
-int CONF_modules_load_file(const char *filename, const char *appname,
- unsigned long flags)
- {
- char *file = NULL;
- CONF *conf = NULL;
- int ret = 0;
- conf = NCONF_new(NULL);
- if (!conf)
- goto err;
-
- if (filename == NULL)
- {
- file = CONF_get1_default_config_file();
- if (!file)
- goto err;
- }
- else
- file = (char *)filename;
-
- if (NCONF_load(conf, file, NULL) <= 0)
- {
- if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
- (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE))
- {
- ERR_clear_error();
- ret = 1;
- }
- goto err;
- }
-
- ret = CONF_modules_load(conf, appname, flags);
-
- err:
- if (filename == NULL)
- OPENSSL_free(file);
- NCONF_free(conf);
-
- return ret;
- }
-
-static int module_run(const CONF *cnf, char *name, char *value,
- unsigned long flags)
- {
- CONF_MODULE *md;
- int ret;
-
- md = module_find(name);
-
- /* Module not found: try to load DSO */
- if (!md && !(flags & CONF_MFLAGS_NO_DSO))
- md = module_load_dso(cnf, name, value, flags);
-
- if (!md)
- {
- if (!(flags & CONF_MFLAGS_SILENT))
- {
- CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
- ERR_add_error_data(2, "module=", name);
- }
- return -1;
- }
-
- ret = module_init(md, name, value, cnf);
-
- if (ret <= 0)
- {
- if (!(flags & CONF_MFLAGS_SILENT))
- {
- char rcode[10];
- CONFerr(CONF_F_CONF_MODULES_LOAD, CONF_R_MODULE_INITIALIZATION_ERROR);
- sprintf(rcode, "%-8d", ret);
- ERR_add_error_data(6, "module=", name, ", value=", value, ", retcode=", rcode);
- }
- }
-
- return ret;
- }
+ unsigned long flags)
+{
+ STACK_OF(CONF_VALUE) *values;
+ CONF_VALUE *vl;
+ char *vsection = NULL;
+
+ int ret, i;
+
+ if (!cnf)
+ return 1;
+
+ if (appname)
+ vsection = NCONF_get_string(cnf, NULL, appname);
+
+ if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION)))
+ vsection = NCONF_get_string(cnf, NULL, "openssl_conf");
+
+ if (!vsection) {
+ ERR_clear_error();
+ return 1;
+ }
+
+ OSSL_TRACE1(CONF, "Configuration in section %s\n", vsection);
+ values = NCONF_get_section(cnf, vsection);
+
+ if (!values)
+ return 0;
+
+ for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
+ vl = sk_CONF_VALUE_value(values, i);
+ ret = module_run(cnf, vl->name, vl->value, flags);
+ OSSL_TRACE3(CONF, "Running module %s (%s) returned %d\n",
+ vl->name, vl->value, ret);
+ if (ret <= 0)
+ if (!(flags & CONF_MFLAGS_IGNORE_ERRORS))
+ return ret;
+ }
+
+ return 1;
+
+}
+
+int CONF_modules_load_file_with_libctx(OPENSSL_CTX *libctx,
+ const char *filename,
+ const char *appname, unsigned long flags)
+{
+ char *file = NULL;
+ CONF *conf = NULL;
+ int ret = 0;
+
+ conf = NCONF_new_with_libctx(libctx, NULL);
+ if (conf == NULL)
+ goto err;
+
+ if (filename == NULL) {
+ file = CONF_get1_default_config_file();
+ if (file == NULL)
+ goto err;
+ } else {
+ file = (char *)filename;
+ }
+
+ if (NCONF_load(conf, file, NULL) <= 0) {
+ if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
+ (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) {
+ ERR_clear_error();
+ ret = 1;
+ }
+ goto err;
+ }
+
+ ret = CONF_modules_load(conf, appname, flags);
+
+ err:
+ if (filename == NULL)
+ OPENSSL_free(file);
+ NCONF_free(conf);
+
+ if (flags & CONF_MFLAGS_IGNORE_RETURN_CODES)
+ return 1;
+
+ return ret;
+}
+
+int CONF_modules_load_file(const char *filename,
+ const char *appname, unsigned long flags)
+{
+ return CONF_modules_load_file_with_libctx(NULL, filename, appname, flags);
+}
+
+DEFINE_RUN_ONCE_STATIC(do_load_builtin_modules)
+{
+ OPENSSL_load_builtin_modules();
+#ifndef OPENSSL_NO_ENGINE
+ /* Need to load ENGINEs */
+ ENGINE_load_builtin_engines();
+#endif
+ ERR_clear_error();
+ return 1;
+}
+
+static int module_run(const CONF *cnf, const char *name, const char *value,
+ unsigned long flags)
+{
+ CONF_MODULE *md;
+ int ret;
+
+ if (!RUN_ONCE(&load_builtin_modules, do_load_builtin_modules))
+ return -1;
+
+ md = module_find(name);
+
+ /* Module not found: try to load DSO */
+ if (!md && !(flags & CONF_MFLAGS_NO_DSO))
+ md = module_load_dso(cnf, name, value);
+
+ if (!md) {
+ if (!(flags & CONF_MFLAGS_SILENT)) {
+ CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
+ ERR_add_error_data(2, "module=", name);
+ }
+ return -1;
+ }
+
+ ret = module_init(md, name, value, cnf);
+
+ if (ret <= 0) {
+ if (!(flags & CONF_MFLAGS_SILENT)) {
+ char rcode[DECIMAL_SIZE(ret) + 1];
+
+ CONFerr(CONF_F_MODULE_RUN, CONF_R_MODULE_INITIALIZATION_ERROR);
+ BIO_snprintf(rcode, sizeof(rcode), "%-8d", ret);
+ ERR_add_error_data(6, "module=", name, ", value=", value,
+ ", retcode=", rcode);
+ }
+ }
+
+ return ret;
+}