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