| 1 |
/* |
| 2 |
* Copyright 1995-2025 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 |
/* Part of the code in here was originally in conf.c, which is now removed */ |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <string.h> |
| 14 |
#include "internal/e_os.h" /* struct stat */ |
| 15 |
#ifdef __TANDEM |
| 16 |
#include <sys/types.h> /* needed for stat.h */ |
| 17 |
#include <sys/stat.h> /* struct stat */ |
| 18 |
#endif |
| 19 |
#include "internal/cryptlib.h" |
| 20 |
#include "internal/o_dir.h" |
| 21 |
#include <openssl/lhash.h> |
| 22 |
#include <openssl/conf.h> |
| 23 |
#include <openssl/conf_api.h> |
| 24 |
#include "conf_local.h" |
| 25 |
#include "conf_def.h" |
| 26 |
#include <openssl/buffer.h> |
| 27 |
#include <openssl/err.h> |
| 28 |
#ifndef OPENSSL_NO_POSIX_IO |
| 29 |
#include <sys/stat.h> |
| 30 |
#ifdef _WIN32 |
| 31 |
#define stat _stat |
| 32 |
#endif |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifndef S_ISDIR |
| 36 |
#define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR) |
| 37 |
#endif |
| 38 |
|
| 39 |
/* |
| 40 |
* The maximum length we can grow a value to after variable expansion. 64k |
| 41 |
* should be more than enough for all reasonable uses. |
| 42 |
*/ |
| 43 |
#define MAX_CONF_VALUE_LENGTH 65536 |
| 44 |
|
| 45 |
static int is_keytype(const CONF *conf, char c, unsigned short type); |
| 46 |
static char *eat_ws(CONF *conf, char *p); |
| 47 |
static void trim_ws(CONF *conf, char *start); |
| 48 |
static char *eat_alpha_numeric(CONF *conf, char *p); |
| 49 |
static void clear_comments(CONF *conf, char *p); |
| 50 |
static int str_copy(CONF *conf, char *section, char **to, char *from); |
| 51 |
static char *scan_quote(CONF *conf, char *p); |
| 52 |
static char *scan_dquote(CONF *conf, char *p); |
| 53 |
#define scan_esc(conf, p) (((IS_EOF((conf), (p)[1])) ? ((p) + 1) : ((p) + 2))) |
| 54 |
#ifndef OPENSSL_NO_POSIX_IO |
| 55 |
static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx, |
| 56 |
char **dirpath); |
| 57 |
static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx); |
| 58 |
#endif |
| 59 |
|
| 60 |
static CONF *def_create(CONF_METHOD *meth); |
| 61 |
static int def_init_default(CONF *conf); |
| 62 |
#ifndef OPENSSL_NO_DEPRECATED_3_0 |
| 63 |
static int def_init_WIN32(CONF *conf); |
| 64 |
#endif |
| 65 |
static int def_destroy(CONF *conf); |
| 66 |
static int def_destroy_data(CONF *conf); |
| 67 |
static int def_load(CONF *conf, const char *name, long *eline); |
| 68 |
static int def_load_bio(CONF *conf, BIO *bp, long *eline); |
| 69 |
static int def_dump(const CONF *conf, BIO *bp); |
| 70 |
static int def_is_number(const CONF *conf, char c); |
| 71 |
static int def_to_int(const CONF *conf, char c); |
| 72 |
|
| 73 |
static CONF_METHOD default_method = { |
| 74 |
"OpenSSL default", |
| 75 |
def_create, |
| 76 |
def_init_default, |
| 77 |
def_destroy, |
| 78 |
def_destroy_data, |
| 79 |
def_load_bio, |
| 80 |
def_dump, |
| 81 |
def_is_number, |
| 82 |
def_to_int, |
| 83 |
def_load |
| 84 |
}; |
| 85 |
|
| 86 |
CONF_METHOD *NCONF_default(void) |
| 87 |
{ |
| 88 |
return &default_method; |
| 89 |
} |
| 90 |
|
| 91 |
#ifndef OPENSSL_NO_DEPRECATED_3_0 |
| 92 |
static CONF_METHOD WIN32_method = { |
| 93 |
"WIN32", |
| 94 |
def_create, |
| 95 |
def_init_WIN32, |
| 96 |
def_destroy, |
| 97 |
def_destroy_data, |
| 98 |
def_load_bio, |
| 99 |
def_dump, |
| 100 |
def_is_number, |
| 101 |
def_to_int, |
| 102 |
def_load |
| 103 |
}; |
| 104 |
|
| 105 |
CONF_METHOD *NCONF_WIN32(void) |
| 106 |
{ |
| 107 |
return &WIN32_method; |
| 108 |
} |
| 109 |
#endif |
| 110 |
|
| 111 |
static CONF *def_create(CONF_METHOD *meth) |
| 112 |
{ |
| 113 |
CONF *ret; |
| 114 |
|
| 115 |
ret = OPENSSL_malloc(sizeof(*ret)); |
| 116 |
if (ret != NULL) |
| 117 |
if (meth->init(ret) == 0) { |
| 118 |
OPENSSL_free(ret); |
| 119 |
ret = NULL; |
| 120 |
} |
| 121 |
return ret; |
| 122 |
} |
| 123 |
|
| 124 |
static int def_init_default(CONF *conf) |
| 125 |
{ |
| 126 |
if (conf == NULL) |
| 127 |
return 0; |
| 128 |
|
| 129 |
memset(conf, 0, sizeof(*conf)); |
| 130 |
conf->meth = &default_method; |
| 131 |
conf->meth_data = (void *)CONF_type_default; |
| 132 |
|
| 133 |
return 1; |
| 134 |
} |
| 135 |
|
| 136 |
#ifndef OPENSSL_NO_DEPRECATED_3_0 |
| 137 |
static int def_init_WIN32(CONF *conf) |
| 138 |
{ |
| 139 |
if (conf == NULL) |
| 140 |
return 0; |
| 141 |
|
| 142 |
memset(conf, 0, sizeof(*conf)); |
| 143 |
conf->meth = &WIN32_method; |
| 144 |
conf->meth_data = (void *)CONF_type_win32; |
| 145 |
|
| 146 |
return 1; |
| 147 |
} |
| 148 |
#endif |
| 149 |
|
| 150 |
static int def_destroy(CONF *conf) |
| 151 |
{ |
| 152 |
if (def_destroy_data(conf)) { |
| 153 |
OPENSSL_free(conf); |
| 154 |
return 1; |
| 155 |
} |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
static int def_destroy_data(CONF *conf) |
| 160 |
{ |
| 161 |
if (conf == NULL) |
| 162 |
return 0; |
| 163 |
_CONF_free_data(conf); |
| 164 |
return 1; |
| 165 |
} |
| 166 |
|
| 167 |
static int def_load(CONF *conf, const char *name, long *line) |
| 168 |
{ |
| 169 |
int ret; |
| 170 |
BIO *in = NULL; |
| 171 |
|
| 172 |
#ifdef OPENSSL_SYS_VMS |
| 173 |
in = BIO_new_file(name, "r"); |
| 174 |
#else |
| 175 |
in = BIO_new_file(name, "rb"); |
| 176 |
#endif |
| 177 |
if (in == NULL) { |
| 178 |
if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE) |
| 179 |
ERR_raise(ERR_LIB_CONF, CONF_R_NO_SUCH_FILE); |
| 180 |
else |
| 181 |
ERR_raise(ERR_LIB_CONF, ERR_R_SYS_LIB); |
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
ret = def_load_bio(conf, in, line); |
| 186 |
BIO_free(in); |
| 187 |
|
| 188 |
return ret; |
| 189 |
} |
| 190 |
|
| 191 |
/* Parse a boolean value and fill in *flag. Return 0 on error. */ |
| 192 |
static int parsebool(const char *pval, int *flag) |
| 193 |
{ |
| 194 |
if (OPENSSL_strcasecmp(pval, "on") == 0 |
| 195 |
|| OPENSSL_strcasecmp(pval, "true") == 0) { |
| 196 |
*flag = 1; |
| 197 |
} else if (OPENSSL_strcasecmp(pval, "off") == 0 |
| 198 |
|| OPENSSL_strcasecmp(pval, "false") == 0) { |
| 199 |
*flag = 0; |
| 200 |
} else { |
| 201 |
ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA); |
| 202 |
return 0; |
| 203 |
} |
| 204 |
return 1; |
| 205 |
} |
| 206 |
|
| 207 |
static int def_load_bio(CONF *conf, BIO *in, long *line) |
| 208 |
{ |
| 209 |
/* The macro BUFSIZE conflicts with a system macro in VxWorks */ |
| 210 |
#define CONFBUFSIZE 512 |
| 211 |
int bufnum = 0, i, ii; |
| 212 |
BUF_MEM *buff = NULL; |
| 213 |
char *s, *p, *end; |
| 214 |
int again; |
| 215 |
int first_call = 1; |
| 216 |
long eline = 0; |
| 217 |
char btmp[DECIMAL_SIZE(eline) + 1]; |
| 218 |
CONF_VALUE *v = NULL, *tv; |
| 219 |
CONF_VALUE *sv = NULL; |
| 220 |
char *section = NULL, *buf; |
| 221 |
char *start, *psection, *pname; |
| 222 |
void *h = (void *)(conf->data); |
| 223 |
STACK_OF(BIO) *biosk = NULL; |
| 224 |
#ifndef OPENSSL_NO_POSIX_IO |
| 225 |
char *dirpath = NULL; |
| 226 |
OPENSSL_DIR_CTX *dirctx = NULL; |
| 227 |
#endif |
| 228 |
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 229 |
int numincludes = 0; |
| 230 |
#endif |
| 231 |
|
| 232 |
if ((buff = BUF_MEM_new()) == NULL) { |
| 233 |
ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB); |
| 234 |
goto err; |
| 235 |
} |
| 236 |
|
| 237 |
section = OPENSSL_strdup("default"); |
| 238 |
if (section == NULL) |
| 239 |
goto err; |
| 240 |
|
| 241 |
if (_CONF_new_data(conf) == 0) { |
| 242 |
ERR_raise(ERR_LIB_CONF, ERR_R_CONF_LIB); |
| 243 |
goto err; |
| 244 |
} |
| 245 |
|
| 246 |
sv = _CONF_new_section(conf, section); |
| 247 |
if (sv == NULL) { |
| 248 |
ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
| 249 |
goto err; |
| 250 |
} |
| 251 |
|
| 252 |
bufnum = 0; |
| 253 |
again = 0; |
| 254 |
for (;;) { |
| 255 |
if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) { |
| 256 |
ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB); |
| 257 |
goto err; |
| 258 |
} |
| 259 |
p = &(buff->data[bufnum]); |
| 260 |
*p = '\0'; |
| 261 |
read_retry: |
| 262 |
if (in != NULL && BIO_gets(in, p, CONFBUFSIZE - 1) < 0) |
| 263 |
goto err; |
| 264 |
p[CONFBUFSIZE - 1] = '\0'; |
| 265 |
ii = i = (int)strlen(p); |
| 266 |
if (first_call) { |
| 267 |
/* Other BOMs imply unsupported multibyte encoding, |
| 268 |
* so don't strip them and let the error raise */ |
| 269 |
const unsigned char utf8_bom[3] = { 0xEF, 0xBB, 0xBF }; |
| 270 |
|
| 271 |
if (i >= 3 && memcmp(p, utf8_bom, 3) == 0) { |
| 272 |
memmove(p, p + 3, i - 3); |
| 273 |
p[i - 3] = 0; |
| 274 |
i -= 3; |
| 275 |
ii -= 3; |
| 276 |
} |
| 277 |
first_call = 0; |
| 278 |
} |
| 279 |
if (i == 0 && !again) { |
| 280 |
/* the currently processed BIO is NULL or at EOF */ |
| 281 |
BIO *parent; |
| 282 |
|
| 283 |
#ifndef OPENSSL_NO_POSIX_IO |
| 284 |
/* continue processing with the next file from directory */ |
| 285 |
if (dirctx != NULL) { |
| 286 |
BIO *next; |
| 287 |
|
| 288 |
if ((next = get_next_file(dirpath, &dirctx)) != NULL) { |
| 289 |
BIO_vfree(in); |
| 290 |
in = next; |
| 291 |
goto read_retry; |
| 292 |
} else { |
| 293 |
OPENSSL_free(dirpath); |
| 294 |
dirpath = NULL; |
| 295 |
} |
| 296 |
} |
| 297 |
#endif |
| 298 |
/* no more files in directory, continue with processing parent */ |
| 299 |
if ((parent = sk_BIO_pop(biosk)) == NULL) { |
| 300 |
/* everything processed get out of the loop */ |
| 301 |
break; |
| 302 |
} else { |
| 303 |
BIO_vfree(in); |
| 304 |
in = parent; |
| 305 |
goto read_retry; |
| 306 |
} |
| 307 |
} |
| 308 |
again = 0; |
| 309 |
while (i > 0) { |
| 310 |
if ((p[i - 1] != '\r') && (p[i - 1] != '\n')) |
| 311 |
break; |
| 312 |
else |
| 313 |
i--; |
| 314 |
} |
| 315 |
/* |
| 316 |
* we removed some trailing stuff so there is a new line on the end. |
| 317 |
*/ |
| 318 |
if (ii && i == ii) |
| 319 |
again = 1; /* long line */ |
| 320 |
else { |
| 321 |
p[i] = '\0'; |
| 322 |
eline++; /* another input line */ |
| 323 |
} |
| 324 |
|
| 325 |
/* we now have a line with trailing \r\n removed */ |
| 326 |
|
| 327 |
/* i is the number of bytes */ |
| 328 |
bufnum += i; |
| 329 |
|
| 330 |
v = NULL; |
| 331 |
/* check for line continuation */ |
| 332 |
if (!again && bufnum >= 1) { |
| 333 |
/* |
| 334 |
* If we have bytes and the last char '\\' and second last char |
| 335 |
* is not '\\' |
| 336 |
*/ |
| 337 |
p = &(buff->data[bufnum - 1]); |
| 338 |
if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) { |
| 339 |
bufnum--; |
| 340 |
again = 1; |
| 341 |
} |
| 342 |
} |
| 343 |
if (again) |
| 344 |
continue; |
| 345 |
bufnum = 0; |
| 346 |
buf = buff->data; |
| 347 |
|
| 348 |
clear_comments(conf, buf); |
| 349 |
s = eat_ws(conf, buf); |
| 350 |
if (IS_EOF(conf, *s)) |
| 351 |
continue; /* blank line */ |
| 352 |
if (*s == '[') { |
| 353 |
char *ss; |
| 354 |
|
| 355 |
s++; |
| 356 |
start = eat_ws(conf, s); |
| 357 |
ss = start; |
| 358 |
again: |
| 359 |
end = eat_alpha_numeric(conf, ss); |
| 360 |
p = eat_ws(conf, end); |
| 361 |
if (*p != ']') { |
| 362 |
if (*p != '\0' && ss != p) { |
| 363 |
ss = p; |
| 364 |
goto again; |
| 365 |
} |
| 366 |
ERR_raise(ERR_LIB_CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET); |
| 367 |
goto err; |
| 368 |
} |
| 369 |
*end = '\0'; |
| 370 |
if (!str_copy(conf, NULL, §ion, start)) |
| 371 |
goto err; |
| 372 |
if ((sv = _CONF_get_section(conf, section)) == NULL) |
| 373 |
sv = _CONF_new_section(conf, section); |
| 374 |
if (sv == NULL) { |
| 375 |
ERR_raise(ERR_LIB_CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
| 376 |
goto err; |
| 377 |
} |
| 378 |
continue; |
| 379 |
} else { |
| 380 |
pname = s; |
| 381 |
end = eat_alpha_numeric(conf, s); |
| 382 |
if ((end[0] == ':') && (end[1] == ':')) { |
| 383 |
*end = '\0'; |
| 384 |
end += 2; |
| 385 |
psection = pname; |
| 386 |
pname = end; |
| 387 |
end = eat_alpha_numeric(conf, end); |
| 388 |
} else { |
| 389 |
psection = section; |
| 390 |
} |
| 391 |
p = eat_ws(conf, end); |
| 392 |
if (CHECK_AND_SKIP_PREFIX(pname, ".pragma") |
| 393 |
&& (p != pname || *p == '=')) { |
| 394 |
char *pval; |
| 395 |
|
| 396 |
if (*p == '=') { |
| 397 |
p++; |
| 398 |
p = eat_ws(conf, p); |
| 399 |
} |
| 400 |
trim_ws(conf, p); |
| 401 |
|
| 402 |
/* Pragma values take the form keyword:value */ |
| 403 |
pval = strchr(p, ':'); |
| 404 |
if (pval == NULL || pval == p || pval[1] == '\0') { |
| 405 |
ERR_raise(ERR_LIB_CONF, CONF_R_INVALID_PRAGMA); |
| 406 |
goto err; |
| 407 |
} |
| 408 |
|
| 409 |
*pval++ = '\0'; |
| 410 |
trim_ws(conf, p); |
| 411 |
pval = eat_ws(conf, pval); |
| 412 |
|
| 413 |
/* |
| 414 |
* Known pragmas: |
| 415 |
* |
| 416 |
* dollarid takes "on", "true or "off", "false" |
| 417 |
* abspath takes "on", "true or "off", "false" |
| 418 |
* includedir directory prefix |
| 419 |
*/ |
| 420 |
if (strcmp(p, "dollarid") == 0) { |
| 421 |
if (!parsebool(pval, &conf->flag_dollarid)) |
| 422 |
goto err; |
| 423 |
} else if (strcmp(p, "abspath") == 0) { |
| 424 |
if (!parsebool(pval, &conf->flag_abspath)) |
| 425 |
goto err; |
| 426 |
} else if (strcmp(p, "includedir") == 0) { |
| 427 |
OPENSSL_free(conf->includedir); |
| 428 |
if ((conf->includedir = OPENSSL_strdup(pval)) == NULL) |
| 429 |
goto err; |
| 430 |
} |
| 431 |
|
| 432 |
/* |
| 433 |
* We *ignore* any unknown pragma. |
| 434 |
*/ |
| 435 |
continue; |
| 436 |
} else if (CHECK_AND_SKIP_PREFIX(pname, ".include") |
| 437 |
&& (p != pname || *p == '=')) { |
| 438 |
char *include = NULL; |
| 439 |
BIO *next; |
| 440 |
const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE"); |
| 441 |
char *include_path = NULL; |
| 442 |
|
| 443 |
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 444 |
/* |
| 445 |
* The include processing below can cause the "conf" fuzzer to |
| 446 |
* timeout due to the fuzzer inserting large and complicated |
| 447 |
* includes - with a large amount of time spent in |
| 448 |
* OPENSSL_strlcat/OPENSSL_strcpy. This is not a security |
| 449 |
* concern because config files should never come from untrusted |
| 450 |
* sources. We just set an arbitrary limit on the allowed |
| 451 |
* number of includes when fuzzing to prevent this timeout. |
| 452 |
*/ |
| 453 |
if (numincludes++ > 10) |
| 454 |
goto err; |
| 455 |
#endif |
| 456 |
|
| 457 |
if (include_dir == NULL) |
| 458 |
include_dir = conf->includedir; |
| 459 |
|
| 460 |
if (*p == '=') { |
| 461 |
p++; |
| 462 |
p = eat_ws(conf, p); |
| 463 |
} |
| 464 |
trim_ws(conf, p); |
| 465 |
if (!str_copy(conf, psection, &include, p)) |
| 466 |
goto err; |
| 467 |
|
| 468 |
if (include_dir != NULL && !ossl_is_absolute_path(include)) { |
| 469 |
size_t newlen = strlen(include_dir) + strlen(include) + 2; |
| 470 |
|
| 471 |
include_path = OPENSSL_malloc(newlen); |
| 472 |
if (include_path == NULL) { |
| 473 |
OPENSSL_free(include); |
| 474 |
goto err; |
| 475 |
} |
| 476 |
|
| 477 |
OPENSSL_strlcpy(include_path, include_dir, newlen); |
| 478 |
if (!ossl_ends_with_dirsep(include_path)) |
| 479 |
OPENSSL_strlcat(include_path, "/", newlen); |
| 480 |
OPENSSL_strlcat(include_path, include, newlen); |
| 481 |
OPENSSL_free(include); |
| 482 |
} else { |
| 483 |
include_path = include; |
| 484 |
} |
| 485 |
|
| 486 |
if (conf->flag_abspath |
| 487 |
&& !ossl_is_absolute_path(include_path)) { |
| 488 |
ERR_raise(ERR_LIB_CONF, CONF_R_RELATIVE_PATH); |
| 489 |
OPENSSL_free(include_path); |
| 490 |
goto err; |
| 491 |
} |
| 492 |
|
| 493 |
/* get the BIO of the included file */ |
| 494 |
#ifndef OPENSSL_NO_POSIX_IO |
| 495 |
next = process_include(include_path, &dirctx, &dirpath); |
| 496 |
if (include_path != dirpath) { |
| 497 |
/* dirpath will contain include in case of a directory */ |
| 498 |
OPENSSL_free(include_path); |
| 499 |
} |
| 500 |
#else |
| 501 |
next = BIO_new_file(include_path, "r"); |
| 502 |
OPENSSL_free(include_path); |
| 503 |
#endif |
| 504 |
|
| 505 |
if (next != NULL) { |
| 506 |
/* push the currently processing BIO onto stack */ |
| 507 |
if (biosk == NULL) { |
| 508 |
if ((biosk = sk_BIO_new_null()) == NULL) { |
| 509 |
ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB); |
| 510 |
BIO_free(next); |
| 511 |
goto err; |
| 512 |
} |
| 513 |
} |
| 514 |
if (!sk_BIO_push(biosk, in)) { |
| 515 |
ERR_raise(ERR_LIB_CONF, ERR_R_CRYPTO_LIB); |
| 516 |
BIO_free(next); |
| 517 |
goto err; |
| 518 |
} |
| 519 |
/* continue with reading from the included BIO */ |
| 520 |
in = next; |
| 521 |
} |
| 522 |
continue; |
| 523 |
} else if (*p != '=') { |
| 524 |
ERR_raise_data(ERR_LIB_CONF, CONF_R_MISSING_EQUAL_SIGN, |
| 525 |
"HERE-->%s", p); |
| 526 |
goto err; |
| 527 |
} |
| 528 |
*end = '\0'; |
| 529 |
p++; |
| 530 |
start = eat_ws(conf, p); |
| 531 |
trim_ws(conf, start); |
| 532 |
|
| 533 |
if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) |
| 534 |
goto err; |
| 535 |
v->name = OPENSSL_strdup(pname); |
| 536 |
v->value = NULL; |
| 537 |
if (v->name == NULL) |
| 538 |
goto err; |
| 539 |
if (!str_copy(conf, psection, &(v->value), start)) |
| 540 |
goto err; |
| 541 |
|
| 542 |
if (strcmp(psection, section) != 0) { |
| 543 |
if ((tv = _CONF_get_section(conf, psection)) |
| 544 |
== NULL) |
| 545 |
tv = _CONF_new_section(conf, psection); |
| 546 |
if (tv == NULL) { |
| 547 |
ERR_raise(ERR_LIB_CONF, |
| 548 |
CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
| 549 |
goto err; |
| 550 |
} |
| 551 |
} else |
| 552 |
tv = sv; |
| 553 |
if (_CONF_add_string(conf, tv, v) == 0) { |
| 554 |
ERR_raise(ERR_LIB_CONF, ERR_R_CONF_LIB); |
| 555 |
goto err; |
| 556 |
} |
| 557 |
v = NULL; |
| 558 |
} |
| 559 |
} |
| 560 |
BUF_MEM_free(buff); |
| 561 |
OPENSSL_free(section); |
| 562 |
/* |
| 563 |
* No need to pop, since we only get here if the stack is empty. |
| 564 |
* If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE! |
| 565 |
*/ |
| 566 |
sk_BIO_free(biosk); |
| 567 |
return 1; |
| 568 |
|
| 569 |
err: |
| 570 |
BUF_MEM_free(buff); |
| 571 |
OPENSSL_free(section); |
| 572 |
/* |
| 573 |
* Since |in| is the first element of the stack and should NOT be freed |
| 574 |
* here, we cannot use sk_BIO_pop_free(). Instead, we pop and free one |
| 575 |
* BIO at a time, making sure that the last one popped isn't. |
| 576 |
*/ |
| 577 |
while (sk_BIO_num(biosk) > 0) { |
| 578 |
BIO *popped = sk_BIO_pop(biosk); |
| 579 |
BIO_vfree(in); |
| 580 |
in = popped; |
| 581 |
} |
| 582 |
sk_BIO_free(biosk); |
| 583 |
#ifndef OPENSSL_NO_POSIX_IO |
| 584 |
OPENSSL_free(dirpath); |
| 585 |
if (dirctx != NULL) |
| 586 |
OPENSSL_DIR_end(&dirctx); |
| 587 |
#endif |
| 588 |
if (line != NULL) |
| 589 |
*line = eline; |
| 590 |
BIO_snprintf(btmp, sizeof(btmp), "%ld", eline); |
| 591 |
ERR_add_error_data(2, "line ", btmp); |
| 592 |
if (h != conf->data) { |
| 593 |
CONF_free(conf->data); |
| 594 |
conf->data = NULL; |
| 595 |
} |
| 596 |
if (v != NULL) { |
| 597 |
OPENSSL_free(v->name); |
| 598 |
OPENSSL_free(v->value); |
| 599 |
OPENSSL_free(v); |
| 600 |
} |
| 601 |
return 0; |
| 602 |
} |
| 603 |
|
| 604 |
static void clear_comments(CONF *conf, char *p) |
| 605 |
{ |
| 606 |
for (;;) { |
| 607 |
if (IS_FCOMMENT(conf, *p)) { |
| 608 |
*p = '\0'; |
| 609 |
return; |
| 610 |
} |
| 611 |
if (!IS_WS(conf, *p)) { |
| 612 |
break; |
| 613 |
} |
| 614 |
p++; |
| 615 |
} |
| 616 |
|
| 617 |
for (;;) { |
| 618 |
if (IS_COMMENT(conf, *p)) { |
| 619 |
*p = '\0'; |
| 620 |
return; |
| 621 |
} |
| 622 |
if (IS_DQUOTE(conf, *p)) { |
| 623 |
p = scan_dquote(conf, p); |
| 624 |
continue; |
| 625 |
} |
| 626 |
if (IS_QUOTE(conf, *p)) { |
| 627 |
p = scan_quote(conf, p); |
| 628 |
continue; |
| 629 |
} |
| 630 |
if (IS_ESC(conf, *p)) { |
| 631 |
p = scan_esc(conf, p); |
| 632 |
continue; |
| 633 |
} |
| 634 |
if (IS_EOF(conf, *p)) |
| 635 |
return; |
| 636 |
else |
| 637 |
p++; |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
static int str_copy(CONF *conf, char *section, char **pto, char *from) |
| 642 |
{ |
| 643 |
int q, r, rr = 0, to = 0; |
| 644 |
char *s, *e, *rp, *p, *rrp, *np, *cp, v; |
| 645 |
BUF_MEM *buf; |
| 646 |
|
| 647 |
if ((buf = BUF_MEM_new()) == NULL) |
| 648 |
return 0; |
| 649 |
|
| 650 |
if (!BUF_MEM_grow(buf, strlen(from) + 1)) |
| 651 |
goto err; |
| 652 |
|
| 653 |
for (;;) { |
| 654 |
if (IS_QUOTE(conf, *from)) { |
| 655 |
q = *from; |
| 656 |
from++; |
| 657 |
while (!IS_EOF(conf, *from) && (*from != q)) { |
| 658 |
if (IS_ESC(conf, *from)) { |
| 659 |
from++; |
| 660 |
if (IS_EOF(conf, *from)) |
| 661 |
break; |
| 662 |
} |
| 663 |
buf->data[to++] = *(from++); |
| 664 |
} |
| 665 |
if (*from == q) |
| 666 |
from++; |
| 667 |
} else if (IS_DQUOTE(conf, *from)) { |
| 668 |
q = *from; |
| 669 |
from++; |
| 670 |
while (!IS_EOF(conf, *from)) { |
| 671 |
if (*from == q) { |
| 672 |
if (*(from + 1) == q) { |
| 673 |
from++; |
| 674 |
} else { |
| 675 |
break; |
| 676 |
} |
| 677 |
} |
| 678 |
buf->data[to++] = *(from++); |
| 679 |
} |
| 680 |
if (*from == q) |
| 681 |
from++; |
| 682 |
} else if (IS_ESC(conf, *from)) { |
| 683 |
from++; |
| 684 |
v = *(from++); |
| 685 |
if (IS_EOF(conf, v)) |
| 686 |
break; |
| 687 |
else if (v == 'r') |
| 688 |
v = '\r'; |
| 689 |
else if (v == 'n') |
| 690 |
v = '\n'; |
| 691 |
else if (v == 'b') |
| 692 |
v = '\b'; |
| 693 |
else if (v == 't') |
| 694 |
v = '\t'; |
| 695 |
buf->data[to++] = v; |
| 696 |
} else if (IS_EOF(conf, *from)) |
| 697 |
break; |
| 698 |
else if (*from == '$' |
| 699 |
&& (!conf->flag_dollarid |
| 700 |
|| from[1] == '{' |
| 701 |
|| from[1] == '(')) { |
| 702 |
size_t newsize; |
| 703 |
|
| 704 |
/* try to expand it */ |
| 705 |
rrp = NULL; |
| 706 |
s = &(from[1]); |
| 707 |
if (*s == '{') |
| 708 |
q = '}'; |
| 709 |
else if (*s == '(') |
| 710 |
q = ')'; |
| 711 |
else |
| 712 |
q = 0; |
| 713 |
|
| 714 |
if (q) |
| 715 |
s++; |
| 716 |
cp = section; |
| 717 |
e = np = s; |
| 718 |
while (IS_ALNUM(conf, *e) |
| 719 |
|| (conf->flag_dollarid && IS_DOLLAR(conf, *e))) |
| 720 |
e++; |
| 721 |
if ((e[0] == ':') && (e[1] == ':')) { |
| 722 |
cp = np; |
| 723 |
rrp = e; |
| 724 |
rr = *e; |
| 725 |
*rrp = '\0'; |
| 726 |
e += 2; |
| 727 |
np = e; |
| 728 |
while (IS_ALNUM(conf, *e) |
| 729 |
|| (conf->flag_dollarid && IS_DOLLAR(conf, *e))) |
| 730 |
e++; |
| 731 |
} |
| 732 |
r = *e; |
| 733 |
*e = '\0'; |
| 734 |
rp = e; |
| 735 |
if (q) { |
| 736 |
if (r != q) { |
| 737 |
ERR_raise(ERR_LIB_CONF, CONF_R_NO_CLOSE_BRACE); |
| 738 |
goto err; |
| 739 |
} |
| 740 |
e++; |
| 741 |
} |
| 742 |
/*- |
| 743 |
* So at this point we have |
| 744 |
* np which is the start of the name string which is |
| 745 |
* '\0' terminated. |
| 746 |
* cp which is the start of the section string which is |
| 747 |
* '\0' terminated. |
| 748 |
* e is the 'next point after'. |
| 749 |
* r and rr are the chars replaced by the '\0' |
| 750 |
* rp and rrp is where 'r' and 'rr' came from. |
| 751 |
*/ |
| 752 |
p = _CONF_get_string(conf, cp, np); |
| 753 |
if (rrp != NULL) |
| 754 |
*rrp = rr; |
| 755 |
*rp = r; |
| 756 |
if (p == NULL) { |
| 757 |
ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_HAS_NO_VALUE); |
| 758 |
goto err; |
| 759 |
} |
| 760 |
newsize = strlen(p) + buf->length - (e - from); |
| 761 |
if (newsize > MAX_CONF_VALUE_LENGTH) { |
| 762 |
ERR_raise(ERR_LIB_CONF, CONF_R_VARIABLE_EXPANSION_TOO_LONG); |
| 763 |
goto err; |
| 764 |
} |
| 765 |
if (!BUF_MEM_grow_clean(buf, newsize)) { |
| 766 |
ERR_raise(ERR_LIB_CONF, ERR_R_BUF_LIB); |
| 767 |
goto err; |
| 768 |
} |
| 769 |
while (*p) |
| 770 |
buf->data[to++] = *(p++); |
| 771 |
|
| 772 |
from = e; |
| 773 |
|
| 774 |
/* |
| 775 |
* In case there were no braces or parenthesis around the |
| 776 |
* variable reference, we have to put back the character that was |
| 777 |
* replaced with a '\0'. /RL |
| 778 |
*/ |
| 779 |
*rp = r; |
| 780 |
} else |
| 781 |
buf->data[to++] = *(from++); |
| 782 |
} |
| 783 |
buf->data[to] = '\0'; |
| 784 |
OPENSSL_free(*pto); |
| 785 |
*pto = buf->data; |
| 786 |
OPENSSL_free(buf); |
| 787 |
return 1; |
| 788 |
err: |
| 789 |
BUF_MEM_free(buf); |
| 790 |
return 0; |
| 791 |
} |
| 792 |
|
| 793 |
#ifndef OPENSSL_NO_POSIX_IO |
| 794 |
/* |
| 795 |
* Check whether included path is a directory. |
| 796 |
* Returns next BIO to process and in case of a directory |
| 797 |
* also an opened directory context and the include path. |
| 798 |
*/ |
| 799 |
static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx, |
| 800 |
char **dirpath) |
| 801 |
{ |
| 802 |
struct stat st; |
| 803 |
BIO *next; |
| 804 |
|
| 805 |
if (stat(include, &st) < 0) { |
| 806 |
ERR_raise_data(ERR_LIB_SYS, errno, "calling stat(%s)", include); |
| 807 |
/* missing include file is not fatal error */ |
| 808 |
return NULL; |
| 809 |
} |
| 810 |
|
| 811 |
if (S_ISDIR(st.st_mode)) { |
| 812 |
if (*dirctx != NULL) { |
| 813 |
ERR_raise_data(ERR_LIB_CONF, CONF_R_RECURSIVE_DIRECTORY_INCLUDE, |
| 814 |
"%s", include); |
| 815 |
return NULL; |
| 816 |
} |
| 817 |
/* a directory, load its contents */ |
| 818 |
if ((next = get_next_file(include, dirctx)) != NULL) |
| 819 |
*dirpath = include; |
| 820 |
return next; |
| 821 |
} |
| 822 |
|
| 823 |
next = BIO_new_file(include, "r"); |
| 824 |
return next; |
| 825 |
} |
| 826 |
|
| 827 |
/* |
| 828 |
* Get next file from the directory path. |
| 829 |
* Returns BIO of the next file to read and updates dirctx. |
| 830 |
*/ |
| 831 |
static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx) |
| 832 |
{ |
| 833 |
const char *filename; |
| 834 |
size_t pathlen; |
| 835 |
|
| 836 |
pathlen = strlen(path); |
| 837 |
while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) { |
| 838 |
size_t namelen; |
| 839 |
|
| 840 |
namelen = strlen(filename); |
| 841 |
|
| 842 |
if ((namelen > 5 |
| 843 |
&& OPENSSL_strcasecmp(filename + namelen - 5, ".conf") == 0) |
| 844 |
|| (namelen > 4 |
| 845 |
&& OPENSSL_strcasecmp(filename + namelen - 4, ".cnf") == 0)) { |
| 846 |
size_t newlen; |
| 847 |
char *newpath; |
| 848 |
BIO *bio; |
| 849 |
|
| 850 |
newlen = pathlen + namelen + 2; |
| 851 |
newpath = OPENSSL_zalloc(newlen); |
| 852 |
if (newpath == NULL) |
| 853 |
break; |
| 854 |
#ifdef OPENSSL_SYS_VMS |
| 855 |
/* |
| 856 |
* If the given path isn't clear VMS syntax, |
| 857 |
* we treat it as on Unix. |
| 858 |
*/ |
| 859 |
if (path[pathlen - 1] == ']' |
| 860 |
|| path[pathlen - 1] == '>' |
| 861 |
|| path[pathlen - 1] == ':') { |
| 862 |
/* Clear VMS directory syntax, just copy as is */ |
| 863 |
OPENSSL_strlcpy(newpath, path, newlen); |
| 864 |
} |
| 865 |
#endif |
| 866 |
if (newpath[0] == '\0') { |
| 867 |
OPENSSL_strlcpy(newpath, path, newlen); |
| 868 |
OPENSSL_strlcat(newpath, "/", newlen); |
| 869 |
} |
| 870 |
OPENSSL_strlcat(newpath, filename, newlen); |
| 871 |
|
| 872 |
bio = BIO_new_file(newpath, "r"); |
| 873 |
OPENSSL_free(newpath); |
| 874 |
/* Errors when opening files are non-fatal. */ |
| 875 |
if (bio != NULL) |
| 876 |
return bio; |
| 877 |
} |
| 878 |
} |
| 879 |
OPENSSL_DIR_end(dirctx); |
| 880 |
*dirctx = NULL; |
| 881 |
return NULL; |
| 882 |
} |
| 883 |
#endif |
| 884 |
|
| 885 |
static int is_keytype(const CONF *conf, char c, unsigned short type) |
| 886 |
{ |
| 887 |
const unsigned short *keytypes = (const unsigned short *)conf->meth_data; |
| 888 |
unsigned char key = (unsigned char)c; |
| 889 |
|
| 890 |
#ifdef CHARSET_EBCDIC |
| 891 |
#if CHAR_BIT > 8 |
| 892 |
if (key > 255) { |
| 893 |
/* key is out of range for os_toascii table */ |
| 894 |
return 0; |
| 895 |
} |
| 896 |
#endif |
| 897 |
/* convert key from ebcdic to ascii */ |
| 898 |
key = os_toascii[key]; |
| 899 |
#endif |
| 900 |
|
| 901 |
if (key > 127) { |
| 902 |
/* key is not a seven bit ascii character */ |
| 903 |
return 0; |
| 904 |
} |
| 905 |
|
| 906 |
return (keytypes[key] & type) ? 1 : 0; |
| 907 |
} |
| 908 |
|
| 909 |
static char *eat_ws(CONF *conf, char *p) |
| 910 |
{ |
| 911 |
while (IS_WS(conf, *p) && (!IS_EOF(conf, *p))) |
| 912 |
p++; |
| 913 |
return p; |
| 914 |
} |
| 915 |
|
| 916 |
static void trim_ws(CONF *conf, char *start) |
| 917 |
{ |
| 918 |
char *p = start; |
| 919 |
|
| 920 |
while (!IS_EOF(conf, *p)) |
| 921 |
p++; |
| 922 |
p--; |
| 923 |
while ((p >= start) && IS_WS(conf, *p)) |
| 924 |
p--; |
| 925 |
p++; |
| 926 |
*p = '\0'; |
| 927 |
} |
| 928 |
|
| 929 |
static char *eat_alpha_numeric(CONF *conf, char *p) |
| 930 |
{ |
| 931 |
for (;;) { |
| 932 |
if (IS_ESC(conf, *p)) { |
| 933 |
p = scan_esc(conf, p); |
| 934 |
continue; |
| 935 |
} |
| 936 |
if (!(IS_ALNUM_PUNCT(conf, *p) |
| 937 |
|| (conf->flag_dollarid && IS_DOLLAR(conf, *p)))) |
| 938 |
return p; |
| 939 |
p++; |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
static char *scan_quote(CONF *conf, char *p) |
| 944 |
{ |
| 945 |
int q = *p; |
| 946 |
|
| 947 |
p++; |
| 948 |
while (!(IS_EOF(conf, *p)) && (*p != q)) { |
| 949 |
if (IS_ESC(conf, *p)) { |
| 950 |
p++; |
| 951 |
if (IS_EOF(conf, *p)) |
| 952 |
return p; |
| 953 |
} |
| 954 |
p++; |
| 955 |
} |
| 956 |
if (*p == q) |
| 957 |
p++; |
| 958 |
return p; |
| 959 |
} |
| 960 |
|
| 961 |
static char *scan_dquote(CONF *conf, char *p) |
| 962 |
{ |
| 963 |
int q = *p; |
| 964 |
|
| 965 |
p++; |
| 966 |
while (!(IS_EOF(conf, *p))) { |
| 967 |
if (*p == q) { |
| 968 |
if (*(p + 1) == q) { |
| 969 |
p++; |
| 970 |
} else { |
| 971 |
break; |
| 972 |
} |
| 973 |
} |
| 974 |
p++; |
| 975 |
} |
| 976 |
if (*p == q) |
| 977 |
p++; |
| 978 |
return p; |
| 979 |
} |
| 980 |
|
| 981 |
static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out) |
| 982 |
{ |
| 983 |
if (a->name) |
| 984 |
BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value); |
| 985 |
else |
| 986 |
BIO_printf(out, "[[%s]]\n", a->section); |
| 987 |
} |
| 988 |
|
| 989 |
IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO); |
| 990 |
|
| 991 |
static int def_dump(const CONF *conf, BIO *out) |
| 992 |
{ |
| 993 |
lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out); |
| 994 |
return 1; |
| 995 |
} |
| 996 |
|
| 997 |
static int def_is_number(const CONF *conf, char c) |
| 998 |
{ |
| 999 |
return IS_NUMBER(conf, c); |
| 1000 |
} |
| 1001 |
|
| 1002 |
static int def_to_int(const CONF *conf, char c) |
| 1003 |
{ |
| 1004 |
return c - '0'; |
| 1005 |
} |