William Lallemand | 6e9556b | 2020-05-12 17:52:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 2020 HAProxy Technologies, William Lallemand <wlallemand@haproxy.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version |
| 8 | * 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | */ |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 11 | #include <sys/stat.h> |
| 12 | #include <sys/types.h> |
William Lallemand | 6e9556b | 2020-05-12 17:52:44 +0200 | [diff] [blame] | 13 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 14 | #include <dirent.h> |
William Lallemand | 212e993 | 2020-05-18 08:33:09 +0200 | [diff] [blame] | 15 | #include <errno.h> |
William Lallemand | 6e9556b | 2020-05-12 17:52:44 +0200 | [diff] [blame] | 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 18 | #include <syslog.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 19 | |
| 20 | #include <import/ebpttree.h> |
| 21 | #include <import/ebsttree.h> |
William Lallemand | 6e9556b | 2020-05-12 17:52:44 +0200 | [diff] [blame] | 22 | |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 23 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 24 | #include <haproxy/cli.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 25 | #include <haproxy/errors.h> |
Willy Tarreau | 47d7f90 | 2020-06-04 14:25:47 +0200 | [diff] [blame] | 26 | #include <haproxy/ssl_ckch.h> |
Willy Tarreau | 52d8872 | 2020-06-04 14:29:23 +0200 | [diff] [blame] | 27 | #include <haproxy/ssl_crtlist.h> |
Willy Tarreau | 209108d | 2020-06-04 20:30:20 +0200 | [diff] [blame] | 28 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 29 | #include <haproxy/stream_interface.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 30 | #include <haproxy/tools.h> |
William Lallemand | 6e9556b | 2020-05-12 17:52:44 +0200 | [diff] [blame] | 31 | |
William Lallemand | 6e9556b | 2020-05-12 17:52:44 +0200 | [diff] [blame] | 32 | |
William Lallemand | 6e9556b | 2020-05-12 17:52:44 +0200 | [diff] [blame] | 33 | /* release ssl bind conf */ |
| 34 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
| 35 | { |
| 36 | if (conf) { |
| 37 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 38 | free(conf->npn_str); |
| 39 | conf->npn_str = NULL; |
| 40 | #endif |
| 41 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 42 | free(conf->alpn_str); |
| 43 | conf->alpn_str = NULL; |
| 44 | #endif |
| 45 | free(conf->ca_file); |
| 46 | conf->ca_file = NULL; |
| 47 | free(conf->ca_verify_file); |
| 48 | conf->ca_verify_file = NULL; |
| 49 | free(conf->crl_file); |
| 50 | conf->crl_file = NULL; |
| 51 | free(conf->ciphers); |
| 52 | conf->ciphers = NULL; |
| 53 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 54 | free(conf->ciphersuites); |
| 55 | conf->ciphersuites = NULL; |
| 56 | #endif |
| 57 | free(conf->curves); |
| 58 | conf->curves = NULL; |
| 59 | free(conf->ecdhe); |
| 60 | conf->ecdhe = NULL; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /* free sni filters */ |
| 66 | void crtlist_free_filters(char **args) |
| 67 | { |
| 68 | int i; |
| 69 | |
| 70 | if (!args) |
| 71 | return; |
| 72 | |
| 73 | for (i = 0; args[i]; i++) |
| 74 | free(args[i]); |
| 75 | |
| 76 | free(args); |
| 77 | } |
| 78 | |
| 79 | /* Alloc and duplicate a char ** array */ |
| 80 | char **crtlist_dup_filters(char **args, int fcount) |
| 81 | { |
| 82 | char **dst; |
| 83 | int i; |
| 84 | |
| 85 | if (fcount == 0) |
| 86 | return NULL; |
| 87 | |
| 88 | dst = calloc(fcount + 1, sizeof(*dst)); |
| 89 | if (!dst) |
| 90 | return NULL; |
| 91 | |
| 92 | for (i = 0; i < fcount; i++) { |
| 93 | dst[i] = strdup(args[i]); |
| 94 | if (!dst[i]) |
| 95 | goto error; |
| 96 | } |
| 97 | return dst; |
| 98 | |
| 99 | error: |
| 100 | crtlist_free_filters(dst); |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Detach and free a crtlist_entry. |
| 106 | * Free the filters, the ssl_conf and call ckch_inst_free() for each ckch_inst |
| 107 | */ |
| 108 | void crtlist_entry_free(struct crtlist_entry *entry) |
| 109 | { |
| 110 | struct ckch_inst *inst, *inst_s; |
| 111 | |
| 112 | if (entry == NULL) |
| 113 | return; |
| 114 | |
| 115 | ebpt_delete(&entry->node); |
| 116 | LIST_DEL(&entry->by_crtlist); |
| 117 | LIST_DEL(&entry->by_ckch_store); |
| 118 | crtlist_free_filters(entry->filters); |
| 119 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 120 | free(entry->ssl_conf); |
| 121 | list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) { |
| 122 | ckch_inst_free(inst); |
| 123 | } |
| 124 | free(entry); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Allocate and initialize a crtlist_entry |
| 129 | */ |
| 130 | struct crtlist_entry *crtlist_entry_new() |
| 131 | { |
| 132 | struct crtlist_entry *entry; |
| 133 | |
| 134 | entry = calloc(1, sizeof(*entry)); |
| 135 | if (entry == NULL) |
| 136 | return NULL; |
| 137 | |
| 138 | LIST_INIT(&entry->ckch_inst); |
| 139 | |
| 140 | /* initialize the nodes so we can LIST_DEL in any cases */ |
| 141 | LIST_INIT(&entry->by_crtlist); |
| 142 | LIST_INIT(&entry->by_ckch_store); |
| 143 | |
| 144 | return entry; |
| 145 | } |
| 146 | |
| 147 | /* Free a crtlist, from the crt_entry to the content of the ssl_conf */ |
| 148 | void crtlist_free(struct crtlist *crtlist) |
| 149 | { |
| 150 | struct crtlist_entry *entry, *s_entry; |
William Lallemand | 6a3168a | 2020-06-23 11:43:35 +0200 | [diff] [blame] | 151 | struct bind_conf_list *bind_conf_node; |
William Lallemand | 6e9556b | 2020-05-12 17:52:44 +0200 | [diff] [blame] | 152 | |
| 153 | if (crtlist == NULL) |
| 154 | return; |
| 155 | |
William Lallemand | 6a3168a | 2020-06-23 11:43:35 +0200 | [diff] [blame] | 156 | bind_conf_node = crtlist->bind_conf; |
| 157 | while (bind_conf_node) { |
| 158 | struct bind_conf_list *next = bind_conf_node->next; |
| 159 | free(bind_conf_node); |
| 160 | bind_conf_node = next; |
| 161 | } |
| 162 | |
William Lallemand | 6e9556b | 2020-05-12 17:52:44 +0200 | [diff] [blame] | 163 | list_for_each_entry_safe(entry, s_entry, &crtlist->ord_entries, by_crtlist) { |
| 164 | crtlist_entry_free(entry); |
| 165 | } |
| 166 | ebmb_delete(&crtlist->node); |
| 167 | free(crtlist); |
| 168 | } |
| 169 | |
| 170 | /* Alloc and initialize a struct crtlist |
| 171 | * <filename> is the key of the ebmb_node |
| 172 | * <unique> initialize the list of entries to be unique (1) or not (0) |
| 173 | */ |
| 174 | struct crtlist *crtlist_new(const char *filename, int unique) |
| 175 | { |
| 176 | struct crtlist *newlist; |
| 177 | |
| 178 | newlist = calloc(1, sizeof(*newlist) + strlen(filename) + 1); |
| 179 | if (newlist == NULL) |
| 180 | return NULL; |
| 181 | |
| 182 | memcpy(newlist->node.key, filename, strlen(filename) + 1); |
| 183 | if (unique) |
| 184 | newlist->entries = EB_ROOT_UNIQUE; |
| 185 | else |
| 186 | newlist->entries = EB_ROOT; |
| 187 | |
| 188 | LIST_INIT(&newlist->ord_entries); |
| 189 | |
| 190 | return newlist; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Read a single crt-list line. /!\ alter the <line> string. |
| 195 | * Fill <crt_path> and <crtlist_entry> |
| 196 | * <crtlist_entry> must be alloc and free by the caller |
| 197 | * <crtlist_entry->ssl_conf> is alloc by the function |
| 198 | * <crtlist_entry->filters> is alloc by the function |
| 199 | * <crt_path> is a ptr in <line> |
| 200 | * Return an error code |
| 201 | */ |
| 202 | int crtlist_parse_line(char *line, char **crt_path, struct crtlist_entry *entry, const char *file, int linenum, char **err) |
| 203 | { |
| 204 | int cfgerr = 0; |
| 205 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
| 206 | char *end; |
| 207 | char *args[MAX_CRT_ARGS + 1]; |
| 208 | struct ssl_bind_conf *ssl_conf = NULL; |
| 209 | |
| 210 | if (!line || !crt_path || !entry) |
| 211 | return ERR_ALERT | ERR_FATAL; |
| 212 | |
| 213 | end = line + strlen(line); |
| 214 | if (end-line >= CRT_LINESIZE-1 && *(end-1) != '\n') { |
| 215 | /* Check if we reached the limit and the last char is not \n. |
| 216 | * Watch out for the last line without the terminating '\n'! |
| 217 | */ |
| 218 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 219 | linenum, file, CRT_LINESIZE-1); |
| 220 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 221 | goto error; |
| 222 | } |
| 223 | arg = 0; |
| 224 | newarg = 1; |
| 225 | while (*line) { |
| 226 | if (isspace((unsigned char)*line)) { |
| 227 | newarg = 1; |
| 228 | *line = 0; |
| 229 | } else if (*line == '[') { |
| 230 | if (ssl_b) { |
| 231 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
| 232 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 233 | goto error; |
| 234 | } |
| 235 | if (!arg) { |
| 236 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
| 237 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 238 | goto error; |
| 239 | } |
| 240 | ssl_b = arg; |
| 241 | newarg = 1; |
| 242 | *line = 0; |
| 243 | } else if (*line == ']') { |
| 244 | if (ssl_e) { |
| 245 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
| 246 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 247 | goto error; |
| 248 | } |
| 249 | if (!ssl_b) { |
| 250 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
| 251 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 252 | goto error; |
| 253 | } |
| 254 | ssl_e = arg; |
| 255 | newarg = 1; |
| 256 | *line = 0; |
| 257 | } else if (newarg) { |
| 258 | if (arg == MAX_CRT_ARGS) { |
| 259 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
| 260 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 261 | goto error; |
| 262 | } |
| 263 | newarg = 0; |
| 264 | args[arg++] = line; |
| 265 | } |
| 266 | line++; |
| 267 | } |
| 268 | args[arg++] = line; |
| 269 | |
| 270 | /* empty line */ |
| 271 | if (!*args[0]) { |
| 272 | cfgerr |= ERR_NONE; |
| 273 | goto error; |
| 274 | } |
| 275 | |
| 276 | *crt_path = args[0]; |
| 277 | |
| 278 | if (ssl_b) { |
| 279 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 280 | if (!ssl_conf) { |
| 281 | memprintf(err, "not enough memory!"); |
| 282 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 283 | goto error; |
| 284 | } |
| 285 | } |
| 286 | cur_arg = ssl_b ? ssl_b : 1; |
| 287 | while (cur_arg < ssl_e) { |
| 288 | newarg = 0; |
| 289 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 290 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 291 | newarg = 1; |
| 292 | cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, NULL, ssl_conf, err); |
| 293 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 294 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 295 | args[cur_arg], linenum, file); |
| 296 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 297 | goto error; |
| 298 | } |
| 299 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | if (!cfgerr && !newarg) { |
| 304 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 305 | args[cur_arg], linenum, file); |
| 306 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 307 | goto error; |
| 308 | } |
| 309 | } |
| 310 | entry->linenum = linenum; |
| 311 | entry->ssl_conf = ssl_conf; |
| 312 | entry->filters = crtlist_dup_filters(&args[cur_arg], arg - cur_arg - 1); |
| 313 | entry->fcount = arg - cur_arg - 1; |
| 314 | |
| 315 | return cfgerr; |
| 316 | |
| 317 | error: |
| 318 | crtlist_free_filters(entry->filters); |
| 319 | entry->filters = NULL; |
| 320 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 321 | free(entry->ssl_conf); |
| 322 | entry->ssl_conf = NULL; |
| 323 | return cfgerr; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | |
| 328 | /* This function parse a crt-list file and store it in a struct crtlist, each line is a crtlist_entry structure |
| 329 | * Fill the <crtlist> argument with a pointer to a new crtlist struct |
| 330 | * |
| 331 | * This function tries to open and store certificate files. |
| 332 | */ |
| 333 | int crtlist_parse_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, struct crtlist **crtlist, char **err) |
| 334 | { |
| 335 | struct crtlist *newlist; |
| 336 | struct crtlist_entry *entry = NULL; |
| 337 | char thisline[CRT_LINESIZE]; |
| 338 | char path[MAXPATHLEN+1]; |
| 339 | FILE *f; |
| 340 | struct stat buf; |
| 341 | int linenum = 0; |
| 342 | int cfgerr = 0; |
| 343 | |
| 344 | if ((f = fopen(file, "r")) == NULL) { |
| 345 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
| 346 | return ERR_ALERT | ERR_FATAL; |
| 347 | } |
| 348 | |
| 349 | newlist = crtlist_new(file, 0); |
| 350 | if (newlist == NULL) { |
| 351 | memprintf(err, "Not enough memory!"); |
| 352 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 353 | goto error; |
| 354 | } |
| 355 | |
| 356 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 357 | char *end; |
| 358 | char *line = thisline; |
| 359 | char *crt_path; |
| 360 | struct ckch_store *ckchs; |
| 361 | |
| 362 | linenum++; |
| 363 | end = line + strlen(line); |
| 364 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 365 | /* Check if we reached the limit and the last char is not \n. |
| 366 | * Watch out for the last line without the terminating '\n'! |
| 367 | */ |
| 368 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 369 | linenum, file, (int)sizeof(thisline)-1); |
| 370 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | if (*line == '#' || *line == '\n' || *line == '\r') |
| 375 | continue; |
| 376 | |
| 377 | entry = crtlist_entry_new(); |
| 378 | if (entry == NULL) { |
| 379 | memprintf(err, "Not enough memory!"); |
| 380 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 381 | goto error; |
| 382 | } |
| 383 | |
| 384 | *(end - 1) = '\0'; /* line parser mustn't receive any \n */ |
| 385 | cfgerr |= crtlist_parse_line(thisline, &crt_path, entry, file, linenum, err); |
| 386 | if (cfgerr) |
| 387 | goto error; |
| 388 | |
| 389 | /* empty line */ |
| 390 | if (!crt_path || !*crt_path) { |
| 391 | crtlist_entry_free(entry); |
| 392 | entry = NULL; |
| 393 | continue; |
| 394 | } |
| 395 | |
| 396 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 397 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 398 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 399 | crt_path, linenum, file); |
| 400 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 401 | goto error; |
| 402 | } |
| 403 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 404 | crt_path = path; |
| 405 | } |
| 406 | |
| 407 | /* Look for a ckch_store or create one */ |
| 408 | ckchs = ckchs_lookup(crt_path); |
| 409 | if (ckchs == NULL) { |
| 410 | if (stat(crt_path, &buf) == 0) |
| 411 | ckchs = ckchs_load_cert_file(crt_path, 0, err); |
| 412 | else |
| 413 | ckchs = ckchs_load_cert_file(crt_path, 1, err); |
| 414 | } |
| 415 | if (ckchs == NULL) |
| 416 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 417 | |
| 418 | if (cfgerr & ERR_CODE) |
| 419 | goto error; |
| 420 | |
| 421 | entry->node.key = ckchs; |
| 422 | entry->crtlist = newlist; |
| 423 | ebpt_insert(&newlist->entries, &entry->node); |
| 424 | LIST_ADDQ(&newlist->ord_entries, &entry->by_crtlist); |
| 425 | LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store); |
| 426 | |
| 427 | entry = NULL; |
| 428 | } |
| 429 | if (cfgerr & ERR_CODE) |
| 430 | goto error; |
| 431 | |
| 432 | newlist->linecount = linenum; |
| 433 | |
| 434 | fclose(f); |
| 435 | *crtlist = newlist; |
| 436 | |
| 437 | return cfgerr; |
| 438 | error: |
| 439 | crtlist_entry_free(entry); |
| 440 | |
| 441 | fclose(f); |
| 442 | crtlist_free(newlist); |
| 443 | return cfgerr; |
| 444 | } |
| 445 | |
| 446 | /* This function reads a directory and stores it in a struct crtlist, each file is a crtlist_entry structure |
| 447 | * Fill the <crtlist> argument with a pointer to a new crtlist struct |
| 448 | * |
| 449 | * This function tries to open and store certificate files. |
| 450 | */ |
| 451 | int crtlist_load_cert_dir(char *path, struct bind_conf *bind_conf, struct crtlist **crtlist, char **err) |
| 452 | { |
| 453 | struct crtlist *dir; |
| 454 | struct dirent **de_list; |
| 455 | int i, n; |
| 456 | struct stat buf; |
| 457 | char *end; |
| 458 | char fp[MAXPATHLEN+1]; |
| 459 | int cfgerr = 0; |
| 460 | struct ckch_store *ckchs; |
| 461 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 462 | int is_bundle; |
| 463 | int j; |
| 464 | #endif |
| 465 | |
| 466 | dir = crtlist_new(path, 1); |
| 467 | if (dir == NULL) { |
| 468 | memprintf(err, "not enough memory"); |
| 469 | return ERR_ALERT | ERR_FATAL; |
| 470 | } |
| 471 | |
| 472 | n = scandir(path, &de_list, 0, alphasort); |
| 473 | if (n < 0) { |
| 474 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 475 | err && *err ? *err : "", path, strerror(errno)); |
| 476 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 477 | } |
| 478 | else { |
| 479 | for (i = 0; i < n; i++) { |
| 480 | struct crtlist_entry *entry; |
| 481 | struct dirent *de = de_list[i]; |
| 482 | |
| 483 | end = strrchr(de->d_name, '.'); |
| 484 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key"))) |
| 485 | goto ignore_entry; |
| 486 | |
| 487 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 488 | if (stat(fp, &buf) != 0) { |
| 489 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 490 | err && *err ? *err : "", fp, strerror(errno)); |
| 491 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 492 | goto ignore_entry; |
| 493 | } |
| 494 | if (!S_ISREG(buf.st_mode)) |
| 495 | goto ignore_entry; |
| 496 | |
| 497 | entry = crtlist_entry_new(); |
| 498 | if (entry == NULL) { |
| 499 | memprintf(err, "not enough memory '%s'", fp); |
| 500 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 501 | goto ignore_entry; |
| 502 | } |
| 503 | |
| 504 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 505 | is_bundle = 0; |
| 506 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 507 | |
| 508 | if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) { |
| 509 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 510 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 511 | is_bundle = 1; |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | if (is_bundle) { |
| 517 | int dp_len; |
| 518 | |
| 519 | dp_len = end - de->d_name; |
| 520 | |
| 521 | /* increment i and free de until we get to a non-bundle cert |
| 522 | * Note here that we look at de_list[i + 1] before freeing de |
| 523 | * this is important since ignore_entry will free de. This also |
| 524 | * guarantees that de->d_name continues to hold the same prefix. |
| 525 | */ |
| 526 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) { |
| 527 | free(de); |
| 528 | i++; |
| 529 | de = de_list[i]; |
| 530 | } |
| 531 | |
| 532 | snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name); |
| 533 | ckchs = ckchs_lookup(fp); |
| 534 | if (ckchs == NULL) |
| 535 | ckchs = ckchs_load_cert_file(fp, 1, err); |
| 536 | if (ckchs == NULL) { |
| 537 | free(de); |
| 538 | free(entry); |
| 539 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 540 | goto end; |
| 541 | } |
| 542 | entry->node.key = ckchs; |
| 543 | entry->crtlist = dir; |
| 544 | LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store); |
| 545 | LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist); |
| 546 | ebpt_insert(&dir->entries, &entry->node); |
| 547 | |
| 548 | /* Successfully processed the bundle */ |
| 549 | goto ignore_entry; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | #endif |
| 554 | ckchs = ckchs_lookup(fp); |
| 555 | if (ckchs == NULL) |
| 556 | ckchs = ckchs_load_cert_file(fp, 0, err); |
| 557 | if (ckchs == NULL) { |
| 558 | free(de); |
| 559 | free(entry); |
| 560 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 561 | goto end; |
| 562 | } |
| 563 | entry->node.key = ckchs; |
| 564 | entry->crtlist = dir; |
| 565 | LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store); |
| 566 | LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist); |
| 567 | ebpt_insert(&dir->entries, &entry->node); |
| 568 | |
| 569 | ignore_entry: |
| 570 | free(de); |
| 571 | } |
| 572 | end: |
| 573 | free(de_list); |
| 574 | } |
| 575 | |
| 576 | if (cfgerr & ERR_CODE) { |
| 577 | /* free the dir and entries on error */ |
| 578 | crtlist_free(dir); |
| 579 | } else { |
| 580 | *crtlist = dir; |
| 581 | } |
| 582 | return cfgerr; |
| 583 | |
| 584 | } |
| 585 | |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 586 | /* |
| 587 | * Take an ssl_bind_conf structure and append the configuration line used to |
| 588 | * create it in the buffer |
| 589 | */ |
| 590 | static void dump_crtlist_sslconf(struct buffer *buf, const struct ssl_bind_conf *conf) |
| 591 | { |
| 592 | int space = 0; |
| 593 | |
| 594 | if (conf == NULL) |
| 595 | return; |
| 596 | |
| 597 | chunk_appendf(buf, " ["); |
| 598 | #ifdef OPENSSL_NPN_NEGOTIATED |
| 599 | if (conf->npn_str) { |
| 600 | int len = conf->npn_len; |
| 601 | char *ptr = conf->npn_str; |
| 602 | int comma = 0; |
| 603 | |
| 604 | if (space) chunk_appendf(buf, " "); |
| 605 | chunk_appendf(buf, "npn "); |
| 606 | while (len) { |
| 607 | unsigned short size; |
| 608 | |
| 609 | size = *ptr; |
| 610 | ptr++; |
| 611 | if (comma) |
| 612 | chunk_memcat(buf, ",", 1); |
| 613 | chunk_memcat(buf, ptr, size); |
| 614 | ptr += size; |
| 615 | len -= size + 1; |
| 616 | comma = 1; |
| 617 | } |
| 618 | chunk_memcat(buf, "", 1); /* finish with a \0 */ |
| 619 | space++; |
| 620 | } |
| 621 | #endif |
| 622 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 623 | if (conf->alpn_str) { |
| 624 | int len = conf->alpn_len; |
| 625 | char *ptr = conf->alpn_str; |
| 626 | int comma = 0; |
| 627 | |
| 628 | if (space) chunk_appendf(buf, " "); |
| 629 | chunk_appendf(buf, "alpn "); |
| 630 | while (len) { |
| 631 | unsigned short size; |
| 632 | |
| 633 | size = *ptr; |
| 634 | ptr++; |
| 635 | if (comma) |
| 636 | chunk_memcat(buf, ",", 1); |
| 637 | chunk_memcat(buf, ptr, size); |
| 638 | ptr += size; |
| 639 | len -= size + 1; |
| 640 | comma = 1; |
| 641 | } |
| 642 | chunk_memcat(buf, "", 1); /* finish with a \0 */ |
| 643 | space++; |
| 644 | } |
| 645 | #endif |
| 646 | /* verify */ |
| 647 | { |
| 648 | if (conf->verify == SSL_SOCK_VERIFY_NONE) { |
| 649 | if (space) chunk_appendf(buf, " "); |
| 650 | chunk_appendf(buf, "verify none"); |
| 651 | space++; |
| 652 | } else if (conf->verify == SSL_SOCK_VERIFY_OPTIONAL) { |
| 653 | if (space) chunk_appendf(buf, " "); |
| 654 | chunk_appendf(buf, "verify optional"); |
| 655 | space++; |
| 656 | } else if (conf->verify == SSL_SOCK_VERIFY_REQUIRED) { |
| 657 | if (space) chunk_appendf(buf, " "); |
| 658 | chunk_appendf(buf, "verify required"); |
| 659 | space++; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | if (conf->no_ca_names) { |
| 664 | if (space) chunk_appendf(buf, " "); |
| 665 | chunk_appendf(buf, "no-ca-names"); |
| 666 | space++; |
| 667 | } |
| 668 | |
| 669 | if (conf->early_data) { |
| 670 | if (space) chunk_appendf(buf, " "); |
| 671 | chunk_appendf(buf, "allow-0rtt"); |
| 672 | space++; |
| 673 | } |
| 674 | if (conf->ca_file) { |
| 675 | if (space) chunk_appendf(buf, " "); |
| 676 | chunk_appendf(buf, "ca-file %s", conf->ca_file); |
| 677 | space++; |
| 678 | } |
| 679 | if (conf->crl_file) { |
| 680 | if (space) chunk_appendf(buf, " "); |
| 681 | chunk_appendf(buf, "crl-file %s", conf->crl_file); |
| 682 | space++; |
| 683 | } |
| 684 | if (conf->ciphers) { |
| 685 | if (space) chunk_appendf(buf, " "); |
| 686 | chunk_appendf(buf, "ciphers %s", conf->ciphers); |
| 687 | space++; |
| 688 | } |
| 689 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER) |
| 690 | if (conf->ciphersuites) { |
| 691 | if (space) chunk_appendf(buf, " "); |
| 692 | chunk_appendf(buf, "ciphersuites %s", conf->ciphersuites); |
| 693 | space++; |
| 694 | } |
| 695 | #endif |
| 696 | if (conf->curves) { |
| 697 | if (space) chunk_appendf(buf, " "); |
| 698 | chunk_appendf(buf, "curves %s", conf->curves); |
| 699 | space++; |
| 700 | } |
| 701 | if (conf->ecdhe) { |
| 702 | if (space) chunk_appendf(buf, " "); |
| 703 | chunk_appendf(buf, "ecdhe %s", conf->ecdhe); |
| 704 | space++; |
| 705 | } |
| 706 | |
| 707 | /* the crt-lists only support ssl-min-ver and ssl-max-ver */ |
William Lallemand | 8177ad9 | 2020-05-20 16:49:02 +0200 | [diff] [blame] | 708 | if (conf->ssl_methods_cfg.min) { |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 709 | if (space) chunk_appendf(buf, " "); |
William Lallemand | 8177ad9 | 2020-05-20 16:49:02 +0200 | [diff] [blame] | 710 | chunk_appendf(buf, "ssl-min-ver %s", methodVersions[conf->ssl_methods_cfg.min].name); |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 711 | space++; |
| 712 | } |
| 713 | |
William Lallemand | 8177ad9 | 2020-05-20 16:49:02 +0200 | [diff] [blame] | 714 | if (conf->ssl_methods_cfg.max) { |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 715 | if (space) chunk_appendf(buf, " "); |
William Lallemand | 8177ad9 | 2020-05-20 16:49:02 +0200 | [diff] [blame] | 716 | chunk_appendf(buf, "ssl-max-ver %s", methodVersions[conf->ssl_methods_cfg.max].name); |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 717 | space++; |
| 718 | } |
| 719 | |
| 720 | chunk_appendf(buf, "]"); |
| 721 | |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | /* dump a list of filters */ |
| 726 | static void dump_crtlist_filters(struct buffer *buf, struct crtlist_entry *entry) |
| 727 | { |
| 728 | int i; |
| 729 | |
| 730 | if (!entry->fcount) |
| 731 | return; |
| 732 | |
| 733 | for (i = 0; i < entry->fcount; i++) { |
| 734 | chunk_appendf(buf, " %s", entry->filters[i]); |
| 735 | } |
| 736 | return; |
| 737 | } |
| 738 | |
| 739 | /************************** CLI functions ****************************/ |
| 740 | |
| 741 | |
| 742 | /* CLI IO handler for '(show|dump) ssl crt-list' */ |
| 743 | static int cli_io_handler_dump_crtlist(struct appctx *appctx) |
| 744 | { |
| 745 | struct buffer *trash = alloc_trash_chunk(); |
| 746 | struct stream_interface *si = appctx->owner; |
| 747 | struct ebmb_node *lnode; |
| 748 | |
| 749 | if (trash == NULL) |
| 750 | return 1; |
| 751 | |
| 752 | /* dump the list of crt-lists */ |
| 753 | lnode = appctx->ctx.cli.p1; |
| 754 | if (lnode == NULL) |
| 755 | lnode = ebmb_first(&crtlists_tree); |
| 756 | while (lnode) { |
| 757 | chunk_appendf(trash, "%s\n", lnode->key); |
| 758 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 759 | si_rx_room_blk(si); |
| 760 | goto yield; |
| 761 | } |
| 762 | lnode = ebmb_next(lnode); |
| 763 | } |
| 764 | free_trash_chunk(trash); |
| 765 | return 1; |
| 766 | yield: |
| 767 | appctx->ctx.cli.p1 = lnode; |
| 768 | free_trash_chunk(trash); |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | /* CLI IO handler for '(show|dump) ssl crt-list <filename>' */ |
| 773 | static int cli_io_handler_dump_crtlist_entries(struct appctx *appctx) |
| 774 | { |
| 775 | struct buffer *trash = alloc_trash_chunk(); |
| 776 | struct crtlist *crtlist; |
| 777 | struct stream_interface *si = appctx->owner; |
| 778 | struct crtlist_entry *entry; |
| 779 | |
| 780 | if (trash == NULL) |
| 781 | return 1; |
| 782 | |
| 783 | crtlist = ebmb_entry(appctx->ctx.cli.p0, struct crtlist, node); |
| 784 | |
| 785 | entry = appctx->ctx.cli.p1; |
| 786 | if (entry == NULL) { |
| 787 | entry = LIST_ELEM((crtlist->ord_entries).n, typeof(entry), by_crtlist); |
| 788 | chunk_appendf(trash, "# %s\n", crtlist->node.key); |
| 789 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 790 | si_rx_room_blk(si); |
| 791 | goto yield; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | list_for_each_entry_from(entry, &crtlist->ord_entries, by_crtlist) { |
| 796 | struct ckch_store *store; |
| 797 | const char *filename; |
| 798 | |
| 799 | store = entry->node.key; |
| 800 | filename = store->path; |
| 801 | chunk_appendf(trash, "%s", filename); |
| 802 | if (appctx->ctx.cli.i0 == 's') /* show */ |
| 803 | chunk_appendf(trash, ":%d", entry->linenum); |
| 804 | dump_crtlist_sslconf(trash, entry->ssl_conf); |
| 805 | dump_crtlist_filters(trash, entry); |
| 806 | chunk_appendf(trash, "\n"); |
| 807 | |
| 808 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 809 | si_rx_room_blk(si); |
| 810 | goto yield; |
| 811 | } |
| 812 | } |
| 813 | free_trash_chunk(trash); |
| 814 | return 1; |
| 815 | yield: |
| 816 | appctx->ctx.cli.p1 = entry; |
| 817 | free_trash_chunk(trash); |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | /* CLI argument parser for '(show|dump) ssl crt-list' */ |
| 822 | static int cli_parse_dump_crtlist(char **args, char *payload, struct appctx *appctx, void *private) |
| 823 | { |
| 824 | struct ebmb_node *lnode; |
| 825 | char *filename = NULL; |
| 826 | int mode; |
William Lallemand | 99cc218 | 2020-06-25 15:19:51 +0200 | [diff] [blame] | 827 | char *end; |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 828 | |
| 829 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 830 | return 1; |
| 831 | |
| 832 | appctx->ctx.cli.p0 = NULL; |
| 833 | appctx->ctx.cli.p1 = NULL; |
| 834 | |
| 835 | if (*args[3] && !strcmp(args[3], "-n")) { |
| 836 | mode = 's'; |
| 837 | filename = args[4]; |
| 838 | } else { |
| 839 | mode = 'd'; |
| 840 | filename = args[3]; |
| 841 | } |
| 842 | |
| 843 | if (mode == 's' && !*args[4]) |
| 844 | return cli_err(appctx, "'show ssl crt-list -n' expects a filename or a directory\n"); |
| 845 | |
| 846 | if (filename && *filename) { |
William Lallemand | 99cc218 | 2020-06-25 15:19:51 +0200 | [diff] [blame] | 847 | |
| 848 | |
| 849 | /* strip trailing slashes, including first one */ |
| 850 | for (end = filename + strlen(filename) - 1; end >= filename && *end == '/'; end--) |
| 851 | *end = 0; |
| 852 | |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 853 | lnode = ebst_lookup(&crtlists_tree, filename); |
| 854 | if (lnode == NULL) |
| 855 | return cli_err(appctx, "didn't find the specified filename\n"); |
| 856 | |
| 857 | appctx->ctx.cli.p0 = lnode; |
| 858 | appctx->io_handler = cli_io_handler_dump_crtlist_entries; |
| 859 | } |
| 860 | appctx->ctx.cli.i0 = mode; |
| 861 | |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | /* release function of the "add ssl crt-list' command, free things and unlock |
| 866 | the spinlock */ |
| 867 | static void cli_release_add_crtlist(struct appctx *appctx) |
| 868 | { |
| 869 | struct crtlist_entry *entry = appctx->ctx.cli.p1; |
| 870 | |
| 871 | if (appctx->st2 != SETCERT_ST_FIN) { |
| 872 | struct ckch_inst *inst, *inst_s; |
| 873 | /* upon error free the ckch_inst and everything inside */ |
| 874 | ebpt_delete(&entry->node); |
| 875 | LIST_DEL(&entry->by_crtlist); |
| 876 | LIST_DEL(&entry->by_ckch_store); |
| 877 | |
| 878 | list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_ckchs) { |
| 879 | ckch_inst_free(inst); |
| 880 | } |
| 881 | crtlist_free_filters(entry->filters); |
| 882 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 883 | free(entry->ssl_conf); |
| 884 | free(entry); |
| 885 | } |
| 886 | |
| 887 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 888 | } |
| 889 | |
| 890 | |
| 891 | /* IO Handler for the "add ssl crt-list" command It adds a new entry in the |
| 892 | * crt-list and generates the ckch_insts for each bind_conf that uses this crt-list |
| 893 | * |
| 894 | * The logic is the same as the "commit ssl cert" command but without the |
| 895 | * freeing of the old structures, because there are none. |
| 896 | */ |
| 897 | static int cli_io_handler_add_crtlist(struct appctx *appctx) |
| 898 | { |
| 899 | struct bind_conf_list *bind_conf_node; |
| 900 | struct stream_interface *si = appctx->owner; |
| 901 | struct crtlist *crtlist = appctx->ctx.cli.p0; |
| 902 | struct crtlist_entry *entry = appctx->ctx.cli.p1; |
| 903 | struct ckch_store *store = entry->node.key; |
| 904 | struct buffer *trash = alloc_trash_chunk(); |
| 905 | struct ckch_inst *new_inst; |
| 906 | char *err = NULL; |
| 907 | int i = 0; |
| 908 | int errcode = 0; |
| 909 | |
| 910 | if (trash == NULL) |
| 911 | goto error; |
| 912 | |
| 913 | /* for each bind_conf which use the crt-list, a new ckch_inst must be |
| 914 | * created. |
| 915 | */ |
| 916 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 917 | goto error; |
| 918 | |
| 919 | while (1) { |
| 920 | switch (appctx->st2) { |
| 921 | case SETCERT_ST_INIT: |
| 922 | /* This state just print the update message */ |
| 923 | chunk_printf(trash, "Inserting certificate '%s' in crt-list '%s'", store->path, crtlist->node.key); |
| 924 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 925 | si_rx_room_blk(si); |
| 926 | goto yield; |
| 927 | } |
| 928 | appctx->st2 = SETCERT_ST_GEN; |
| 929 | /* fallthrough */ |
| 930 | case SETCERT_ST_GEN: |
| 931 | bind_conf_node = appctx->ctx.cli.p2; /* get the previous ptr from the yield */ |
| 932 | if (bind_conf_node == NULL) |
| 933 | bind_conf_node = crtlist->bind_conf; |
| 934 | for (; bind_conf_node; bind_conf_node = bind_conf_node->next) { |
| 935 | struct bind_conf *bind_conf = bind_conf_node->bind_conf; |
| 936 | struct sni_ctx *sni; |
| 937 | |
| 938 | /* yield every 10 generations */ |
| 939 | if (i > 10) { |
| 940 | appctx->ctx.cli.p2 = bind_conf_node; |
| 941 | goto yield; |
| 942 | } |
| 943 | |
| 944 | /* we don't support multi-cert bundles, only simple ones */ |
| 945 | errcode |= ckch_inst_new_load_store(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &new_inst, &err); |
| 946 | if (errcode & ERR_CODE) |
| 947 | goto error; |
| 948 | |
| 949 | /* we need to initialize the SSL_CTX generated */ |
| 950 | /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */ |
| 951 | list_for_each_entry(sni, &new_inst->sni_ctx, by_ckch_inst) { |
| 952 | if (!sni->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */ |
| 953 | errcode |= ssl_sock_prepare_ctx(bind_conf, new_inst->ssl_conf, sni->ctx, &err); |
| 954 | if (errcode & ERR_CODE) |
| 955 | goto error; |
| 956 | } |
| 957 | } |
| 958 | /* display one dot for each new instance */ |
| 959 | chunk_appendf(trash, "."); |
| 960 | i++; |
| 961 | LIST_ADDQ(&store->ckch_inst, &new_inst->by_ckchs); |
William Lallemand | 9ab8f8d | 2020-06-24 01:00:52 +0200 | [diff] [blame] | 962 | LIST_ADDQ(&entry->ckch_inst, &new_inst->by_crtlist_entry); |
| 963 | new_inst->crtlist_entry = entry; |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 964 | } |
| 965 | appctx->st2 = SETCERT_ST_INSERT; |
| 966 | /* fallthrough */ |
| 967 | case SETCERT_ST_INSERT: |
| 968 | /* insert SNIs in bind_conf */ |
| 969 | list_for_each_entry(new_inst, &store->ckch_inst, by_ckchs) { |
| 970 | HA_RWLOCK_WRLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock); |
| 971 | ssl_sock_load_cert_sni(new_inst, new_inst->bind_conf); |
| 972 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock); |
| 973 | } |
| 974 | entry->linenum = ++crtlist->linecount; |
| 975 | appctx->st2 = SETCERT_ST_FIN; |
| 976 | goto end; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | end: |
| 981 | chunk_appendf(trash, "\n"); |
| 982 | if (errcode & ERR_WARN) |
| 983 | chunk_appendf(trash, "%s", err); |
| 984 | chunk_appendf(trash, "Success!\n"); |
| 985 | if (ci_putchk(si_ic(si), trash) == -1) |
| 986 | si_rx_room_blk(si); |
| 987 | free_trash_chunk(trash); |
| 988 | /* success: call the release function and don't come back */ |
| 989 | return 1; |
| 990 | yield: |
| 991 | /* store the state */ |
| 992 | if (ci_putchk(si_ic(si), trash) == -1) |
| 993 | si_rx_room_blk(si); |
| 994 | free_trash_chunk(trash); |
| 995 | si_rx_endp_more(si); /* let's come back later */ |
| 996 | return 0; /* should come back */ |
| 997 | |
| 998 | error: |
| 999 | /* spin unlock and free are done in the release function */ |
| 1000 | if (trash) { |
| 1001 | chunk_appendf(trash, "\n%sFailed!\n", err); |
| 1002 | if (ci_putchk(si_ic(si), trash) == -1) |
| 1003 | si_rx_room_blk(si); |
| 1004 | free_trash_chunk(trash); |
| 1005 | } |
| 1006 | /* error: call the release function and don't come back */ |
| 1007 | return 1; |
| 1008 | } |
| 1009 | |
| 1010 | |
| 1011 | /* |
| 1012 | * Parse a "add ssl crt-list <crt-list> <certfile>" line. |
| 1013 | * Filters and option must be passed through payload: |
| 1014 | */ |
| 1015 | static int cli_parse_add_crtlist(char **args, char *payload, struct appctx *appctx, void *private) |
| 1016 | { |
| 1017 | int cfgerr = 0; |
| 1018 | struct ckch_store *store; |
| 1019 | char *err = NULL; |
| 1020 | char path[MAXPATHLEN+1]; |
| 1021 | char *crtlist_path; |
| 1022 | char *cert_path = NULL; |
| 1023 | struct ebmb_node *eb; |
| 1024 | struct ebpt_node *inserted; |
| 1025 | struct crtlist *crtlist; |
| 1026 | struct crtlist_entry *entry = NULL; |
William Lallemand | 99cc218 | 2020-06-25 15:19:51 +0200 | [diff] [blame] | 1027 | char *end; |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 1028 | |
| 1029 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1030 | return 1; |
| 1031 | |
| 1032 | if (!*args[3] || (!payload && !*args[4])) |
| 1033 | return cli_err(appctx, "'add ssl crtlist' expects a filename and a certificate name\n"); |
| 1034 | |
| 1035 | crtlist_path = args[3]; |
| 1036 | |
William Lallemand | 99cc218 | 2020-06-25 15:19:51 +0200 | [diff] [blame] | 1037 | /* strip trailing slashes, including first one */ |
| 1038 | for (end = crtlist_path + strlen(crtlist_path) - 1; end >= crtlist_path && *end == '/'; end--) |
| 1039 | *end = 0; |
| 1040 | |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 1041 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 1042 | return cli_err(appctx, "Operations on certificates are currently locked!\n"); |
| 1043 | |
| 1044 | eb = ebst_lookup(&crtlists_tree, crtlist_path); |
| 1045 | if (!eb) { |
| 1046 | memprintf(&err, "crt-list '%s' does not exist!", crtlist_path); |
| 1047 | goto error; |
| 1048 | } |
| 1049 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 1050 | |
| 1051 | entry = crtlist_entry_new(); |
| 1052 | if (entry == NULL) { |
| 1053 | memprintf(&err, "Not enough memory!"); |
| 1054 | goto error; |
| 1055 | } |
| 1056 | |
| 1057 | if (payload) { |
| 1058 | char *lf; |
| 1059 | |
| 1060 | lf = strrchr(payload, '\n'); |
| 1061 | if (lf) { |
| 1062 | memprintf(&err, "only one line of payload is supported!"); |
| 1063 | goto error; |
| 1064 | } |
| 1065 | /* cert_path is filled here */ |
| 1066 | cfgerr |= crtlist_parse_line(payload, &cert_path, entry, "CLI", 1, &err); |
| 1067 | if (cfgerr & ERR_CODE) |
| 1068 | goto error; |
| 1069 | } else { |
| 1070 | cert_path = args[4]; |
| 1071 | } |
| 1072 | |
| 1073 | if (!cert_path) { |
| 1074 | memprintf(&err, "'add ssl crtlist' should contain the certificate name in the payload"); |
| 1075 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 1076 | goto error; |
| 1077 | } |
| 1078 | |
| 1079 | if (eb_gettag(crtlist->entries.b[EB_RGHT])) { |
| 1080 | char *slash; |
| 1081 | |
| 1082 | slash = strrchr(cert_path, '/'); |
| 1083 | if (!slash) { |
| 1084 | memprintf(&err, "'%s' is a directory, certificate path '%s' must contain the directory path", (char *)crtlist->node.key, cert_path); |
| 1085 | goto error; |
| 1086 | } |
| 1087 | /* temporary replace / by 0 to do an strcmp */ |
| 1088 | *slash = '\0'; |
| 1089 | if (strcmp(cert_path, (char*)crtlist->node.key) != 0) { |
| 1090 | *slash = '/'; |
| 1091 | memprintf(&err, "'%s' is a directory, certificate path '%s' must contain the directory path", (char *)crtlist->node.key, cert_path); |
| 1092 | goto error; |
| 1093 | } |
| 1094 | *slash = '/'; |
| 1095 | } |
| 1096 | |
| 1097 | if (*cert_path != '/' && global_ssl.crt_base) { |
| 1098 | if ((strlen(global_ssl.crt_base) + 1 + strlen(cert_path)) > MAXPATHLEN) { |
| 1099 | memprintf(&err, "'%s' : path too long", cert_path); |
| 1100 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 1101 | goto error; |
| 1102 | } |
| 1103 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, cert_path); |
| 1104 | cert_path = path; |
| 1105 | } |
| 1106 | |
| 1107 | store = ckchs_lookup(cert_path); |
| 1108 | if (store == NULL) { |
| 1109 | memprintf(&err, "certificate '%s' does not exist!", cert_path); |
| 1110 | goto error; |
| 1111 | } |
| 1112 | if (store->multi) { |
| 1113 | memprintf(&err, "certificate '%s' is a bundle. You can disable the bundle merging with the directive 'ssl-load-extra-files' in the global section.", cert_path); |
| 1114 | goto error; |
| 1115 | } |
| 1116 | if (store->ckch == NULL || store->ckch->cert == NULL) { |
| 1117 | memprintf(&err, "certificate '%s' is empty!", cert_path); |
| 1118 | goto error; |
| 1119 | } |
| 1120 | |
| 1121 | /* check if it's possible to insert this new crtlist_entry */ |
| 1122 | entry->node.key = store; |
| 1123 | inserted = ebpt_insert(&crtlist->entries, &entry->node); |
| 1124 | if (inserted != &entry->node) { |
| 1125 | memprintf(&err, "file already exists in this directory!"); |
| 1126 | goto error; |
| 1127 | } |
| 1128 | |
| 1129 | /* this is supposed to be a directory (EB_ROOT_UNIQUE), so no ssl_conf are allowed */ |
| 1130 | if ((entry->ssl_conf || entry->filters) && eb_gettag(crtlist->entries.b[EB_RGHT])) { |
| 1131 | memprintf(&err, "this is a directory, SSL configuration and filters are not allowed"); |
| 1132 | goto error; |
| 1133 | } |
| 1134 | |
| 1135 | LIST_ADDQ(&crtlist->ord_entries, &entry->by_crtlist); |
| 1136 | entry->crtlist = crtlist; |
| 1137 | LIST_ADDQ(&store->crtlist_entry, &entry->by_ckch_store); |
| 1138 | |
| 1139 | appctx->st2 = SETCERT_ST_INIT; |
| 1140 | appctx->ctx.cli.p0 = crtlist; |
| 1141 | appctx->ctx.cli.p1 = entry; |
| 1142 | |
| 1143 | /* unlock is done in the release handler */ |
| 1144 | return 0; |
| 1145 | |
| 1146 | error: |
| 1147 | crtlist_entry_free(entry); |
| 1148 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1149 | err = memprintf(&err, "Can't edit the crt-list: %s\n", err ? err : ""); |
| 1150 | return cli_dynerr(appctx, err); |
| 1151 | } |
| 1152 | |
| 1153 | /* Parse a "del ssl crt-list <crt-list> <certfile>" line. */ |
| 1154 | static int cli_parse_del_crtlist(char **args, char *payload, struct appctx *appctx, void *private) |
| 1155 | { |
| 1156 | struct ckch_store *store; |
| 1157 | char *err = NULL; |
| 1158 | char *crtlist_path, *cert_path; |
| 1159 | struct ebmb_node *ebmb; |
| 1160 | struct ebpt_node *ebpt; |
| 1161 | struct crtlist *crtlist; |
| 1162 | struct crtlist_entry *entry = NULL; |
| 1163 | struct ckch_inst *inst, *inst_s; |
| 1164 | int linenum = 0; |
| 1165 | char *colons; |
William Lallemand | 99cc218 | 2020-06-25 15:19:51 +0200 | [diff] [blame] | 1166 | char *end; |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 1167 | |
| 1168 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1169 | return 1; |
| 1170 | |
| 1171 | if (!*args[3] || !*args[4]) |
| 1172 | return cli_err(appctx, "'del ssl crtlist' expects a filename and a certificate name\n"); |
| 1173 | |
| 1174 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 1175 | return cli_err(appctx, "Can't delete!\nOperations on certificates are currently locked!\n"); |
| 1176 | |
| 1177 | crtlist_path = args[3]; |
| 1178 | cert_path = args[4]; |
| 1179 | |
| 1180 | colons = strchr(cert_path, ':'); |
| 1181 | if (colons) { |
| 1182 | char *endptr; |
| 1183 | |
| 1184 | linenum = strtol(colons + 1, &endptr, 10); |
| 1185 | if (colons + 1 == endptr || *endptr != '\0') { |
| 1186 | memprintf(&err, "wrong line number after colons in '%s'!", cert_path); |
| 1187 | goto error; |
| 1188 | } |
| 1189 | *colons = '\0'; |
| 1190 | } |
William Lallemand | 99cc218 | 2020-06-25 15:19:51 +0200 | [diff] [blame] | 1191 | |
| 1192 | /* strip trailing slashes, including first one */ |
| 1193 | for (end = crtlist_path + strlen(crtlist_path) - 1; end >= crtlist_path && *end == '/'; end--) |
| 1194 | *end = 0; |
| 1195 | |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 1196 | /* look for crtlist */ |
| 1197 | ebmb = ebst_lookup(&crtlists_tree, crtlist_path); |
| 1198 | if (!ebmb) { |
| 1199 | memprintf(&err, "crt-list '%s' does not exist!", crtlist_path); |
| 1200 | goto error; |
| 1201 | } |
| 1202 | crtlist = ebmb_entry(ebmb, struct crtlist, node); |
| 1203 | |
| 1204 | /* look for store */ |
| 1205 | store = ckchs_lookup(cert_path); |
| 1206 | if (store == NULL) { |
| 1207 | memprintf(&err, "certificate '%s' does not exist!", cert_path); |
| 1208 | goto error; |
| 1209 | } |
| 1210 | if (store->multi) { |
| 1211 | memprintf(&err, "certificate '%s' is a bundle. You can disable the bundle merging with the directive 'ssl-load-extra-files' in the global section.", cert_path); |
| 1212 | goto error; |
| 1213 | } |
| 1214 | if (store->ckch == NULL || store->ckch->cert == NULL) { |
| 1215 | memprintf(&err, "certificate '%s' is empty!", cert_path); |
| 1216 | goto error; |
| 1217 | } |
| 1218 | |
| 1219 | ebpt = ebpt_lookup(&crtlist->entries, store); |
| 1220 | if (!ebpt) { |
| 1221 | memprintf(&err, "certificate '%s' can't be found in crt-list '%s'!", cert_path, crtlist_path); |
| 1222 | goto error; |
| 1223 | } |
| 1224 | |
| 1225 | /* list the line number of entries for errors in err, and select the right ebpt */ |
| 1226 | for (; ebpt; ebpt = ebpt_next_dup(ebpt)) { |
| 1227 | struct crtlist_entry *tmp; |
| 1228 | |
| 1229 | tmp = ebpt_entry(ebpt, struct crtlist_entry, node); |
| 1230 | memprintf(&err, "%s%s%d", err ? err : "", err ? ", " : "", tmp->linenum); |
| 1231 | |
| 1232 | /* select the entry we wanted */ |
| 1233 | if (linenum == 0 || tmp->linenum == linenum) { |
| 1234 | if (!entry) |
| 1235 | entry = tmp; |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | /* we didn't found the specified entry */ |
| 1240 | if (!entry) { |
| 1241 | memprintf(&err, "found a certificate '%s' but the line number is incorrect, please specify a correct line number preceded by colons (%s)!", cert_path, err ? err : NULL); |
| 1242 | goto error; |
| 1243 | } |
| 1244 | |
| 1245 | /* we didn't specified a line number but there were several entries */ |
| 1246 | if (linenum == 0 && ebpt_next_dup(&entry->node)) { |
| 1247 | memprintf(&err, "found the certificate '%s' in several entries, please specify a line number preceded by colons (%s)!", cert_path, err ? err : NULL); |
| 1248 | goto error; |
| 1249 | } |
| 1250 | |
| 1251 | /* upon error free the ckch_inst and everything inside */ |
| 1252 | |
| 1253 | ebpt_delete(&entry->node); |
| 1254 | LIST_DEL(&entry->by_crtlist); |
| 1255 | LIST_DEL(&entry->by_ckch_store); |
| 1256 | |
| 1257 | list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) { |
| 1258 | struct sni_ctx *sni, *sni_s; |
| 1259 | |
| 1260 | HA_RWLOCK_WRLOCK(SNI_LOCK, &inst->bind_conf->sni_lock); |
| 1261 | list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) { |
| 1262 | ebmb_delete(&sni->name); |
| 1263 | LIST_DEL(&sni->by_ckch_inst); |
| 1264 | SSL_CTX_free(sni->ctx); |
| 1265 | free(sni); |
| 1266 | } |
| 1267 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &inst->bind_conf->sni_lock); |
| 1268 | LIST_DEL(&inst->by_ckchs); |
| 1269 | free(inst); |
| 1270 | } |
| 1271 | |
| 1272 | crtlist_free_filters(entry->filters); |
| 1273 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 1274 | free(entry->ssl_conf); |
| 1275 | free(entry); |
| 1276 | |
| 1277 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1278 | err = memprintf(&err, "Entry '%s' deleted in crtlist '%s'!\n", cert_path, crtlist_path); |
| 1279 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 1280 | |
| 1281 | error: |
| 1282 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1283 | err = memprintf(&err, "Can't delete the entry: %s\n", err ? err : ""); |
| 1284 | return cli_dynerr(appctx, err); |
| 1285 | } |
| 1286 | |
| 1287 | |
William Lallemand | ee8530c | 2020-06-23 18:19:42 +0200 | [diff] [blame] | 1288 | /* unlink and free all crt-list and crt-list entries */ |
| 1289 | void crtlist_deinit() |
| 1290 | { |
| 1291 | struct eb_node *node, *next; |
| 1292 | struct crtlist *crtlist; |
| 1293 | |
| 1294 | node = eb_first(&crtlists_tree); |
| 1295 | while (node) { |
| 1296 | next = eb_next(node); |
| 1297 | crtlist = ebmb_entry(node, struct crtlist, node); |
| 1298 | crtlist_free(crtlist); |
| 1299 | node = next; |
| 1300 | } |
| 1301 | } |
| 1302 | |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 1303 | |
| 1304 | /* register cli keywords */ |
| 1305 | static struct cli_kw_list cli_kws = {{ },{ |
| 1306 | { { "add", "ssl", "crt-list", NULL }, "add ssl crt-list <filename> <certfile> [options] : add a line <certfile> to a crt-list <filename>", cli_parse_add_crtlist, cli_io_handler_add_crtlist, cli_release_add_crtlist }, |
| 1307 | { { "del", "ssl", "crt-list", NULL }, "del ssl crt-list <filename> <certfile[:line]> : delete a line <certfile> in a crt-list <filename>", cli_parse_del_crtlist, NULL, NULL }, |
| 1308 | { { "show", "ssl", "crt-list", NULL }, "show ssl crt-list [-n] [<filename>] : show the list of crt-lists or the content of a crt-list <filename>", cli_parse_dump_crtlist, cli_io_handler_dump_crtlist, NULL }, |
| 1309 | { { NULL }, NULL, NULL, NULL } } |
| 1310 | }; |
| 1311 | |
| 1312 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 1313 | |