| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/conf.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <ctype.h> |
| 19 | #include <string.h> |
| 20 |
| 21 | #include <openssl/bio.h> |
| 22 | #include <openssl/buf.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/lhash.h> |
| 25 | #include <openssl/mem.h> |
| 26 |
| 27 | #include "../internal.h" |
| 28 | #include "internal.h" |
| 29 |
| 30 |
| 31 | struct conf_section_st { |
| 32 | char *name; |
| 33 | // values contains non-owning pointers to the values in the section. |
| 34 | STACK_OF(CONF_VALUE) *values; |
| 35 | }; |
| 36 |
| 37 | static const char kDefaultSectionName[] = "default"; |
| 38 |
| 39 | static uint32_t conf_section_hash(const CONF_SECTION *s) { |
| 40 | return OPENSSL_strhash(s->name); |
| 41 | } |
| 42 |
| 43 | static int conf_section_cmp(const CONF_SECTION *a, const CONF_SECTION *b) { |
| 44 | return strcmp(a->name, b->name); |
| 45 | } |
| 46 |
| 47 | static uint32_t conf_value_hash(const CONF_VALUE *v) { |
| 48 | const uint32_t section_hash = OPENSSL_strhash(v->section); |
| 49 | const uint32_t name_hash = OPENSSL_strhash(v->name); |
| 50 | return (section_hash << 2) ^ name_hash; |
| 51 | } |
| 52 |
| 53 | static int conf_value_cmp(const CONF_VALUE *a, const CONF_VALUE *b) { |
| 54 | int cmp = strcmp(a->section, b->section); |
| 55 | if (cmp != 0) { |
| 56 | return cmp; |
| 57 | } |
| 58 |
| 59 | return strcmp(a->name, b->name); |
| 60 | } |
| 61 |
| 62 | CONF *NCONF_new(void *method) { |
| 63 | if (method != NULL) { |
| 64 | return NULL; |
| 65 | } |
| 66 |
| 67 | CONF *conf = reinterpret_cast<CONF *>(OPENSSL_malloc(sizeof(CONF))); |
| 68 | if (conf == NULL) { |
| 69 | return NULL; |
| 70 | } |
| 71 |
| 72 | conf->sections = lh_CONF_SECTION_new(conf_section_hash, conf_section_cmp); |
| 73 | conf->values = lh_CONF_VALUE_new(conf_value_hash, conf_value_cmp); |
| 74 | if (conf->sections == NULL || conf->values == NULL) { |
| 75 | NCONF_free(conf); |
| 76 | return NULL; |
| 77 | } |
| 78 |
| 79 | return conf; |
| 80 | } |
| 81 |
| 82 | CONF_VALUE *CONF_VALUE_new(void) { |
| 83 | return reinterpret_cast<CONF_VALUE *>(OPENSSL_zalloc(sizeof(CONF_VALUE))); |
| 84 | } |
| 85 |
| 86 | static void value_free(CONF_VALUE *value) { |
| 87 | if (value == NULL) { |
| 88 | return; |
| 89 | } |
| 90 | OPENSSL_free(value->section); |
| 91 | OPENSSL_free(value->name); |
| 92 | OPENSSL_free(value->value); |
| 93 | OPENSSL_free(value); |
| 94 | } |
| 95 |
| 96 | static void section_free(CONF_SECTION *section) { |
| 97 | if (section == NULL) { |
| 98 | return; |
| 99 | } |
| 100 | OPENSSL_free(section->name); |
| 101 | sk_CONF_VALUE_free(section->values); |
| 102 | OPENSSL_free(section); |
| 103 | } |
| 104 |
| 105 | static void value_free_arg(CONF_VALUE *value, void *arg) { value_free(value); } |
| 106 |
| 107 | static void section_free_arg(CONF_SECTION *section, void *arg) { |
| 108 | section_free(section); |
| 109 | } |
| 110 |
| 111 | void NCONF_free(CONF *conf) { |
| 112 | if (conf == NULL) { |
| 113 | return; |
| 114 | } |
| 115 |
| 116 | lh_CONF_SECTION_doall_arg(conf->sections, section_free_arg, NULL); |
| 117 | lh_CONF_SECTION_free(conf->sections); |
| 118 | lh_CONF_VALUE_doall_arg(conf->values, value_free_arg, NULL); |
| 119 | lh_CONF_VALUE_free(conf->values); |
| 120 | OPENSSL_free(conf); |
| 121 | } |
| 122 |
| 123 | static CONF_SECTION *NCONF_new_section(const CONF *conf, const char *section) { |
| 124 | CONF_SECTION *s = |
| 125 | reinterpret_cast<CONF_SECTION *>(OPENSSL_malloc(sizeof(CONF_SECTION))); |
| 126 | if (!s) { |
| 127 | return NULL; |
| 128 | } |
| 129 | s->name = OPENSSL_strdup(section); |
| 130 | s->values = sk_CONF_VALUE_new_null(); |
| 131 | if (s->name == NULL || s->values == NULL) { |
| 132 | goto err; |
| 133 | } |
| 134 |
| 135 | CONF_SECTION *old_section; |
| 136 | if (!lh_CONF_SECTION_insert(conf->sections, &old_section, s)) { |
| 137 | goto err; |
| 138 | } |
| 139 | section_free(old_section); |
| 140 | return s; |
| 141 |
| 142 | err: |
| 143 | section_free(s); |
| 144 | return NULL; |
| 145 | } |
| 146 |
| 147 | static int is_comment(char c) { return c == '#'; } |
| 148 |
| 149 | static int is_quote(char c) { return c == '"' || c == '\'' || c == '`'; } |
| 150 |
| 151 | static int is_esc(char c) { return c == '\\'; } |
| 152 |
| 153 | static int is_conf_ws(char c) { |
| 154 | // This differs from |OPENSSL_isspace| in that CONF does not accept '\v' and |
| 155 | // '\f' as whitespace. |
| 156 | return c == ' ' || c == '\t' || c == '\r' || c == '\n'; |
| 157 | } |
| 158 |
| 159 | static int is_name_char(char c) { |
| 160 | // Alphanumeric characters, and a handful of symbols, may appear in value and |
| 161 | // section names without escaping. |
| 162 | return OPENSSL_isalnum(c) || c == '_' || c == '!' || c == '.' || c == '%' || |
| 163 | c == '&' || c == '*' || c == '+' || c == ',' || c == '/' || c == ';' || |
| 164 | c == '?' || c == '@' || c == '^' || c == '~' || c == '|' || c == '-'; |
| 165 | } |
| 166 |
| 167 | static int str_copy(CONF *conf, char *section, char **pto, char *from) { |
| 168 | int q, to = 0, len = 0; |
| 169 | char v; |
| 170 | BUF_MEM *buf; |
| 171 |
| 172 | buf = BUF_MEM_new(); |
| 173 | if (buf == NULL) { |
| 174 | return 0; |
| 175 | } |
| 176 |
| 177 | len = strlen(from) + 1; |
| 178 | if (!BUF_MEM_grow(buf, len)) { |
| 179 | goto err; |
| 180 | } |
| 181 |
| 182 | for (;;) { |
| 183 | if (is_quote(*from)) { |
| 184 | q = *from; |
| 185 | from++; |
| 186 | while (*from != '\0' && *from != q) { |
| 187 | if (is_esc(*from)) { |
| 188 | from++; |
| 189 | if (*from == '\0') { |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | buf->data[to++] = *(from++); |
| 194 | } |
| 195 | if (*from == q) { |
| 196 | from++; |
| 197 | } |
| 198 | } else if (is_esc(*from)) { |
| 199 | from++; |
| 200 | v = *(from++); |
| 201 | if (v == '\0') { |
| 202 | break; |
| 203 | } else if (v == 'r') { |
| 204 | v = '\r'; |
| 205 | } else if (v == 'n') { |
| 206 | v = '\n'; |
| 207 | } else if (v == 'b') { |
| 208 | v = '\b'; |
| 209 | } else if (v == 't') { |
| 210 | v = '\t'; |
| 211 | } |
| 212 | buf->data[to++] = v; |
| 213 | } else if (*from == '\0') { |
| 214 | break; |
| 215 | } else if (*from == '$') { |
| 216 | // Historically, $foo would expand to a previously-parsed value. This |
| 217 | // feature has been removed as it was unused and is a DoS vector. If |
| 218 | // trying to embed '$' in a line, either escape it or wrap the value in |
| 219 | // quotes. |
| 220 | OPENSSL_PUT_ERROR(CONF, CONF_R_VARIABLE_EXPANSION_NOT_SUPPORTED); |
| 221 | goto err; |
| 222 | } else { |
| 223 | buf->data[to++] = *(from++); |
| 224 | } |
| 225 | } |
| 226 |
| 227 | buf->data[to] = '\0'; |
| 228 | OPENSSL_free(*pto); |
| 229 | *pto = buf->data; |
| 230 | OPENSSL_free(buf); |
| 231 | return 1; |
| 232 |
| 233 | err: |
| 234 | BUF_MEM_free(buf); |
| 235 | return 0; |
| 236 | } |
| 237 |
| 238 | static CONF_SECTION *get_section(const CONF *conf, const char *section) { |
| 239 | CONF_SECTION templ; |
| 240 | OPENSSL_memset(&templ, 0, sizeof(templ)); |
| 241 | templ.name = (char *)section; |
| 242 | return lh_CONF_SECTION_retrieve(conf->sections, &templ); |
| 243 | } |
| 244 |
| 245 | const STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, |
| 246 | const char *section) { |
| 247 | const CONF_SECTION *section_obj = get_section(conf, section); |
| 248 | if (section_obj == NULL) { |
| 249 | return NULL; |
| 250 | } |
| 251 | return section_obj->values; |
| 252 | } |
| 253 |
| 254 | const char *NCONF_get_string(const CONF *conf, const char *section, |
| 255 | const char *name) { |
| 256 | CONF_VALUE templ, *value; |
| 257 |
| 258 | if (section == NULL) { |
| 259 | section = kDefaultSectionName; |
| 260 | } |
| 261 |
| 262 | OPENSSL_memset(&templ, 0, sizeof(templ)); |
| 263 | templ.section = (char *)section; |
| 264 | templ.name = (char *)name; |
| 265 | value = lh_CONF_VALUE_retrieve(conf->values, &templ); |
| 266 | if (value == NULL) { |
| 267 | return NULL; |
| 268 | } |
| 269 | return value->value; |
| 270 | } |
| 271 |
| 272 | static int add_string(const CONF *conf, CONF_SECTION *section, |
| 273 | CONF_VALUE *value) { |
| 274 | value->section = OPENSSL_strdup(section->name); |
| 275 | if (value->section == NULL) { |
| 276 | return 0; |
| 277 | } |
| 278 |
| 279 | if (!sk_CONF_VALUE_push(section->values, value)) { |
| 280 | return 0; |
| 281 | } |
| 282 |
| 283 | CONF_VALUE *old_value; |
| 284 | if (!lh_CONF_VALUE_insert(conf->values, &old_value, value)) { |
| 285 | // Remove |value| from |section->values|, so we do not leave a dangling |
| 286 | // pointer. |
| 287 | sk_CONF_VALUE_pop(section->values); |
| 288 | return 0; |
| 289 | } |
| 290 | if (old_value != NULL) { |
| 291 | (void)sk_CONF_VALUE_delete_ptr(section->values, old_value); |
| 292 | value_free(old_value); |
| 293 | } |
| 294 |
| 295 | return 1; |
| 296 | } |
| 297 |
| 298 | static char *eat_ws(char *p) { |
| 299 | while (*p != '\0' && is_conf_ws(*p)) { |
| 300 | p++; |
| 301 | } |
| 302 | return p; |
| 303 | } |
| 304 |
| 305 | static char *scan_esc(char *p) { |
| 306 | assert(p[0] == '\\'); |
| 307 | return p[1] == '\0' ? p + 1 : p + 2; |
| 308 | } |
| 309 |
| 310 | static char *eat_name(char *p) { |
| 311 | for (;;) { |
| 312 | if (is_esc(*p)) { |
| 313 | p = scan_esc(p); |
| 314 | continue; |
| 315 | } |
| 316 | if (!is_name_char(*p)) { |
| 317 | return p; |
| 318 | } |
| 319 | p++; |
| 320 | } |
| 321 | } |
| 322 |
| 323 | static char *scan_quote(char *p) { |
| 324 | int q = *p; |
| 325 |
| 326 | p++; |
| 327 | while (*p != '\0' && *p != q) { |
| 328 | if (is_esc(*p)) { |
| 329 | p++; |
| 330 | if (*p == '\0') { |
| 331 | return p; |
| 332 | } |
| 333 | } |
| 334 | p++; |
| 335 | } |
| 336 | if (*p == q) { |
| 337 | p++; |
| 338 | } |
| 339 | return p; |
| 340 | } |
| 341 |
| 342 | static void clear_comments(char *p) { |
| 343 | for (;;) { |
| 344 | if (!is_conf_ws(*p)) { |
| 345 | break; |
| 346 | } |
| 347 | p++; |
| 348 | } |
| 349 |
| 350 | for (;;) { |
| 351 | if (is_comment(*p)) { |
| 352 | *p = '\0'; |
| 353 | return; |
| 354 | } |
| 355 | if (is_quote(*p)) { |
| 356 | p = scan_quote(p); |
| 357 | continue; |
| 358 | } |
| 359 | if (is_esc(*p)) { |
| 360 | p = scan_esc(p); |
| 361 | continue; |
| 362 | } |
| 363 | if (*p == '\0') { |
| 364 | return; |
| 365 | } else { |
| 366 | p++; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 |
| 371 | int NCONF_load_bio(CONF *conf, BIO *in, long *out_error_line) { |
| 372 | static const size_t CONFBUFSIZE = 512; |
| 373 | int bufnum = 0, i, ii; |
| 374 | BUF_MEM *buff = NULL; |
| 375 | char *s, *p, *end; |
| 376 | int again; |
| 377 | long eline = 0; |
| 378 | CONF_VALUE *v = NULL; |
| 379 | CONF_SECTION *sv = NULL; |
| 380 | char *section = NULL, *buf; |
| 381 | char *start, *psection, *pname; |
| 382 |
| 383 | if ((buff = BUF_MEM_new()) == NULL) { |
| 384 | OPENSSL_PUT_ERROR(CONF, ERR_R_BUF_LIB); |
| 385 | goto err; |
| 386 | } |
| 387 |
| 388 | section = OPENSSL_strdup(kDefaultSectionName); |
| 389 | if (section == NULL) { |
| 390 | goto err; |
| 391 | } |
| 392 |
| 393 | sv = NCONF_new_section(conf, section); |
| 394 | if (sv == NULL) { |
| 395 | OPENSSL_PUT_ERROR(CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
| 396 | goto err; |
| 397 | } |
| 398 |
| 399 | bufnum = 0; |
| 400 | again = 0; |
| 401 | for (;;) { |
| 402 | if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) { |
| 403 | OPENSSL_PUT_ERROR(CONF, ERR_R_BUF_LIB); |
| 404 | goto err; |
| 405 | } |
| 406 | p = &(buff->data[bufnum]); |
| 407 | *p = '\0'; |
| 408 | BIO_gets(in, p, CONFBUFSIZE - 1); |
| 409 | p[CONFBUFSIZE - 1] = '\0'; |
| 410 | ii = i = strlen(p); |
| 411 | if (i == 0 && !again) { |
| 412 | break; |
| 413 | } |
| 414 | again = 0; |
| 415 | while (i > 0) { |
| 416 | if ((p[i - 1] != '\r') && (p[i - 1] != '\n')) { |
| 417 | break; |
| 418 | } else { |
| 419 | i--; |
| 420 | } |
| 421 | } |
| 422 | // we removed some trailing stuff so there is a new |
| 423 | // line on the end. |
| 424 | if (ii && i == ii) { |
| 425 | again = 1; // long line |
| 426 | } else { |
| 427 | p[i] = '\0'; |
| 428 | eline++; // another input line |
| 429 | } |
| 430 |
| 431 | // we now have a line with trailing \r\n removed |
| 432 |
| 433 | // i is the number of bytes |
| 434 | bufnum += i; |
| 435 |
| 436 | v = NULL; |
| 437 | // check for line continuation |
| 438 | if (bufnum >= 1) { |
| 439 | // If we have bytes and the last char '\\' and |
| 440 | // second last char is not '\\' |
| 441 | p = &(buff->data[bufnum - 1]); |
| 442 | if (is_esc(p[0]) && ((bufnum <= 1) || !is_esc(p[-1]))) { |
| 443 | bufnum--; |
| 444 | again = 1; |
| 445 | } |
| 446 | } |
| 447 | if (again) { |
| 448 | continue; |
| 449 | } |
| 450 | bufnum = 0; |
| 451 | buf = buff->data; |
| 452 |
| 453 | clear_comments(buf); |
| 454 | s = eat_ws(buf); |
| 455 | if (*s == '\0') { |
| 456 | continue; // blank line |
| 457 | } |
| 458 | if (*s == '[') { |
| 459 | char *ss; |
| 460 |
| 461 | s++; |
| 462 | start = eat_ws(s); |
| 463 | ss = start; |
| 464 | again: |
| 465 | end = eat_name(ss); |
| 466 | p = eat_ws(end); |
| 467 | if (*p != ']') { |
| 468 | if (*p != '\0' && ss != p) { |
| 469 | ss = p; |
| 470 | goto again; |
| 471 | } |
| 472 | OPENSSL_PUT_ERROR(CONF, CONF_R_MISSING_CLOSE_SQUARE_BRACKET); |
| 473 | goto err; |
| 474 | } |
| 475 | *end = '\0'; |
| 476 | if (!str_copy(conf, NULL, §ion, start)) { |
| 477 | goto err; |
| 478 | } |
| 479 | if ((sv = get_section(conf, section)) == NULL) { |
| 480 | sv = NCONF_new_section(conf, section); |
| 481 | } |
| 482 | if (sv == NULL) { |
| 483 | OPENSSL_PUT_ERROR(CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
| 484 | goto err; |
| 485 | } |
| 486 | continue; |
| 487 | } else { |
| 488 | pname = s; |
| 489 | psection = NULL; |
| 490 | end = eat_name(s); |
| 491 | if ((end[0] == ':') && (end[1] == ':')) { |
| 492 | *end = '\0'; |
| 493 | end += 2; |
| 494 | psection = pname; |
| 495 | pname = end; |
| 496 | end = eat_name(end); |
| 497 | } |
| 498 | p = eat_ws(end); |
| 499 | if (*p != '=') { |
| 500 | OPENSSL_PUT_ERROR(CONF, CONF_R_MISSING_EQUAL_SIGN); |
| 501 | goto err; |
| 502 | } |
| 503 | *end = '\0'; |
| 504 | p++; |
| 505 | start = eat_ws(p); |
| 506 | while (*p != '\0') { |
| 507 | p++; |
| 508 | } |
| 509 | p--; |
| 510 | while (p != start && is_conf_ws(*p)) { |
| 511 | p--; |
| 512 | } |
| 513 | p++; |
| 514 | *p = '\0'; |
| 515 |
| 516 | if (!(v = CONF_VALUE_new())) { |
| 517 | goto err; |
| 518 | } |
| 519 | if (psection == NULL) { |
| 520 | psection = section; |
| 521 | } |
| 522 | v->name = OPENSSL_strdup(pname); |
| 523 | if (v->name == NULL) { |
| 524 | goto err; |
| 525 | } |
| 526 | if (!str_copy(conf, psection, &(v->value), start)) { |
| 527 | goto err; |
| 528 | } |
| 529 |
| 530 | CONF_SECTION *tv; |
| 531 | if (strcmp(psection, section) != 0) { |
| 532 | if ((tv = get_section(conf, psection)) == NULL) { |
| 533 | tv = NCONF_new_section(conf, psection); |
| 534 | } |
| 535 | if (tv == NULL) { |
| 536 | OPENSSL_PUT_ERROR(CONF, CONF_R_UNABLE_TO_CREATE_NEW_SECTION); |
| 537 | goto err; |
| 538 | } |
| 539 | } else { |
| 540 | tv = sv; |
| 541 | } |
| 542 | if (add_string(conf, tv, v) == 0) { |
| 543 | goto err; |
| 544 | } |
| 545 | v = NULL; |
| 546 | } |
| 547 | } |
| 548 | BUF_MEM_free(buff); |
| 549 | OPENSSL_free(section); |
| 550 | return 1; |
| 551 |
| 552 | err: |
| 553 | BUF_MEM_free(buff); |
| 554 | OPENSSL_free(section); |
| 555 | if (out_error_line != NULL) { |
| 556 | *out_error_line = eline; |
| 557 | } |
| 558 | ERR_add_error_dataf("line %ld", eline); |
| 559 | value_free(v); |
| 560 | return 0; |
| 561 | } |
| 562 |
| 563 | int NCONF_load(CONF *conf, const char *filename, long *out_error_line) { |
| 564 | BIO *in = BIO_new_file(filename, "rb"); |
| 565 | int ret; |
| 566 |
| 567 | if (in == NULL) { |
| 568 | OPENSSL_PUT_ERROR(CONF, ERR_R_SYS_LIB); |
| 569 | return 0; |
| 570 | } |
| 571 |
| 572 | ret = NCONF_load_bio(conf, in, out_error_line); |
| 573 | BIO_free(in); |
| 574 |
| 575 | return ret; |
| 576 | } |
| 577 |
| 578 | int CONF_parse_list(const char *list, char sep, int remove_whitespace, |
| 579 | int (*list_cb)(const char *elem, size_t len, void *usr), |
| 580 | void *arg) { |
| 581 | int ret; |
| 582 | const char *lstart, *tmpend, *p; |
| 583 |
| 584 | if (list == NULL) { |
| 585 | OPENSSL_PUT_ERROR(CONF, CONF_R_LIST_CANNOT_BE_NULL); |
| 586 | return 0; |
| 587 | } |
| 588 |
| 589 | lstart = list; |
| 590 | for (;;) { |
| 591 | if (remove_whitespace) { |
| 592 | while (*lstart && OPENSSL_isspace((unsigned char)*lstart)) { |
| 593 | lstart++; |
| 594 | } |
| 595 | } |
| 596 | p = strchr(lstart, sep); |
| 597 | if (p == lstart || !*lstart) { |
| 598 | ret = list_cb(NULL, 0, arg); |
| 599 | } else { |
| 600 | if (p) { |
| 601 | tmpend = p - 1; |
| 602 | } else { |
| 603 | tmpend = lstart + strlen(lstart) - 1; |
| 604 | } |
| 605 | if (remove_whitespace) { |
| 606 | while (OPENSSL_isspace((unsigned char)*tmpend)) { |
| 607 | tmpend--; |
| 608 | } |
| 609 | } |
| 610 | ret = list_cb(lstart, tmpend - lstart + 1, arg); |
| 611 | } |
| 612 | if (ret <= 0) { |
| 613 | return ret; |
| 614 | } |
| 615 | if (p == NULL) { |
| 616 | return 1; |
| 617 | } |
| 618 | lstart = p + 1; |
| 619 | } |
| 620 | } |
| 621 |
| 622 | int CONF_modules_load_file(const char *filename, const char *appname, |
| 623 | unsigned long flags) { |
| 624 | return 1; |
| 625 | } |
| 626 |
| 627 | void CONF_modules_free(void) {} |
| 628 |
| 629 | void OPENSSL_config(const char *config_name) {} |
| 630 |
| 631 | void OPENSSL_no_config(void) {} |
| 632 |