blob: cf3382f9d0269a3d9556bf2a51993335964a742d [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
12#include <stdlib.h>
13#include <string.h>
14#include <sys/stat.h>
15#include <sys/types.h>
16
17#include <common/errors.h>
18#include <common/standard.h>
19
20#include <dirent.h>
21#include <ebpttree.h>
22
23#include <types/ssl_crtlist.h>
24#include <types/ssl_ckch.h>
25#include <types/ssl_sock.h>
26
27#include <proto/ssl_crtlist.h>
28#include <proto/ssl_ckch.h>
29#include <proto/ssl_sock.h>
30
31/* release ssl bind conf */
32void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
33{
34 if (conf) {
35#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
36 free(conf->npn_str);
37 conf->npn_str = NULL;
38#endif
39#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
40 free(conf->alpn_str);
41 conf->alpn_str = NULL;
42#endif
43 free(conf->ca_file);
44 conf->ca_file = NULL;
45 free(conf->ca_verify_file);
46 conf->ca_verify_file = NULL;
47 free(conf->crl_file);
48 conf->crl_file = NULL;
49 free(conf->ciphers);
50 conf->ciphers = NULL;
51#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
52 free(conf->ciphersuites);
53 conf->ciphersuites = NULL;
54#endif
55 free(conf->curves);
56 conf->curves = NULL;
57 free(conf->ecdhe);
58 conf->ecdhe = NULL;
59 }
60}
61
62
63/* free sni filters */
64void crtlist_free_filters(char **args)
65{
66 int i;
67
68 if (!args)
69 return;
70
71 for (i = 0; args[i]; i++)
72 free(args[i]);
73
74 free(args);
75}
76
77/* Alloc and duplicate a char ** array */
78char **crtlist_dup_filters(char **args, int fcount)
79{
80 char **dst;
81 int i;
82
83 if (fcount == 0)
84 return NULL;
85
86 dst = calloc(fcount + 1, sizeof(*dst));
87 if (!dst)
88 return NULL;
89
90 for (i = 0; i < fcount; i++) {
91 dst[i] = strdup(args[i]);
92 if (!dst[i])
93 goto error;
94 }
95 return dst;
96
97error:
98 crtlist_free_filters(dst);
99 return NULL;
100}
101
102/*
103 * Detach and free a crtlist_entry.
104 * Free the filters, the ssl_conf and call ckch_inst_free() for each ckch_inst
105 */
106void crtlist_entry_free(struct crtlist_entry *entry)
107{
108 struct ckch_inst *inst, *inst_s;
109
110 if (entry == NULL)
111 return;
112
113 ebpt_delete(&entry->node);
114 LIST_DEL(&entry->by_crtlist);
115 LIST_DEL(&entry->by_ckch_store);
116 crtlist_free_filters(entry->filters);
117 ssl_sock_free_ssl_conf(entry->ssl_conf);
118 free(entry->ssl_conf);
119 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) {
120 ckch_inst_free(inst);
121 }
122 free(entry);
123}
124
125/*
126 * Allocate and initialize a crtlist_entry
127 */
128struct crtlist_entry *crtlist_entry_new()
129{
130 struct crtlist_entry *entry;
131
132 entry = calloc(1, sizeof(*entry));
133 if (entry == NULL)
134 return NULL;
135
136 LIST_INIT(&entry->ckch_inst);
137
138 /* initialize the nodes so we can LIST_DEL in any cases */
139 LIST_INIT(&entry->by_crtlist);
140 LIST_INIT(&entry->by_ckch_store);
141
142 return entry;
143}
144
145/* Free a crtlist, from the crt_entry to the content of the ssl_conf */
146void crtlist_free(struct crtlist *crtlist)
147{
148 struct crtlist_entry *entry, *s_entry;
149
150 if (crtlist == NULL)
151 return;
152
153 list_for_each_entry_safe(entry, s_entry, &crtlist->ord_entries, by_crtlist) {
154 crtlist_entry_free(entry);
155 }
156 ebmb_delete(&crtlist->node);
157 free(crtlist);
158}
159
160/* Alloc and initialize a struct crtlist
161 * <filename> is the key of the ebmb_node
162 * <unique> initialize the list of entries to be unique (1) or not (0)
163 */
164struct crtlist *crtlist_new(const char *filename, int unique)
165{
166 struct crtlist *newlist;
167
168 newlist = calloc(1, sizeof(*newlist) + strlen(filename) + 1);
169 if (newlist == NULL)
170 return NULL;
171
172 memcpy(newlist->node.key, filename, strlen(filename) + 1);
173 if (unique)
174 newlist->entries = EB_ROOT_UNIQUE;
175 else
176 newlist->entries = EB_ROOT;
177
178 LIST_INIT(&newlist->ord_entries);
179
180 return newlist;
181}
182
183/*
184 * Read a single crt-list line. /!\ alter the <line> string.
185 * Fill <crt_path> and <crtlist_entry>
186 * <crtlist_entry> must be alloc and free by the caller
187 * <crtlist_entry->ssl_conf> is alloc by the function
188 * <crtlist_entry->filters> is alloc by the function
189 * <crt_path> is a ptr in <line>
190 * Return an error code
191 */
192int crtlist_parse_line(char *line, char **crt_path, struct crtlist_entry *entry, const char *file, int linenum, char **err)
193{
194 int cfgerr = 0;
195 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
196 char *end;
197 char *args[MAX_CRT_ARGS + 1];
198 struct ssl_bind_conf *ssl_conf = NULL;
199
200 if (!line || !crt_path || !entry)
201 return ERR_ALERT | ERR_FATAL;
202
203 end = line + strlen(line);
204 if (end-line >= CRT_LINESIZE-1 && *(end-1) != '\n') {
205 /* Check if we reached the limit and the last char is not \n.
206 * Watch out for the last line without the terminating '\n'!
207 */
208 memprintf(err, "line %d too long in file '%s', limit is %d characters",
209 linenum, file, CRT_LINESIZE-1);
210 cfgerr |= ERR_ALERT | ERR_FATAL;
211 goto error;
212 }
213 arg = 0;
214 newarg = 1;
215 while (*line) {
216 if (isspace((unsigned char)*line)) {
217 newarg = 1;
218 *line = 0;
219 } else if (*line == '[') {
220 if (ssl_b) {
221 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
222 cfgerr |= ERR_ALERT | ERR_FATAL;
223 goto error;
224 }
225 if (!arg) {
226 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
227 cfgerr |= ERR_ALERT | ERR_FATAL;
228 goto error;
229 }
230 ssl_b = arg;
231 newarg = 1;
232 *line = 0;
233 } else if (*line == ']') {
234 if (ssl_e) {
235 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
236 cfgerr |= ERR_ALERT | ERR_FATAL;
237 goto error;
238 }
239 if (!ssl_b) {
240 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
241 cfgerr |= ERR_ALERT | ERR_FATAL;
242 goto error;
243 }
244 ssl_e = arg;
245 newarg = 1;
246 *line = 0;
247 } else if (newarg) {
248 if (arg == MAX_CRT_ARGS) {
249 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
250 cfgerr |= ERR_ALERT | ERR_FATAL;
251 goto error;
252 }
253 newarg = 0;
254 args[arg++] = line;
255 }
256 line++;
257 }
258 args[arg++] = line;
259
260 /* empty line */
261 if (!*args[0]) {
262 cfgerr |= ERR_NONE;
263 goto error;
264 }
265
266 *crt_path = args[0];
267
268 if (ssl_b) {
269 ssl_conf = calloc(1, sizeof *ssl_conf);
270 if (!ssl_conf) {
271 memprintf(err, "not enough memory!");
272 cfgerr |= ERR_ALERT | ERR_FATAL;
273 goto error;
274 }
275 }
276 cur_arg = ssl_b ? ssl_b : 1;
277 while (cur_arg < ssl_e) {
278 newarg = 0;
279 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
280 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
281 newarg = 1;
282 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, NULL, ssl_conf, err);
283 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
284 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
285 args[cur_arg], linenum, file);
286 cfgerr |= ERR_ALERT | ERR_FATAL;
287 goto error;
288 }
289 cur_arg += 1 + ssl_bind_kws[i].skip;
290 break;
291 }
292 }
293 if (!cfgerr && !newarg) {
294 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
295 args[cur_arg], linenum, file);
296 cfgerr |= ERR_ALERT | ERR_FATAL;
297 goto error;
298 }
299 }
300 entry->linenum = linenum;
301 entry->ssl_conf = ssl_conf;
302 entry->filters = crtlist_dup_filters(&args[cur_arg], arg - cur_arg - 1);
303 entry->fcount = arg - cur_arg - 1;
304
305 return cfgerr;
306
307error:
308 crtlist_free_filters(entry->filters);
309 entry->filters = NULL;
310 ssl_sock_free_ssl_conf(entry->ssl_conf);
311 free(entry->ssl_conf);
312 entry->ssl_conf = NULL;
313 return cfgerr;
314}
315
316
317
318/* This function parse a crt-list file and store it in a struct crtlist, each line is a crtlist_entry structure
319 * Fill the <crtlist> argument with a pointer to a new crtlist struct
320 *
321 * This function tries to open and store certificate files.
322 */
323int crtlist_parse_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, struct crtlist **crtlist, char **err)
324{
325 struct crtlist *newlist;
326 struct crtlist_entry *entry = NULL;
327 char thisline[CRT_LINESIZE];
328 char path[MAXPATHLEN+1];
329 FILE *f;
330 struct stat buf;
331 int linenum = 0;
332 int cfgerr = 0;
333
334 if ((f = fopen(file, "r")) == NULL) {
335 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
336 return ERR_ALERT | ERR_FATAL;
337 }
338
339 newlist = crtlist_new(file, 0);
340 if (newlist == NULL) {
341 memprintf(err, "Not enough memory!");
342 cfgerr |= ERR_ALERT | ERR_FATAL;
343 goto error;
344 }
345
346 while (fgets(thisline, sizeof(thisline), f) != NULL) {
347 char *end;
348 char *line = thisline;
349 char *crt_path;
350 struct ckch_store *ckchs;
351
352 linenum++;
353 end = line + strlen(line);
354 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
355 /* Check if we reached the limit and the last char is not \n.
356 * Watch out for the last line without the terminating '\n'!
357 */
358 memprintf(err, "line %d too long in file '%s', limit is %d characters",
359 linenum, file, (int)sizeof(thisline)-1);
360 cfgerr |= ERR_ALERT | ERR_FATAL;
361 break;
362 }
363
364 if (*line == '#' || *line == '\n' || *line == '\r')
365 continue;
366
367 entry = crtlist_entry_new();
368 if (entry == NULL) {
369 memprintf(err, "Not enough memory!");
370 cfgerr |= ERR_ALERT | ERR_FATAL;
371 goto error;
372 }
373
374 *(end - 1) = '\0'; /* line parser mustn't receive any \n */
375 cfgerr |= crtlist_parse_line(thisline, &crt_path, entry, file, linenum, err);
376 if (cfgerr)
377 goto error;
378
379 /* empty line */
380 if (!crt_path || !*crt_path) {
381 crtlist_entry_free(entry);
382 entry = NULL;
383 continue;
384 }
385
386 if (*crt_path != '/' && global_ssl.crt_base) {
387 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
388 memprintf(err, "'%s' : path too long on line %d in file '%s'",
389 crt_path, linenum, file);
390 cfgerr |= ERR_ALERT | ERR_FATAL;
391 goto error;
392 }
393 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
394 crt_path = path;
395 }
396
397 /* Look for a ckch_store or create one */
398 ckchs = ckchs_lookup(crt_path);
399 if (ckchs == NULL) {
400 if (stat(crt_path, &buf) == 0)
401 ckchs = ckchs_load_cert_file(crt_path, 0, err);
402 else
403 ckchs = ckchs_load_cert_file(crt_path, 1, err);
404 }
405 if (ckchs == NULL)
406 cfgerr |= ERR_ALERT | ERR_FATAL;
407
408 if (cfgerr & ERR_CODE)
409 goto error;
410
411 entry->node.key = ckchs;
412 entry->crtlist = newlist;
413 ebpt_insert(&newlist->entries, &entry->node);
414 LIST_ADDQ(&newlist->ord_entries, &entry->by_crtlist);
415 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
416
417 entry = NULL;
418 }
419 if (cfgerr & ERR_CODE)
420 goto error;
421
422 newlist->linecount = linenum;
423
424 fclose(f);
425 *crtlist = newlist;
426
427 return cfgerr;
428error:
429 crtlist_entry_free(entry);
430
431 fclose(f);
432 crtlist_free(newlist);
433 return cfgerr;
434}
435
436/* This function reads a directory and stores it in a struct crtlist, each file is a crtlist_entry structure
437 * Fill the <crtlist> argument with a pointer to a new crtlist struct
438 *
439 * This function tries to open and store certificate files.
440 */
441int crtlist_load_cert_dir(char *path, struct bind_conf *bind_conf, struct crtlist **crtlist, char **err)
442{
443 struct crtlist *dir;
444 struct dirent **de_list;
445 int i, n;
446 struct stat buf;
447 char *end;
448 char fp[MAXPATHLEN+1];
449 int cfgerr = 0;
450 struct ckch_store *ckchs;
451#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
452 int is_bundle;
453 int j;
454#endif
455
456 dir = crtlist_new(path, 1);
457 if (dir == NULL) {
458 memprintf(err, "not enough memory");
459 return ERR_ALERT | ERR_FATAL;
460 }
461
462 n = scandir(path, &de_list, 0, alphasort);
463 if (n < 0) {
464 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
465 err && *err ? *err : "", path, strerror(errno));
466 cfgerr |= ERR_ALERT | ERR_FATAL;
467 }
468 else {
469 for (i = 0; i < n; i++) {
470 struct crtlist_entry *entry;
471 struct dirent *de = de_list[i];
472
473 end = strrchr(de->d_name, '.');
474 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key")))
475 goto ignore_entry;
476
477 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
478 if (stat(fp, &buf) != 0) {
479 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
480 err && *err ? *err : "", fp, strerror(errno));
481 cfgerr |= ERR_ALERT | ERR_FATAL;
482 goto ignore_entry;
483 }
484 if (!S_ISREG(buf.st_mode))
485 goto ignore_entry;
486
487 entry = crtlist_entry_new();
488 if (entry == NULL) {
489 memprintf(err, "not enough memory '%s'", fp);
490 cfgerr |= ERR_ALERT | ERR_FATAL;
491 goto ignore_entry;
492 }
493
494#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
495 is_bundle = 0;
496 /* Check if current entry in directory is part of a multi-cert bundle */
497
498 if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) {
499 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
500 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
501 is_bundle = 1;
502 break;
503 }
504 }
505
506 if (is_bundle) {
507 int dp_len;
508
509 dp_len = end - de->d_name;
510
511 /* increment i and free de until we get to a non-bundle cert
512 * Note here that we look at de_list[i + 1] before freeing de
513 * this is important since ignore_entry will free de. This also
514 * guarantees that de->d_name continues to hold the same prefix.
515 */
516 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
517 free(de);
518 i++;
519 de = de_list[i];
520 }
521
522 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
523 ckchs = ckchs_lookup(fp);
524 if (ckchs == NULL)
525 ckchs = ckchs_load_cert_file(fp, 1, err);
526 if (ckchs == NULL) {
527 free(de);
528 free(entry);
529 cfgerr |= ERR_ALERT | ERR_FATAL;
530 goto end;
531 }
532 entry->node.key = ckchs;
533 entry->crtlist = dir;
534 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
535 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
536 ebpt_insert(&dir->entries, &entry->node);
537
538 /* Successfully processed the bundle */
539 goto ignore_entry;
540 }
541 }
542
543#endif
544 ckchs = ckchs_lookup(fp);
545 if (ckchs == NULL)
546 ckchs = ckchs_load_cert_file(fp, 0, err);
547 if (ckchs == NULL) {
548 free(de);
549 free(entry);
550 cfgerr |= ERR_ALERT | ERR_FATAL;
551 goto end;
552 }
553 entry->node.key = ckchs;
554 entry->crtlist = dir;
555 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
556 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
557 ebpt_insert(&dir->entries, &entry->node);
558
559ignore_entry:
560 free(de);
561 }
562end:
563 free(de_list);
564 }
565
566 if (cfgerr & ERR_CODE) {
567 /* free the dir and entries on error */
568 crtlist_free(dir);
569 } else {
570 *crtlist = dir;
571 }
572 return cfgerr;
573
574}
575