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