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