[331] | 1 | /*
|
---|
| 2 | * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the OpenSSL license (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 | #include <stdio.h>
|
---|
| 11 | #include <string.h>
|
---|
| 12 | #include <internal/conf.h>
|
---|
| 13 | #include <openssl/crypto.h>
|
---|
| 14 | #include <openssl/err.h>
|
---|
| 15 | #include <openssl/conf.h>
|
---|
| 16 | #include <openssl/conf_api.h>
|
---|
| 17 | #include <openssl/lhash.h>
|
---|
| 18 | #include "e_os.h"
|
---|
| 19 |
|
---|
| 20 | static CONF_METHOD *default_CONF_method = NULL;
|
---|
| 21 |
|
---|
| 22 | /* Init a 'CONF' structure from an old LHASH */
|
---|
| 23 |
|
---|
| 24 | void CONF_set_nconf(CONF *conf, LHASH_OF(CONF_VALUE) *hash)
|
---|
| 25 | {
|
---|
| 26 | if (default_CONF_method == NULL)
|
---|
| 27 | default_CONF_method = NCONF_default();
|
---|
| 28 |
|
---|
| 29 | default_CONF_method->init(conf);
|
---|
| 30 | conf->data = hash;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | /*
|
---|
| 34 | * The following section contains the "CONF classic" functions, rewritten in
|
---|
| 35 | * terms of the new CONF interface.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | int CONF_set_default_method(CONF_METHOD *meth)
|
---|
| 39 | {
|
---|
| 40 | default_CONF_method = meth;
|
---|
| 41 | return 1;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file,
|
---|
| 45 | long *eline)
|
---|
| 46 | {
|
---|
| 47 | LHASH_OF(CONF_VALUE) *ltmp;
|
---|
| 48 | BIO *in = NULL;
|
---|
| 49 |
|
---|
| 50 | #ifdef OPENSSL_SYS_VMS
|
---|
| 51 | in = BIO_new_file(file, "r");
|
---|
| 52 | #else
|
---|
| 53 | in = BIO_new_file(file, "rb");
|
---|
| 54 | #endif
|
---|
| 55 | if (in == NULL) {
|
---|
| 56 | CONFerr(CONF_F_CONF_LOAD, ERR_R_SYS_LIB);
|
---|
| 57 | return NULL;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | ltmp = CONF_load_bio(conf, in, eline);
|
---|
| 61 | BIO_free(in);
|
---|
| 62 |
|
---|
| 63 | return ltmp;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | #ifndef OPENSSL_NO_STDIO
|
---|
| 67 | LHASH_OF(CONF_VALUE) *CONF_load_fp(LHASH_OF(CONF_VALUE) *conf, FILE *fp,
|
---|
| 68 | long *eline)
|
---|
| 69 | {
|
---|
| 70 | BIO *btmp;
|
---|
| 71 | LHASH_OF(CONF_VALUE) *ltmp;
|
---|
| 72 | if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
|
---|
| 73 | CONFerr(CONF_F_CONF_LOAD_FP, ERR_R_BUF_LIB);
|
---|
| 74 | return NULL;
|
---|
| 75 | }
|
---|
| 76 | ltmp = CONF_load_bio(conf, btmp, eline);
|
---|
| 77 | BIO_free(btmp);
|
---|
| 78 | return ltmp;
|
---|
| 79 | }
|
---|
| 80 | #endif
|
---|
| 81 |
|
---|
| 82 | LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp,
|
---|
| 83 | long *eline)
|
---|
| 84 | {
|
---|
| 85 | CONF ctmp;
|
---|
| 86 | int ret;
|
---|
| 87 |
|
---|
| 88 | CONF_set_nconf(&ctmp, conf);
|
---|
| 89 |
|
---|
| 90 | ret = NCONF_load_bio(&ctmp, bp, eline);
|
---|
| 91 | if (ret)
|
---|
| 92 | return ctmp.data;
|
---|
| 93 | return NULL;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf,
|
---|
| 97 | const char *section)
|
---|
| 98 | {
|
---|
| 99 | if (conf == NULL) {
|
---|
| 100 | return NULL;
|
---|
| 101 | } else {
|
---|
| 102 | CONF ctmp;
|
---|
| 103 | CONF_set_nconf(&ctmp, conf);
|
---|
| 104 | return NCONF_get_section(&ctmp, section);
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf, const char *group,
|
---|
| 109 | const char *name)
|
---|
| 110 | {
|
---|
| 111 | if (conf == NULL) {
|
---|
| 112 | return NCONF_get_string(NULL, group, name);
|
---|
| 113 | } else {
|
---|
| 114 | CONF ctmp;
|
---|
| 115 | CONF_set_nconf(&ctmp, conf);
|
---|
| 116 | return NCONF_get_string(&ctmp, group, name);
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | long CONF_get_number(LHASH_OF(CONF_VALUE) *conf, const char *group,
|
---|
| 121 | const char *name)
|
---|
| 122 | {
|
---|
| 123 | int status;
|
---|
| 124 | long result = 0;
|
---|
| 125 |
|
---|
| 126 | if (conf == NULL) {
|
---|
| 127 | status = NCONF_get_number_e(NULL, group, name, &result);
|
---|
| 128 | } else {
|
---|
| 129 | CONF ctmp;
|
---|
| 130 | CONF_set_nconf(&ctmp, conf);
|
---|
| 131 | status = NCONF_get_number_e(&ctmp, group, name, &result);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | if (status == 0) {
|
---|
| 135 | /* This function does not believe in errors... */
|
---|
| 136 | ERR_clear_error();
|
---|
| 137 | }
|
---|
| 138 | return result;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | void CONF_free(LHASH_OF(CONF_VALUE) *conf)
|
---|
| 142 | {
|
---|
| 143 | CONF ctmp;
|
---|
| 144 | CONF_set_nconf(&ctmp, conf);
|
---|
| 145 | NCONF_free_data(&ctmp);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | #ifndef OPENSSL_NO_STDIO
|
---|
| 149 | int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out)
|
---|
| 150 | {
|
---|
| 151 | BIO *btmp;
|
---|
| 152 | int ret;
|
---|
| 153 |
|
---|
| 154 | if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
|
---|
| 155 | CONFerr(CONF_F_CONF_DUMP_FP, ERR_R_BUF_LIB);
|
---|
| 156 | return 0;
|
---|
| 157 | }
|
---|
| 158 | ret = CONF_dump_bio(conf, btmp);
|
---|
| 159 | BIO_free(btmp);
|
---|
| 160 | return ret;
|
---|
| 161 | }
|
---|
| 162 | #endif
|
---|
| 163 |
|
---|
| 164 | int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out)
|
---|
| 165 | {
|
---|
| 166 | CONF ctmp;
|
---|
| 167 | CONF_set_nconf(&ctmp, conf);
|
---|
| 168 | return NCONF_dump_bio(&ctmp, out);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /*
|
---|
| 172 | * The following section contains the "New CONF" functions. They are
|
---|
| 173 | * completely centralised around a new CONF structure that may contain
|
---|
| 174 | * basically anything, but at least a method pointer and a table of data.
|
---|
| 175 | * These functions are also written in terms of the bridge functions used by
|
---|
| 176 | * the "CONF classic" functions, for consistency.
|
---|
| 177 | */
|
---|
| 178 |
|
---|
| 179 | CONF *NCONF_new(CONF_METHOD *meth)
|
---|
| 180 | {
|
---|
| 181 | CONF *ret;
|
---|
| 182 |
|
---|
| 183 | if (meth == NULL)
|
---|
| 184 | meth = NCONF_default();
|
---|
| 185 |
|
---|
| 186 | ret = meth->create(meth);
|
---|
| 187 | if (ret == NULL) {
|
---|
| 188 | CONFerr(CONF_F_NCONF_NEW, ERR_R_MALLOC_FAILURE);
|
---|
| 189 | return (NULL);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | return ret;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | void NCONF_free(CONF *conf)
|
---|
| 196 | {
|
---|
| 197 | if (conf == NULL)
|
---|
| 198 | return;
|
---|
| 199 | conf->meth->destroy(conf);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | void NCONF_free_data(CONF *conf)
|
---|
| 203 | {
|
---|
| 204 | if (conf == NULL)
|
---|
| 205 | return;
|
---|
| 206 | conf->meth->destroy_data(conf);
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | int NCONF_load(CONF *conf, const char *file, long *eline)
|
---|
| 210 | {
|
---|
| 211 | if (conf == NULL) {
|
---|
| 212 | CONFerr(CONF_F_NCONF_LOAD, CONF_R_NO_CONF);
|
---|
| 213 | return 0;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | return conf->meth->load(conf, file, eline);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | #ifndef OPENSSL_NO_STDIO
|
---|
| 220 | int NCONF_load_fp(CONF *conf, FILE *fp, long *eline)
|
---|
| 221 | {
|
---|
| 222 | BIO *btmp;
|
---|
| 223 | int ret;
|
---|
| 224 | if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
|
---|
| 225 | CONFerr(CONF_F_NCONF_LOAD_FP, ERR_R_BUF_LIB);
|
---|
| 226 | return 0;
|
---|
| 227 | }
|
---|
| 228 | ret = NCONF_load_bio(conf, btmp, eline);
|
---|
| 229 | BIO_free(btmp);
|
---|
| 230 | return ret;
|
---|
| 231 | }
|
---|
| 232 | #endif
|
---|
| 233 |
|
---|
| 234 | int NCONF_load_bio(CONF *conf, BIO *bp, long *eline)
|
---|
| 235 | {
|
---|
| 236 | if (conf == NULL) {
|
---|
| 237 | CONFerr(CONF_F_NCONF_LOAD_BIO, CONF_R_NO_CONF);
|
---|
| 238 | return 0;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | return conf->meth->load_bio(conf, bp, eline);
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section)
|
---|
| 245 | {
|
---|
| 246 | if (conf == NULL) {
|
---|
| 247 | CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_CONF);
|
---|
| 248 | return NULL;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | if (section == NULL) {
|
---|
| 252 | CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_SECTION);
|
---|
| 253 | return NULL;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | return _CONF_get_section_values(conf, section);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | char *NCONF_get_string(const CONF *conf, const char *group, const char *name)
|
---|
| 260 | {
|
---|
| 261 | char *s = _CONF_get_string(conf, group, name);
|
---|
| 262 |
|
---|
| 263 | /*
|
---|
| 264 | * Since we may get a value from an environment variable even if conf is
|
---|
| 265 | * NULL, let's check the value first
|
---|
| 266 | */
|
---|
| 267 | if (s)
|
---|
| 268 | return s;
|
---|
| 269 |
|
---|
| 270 | if (conf == NULL) {
|
---|
| 271 | CONFerr(CONF_F_NCONF_GET_STRING,
|
---|
| 272 | CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
|
---|
| 273 | return NULL;
|
---|
| 274 | }
|
---|
| 275 | CONFerr(CONF_F_NCONF_GET_STRING, CONF_R_NO_VALUE);
|
---|
| 276 | ERR_add_error_data(4, "group=", group, " name=", name);
|
---|
| 277 | return NULL;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | int NCONF_get_number_e(const CONF *conf, const char *group, const char *name,
|
---|
| 281 | long *result)
|
---|
| 282 | {
|
---|
| 283 | char *str;
|
---|
| 284 |
|
---|
| 285 | if (result == NULL) {
|
---|
| 286 | CONFerr(CONF_F_NCONF_GET_NUMBER_E, ERR_R_PASSED_NULL_PARAMETER);
|
---|
| 287 | return 0;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | str = NCONF_get_string(conf, group, name);
|
---|
| 291 |
|
---|
| 292 | if (str == NULL)
|
---|
| 293 | return 0;
|
---|
| 294 |
|
---|
| 295 | for (*result = 0; conf->meth->is_number(conf, *str);) {
|
---|
| 296 | *result = (*result) * 10 + conf->meth->to_int(conf, *str);
|
---|
| 297 | str++;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | return 1;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | #ifndef OPENSSL_NO_STDIO
|
---|
| 304 | int NCONF_dump_fp(const CONF *conf, FILE *out)
|
---|
| 305 | {
|
---|
| 306 | BIO *btmp;
|
---|
| 307 | int ret;
|
---|
| 308 | if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
|
---|
| 309 | CONFerr(CONF_F_NCONF_DUMP_FP, ERR_R_BUF_LIB);
|
---|
| 310 | return 0;
|
---|
| 311 | }
|
---|
| 312 | ret = NCONF_dump_bio(conf, btmp);
|
---|
| 313 | BIO_free(btmp);
|
---|
| 314 | return ret;
|
---|
| 315 | }
|
---|
| 316 | #endif
|
---|
| 317 |
|
---|
| 318 | int NCONF_dump_bio(const CONF *conf, BIO *out)
|
---|
| 319 | {
|
---|
| 320 | if (conf == NULL) {
|
---|
| 321 | CONFerr(CONF_F_NCONF_DUMP_BIO, CONF_R_NO_CONF);
|
---|
| 322 | return 0;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | return conf->meth->dump(conf, out);
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | /*
|
---|
| 329 | * These routines call the C malloc/free, to avoid intermixing with
|
---|
| 330 | * OpenSSL function pointers before the library is initialized.
|
---|
| 331 | */
|
---|
| 332 | OPENSSL_INIT_SETTINGS *OPENSSL_INIT_new(void)
|
---|
| 333 | {
|
---|
| 334 | OPENSSL_INIT_SETTINGS *ret = malloc(sizeof(*ret));
|
---|
| 335 |
|
---|
| 336 | if (ret != NULL)
|
---|
| 337 | memset(ret, 0, sizeof(*ret));
|
---|
| 338 | return ret;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 |
|
---|
| 342 | #ifndef OPENSSL_NO_STDIO
|
---|
| 343 | int OPENSSL_INIT_set_config_appname(OPENSSL_INIT_SETTINGS *settings,
|
---|
| 344 | const char *appname)
|
---|
| 345 | {
|
---|
| 346 | char *newappname = NULL;
|
---|
| 347 |
|
---|
| 348 | if (appname != NULL) {
|
---|
| 349 | newappname = strdup(appname);
|
---|
| 350 | if (newappname == NULL)
|
---|
| 351 | return 0;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | free(settings->appname);
|
---|
| 355 | settings->appname = newappname;
|
---|
| 356 |
|
---|
| 357 | return 1;
|
---|
| 358 | }
|
---|
| 359 | #endif
|
---|
| 360 |
|
---|
| 361 | void OPENSSL_INIT_free(OPENSSL_INIT_SETTINGS *settings)
|
---|
| 362 | {
|
---|
| 363 | free(settings->appname);
|
---|
| 364 | free(settings);
|
---|
| 365 | }
|
---|