blob: 0992240e22b374194c8c994b5083d944737603db [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
William Lallemand0f17ab22022-07-19 18:03:16 +02001155/*
1156 * Try to load a ca-file from disk into the ca-file cache.
1157 *
1158 * Return 0 upon error
1159 */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001160int ssl_store_load_locations_file(char *path, int create_if_none, enum cafile_type type)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001161{
1162 X509_STORE *store = ssl_store_get0_locations_file(path);
1163
1164 /* If this function is called by the CLI, we should not call the
1165 * X509_STORE_load_locations function because it performs forbidden disk
1166 * accesses. */
1167 if (!store && create_if_none) {
William Lallemand87fd9942022-04-01 20:12:03 +02001168 STACK_OF(X509_OBJECT) *objs;
1169 int cert_count = 0;
1170 struct stat buf;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001171 struct cafile_entry *ca_e;
William Lallemandc6b17632022-04-01 23:39:37 +02001172 const char *file = NULL;
1173 const char *dir = NULL;
William Lallemand0f17ab22022-07-19 18:03:16 +02001174 unsigned long e;
William Lallemand87fd9942022-04-01 20:12:03 +02001175
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001176 store = X509_STORE_new();
William Lallemand0f17ab22022-07-19 18:03:16 +02001177 if (!store) {
1178 ha_alert("Cannot allocate memory!");
1179 goto err;
1180 }
William Lallemand87fd9942022-04-01 20:12:03 +02001181
William Lallemandc6b17632022-04-01 23:39:37 +02001182 if (strcmp(path, "@system-ca") == 0) {
1183 dir = X509_get_default_cert_dir();
William Lallemand0f17ab22022-07-19 18:03:16 +02001184 if (!dir) {
1185 ha_alert("Couldn't get the system CA directory from X509_get_default_cert_dir().");
1186 goto err;
1187 }
William Lallemand87fd9942022-04-01 20:12:03 +02001188
William Lallemandc6b17632022-04-01 23:39:37 +02001189 } else {
1190
William Lallemand0f17ab22022-07-19 18:03:16 +02001191 if (stat(path, &buf) == -1) {
1192 ha_alert("Couldn't open the ca-file '%s' (%s).", path, strerror(errno));
William Lallemandc6b17632022-04-01 23:39:37 +02001193 goto err;
William Lallemand0f17ab22022-07-19 18:03:16 +02001194 }
William Lallemandc6b17632022-04-01 23:39:37 +02001195
1196 if (S_ISDIR(buf.st_mode))
1197 dir = path;
1198 else
1199 file = path;
1200 }
William Lallemand87fd9942022-04-01 20:12:03 +02001201
1202 if (file) {
1203 if (!X509_STORE_load_locations(store, file, NULL)) {
William Lallemand0f17ab22022-07-19 18:03:16 +02001204 e = ERR_get_error();
1205 ha_alert("Couldn't open the ca-file '%s' (%s).", path, ERR_reason_error_string(e));
William Lallemand87fd9942022-04-01 20:12:03 +02001206 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001207 }
William Lallemand80296b42022-04-05 10:19:30 +02001208 } else if (dir) {
William Lallemand87fd9942022-04-01 20:12:03 +02001209 int n, i;
1210 struct dirent **de_list;
1211
1212 n = scandir(dir, &de_list, 0, alphasort);
1213 if (n < 0)
1214 goto err;
1215
1216 for (i= 0; i < n; i++) {
1217 char *end;
1218 struct dirent *de = de_list[i];
1219 BIO *in = NULL;
1220 X509 *ca = NULL;;
1221
William Lallemand43482322022-07-18 18:42:52 +02001222 ERR_clear_error();
1223
William Lallemand87fd9942022-04-01 20:12:03 +02001224 /* we try to load the files that would have
1225 * been loaded in an hashed directory loaded by
1226 * X509_LOOKUP_hash_dir, so according to "man 1
1227 * c_rehash", we should load ".pem", ".crt",
William Lallemande4b93eb2022-05-09 09:29:00 +02001228 * ".cer", or ".crl". Files starting with a dot
1229 * are ignored.
William Lallemand87fd9942022-04-01 20:12:03 +02001230 */
1231 end = strrchr(de->d_name, '.');
William Lallemande4b93eb2022-05-09 09:29:00 +02001232 if (!end || de->d_name[0] == '.' ||
1233 (strcmp(end, ".pem") != 0 &&
1234 strcmp(end, ".crt") != 0 &&
1235 strcmp(end, ".cer") != 0 &&
1236 strcmp(end, ".crl") != 0)) {
William Lallemand87fd9942022-04-01 20:12:03 +02001237 free(de);
1238 continue;
1239 }
1240 in = BIO_new(BIO_s_file());
1241 if (in == NULL)
1242 goto scandir_err;
1243
William Lallemandc6b17632022-04-01 23:39:37 +02001244 chunk_printf(&trash, "%s/%s", dir, de->d_name);
William Lallemand87fd9942022-04-01 20:12:03 +02001245
1246 if (BIO_read_filename(in, trash.area) == 0)
1247 goto scandir_err;
1248
1249 if (PEM_read_bio_X509_AUX(in, &ca, NULL, NULL) == NULL)
1250 goto scandir_err;
1251
William Lallemand43482322022-07-18 18:42:52 +02001252 if (X509_STORE_add_cert(store, ca) == 0) {
1253 /* only exits on error if the error is not about duplicate certificates */
1254 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1255 goto scandir_err;
1256 }
1257 }
William Lallemand87fd9942022-04-01 20:12:03 +02001258
William Lallemand4cfbf3c2022-04-26 15:57:33 +02001259 X509_free(ca);
William Lallemand87fd9942022-04-01 20:12:03 +02001260 BIO_free(in);
1261 free(de);
1262 continue;
1263
1264scandir_err:
William Lallemand0f17ab22022-07-19 18:03:16 +02001265 e = ERR_get_error();
William Lallemand4cfbf3c2022-04-26 15:57:33 +02001266 X509_free(ca);
William Lallemand87fd9942022-04-01 20:12:03 +02001267 BIO_free(in);
1268 free(de);
William Lallemand0f17ab22022-07-19 18:03:16 +02001269 /* warn if it can load one of the files, but don't abort */
1270 ha_warning("ca-file: '%s' couldn't load '%s' (%s)\n", path, trash.area, ERR_reason_error_string(e));
William Lallemand87fd9942022-04-01 20:12:03 +02001271
1272 }
1273 free(de_list);
William Lallemand80296b42022-04-05 10:19:30 +02001274 } else {
1275 ha_alert("ca-file: couldn't load '%s'\n", path);
1276 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001277 }
William Lallemand87fd9942022-04-01 20:12:03 +02001278
1279 objs = X509_STORE_get0_objects(store);
1280 cert_count = sk_X509_OBJECT_num(objs);
William Lallemand0f17ab22022-07-19 18:03:16 +02001281 if (cert_count == 0) {
William Lallemand87fd9942022-04-01 20:12:03 +02001282 ha_warning("ca-file: 0 CA were loaded from '%s'\n", path);
William Lallemand0f17ab22022-07-19 18:03:16 +02001283 }
William Lallemand87fd9942022-04-01 20:12:03 +02001284 ca_e = ssl_store_create_cafile_entry(path, store, type);
William Lallemand0f17ab22022-07-19 18:03:16 +02001285 if (!ca_e) {
1286 ha_alert("Cannot allocate memory!\n");
William Lallemand87fd9942022-04-01 20:12:03 +02001287 goto err;
William Lallemand0f17ab22022-07-19 18:03:16 +02001288 }
William Lallemand87fd9942022-04-01 20:12:03 +02001289 ebst_insert(&cafile_tree, &ca_e->node);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001290 }
1291 return (store != NULL);
William Lallemand87fd9942022-04-01 20:12:03 +02001292
1293err:
1294 X509_STORE_free(store);
1295 store = NULL;
1296 return 0;
1297
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001298}
1299
1300
William Lallemandda8584c2020-05-14 10:14:37 +02001301/*************************** CLI commands ***********************/
1302
1303/* Type of SSL payloads that can be updated over the CLI */
1304
William Lallemandff8bf982022-03-29 10:44:23 +02001305struct cert_exts cert_exts[] = {
1306 { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand26654e72022-03-30 12:01:32 +02001307 { "crt", CERT_TYPE_CRT, &ssl_sock_load_pem_into_ckch },
William Lallemandff8bf982022-03-29 10:44:23 +02001308 { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemandda8584c2020-05-14 10:14:37 +02001309#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemandff8bf982022-03-29 10:44:23 +02001310 { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001311#endif
Ilya Shipitsinc47d6762021-02-13 11:45:33 +05001312#ifdef HAVE_SSL_SCTL
William Lallemandff8bf982022-03-29 10:44:23 +02001313 { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001314#endif
William Lallemandff8bf982022-03-29 10:44:23 +02001315 { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
1316 { NULL, CERT_TYPE_MAX, NULL },
William Lallemandda8584c2020-05-14 10:14:37 +02001317};
1318
1319
1320/* release function of the `show ssl cert' command */
1321static void cli_release_show_cert(struct appctx *appctx)
1322{
1323 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1324}
1325
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001326/* IO handler of "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001327 * It makes use of a show_cert_ctx context, and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001328 */
William Lallemandda8584c2020-05-14 10:14:37 +02001329static int cli_io_handler_show_cert(struct appctx *appctx)
1330{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001331 struct show_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001332 struct buffer *trash = alloc_trash_chunk();
1333 struct ebmb_node *node;
Christopher Fauletd1d2e4d2022-06-03 16:24:02 +02001334 struct ckch_store *ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001335
1336 if (trash == NULL)
1337 return 1;
1338
Christopher Faulet3e94f5d2022-06-03 10:46:40 +02001339 if (!ctx->old_ckchs && ckchs_transaction.old_ckchs) {
1340 ckchs = ckchs_transaction.old_ckchs;
1341 chunk_appendf(trash, "# transaction\n");
1342 chunk_appendf(trash, "*%s\n", ckchs->path);
1343 if (applet_putchk(appctx, trash) == -1)
1344 goto yield;
1345 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001346 }
1347
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001348 if (!ctx->cur_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02001349 chunk_appendf(trash, "# filename\n");
1350 node = ebmb_first(&ckchs_tree);
1351 } else {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001352 node = &ctx->cur_ckchs->node;
William Lallemandda8584c2020-05-14 10:14:37 +02001353 }
1354 while (node) {
1355 ckchs = ebmb_entry(node, struct ckch_store, node);
William Lallemand5685ccf2020-09-16 16:12:25 +02001356 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001357
1358 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001359 if (applet_putchk(appctx, trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001360 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001361 }
1362
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001363 ctx->cur_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001364 free_trash_chunk(trash);
1365 return 1;
1366yield:
1367
1368 free_trash_chunk(trash);
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001369 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001370 return 0; /* should come back */
1371}
1372
1373/*
1374 * Extract and format the DNS SAN extensions and copy result into a chuink
1375 * Return 0;
1376 */
1377#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1378static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
1379{
1380 int i;
1381 char *str;
1382 STACK_OF(GENERAL_NAME) *names = NULL;
1383
1384 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1385 if (names) {
1386 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
1387 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
1388 if (i > 0)
1389 chunk_appendf(out, ", ");
1390 if (name->type == GEN_DNS) {
1391 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
1392 chunk_appendf(out, "DNS:%s", str);
1393 OPENSSL_free(str);
1394 }
1395 }
1396 }
1397 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
1398 }
1399 return 0;
1400}
1401#endif
1402
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001403/*
1404 * Build the ckch_inst_link that will be chained in the CA file entry and the
1405 * corresponding ckch_inst_link_ref that will be chained in the ckch instance.
1406 * Return 0 in case of success.
1407 */
1408static int do_chain_inst_and_cafile(struct cafile_entry *cafile_entry, struct ckch_inst *ckch_inst)
1409{
1410 struct ckch_inst_link *new_link;
1411 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
1412 struct ckch_inst_link *link = LIST_ELEM(cafile_entry->ckch_inst_link.n,
1413 typeof(link), list);
1414 /* Do not add multiple references to the same
1415 * instance in a cafile_entry */
1416 if (link->ckch_inst == ckch_inst) {
1417 return 1;
1418 }
1419 }
1420
1421 new_link = calloc(1, sizeof(*new_link));
1422 if (new_link) {
1423 struct ckch_inst_link_ref *new_link_ref = calloc(1, sizeof(*new_link_ref));
1424 if (!new_link_ref) {
1425 free(new_link);
1426 return 1;
1427 }
1428
1429 new_link->ckch_inst = ckch_inst;
1430 new_link_ref->link = new_link;
1431 LIST_INIT(&new_link->list);
1432 LIST_INIT(&new_link_ref->list);
1433
1434 LIST_APPEND(&cafile_entry->ckch_inst_link, &new_link->list);
1435 LIST_APPEND(&ckch_inst->cafile_link_refs, &new_link_ref->list);
1436 }
1437
1438 return 0;
1439}
1440
1441
1442/*
1443 * Link a CA file tree entry to the ckch instance that uses it.
1444 * To determine if and which CA file tree entries need to be linked to the
1445 * instance, we follow the same logic performed in ssl_sock_prepare_ctx when
1446 * processing the verify option.
1447 * This function works for a frontend as well as for a backend, depending on the
1448 * configuration parameters given (bind_conf or server).
1449 */
1450void ckch_inst_add_cafile_link(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf,
1451 struct ssl_bind_conf *ssl_conf, const struct server *srv)
1452{
1453 int verify = SSL_VERIFY_NONE;
1454
1455 if (srv) {
1456
1457 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
1458 verify = SSL_VERIFY_PEER;
1459 switch (srv->ssl_ctx.verify) {
1460 case SSL_SOCK_VERIFY_NONE:
1461 verify = SSL_VERIFY_NONE;
1462 break;
1463 case SSL_SOCK_VERIFY_REQUIRED:
1464 verify = SSL_VERIFY_PEER;
1465 break;
1466 }
1467 }
1468 else {
1469 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
1470 case SSL_SOCK_VERIFY_NONE:
1471 verify = SSL_VERIFY_NONE;
1472 break;
1473 case SSL_SOCK_VERIFY_OPTIONAL:
1474 verify = SSL_VERIFY_PEER;
1475 break;
1476 case SSL_SOCK_VERIFY_REQUIRED:
1477 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1478 break;
1479 }
1480 }
1481
1482 if (verify & SSL_VERIFY_PEER) {
1483 struct cafile_entry *ca_file_entry = NULL;
1484 struct cafile_entry *ca_verify_file_entry = NULL;
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001485 struct cafile_entry *crl_file_entry = NULL;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001486 if (srv) {
1487 if (srv->ssl_ctx.ca_file) {
1488 ca_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.ca_file, 0);
1489
1490 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001491 if (srv->ssl_ctx.crl_file) {
1492 crl_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.crl_file, 0);
1493 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001494 }
1495 else {
1496 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
1497 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 +02001498 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 +01001499
1500 if (ca_file)
1501 ca_file_entry = ssl_store_get_cafile_entry(ca_file, 0);
1502 if (ca_verify_file)
1503 ca_verify_file_entry = ssl_store_get_cafile_entry(ca_verify_file, 0);
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001504 if (crl_file)
1505 crl_file_entry = ssl_store_get_cafile_entry(crl_file, 0);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001506 }
1507
1508 if (ca_file_entry) {
1509 /* If we have a ckch instance that is not already in the
1510 * cafile_entry's list, add it to it. */
1511 if (do_chain_inst_and_cafile(ca_file_entry, ckch_inst))
1512 return;
1513
1514 }
1515 if (ca_verify_file_entry && (ca_file_entry != ca_verify_file_entry)) {
1516 /* If we have a ckch instance that is not already in the
1517 * cafile_entry's list, add it to it. */
1518 if (do_chain_inst_and_cafile(ca_verify_file_entry, ckch_inst))
1519 return;
1520 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001521 if (crl_file_entry) {
1522 /* If we have a ckch instance that is not already in the
1523 * cafile_entry's list, add it to it. */
1524 if (do_chain_inst_and_cafile(crl_file_entry, ckch_inst))
1525 return;
1526 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001527 }
1528}
1529
William Lallemandda8584c2020-05-14 10:14:37 +02001530
1531
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001532static int show_cert_detail(X509 *cert, STACK_OF(X509) *chain, struct buffer *out)
William Lallemandda8584c2020-05-14 10:14:37 +02001533{
William Lallemandda8584c2020-05-14 10:14:37 +02001534 BIO *bio = NULL;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001535 struct buffer *tmp = alloc_trash_chunk();
William Lallemandda8584c2020-05-14 10:14:37 +02001536 int i;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001537 int write = -1;
1538 unsigned int len = 0;
1539 X509_NAME *name = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001540
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001541 if (!tmp)
1542 return -1;
William Lallemandda8584c2020-05-14 10:14:37 +02001543
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001544 if (!cert)
William Lallemand5685ccf2020-09-16 16:12:25 +02001545 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02001546
William Lallemand5685ccf2020-09-16 16:12:25 +02001547 if (chain == NULL) {
1548 struct issuer_chain *issuer;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001549 issuer = ssl_get0_issuer_chain(cert);
William Lallemand5685ccf2020-09-16 16:12:25 +02001550 if (issuer) {
1551 chain = issuer->chain;
1552 chunk_appendf(out, "Chain Filename: ");
1553 chunk_appendf(out, "%s\n", issuer->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001554 }
William Lallemand5685ccf2020-09-16 16:12:25 +02001555 }
1556 chunk_appendf(out, "Serial: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001557 if (ssl_sock_get_serial(cert, tmp) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001558 goto end;
1559 dump_binary(out, tmp->area, tmp->data);
1560 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001561
William Lallemand5685ccf2020-09-16 16:12:25 +02001562 chunk_appendf(out, "notBefore: ");
1563 chunk_reset(tmp);
1564 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1565 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001566 if (ASN1_TIME_print(bio, X509_getm_notBefore(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001567 goto end;
1568 write = BIO_read(bio, tmp->area, tmp->size-1);
1569 tmp->area[write] = '\0';
1570 BIO_free(bio);
1571 bio = NULL;
1572 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001573
William Lallemand5685ccf2020-09-16 16:12:25 +02001574 chunk_appendf(out, "notAfter: ");
1575 chunk_reset(tmp);
1576 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1577 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001578 if (ASN1_TIME_print(bio, X509_getm_notAfter(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001579 goto end;
1580 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
1581 goto end;
1582 tmp->area[write] = '\0';
1583 BIO_free(bio);
1584 bio = NULL;
1585 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001586
1587#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand5685ccf2020-09-16 16:12:25 +02001588 chunk_appendf(out, "Subject Alternative Name: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001589 if (ssl_sock_get_san_oneline(cert, out) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001590 goto end;
1591 *(out->area + out->data) = '\0';
1592 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001593#endif
William Lallemand5685ccf2020-09-16 16:12:25 +02001594 chunk_reset(tmp);
1595 chunk_appendf(out, "Algorithm: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001596 if (cert_get_pkey_algo(cert, tmp) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001597 goto end;
1598 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001599
William Lallemand5685ccf2020-09-16 16:12:25 +02001600 chunk_reset(tmp);
1601 chunk_appendf(out, "SHA1 FingerPrint: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001602 if (X509_digest(cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001603 goto end;
1604 tmp->data = len;
1605 dump_binary(out, tmp->area, tmp->data);
1606 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001607
William Lallemand5685ccf2020-09-16 16:12:25 +02001608 chunk_appendf(out, "Subject: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001609 if ((name = X509_get_subject_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +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);
1615
1616 chunk_appendf(out, "Issuer: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001617 if ((name = X509_get_issuer_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001618 goto end;
1619 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1620 goto end;
1621 *(tmp->area + tmp->data) = '\0';
1622 chunk_appendf(out, "%s\n", tmp->area);
1623
1624 /* Displays subject of each certificate in the chain */
1625 for (i = 0; i < sk_X509_num(chain); i++) {
1626 X509 *ca = sk_X509_value(chain, i);
1627
1628 chunk_appendf(out, "Chain Subject: ");
1629 if ((name = X509_get_subject_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001630 goto end;
1631 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1632 goto end;
1633 *(tmp->area + tmp->data) = '\0';
1634 chunk_appendf(out, "%s\n", tmp->area);
1635
William Lallemand5685ccf2020-09-16 16:12:25 +02001636 chunk_appendf(out, "Chain Issuer: ");
1637 if ((name = X509_get_issuer_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001638 goto end;
1639 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1640 goto end;
1641 *(tmp->area + tmp->data) = '\0';
1642 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001643 }
1644
1645end:
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001646 if (bio)
1647 BIO_free(bio);
1648 free_trash_chunk(tmp);
1649
1650 return 0;
1651}
1652
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001653#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 +02001654/*
1655 * Build the OCSP tree entry's key for a given ckch_store.
1656 * Returns a negative value in case of error.
1657 */
1658static int ckch_store_build_certid(struct ckch_store *ckch_store, unsigned char certid[128], unsigned int *key_length)
1659{
1660 OCSP_RESPONSE *resp;
1661 OCSP_BASICRESP *bs = NULL;
1662 OCSP_SINGLERESP *sr;
1663 OCSP_CERTID *id;
1664 unsigned char *p = NULL;
1665
1666 if (!key_length)
1667 return -1;
1668
1669 *key_length = 0;
1670
1671 if (!ckch_store->ckch->ocsp_response)
1672 return 0;
1673
1674 p = (unsigned char *) ckch_store->ckch->ocsp_response->area;
1675
1676 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
1677 ckch_store->ckch->ocsp_response->data);
1678 if (!resp) {
1679 goto end;
1680 }
1681
1682 bs = OCSP_response_get1_basic(resp);
1683 if (!bs) {
1684 goto end;
1685 }
1686
1687 sr = OCSP_resp_get0(bs, 0);
1688 if (!sr) {
1689 goto end;
1690 }
1691
1692 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
1693
1694 p = certid;
1695 *key_length = i2d_OCSP_CERTID(id, &p);
1696
1697end:
1698 return *key_length > 0;
1699}
1700#endif
1701
1702/*
1703 * Dump the OCSP certificate key (if it exists) of certificate <ckch> into
1704 * buffer <out>.
1705 * Returns 0 in case of success.
1706 */
1707static int ckch_store_show_ocsp_certid(struct ckch_store *ckch_store, struct buffer *out)
1708{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001709#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 +02001710 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1711 unsigned int key_length = 0;
1712 int i;
1713
1714 if (ckch_store_build_certid(ckch_store, (unsigned char*)key, &key_length) >= 0) {
1715 /* Dump the CERTID info */
1716 chunk_appendf(out, "OCSP Response Key: ");
1717 for (i = 0; i < key_length; ++i) {
1718 chunk_appendf(out, "%02x", key[i]);
1719 }
1720 chunk_appendf(out, "\n");
1721 }
1722#endif
1723
1724 return 0;
1725}
1726
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001727
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001728/* IO handler of the details "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001729 * It uses a struct show_cert_ctx and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001730 */
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001731static int cli_io_handler_show_cert_detail(struct appctx *appctx)
1732{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001733 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001734 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001735 struct buffer *out = alloc_trash_chunk();
1736 int retval = 0;
1737
1738 if (!out)
1739 goto end_no_putchk;
1740
1741 chunk_appendf(out, "Filename: ");
1742 if (ckchs == ckchs_transaction.new_ckchs)
1743 chunk_appendf(out, "*");
1744 chunk_appendf(out, "%s\n", ckchs->path);
1745
1746 chunk_appendf(out, "Status: ");
1747 if (ckchs->ckch->cert == NULL)
1748 chunk_appendf(out, "Empty\n");
1749 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
1750 chunk_appendf(out, "Unused\n");
1751 else
1752 chunk_appendf(out, "Used\n");
1753
1754 retval = show_cert_detail(ckchs->ckch->cert, ckchs->ckch->chain, out);
1755 if (retval < 0)
1756 goto end_no_putchk;
1757 else if (retval)
1758 goto end;
1759
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001760 ckch_store_show_ocsp_certid(ckchs, out);
1761
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001762end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001763 if (applet_putchk(appctx, out) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001764 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001765
1766end_no_putchk:
William Lallemandda8584c2020-05-14 10:14:37 +02001767 free_trash_chunk(out);
1768 return 1;
1769yield:
William Lallemandda8584c2020-05-14 10:14:37 +02001770 free_trash_chunk(out);
1771 return 0; /* should come back */
1772}
1773
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001774
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001775/* IO handler of the details "show ssl cert <filename.ocsp>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001776 * It uses a show_cert_ctx.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001777 */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001778static int cli_io_handler_show_cert_ocsp_detail(struct appctx *appctx)
1779{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001780#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL)
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001781 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001782 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001783 struct buffer *out = alloc_trash_chunk();
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001784 int from_transaction = ctx->transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001785
1786 if (!out)
1787 goto end_no_putchk;
1788
1789 /* If we try to display an ongoing transaction's OCSP response, we
1790 * need to dump the ckch's ocsp_response buffer directly.
1791 * Otherwise, we must rebuild the certificate's certid in order to
1792 * look for the current OCSP response in the tree. */
1793 if (from_transaction && ckchs->ckch->ocsp_response) {
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001794 if (ssl_ocsp_response_print(ckchs->ckch->ocsp_response, out))
1795 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001796 }
1797 else {
1798 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1799 unsigned int key_length = 0;
1800
1801 if (ckch_store_build_certid(ckchs, (unsigned char*)key, &key_length) < 0)
1802 goto end_no_putchk;
1803
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001804 if (ssl_get_ocspresponse_detail(key, out))
1805 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001806 }
1807
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001808 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001809 goto yield;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001810
1811end_no_putchk:
1812 free_trash_chunk(out);
1813 return 1;
1814yield:
1815 free_trash_chunk(out);
1816 return 0; /* should come back */
1817#else
1818 return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n");
1819#endif
1820}
1821
William Lallemandda8584c2020-05-14 10:14:37 +02001822/* parsing function for 'show ssl cert [certfile]' */
1823static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
1824{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001825 struct show_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02001826 struct ckch_store *ckchs;
1827
1828 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1829 return cli_err(appctx, "Can't allocate memory!\n");
1830
1831 /* The operations on the CKCH architecture are locked so we can
1832 * manipulate ckch_store and ckch_inst */
1833 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1834 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
1835
1836 /* check if there is a certificate to lookup */
1837 if (*args[3]) {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001838 int show_ocsp_detail = 0;
1839 int from_transaction = 0;
1840 char *end;
1841
1842 /* We manage the special case "certname.ocsp" through which we
1843 * can show the details of an OCSP response. */
1844 end = strrchr(args[3], '.');
1845 if (end && strcmp(end+1, "ocsp") == 0) {
1846 *end = '\0';
1847 show_ocsp_detail = 1;
1848 }
1849
William Lallemandda8584c2020-05-14 10:14:37 +02001850 if (*args[3] == '*') {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001851 from_transaction = 1;
William Lallemandda8584c2020-05-14 10:14:37 +02001852 if (!ckchs_transaction.new_ckchs)
1853 goto error;
1854
1855 ckchs = ckchs_transaction.new_ckchs;
1856
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001857 if (strcmp(args[3] + 1, ckchs->path) != 0)
William Lallemandda8584c2020-05-14 10:14:37 +02001858 goto error;
1859
1860 } else {
1861 if ((ckchs = ckchs_lookup(args[3])) == NULL)
1862 goto error;
1863
1864 }
1865
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001866 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001867 /* use the IO handler that shows details */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001868 if (show_ocsp_detail) {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001869 ctx->transaction = from_transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001870 appctx->io_handler = cli_io_handler_show_cert_ocsp_detail;
1871 }
1872 else
1873 appctx->io_handler = cli_io_handler_show_cert_detail;
William Lallemandda8584c2020-05-14 10:14:37 +02001874 }
1875
1876 return 0;
1877
1878error:
1879 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1880 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
1881}
1882
1883/* release function of the `set ssl cert' command, free things and unlock the spinlock */
1884static void cli_release_commit_cert(struct appctx *appctx)
1885{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02001886 struct commit_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001887
1888 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulet9d56e242022-05-31 16:37:01 +02001889 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
1890 if (ctx->new_ckchs)
1891 ckch_store_free(ctx->new_ckchs);
1892 ha_free(&ctx->err);
William Lallemandda8584c2020-05-14 10:14:37 +02001893}
1894
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001895
1896/*
1897 * Rebuild a new instance 'new_inst' based on an old instance 'ckchi' and a
1898 * specific ckch_store.
1899 * Returns 0 in case of success, 1 otherwise.
1900 */
William Lallemande60c7d62022-03-30 11:26:15 +02001901int ckch_inst_rebuild(struct ckch_store *ckch_store, struct ckch_inst *ckchi,
1902 struct ckch_inst **new_inst, char **err)
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001903{
1904 int retval = 0;
1905 int errcode = 0;
1906 struct sni_ctx *sc0, *sc0s;
1907 char **sni_filter = NULL;
1908 int fcount = 0;
1909
1910 if (ckchi->crtlist_entry) {
1911 sni_filter = ckchi->crtlist_entry->filters;
1912 fcount = ckchi->crtlist_entry->fcount;
1913 }
1914
1915 if (ckchi->is_server_instance)
1916 errcode |= ckch_inst_new_load_srv_store(ckch_store->path, ckch_store, new_inst, err);
1917 else
1918 errcode |= ckch_inst_new_load_store(ckch_store->path, ckch_store, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, new_inst, err);
1919
1920 if (errcode & ERR_CODE)
1921 return 1;
1922
1923 /* if the previous ckchi was used as the default */
1924 if (ckchi->is_default)
1925 (*new_inst)->is_default = 1;
1926
1927 (*new_inst)->is_server_instance = ckchi->is_server_instance;
1928 (*new_inst)->server = ckchi->server;
1929 /* Create a new SSL_CTX and link it to the new instance. */
1930 if ((*new_inst)->is_server_instance) {
1931 retval = ssl_sock_prep_srv_ctx_and_inst(ckchi->server, (*new_inst)->ctx, (*new_inst));
1932 if (retval)
1933 return 1;
1934 }
1935
1936 /* create the link to the crtlist_entry */
1937 (*new_inst)->crtlist_entry = ckchi->crtlist_entry;
1938
1939 /* we need to initialize the SSL_CTX generated */
1940 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
1941 list_for_each_entry_safe(sc0, sc0s, &(*new_inst)->sni_ctx, by_ckch_inst) {
1942 if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
1943 errcode |= ssl_sock_prep_ctx_and_inst(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, *new_inst, err);
1944 if (errcode & ERR_CODE)
1945 return 1;
1946 }
1947 }
1948
1949 return 0;
1950}
1951
1952/*
1953 * Load all the new SNIs of a newly built ckch instance in the trees, or replace
1954 * a server's main ckch instance.
1955 */
1956static void __ssl_sock_load_new_ckch_instance(struct ckch_inst *ckchi)
1957{
1958 /* The bind_conf will be null on server ckch_instances. */
1959 if (ckchi->is_server_instance) {
1960 int i;
1961 /* a lock is needed here since we have to free the SSL cache */
1962 HA_RWLOCK_WRLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
1963 /* free the server current SSL_CTX */
1964 SSL_CTX_free(ckchi->server->ssl_ctx.ctx);
1965 /* Actual ssl context update */
1966 SSL_CTX_up_ref(ckchi->ctx);
1967 ckchi->server->ssl_ctx.ctx = ckchi->ctx;
1968 ckchi->server->ssl_ctx.inst = ckchi;
1969
1970 /* flush the session cache of the server */
1971 for (i = 0; i < global.nbthread; i++) {
William Lallemandce990332021-11-23 15:15:09 +01001972 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].sni);
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001973 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].ptr);
1974 }
1975 HA_RWLOCK_WRUNLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
1976
1977 } else {
1978 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
1979 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
1980 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
1981 }
1982}
1983
1984/*
1985 * Delete a ckch instance that was replaced after a CLI command.
1986 */
1987static void __ckch_inst_free_locked(struct ckch_inst *ckchi)
1988{
1989 if (ckchi->is_server_instance) {
1990 /* no lock for servers */
1991 ckch_inst_free(ckchi);
1992 } else {
1993 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
1994
1995 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
1996 ckch_inst_free(ckchi);
1997 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
1998 }
1999}
2000
William Lallemand3b5a3a62022-03-29 14:29:31 +02002001/* Replace a ckch_store in the ckch tree and insert the whole dependencies,
2002* then free the previous dependencies and store.
2003* Used in the case of a certificate update.
2004*
2005* Every dependencies must allocated before using this function.
2006*
2007* This function can't fail as it only update pointers, and does not alloc anything.
2008*
2009* /!\ This function must be used under the ckch lock. /!\
2010*
2011* - Insert every dependencies (SNI, crtlist_entry, ckch_inst, etc)
2012* - Delete the old ckch_store from the tree
2013* - Insert the new ckch_store
2014* - Free the old dependencies and the old ckch_store
2015*/
2016void ckch_store_replace(struct ckch_store *old_ckchs, struct ckch_store *new_ckchs)
2017{
2018 struct crtlist_entry *entry;
2019 struct ckch_inst *ckchi, *ckchis;
2020
2021 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
2022 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
2023 ebpt_delete(&entry->node);
2024 /* change the ptr and reinsert the node */
2025 entry->node.key = new_ckchs;
2026 ebpt_insert(&entry->crtlist->entries, &entry->node);
2027 }
2028 /* insert the new ckch_insts in the crtlist_entry */
2029 list_for_each_entry(ckchi, &new_ckchs->ckch_inst, by_ckchs) {
2030 if (ckchi->crtlist_entry)
2031 LIST_INSERT(&ckchi->crtlist_entry->ckch_inst, &ckchi->by_crtlist_entry);
2032 }
2033 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2034 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
2035 __ssl_sock_load_new_ckch_instance(ckchi);
2036 }
2037 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
2038 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
2039 __ckch_inst_free_locked(ckchi);
2040 }
2041
2042 ckch_store_free(old_ckchs);
2043 ebst_insert(&ckchs_tree, &new_ckchs->node);
2044}
2045
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002046
William Lallemandda8584c2020-05-14 10:14:37 +02002047/*
2048 * This function tries to create the new ckch_inst and their SNIs
William Lallemand30fcca12022-03-30 12:03:12 +02002049 *
2050 * /!\ don't forget to update __hlua_ckch_commit() if you changes things there. /!\
William Lallemandda8584c2020-05-14 10:14:37 +02002051 */
2052static int cli_io_handler_commit_cert(struct appctx *appctx)
2053{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002054 struct commit_cert_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002055 struct stconn *sc = appctx_sc(appctx);
William Lallemandda8584c2020-05-14 10:14:37 +02002056 int y = 0;
William Lallemandda8584c2020-05-14 10:14:37 +02002057 struct ckch_store *old_ckchs, *new_ckchs = NULL;
William Lallemand3b5a3a62022-03-29 14:29:31 +02002058 struct ckch_inst *ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002059
Willy Tarreau475e4632022-05-27 10:26:46 +02002060 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Christopher Faulet9d56e242022-05-31 16:37:01 +02002061 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02002062
2063 while (1) {
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002064 switch (ctx->state) {
2065 case CERT_ST_INIT:
William Lallemandda8584c2020-05-14 10:14:37 +02002066 /* This state just print the update message */
Christopher Faulet9d56e242022-05-31 16:37:01 +02002067 chunk_printf(&trash, "Committing %s", ckchs_transaction.path);
2068 if (applet_putchk(appctx, &trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02002069 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002070
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002071 ctx->state = CERT_ST_GEN;
William Lallemandda8584c2020-05-14 10:14:37 +02002072 /* fallthrough */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002073 case CERT_ST_GEN:
William Lallemandda8584c2020-05-14 10:14:37 +02002074 /*
2075 * This state generates the ckch instances with their
2076 * sni_ctxs and SSL_CTX.
2077 *
2078 * Since the SSL_CTX generation can be CPU consumer, we
2079 * yield every 10 instances.
2080 */
2081
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002082 old_ckchs = ctx->old_ckchs;
2083 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002084
William Lallemandda8584c2020-05-14 10:14:37 +02002085 /* get the next ckchi to regenerate */
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002086 ckchi = ctx->next_ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002087 /* we didn't start yet, set it to the first elem */
2088 if (ckchi == NULL)
2089 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
2090
2091 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
2092 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
2093 struct ckch_inst *new_inst;
William Lallemandda8584c2020-05-14 10:14:37 +02002094
Christopher Faulet9d56e242022-05-31 16:37:01 +02002095 /* save the next ckchi to compute in case of yield */
2096 ctx->next_ckchi = ckchi;
2097
William Lallemandda8584c2020-05-14 10:14:37 +02002098 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2099 if (y >= 10) {
Christopher Faulet9d56e242022-05-31 16:37:01 +02002100 applet_have_more_data(appctx); /* let's come back later */
William Lallemandda8584c2020-05-14 10:14:37 +02002101 goto yield;
2102 }
2103
Christopher Faulet9d56e242022-05-31 16:37:01 +02002104 /* display one dot per new instance */
2105 if (applet_putstr(appctx, ".") == -1)
2106 goto yield;
2107
2108 ctx->err = NULL;
2109 if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &ctx->err)) {
2110 ctx->state = CERT_ST_ERROR;
William Lallemandda8584c2020-05-14 10:14:37 +02002111 goto error;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002112 }
William Lallemandda8584c2020-05-14 10:14:37 +02002113
William Lallemandda8584c2020-05-14 10:14:37 +02002114 /* link the new ckch_inst to the duplicate */
Willy Tarreau2b718102021-04-21 07:32:39 +02002115 LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002116 y++;
2117 }
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002118 ctx->state = CERT_ST_INSERT;
William Lallemandda8584c2020-05-14 10:14:37 +02002119 /* fallthrough */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002120 case CERT_ST_INSERT:
William Lallemandda8584c2020-05-14 10:14:37 +02002121 /* The generation is finished, we can insert everything */
2122
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002123 old_ckchs = ctx->old_ckchs;
2124 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002125
William Lallemand3b5a3a62022-03-29 14:29:31 +02002126 /* insert everything and remove the previous objects */
2127 ckch_store_replace(old_ckchs, new_ckchs);
Christopher Faulet9d56e242022-05-31 16:37:01 +02002128 ctx->new_ckchs = ctx->old_ckchs = NULL;
2129 ctx->state = CERT_ST_SUCCESS;
2130 /* fallthrough */
2131 case CERT_ST_SUCCESS:
2132 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2133 goto yield;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002134 ctx->state = CERT_ST_FIN;
William Lallemandda8584c2020-05-14 10:14:37 +02002135 /* fallthrough */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002136 case CERT_ST_FIN:
William Lallemandda8584c2020-05-14 10:14:37 +02002137 /* we achieved the transaction, we can set everything to NULL */
William Lallemandda8584c2020-05-14 10:14:37 +02002138 ckchs_transaction.new_ckchs = NULL;
2139 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002140 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002141 goto end;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002142
2143 case CERT_ST_ERROR:
2144 error:
2145 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2146 if (applet_putchk(appctx, &trash) == -1)
2147 goto yield;
2148 ctx->state = CERT_ST_FIN;
2149 break;
William Lallemandda8584c2020-05-14 10:14:37 +02002150 }
2151 }
2152end:
William Lallemandda8584c2020-05-14 10:14:37 +02002153 /* success: call the release function and don't come back */
2154 return 1;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002155
William Lallemandda8584c2020-05-14 10:14:37 +02002156yield:
William Lallemandda8584c2020-05-14 10:14:37 +02002157 return 0; /* should come back */
William Lallemandda8584c2020-05-14 10:14:37 +02002158}
2159
2160/*
2161 * Parsing function of 'commit ssl cert'
2162 */
2163static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
2164{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002165 struct commit_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02002166 char *err = NULL;
2167
2168 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2169 return 1;
2170
2171 if (!*args[3])
2172 return cli_err(appctx, "'commit ssl cert expects a filename\n");
2173
2174 /* The operations on the CKCH architecture are locked so we can
2175 * manipulate ckch_store and ckch_inst */
2176 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2177 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
2178
2179 if (!ckchs_transaction.path) {
2180 memprintf(&err, "No ongoing transaction! !\n");
2181 goto error;
2182 }
2183
2184 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2185 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
2186 goto error;
2187 }
2188
William Lallemand5685ccf2020-09-16 16:12:25 +02002189 /* if a certificate is here, a private key must be here too */
2190 if (ckchs_transaction.new_ckchs->ckch->cert && !ckchs_transaction.new_ckchs->ckch->key) {
2191 memprintf(&err, "The transaction must contain at least a certificate and a private key!\n");
2192 goto error;
2193 }
William Lallemanda9419522020-06-24 16:26:41 +02002194
William Lallemand5685ccf2020-09-16 16:12:25 +02002195 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
2196 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
2197 goto error;
William Lallemandda8584c2020-05-14 10:14:37 +02002198 }
2199
2200 /* init the appctx structure */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002201 ctx->state = CERT_ST_INIT;
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002202 ctx->next_ckchi = NULL;
2203 ctx->new_ckchs = ckchs_transaction.new_ckchs;
2204 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002205
2206 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
2207 return 0;
2208
2209error:
2210
2211 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2212 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2213
2214 return cli_dynerr(appctx, err);
2215}
2216
2217
2218
2219
2220/*
2221 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
Willy Tarreau329f4b42022-05-04 20:05:55 +02002222 * It uses a set_cert_ctx context, and ckchs_transaction under a lock.
William Lallemandda8584c2020-05-14 10:14:37 +02002223 */
2224static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
2225{
2226 struct ckch_store *new_ckchs = NULL;
2227 struct ckch_store *old_ckchs = NULL;
2228 char *err = NULL;
2229 int i;
William Lallemandda8584c2020-05-14 10:14:37 +02002230 int errcode = 0;
2231 char *end;
William Lallemandff8bf982022-03-29 10:44:23 +02002232 struct cert_exts *cert_ext = &cert_exts[0]; /* default one, PEM */
William Lallemandda8584c2020-05-14 10:14:37 +02002233 struct cert_key_and_chain *ckch;
2234 struct buffer *buf;
2235
2236 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2237 return 1;
2238
William Lallemandda8584c2020-05-14 10:14:37 +02002239 if (!*args[3] || !payload)
2240 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
2241
2242 /* The operations on the CKCH architecture are locked so we can
2243 * manipulate ckch_store and ckch_inst */
2244 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2245 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
2246
William Lallemand5ba80d62021-05-04 16:17:27 +02002247 if ((buf = alloc_trash_chunk()) == NULL) {
2248 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2249 errcode |= ERR_ALERT | ERR_FATAL;
2250 goto end;
2251 }
William Lallemande5ff4ad2020-06-08 09:40:37 +02002252
William Lallemandda8584c2020-05-14 10:14:37 +02002253 if (!chunk_strcpy(buf, args[3])) {
2254 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2255 errcode |= ERR_ALERT | ERR_FATAL;
2256 goto end;
2257 }
2258
2259 /* check which type of file we want to update */
William Lallemandff8bf982022-03-29 10:44:23 +02002260 for (i = 0; cert_exts[i].ext != NULL; i++) {
William Lallemandda8584c2020-05-14 10:14:37 +02002261 end = strrchr(buf->area, '.');
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002262 if (end && *cert_exts[i].ext && (strcmp(end + 1, cert_exts[i].ext) == 0)) {
William Lallemandda8584c2020-05-14 10:14:37 +02002263 *end = '\0';
William Lallemand089c1382020-10-23 17:35:12 +02002264 buf->data = strlen(buf->area);
William Lallemandff8bf982022-03-29 10:44:23 +02002265 cert_ext = &cert_exts[i];
William Lallemandda8584c2020-05-14 10:14:37 +02002266 break;
2267 }
2268 }
2269
William Lallemandda8584c2020-05-14 10:14:37 +02002270 /* if there is an ongoing transaction */
2271 if (ckchs_transaction.path) {
William Lallemandda8584c2020-05-14 10:14:37 +02002272 /* if there is an ongoing transaction, check if this is the same file */
2273 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
William Lallemand089c1382020-10-23 17:35:12 +02002274 /* we didn't find the transaction, must try more cases below */
2275
2276 /* 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 }
2283
2284 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
2285 /* remove .crt of the error message */
2286 *(b_orig(buf) + b_data(buf) + strlen(".crt")) = '\0';
2287 b_sub(buf, strlen(".crt"));
2288
2289 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
2290 errcode |= ERR_ALERT | ERR_FATAL;
2291 goto end;
2292 }
2293 }
William Lallemandda8584c2020-05-14 10:14:37 +02002294 }
2295
Christopher Faulet24a20b92022-06-03 11:50:40 +02002296 old_ckchs = ckchs_transaction.new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002297
2298 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002299
William Lallemand95fefa12020-09-09 12:01:33 +02002300 /* lookup for the certificate in the tree */
Christopher Faulet24a20b92022-06-03 11:50:40 +02002301 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002302
Christopher Faulet24a20b92022-06-03 11:50:40 +02002303 if (!old_ckchs) {
William Lallemand089c1382020-10-23 17:35:12 +02002304 /* 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 +02002305 if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
William Lallemand089c1382020-10-23 17:35:12 +02002306 if (!chunk_strcat(buf, ".crt")) {
2307 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2308 errcode |= ERR_ALERT | ERR_FATAL;
2309 goto end;
2310 }
Christopher Faulet24a20b92022-06-03 11:50:40 +02002311 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002312 }
2313 }
William Lallemandda8584c2020-05-14 10:14:37 +02002314 }
2315
Christopher Faulet24a20b92022-06-03 11:50:40 +02002316 if (!old_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02002317 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
2318 err ? err : "");
2319 errcode |= ERR_ALERT | ERR_FATAL;
2320 goto end;
2321 }
2322
William Lallemandda8584c2020-05-14 10:14:37 +02002323 /* duplicate the ckch store */
2324 new_ckchs = ckchs_dup(old_ckchs);
2325 if (!new_ckchs) {
2326 memprintf(&err, "%sCannot allocate memory!\n",
2327 err ? err : "");
2328 errcode |= ERR_ALERT | ERR_FATAL;
2329 goto end;
2330 }
2331
William Lallemand95fefa12020-09-09 12:01:33 +02002332 ckch = new_ckchs->ckch;
William Lallemandda8584c2020-05-14 10:14:37 +02002333
2334 /* appply the change on the duplicate */
William Lallemandff8bf982022-03-29 10:44:23 +02002335 if (cert_ext->load(buf->area, payload, ckch, &err) != 0) {
William Lallemandda8584c2020-05-14 10:14:37 +02002336 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
2337 errcode |= ERR_ALERT | ERR_FATAL;
2338 goto end;
2339 }
2340
William Lallemandda8584c2020-05-14 10:14:37 +02002341 /* we succeed, we can save the ckchs in the transaction */
2342
2343 /* if there wasn't a transaction, update the old ckchs */
2344 if (!ckchs_transaction.old_ckchs) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002345 ckchs_transaction.old_ckchs = old_ckchs;
2346 ckchs_transaction.path = old_ckchs->path;
William Lallemandda8584c2020-05-14 10:14:37 +02002347 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
2348 } else {
2349 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
2350
2351 }
2352
2353 /* free the previous ckchs if there was a transaction */
2354 ckch_store_free(ckchs_transaction.new_ckchs);
2355
Christopher Faulet24a20b92022-06-03 11:50:40 +02002356 ckchs_transaction.new_ckchs = new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002357
2358
2359 /* creates the SNI ctxs later in the IO handler */
2360
2361end:
2362 free_trash_chunk(buf);
2363
2364 if (errcode & ERR_CODE) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002365 ckch_store_free(new_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002366 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2367 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2368 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002369 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2370 return cli_dynmsg(appctx, LOG_NOTICE, err);
2371 }
2372 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
2373}
2374
2375/* parsing function of 'abort ssl cert' */
2376static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
2377{
2378 char *err = NULL;
2379
2380 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2381 return 1;
2382
2383 if (!*args[3])
2384 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
2385
2386 /* The operations on the CKCH architecture are locked so we can
2387 * manipulate ckch_store and ckch_inst */
2388 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2389 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2390
2391 if (!ckchs_transaction.path) {
2392 memprintf(&err, "No ongoing transaction!\n");
2393 goto error;
2394 }
2395
2396 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2397 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
2398 goto error;
2399 }
2400
2401 /* Only free the ckchs there, because the SNI and instances were not generated yet */
2402 ckch_store_free(ckchs_transaction.new_ckchs);
2403 ckchs_transaction.new_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002404 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002405 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002406
2407 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2408
2409 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2410 return cli_dynmsg(appctx, LOG_NOTICE, err);
2411
2412error:
2413 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2414
2415 return cli_dynerr(appctx, err);
2416}
2417
2418/* parsing function of 'new ssl cert' */
2419static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
2420{
2421 struct ckch_store *store;
2422 char *err = NULL;
2423 char *path;
2424
2425 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2426 return 1;
2427
2428 if (!*args[3])
2429 return cli_err(appctx, "'new ssl cert' expects a filename\n");
2430
2431 path = args[3];
2432
2433 /* The operations on the CKCH architecture are locked so we can
2434 * manipulate ckch_store and ckch_inst */
2435 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2436 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
2437
2438 store = ckchs_lookup(path);
2439 if (store != NULL) {
2440 memprintf(&err, "Certificate '%s' already exists!\n", path);
2441 store = NULL; /* we don't want to free it */
2442 goto error;
2443 }
2444 /* we won't support multi-certificate bundle here */
William Lallemandbd8e6ed2020-09-16 16:08:08 +02002445 store = ckch_store_new(path);
William Lallemandda8584c2020-05-14 10:14:37 +02002446 if (!store) {
2447 memprintf(&err, "unable to allocate memory.\n");
2448 goto error;
2449 }
2450
2451 /* insert into the ckchs tree */
2452 ebst_insert(&ckchs_tree, &store->node);
2453 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
2454
2455 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2456 return cli_dynmsg(appctx, LOG_NOTICE, err);
2457error:
2458 free(store);
2459 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2460 return cli_dynerr(appctx, err);
2461}
2462
2463/* parsing function of 'del ssl cert' */
2464static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
2465{
2466 struct ckch_store *store;
2467 char *err = NULL;
2468 char *filename;
2469
2470 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2471 return 1;
2472
2473 if (!*args[3])
2474 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
2475
2476 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2477 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
2478
2479 filename = args[3];
2480
Christopher Faulet926fefc2022-05-31 18:04:25 +02002481 if (ckchs_transaction.path && strcmp(ckchs_transaction.path, filename) == 0) {
2482 memprintf(&err, "ongoing transaction for the certificate '%s'", filename);
2483 goto error;
2484 }
2485
William Lallemandda8584c2020-05-14 10:14:37 +02002486 store = ckchs_lookup(filename);
2487 if (store == NULL) {
2488 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
2489 goto error;
2490 }
2491 if (!LIST_ISEMPTY(&store->ckch_inst)) {
2492 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
2493 goto error;
2494 }
2495
2496 ebmb_delete(&store->node);
2497 ckch_store_free(store);
2498
2499 memprintf(&err, "Certificate '%s' deleted!\n", filename);
2500
2501 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2502 return cli_dynmsg(appctx, LOG_NOTICE, err);
2503
2504error:
2505 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
2506 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2507 return cli_dynerr(appctx, err);
2508}
2509
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002510
Remi Tricot-Le Breton9f40fe02021-03-16 16:21:27 +01002511
2512/* parsing function of 'new ssl ca-file' */
2513static int cli_parse_new_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2514{
2515 struct cafile_entry *cafile_entry;
2516 char *err = NULL;
2517 char *path;
2518
2519 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2520 return 1;
2521
2522 if (!*args[3])
2523 return cli_err(appctx, "'new ssl ca-file' expects a filename\n");
2524
2525 path = args[3];
2526
2527 /* The operations on the CKCH architecture are locked so we can
2528 * manipulate ckch_store and ckch_inst */
2529 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2530 return cli_err(appctx, "Can't create a CA file!\nOperations on certificates are currently locked!\n");
2531
2532 cafile_entry = ssl_store_get_cafile_entry(path, 0);
2533 if (cafile_entry) {
2534 memprintf(&err, "CA file '%s' already exists!\n", path);
2535 goto error;
2536 }
2537
2538 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CERT);
2539 if (!cafile_entry) {
2540 memprintf(&err, "%sCannot allocate memory!\n",
2541 err ? err : "");
2542 goto error;
2543 }
2544
2545 /* Add the newly created cafile_entry to the tree so that
2546 * any new ckch instance created from now can use it. */
2547 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
2548 goto error;
2549
2550 memprintf(&err, "New CA file created '%s'!\n", path);
2551
2552 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2553 return cli_dynmsg(appctx, LOG_NOTICE, err);
2554error:
2555 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2556 return cli_dynerr(appctx, err);
2557}
2558
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002559/*
2560 * Parsing function of `set ssl ca-file`
2561 */
2562static int cli_parse_set_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2563{
Christopher Faulet132c5952022-06-03 11:56:26 +02002564 struct cafile_entry *old_cafile_entry = NULL;
2565 struct cafile_entry *new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002566 char *err = NULL;
2567 int errcode = 0;
2568 struct buffer *buf;
2569
2570 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2571 return 1;
2572
2573 if (!*args[3] || !payload)
2574 return cli_err(appctx, "'set ssl ca-file expects a filename and CAs as a payload\n");
2575
2576 /* The operations on the CKCH architecture are locked so we can
2577 * manipulate ckch_store and ckch_inst */
2578 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2579 return cli_err(appctx, "Can't update the CA file!\nOperations on certificates are currently locked!\n");
2580
2581 if ((buf = alloc_trash_chunk()) == NULL) {
2582 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2583 errcode |= ERR_ALERT | ERR_FATAL;
2584 goto end;
2585 }
2586
2587 if (!chunk_strcpy(buf, args[3])) {
2588 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2589 errcode |= ERR_ALERT | ERR_FATAL;
2590 goto end;
2591 }
2592
Christopher Faulet132c5952022-06-03 11:56:26 +02002593 old_cafile_entry = NULL;
2594 new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002595
2596 /* if there is an ongoing transaction */
2597 if (cafile_transaction.path) {
2598 /* if there is an ongoing transaction, check if this is the same file */
2599 if (strcmp(cafile_transaction.path, buf->area) != 0) {
2600 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, buf->area);
2601 errcode |= ERR_ALERT | ERR_FATAL;
2602 goto end;
2603 }
Christopher Faulet132c5952022-06-03 11:56:26 +02002604 old_cafile_entry = cafile_transaction.old_cafile_entry;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002605 }
2606 else {
2607 /* lookup for the certificate in the tree */
Christopher Faulet132c5952022-06-03 11:56:26 +02002608 old_cafile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002609 }
2610
Christopher Faulet132c5952022-06-03 11:56:26 +02002611 if (!old_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002612 memprintf(&err, "%sCan't replace a CA file which is not referenced by the configuration!\n",
2613 err ? err : "");
2614 errcode |= ERR_ALERT | ERR_FATAL;
2615 goto end;
2616 }
2617
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002618 /* Create a new cafile_entry without adding it to the cafile tree. */
Christopher Faulet132c5952022-06-03 11:56:26 +02002619 new_cafile_entry = ssl_store_create_cafile_entry(old_cafile_entry->path, NULL, CAFILE_CERT);
2620 if (!new_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002621 memprintf(&err, "%sCannot allocate memory!\n",
2622 err ? err : "");
2623 errcode |= ERR_ALERT | ERR_FATAL;
2624 goto end;
2625 }
2626
2627 /* Fill the new entry with the new CAs. */
Christopher Faulet132c5952022-06-03 11:56:26 +02002628 if (ssl_store_load_ca_from_buf(new_cafile_entry, payload)) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002629 memprintf(&err, "%sInvalid payload\n", err ? err : "");
2630 errcode |= ERR_ALERT | ERR_FATAL;
2631 goto end;
2632 }
2633
2634 /* we succeed, we can save the ca in the transaction */
2635
2636 /* if there wasn't a transaction, update the old CA */
2637 if (!cafile_transaction.old_cafile_entry) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002638 cafile_transaction.old_cafile_entry = old_cafile_entry;
2639 cafile_transaction.path = old_cafile_entry->path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002640 err = memprintf(&err, "transaction created for CA %s!\n", cafile_transaction.path);
2641 } else {
2642 err = memprintf(&err, "transaction updated for CA %s!\n", cafile_transaction.path);
2643 }
2644
2645 /* free the previous CA if there was a transaction */
2646 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2647
Christopher Faulet132c5952022-06-03 11:56:26 +02002648 cafile_transaction.new_cafile_entry = new_cafile_entry;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002649
2650 /* creates the SNI ctxs later in the IO handler */
2651
2652end:
2653 free_trash_chunk(buf);
2654
2655 if (errcode & ERR_CODE) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002656 ssl_store_delete_cafile_entry(new_cafile_entry);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002657 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2658 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2659 } else {
2660
2661 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2662 return cli_dynmsg(appctx, LOG_NOTICE, err);
2663 }
2664}
2665
2666
2667/*
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002668 * Parsing function of 'commit ssl ca-file'.
2669 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl crl-file".
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002670 */
2671static int cli_parse_commit_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2672{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002673 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002674 char *err = NULL;
2675
2676 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2677 return 1;
2678
2679 if (!*args[3])
2680 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
2681
2682 /* The operations on the CKCH architecture are locked so we can
2683 * manipulate ckch_store and ckch_inst */
2684 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2685 return cli_err(appctx, "Can't commit the CA file!\nOperations on certificates are currently locked!\n");
2686
2687 if (!cafile_transaction.path) {
2688 memprintf(&err, "No ongoing transaction! !\n");
2689 goto error;
2690 }
2691
2692 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2693 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, args[3]);
2694 goto error;
2695 }
2696 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002697 ctx->state = CACRL_ST_INIT;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002698 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002699 ctx->old_entry = cafile_transaction.old_cafile_entry;
2700 ctx->new_entry = cafile_transaction.new_cafile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002701 ctx->cafile_type = CAFILE_CERT;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002702
2703 return 0;
2704
2705error:
2706
2707 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2708 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2709
2710 return cli_dynerr(appctx, err);
2711}
2712
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002713/*
2714 * This function tries to create new ckch instances and their SNIs using a newly
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002715 * set certificate authority (CA file) or a newly set Certificate Revocation
2716 * List (CRL), depending on the command being called.
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002717 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002718static int cli_io_handler_commit_cafile_crlfile(struct appctx *appctx)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002719{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002720 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002721 struct stconn *sc = appctx_sc(appctx);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002722 int y = 0;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002723 struct cafile_entry *old_cafile_entry = ctx->old_entry;
2724 struct cafile_entry *new_cafile_entry = ctx->new_entry;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002725 struct ckch_inst_link *ckchi_link;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002726 char *path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002727
Willy Tarreau475e4632022-05-27 10:26:46 +02002728 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002729 goto end;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002730
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002731 /* The ctx was already validated by the ca-file/crl-file parsing
2732 * function. Entries can only be NULL in CACRL_ST_SUCCESS or
2733 * CACRL_ST_FIN states
2734 */
2735 switch (ctx->cafile_type) {
2736 case CAFILE_CERT:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002737 path = cafile_transaction.path;
2738 break;
2739 case CAFILE_CRL:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002740 path = crlfile_transaction.path;
2741 break;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002742 default:
Willy Tarreaud543ae02022-06-22 05:40:25 +02002743 path = NULL;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002744 goto error;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002745 }
2746
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002747 while (1) {
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002748 switch (ctx->state) {
2749 case CACRL_ST_INIT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002750 /* This state just print the update message */
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002751 chunk_printf(&trash, "Committing %s", path);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002752 if (applet_putchk(appctx, &trash) == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002753 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002754
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002755 ctx->state = CACRL_ST_GEN;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002756 /* fallthrough */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002757 case CACRL_ST_GEN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002758 /*
2759 * This state generates the ckch instances with their
2760 * sni_ctxs and SSL_CTX.
2761 *
2762 * Since the SSL_CTX generation can be CPU consumer, we
2763 * yield every 10 instances.
2764 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002765
2766 /* get the next ckchi to regenerate */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002767 ckchi_link = ctx->next_ckchi_link;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002768
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002769 /* we didn't start yet, set it to the first elem */
2770 if (ckchi_link == NULL) {
2771 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2772 /* Add the newly created cafile_entry to the tree so that
2773 * any new ckch instance created from now can use it. */
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002774 if (ssl_store_add_uncommitted_cafile_entry(new_cafile_entry)) {
2775 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002776 goto error;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002777 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002778 }
2779
2780 list_for_each_entry_from(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002781 struct ckch_inst *new_inst;
2782
2783 /* save the next ckchi to compute */
2784 ctx->next_ckchi_link = ckchi_link;
2785
2786 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2787 if (y >= 10) {
2788 applet_have_more_data(appctx); /* let's come back later */
2789 goto yield;
2790 }
2791
2792 /* display one dot per new instance */
2793 if (applet_putstr(appctx, ".") == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002794 goto yield;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002795
2796 /* Rebuild a new ckch instance that uses the same ckch_store
2797 * than a reference ckchi instance but will use a new CA file. */
2798 ctx->err = NULL;
2799 if (ckch_inst_rebuild(ckchi_link->ckch_inst->ckch_store, ckchi_link->ckch_inst, &new_inst, &ctx->err)) {
2800 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002801 goto error;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002802 }
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002803
2804 y++;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002805 }
2806
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002807 ctx->state = CACRL_ST_INSERT;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002808 /* fallthrough */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002809 case CACRL_ST_INSERT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002810 /* The generation is finished, we can insert everything */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002811
2812 /* insert the new ckch_insts in the crtlist_entry */
2813 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2814 if (ckchi_link->ckch_inst->crtlist_entry)
2815 LIST_INSERT(&ckchi_link->ckch_inst->crtlist_entry->ckch_inst,
2816 &ckchi_link->ckch_inst->by_crtlist_entry);
2817 }
2818
2819 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2820 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2821 __ssl_sock_load_new_ckch_instance(ckchi_link->ckch_inst);
2822 }
2823
2824 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
2825 list_for_each_entry(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
2826 __ckch_inst_free_locked(ckchi_link->ckch_inst);
2827 }
2828
2829
2830 /* Remove the old cafile entry from the tree */
2831 ebmb_delete(&old_cafile_entry->node);
2832 ssl_store_delete_cafile_entry(old_cafile_entry);
2833
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002834 ctx->old_entry = ctx->new_entry = NULL;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002835 ctx->state = CACRL_ST_SUCCESS;
2836 /* fallthrough */
2837 case CACRL_ST_SUCCESS:
2838 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2839 goto yield;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002840 ctx->state = CACRL_ST_FIN;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002841 /* fallthrough */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002842 case CACRL_ST_FIN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002843 /* we achieved the transaction, we can set everything to NULL */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002844 switch (ctx->cafile_type) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002845 case CAFILE_CERT:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002846 cafile_transaction.old_cafile_entry = NULL;
2847 cafile_transaction.new_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002848 cafile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002849 break;
2850 case CAFILE_CRL:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002851 crlfile_transaction.old_crlfile_entry = NULL;
2852 crlfile_transaction.new_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002853 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002854 break;
2855 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002856 goto end;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002857
2858 case CACRL_ST_ERROR:
2859 error:
2860 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2861 if (applet_putchk(appctx, &trash) == -1)
2862 goto yield;
2863 ctx->state = CACRL_ST_FIN;
2864 break;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002865 }
2866 }
2867end:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002868 /* success: call the release function and don't come back */
2869 return 1;
2870yield:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002871 return 0; /* should come back */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002872}
2873
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002874
2875/* parsing function of 'abort ssl ca-file' */
2876static int cli_parse_abort_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2877{
2878 char *err = NULL;
2879
2880 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2881 return 1;
2882
2883 if (!*args[3])
2884 return cli_err(appctx, "'abort ssl ca-file' expects a filename\n");
2885
2886 /* The operations on the CKCH architecture are locked so we can
2887 * manipulate ckch_store and ckch_inst */
2888 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2889 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2890
2891 if (!cafile_transaction.path) {
2892 memprintf(&err, "No ongoing transaction!\n");
2893 goto error;
2894 }
2895
2896 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2897 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", cafile_transaction.path, args[3]);
2898 goto error;
2899 }
2900
2901 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
2902 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2903 cafile_transaction.new_cafile_entry = NULL;
2904 cafile_transaction.old_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002905 cafile_transaction.path = NULL;
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002906
2907 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2908
2909 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2910 return cli_dynmsg(appctx, LOG_NOTICE, err);
2911
2912error:
2913 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2914
2915 return cli_dynerr(appctx, err);
2916}
2917
Willy Tarreau821c3b02022-05-04 15:47:39 +02002918/* release function of the `commit ssl ca-file' command, free things and unlock the spinlock.
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002919 * It uses a commit_cacrlfile_ctx context.
Willy Tarreau821c3b02022-05-04 15:47:39 +02002920 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002921static void cli_release_commit_cafile(struct appctx *appctx)
2922{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002923 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002924 struct cafile_entry *new_cafile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002925
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002926 /* Remove the uncommitted cafile_entry from the tree. */
2927 if (new_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002928 ebmb_delete(&new_cafile_entry->node);
2929 ssl_store_delete_cafile_entry(new_cafile_entry);
2930 }
2931 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002932 ha_free(&ctx->err);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002933}
2934
2935
Willy Tarreau821c3b02022-05-04 15:47:39 +02002936/* IO handler of details "show ssl ca-file <filename[:index]>".
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02002937 * It uses a show_cafile_ctx context, and the global
2938 * cafile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02002939 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002940static int cli_io_handler_show_cafile_detail(struct appctx *appctx)
2941{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02002942 struct show_cafile_ctx *ctx = appctx->svcctx;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02002943 struct cafile_entry *cafile_entry = ctx->cur_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002944 struct buffer *out = alloc_trash_chunk();
William Lallemand03a32e52022-04-26 18:17:15 +02002945 int i = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002946 X509 *cert;
2947 STACK_OF(X509_OBJECT) *objs;
2948 int retval = 0;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02002949 int ca_index = ctx->ca_index;
2950 int show_all = ctx->show_all;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002951
2952 if (!out)
2953 goto end_no_putchk;
2954
2955 chunk_appendf(out, "Filename: ");
2956 if (cafile_entry == cafile_transaction.new_cafile_entry)
2957 chunk_appendf(out, "*");
2958 chunk_appendf(out, "%s\n", cafile_entry->path);
2959
2960 chunk_appendf(out, "Status: ");
2961 if (!cafile_entry->ca_store)
2962 chunk_appendf(out, "Empty\n");
2963 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
2964 chunk_appendf(out, "Unused\n");
2965 else
2966 chunk_appendf(out, "Used\n");
2967
2968 if (!cafile_entry->ca_store)
2969 goto end;
2970
2971 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
William Lallemand03a32e52022-04-26 18:17:15 +02002972 for (i = ca_index; i < sk_X509_OBJECT_num(objs); i++) {
2973
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002974 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
2975 if (!cert)
2976 continue;
2977
William Lallemand03a32e52022-04-26 18:17:15 +02002978 /* file starts at line 1 */
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02002979 chunk_appendf(out, " \nCertificate #%d:\n", i+1);
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002980 retval = show_cert_detail(cert, NULL, out);
2981 if (retval < 0)
2982 goto end_no_putchk;
William Lallemand03a32e52022-04-26 18:17:15 +02002983 else if (retval)
2984 goto yield;
2985
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002986 if (applet_putchk(appctx, out) == -1)
William Lallemand03a32e52022-04-26 18:17:15 +02002987 goto yield;
William Lallemand03a32e52022-04-26 18:17:15 +02002988
2989 if (!show_all) /* only need to dump one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002990 goto end;
2991 }
2992
2993end:
William Lallemand03a32e52022-04-26 18:17:15 +02002994 free_trash_chunk(out);
2995 return 1; /* end, don't come back */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002996
2997end_no_putchk:
2998 free_trash_chunk(out);
2999 return 1;
3000yield:
William Lallemand03a32e52022-04-26 18:17:15 +02003001 /* save the current state */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003002 ctx->ca_index = i;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003003 free_trash_chunk(out);
3004 return 0; /* should come back */
3005}
3006
3007
Willy Tarreau06305792022-05-04 15:57:30 +02003008/* parsing function for 'show ssl ca-file [cafile[:index]]'.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003009 * It prepares a show_cafile_ctx context, and checks the global
3010 * cafile_transaction under the ckch_lock (read only).
Willy Tarreau06305792022-05-04 15:57:30 +02003011 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003012static int cli_parse_show_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3013{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003014 struct show_cafile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003015 struct cafile_entry *cafile_entry;
William Lallemand03a32e52022-04-26 18:17:15 +02003016 int ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003017 char *colons;
3018 char *err = NULL;
3019
3020 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3021 return cli_err(appctx, "Can't allocate memory!\n");
3022
3023 /* The operations on the CKCH architecture are locked so we can
3024 * manipulate ckch_store and ckch_inst */
3025 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3026 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3027
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003028 ctx->show_all = 1; /* show all certificates */
3029 ctx->ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003030 /* check if there is a certificate to lookup */
3031 if (*args[3]) {
3032
3033 /* Look for an optional CA index after the CA file name */
3034 colons = strchr(args[3], ':');
3035 if (colons) {
3036 char *endptr;
3037
3038 ca_index = strtol(colons + 1, &endptr, 10);
3039 /* Indexes start at 1 */
3040 if (colons + 1 == endptr || *endptr != '\0' || ca_index <= 0) {
3041 memprintf(&err, "wrong CA index after colons in '%s'!", args[3]);
3042 goto error;
3043 }
3044 *colons = '\0';
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003045 ctx->ca_index = ca_index - 1; /* we start counting at 0 in the ca_store, but at 1 on the CLI */
3046 ctx->show_all = 0; /* show only one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003047 }
3048
3049 if (*args[3] == '*') {
3050 if (!cafile_transaction.new_cafile_entry)
3051 goto error;
3052
3053 cafile_entry = cafile_transaction.new_cafile_entry;
3054
3055 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3056 goto error;
3057
3058 } else {
3059 /* Get the "original" cafile_entry and not the
3060 * uncommitted one if it exists. */
3061 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CERT)
3062 goto error;
3063 }
3064
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003065 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003066 /* use the IO handler that shows details */
3067 appctx->io_handler = cli_io_handler_show_cafile_detail;
3068 }
3069
3070 return 0;
3071
3072error:
3073 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3074 if (err)
3075 return cli_dynerr(appctx, err);
3076 return cli_err(appctx, "Can't display the CA file : Not found!\n");
3077}
3078
3079
3080/* release function of the 'show ssl ca-file' command */
3081static void cli_release_show_cafile(struct appctx *appctx)
3082{
3083 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3084}
3085
3086
3087/* This function returns the number of certificates in a cafile_entry. */
3088static int get_certificate_count(struct cafile_entry *cafile_entry)
3089{
3090 int cert_count = 0;
3091 STACK_OF(X509_OBJECT) *objs;
3092
3093 if (cafile_entry && cafile_entry->ca_store) {
3094 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3095 if (objs)
3096 cert_count = sk_X509_OBJECT_num(objs);
3097 }
3098 return cert_count;
3099}
3100
3101/* IO handler of "show ssl ca-file". The command taking a specific CA file name
Willy Tarreau821c3b02022-05-04 15:47:39 +02003102 * is managed in cli_io_handler_show_cafile_detail.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003103 * It uses a show_cafile_ctx and the global cafile_transaction.new_cafile_entry
3104 * in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003105 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003106static int cli_io_handler_show_cafile(struct appctx *appctx)
3107{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003108 struct show_cafile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003109 struct buffer *trash = alloc_trash_chunk();
3110 struct ebmb_node *node;
Christopher Faulet677cb4f2022-06-03 16:25:35 +02003111 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003112
3113 if (trash == NULL)
3114 return 1;
3115
Christopher Faulet5a2154b2022-06-03 10:42:48 +02003116 if (!ctx->old_cafile_entry && cafile_transaction.old_cafile_entry) {
3117 chunk_appendf(trash, "# transaction\n");
3118 chunk_appendf(trash, "*%s", cafile_transaction.old_cafile_entry->path);
3119 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_transaction.new_cafile_entry));
3120 if (applet_putchk(appctx, trash) == -1)
3121 goto yield;
3122 ctx->old_cafile_entry = cafile_transaction.new_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003123 }
3124
3125 /* First time in this io_handler. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003126 if (!ctx->cur_cafile_entry) {
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003127 chunk_appendf(trash, "# filename\n");
3128 node = ebmb_first(&cafile_tree);
3129 } else {
3130 /* We yielded during a previous call. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003131 node = &ctx->cur_cafile_entry->node;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003132 }
3133
3134 while (node) {
3135 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3136 if (cafile_entry->type == CAFILE_CERT) {
3137 chunk_appendf(trash, "%s", cafile_entry->path);
3138
3139 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_entry));
3140 }
3141
3142 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003143 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003144 goto yield;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003145 }
3146
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003147 ctx->cur_cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003148 free_trash_chunk(trash);
3149 return 1;
3150yield:
3151
3152 free_trash_chunk(trash);
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003153 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003154 return 0; /* should come back */
3155}
3156
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003157/* parsing function of 'del ssl ca-file' */
3158static int cli_parse_del_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3159{
3160 struct cafile_entry *cafile_entry;
3161 char *err = NULL;
3162 char *filename;
3163
3164 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3165 return 1;
3166
3167 if (!*args[3])
3168 return cli_err(appctx, "'del ssl ca-file' expects a CA file name\n");
3169
3170 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3171 return cli_err(appctx, "Can't delete the CA file!\nOperations on certificates are currently locked!\n");
3172
3173 filename = args[3];
3174
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003175 if (cafile_transaction.path && strcmp(cafile_transaction.path, filename) == 0) {
3176 memprintf(&err, "ongoing transaction for the CA file '%s'", filename);
3177 goto error;
3178 }
3179
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003180 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3181 if (!cafile_entry) {
3182 memprintf(&err, "CA file '%s' doesn't exist!\n", filename);
3183 goto error;
3184 }
3185
3186 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3187 memprintf(&err, "CA file '%s' in use, can't be deleted!\n", filename);
3188 goto error;
3189 }
3190
3191 /* Remove the cafile_entry from the tree */
3192 ebmb_delete(&cafile_entry->node);
3193 ssl_store_delete_cafile_entry(cafile_entry);
3194
3195 memprintf(&err, "CA file '%s' deleted!\n", filename);
3196
3197 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3198 return cli_dynmsg(appctx, LOG_NOTICE, err);
3199
3200error:
3201 memprintf(&err, "Can't remove the CA file: %s\n", err ? err : "");
3202 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3203 return cli_dynerr(appctx, err);
3204}
3205
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003206/* parsing function of 'new ssl crl-file' */
3207static int cli_parse_new_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3208{
3209 struct cafile_entry *cafile_entry;
3210 char *err = NULL;
3211 char *path;
3212
3213 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3214 return 1;
3215
3216 if (!*args[3])
3217 return cli_err(appctx, "'new ssl crl-file' expects a filename\n");
3218
3219 path = args[3];
3220
3221 /* The operations on the CKCH architecture are locked so we can
3222 * manipulate ckch_store and ckch_inst */
3223 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003224 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 +02003225
3226 cafile_entry = ssl_store_get_cafile_entry(path, 0);
3227 if (cafile_entry) {
3228 memprintf(&err, "CRL file '%s' already exists!\n", path);
3229 goto error;
3230 }
3231
3232 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CRL);
3233 if (!cafile_entry) {
3234 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3235 goto error;
3236 }
3237
3238 /* Add the newly created cafile_entry to the tree so that
3239 * any new ckch instance created from now can use it. */
3240 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
3241 goto error;
3242
3243 memprintf(&err, "New CRL file created '%s'!\n", path);
3244
3245 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3246 return cli_dynmsg(appctx, LOG_NOTICE, err);
3247error:
3248 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3249 return cli_dynerr(appctx, err);
3250}
3251
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003252/* Parsing function of `set ssl crl-file` */
3253static int cli_parse_set_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3254{
Christopher Faulet1f90f332022-06-03 16:34:30 +02003255 struct cafile_entry *old_crlfile_entry = NULL;
3256 struct cafile_entry *new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003257 char *err = NULL;
3258 int errcode = 0;
3259 struct buffer *buf;
3260
3261 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3262 return 1;
3263
3264 if (!*args[3] || !payload)
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003265 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 +02003266
3267 /* The operations on the CKCH architecture are locked so we can
3268 * manipulate ckch_store and ckch_inst */
3269 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3270 return cli_err(appctx, "Can't update the CRL file!\nOperations on certificates are currently locked!\n");
3271
3272 if ((buf = alloc_trash_chunk()) == NULL) {
3273 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3274 errcode |= ERR_ALERT | ERR_FATAL;
3275 goto end;
3276 }
3277
3278 if (!chunk_strcpy(buf, args[3])) {
3279 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3280 errcode |= ERR_ALERT | ERR_FATAL;
3281 goto end;
3282 }
3283
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003284 old_crlfile_entry = NULL;
3285 new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003286
3287 /* if there is an ongoing transaction */
3288 if (crlfile_transaction.path) {
3289 /* if there is an ongoing transaction, check if this is the same file */
3290 if (strcmp(crlfile_transaction.path, buf->area) != 0) {
3291 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, buf->area);
3292 errcode |= ERR_ALERT | ERR_FATAL;
3293 goto end;
3294 }
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003295 old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003296 }
3297 else {
3298 /* lookup for the certificate in the tree */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003299 old_crlfile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003300 }
3301
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003302 if (!old_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003303 memprintf(&err, "%sCan't replace a CRL file which is not referenced by the configuration!\n",
3304 err ? err : "");
3305 errcode |= ERR_ALERT | ERR_FATAL;
3306 goto end;
3307 }
3308
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003309 /* Create a new cafile_entry without adding it to the cafile tree. */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003310 new_crlfile_entry = ssl_store_create_cafile_entry(old_crlfile_entry->path, NULL, CAFILE_CRL);
3311 if (!new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003312 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3313 errcode |= ERR_ALERT | ERR_FATAL;
3314 goto end;
3315 }
3316
3317 /* Fill the new entry with the new CRL. */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003318 if (ssl_store_load_ca_from_buf(new_crlfile_entry, payload)) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003319 memprintf(&err, "%sInvalid payload\n", err ? err : "");
3320 errcode |= ERR_ALERT | ERR_FATAL;
3321 goto end;
3322 }
3323
3324 /* we succeed, we can save the crl in the transaction */
3325
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003326 /* if there wasn't a transaction, update the old CRL */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003327 if (!crlfile_transaction.old_crlfile_entry) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003328 crlfile_transaction.old_crlfile_entry = old_crlfile_entry;
3329 crlfile_transaction.path = old_crlfile_entry->path;
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003330 err = memprintf(&err, "transaction created for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003331 } else {
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003332 err = memprintf(&err, "transaction updated for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003333 }
3334
3335 /* free the previous CRL file if there was a transaction */
3336 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3337
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003338 crlfile_transaction.new_crlfile_entry = new_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003339
3340 /* creates the SNI ctxs later in the IO handler */
3341
3342end:
3343 free_trash_chunk(buf);
3344
3345 if (errcode & ERR_CODE) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003346 ssl_store_delete_cafile_entry(new_crlfile_entry);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003347 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3348 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
3349 } else {
3350
3351 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3352 return cli_dynmsg(appctx, LOG_NOTICE, err);
3353 }
3354}
3355
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003356/* Parsing function of 'commit ssl crl-file'.
3357 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl ca-file".
3358 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003359static int cli_parse_commit_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3360{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003361 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003362 char *err = NULL;
3363
3364 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3365 return 1;
3366
3367 if (!*args[3])
3368 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
3369
3370 /* The operations on the CKCH architecture are locked so we can
3371 * manipulate ckch_store and ckch_inst */
3372 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3373 return cli_err(appctx, "Can't commit the CRL file!\nOperations on certificates are currently locked!\n");
3374
3375 if (!crlfile_transaction.path) {
3376 memprintf(&err, "No ongoing transaction! !\n");
3377 goto error;
3378 }
3379
3380 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3381 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, args[3]);
3382 goto error;
3383 }
3384 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02003385 ctx->state = CACRL_ST_INIT;
Christopher Fauletf814c4a2022-06-03 11:32:05 +02003386 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003387 ctx->old_entry = crlfile_transaction.old_crlfile_entry;
3388 ctx->new_entry = crlfile_transaction.new_crlfile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003389 ctx->cafile_type = CAFILE_CRL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003390
3391 return 0;
3392
3393error:
3394
3395 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3396 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
3397
3398 return cli_dynerr(appctx, err);
3399}
3400
3401
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003402/* release function of the `commit ssl crl-file' command, free things and unlock the spinlock.
3403 * it uses a commit_cacrlfile_ctx that's the same as for "commit ssl ca-file".
3404 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003405static void cli_release_commit_crlfile(struct appctx *appctx)
3406{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003407 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003408 struct cafile_entry *new_crlfile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003409
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003410 /* Remove the uncommitted cafile_entry from the tree. */
3411 if (new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003412 ebmb_delete(&new_crlfile_entry->node);
3413 ssl_store_delete_cafile_entry(new_crlfile_entry);
3414 }
3415 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003416 ha_free(&ctx->err);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003417}
3418
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003419/* parsing function of 'del ssl crl-file' */
3420static int cli_parse_del_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3421{
3422 struct cafile_entry *cafile_entry;
3423 char *err = NULL;
3424 char *filename;
3425
3426 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3427 return 1;
3428
3429 if (!*args[3])
3430 return cli_err(appctx, "'del ssl crl-file' expects a CRL file name\n");
3431
3432 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3433 return cli_err(appctx, "Can't delete the CRL file!\nOperations on certificates are currently locked!\n");
3434
3435 filename = args[3];
3436
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003437 if (crlfile_transaction.path && strcmp(crlfile_transaction.path, filename) == 0) {
3438 memprintf(&err, "ongoing transaction for the CRL file '%s'", filename);
3439 goto error;
3440 }
3441
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003442 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3443 if (!cafile_entry) {
3444 memprintf(&err, "CRL file '%s' doesn't exist!\n", filename);
3445 goto error;
3446 }
3447 if (cafile_entry->type != CAFILE_CRL) {
3448 memprintf(&err, "'del ssl crl-file' does not work on CA files!\n");
3449 goto error;
3450 }
3451
3452 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3453 memprintf(&err, "CRL file '%s' in use, can't be deleted!\n", filename);
3454 goto error;
3455 }
3456
3457 /* Remove the cafile_entry from the tree */
3458 ebmb_delete(&cafile_entry->node);
3459 ssl_store_delete_cafile_entry(cafile_entry);
3460
3461 memprintf(&err, "CRL file '%s' deleted!\n", filename);
3462
3463 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3464 return cli_dynmsg(appctx, LOG_NOTICE, err);
3465
3466error:
3467 memprintf(&err, "Can't remove the CRL file: %s\n", err ? err : "");
3468 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3469 return cli_dynerr(appctx, err);
3470}
3471
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003472/* parsing function of 'abort ssl crl-file' */
3473static int cli_parse_abort_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3474{
3475 char *err = NULL;
3476
3477 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3478 return 1;
3479
3480 if (!*args[3])
3481 return cli_err(appctx, "'abort ssl crl-file' expects a filename\n");
3482
3483 /* The operations on the CKCH architecture are locked so we can
3484 * manipulate ckch_store and ckch_inst */
3485 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3486 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
3487
3488 if (!crlfile_transaction.path) {
3489 memprintf(&err, "No ongoing transaction!\n");
3490 goto error;
3491 }
3492
3493 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3494 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", crlfile_transaction.path, args[3]);
3495 goto error;
3496 }
3497
3498 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3499 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3500 crlfile_transaction.new_crlfile_entry = NULL;
3501 crlfile_transaction.old_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02003502 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003503
3504 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3505
3506 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3507 return cli_dynmsg(appctx, LOG_NOTICE, err);
3508
3509error:
3510 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3511
3512 return cli_dynerr(appctx, err);
3513}
3514
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003515
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003516/*
3517 * Display a Certificate Resignation List's information.
3518 * The information displayed is inspired by the output of 'openssl crl -in
3519 * crl.pem -text'.
3520 * Returns 0 in case of success.
3521 */
3522static int show_crl_detail(X509_CRL *crl, struct buffer *out)
3523{
3524 BIO *bio = NULL;
3525 struct buffer *tmp = alloc_trash_chunk();
3526 long version;
3527 X509_NAME *issuer;
3528 int write = -1;
3529 STACK_OF(X509_REVOKED) *rev = NULL;
3530 X509_REVOKED *rev_entry = NULL;
3531 int i;
3532
3533 if (!tmp)
3534 return -1;
3535
3536 if ((bio = BIO_new(BIO_s_mem())) == NULL)
3537 goto end;
3538
3539 /* Version (as displayed by 'openssl crl') */
3540 version = X509_CRL_get_version(crl);
3541 chunk_appendf(out, "Version %ld\n", version + 1);
3542
3543 /* Signature Algorithm */
3544 chunk_appendf(out, "Signature Algorithm: %s\n", OBJ_nid2ln(X509_CRL_get_signature_nid(crl)));
3545
3546 /* Issuer */
3547 chunk_appendf(out, "Issuer: ");
3548 if ((issuer = X509_CRL_get_issuer(crl)) == NULL)
3549 goto end;
3550 if ((ssl_sock_get_dn_oneline(issuer, tmp)) == -1)
3551 goto end;
3552 *(tmp->area + tmp->data) = '\0';
3553 chunk_appendf(out, "%s\n", tmp->area);
3554
3555 /* Last Update */
3556 chunk_appendf(out, "Last Update: ");
3557 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003558 if (BIO_reset(bio) == -1)
3559 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003560 if (ASN1_TIME_print(bio, X509_CRL_get0_lastUpdate(crl)) == 0)
3561 goto end;
3562 write = BIO_read(bio, tmp->area, tmp->size-1);
3563 tmp->area[write] = '\0';
3564 chunk_appendf(out, "%s\n", tmp->area);
3565
3566
3567 /* Next Update */
3568 chunk_appendf(out, "Next Update: ");
3569 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003570 if (BIO_reset(bio) == -1)
3571 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003572 if (ASN1_TIME_print(bio, X509_CRL_get0_nextUpdate(crl)) == 0)
3573 goto end;
3574 write = BIO_read(bio, tmp->area, tmp->size-1);
3575 tmp->area[write] = '\0';
3576 chunk_appendf(out, "%s\n", tmp->area);
3577
3578
3579 /* Revoked Certificates */
3580 rev = X509_CRL_get_REVOKED(crl);
3581 if (sk_X509_REVOKED_num(rev) > 0)
3582 chunk_appendf(out, "Revoked Certificates:\n");
3583 else
3584 chunk_appendf(out, "No Revoked Certificates.\n");
3585
3586 for (i = 0; i < sk_X509_REVOKED_num(rev); i++) {
3587 rev_entry = sk_X509_REVOKED_value(rev, i);
3588
3589 /* Serial Number and Revocation Date */
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003590 if (BIO_reset(bio) == -1)
3591 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003592 BIO_printf(bio , " Serial Number: ");
Remi Tricot-Le Breton18c7d832021-05-17 18:38:34 +02003593 i2a_ASN1_INTEGER(bio, (ASN1_INTEGER*)X509_REVOKED_get0_serialNumber(rev_entry));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003594 BIO_printf(bio, "\n Revocation Date: ");
Remi Tricot-Le Bretona6b27842021-05-18 10:06:00 +02003595 if (ASN1_TIME_print(bio, X509_REVOKED_get0_revocationDate(rev_entry)) == 0)
3596 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003597 BIO_printf(bio, "\n");
3598
3599 write = BIO_read(bio, tmp->area, tmp->size-1);
3600 tmp->area[write] = '\0';
3601 chunk_appendf(out, "%s", tmp->area);
3602 }
3603
3604end:
3605 free_trash_chunk(tmp);
3606 if (bio)
3607 BIO_free(bio);
3608
3609 return 0;
3610}
3611
Willy Tarreau821c3b02022-05-04 15:47:39 +02003612/* IO handler of details "show ssl crl-file <filename[:index]>".
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003613 * It uses show_crlfile_ctx and the global
3614 * crlfile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003615 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003616static int cli_io_handler_show_crlfile_detail(struct appctx *appctx)
3617{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003618 struct show_crlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003619 struct cafile_entry *cafile_entry = ctx->cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003620 struct buffer *out = alloc_trash_chunk();
3621 int i;
3622 X509_CRL *crl;
3623 STACK_OF(X509_OBJECT) *objs;
3624 int retval = 0;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003625 int index = ctx->index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003626
3627 if (!out)
3628 goto end_no_putchk;
3629
3630 chunk_appendf(out, "Filename: ");
3631 if (cafile_entry == crlfile_transaction.new_crlfile_entry)
3632 chunk_appendf(out, "*");
3633 chunk_appendf(out, "%s\n", cafile_entry->path);
3634
3635 chunk_appendf(out, "Status: ");
3636 if (!cafile_entry->ca_store)
3637 chunk_appendf(out, "Empty\n");
3638 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3639 chunk_appendf(out, "Unused\n");
3640 else
3641 chunk_appendf(out, "Used\n");
3642
3643 if (!cafile_entry->ca_store)
3644 goto end;
3645
3646 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3647 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
3648 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
3649 if (!crl)
3650 continue;
3651
3652 /* CRL indexes start at 1 on the CLI output. */
3653 if (index && index-1 != i)
3654 continue;
3655
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003656 chunk_appendf(out, " \nCertificate Revocation List #%d:\n", i+1);
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003657 retval = show_crl_detail(crl, out);
3658 if (retval < 0)
3659 goto end_no_putchk;
3660 else if (retval || index)
3661 goto end;
3662 }
3663
3664end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003665 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003666 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003667
3668end_no_putchk:
3669 free_trash_chunk(out);
3670 return 1;
3671yield:
3672 free_trash_chunk(out);
3673 return 0; /* should come back */
3674}
3675
Willy Tarreau821c3b02022-05-04 15:47:39 +02003676/* parsing function for 'show ssl crl-file [crlfile[:index]]'.
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003677 * It sets the context to a show_crlfile_ctx, and the global
Willy Tarreau821c3b02022-05-04 15:47:39 +02003678 * cafile_transaction.new_crlfile_entry under the ckch_lock.
3679 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003680static int cli_parse_show_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3681{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003682 struct show_crlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003683 struct cafile_entry *cafile_entry;
3684 long index = 0;
3685 char *colons;
3686 char *err = NULL;
3687
3688 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3689 return cli_err(appctx, "Can't allocate memory!\n");
3690
3691 /* The operations on the CKCH architecture are locked so we can
3692 * manipulate ckch_store and ckch_inst */
3693 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3694 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3695
3696 /* check if there is a certificate to lookup */
3697 if (*args[3]) {
3698
3699 /* Look for an optional index after the CRL file name */
3700 colons = strchr(args[3], ':');
3701 if (colons) {
3702 char *endptr;
3703
3704 index = strtol(colons + 1, &endptr, 10);
3705 /* Indexes start at 1 */
3706 if (colons + 1 == endptr || *endptr != '\0' || index <= 0) {
3707 memprintf(&err, "wrong CRL index after colons in '%s'!", args[3]);
3708 goto error;
3709 }
3710 *colons = '\0';
3711 }
3712
3713 if (*args[3] == '*') {
3714 if (!crlfile_transaction.new_crlfile_entry)
3715 goto error;
3716
3717 cafile_entry = crlfile_transaction.new_crlfile_entry;
3718
3719 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3720 goto error;
3721
3722 } else {
3723 /* Get the "original" cafile_entry and not the
3724 * uncommitted one if it exists. */
3725 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CRL)
3726 goto error;
3727 }
3728
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003729 ctx->cafile_entry = cafile_entry;
3730 ctx->index = index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003731 /* use the IO handler that shows details */
3732 appctx->io_handler = cli_io_handler_show_crlfile_detail;
3733 }
3734
3735 return 0;
3736
3737error:
3738 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3739 if (err)
3740 return cli_dynerr(appctx, err);
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003741 return cli_err(appctx, "Can't display the CRL file : Not found!\n");
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003742}
3743
3744/* IO handler of "show ssl crl-file". The command taking a specific CRL file name
3745 * is managed in cli_io_handler_show_crlfile_detail. */
3746static int cli_io_handler_show_crlfile(struct appctx *appctx)
3747{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003748 struct show_crlfile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003749 struct buffer *trash = alloc_trash_chunk();
3750 struct ebmb_node *node;
Christopher Faulet88041b32022-06-03 16:26:56 +02003751 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003752
3753 if (trash == NULL)
3754 return 1;
3755
Christopher Faulet9a99e542022-06-03 10:32:18 +02003756 if (!ctx->old_crlfile_entry && crlfile_transaction.old_crlfile_entry) {
3757 chunk_appendf(trash, "# transaction\n");
3758 chunk_appendf(trash, "*%s\n", crlfile_transaction.old_crlfile_entry->path);
3759 if (applet_putchk(appctx, trash) == -1)
3760 goto yield;
3761 ctx->old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003762 }
3763
3764 /* First time in this io_handler. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003765 if (!ctx->cafile_entry) {
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003766 chunk_appendf(trash, "# filename\n");
3767 node = ebmb_first(&cafile_tree);
3768 } else {
3769 /* We yielded during a previous call. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003770 node = &ctx->cafile_entry->node;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003771 }
3772
3773 while (node) {
3774 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3775 if (cafile_entry->type == CAFILE_CRL) {
3776 chunk_appendf(trash, "%s\n", cafile_entry->path);
3777 }
3778
3779 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003780 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003781 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003782 }
3783
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003784 ctx->cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003785 free_trash_chunk(trash);
3786 return 1;
3787yield:
3788
3789 free_trash_chunk(trash);
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003790 ctx->cafile_entry = cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003791 return 0; /* should come back */
3792}
3793
3794
3795/* release function of the 'show ssl crl-file' command */
3796static void cli_release_show_crlfile(struct appctx *appctx)
3797{
3798 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3799}
3800
3801
William Lallemandee8530c2020-06-23 18:19:42 +02003802void ckch_deinit()
3803{
3804 struct eb_node *node, *next;
3805 struct ckch_store *store;
William Lallemandb0c48272022-04-26 15:44:53 +02003806 struct ebmb_node *canode;
William Lallemandee8530c2020-06-23 18:19:42 +02003807
William Lallemandb0c48272022-04-26 15:44:53 +02003808 /* deinit the ckch stores */
William Lallemandee8530c2020-06-23 18:19:42 +02003809 node = eb_first(&ckchs_tree);
3810 while (node) {
3811 next = eb_next(node);
3812 store = ebmb_entry(node, struct ckch_store, node);
3813 ckch_store_free(store);
3814 node = next;
3815 }
William Lallemandb0c48272022-04-26 15:44:53 +02003816
3817 /* deinit the ca-file store */
3818 canode = ebmb_first(&cafile_tree);
3819 while (canode) {
3820 struct cafile_entry *entry = NULL;
3821
3822 entry = ebmb_entry(canode, struct cafile_entry, node);
3823 canode = ebmb_next(canode);
3824 ssl_store_delete_cafile_entry(entry);
3825 }
William Lallemandee8530c2020-06-23 18:19:42 +02003826}
William Lallemandda8584c2020-05-14 10:14:37 +02003827
3828/* register cli keywords */
3829static struct cli_kw_list cli_kws = {{ },{
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003830 { { "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 },
3831 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
3832 { { "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 },
3833 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
3834 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
3835 { { "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 },
3836
Amaury Denoyelleb11ad9e2021-05-21 11:01:10 +02003837 { { "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 +01003838 { { "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 +02003839 { { "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 +01003840 { { "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 +01003841 { { "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 +01003842 { { "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 +02003843
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003844 { { "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 +02003845 { { "set", "ssl", "crl-file", NULL }, "set ssl crl-file <crlfile> <payload> : replace a CRL file", cli_parse_set_crlfile, NULL, NULL },
3846 { { "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 +02003847 { { "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 +02003848 { { "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 +02003849 { { "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 +02003850 { { NULL }, NULL, NULL, NULL }
3851}};
3852
3853INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3854