blob: 137a3a7ddafb3e7de70fe1f5c3cf9cbdf1be0415 [file] [log] [blame]
William Lallemand03c331c2020-05-13 10:10:01 +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#define _GNU_SOURCE
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020019#include <syslog.h>
William Lallemand03c331c2020-05-13 10:10:01 +020020#include <unistd.h>
21
22#include <sys/stat.h>
23#include <sys/types.h>
24
Willy Tarreaub2551052020-06-09 09:07:15 +020025#include <import/ebsttree.h>
26
Willy Tarreau8d366972020-05-27 16:10:29 +020027#include <haproxy/base64.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020028#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020029#include <haproxy/cli.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020030#include <haproxy/errors.h>
Willy Tarreau47d7f902020-06-04 14:25:47 +020031#include <haproxy/ssl_ckch.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020032#include <haproxy/ssl_sock.h>
Willy Tarreaub2bd8652020-06-04 14:21:22 +020033#include <haproxy/ssl_utils.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020034#include <haproxy/stream_interface.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020035#include <haproxy/tools.h>
William Lallemand03c331c2020-05-13 10:10:01 +020036
William Lallemandda8584c2020-05-14 10:14:37 +020037/* Uncommitted CKCH transaction */
38
39static struct {
40 struct ckch_store *new_ckchs;
41 struct ckch_store *old_ckchs;
42 char *path;
43} ckchs_transaction;
44
45
William Lallemand03c331c2020-05-13 10:10:01 +020046
47/******************** cert_key_and_chain functions *************************
48 * These are the functions that fills a cert_key_and_chain structure. For the
49 * functions filling a SSL_CTX from a cert_key_and_chain, see ssl_sock.c
50 */
51
52/*
53 * Try to parse Signed Certificate Timestamp List structure. This function
54 * makes only basic test if the data seems like SCTL. No signature validation
55 * is performed.
56 */
57static int ssl_sock_parse_sctl(struct buffer *sctl)
58{
59 int ret = 1;
60 int len, pos, sct_len;
61 unsigned char *data;
62
63 if (sctl->data < 2)
64 goto out;
65
66 data = (unsigned char *) sctl->area;
67 len = (data[0] << 8) | data[1];
68
69 if (len + 2 != sctl->data)
70 goto out;
71
72 data = data + 2;
73 pos = 0;
74 while (pos < len) {
75 if (len - pos < 2)
76 goto out;
77
78 sct_len = (data[pos] << 8) | data[pos + 1];
79 if (pos + sct_len + 2 > len)
80 goto out;
81
82 pos += sct_len + 2;
83 }
84
85 ret = 0;
86
87out:
88 return ret;
89}
90
91/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
92 * It fills the ckch->sctl buffer
93 * return 0 on success or != 0 on failure */
94int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err)
95{
96 int fd = -1;
97 int r = 0;
98 int ret = 1;
99 struct buffer tmp;
100 struct buffer *src;
101 struct buffer *sctl;
102
103 if (buf) {
104 tmp.area = buf;
105 tmp.data = strlen(buf);
106 tmp.size = tmp.data + 1;
107 src = &tmp;
108 } else {
109 fd = open(sctl_path, O_RDONLY);
110 if (fd == -1)
111 goto end;
112
113 trash.data = 0;
114 while (trash.data < trash.size) {
115 r = read(fd, trash.area + trash.data, trash.size - trash.data);
116 if (r < 0) {
117 if (errno == EINTR)
118 continue;
119 goto end;
120 }
121 else if (r == 0) {
122 break;
123 }
124 trash.data += r;
125 }
126 src = &trash;
127 }
128
129 ret = ssl_sock_parse_sctl(src);
130 if (ret)
131 goto end;
132
133 sctl = calloc(1, sizeof(*sctl));
134 if (!chunk_dup(sctl, src)) {
135 free(sctl);
136 sctl = NULL;
137 goto end;
138 }
139 /* no error, fill ckch with new context, old context must be free */
140 if (ckch->sctl) {
141 free(ckch->sctl->area);
142 ckch->sctl->area = NULL;
143 free(ckch->sctl);
144 }
145 ckch->sctl = sctl;
146 ret = 0;
147end:
148 if (fd != -1)
149 close(fd);
150
151 return ret;
152}
153
154#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
155/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500156 * This function load the OCSP Response in DER format contained in file at
William Lallemand03c331c2020-05-13 10:10:01 +0200157 * path 'ocsp_path' or base64 in a buffer <buf>
158 *
159 * Returns 0 on success, 1 in error case.
160 */
161int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err)
162{
163 int fd = -1;
164 int r = 0;
165 int ret = 1;
166 struct buffer *ocsp_response;
167 struct buffer *src = NULL;
168
169 if (buf) {
170 int i, j;
171 /* if it's from a buffer it will be base64 */
172
173 /* remove \r and \n from the payload */
174 for (i = 0, j = 0; buf[i]; i++) {
175 if (buf[i] == '\r' || buf[i] == '\n')
176 continue;
177 buf[j++] = buf[i];
178 }
179 buf[j] = 0;
180
181 ret = base64dec(buf, j, trash.area, trash.size);
182 if (ret < 0) {
183 memprintf(err, "Error reading OCSP response in base64 format");
184 goto end;
185 }
186 trash.data = ret;
187 src = &trash;
188 } else {
189 fd = open(ocsp_path, O_RDONLY);
190 if (fd == -1) {
191 memprintf(err, "Error opening OCSP response file");
192 goto end;
193 }
194
195 trash.data = 0;
196 while (trash.data < trash.size) {
197 r = read(fd, trash.area + trash.data, trash.size - trash.data);
198 if (r < 0) {
199 if (errno == EINTR)
200 continue;
201
202 memprintf(err, "Error reading OCSP response from file");
203 goto end;
204 }
205 else if (r == 0) {
206 break;
207 }
208 trash.data += r;
209 }
210 close(fd);
211 fd = -1;
212 src = &trash;
213 }
214
215 ocsp_response = calloc(1, sizeof(*ocsp_response));
216 if (!chunk_dup(ocsp_response, src)) {
217 free(ocsp_response);
218 ocsp_response = NULL;
219 goto end;
220 }
221 /* no error, fill ckch with new context, old context must be free */
222 if (ckch->ocsp_response) {
223 free(ckch->ocsp_response->area);
224 ckch->ocsp_response->area = NULL;
225 free(ckch->ocsp_response);
226 }
227 ckch->ocsp_response = ocsp_response;
228 ret = 0;
229end:
230 if (fd != -1)
231 close(fd);
232
233 return ret;
234}
235#endif
236
237/*
238 * Try to load in a ckch every files related to a ckch.
239 * (PEM, sctl, ocsp, issuer etc.)
240 *
241 * This function is only used to load files during the configuration parsing,
242 * it is not used with the CLI.
243 *
244 * This allows us to carry the contents of the file without having to read the
245 * file multiple times. The caller must call
246 * ssl_sock_free_cert_key_and_chain_contents.
247 *
248 * returns:
249 * 0 on Success
250 * 1 on SSL Failure
251 */
252int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
253{
254 int ret = 1;
255
256 /* try to load the PEM */
257 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
258 goto end;
259 }
260
261 /* try to load an external private key if it wasn't in the PEM */
262 if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) {
263 char fp[MAXPATHLEN+1];
264 struct stat st;
265
266 snprintf(fp, MAXPATHLEN+1, "%s.key", path);
267 if (stat(fp, &st) == 0) {
268 if (ssl_sock_load_key_into_ckch(fp, NULL, ckch, err)) {
269 memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n",
270 err && *err ? *err : "", fp);
271 goto end;
272 }
273 }
274 }
275
276 if (ckch->key == NULL) {
277 memprintf(err, "%sNo Private Key found in '%s' or '%s.key'.\n", err && *err ? *err : "", path, path);
278 goto end;
279 }
280
281 if (!X509_check_private_key(ckch->cert, ckch->key)) {
282 memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n",
283 err && *err ? *err : "", path);
284 goto end;
285 }
286
287#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
288 /* try to load the sctl file */
289 if (global_ssl.extra_files & SSL_GF_SCTL) {
290 char fp[MAXPATHLEN+1];
291 struct stat st;
292
293 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
294 if (stat(fp, &st) == 0) {
295 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
296 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
297 err && *err ? *err : "", fp);
298 ret = 1;
299 goto end;
300 }
301 }
302 }
303#endif
304
305 /* try to load an ocsp response file */
306 if (global_ssl.extra_files & SSL_GF_OCSP) {
307 char fp[MAXPATHLEN+1];
308 struct stat st;
309
310 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
311 if (stat(fp, &st) == 0) {
312 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
313 ret = 1;
314 goto end;
315 }
316 }
317 }
318
319#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
320 if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
321 /* if no issuer was found, try to load an issuer from the .issuer */
322 if (!ckch->ocsp_issuer) {
323 struct stat st;
324 char fp[MAXPATHLEN+1];
325
326 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
327 if (stat(fp, &st) == 0) {
328 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
329 ret = 1;
330 goto end;
331 }
332
333 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
334 memprintf(err, "%s '%s' is not an issuer'.\n",
335 err && *err ? *err : "", fp);
336 ret = 1;
337 goto end;
338 }
339 }
340 }
341 }
342#endif
343
344 ret = 0;
345
346end:
347
348 ERR_clear_error();
349
350 /* Something went wrong in one of the reads */
351 if (ret != 0)
352 ssl_sock_free_cert_key_and_chain_contents(ckch);
353
354 return ret;
355}
356
357/*
358 * Try to load a private key file from a <path> or a buffer <buf>
359 *
360 * If it failed you should not attempt to use the ckch but free it.
361 *
362 * Return 0 on success or != 0 on failure
363 */
364int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
365{
366 BIO *in = NULL;
367 int ret = 1;
368 EVP_PKEY *key = NULL;
369
370 if (buf) {
371 /* reading from a buffer */
372 in = BIO_new_mem_buf(buf, -1);
373 if (in == NULL) {
374 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
375 goto end;
376 }
377
378 } else {
379 /* reading from a file */
380 in = BIO_new(BIO_s_file());
381 if (in == NULL)
382 goto end;
383
384 if (BIO_read_filename(in, path) <= 0)
385 goto end;
386 }
387
388 /* Read Private Key */
389 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
390 if (key == NULL) {
391 memprintf(err, "%sunable to load private key from file '%s'.\n",
392 err && *err ? *err : "", path);
393 goto end;
394 }
395
396 ret = 0;
397
398 SWAP(ckch->key, key);
399
400end:
401
402 ERR_clear_error();
403 if (in)
404 BIO_free(in);
405 if (key)
406 EVP_PKEY_free(key);
407
408 return ret;
409}
410
411/*
412 * Try to load a PEM file from a <path> or a buffer <buf>
413 * The PEM must contain at least a Certificate,
414 * It could contain a DH, a certificate chain and a PrivateKey.
415 *
416 * If it failed you should not attempt to use the ckch but free it.
417 *
418 * Return 0 on success or != 0 on failure
419 */
420int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
421{
422 BIO *in = NULL;
423 int ret = 1;
424 X509 *ca;
425 X509 *cert = NULL;
426 EVP_PKEY *key = NULL;
427 DH *dh = NULL;
428 STACK_OF(X509) *chain = NULL;
429
430 if (buf) {
431 /* reading from a buffer */
432 in = BIO_new_mem_buf(buf, -1);
433 if (in == NULL) {
434 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
435 goto end;
436 }
437
438 } else {
439 /* reading from a file */
440 in = BIO_new(BIO_s_file());
441 if (in == NULL) {
442 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
443 goto end;
444 }
445
446 if (BIO_read_filename(in, path) <= 0) {
447 memprintf(err, "%scannot open the file '%s'.\n",
448 err && *err ? *err : "", path);
449 goto end;
450 }
451 }
452
453 /* Read Private Key */
454 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
455 /* no need to check for errors here, because the private key could be loaded later */
456
457#ifndef OPENSSL_NO_DH
458 /* Seek back to beginning of file */
459 if (BIO_reset(in) == -1) {
460 memprintf(err, "%san error occurred while reading the file '%s'.\n",
461 err && *err ? *err : "", path);
462 goto end;
463 }
464
465 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
466 /* no need to return an error there, dh is not mandatory */
467#endif
468
469 /* Seek back to beginning of file */
470 if (BIO_reset(in) == -1) {
471 memprintf(err, "%san error occurred while reading the file '%s'.\n",
472 err && *err ? *err : "", path);
473 goto end;
474 }
475
476 /* Read Certificate */
477 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
478 if (cert == NULL) {
479 memprintf(err, "%sunable to load certificate from file '%s'.\n",
480 err && *err ? *err : "", path);
481 goto end;
482 }
483
484 /* Look for a Certificate Chain */
485 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
486 if (chain == NULL)
487 chain = sk_X509_new_null();
488 if (!sk_X509_push(chain, ca)) {
489 X509_free(ca);
490 goto end;
491 }
492 }
493
494 ret = ERR_get_error();
495 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
496 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
497 err && *err ? *err : "", path);
498 goto end;
499 }
500
501 /* once it loaded the PEM, it should remove everything else in the ckch */
502 if (ckch->ocsp_response) {
503 free(ckch->ocsp_response->area);
504 ckch->ocsp_response->area = NULL;
505 free(ckch->ocsp_response);
506 ckch->ocsp_response = NULL;
507 }
508
509 if (ckch->sctl) {
510 free(ckch->sctl->area);
511 ckch->sctl->area = NULL;
512 free(ckch->sctl);
513 ckch->sctl = NULL;
514 }
515
516 if (ckch->ocsp_issuer) {
517 X509_free(ckch->ocsp_issuer);
518 ckch->ocsp_issuer = NULL;
519 }
520
521 /* no error, fill ckch with new context, old context will be free at end: */
522 SWAP(ckch->key, key);
523 SWAP(ckch->dh, dh);
524 SWAP(ckch->cert, cert);
525 SWAP(ckch->chain, chain);
526
527 ret = 0;
528
529end:
530
531 ERR_clear_error();
532 if (in)
533 BIO_free(in);
534 if (key)
535 EVP_PKEY_free(key);
536 if (dh)
537 DH_free(dh);
538 if (cert)
539 X509_free(cert);
540 if (chain)
541 sk_X509_pop_free(chain, X509_free);
542
543 return ret;
544}
545
546/* Frees the contents of a cert_key_and_chain
547 */
548void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
549{
550 if (!ckch)
551 return;
552
553 /* Free the certificate and set pointer to NULL */
554 if (ckch->cert)
555 X509_free(ckch->cert);
556 ckch->cert = NULL;
557
558 /* Free the key and set pointer to NULL */
559 if (ckch->key)
560 EVP_PKEY_free(ckch->key);
561 ckch->key = NULL;
562
563 /* Free each certificate in the chain */
564 if (ckch->chain)
565 sk_X509_pop_free(ckch->chain, X509_free);
566 ckch->chain = NULL;
567
568 if (ckch->dh)
569 DH_free(ckch->dh);
570 ckch->dh = NULL;
571
572 if (ckch->sctl) {
573 free(ckch->sctl->area);
574 ckch->sctl->area = NULL;
575 free(ckch->sctl);
576 ckch->sctl = NULL;
577 }
578
579 if (ckch->ocsp_response) {
580 free(ckch->ocsp_response->area);
581 ckch->ocsp_response->area = NULL;
582 free(ckch->ocsp_response);
583 ckch->ocsp_response = NULL;
584 }
585
586 if (ckch->ocsp_issuer)
587 X509_free(ckch->ocsp_issuer);
588 ckch->ocsp_issuer = NULL;
589}
590
591/*
592 *
593 * This function copy a cert_key_and_chain in memory
594 *
595 * It's used to try to apply changes on a ckch before committing them, because
596 * most of the time it's not possible to revert those changes
597 *
598 * Return a the dst or NULL
599 */
600struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
601 struct cert_key_and_chain *dst)
602{
603 if (src->cert) {
604 dst->cert = src->cert;
605 X509_up_ref(src->cert);
606 }
607
608 if (src->key) {
609 dst->key = src->key;
610 EVP_PKEY_up_ref(src->key);
611 }
612
613 if (src->chain) {
614 dst->chain = X509_chain_up_ref(src->chain);
615 }
616
617 if (src->dh) {
618 DH_up_ref(src->dh);
619 dst->dh = src->dh;
620 }
621
622 if (src->sctl) {
623 struct buffer *sctl;
624
625 sctl = calloc(1, sizeof(*sctl));
626 if (!chunk_dup(sctl, src->sctl)) {
627 free(sctl);
628 sctl = NULL;
629 goto error;
630 }
631 dst->sctl = sctl;
632 }
633
634 if (src->ocsp_response) {
635 struct buffer *ocsp_response;
636
637 ocsp_response = calloc(1, sizeof(*ocsp_response));
638 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
639 free(ocsp_response);
640 ocsp_response = NULL;
641 goto error;
642 }
643 dst->ocsp_response = ocsp_response;
644 }
645
646 if (src->ocsp_issuer) {
647 X509_up_ref(src->ocsp_issuer);
648 dst->ocsp_issuer = src->ocsp_issuer;
649 }
650
651 return dst;
652
653error:
654
655 /* free everything */
656 ssl_sock_free_cert_key_and_chain_contents(dst);
657
658 return NULL;
659}
660
661/*
662 * return 0 on success or != 0 on failure
663 */
664int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
665{
666 int ret = 1;
667 BIO *in = NULL;
668 X509 *issuer;
669
670 if (buf) {
671 /* reading from a buffer */
672 in = BIO_new_mem_buf(buf, -1);
673 if (in == NULL) {
674 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
675 goto end;
676 }
677
678 } else {
679 /* reading from a file */
680 in = BIO_new(BIO_s_file());
681 if (in == NULL)
682 goto end;
683
684 if (BIO_read_filename(in, path) <= 0)
685 goto end;
686 }
687
688 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
689 if (!issuer) {
690 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
691 err && *err ? *err : "", path);
692 goto end;
693 }
694 /* no error, fill ckch with new context, old context must be free */
695 if (ckch->ocsp_issuer)
696 X509_free(ckch->ocsp_issuer);
697 ckch->ocsp_issuer = issuer;
698 ret = 0;
699
700end:
701
702 ERR_clear_error();
703 if (in)
704 BIO_free(in);
705
706 return ret;
707}
708
709/******************** ckch_store functions ***********************************
710 * The ckch_store is a structure used to cache and index the SSL files used in
711 * configuration
712 */
713
714/*
715 * Free a ckch_store, its ckch, its instances and remove it from the ebtree
716 */
717void ckch_store_free(struct ckch_store *store)
718{
719 struct ckch_inst *inst, *inst_s;
720
721 if (!store)
722 return;
723
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200724 ssl_sock_free_cert_key_and_chain_contents(store->ckch);
William Lallemand03c331c2020-05-13 10:10:01 +0200725
726 free(store->ckch);
727 store->ckch = NULL;
728
729 list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) {
730 ckch_inst_free(inst);
731 }
732 ebmb_delete(&store->node);
733 free(store);
734}
735
736/*
737 * create and initialize a ckch_store
738 * <path> is the key name
739 * <nmemb> is the number of store->ckch objects to allocate
740 *
741 * Return a ckch_store or NULL upon failure.
742 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200743struct ckch_store *ckch_store_new(const char *filename)
William Lallemand03c331c2020-05-13 10:10:01 +0200744{
745 struct ckch_store *store;
746 int pathlen;
747
748 pathlen = strlen(filename);
749 store = calloc(1, sizeof(*store) + pathlen + 1);
750 if (!store)
751 return NULL;
752
William Lallemand03c331c2020-05-13 10:10:01 +0200753 memcpy(store->path, filename, pathlen + 1);
754
755 LIST_INIT(&store->ckch_inst);
756 LIST_INIT(&store->crtlist_entry);
757
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200758 store->ckch = calloc(1, sizeof(*store->ckch));
William Lallemand03c331c2020-05-13 10:10:01 +0200759 if (!store->ckch)
760 goto error;
761
762 return store;
763error:
764 ckch_store_free(store);
765 return NULL;
766}
767
768/* allocate and duplicate a ckch_store
769 * Return a new ckch_store or NULL */
770struct ckch_store *ckchs_dup(const struct ckch_store *src)
771{
772 struct ckch_store *dst;
773
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200774 dst = ckch_store_new(src->path);
William Lallemand03c331c2020-05-13 10:10:01 +0200775
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200776 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
777 goto error;
William Lallemand03c331c2020-05-13 10:10:01 +0200778
779 return dst;
780
781error:
782 ckch_store_free(dst);
783
784 return NULL;
785}
786
787/*
788 * lookup a path into the ckchs tree.
789 */
790struct ckch_store *ckchs_lookup(char *path)
791{
792 struct ebmb_node *eb;
793
794 eb = ebst_lookup(&ckchs_tree, path);
795 if (!eb)
796 return NULL;
797
798 return ebmb_entry(eb, struct ckch_store, node);
799}
800
801/*
802 * This function allocate a ckch_store and populate it with certificates from files.
803 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200804struct ckch_store *ckchs_load_cert_file(char *path, char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200805{
806 struct ckch_store *ckchs;
807
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200808 ckchs = ckch_store_new(path);
William Lallemand03c331c2020-05-13 10:10:01 +0200809 if (!ckchs) {
810 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
811 goto end;
812 }
William Lallemand03c331c2020-05-13 10:10:01 +0200813
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200814 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
815 goto end;
William Lallemand03c331c2020-05-13 10:10:01 +0200816
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200817 /* insert into the ckchs tree */
818 memcpy(ckchs->path, path, strlen(path) + 1);
819 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand03c331c2020-05-13 10:10:01 +0200820 return ckchs;
821
822end:
823 ckch_store_free(ckchs);
824
825 return NULL;
826}
827
William Lallemandfa1d8b42020-05-13 15:46:10 +0200828
829/******************** ckch_inst functions ******************************/
830
831/* unlink a ckch_inst, free all SNIs, free the ckch_inst */
832/* The caller must use the lock of the bind_conf if used with inserted SNIs */
833void ckch_inst_free(struct ckch_inst *inst)
834{
835 struct sni_ctx *sni, *sni_s;
836
837 if (inst == NULL)
838 return;
839
840 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
841 SSL_CTX_free(sni->ctx);
842 LIST_DEL(&sni->by_ckch_inst);
843 ebmb_delete(&sni->name);
844 free(sni);
845 }
846 LIST_DEL(&inst->by_ckchs);
847 LIST_DEL(&inst->by_crtlist_entry);
848 free(inst);
849}
850
851/* Alloc and init a ckch_inst */
852struct ckch_inst *ckch_inst_new()
853{
854 struct ckch_inst *ckch_inst;
855
856 ckch_inst = calloc(1, sizeof *ckch_inst);
857 if (!ckch_inst)
858 return NULL;
859
860 LIST_INIT(&ckch_inst->sni_ctx);
861 LIST_INIT(&ckch_inst->by_ckchs);
862 LIST_INIT(&ckch_inst->by_crtlist_entry);
863
864 return ckch_inst;
865}
866
William Lallemandda8584c2020-05-14 10:14:37 +0200867/*************************** CLI commands ***********************/
868
869/* Type of SSL payloads that can be updated over the CLI */
870
871enum {
872 CERT_TYPE_PEM = 0,
873 CERT_TYPE_KEY,
874#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
875 CERT_TYPE_OCSP,
876#endif
877 CERT_TYPE_ISSUER,
878#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
879 CERT_TYPE_SCTL,
880#endif
881 CERT_TYPE_MAX,
882};
883
884struct {
885 const char *ext;
886 int type;
887 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
888 /* add a parsing callback */
889} cert_exts[CERT_TYPE_MAX+1] = {
890 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
891 [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
892#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
893 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
894#endif
895#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
896 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
897#endif
898 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
899 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
900};
901
902
903/* release function of the `show ssl cert' command */
904static void cli_release_show_cert(struct appctx *appctx)
905{
906 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
907}
908
909/* IO handler of "show ssl cert <filename>" */
910static int cli_io_handler_show_cert(struct appctx *appctx)
911{
912 struct buffer *trash = alloc_trash_chunk();
913 struct ebmb_node *node;
914 struct stream_interface *si = appctx->owner;
915 struct ckch_store *ckchs;
916
917 if (trash == NULL)
918 return 1;
919
920 if (!appctx->ctx.ssl.old_ckchs) {
921 if (ckchs_transaction.old_ckchs) {
922 ckchs = ckchs_transaction.old_ckchs;
923 chunk_appendf(trash, "# transaction\n");
William Lallemand5685ccf2020-09-16 16:12:25 +0200924 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +0200925 }
926 }
927
928 if (!appctx->ctx.cli.p0) {
929 chunk_appendf(trash, "# filename\n");
930 node = ebmb_first(&ckchs_tree);
931 } else {
932 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
933 }
934 while (node) {
935 ckchs = ebmb_entry(node, struct ckch_store, node);
William Lallemand5685ccf2020-09-16 16:12:25 +0200936 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +0200937
938 node = ebmb_next(node);
939 if (ci_putchk(si_ic(si), trash) == -1) {
940 si_rx_room_blk(si);
941 goto yield;
942 }
943 }
944
945 appctx->ctx.cli.p0 = NULL;
946 free_trash_chunk(trash);
947 return 1;
948yield:
949
950 free_trash_chunk(trash);
951 appctx->ctx.cli.p0 = ckchs;
952 return 0; /* should come back */
953}
954
955/*
956 * Extract and format the DNS SAN extensions and copy result into a chuink
957 * Return 0;
958 */
959#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
960static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
961{
962 int i;
963 char *str;
964 STACK_OF(GENERAL_NAME) *names = NULL;
965
966 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
967 if (names) {
968 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
969 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
970 if (i > 0)
971 chunk_appendf(out, ", ");
972 if (name->type == GEN_DNS) {
973 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
974 chunk_appendf(out, "DNS:%s", str);
975 OPENSSL_free(str);
976 }
977 }
978 }
979 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
980 }
981 return 0;
982}
983#endif
984
985
986
987
988/* IO handler of the details "show ssl cert <filename>" */
989static int cli_io_handler_show_cert_detail(struct appctx *appctx)
990{
991 struct stream_interface *si = appctx->owner;
992 struct ckch_store *ckchs = appctx->ctx.cli.p0;
993 struct buffer *out = alloc_trash_chunk();
994 struct buffer *tmp = alloc_trash_chunk();
995 X509_NAME *name = NULL;
996 STACK_OF(X509) *chain;
997 unsigned int len = 0;
998 int write = -1;
999 BIO *bio = NULL;
1000 int i;
1001
1002 if (!tmp || !out)
1003 goto end_no_putchk;
1004
William Lallemand5685ccf2020-09-16 16:12:25 +02001005 chunk_appendf(out, "Filename: ");
1006 if (ckchs == ckchs_transaction.new_ckchs)
1007 chunk_appendf(out, "*");
1008 chunk_appendf(out, "%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001009
William Lallemand5685ccf2020-09-16 16:12:25 +02001010 chunk_appendf(out, "Status: ");
1011 if (ckchs->ckch->cert == NULL)
1012 chunk_appendf(out, "Empty\n");
1013 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
1014 chunk_appendf(out, "Unused\n");
1015 else
1016 chunk_appendf(out, "Used\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001017
William Lallemand5685ccf2020-09-16 16:12:25 +02001018 if (ckchs->ckch->cert == NULL)
1019 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02001020
William Lallemand5685ccf2020-09-16 16:12:25 +02001021 chain = ckchs->ckch->chain;
1022 if (chain == NULL) {
1023 struct issuer_chain *issuer;
1024 issuer = ssl_get0_issuer_chain(ckchs->ckch->cert);
1025 if (issuer) {
1026 chain = issuer->chain;
1027 chunk_appendf(out, "Chain Filename: ");
1028 chunk_appendf(out, "%s\n", issuer->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001029 }
William Lallemand5685ccf2020-09-16 16:12:25 +02001030 }
1031 chunk_appendf(out, "Serial: ");
1032 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
1033 goto end;
1034 dump_binary(out, tmp->area, tmp->data);
1035 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001036
William Lallemand5685ccf2020-09-16 16:12:25 +02001037 chunk_appendf(out, "notBefore: ");
1038 chunk_reset(tmp);
1039 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1040 goto end;
1041 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
1042 goto end;
1043 write = BIO_read(bio, tmp->area, tmp->size-1);
1044 tmp->area[write] = '\0';
1045 BIO_free(bio);
1046 bio = NULL;
1047 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001048
William Lallemand5685ccf2020-09-16 16:12:25 +02001049 chunk_appendf(out, "notAfter: ");
1050 chunk_reset(tmp);
1051 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1052 goto end;
1053 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
1054 goto end;
1055 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
1056 goto end;
1057 tmp->area[write] = '\0';
1058 BIO_free(bio);
1059 bio = NULL;
1060 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001061
1062#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand5685ccf2020-09-16 16:12:25 +02001063 chunk_appendf(out, "Subject Alternative Name: ");
1064 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
1065 goto end;
1066 *(out->area + out->data) = '\0';
1067 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001068#endif
William Lallemand5685ccf2020-09-16 16:12:25 +02001069 chunk_reset(tmp);
1070 chunk_appendf(out, "Algorithm: ");
1071 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
1072 goto end;
1073 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001074
William Lallemand5685ccf2020-09-16 16:12:25 +02001075 chunk_reset(tmp);
1076 chunk_appendf(out, "SHA1 FingerPrint: ");
1077 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
1078 goto end;
1079 tmp->data = len;
1080 dump_binary(out, tmp->area, tmp->data);
1081 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001082
William Lallemand5685ccf2020-09-16 16:12:25 +02001083 chunk_appendf(out, "Subject: ");
1084 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
1085 goto end;
1086 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1087 goto end;
1088 *(tmp->area + tmp->data) = '\0';
1089 chunk_appendf(out, "%s\n", tmp->area);
1090
1091 chunk_appendf(out, "Issuer: ");
1092 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
1093 goto end;
1094 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1095 goto end;
1096 *(tmp->area + tmp->data) = '\0';
1097 chunk_appendf(out, "%s\n", tmp->area);
1098
1099 /* Displays subject of each certificate in the chain */
1100 for (i = 0; i < sk_X509_num(chain); i++) {
1101 X509 *ca = sk_X509_value(chain, i);
1102
1103 chunk_appendf(out, "Chain Subject: ");
1104 if ((name = X509_get_subject_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001105 goto end;
1106 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1107 goto end;
1108 *(tmp->area + tmp->data) = '\0';
1109 chunk_appendf(out, "%s\n", tmp->area);
1110
William Lallemand5685ccf2020-09-16 16:12:25 +02001111 chunk_appendf(out, "Chain Issuer: ");
1112 if ((name = X509_get_issuer_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001113 goto end;
1114 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1115 goto end;
1116 *(tmp->area + tmp->data) = '\0';
1117 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001118 }
1119
1120end:
1121 if (ci_putchk(si_ic(si), out) == -1) {
1122 si_rx_room_blk(si);
1123 goto yield;
1124 }
1125
1126end_no_putchk:
1127 if (bio)
1128 BIO_free(bio);
1129 free_trash_chunk(tmp);
1130 free_trash_chunk(out);
1131 return 1;
1132yield:
1133 free_trash_chunk(tmp);
1134 free_trash_chunk(out);
1135 return 0; /* should come back */
1136}
1137
1138/* parsing function for 'show ssl cert [certfile]' */
1139static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
1140{
1141 struct ckch_store *ckchs;
1142
1143 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1144 return cli_err(appctx, "Can't allocate memory!\n");
1145
1146 /* The operations on the CKCH architecture are locked so we can
1147 * manipulate ckch_store and ckch_inst */
1148 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1149 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
1150
1151 /* check if there is a certificate to lookup */
1152 if (*args[3]) {
1153 if (*args[3] == '*') {
1154 if (!ckchs_transaction.new_ckchs)
1155 goto error;
1156
1157 ckchs = ckchs_transaction.new_ckchs;
1158
1159 if (strcmp(args[3] + 1, ckchs->path))
1160 goto error;
1161
1162 } else {
1163 if ((ckchs = ckchs_lookup(args[3])) == NULL)
1164 goto error;
1165
1166 }
1167
William Lallemandda8584c2020-05-14 10:14:37 +02001168 appctx->ctx.cli.p0 = ckchs;
1169 /* use the IO handler that shows details */
1170 appctx->io_handler = cli_io_handler_show_cert_detail;
1171 }
1172
1173 return 0;
1174
1175error:
1176 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1177 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
1178}
1179
1180/* release function of the `set ssl cert' command, free things and unlock the spinlock */
1181static void cli_release_commit_cert(struct appctx *appctx)
1182{
1183 struct ckch_store *new_ckchs;
1184
1185 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1186
1187 if (appctx->st2 != SETCERT_ST_FIN) {
1188 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
1189 new_ckchs = appctx->ctx.ssl.new_ckchs;
1190
1191 /* if the allocation failed, we need to free everything from the temporary list */
1192 ckch_store_free(new_ckchs);
1193 }
1194}
1195
1196/*
1197 * This function tries to create the new ckch_inst and their SNIs
1198 */
1199static int cli_io_handler_commit_cert(struct appctx *appctx)
1200{
1201 struct stream_interface *si = appctx->owner;
1202 int y = 0;
1203 char *err = NULL;
1204 int errcode = 0;
1205 struct ckch_store *old_ckchs, *new_ckchs = NULL;
1206 struct ckch_inst *ckchi, *ckchis;
1207 struct buffer *trash = alloc_trash_chunk();
1208 struct sni_ctx *sc0, *sc0s;
1209 struct crtlist_entry *entry;
1210
1211 if (trash == NULL)
1212 goto error;
1213
1214 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1215 goto error;
1216
1217 while (1) {
1218 switch (appctx->st2) {
1219 case SETCERT_ST_INIT:
1220 /* This state just print the update message */
1221 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
1222 if (ci_putchk(si_ic(si), trash) == -1) {
1223 si_rx_room_blk(si);
1224 goto yield;
1225 }
1226 appctx->st2 = SETCERT_ST_GEN;
1227 /* fallthrough */
1228 case SETCERT_ST_GEN:
1229 /*
1230 * This state generates the ckch instances with their
1231 * sni_ctxs and SSL_CTX.
1232 *
1233 * Since the SSL_CTX generation can be CPU consumer, we
1234 * yield every 10 instances.
1235 */
1236
1237 old_ckchs = appctx->ctx.ssl.old_ckchs;
1238 new_ckchs = appctx->ctx.ssl.new_ckchs;
1239
1240 if (!new_ckchs)
1241 continue;
1242
1243 /* get the next ckchi to regenerate */
1244 ckchi = appctx->ctx.ssl.next_ckchi;
1245 /* we didn't start yet, set it to the first elem */
1246 if (ckchi == NULL)
1247 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
1248
1249 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
1250 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
1251 struct ckch_inst *new_inst;
1252 char **sni_filter = NULL;
1253 int fcount = 0;
1254
1255 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
1256 if (y >= 10) {
1257 /* save the next ckchi to compute */
1258 appctx->ctx.ssl.next_ckchi = ckchi;
1259 goto yield;
1260 }
1261
1262 if (ckchi->crtlist_entry) {
1263 sni_filter = ckchi->crtlist_entry->filters;
1264 fcount = ckchi->crtlist_entry->fcount;
1265 }
1266
William Lallemand95fefa12020-09-09 12:01:33 +02001267 errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err);
William Lallemandda8584c2020-05-14 10:14:37 +02001268
1269 if (errcode & ERR_CODE)
1270 goto error;
1271
1272 /* if the previous ckchi was used as the default */
1273 if (ckchi->is_default)
1274 new_inst->is_default = 1;
1275
1276 /* we need to initialize the SSL_CTX generated */
1277 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
1278 list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) {
1279 if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
1280 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
1281 if (errcode & ERR_CODE)
1282 goto error;
1283 }
1284 }
1285
1286
1287 /* display one dot per new instance */
1288 chunk_appendf(trash, ".");
1289 /* link the new ckch_inst to the duplicate */
1290 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
1291 y++;
1292 }
1293 appctx->st2 = SETCERT_ST_INSERT;
1294 /* fallthrough */
1295 case SETCERT_ST_INSERT:
1296 /* The generation is finished, we can insert everything */
1297
1298 old_ckchs = appctx->ctx.ssl.old_ckchs;
1299 new_ckchs = appctx->ctx.ssl.new_ckchs;
1300
1301 if (!new_ckchs)
1302 continue;
1303
1304 /* get the list of crtlist_entry in the old store, and update the pointers to the store */
1305 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
1306 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
1307 ebpt_delete(&entry->node);
1308 /* change the ptr and reinsert the node */
1309 entry->node.key = new_ckchs;
1310 ebpt_insert(&entry->crtlist->entries, &entry->node);
1311 }
1312
1313 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
1314 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
1315 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
1316 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
1317 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
1318 }
1319
1320 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
1321 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
1322 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
1323
1324 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
1325 ckch_inst_free(ckchi);
1326 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
1327 }
1328
1329 /* Replace the old ckchs by the new one */
1330 ckch_store_free(old_ckchs);
1331 ebst_insert(&ckchs_tree, &new_ckchs->node);
1332 appctx->st2 = SETCERT_ST_FIN;
1333 /* fallthrough */
1334 case SETCERT_ST_FIN:
1335 /* we achieved the transaction, we can set everything to NULL */
1336 free(ckchs_transaction.path);
1337 ckchs_transaction.path = NULL;
1338 ckchs_transaction.new_ckchs = NULL;
1339 ckchs_transaction.old_ckchs = NULL;
1340 goto end;
1341 }
1342 }
1343end:
1344
1345 chunk_appendf(trash, "\n");
1346 if (errcode & ERR_WARN)
1347 chunk_appendf(trash, "%s", err);
1348 chunk_appendf(trash, "Success!\n");
1349 if (ci_putchk(si_ic(si), trash) == -1)
1350 si_rx_room_blk(si);
1351 free_trash_chunk(trash);
1352 /* success: call the release function and don't come back */
1353 return 1;
1354yield:
1355 /* store the state */
1356 if (ci_putchk(si_ic(si), trash) == -1)
1357 si_rx_room_blk(si);
1358 free_trash_chunk(trash);
1359 si_rx_endp_more(si); /* let's come back later */
1360 return 0; /* should come back */
1361
1362error:
1363 /* spin unlock and free are done in the release function */
1364 if (trash) {
1365 chunk_appendf(trash, "\n%sFailed!\n", err);
1366 if (ci_putchk(si_ic(si), trash) == -1)
1367 si_rx_room_blk(si);
1368 free_trash_chunk(trash);
1369 }
1370 /* error: call the release function and don't come back */
1371 return 1;
1372}
1373
1374/*
1375 * Parsing function of 'commit ssl cert'
1376 */
1377static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
1378{
1379 char *err = NULL;
1380
1381 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1382 return 1;
1383
1384 if (!*args[3])
1385 return cli_err(appctx, "'commit ssl cert expects a filename\n");
1386
1387 /* The operations on the CKCH architecture are locked so we can
1388 * manipulate ckch_store and ckch_inst */
1389 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1390 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
1391
1392 if (!ckchs_transaction.path) {
1393 memprintf(&err, "No ongoing transaction! !\n");
1394 goto error;
1395 }
1396
1397 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
1398 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
1399 goto error;
1400 }
1401
William Lallemand5685ccf2020-09-16 16:12:25 +02001402 /* if a certificate is here, a private key must be here too */
1403 if (ckchs_transaction.new_ckchs->ckch->cert && !ckchs_transaction.new_ckchs->ckch->key) {
1404 memprintf(&err, "The transaction must contain at least a certificate and a private key!\n");
1405 goto error;
1406 }
William Lallemanda9419522020-06-24 16:26:41 +02001407
William Lallemand5685ccf2020-09-16 16:12:25 +02001408 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
1409 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
1410 goto error;
William Lallemandda8584c2020-05-14 10:14:37 +02001411 }
1412
1413 /* init the appctx structure */
1414 appctx->st2 = SETCERT_ST_INIT;
1415 appctx->ctx.ssl.next_ckchi = NULL;
1416 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
1417 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
1418
1419 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
1420 return 0;
1421
1422error:
1423
1424 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1425 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
1426
1427 return cli_dynerr(appctx, err);
1428}
1429
1430
1431
1432
1433/*
1434 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
1435 */
1436static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
1437{
1438 struct ckch_store *new_ckchs = NULL;
1439 struct ckch_store *old_ckchs = NULL;
1440 char *err = NULL;
1441 int i;
William Lallemandda8584c2020-05-14 10:14:37 +02001442 int errcode = 0;
1443 char *end;
1444 int type = CERT_TYPE_PEM;
1445 struct cert_key_and_chain *ckch;
1446 struct buffer *buf;
1447
1448 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1449 return 1;
1450
William Lallemandda8584c2020-05-14 10:14:37 +02001451 if (!*args[3] || !payload)
1452 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
1453
1454 /* The operations on the CKCH architecture are locked so we can
1455 * manipulate ckch_store and ckch_inst */
1456 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1457 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
1458
William Lallemande5ff4ad2020-06-08 09:40:37 +02001459 if ((buf = alloc_trash_chunk()) == NULL)
1460 return cli_err(appctx, "Can't allocate memory\n");
1461
William Lallemandda8584c2020-05-14 10:14:37 +02001462 if (!chunk_strcpy(buf, args[3])) {
1463 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
1464 errcode |= ERR_ALERT | ERR_FATAL;
1465 goto end;
1466 }
1467
1468 /* check which type of file we want to update */
1469 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
1470 end = strrchr(buf->area, '.');
1471 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
1472 *end = '\0';
1473 type = cert_exts[i].type;
1474 break;
1475 }
1476 }
1477
1478 appctx->ctx.ssl.old_ckchs = NULL;
1479 appctx->ctx.ssl.new_ckchs = NULL;
1480
1481 /* if there is an ongoing transaction */
1482 if (ckchs_transaction.path) {
William Lallemandda8584c2020-05-14 10:14:37 +02001483 /* if there is an ongoing transaction, check if this is the same file */
1484 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
1485 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
1486 errcode |= ERR_ALERT | ERR_FATAL;
1487 goto end;
1488 }
1489
1490 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
1491
1492 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02001493
William Lallemand95fefa12020-09-09 12:01:33 +02001494 /* lookup for the certificate in the tree */
1495 appctx->ctx.ssl.old_ckchs = ckchs_lookup(buf->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001496 }
1497
1498 if (!appctx->ctx.ssl.old_ckchs) {
1499 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
1500 err ? err : "");
1501 errcode |= ERR_ALERT | ERR_FATAL;
1502 goto end;
1503 }
1504
1505 if (!appctx->ctx.ssl.path) {
1506 /* this is a new transaction, set the path of the transaction */
1507 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
1508 if (!appctx->ctx.ssl.path) {
1509 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
1510 errcode |= ERR_ALERT | ERR_FATAL;
1511 goto end;
1512 }
1513 }
1514
1515 old_ckchs = appctx->ctx.ssl.old_ckchs;
1516
1517 /* duplicate the ckch store */
1518 new_ckchs = ckchs_dup(old_ckchs);
1519 if (!new_ckchs) {
1520 memprintf(&err, "%sCannot allocate memory!\n",
1521 err ? err : "");
1522 errcode |= ERR_ALERT | ERR_FATAL;
1523 goto end;
1524 }
1525
William Lallemand95fefa12020-09-09 12:01:33 +02001526 ckch = new_ckchs->ckch;
William Lallemandda8584c2020-05-14 10:14:37 +02001527
1528 /* appply the change on the duplicate */
1529 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
1530 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
1531 errcode |= ERR_ALERT | ERR_FATAL;
1532 goto end;
1533 }
1534
1535 appctx->ctx.ssl.new_ckchs = new_ckchs;
1536
1537 /* we succeed, we can save the ckchs in the transaction */
1538
1539 /* if there wasn't a transaction, update the old ckchs */
1540 if (!ckchs_transaction.old_ckchs) {
1541 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
1542 ckchs_transaction.path = appctx->ctx.ssl.path;
1543 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
1544 } else {
1545 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
1546
1547 }
1548
1549 /* free the previous ckchs if there was a transaction */
1550 ckch_store_free(ckchs_transaction.new_ckchs);
1551
1552 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
1553
1554
1555 /* creates the SNI ctxs later in the IO handler */
1556
1557end:
1558 free_trash_chunk(buf);
1559
1560 if (errcode & ERR_CODE) {
1561
1562 ckch_store_free(appctx->ctx.ssl.new_ckchs);
1563 appctx->ctx.ssl.new_ckchs = NULL;
1564
1565 appctx->ctx.ssl.old_ckchs = NULL;
1566
1567 free(appctx->ctx.ssl.path);
1568 appctx->ctx.ssl.path = NULL;
1569
1570 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1571 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
1572 } else {
1573
1574 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1575 return cli_dynmsg(appctx, LOG_NOTICE, err);
1576 }
1577 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
1578}
1579
1580/* parsing function of 'abort ssl cert' */
1581static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
1582{
1583 char *err = NULL;
1584
1585 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1586 return 1;
1587
1588 if (!*args[3])
1589 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
1590
1591 /* The operations on the CKCH architecture are locked so we can
1592 * manipulate ckch_store and ckch_inst */
1593 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1594 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
1595
1596 if (!ckchs_transaction.path) {
1597 memprintf(&err, "No ongoing transaction!\n");
1598 goto error;
1599 }
1600
1601 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
1602 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
1603 goto error;
1604 }
1605
1606 /* Only free the ckchs there, because the SNI and instances were not generated yet */
1607 ckch_store_free(ckchs_transaction.new_ckchs);
1608 ckchs_transaction.new_ckchs = NULL;
1609 ckch_store_free(ckchs_transaction.old_ckchs);
1610 ckchs_transaction.old_ckchs = NULL;
1611 free(ckchs_transaction.path);
1612 ckchs_transaction.path = NULL;
1613
1614 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1615
1616 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
1617 return cli_dynmsg(appctx, LOG_NOTICE, err);
1618
1619error:
1620 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1621
1622 return cli_dynerr(appctx, err);
1623}
1624
1625/* parsing function of 'new ssl cert' */
1626static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
1627{
1628 struct ckch_store *store;
1629 char *err = NULL;
1630 char *path;
1631
1632 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1633 return 1;
1634
1635 if (!*args[3])
1636 return cli_err(appctx, "'new ssl cert' expects a filename\n");
1637
1638 path = args[3];
1639
1640 /* The operations on the CKCH architecture are locked so we can
1641 * manipulate ckch_store and ckch_inst */
1642 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1643 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
1644
1645 store = ckchs_lookup(path);
1646 if (store != NULL) {
1647 memprintf(&err, "Certificate '%s' already exists!\n", path);
1648 store = NULL; /* we don't want to free it */
1649 goto error;
1650 }
1651 /* we won't support multi-certificate bundle here */
William Lallemandbd8e6ed2020-09-16 16:08:08 +02001652 store = ckch_store_new(path);
William Lallemandda8584c2020-05-14 10:14:37 +02001653 if (!store) {
1654 memprintf(&err, "unable to allocate memory.\n");
1655 goto error;
1656 }
1657
1658 /* insert into the ckchs tree */
1659 ebst_insert(&ckchs_tree, &store->node);
1660 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
1661
1662 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1663 return cli_dynmsg(appctx, LOG_NOTICE, err);
1664error:
1665 free(store);
1666 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1667 return cli_dynerr(appctx, err);
1668}
1669
1670/* parsing function of 'del ssl cert' */
1671static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
1672{
1673 struct ckch_store *store;
1674 char *err = NULL;
1675 char *filename;
1676
1677 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1678 return 1;
1679
1680 if (!*args[3])
1681 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
1682
1683 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1684 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
1685
1686 filename = args[3];
1687
1688 store = ckchs_lookup(filename);
1689 if (store == NULL) {
1690 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
1691 goto error;
1692 }
1693 if (!LIST_ISEMPTY(&store->ckch_inst)) {
1694 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
1695 goto error;
1696 }
1697
1698 ebmb_delete(&store->node);
1699 ckch_store_free(store);
1700
1701 memprintf(&err, "Certificate '%s' deleted!\n", filename);
1702
1703 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1704 return cli_dynmsg(appctx, LOG_NOTICE, err);
1705
1706error:
1707 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
1708 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1709 return cli_dynerr(appctx, err);
1710}
1711
William Lallemandee8530c2020-06-23 18:19:42 +02001712void ckch_deinit()
1713{
1714 struct eb_node *node, *next;
1715 struct ckch_store *store;
1716
1717 node = eb_first(&ckchs_tree);
1718 while (node) {
1719 next = eb_next(node);
1720 store = ebmb_entry(node, struct ckch_store, node);
1721 ckch_store_free(store);
1722 node = next;
1723 }
1724}
William Lallemandda8584c2020-05-14 10:14:37 +02001725
1726/* register cli keywords */
1727static struct cli_kw_list cli_kws = {{ },{
1728 { { "new", "ssl", "cert", NULL }, "new ssl cert <certfile> : create a new certificate file to be used in a crt-list or a directory", cli_parse_new_cert, NULL, NULL },
1729 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
1730 { { "commit", "ssl", "cert", NULL }, "commit ssl cert <certfile> : commit a certificate file", cli_parse_commit_cert, cli_io_handler_commit_cert, cli_release_commit_cert },
1731 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
1732 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
1733 { { "show", "ssl", "cert", NULL }, "show ssl cert [<certfile>] : display the SSL certificates used in memory, or the details of a <certfile>", cli_parse_show_cert, cli_io_handler_show_cert, cli_release_show_cert },
1734 { { NULL }, NULL, NULL, NULL }
1735}};
1736
1737INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
1738