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