blob: 2ba78300f01fe54e97a34a1451e59810db8c1010 [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");
924 if (!ckchs->multi) {
925 chunk_appendf(trash, "*%s\n", ckchs->path);
926#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
927 } else {
928 int n;
929
930 chunk_appendf(trash, "*%s:", ckchs->path);
931 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
932 if (ckchs->ckch[n].cert)
933 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
934 }
935 chunk_appendf(trash, "\n");
936#endif
937 }
938 }
939 }
940
941 if (!appctx->ctx.cli.p0) {
942 chunk_appendf(trash, "# filename\n");
943 node = ebmb_first(&ckchs_tree);
944 } else {
945 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
946 }
947 while (node) {
948 ckchs = ebmb_entry(node, struct ckch_store, node);
949 if (!ckchs->multi) {
950 chunk_appendf(trash, "%s\n", ckchs->path);
951#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
952 } else {
953 int n;
954
955 chunk_appendf(trash, "%s:", ckchs->path);
956 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
957 if (ckchs->ckch[n].cert)
958 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
959 }
960 chunk_appendf(trash, "\n");
961#endif
962 }
963
964 node = ebmb_next(node);
965 if (ci_putchk(si_ic(si), trash) == -1) {
966 si_rx_room_blk(si);
967 goto yield;
968 }
969 }
970
971 appctx->ctx.cli.p0 = NULL;
972 free_trash_chunk(trash);
973 return 1;
974yield:
975
976 free_trash_chunk(trash);
977 appctx->ctx.cli.p0 = ckchs;
978 return 0; /* should come back */
979}
980
981/*
982 * Extract and format the DNS SAN extensions and copy result into a chuink
983 * Return 0;
984 */
985#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
986static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
987{
988 int i;
989 char *str;
990 STACK_OF(GENERAL_NAME) *names = NULL;
991
992 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
993 if (names) {
994 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
995 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
996 if (i > 0)
997 chunk_appendf(out, ", ");
998 if (name->type == GEN_DNS) {
999 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
1000 chunk_appendf(out, "DNS:%s", str);
1001 OPENSSL_free(str);
1002 }
1003 }
1004 }
1005 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
1006 }
1007 return 0;
1008}
1009#endif
1010
1011
1012
1013
1014/* IO handler of the details "show ssl cert <filename>" */
1015static int cli_io_handler_show_cert_detail(struct appctx *appctx)
1016{
1017 struct stream_interface *si = appctx->owner;
1018 struct ckch_store *ckchs = appctx->ctx.cli.p0;
1019 struct buffer *out = alloc_trash_chunk();
1020 struct buffer *tmp = alloc_trash_chunk();
1021 X509_NAME *name = NULL;
1022 STACK_OF(X509) *chain;
1023 unsigned int len = 0;
1024 int write = -1;
1025 BIO *bio = NULL;
1026 int i;
1027
1028 if (!tmp || !out)
1029 goto end_no_putchk;
1030
1031 if (!ckchs->multi) {
1032 chunk_appendf(out, "Filename: ");
1033 if (ckchs == ckchs_transaction.new_ckchs)
1034 chunk_appendf(out, "*");
1035 chunk_appendf(out, "%s\n", ckchs->path);
1036
1037 chunk_appendf(out, "Status: ");
1038 if (ckchs->ckch->cert == NULL)
1039 chunk_appendf(out, "Empty\n");
1040 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
1041 chunk_appendf(out, "Unused\n");
1042 else
1043 chunk_appendf(out, "Used\n");
1044
1045 if (ckchs->ckch->cert == NULL)
1046 goto end;
1047
1048 chain = ckchs->ckch->chain;
1049 if (chain == NULL) {
1050 struct issuer_chain *issuer;
1051 issuer = ssl_get0_issuer_chain(ckchs->ckch->cert);
1052 if (issuer) {
1053 chain = issuer->chain;
1054 chunk_appendf(out, "Chain Filename: ");
1055 chunk_appendf(out, "%s\n", issuer->path);
1056 }
1057 }
1058 chunk_appendf(out, "Serial: ");
1059 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
1060 goto end;
1061 dump_binary(out, tmp->area, tmp->data);
1062 chunk_appendf(out, "\n");
1063
1064 chunk_appendf(out, "notBefore: ");
1065 chunk_reset(tmp);
1066 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1067 goto end;
1068 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
1069 goto end;
1070 write = BIO_read(bio, tmp->area, tmp->size-1);
1071 tmp->area[write] = '\0';
1072 BIO_free(bio);
1073 bio = NULL;
1074 chunk_appendf(out, "%s\n", tmp->area);
1075
1076 chunk_appendf(out, "notAfter: ");
1077 chunk_reset(tmp);
1078 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1079 goto end;
1080 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
1081 goto end;
1082 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
1083 goto end;
1084 tmp->area[write] = '\0';
1085 BIO_free(bio);
1086 bio = NULL;
1087 chunk_appendf(out, "%s\n", tmp->area);
1088
1089#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1090 chunk_appendf(out, "Subject Alternative Name: ");
1091 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
1092 goto end;
1093 *(out->area + out->data) = '\0';
1094 chunk_appendf(out, "\n");
1095#endif
1096 chunk_reset(tmp);
1097 chunk_appendf(out, "Algorithm: ");
1098 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
1099 goto end;
1100 chunk_appendf(out, "%s\n", tmp->area);
1101
1102 chunk_reset(tmp);
1103 chunk_appendf(out, "SHA1 FingerPrint: ");
1104 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
1105 goto end;
1106 tmp->data = len;
1107 dump_binary(out, tmp->area, tmp->data);
1108 chunk_appendf(out, "\n");
1109
1110 chunk_appendf(out, "Subject: ");
1111 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
1112 goto end;
1113 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1114 goto end;
1115 *(tmp->area + tmp->data) = '\0';
1116 chunk_appendf(out, "%s\n", tmp->area);
1117
1118 chunk_appendf(out, "Issuer: ");
1119 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
1120 goto end;
1121 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1122 goto end;
1123 *(tmp->area + tmp->data) = '\0';
1124 chunk_appendf(out, "%s\n", tmp->area);
1125
1126 /* Displays subject of each certificate in the chain */
1127 for (i = 0; i < sk_X509_num(chain); i++) {
1128 X509 *ca = sk_X509_value(chain, i);
1129
1130 chunk_appendf(out, "Chain Subject: ");
1131 if ((name = X509_get_subject_name(ca)) == NULL)
1132 goto end;
1133 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1134 goto end;
1135 *(tmp->area + tmp->data) = '\0';
1136 chunk_appendf(out, "%s\n", tmp->area);
1137
1138 chunk_appendf(out, "Chain Issuer: ");
1139 if ((name = X509_get_issuer_name(ca)) == NULL)
1140 goto end;
1141 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1142 goto end;
1143 *(tmp->area + tmp->data) = '\0';
1144 chunk_appendf(out, "%s\n", tmp->area);
1145 }
1146 }
1147
1148end:
1149 if (ci_putchk(si_ic(si), out) == -1) {
1150 si_rx_room_blk(si);
1151 goto yield;
1152 }
1153
1154end_no_putchk:
1155 if (bio)
1156 BIO_free(bio);
1157 free_trash_chunk(tmp);
1158 free_trash_chunk(out);
1159 return 1;
1160yield:
1161 free_trash_chunk(tmp);
1162 free_trash_chunk(out);
1163 return 0; /* should come back */
1164}
1165
1166/* parsing function for 'show ssl cert [certfile]' */
1167static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
1168{
1169 struct ckch_store *ckchs;
1170
1171 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1172 return cli_err(appctx, "Can't allocate memory!\n");
1173
1174 /* The operations on the CKCH architecture are locked so we can
1175 * manipulate ckch_store and ckch_inst */
1176 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1177 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
1178
1179 /* check if there is a certificate to lookup */
1180 if (*args[3]) {
1181 if (*args[3] == '*') {
1182 if (!ckchs_transaction.new_ckchs)
1183 goto error;
1184
1185 ckchs = ckchs_transaction.new_ckchs;
1186
1187 if (strcmp(args[3] + 1, ckchs->path))
1188 goto error;
1189
1190 } else {
1191 if ((ckchs = ckchs_lookup(args[3])) == NULL)
1192 goto error;
1193
1194 }
1195
1196 if (ckchs->multi)
1197 goto error;
1198
1199 appctx->ctx.cli.p0 = ckchs;
1200 /* use the IO handler that shows details */
1201 appctx->io_handler = cli_io_handler_show_cert_detail;
1202 }
1203
1204 return 0;
1205
1206error:
1207 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1208 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
1209}
1210
1211/* release function of the `set ssl cert' command, free things and unlock the spinlock */
1212static void cli_release_commit_cert(struct appctx *appctx)
1213{
1214 struct ckch_store *new_ckchs;
1215
1216 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1217
1218 if (appctx->st2 != SETCERT_ST_FIN) {
1219 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
1220 new_ckchs = appctx->ctx.ssl.new_ckchs;
1221
1222 /* if the allocation failed, we need to free everything from the temporary list */
1223 ckch_store_free(new_ckchs);
1224 }
1225}
1226
1227/*
1228 * This function tries to create the new ckch_inst and their SNIs
1229 */
1230static int cli_io_handler_commit_cert(struct appctx *appctx)
1231{
1232 struct stream_interface *si = appctx->owner;
1233 int y = 0;
1234 char *err = NULL;
1235 int errcode = 0;
1236 struct ckch_store *old_ckchs, *new_ckchs = NULL;
1237 struct ckch_inst *ckchi, *ckchis;
1238 struct buffer *trash = alloc_trash_chunk();
1239 struct sni_ctx *sc0, *sc0s;
1240 struct crtlist_entry *entry;
1241
1242 if (trash == NULL)
1243 goto error;
1244
1245 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1246 goto error;
1247
1248 while (1) {
1249 switch (appctx->st2) {
1250 case SETCERT_ST_INIT:
1251 /* This state just print the update message */
1252 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
1253 if (ci_putchk(si_ic(si), trash) == -1) {
1254 si_rx_room_blk(si);
1255 goto yield;
1256 }
1257 appctx->st2 = SETCERT_ST_GEN;
1258 /* fallthrough */
1259 case SETCERT_ST_GEN:
1260 /*
1261 * This state generates the ckch instances with their
1262 * sni_ctxs and SSL_CTX.
1263 *
1264 * Since the SSL_CTX generation can be CPU consumer, we
1265 * yield every 10 instances.
1266 */
1267
1268 old_ckchs = appctx->ctx.ssl.old_ckchs;
1269 new_ckchs = appctx->ctx.ssl.new_ckchs;
1270
1271 if (!new_ckchs)
1272 continue;
1273
1274 /* get the next ckchi to regenerate */
1275 ckchi = appctx->ctx.ssl.next_ckchi;
1276 /* we didn't start yet, set it to the first elem */
1277 if (ckchi == NULL)
1278 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
1279
1280 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
1281 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
1282 struct ckch_inst *new_inst;
1283 char **sni_filter = NULL;
1284 int fcount = 0;
1285
1286 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
1287 if (y >= 10) {
1288 /* save the next ckchi to compute */
1289 appctx->ctx.ssl.next_ckchi = ckchi;
1290 goto yield;
1291 }
1292
1293 if (ckchi->crtlist_entry) {
1294 sni_filter = ckchi->crtlist_entry->filters;
1295 fcount = ckchi->crtlist_entry->fcount;
1296 }
1297
William Lallemand95fefa12020-09-09 12:01:33 +02001298 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 +02001299
1300 if (errcode & ERR_CODE)
1301 goto error;
1302
1303 /* if the previous ckchi was used as the default */
1304 if (ckchi->is_default)
1305 new_inst->is_default = 1;
1306
1307 /* we need to initialize the SSL_CTX generated */
1308 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
1309 list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) {
1310 if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
1311 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
1312 if (errcode & ERR_CODE)
1313 goto error;
1314 }
1315 }
1316
1317
1318 /* display one dot per new instance */
1319 chunk_appendf(trash, ".");
1320 /* link the new ckch_inst to the duplicate */
1321 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
1322 y++;
1323 }
1324 appctx->st2 = SETCERT_ST_INSERT;
1325 /* fallthrough */
1326 case SETCERT_ST_INSERT:
1327 /* The generation is finished, we can insert everything */
1328
1329 old_ckchs = appctx->ctx.ssl.old_ckchs;
1330 new_ckchs = appctx->ctx.ssl.new_ckchs;
1331
1332 if (!new_ckchs)
1333 continue;
1334
1335 /* get the list of crtlist_entry in the old store, and update the pointers to the store */
1336 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
1337 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
1338 ebpt_delete(&entry->node);
1339 /* change the ptr and reinsert the node */
1340 entry->node.key = new_ckchs;
1341 ebpt_insert(&entry->crtlist->entries, &entry->node);
1342 }
1343
1344 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
1345 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
1346 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
1347 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
1348 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
1349 }
1350
1351 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
1352 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
1353 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
1354
1355 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
1356 ckch_inst_free(ckchi);
1357 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
1358 }
1359
1360 /* Replace the old ckchs by the new one */
1361 ckch_store_free(old_ckchs);
1362 ebst_insert(&ckchs_tree, &new_ckchs->node);
1363 appctx->st2 = SETCERT_ST_FIN;
1364 /* fallthrough */
1365 case SETCERT_ST_FIN:
1366 /* we achieved the transaction, we can set everything to NULL */
1367 free(ckchs_transaction.path);
1368 ckchs_transaction.path = NULL;
1369 ckchs_transaction.new_ckchs = NULL;
1370 ckchs_transaction.old_ckchs = NULL;
1371 goto end;
1372 }
1373 }
1374end:
1375
1376 chunk_appendf(trash, "\n");
1377 if (errcode & ERR_WARN)
1378 chunk_appendf(trash, "%s", err);
1379 chunk_appendf(trash, "Success!\n");
1380 if (ci_putchk(si_ic(si), trash) == -1)
1381 si_rx_room_blk(si);
1382 free_trash_chunk(trash);
1383 /* success: call the release function and don't come back */
1384 return 1;
1385yield:
1386 /* store the state */
1387 if (ci_putchk(si_ic(si), trash) == -1)
1388 si_rx_room_blk(si);
1389 free_trash_chunk(trash);
1390 si_rx_endp_more(si); /* let's come back later */
1391 return 0; /* should come back */
1392
1393error:
1394 /* spin unlock and free are done in the release function */
1395 if (trash) {
1396 chunk_appendf(trash, "\n%sFailed!\n", err);
1397 if (ci_putchk(si_ic(si), trash) == -1)
1398 si_rx_room_blk(si);
1399 free_trash_chunk(trash);
1400 }
1401 /* error: call the release function and don't come back */
1402 return 1;
1403}
1404
1405/*
1406 * Parsing function of 'commit ssl cert'
1407 */
1408static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
1409{
1410 char *err = NULL;
1411
1412 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1413 return 1;
1414
1415 if (!*args[3])
1416 return cli_err(appctx, "'commit ssl cert expects a filename\n");
1417
1418 /* The operations on the CKCH architecture are locked so we can
1419 * manipulate ckch_store and ckch_inst */
1420 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1421 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
1422
1423 if (!ckchs_transaction.path) {
1424 memprintf(&err, "No ongoing transaction! !\n");
1425 goto error;
1426 }
1427
1428 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
1429 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
1430 goto error;
1431 }
1432
1433#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
1434 if (ckchs_transaction.new_ckchs->multi) {
1435 int n;
1436
1437 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemanda9419522020-06-24 16:26:41 +02001438 /* if a certificate is here, a private key must be here too */
1439 if (ckchs_transaction.new_ckchs->ckch[n].cert && !ckchs_transaction.new_ckchs->ckch[n].key) {
1440 memprintf(&err, "The transaction must contain at least a certificate and a private key!\n");
1441 goto error;
1442 }
1443
William Lallemandda8584c2020-05-14 10:14:37 +02001444 if (ckchs_transaction.new_ckchs->ckch[n].cert && !X509_check_private_key(ckchs_transaction.new_ckchs->ckch[n].cert, ckchs_transaction.new_ckchs->ckch[n].key)) {
1445 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
1446 goto error;
1447 }
1448 }
1449 } else
1450#endif
1451 {
William Lallemanda9419522020-06-24 16:26:41 +02001452 /* if a certificate is here, a private key must be here too */
1453 if (ckchs_transaction.new_ckchs->ckch->cert && !ckchs_transaction.new_ckchs->ckch->key) {
1454 memprintf(&err, "The transaction must contain at least a certificate and a private key!\n");
1455 goto error;
1456 }
1457
William Lallemandda8584c2020-05-14 10:14:37 +02001458 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
1459 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
1460 goto error;
1461 }
1462 }
1463
1464 /* init the appctx structure */
1465 appctx->st2 = SETCERT_ST_INIT;
1466 appctx->ctx.ssl.next_ckchi = NULL;
1467 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
1468 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
1469
1470 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
1471 return 0;
1472
1473error:
1474
1475 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1476 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
1477
1478 return cli_dynerr(appctx, err);
1479}
1480
1481
1482
1483
1484/*
1485 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
1486 */
1487static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
1488{
1489 struct ckch_store *new_ckchs = NULL;
1490 struct ckch_store *old_ckchs = NULL;
1491 char *err = NULL;
1492 int i;
William Lallemandda8584c2020-05-14 10:14:37 +02001493 int errcode = 0;
1494 char *end;
1495 int type = CERT_TYPE_PEM;
1496 struct cert_key_and_chain *ckch;
1497 struct buffer *buf;
1498
1499 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1500 return 1;
1501
William Lallemandda8584c2020-05-14 10:14:37 +02001502 if (!*args[3] || !payload)
1503 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
1504
1505 /* The operations on the CKCH architecture are locked so we can
1506 * manipulate ckch_store and ckch_inst */
1507 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1508 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
1509
William Lallemande5ff4ad2020-06-08 09:40:37 +02001510 if ((buf = alloc_trash_chunk()) == NULL)
1511 return cli_err(appctx, "Can't allocate memory\n");
1512
William Lallemandda8584c2020-05-14 10:14:37 +02001513 if (!chunk_strcpy(buf, args[3])) {
1514 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
1515 errcode |= ERR_ALERT | ERR_FATAL;
1516 goto end;
1517 }
1518
1519 /* check which type of file we want to update */
1520 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
1521 end = strrchr(buf->area, '.');
1522 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
1523 *end = '\0';
1524 type = cert_exts[i].type;
1525 break;
1526 }
1527 }
1528
1529 appctx->ctx.ssl.old_ckchs = NULL;
1530 appctx->ctx.ssl.new_ckchs = NULL;
1531
1532 /* if there is an ongoing transaction */
1533 if (ckchs_transaction.path) {
William Lallemandda8584c2020-05-14 10:14:37 +02001534 /* if there is an ongoing transaction, check if this is the same file */
1535 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
1536 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
1537 errcode |= ERR_ALERT | ERR_FATAL;
1538 goto end;
1539 }
1540
1541 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
1542
1543 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02001544
William Lallemand95fefa12020-09-09 12:01:33 +02001545 /* lookup for the certificate in the tree */
1546 appctx->ctx.ssl.old_ckchs = ckchs_lookup(buf->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001547 }
1548
1549 if (!appctx->ctx.ssl.old_ckchs) {
1550 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
1551 err ? err : "");
1552 errcode |= ERR_ALERT | ERR_FATAL;
1553 goto end;
1554 }
1555
1556 if (!appctx->ctx.ssl.path) {
1557 /* this is a new transaction, set the path of the transaction */
1558 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
1559 if (!appctx->ctx.ssl.path) {
1560 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
1561 errcode |= ERR_ALERT | ERR_FATAL;
1562 goto end;
1563 }
1564 }
1565
1566 old_ckchs = appctx->ctx.ssl.old_ckchs;
1567
1568 /* duplicate the ckch store */
1569 new_ckchs = ckchs_dup(old_ckchs);
1570 if (!new_ckchs) {
1571 memprintf(&err, "%sCannot allocate memory!\n",
1572 err ? err : "");
1573 errcode |= ERR_ALERT | ERR_FATAL;
1574 goto end;
1575 }
1576
William Lallemand95fefa12020-09-09 12:01:33 +02001577 ckch = new_ckchs->ckch;
William Lallemandda8584c2020-05-14 10:14:37 +02001578
1579 /* appply the change on the duplicate */
1580 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
1581 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
1582 errcode |= ERR_ALERT | ERR_FATAL;
1583 goto end;
1584 }
1585
1586 appctx->ctx.ssl.new_ckchs = new_ckchs;
1587
1588 /* we succeed, we can save the ckchs in the transaction */
1589
1590 /* if there wasn't a transaction, update the old ckchs */
1591 if (!ckchs_transaction.old_ckchs) {
1592 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
1593 ckchs_transaction.path = appctx->ctx.ssl.path;
1594 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
1595 } else {
1596 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
1597
1598 }
1599
1600 /* free the previous ckchs if there was a transaction */
1601 ckch_store_free(ckchs_transaction.new_ckchs);
1602
1603 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
1604
1605
1606 /* creates the SNI ctxs later in the IO handler */
1607
1608end:
1609 free_trash_chunk(buf);
1610
1611 if (errcode & ERR_CODE) {
1612
1613 ckch_store_free(appctx->ctx.ssl.new_ckchs);
1614 appctx->ctx.ssl.new_ckchs = NULL;
1615
1616 appctx->ctx.ssl.old_ckchs = NULL;
1617
1618 free(appctx->ctx.ssl.path);
1619 appctx->ctx.ssl.path = NULL;
1620
1621 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1622 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
1623 } else {
1624
1625 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1626 return cli_dynmsg(appctx, LOG_NOTICE, err);
1627 }
1628 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
1629}
1630
1631/* parsing function of 'abort ssl cert' */
1632static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
1633{
1634 char *err = NULL;
1635
1636 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1637 return 1;
1638
1639 if (!*args[3])
1640 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
1641
1642 /* The operations on the CKCH architecture are locked so we can
1643 * manipulate ckch_store and ckch_inst */
1644 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1645 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
1646
1647 if (!ckchs_transaction.path) {
1648 memprintf(&err, "No ongoing transaction!\n");
1649 goto error;
1650 }
1651
1652 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
1653 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
1654 goto error;
1655 }
1656
1657 /* Only free the ckchs there, because the SNI and instances were not generated yet */
1658 ckch_store_free(ckchs_transaction.new_ckchs);
1659 ckchs_transaction.new_ckchs = NULL;
1660 ckch_store_free(ckchs_transaction.old_ckchs);
1661 ckchs_transaction.old_ckchs = NULL;
1662 free(ckchs_transaction.path);
1663 ckchs_transaction.path = NULL;
1664
1665 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1666
1667 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
1668 return cli_dynmsg(appctx, LOG_NOTICE, err);
1669
1670error:
1671 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1672
1673 return cli_dynerr(appctx, err);
1674}
1675
1676/* parsing function of 'new ssl cert' */
1677static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
1678{
1679 struct ckch_store *store;
1680 char *err = NULL;
1681 char *path;
1682
1683 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1684 return 1;
1685
1686 if (!*args[3])
1687 return cli_err(appctx, "'new ssl cert' expects a filename\n");
1688
1689 path = args[3];
1690
1691 /* The operations on the CKCH architecture are locked so we can
1692 * manipulate ckch_store and ckch_inst */
1693 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1694 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
1695
1696 store = ckchs_lookup(path);
1697 if (store != NULL) {
1698 memprintf(&err, "Certificate '%s' already exists!\n", path);
1699 store = NULL; /* we don't want to free it */
1700 goto error;
1701 }
1702 /* we won't support multi-certificate bundle here */
William Lallemandbd8e6ed2020-09-16 16:08:08 +02001703 store = ckch_store_new(path);
William Lallemandda8584c2020-05-14 10:14:37 +02001704 if (!store) {
1705 memprintf(&err, "unable to allocate memory.\n");
1706 goto error;
1707 }
1708
1709 /* insert into the ckchs tree */
1710 ebst_insert(&ckchs_tree, &store->node);
1711 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
1712
1713 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1714 return cli_dynmsg(appctx, LOG_NOTICE, err);
1715error:
1716 free(store);
1717 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1718 return cli_dynerr(appctx, err);
1719}
1720
1721/* parsing function of 'del ssl cert' */
1722static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
1723{
1724 struct ckch_store *store;
1725 char *err = NULL;
1726 char *filename;
1727
1728 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1729 return 1;
1730
1731 if (!*args[3])
1732 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
1733
1734 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1735 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
1736
1737 filename = args[3];
1738
1739 store = ckchs_lookup(filename);
1740 if (store == NULL) {
1741 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
1742 goto error;
1743 }
1744 if (!LIST_ISEMPTY(&store->ckch_inst)) {
1745 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
1746 goto error;
1747 }
1748
1749 ebmb_delete(&store->node);
1750 ckch_store_free(store);
1751
1752 memprintf(&err, "Certificate '%s' deleted!\n", filename);
1753
1754 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1755 return cli_dynmsg(appctx, LOG_NOTICE, err);
1756
1757error:
1758 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
1759 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1760 return cli_dynerr(appctx, err);
1761}
1762
William Lallemandee8530c2020-06-23 18:19:42 +02001763void ckch_deinit()
1764{
1765 struct eb_node *node, *next;
1766 struct ckch_store *store;
1767
1768 node = eb_first(&ckchs_tree);
1769 while (node) {
1770 next = eb_next(node);
1771 store = ebmb_entry(node, struct ckch_store, node);
1772 ckch_store_free(store);
1773 node = next;
1774 }
1775}
William Lallemandda8584c2020-05-14 10:14:37 +02001776
1777/* register cli keywords */
1778static struct cli_kw_list cli_kws = {{ },{
1779 { { "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 },
1780 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
1781 { { "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 },
1782 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
1783 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
1784 { { "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 },
1785 { { NULL }, NULL, NULL, NULL }
1786}};
1787
1788INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
1789