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