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