blob: 443c12cacfaca7e80b3cedf696bfaa50f72fede3 [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 Tarreau74f24562021-10-06 17:54:12 +020025#include <import/ebpttree.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <import/ebsttree.h>
27
Willy Tarreau8d366972020-05-27 16:10:29 +020028#include <haproxy/base64.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020029#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020030#include <haproxy/cli.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020031#include <haproxy/errors.h>
Willy Tarreau47d7f902020-06-04 14:25:47 +020032#include <haproxy/ssl_ckch.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020033#include <haproxy/ssl_sock.h>
Willy Tarreaub2bd8652020-06-04 14:21:22 +020034#include <haproxy/ssl_utils.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020035#include <haproxy/stream_interface.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020036#include <haproxy/tools.h>
William Lallemand03c331c2020-05-13 10:10:01 +020037
William Lallemandda8584c2020-05-14 10:14:37 +020038/* Uncommitted CKCH transaction */
39
40static struct {
41 struct ckch_store *new_ckchs;
42 struct ckch_store *old_ckchs;
43 char *path;
44} ckchs_transaction;
45
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +010046/* Uncommitted CA file transaction */
47
48static struct {
49 struct cafile_entry *old_cafile_entry;
50 struct cafile_entry *new_cafile_entry;
51 char *path;
52} cafile_transaction;
William Lallemandda8584c2020-05-14 10:14:37 +020053
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +020054/* Uncommitted CRL file transaction */
55
56static struct {
57 struct cafile_entry *old_crlfile_entry;
58 struct cafile_entry *new_crlfile_entry;
59 char *path;
60} crlfile_transaction;
61
William Lallemand03c331c2020-05-13 10:10:01 +020062
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +010063
William Lallemand03c331c2020-05-13 10:10:01 +020064/******************** cert_key_and_chain functions *************************
65 * These are the functions that fills a cert_key_and_chain structure. For the
66 * functions filling a SSL_CTX from a cert_key_and_chain, see ssl_sock.c
67 */
68
69/*
70 * Try to parse Signed Certificate Timestamp List structure. This function
71 * makes only basic test if the data seems like SCTL. No signature validation
72 * is performed.
73 */
74static int ssl_sock_parse_sctl(struct buffer *sctl)
75{
76 int ret = 1;
77 int len, pos, sct_len;
78 unsigned char *data;
79
80 if (sctl->data < 2)
81 goto out;
82
83 data = (unsigned char *) sctl->area;
84 len = (data[0] << 8) | data[1];
85
86 if (len + 2 != sctl->data)
87 goto out;
88
89 data = data + 2;
90 pos = 0;
91 while (pos < len) {
92 if (len - pos < 2)
93 goto out;
94
95 sct_len = (data[pos] << 8) | data[pos + 1];
96 if (pos + sct_len + 2 > len)
97 goto out;
98
99 pos += sct_len + 2;
100 }
101
102 ret = 0;
103
104out:
105 return ret;
106}
107
108/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
109 * It fills the ckch->sctl buffer
110 * return 0 on success or != 0 on failure */
111int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err)
112{
113 int fd = -1;
114 int r = 0;
115 int ret = 1;
116 struct buffer tmp;
117 struct buffer *src;
118 struct buffer *sctl;
119
120 if (buf) {
William Lallemand8d673942021-01-27 14:58:51 +0100121 chunk_initstr(&tmp, buf);
William Lallemand03c331c2020-05-13 10:10:01 +0200122 src = &tmp;
123 } else {
124 fd = open(sctl_path, O_RDONLY);
125 if (fd == -1)
126 goto end;
127
128 trash.data = 0;
129 while (trash.data < trash.size) {
130 r = read(fd, trash.area + trash.data, trash.size - trash.data);
131 if (r < 0) {
132 if (errno == EINTR)
133 continue;
134 goto end;
135 }
136 else if (r == 0) {
137 break;
138 }
139 trash.data += r;
140 }
141 src = &trash;
142 }
143
144 ret = ssl_sock_parse_sctl(src);
145 if (ret)
146 goto end;
147
148 sctl = calloc(1, sizeof(*sctl));
149 if (!chunk_dup(sctl, src)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100150 ha_free(&sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200151 goto end;
152 }
153 /* no error, fill ckch with new context, old context must be free */
154 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100155 ha_free(&ckch->sctl->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200156 free(ckch->sctl);
157 }
158 ckch->sctl = sctl;
159 ret = 0;
160end:
161 if (fd != -1)
162 close(fd);
163
164 return ret;
165}
166
167#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
168/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500169 * This function load the OCSP Response in DER format contained in file at
William Lallemand03c331c2020-05-13 10:10:01 +0200170 * path 'ocsp_path' or base64 in a buffer <buf>
171 *
172 * Returns 0 on success, 1 in error case.
173 */
174int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err)
175{
176 int fd = -1;
177 int r = 0;
178 int ret = 1;
179 struct buffer *ocsp_response;
180 struct buffer *src = NULL;
181
182 if (buf) {
183 int i, j;
184 /* if it's from a buffer it will be base64 */
185
186 /* remove \r and \n from the payload */
187 for (i = 0, j = 0; buf[i]; i++) {
188 if (buf[i] == '\r' || buf[i] == '\n')
189 continue;
190 buf[j++] = buf[i];
191 }
192 buf[j] = 0;
193
194 ret = base64dec(buf, j, trash.area, trash.size);
195 if (ret < 0) {
196 memprintf(err, "Error reading OCSP response in base64 format");
197 goto end;
198 }
199 trash.data = ret;
200 src = &trash;
201 } else {
202 fd = open(ocsp_path, O_RDONLY);
203 if (fd == -1) {
204 memprintf(err, "Error opening OCSP response file");
205 goto end;
206 }
207
208 trash.data = 0;
209 while (trash.data < trash.size) {
210 r = read(fd, trash.area + trash.data, trash.size - trash.data);
211 if (r < 0) {
212 if (errno == EINTR)
213 continue;
214
215 memprintf(err, "Error reading OCSP response from file");
216 goto end;
217 }
218 else if (r == 0) {
219 break;
220 }
221 trash.data += r;
222 }
223 close(fd);
224 fd = -1;
225 src = &trash;
226 }
227
228 ocsp_response = calloc(1, sizeof(*ocsp_response));
229 if (!chunk_dup(ocsp_response, src)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100230 ha_free(&ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200231 goto end;
232 }
233 /* no error, fill ckch with new context, old context must be free */
234 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100235 ha_free(&ckch->ocsp_response->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200236 free(ckch->ocsp_response);
237 }
238 ckch->ocsp_response = ocsp_response;
239 ret = 0;
240end:
241 if (fd != -1)
242 close(fd);
243
244 return ret;
245}
246#endif
247
248/*
249 * Try to load in a ckch every files related to a ckch.
250 * (PEM, sctl, ocsp, issuer etc.)
251 *
252 * This function is only used to load files during the configuration parsing,
253 * it is not used with the CLI.
254 *
255 * This allows us to carry the contents of the file without having to read the
256 * file multiple times. The caller must call
257 * ssl_sock_free_cert_key_and_chain_contents.
258 *
259 * returns:
260 * 0 on Success
261 * 1 on SSL Failure
262 */
263int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
264{
William Lallemand8e8581e2020-10-20 17:36:46 +0200265 struct buffer *fp = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200266 int ret = 1;
267
268 /* try to load the PEM */
269 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
270 goto end;
271 }
272
William Lallemand8e8581e2020-10-20 17:36:46 +0200273 fp = alloc_trash_chunk();
274 if (!fp) {
275 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
276 goto end;
277 }
278
279 if (!chunk_strcpy(fp, path) || (b_data(fp) > MAXPATHLEN)) {
280 memprintf(err, "%s '%s' filename too long'.\n",
281 err && *err ? *err : "", fp->area);
282 ret = 1;
283 goto end;
284 }
285
William Lallemand089c1382020-10-23 17:35:12 +0200286 /* remove the ".crt" extension */
William Lallemand8e8581e2020-10-20 17:36:46 +0200287 if (global_ssl.extra_files_noext) {
288 char *ext;
289
290 /* look for the extension */
291 if ((ext = strrchr(fp->area, '.'))) {
William Lallemand8e8581e2020-10-20 17:36:46 +0200292
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100293 if (strcmp(ext, ".crt") == 0) {
William Lallemand8e8581e2020-10-20 17:36:46 +0200294 *ext = '\0';
William Lallemand089c1382020-10-23 17:35:12 +0200295 fp->data = strlen(fp->area);
296 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200297 }
298
299 }
300
William Lallemand03c331c2020-05-13 10:10:01 +0200301 /* try to load an external private key if it wasn't in the PEM */
302 if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200303 struct stat st;
304
William Lallemand8e8581e2020-10-20 17:36:46 +0200305
306 if (!chunk_strcat(fp, ".key") || (b_data(fp) > MAXPATHLEN)) {
307 memprintf(err, "%s '%s' filename too long'.\n",
308 err && *err ? *err : "", fp->area);
309 ret = 1;
310 goto end;
311 }
312
313 if (stat(fp->area, &st) == 0) {
314 if (ssl_sock_load_key_into_ckch(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200315 memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n",
William Lallemand8e8581e2020-10-20 17:36:46 +0200316 err && *err ? *err : "", fp->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200317 goto end;
318 }
319 }
William Lallemand03c331c2020-05-13 10:10:01 +0200320
William Lallemand8e8581e2020-10-20 17:36:46 +0200321 if (ckch->key == NULL) {
322 memprintf(err, "%sNo Private Key found in '%s'.\n", err && *err ? *err : "", fp->area);
323 goto end;
324 }
325 /* remove the added extension */
326 *(fp->area + fp->data - strlen(".key")) = '\0';
327 b_sub(fp, strlen(".key"));
William Lallemand03c331c2020-05-13 10:10:01 +0200328 }
329
330 if (!X509_check_private_key(ckch->cert, ckch->key)) {
331 memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n",
332 err && *err ? *err : "", path);
333 goto end;
334 }
335
Ilya Shipitsinc47d6762021-02-13 11:45:33 +0500336#ifdef HAVE_SSL_SCTL
William Lallemand03c331c2020-05-13 10:10:01 +0200337 /* try to load the sctl file */
338 if (global_ssl.extra_files & SSL_GF_SCTL) {
William Lallemand03c331c2020-05-13 10:10:01 +0200339 struct stat st;
340
William Lallemand8e8581e2020-10-20 17:36:46 +0200341 if (!chunk_strcat(fp, ".sctl") || b_data(fp) > MAXPATHLEN) {
342 memprintf(err, "%s '%s' filename too long'.\n",
343 err && *err ? *err : "", fp->area);
344 ret = 1;
345 goto end;
346 }
347
348 if (stat(fp->area, &st) == 0) {
349 if (ssl_sock_load_sctl_from_file(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200350 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
William Lallemand8e8581e2020-10-20 17:36:46 +0200351 err && *err ? *err : "", fp->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200352 ret = 1;
353 goto end;
354 }
355 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200356 /* remove the added extension */
357 *(fp->area + fp->data - strlen(".sctl")) = '\0';
358 b_sub(fp, strlen(".sctl"));
William Lallemand03c331c2020-05-13 10:10:01 +0200359 }
360#endif
361
362 /* try to load an ocsp response file */
363 if (global_ssl.extra_files & SSL_GF_OCSP) {
William Lallemand03c331c2020-05-13 10:10:01 +0200364 struct stat st;
365
William Lallemand8e8581e2020-10-20 17:36:46 +0200366 if (!chunk_strcat(fp, ".ocsp") || b_data(fp) > MAXPATHLEN) {
367 memprintf(err, "%s '%s' filename too long'.\n",
368 err && *err ? *err : "", fp->area);
369 ret = 1;
370 goto end;
371 }
372
373 if (stat(fp->area, &st) == 0) {
374 if (ssl_sock_load_ocsp_response_from_file(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200375 ret = 1;
376 goto end;
377 }
378 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200379 /* remove the added extension */
380 *(fp->area + fp->data - strlen(".ocsp")) = '\0';
381 b_sub(fp, strlen(".ocsp"));
William Lallemand03c331c2020-05-13 10:10:01 +0200382 }
383
384#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
385 if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
386 /* if no issuer was found, try to load an issuer from the .issuer */
387 if (!ckch->ocsp_issuer) {
388 struct stat st;
William Lallemand8e8581e2020-10-20 17:36:46 +0200389
390 if (!chunk_strcat(fp, ".issuer") || b_data(fp) > MAXPATHLEN) {
391 memprintf(err, "%s '%s' filename too long'.\n",
392 err && *err ? *err : "", fp->area);
393 ret = 1;
394 goto end;
395 }
William Lallemand03c331c2020-05-13 10:10:01 +0200396
William Lallemand8e8581e2020-10-20 17:36:46 +0200397 if (stat(fp->area, &st) == 0) {
398 if (ssl_sock_load_issuer_file_into_ckch(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200399 ret = 1;
400 goto end;
401 }
402
403 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
404 memprintf(err, "%s '%s' is not an issuer'.\n",
William Lallemand8e8581e2020-10-20 17:36:46 +0200405 err && *err ? *err : "", fp->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200406 ret = 1;
407 goto end;
408 }
409 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200410 /* remove the added extension */
411 *(fp->area + fp->data - strlen(".issuer")) = '\0';
412 b_sub(fp, strlen(".issuer"));
William Lallemand03c331c2020-05-13 10:10:01 +0200413 }
414 }
415#endif
416
417 ret = 0;
418
419end:
420
421 ERR_clear_error();
422
423 /* Something went wrong in one of the reads */
424 if (ret != 0)
425 ssl_sock_free_cert_key_and_chain_contents(ckch);
426
William Lallemand8e8581e2020-10-20 17:36:46 +0200427 free_trash_chunk(fp);
428
William Lallemand03c331c2020-05-13 10:10:01 +0200429 return ret;
430}
431
432/*
433 * Try to load a private key file from a <path> or a buffer <buf>
434 *
435 * If it failed you should not attempt to use the ckch but free it.
436 *
437 * Return 0 on success or != 0 on failure
438 */
439int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
440{
441 BIO *in = NULL;
442 int ret = 1;
443 EVP_PKEY *key = NULL;
444
445 if (buf) {
446 /* reading from a buffer */
447 in = BIO_new_mem_buf(buf, -1);
448 if (in == NULL) {
449 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
450 goto end;
451 }
452
453 } else {
454 /* reading from a file */
455 in = BIO_new(BIO_s_file());
456 if (in == NULL)
457 goto end;
458
459 if (BIO_read_filename(in, path) <= 0)
460 goto end;
461 }
462
463 /* Read Private Key */
464 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
465 if (key == NULL) {
466 memprintf(err, "%sunable to load private key from file '%s'.\n",
467 err && *err ? *err : "", path);
468 goto end;
469 }
470
471 ret = 0;
472
473 SWAP(ckch->key, key);
474
475end:
476
477 ERR_clear_error();
478 if (in)
479 BIO_free(in);
480 if (key)
481 EVP_PKEY_free(key);
482
483 return ret;
484}
485
486/*
487 * Try to load a PEM file from a <path> or a buffer <buf>
488 * The PEM must contain at least a Certificate,
489 * It could contain a DH, a certificate chain and a PrivateKey.
490 *
491 * If it failed you should not attempt to use the ckch but free it.
492 *
493 * Return 0 on success or != 0 on failure
494 */
495int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
496{
497 BIO *in = NULL;
498 int ret = 1;
499 X509 *ca;
500 X509 *cert = NULL;
501 EVP_PKEY *key = NULL;
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100502 HASSL_DH *dh = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200503 STACK_OF(X509) *chain = NULL;
504
505 if (buf) {
506 /* reading from a buffer */
507 in = BIO_new_mem_buf(buf, -1);
508 if (in == NULL) {
509 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
510 goto end;
511 }
512
513 } else {
514 /* reading from a file */
515 in = BIO_new(BIO_s_file());
516 if (in == NULL) {
517 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
518 goto end;
519 }
520
521 if (BIO_read_filename(in, path) <= 0) {
522 memprintf(err, "%scannot open the file '%s'.\n",
523 err && *err ? *err : "", path);
524 goto end;
525 }
526 }
527
528 /* Read Private Key */
529 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
530 /* no need to check for errors here, because the private key could be loaded later */
531
532#ifndef OPENSSL_NO_DH
533 /* Seek back to beginning of file */
534 if (BIO_reset(in) == -1) {
535 memprintf(err, "%san error occurred while reading the file '%s'.\n",
536 err && *err ? *err : "", path);
537 goto end;
538 }
539
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100540 dh = ssl_sock_get_dh_from_bio(in);
541 ERR_clear_error();
William Lallemand03c331c2020-05-13 10:10:01 +0200542 /* no need to return an error there, dh is not mandatory */
543#endif
544
545 /* Seek back to beginning of file */
546 if (BIO_reset(in) == -1) {
547 memprintf(err, "%san error occurred while reading the file '%s'.\n",
548 err && *err ? *err : "", path);
549 goto end;
550 }
551
552 /* Read Certificate */
553 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
554 if (cert == NULL) {
555 memprintf(err, "%sunable to load certificate from file '%s'.\n",
556 err && *err ? *err : "", path);
557 goto end;
558 }
559
560 /* Look for a Certificate Chain */
561 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
562 if (chain == NULL)
563 chain = sk_X509_new_null();
564 if (!sk_X509_push(chain, ca)) {
565 X509_free(ca);
566 goto end;
567 }
568 }
569
570 ret = ERR_get_error();
571 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
572 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
573 err && *err ? *err : "", path);
574 goto end;
575 }
576
577 /* once it loaded the PEM, it should remove everything else in the ckch */
578 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100579 ha_free(&ckch->ocsp_response->area);
580 ha_free(&ckch->ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200581 }
582
583 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100584 ha_free(&ckch->sctl->area);
585 ha_free(&ckch->sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200586 }
587
588 if (ckch->ocsp_issuer) {
589 X509_free(ckch->ocsp_issuer);
590 ckch->ocsp_issuer = NULL;
591 }
592
593 /* no error, fill ckch with new context, old context will be free at end: */
594 SWAP(ckch->key, key);
595 SWAP(ckch->dh, dh);
596 SWAP(ckch->cert, cert);
597 SWAP(ckch->chain, chain);
598
599 ret = 0;
600
601end:
602
603 ERR_clear_error();
604 if (in)
605 BIO_free(in);
606 if (key)
607 EVP_PKEY_free(key);
608 if (dh)
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100609 HASSL_DH_free(dh);
William Lallemand03c331c2020-05-13 10:10:01 +0200610 if (cert)
611 X509_free(cert);
612 if (chain)
613 sk_X509_pop_free(chain, X509_free);
614
615 return ret;
616}
617
618/* Frees the contents of a cert_key_and_chain
619 */
620void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
621{
622 if (!ckch)
623 return;
624
625 /* Free the certificate and set pointer to NULL */
626 if (ckch->cert)
627 X509_free(ckch->cert);
628 ckch->cert = NULL;
629
630 /* Free the key and set pointer to NULL */
631 if (ckch->key)
632 EVP_PKEY_free(ckch->key);
633 ckch->key = NULL;
634
635 /* Free each certificate in the chain */
636 if (ckch->chain)
637 sk_X509_pop_free(ckch->chain, X509_free);
638 ckch->chain = NULL;
639
640 if (ckch->dh)
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100641 HASSL_DH_free(ckch->dh);
William Lallemand03c331c2020-05-13 10:10:01 +0200642 ckch->dh = NULL;
643
644 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100645 ha_free(&ckch->sctl->area);
646 ha_free(&ckch->sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200647 }
648
649 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100650 ha_free(&ckch->ocsp_response->area);
651 ha_free(&ckch->ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200652 }
653
654 if (ckch->ocsp_issuer)
655 X509_free(ckch->ocsp_issuer);
656 ckch->ocsp_issuer = NULL;
657}
658
659/*
660 *
661 * This function copy a cert_key_and_chain in memory
662 *
663 * It's used to try to apply changes on a ckch before committing them, because
664 * most of the time it's not possible to revert those changes
665 *
666 * Return a the dst or NULL
667 */
668struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
669 struct cert_key_and_chain *dst)
670{
William Lallemand6c096142021-02-23 14:45:45 +0100671 if (!src || !dst)
672 return NULL;
673
William Lallemand03c331c2020-05-13 10:10:01 +0200674 if (src->cert) {
675 dst->cert = src->cert;
676 X509_up_ref(src->cert);
677 }
678
679 if (src->key) {
680 dst->key = src->key;
681 EVP_PKEY_up_ref(src->key);
682 }
683
684 if (src->chain) {
685 dst->chain = X509_chain_up_ref(src->chain);
686 }
687
688 if (src->dh) {
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100689 HASSL_DH_up_ref(src->dh);
William Lallemand03c331c2020-05-13 10:10:01 +0200690 dst->dh = src->dh;
691 }
692
693 if (src->sctl) {
694 struct buffer *sctl;
695
696 sctl = calloc(1, sizeof(*sctl));
697 if (!chunk_dup(sctl, src->sctl)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100698 ha_free(&sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200699 goto error;
700 }
701 dst->sctl = sctl;
702 }
703
704 if (src->ocsp_response) {
705 struct buffer *ocsp_response;
706
707 ocsp_response = calloc(1, sizeof(*ocsp_response));
708 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100709 ha_free(&ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200710 goto error;
711 }
712 dst->ocsp_response = ocsp_response;
713 }
714
715 if (src->ocsp_issuer) {
716 X509_up_ref(src->ocsp_issuer);
717 dst->ocsp_issuer = src->ocsp_issuer;
718 }
719
720 return dst;
721
722error:
723
724 /* free everything */
725 ssl_sock_free_cert_key_and_chain_contents(dst);
726
727 return NULL;
728}
729
730/*
731 * return 0 on success or != 0 on failure
732 */
733int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
734{
735 int ret = 1;
736 BIO *in = NULL;
737 X509 *issuer;
738
739 if (buf) {
740 /* reading from a buffer */
741 in = BIO_new_mem_buf(buf, -1);
742 if (in == NULL) {
743 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
744 goto end;
745 }
746
747 } else {
748 /* reading from a file */
749 in = BIO_new(BIO_s_file());
750 if (in == NULL)
751 goto end;
752
753 if (BIO_read_filename(in, path) <= 0)
754 goto end;
755 }
756
757 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
758 if (!issuer) {
759 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
760 err && *err ? *err : "", path);
761 goto end;
762 }
763 /* no error, fill ckch with new context, old context must be free */
764 if (ckch->ocsp_issuer)
765 X509_free(ckch->ocsp_issuer);
766 ckch->ocsp_issuer = issuer;
767 ret = 0;
768
769end:
770
771 ERR_clear_error();
772 if (in)
773 BIO_free(in);
774
775 return ret;
776}
777
778/******************** ckch_store functions ***********************************
779 * The ckch_store is a structure used to cache and index the SSL files used in
780 * configuration
781 */
782
783/*
784 * Free a ckch_store, its ckch, its instances and remove it from the ebtree
785 */
786void ckch_store_free(struct ckch_store *store)
787{
788 struct ckch_inst *inst, *inst_s;
789
790 if (!store)
791 return;
792
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200793 ssl_sock_free_cert_key_and_chain_contents(store->ckch);
William Lallemand03c331c2020-05-13 10:10:01 +0200794
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100795 ha_free(&store->ckch);
William Lallemand03c331c2020-05-13 10:10:01 +0200796
797 list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) {
798 ckch_inst_free(inst);
799 }
800 ebmb_delete(&store->node);
801 free(store);
802}
803
804/*
805 * create and initialize a ckch_store
806 * <path> is the key name
807 * <nmemb> is the number of store->ckch objects to allocate
808 *
809 * Return a ckch_store or NULL upon failure.
810 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200811struct ckch_store *ckch_store_new(const char *filename)
William Lallemand03c331c2020-05-13 10:10:01 +0200812{
813 struct ckch_store *store;
814 int pathlen;
815
816 pathlen = strlen(filename);
817 store = calloc(1, sizeof(*store) + pathlen + 1);
818 if (!store)
819 return NULL;
820
William Lallemand03c331c2020-05-13 10:10:01 +0200821 memcpy(store->path, filename, pathlen + 1);
822
823 LIST_INIT(&store->ckch_inst);
824 LIST_INIT(&store->crtlist_entry);
825
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200826 store->ckch = calloc(1, sizeof(*store->ckch));
William Lallemand03c331c2020-05-13 10:10:01 +0200827 if (!store->ckch)
828 goto error;
829
830 return store;
831error:
832 ckch_store_free(store);
833 return NULL;
834}
835
836/* allocate and duplicate a ckch_store
837 * Return a new ckch_store or NULL */
838struct ckch_store *ckchs_dup(const struct ckch_store *src)
839{
840 struct ckch_store *dst;
841
William Lallemand6c096142021-02-23 14:45:45 +0100842 if (!src)
843 return NULL;
844
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200845 dst = ckch_store_new(src->path);
Eric Salama6ac61e32021-02-23 16:50:57 +0100846 if (!dst)
847 return NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200848
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200849 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
850 goto error;
William Lallemand03c331c2020-05-13 10:10:01 +0200851
852 return dst;
853
854error:
855 ckch_store_free(dst);
856
857 return NULL;
858}
859
860/*
861 * lookup a path into the ckchs tree.
862 */
863struct ckch_store *ckchs_lookup(char *path)
864{
865 struct ebmb_node *eb;
866
867 eb = ebst_lookup(&ckchs_tree, path);
868 if (!eb)
869 return NULL;
870
871 return ebmb_entry(eb, struct ckch_store, node);
872}
873
874/*
875 * This function allocate a ckch_store and populate it with certificates from files.
876 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200877struct ckch_store *ckchs_load_cert_file(char *path, char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200878{
879 struct ckch_store *ckchs;
880
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200881 ckchs = ckch_store_new(path);
William Lallemand03c331c2020-05-13 10:10:01 +0200882 if (!ckchs) {
883 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
884 goto end;
885 }
William Lallemand03c331c2020-05-13 10:10:01 +0200886
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200887 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
888 goto end;
William Lallemand03c331c2020-05-13 10:10:01 +0200889
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200890 /* insert into the ckchs tree */
891 memcpy(ckchs->path, path, strlen(path) + 1);
892 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand03c331c2020-05-13 10:10:01 +0200893 return ckchs;
894
895end:
896 ckch_store_free(ckchs);
897
898 return NULL;
899}
900
William Lallemandfa1d8b42020-05-13 15:46:10 +0200901
902/******************** ckch_inst functions ******************************/
903
904/* unlink a ckch_inst, free all SNIs, free the ckch_inst */
905/* The caller must use the lock of the bind_conf if used with inserted SNIs */
906void ckch_inst_free(struct ckch_inst *inst)
907{
908 struct sni_ctx *sni, *sni_s;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100909 struct ckch_inst_link_ref *link_ref, *link_ref_s;
William Lallemandfa1d8b42020-05-13 15:46:10 +0200910
911 if (inst == NULL)
912 return;
913
914 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
915 SSL_CTX_free(sni->ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +0200916 LIST_DELETE(&sni->by_ckch_inst);
William Lallemandfa1d8b42020-05-13 15:46:10 +0200917 ebmb_delete(&sni->name);
918 free(sni);
919 }
Remi Tricot-Le Bretonf3eedfe2021-01-25 17:19:44 +0100920 SSL_CTX_free(inst->ctx);
921 inst->ctx = NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +0200922 LIST_DELETE(&inst->by_ckchs);
923 LIST_DELETE(&inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100924
925 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
926 LIST_DELETE(&link_ref->link->list);
927 LIST_DELETE(&link_ref->list);
928 free(link_ref);
929 }
930
William Lallemandfa1d8b42020-05-13 15:46:10 +0200931 free(inst);
932}
933
934/* Alloc and init a ckch_inst */
935struct ckch_inst *ckch_inst_new()
936{
937 struct ckch_inst *ckch_inst;
938
939 ckch_inst = calloc(1, sizeof *ckch_inst);
940 if (!ckch_inst)
941 return NULL;
942
943 LIST_INIT(&ckch_inst->sni_ctx);
944 LIST_INIT(&ckch_inst->by_ckchs);
945 LIST_INIT(&ckch_inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100946 LIST_INIT(&ckch_inst->cafile_link_refs);
William Lallemandfa1d8b42020-05-13 15:46:10 +0200947
948 return ckch_inst;
949}
950
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200951
952/******************** ssl_store functions ******************************/
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100953struct eb_root cafile_tree = EB_ROOT;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200954
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100955/*
956 * Returns the cafile_entry found in the cafile_tree indexed by the path 'path'.
957 * If 'oldest_entry' is 1, returns the "original" cafile_entry (since
958 * during a set cafile/commit cafile cycle there might be two entries for any
959 * given path, the original one and the new one set via the CLI but not
960 * committed yet).
961 */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100962struct cafile_entry *ssl_store_get_cafile_entry(char *path, int oldest_entry)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200963{
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100964 struct cafile_entry *ca_e = NULL;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200965 struct ebmb_node *eb;
966
967 eb = ebst_lookup(&cafile_tree, path);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100968 while (eb) {
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200969 ca_e = ebmb_entry(eb, struct cafile_entry, node);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100970 /* The ebst_lookup in a tree that has duplicates returns the
971 * oldest entry first. If we want the latest entry, we need to
972 * iterate over all the duplicates until we find the last one
973 * (in our case there should never be more than two entries for
974 * any given path). */
975 if (oldest_entry)
976 return ca_e;
977 eb = ebmb_next_dup(eb);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200978 }
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100979 return ca_e;
980}
981
Remi Tricot-Le Breton38c999b2021-02-23 16:28:43 +0100982int ssl_store_add_uncommitted_cafile_entry(struct cafile_entry *entry)
983{
984 return (ebst_insert(&cafile_tree, &entry->node) != &entry->node);
985}
986
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100987X509_STORE* ssl_store_get0_locations_file(char *path)
988{
989 struct cafile_entry *ca_e = ssl_store_get_cafile_entry(path, 0);
990
991 if (ca_e)
992 return ca_e->ca_store;
993
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200994 return NULL;
995}
996
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +0100997/* Create a cafile_entry object, without adding it to the cafile_tree. */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +0200998struct cafile_entry *ssl_store_create_cafile_entry(char *path, X509_STORE *store, enum cafile_type type)
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +0100999{
1000 struct cafile_entry *ca_e;
1001 int pathlen;
1002
1003 pathlen = strlen(path);
1004
1005 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
1006 if (ca_e) {
1007 memcpy(ca_e->path, path, pathlen + 1);
1008 ca_e->ca_store = store;
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001009 ca_e->type = type;
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001010 LIST_INIT(&ca_e->ckch_inst_link);
1011 }
1012 return ca_e;
1013}
1014
1015/* Delete a cafile_entry. The caller is responsible from removing this entry
1016 * from the cafile_tree first if is was previously added into it. */
1017void ssl_store_delete_cafile_entry(struct cafile_entry *ca_e)
1018{
1019 struct ckch_inst_link *link, *link_s;
1020 if (!ca_e)
1021 return;
1022
1023 X509_STORE_free(ca_e->ca_store);
1024
1025 list_for_each_entry_safe(link, link_s, &ca_e->ckch_inst_link, list) {
1026 struct ckch_inst *inst = link->ckch_inst;
1027 struct ckch_inst_link_ref *link_ref, *link_ref_s;
1028 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
1029 if (link_ref->link == link) {
1030 LIST_DELETE(&link_ref->list);
1031 free(link_ref);
1032 break;
1033 }
1034 }
1035 LIST_DELETE(&link->list);
1036 free(link);
1037 }
1038
1039 free(ca_e);
1040}
1041
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001042/*
1043 * Build a cafile_entry out of a buffer instead of out of a file.
1044 * This function is used when the "commit ssl ca-file" cli command is used.
1045 * It can parse CERTIFICATE sections as well as CRL ones.
1046 * Returns 0 in case of success, 1 otherwise.
1047 */
1048int ssl_store_load_ca_from_buf(struct cafile_entry *ca_e, char *cert_buf)
1049{
1050 int retval = 0;
1051
1052 if (!ca_e)
1053 return 1;
1054
1055 if (!ca_e->ca_store) {
1056 ca_e->ca_store = X509_STORE_new();
1057 if (ca_e->ca_store) {
1058 BIO *bio = BIO_new_mem_buf(cert_buf, strlen(cert_buf));
1059 if (bio) {
1060 X509_INFO *info;
1061 int i;
1062 STACK_OF(X509_INFO) *infos = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
1063 if (!infos)
1064 {
1065 BIO_free(bio);
1066 return 1;
1067 }
1068
1069 for (i = 0; i < sk_X509_INFO_num(infos) && !retval; i++) {
1070 info = sk_X509_INFO_value(infos, i);
1071 /* X509_STORE_add_cert and X509_STORE_add_crl return 1 on success */
1072 if (info->x509) {
1073 retval = !X509_STORE_add_cert(ca_e->ca_store, info->x509);
1074 }
1075 if (!retval && info->crl) {
1076 retval = !X509_STORE_add_crl(ca_e->ca_store, info->crl);
1077 }
1078 }
1079 retval = retval || (i != sk_X509_INFO_num(infos));
1080
1081 /* Cleanup */
1082 sk_X509_INFO_pop_free(infos, X509_INFO_free);
1083 BIO_free(bio);
1084 }
1085 }
1086 }
1087
1088 return retval;
1089}
1090
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001091int ssl_store_load_locations_file(char *path, int create_if_none, enum cafile_type type)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001092{
1093 X509_STORE *store = ssl_store_get0_locations_file(path);
1094
1095 /* If this function is called by the CLI, we should not call the
1096 * X509_STORE_load_locations function because it performs forbidden disk
1097 * accesses. */
1098 if (!store && create_if_none) {
1099 struct cafile_entry *ca_e;
1100 store = X509_STORE_new();
1101 if (X509_STORE_load_locations(store, path, NULL)) {
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001102 ca_e = ssl_store_create_cafile_entry(path, store, type);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001103 if (ca_e) {
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001104 ebst_insert(&cafile_tree, &ca_e->node);
1105 }
1106 } else {
1107 X509_STORE_free(store);
1108 store = NULL;
1109 }
1110 }
1111 return (store != NULL);
1112}
1113
1114
William Lallemandda8584c2020-05-14 10:14:37 +02001115/*************************** CLI commands ***********************/
1116
1117/* Type of SSL payloads that can be updated over the CLI */
1118
1119enum {
1120 CERT_TYPE_PEM = 0,
1121 CERT_TYPE_KEY,
1122#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
1123 CERT_TYPE_OCSP,
1124#endif
1125 CERT_TYPE_ISSUER,
Ilya Shipitsinc47d6762021-02-13 11:45:33 +05001126#ifdef HAVE_SSL_SCTL
William Lallemandda8584c2020-05-14 10:14:37 +02001127 CERT_TYPE_SCTL,
1128#endif
1129 CERT_TYPE_MAX,
1130};
1131
1132struct {
1133 const char *ext;
1134 int type;
1135 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
1136 /* add a parsing callback */
1137} cert_exts[CERT_TYPE_MAX+1] = {
1138 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
1139 [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
1140#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
1141 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
1142#endif
Ilya Shipitsinc47d6762021-02-13 11:45:33 +05001143#ifdef HAVE_SSL_SCTL
William Lallemandda8584c2020-05-14 10:14:37 +02001144 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
1145#endif
1146 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
1147 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
1148};
1149
1150
1151/* release function of the `show ssl cert' command */
1152static void cli_release_show_cert(struct appctx *appctx)
1153{
1154 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1155}
1156
1157/* IO handler of "show ssl cert <filename>" */
1158static int cli_io_handler_show_cert(struct appctx *appctx)
1159{
1160 struct buffer *trash = alloc_trash_chunk();
1161 struct ebmb_node *node;
1162 struct stream_interface *si = appctx->owner;
1163 struct ckch_store *ckchs;
1164
1165 if (trash == NULL)
1166 return 1;
1167
1168 if (!appctx->ctx.ssl.old_ckchs) {
1169 if (ckchs_transaction.old_ckchs) {
1170 ckchs = ckchs_transaction.old_ckchs;
1171 chunk_appendf(trash, "# transaction\n");
William Lallemand5685ccf2020-09-16 16:12:25 +02001172 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001173 }
1174 }
1175
1176 if (!appctx->ctx.cli.p0) {
1177 chunk_appendf(trash, "# filename\n");
1178 node = ebmb_first(&ckchs_tree);
1179 } else {
1180 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
1181 }
1182 while (node) {
1183 ckchs = ebmb_entry(node, struct ckch_store, node);
William Lallemand5685ccf2020-09-16 16:12:25 +02001184 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001185
1186 node = ebmb_next(node);
1187 if (ci_putchk(si_ic(si), trash) == -1) {
1188 si_rx_room_blk(si);
1189 goto yield;
1190 }
1191 }
1192
1193 appctx->ctx.cli.p0 = NULL;
1194 free_trash_chunk(trash);
1195 return 1;
1196yield:
1197
1198 free_trash_chunk(trash);
1199 appctx->ctx.cli.p0 = ckchs;
1200 return 0; /* should come back */
1201}
1202
1203/*
1204 * Extract and format the DNS SAN extensions and copy result into a chuink
1205 * Return 0;
1206 */
1207#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1208static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
1209{
1210 int i;
1211 char *str;
1212 STACK_OF(GENERAL_NAME) *names = NULL;
1213
1214 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1215 if (names) {
1216 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
1217 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
1218 if (i > 0)
1219 chunk_appendf(out, ", ");
1220 if (name->type == GEN_DNS) {
1221 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
1222 chunk_appendf(out, "DNS:%s", str);
1223 OPENSSL_free(str);
1224 }
1225 }
1226 }
1227 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
1228 }
1229 return 0;
1230}
1231#endif
1232
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001233/*
1234 * Build the ckch_inst_link that will be chained in the CA file entry and the
1235 * corresponding ckch_inst_link_ref that will be chained in the ckch instance.
1236 * Return 0 in case of success.
1237 */
1238static int do_chain_inst_and_cafile(struct cafile_entry *cafile_entry, struct ckch_inst *ckch_inst)
1239{
1240 struct ckch_inst_link *new_link;
1241 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
1242 struct ckch_inst_link *link = LIST_ELEM(cafile_entry->ckch_inst_link.n,
1243 typeof(link), list);
1244 /* Do not add multiple references to the same
1245 * instance in a cafile_entry */
1246 if (link->ckch_inst == ckch_inst) {
1247 return 1;
1248 }
1249 }
1250
1251 new_link = calloc(1, sizeof(*new_link));
1252 if (new_link) {
1253 struct ckch_inst_link_ref *new_link_ref = calloc(1, sizeof(*new_link_ref));
1254 if (!new_link_ref) {
1255 free(new_link);
1256 return 1;
1257 }
1258
1259 new_link->ckch_inst = ckch_inst;
1260 new_link_ref->link = new_link;
1261 LIST_INIT(&new_link->list);
1262 LIST_INIT(&new_link_ref->list);
1263
1264 LIST_APPEND(&cafile_entry->ckch_inst_link, &new_link->list);
1265 LIST_APPEND(&ckch_inst->cafile_link_refs, &new_link_ref->list);
1266 }
1267
1268 return 0;
1269}
1270
1271
1272/*
1273 * Link a CA file tree entry to the ckch instance that uses it.
1274 * To determine if and which CA file tree entries need to be linked to the
1275 * instance, we follow the same logic performed in ssl_sock_prepare_ctx when
1276 * processing the verify option.
1277 * This function works for a frontend as well as for a backend, depending on the
1278 * configuration parameters given (bind_conf or server).
1279 */
1280void ckch_inst_add_cafile_link(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf,
1281 struct ssl_bind_conf *ssl_conf, const struct server *srv)
1282{
1283 int verify = SSL_VERIFY_NONE;
1284
1285 if (srv) {
1286
1287 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
1288 verify = SSL_VERIFY_PEER;
1289 switch (srv->ssl_ctx.verify) {
1290 case SSL_SOCK_VERIFY_NONE:
1291 verify = SSL_VERIFY_NONE;
1292 break;
1293 case SSL_SOCK_VERIFY_REQUIRED:
1294 verify = SSL_VERIFY_PEER;
1295 break;
1296 }
1297 }
1298 else {
1299 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
1300 case SSL_SOCK_VERIFY_NONE:
1301 verify = SSL_VERIFY_NONE;
1302 break;
1303 case SSL_SOCK_VERIFY_OPTIONAL:
1304 verify = SSL_VERIFY_PEER;
1305 break;
1306 case SSL_SOCK_VERIFY_REQUIRED:
1307 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1308 break;
1309 }
1310 }
1311
1312 if (verify & SSL_VERIFY_PEER) {
1313 struct cafile_entry *ca_file_entry = NULL;
1314 struct cafile_entry *ca_verify_file_entry = NULL;
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001315 struct cafile_entry *crl_file_entry = NULL;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001316 if (srv) {
1317 if (srv->ssl_ctx.ca_file) {
1318 ca_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.ca_file, 0);
1319
1320 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001321 if (srv->ssl_ctx.crl_file) {
1322 crl_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.crl_file, 0);
1323 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001324 }
1325 else {
1326 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
1327 char *ca_verify_file = (ssl_conf && ssl_conf->ca_verify_file) ? ssl_conf->ca_verify_file : bind_conf->ssl_conf.ca_verify_file;
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001328 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001329
1330 if (ca_file)
1331 ca_file_entry = ssl_store_get_cafile_entry(ca_file, 0);
1332 if (ca_verify_file)
1333 ca_verify_file_entry = ssl_store_get_cafile_entry(ca_verify_file, 0);
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001334 if (crl_file)
1335 crl_file_entry = ssl_store_get_cafile_entry(crl_file, 0);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001336 }
1337
1338 if (ca_file_entry) {
1339 /* If we have a ckch instance that is not already in the
1340 * cafile_entry's list, add it to it. */
1341 if (do_chain_inst_and_cafile(ca_file_entry, ckch_inst))
1342 return;
1343
1344 }
1345 if (ca_verify_file_entry && (ca_file_entry != ca_verify_file_entry)) {
1346 /* If we have a ckch instance that is not already in the
1347 * cafile_entry's list, add it to it. */
1348 if (do_chain_inst_and_cafile(ca_verify_file_entry, ckch_inst))
1349 return;
1350 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001351 if (crl_file_entry) {
1352 /* If we have a ckch instance that is not already in the
1353 * cafile_entry's list, add it to it. */
1354 if (do_chain_inst_and_cafile(crl_file_entry, ckch_inst))
1355 return;
1356 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001357 }
1358}
1359
William Lallemandda8584c2020-05-14 10:14:37 +02001360
1361
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001362static int show_cert_detail(X509 *cert, STACK_OF(X509) *chain, struct buffer *out)
William Lallemandda8584c2020-05-14 10:14:37 +02001363{
William Lallemandda8584c2020-05-14 10:14:37 +02001364 BIO *bio = NULL;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001365 struct buffer *tmp = alloc_trash_chunk();
William Lallemandda8584c2020-05-14 10:14:37 +02001366 int i;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001367 int write = -1;
1368 unsigned int len = 0;
1369 X509_NAME *name = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001370
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001371 if (!tmp)
1372 return -1;
William Lallemandda8584c2020-05-14 10:14:37 +02001373
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001374 if (!cert)
William Lallemand5685ccf2020-09-16 16:12:25 +02001375 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02001376
William Lallemand5685ccf2020-09-16 16:12:25 +02001377 if (chain == NULL) {
1378 struct issuer_chain *issuer;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001379 issuer = ssl_get0_issuer_chain(cert);
William Lallemand5685ccf2020-09-16 16:12:25 +02001380 if (issuer) {
1381 chain = issuer->chain;
1382 chunk_appendf(out, "Chain Filename: ");
1383 chunk_appendf(out, "%s\n", issuer->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001384 }
William Lallemand5685ccf2020-09-16 16:12:25 +02001385 }
1386 chunk_appendf(out, "Serial: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001387 if (ssl_sock_get_serial(cert, tmp) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001388 goto end;
1389 dump_binary(out, tmp->area, tmp->data);
1390 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001391
William Lallemand5685ccf2020-09-16 16:12:25 +02001392 chunk_appendf(out, "notBefore: ");
1393 chunk_reset(tmp);
1394 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1395 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001396 if (ASN1_TIME_print(bio, X509_getm_notBefore(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001397 goto end;
1398 write = BIO_read(bio, tmp->area, tmp->size-1);
1399 tmp->area[write] = '\0';
1400 BIO_free(bio);
1401 bio = NULL;
1402 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001403
William Lallemand5685ccf2020-09-16 16:12:25 +02001404 chunk_appendf(out, "notAfter: ");
1405 chunk_reset(tmp);
1406 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1407 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001408 if (ASN1_TIME_print(bio, X509_getm_notAfter(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001409 goto end;
1410 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
1411 goto end;
1412 tmp->area[write] = '\0';
1413 BIO_free(bio);
1414 bio = NULL;
1415 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001416
1417#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand5685ccf2020-09-16 16:12:25 +02001418 chunk_appendf(out, "Subject Alternative Name: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001419 if (ssl_sock_get_san_oneline(cert, out) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001420 goto end;
1421 *(out->area + out->data) = '\0';
1422 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001423#endif
William Lallemand5685ccf2020-09-16 16:12:25 +02001424 chunk_reset(tmp);
1425 chunk_appendf(out, "Algorithm: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001426 if (cert_get_pkey_algo(cert, tmp) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001427 goto end;
1428 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001429
William Lallemand5685ccf2020-09-16 16:12:25 +02001430 chunk_reset(tmp);
1431 chunk_appendf(out, "SHA1 FingerPrint: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001432 if (X509_digest(cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001433 goto end;
1434 tmp->data = len;
1435 dump_binary(out, tmp->area, tmp->data);
1436 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001437
William Lallemand5685ccf2020-09-16 16:12:25 +02001438 chunk_appendf(out, "Subject: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001439 if ((name = X509_get_subject_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001440 goto end;
1441 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1442 goto end;
1443 *(tmp->area + tmp->data) = '\0';
1444 chunk_appendf(out, "%s\n", tmp->area);
1445
1446 chunk_appendf(out, "Issuer: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001447 if ((name = X509_get_issuer_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001448 goto end;
1449 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1450 goto end;
1451 *(tmp->area + tmp->data) = '\0';
1452 chunk_appendf(out, "%s\n", tmp->area);
1453
1454 /* Displays subject of each certificate in the chain */
1455 for (i = 0; i < sk_X509_num(chain); i++) {
1456 X509 *ca = sk_X509_value(chain, i);
1457
1458 chunk_appendf(out, "Chain Subject: ");
1459 if ((name = X509_get_subject_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001460 goto end;
1461 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1462 goto end;
1463 *(tmp->area + tmp->data) = '\0';
1464 chunk_appendf(out, "%s\n", tmp->area);
1465
William Lallemand5685ccf2020-09-16 16:12:25 +02001466 chunk_appendf(out, "Chain Issuer: ");
1467 if ((name = X509_get_issuer_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001468 goto end;
1469 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1470 goto end;
1471 *(tmp->area + tmp->data) = '\0';
1472 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001473 }
1474
1475end:
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001476 if (bio)
1477 BIO_free(bio);
1478 free_trash_chunk(tmp);
1479
1480 return 0;
1481}
1482
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001483#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL)
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001484/*
1485 * Build the OCSP tree entry's key for a given ckch_store.
1486 * Returns a negative value in case of error.
1487 */
1488static int ckch_store_build_certid(struct ckch_store *ckch_store, unsigned char certid[128], unsigned int *key_length)
1489{
1490 OCSP_RESPONSE *resp;
1491 OCSP_BASICRESP *bs = NULL;
1492 OCSP_SINGLERESP *sr;
1493 OCSP_CERTID *id;
1494 unsigned char *p = NULL;
1495
1496 if (!key_length)
1497 return -1;
1498
1499 *key_length = 0;
1500
1501 if (!ckch_store->ckch->ocsp_response)
1502 return 0;
1503
1504 p = (unsigned char *) ckch_store->ckch->ocsp_response->area;
1505
1506 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
1507 ckch_store->ckch->ocsp_response->data);
1508 if (!resp) {
1509 goto end;
1510 }
1511
1512 bs = OCSP_response_get1_basic(resp);
1513 if (!bs) {
1514 goto end;
1515 }
1516
1517 sr = OCSP_resp_get0(bs, 0);
1518 if (!sr) {
1519 goto end;
1520 }
1521
1522 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
1523
1524 p = certid;
1525 *key_length = i2d_OCSP_CERTID(id, &p);
1526
1527end:
1528 return *key_length > 0;
1529}
1530#endif
1531
1532/*
1533 * Dump the OCSP certificate key (if it exists) of certificate <ckch> into
1534 * buffer <out>.
1535 * Returns 0 in case of success.
1536 */
1537static int ckch_store_show_ocsp_certid(struct ckch_store *ckch_store, struct buffer *out)
1538{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001539#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL)
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001540 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1541 unsigned int key_length = 0;
1542 int i;
1543
1544 if (ckch_store_build_certid(ckch_store, (unsigned char*)key, &key_length) >= 0) {
1545 /* Dump the CERTID info */
1546 chunk_appendf(out, "OCSP Response Key: ");
1547 for (i = 0; i < key_length; ++i) {
1548 chunk_appendf(out, "%02x", key[i]);
1549 }
1550 chunk_appendf(out, "\n");
1551 }
1552#endif
1553
1554 return 0;
1555}
1556
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001557
1558/* IO handler of the details "show ssl cert <filename>" */
1559static int cli_io_handler_show_cert_detail(struct appctx *appctx)
1560{
1561 struct stream_interface *si = appctx->owner;
1562 struct ckch_store *ckchs = appctx->ctx.cli.p0;
1563 struct buffer *out = alloc_trash_chunk();
1564 int retval = 0;
1565
1566 if (!out)
1567 goto end_no_putchk;
1568
1569 chunk_appendf(out, "Filename: ");
1570 if (ckchs == ckchs_transaction.new_ckchs)
1571 chunk_appendf(out, "*");
1572 chunk_appendf(out, "%s\n", ckchs->path);
1573
1574 chunk_appendf(out, "Status: ");
1575 if (ckchs->ckch->cert == NULL)
1576 chunk_appendf(out, "Empty\n");
1577 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
1578 chunk_appendf(out, "Unused\n");
1579 else
1580 chunk_appendf(out, "Used\n");
1581
1582 retval = show_cert_detail(ckchs->ckch->cert, ckchs->ckch->chain, out);
1583 if (retval < 0)
1584 goto end_no_putchk;
1585 else if (retval)
1586 goto end;
1587
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001588 ckch_store_show_ocsp_certid(ckchs, out);
1589
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001590end:
William Lallemandda8584c2020-05-14 10:14:37 +02001591 if (ci_putchk(si_ic(si), out) == -1) {
1592 si_rx_room_blk(si);
1593 goto yield;
1594 }
1595
1596end_no_putchk:
William Lallemandda8584c2020-05-14 10:14:37 +02001597 free_trash_chunk(out);
1598 return 1;
1599yield:
William Lallemandda8584c2020-05-14 10:14:37 +02001600 free_trash_chunk(out);
1601 return 0; /* should come back */
1602}
1603
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001604
1605/* IO handler of the details "show ssl cert <filename.ocsp>" */
1606static int cli_io_handler_show_cert_ocsp_detail(struct appctx *appctx)
1607{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001608#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL)
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001609 struct stream_interface *si = appctx->owner;
1610 struct ckch_store *ckchs = appctx->ctx.cli.p0;
1611 struct buffer *out = alloc_trash_chunk();
1612 int from_transaction = appctx->ctx.cli.i0;
1613
1614 if (!out)
1615 goto end_no_putchk;
1616
1617 /* If we try to display an ongoing transaction's OCSP response, we
1618 * need to dump the ckch's ocsp_response buffer directly.
1619 * Otherwise, we must rebuild the certificate's certid in order to
1620 * look for the current OCSP response in the tree. */
1621 if (from_transaction && ckchs->ckch->ocsp_response) {
1622 ssl_ocsp_response_print(ckchs->ckch->ocsp_response, out);
1623 }
1624 else {
1625 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1626 unsigned int key_length = 0;
1627
1628 if (ckch_store_build_certid(ckchs, (unsigned char*)key, &key_length) < 0)
1629 goto end_no_putchk;
1630
1631 ssl_get_ocspresponse_detail(key, out);
1632 }
1633
1634 if (ci_putchk(si_ic(si), out) == -1) {
1635 si_rx_room_blk(si);
1636 goto yield;
1637 }
1638
1639end_no_putchk:
1640 free_trash_chunk(out);
1641 return 1;
1642yield:
1643 free_trash_chunk(out);
1644 return 0; /* should come back */
1645#else
1646 return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n");
1647#endif
1648}
1649
William Lallemandda8584c2020-05-14 10:14:37 +02001650/* parsing function for 'show ssl cert [certfile]' */
1651static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
1652{
1653 struct ckch_store *ckchs;
1654
1655 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1656 return cli_err(appctx, "Can't allocate memory!\n");
1657
1658 /* The operations on the CKCH architecture are locked so we can
1659 * manipulate ckch_store and ckch_inst */
1660 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1661 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
1662
1663 /* check if there is a certificate to lookup */
1664 if (*args[3]) {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001665 int show_ocsp_detail = 0;
1666 int from_transaction = 0;
1667 char *end;
1668
1669 /* We manage the special case "certname.ocsp" through which we
1670 * can show the details of an OCSP response. */
1671 end = strrchr(args[3], '.');
1672 if (end && strcmp(end+1, "ocsp") == 0) {
1673 *end = '\0';
1674 show_ocsp_detail = 1;
1675 }
1676
William Lallemandda8584c2020-05-14 10:14:37 +02001677 if (*args[3] == '*') {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001678 from_transaction = 1;
William Lallemandda8584c2020-05-14 10:14:37 +02001679 if (!ckchs_transaction.new_ckchs)
1680 goto error;
1681
1682 ckchs = ckchs_transaction.new_ckchs;
1683
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001684 if (strcmp(args[3] + 1, ckchs->path) != 0)
William Lallemandda8584c2020-05-14 10:14:37 +02001685 goto error;
1686
1687 } else {
1688 if ((ckchs = ckchs_lookup(args[3])) == NULL)
1689 goto error;
1690
1691 }
1692
William Lallemandda8584c2020-05-14 10:14:37 +02001693 appctx->ctx.cli.p0 = ckchs;
1694 /* use the IO handler that shows details */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001695 if (show_ocsp_detail) {
1696 appctx->ctx.cli.i0 = from_transaction;
1697 appctx->io_handler = cli_io_handler_show_cert_ocsp_detail;
1698 }
1699 else
1700 appctx->io_handler = cli_io_handler_show_cert_detail;
William Lallemandda8584c2020-05-14 10:14:37 +02001701 }
1702
1703 return 0;
1704
1705error:
1706 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1707 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
1708}
1709
1710/* release function of the `set ssl cert' command, free things and unlock the spinlock */
1711static void cli_release_commit_cert(struct appctx *appctx)
1712{
1713 struct ckch_store *new_ckchs;
1714
1715 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1716
1717 if (appctx->st2 != SETCERT_ST_FIN) {
1718 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
1719 new_ckchs = appctx->ctx.ssl.new_ckchs;
1720
1721 /* if the allocation failed, we need to free everything from the temporary list */
1722 ckch_store_free(new_ckchs);
1723 }
1724}
1725
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001726
1727/*
1728 * Rebuild a new instance 'new_inst' based on an old instance 'ckchi' and a
1729 * specific ckch_store.
1730 * Returns 0 in case of success, 1 otherwise.
1731 */
1732static int ckch_inst_rebuild(struct ckch_store *ckch_store, struct ckch_inst *ckchi,
1733 struct ckch_inst **new_inst, char **err)
1734{
1735 int retval = 0;
1736 int errcode = 0;
1737 struct sni_ctx *sc0, *sc0s;
1738 char **sni_filter = NULL;
1739 int fcount = 0;
1740
1741 if (ckchi->crtlist_entry) {
1742 sni_filter = ckchi->crtlist_entry->filters;
1743 fcount = ckchi->crtlist_entry->fcount;
1744 }
1745
1746 if (ckchi->is_server_instance)
1747 errcode |= ckch_inst_new_load_srv_store(ckch_store->path, ckch_store, new_inst, err);
1748 else
1749 errcode |= ckch_inst_new_load_store(ckch_store->path, ckch_store, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, new_inst, err);
1750
1751 if (errcode & ERR_CODE)
1752 return 1;
1753
1754 /* if the previous ckchi was used as the default */
1755 if (ckchi->is_default)
1756 (*new_inst)->is_default = 1;
1757
1758 (*new_inst)->is_server_instance = ckchi->is_server_instance;
1759 (*new_inst)->server = ckchi->server;
1760 /* Create a new SSL_CTX and link it to the new instance. */
1761 if ((*new_inst)->is_server_instance) {
1762 retval = ssl_sock_prep_srv_ctx_and_inst(ckchi->server, (*new_inst)->ctx, (*new_inst));
1763 if (retval)
1764 return 1;
1765 }
1766
1767 /* create the link to the crtlist_entry */
1768 (*new_inst)->crtlist_entry = ckchi->crtlist_entry;
1769
1770 /* we need to initialize the SSL_CTX generated */
1771 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
1772 list_for_each_entry_safe(sc0, sc0s, &(*new_inst)->sni_ctx, by_ckch_inst) {
1773 if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
1774 errcode |= ssl_sock_prep_ctx_and_inst(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, *new_inst, err);
1775 if (errcode & ERR_CODE)
1776 return 1;
1777 }
1778 }
1779
1780 return 0;
1781}
1782
1783/*
1784 * Load all the new SNIs of a newly built ckch instance in the trees, or replace
1785 * a server's main ckch instance.
1786 */
1787static void __ssl_sock_load_new_ckch_instance(struct ckch_inst *ckchi)
1788{
1789 /* The bind_conf will be null on server ckch_instances. */
1790 if (ckchi->is_server_instance) {
1791 int i;
1792 /* a lock is needed here since we have to free the SSL cache */
1793 HA_RWLOCK_WRLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
1794 /* free the server current SSL_CTX */
1795 SSL_CTX_free(ckchi->server->ssl_ctx.ctx);
1796 /* Actual ssl context update */
1797 SSL_CTX_up_ref(ckchi->ctx);
1798 ckchi->server->ssl_ctx.ctx = ckchi->ctx;
1799 ckchi->server->ssl_ctx.inst = ckchi;
1800
1801 /* flush the session cache of the server */
1802 for (i = 0; i < global.nbthread; i++) {
William Lallemandce990332021-11-23 15:15:09 +01001803 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].sni);
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001804 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].ptr);
1805 }
1806 HA_RWLOCK_WRUNLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
1807
1808 } else {
1809 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
1810 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
1811 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
1812 }
1813}
1814
1815/*
1816 * Delete a ckch instance that was replaced after a CLI command.
1817 */
1818static void __ckch_inst_free_locked(struct ckch_inst *ckchi)
1819{
1820 if (ckchi->is_server_instance) {
1821 /* no lock for servers */
1822 ckch_inst_free(ckchi);
1823 } else {
1824 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
1825
1826 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
1827 ckch_inst_free(ckchi);
1828 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
1829 }
1830}
1831
1832
William Lallemandda8584c2020-05-14 10:14:37 +02001833/*
1834 * This function tries to create the new ckch_inst and their SNIs
1835 */
1836static int cli_io_handler_commit_cert(struct appctx *appctx)
1837{
1838 struct stream_interface *si = appctx->owner;
1839 int y = 0;
1840 char *err = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001841 struct ckch_store *old_ckchs, *new_ckchs = NULL;
1842 struct ckch_inst *ckchi, *ckchis;
1843 struct buffer *trash = alloc_trash_chunk();
William Lallemandda8584c2020-05-14 10:14:37 +02001844 struct crtlist_entry *entry;
1845
1846 if (trash == NULL)
1847 goto error;
1848
1849 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1850 goto error;
1851
1852 while (1) {
1853 switch (appctx->st2) {
1854 case SETCERT_ST_INIT:
1855 /* This state just print the update message */
1856 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
1857 if (ci_putchk(si_ic(si), trash) == -1) {
1858 si_rx_room_blk(si);
1859 goto yield;
1860 }
1861 appctx->st2 = SETCERT_ST_GEN;
1862 /* fallthrough */
1863 case SETCERT_ST_GEN:
1864 /*
1865 * This state generates the ckch instances with their
1866 * sni_ctxs and SSL_CTX.
1867 *
1868 * Since the SSL_CTX generation can be CPU consumer, we
1869 * yield every 10 instances.
1870 */
1871
1872 old_ckchs = appctx->ctx.ssl.old_ckchs;
1873 new_ckchs = appctx->ctx.ssl.new_ckchs;
1874
1875 if (!new_ckchs)
1876 continue;
1877
1878 /* get the next ckchi to regenerate */
1879 ckchi = appctx->ctx.ssl.next_ckchi;
1880 /* we didn't start yet, set it to the first elem */
1881 if (ckchi == NULL)
1882 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
1883
1884 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
1885 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
1886 struct ckch_inst *new_inst;
William Lallemandda8584c2020-05-14 10:14:37 +02001887
1888 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
1889 if (y >= 10) {
1890 /* save the next ckchi to compute */
1891 appctx->ctx.ssl.next_ckchi = ckchi;
1892 goto yield;
1893 }
1894
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001895 if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err))
William Lallemandda8584c2020-05-14 10:14:37 +02001896 goto error;
1897
William Lallemandda8584c2020-05-14 10:14:37 +02001898 /* display one dot per new instance */
1899 chunk_appendf(trash, ".");
1900 /* link the new ckch_inst to the duplicate */
Willy Tarreau2b718102021-04-21 07:32:39 +02001901 LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02001902 y++;
1903 }
1904 appctx->st2 = SETCERT_ST_INSERT;
1905 /* fallthrough */
1906 case SETCERT_ST_INSERT:
1907 /* The generation is finished, we can insert everything */
1908
1909 old_ckchs = appctx->ctx.ssl.old_ckchs;
1910 new_ckchs = appctx->ctx.ssl.new_ckchs;
1911
1912 if (!new_ckchs)
1913 continue;
1914
1915 /* get the list of crtlist_entry in the old store, and update the pointers to the store */
1916 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
1917 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
1918 ebpt_delete(&entry->node);
1919 /* change the ptr and reinsert the node */
1920 entry->node.key = new_ckchs;
1921 ebpt_insert(&entry->crtlist->entries, &entry->node);
1922 }
1923
William Lallemanda55685b2020-12-15 14:57:46 +01001924 /* insert the new ckch_insts in the crtlist_entry */
1925 list_for_each_entry(ckchi, &new_ckchs->ckch_inst, by_ckchs) {
1926 if (ckchi->crtlist_entry)
Willy Tarreau2b718102021-04-21 07:32:39 +02001927 LIST_INSERT(&ckchi->crtlist_entry->ckch_inst, &ckchi->by_crtlist_entry);
William Lallemanda55685b2020-12-15 14:57:46 +01001928 }
1929
William Lallemandda8584c2020-05-14 10:14:37 +02001930 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
1931 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001932 __ssl_sock_load_new_ckch_instance(ckchi);
William Lallemandda8584c2020-05-14 10:14:37 +02001933 }
1934
1935 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
1936 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001937 __ckch_inst_free_locked(ckchi);
William Lallemandda8584c2020-05-14 10:14:37 +02001938 }
1939
1940 /* Replace the old ckchs by the new one */
1941 ckch_store_free(old_ckchs);
1942 ebst_insert(&ckchs_tree, &new_ckchs->node);
1943 appctx->st2 = SETCERT_ST_FIN;
1944 /* fallthrough */
1945 case SETCERT_ST_FIN:
1946 /* we achieved the transaction, we can set everything to NULL */
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001947 ha_free(&ckchs_transaction.path);
William Lallemandda8584c2020-05-14 10:14:37 +02001948 ckchs_transaction.new_ckchs = NULL;
1949 ckchs_transaction.old_ckchs = NULL;
1950 goto end;
1951 }
1952 }
1953end:
1954
1955 chunk_appendf(trash, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001956 chunk_appendf(trash, "Success!\n");
1957 if (ci_putchk(si_ic(si), trash) == -1)
1958 si_rx_room_blk(si);
1959 free_trash_chunk(trash);
1960 /* success: call the release function and don't come back */
1961 return 1;
1962yield:
1963 /* store the state */
1964 if (ci_putchk(si_ic(si), trash) == -1)
1965 si_rx_room_blk(si);
1966 free_trash_chunk(trash);
1967 si_rx_endp_more(si); /* let's come back later */
1968 return 0; /* should come back */
1969
1970error:
1971 /* spin unlock and free are done in the release function */
1972 if (trash) {
1973 chunk_appendf(trash, "\n%sFailed!\n", err);
1974 if (ci_putchk(si_ic(si), trash) == -1)
1975 si_rx_room_blk(si);
1976 free_trash_chunk(trash);
1977 }
1978 /* error: call the release function and don't come back */
1979 return 1;
1980}
1981
1982/*
1983 * Parsing function of 'commit ssl cert'
1984 */
1985static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
1986{
1987 char *err = NULL;
1988
1989 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1990 return 1;
1991
1992 if (!*args[3])
1993 return cli_err(appctx, "'commit ssl cert expects a filename\n");
1994
1995 /* The operations on the CKCH architecture are locked so we can
1996 * manipulate ckch_store and ckch_inst */
1997 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1998 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
1999
2000 if (!ckchs_transaction.path) {
2001 memprintf(&err, "No ongoing transaction! !\n");
2002 goto error;
2003 }
2004
2005 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2006 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
2007 goto error;
2008 }
2009
William Lallemand5685ccf2020-09-16 16:12:25 +02002010 /* if a certificate is here, a private key must be here too */
2011 if (ckchs_transaction.new_ckchs->ckch->cert && !ckchs_transaction.new_ckchs->ckch->key) {
2012 memprintf(&err, "The transaction must contain at least a certificate and a private key!\n");
2013 goto error;
2014 }
William Lallemanda9419522020-06-24 16:26:41 +02002015
William Lallemand5685ccf2020-09-16 16:12:25 +02002016 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
2017 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
2018 goto error;
William Lallemandda8584c2020-05-14 10:14:37 +02002019 }
2020
2021 /* init the appctx structure */
2022 appctx->st2 = SETCERT_ST_INIT;
2023 appctx->ctx.ssl.next_ckchi = NULL;
2024 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
2025 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
2026
2027 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
2028 return 0;
2029
2030error:
2031
2032 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2033 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2034
2035 return cli_dynerr(appctx, err);
2036}
2037
2038
2039
2040
2041/*
2042 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
2043 */
2044static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
2045{
2046 struct ckch_store *new_ckchs = NULL;
2047 struct ckch_store *old_ckchs = NULL;
2048 char *err = NULL;
2049 int i;
William Lallemandda8584c2020-05-14 10:14:37 +02002050 int errcode = 0;
2051 char *end;
2052 int type = CERT_TYPE_PEM;
2053 struct cert_key_and_chain *ckch;
2054 struct buffer *buf;
2055
2056 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2057 return 1;
2058
William Lallemandda8584c2020-05-14 10:14:37 +02002059 if (!*args[3] || !payload)
2060 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
2061
2062 /* The operations on the CKCH architecture are locked so we can
2063 * manipulate ckch_store and ckch_inst */
2064 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2065 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
2066
William Lallemand5ba80d62021-05-04 16:17:27 +02002067 if ((buf = alloc_trash_chunk()) == NULL) {
2068 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2069 errcode |= ERR_ALERT | ERR_FATAL;
2070 goto end;
2071 }
William Lallemande5ff4ad2020-06-08 09:40:37 +02002072
William Lallemandda8584c2020-05-14 10:14:37 +02002073 if (!chunk_strcpy(buf, args[3])) {
2074 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2075 errcode |= ERR_ALERT | ERR_FATAL;
2076 goto end;
2077 }
2078
2079 /* check which type of file we want to update */
2080 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
2081 end = strrchr(buf->area, '.');
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002082 if (end && *cert_exts[i].ext && (strcmp(end + 1, cert_exts[i].ext) == 0)) {
William Lallemandda8584c2020-05-14 10:14:37 +02002083 *end = '\0';
William Lallemand089c1382020-10-23 17:35:12 +02002084 buf->data = strlen(buf->area);
William Lallemandda8584c2020-05-14 10:14:37 +02002085 type = cert_exts[i].type;
2086 break;
2087 }
2088 }
2089
2090 appctx->ctx.ssl.old_ckchs = NULL;
2091 appctx->ctx.ssl.new_ckchs = NULL;
2092
2093 /* if there is an ongoing transaction */
2094 if (ckchs_transaction.path) {
William Lallemandda8584c2020-05-14 10:14:37 +02002095 /* if there is an ongoing transaction, check if this is the same file */
2096 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
William Lallemand089c1382020-10-23 17:35:12 +02002097 /* we didn't find the transaction, must try more cases below */
2098
2099 /* if the del-ext option is activated we should try to take a look at a ".crt" too. */
2100 if (type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
2101 if (!chunk_strcat(buf, ".crt")) {
2102 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2103 errcode |= ERR_ALERT | ERR_FATAL;
2104 goto end;
2105 }
2106
2107 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
2108 /* remove .crt of the error message */
2109 *(b_orig(buf) + b_data(buf) + strlen(".crt")) = '\0';
2110 b_sub(buf, strlen(".crt"));
2111
2112 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
2113 errcode |= ERR_ALERT | ERR_FATAL;
2114 goto end;
2115 }
2116 }
William Lallemandda8584c2020-05-14 10:14:37 +02002117 }
2118
2119 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
2120
2121 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002122
William Lallemand95fefa12020-09-09 12:01:33 +02002123 /* lookup for the certificate in the tree */
2124 appctx->ctx.ssl.old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002125
2126 if (!appctx->ctx.ssl.old_ckchs) {
2127 /* if the del-ext option is activated we should try to take a look at a ".crt" too. */
2128 if (type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
2129 if (!chunk_strcat(buf, ".crt")) {
2130 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2131 errcode |= ERR_ALERT | ERR_FATAL;
2132 goto end;
2133 }
2134 appctx->ctx.ssl.old_ckchs = ckchs_lookup(buf->area);
2135 }
2136 }
William Lallemandda8584c2020-05-14 10:14:37 +02002137 }
2138
2139 if (!appctx->ctx.ssl.old_ckchs) {
2140 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
2141 err ? err : "");
2142 errcode |= ERR_ALERT | ERR_FATAL;
2143 goto end;
2144 }
2145
2146 if (!appctx->ctx.ssl.path) {
2147 /* this is a new transaction, set the path of the transaction */
2148 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
2149 if (!appctx->ctx.ssl.path) {
2150 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2151 errcode |= ERR_ALERT | ERR_FATAL;
2152 goto end;
2153 }
2154 }
2155
2156 old_ckchs = appctx->ctx.ssl.old_ckchs;
2157
2158 /* duplicate the ckch store */
2159 new_ckchs = ckchs_dup(old_ckchs);
2160 if (!new_ckchs) {
2161 memprintf(&err, "%sCannot allocate memory!\n",
2162 err ? err : "");
2163 errcode |= ERR_ALERT | ERR_FATAL;
2164 goto end;
2165 }
2166
William Lallemand95fefa12020-09-09 12:01:33 +02002167 ckch = new_ckchs->ckch;
William Lallemandda8584c2020-05-14 10:14:37 +02002168
2169 /* appply the change on the duplicate */
2170 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
2171 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
2172 errcode |= ERR_ALERT | ERR_FATAL;
2173 goto end;
2174 }
2175
2176 appctx->ctx.ssl.new_ckchs = new_ckchs;
2177
2178 /* we succeed, we can save the ckchs in the transaction */
2179
2180 /* if there wasn't a transaction, update the old ckchs */
2181 if (!ckchs_transaction.old_ckchs) {
2182 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
2183 ckchs_transaction.path = appctx->ctx.ssl.path;
2184 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
2185 } else {
2186 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
2187
2188 }
2189
2190 /* free the previous ckchs if there was a transaction */
2191 ckch_store_free(ckchs_transaction.new_ckchs);
2192
2193 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
2194
2195
2196 /* creates the SNI ctxs later in the IO handler */
2197
2198end:
2199 free_trash_chunk(buf);
2200
2201 if (errcode & ERR_CODE) {
2202
2203 ckch_store_free(appctx->ctx.ssl.new_ckchs);
2204 appctx->ctx.ssl.new_ckchs = NULL;
2205
2206 appctx->ctx.ssl.old_ckchs = NULL;
2207
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002208 ha_free(&appctx->ctx.ssl.path);
William Lallemandda8584c2020-05-14 10:14:37 +02002209
2210 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2211 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2212 } else {
2213
2214 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2215 return cli_dynmsg(appctx, LOG_NOTICE, err);
2216 }
2217 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
2218}
2219
2220/* parsing function of 'abort ssl cert' */
2221static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
2222{
2223 char *err = NULL;
2224
2225 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2226 return 1;
2227
2228 if (!*args[3])
2229 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
2230
2231 /* The operations on the CKCH architecture are locked so we can
2232 * manipulate ckch_store and ckch_inst */
2233 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2234 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2235
2236 if (!ckchs_transaction.path) {
2237 memprintf(&err, "No ongoing transaction!\n");
2238 goto error;
2239 }
2240
2241 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2242 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
2243 goto error;
2244 }
2245
2246 /* Only free the ckchs there, because the SNI and instances were not generated yet */
2247 ckch_store_free(ckchs_transaction.new_ckchs);
2248 ckchs_transaction.new_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002249 ckchs_transaction.old_ckchs = NULL;
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002250 ha_free(&ckchs_transaction.path);
William Lallemandda8584c2020-05-14 10:14:37 +02002251
2252 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2253
2254 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2255 return cli_dynmsg(appctx, LOG_NOTICE, err);
2256
2257error:
2258 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2259
2260 return cli_dynerr(appctx, err);
2261}
2262
2263/* parsing function of 'new ssl cert' */
2264static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
2265{
2266 struct ckch_store *store;
2267 char *err = NULL;
2268 char *path;
2269
2270 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2271 return 1;
2272
2273 if (!*args[3])
2274 return cli_err(appctx, "'new ssl cert' expects a filename\n");
2275
2276 path = args[3];
2277
2278 /* The operations on the CKCH architecture are locked so we can
2279 * manipulate ckch_store and ckch_inst */
2280 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2281 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
2282
2283 store = ckchs_lookup(path);
2284 if (store != NULL) {
2285 memprintf(&err, "Certificate '%s' already exists!\n", path);
2286 store = NULL; /* we don't want to free it */
2287 goto error;
2288 }
2289 /* we won't support multi-certificate bundle here */
William Lallemandbd8e6ed2020-09-16 16:08:08 +02002290 store = ckch_store_new(path);
William Lallemandda8584c2020-05-14 10:14:37 +02002291 if (!store) {
2292 memprintf(&err, "unable to allocate memory.\n");
2293 goto error;
2294 }
2295
2296 /* insert into the ckchs tree */
2297 ebst_insert(&ckchs_tree, &store->node);
2298 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
2299
2300 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2301 return cli_dynmsg(appctx, LOG_NOTICE, err);
2302error:
2303 free(store);
2304 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2305 return cli_dynerr(appctx, err);
2306}
2307
2308/* parsing function of 'del ssl cert' */
2309static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
2310{
2311 struct ckch_store *store;
2312 char *err = NULL;
2313 char *filename;
2314
2315 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2316 return 1;
2317
2318 if (!*args[3])
2319 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
2320
2321 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2322 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
2323
2324 filename = args[3];
2325
2326 store = ckchs_lookup(filename);
2327 if (store == NULL) {
2328 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
2329 goto error;
2330 }
2331 if (!LIST_ISEMPTY(&store->ckch_inst)) {
2332 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
2333 goto error;
2334 }
2335
2336 ebmb_delete(&store->node);
2337 ckch_store_free(store);
2338
2339 memprintf(&err, "Certificate '%s' deleted!\n", filename);
2340
2341 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2342 return cli_dynmsg(appctx, LOG_NOTICE, err);
2343
2344error:
2345 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
2346 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2347 return cli_dynerr(appctx, err);
2348}
2349
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002350
Remi Tricot-Le Breton9f40fe02021-03-16 16:21:27 +01002351
2352/* parsing function of 'new ssl ca-file' */
2353static int cli_parse_new_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2354{
2355 struct cafile_entry *cafile_entry;
2356 char *err = NULL;
2357 char *path;
2358
2359 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2360 return 1;
2361
2362 if (!*args[3])
2363 return cli_err(appctx, "'new ssl ca-file' expects a filename\n");
2364
2365 path = args[3];
2366
2367 /* The operations on the CKCH architecture are locked so we can
2368 * manipulate ckch_store and ckch_inst */
2369 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2370 return cli_err(appctx, "Can't create a CA file!\nOperations on certificates are currently locked!\n");
2371
2372 cafile_entry = ssl_store_get_cafile_entry(path, 0);
2373 if (cafile_entry) {
2374 memprintf(&err, "CA file '%s' already exists!\n", path);
2375 goto error;
2376 }
2377
2378 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CERT);
2379 if (!cafile_entry) {
2380 memprintf(&err, "%sCannot allocate memory!\n",
2381 err ? err : "");
2382 goto error;
2383 }
2384
2385 /* Add the newly created cafile_entry to the tree so that
2386 * any new ckch instance created from now can use it. */
2387 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
2388 goto error;
2389
2390 memprintf(&err, "New CA file created '%s'!\n", path);
2391
2392 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2393 return cli_dynmsg(appctx, LOG_NOTICE, err);
2394error:
2395 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2396 return cli_dynerr(appctx, err);
2397}
2398
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002399/*
2400 * Parsing function of `set ssl ca-file`
2401 */
2402static int cli_parse_set_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2403{
2404 char *err = NULL;
2405 int errcode = 0;
2406 struct buffer *buf;
2407
2408 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2409 return 1;
2410
2411 if (!*args[3] || !payload)
2412 return cli_err(appctx, "'set ssl ca-file expects a filename and CAs as a payload\n");
2413
2414 /* The operations on the CKCH architecture are locked so we can
2415 * manipulate ckch_store and ckch_inst */
2416 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2417 return cli_err(appctx, "Can't update the CA file!\nOperations on certificates are currently locked!\n");
2418
2419 if ((buf = alloc_trash_chunk()) == NULL) {
2420 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2421 errcode |= ERR_ALERT | ERR_FATAL;
2422 goto end;
2423 }
2424
2425 if (!chunk_strcpy(buf, args[3])) {
2426 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2427 errcode |= ERR_ALERT | ERR_FATAL;
2428 goto end;
2429 }
2430
2431 appctx->ctx.ssl.old_cafile_entry = NULL;
2432 appctx->ctx.ssl.new_cafile_entry = NULL;
2433
2434 /* if there is an ongoing transaction */
2435 if (cafile_transaction.path) {
2436 /* if there is an ongoing transaction, check if this is the same file */
2437 if (strcmp(cafile_transaction.path, buf->area) != 0) {
2438 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, buf->area);
2439 errcode |= ERR_ALERT | ERR_FATAL;
2440 goto end;
2441 }
2442 appctx->ctx.ssl.old_cafile_entry = cafile_transaction.old_cafile_entry;
2443 }
2444 else {
2445 /* lookup for the certificate in the tree */
2446 appctx->ctx.ssl.old_cafile_entry = ssl_store_get_cafile_entry(buf->area, 0);
2447 }
2448
2449 if (!appctx->ctx.ssl.old_cafile_entry) {
2450 memprintf(&err, "%sCan't replace a CA file which is not referenced by the configuration!\n",
2451 err ? err : "");
2452 errcode |= ERR_ALERT | ERR_FATAL;
2453 goto end;
2454 }
2455
2456 if (!appctx->ctx.ssl.path) {
2457 /* this is a new transaction, set the path of the transaction */
2458 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_cafile_entry->path);
2459 if (!appctx->ctx.ssl.path) {
2460 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2461 errcode |= ERR_ALERT | ERR_FATAL;
2462 goto end;
2463 }
2464 }
2465
2466 if (appctx->ctx.ssl.new_cafile_entry)
2467 ssl_store_delete_cafile_entry(appctx->ctx.ssl.new_cafile_entry);
2468
2469 /* Create a new cafile_entry without adding it to the cafile tree. */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02002470 appctx->ctx.ssl.new_cafile_entry = ssl_store_create_cafile_entry(appctx->ctx.ssl.path, NULL, CAFILE_CERT);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002471 if (!appctx->ctx.ssl.new_cafile_entry) {
2472 memprintf(&err, "%sCannot allocate memory!\n",
2473 err ? err : "");
2474 errcode |= ERR_ALERT | ERR_FATAL;
2475 goto end;
2476 }
2477
2478 /* Fill the new entry with the new CAs. */
2479 if (ssl_store_load_ca_from_buf(appctx->ctx.ssl.new_cafile_entry, payload)) {
2480 memprintf(&err, "%sInvalid payload\n", err ? err : "");
2481 errcode |= ERR_ALERT | ERR_FATAL;
2482 goto end;
2483 }
2484
2485 /* we succeed, we can save the ca in the transaction */
2486
2487 /* if there wasn't a transaction, update the old CA */
2488 if (!cafile_transaction.old_cafile_entry) {
2489 cafile_transaction.old_cafile_entry = appctx->ctx.ssl.old_cafile_entry;
2490 cafile_transaction.path = appctx->ctx.ssl.path;
2491 err = memprintf(&err, "transaction created for CA %s!\n", cafile_transaction.path);
2492 } else {
2493 err = memprintf(&err, "transaction updated for CA %s!\n", cafile_transaction.path);
2494 }
2495
2496 /* free the previous CA if there was a transaction */
2497 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2498
2499 cafile_transaction.new_cafile_entry = appctx->ctx.ssl.new_cafile_entry;
2500
2501 /* creates the SNI ctxs later in the IO handler */
2502
2503end:
2504 free_trash_chunk(buf);
2505
2506 if (errcode & ERR_CODE) {
2507 ssl_store_delete_cafile_entry(appctx->ctx.ssl.new_cafile_entry);
2508 appctx->ctx.ssl.new_cafile_entry = NULL;
2509 appctx->ctx.ssl.old_cafile_entry = NULL;
2510
Tim Duesterhus025b93e2021-11-04 21:03:52 +01002511 ha_free(&appctx->ctx.ssl.path);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002512
2513 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2514 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2515 } else {
2516
2517 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2518 return cli_dynmsg(appctx, LOG_NOTICE, err);
2519 }
2520}
2521
2522
2523/*
2524 * Parsing function of 'commit ssl ca-file'
2525 */
2526static int cli_parse_commit_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2527{
2528 char *err = NULL;
2529
2530 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2531 return 1;
2532
2533 if (!*args[3])
2534 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
2535
2536 /* The operations on the CKCH architecture are locked so we can
2537 * manipulate ckch_store and ckch_inst */
2538 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2539 return cli_err(appctx, "Can't commit the CA file!\nOperations on certificates are currently locked!\n");
2540
2541 if (!cafile_transaction.path) {
2542 memprintf(&err, "No ongoing transaction! !\n");
2543 goto error;
2544 }
2545
2546 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2547 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, args[3]);
2548 goto error;
2549 }
2550 /* init the appctx structure */
2551 appctx->st2 = SETCERT_ST_INIT;
2552 appctx->ctx.ssl.next_ckchi_link = NULL;
2553 appctx->ctx.ssl.old_cafile_entry = cafile_transaction.old_cafile_entry;
2554 appctx->ctx.ssl.new_cafile_entry = cafile_transaction.new_cafile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002555 appctx->ctx.ssl.cafile_type = CAFILE_CERT;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002556
2557 return 0;
2558
2559error:
2560
2561 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2562 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2563
2564 return cli_dynerr(appctx, err);
2565}
2566
2567enum {
2568 CREATE_NEW_INST_OK = 0,
2569 CREATE_NEW_INST_YIELD = -1,
2570 CREATE_NEW_INST_ERR = -2
2571};
2572
2573static inline int __create_new_instance(struct appctx *appctx, struct ckch_inst *ckchi, int *count,
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02002574 struct buffer *trash, char **err)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002575{
2576 struct ckch_inst *new_inst;
2577
2578 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2579 if (*count >= 10) {
2580 /* save the next ckchi to compute */
2581 appctx->ctx.ssl.next_ckchi = ckchi;
2582 return CREATE_NEW_INST_YIELD;
2583 }
2584
2585 /* Rebuild a new ckch instance that uses the same ckch_store
2586 * than a reference ckchi instance but will use a new CA file. */
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02002587 if (ckch_inst_rebuild(ckchi->ckch_store, ckchi, &new_inst, err))
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002588 return CREATE_NEW_INST_ERR;
2589
2590 /* display one dot per new instance */
2591 chunk_appendf(trash, ".");
2592 ++(*count);
2593
2594 return CREATE_NEW_INST_OK;
2595}
2596
2597/*
2598 * This function tries to create new ckch instances and their SNIs using a newly
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002599 * set certificate authority (CA file) or a newly set Certificate Revocation
2600 * List (CRL), depending on the command being called.
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002601 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002602static int cli_io_handler_commit_cafile_crlfile(struct appctx *appctx)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002603{
2604 struct stream_interface *si = appctx->owner;
2605 int y = 0;
2606 char *err = NULL;
Remi Tricot-Le Bretona6b27842021-05-18 10:06:00 +02002607 struct cafile_entry *old_cafile_entry = NULL, *new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002608 struct ckch_inst_link *ckchi_link;
2609 struct buffer *trash = alloc_trash_chunk();
2610
2611 if (trash == NULL)
2612 goto error;
2613
2614 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
2615 goto error;
2616
2617 while (1) {
2618 switch (appctx->st2) {
2619 case SETCERT_ST_INIT:
2620 /* This state just print the update message */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002621 switch (appctx->ctx.ssl.cafile_type) {
2622 case CAFILE_CERT:
2623 chunk_printf(trash, "Committing %s", cafile_transaction.path);
2624 break;
2625 case CAFILE_CRL:
2626 chunk_printf(trash, "Committing %s", crlfile_transaction.path);
2627 break;
2628 default:
2629 goto error;
2630 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002631 if (ci_putchk(si_ic(si), trash) == -1) {
2632 si_rx_room_blk(si);
2633 goto yield;
2634 }
2635 appctx->st2 = SETCERT_ST_GEN;
2636 /* fallthrough */
2637 case SETCERT_ST_GEN:
2638 /*
2639 * This state generates the ckch instances with their
2640 * sni_ctxs and SSL_CTX.
2641 *
2642 * Since the SSL_CTX generation can be CPU consumer, we
2643 * yield every 10 instances.
2644 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002645 switch (appctx->ctx.ssl.cafile_type) {
2646 case CAFILE_CERT:
2647 old_cafile_entry = appctx->ctx.ssl.old_cafile_entry;
2648 new_cafile_entry = appctx->ctx.ssl.new_cafile_entry;
2649 break;
2650 case CAFILE_CRL:
2651 old_cafile_entry = appctx->ctx.ssl.old_crlfile_entry;
2652 new_cafile_entry = appctx->ctx.ssl.new_crlfile_entry;
2653 break;
2654 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002655 if (!new_cafile_entry)
2656 continue;
2657
2658 /* get the next ckchi to regenerate */
2659 ckchi_link = appctx->ctx.ssl.next_ckchi_link;
2660 /* we didn't start yet, set it to the first elem */
2661 if (ckchi_link == NULL) {
2662 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2663 /* Add the newly created cafile_entry to the tree so that
2664 * any new ckch instance created from now can use it. */
2665 if (ssl_store_add_uncommitted_cafile_entry(new_cafile_entry))
2666 goto error;
2667 }
2668
2669 list_for_each_entry_from(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02002670 switch (__create_new_instance(appctx, ckchi_link->ckch_inst, &y, trash, &err)) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002671 case CREATE_NEW_INST_YIELD:
2672 appctx->ctx.ssl.next_ckchi_link = ckchi_link;
2673 goto yield;
2674 case CREATE_NEW_INST_ERR:
2675 goto error;
2676 default: break;
2677 }
2678 }
2679
2680 appctx->st2 = SETCERT_ST_INSERT;
2681 /* fallthrough */
2682 case SETCERT_ST_INSERT:
2683 /* The generation is finished, we can insert everything */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002684 switch (appctx->ctx.ssl.cafile_type) {
2685 case CAFILE_CERT:
2686 old_cafile_entry = appctx->ctx.ssl.old_cafile_entry;
2687 new_cafile_entry = appctx->ctx.ssl.new_cafile_entry;
2688 break;
2689 case CAFILE_CRL:
2690 old_cafile_entry = appctx->ctx.ssl.old_crlfile_entry;
2691 new_cafile_entry = appctx->ctx.ssl.new_crlfile_entry;
2692 break;
2693 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002694 if (!new_cafile_entry)
2695 continue;
2696
2697 /* insert the new ckch_insts in the crtlist_entry */
2698 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2699 if (ckchi_link->ckch_inst->crtlist_entry)
2700 LIST_INSERT(&ckchi_link->ckch_inst->crtlist_entry->ckch_inst,
2701 &ckchi_link->ckch_inst->by_crtlist_entry);
2702 }
2703
2704 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2705 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2706 __ssl_sock_load_new_ckch_instance(ckchi_link->ckch_inst);
2707 }
2708
2709 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
2710 list_for_each_entry(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
2711 __ckch_inst_free_locked(ckchi_link->ckch_inst);
2712 }
2713
2714
2715 /* Remove the old cafile entry from the tree */
2716 ebmb_delete(&old_cafile_entry->node);
2717 ssl_store_delete_cafile_entry(old_cafile_entry);
2718
2719 appctx->st2 = SETCERT_ST_FIN;
2720 /* fallthrough */
2721 case SETCERT_ST_FIN:
2722 /* we achieved the transaction, we can set everything to NULL */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002723 switch (appctx->ctx.ssl.cafile_type) {
2724 case CAFILE_CERT:
2725 ha_free(&cafile_transaction.path);
2726 cafile_transaction.old_cafile_entry = NULL;
2727 cafile_transaction.new_cafile_entry = NULL;
2728 break;
2729 case CAFILE_CRL:
2730 ha_free(&crlfile_transaction.path);
2731 crlfile_transaction.old_crlfile_entry = NULL;
2732 crlfile_transaction.new_crlfile_entry = NULL;
2733 break;
2734 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002735 goto end;
2736 }
2737 }
2738end:
2739
2740 chunk_appendf(trash, "\n");
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002741 chunk_appendf(trash, "Success!\n");
2742 if (ci_putchk(si_ic(si), trash) == -1)
2743 si_rx_room_blk(si);
2744 free_trash_chunk(trash);
2745 /* success: call the release function and don't come back */
2746 return 1;
2747yield:
2748 /* store the state */
2749 if (ci_putchk(si_ic(si), trash) == -1)
2750 si_rx_room_blk(si);
2751 free_trash_chunk(trash);
2752 si_rx_endp_more(si); /* let's come back later */
2753 return 0; /* should come back */
2754
2755error:
2756 /* spin unlock and free are done in the release function */
2757 if (trash) {
2758 chunk_appendf(trash, "\n%sFailed!\n", err);
2759 if (ci_putchk(si_ic(si), trash) == -1)
2760 si_rx_room_blk(si);
2761 free_trash_chunk(trash);
2762 }
2763 /* error: call the release function and don't come back */
2764 return 1;
2765}
2766
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002767
2768/* parsing function of 'abort ssl ca-file' */
2769static int cli_parse_abort_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2770{
2771 char *err = NULL;
2772
2773 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2774 return 1;
2775
2776 if (!*args[3])
2777 return cli_err(appctx, "'abort ssl ca-file' expects a filename\n");
2778
2779 /* The operations on the CKCH architecture are locked so we can
2780 * manipulate ckch_store and ckch_inst */
2781 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2782 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2783
2784 if (!cafile_transaction.path) {
2785 memprintf(&err, "No ongoing transaction!\n");
2786 goto error;
2787 }
2788
2789 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2790 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", cafile_transaction.path, args[3]);
2791 goto error;
2792 }
2793
2794 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
2795 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2796 cafile_transaction.new_cafile_entry = NULL;
2797 cafile_transaction.old_cafile_entry = NULL;
2798 ha_free(&cafile_transaction.path);
2799
2800 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2801
2802 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2803 return cli_dynmsg(appctx, LOG_NOTICE, err);
2804
2805error:
2806 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2807
2808 return cli_dynerr(appctx, err);
2809}
2810
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002811/* release function of the `commit ssl ca-file' command, free things and unlock the spinlock */
2812static void cli_release_commit_cafile(struct appctx *appctx)
2813{
2814 if (appctx->st2 != SETCERT_ST_FIN) {
2815 struct cafile_entry *new_cafile_entry = appctx->ctx.ssl.new_cafile_entry;
2816
2817 /* Remove the uncommitted cafile_entry from the tree. */
2818 ebmb_delete(&new_cafile_entry->node);
2819 ssl_store_delete_cafile_entry(new_cafile_entry);
2820 }
2821 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2822}
2823
2824
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002825/* IO handler of details "show ssl ca-file <filename[:index]>" */
2826static int cli_io_handler_show_cafile_detail(struct appctx *appctx)
2827{
2828 struct stream_interface *si = appctx->owner;
2829 struct cafile_entry *cafile_entry = appctx->ctx.cli.p0;
2830 struct buffer *out = alloc_trash_chunk();
2831 int i;
2832 X509 *cert;
2833 STACK_OF(X509_OBJECT) *objs;
2834 int retval = 0;
2835 long ca_index = (long)appctx->ctx.cli.p1;
2836
2837 if (!out)
2838 goto end_no_putchk;
2839
2840 chunk_appendf(out, "Filename: ");
2841 if (cafile_entry == cafile_transaction.new_cafile_entry)
2842 chunk_appendf(out, "*");
2843 chunk_appendf(out, "%s\n", cafile_entry->path);
2844
2845 chunk_appendf(out, "Status: ");
2846 if (!cafile_entry->ca_store)
2847 chunk_appendf(out, "Empty\n");
2848 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
2849 chunk_appendf(out, "Unused\n");
2850 else
2851 chunk_appendf(out, "Used\n");
2852
2853 if (!cafile_entry->ca_store)
2854 goto end;
2855
2856 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
2857 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
2858 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
2859 if (!cert)
2860 continue;
2861
2862 /* Certificate indexes start at 1 on the CLI output. */
2863 if (ca_index && ca_index-1 != i)
2864 continue;
2865
2866 chunk_appendf(out, "\nCertificate #%d:\n", i+1);
2867 retval = show_cert_detail(cert, NULL, out);
2868 if (retval < 0)
2869 goto end_no_putchk;
2870 else if (retval || ca_index)
2871 goto end;
2872 }
2873
2874end:
2875 if (ci_putchk(si_ic(si), out) == -1) {
2876 si_rx_room_blk(si);
2877 goto yield;
2878 }
2879
2880end_no_putchk:
2881 free_trash_chunk(out);
2882 return 1;
2883yield:
2884 free_trash_chunk(out);
2885 return 0; /* should come back */
2886}
2887
2888
2889/* parsing function for 'show ssl ca-file [cafile[:index]]' */
2890static int cli_parse_show_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2891{
2892 struct cafile_entry *cafile_entry;
2893 long ca_index = 0;
2894 char *colons;
2895 char *err = NULL;
2896
2897 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
2898 return cli_err(appctx, "Can't allocate memory!\n");
2899
2900 /* The operations on the CKCH architecture are locked so we can
2901 * manipulate ckch_store and ckch_inst */
2902 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2903 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
2904
2905 /* check if there is a certificate to lookup */
2906 if (*args[3]) {
2907
2908 /* Look for an optional CA index after the CA file name */
2909 colons = strchr(args[3], ':');
2910 if (colons) {
2911 char *endptr;
2912
2913 ca_index = strtol(colons + 1, &endptr, 10);
2914 /* Indexes start at 1 */
2915 if (colons + 1 == endptr || *endptr != '\0' || ca_index <= 0) {
2916 memprintf(&err, "wrong CA index after colons in '%s'!", args[3]);
2917 goto error;
2918 }
2919 *colons = '\0';
2920 }
2921
2922 if (*args[3] == '*') {
2923 if (!cafile_transaction.new_cafile_entry)
2924 goto error;
2925
2926 cafile_entry = cafile_transaction.new_cafile_entry;
2927
2928 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
2929 goto error;
2930
2931 } else {
2932 /* Get the "original" cafile_entry and not the
2933 * uncommitted one if it exists. */
2934 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CERT)
2935 goto error;
2936 }
2937
2938 appctx->ctx.cli.p0 = cafile_entry;
2939 appctx->ctx.cli.p1 = (void*)ca_index;
2940 /* use the IO handler that shows details */
2941 appctx->io_handler = cli_io_handler_show_cafile_detail;
2942 }
2943
2944 return 0;
2945
2946error:
2947 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2948 if (err)
2949 return cli_dynerr(appctx, err);
2950 return cli_err(appctx, "Can't display the CA file : Not found!\n");
2951}
2952
2953
2954/* release function of the 'show ssl ca-file' command */
2955static void cli_release_show_cafile(struct appctx *appctx)
2956{
2957 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2958}
2959
2960
2961/* This function returns the number of certificates in a cafile_entry. */
2962static int get_certificate_count(struct cafile_entry *cafile_entry)
2963{
2964 int cert_count = 0;
2965 STACK_OF(X509_OBJECT) *objs;
2966
2967 if (cafile_entry && cafile_entry->ca_store) {
2968 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
2969 if (objs)
2970 cert_count = sk_X509_OBJECT_num(objs);
2971 }
2972 return cert_count;
2973}
2974
2975/* IO handler of "show ssl ca-file". The command taking a specific CA file name
2976 * is managed in cli_io_handler_show_cafile_detail. */
2977static int cli_io_handler_show_cafile(struct appctx *appctx)
2978{
2979 struct buffer *trash = alloc_trash_chunk();
2980 struct ebmb_node *node;
2981 struct stream_interface *si = appctx->owner;
2982 struct cafile_entry *cafile_entry;
2983
2984 if (trash == NULL)
2985 return 1;
2986
2987 if (!appctx->ctx.ssl.old_cafile_entry) {
2988 if (cafile_transaction.old_cafile_entry) {
2989 chunk_appendf(trash, "# transaction\n");
2990 chunk_appendf(trash, "*%s", cafile_transaction.old_cafile_entry->path);
2991
2992 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_transaction.new_cafile_entry));
2993 }
2994 }
2995
2996 /* First time in this io_handler. */
2997 if (!appctx->ctx.cli.p0) {
2998 chunk_appendf(trash, "# filename\n");
2999 node = ebmb_first(&cafile_tree);
3000 } else {
3001 /* We yielded during a previous call. */
3002 node = &((struct cafile_entry*)appctx->ctx.cli.p0)->node;
3003 }
3004
3005 while (node) {
3006 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3007 if (cafile_entry->type == CAFILE_CERT) {
3008 chunk_appendf(trash, "%s", cafile_entry->path);
3009
3010 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_entry));
3011 }
3012
3013 node = ebmb_next(node);
3014 if (ci_putchk(si_ic(si), trash) == -1) {
3015 si_rx_room_blk(si);
3016 goto yield;
3017 }
3018 }
3019
3020 appctx->ctx.cli.p0 = NULL;
3021 free_trash_chunk(trash);
3022 return 1;
3023yield:
3024
3025 free_trash_chunk(trash);
3026 appctx->ctx.cli.p0 = cafile_entry;
3027 return 0; /* should come back */
3028}
3029
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003030/* parsing function of 'del ssl ca-file' */
3031static int cli_parse_del_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3032{
3033 struct cafile_entry *cafile_entry;
3034 char *err = NULL;
3035 char *filename;
3036
3037 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3038 return 1;
3039
3040 if (!*args[3])
3041 return cli_err(appctx, "'del ssl ca-file' expects a CA file name\n");
3042
3043 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3044 return cli_err(appctx, "Can't delete the CA file!\nOperations on certificates are currently locked!\n");
3045
3046 filename = args[3];
3047
3048 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3049 if (!cafile_entry) {
3050 memprintf(&err, "CA file '%s' doesn't exist!\n", filename);
3051 goto error;
3052 }
3053
3054 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3055 memprintf(&err, "CA file '%s' in use, can't be deleted!\n", filename);
3056 goto error;
3057 }
3058
3059 /* Remove the cafile_entry from the tree */
3060 ebmb_delete(&cafile_entry->node);
3061 ssl_store_delete_cafile_entry(cafile_entry);
3062
3063 memprintf(&err, "CA file '%s' deleted!\n", filename);
3064
3065 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3066 return cli_dynmsg(appctx, LOG_NOTICE, err);
3067
3068error:
3069 memprintf(&err, "Can't remove the CA file: %s\n", err ? err : "");
3070 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3071 return cli_dynerr(appctx, err);
3072}
3073
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003074/* parsing function of 'new ssl crl-file' */
3075static int cli_parse_new_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3076{
3077 struct cafile_entry *cafile_entry;
3078 char *err = NULL;
3079 char *path;
3080
3081 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3082 return 1;
3083
3084 if (!*args[3])
3085 return cli_err(appctx, "'new ssl crl-file' expects a filename\n");
3086
3087 path = args[3];
3088
3089 /* The operations on the CKCH architecture are locked so we can
3090 * manipulate ckch_store and ckch_inst */
3091 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3092 return cli_err(appctx, "Can't create a CA file!\nOperations on certificates are currently locked!\n");
3093
3094 cafile_entry = ssl_store_get_cafile_entry(path, 0);
3095 if (cafile_entry) {
3096 memprintf(&err, "CRL file '%s' already exists!\n", path);
3097 goto error;
3098 }
3099
3100 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CRL);
3101 if (!cafile_entry) {
3102 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3103 goto error;
3104 }
3105
3106 /* Add the newly created cafile_entry to the tree so that
3107 * any new ckch instance created from now can use it. */
3108 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
3109 goto error;
3110
3111 memprintf(&err, "New CRL file created '%s'!\n", path);
3112
3113 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3114 return cli_dynmsg(appctx, LOG_NOTICE, err);
3115error:
3116 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3117 return cli_dynerr(appctx, err);
3118}
3119
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003120/* Parsing function of `set ssl crl-file` */
3121static int cli_parse_set_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3122{
3123 char *err = NULL;
3124 int errcode = 0;
3125 struct buffer *buf;
3126
3127 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3128 return 1;
3129
3130 if (!*args[3] || !payload)
3131 return cli_err(appctx, "'set ssl crl-file expects a filename and CAs as a payload\n");
3132
3133 /* The operations on the CKCH architecture are locked so we can
3134 * manipulate ckch_store and ckch_inst */
3135 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3136 return cli_err(appctx, "Can't update the CRL file!\nOperations on certificates are currently locked!\n");
3137
3138 if ((buf = alloc_trash_chunk()) == NULL) {
3139 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3140 errcode |= ERR_ALERT | ERR_FATAL;
3141 goto end;
3142 }
3143
3144 if (!chunk_strcpy(buf, args[3])) {
3145 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3146 errcode |= ERR_ALERT | ERR_FATAL;
3147 goto end;
3148 }
3149
3150 appctx->ctx.ssl.old_crlfile_entry = NULL;
3151 appctx->ctx.ssl.new_crlfile_entry = NULL;
3152
3153 /* if there is an ongoing transaction */
3154 if (crlfile_transaction.path) {
3155 /* if there is an ongoing transaction, check if this is the same file */
3156 if (strcmp(crlfile_transaction.path, buf->area) != 0) {
3157 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, buf->area);
3158 errcode |= ERR_ALERT | ERR_FATAL;
3159 goto end;
3160 }
3161 appctx->ctx.ssl.old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
3162 }
3163 else {
3164 /* lookup for the certificate in the tree */
3165 appctx->ctx.ssl.old_crlfile_entry = ssl_store_get_cafile_entry(buf->area, 0);
3166 }
3167
3168 if (!appctx->ctx.ssl.old_crlfile_entry) {
3169 memprintf(&err, "%sCan't replace a CRL file which is not referenced by the configuration!\n",
3170 err ? err : "");
3171 errcode |= ERR_ALERT | ERR_FATAL;
3172 goto end;
3173 }
3174
3175 if (!appctx->ctx.ssl.path) {
3176 /* this is a new transaction, set the path of the transaction */
3177 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_crlfile_entry->path);
3178 if (!appctx->ctx.ssl.path) {
3179 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3180 errcode |= ERR_ALERT | ERR_FATAL;
3181 goto end;
3182 }
3183 }
3184
3185 if (appctx->ctx.ssl.new_crlfile_entry)
3186 ssl_store_delete_cafile_entry(appctx->ctx.ssl.new_crlfile_entry);
3187
3188 /* Create a new cafile_entry without adding it to the cafile tree. */
3189 appctx->ctx.ssl.new_crlfile_entry = ssl_store_create_cafile_entry(appctx->ctx.ssl.path, NULL, CAFILE_CRL);
3190 if (!appctx->ctx.ssl.new_crlfile_entry) {
3191 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3192 errcode |= ERR_ALERT | ERR_FATAL;
3193 goto end;
3194 }
3195
3196 /* Fill the new entry with the new CRL. */
3197 if (ssl_store_load_ca_from_buf(appctx->ctx.ssl.new_crlfile_entry, payload)) {
3198 memprintf(&err, "%sInvalid payload\n", err ? err : "");
3199 errcode |= ERR_ALERT | ERR_FATAL;
3200 goto end;
3201 }
3202
3203 /* we succeed, we can save the crl in the transaction */
3204
3205 /* if there wasn't a transaction, update the old CA */
3206 if (!crlfile_transaction.old_crlfile_entry) {
3207 crlfile_transaction.old_crlfile_entry = appctx->ctx.ssl.old_crlfile_entry;
3208 crlfile_transaction.path = appctx->ctx.ssl.path;
3209 err = memprintf(&err, "transaction created for CA %s!\n", crlfile_transaction.path);
3210 } else {
3211 err = memprintf(&err, "transaction updated for CA %s!\n", crlfile_transaction.path);
3212 }
3213
3214 /* free the previous CRL file if there was a transaction */
3215 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3216
3217 crlfile_transaction.new_crlfile_entry = appctx->ctx.ssl.new_crlfile_entry;
3218
3219 /* creates the SNI ctxs later in the IO handler */
3220
3221end:
3222 free_trash_chunk(buf);
3223
3224 if (errcode & ERR_CODE) {
3225 ssl_store_delete_cafile_entry(appctx->ctx.ssl.new_crlfile_entry);
3226 appctx->ctx.ssl.new_crlfile_entry = NULL;
3227 appctx->ctx.ssl.old_crlfile_entry = NULL;
3228
Tim Duesterhus025b93e2021-11-04 21:03:52 +01003229 ha_free(&appctx->ctx.ssl.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003230
3231 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3232 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
3233 } else {
3234
3235 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3236 return cli_dynmsg(appctx, LOG_NOTICE, err);
3237 }
3238}
3239
3240/* Parsing function of 'commit ssl crl-file' */
3241static int cli_parse_commit_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3242{
3243 char *err = NULL;
3244
3245 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3246 return 1;
3247
3248 if (!*args[3])
3249 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
3250
3251 /* The operations on the CKCH architecture are locked so we can
3252 * manipulate ckch_store and ckch_inst */
3253 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3254 return cli_err(appctx, "Can't commit the CRL file!\nOperations on certificates are currently locked!\n");
3255
3256 if (!crlfile_transaction.path) {
3257 memprintf(&err, "No ongoing transaction! !\n");
3258 goto error;
3259 }
3260
3261 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3262 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, args[3]);
3263 goto error;
3264 }
3265 /* init the appctx structure */
3266 appctx->st2 = SETCERT_ST_INIT;
3267 appctx->ctx.ssl.next_ckchi = NULL;
3268 appctx->ctx.ssl.old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
3269 appctx->ctx.ssl.new_crlfile_entry = crlfile_transaction.new_crlfile_entry;
3270 appctx->ctx.ssl.cafile_type = CAFILE_CRL;
3271
3272 return 0;
3273
3274error:
3275
3276 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3277 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
3278
3279 return cli_dynerr(appctx, err);
3280}
3281
3282
3283/* release function of the `commit ssl crl-file' command, free things and unlock the spinlock */
3284static void cli_release_commit_crlfile(struct appctx *appctx)
3285{
3286 if (appctx->st2 != SETCERT_ST_FIN) {
3287 struct cafile_entry *new_crlfile_entry = appctx->ctx.ssl.new_crlfile_entry;
3288
3289 /* Remove the uncommitted cafile_entry from the tree. */
3290 ebmb_delete(&new_crlfile_entry->node);
3291 ssl_store_delete_cafile_entry(new_crlfile_entry);
3292 }
3293 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3294}
3295
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003296/* parsing function of 'del ssl crl-file' */
3297static int cli_parse_del_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3298{
3299 struct cafile_entry *cafile_entry;
3300 char *err = NULL;
3301 char *filename;
3302
3303 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3304 return 1;
3305
3306 if (!*args[3])
3307 return cli_err(appctx, "'del ssl crl-file' expects a CRL file name\n");
3308
3309 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3310 return cli_err(appctx, "Can't delete the CRL file!\nOperations on certificates are currently locked!\n");
3311
3312 filename = args[3];
3313
3314 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3315 if (!cafile_entry) {
3316 memprintf(&err, "CRL file '%s' doesn't exist!\n", filename);
3317 goto error;
3318 }
3319 if (cafile_entry->type != CAFILE_CRL) {
3320 memprintf(&err, "'del ssl crl-file' does not work on CA files!\n");
3321 goto error;
3322 }
3323
3324 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3325 memprintf(&err, "CRL file '%s' in use, can't be deleted!\n", filename);
3326 goto error;
3327 }
3328
3329 /* Remove the cafile_entry from the tree */
3330 ebmb_delete(&cafile_entry->node);
3331 ssl_store_delete_cafile_entry(cafile_entry);
3332
3333 memprintf(&err, "CRL file '%s' deleted!\n", filename);
3334
3335 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3336 return cli_dynmsg(appctx, LOG_NOTICE, err);
3337
3338error:
3339 memprintf(&err, "Can't remove the CRL file: %s\n", err ? err : "");
3340 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3341 return cli_dynerr(appctx, err);
3342}
3343
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003344/* parsing function of 'abort ssl crl-file' */
3345static int cli_parse_abort_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3346{
3347 char *err = NULL;
3348
3349 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3350 return 1;
3351
3352 if (!*args[3])
3353 return cli_err(appctx, "'abort ssl crl-file' expects a filename\n");
3354
3355 /* The operations on the CKCH architecture are locked so we can
3356 * manipulate ckch_store and ckch_inst */
3357 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3358 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
3359
3360 if (!crlfile_transaction.path) {
3361 memprintf(&err, "No ongoing transaction!\n");
3362 goto error;
3363 }
3364
3365 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3366 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", crlfile_transaction.path, args[3]);
3367 goto error;
3368 }
3369
3370 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3371 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3372 crlfile_transaction.new_crlfile_entry = NULL;
3373 crlfile_transaction.old_crlfile_entry = NULL;
3374 ha_free(&crlfile_transaction.path);
3375
3376 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3377
3378 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3379 return cli_dynmsg(appctx, LOG_NOTICE, err);
3380
3381error:
3382 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3383
3384 return cli_dynerr(appctx, err);
3385}
3386
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003387
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003388/*
3389 * Display a Certificate Resignation List's information.
3390 * The information displayed is inspired by the output of 'openssl crl -in
3391 * crl.pem -text'.
3392 * Returns 0 in case of success.
3393 */
3394static int show_crl_detail(X509_CRL *crl, struct buffer *out)
3395{
3396 BIO *bio = NULL;
3397 struct buffer *tmp = alloc_trash_chunk();
3398 long version;
3399 X509_NAME *issuer;
3400 int write = -1;
3401 STACK_OF(X509_REVOKED) *rev = NULL;
3402 X509_REVOKED *rev_entry = NULL;
3403 int i;
3404
3405 if (!tmp)
3406 return -1;
3407
3408 if ((bio = BIO_new(BIO_s_mem())) == NULL)
3409 goto end;
3410
3411 /* Version (as displayed by 'openssl crl') */
3412 version = X509_CRL_get_version(crl);
3413 chunk_appendf(out, "Version %ld\n", version + 1);
3414
3415 /* Signature Algorithm */
3416 chunk_appendf(out, "Signature Algorithm: %s\n", OBJ_nid2ln(X509_CRL_get_signature_nid(crl)));
3417
3418 /* Issuer */
3419 chunk_appendf(out, "Issuer: ");
3420 if ((issuer = X509_CRL_get_issuer(crl)) == NULL)
3421 goto end;
3422 if ((ssl_sock_get_dn_oneline(issuer, tmp)) == -1)
3423 goto end;
3424 *(tmp->area + tmp->data) = '\0';
3425 chunk_appendf(out, "%s\n", tmp->area);
3426
3427 /* Last Update */
3428 chunk_appendf(out, "Last Update: ");
3429 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003430 if (BIO_reset(bio) == -1)
3431 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003432 if (ASN1_TIME_print(bio, X509_CRL_get0_lastUpdate(crl)) == 0)
3433 goto end;
3434 write = BIO_read(bio, tmp->area, tmp->size-1);
3435 tmp->area[write] = '\0';
3436 chunk_appendf(out, "%s\n", tmp->area);
3437
3438
3439 /* Next Update */
3440 chunk_appendf(out, "Next Update: ");
3441 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003442 if (BIO_reset(bio) == -1)
3443 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003444 if (ASN1_TIME_print(bio, X509_CRL_get0_nextUpdate(crl)) == 0)
3445 goto end;
3446 write = BIO_read(bio, tmp->area, tmp->size-1);
3447 tmp->area[write] = '\0';
3448 chunk_appendf(out, "%s\n", tmp->area);
3449
3450
3451 /* Revoked Certificates */
3452 rev = X509_CRL_get_REVOKED(crl);
3453 if (sk_X509_REVOKED_num(rev) > 0)
3454 chunk_appendf(out, "Revoked Certificates:\n");
3455 else
3456 chunk_appendf(out, "No Revoked Certificates.\n");
3457
3458 for (i = 0; i < sk_X509_REVOKED_num(rev); i++) {
3459 rev_entry = sk_X509_REVOKED_value(rev, i);
3460
3461 /* Serial Number and Revocation Date */
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003462 if (BIO_reset(bio) == -1)
3463 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003464 BIO_printf(bio , " Serial Number: ");
Remi Tricot-Le Breton18c7d832021-05-17 18:38:34 +02003465 i2a_ASN1_INTEGER(bio, (ASN1_INTEGER*)X509_REVOKED_get0_serialNumber(rev_entry));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003466 BIO_printf(bio, "\n Revocation Date: ");
Remi Tricot-Le Bretona6b27842021-05-18 10:06:00 +02003467 if (ASN1_TIME_print(bio, X509_REVOKED_get0_revocationDate(rev_entry)) == 0)
3468 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003469 BIO_printf(bio, "\n");
3470
3471 write = BIO_read(bio, tmp->area, tmp->size-1);
3472 tmp->area[write] = '\0';
3473 chunk_appendf(out, "%s", tmp->area);
3474 }
3475
3476end:
3477 free_trash_chunk(tmp);
3478 if (bio)
3479 BIO_free(bio);
3480
3481 return 0;
3482}
3483
3484/* IO handler of details "show ssl crl-file <filename[:index]>" */
3485static int cli_io_handler_show_crlfile_detail(struct appctx *appctx)
3486{
3487 struct stream_interface *si = appctx->owner;
3488 struct cafile_entry *cafile_entry = appctx->ctx.cli.p0;
3489 struct buffer *out = alloc_trash_chunk();
3490 int i;
3491 X509_CRL *crl;
3492 STACK_OF(X509_OBJECT) *objs;
3493 int retval = 0;
3494 long index = (long)appctx->ctx.cli.p1;
3495
3496 if (!out)
3497 goto end_no_putchk;
3498
3499 chunk_appendf(out, "Filename: ");
3500 if (cafile_entry == crlfile_transaction.new_crlfile_entry)
3501 chunk_appendf(out, "*");
3502 chunk_appendf(out, "%s\n", cafile_entry->path);
3503
3504 chunk_appendf(out, "Status: ");
3505 if (!cafile_entry->ca_store)
3506 chunk_appendf(out, "Empty\n");
3507 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3508 chunk_appendf(out, "Unused\n");
3509 else
3510 chunk_appendf(out, "Used\n");
3511
3512 if (!cafile_entry->ca_store)
3513 goto end;
3514
3515 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3516 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
3517 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
3518 if (!crl)
3519 continue;
3520
3521 /* CRL indexes start at 1 on the CLI output. */
3522 if (index && index-1 != i)
3523 continue;
3524
3525 chunk_appendf(out, "\nCertificate Revocation List #%d:\n", i+1);
3526 retval = show_crl_detail(crl, out);
3527 if (retval < 0)
3528 goto end_no_putchk;
3529 else if (retval || index)
3530 goto end;
3531 }
3532
3533end:
3534 if (ci_putchk(si_ic(si), out) == -1) {
3535 si_rx_room_blk(si);
3536 goto yield;
3537 }
3538
3539end_no_putchk:
3540 free_trash_chunk(out);
3541 return 1;
3542yield:
3543 free_trash_chunk(out);
3544 return 0; /* should come back */
3545}
3546
3547/* parsing function for 'show ssl crl-file [crlfile[:index]]' */
3548static int cli_parse_show_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3549{
3550 struct cafile_entry *cafile_entry;
3551 long index = 0;
3552 char *colons;
3553 char *err = NULL;
3554
3555 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3556 return cli_err(appctx, "Can't allocate memory!\n");
3557
3558 /* The operations on the CKCH architecture are locked so we can
3559 * manipulate ckch_store and ckch_inst */
3560 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3561 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3562
3563 /* check if there is a certificate to lookup */
3564 if (*args[3]) {
3565
3566 /* Look for an optional index after the CRL file name */
3567 colons = strchr(args[3], ':');
3568 if (colons) {
3569 char *endptr;
3570
3571 index = strtol(colons + 1, &endptr, 10);
3572 /* Indexes start at 1 */
3573 if (colons + 1 == endptr || *endptr != '\0' || index <= 0) {
3574 memprintf(&err, "wrong CRL index after colons in '%s'!", args[3]);
3575 goto error;
3576 }
3577 *colons = '\0';
3578 }
3579
3580 if (*args[3] == '*') {
3581 if (!crlfile_transaction.new_crlfile_entry)
3582 goto error;
3583
3584 cafile_entry = crlfile_transaction.new_crlfile_entry;
3585
3586 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3587 goto error;
3588
3589 } else {
3590 /* Get the "original" cafile_entry and not the
3591 * uncommitted one if it exists. */
3592 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CRL)
3593 goto error;
3594 }
3595
3596 appctx->ctx.cli.p0 = cafile_entry;
3597 appctx->ctx.cli.p1 = (void*)index;
3598 /* use the IO handler that shows details */
3599 appctx->io_handler = cli_io_handler_show_crlfile_detail;
3600 }
3601
3602 return 0;
3603
3604error:
3605 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3606 if (err)
3607 return cli_dynerr(appctx, err);
3608 return cli_err(appctx, "Can't display the CA file : Not found!\n");
3609}
3610
3611/* IO handler of "show ssl crl-file". The command taking a specific CRL file name
3612 * is managed in cli_io_handler_show_crlfile_detail. */
3613static int cli_io_handler_show_crlfile(struct appctx *appctx)
3614{
3615 struct buffer *trash = alloc_trash_chunk();
3616 struct ebmb_node *node;
3617 struct stream_interface *si = appctx->owner;
3618 struct cafile_entry *cafile_entry;
3619
3620 if (trash == NULL)
3621 return 1;
3622
3623 if (!appctx->ctx.ssl.old_crlfile_entry) {
3624 if (crlfile_transaction.old_crlfile_entry) {
3625 chunk_appendf(trash, "# transaction\n");
3626 chunk_appendf(trash, "*%s\n", crlfile_transaction.old_crlfile_entry->path);
3627 }
3628 }
3629
3630 /* First time in this io_handler. */
3631 if (!appctx->ctx.cli.p0) {
3632 chunk_appendf(trash, "# filename\n");
3633 node = ebmb_first(&cafile_tree);
3634 } else {
3635 /* We yielded during a previous call. */
3636 node = &((struct cafile_entry*)appctx->ctx.cli.p0)->node;
3637 }
3638
3639 while (node) {
3640 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3641 if (cafile_entry->type == CAFILE_CRL) {
3642 chunk_appendf(trash, "%s\n", cafile_entry->path);
3643 }
3644
3645 node = ebmb_next(node);
3646 if (ci_putchk(si_ic(si), trash) == -1) {
3647 si_rx_room_blk(si);
3648 goto yield;
3649 }
3650 }
3651
3652 appctx->ctx.cli.p0 = NULL;
3653 free_trash_chunk(trash);
3654 return 1;
3655yield:
3656
3657 free_trash_chunk(trash);
3658 appctx->ctx.cli.p0 = cafile_entry;
3659 return 0; /* should come back */
3660}
3661
3662
3663/* release function of the 'show ssl crl-file' command */
3664static void cli_release_show_crlfile(struct appctx *appctx)
3665{
3666 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3667}
3668
3669
William Lallemandee8530c2020-06-23 18:19:42 +02003670void ckch_deinit()
3671{
3672 struct eb_node *node, *next;
3673 struct ckch_store *store;
3674
3675 node = eb_first(&ckchs_tree);
3676 while (node) {
3677 next = eb_next(node);
3678 store = ebmb_entry(node, struct ckch_store, node);
3679 ckch_store_free(store);
3680 node = next;
3681 }
3682}
William Lallemandda8584c2020-05-14 10:14:37 +02003683
3684/* register cli keywords */
3685static struct cli_kw_list cli_kws = {{ },{
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003686 { { "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 },
3687 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
3688 { { "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 },
3689 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
3690 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
3691 { { "show", "ssl", "cert", NULL }, "show ssl cert [<certfile>] : display the SSL certificates used in memory, or the details of a file", cli_parse_show_cert, cli_io_handler_show_cert, cli_release_show_cert },
3692
Amaury Denoyelleb11ad9e2021-05-21 11:01:10 +02003693 { { "new", "ssl", "ca-file", NULL }, "new ssl ca-file <cafile> : create a new CA file to be used in a crt-list", cli_parse_new_cafile, NULL, NULL },
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003694 { { "set", "ssl", "ca-file", NULL }, "set ssl ca-file <cafile> <payload> : replace a CA file", cli_parse_set_cafile, NULL, NULL },
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003695 { { "commit", "ssl", "ca-file", NULL }, "commit ssl ca-file <cafile> : commit a CA file", cli_parse_commit_cafile, cli_io_handler_commit_cafile_crlfile, cli_release_commit_cafile },
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01003696 { { "abort", "ssl", "ca-file", NULL }, "abort ssl ca-file <cafile> : abort a transaction for a CA file", cli_parse_abort_cafile, NULL, NULL },
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003697 { { "del", "ssl", "ca-file", NULL }, "del ssl ca-file <cafile> : delete an unused CA file", cli_parse_del_cafile, NULL, NULL },
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003698 { { "show", "ssl", "ca-file", NULL }, "show ssl ca-file [<cafile>[:<index>]] : display the SSL CA files used in memory, or the details of a <cafile>, or a single certificate of index <index> of a CA file <cafile>", cli_parse_show_cafile, cli_io_handler_show_cafile, cli_release_show_cafile },
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003699
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003700 { { "new", "ssl", "crl-file", NULL }, "new ssl crlfile <crlfile> : create a new CRL file to be used in a crt-list", cli_parse_new_crlfile, NULL, NULL },
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003701 { { "set", "ssl", "crl-file", NULL }, "set ssl crl-file <crlfile> <payload> : replace a CRL file", cli_parse_set_crlfile, NULL, NULL },
3702 { { "commit", "ssl", "crl-file", NULL },"commit ssl crl-file <crlfile> : commit a CRL file", cli_parse_commit_crlfile, cli_io_handler_commit_cafile_crlfile, cli_release_commit_crlfile },
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003703 { { "abort", "ssl", "crl-file", NULL }, "abort ssl crl-file <crlfile> : abort a transaction for a CRL file", cli_parse_abort_crlfile, NULL, NULL },
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003704 { { "del", "ssl", "crl-file", NULL }, "del ssl crl-file <crlfile> : delete an unused CRL file", cli_parse_del_crlfile, NULL, NULL },
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003705 { { "show", "ssl", "crl-file", NULL }, "show ssl crl-file [<crlfile[:<index>>]] : display the SSL CRL files used in memory, or the details of a <crlfile>, or a single CRL of index <index> of CRL file <crlfile>", cli_parse_show_crlfile, cli_io_handler_show_crlfile, cli_release_show_crlfile },
William Lallemandda8584c2020-05-14 10:14:37 +02003706 { { NULL }, NULL, NULL, NULL }
3707}};
3708
3709INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3710