blob: 0f430a469d4b1aca81dd23ef73a30ef148cc8016 [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>
William Lallemand87fd9942022-04-01 20:12:03 +020014#include <dirent.h>
William Lallemand03c331c2020-05-13 10:10:01 +020015#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020020#include <syslog.h>
William Lallemand03c331c2020-05-13 10:10:01 +020021#include <unistd.h>
22
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau74f24562021-10-06 17:54:12 +020026#include <import/ebpttree.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <import/ebsttree.h>
28
Willy Tarreau50c2f1e2022-05-04 19:26:59 +020029#include <haproxy/applet.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020030#include <haproxy/base64.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020031#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020032#include <haproxy/cli.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020033#include <haproxy/errors.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020034#include <haproxy/sc_strm.h>
Willy Tarreau47d7f902020-06-04 14:25:47 +020035#include <haproxy/ssl_ckch.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020036#include <haproxy/ssl_sock.h>
Willy Tarreaub2bd8652020-06-04 14:21:22 +020037#include <haproxy/ssl_utils.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020038#include <haproxy/stconn.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020039#include <haproxy/tools.h>
William Lallemand03c331c2020-05-13 10:10:01 +020040
William Lallemandda8584c2020-05-14 10:14:37 +020041/* Uncommitted CKCH transaction */
42
43static struct {
44 struct ckch_store *new_ckchs;
45 struct ckch_store *old_ckchs;
46 char *path;
47} ckchs_transaction;
48
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +010049/* Uncommitted CA file transaction */
50
51static struct {
52 struct cafile_entry *old_cafile_entry;
53 struct cafile_entry *new_cafile_entry;
54 char *path;
55} cafile_transaction;
William Lallemandda8584c2020-05-14 10:14:37 +020056
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +020057/* Uncommitted CRL file transaction */
58
59static struct {
60 struct cafile_entry *old_crlfile_entry;
61 struct cafile_entry *new_crlfile_entry;
62 char *path;
63} crlfile_transaction;
64
Willy Tarreau50c2f1e2022-05-04 19:26:59 +020065/* CLI context used by "show cafile" */
66struct show_cafile_ctx {
67 struct cafile_entry *cur_cafile_entry;
68 struct cafile_entry *old_cafile_entry;
69 int ca_index;
70 int show_all;
71};
William Lallemand03c331c2020-05-13 10:10:01 +020072
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +020073/* CLI context used by "show crlfile" */
74struct show_crlfile_ctx {
75 struct cafile_entry *cafile_entry;
Christopher Faulet51095ee2022-06-03 10:21:27 +020076 struct cafile_entry *old_crlfile_entry;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +020077 int index;
78};
79
Willy Tarreau96c9a6c2022-05-04 19:51:37 +020080/* CLI context used by "show cert" */
81struct show_cert_ctx {
82 struct ckch_store *old_ckchs;
83 struct ckch_store *cur_ckchs;
84 int transaction;
85};
86
Willy Tarreaua645b6a2022-05-04 19:58:00 +020087/* CLI context used by "commit cert" */
88struct commit_cert_ctx {
89 struct ckch_store *old_ckchs;
90 struct ckch_store *new_ckchs;
91 struct ckch_inst *next_ckchi;
Christopher Faulet9d56e242022-05-31 16:37:01 +020092 char *err;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +020093 enum {
94 CERT_ST_INIT = 0,
95 CERT_ST_GEN,
96 CERT_ST_INSERT,
Christopher Faulet9d56e242022-05-31 16:37:01 +020097 CERT_ST_SUCCESS,
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +020098 CERT_ST_FIN,
Christopher Faulet9d56e242022-05-31 16:37:01 +020099 CERT_ST_ERROR,
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +0200100 } state;
Willy Tarreaua645b6a2022-05-04 19:58:00 +0200101};
102
Willy Tarreaudec23dc2022-05-04 20:25:05 +0200103/* CLI context used by "commit cafile" and "commit crlfile" */
104struct commit_cacrlfile_ctx {
Christopher Faulet6af2fc62022-06-03 11:42:38 +0200105 struct cafile_entry *old_entry;
106 struct cafile_entry *new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +0200107 struct ckch_inst_link *next_ckchi_link;
Christopher Faulet14df9132022-06-03 09:17:09 +0200108 enum cafile_type cafile_type; /* either CA or CRL, depending on the current command */
Christopher Faulete9c3bd12022-05-31 17:51:06 +0200109 char *err;
Willy Tarreau1d6dd802022-05-05 08:17:29 +0200110 enum {
111 CACRL_ST_INIT = 0,
112 CACRL_ST_GEN,
113 CACRL_ST_INSERT,
Christopher Faulete9c3bd12022-05-31 17:51:06 +0200114 CACRL_ST_SUCCESS,
Willy Tarreau1d6dd802022-05-05 08:17:29 +0200115 CACRL_ST_FIN,
Christopher Faulete9c3bd12022-05-31 17:51:06 +0200116 CACRL_ST_ERROR,
Willy Tarreau1d6dd802022-05-05 08:17:29 +0200117 } state;
Willy Tarreaudec23dc2022-05-04 20:25:05 +0200118};
119
Willy Tarreaua37693f2022-05-04 20:12:55 +0200120
William Lallemand03c331c2020-05-13 10:10:01 +0200121/******************** cert_key_and_chain functions *************************
122 * These are the functions that fills a cert_key_and_chain structure. For the
123 * functions filling a SSL_CTX from a cert_key_and_chain, see ssl_sock.c
124 */
125
126/*
127 * Try to parse Signed Certificate Timestamp List structure. This function
128 * makes only basic test if the data seems like SCTL. No signature validation
129 * is performed.
130 */
131static int ssl_sock_parse_sctl(struct buffer *sctl)
132{
133 int ret = 1;
134 int len, pos, sct_len;
135 unsigned char *data;
136
137 if (sctl->data < 2)
138 goto out;
139
140 data = (unsigned char *) sctl->area;
141 len = (data[0] << 8) | data[1];
142
143 if (len + 2 != sctl->data)
144 goto out;
145
146 data = data + 2;
147 pos = 0;
148 while (pos < len) {
149 if (len - pos < 2)
150 goto out;
151
152 sct_len = (data[pos] << 8) | data[pos + 1];
153 if (pos + sct_len + 2 > len)
154 goto out;
155
156 pos += sct_len + 2;
157 }
158
159 ret = 0;
160
161out:
162 return ret;
163}
164
165/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
166 * It fills the ckch->sctl buffer
167 * return 0 on success or != 0 on failure */
168int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err)
169{
170 int fd = -1;
171 int r = 0;
172 int ret = 1;
173 struct buffer tmp;
174 struct buffer *src;
175 struct buffer *sctl;
176
177 if (buf) {
William Lallemand8d673942021-01-27 14:58:51 +0100178 chunk_initstr(&tmp, buf);
William Lallemand03c331c2020-05-13 10:10:01 +0200179 src = &tmp;
180 } else {
181 fd = open(sctl_path, O_RDONLY);
182 if (fd == -1)
183 goto end;
184
185 trash.data = 0;
186 while (trash.data < trash.size) {
187 r = read(fd, trash.area + trash.data, trash.size - trash.data);
188 if (r < 0) {
189 if (errno == EINTR)
190 continue;
191 goto end;
192 }
193 else if (r == 0) {
194 break;
195 }
196 trash.data += r;
197 }
198 src = &trash;
199 }
200
201 ret = ssl_sock_parse_sctl(src);
202 if (ret)
203 goto end;
204
205 sctl = calloc(1, sizeof(*sctl));
206 if (!chunk_dup(sctl, src)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100207 ha_free(&sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200208 goto end;
209 }
210 /* no error, fill ckch with new context, old context must be free */
211 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100212 ha_free(&ckch->sctl->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200213 free(ckch->sctl);
214 }
215 ckch->sctl = sctl;
216 ret = 0;
217end:
218 if (fd != -1)
219 close(fd);
220
221 return ret;
222}
223
224#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
225/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500226 * This function load the OCSP Response in DER format contained in file at
William Lallemand03c331c2020-05-13 10:10:01 +0200227 * path 'ocsp_path' or base64 in a buffer <buf>
228 *
229 * Returns 0 on success, 1 in error case.
230 */
231int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err)
232{
233 int fd = -1;
234 int r = 0;
235 int ret = 1;
236 struct buffer *ocsp_response;
237 struct buffer *src = NULL;
238
239 if (buf) {
240 int i, j;
241 /* if it's from a buffer it will be base64 */
242
243 /* remove \r and \n from the payload */
244 for (i = 0, j = 0; buf[i]; i++) {
245 if (buf[i] == '\r' || buf[i] == '\n')
246 continue;
247 buf[j++] = buf[i];
248 }
249 buf[j] = 0;
250
251 ret = base64dec(buf, j, trash.area, trash.size);
252 if (ret < 0) {
253 memprintf(err, "Error reading OCSP response in base64 format");
254 goto end;
255 }
256 trash.data = ret;
257 src = &trash;
258 } else {
259 fd = open(ocsp_path, O_RDONLY);
260 if (fd == -1) {
261 memprintf(err, "Error opening OCSP response file");
262 goto end;
263 }
264
265 trash.data = 0;
266 while (trash.data < trash.size) {
267 r = read(fd, trash.area + trash.data, trash.size - trash.data);
268 if (r < 0) {
269 if (errno == EINTR)
270 continue;
271
272 memprintf(err, "Error reading OCSP response from file");
273 goto end;
274 }
275 else if (r == 0) {
276 break;
277 }
278 trash.data += r;
279 }
280 close(fd);
281 fd = -1;
282 src = &trash;
283 }
284
285 ocsp_response = calloc(1, sizeof(*ocsp_response));
286 if (!chunk_dup(ocsp_response, src)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100287 ha_free(&ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200288 goto end;
289 }
290 /* no error, fill ckch with new context, old context must be free */
291 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100292 ha_free(&ckch->ocsp_response->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200293 free(ckch->ocsp_response);
294 }
295 ckch->ocsp_response = ocsp_response;
296 ret = 0;
297end:
298 if (fd != -1)
299 close(fd);
300
301 return ret;
302}
303#endif
304
305/*
306 * Try to load in a ckch every files related to a ckch.
307 * (PEM, sctl, ocsp, issuer etc.)
308 *
309 * This function is only used to load files during the configuration parsing,
310 * it is not used with the CLI.
311 *
312 * This allows us to carry the contents of the file without having to read the
313 * file multiple times. The caller must call
314 * ssl_sock_free_cert_key_and_chain_contents.
315 *
316 * returns:
317 * 0 on Success
318 * 1 on SSL Failure
319 */
320int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
321{
William Lallemand8e8581e2020-10-20 17:36:46 +0200322 struct buffer *fp = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200323 int ret = 1;
Remi Tricot-Le Breton9bf3a1f2022-05-09 11:07:13 +0200324 struct stat st;
William Lallemand03c331c2020-05-13 10:10:01 +0200325
326 /* try to load the PEM */
327 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
328 goto end;
329 }
330
William Lallemand8e8581e2020-10-20 17:36:46 +0200331 fp = alloc_trash_chunk();
332 if (!fp) {
333 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
334 goto end;
335 }
336
337 if (!chunk_strcpy(fp, path) || (b_data(fp) > MAXPATHLEN)) {
338 memprintf(err, "%s '%s' filename too long'.\n",
339 err && *err ? *err : "", fp->area);
340 ret = 1;
341 goto end;
342 }
343
William Lallemand089c1382020-10-23 17:35:12 +0200344 /* remove the ".crt" extension */
William Lallemand8e8581e2020-10-20 17:36:46 +0200345 if (global_ssl.extra_files_noext) {
346 char *ext;
347
348 /* look for the extension */
349 if ((ext = strrchr(fp->area, '.'))) {
William Lallemand8e8581e2020-10-20 17:36:46 +0200350
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100351 if (strcmp(ext, ".crt") == 0) {
William Lallemand8e8581e2020-10-20 17:36:46 +0200352 *ext = '\0';
William Lallemand089c1382020-10-23 17:35:12 +0200353 fp->data = strlen(fp->area);
354 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200355 }
356
357 }
358
Remi Tricot-Le Breton1bad7db2022-06-07 16:29:44 +0200359 if (ckch->key == NULL) {
360 /* If no private key was found yet and we cannot look for it in extra
361 * files, raise an error.
362 */
363 if (!(global_ssl.extra_files & SSL_GF_KEY)) {
364 memprintf(err, "%sNo Private Key found in '%s'.\n", err && *err ? *err : "", fp->area);
365 goto end;
366 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200367
Remi Tricot-Le Breton1bad7db2022-06-07 16:29:44 +0200368 /* try to load an external private key if it wasn't in the PEM */
369 if (!chunk_strcat(fp, ".key") || (b_data(fp) > MAXPATHLEN)) {
370 memprintf(err, "%s '%s' filename too long'.\n",
Remi Tricot-Le Breton9bf3a1f2022-05-09 11:07:13 +0200371 err && *err ? *err : "", fp->area);
Remi Tricot-Le Breton1bad7db2022-06-07 16:29:44 +0200372 ret = 1;
William Lallemand8e8581e2020-10-20 17:36:46 +0200373 goto end;
374 }
William Lallemand03c331c2020-05-13 10:10:01 +0200375
Remi Tricot-Le Breton1bad7db2022-06-07 16:29:44 +0200376 if (stat(fp->area, &st) == 0) {
377 if (ssl_sock_load_key_into_ckch(fp->area, NULL, ckch, err)) {
378 memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n",
379 err && *err ? *err : "", fp->area);
380 goto end;
381 }
382 }
383
384 if (ckch->key == NULL) {
385 memprintf(err, "%sNo Private Key found in '%s'.\n", err && *err ? *err : "", fp->area);
386 goto end;
387 }
388 /* remove the added extension */
389 *(fp->area + fp->data - strlen(".key")) = '\0';
390 b_sub(fp, strlen(".key"));
William Lallemand03c331c2020-05-13 10:10:01 +0200391 }
Remi Tricot-Le Breton9bf3a1f2022-05-09 11:07:13 +0200392
William Lallemand03c331c2020-05-13 10:10:01 +0200393
394 if (!X509_check_private_key(ckch->cert, ckch->key)) {
395 memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n",
396 err && *err ? *err : "", path);
397 goto end;
398 }
399
Ilya Shipitsinc47d6762021-02-13 11:45:33 +0500400#ifdef HAVE_SSL_SCTL
William Lallemand03c331c2020-05-13 10:10:01 +0200401 /* try to load the sctl file */
402 if (global_ssl.extra_files & SSL_GF_SCTL) {
William Lallemand03c331c2020-05-13 10:10:01 +0200403 struct stat st;
404
William Lallemand8e8581e2020-10-20 17:36:46 +0200405 if (!chunk_strcat(fp, ".sctl") || b_data(fp) > MAXPATHLEN) {
406 memprintf(err, "%s '%s' filename too long'.\n",
407 err && *err ? *err : "", fp->area);
408 ret = 1;
409 goto end;
410 }
411
412 if (stat(fp->area, &st) == 0) {
413 if (ssl_sock_load_sctl_from_file(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200414 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
William Lallemand8e8581e2020-10-20 17:36:46 +0200415 err && *err ? *err : "", fp->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200416 ret = 1;
417 goto end;
418 }
419 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200420 /* remove the added extension */
421 *(fp->area + fp->data - strlen(".sctl")) = '\0';
422 b_sub(fp, strlen(".sctl"));
William Lallemand03c331c2020-05-13 10:10:01 +0200423 }
424#endif
425
426 /* try to load an ocsp response file */
427 if (global_ssl.extra_files & SSL_GF_OCSP) {
William Lallemand03c331c2020-05-13 10:10:01 +0200428 struct stat st;
429
William Lallemand8e8581e2020-10-20 17:36:46 +0200430 if (!chunk_strcat(fp, ".ocsp") || b_data(fp) > MAXPATHLEN) {
431 memprintf(err, "%s '%s' filename too long'.\n",
432 err && *err ? *err : "", fp->area);
433 ret = 1;
434 goto end;
435 }
436
437 if (stat(fp->area, &st) == 0) {
438 if (ssl_sock_load_ocsp_response_from_file(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200439 ret = 1;
440 goto end;
441 }
442 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200443 /* remove the added extension */
444 *(fp->area + fp->data - strlen(".ocsp")) = '\0';
445 b_sub(fp, strlen(".ocsp"));
William Lallemand03c331c2020-05-13 10:10:01 +0200446 }
447
448#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
449 if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
450 /* if no issuer was found, try to load an issuer from the .issuer */
451 if (!ckch->ocsp_issuer) {
452 struct stat st;
William Lallemand8e8581e2020-10-20 17:36:46 +0200453
454 if (!chunk_strcat(fp, ".issuer") || b_data(fp) > MAXPATHLEN) {
455 memprintf(err, "%s '%s' filename too long'.\n",
456 err && *err ? *err : "", fp->area);
457 ret = 1;
458 goto end;
459 }
William Lallemand03c331c2020-05-13 10:10:01 +0200460
William Lallemand8e8581e2020-10-20 17:36:46 +0200461 if (stat(fp->area, &st) == 0) {
462 if (ssl_sock_load_issuer_file_into_ckch(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200463 ret = 1;
464 goto end;
465 }
466
467 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
468 memprintf(err, "%s '%s' is not an issuer'.\n",
William Lallemand8e8581e2020-10-20 17:36:46 +0200469 err && *err ? *err : "", fp->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200470 ret = 1;
471 goto end;
472 }
473 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200474 /* remove the added extension */
475 *(fp->area + fp->data - strlen(".issuer")) = '\0';
476 b_sub(fp, strlen(".issuer"));
William Lallemand03c331c2020-05-13 10:10:01 +0200477 }
478 }
479#endif
480
481 ret = 0;
482
483end:
484
485 ERR_clear_error();
486
487 /* Something went wrong in one of the reads */
488 if (ret != 0)
489 ssl_sock_free_cert_key_and_chain_contents(ckch);
490
William Lallemand8e8581e2020-10-20 17:36:46 +0200491 free_trash_chunk(fp);
492
William Lallemand03c331c2020-05-13 10:10:01 +0200493 return ret;
494}
495
496/*
497 * Try to load a private key file from a <path> or a buffer <buf>
498 *
499 * If it failed you should not attempt to use the ckch but free it.
500 *
501 * Return 0 on success or != 0 on failure
502 */
503int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
504{
505 BIO *in = NULL;
506 int ret = 1;
507 EVP_PKEY *key = NULL;
508
509 if (buf) {
510 /* reading from a buffer */
511 in = BIO_new_mem_buf(buf, -1);
512 if (in == NULL) {
513 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
514 goto end;
515 }
516
517 } else {
518 /* reading from a file */
519 in = BIO_new(BIO_s_file());
520 if (in == NULL)
521 goto end;
522
523 if (BIO_read_filename(in, path) <= 0)
524 goto end;
525 }
526
527 /* Read Private Key */
528 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
529 if (key == NULL) {
530 memprintf(err, "%sunable to load private key from file '%s'.\n",
531 err && *err ? *err : "", path);
532 goto end;
533 }
534
535 ret = 0;
536
537 SWAP(ckch->key, key);
538
539end:
540
541 ERR_clear_error();
542 if (in)
543 BIO_free(in);
544 if (key)
545 EVP_PKEY_free(key);
546
547 return ret;
548}
549
550/*
551 * Try to load a PEM file from a <path> or a buffer <buf>
552 * The PEM must contain at least a Certificate,
553 * It could contain a DH, a certificate chain and a PrivateKey.
554 *
555 * If it failed you should not attempt to use the ckch but free it.
556 *
557 * Return 0 on success or != 0 on failure
558 */
559int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
560{
561 BIO *in = NULL;
562 int ret = 1;
563 X509 *ca;
564 X509 *cert = NULL;
565 EVP_PKEY *key = NULL;
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100566 HASSL_DH *dh = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200567 STACK_OF(X509) *chain = NULL;
568
569 if (buf) {
570 /* reading from a buffer */
571 in = BIO_new_mem_buf(buf, -1);
572 if (in == NULL) {
573 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
574 goto end;
575 }
576
577 } else {
578 /* reading from a file */
579 in = BIO_new(BIO_s_file());
580 if (in == NULL) {
581 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
582 goto end;
583 }
584
585 if (BIO_read_filename(in, path) <= 0) {
586 memprintf(err, "%scannot open the file '%s'.\n",
587 err && *err ? *err : "", path);
588 goto end;
589 }
590 }
591
592 /* Read Private Key */
593 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
594 /* no need to check for errors here, because the private key could be loaded later */
595
596#ifndef OPENSSL_NO_DH
597 /* Seek back to beginning of file */
598 if (BIO_reset(in) == -1) {
599 memprintf(err, "%san error occurred while reading the file '%s'.\n",
600 err && *err ? *err : "", path);
601 goto end;
602 }
603
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100604 dh = ssl_sock_get_dh_from_bio(in);
605 ERR_clear_error();
William Lallemand03c331c2020-05-13 10:10:01 +0200606 /* no need to return an error there, dh is not mandatory */
607#endif
608
609 /* Seek back to beginning of file */
610 if (BIO_reset(in) == -1) {
611 memprintf(err, "%san error occurred while reading the file '%s'.\n",
612 err && *err ? *err : "", path);
613 goto end;
614 }
615
616 /* Read Certificate */
617 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
618 if (cert == NULL) {
619 memprintf(err, "%sunable to load certificate from file '%s'.\n",
620 err && *err ? *err : "", path);
621 goto end;
622 }
623
624 /* Look for a Certificate Chain */
625 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
626 if (chain == NULL)
627 chain = sk_X509_new_null();
628 if (!sk_X509_push(chain, ca)) {
629 X509_free(ca);
630 goto end;
631 }
632 }
633
634 ret = ERR_get_error();
635 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
636 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
637 err && *err ? *err : "", path);
638 goto end;
639 }
640
641 /* once it loaded the PEM, it should remove everything else in the ckch */
642 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100643 ha_free(&ckch->ocsp_response->area);
644 ha_free(&ckch->ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200645 }
646
647 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100648 ha_free(&ckch->sctl->area);
649 ha_free(&ckch->sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200650 }
651
652 if (ckch->ocsp_issuer) {
653 X509_free(ckch->ocsp_issuer);
654 ckch->ocsp_issuer = NULL;
655 }
656
657 /* no error, fill ckch with new context, old context will be free at end: */
658 SWAP(ckch->key, key);
659 SWAP(ckch->dh, dh);
660 SWAP(ckch->cert, cert);
661 SWAP(ckch->chain, chain);
662
663 ret = 0;
664
665end:
666
667 ERR_clear_error();
668 if (in)
669 BIO_free(in);
670 if (key)
671 EVP_PKEY_free(key);
672 if (dh)
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100673 HASSL_DH_free(dh);
William Lallemand03c331c2020-05-13 10:10:01 +0200674 if (cert)
675 X509_free(cert);
676 if (chain)
677 sk_X509_pop_free(chain, X509_free);
678
679 return ret;
680}
681
682/* Frees the contents of a cert_key_and_chain
683 */
684void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
685{
686 if (!ckch)
687 return;
688
689 /* Free the certificate and set pointer to NULL */
690 if (ckch->cert)
691 X509_free(ckch->cert);
692 ckch->cert = NULL;
693
694 /* Free the key and set pointer to NULL */
695 if (ckch->key)
696 EVP_PKEY_free(ckch->key);
697 ckch->key = NULL;
698
699 /* Free each certificate in the chain */
700 if (ckch->chain)
701 sk_X509_pop_free(ckch->chain, X509_free);
702 ckch->chain = NULL;
703
704 if (ckch->dh)
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100705 HASSL_DH_free(ckch->dh);
William Lallemand03c331c2020-05-13 10:10:01 +0200706 ckch->dh = NULL;
707
708 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100709 ha_free(&ckch->sctl->area);
710 ha_free(&ckch->sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200711 }
712
713 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100714 ha_free(&ckch->ocsp_response->area);
715 ha_free(&ckch->ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200716 }
717
718 if (ckch->ocsp_issuer)
719 X509_free(ckch->ocsp_issuer);
720 ckch->ocsp_issuer = NULL;
721}
722
723/*
724 *
725 * This function copy a cert_key_and_chain in memory
726 *
727 * It's used to try to apply changes on a ckch before committing them, because
728 * most of the time it's not possible to revert those changes
729 *
730 * Return a the dst or NULL
731 */
732struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
733 struct cert_key_and_chain *dst)
734{
William Lallemand6c096142021-02-23 14:45:45 +0100735 if (!src || !dst)
736 return NULL;
737
William Lallemand03c331c2020-05-13 10:10:01 +0200738 if (src->cert) {
739 dst->cert = src->cert;
740 X509_up_ref(src->cert);
741 }
742
743 if (src->key) {
744 dst->key = src->key;
745 EVP_PKEY_up_ref(src->key);
746 }
747
748 if (src->chain) {
749 dst->chain = X509_chain_up_ref(src->chain);
750 }
751
752 if (src->dh) {
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100753 HASSL_DH_up_ref(src->dh);
William Lallemand03c331c2020-05-13 10:10:01 +0200754 dst->dh = src->dh;
755 }
756
757 if (src->sctl) {
758 struct buffer *sctl;
759
760 sctl = calloc(1, sizeof(*sctl));
761 if (!chunk_dup(sctl, src->sctl)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100762 ha_free(&sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200763 goto error;
764 }
765 dst->sctl = sctl;
766 }
767
768 if (src->ocsp_response) {
769 struct buffer *ocsp_response;
770
771 ocsp_response = calloc(1, sizeof(*ocsp_response));
772 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100773 ha_free(&ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200774 goto error;
775 }
776 dst->ocsp_response = ocsp_response;
777 }
778
779 if (src->ocsp_issuer) {
780 X509_up_ref(src->ocsp_issuer);
781 dst->ocsp_issuer = src->ocsp_issuer;
782 }
783
784 return dst;
785
786error:
787
788 /* free everything */
789 ssl_sock_free_cert_key_and_chain_contents(dst);
790
791 return NULL;
792}
793
794/*
795 * return 0 on success or != 0 on failure
796 */
797int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
798{
799 int ret = 1;
800 BIO *in = NULL;
801 X509 *issuer;
802
803 if (buf) {
804 /* reading from a buffer */
805 in = BIO_new_mem_buf(buf, -1);
806 if (in == NULL) {
807 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
808 goto end;
809 }
810
811 } else {
812 /* reading from a file */
813 in = BIO_new(BIO_s_file());
814 if (in == NULL)
815 goto end;
816
817 if (BIO_read_filename(in, path) <= 0)
818 goto end;
819 }
820
821 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
822 if (!issuer) {
823 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
824 err && *err ? *err : "", path);
825 goto end;
826 }
827 /* no error, fill ckch with new context, old context must be free */
828 if (ckch->ocsp_issuer)
829 X509_free(ckch->ocsp_issuer);
830 ckch->ocsp_issuer = issuer;
831 ret = 0;
832
833end:
834
835 ERR_clear_error();
836 if (in)
837 BIO_free(in);
838
839 return ret;
840}
841
842/******************** ckch_store functions ***********************************
843 * The ckch_store is a structure used to cache and index the SSL files used in
844 * configuration
845 */
846
847/*
848 * Free a ckch_store, its ckch, its instances and remove it from the ebtree
849 */
850void ckch_store_free(struct ckch_store *store)
851{
852 struct ckch_inst *inst, *inst_s;
853
854 if (!store)
855 return;
856
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200857 ssl_sock_free_cert_key_and_chain_contents(store->ckch);
William Lallemand03c331c2020-05-13 10:10:01 +0200858
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100859 ha_free(&store->ckch);
William Lallemand03c331c2020-05-13 10:10:01 +0200860
861 list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) {
862 ckch_inst_free(inst);
863 }
864 ebmb_delete(&store->node);
865 free(store);
866}
867
868/*
869 * create and initialize a ckch_store
870 * <path> is the key name
871 * <nmemb> is the number of store->ckch objects to allocate
872 *
873 * Return a ckch_store or NULL upon failure.
874 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200875struct ckch_store *ckch_store_new(const char *filename)
William Lallemand03c331c2020-05-13 10:10:01 +0200876{
877 struct ckch_store *store;
878 int pathlen;
879
880 pathlen = strlen(filename);
881 store = calloc(1, sizeof(*store) + pathlen + 1);
882 if (!store)
883 return NULL;
884
William Lallemand03c331c2020-05-13 10:10:01 +0200885 memcpy(store->path, filename, pathlen + 1);
886
887 LIST_INIT(&store->ckch_inst);
888 LIST_INIT(&store->crtlist_entry);
889
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200890 store->ckch = calloc(1, sizeof(*store->ckch));
William Lallemand03c331c2020-05-13 10:10:01 +0200891 if (!store->ckch)
892 goto error;
893
894 return store;
895error:
896 ckch_store_free(store);
897 return NULL;
898}
899
900/* allocate and duplicate a ckch_store
901 * Return a new ckch_store or NULL */
902struct ckch_store *ckchs_dup(const struct ckch_store *src)
903{
904 struct ckch_store *dst;
905
William Lallemand6c096142021-02-23 14:45:45 +0100906 if (!src)
907 return NULL;
908
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200909 dst = ckch_store_new(src->path);
Eric Salama6ac61e32021-02-23 16:50:57 +0100910 if (!dst)
911 return NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200912
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200913 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
914 goto error;
William Lallemand03c331c2020-05-13 10:10:01 +0200915
916 return dst;
917
918error:
919 ckch_store_free(dst);
920
921 return NULL;
922}
923
924/*
925 * lookup a path into the ckchs tree.
926 */
927struct ckch_store *ckchs_lookup(char *path)
928{
929 struct ebmb_node *eb;
930
931 eb = ebst_lookup(&ckchs_tree, path);
932 if (!eb)
933 return NULL;
934
935 return ebmb_entry(eb, struct ckch_store, node);
936}
937
938/*
939 * This function allocate a ckch_store and populate it with certificates from files.
940 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200941struct ckch_store *ckchs_load_cert_file(char *path, char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200942{
943 struct ckch_store *ckchs;
944
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200945 ckchs = ckch_store_new(path);
William Lallemand03c331c2020-05-13 10:10:01 +0200946 if (!ckchs) {
947 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
948 goto end;
949 }
William Lallemand03c331c2020-05-13 10:10:01 +0200950
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200951 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
952 goto end;
William Lallemand03c331c2020-05-13 10:10:01 +0200953
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200954 /* insert into the ckchs tree */
955 memcpy(ckchs->path, path, strlen(path) + 1);
956 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand03c331c2020-05-13 10:10:01 +0200957 return ckchs;
958
959end:
960 ckch_store_free(ckchs);
961
962 return NULL;
963}
964
William Lallemandfa1d8b42020-05-13 15:46:10 +0200965
966/******************** ckch_inst functions ******************************/
967
968/* unlink a ckch_inst, free all SNIs, free the ckch_inst */
969/* The caller must use the lock of the bind_conf if used with inserted SNIs */
970void ckch_inst_free(struct ckch_inst *inst)
971{
972 struct sni_ctx *sni, *sni_s;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100973 struct ckch_inst_link_ref *link_ref, *link_ref_s;
William Lallemandfa1d8b42020-05-13 15:46:10 +0200974
975 if (inst == NULL)
976 return;
977
978 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
979 SSL_CTX_free(sni->ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +0200980 LIST_DELETE(&sni->by_ckch_inst);
William Lallemandfa1d8b42020-05-13 15:46:10 +0200981 ebmb_delete(&sni->name);
982 free(sni);
983 }
Remi Tricot-Le Bretonf3eedfe2021-01-25 17:19:44 +0100984 SSL_CTX_free(inst->ctx);
985 inst->ctx = NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +0200986 LIST_DELETE(&inst->by_ckchs);
987 LIST_DELETE(&inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100988
989 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
990 LIST_DELETE(&link_ref->link->list);
991 LIST_DELETE(&link_ref->list);
992 free(link_ref);
993 }
994
William Lallemandfa1d8b42020-05-13 15:46:10 +0200995 free(inst);
996}
997
998/* Alloc and init a ckch_inst */
999struct ckch_inst *ckch_inst_new()
1000{
1001 struct ckch_inst *ckch_inst;
1002
1003 ckch_inst = calloc(1, sizeof *ckch_inst);
1004 if (!ckch_inst)
1005 return NULL;
1006
1007 LIST_INIT(&ckch_inst->sni_ctx);
1008 LIST_INIT(&ckch_inst->by_ckchs);
1009 LIST_INIT(&ckch_inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001010 LIST_INIT(&ckch_inst->cafile_link_refs);
William Lallemandfa1d8b42020-05-13 15:46:10 +02001011
1012 return ckch_inst;
1013}
1014
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001015
1016/******************** ssl_store functions ******************************/
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001017struct eb_root cafile_tree = EB_ROOT;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001018
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001019/*
1020 * Returns the cafile_entry found in the cafile_tree indexed by the path 'path'.
1021 * If 'oldest_entry' is 1, returns the "original" cafile_entry (since
1022 * during a set cafile/commit cafile cycle there might be two entries for any
1023 * given path, the original one and the new one set via the CLI but not
1024 * committed yet).
1025 */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001026struct cafile_entry *ssl_store_get_cafile_entry(char *path, int oldest_entry)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001027{
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001028 struct cafile_entry *ca_e = NULL;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001029 struct ebmb_node *eb;
1030
1031 eb = ebst_lookup(&cafile_tree, path);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001032 while (eb) {
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001033 ca_e = ebmb_entry(eb, struct cafile_entry, node);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001034 /* The ebst_lookup in a tree that has duplicates returns the
1035 * oldest entry first. If we want the latest entry, we need to
1036 * iterate over all the duplicates until we find the last one
1037 * (in our case there should never be more than two entries for
1038 * any given path). */
1039 if (oldest_entry)
1040 return ca_e;
1041 eb = ebmb_next_dup(eb);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001042 }
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001043 return ca_e;
1044}
1045
Remi Tricot-Le Breton38c999b2021-02-23 16:28:43 +01001046int ssl_store_add_uncommitted_cafile_entry(struct cafile_entry *entry)
1047{
1048 return (ebst_insert(&cafile_tree, &entry->node) != &entry->node);
1049}
1050
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001051X509_STORE* ssl_store_get0_locations_file(char *path)
1052{
1053 struct cafile_entry *ca_e = ssl_store_get_cafile_entry(path, 0);
1054
1055 if (ca_e)
1056 return ca_e->ca_store;
1057
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001058 return NULL;
1059}
1060
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001061/* Create a cafile_entry object, without adding it to the cafile_tree. */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001062struct 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 +01001063{
1064 struct cafile_entry *ca_e;
1065 int pathlen;
1066
1067 pathlen = strlen(path);
1068
1069 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
1070 if (ca_e) {
1071 memcpy(ca_e->path, path, pathlen + 1);
1072 ca_e->ca_store = store;
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001073 ca_e->type = type;
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001074 LIST_INIT(&ca_e->ckch_inst_link);
1075 }
1076 return ca_e;
1077}
1078
1079/* Delete a cafile_entry. The caller is responsible from removing this entry
1080 * from the cafile_tree first if is was previously added into it. */
1081void ssl_store_delete_cafile_entry(struct cafile_entry *ca_e)
1082{
1083 struct ckch_inst_link *link, *link_s;
1084 if (!ca_e)
1085 return;
1086
1087 X509_STORE_free(ca_e->ca_store);
1088
1089 list_for_each_entry_safe(link, link_s, &ca_e->ckch_inst_link, list) {
1090 struct ckch_inst *inst = link->ckch_inst;
1091 struct ckch_inst_link_ref *link_ref, *link_ref_s;
1092 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
1093 if (link_ref->link == link) {
1094 LIST_DELETE(&link_ref->list);
1095 free(link_ref);
1096 break;
1097 }
1098 }
1099 LIST_DELETE(&link->list);
1100 free(link);
1101 }
1102
1103 free(ca_e);
1104}
1105
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001106/*
1107 * Build a cafile_entry out of a buffer instead of out of a file.
1108 * This function is used when the "commit ssl ca-file" cli command is used.
1109 * It can parse CERTIFICATE sections as well as CRL ones.
1110 * Returns 0 in case of success, 1 otherwise.
1111 */
1112int ssl_store_load_ca_from_buf(struct cafile_entry *ca_e, char *cert_buf)
1113{
1114 int retval = 0;
1115
1116 if (!ca_e)
1117 return 1;
1118
1119 if (!ca_e->ca_store) {
1120 ca_e->ca_store = X509_STORE_new();
1121 if (ca_e->ca_store) {
1122 BIO *bio = BIO_new_mem_buf(cert_buf, strlen(cert_buf));
1123 if (bio) {
1124 X509_INFO *info;
1125 int i;
1126 STACK_OF(X509_INFO) *infos = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
1127 if (!infos)
1128 {
1129 BIO_free(bio);
1130 return 1;
1131 }
1132
1133 for (i = 0; i < sk_X509_INFO_num(infos) && !retval; i++) {
1134 info = sk_X509_INFO_value(infos, i);
1135 /* X509_STORE_add_cert and X509_STORE_add_crl return 1 on success */
1136 if (info->x509) {
1137 retval = !X509_STORE_add_cert(ca_e->ca_store, info->x509);
1138 }
1139 if (!retval && info->crl) {
1140 retval = !X509_STORE_add_crl(ca_e->ca_store, info->crl);
1141 }
1142 }
1143 retval = retval || (i != sk_X509_INFO_num(infos));
1144
1145 /* Cleanup */
1146 sk_X509_INFO_pop_free(infos, X509_INFO_free);
1147 BIO_free(bio);
1148 }
1149 }
1150 }
1151
1152 return retval;
1153}
1154
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001155int ssl_store_load_locations_file(char *path, int create_if_none, enum cafile_type type)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001156{
1157 X509_STORE *store = ssl_store_get0_locations_file(path);
1158
1159 /* If this function is called by the CLI, we should not call the
1160 * X509_STORE_load_locations function because it performs forbidden disk
1161 * accesses. */
1162 if (!store && create_if_none) {
William Lallemand87fd9942022-04-01 20:12:03 +02001163 STACK_OF(X509_OBJECT) *objs;
1164 int cert_count = 0;
1165 struct stat buf;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001166 struct cafile_entry *ca_e;
William Lallemandc6b17632022-04-01 23:39:37 +02001167 const char *file = NULL;
1168 const char *dir = NULL;
William Lallemand87fd9942022-04-01 20:12:03 +02001169
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001170 store = X509_STORE_new();
William Lallemand87fd9942022-04-01 20:12:03 +02001171
William Lallemandc6b17632022-04-01 23:39:37 +02001172 if (strcmp(path, "@system-ca") == 0) {
1173 dir = X509_get_default_cert_dir();
William Lallemand87fd9942022-04-01 20:12:03 +02001174
William Lallemandc6b17632022-04-01 23:39:37 +02001175 } else {
1176
1177 if (stat(path, &buf))
1178 goto err;
1179
1180 if (S_ISDIR(buf.st_mode))
1181 dir = path;
1182 else
1183 file = path;
1184 }
William Lallemand87fd9942022-04-01 20:12:03 +02001185
1186 if (file) {
1187 if (!X509_STORE_load_locations(store, file, NULL)) {
1188 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001189 }
William Lallemand80296b42022-04-05 10:19:30 +02001190 } else if (dir) {
William Lallemand87fd9942022-04-01 20:12:03 +02001191 int n, i;
1192 struct dirent **de_list;
1193
1194 n = scandir(dir, &de_list, 0, alphasort);
1195 if (n < 0)
1196 goto err;
1197
1198 for (i= 0; i < n; i++) {
1199 char *end;
1200 struct dirent *de = de_list[i];
1201 BIO *in = NULL;
1202 X509 *ca = NULL;;
1203
1204 /* we try to load the files that would have
1205 * been loaded in an hashed directory loaded by
1206 * X509_LOOKUP_hash_dir, so according to "man 1
1207 * c_rehash", we should load ".pem", ".crt",
William Lallemande4b93eb2022-05-09 09:29:00 +02001208 * ".cer", or ".crl". Files starting with a dot
1209 * are ignored.
William Lallemand87fd9942022-04-01 20:12:03 +02001210 */
1211 end = strrchr(de->d_name, '.');
William Lallemande4b93eb2022-05-09 09:29:00 +02001212 if (!end || de->d_name[0] == '.' ||
1213 (strcmp(end, ".pem") != 0 &&
1214 strcmp(end, ".crt") != 0 &&
1215 strcmp(end, ".cer") != 0 &&
1216 strcmp(end, ".crl") != 0)) {
William Lallemand87fd9942022-04-01 20:12:03 +02001217 free(de);
1218 continue;
1219 }
1220 in = BIO_new(BIO_s_file());
1221 if (in == NULL)
1222 goto scandir_err;
1223
William Lallemandc6b17632022-04-01 23:39:37 +02001224 chunk_printf(&trash, "%s/%s", dir, de->d_name);
William Lallemand87fd9942022-04-01 20:12:03 +02001225
1226 if (BIO_read_filename(in, trash.area) == 0)
1227 goto scandir_err;
1228
1229 if (PEM_read_bio_X509_AUX(in, &ca, NULL, NULL) == NULL)
1230 goto scandir_err;
1231
1232 if (X509_STORE_add_cert(store, ca) == 0)
1233 goto scandir_err;
1234
William Lallemand4cfbf3c2022-04-26 15:57:33 +02001235 X509_free(ca);
William Lallemand87fd9942022-04-01 20:12:03 +02001236 BIO_free(in);
1237 free(de);
1238 continue;
1239
1240scandir_err:
William Lallemand4cfbf3c2022-04-26 15:57:33 +02001241 X509_free(ca);
William Lallemand87fd9942022-04-01 20:12:03 +02001242 BIO_free(in);
1243 free(de);
1244 ha_warning("ca-file: '%s' couldn't load '%s'\n", path, trash.area);
William Lallemand87fd9942022-04-01 20:12:03 +02001245
1246 }
1247 free(de_list);
William Lallemand80296b42022-04-05 10:19:30 +02001248 } else {
1249 ha_alert("ca-file: couldn't load '%s'\n", path);
1250 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001251 }
William Lallemand87fd9942022-04-01 20:12:03 +02001252
1253 objs = X509_STORE_get0_objects(store);
1254 cert_count = sk_X509_OBJECT_num(objs);
1255 if (cert_count == 0)
1256 ha_warning("ca-file: 0 CA were loaded from '%s'\n", path);
1257
1258 ca_e = ssl_store_create_cafile_entry(path, store, type);
1259 if (!ca_e)
1260 goto err;
1261 ebst_insert(&cafile_tree, &ca_e->node);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001262 }
1263 return (store != NULL);
William Lallemand87fd9942022-04-01 20:12:03 +02001264
1265err:
1266 X509_STORE_free(store);
1267 store = NULL;
1268 return 0;
1269
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001270}
1271
1272
William Lallemandda8584c2020-05-14 10:14:37 +02001273/*************************** CLI commands ***********************/
1274
1275/* Type of SSL payloads that can be updated over the CLI */
1276
William Lallemandff8bf982022-03-29 10:44:23 +02001277struct cert_exts cert_exts[] = {
1278 { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand26654e72022-03-30 12:01:32 +02001279 { "crt", CERT_TYPE_CRT, &ssl_sock_load_pem_into_ckch },
William Lallemandff8bf982022-03-29 10:44:23 +02001280 { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemandda8584c2020-05-14 10:14:37 +02001281#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemandff8bf982022-03-29 10:44:23 +02001282 { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001283#endif
Ilya Shipitsinc47d6762021-02-13 11:45:33 +05001284#ifdef HAVE_SSL_SCTL
William Lallemandff8bf982022-03-29 10:44:23 +02001285 { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001286#endif
William Lallemandff8bf982022-03-29 10:44:23 +02001287 { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
1288 { NULL, CERT_TYPE_MAX, NULL },
William Lallemandda8584c2020-05-14 10:14:37 +02001289};
1290
1291
1292/* release function of the `show ssl cert' command */
1293static void cli_release_show_cert(struct appctx *appctx)
1294{
1295 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1296}
1297
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001298/* IO handler of "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001299 * It makes use of a show_cert_ctx context, and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001300 */
William Lallemandda8584c2020-05-14 10:14:37 +02001301static int cli_io_handler_show_cert(struct appctx *appctx)
1302{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001303 struct show_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001304 struct buffer *trash = alloc_trash_chunk();
1305 struct ebmb_node *node;
Christopher Fauletd1d2e4d2022-06-03 16:24:02 +02001306 struct ckch_store *ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001307
1308 if (trash == NULL)
1309 return 1;
1310
Christopher Faulet3e94f5d2022-06-03 10:46:40 +02001311 if (!ctx->old_ckchs && ckchs_transaction.old_ckchs) {
1312 ckchs = ckchs_transaction.old_ckchs;
1313 chunk_appendf(trash, "# transaction\n");
1314 chunk_appendf(trash, "*%s\n", ckchs->path);
1315 if (applet_putchk(appctx, trash) == -1)
1316 goto yield;
1317 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001318 }
1319
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001320 if (!ctx->cur_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02001321 chunk_appendf(trash, "# filename\n");
1322 node = ebmb_first(&ckchs_tree);
1323 } else {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001324 node = &ctx->cur_ckchs->node;
William Lallemandda8584c2020-05-14 10:14:37 +02001325 }
1326 while (node) {
1327 ckchs = ebmb_entry(node, struct ckch_store, node);
William Lallemand5685ccf2020-09-16 16:12:25 +02001328 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001329
1330 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001331 if (applet_putchk(appctx, trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001332 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001333 }
1334
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001335 ctx->cur_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001336 free_trash_chunk(trash);
1337 return 1;
1338yield:
1339
1340 free_trash_chunk(trash);
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001341 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001342 return 0; /* should come back */
1343}
1344
1345/*
1346 * Extract and format the DNS SAN extensions and copy result into a chuink
1347 * Return 0;
1348 */
1349#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1350static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
1351{
1352 int i;
1353 char *str;
1354 STACK_OF(GENERAL_NAME) *names = NULL;
1355
1356 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1357 if (names) {
1358 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
1359 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
1360 if (i > 0)
1361 chunk_appendf(out, ", ");
1362 if (name->type == GEN_DNS) {
1363 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
1364 chunk_appendf(out, "DNS:%s", str);
1365 OPENSSL_free(str);
1366 }
1367 }
1368 }
1369 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
1370 }
1371 return 0;
1372}
1373#endif
1374
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001375/*
1376 * Build the ckch_inst_link that will be chained in the CA file entry and the
1377 * corresponding ckch_inst_link_ref that will be chained in the ckch instance.
1378 * Return 0 in case of success.
1379 */
1380static int do_chain_inst_and_cafile(struct cafile_entry *cafile_entry, struct ckch_inst *ckch_inst)
1381{
1382 struct ckch_inst_link *new_link;
1383 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
1384 struct ckch_inst_link *link = LIST_ELEM(cafile_entry->ckch_inst_link.n,
1385 typeof(link), list);
1386 /* Do not add multiple references to the same
1387 * instance in a cafile_entry */
1388 if (link->ckch_inst == ckch_inst) {
1389 return 1;
1390 }
1391 }
1392
1393 new_link = calloc(1, sizeof(*new_link));
1394 if (new_link) {
1395 struct ckch_inst_link_ref *new_link_ref = calloc(1, sizeof(*new_link_ref));
1396 if (!new_link_ref) {
1397 free(new_link);
1398 return 1;
1399 }
1400
1401 new_link->ckch_inst = ckch_inst;
1402 new_link_ref->link = new_link;
1403 LIST_INIT(&new_link->list);
1404 LIST_INIT(&new_link_ref->list);
1405
1406 LIST_APPEND(&cafile_entry->ckch_inst_link, &new_link->list);
1407 LIST_APPEND(&ckch_inst->cafile_link_refs, &new_link_ref->list);
1408 }
1409
1410 return 0;
1411}
1412
1413
1414/*
1415 * Link a CA file tree entry to the ckch instance that uses it.
1416 * To determine if and which CA file tree entries need to be linked to the
1417 * instance, we follow the same logic performed in ssl_sock_prepare_ctx when
1418 * processing the verify option.
1419 * This function works for a frontend as well as for a backend, depending on the
1420 * configuration parameters given (bind_conf or server).
1421 */
1422void ckch_inst_add_cafile_link(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf,
1423 struct ssl_bind_conf *ssl_conf, const struct server *srv)
1424{
1425 int verify = SSL_VERIFY_NONE;
1426
1427 if (srv) {
1428
1429 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
1430 verify = SSL_VERIFY_PEER;
1431 switch (srv->ssl_ctx.verify) {
1432 case SSL_SOCK_VERIFY_NONE:
1433 verify = SSL_VERIFY_NONE;
1434 break;
1435 case SSL_SOCK_VERIFY_REQUIRED:
1436 verify = SSL_VERIFY_PEER;
1437 break;
1438 }
1439 }
1440 else {
1441 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
1442 case SSL_SOCK_VERIFY_NONE:
1443 verify = SSL_VERIFY_NONE;
1444 break;
1445 case SSL_SOCK_VERIFY_OPTIONAL:
1446 verify = SSL_VERIFY_PEER;
1447 break;
1448 case SSL_SOCK_VERIFY_REQUIRED:
1449 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1450 break;
1451 }
1452 }
1453
1454 if (verify & SSL_VERIFY_PEER) {
1455 struct cafile_entry *ca_file_entry = NULL;
1456 struct cafile_entry *ca_verify_file_entry = NULL;
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001457 struct cafile_entry *crl_file_entry = NULL;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001458 if (srv) {
1459 if (srv->ssl_ctx.ca_file) {
1460 ca_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.ca_file, 0);
1461
1462 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001463 if (srv->ssl_ctx.crl_file) {
1464 crl_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.crl_file, 0);
1465 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001466 }
1467 else {
1468 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
1469 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 +02001470 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 +01001471
1472 if (ca_file)
1473 ca_file_entry = ssl_store_get_cafile_entry(ca_file, 0);
1474 if (ca_verify_file)
1475 ca_verify_file_entry = ssl_store_get_cafile_entry(ca_verify_file, 0);
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001476 if (crl_file)
1477 crl_file_entry = ssl_store_get_cafile_entry(crl_file, 0);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001478 }
1479
1480 if (ca_file_entry) {
1481 /* If we have a ckch instance that is not already in the
1482 * cafile_entry's list, add it to it. */
1483 if (do_chain_inst_and_cafile(ca_file_entry, ckch_inst))
1484 return;
1485
1486 }
1487 if (ca_verify_file_entry && (ca_file_entry != ca_verify_file_entry)) {
1488 /* If we have a ckch instance that is not already in the
1489 * cafile_entry's list, add it to it. */
1490 if (do_chain_inst_and_cafile(ca_verify_file_entry, ckch_inst))
1491 return;
1492 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001493 if (crl_file_entry) {
1494 /* If we have a ckch instance that is not already in the
1495 * cafile_entry's list, add it to it. */
1496 if (do_chain_inst_and_cafile(crl_file_entry, ckch_inst))
1497 return;
1498 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001499 }
1500}
1501
William Lallemandda8584c2020-05-14 10:14:37 +02001502
1503
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001504static int show_cert_detail(X509 *cert, STACK_OF(X509) *chain, struct buffer *out)
William Lallemandda8584c2020-05-14 10:14:37 +02001505{
William Lallemandda8584c2020-05-14 10:14:37 +02001506 BIO *bio = NULL;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001507 struct buffer *tmp = alloc_trash_chunk();
William Lallemandda8584c2020-05-14 10:14:37 +02001508 int i;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001509 int write = -1;
1510 unsigned int len = 0;
1511 X509_NAME *name = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001512
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001513 if (!tmp)
1514 return -1;
William Lallemandda8584c2020-05-14 10:14:37 +02001515
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001516 if (!cert)
William Lallemand5685ccf2020-09-16 16:12:25 +02001517 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02001518
William Lallemand5685ccf2020-09-16 16:12:25 +02001519 if (chain == NULL) {
1520 struct issuer_chain *issuer;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001521 issuer = ssl_get0_issuer_chain(cert);
William Lallemand5685ccf2020-09-16 16:12:25 +02001522 if (issuer) {
1523 chain = issuer->chain;
1524 chunk_appendf(out, "Chain Filename: ");
1525 chunk_appendf(out, "%s\n", issuer->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001526 }
William Lallemand5685ccf2020-09-16 16:12:25 +02001527 }
1528 chunk_appendf(out, "Serial: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001529 if (ssl_sock_get_serial(cert, tmp) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001530 goto end;
1531 dump_binary(out, tmp->area, tmp->data);
1532 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001533
William Lallemand5685ccf2020-09-16 16:12:25 +02001534 chunk_appendf(out, "notBefore: ");
1535 chunk_reset(tmp);
1536 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1537 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001538 if (ASN1_TIME_print(bio, X509_getm_notBefore(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001539 goto end;
1540 write = BIO_read(bio, tmp->area, tmp->size-1);
1541 tmp->area[write] = '\0';
1542 BIO_free(bio);
1543 bio = NULL;
1544 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001545
William Lallemand5685ccf2020-09-16 16:12:25 +02001546 chunk_appendf(out, "notAfter: ");
1547 chunk_reset(tmp);
1548 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1549 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001550 if (ASN1_TIME_print(bio, X509_getm_notAfter(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001551 goto end;
1552 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
1553 goto end;
1554 tmp->area[write] = '\0';
1555 BIO_free(bio);
1556 bio = NULL;
1557 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001558
1559#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand5685ccf2020-09-16 16:12:25 +02001560 chunk_appendf(out, "Subject Alternative Name: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001561 if (ssl_sock_get_san_oneline(cert, out) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001562 goto end;
1563 *(out->area + out->data) = '\0';
1564 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001565#endif
William Lallemand5685ccf2020-09-16 16:12:25 +02001566 chunk_reset(tmp);
1567 chunk_appendf(out, "Algorithm: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001568 if (cert_get_pkey_algo(cert, tmp) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001569 goto end;
1570 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001571
William Lallemand5685ccf2020-09-16 16:12:25 +02001572 chunk_reset(tmp);
1573 chunk_appendf(out, "SHA1 FingerPrint: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001574 if (X509_digest(cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001575 goto end;
1576 tmp->data = len;
1577 dump_binary(out, tmp->area, tmp->data);
1578 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001579
William Lallemand5685ccf2020-09-16 16:12:25 +02001580 chunk_appendf(out, "Subject: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001581 if ((name = X509_get_subject_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001582 goto end;
1583 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1584 goto end;
1585 *(tmp->area + tmp->data) = '\0';
1586 chunk_appendf(out, "%s\n", tmp->area);
1587
1588 chunk_appendf(out, "Issuer: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001589 if ((name = X509_get_issuer_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001590 goto end;
1591 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1592 goto end;
1593 *(tmp->area + tmp->data) = '\0';
1594 chunk_appendf(out, "%s\n", tmp->area);
1595
1596 /* Displays subject of each certificate in the chain */
1597 for (i = 0; i < sk_X509_num(chain); i++) {
1598 X509 *ca = sk_X509_value(chain, i);
1599
1600 chunk_appendf(out, "Chain Subject: ");
1601 if ((name = X509_get_subject_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001602 goto end;
1603 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1604 goto end;
1605 *(tmp->area + tmp->data) = '\0';
1606 chunk_appendf(out, "%s\n", tmp->area);
1607
William Lallemand5685ccf2020-09-16 16:12:25 +02001608 chunk_appendf(out, "Chain Issuer: ");
1609 if ((name = X509_get_issuer_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001610 goto end;
1611 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1612 goto end;
1613 *(tmp->area + tmp->data) = '\0';
1614 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001615 }
1616
1617end:
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001618 if (bio)
1619 BIO_free(bio);
1620 free_trash_chunk(tmp);
1621
1622 return 0;
1623}
1624
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001625#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 +02001626/*
1627 * Build the OCSP tree entry's key for a given ckch_store.
1628 * Returns a negative value in case of error.
1629 */
1630static int ckch_store_build_certid(struct ckch_store *ckch_store, unsigned char certid[128], unsigned int *key_length)
1631{
1632 OCSP_RESPONSE *resp;
1633 OCSP_BASICRESP *bs = NULL;
1634 OCSP_SINGLERESP *sr;
1635 OCSP_CERTID *id;
1636 unsigned char *p = NULL;
1637
1638 if (!key_length)
1639 return -1;
1640
1641 *key_length = 0;
1642
1643 if (!ckch_store->ckch->ocsp_response)
1644 return 0;
1645
1646 p = (unsigned char *) ckch_store->ckch->ocsp_response->area;
1647
1648 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
1649 ckch_store->ckch->ocsp_response->data);
1650 if (!resp) {
1651 goto end;
1652 }
1653
1654 bs = OCSP_response_get1_basic(resp);
1655 if (!bs) {
1656 goto end;
1657 }
1658
1659 sr = OCSP_resp_get0(bs, 0);
1660 if (!sr) {
1661 goto end;
1662 }
1663
1664 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
1665
1666 p = certid;
1667 *key_length = i2d_OCSP_CERTID(id, &p);
1668
1669end:
1670 return *key_length > 0;
1671}
1672#endif
1673
1674/*
1675 * Dump the OCSP certificate key (if it exists) of certificate <ckch> into
1676 * buffer <out>.
1677 * Returns 0 in case of success.
1678 */
1679static int ckch_store_show_ocsp_certid(struct ckch_store *ckch_store, struct buffer *out)
1680{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001681#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 +02001682 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1683 unsigned int key_length = 0;
1684 int i;
1685
1686 if (ckch_store_build_certid(ckch_store, (unsigned char*)key, &key_length) >= 0) {
1687 /* Dump the CERTID info */
1688 chunk_appendf(out, "OCSP Response Key: ");
1689 for (i = 0; i < key_length; ++i) {
1690 chunk_appendf(out, "%02x", key[i]);
1691 }
1692 chunk_appendf(out, "\n");
1693 }
1694#endif
1695
1696 return 0;
1697}
1698
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001699
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001700/* IO handler of the details "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001701 * It uses a struct show_cert_ctx and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001702 */
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001703static int cli_io_handler_show_cert_detail(struct appctx *appctx)
1704{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001705 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001706 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001707 struct buffer *out = alloc_trash_chunk();
1708 int retval = 0;
1709
1710 if (!out)
1711 goto end_no_putchk;
1712
1713 chunk_appendf(out, "Filename: ");
1714 if (ckchs == ckchs_transaction.new_ckchs)
1715 chunk_appendf(out, "*");
1716 chunk_appendf(out, "%s\n", ckchs->path);
1717
1718 chunk_appendf(out, "Status: ");
1719 if (ckchs->ckch->cert == NULL)
1720 chunk_appendf(out, "Empty\n");
1721 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
1722 chunk_appendf(out, "Unused\n");
1723 else
1724 chunk_appendf(out, "Used\n");
1725
1726 retval = show_cert_detail(ckchs->ckch->cert, ckchs->ckch->chain, out);
1727 if (retval < 0)
1728 goto end_no_putchk;
1729 else if (retval)
1730 goto end;
1731
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001732 ckch_store_show_ocsp_certid(ckchs, out);
1733
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001734end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001735 if (applet_putchk(appctx, out) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001736 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001737
1738end_no_putchk:
William Lallemandda8584c2020-05-14 10:14:37 +02001739 free_trash_chunk(out);
1740 return 1;
1741yield:
William Lallemandda8584c2020-05-14 10:14:37 +02001742 free_trash_chunk(out);
1743 return 0; /* should come back */
1744}
1745
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001746
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001747/* IO handler of the details "show ssl cert <filename.ocsp>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001748 * It uses a show_cert_ctx.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001749 */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001750static int cli_io_handler_show_cert_ocsp_detail(struct appctx *appctx)
1751{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001752#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL)
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001753 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001754 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001755 struct buffer *out = alloc_trash_chunk();
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001756 int from_transaction = ctx->transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001757
1758 if (!out)
1759 goto end_no_putchk;
1760
1761 /* If we try to display an ongoing transaction's OCSP response, we
1762 * need to dump the ckch's ocsp_response buffer directly.
1763 * Otherwise, we must rebuild the certificate's certid in order to
1764 * look for the current OCSP response in the tree. */
1765 if (from_transaction && ckchs->ckch->ocsp_response) {
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001766 if (ssl_ocsp_response_print(ckchs->ckch->ocsp_response, out))
1767 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001768 }
1769 else {
1770 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1771 unsigned int key_length = 0;
1772
1773 if (ckch_store_build_certid(ckchs, (unsigned char*)key, &key_length) < 0)
1774 goto end_no_putchk;
1775
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001776 if (ssl_get_ocspresponse_detail(key, out))
1777 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001778 }
1779
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001780 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001781 goto yield;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001782
1783end_no_putchk:
1784 free_trash_chunk(out);
1785 return 1;
1786yield:
1787 free_trash_chunk(out);
1788 return 0; /* should come back */
1789#else
1790 return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n");
1791#endif
1792}
1793
William Lallemandda8584c2020-05-14 10:14:37 +02001794/* parsing function for 'show ssl cert [certfile]' */
1795static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
1796{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001797 struct show_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02001798 struct ckch_store *ckchs;
1799
1800 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1801 return cli_err(appctx, "Can't allocate memory!\n");
1802
1803 /* The operations on the CKCH architecture are locked so we can
1804 * manipulate ckch_store and ckch_inst */
1805 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1806 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
1807
1808 /* check if there is a certificate to lookup */
1809 if (*args[3]) {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001810 int show_ocsp_detail = 0;
1811 int from_transaction = 0;
1812 char *end;
1813
1814 /* We manage the special case "certname.ocsp" through which we
1815 * can show the details of an OCSP response. */
1816 end = strrchr(args[3], '.');
1817 if (end && strcmp(end+1, "ocsp") == 0) {
1818 *end = '\0';
1819 show_ocsp_detail = 1;
1820 }
1821
William Lallemandda8584c2020-05-14 10:14:37 +02001822 if (*args[3] == '*') {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001823 from_transaction = 1;
William Lallemandda8584c2020-05-14 10:14:37 +02001824 if (!ckchs_transaction.new_ckchs)
1825 goto error;
1826
1827 ckchs = ckchs_transaction.new_ckchs;
1828
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001829 if (strcmp(args[3] + 1, ckchs->path) != 0)
William Lallemandda8584c2020-05-14 10:14:37 +02001830 goto error;
1831
1832 } else {
1833 if ((ckchs = ckchs_lookup(args[3])) == NULL)
1834 goto error;
1835
1836 }
1837
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001838 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001839 /* use the IO handler that shows details */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001840 if (show_ocsp_detail) {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001841 ctx->transaction = from_transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001842 appctx->io_handler = cli_io_handler_show_cert_ocsp_detail;
1843 }
1844 else
1845 appctx->io_handler = cli_io_handler_show_cert_detail;
William Lallemandda8584c2020-05-14 10:14:37 +02001846 }
1847
1848 return 0;
1849
1850error:
1851 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1852 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
1853}
1854
1855/* release function of the `set ssl cert' command, free things and unlock the spinlock */
1856static void cli_release_commit_cert(struct appctx *appctx)
1857{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02001858 struct commit_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001859
1860 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulet9d56e242022-05-31 16:37:01 +02001861 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
1862 if (ctx->new_ckchs)
1863 ckch_store_free(ctx->new_ckchs);
1864 ha_free(&ctx->err);
William Lallemandda8584c2020-05-14 10:14:37 +02001865}
1866
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001867
1868/*
1869 * Rebuild a new instance 'new_inst' based on an old instance 'ckchi' and a
1870 * specific ckch_store.
1871 * Returns 0 in case of success, 1 otherwise.
1872 */
William Lallemande60c7d62022-03-30 11:26:15 +02001873int ckch_inst_rebuild(struct ckch_store *ckch_store, struct ckch_inst *ckchi,
1874 struct ckch_inst **new_inst, char **err)
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001875{
1876 int retval = 0;
1877 int errcode = 0;
1878 struct sni_ctx *sc0, *sc0s;
1879 char **sni_filter = NULL;
1880 int fcount = 0;
1881
1882 if (ckchi->crtlist_entry) {
1883 sni_filter = ckchi->crtlist_entry->filters;
1884 fcount = ckchi->crtlist_entry->fcount;
1885 }
1886
1887 if (ckchi->is_server_instance)
1888 errcode |= ckch_inst_new_load_srv_store(ckch_store->path, ckch_store, new_inst, err);
1889 else
1890 errcode |= ckch_inst_new_load_store(ckch_store->path, ckch_store, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, new_inst, err);
1891
1892 if (errcode & ERR_CODE)
1893 return 1;
1894
1895 /* if the previous ckchi was used as the default */
1896 if (ckchi->is_default)
1897 (*new_inst)->is_default = 1;
1898
1899 (*new_inst)->is_server_instance = ckchi->is_server_instance;
1900 (*new_inst)->server = ckchi->server;
1901 /* Create a new SSL_CTX and link it to the new instance. */
1902 if ((*new_inst)->is_server_instance) {
1903 retval = ssl_sock_prep_srv_ctx_and_inst(ckchi->server, (*new_inst)->ctx, (*new_inst));
1904 if (retval)
1905 return 1;
1906 }
1907
1908 /* create the link to the crtlist_entry */
1909 (*new_inst)->crtlist_entry = ckchi->crtlist_entry;
1910
1911 /* we need to initialize the SSL_CTX generated */
1912 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
1913 list_for_each_entry_safe(sc0, sc0s, &(*new_inst)->sni_ctx, by_ckch_inst) {
1914 if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
1915 errcode |= ssl_sock_prep_ctx_and_inst(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, *new_inst, err);
1916 if (errcode & ERR_CODE)
1917 return 1;
1918 }
1919 }
1920
1921 return 0;
1922}
1923
1924/*
1925 * Load all the new SNIs of a newly built ckch instance in the trees, or replace
1926 * a server's main ckch instance.
1927 */
1928static void __ssl_sock_load_new_ckch_instance(struct ckch_inst *ckchi)
1929{
1930 /* The bind_conf will be null on server ckch_instances. */
1931 if (ckchi->is_server_instance) {
1932 int i;
1933 /* a lock is needed here since we have to free the SSL cache */
1934 HA_RWLOCK_WRLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
1935 /* free the server current SSL_CTX */
1936 SSL_CTX_free(ckchi->server->ssl_ctx.ctx);
1937 /* Actual ssl context update */
1938 SSL_CTX_up_ref(ckchi->ctx);
1939 ckchi->server->ssl_ctx.ctx = ckchi->ctx;
1940 ckchi->server->ssl_ctx.inst = ckchi;
1941
1942 /* flush the session cache of the server */
1943 for (i = 0; i < global.nbthread; i++) {
William Lallemandce990332021-11-23 15:15:09 +01001944 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].sni);
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001945 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].ptr);
1946 }
1947 HA_RWLOCK_WRUNLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
1948
1949 } else {
1950 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
1951 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
1952 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
1953 }
1954}
1955
1956/*
1957 * Delete a ckch instance that was replaced after a CLI command.
1958 */
1959static void __ckch_inst_free_locked(struct ckch_inst *ckchi)
1960{
1961 if (ckchi->is_server_instance) {
1962 /* no lock for servers */
1963 ckch_inst_free(ckchi);
1964 } else {
1965 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
1966
1967 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
1968 ckch_inst_free(ckchi);
1969 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
1970 }
1971}
1972
William Lallemand3b5a3a62022-03-29 14:29:31 +02001973/* Replace a ckch_store in the ckch tree and insert the whole dependencies,
1974* then free the previous dependencies and store.
1975* Used in the case of a certificate update.
1976*
1977* Every dependencies must allocated before using this function.
1978*
1979* This function can't fail as it only update pointers, and does not alloc anything.
1980*
1981* /!\ This function must be used under the ckch lock. /!\
1982*
1983* - Insert every dependencies (SNI, crtlist_entry, ckch_inst, etc)
1984* - Delete the old ckch_store from the tree
1985* - Insert the new ckch_store
1986* - Free the old dependencies and the old ckch_store
1987*/
1988void ckch_store_replace(struct ckch_store *old_ckchs, struct ckch_store *new_ckchs)
1989{
1990 struct crtlist_entry *entry;
1991 struct ckch_inst *ckchi, *ckchis;
1992
1993 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
1994 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
1995 ebpt_delete(&entry->node);
1996 /* change the ptr and reinsert the node */
1997 entry->node.key = new_ckchs;
1998 ebpt_insert(&entry->crtlist->entries, &entry->node);
1999 }
2000 /* insert the new ckch_insts in the crtlist_entry */
2001 list_for_each_entry(ckchi, &new_ckchs->ckch_inst, by_ckchs) {
2002 if (ckchi->crtlist_entry)
2003 LIST_INSERT(&ckchi->crtlist_entry->ckch_inst, &ckchi->by_crtlist_entry);
2004 }
2005 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2006 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
2007 __ssl_sock_load_new_ckch_instance(ckchi);
2008 }
2009 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
2010 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
2011 __ckch_inst_free_locked(ckchi);
2012 }
2013
2014 ckch_store_free(old_ckchs);
2015 ebst_insert(&ckchs_tree, &new_ckchs->node);
2016}
2017
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002018
William Lallemandda8584c2020-05-14 10:14:37 +02002019/*
2020 * This function tries to create the new ckch_inst and their SNIs
William Lallemand30fcca12022-03-30 12:03:12 +02002021 *
2022 * /!\ don't forget to update __hlua_ckch_commit() if you changes things there. /!\
William Lallemandda8584c2020-05-14 10:14:37 +02002023 */
2024static int cli_io_handler_commit_cert(struct appctx *appctx)
2025{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002026 struct commit_cert_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002027 struct stconn *sc = appctx_sc(appctx);
William Lallemandda8584c2020-05-14 10:14:37 +02002028 int y = 0;
William Lallemandda8584c2020-05-14 10:14:37 +02002029 struct ckch_store *old_ckchs, *new_ckchs = NULL;
William Lallemand3b5a3a62022-03-29 14:29:31 +02002030 struct ckch_inst *ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002031
Willy Tarreau475e4632022-05-27 10:26:46 +02002032 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Christopher Faulet9d56e242022-05-31 16:37:01 +02002033 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02002034
2035 while (1) {
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002036 switch (ctx->state) {
2037 case CERT_ST_INIT:
William Lallemandda8584c2020-05-14 10:14:37 +02002038 /* This state just print the update message */
Christopher Faulet9d56e242022-05-31 16:37:01 +02002039 chunk_printf(&trash, "Committing %s", ckchs_transaction.path);
2040 if (applet_putchk(appctx, &trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02002041 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002042
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002043 ctx->state = CERT_ST_GEN;
William Lallemandda8584c2020-05-14 10:14:37 +02002044 /* fallthrough */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002045 case CERT_ST_GEN:
William Lallemandda8584c2020-05-14 10:14:37 +02002046 /*
2047 * This state generates the ckch instances with their
2048 * sni_ctxs and SSL_CTX.
2049 *
2050 * Since the SSL_CTX generation can be CPU consumer, we
2051 * yield every 10 instances.
2052 */
2053
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002054 old_ckchs = ctx->old_ckchs;
2055 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002056
William Lallemandda8584c2020-05-14 10:14:37 +02002057 /* get the next ckchi to regenerate */
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002058 ckchi = ctx->next_ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002059 /* we didn't start yet, set it to the first elem */
2060 if (ckchi == NULL)
2061 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
2062
2063 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
2064 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
2065 struct ckch_inst *new_inst;
William Lallemandda8584c2020-05-14 10:14:37 +02002066
Christopher Faulet9d56e242022-05-31 16:37:01 +02002067 /* save the next ckchi to compute in case of yield */
2068 ctx->next_ckchi = ckchi;
2069
William Lallemandda8584c2020-05-14 10:14:37 +02002070 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2071 if (y >= 10) {
Christopher Faulet9d56e242022-05-31 16:37:01 +02002072 applet_have_more_data(appctx); /* let's come back later */
William Lallemandda8584c2020-05-14 10:14:37 +02002073 goto yield;
2074 }
2075
Christopher Faulet9d56e242022-05-31 16:37:01 +02002076 /* display one dot per new instance */
2077 if (applet_putstr(appctx, ".") == -1)
2078 goto yield;
2079
2080 ctx->err = NULL;
2081 if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &ctx->err)) {
2082 ctx->state = CERT_ST_ERROR;
William Lallemandda8584c2020-05-14 10:14:37 +02002083 goto error;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002084 }
William Lallemandda8584c2020-05-14 10:14:37 +02002085
William Lallemandda8584c2020-05-14 10:14:37 +02002086 /* link the new ckch_inst to the duplicate */
Willy Tarreau2b718102021-04-21 07:32:39 +02002087 LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002088 y++;
2089 }
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002090 ctx->state = CERT_ST_INSERT;
William Lallemandda8584c2020-05-14 10:14:37 +02002091 /* fallthrough */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002092 case CERT_ST_INSERT:
William Lallemandda8584c2020-05-14 10:14:37 +02002093 /* The generation is finished, we can insert everything */
2094
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002095 old_ckchs = ctx->old_ckchs;
2096 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002097
William Lallemand3b5a3a62022-03-29 14:29:31 +02002098 /* insert everything and remove the previous objects */
2099 ckch_store_replace(old_ckchs, new_ckchs);
Christopher Faulet9d56e242022-05-31 16:37:01 +02002100 ctx->new_ckchs = ctx->old_ckchs = NULL;
2101 ctx->state = CERT_ST_SUCCESS;
2102 /* fallthrough */
2103 case CERT_ST_SUCCESS:
2104 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2105 goto yield;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002106 ctx->state = CERT_ST_FIN;
William Lallemandda8584c2020-05-14 10:14:37 +02002107 /* fallthrough */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002108 case CERT_ST_FIN:
William Lallemandda8584c2020-05-14 10:14:37 +02002109 /* we achieved the transaction, we can set everything to NULL */
William Lallemandda8584c2020-05-14 10:14:37 +02002110 ckchs_transaction.new_ckchs = NULL;
2111 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002112 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002113 goto end;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002114
2115 case CERT_ST_ERROR:
2116 error:
2117 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2118 if (applet_putchk(appctx, &trash) == -1)
2119 goto yield;
2120 ctx->state = CERT_ST_FIN;
2121 break;
William Lallemandda8584c2020-05-14 10:14:37 +02002122 }
2123 }
2124end:
William Lallemandda8584c2020-05-14 10:14:37 +02002125 /* success: call the release function and don't come back */
2126 return 1;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002127
William Lallemandda8584c2020-05-14 10:14:37 +02002128yield:
William Lallemandda8584c2020-05-14 10:14:37 +02002129 return 0; /* should come back */
William Lallemandda8584c2020-05-14 10:14:37 +02002130}
2131
2132/*
2133 * Parsing function of 'commit ssl cert'
2134 */
2135static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
2136{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002137 struct commit_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02002138 char *err = NULL;
2139
2140 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2141 return 1;
2142
2143 if (!*args[3])
2144 return cli_err(appctx, "'commit ssl cert expects a filename\n");
2145
2146 /* The operations on the CKCH architecture are locked so we can
2147 * manipulate ckch_store and ckch_inst */
2148 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2149 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
2150
2151 if (!ckchs_transaction.path) {
2152 memprintf(&err, "No ongoing transaction! !\n");
2153 goto error;
2154 }
2155
2156 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2157 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
2158 goto error;
2159 }
2160
William Lallemand5685ccf2020-09-16 16:12:25 +02002161 /* if a certificate is here, a private key must be here too */
2162 if (ckchs_transaction.new_ckchs->ckch->cert && !ckchs_transaction.new_ckchs->ckch->key) {
2163 memprintf(&err, "The transaction must contain at least a certificate and a private key!\n");
2164 goto error;
2165 }
William Lallemanda9419522020-06-24 16:26:41 +02002166
William Lallemand5685ccf2020-09-16 16:12:25 +02002167 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
2168 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
2169 goto error;
William Lallemandda8584c2020-05-14 10:14:37 +02002170 }
2171
2172 /* init the appctx structure */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002173 ctx->state = CERT_ST_INIT;
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002174 ctx->next_ckchi = NULL;
2175 ctx->new_ckchs = ckchs_transaction.new_ckchs;
2176 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002177
2178 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
2179 return 0;
2180
2181error:
2182
2183 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2184 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2185
2186 return cli_dynerr(appctx, err);
2187}
2188
2189
2190
2191
2192/*
2193 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
Willy Tarreau329f4b42022-05-04 20:05:55 +02002194 * It uses a set_cert_ctx context, and ckchs_transaction under a lock.
William Lallemandda8584c2020-05-14 10:14:37 +02002195 */
2196static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
2197{
2198 struct ckch_store *new_ckchs = NULL;
2199 struct ckch_store *old_ckchs = NULL;
2200 char *err = NULL;
2201 int i;
William Lallemandda8584c2020-05-14 10:14:37 +02002202 int errcode = 0;
2203 char *end;
William Lallemandff8bf982022-03-29 10:44:23 +02002204 struct cert_exts *cert_ext = &cert_exts[0]; /* default one, PEM */
William Lallemandda8584c2020-05-14 10:14:37 +02002205 struct cert_key_and_chain *ckch;
2206 struct buffer *buf;
2207
2208 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2209 return 1;
2210
William Lallemandda8584c2020-05-14 10:14:37 +02002211 if (!*args[3] || !payload)
2212 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
2213
2214 /* The operations on the CKCH architecture are locked so we can
2215 * manipulate ckch_store and ckch_inst */
2216 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2217 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
2218
William Lallemand5ba80d62021-05-04 16:17:27 +02002219 if ((buf = alloc_trash_chunk()) == NULL) {
2220 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2221 errcode |= ERR_ALERT | ERR_FATAL;
2222 goto end;
2223 }
William Lallemande5ff4ad2020-06-08 09:40:37 +02002224
William Lallemandda8584c2020-05-14 10:14:37 +02002225 if (!chunk_strcpy(buf, args[3])) {
2226 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2227 errcode |= ERR_ALERT | ERR_FATAL;
2228 goto end;
2229 }
2230
2231 /* check which type of file we want to update */
William Lallemandff8bf982022-03-29 10:44:23 +02002232 for (i = 0; cert_exts[i].ext != NULL; i++) {
William Lallemandda8584c2020-05-14 10:14:37 +02002233 end = strrchr(buf->area, '.');
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002234 if (end && *cert_exts[i].ext && (strcmp(end + 1, cert_exts[i].ext) == 0)) {
William Lallemandda8584c2020-05-14 10:14:37 +02002235 *end = '\0';
William Lallemand089c1382020-10-23 17:35:12 +02002236 buf->data = strlen(buf->area);
William Lallemandff8bf982022-03-29 10:44:23 +02002237 cert_ext = &cert_exts[i];
William Lallemandda8584c2020-05-14 10:14:37 +02002238 break;
2239 }
2240 }
2241
William Lallemandda8584c2020-05-14 10:14:37 +02002242 /* if there is an ongoing transaction */
2243 if (ckchs_transaction.path) {
William Lallemandda8584c2020-05-14 10:14:37 +02002244 /* if there is an ongoing transaction, check if this is the same file */
2245 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
William Lallemand089c1382020-10-23 17:35:12 +02002246 /* we didn't find the transaction, must try more cases below */
2247
2248 /* if the del-ext option is activated we should try to take a look at a ".crt" too. */
William Lallemandff8bf982022-03-29 10:44:23 +02002249 if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
William Lallemand089c1382020-10-23 17:35:12 +02002250 if (!chunk_strcat(buf, ".crt")) {
2251 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2252 errcode |= ERR_ALERT | ERR_FATAL;
2253 goto end;
2254 }
2255
2256 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
2257 /* remove .crt of the error message */
2258 *(b_orig(buf) + b_data(buf) + strlen(".crt")) = '\0';
2259 b_sub(buf, strlen(".crt"));
2260
2261 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
2262 errcode |= ERR_ALERT | ERR_FATAL;
2263 goto end;
2264 }
2265 }
William Lallemandda8584c2020-05-14 10:14:37 +02002266 }
2267
Christopher Faulet24a20b92022-06-03 11:50:40 +02002268 old_ckchs = ckchs_transaction.new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002269
2270 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002271
William Lallemand95fefa12020-09-09 12:01:33 +02002272 /* lookup for the certificate in the tree */
Christopher Faulet24a20b92022-06-03 11:50:40 +02002273 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002274
Christopher Faulet24a20b92022-06-03 11:50:40 +02002275 if (!old_ckchs) {
William Lallemand089c1382020-10-23 17:35:12 +02002276 /* if the del-ext option is activated we should try to take a look at a ".crt" too. */
William Lallemandff8bf982022-03-29 10:44:23 +02002277 if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
William Lallemand089c1382020-10-23 17:35:12 +02002278 if (!chunk_strcat(buf, ".crt")) {
2279 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2280 errcode |= ERR_ALERT | ERR_FATAL;
2281 goto end;
2282 }
Christopher Faulet24a20b92022-06-03 11:50:40 +02002283 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002284 }
2285 }
William Lallemandda8584c2020-05-14 10:14:37 +02002286 }
2287
Christopher Faulet24a20b92022-06-03 11:50:40 +02002288 if (!old_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02002289 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
2290 err ? err : "");
2291 errcode |= ERR_ALERT | ERR_FATAL;
2292 goto end;
2293 }
2294
William Lallemandda8584c2020-05-14 10:14:37 +02002295 /* duplicate the ckch store */
2296 new_ckchs = ckchs_dup(old_ckchs);
2297 if (!new_ckchs) {
2298 memprintf(&err, "%sCannot allocate memory!\n",
2299 err ? err : "");
2300 errcode |= ERR_ALERT | ERR_FATAL;
2301 goto end;
2302 }
2303
William Lallemand95fefa12020-09-09 12:01:33 +02002304 ckch = new_ckchs->ckch;
William Lallemandda8584c2020-05-14 10:14:37 +02002305
2306 /* appply the change on the duplicate */
William Lallemandff8bf982022-03-29 10:44:23 +02002307 if (cert_ext->load(buf->area, payload, ckch, &err) != 0) {
William Lallemandda8584c2020-05-14 10:14:37 +02002308 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
2309 errcode |= ERR_ALERT | ERR_FATAL;
2310 goto end;
2311 }
2312
William Lallemandda8584c2020-05-14 10:14:37 +02002313 /* we succeed, we can save the ckchs in the transaction */
2314
2315 /* if there wasn't a transaction, update the old ckchs */
2316 if (!ckchs_transaction.old_ckchs) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002317 ckchs_transaction.old_ckchs = old_ckchs;
2318 ckchs_transaction.path = old_ckchs->path;
William Lallemandda8584c2020-05-14 10:14:37 +02002319 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
2320 } else {
2321 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
2322
2323 }
2324
2325 /* free the previous ckchs if there was a transaction */
2326 ckch_store_free(ckchs_transaction.new_ckchs);
2327
Christopher Faulet24a20b92022-06-03 11:50:40 +02002328 ckchs_transaction.new_ckchs = new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002329
2330
2331 /* creates the SNI ctxs later in the IO handler */
2332
2333end:
2334 free_trash_chunk(buf);
2335
2336 if (errcode & ERR_CODE) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002337 ckch_store_free(new_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002338 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2339 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2340 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002341 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2342 return cli_dynmsg(appctx, LOG_NOTICE, err);
2343 }
2344 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
2345}
2346
2347/* parsing function of 'abort ssl cert' */
2348static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
2349{
2350 char *err = NULL;
2351
2352 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2353 return 1;
2354
2355 if (!*args[3])
2356 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
2357
2358 /* The operations on the CKCH architecture are locked so we can
2359 * manipulate ckch_store and ckch_inst */
2360 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2361 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2362
2363 if (!ckchs_transaction.path) {
2364 memprintf(&err, "No ongoing transaction!\n");
2365 goto error;
2366 }
2367
2368 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2369 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
2370 goto error;
2371 }
2372
2373 /* Only free the ckchs there, because the SNI and instances were not generated yet */
2374 ckch_store_free(ckchs_transaction.new_ckchs);
2375 ckchs_transaction.new_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002376 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002377 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002378
2379 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2380
2381 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2382 return cli_dynmsg(appctx, LOG_NOTICE, err);
2383
2384error:
2385 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2386
2387 return cli_dynerr(appctx, err);
2388}
2389
2390/* parsing function of 'new ssl cert' */
2391static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
2392{
2393 struct ckch_store *store;
2394 char *err = NULL;
2395 char *path;
2396
2397 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2398 return 1;
2399
2400 if (!*args[3])
2401 return cli_err(appctx, "'new ssl cert' expects a filename\n");
2402
2403 path = args[3];
2404
2405 /* The operations on the CKCH architecture are locked so we can
2406 * manipulate ckch_store and ckch_inst */
2407 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2408 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
2409
2410 store = ckchs_lookup(path);
2411 if (store != NULL) {
2412 memprintf(&err, "Certificate '%s' already exists!\n", path);
2413 store = NULL; /* we don't want to free it */
2414 goto error;
2415 }
2416 /* we won't support multi-certificate bundle here */
William Lallemandbd8e6ed2020-09-16 16:08:08 +02002417 store = ckch_store_new(path);
William Lallemandda8584c2020-05-14 10:14:37 +02002418 if (!store) {
2419 memprintf(&err, "unable to allocate memory.\n");
2420 goto error;
2421 }
2422
2423 /* insert into the ckchs tree */
2424 ebst_insert(&ckchs_tree, &store->node);
2425 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
2426
2427 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2428 return cli_dynmsg(appctx, LOG_NOTICE, err);
2429error:
2430 free(store);
2431 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2432 return cli_dynerr(appctx, err);
2433}
2434
2435/* parsing function of 'del ssl cert' */
2436static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
2437{
2438 struct ckch_store *store;
2439 char *err = NULL;
2440 char *filename;
2441
2442 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2443 return 1;
2444
2445 if (!*args[3])
2446 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
2447
2448 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2449 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
2450
2451 filename = args[3];
2452
Christopher Faulet926fefc2022-05-31 18:04:25 +02002453 if (ckchs_transaction.path && strcmp(ckchs_transaction.path, filename) == 0) {
2454 memprintf(&err, "ongoing transaction for the certificate '%s'", filename);
2455 goto error;
2456 }
2457
William Lallemandda8584c2020-05-14 10:14:37 +02002458 store = ckchs_lookup(filename);
2459 if (store == NULL) {
2460 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
2461 goto error;
2462 }
2463 if (!LIST_ISEMPTY(&store->ckch_inst)) {
2464 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
2465 goto error;
2466 }
2467
2468 ebmb_delete(&store->node);
2469 ckch_store_free(store);
2470
2471 memprintf(&err, "Certificate '%s' deleted!\n", filename);
2472
2473 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2474 return cli_dynmsg(appctx, LOG_NOTICE, err);
2475
2476error:
2477 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
2478 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2479 return cli_dynerr(appctx, err);
2480}
2481
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002482
Remi Tricot-Le Breton9f40fe02021-03-16 16:21:27 +01002483
2484/* parsing function of 'new ssl ca-file' */
2485static int cli_parse_new_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2486{
2487 struct cafile_entry *cafile_entry;
2488 char *err = NULL;
2489 char *path;
2490
2491 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2492 return 1;
2493
2494 if (!*args[3])
2495 return cli_err(appctx, "'new ssl ca-file' expects a filename\n");
2496
2497 path = args[3];
2498
2499 /* The operations on the CKCH architecture are locked so we can
2500 * manipulate ckch_store and ckch_inst */
2501 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2502 return cli_err(appctx, "Can't create a CA file!\nOperations on certificates are currently locked!\n");
2503
2504 cafile_entry = ssl_store_get_cafile_entry(path, 0);
2505 if (cafile_entry) {
2506 memprintf(&err, "CA file '%s' already exists!\n", path);
2507 goto error;
2508 }
2509
2510 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CERT);
2511 if (!cafile_entry) {
2512 memprintf(&err, "%sCannot allocate memory!\n",
2513 err ? err : "");
2514 goto error;
2515 }
2516
2517 /* Add the newly created cafile_entry to the tree so that
2518 * any new ckch instance created from now can use it. */
2519 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
2520 goto error;
2521
2522 memprintf(&err, "New CA file created '%s'!\n", path);
2523
2524 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2525 return cli_dynmsg(appctx, LOG_NOTICE, err);
2526error:
2527 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2528 return cli_dynerr(appctx, err);
2529}
2530
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002531/*
2532 * Parsing function of `set ssl ca-file`
2533 */
2534static int cli_parse_set_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2535{
Christopher Faulet132c5952022-06-03 11:56:26 +02002536 struct cafile_entry *old_cafile_entry = NULL;
2537 struct cafile_entry *new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002538 char *err = NULL;
2539 int errcode = 0;
2540 struct buffer *buf;
2541
2542 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2543 return 1;
2544
2545 if (!*args[3] || !payload)
2546 return cli_err(appctx, "'set ssl ca-file expects a filename and CAs as a payload\n");
2547
2548 /* The operations on the CKCH architecture are locked so we can
2549 * manipulate ckch_store and ckch_inst */
2550 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2551 return cli_err(appctx, "Can't update the CA file!\nOperations on certificates are currently locked!\n");
2552
2553 if ((buf = alloc_trash_chunk()) == NULL) {
2554 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2555 errcode |= ERR_ALERT | ERR_FATAL;
2556 goto end;
2557 }
2558
2559 if (!chunk_strcpy(buf, args[3])) {
2560 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2561 errcode |= ERR_ALERT | ERR_FATAL;
2562 goto end;
2563 }
2564
Christopher Faulet132c5952022-06-03 11:56:26 +02002565 old_cafile_entry = NULL;
2566 new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002567
2568 /* if there is an ongoing transaction */
2569 if (cafile_transaction.path) {
2570 /* if there is an ongoing transaction, check if this is the same file */
2571 if (strcmp(cafile_transaction.path, buf->area) != 0) {
2572 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, buf->area);
2573 errcode |= ERR_ALERT | ERR_FATAL;
2574 goto end;
2575 }
Christopher Faulet132c5952022-06-03 11:56:26 +02002576 old_cafile_entry = cafile_transaction.old_cafile_entry;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002577 }
2578 else {
2579 /* lookup for the certificate in the tree */
Christopher Faulet132c5952022-06-03 11:56:26 +02002580 old_cafile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002581 }
2582
Christopher Faulet132c5952022-06-03 11:56:26 +02002583 if (!old_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002584 memprintf(&err, "%sCan't replace a CA file which is not referenced by the configuration!\n",
2585 err ? err : "");
2586 errcode |= ERR_ALERT | ERR_FATAL;
2587 goto end;
2588 }
2589
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002590 /* Create a new cafile_entry without adding it to the cafile tree. */
Christopher Faulet132c5952022-06-03 11:56:26 +02002591 new_cafile_entry = ssl_store_create_cafile_entry(old_cafile_entry->path, NULL, CAFILE_CERT);
2592 if (!new_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002593 memprintf(&err, "%sCannot allocate memory!\n",
2594 err ? err : "");
2595 errcode |= ERR_ALERT | ERR_FATAL;
2596 goto end;
2597 }
2598
2599 /* Fill the new entry with the new CAs. */
Christopher Faulet132c5952022-06-03 11:56:26 +02002600 if (ssl_store_load_ca_from_buf(new_cafile_entry, payload)) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002601 memprintf(&err, "%sInvalid payload\n", err ? err : "");
2602 errcode |= ERR_ALERT | ERR_FATAL;
2603 goto end;
2604 }
2605
2606 /* we succeed, we can save the ca in the transaction */
2607
2608 /* if there wasn't a transaction, update the old CA */
2609 if (!cafile_transaction.old_cafile_entry) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002610 cafile_transaction.old_cafile_entry = old_cafile_entry;
2611 cafile_transaction.path = old_cafile_entry->path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002612 err = memprintf(&err, "transaction created for CA %s!\n", cafile_transaction.path);
2613 } else {
2614 err = memprintf(&err, "transaction updated for CA %s!\n", cafile_transaction.path);
2615 }
2616
2617 /* free the previous CA if there was a transaction */
2618 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2619
Christopher Faulet132c5952022-06-03 11:56:26 +02002620 cafile_transaction.new_cafile_entry = new_cafile_entry;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002621
2622 /* creates the SNI ctxs later in the IO handler */
2623
2624end:
2625 free_trash_chunk(buf);
2626
2627 if (errcode & ERR_CODE) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002628 ssl_store_delete_cafile_entry(new_cafile_entry);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002629 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2630 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2631 } else {
2632
2633 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2634 return cli_dynmsg(appctx, LOG_NOTICE, err);
2635 }
2636}
2637
2638
2639/*
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002640 * Parsing function of 'commit ssl ca-file'.
2641 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl crl-file".
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002642 */
2643static int cli_parse_commit_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2644{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002645 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002646 char *err = NULL;
2647
2648 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2649 return 1;
2650
2651 if (!*args[3])
2652 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
2653
2654 /* The operations on the CKCH architecture are locked so we can
2655 * manipulate ckch_store and ckch_inst */
2656 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2657 return cli_err(appctx, "Can't commit the CA file!\nOperations on certificates are currently locked!\n");
2658
2659 if (!cafile_transaction.path) {
2660 memprintf(&err, "No ongoing transaction! !\n");
2661 goto error;
2662 }
2663
2664 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2665 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, args[3]);
2666 goto error;
2667 }
2668 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002669 ctx->state = CACRL_ST_INIT;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002670 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002671 ctx->old_entry = cafile_transaction.old_cafile_entry;
2672 ctx->new_entry = cafile_transaction.new_cafile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002673 ctx->cafile_type = CAFILE_CERT;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002674
2675 return 0;
2676
2677error:
2678
2679 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2680 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2681
2682 return cli_dynerr(appctx, err);
2683}
2684
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002685/*
2686 * This function tries to create new ckch instances and their SNIs using a newly
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002687 * set certificate authority (CA file) or a newly set Certificate Revocation
2688 * List (CRL), depending on the command being called.
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002689 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002690static int cli_io_handler_commit_cafile_crlfile(struct appctx *appctx)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002691{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002692 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002693 struct stconn *sc = appctx_sc(appctx);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002694 int y = 0;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002695 struct cafile_entry *old_cafile_entry = ctx->old_entry;
2696 struct cafile_entry *new_cafile_entry = ctx->new_entry;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002697 struct ckch_inst_link *ckchi_link;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002698 char *path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002699
Willy Tarreau475e4632022-05-27 10:26:46 +02002700 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002701 goto end;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002702
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002703 /* The ctx was already validated by the ca-file/crl-file parsing
2704 * function. Entries can only be NULL in CACRL_ST_SUCCESS or
2705 * CACRL_ST_FIN states
2706 */
2707 switch (ctx->cafile_type) {
2708 case CAFILE_CERT:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002709 path = cafile_transaction.path;
2710 break;
2711 case CAFILE_CRL:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002712 path = crlfile_transaction.path;
2713 break;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002714 default:
Willy Tarreaud543ae02022-06-22 05:40:25 +02002715 path = NULL;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002716 goto error;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002717 }
2718
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002719 while (1) {
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002720 switch (ctx->state) {
2721 case CACRL_ST_INIT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002722 /* This state just print the update message */
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002723 chunk_printf(&trash, "Committing %s", path);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002724 if (applet_putchk(appctx, &trash) == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002725 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002726
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002727 ctx->state = CACRL_ST_GEN;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002728 /* fallthrough */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002729 case CACRL_ST_GEN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002730 /*
2731 * This state generates the ckch instances with their
2732 * sni_ctxs and SSL_CTX.
2733 *
2734 * Since the SSL_CTX generation can be CPU consumer, we
2735 * yield every 10 instances.
2736 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002737
2738 /* get the next ckchi to regenerate */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002739 ckchi_link = ctx->next_ckchi_link;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002740
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002741 /* we didn't start yet, set it to the first elem */
2742 if (ckchi_link == NULL) {
2743 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2744 /* Add the newly created cafile_entry to the tree so that
2745 * any new ckch instance created from now can use it. */
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002746 if (ssl_store_add_uncommitted_cafile_entry(new_cafile_entry)) {
2747 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002748 goto error;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002749 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002750 }
2751
2752 list_for_each_entry_from(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002753 struct ckch_inst *new_inst;
2754
2755 /* save the next ckchi to compute */
2756 ctx->next_ckchi_link = ckchi_link;
2757
2758 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2759 if (y >= 10) {
2760 applet_have_more_data(appctx); /* let's come back later */
2761 goto yield;
2762 }
2763
2764 /* display one dot per new instance */
2765 if (applet_putstr(appctx, ".") == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002766 goto yield;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002767
2768 /* Rebuild a new ckch instance that uses the same ckch_store
2769 * than a reference ckchi instance but will use a new CA file. */
2770 ctx->err = NULL;
2771 if (ckch_inst_rebuild(ckchi_link->ckch_inst->ckch_store, ckchi_link->ckch_inst, &new_inst, &ctx->err)) {
2772 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002773 goto error;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002774 }
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002775
2776 y++;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002777 }
2778
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002779 ctx->state = CACRL_ST_INSERT;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002780 /* fallthrough */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002781 case CACRL_ST_INSERT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002782 /* The generation is finished, we can insert everything */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002783
2784 /* insert the new ckch_insts in the crtlist_entry */
2785 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2786 if (ckchi_link->ckch_inst->crtlist_entry)
2787 LIST_INSERT(&ckchi_link->ckch_inst->crtlist_entry->ckch_inst,
2788 &ckchi_link->ckch_inst->by_crtlist_entry);
2789 }
2790
2791 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2792 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2793 __ssl_sock_load_new_ckch_instance(ckchi_link->ckch_inst);
2794 }
2795
2796 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
2797 list_for_each_entry(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
2798 __ckch_inst_free_locked(ckchi_link->ckch_inst);
2799 }
2800
2801
2802 /* Remove the old cafile entry from the tree */
2803 ebmb_delete(&old_cafile_entry->node);
2804 ssl_store_delete_cafile_entry(old_cafile_entry);
2805
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002806 ctx->old_entry = ctx->new_entry = NULL;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002807 ctx->state = CACRL_ST_SUCCESS;
2808 /* fallthrough */
2809 case CACRL_ST_SUCCESS:
2810 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2811 goto yield;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002812 ctx->state = CACRL_ST_FIN;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002813 /* fallthrough */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002814 case CACRL_ST_FIN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002815 /* we achieved the transaction, we can set everything to NULL */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002816 switch (ctx->cafile_type) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002817 case CAFILE_CERT:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002818 cafile_transaction.old_cafile_entry = NULL;
2819 cafile_transaction.new_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002820 cafile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002821 break;
2822 case CAFILE_CRL:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002823 crlfile_transaction.old_crlfile_entry = NULL;
2824 crlfile_transaction.new_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002825 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002826 break;
2827 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002828 goto end;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002829
2830 case CACRL_ST_ERROR:
2831 error:
2832 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2833 if (applet_putchk(appctx, &trash) == -1)
2834 goto yield;
2835 ctx->state = CACRL_ST_FIN;
2836 break;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002837 }
2838 }
2839end:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002840 /* success: call the release function and don't come back */
2841 return 1;
2842yield:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002843 return 0; /* should come back */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002844}
2845
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002846
2847/* parsing function of 'abort ssl ca-file' */
2848static int cli_parse_abort_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2849{
2850 char *err = NULL;
2851
2852 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2853 return 1;
2854
2855 if (!*args[3])
2856 return cli_err(appctx, "'abort ssl ca-file' expects a filename\n");
2857
2858 /* The operations on the CKCH architecture are locked so we can
2859 * manipulate ckch_store and ckch_inst */
2860 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2861 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2862
2863 if (!cafile_transaction.path) {
2864 memprintf(&err, "No ongoing transaction!\n");
2865 goto error;
2866 }
2867
2868 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2869 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", cafile_transaction.path, args[3]);
2870 goto error;
2871 }
2872
2873 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
2874 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2875 cafile_transaction.new_cafile_entry = NULL;
2876 cafile_transaction.old_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002877 cafile_transaction.path = NULL;
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002878
2879 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2880
2881 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2882 return cli_dynmsg(appctx, LOG_NOTICE, err);
2883
2884error:
2885 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2886
2887 return cli_dynerr(appctx, err);
2888}
2889
Willy Tarreau821c3b02022-05-04 15:47:39 +02002890/* release function of the `commit ssl ca-file' command, free things and unlock the spinlock.
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002891 * It uses a commit_cacrlfile_ctx context.
Willy Tarreau821c3b02022-05-04 15:47:39 +02002892 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002893static void cli_release_commit_cafile(struct appctx *appctx)
2894{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002895 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002896 struct cafile_entry *new_cafile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002897
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002898 /* Remove the uncommitted cafile_entry from the tree. */
2899 if (new_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002900 ebmb_delete(&new_cafile_entry->node);
2901 ssl_store_delete_cafile_entry(new_cafile_entry);
2902 }
2903 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002904 ha_free(&ctx->err);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002905}
2906
2907
Willy Tarreau821c3b02022-05-04 15:47:39 +02002908/* IO handler of details "show ssl ca-file <filename[:index]>".
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02002909 * It uses a show_cafile_ctx context, and the global
2910 * cafile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02002911 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002912static int cli_io_handler_show_cafile_detail(struct appctx *appctx)
2913{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02002914 struct show_cafile_ctx *ctx = appctx->svcctx;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02002915 struct cafile_entry *cafile_entry = ctx->cur_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002916 struct buffer *out = alloc_trash_chunk();
William Lallemand03a32e52022-04-26 18:17:15 +02002917 int i = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002918 X509 *cert;
2919 STACK_OF(X509_OBJECT) *objs;
2920 int retval = 0;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02002921 int ca_index = ctx->ca_index;
2922 int show_all = ctx->show_all;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002923
2924 if (!out)
2925 goto end_no_putchk;
2926
2927 chunk_appendf(out, "Filename: ");
2928 if (cafile_entry == cafile_transaction.new_cafile_entry)
2929 chunk_appendf(out, "*");
2930 chunk_appendf(out, "%s\n", cafile_entry->path);
2931
2932 chunk_appendf(out, "Status: ");
2933 if (!cafile_entry->ca_store)
2934 chunk_appendf(out, "Empty\n");
2935 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
2936 chunk_appendf(out, "Unused\n");
2937 else
2938 chunk_appendf(out, "Used\n");
2939
2940 if (!cafile_entry->ca_store)
2941 goto end;
2942
2943 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
William Lallemand03a32e52022-04-26 18:17:15 +02002944 for (i = ca_index; i < sk_X509_OBJECT_num(objs); i++) {
2945
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002946 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
2947 if (!cert)
2948 continue;
2949
William Lallemand03a32e52022-04-26 18:17:15 +02002950 /* file starts at line 1 */
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02002951 chunk_appendf(out, " \nCertificate #%d:\n", i+1);
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002952 retval = show_cert_detail(cert, NULL, out);
2953 if (retval < 0)
2954 goto end_no_putchk;
William Lallemand03a32e52022-04-26 18:17:15 +02002955 else if (retval)
2956 goto yield;
2957
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002958 if (applet_putchk(appctx, out) == -1)
William Lallemand03a32e52022-04-26 18:17:15 +02002959 goto yield;
William Lallemand03a32e52022-04-26 18:17:15 +02002960
2961 if (!show_all) /* only need to dump one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002962 goto end;
2963 }
2964
2965end:
William Lallemand03a32e52022-04-26 18:17:15 +02002966 free_trash_chunk(out);
2967 return 1; /* end, don't come back */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002968
2969end_no_putchk:
2970 free_trash_chunk(out);
2971 return 1;
2972yield:
William Lallemand03a32e52022-04-26 18:17:15 +02002973 /* save the current state */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02002974 ctx->ca_index = i;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002975 free_trash_chunk(out);
2976 return 0; /* should come back */
2977}
2978
2979
Willy Tarreau06305792022-05-04 15:57:30 +02002980/* parsing function for 'show ssl ca-file [cafile[:index]]'.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02002981 * It prepares a show_cafile_ctx context, and checks the global
2982 * cafile_transaction under the ckch_lock (read only).
Willy Tarreau06305792022-05-04 15:57:30 +02002983 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002984static int cli_parse_show_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2985{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02002986 struct show_cafile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002987 struct cafile_entry *cafile_entry;
William Lallemand03a32e52022-04-26 18:17:15 +02002988 int ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002989 char *colons;
2990 char *err = NULL;
2991
2992 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
2993 return cli_err(appctx, "Can't allocate memory!\n");
2994
2995 /* The operations on the CKCH architecture are locked so we can
2996 * manipulate ckch_store and ckch_inst */
2997 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2998 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
2999
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003000 ctx->show_all = 1; /* show all certificates */
3001 ctx->ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003002 /* check if there is a certificate to lookup */
3003 if (*args[3]) {
3004
3005 /* Look for an optional CA index after the CA file name */
3006 colons = strchr(args[3], ':');
3007 if (colons) {
3008 char *endptr;
3009
3010 ca_index = strtol(colons + 1, &endptr, 10);
3011 /* Indexes start at 1 */
3012 if (colons + 1 == endptr || *endptr != '\0' || ca_index <= 0) {
3013 memprintf(&err, "wrong CA index after colons in '%s'!", args[3]);
3014 goto error;
3015 }
3016 *colons = '\0';
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003017 ctx->ca_index = ca_index - 1; /* we start counting at 0 in the ca_store, but at 1 on the CLI */
3018 ctx->show_all = 0; /* show only one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003019 }
3020
3021 if (*args[3] == '*') {
3022 if (!cafile_transaction.new_cafile_entry)
3023 goto error;
3024
3025 cafile_entry = cafile_transaction.new_cafile_entry;
3026
3027 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3028 goto error;
3029
3030 } else {
3031 /* Get the "original" cafile_entry and not the
3032 * uncommitted one if it exists. */
3033 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CERT)
3034 goto error;
3035 }
3036
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003037 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003038 /* use the IO handler that shows details */
3039 appctx->io_handler = cli_io_handler_show_cafile_detail;
3040 }
3041
3042 return 0;
3043
3044error:
3045 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3046 if (err)
3047 return cli_dynerr(appctx, err);
3048 return cli_err(appctx, "Can't display the CA file : Not found!\n");
3049}
3050
3051
3052/* release function of the 'show ssl ca-file' command */
3053static void cli_release_show_cafile(struct appctx *appctx)
3054{
3055 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3056}
3057
3058
3059/* This function returns the number of certificates in a cafile_entry. */
3060static int get_certificate_count(struct cafile_entry *cafile_entry)
3061{
3062 int cert_count = 0;
3063 STACK_OF(X509_OBJECT) *objs;
3064
3065 if (cafile_entry && cafile_entry->ca_store) {
3066 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3067 if (objs)
3068 cert_count = sk_X509_OBJECT_num(objs);
3069 }
3070 return cert_count;
3071}
3072
3073/* IO handler of "show ssl ca-file". The command taking a specific CA file name
Willy Tarreau821c3b02022-05-04 15:47:39 +02003074 * is managed in cli_io_handler_show_cafile_detail.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003075 * It uses a show_cafile_ctx and the global cafile_transaction.new_cafile_entry
3076 * in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003077 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003078static int cli_io_handler_show_cafile(struct appctx *appctx)
3079{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003080 struct show_cafile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003081 struct buffer *trash = alloc_trash_chunk();
3082 struct ebmb_node *node;
Christopher Faulet677cb4f2022-06-03 16:25:35 +02003083 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003084
3085 if (trash == NULL)
3086 return 1;
3087
Christopher Faulet5a2154b2022-06-03 10:42:48 +02003088 if (!ctx->old_cafile_entry && cafile_transaction.old_cafile_entry) {
3089 chunk_appendf(trash, "# transaction\n");
3090 chunk_appendf(trash, "*%s", cafile_transaction.old_cafile_entry->path);
3091 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_transaction.new_cafile_entry));
3092 if (applet_putchk(appctx, trash) == -1)
3093 goto yield;
3094 ctx->old_cafile_entry = cafile_transaction.new_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003095 }
3096
3097 /* First time in this io_handler. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003098 if (!ctx->cur_cafile_entry) {
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003099 chunk_appendf(trash, "# filename\n");
3100 node = ebmb_first(&cafile_tree);
3101 } else {
3102 /* We yielded during a previous call. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003103 node = &ctx->cur_cafile_entry->node;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003104 }
3105
3106 while (node) {
3107 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3108 if (cafile_entry->type == CAFILE_CERT) {
3109 chunk_appendf(trash, "%s", cafile_entry->path);
3110
3111 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_entry));
3112 }
3113
3114 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003115 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003116 goto yield;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003117 }
3118
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003119 ctx->cur_cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003120 free_trash_chunk(trash);
3121 return 1;
3122yield:
3123
3124 free_trash_chunk(trash);
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003125 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003126 return 0; /* should come back */
3127}
3128
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003129/* parsing function of 'del ssl ca-file' */
3130static int cli_parse_del_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3131{
3132 struct cafile_entry *cafile_entry;
3133 char *err = NULL;
3134 char *filename;
3135
3136 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3137 return 1;
3138
3139 if (!*args[3])
3140 return cli_err(appctx, "'del ssl ca-file' expects a CA file name\n");
3141
3142 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3143 return cli_err(appctx, "Can't delete the CA file!\nOperations on certificates are currently locked!\n");
3144
3145 filename = args[3];
3146
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003147 if (cafile_transaction.path && strcmp(cafile_transaction.path, filename) == 0) {
3148 memprintf(&err, "ongoing transaction for the CA file '%s'", filename);
3149 goto error;
3150 }
3151
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003152 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3153 if (!cafile_entry) {
3154 memprintf(&err, "CA file '%s' doesn't exist!\n", filename);
3155 goto error;
3156 }
3157
3158 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3159 memprintf(&err, "CA file '%s' in use, can't be deleted!\n", filename);
3160 goto error;
3161 }
3162
3163 /* Remove the cafile_entry from the tree */
3164 ebmb_delete(&cafile_entry->node);
3165 ssl_store_delete_cafile_entry(cafile_entry);
3166
3167 memprintf(&err, "CA file '%s' deleted!\n", filename);
3168
3169 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3170 return cli_dynmsg(appctx, LOG_NOTICE, err);
3171
3172error:
3173 memprintf(&err, "Can't remove the CA file: %s\n", err ? err : "");
3174 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3175 return cli_dynerr(appctx, err);
3176}
3177
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003178/* parsing function of 'new ssl crl-file' */
3179static int cli_parse_new_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3180{
3181 struct cafile_entry *cafile_entry;
3182 char *err = NULL;
3183 char *path;
3184
3185 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3186 return 1;
3187
3188 if (!*args[3])
3189 return cli_err(appctx, "'new ssl crl-file' expects a filename\n");
3190
3191 path = args[3];
3192
3193 /* The operations on the CKCH architecture are locked so we can
3194 * manipulate ckch_store and ckch_inst */
3195 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003196 return cli_err(appctx, "Can't create a CRL file!\nOperations on certificates are currently locked!\n");
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003197
3198 cafile_entry = ssl_store_get_cafile_entry(path, 0);
3199 if (cafile_entry) {
3200 memprintf(&err, "CRL file '%s' already exists!\n", path);
3201 goto error;
3202 }
3203
3204 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CRL);
3205 if (!cafile_entry) {
3206 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3207 goto error;
3208 }
3209
3210 /* Add the newly created cafile_entry to the tree so that
3211 * any new ckch instance created from now can use it. */
3212 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
3213 goto error;
3214
3215 memprintf(&err, "New CRL file created '%s'!\n", path);
3216
3217 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3218 return cli_dynmsg(appctx, LOG_NOTICE, err);
3219error:
3220 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3221 return cli_dynerr(appctx, err);
3222}
3223
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003224/* Parsing function of `set ssl crl-file` */
3225static int cli_parse_set_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3226{
Christopher Faulet1f90f332022-06-03 16:34:30 +02003227 struct cafile_entry *old_crlfile_entry = NULL;
3228 struct cafile_entry *new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003229 char *err = NULL;
3230 int errcode = 0;
3231 struct buffer *buf;
3232
3233 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3234 return 1;
3235
3236 if (!*args[3] || !payload)
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003237 return cli_err(appctx, "'set ssl crl-file expects a filename and CRLs as a payload\n");
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003238
3239 /* The operations on the CKCH architecture are locked so we can
3240 * manipulate ckch_store and ckch_inst */
3241 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3242 return cli_err(appctx, "Can't update the CRL file!\nOperations on certificates are currently locked!\n");
3243
3244 if ((buf = alloc_trash_chunk()) == NULL) {
3245 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3246 errcode |= ERR_ALERT | ERR_FATAL;
3247 goto end;
3248 }
3249
3250 if (!chunk_strcpy(buf, args[3])) {
3251 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3252 errcode |= ERR_ALERT | ERR_FATAL;
3253 goto end;
3254 }
3255
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003256 old_crlfile_entry = NULL;
3257 new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003258
3259 /* if there is an ongoing transaction */
3260 if (crlfile_transaction.path) {
3261 /* if there is an ongoing transaction, check if this is the same file */
3262 if (strcmp(crlfile_transaction.path, buf->area) != 0) {
3263 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, buf->area);
3264 errcode |= ERR_ALERT | ERR_FATAL;
3265 goto end;
3266 }
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003267 old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003268 }
3269 else {
3270 /* lookup for the certificate in the tree */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003271 old_crlfile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003272 }
3273
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003274 if (!old_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003275 memprintf(&err, "%sCan't replace a CRL file which is not referenced by the configuration!\n",
3276 err ? err : "");
3277 errcode |= ERR_ALERT | ERR_FATAL;
3278 goto end;
3279 }
3280
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003281 /* Create a new cafile_entry without adding it to the cafile tree. */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003282 new_crlfile_entry = ssl_store_create_cafile_entry(old_crlfile_entry->path, NULL, CAFILE_CRL);
3283 if (!new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003284 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3285 errcode |= ERR_ALERT | ERR_FATAL;
3286 goto end;
3287 }
3288
3289 /* Fill the new entry with the new CRL. */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003290 if (ssl_store_load_ca_from_buf(new_crlfile_entry, payload)) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003291 memprintf(&err, "%sInvalid payload\n", err ? err : "");
3292 errcode |= ERR_ALERT | ERR_FATAL;
3293 goto end;
3294 }
3295
3296 /* we succeed, we can save the crl in the transaction */
3297
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003298 /* if there wasn't a transaction, update the old CRL */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003299 if (!crlfile_transaction.old_crlfile_entry) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003300 crlfile_transaction.old_crlfile_entry = old_crlfile_entry;
3301 crlfile_transaction.path = old_crlfile_entry->path;
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003302 err = memprintf(&err, "transaction created for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003303 } else {
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003304 err = memprintf(&err, "transaction updated for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003305 }
3306
3307 /* free the previous CRL file if there was a transaction */
3308 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3309
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003310 crlfile_transaction.new_crlfile_entry = new_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003311
3312 /* creates the SNI ctxs later in the IO handler */
3313
3314end:
3315 free_trash_chunk(buf);
3316
3317 if (errcode & ERR_CODE) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003318 ssl_store_delete_cafile_entry(new_crlfile_entry);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003319 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3320 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
3321 } else {
3322
3323 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3324 return cli_dynmsg(appctx, LOG_NOTICE, err);
3325 }
3326}
3327
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003328/* Parsing function of 'commit ssl crl-file'.
3329 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl ca-file".
3330 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003331static int cli_parse_commit_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3332{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003333 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003334 char *err = NULL;
3335
3336 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3337 return 1;
3338
3339 if (!*args[3])
3340 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
3341
3342 /* The operations on the CKCH architecture are locked so we can
3343 * manipulate ckch_store and ckch_inst */
3344 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3345 return cli_err(appctx, "Can't commit the CRL file!\nOperations on certificates are currently locked!\n");
3346
3347 if (!crlfile_transaction.path) {
3348 memprintf(&err, "No ongoing transaction! !\n");
3349 goto error;
3350 }
3351
3352 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3353 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, args[3]);
3354 goto error;
3355 }
3356 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02003357 ctx->state = CACRL_ST_INIT;
Christopher Fauletf814c4a2022-06-03 11:32:05 +02003358 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003359 ctx->old_entry = crlfile_transaction.old_crlfile_entry;
3360 ctx->new_entry = crlfile_transaction.new_crlfile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003361 ctx->cafile_type = CAFILE_CRL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003362
3363 return 0;
3364
3365error:
3366
3367 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3368 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
3369
3370 return cli_dynerr(appctx, err);
3371}
3372
3373
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003374/* release function of the `commit ssl crl-file' command, free things and unlock the spinlock.
3375 * it uses a commit_cacrlfile_ctx that's the same as for "commit ssl ca-file".
3376 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003377static void cli_release_commit_crlfile(struct appctx *appctx)
3378{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003379 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003380 struct cafile_entry *new_crlfile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003381
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003382 /* Remove the uncommitted cafile_entry from the tree. */
3383 if (new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003384 ebmb_delete(&new_crlfile_entry->node);
3385 ssl_store_delete_cafile_entry(new_crlfile_entry);
3386 }
3387 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003388 ha_free(&ctx->err);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003389}
3390
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003391/* parsing function of 'del ssl crl-file' */
3392static int cli_parse_del_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3393{
3394 struct cafile_entry *cafile_entry;
3395 char *err = NULL;
3396 char *filename;
3397
3398 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3399 return 1;
3400
3401 if (!*args[3])
3402 return cli_err(appctx, "'del ssl crl-file' expects a CRL file name\n");
3403
3404 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3405 return cli_err(appctx, "Can't delete the CRL file!\nOperations on certificates are currently locked!\n");
3406
3407 filename = args[3];
3408
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003409 if (crlfile_transaction.path && strcmp(crlfile_transaction.path, filename) == 0) {
3410 memprintf(&err, "ongoing transaction for the CRL file '%s'", filename);
3411 goto error;
3412 }
3413
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003414 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3415 if (!cafile_entry) {
3416 memprintf(&err, "CRL file '%s' doesn't exist!\n", filename);
3417 goto error;
3418 }
3419 if (cafile_entry->type != CAFILE_CRL) {
3420 memprintf(&err, "'del ssl crl-file' does not work on CA files!\n");
3421 goto error;
3422 }
3423
3424 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3425 memprintf(&err, "CRL file '%s' in use, can't be deleted!\n", filename);
3426 goto error;
3427 }
3428
3429 /* Remove the cafile_entry from the tree */
3430 ebmb_delete(&cafile_entry->node);
3431 ssl_store_delete_cafile_entry(cafile_entry);
3432
3433 memprintf(&err, "CRL file '%s' deleted!\n", filename);
3434
3435 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3436 return cli_dynmsg(appctx, LOG_NOTICE, err);
3437
3438error:
3439 memprintf(&err, "Can't remove the CRL file: %s\n", err ? err : "");
3440 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3441 return cli_dynerr(appctx, err);
3442}
3443
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003444/* parsing function of 'abort ssl crl-file' */
3445static int cli_parse_abort_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3446{
3447 char *err = NULL;
3448
3449 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3450 return 1;
3451
3452 if (!*args[3])
3453 return cli_err(appctx, "'abort ssl crl-file' expects a filename\n");
3454
3455 /* The operations on the CKCH architecture are locked so we can
3456 * manipulate ckch_store and ckch_inst */
3457 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3458 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
3459
3460 if (!crlfile_transaction.path) {
3461 memprintf(&err, "No ongoing transaction!\n");
3462 goto error;
3463 }
3464
3465 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3466 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", crlfile_transaction.path, args[3]);
3467 goto error;
3468 }
3469
3470 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3471 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3472 crlfile_transaction.new_crlfile_entry = NULL;
3473 crlfile_transaction.old_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02003474 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003475
3476 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3477
3478 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3479 return cli_dynmsg(appctx, LOG_NOTICE, err);
3480
3481error:
3482 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3483
3484 return cli_dynerr(appctx, err);
3485}
3486
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003487
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003488/*
3489 * Display a Certificate Resignation List's information.
3490 * The information displayed is inspired by the output of 'openssl crl -in
3491 * crl.pem -text'.
3492 * Returns 0 in case of success.
3493 */
3494static int show_crl_detail(X509_CRL *crl, struct buffer *out)
3495{
3496 BIO *bio = NULL;
3497 struct buffer *tmp = alloc_trash_chunk();
3498 long version;
3499 X509_NAME *issuer;
3500 int write = -1;
3501 STACK_OF(X509_REVOKED) *rev = NULL;
3502 X509_REVOKED *rev_entry = NULL;
3503 int i;
3504
3505 if (!tmp)
3506 return -1;
3507
3508 if ((bio = BIO_new(BIO_s_mem())) == NULL)
3509 goto end;
3510
3511 /* Version (as displayed by 'openssl crl') */
3512 version = X509_CRL_get_version(crl);
3513 chunk_appendf(out, "Version %ld\n", version + 1);
3514
3515 /* Signature Algorithm */
3516 chunk_appendf(out, "Signature Algorithm: %s\n", OBJ_nid2ln(X509_CRL_get_signature_nid(crl)));
3517
3518 /* Issuer */
3519 chunk_appendf(out, "Issuer: ");
3520 if ((issuer = X509_CRL_get_issuer(crl)) == NULL)
3521 goto end;
3522 if ((ssl_sock_get_dn_oneline(issuer, tmp)) == -1)
3523 goto end;
3524 *(tmp->area + tmp->data) = '\0';
3525 chunk_appendf(out, "%s\n", tmp->area);
3526
3527 /* Last Update */
3528 chunk_appendf(out, "Last Update: ");
3529 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003530 if (BIO_reset(bio) == -1)
3531 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003532 if (ASN1_TIME_print(bio, X509_CRL_get0_lastUpdate(crl)) == 0)
3533 goto end;
3534 write = BIO_read(bio, tmp->area, tmp->size-1);
3535 tmp->area[write] = '\0';
3536 chunk_appendf(out, "%s\n", tmp->area);
3537
3538
3539 /* Next Update */
3540 chunk_appendf(out, "Next Update: ");
3541 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003542 if (BIO_reset(bio) == -1)
3543 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003544 if (ASN1_TIME_print(bio, X509_CRL_get0_nextUpdate(crl)) == 0)
3545 goto end;
3546 write = BIO_read(bio, tmp->area, tmp->size-1);
3547 tmp->area[write] = '\0';
3548 chunk_appendf(out, "%s\n", tmp->area);
3549
3550
3551 /* Revoked Certificates */
3552 rev = X509_CRL_get_REVOKED(crl);
3553 if (sk_X509_REVOKED_num(rev) > 0)
3554 chunk_appendf(out, "Revoked Certificates:\n");
3555 else
3556 chunk_appendf(out, "No Revoked Certificates.\n");
3557
3558 for (i = 0; i < sk_X509_REVOKED_num(rev); i++) {
3559 rev_entry = sk_X509_REVOKED_value(rev, i);
3560
3561 /* Serial Number and Revocation Date */
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003562 if (BIO_reset(bio) == -1)
3563 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003564 BIO_printf(bio , " Serial Number: ");
Remi Tricot-Le Breton18c7d832021-05-17 18:38:34 +02003565 i2a_ASN1_INTEGER(bio, (ASN1_INTEGER*)X509_REVOKED_get0_serialNumber(rev_entry));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003566 BIO_printf(bio, "\n Revocation Date: ");
Remi Tricot-Le Bretona6b27842021-05-18 10:06:00 +02003567 if (ASN1_TIME_print(bio, X509_REVOKED_get0_revocationDate(rev_entry)) == 0)
3568 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003569 BIO_printf(bio, "\n");
3570
3571 write = BIO_read(bio, tmp->area, tmp->size-1);
3572 tmp->area[write] = '\0';
3573 chunk_appendf(out, "%s", tmp->area);
3574 }
3575
3576end:
3577 free_trash_chunk(tmp);
3578 if (bio)
3579 BIO_free(bio);
3580
3581 return 0;
3582}
3583
Willy Tarreau821c3b02022-05-04 15:47:39 +02003584/* IO handler of details "show ssl crl-file <filename[:index]>".
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003585 * It uses show_crlfile_ctx and the global
3586 * crlfile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003587 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003588static int cli_io_handler_show_crlfile_detail(struct appctx *appctx)
3589{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003590 struct show_crlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003591 struct cafile_entry *cafile_entry = ctx->cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003592 struct buffer *out = alloc_trash_chunk();
3593 int i;
3594 X509_CRL *crl;
3595 STACK_OF(X509_OBJECT) *objs;
3596 int retval = 0;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003597 int index = ctx->index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003598
3599 if (!out)
3600 goto end_no_putchk;
3601
3602 chunk_appendf(out, "Filename: ");
3603 if (cafile_entry == crlfile_transaction.new_crlfile_entry)
3604 chunk_appendf(out, "*");
3605 chunk_appendf(out, "%s\n", cafile_entry->path);
3606
3607 chunk_appendf(out, "Status: ");
3608 if (!cafile_entry->ca_store)
3609 chunk_appendf(out, "Empty\n");
3610 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3611 chunk_appendf(out, "Unused\n");
3612 else
3613 chunk_appendf(out, "Used\n");
3614
3615 if (!cafile_entry->ca_store)
3616 goto end;
3617
3618 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3619 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
3620 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
3621 if (!crl)
3622 continue;
3623
3624 /* CRL indexes start at 1 on the CLI output. */
3625 if (index && index-1 != i)
3626 continue;
3627
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003628 chunk_appendf(out, " \nCertificate Revocation List #%d:\n", i+1);
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003629 retval = show_crl_detail(crl, out);
3630 if (retval < 0)
3631 goto end_no_putchk;
3632 else if (retval || index)
3633 goto end;
3634 }
3635
3636end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003637 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003638 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003639
3640end_no_putchk:
3641 free_trash_chunk(out);
3642 return 1;
3643yield:
3644 free_trash_chunk(out);
3645 return 0; /* should come back */
3646}
3647
Willy Tarreau821c3b02022-05-04 15:47:39 +02003648/* parsing function for 'show ssl crl-file [crlfile[:index]]'.
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003649 * It sets the context to a show_crlfile_ctx, and the global
Willy Tarreau821c3b02022-05-04 15:47:39 +02003650 * cafile_transaction.new_crlfile_entry under the ckch_lock.
3651 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003652static int cli_parse_show_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3653{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003654 struct show_crlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003655 struct cafile_entry *cafile_entry;
3656 long index = 0;
3657 char *colons;
3658 char *err = NULL;
3659
3660 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3661 return cli_err(appctx, "Can't allocate memory!\n");
3662
3663 /* The operations on the CKCH architecture are locked so we can
3664 * manipulate ckch_store and ckch_inst */
3665 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3666 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3667
3668 /* check if there is a certificate to lookup */
3669 if (*args[3]) {
3670
3671 /* Look for an optional index after the CRL file name */
3672 colons = strchr(args[3], ':');
3673 if (colons) {
3674 char *endptr;
3675
3676 index = strtol(colons + 1, &endptr, 10);
3677 /* Indexes start at 1 */
3678 if (colons + 1 == endptr || *endptr != '\0' || index <= 0) {
3679 memprintf(&err, "wrong CRL index after colons in '%s'!", args[3]);
3680 goto error;
3681 }
3682 *colons = '\0';
3683 }
3684
3685 if (*args[3] == '*') {
3686 if (!crlfile_transaction.new_crlfile_entry)
3687 goto error;
3688
3689 cafile_entry = crlfile_transaction.new_crlfile_entry;
3690
3691 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3692 goto error;
3693
3694 } else {
3695 /* Get the "original" cafile_entry and not the
3696 * uncommitted one if it exists. */
3697 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CRL)
3698 goto error;
3699 }
3700
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003701 ctx->cafile_entry = cafile_entry;
3702 ctx->index = index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003703 /* use the IO handler that shows details */
3704 appctx->io_handler = cli_io_handler_show_crlfile_detail;
3705 }
3706
3707 return 0;
3708
3709error:
3710 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3711 if (err)
3712 return cli_dynerr(appctx, err);
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003713 return cli_err(appctx, "Can't display the CRL file : Not found!\n");
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003714}
3715
3716/* IO handler of "show ssl crl-file". The command taking a specific CRL file name
3717 * is managed in cli_io_handler_show_crlfile_detail. */
3718static int cli_io_handler_show_crlfile(struct appctx *appctx)
3719{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003720 struct show_crlfile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003721 struct buffer *trash = alloc_trash_chunk();
3722 struct ebmb_node *node;
Christopher Faulet88041b32022-06-03 16:26:56 +02003723 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003724
3725 if (trash == NULL)
3726 return 1;
3727
Christopher Faulet9a99e542022-06-03 10:32:18 +02003728 if (!ctx->old_crlfile_entry && crlfile_transaction.old_crlfile_entry) {
3729 chunk_appendf(trash, "# transaction\n");
3730 chunk_appendf(trash, "*%s\n", crlfile_transaction.old_crlfile_entry->path);
3731 if (applet_putchk(appctx, trash) == -1)
3732 goto yield;
3733 ctx->old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003734 }
3735
3736 /* First time in this io_handler. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003737 if (!ctx->cafile_entry) {
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003738 chunk_appendf(trash, "# filename\n");
3739 node = ebmb_first(&cafile_tree);
3740 } else {
3741 /* We yielded during a previous call. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003742 node = &ctx->cafile_entry->node;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003743 }
3744
3745 while (node) {
3746 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3747 if (cafile_entry->type == CAFILE_CRL) {
3748 chunk_appendf(trash, "%s\n", cafile_entry->path);
3749 }
3750
3751 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003752 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003753 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003754 }
3755
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003756 ctx->cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003757 free_trash_chunk(trash);
3758 return 1;
3759yield:
3760
3761 free_trash_chunk(trash);
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003762 ctx->cafile_entry = cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003763 return 0; /* should come back */
3764}
3765
3766
3767/* release function of the 'show ssl crl-file' command */
3768static void cli_release_show_crlfile(struct appctx *appctx)
3769{
3770 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3771}
3772
3773
William Lallemandee8530c2020-06-23 18:19:42 +02003774void ckch_deinit()
3775{
3776 struct eb_node *node, *next;
3777 struct ckch_store *store;
William Lallemandb0c48272022-04-26 15:44:53 +02003778 struct ebmb_node *canode;
William Lallemandee8530c2020-06-23 18:19:42 +02003779
William Lallemandb0c48272022-04-26 15:44:53 +02003780 /* deinit the ckch stores */
William Lallemandee8530c2020-06-23 18:19:42 +02003781 node = eb_first(&ckchs_tree);
3782 while (node) {
3783 next = eb_next(node);
3784 store = ebmb_entry(node, struct ckch_store, node);
3785 ckch_store_free(store);
3786 node = next;
3787 }
William Lallemandb0c48272022-04-26 15:44:53 +02003788
3789 /* deinit the ca-file store */
3790 canode = ebmb_first(&cafile_tree);
3791 while (canode) {
3792 struct cafile_entry *entry = NULL;
3793
3794 entry = ebmb_entry(canode, struct cafile_entry, node);
3795 canode = ebmb_next(canode);
3796 ssl_store_delete_cafile_entry(entry);
3797 }
William Lallemandee8530c2020-06-23 18:19:42 +02003798}
William Lallemandda8584c2020-05-14 10:14:37 +02003799
3800/* register cli keywords */
3801static struct cli_kw_list cli_kws = {{ },{
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003802 { { "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 },
3803 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
3804 { { "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 },
3805 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
3806 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
3807 { { "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 },
3808
Amaury Denoyelleb11ad9e2021-05-21 11:01:10 +02003809 { { "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 +01003810 { { "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 +02003811 { { "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 +01003812 { { "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 +01003813 { { "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 +01003814 { { "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 +02003815
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003816 { { "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 +02003817 { { "set", "ssl", "crl-file", NULL }, "set ssl crl-file <crlfile> <payload> : replace a CRL file", cli_parse_set_crlfile, NULL, NULL },
3818 { { "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 +02003819 { { "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 +02003820 { { "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 +02003821 { { "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 +02003822 { { NULL }, NULL, NULL, NULL }
3823}};
3824
3825INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3826