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; |
| 827 | |
| 828 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 829 | return 1; |
| 830 | |
| 831 | appctx->ctx.cli.p0 = NULL; |
| 832 | appctx->ctx.cli.p1 = NULL; |
| 833 | |
| 834 | if (*args[3] && !strcmp(args[3], "-n")) { |
| 835 | mode = 's'; |
| 836 | filename = args[4]; |
| 837 | } else { |
| 838 | mode = 'd'; |
| 839 | filename = args[3]; |
| 840 | } |
| 841 | |
| 842 | if (mode == 's' && !*args[4]) |
| 843 | return cli_err(appctx, "'show ssl crt-list -n' expects a filename or a directory\n"); |
| 844 | |
| 845 | if (filename && *filename) { |
| 846 | lnode = ebst_lookup(&crtlists_tree, filename); |
| 847 | if (lnode == NULL) |
| 848 | return cli_err(appctx, "didn't find the specified filename\n"); |
| 849 | |
| 850 | appctx->ctx.cli.p0 = lnode; |
| 851 | appctx->io_handler = cli_io_handler_dump_crtlist_entries; |
| 852 | } |
| 853 | appctx->ctx.cli.i0 = mode; |
| 854 | |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | /* release function of the "add ssl crt-list' command, free things and unlock |
| 859 | the spinlock */ |
| 860 | static void cli_release_add_crtlist(struct appctx *appctx) |
| 861 | { |
| 862 | struct crtlist_entry *entry = appctx->ctx.cli.p1; |
| 863 | |
| 864 | if (appctx->st2 != SETCERT_ST_FIN) { |
| 865 | struct ckch_inst *inst, *inst_s; |
| 866 | /* upon error free the ckch_inst and everything inside */ |
| 867 | ebpt_delete(&entry->node); |
| 868 | LIST_DEL(&entry->by_crtlist); |
| 869 | LIST_DEL(&entry->by_ckch_store); |
| 870 | |
| 871 | list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_ckchs) { |
| 872 | ckch_inst_free(inst); |
| 873 | } |
| 874 | crtlist_free_filters(entry->filters); |
| 875 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 876 | free(entry->ssl_conf); |
| 877 | free(entry); |
| 878 | } |
| 879 | |
| 880 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 881 | } |
| 882 | |
| 883 | |
| 884 | /* IO Handler for the "add ssl crt-list" command It adds a new entry in the |
| 885 | * crt-list and generates the ckch_insts for each bind_conf that uses this crt-list |
| 886 | * |
| 887 | * The logic is the same as the "commit ssl cert" command but without the |
| 888 | * freeing of the old structures, because there are none. |
| 889 | */ |
| 890 | static int cli_io_handler_add_crtlist(struct appctx *appctx) |
| 891 | { |
| 892 | struct bind_conf_list *bind_conf_node; |
| 893 | struct stream_interface *si = appctx->owner; |
| 894 | struct crtlist *crtlist = appctx->ctx.cli.p0; |
| 895 | struct crtlist_entry *entry = appctx->ctx.cli.p1; |
| 896 | struct ckch_store *store = entry->node.key; |
| 897 | struct buffer *trash = alloc_trash_chunk(); |
| 898 | struct ckch_inst *new_inst; |
| 899 | char *err = NULL; |
| 900 | int i = 0; |
| 901 | int errcode = 0; |
| 902 | |
| 903 | if (trash == NULL) |
| 904 | goto error; |
| 905 | |
| 906 | /* for each bind_conf which use the crt-list, a new ckch_inst must be |
| 907 | * created. |
| 908 | */ |
| 909 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 910 | goto error; |
| 911 | |
| 912 | while (1) { |
| 913 | switch (appctx->st2) { |
| 914 | case SETCERT_ST_INIT: |
| 915 | /* This state just print the update message */ |
| 916 | chunk_printf(trash, "Inserting certificate '%s' in crt-list '%s'", store->path, crtlist->node.key); |
| 917 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 918 | si_rx_room_blk(si); |
| 919 | goto yield; |
| 920 | } |
| 921 | appctx->st2 = SETCERT_ST_GEN; |
| 922 | /* fallthrough */ |
| 923 | case SETCERT_ST_GEN: |
| 924 | bind_conf_node = appctx->ctx.cli.p2; /* get the previous ptr from the yield */ |
| 925 | if (bind_conf_node == NULL) |
| 926 | bind_conf_node = crtlist->bind_conf; |
| 927 | for (; bind_conf_node; bind_conf_node = bind_conf_node->next) { |
| 928 | struct bind_conf *bind_conf = bind_conf_node->bind_conf; |
| 929 | struct sni_ctx *sni; |
| 930 | |
| 931 | /* yield every 10 generations */ |
| 932 | if (i > 10) { |
| 933 | appctx->ctx.cli.p2 = bind_conf_node; |
| 934 | goto yield; |
| 935 | } |
| 936 | |
| 937 | /* we don't support multi-cert bundles, only simple ones */ |
| 938 | errcode |= ckch_inst_new_load_store(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &new_inst, &err); |
| 939 | if (errcode & ERR_CODE) |
| 940 | goto error; |
| 941 | |
| 942 | /* we need to initialize the SSL_CTX generated */ |
| 943 | /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */ |
| 944 | list_for_each_entry(sni, &new_inst->sni_ctx, by_ckch_inst) { |
| 945 | if (!sni->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */ |
| 946 | errcode |= ssl_sock_prepare_ctx(bind_conf, new_inst->ssl_conf, sni->ctx, &err); |
| 947 | if (errcode & ERR_CODE) |
| 948 | goto error; |
| 949 | } |
| 950 | } |
| 951 | /* display one dot for each new instance */ |
| 952 | chunk_appendf(trash, "."); |
| 953 | i++; |
| 954 | LIST_ADDQ(&store->ckch_inst, &new_inst->by_ckchs); |
William Lallemand | 9ab8f8d | 2020-06-24 01:00:52 +0200 | [diff] [blame] | 955 | LIST_ADDQ(&entry->ckch_inst, &new_inst->by_crtlist_entry); |
| 956 | new_inst->crtlist_entry = entry; |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 957 | } |
| 958 | appctx->st2 = SETCERT_ST_INSERT; |
| 959 | /* fallthrough */ |
| 960 | case SETCERT_ST_INSERT: |
| 961 | /* insert SNIs in bind_conf */ |
| 962 | list_for_each_entry(new_inst, &store->ckch_inst, by_ckchs) { |
| 963 | HA_RWLOCK_WRLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock); |
| 964 | ssl_sock_load_cert_sni(new_inst, new_inst->bind_conf); |
| 965 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock); |
| 966 | } |
| 967 | entry->linenum = ++crtlist->linecount; |
| 968 | appctx->st2 = SETCERT_ST_FIN; |
| 969 | goto end; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | end: |
| 974 | chunk_appendf(trash, "\n"); |
| 975 | if (errcode & ERR_WARN) |
| 976 | chunk_appendf(trash, "%s", err); |
| 977 | chunk_appendf(trash, "Success!\n"); |
| 978 | if (ci_putchk(si_ic(si), trash) == -1) |
| 979 | si_rx_room_blk(si); |
| 980 | free_trash_chunk(trash); |
| 981 | /* success: call the release function and don't come back */ |
| 982 | return 1; |
| 983 | yield: |
| 984 | /* store the state */ |
| 985 | if (ci_putchk(si_ic(si), trash) == -1) |
| 986 | si_rx_room_blk(si); |
| 987 | free_trash_chunk(trash); |
| 988 | si_rx_endp_more(si); /* let's come back later */ |
| 989 | return 0; /* should come back */ |
| 990 | |
| 991 | error: |
| 992 | /* spin unlock and free are done in the release function */ |
| 993 | if (trash) { |
| 994 | chunk_appendf(trash, "\n%sFailed!\n", err); |
| 995 | if (ci_putchk(si_ic(si), trash) == -1) |
| 996 | si_rx_room_blk(si); |
| 997 | free_trash_chunk(trash); |
| 998 | } |
| 999 | /* error: call the release function and don't come back */ |
| 1000 | return 1; |
| 1001 | } |
| 1002 | |
| 1003 | |
| 1004 | /* |
| 1005 | * Parse a "add ssl crt-list <crt-list> <certfile>" line. |
| 1006 | * Filters and option must be passed through payload: |
| 1007 | */ |
| 1008 | static int cli_parse_add_crtlist(char **args, char *payload, struct appctx *appctx, void *private) |
| 1009 | { |
| 1010 | int cfgerr = 0; |
| 1011 | struct ckch_store *store; |
| 1012 | char *err = NULL; |
| 1013 | char path[MAXPATHLEN+1]; |
| 1014 | char *crtlist_path; |
| 1015 | char *cert_path = NULL; |
| 1016 | struct ebmb_node *eb; |
| 1017 | struct ebpt_node *inserted; |
| 1018 | struct crtlist *crtlist; |
| 1019 | struct crtlist_entry *entry = NULL; |
| 1020 | |
| 1021 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1022 | return 1; |
| 1023 | |
| 1024 | if (!*args[3] || (!payload && !*args[4])) |
| 1025 | return cli_err(appctx, "'add ssl crtlist' expects a filename and a certificate name\n"); |
| 1026 | |
| 1027 | crtlist_path = args[3]; |
| 1028 | |
| 1029 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 1030 | return cli_err(appctx, "Operations on certificates are currently locked!\n"); |
| 1031 | |
| 1032 | eb = ebst_lookup(&crtlists_tree, crtlist_path); |
| 1033 | if (!eb) { |
| 1034 | memprintf(&err, "crt-list '%s' does not exist!", crtlist_path); |
| 1035 | goto error; |
| 1036 | } |
| 1037 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 1038 | |
| 1039 | entry = crtlist_entry_new(); |
| 1040 | if (entry == NULL) { |
| 1041 | memprintf(&err, "Not enough memory!"); |
| 1042 | goto error; |
| 1043 | } |
| 1044 | |
| 1045 | if (payload) { |
| 1046 | char *lf; |
| 1047 | |
| 1048 | lf = strrchr(payload, '\n'); |
| 1049 | if (lf) { |
| 1050 | memprintf(&err, "only one line of payload is supported!"); |
| 1051 | goto error; |
| 1052 | } |
| 1053 | /* cert_path is filled here */ |
| 1054 | cfgerr |= crtlist_parse_line(payload, &cert_path, entry, "CLI", 1, &err); |
| 1055 | if (cfgerr & ERR_CODE) |
| 1056 | goto error; |
| 1057 | } else { |
| 1058 | cert_path = args[4]; |
| 1059 | } |
| 1060 | |
| 1061 | if (!cert_path) { |
| 1062 | memprintf(&err, "'add ssl crtlist' should contain the certificate name in the payload"); |
| 1063 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 1064 | goto error; |
| 1065 | } |
| 1066 | |
| 1067 | if (eb_gettag(crtlist->entries.b[EB_RGHT])) { |
| 1068 | char *slash; |
| 1069 | |
| 1070 | slash = strrchr(cert_path, '/'); |
| 1071 | if (!slash) { |
| 1072 | memprintf(&err, "'%s' is a directory, certificate path '%s' must contain the directory path", (char *)crtlist->node.key, cert_path); |
| 1073 | goto error; |
| 1074 | } |
| 1075 | /* temporary replace / by 0 to do an strcmp */ |
| 1076 | *slash = '\0'; |
| 1077 | if (strcmp(cert_path, (char*)crtlist->node.key) != 0) { |
| 1078 | *slash = '/'; |
| 1079 | memprintf(&err, "'%s' is a directory, certificate path '%s' must contain the directory path", (char *)crtlist->node.key, cert_path); |
| 1080 | goto error; |
| 1081 | } |
| 1082 | *slash = '/'; |
| 1083 | } |
| 1084 | |
| 1085 | if (*cert_path != '/' && global_ssl.crt_base) { |
| 1086 | if ((strlen(global_ssl.crt_base) + 1 + strlen(cert_path)) > MAXPATHLEN) { |
| 1087 | memprintf(&err, "'%s' : path too long", cert_path); |
| 1088 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 1089 | goto error; |
| 1090 | } |
| 1091 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, cert_path); |
| 1092 | cert_path = path; |
| 1093 | } |
| 1094 | |
| 1095 | store = ckchs_lookup(cert_path); |
| 1096 | if (store == NULL) { |
| 1097 | memprintf(&err, "certificate '%s' does not exist!", cert_path); |
| 1098 | goto error; |
| 1099 | } |
| 1100 | if (store->multi) { |
| 1101 | 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); |
| 1102 | goto error; |
| 1103 | } |
| 1104 | if (store->ckch == NULL || store->ckch->cert == NULL) { |
| 1105 | memprintf(&err, "certificate '%s' is empty!", cert_path); |
| 1106 | goto error; |
| 1107 | } |
| 1108 | |
| 1109 | /* check if it's possible to insert this new crtlist_entry */ |
| 1110 | entry->node.key = store; |
| 1111 | inserted = ebpt_insert(&crtlist->entries, &entry->node); |
| 1112 | if (inserted != &entry->node) { |
| 1113 | memprintf(&err, "file already exists in this directory!"); |
| 1114 | goto error; |
| 1115 | } |
| 1116 | |
| 1117 | /* this is supposed to be a directory (EB_ROOT_UNIQUE), so no ssl_conf are allowed */ |
| 1118 | if ((entry->ssl_conf || entry->filters) && eb_gettag(crtlist->entries.b[EB_RGHT])) { |
| 1119 | memprintf(&err, "this is a directory, SSL configuration and filters are not allowed"); |
| 1120 | goto error; |
| 1121 | } |
| 1122 | |
| 1123 | LIST_ADDQ(&crtlist->ord_entries, &entry->by_crtlist); |
| 1124 | entry->crtlist = crtlist; |
| 1125 | LIST_ADDQ(&store->crtlist_entry, &entry->by_ckch_store); |
| 1126 | |
| 1127 | appctx->st2 = SETCERT_ST_INIT; |
| 1128 | appctx->ctx.cli.p0 = crtlist; |
| 1129 | appctx->ctx.cli.p1 = entry; |
| 1130 | |
| 1131 | /* unlock is done in the release handler */ |
| 1132 | return 0; |
| 1133 | |
| 1134 | error: |
| 1135 | crtlist_entry_free(entry); |
| 1136 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1137 | err = memprintf(&err, "Can't edit the crt-list: %s\n", err ? err : ""); |
| 1138 | return cli_dynerr(appctx, err); |
| 1139 | } |
| 1140 | |
| 1141 | /* Parse a "del ssl crt-list <crt-list> <certfile>" line. */ |
| 1142 | static int cli_parse_del_crtlist(char **args, char *payload, struct appctx *appctx, void *private) |
| 1143 | { |
| 1144 | struct ckch_store *store; |
| 1145 | char *err = NULL; |
| 1146 | char *crtlist_path, *cert_path; |
| 1147 | struct ebmb_node *ebmb; |
| 1148 | struct ebpt_node *ebpt; |
| 1149 | struct crtlist *crtlist; |
| 1150 | struct crtlist_entry *entry = NULL; |
| 1151 | struct ckch_inst *inst, *inst_s; |
| 1152 | int linenum = 0; |
| 1153 | char *colons; |
| 1154 | |
| 1155 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1156 | return 1; |
| 1157 | |
| 1158 | if (!*args[3] || !*args[4]) |
| 1159 | return cli_err(appctx, "'del ssl crtlist' expects a filename and a certificate name\n"); |
| 1160 | |
| 1161 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 1162 | return cli_err(appctx, "Can't delete!\nOperations on certificates are currently locked!\n"); |
| 1163 | |
| 1164 | crtlist_path = args[3]; |
| 1165 | cert_path = args[4]; |
| 1166 | |
| 1167 | colons = strchr(cert_path, ':'); |
| 1168 | if (colons) { |
| 1169 | char *endptr; |
| 1170 | |
| 1171 | linenum = strtol(colons + 1, &endptr, 10); |
| 1172 | if (colons + 1 == endptr || *endptr != '\0') { |
| 1173 | memprintf(&err, "wrong line number after colons in '%s'!", cert_path); |
| 1174 | goto error; |
| 1175 | } |
| 1176 | *colons = '\0'; |
| 1177 | } |
| 1178 | /* look for crtlist */ |
| 1179 | ebmb = ebst_lookup(&crtlists_tree, crtlist_path); |
| 1180 | if (!ebmb) { |
| 1181 | memprintf(&err, "crt-list '%s' does not exist!", crtlist_path); |
| 1182 | goto error; |
| 1183 | } |
| 1184 | crtlist = ebmb_entry(ebmb, struct crtlist, node); |
| 1185 | |
| 1186 | /* look for store */ |
| 1187 | store = ckchs_lookup(cert_path); |
| 1188 | if (store == NULL) { |
| 1189 | memprintf(&err, "certificate '%s' does not exist!", cert_path); |
| 1190 | goto error; |
| 1191 | } |
| 1192 | if (store->multi) { |
| 1193 | 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); |
| 1194 | goto error; |
| 1195 | } |
| 1196 | if (store->ckch == NULL || store->ckch->cert == NULL) { |
| 1197 | memprintf(&err, "certificate '%s' is empty!", cert_path); |
| 1198 | goto error; |
| 1199 | } |
| 1200 | |
| 1201 | ebpt = ebpt_lookup(&crtlist->entries, store); |
| 1202 | if (!ebpt) { |
| 1203 | memprintf(&err, "certificate '%s' can't be found in crt-list '%s'!", cert_path, crtlist_path); |
| 1204 | goto error; |
| 1205 | } |
| 1206 | |
| 1207 | /* list the line number of entries for errors in err, and select the right ebpt */ |
| 1208 | for (; ebpt; ebpt = ebpt_next_dup(ebpt)) { |
| 1209 | struct crtlist_entry *tmp; |
| 1210 | |
| 1211 | tmp = ebpt_entry(ebpt, struct crtlist_entry, node); |
| 1212 | memprintf(&err, "%s%s%d", err ? err : "", err ? ", " : "", tmp->linenum); |
| 1213 | |
| 1214 | /* select the entry we wanted */ |
| 1215 | if (linenum == 0 || tmp->linenum == linenum) { |
| 1216 | if (!entry) |
| 1217 | entry = tmp; |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | /* we didn't found the specified entry */ |
| 1222 | if (!entry) { |
| 1223 | 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); |
| 1224 | goto error; |
| 1225 | } |
| 1226 | |
| 1227 | /* we didn't specified a line number but there were several entries */ |
| 1228 | if (linenum == 0 && ebpt_next_dup(&entry->node)) { |
| 1229 | memprintf(&err, "found the certificate '%s' in several entries, please specify a line number preceded by colons (%s)!", cert_path, err ? err : NULL); |
| 1230 | goto error; |
| 1231 | } |
| 1232 | |
| 1233 | /* upon error free the ckch_inst and everything inside */ |
| 1234 | |
| 1235 | ebpt_delete(&entry->node); |
| 1236 | LIST_DEL(&entry->by_crtlist); |
| 1237 | LIST_DEL(&entry->by_ckch_store); |
| 1238 | |
| 1239 | list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) { |
| 1240 | struct sni_ctx *sni, *sni_s; |
| 1241 | |
| 1242 | HA_RWLOCK_WRLOCK(SNI_LOCK, &inst->bind_conf->sni_lock); |
| 1243 | list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) { |
| 1244 | ebmb_delete(&sni->name); |
| 1245 | LIST_DEL(&sni->by_ckch_inst); |
| 1246 | SSL_CTX_free(sni->ctx); |
| 1247 | free(sni); |
| 1248 | } |
| 1249 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &inst->bind_conf->sni_lock); |
| 1250 | LIST_DEL(&inst->by_ckchs); |
| 1251 | free(inst); |
| 1252 | } |
| 1253 | |
| 1254 | crtlist_free_filters(entry->filters); |
| 1255 | ssl_sock_free_ssl_conf(entry->ssl_conf); |
| 1256 | free(entry->ssl_conf); |
| 1257 | free(entry); |
| 1258 | |
| 1259 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1260 | err = memprintf(&err, "Entry '%s' deleted in crtlist '%s'!\n", cert_path, crtlist_path); |
| 1261 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 1262 | |
| 1263 | error: |
| 1264 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1265 | err = memprintf(&err, "Can't delete the entry: %s\n", err ? err : ""); |
| 1266 | return cli_dynerr(appctx, err); |
| 1267 | } |
| 1268 | |
| 1269 | |
William Lallemand | ee8530c | 2020-06-23 18:19:42 +0200 | [diff] [blame] | 1270 | /* unlink and free all crt-list and crt-list entries */ |
| 1271 | void crtlist_deinit() |
| 1272 | { |
| 1273 | struct eb_node *node, *next; |
| 1274 | struct crtlist *crtlist; |
| 1275 | |
| 1276 | node = eb_first(&crtlists_tree); |
| 1277 | while (node) { |
| 1278 | next = eb_next(node); |
| 1279 | crtlist = ebmb_entry(node, struct crtlist, node); |
| 1280 | crtlist_free(crtlist); |
| 1281 | node = next; |
| 1282 | } |
| 1283 | } |
| 1284 | |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 1285 | |
| 1286 | /* register cli keywords */ |
| 1287 | static struct cli_kw_list cli_kws = {{ },{ |
| 1288 | { { "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 }, |
| 1289 | { { "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 }, |
| 1290 | { { "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 }, |
| 1291 | { { NULL }, NULL, NULL, NULL } } |
| 1292 | }; |
| 1293 | |
| 1294 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 1295 | |