blob: ecf69f067464b18541d49c40afbadb641349ef84 [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)) {
William Lallemandf784b902022-10-25 12:31:39 +0200636 memprintf(err, "%sunable to load certificate chain from file '%s': %s\n",
637 err && *err ? *err : "", path, ERR_reason_error_string(ret));
William Lallemand03c331c2020-05-13 10:10:01 +0200638 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
William Lallemande0fa91f2022-08-31 14:26:49 +0200989 /* Free the cafile_link_refs list */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100990 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
William Lallemande0fa91f2022-08-31 14:26:49 +0200991 if (link_ref->link && LIST_INLIST(&link_ref->link->list)) {
992 /* Try to detach and free the ckch_inst_link only if it
993 * was attached, this way it can be used to loop from
994 * the caller */
995 LIST_DEL_INIT(&link_ref->link->list);
996 ha_free(&link_ref->link);
997 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100998 LIST_DELETE(&link_ref->list);
999 free(link_ref);
1000 }
1001
William Lallemandfa1d8b42020-05-13 15:46:10 +02001002 free(inst);
1003}
1004
1005/* Alloc and init a ckch_inst */
1006struct ckch_inst *ckch_inst_new()
1007{
1008 struct ckch_inst *ckch_inst;
1009
1010 ckch_inst = calloc(1, sizeof *ckch_inst);
1011 if (!ckch_inst)
1012 return NULL;
1013
1014 LIST_INIT(&ckch_inst->sni_ctx);
1015 LIST_INIT(&ckch_inst->by_ckchs);
1016 LIST_INIT(&ckch_inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001017 LIST_INIT(&ckch_inst->cafile_link_refs);
William Lallemandfa1d8b42020-05-13 15:46:10 +02001018
1019 return ckch_inst;
1020}
1021
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001022
1023/******************** ssl_store functions ******************************/
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001024struct eb_root cafile_tree = EB_ROOT;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001025
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001026/*
1027 * Returns the cafile_entry found in the cafile_tree indexed by the path 'path'.
1028 * If 'oldest_entry' is 1, returns the "original" cafile_entry (since
1029 * during a set cafile/commit cafile cycle there might be two entries for any
1030 * given path, the original one and the new one set via the CLI but not
1031 * committed yet).
1032 */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001033struct cafile_entry *ssl_store_get_cafile_entry(char *path, int oldest_entry)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001034{
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001035 struct cafile_entry *ca_e = NULL;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001036 struct ebmb_node *eb;
1037
1038 eb = ebst_lookup(&cafile_tree, path);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001039 while (eb) {
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001040 ca_e = ebmb_entry(eb, struct cafile_entry, node);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001041 /* The ebst_lookup in a tree that has duplicates returns the
1042 * oldest entry first. If we want the latest entry, we need to
1043 * iterate over all the duplicates until we find the last one
1044 * (in our case there should never be more than two entries for
1045 * any given path). */
1046 if (oldest_entry)
1047 return ca_e;
1048 eb = ebmb_next_dup(eb);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001049 }
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001050 return ca_e;
1051}
1052
Remi Tricot-Le Breton38c999b2021-02-23 16:28:43 +01001053int ssl_store_add_uncommitted_cafile_entry(struct cafile_entry *entry)
1054{
1055 return (ebst_insert(&cafile_tree, &entry->node) != &entry->node);
1056}
1057
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001058X509_STORE* ssl_store_get0_locations_file(char *path)
1059{
1060 struct cafile_entry *ca_e = ssl_store_get_cafile_entry(path, 0);
1061
1062 if (ca_e)
1063 return ca_e->ca_store;
1064
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001065 return NULL;
1066}
1067
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001068/* Create a cafile_entry object, without adding it to the cafile_tree. */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001069struct 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 +01001070{
1071 struct cafile_entry *ca_e;
1072 int pathlen;
1073
1074 pathlen = strlen(path);
1075
1076 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
1077 if (ca_e) {
1078 memcpy(ca_e->path, path, pathlen + 1);
1079 ca_e->ca_store = store;
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001080 ca_e->type = type;
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001081 LIST_INIT(&ca_e->ckch_inst_link);
1082 }
1083 return ca_e;
1084}
1085
William Lallemand62c0b992022-07-29 17:50:58 +02001086
1087/* Duplicate a cafile_entry
1088 * Allocate the X509_STORE and copy the X509 and CRL inside.
1089 *
1090 * Return the newly allocated cafile_entry or NULL.
1091 *
1092 */
1093struct cafile_entry *ssl_store_dup_cafile_entry(struct cafile_entry *src)
1094{
1095 struct cafile_entry *dst = NULL;
1096 X509_STORE *store = NULL;
1097 STACK_OF(X509_OBJECT) *objs;
1098 int i;
1099
1100 if (!src)
1101 return NULL;
1102
1103 if (src->ca_store) {
1104 /* if there was a store in the src, copy it */
1105 store = X509_STORE_new();
1106 if (!store)
1107 goto err;
1108
1109 objs = X509_STORE_get0_objects(src->ca_store);
1110 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
1111 X509 *cert;
1112 X509_CRL *crl;
1113
1114 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
1115 if (cert) {
1116 if (X509_STORE_add_cert(store, cert) == 0) {
1117 /* only exits on error if the error is not about duplicate certificates */
1118 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1119 goto err;
1120 }
1121 }
1122
1123 }
1124 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
1125 if (crl) {
1126 if (X509_STORE_add_crl(store, crl) == 0) {
1127 /* only exits on error if the error is not about duplicate certificates */
1128 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1129 goto err;
1130 }
1131 }
1132
1133 }
1134 }
1135 }
1136 dst = ssl_store_create_cafile_entry(src->path, store, src->type);
1137
1138 return dst;
1139
1140err:
1141 X509_STORE_free(store);
1142 ha_free(&dst);
1143
1144 return NULL;
1145}
1146
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001147/* Delete a cafile_entry. The caller is responsible from removing this entry
1148 * from the cafile_tree first if is was previously added into it. */
1149void ssl_store_delete_cafile_entry(struct cafile_entry *ca_e)
1150{
1151 struct ckch_inst_link *link, *link_s;
1152 if (!ca_e)
1153 return;
1154
1155 X509_STORE_free(ca_e->ca_store);
1156
1157 list_for_each_entry_safe(link, link_s, &ca_e->ckch_inst_link, list) {
1158 struct ckch_inst *inst = link->ckch_inst;
1159 struct ckch_inst_link_ref *link_ref, *link_ref_s;
1160 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
1161 if (link_ref->link == link) {
1162 LIST_DELETE(&link_ref->list);
1163 free(link_ref);
1164 break;
1165 }
1166 }
1167 LIST_DELETE(&link->list);
1168 free(link);
1169 }
1170
1171 free(ca_e);
1172}
1173
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001174/*
William Lallemandd4774d32022-07-29 17:08:02 +02001175 * Fill a cafile_entry <ca_e> X509_STORE ca_e->store out of a buffer <cert_buf>
1176 * instead of out of a file. The <append> field should be set to 1 if you want
1177 * to keep the existing X509_STORE and append data to it.
1178 *
1179 * This function is used when the "set ssl ca-file" cli command is used.
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001180 * It can parse CERTIFICATE sections as well as CRL ones.
1181 * Returns 0 in case of success, 1 otherwise.
William Lallemandd4774d32022-07-29 17:08:02 +02001182 *
1183 * /!\ Warning: If there was an error the X509_STORE could have been modified so it's
1184 * better to not use it after a return 1.
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001185 */
William Lallemandd4774d32022-07-29 17:08:02 +02001186int ssl_store_load_ca_from_buf(struct cafile_entry *ca_e, char *cert_buf, int append)
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001187{
William Lallemandd4774d32022-07-29 17:08:02 +02001188 BIO *bio = NULL;
1189 STACK_OF(X509_INFO) *infos;
1190 X509_INFO *info;
1191 int i;
1192 int retval = 1;
1193 int retcert = 0;
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001194
1195 if (!ca_e)
1196 return 1;
1197
William Lallemandd4774d32022-07-29 17:08:02 +02001198 if (!append) {
1199 X509_STORE_free(ca_e->ca_store);
1200 ca_e->ca_store = NULL;
1201 }
1202
1203 if (!ca_e->ca_store)
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001204 ca_e->ca_store = X509_STORE_new();
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001205
William Lallemandd4774d32022-07-29 17:08:02 +02001206 if (!ca_e->ca_store)
1207 goto end;
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001208
William Lallemandd4774d32022-07-29 17:08:02 +02001209 bio = BIO_new_mem_buf(cert_buf, strlen(cert_buf));
1210 if (!bio)
1211 goto end;
1212
1213 infos = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
1214 if (!infos)
1215 goto end;
1216
1217 for (i = 0; i < sk_X509_INFO_num(infos) && !retcert; i++) {
1218 info = sk_X509_INFO_value(infos, i);
1219
1220 /* X509_STORE_add_cert and X509_STORE_add_crl return 1 on success */
1221 if (info->x509)
1222 retcert = !X509_STORE_add_cert(ca_e->ca_store, info->x509);
1223 if (!retcert && info->crl)
1224 retcert = !X509_STORE_add_crl(ca_e->ca_store, info->crl);
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001225 }
1226
William Lallemandd4774d32022-07-29 17:08:02 +02001227 /* return an error if we didn't compute all the X509_INFO or if there was none
1228 * set to 0 if everything was right */
1229 if (!(retcert || (i != sk_X509_INFO_num(infos)) || (sk_X509_INFO_num(infos) == 0)))
1230 retval = 0;
1231
1232 /* Cleanup */
1233 sk_X509_INFO_pop_free(infos, X509_INFO_free);
1234
1235end:
1236 BIO_free(bio);
1237
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001238 return retval;
1239}
1240
William Lallemand0f17ab22022-07-19 18:03:16 +02001241/*
1242 * Try to load a ca-file from disk into the ca-file cache.
1243 *
1244 * Return 0 upon error
1245 */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001246int ssl_store_load_locations_file(char *path, int create_if_none, enum cafile_type type)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001247{
1248 X509_STORE *store = ssl_store_get0_locations_file(path);
1249
1250 /* If this function is called by the CLI, we should not call the
1251 * X509_STORE_load_locations function because it performs forbidden disk
1252 * accesses. */
1253 if (!store && create_if_none) {
William Lallemand87fd9942022-04-01 20:12:03 +02001254 STACK_OF(X509_OBJECT) *objs;
1255 int cert_count = 0;
1256 struct stat buf;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001257 struct cafile_entry *ca_e;
William Lallemandc6b17632022-04-01 23:39:37 +02001258 const char *file = NULL;
1259 const char *dir = NULL;
William Lallemand0f17ab22022-07-19 18:03:16 +02001260 unsigned long e;
William Lallemand87fd9942022-04-01 20:12:03 +02001261
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001262 store = X509_STORE_new();
William Lallemand0f17ab22022-07-19 18:03:16 +02001263 if (!store) {
1264 ha_alert("Cannot allocate memory!");
1265 goto err;
1266 }
William Lallemand87fd9942022-04-01 20:12:03 +02001267
William Lallemandc6b17632022-04-01 23:39:37 +02001268 if (strcmp(path, "@system-ca") == 0) {
1269 dir = X509_get_default_cert_dir();
William Lallemand0f17ab22022-07-19 18:03:16 +02001270 if (!dir) {
1271 ha_alert("Couldn't get the system CA directory from X509_get_default_cert_dir().");
1272 goto err;
1273 }
William Lallemand87fd9942022-04-01 20:12:03 +02001274
William Lallemandc6b17632022-04-01 23:39:37 +02001275 } else {
1276
William Lallemand0f17ab22022-07-19 18:03:16 +02001277 if (stat(path, &buf) == -1) {
1278 ha_alert("Couldn't open the ca-file '%s' (%s).", path, strerror(errno));
William Lallemandc6b17632022-04-01 23:39:37 +02001279 goto err;
William Lallemand0f17ab22022-07-19 18:03:16 +02001280 }
William Lallemandc6b17632022-04-01 23:39:37 +02001281
1282 if (S_ISDIR(buf.st_mode))
1283 dir = path;
1284 else
1285 file = path;
1286 }
William Lallemand87fd9942022-04-01 20:12:03 +02001287
1288 if (file) {
1289 if (!X509_STORE_load_locations(store, file, NULL)) {
William Lallemand0f17ab22022-07-19 18:03:16 +02001290 e = ERR_get_error();
1291 ha_alert("Couldn't open the ca-file '%s' (%s).", path, ERR_reason_error_string(e));
William Lallemand87fd9942022-04-01 20:12:03 +02001292 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001293 }
William Lallemand80296b42022-04-05 10:19:30 +02001294 } else if (dir) {
William Lallemand87fd9942022-04-01 20:12:03 +02001295 int n, i;
1296 struct dirent **de_list;
1297
1298 n = scandir(dir, &de_list, 0, alphasort);
1299 if (n < 0)
1300 goto err;
1301
1302 for (i= 0; i < n; i++) {
1303 char *end;
1304 struct dirent *de = de_list[i];
1305 BIO *in = NULL;
1306 X509 *ca = NULL;;
1307
William Lallemand43482322022-07-18 18:42:52 +02001308 ERR_clear_error();
1309
William Lallemand87fd9942022-04-01 20:12:03 +02001310 /* we try to load the files that would have
1311 * been loaded in an hashed directory loaded by
1312 * X509_LOOKUP_hash_dir, so according to "man 1
1313 * c_rehash", we should load ".pem", ".crt",
William Lallemande4b93eb2022-05-09 09:29:00 +02001314 * ".cer", or ".crl". Files starting with a dot
1315 * are ignored.
William Lallemand87fd9942022-04-01 20:12:03 +02001316 */
1317 end = strrchr(de->d_name, '.');
William Lallemande4b93eb2022-05-09 09:29:00 +02001318 if (!end || de->d_name[0] == '.' ||
1319 (strcmp(end, ".pem") != 0 &&
1320 strcmp(end, ".crt") != 0 &&
1321 strcmp(end, ".cer") != 0 &&
1322 strcmp(end, ".crl") != 0)) {
William Lallemand87fd9942022-04-01 20:12:03 +02001323 free(de);
1324 continue;
1325 }
1326 in = BIO_new(BIO_s_file());
1327 if (in == NULL)
1328 goto scandir_err;
1329
William Lallemandc6b17632022-04-01 23:39:37 +02001330 chunk_printf(&trash, "%s/%s", dir, de->d_name);
William Lallemand87fd9942022-04-01 20:12:03 +02001331
1332 if (BIO_read_filename(in, trash.area) == 0)
1333 goto scandir_err;
1334
1335 if (PEM_read_bio_X509_AUX(in, &ca, NULL, NULL) == NULL)
1336 goto scandir_err;
1337
William Lallemand43482322022-07-18 18:42:52 +02001338 if (X509_STORE_add_cert(store, ca) == 0) {
1339 /* only exits on error if the error is not about duplicate certificates */
1340 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1341 goto scandir_err;
1342 }
1343 }
William Lallemand87fd9942022-04-01 20:12:03 +02001344
William Lallemand4cfbf3c2022-04-26 15:57:33 +02001345 X509_free(ca);
William Lallemand87fd9942022-04-01 20:12:03 +02001346 BIO_free(in);
1347 free(de);
1348 continue;
1349
1350scandir_err:
William Lallemand0f17ab22022-07-19 18:03:16 +02001351 e = ERR_get_error();
William Lallemand4cfbf3c2022-04-26 15:57:33 +02001352 X509_free(ca);
William Lallemand87fd9942022-04-01 20:12:03 +02001353 BIO_free(in);
1354 free(de);
William Lallemand0f17ab22022-07-19 18:03:16 +02001355 /* warn if it can load one of the files, but don't abort */
1356 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 +02001357
1358 }
1359 free(de_list);
William Lallemand80296b42022-04-05 10:19:30 +02001360 } else {
1361 ha_alert("ca-file: couldn't load '%s'\n", path);
1362 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001363 }
William Lallemand87fd9942022-04-01 20:12:03 +02001364
1365 objs = X509_STORE_get0_objects(store);
1366 cert_count = sk_X509_OBJECT_num(objs);
William Lallemand0f17ab22022-07-19 18:03:16 +02001367 if (cert_count == 0) {
William Lallemand87fd9942022-04-01 20:12:03 +02001368 ha_warning("ca-file: 0 CA were loaded from '%s'\n", path);
William Lallemand0f17ab22022-07-19 18:03:16 +02001369 }
William Lallemand87fd9942022-04-01 20:12:03 +02001370 ca_e = ssl_store_create_cafile_entry(path, store, type);
William Lallemand0f17ab22022-07-19 18:03:16 +02001371 if (!ca_e) {
1372 ha_alert("Cannot allocate memory!\n");
William Lallemand87fd9942022-04-01 20:12:03 +02001373 goto err;
William Lallemand0f17ab22022-07-19 18:03:16 +02001374 }
William Lallemand87fd9942022-04-01 20:12:03 +02001375 ebst_insert(&cafile_tree, &ca_e->node);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001376 }
1377 return (store != NULL);
William Lallemand87fd9942022-04-01 20:12:03 +02001378
1379err:
1380 X509_STORE_free(store);
1381 store = NULL;
1382 return 0;
1383
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001384}
1385
1386
William Lallemandda8584c2020-05-14 10:14:37 +02001387/*************************** CLI commands ***********************/
1388
1389/* Type of SSL payloads that can be updated over the CLI */
1390
William Lallemandff8bf982022-03-29 10:44:23 +02001391struct cert_exts cert_exts[] = {
1392 { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand26654e72022-03-30 12:01:32 +02001393 { "crt", CERT_TYPE_CRT, &ssl_sock_load_pem_into_ckch },
William Lallemandff8bf982022-03-29 10:44:23 +02001394 { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemandda8584c2020-05-14 10:14:37 +02001395#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemandff8bf982022-03-29 10:44:23 +02001396 { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001397#endif
Ilya Shipitsinc47d6762021-02-13 11:45:33 +05001398#ifdef HAVE_SSL_SCTL
William Lallemandff8bf982022-03-29 10:44:23 +02001399 { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001400#endif
William Lallemandff8bf982022-03-29 10:44:23 +02001401 { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
1402 { NULL, CERT_TYPE_MAX, NULL },
William Lallemandda8584c2020-05-14 10:14:37 +02001403};
1404
1405
1406/* release function of the `show ssl cert' command */
1407static void cli_release_show_cert(struct appctx *appctx)
1408{
1409 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1410}
1411
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001412/* IO handler of "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001413 * It makes use of a show_cert_ctx context, and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001414 */
William Lallemandda8584c2020-05-14 10:14:37 +02001415static int cli_io_handler_show_cert(struct appctx *appctx)
1416{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001417 struct show_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001418 struct buffer *trash = alloc_trash_chunk();
1419 struct ebmb_node *node;
Christopher Fauletd1d2e4d2022-06-03 16:24:02 +02001420 struct ckch_store *ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001421
1422 if (trash == NULL)
1423 return 1;
1424
Christopher Faulet3e94f5d2022-06-03 10:46:40 +02001425 if (!ctx->old_ckchs && ckchs_transaction.old_ckchs) {
1426 ckchs = ckchs_transaction.old_ckchs;
1427 chunk_appendf(trash, "# transaction\n");
1428 chunk_appendf(trash, "*%s\n", ckchs->path);
1429 if (applet_putchk(appctx, trash) == -1)
1430 goto yield;
1431 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001432 }
1433
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001434 if (!ctx->cur_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02001435 chunk_appendf(trash, "# filename\n");
1436 node = ebmb_first(&ckchs_tree);
1437 } else {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001438 node = &ctx->cur_ckchs->node;
William Lallemandda8584c2020-05-14 10:14:37 +02001439 }
1440 while (node) {
1441 ckchs = ebmb_entry(node, struct ckch_store, node);
William Lallemand5685ccf2020-09-16 16:12:25 +02001442 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001443
1444 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001445 if (applet_putchk(appctx, trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001446 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001447 }
1448
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001449 ctx->cur_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001450 free_trash_chunk(trash);
1451 return 1;
1452yield:
1453
1454 free_trash_chunk(trash);
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001455 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001456 return 0; /* should come back */
1457}
1458
1459/*
1460 * Extract and format the DNS SAN extensions and copy result into a chuink
1461 * Return 0;
1462 */
1463#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1464static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
1465{
1466 int i;
1467 char *str;
1468 STACK_OF(GENERAL_NAME) *names = NULL;
1469
1470 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1471 if (names) {
1472 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
1473 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
1474 if (i > 0)
1475 chunk_appendf(out, ", ");
1476 if (name->type == GEN_DNS) {
1477 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
1478 chunk_appendf(out, "DNS:%s", str);
1479 OPENSSL_free(str);
1480 }
1481 }
1482 }
1483 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
1484 }
1485 return 0;
1486}
1487#endif
1488
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001489/*
1490 * Build the ckch_inst_link that will be chained in the CA file entry and the
1491 * corresponding ckch_inst_link_ref that will be chained in the ckch instance.
1492 * Return 0 in case of success.
1493 */
1494static int do_chain_inst_and_cafile(struct cafile_entry *cafile_entry, struct ckch_inst *ckch_inst)
1495{
1496 struct ckch_inst_link *new_link;
1497 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
1498 struct ckch_inst_link *link = LIST_ELEM(cafile_entry->ckch_inst_link.n,
1499 typeof(link), list);
1500 /* Do not add multiple references to the same
1501 * instance in a cafile_entry */
1502 if (link->ckch_inst == ckch_inst) {
1503 return 1;
1504 }
1505 }
1506
1507 new_link = calloc(1, sizeof(*new_link));
1508 if (new_link) {
1509 struct ckch_inst_link_ref *new_link_ref = calloc(1, sizeof(*new_link_ref));
1510 if (!new_link_ref) {
1511 free(new_link);
1512 return 1;
1513 }
1514
1515 new_link->ckch_inst = ckch_inst;
1516 new_link_ref->link = new_link;
1517 LIST_INIT(&new_link->list);
1518 LIST_INIT(&new_link_ref->list);
1519
1520 LIST_APPEND(&cafile_entry->ckch_inst_link, &new_link->list);
1521 LIST_APPEND(&ckch_inst->cafile_link_refs, &new_link_ref->list);
1522 }
1523
1524 return 0;
1525}
1526
1527
1528/*
1529 * Link a CA file tree entry to the ckch instance that uses it.
1530 * To determine if and which CA file tree entries need to be linked to the
1531 * instance, we follow the same logic performed in ssl_sock_prepare_ctx when
1532 * processing the verify option.
1533 * This function works for a frontend as well as for a backend, depending on the
1534 * configuration parameters given (bind_conf or server).
1535 */
1536void ckch_inst_add_cafile_link(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf,
1537 struct ssl_bind_conf *ssl_conf, const struct server *srv)
1538{
1539 int verify = SSL_VERIFY_NONE;
1540
1541 if (srv) {
1542
1543 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
1544 verify = SSL_VERIFY_PEER;
1545 switch (srv->ssl_ctx.verify) {
1546 case SSL_SOCK_VERIFY_NONE:
1547 verify = SSL_VERIFY_NONE;
1548 break;
1549 case SSL_SOCK_VERIFY_REQUIRED:
1550 verify = SSL_VERIFY_PEER;
1551 break;
1552 }
1553 }
1554 else {
1555 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
1556 case SSL_SOCK_VERIFY_NONE:
1557 verify = SSL_VERIFY_NONE;
1558 break;
1559 case SSL_SOCK_VERIFY_OPTIONAL:
1560 verify = SSL_VERIFY_PEER;
1561 break;
1562 case SSL_SOCK_VERIFY_REQUIRED:
1563 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1564 break;
1565 }
1566 }
1567
1568 if (verify & SSL_VERIFY_PEER) {
1569 struct cafile_entry *ca_file_entry = NULL;
1570 struct cafile_entry *ca_verify_file_entry = NULL;
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001571 struct cafile_entry *crl_file_entry = NULL;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001572 if (srv) {
1573 if (srv->ssl_ctx.ca_file) {
1574 ca_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.ca_file, 0);
1575
1576 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001577 if (srv->ssl_ctx.crl_file) {
1578 crl_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.crl_file, 0);
1579 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001580 }
1581 else {
1582 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
1583 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 +02001584 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 +01001585
1586 if (ca_file)
1587 ca_file_entry = ssl_store_get_cafile_entry(ca_file, 0);
1588 if (ca_verify_file)
1589 ca_verify_file_entry = ssl_store_get_cafile_entry(ca_verify_file, 0);
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001590 if (crl_file)
1591 crl_file_entry = ssl_store_get_cafile_entry(crl_file, 0);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001592 }
1593
1594 if (ca_file_entry) {
1595 /* If we have a ckch instance that is not already in the
1596 * cafile_entry's list, add it to it. */
1597 if (do_chain_inst_and_cafile(ca_file_entry, ckch_inst))
1598 return;
1599
1600 }
1601 if (ca_verify_file_entry && (ca_file_entry != ca_verify_file_entry)) {
1602 /* If we have a ckch instance that is not already in the
1603 * cafile_entry's list, add it to it. */
1604 if (do_chain_inst_and_cafile(ca_verify_file_entry, ckch_inst))
1605 return;
1606 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001607 if (crl_file_entry) {
1608 /* If we have a ckch instance that is not already in the
1609 * cafile_entry's list, add it to it. */
1610 if (do_chain_inst_and_cafile(crl_file_entry, ckch_inst))
1611 return;
1612 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001613 }
1614}
1615
William Lallemandda8584c2020-05-14 10:14:37 +02001616
1617
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001618static int show_cert_detail(X509 *cert, STACK_OF(X509) *chain, struct buffer *out)
William Lallemandda8584c2020-05-14 10:14:37 +02001619{
William Lallemandda8584c2020-05-14 10:14:37 +02001620 BIO *bio = NULL;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001621 struct buffer *tmp = alloc_trash_chunk();
William Lallemandda8584c2020-05-14 10:14:37 +02001622 int i;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001623 int write = -1;
1624 unsigned int len = 0;
1625 X509_NAME *name = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001626
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001627 if (!tmp)
1628 return -1;
William Lallemandda8584c2020-05-14 10:14:37 +02001629
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001630 if (!cert)
William Lallemand5685ccf2020-09-16 16:12:25 +02001631 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02001632
William Lallemand5685ccf2020-09-16 16:12:25 +02001633 if (chain == NULL) {
1634 struct issuer_chain *issuer;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001635 issuer = ssl_get0_issuer_chain(cert);
William Lallemand5685ccf2020-09-16 16:12:25 +02001636 if (issuer) {
1637 chain = issuer->chain;
1638 chunk_appendf(out, "Chain Filename: ");
1639 chunk_appendf(out, "%s\n", issuer->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001640 }
William Lallemand5685ccf2020-09-16 16:12:25 +02001641 }
1642 chunk_appendf(out, "Serial: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001643 if (ssl_sock_get_serial(cert, tmp) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001644 goto end;
1645 dump_binary(out, tmp->area, tmp->data);
1646 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001647
William Lallemand5685ccf2020-09-16 16:12:25 +02001648 chunk_appendf(out, "notBefore: ");
1649 chunk_reset(tmp);
1650 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1651 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001652 if (ASN1_TIME_print(bio, X509_getm_notBefore(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001653 goto end;
1654 write = BIO_read(bio, tmp->area, tmp->size-1);
1655 tmp->area[write] = '\0';
1656 BIO_free(bio);
1657 bio = NULL;
1658 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001659
William Lallemand5685ccf2020-09-16 16:12:25 +02001660 chunk_appendf(out, "notAfter: ");
1661 chunk_reset(tmp);
1662 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1663 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001664 if (ASN1_TIME_print(bio, X509_getm_notAfter(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001665 goto end;
1666 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
1667 goto end;
1668 tmp->area[write] = '\0';
1669 BIO_free(bio);
1670 bio = NULL;
1671 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001672
1673#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand5685ccf2020-09-16 16:12:25 +02001674 chunk_appendf(out, "Subject Alternative Name: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001675 if (ssl_sock_get_san_oneline(cert, out) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001676 goto end;
1677 *(out->area + out->data) = '\0';
1678 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001679#endif
William Lallemand5685ccf2020-09-16 16:12:25 +02001680 chunk_reset(tmp);
1681 chunk_appendf(out, "Algorithm: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001682 if (cert_get_pkey_algo(cert, tmp) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001683 goto end;
1684 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001685
William Lallemand5685ccf2020-09-16 16:12:25 +02001686 chunk_reset(tmp);
1687 chunk_appendf(out, "SHA1 FingerPrint: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001688 if (X509_digest(cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001689 goto end;
1690 tmp->data = len;
1691 dump_binary(out, tmp->area, tmp->data);
1692 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001693
William Lallemand5685ccf2020-09-16 16:12:25 +02001694 chunk_appendf(out, "Subject: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001695 if ((name = X509_get_subject_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001696 goto end;
1697 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1698 goto end;
1699 *(tmp->area + tmp->data) = '\0';
1700 chunk_appendf(out, "%s\n", tmp->area);
1701
1702 chunk_appendf(out, "Issuer: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001703 if ((name = X509_get_issuer_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001704 goto end;
1705 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1706 goto end;
1707 *(tmp->area + tmp->data) = '\0';
1708 chunk_appendf(out, "%s\n", tmp->area);
1709
1710 /* Displays subject of each certificate in the chain */
1711 for (i = 0; i < sk_X509_num(chain); i++) {
1712 X509 *ca = sk_X509_value(chain, i);
1713
1714 chunk_appendf(out, "Chain Subject: ");
1715 if ((name = X509_get_subject_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001716 goto end;
1717 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1718 goto end;
1719 *(tmp->area + tmp->data) = '\0';
1720 chunk_appendf(out, "%s\n", tmp->area);
1721
William Lallemand5685ccf2020-09-16 16:12:25 +02001722 chunk_appendf(out, "Chain Issuer: ");
1723 if ((name = X509_get_issuer_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001724 goto end;
1725 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1726 goto end;
1727 *(tmp->area + tmp->data) = '\0';
1728 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001729 }
1730
1731end:
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001732 if (bio)
1733 BIO_free(bio);
1734 free_trash_chunk(tmp);
1735
1736 return 0;
1737}
1738
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001739#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 +02001740/*
1741 * Build the OCSP tree entry's key for a given ckch_store.
1742 * Returns a negative value in case of error.
1743 */
1744static int ckch_store_build_certid(struct ckch_store *ckch_store, unsigned char certid[128], unsigned int *key_length)
1745{
1746 OCSP_RESPONSE *resp;
1747 OCSP_BASICRESP *bs = NULL;
1748 OCSP_SINGLERESP *sr;
1749 OCSP_CERTID *id;
1750 unsigned char *p = NULL;
1751
1752 if (!key_length)
1753 return -1;
1754
1755 *key_length = 0;
1756
1757 if (!ckch_store->ckch->ocsp_response)
1758 return 0;
1759
1760 p = (unsigned char *) ckch_store->ckch->ocsp_response->area;
1761
1762 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
1763 ckch_store->ckch->ocsp_response->data);
1764 if (!resp) {
1765 goto end;
1766 }
1767
1768 bs = OCSP_response_get1_basic(resp);
1769 if (!bs) {
1770 goto end;
1771 }
1772
1773 sr = OCSP_resp_get0(bs, 0);
1774 if (!sr) {
1775 goto end;
1776 }
1777
1778 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
1779
1780 p = certid;
1781 *key_length = i2d_OCSP_CERTID(id, &p);
1782
1783end:
1784 return *key_length > 0;
1785}
1786#endif
1787
1788/*
1789 * Dump the OCSP certificate key (if it exists) of certificate <ckch> into
1790 * buffer <out>.
1791 * Returns 0 in case of success.
1792 */
1793static int ckch_store_show_ocsp_certid(struct ckch_store *ckch_store, struct buffer *out)
1794{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001795#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 +02001796 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1797 unsigned int key_length = 0;
1798 int i;
1799
1800 if (ckch_store_build_certid(ckch_store, (unsigned char*)key, &key_length) >= 0) {
1801 /* Dump the CERTID info */
1802 chunk_appendf(out, "OCSP Response Key: ");
1803 for (i = 0; i < key_length; ++i) {
1804 chunk_appendf(out, "%02x", key[i]);
1805 }
1806 chunk_appendf(out, "\n");
1807 }
1808#endif
1809
1810 return 0;
1811}
1812
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001813
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001814/* IO handler of the details "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001815 * It uses a struct show_cert_ctx and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001816 */
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001817static int cli_io_handler_show_cert_detail(struct appctx *appctx)
1818{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001819 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001820 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001821 struct buffer *out = alloc_trash_chunk();
1822 int retval = 0;
1823
1824 if (!out)
1825 goto end_no_putchk;
1826
1827 chunk_appendf(out, "Filename: ");
1828 if (ckchs == ckchs_transaction.new_ckchs)
1829 chunk_appendf(out, "*");
1830 chunk_appendf(out, "%s\n", ckchs->path);
1831
1832 chunk_appendf(out, "Status: ");
1833 if (ckchs->ckch->cert == NULL)
1834 chunk_appendf(out, "Empty\n");
1835 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
1836 chunk_appendf(out, "Unused\n");
1837 else
1838 chunk_appendf(out, "Used\n");
1839
1840 retval = show_cert_detail(ckchs->ckch->cert, ckchs->ckch->chain, out);
1841 if (retval < 0)
1842 goto end_no_putchk;
1843 else if (retval)
1844 goto end;
1845
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001846 ckch_store_show_ocsp_certid(ckchs, out);
1847
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001848end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001849 if (applet_putchk(appctx, out) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001850 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001851
1852end_no_putchk:
William Lallemandda8584c2020-05-14 10:14:37 +02001853 free_trash_chunk(out);
1854 return 1;
1855yield:
William Lallemandda8584c2020-05-14 10:14:37 +02001856 free_trash_chunk(out);
1857 return 0; /* should come back */
1858}
1859
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001860
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001861/* IO handler of the details "show ssl cert <filename.ocsp>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001862 * It uses a show_cert_ctx.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001863 */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001864static int cli_io_handler_show_cert_ocsp_detail(struct appctx *appctx)
1865{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001866#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL)
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001867 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001868 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001869 struct buffer *out = alloc_trash_chunk();
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001870 int from_transaction = ctx->transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001871
1872 if (!out)
1873 goto end_no_putchk;
1874
1875 /* If we try to display an ongoing transaction's OCSP response, we
1876 * need to dump the ckch's ocsp_response buffer directly.
1877 * Otherwise, we must rebuild the certificate's certid in order to
1878 * look for the current OCSP response in the tree. */
1879 if (from_transaction && ckchs->ckch->ocsp_response) {
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001880 if (ssl_ocsp_response_print(ckchs->ckch->ocsp_response, out))
1881 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001882 }
1883 else {
1884 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1885 unsigned int key_length = 0;
1886
1887 if (ckch_store_build_certid(ckchs, (unsigned char*)key, &key_length) < 0)
1888 goto end_no_putchk;
1889
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001890 if (ssl_get_ocspresponse_detail(key, out))
1891 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001892 }
1893
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001894 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001895 goto yield;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001896
1897end_no_putchk:
1898 free_trash_chunk(out);
1899 return 1;
1900yield:
1901 free_trash_chunk(out);
1902 return 0; /* should come back */
1903#else
1904 return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n");
1905#endif
1906}
1907
William Lallemandda8584c2020-05-14 10:14:37 +02001908/* parsing function for 'show ssl cert [certfile]' */
1909static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
1910{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001911 struct show_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02001912 struct ckch_store *ckchs;
1913
1914 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1915 return cli_err(appctx, "Can't allocate memory!\n");
1916
1917 /* The operations on the CKCH architecture are locked so we can
1918 * manipulate ckch_store and ckch_inst */
1919 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1920 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
1921
1922 /* check if there is a certificate to lookup */
1923 if (*args[3]) {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001924 int show_ocsp_detail = 0;
1925 int from_transaction = 0;
1926 char *end;
1927
1928 /* We manage the special case "certname.ocsp" through which we
1929 * can show the details of an OCSP response. */
1930 end = strrchr(args[3], '.');
1931 if (end && strcmp(end+1, "ocsp") == 0) {
1932 *end = '\0';
1933 show_ocsp_detail = 1;
1934 }
1935
William Lallemandda8584c2020-05-14 10:14:37 +02001936 if (*args[3] == '*') {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001937 from_transaction = 1;
William Lallemandda8584c2020-05-14 10:14:37 +02001938 if (!ckchs_transaction.new_ckchs)
1939 goto error;
1940
1941 ckchs = ckchs_transaction.new_ckchs;
1942
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001943 if (strcmp(args[3] + 1, ckchs->path) != 0)
William Lallemandda8584c2020-05-14 10:14:37 +02001944 goto error;
1945
1946 } else {
1947 if ((ckchs = ckchs_lookup(args[3])) == NULL)
1948 goto error;
1949
1950 }
1951
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001952 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001953 /* use the IO handler that shows details */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001954 if (show_ocsp_detail) {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001955 ctx->transaction = from_transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001956 appctx->io_handler = cli_io_handler_show_cert_ocsp_detail;
1957 }
1958 else
1959 appctx->io_handler = cli_io_handler_show_cert_detail;
William Lallemandda8584c2020-05-14 10:14:37 +02001960 }
1961
1962 return 0;
1963
1964error:
1965 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1966 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
1967}
1968
1969/* release function of the `set ssl cert' command, free things and unlock the spinlock */
1970static void cli_release_commit_cert(struct appctx *appctx)
1971{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02001972 struct commit_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001973
1974 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulet9d56e242022-05-31 16:37:01 +02001975 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
1976 if (ctx->new_ckchs)
1977 ckch_store_free(ctx->new_ckchs);
1978 ha_free(&ctx->err);
William Lallemandda8584c2020-05-14 10:14:37 +02001979}
1980
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001981
1982/*
1983 * Rebuild a new instance 'new_inst' based on an old instance 'ckchi' and a
1984 * specific ckch_store.
1985 * Returns 0 in case of success, 1 otherwise.
1986 */
William Lallemande60c7d62022-03-30 11:26:15 +02001987int ckch_inst_rebuild(struct ckch_store *ckch_store, struct ckch_inst *ckchi,
1988 struct ckch_inst **new_inst, char **err)
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001989{
1990 int retval = 0;
1991 int errcode = 0;
1992 struct sni_ctx *sc0, *sc0s;
1993 char **sni_filter = NULL;
1994 int fcount = 0;
1995
1996 if (ckchi->crtlist_entry) {
1997 sni_filter = ckchi->crtlist_entry->filters;
1998 fcount = ckchi->crtlist_entry->fcount;
1999 }
2000
2001 if (ckchi->is_server_instance)
2002 errcode |= ckch_inst_new_load_srv_store(ckch_store->path, ckch_store, new_inst, err);
2003 else
2004 errcode |= ckch_inst_new_load_store(ckch_store->path, ckch_store, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, new_inst, err);
2005
2006 if (errcode & ERR_CODE)
2007 return 1;
2008
2009 /* if the previous ckchi was used as the default */
2010 if (ckchi->is_default)
2011 (*new_inst)->is_default = 1;
2012
2013 (*new_inst)->is_server_instance = ckchi->is_server_instance;
2014 (*new_inst)->server = ckchi->server;
2015 /* Create a new SSL_CTX and link it to the new instance. */
2016 if ((*new_inst)->is_server_instance) {
2017 retval = ssl_sock_prep_srv_ctx_and_inst(ckchi->server, (*new_inst)->ctx, (*new_inst));
2018 if (retval)
2019 return 1;
2020 }
2021
2022 /* create the link to the crtlist_entry */
2023 (*new_inst)->crtlist_entry = ckchi->crtlist_entry;
2024
2025 /* we need to initialize the SSL_CTX generated */
2026 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
2027 list_for_each_entry_safe(sc0, sc0s, &(*new_inst)->sni_ctx, by_ckch_inst) {
2028 if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
2029 errcode |= ssl_sock_prep_ctx_and_inst(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, *new_inst, err);
2030 if (errcode & ERR_CODE)
2031 return 1;
2032 }
2033 }
2034
2035 return 0;
2036}
2037
2038/*
2039 * Load all the new SNIs of a newly built ckch instance in the trees, or replace
2040 * a server's main ckch instance.
2041 */
2042static void __ssl_sock_load_new_ckch_instance(struct ckch_inst *ckchi)
2043{
2044 /* The bind_conf will be null on server ckch_instances. */
2045 if (ckchi->is_server_instance) {
2046 int i;
2047 /* a lock is needed here since we have to free the SSL cache */
2048 HA_RWLOCK_WRLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
2049 /* free the server current SSL_CTX */
2050 SSL_CTX_free(ckchi->server->ssl_ctx.ctx);
2051 /* Actual ssl context update */
2052 SSL_CTX_up_ref(ckchi->ctx);
2053 ckchi->server->ssl_ctx.ctx = ckchi->ctx;
2054 ckchi->server->ssl_ctx.inst = ckchi;
2055
2056 /* flush the session cache of the server */
2057 for (i = 0; i < global.nbthread; i++) {
William Lallemandce990332021-11-23 15:15:09 +01002058 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].sni);
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002059 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].ptr);
2060 }
2061 HA_RWLOCK_WRUNLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
2062
2063 } else {
2064 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
2065 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
2066 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
2067 }
2068}
2069
2070/*
2071 * Delete a ckch instance that was replaced after a CLI command.
2072 */
2073static void __ckch_inst_free_locked(struct ckch_inst *ckchi)
2074{
2075 if (ckchi->is_server_instance) {
2076 /* no lock for servers */
2077 ckch_inst_free(ckchi);
2078 } else {
2079 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
2080
2081 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
2082 ckch_inst_free(ckchi);
2083 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
2084 }
2085}
2086
William Lallemand3b5a3a62022-03-29 14:29:31 +02002087/* Replace a ckch_store in the ckch tree and insert the whole dependencies,
2088* then free the previous dependencies and store.
2089* Used in the case of a certificate update.
2090*
2091* Every dependencies must allocated before using this function.
2092*
2093* This function can't fail as it only update pointers, and does not alloc anything.
2094*
2095* /!\ This function must be used under the ckch lock. /!\
2096*
2097* - Insert every dependencies (SNI, crtlist_entry, ckch_inst, etc)
2098* - Delete the old ckch_store from the tree
2099* - Insert the new ckch_store
2100* - Free the old dependencies and the old ckch_store
2101*/
2102void ckch_store_replace(struct ckch_store *old_ckchs, struct ckch_store *new_ckchs)
2103{
2104 struct crtlist_entry *entry;
2105 struct ckch_inst *ckchi, *ckchis;
2106
2107 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
2108 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
2109 ebpt_delete(&entry->node);
2110 /* change the ptr and reinsert the node */
2111 entry->node.key = new_ckchs;
2112 ebpt_insert(&entry->crtlist->entries, &entry->node);
2113 }
2114 /* insert the new ckch_insts in the crtlist_entry */
2115 list_for_each_entry(ckchi, &new_ckchs->ckch_inst, by_ckchs) {
2116 if (ckchi->crtlist_entry)
2117 LIST_INSERT(&ckchi->crtlist_entry->ckch_inst, &ckchi->by_crtlist_entry);
2118 }
2119 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2120 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
2121 __ssl_sock_load_new_ckch_instance(ckchi);
2122 }
2123 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
2124 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
2125 __ckch_inst_free_locked(ckchi);
2126 }
2127
2128 ckch_store_free(old_ckchs);
2129 ebst_insert(&ckchs_tree, &new_ckchs->node);
2130}
2131
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002132
William Lallemandda8584c2020-05-14 10:14:37 +02002133/*
2134 * This function tries to create the new ckch_inst and their SNIs
William Lallemand30fcca12022-03-30 12:03:12 +02002135 *
2136 * /!\ don't forget to update __hlua_ckch_commit() if you changes things there. /!\
William Lallemandda8584c2020-05-14 10:14:37 +02002137 */
2138static int cli_io_handler_commit_cert(struct appctx *appctx)
2139{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002140 struct commit_cert_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002141 struct stconn *sc = appctx_sc(appctx);
William Lallemandda8584c2020-05-14 10:14:37 +02002142 int y = 0;
William Lallemandda8584c2020-05-14 10:14:37 +02002143 struct ckch_store *old_ckchs, *new_ckchs = NULL;
William Lallemand3b5a3a62022-03-29 14:29:31 +02002144 struct ckch_inst *ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002145
Willy Tarreau475e4632022-05-27 10:26:46 +02002146 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Christopher Faulet9d56e242022-05-31 16:37:01 +02002147 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02002148
2149 while (1) {
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002150 switch (ctx->state) {
2151 case CERT_ST_INIT:
William Lallemandda8584c2020-05-14 10:14:37 +02002152 /* This state just print the update message */
Christopher Faulet9d56e242022-05-31 16:37:01 +02002153 chunk_printf(&trash, "Committing %s", ckchs_transaction.path);
2154 if (applet_putchk(appctx, &trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02002155 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002156
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002157 ctx->state = CERT_ST_GEN;
William Lallemandda8584c2020-05-14 10:14:37 +02002158 /* fallthrough */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002159 case CERT_ST_GEN:
William Lallemandda8584c2020-05-14 10:14:37 +02002160 /*
2161 * This state generates the ckch instances with their
2162 * sni_ctxs and SSL_CTX.
2163 *
2164 * Since the SSL_CTX generation can be CPU consumer, we
2165 * yield every 10 instances.
2166 */
2167
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002168 old_ckchs = ctx->old_ckchs;
2169 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002170
William Lallemandda8584c2020-05-14 10:14:37 +02002171 /* get the next ckchi to regenerate */
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002172 ckchi = ctx->next_ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002173 /* we didn't start yet, set it to the first elem */
2174 if (ckchi == NULL)
2175 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
2176
2177 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
2178 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
2179 struct ckch_inst *new_inst;
William Lallemandda8584c2020-05-14 10:14:37 +02002180
Christopher Faulet9d56e242022-05-31 16:37:01 +02002181 /* save the next ckchi to compute in case of yield */
2182 ctx->next_ckchi = ckchi;
2183
William Lallemandda8584c2020-05-14 10:14:37 +02002184 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2185 if (y >= 10) {
Christopher Faulet9d56e242022-05-31 16:37:01 +02002186 applet_have_more_data(appctx); /* let's come back later */
William Lallemandda8584c2020-05-14 10:14:37 +02002187 goto yield;
2188 }
2189
Christopher Faulet9d56e242022-05-31 16:37:01 +02002190 /* display one dot per new instance */
2191 if (applet_putstr(appctx, ".") == -1)
2192 goto yield;
2193
2194 ctx->err = NULL;
2195 if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &ctx->err)) {
2196 ctx->state = CERT_ST_ERROR;
William Lallemandda8584c2020-05-14 10:14:37 +02002197 goto error;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002198 }
William Lallemandda8584c2020-05-14 10:14:37 +02002199
William Lallemandda8584c2020-05-14 10:14:37 +02002200 /* link the new ckch_inst to the duplicate */
Willy Tarreau2b718102021-04-21 07:32:39 +02002201 LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002202 y++;
2203 }
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002204 ctx->state = CERT_ST_INSERT;
William Lallemandda8584c2020-05-14 10:14:37 +02002205 /* fallthrough */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002206 case CERT_ST_INSERT:
William Lallemandda8584c2020-05-14 10:14:37 +02002207 /* The generation is finished, we can insert everything */
2208
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002209 old_ckchs = ctx->old_ckchs;
2210 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002211
William Lallemand3b5a3a62022-03-29 14:29:31 +02002212 /* insert everything and remove the previous objects */
2213 ckch_store_replace(old_ckchs, new_ckchs);
Christopher Faulet9d56e242022-05-31 16:37:01 +02002214 ctx->new_ckchs = ctx->old_ckchs = NULL;
2215 ctx->state = CERT_ST_SUCCESS;
2216 /* fallthrough */
2217 case CERT_ST_SUCCESS:
2218 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2219 goto yield;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002220 ctx->state = CERT_ST_FIN;
William Lallemandda8584c2020-05-14 10:14:37 +02002221 /* fallthrough */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002222 case CERT_ST_FIN:
William Lallemandda8584c2020-05-14 10:14:37 +02002223 /* we achieved the transaction, we can set everything to NULL */
William Lallemandda8584c2020-05-14 10:14:37 +02002224 ckchs_transaction.new_ckchs = NULL;
2225 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002226 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002227 goto end;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002228
2229 case CERT_ST_ERROR:
2230 error:
2231 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2232 if (applet_putchk(appctx, &trash) == -1)
2233 goto yield;
2234 ctx->state = CERT_ST_FIN;
2235 break;
William Lallemandda8584c2020-05-14 10:14:37 +02002236 }
2237 }
2238end:
William Lallemandda8584c2020-05-14 10:14:37 +02002239 /* success: call the release function and don't come back */
2240 return 1;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002241
William Lallemandda8584c2020-05-14 10:14:37 +02002242yield:
William Lallemandda8584c2020-05-14 10:14:37 +02002243 return 0; /* should come back */
William Lallemandda8584c2020-05-14 10:14:37 +02002244}
2245
2246/*
2247 * Parsing function of 'commit ssl cert'
2248 */
2249static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
2250{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002251 struct commit_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02002252 char *err = NULL;
2253
2254 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2255 return 1;
2256
2257 if (!*args[3])
2258 return cli_err(appctx, "'commit ssl cert expects a filename\n");
2259
2260 /* The operations on the CKCH architecture are locked so we can
2261 * manipulate ckch_store and ckch_inst */
2262 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2263 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
2264
2265 if (!ckchs_transaction.path) {
2266 memprintf(&err, "No ongoing transaction! !\n");
2267 goto error;
2268 }
2269
2270 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2271 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
2272 goto error;
2273 }
2274
William Lallemand5685ccf2020-09-16 16:12:25 +02002275 /* if a certificate is here, a private key must be here too */
2276 if (ckchs_transaction.new_ckchs->ckch->cert && !ckchs_transaction.new_ckchs->ckch->key) {
2277 memprintf(&err, "The transaction must contain at least a certificate and a private key!\n");
2278 goto error;
2279 }
William Lallemanda9419522020-06-24 16:26:41 +02002280
William Lallemand5685ccf2020-09-16 16:12:25 +02002281 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
2282 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
2283 goto error;
William Lallemandda8584c2020-05-14 10:14:37 +02002284 }
2285
2286 /* init the appctx structure */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002287 ctx->state = CERT_ST_INIT;
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002288 ctx->next_ckchi = NULL;
2289 ctx->new_ckchs = ckchs_transaction.new_ckchs;
2290 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002291
2292 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
2293 return 0;
2294
2295error:
2296
2297 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2298 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2299
2300 return cli_dynerr(appctx, err);
2301}
2302
2303
2304
2305
2306/*
2307 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
Willy Tarreau329f4b42022-05-04 20:05:55 +02002308 * It uses a set_cert_ctx context, and ckchs_transaction under a lock.
William Lallemandda8584c2020-05-14 10:14:37 +02002309 */
2310static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
2311{
2312 struct ckch_store *new_ckchs = NULL;
2313 struct ckch_store *old_ckchs = NULL;
2314 char *err = NULL;
2315 int i;
William Lallemandda8584c2020-05-14 10:14:37 +02002316 int errcode = 0;
2317 char *end;
William Lallemandff8bf982022-03-29 10:44:23 +02002318 struct cert_exts *cert_ext = &cert_exts[0]; /* default one, PEM */
William Lallemandda8584c2020-05-14 10:14:37 +02002319 struct cert_key_and_chain *ckch;
2320 struct buffer *buf;
2321
2322 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2323 return 1;
2324
William Lallemandda8584c2020-05-14 10:14:37 +02002325 if (!*args[3] || !payload)
2326 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
2327
2328 /* The operations on the CKCH architecture are locked so we can
2329 * manipulate ckch_store and ckch_inst */
2330 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2331 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
2332
William Lallemand5ba80d62021-05-04 16:17:27 +02002333 if ((buf = alloc_trash_chunk()) == NULL) {
2334 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2335 errcode |= ERR_ALERT | ERR_FATAL;
2336 goto end;
2337 }
William Lallemande5ff4ad2020-06-08 09:40:37 +02002338
William Lallemandda8584c2020-05-14 10:14:37 +02002339 if (!chunk_strcpy(buf, args[3])) {
2340 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2341 errcode |= ERR_ALERT | ERR_FATAL;
2342 goto end;
2343 }
2344
2345 /* check which type of file we want to update */
William Lallemandff8bf982022-03-29 10:44:23 +02002346 for (i = 0; cert_exts[i].ext != NULL; i++) {
William Lallemandda8584c2020-05-14 10:14:37 +02002347 end = strrchr(buf->area, '.');
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002348 if (end && *cert_exts[i].ext && (strcmp(end + 1, cert_exts[i].ext) == 0)) {
William Lallemandda8584c2020-05-14 10:14:37 +02002349 *end = '\0';
William Lallemand089c1382020-10-23 17:35:12 +02002350 buf->data = strlen(buf->area);
William Lallemandff8bf982022-03-29 10:44:23 +02002351 cert_ext = &cert_exts[i];
William Lallemandda8584c2020-05-14 10:14:37 +02002352 break;
2353 }
2354 }
2355
William Lallemandda8584c2020-05-14 10:14:37 +02002356 /* if there is an ongoing transaction */
2357 if (ckchs_transaction.path) {
William Lallemandda8584c2020-05-14 10:14:37 +02002358 /* if there is an ongoing transaction, check if this is the same file */
2359 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
William Lallemand089c1382020-10-23 17:35:12 +02002360 /* we didn't find the transaction, must try more cases below */
2361
2362 /* 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 +02002363 if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
William Lallemand089c1382020-10-23 17:35:12 +02002364 if (!chunk_strcat(buf, ".crt")) {
2365 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2366 errcode |= ERR_ALERT | ERR_FATAL;
2367 goto end;
2368 }
2369
2370 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
2371 /* remove .crt of the error message */
2372 *(b_orig(buf) + b_data(buf) + strlen(".crt")) = '\0';
2373 b_sub(buf, strlen(".crt"));
2374
2375 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
2376 errcode |= ERR_ALERT | ERR_FATAL;
2377 goto end;
2378 }
2379 }
William Lallemandda8584c2020-05-14 10:14:37 +02002380 }
2381
Christopher Faulet24a20b92022-06-03 11:50:40 +02002382 old_ckchs = ckchs_transaction.new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002383
2384 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002385
William Lallemand95fefa12020-09-09 12:01:33 +02002386 /* lookup for the certificate in the tree */
Christopher Faulet24a20b92022-06-03 11:50:40 +02002387 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002388
Christopher Faulet24a20b92022-06-03 11:50:40 +02002389 if (!old_ckchs) {
William Lallemand089c1382020-10-23 17:35:12 +02002390 /* 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 +02002391 if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
William Lallemand089c1382020-10-23 17:35:12 +02002392 if (!chunk_strcat(buf, ".crt")) {
2393 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2394 errcode |= ERR_ALERT | ERR_FATAL;
2395 goto end;
2396 }
Christopher Faulet24a20b92022-06-03 11:50:40 +02002397 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002398 }
2399 }
William Lallemandda8584c2020-05-14 10:14:37 +02002400 }
2401
Christopher Faulet24a20b92022-06-03 11:50:40 +02002402 if (!old_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02002403 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
2404 err ? err : "");
2405 errcode |= ERR_ALERT | ERR_FATAL;
2406 goto end;
2407 }
2408
William Lallemandda8584c2020-05-14 10:14:37 +02002409 /* duplicate the ckch store */
2410 new_ckchs = ckchs_dup(old_ckchs);
2411 if (!new_ckchs) {
2412 memprintf(&err, "%sCannot allocate memory!\n",
2413 err ? err : "");
2414 errcode |= ERR_ALERT | ERR_FATAL;
2415 goto end;
2416 }
2417
William Lallemand95fefa12020-09-09 12:01:33 +02002418 ckch = new_ckchs->ckch;
William Lallemandda8584c2020-05-14 10:14:37 +02002419
2420 /* appply the change on the duplicate */
William Lallemandff8bf982022-03-29 10:44:23 +02002421 if (cert_ext->load(buf->area, payload, ckch, &err) != 0) {
William Lallemandda8584c2020-05-14 10:14:37 +02002422 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
2423 errcode |= ERR_ALERT | ERR_FATAL;
2424 goto end;
2425 }
2426
William Lallemandda8584c2020-05-14 10:14:37 +02002427 /* we succeed, we can save the ckchs in the transaction */
2428
2429 /* if there wasn't a transaction, update the old ckchs */
2430 if (!ckchs_transaction.old_ckchs) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002431 ckchs_transaction.old_ckchs = old_ckchs;
2432 ckchs_transaction.path = old_ckchs->path;
William Lallemandda8584c2020-05-14 10:14:37 +02002433 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
2434 } else {
2435 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
2436
2437 }
2438
2439 /* free the previous ckchs if there was a transaction */
2440 ckch_store_free(ckchs_transaction.new_ckchs);
2441
Christopher Faulet24a20b92022-06-03 11:50:40 +02002442 ckchs_transaction.new_ckchs = new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002443
2444
2445 /* creates the SNI ctxs later in the IO handler */
2446
2447end:
2448 free_trash_chunk(buf);
2449
2450 if (errcode & ERR_CODE) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002451 ckch_store_free(new_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002452 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2453 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2454 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002455 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2456 return cli_dynmsg(appctx, LOG_NOTICE, err);
2457 }
2458 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
2459}
2460
2461/* parsing function of 'abort ssl cert' */
2462static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
2463{
2464 char *err = NULL;
2465
2466 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2467 return 1;
2468
2469 if (!*args[3])
2470 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
2471
2472 /* The operations on the CKCH architecture are locked so we can
2473 * manipulate ckch_store and ckch_inst */
2474 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2475 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2476
2477 if (!ckchs_transaction.path) {
2478 memprintf(&err, "No ongoing transaction!\n");
2479 goto error;
2480 }
2481
2482 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2483 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
2484 goto error;
2485 }
2486
2487 /* Only free the ckchs there, because the SNI and instances were not generated yet */
2488 ckch_store_free(ckchs_transaction.new_ckchs);
2489 ckchs_transaction.new_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002490 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002491 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002492
2493 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2494
2495 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2496 return cli_dynmsg(appctx, LOG_NOTICE, err);
2497
2498error:
2499 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2500
2501 return cli_dynerr(appctx, err);
2502}
2503
2504/* parsing function of 'new ssl cert' */
2505static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
2506{
2507 struct ckch_store *store;
2508 char *err = NULL;
2509 char *path;
2510
2511 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2512 return 1;
2513
2514 if (!*args[3])
2515 return cli_err(appctx, "'new ssl cert' expects a filename\n");
2516
2517 path = args[3];
2518
2519 /* The operations on the CKCH architecture are locked so we can
2520 * manipulate ckch_store and ckch_inst */
2521 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2522 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
2523
2524 store = ckchs_lookup(path);
2525 if (store != NULL) {
2526 memprintf(&err, "Certificate '%s' already exists!\n", path);
2527 store = NULL; /* we don't want to free it */
2528 goto error;
2529 }
2530 /* we won't support multi-certificate bundle here */
William Lallemandbd8e6ed2020-09-16 16:08:08 +02002531 store = ckch_store_new(path);
William Lallemandda8584c2020-05-14 10:14:37 +02002532 if (!store) {
2533 memprintf(&err, "unable to allocate memory.\n");
2534 goto error;
2535 }
2536
2537 /* insert into the ckchs tree */
2538 ebst_insert(&ckchs_tree, &store->node);
2539 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
2540
2541 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2542 return cli_dynmsg(appctx, LOG_NOTICE, err);
2543error:
2544 free(store);
2545 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2546 return cli_dynerr(appctx, err);
2547}
2548
2549/* parsing function of 'del ssl cert' */
2550static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
2551{
2552 struct ckch_store *store;
2553 char *err = NULL;
2554 char *filename;
2555
2556 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2557 return 1;
2558
2559 if (!*args[3])
2560 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
2561
2562 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2563 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
2564
2565 filename = args[3];
2566
Christopher Faulet926fefc2022-05-31 18:04:25 +02002567 if (ckchs_transaction.path && strcmp(ckchs_transaction.path, filename) == 0) {
2568 memprintf(&err, "ongoing transaction for the certificate '%s'", filename);
2569 goto error;
2570 }
2571
William Lallemandda8584c2020-05-14 10:14:37 +02002572 store = ckchs_lookup(filename);
2573 if (store == NULL) {
2574 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
2575 goto error;
2576 }
2577 if (!LIST_ISEMPTY(&store->ckch_inst)) {
2578 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
2579 goto error;
2580 }
2581
2582 ebmb_delete(&store->node);
2583 ckch_store_free(store);
2584
2585 memprintf(&err, "Certificate '%s' deleted!\n", filename);
2586
2587 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2588 return cli_dynmsg(appctx, LOG_NOTICE, err);
2589
2590error:
2591 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
2592 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2593 return cli_dynerr(appctx, err);
2594}
2595
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002596
Remi Tricot-Le Breton9f40fe02021-03-16 16:21:27 +01002597
2598/* parsing function of 'new ssl ca-file' */
2599static int cli_parse_new_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2600{
2601 struct cafile_entry *cafile_entry;
2602 char *err = NULL;
2603 char *path;
2604
2605 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2606 return 1;
2607
2608 if (!*args[3])
2609 return cli_err(appctx, "'new ssl ca-file' expects a filename\n");
2610
2611 path = args[3];
2612
2613 /* The operations on the CKCH architecture are locked so we can
2614 * manipulate ckch_store and ckch_inst */
2615 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2616 return cli_err(appctx, "Can't create a CA file!\nOperations on certificates are currently locked!\n");
2617
2618 cafile_entry = ssl_store_get_cafile_entry(path, 0);
2619 if (cafile_entry) {
2620 memprintf(&err, "CA file '%s' already exists!\n", path);
2621 goto error;
2622 }
2623
2624 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CERT);
2625 if (!cafile_entry) {
2626 memprintf(&err, "%sCannot allocate memory!\n",
2627 err ? err : "");
2628 goto error;
2629 }
2630
2631 /* Add the newly created cafile_entry to the tree so that
2632 * any new ckch instance created from now can use it. */
2633 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
2634 goto error;
2635
2636 memprintf(&err, "New CA file created '%s'!\n", path);
2637
2638 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2639 return cli_dynmsg(appctx, LOG_NOTICE, err);
2640error:
2641 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2642 return cli_dynerr(appctx, err);
2643}
2644
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002645/*
2646 * Parsing function of `set ssl ca-file`
2647 */
2648static int cli_parse_set_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2649{
Christopher Faulet132c5952022-06-03 11:56:26 +02002650 struct cafile_entry *old_cafile_entry = NULL;
2651 struct cafile_entry *new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002652 char *err = NULL;
2653 int errcode = 0;
2654 struct buffer *buf;
William Lallemand62c0b992022-07-29 17:50:58 +02002655 int add_cmd = 0;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002656
2657 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2658 return 1;
2659
William Lallemand62c0b992022-07-29 17:50:58 +02002660 /* this is "add ssl ca-file" */
2661 if (*args[0] == 'a')
2662 add_cmd = 1;
2663
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002664 if (!*args[3] || !payload)
2665 return cli_err(appctx, "'set ssl ca-file expects a filename and CAs as a payload\n");
2666
2667 /* The operations on the CKCH architecture are locked so we can
2668 * manipulate ckch_store and ckch_inst */
2669 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2670 return cli_err(appctx, "Can't update the CA file!\nOperations on certificates are currently locked!\n");
2671
2672 if ((buf = alloc_trash_chunk()) == NULL) {
2673 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2674 errcode |= ERR_ALERT | ERR_FATAL;
2675 goto end;
2676 }
2677
2678 if (!chunk_strcpy(buf, args[3])) {
2679 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2680 errcode |= ERR_ALERT | ERR_FATAL;
2681 goto end;
2682 }
2683
Christopher Faulet132c5952022-06-03 11:56:26 +02002684 old_cafile_entry = NULL;
2685 new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002686
2687 /* if there is an ongoing transaction */
2688 if (cafile_transaction.path) {
2689 /* if there is an ongoing transaction, check if this is the same file */
2690 if (strcmp(cafile_transaction.path, buf->area) != 0) {
2691 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, buf->area);
2692 errcode |= ERR_ALERT | ERR_FATAL;
2693 goto end;
2694 }
Christopher Faulet132c5952022-06-03 11:56:26 +02002695 old_cafile_entry = cafile_transaction.old_cafile_entry;
William Lallemand62c0b992022-07-29 17:50:58 +02002696 } else {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002697 /* lookup for the certificate in the tree */
Christopher Faulet132c5952022-06-03 11:56:26 +02002698 old_cafile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002699 }
2700
Christopher Faulet132c5952022-06-03 11:56:26 +02002701 if (!old_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002702 memprintf(&err, "%sCan't replace a CA file which is not referenced by the configuration!\n",
2703 err ? err : "");
2704 errcode |= ERR_ALERT | ERR_FATAL;
2705 goto end;
2706 }
2707
William Lallemand62c0b992022-07-29 17:50:58 +02002708 /* if the transaction is new, duplicate the old_ca_file_entry, otherwise duplicate the cafile in the current transaction */
2709 if (cafile_transaction.new_cafile_entry)
2710 new_cafile_entry = ssl_store_dup_cafile_entry(cafile_transaction.new_cafile_entry);
2711 else
2712 new_cafile_entry = ssl_store_dup_cafile_entry(old_cafile_entry);
2713
Christopher Faulet132c5952022-06-03 11:56:26 +02002714 if (!new_cafile_entry) {
William Lallemand62c0b992022-07-29 17:50:58 +02002715 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002716 errcode |= ERR_ALERT | ERR_FATAL;
2717 goto end;
2718 }
2719
William Lallemand62c0b992022-07-29 17:50:58 +02002720 /* Fill the new entry with the new CAs. The add_cmd variable determine
2721 if we flush the X509_STORE or not */
2722 if (ssl_store_load_ca_from_buf(new_cafile_entry, payload, add_cmd)) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002723 memprintf(&err, "%sInvalid payload\n", err ? err : "");
2724 errcode |= ERR_ALERT | ERR_FATAL;
2725 goto end;
2726 }
2727
2728 /* we succeed, we can save the ca in the transaction */
2729
2730 /* if there wasn't a transaction, update the old CA */
2731 if (!cafile_transaction.old_cafile_entry) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002732 cafile_transaction.old_cafile_entry = old_cafile_entry;
2733 cafile_transaction.path = old_cafile_entry->path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002734 err = memprintf(&err, "transaction created for CA %s!\n", cafile_transaction.path);
2735 } else {
2736 err = memprintf(&err, "transaction updated for CA %s!\n", cafile_transaction.path);
2737 }
2738
2739 /* free the previous CA if there was a transaction */
2740 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2741
Christopher Faulet132c5952022-06-03 11:56:26 +02002742 cafile_transaction.new_cafile_entry = new_cafile_entry;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002743
2744 /* creates the SNI ctxs later in the IO handler */
2745
2746end:
2747 free_trash_chunk(buf);
2748
2749 if (errcode & ERR_CODE) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002750 ssl_store_delete_cafile_entry(new_cafile_entry);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002751 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2752 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2753 } else {
2754
2755 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2756 return cli_dynmsg(appctx, LOG_NOTICE, err);
2757 }
2758}
2759
2760
2761/*
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002762 * Parsing function of 'commit ssl ca-file'.
2763 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl crl-file".
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002764 */
2765static int cli_parse_commit_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2766{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002767 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002768 char *err = NULL;
2769
2770 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2771 return 1;
2772
2773 if (!*args[3])
2774 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
2775
2776 /* The operations on the CKCH architecture are locked so we can
2777 * manipulate ckch_store and ckch_inst */
2778 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2779 return cli_err(appctx, "Can't commit the CA file!\nOperations on certificates are currently locked!\n");
2780
2781 if (!cafile_transaction.path) {
2782 memprintf(&err, "No ongoing transaction! !\n");
2783 goto error;
2784 }
2785
2786 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2787 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, args[3]);
2788 goto error;
2789 }
2790 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002791 ctx->state = CACRL_ST_INIT;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002792 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002793 ctx->old_entry = cafile_transaction.old_cafile_entry;
2794 ctx->new_entry = cafile_transaction.new_cafile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002795 ctx->cafile_type = CAFILE_CERT;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002796
2797 return 0;
2798
2799error:
2800
2801 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2802 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2803
2804 return cli_dynerr(appctx, err);
2805}
2806
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002807/*
2808 * This function tries to create new ckch instances and their SNIs using a newly
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002809 * set certificate authority (CA file) or a newly set Certificate Revocation
2810 * List (CRL), depending on the command being called.
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002811 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002812static int cli_io_handler_commit_cafile_crlfile(struct appctx *appctx)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002813{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002814 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002815 struct stconn *sc = appctx_sc(appctx);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002816 int y = 0;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002817 struct cafile_entry *old_cafile_entry = ctx->old_entry;
2818 struct cafile_entry *new_cafile_entry = ctx->new_entry;
William Lallemand0bfa3e72022-08-30 17:32:38 +02002819 struct ckch_inst_link *ckchi_link;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002820 char *path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002821
Willy Tarreau475e4632022-05-27 10:26:46 +02002822 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002823 goto end;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002824
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002825 /* The ctx was already validated by the ca-file/crl-file parsing
2826 * function. Entries can only be NULL in CACRL_ST_SUCCESS or
2827 * CACRL_ST_FIN states
2828 */
2829 switch (ctx->cafile_type) {
2830 case CAFILE_CERT:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002831 path = cafile_transaction.path;
2832 break;
2833 case CAFILE_CRL:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002834 path = crlfile_transaction.path;
2835 break;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002836 default:
Willy Tarreaud543ae02022-06-22 05:40:25 +02002837 path = NULL;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002838 goto error;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002839 }
2840
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002841 while (1) {
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002842 switch (ctx->state) {
2843 case CACRL_ST_INIT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002844 /* This state just print the update message */
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002845 chunk_printf(&trash, "Committing %s", path);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002846 if (applet_putchk(appctx, &trash) == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002847 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002848
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002849 ctx->state = CACRL_ST_GEN;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002850 /* fallthrough */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002851 case CACRL_ST_GEN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002852 /*
2853 * This state generates the ckch instances with their
2854 * sni_ctxs and SSL_CTX.
2855 *
2856 * Since the SSL_CTX generation can be CPU consumer, we
2857 * yield every 10 instances.
2858 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002859
2860 /* get the next ckchi to regenerate */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002861 ckchi_link = ctx->next_ckchi_link;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002862
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002863 /* we didn't start yet, set it to the first elem */
2864 if (ckchi_link == NULL) {
2865 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2866 /* Add the newly created cafile_entry to the tree so that
2867 * any new ckch instance created from now can use it. */
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002868 if (ssl_store_add_uncommitted_cafile_entry(new_cafile_entry)) {
2869 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002870 goto error;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002871 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002872 }
2873
2874 list_for_each_entry_from(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002875 struct ckch_inst *new_inst;
2876
2877 /* save the next ckchi to compute */
2878 ctx->next_ckchi_link = ckchi_link;
2879
2880 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2881 if (y >= 10) {
2882 applet_have_more_data(appctx); /* let's come back later */
2883 goto yield;
2884 }
2885
2886 /* display one dot per new instance */
2887 if (applet_putstr(appctx, ".") == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002888 goto yield;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002889
2890 /* Rebuild a new ckch instance that uses the same ckch_store
2891 * than a reference ckchi instance but will use a new CA file. */
2892 ctx->err = NULL;
2893 if (ckch_inst_rebuild(ckchi_link->ckch_inst->ckch_store, ckchi_link->ckch_inst, &new_inst, &ctx->err)) {
2894 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002895 goto error;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002896 }
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002897
2898 y++;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002899 }
2900
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002901 ctx->state = CACRL_ST_INSERT;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002902 /* fallthrough */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002903 case CACRL_ST_INSERT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002904 /* The generation is finished, we can insert everything */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002905
2906 /* insert the new ckch_insts in the crtlist_entry */
2907 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2908 if (ckchi_link->ckch_inst->crtlist_entry)
2909 LIST_INSERT(&ckchi_link->ckch_inst->crtlist_entry->ckch_inst,
2910 &ckchi_link->ckch_inst->by_crtlist_entry);
2911 }
2912
2913 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2914 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2915 __ssl_sock_load_new_ckch_instance(ckchi_link->ckch_inst);
2916 }
2917
William Lallemande0fa91f2022-08-31 14:26:49 +02002918 /* delete the old sni_ctx, the old ckch_insts
2919 * and the ckch_store. ckch_inst_free() also
2920 * manipulates the list so it's cleaner to loop
2921 * until it's empty */
2922 while (!LIST_ISEMPTY(&old_cafile_entry->ckch_inst_link)) {
2923 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2924
2925 LIST_DEL_INIT(&ckchi_link->list); /* must reinit because ckch_inst checks the list */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002926 __ckch_inst_free_locked(ckchi_link->ckch_inst);
William Lallemande0fa91f2022-08-31 14:26:49 +02002927 free(ckchi_link);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002928 }
2929
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002930 /* Remove the old cafile entry from the tree */
2931 ebmb_delete(&old_cafile_entry->node);
2932 ssl_store_delete_cafile_entry(old_cafile_entry);
2933
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002934 ctx->old_entry = ctx->new_entry = NULL;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002935 ctx->state = CACRL_ST_SUCCESS;
2936 /* fallthrough */
2937 case CACRL_ST_SUCCESS:
2938 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2939 goto yield;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002940 ctx->state = CACRL_ST_FIN;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002941 /* fallthrough */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002942 case CACRL_ST_FIN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002943 /* we achieved the transaction, we can set everything to NULL */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002944 switch (ctx->cafile_type) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002945 case CAFILE_CERT:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002946 cafile_transaction.old_cafile_entry = NULL;
2947 cafile_transaction.new_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002948 cafile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002949 break;
2950 case CAFILE_CRL:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002951 crlfile_transaction.old_crlfile_entry = NULL;
2952 crlfile_transaction.new_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002953 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002954 break;
2955 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002956 goto end;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002957
2958 case CACRL_ST_ERROR:
2959 error:
2960 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2961 if (applet_putchk(appctx, &trash) == -1)
2962 goto yield;
2963 ctx->state = CACRL_ST_FIN;
2964 break;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002965 }
2966 }
2967end:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002968 /* success: call the release function and don't come back */
2969 return 1;
2970yield:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002971 return 0; /* should come back */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002972}
2973
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002974
2975/* parsing function of 'abort ssl ca-file' */
2976static int cli_parse_abort_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2977{
2978 char *err = NULL;
2979
2980 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2981 return 1;
2982
2983 if (!*args[3])
2984 return cli_err(appctx, "'abort ssl ca-file' expects a filename\n");
2985
2986 /* The operations on the CKCH architecture are locked so we can
2987 * manipulate ckch_store and ckch_inst */
2988 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2989 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2990
2991 if (!cafile_transaction.path) {
2992 memprintf(&err, "No ongoing transaction!\n");
2993 goto error;
2994 }
2995
2996 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2997 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", cafile_transaction.path, args[3]);
2998 goto error;
2999 }
3000
3001 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3002 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
3003 cafile_transaction.new_cafile_entry = NULL;
3004 cafile_transaction.old_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02003005 cafile_transaction.path = NULL;
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01003006
3007 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3008
3009 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3010 return cli_dynmsg(appctx, LOG_NOTICE, err);
3011
3012error:
3013 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3014
3015 return cli_dynerr(appctx, err);
3016}
3017
Willy Tarreau821c3b02022-05-04 15:47:39 +02003018/* release function of the `commit ssl ca-file' command, free things and unlock the spinlock.
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003019 * It uses a commit_cacrlfile_ctx context.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003020 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003021static void cli_release_commit_cafile(struct appctx *appctx)
3022{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003023 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003024 struct cafile_entry *new_cafile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003025
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003026 /* Remove the uncommitted cafile_entry from the tree. */
3027 if (new_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003028 ebmb_delete(&new_cafile_entry->node);
3029 ssl_store_delete_cafile_entry(new_cafile_entry);
3030 }
3031 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003032 ha_free(&ctx->err);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003033}
3034
3035
Willy Tarreau821c3b02022-05-04 15:47:39 +02003036/* IO handler of details "show ssl ca-file <filename[:index]>".
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003037 * It uses a show_cafile_ctx context, and the global
3038 * cafile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003039 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003040static int cli_io_handler_show_cafile_detail(struct appctx *appctx)
3041{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003042 struct show_cafile_ctx *ctx = appctx->svcctx;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003043 struct cafile_entry *cafile_entry = ctx->cur_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003044 struct buffer *out = alloc_trash_chunk();
William Lallemand03a32e52022-04-26 18:17:15 +02003045 int i = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003046 X509 *cert;
3047 STACK_OF(X509_OBJECT) *objs;
3048 int retval = 0;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003049 int ca_index = ctx->ca_index;
3050 int show_all = ctx->show_all;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003051
3052 if (!out)
3053 goto end_no_putchk;
3054
3055 chunk_appendf(out, "Filename: ");
3056 if (cafile_entry == cafile_transaction.new_cafile_entry)
3057 chunk_appendf(out, "*");
3058 chunk_appendf(out, "%s\n", cafile_entry->path);
3059
3060 chunk_appendf(out, "Status: ");
3061 if (!cafile_entry->ca_store)
3062 chunk_appendf(out, "Empty\n");
3063 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3064 chunk_appendf(out, "Unused\n");
3065 else
3066 chunk_appendf(out, "Used\n");
3067
3068 if (!cafile_entry->ca_store)
3069 goto end;
3070
3071 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
William Lallemand03a32e52022-04-26 18:17:15 +02003072 for (i = ca_index; i < sk_X509_OBJECT_num(objs); i++) {
3073
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003074 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
3075 if (!cert)
3076 continue;
3077
William Lallemand03a32e52022-04-26 18:17:15 +02003078 /* file starts at line 1 */
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003079 chunk_appendf(out, " \nCertificate #%d:\n", i+1);
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003080 retval = show_cert_detail(cert, NULL, out);
3081 if (retval < 0)
3082 goto end_no_putchk;
William Lallemand03a32e52022-04-26 18:17:15 +02003083 else if (retval)
3084 goto yield;
3085
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003086 if (applet_putchk(appctx, out) == -1)
William Lallemand03a32e52022-04-26 18:17:15 +02003087 goto yield;
William Lallemand03a32e52022-04-26 18:17:15 +02003088
3089 if (!show_all) /* only need to dump one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003090 goto end;
3091 }
3092
3093end:
William Lallemand03a32e52022-04-26 18:17:15 +02003094 free_trash_chunk(out);
3095 return 1; /* end, don't come back */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003096
3097end_no_putchk:
3098 free_trash_chunk(out);
3099 return 1;
3100yield:
William Lallemand03a32e52022-04-26 18:17:15 +02003101 /* save the current state */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003102 ctx->ca_index = i;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003103 free_trash_chunk(out);
3104 return 0; /* should come back */
3105}
3106
3107
Willy Tarreau06305792022-05-04 15:57:30 +02003108/* parsing function for 'show ssl ca-file [cafile[:index]]'.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003109 * It prepares a show_cafile_ctx context, and checks the global
3110 * cafile_transaction under the ckch_lock (read only).
Willy Tarreau06305792022-05-04 15:57:30 +02003111 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003112static int cli_parse_show_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3113{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003114 struct show_cafile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003115 struct cafile_entry *cafile_entry;
William Lallemand03a32e52022-04-26 18:17:15 +02003116 int ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003117 char *colons;
3118 char *err = NULL;
3119
3120 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3121 return cli_err(appctx, "Can't allocate memory!\n");
3122
3123 /* The operations on the CKCH architecture are locked so we can
3124 * manipulate ckch_store and ckch_inst */
3125 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3126 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3127
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003128 ctx->show_all = 1; /* show all certificates */
3129 ctx->ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003130 /* check if there is a certificate to lookup */
3131 if (*args[3]) {
3132
3133 /* Look for an optional CA index after the CA file name */
3134 colons = strchr(args[3], ':');
3135 if (colons) {
3136 char *endptr;
3137
3138 ca_index = strtol(colons + 1, &endptr, 10);
3139 /* Indexes start at 1 */
3140 if (colons + 1 == endptr || *endptr != '\0' || ca_index <= 0) {
3141 memprintf(&err, "wrong CA index after colons in '%s'!", args[3]);
3142 goto error;
3143 }
3144 *colons = '\0';
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003145 ctx->ca_index = ca_index - 1; /* we start counting at 0 in the ca_store, but at 1 on the CLI */
3146 ctx->show_all = 0; /* show only one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003147 }
3148
3149 if (*args[3] == '*') {
3150 if (!cafile_transaction.new_cafile_entry)
3151 goto error;
3152
3153 cafile_entry = cafile_transaction.new_cafile_entry;
3154
3155 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3156 goto error;
3157
3158 } else {
3159 /* Get the "original" cafile_entry and not the
3160 * uncommitted one if it exists. */
3161 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CERT)
3162 goto error;
3163 }
3164
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003165 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003166 /* use the IO handler that shows details */
3167 appctx->io_handler = cli_io_handler_show_cafile_detail;
3168 }
3169
3170 return 0;
3171
3172error:
3173 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3174 if (err)
3175 return cli_dynerr(appctx, err);
3176 return cli_err(appctx, "Can't display the CA file : Not found!\n");
3177}
3178
3179
3180/* release function of the 'show ssl ca-file' command */
3181static void cli_release_show_cafile(struct appctx *appctx)
3182{
3183 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3184}
3185
3186
3187/* This function returns the number of certificates in a cafile_entry. */
3188static int get_certificate_count(struct cafile_entry *cafile_entry)
3189{
3190 int cert_count = 0;
3191 STACK_OF(X509_OBJECT) *objs;
3192
3193 if (cafile_entry && cafile_entry->ca_store) {
3194 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3195 if (objs)
3196 cert_count = sk_X509_OBJECT_num(objs);
3197 }
3198 return cert_count;
3199}
3200
3201/* IO handler of "show ssl ca-file". The command taking a specific CA file name
Willy Tarreau821c3b02022-05-04 15:47:39 +02003202 * is managed in cli_io_handler_show_cafile_detail.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003203 * It uses a show_cafile_ctx and the global cafile_transaction.new_cafile_entry
3204 * in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003205 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003206static int cli_io_handler_show_cafile(struct appctx *appctx)
3207{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003208 struct show_cafile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003209 struct buffer *trash = alloc_trash_chunk();
3210 struct ebmb_node *node;
Christopher Faulet677cb4f2022-06-03 16:25:35 +02003211 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003212
3213 if (trash == NULL)
3214 return 1;
3215
Christopher Faulet5a2154b2022-06-03 10:42:48 +02003216 if (!ctx->old_cafile_entry && cafile_transaction.old_cafile_entry) {
3217 chunk_appendf(trash, "# transaction\n");
3218 chunk_appendf(trash, "*%s", cafile_transaction.old_cafile_entry->path);
3219 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_transaction.new_cafile_entry));
3220 if (applet_putchk(appctx, trash) == -1)
3221 goto yield;
3222 ctx->old_cafile_entry = cafile_transaction.new_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003223 }
3224
3225 /* First time in this io_handler. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003226 if (!ctx->cur_cafile_entry) {
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003227 chunk_appendf(trash, "# filename\n");
3228 node = ebmb_first(&cafile_tree);
3229 } else {
3230 /* We yielded during a previous call. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003231 node = &ctx->cur_cafile_entry->node;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003232 }
3233
3234 while (node) {
3235 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3236 if (cafile_entry->type == CAFILE_CERT) {
3237 chunk_appendf(trash, "%s", cafile_entry->path);
3238
3239 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_entry));
3240 }
3241
3242 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003243 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003244 goto yield;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003245 }
3246
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003247 ctx->cur_cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003248 free_trash_chunk(trash);
3249 return 1;
3250yield:
3251
3252 free_trash_chunk(trash);
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003253 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003254 return 0; /* should come back */
3255}
3256
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003257/* parsing function of 'del ssl ca-file' */
3258static int cli_parse_del_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3259{
3260 struct cafile_entry *cafile_entry;
3261 char *err = NULL;
3262 char *filename;
3263
3264 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3265 return 1;
3266
3267 if (!*args[3])
3268 return cli_err(appctx, "'del ssl ca-file' expects a CA file name\n");
3269
3270 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3271 return cli_err(appctx, "Can't delete the CA file!\nOperations on certificates are currently locked!\n");
3272
3273 filename = args[3];
3274
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003275 if (cafile_transaction.path && strcmp(cafile_transaction.path, filename) == 0) {
3276 memprintf(&err, "ongoing transaction for the CA file '%s'", filename);
3277 goto error;
3278 }
3279
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003280 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3281 if (!cafile_entry) {
3282 memprintf(&err, "CA file '%s' doesn't exist!\n", filename);
3283 goto error;
3284 }
3285
3286 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3287 memprintf(&err, "CA file '%s' in use, can't be deleted!\n", filename);
3288 goto error;
3289 }
3290
3291 /* Remove the cafile_entry from the tree */
3292 ebmb_delete(&cafile_entry->node);
3293 ssl_store_delete_cafile_entry(cafile_entry);
3294
3295 memprintf(&err, "CA file '%s' deleted!\n", filename);
3296
3297 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3298 return cli_dynmsg(appctx, LOG_NOTICE, err);
3299
3300error:
3301 memprintf(&err, "Can't remove the CA file: %s\n", err ? err : "");
3302 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3303 return cli_dynerr(appctx, err);
3304}
3305
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003306/* parsing function of 'new ssl crl-file' */
3307static int cli_parse_new_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3308{
3309 struct cafile_entry *cafile_entry;
3310 char *err = NULL;
3311 char *path;
3312
3313 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3314 return 1;
3315
3316 if (!*args[3])
3317 return cli_err(appctx, "'new ssl crl-file' expects a filename\n");
3318
3319 path = args[3];
3320
3321 /* The operations on the CKCH architecture are locked so we can
3322 * manipulate ckch_store and ckch_inst */
3323 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003324 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 +02003325
3326 cafile_entry = ssl_store_get_cafile_entry(path, 0);
3327 if (cafile_entry) {
3328 memprintf(&err, "CRL file '%s' already exists!\n", path);
3329 goto error;
3330 }
3331
3332 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CRL);
3333 if (!cafile_entry) {
3334 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3335 goto error;
3336 }
3337
3338 /* Add the newly created cafile_entry to the tree so that
3339 * any new ckch instance created from now can use it. */
3340 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
3341 goto error;
3342
3343 memprintf(&err, "New CRL file created '%s'!\n", path);
3344
3345 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3346 return cli_dynmsg(appctx, LOG_NOTICE, err);
3347error:
3348 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3349 return cli_dynerr(appctx, err);
3350}
3351
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003352/* Parsing function of `set ssl crl-file` */
3353static int cli_parse_set_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3354{
Christopher Faulet1f90f332022-06-03 16:34:30 +02003355 struct cafile_entry *old_crlfile_entry = NULL;
3356 struct cafile_entry *new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003357 char *err = NULL;
3358 int errcode = 0;
3359 struct buffer *buf;
3360
3361 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3362 return 1;
3363
3364 if (!*args[3] || !payload)
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003365 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 +02003366
3367 /* The operations on the CKCH architecture are locked so we can
3368 * manipulate ckch_store and ckch_inst */
3369 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3370 return cli_err(appctx, "Can't update the CRL file!\nOperations on certificates are currently locked!\n");
3371
3372 if ((buf = alloc_trash_chunk()) == NULL) {
3373 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3374 errcode |= ERR_ALERT | ERR_FATAL;
3375 goto end;
3376 }
3377
3378 if (!chunk_strcpy(buf, args[3])) {
3379 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3380 errcode |= ERR_ALERT | ERR_FATAL;
3381 goto end;
3382 }
3383
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003384 old_crlfile_entry = NULL;
3385 new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003386
3387 /* if there is an ongoing transaction */
3388 if (crlfile_transaction.path) {
3389 /* if there is an ongoing transaction, check if this is the same file */
3390 if (strcmp(crlfile_transaction.path, buf->area) != 0) {
3391 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, buf->area);
3392 errcode |= ERR_ALERT | ERR_FATAL;
3393 goto end;
3394 }
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003395 old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003396 }
3397 else {
3398 /* lookup for the certificate in the tree */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003399 old_crlfile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003400 }
3401
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003402 if (!old_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003403 memprintf(&err, "%sCan't replace a CRL file which is not referenced by the configuration!\n",
3404 err ? err : "");
3405 errcode |= ERR_ALERT | ERR_FATAL;
3406 goto end;
3407 }
3408
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003409 /* Create a new cafile_entry without adding it to the cafile tree. */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003410 new_crlfile_entry = ssl_store_create_cafile_entry(old_crlfile_entry->path, NULL, CAFILE_CRL);
3411 if (!new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003412 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3413 errcode |= ERR_ALERT | ERR_FATAL;
3414 goto end;
3415 }
3416
3417 /* Fill the new entry with the new CRL. */
William Lallemandd4774d32022-07-29 17:08:02 +02003418 if (ssl_store_load_ca_from_buf(new_crlfile_entry, payload, 0)) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003419 memprintf(&err, "%sInvalid payload\n", err ? err : "");
3420 errcode |= ERR_ALERT | ERR_FATAL;
3421 goto end;
3422 }
3423
3424 /* we succeed, we can save the crl in the transaction */
3425
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003426 /* if there wasn't a transaction, update the old CRL */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003427 if (!crlfile_transaction.old_crlfile_entry) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003428 crlfile_transaction.old_crlfile_entry = old_crlfile_entry;
3429 crlfile_transaction.path = old_crlfile_entry->path;
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003430 err = memprintf(&err, "transaction created for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003431 } else {
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003432 err = memprintf(&err, "transaction updated for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003433 }
3434
3435 /* free the previous CRL file if there was a transaction */
3436 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3437
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003438 crlfile_transaction.new_crlfile_entry = new_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003439
3440 /* creates the SNI ctxs later in the IO handler */
3441
3442end:
3443 free_trash_chunk(buf);
3444
3445 if (errcode & ERR_CODE) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003446 ssl_store_delete_cafile_entry(new_crlfile_entry);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003447 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3448 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
3449 } else {
3450
3451 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3452 return cli_dynmsg(appctx, LOG_NOTICE, err);
3453 }
3454}
3455
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003456/* Parsing function of 'commit ssl crl-file'.
3457 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl ca-file".
3458 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003459static int cli_parse_commit_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3460{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003461 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003462 char *err = NULL;
3463
3464 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3465 return 1;
3466
3467 if (!*args[3])
3468 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
3469
3470 /* The operations on the CKCH architecture are locked so we can
3471 * manipulate ckch_store and ckch_inst */
3472 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3473 return cli_err(appctx, "Can't commit the CRL file!\nOperations on certificates are currently locked!\n");
3474
3475 if (!crlfile_transaction.path) {
3476 memprintf(&err, "No ongoing transaction! !\n");
3477 goto error;
3478 }
3479
3480 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3481 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, args[3]);
3482 goto error;
3483 }
3484 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02003485 ctx->state = CACRL_ST_INIT;
Christopher Fauletf814c4a2022-06-03 11:32:05 +02003486 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003487 ctx->old_entry = crlfile_transaction.old_crlfile_entry;
3488 ctx->new_entry = crlfile_transaction.new_crlfile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003489 ctx->cafile_type = CAFILE_CRL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003490
3491 return 0;
3492
3493error:
3494
3495 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3496 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
3497
3498 return cli_dynerr(appctx, err);
3499}
3500
3501
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003502/* release function of the `commit ssl crl-file' command, free things and unlock the spinlock.
3503 * it uses a commit_cacrlfile_ctx that's the same as for "commit ssl ca-file".
3504 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003505static void cli_release_commit_crlfile(struct appctx *appctx)
3506{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003507 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003508 struct cafile_entry *new_crlfile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003509
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003510 /* Remove the uncommitted cafile_entry from the tree. */
3511 if (new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003512 ebmb_delete(&new_crlfile_entry->node);
3513 ssl_store_delete_cafile_entry(new_crlfile_entry);
3514 }
3515 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003516 ha_free(&ctx->err);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003517}
3518
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003519/* parsing function of 'del ssl crl-file' */
3520static int cli_parse_del_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3521{
3522 struct cafile_entry *cafile_entry;
3523 char *err = NULL;
3524 char *filename;
3525
3526 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3527 return 1;
3528
3529 if (!*args[3])
3530 return cli_err(appctx, "'del ssl crl-file' expects a CRL file name\n");
3531
3532 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3533 return cli_err(appctx, "Can't delete the CRL file!\nOperations on certificates are currently locked!\n");
3534
3535 filename = args[3];
3536
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003537 if (crlfile_transaction.path && strcmp(crlfile_transaction.path, filename) == 0) {
3538 memprintf(&err, "ongoing transaction for the CRL file '%s'", filename);
3539 goto error;
3540 }
3541
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003542 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3543 if (!cafile_entry) {
3544 memprintf(&err, "CRL file '%s' doesn't exist!\n", filename);
3545 goto error;
3546 }
3547 if (cafile_entry->type != CAFILE_CRL) {
3548 memprintf(&err, "'del ssl crl-file' does not work on CA files!\n");
3549 goto error;
3550 }
3551
3552 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3553 memprintf(&err, "CRL file '%s' in use, can't be deleted!\n", filename);
3554 goto error;
3555 }
3556
3557 /* Remove the cafile_entry from the tree */
3558 ebmb_delete(&cafile_entry->node);
3559 ssl_store_delete_cafile_entry(cafile_entry);
3560
3561 memprintf(&err, "CRL file '%s' deleted!\n", filename);
3562
3563 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3564 return cli_dynmsg(appctx, LOG_NOTICE, err);
3565
3566error:
3567 memprintf(&err, "Can't remove the CRL file: %s\n", err ? err : "");
3568 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3569 return cli_dynerr(appctx, err);
3570}
3571
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003572/* parsing function of 'abort ssl crl-file' */
3573static int cli_parse_abort_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3574{
3575 char *err = NULL;
3576
3577 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3578 return 1;
3579
3580 if (!*args[3])
3581 return cli_err(appctx, "'abort ssl crl-file' expects a filename\n");
3582
3583 /* The operations on the CKCH architecture are locked so we can
3584 * manipulate ckch_store and ckch_inst */
3585 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3586 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
3587
3588 if (!crlfile_transaction.path) {
3589 memprintf(&err, "No ongoing transaction!\n");
3590 goto error;
3591 }
3592
3593 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3594 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", crlfile_transaction.path, args[3]);
3595 goto error;
3596 }
3597
3598 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3599 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3600 crlfile_transaction.new_crlfile_entry = NULL;
3601 crlfile_transaction.old_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02003602 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003603
3604 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3605
3606 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3607 return cli_dynmsg(appctx, LOG_NOTICE, err);
3608
3609error:
3610 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3611
3612 return cli_dynerr(appctx, err);
3613}
3614
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003615
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003616/*
3617 * Display a Certificate Resignation List's information.
3618 * The information displayed is inspired by the output of 'openssl crl -in
3619 * crl.pem -text'.
3620 * Returns 0 in case of success.
3621 */
3622static int show_crl_detail(X509_CRL *crl, struct buffer *out)
3623{
3624 BIO *bio = NULL;
3625 struct buffer *tmp = alloc_trash_chunk();
3626 long version;
3627 X509_NAME *issuer;
3628 int write = -1;
3629 STACK_OF(X509_REVOKED) *rev = NULL;
3630 X509_REVOKED *rev_entry = NULL;
3631 int i;
3632
3633 if (!tmp)
3634 return -1;
3635
3636 if ((bio = BIO_new(BIO_s_mem())) == NULL)
3637 goto end;
3638
3639 /* Version (as displayed by 'openssl crl') */
3640 version = X509_CRL_get_version(crl);
3641 chunk_appendf(out, "Version %ld\n", version + 1);
3642
3643 /* Signature Algorithm */
3644 chunk_appendf(out, "Signature Algorithm: %s\n", OBJ_nid2ln(X509_CRL_get_signature_nid(crl)));
3645
3646 /* Issuer */
3647 chunk_appendf(out, "Issuer: ");
3648 if ((issuer = X509_CRL_get_issuer(crl)) == NULL)
3649 goto end;
3650 if ((ssl_sock_get_dn_oneline(issuer, tmp)) == -1)
3651 goto end;
3652 *(tmp->area + tmp->data) = '\0';
3653 chunk_appendf(out, "%s\n", tmp->area);
3654
3655 /* Last Update */
3656 chunk_appendf(out, "Last Update: ");
3657 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003658 if (BIO_reset(bio) == -1)
3659 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003660 if (ASN1_TIME_print(bio, X509_CRL_get0_lastUpdate(crl)) == 0)
3661 goto end;
3662 write = BIO_read(bio, tmp->area, tmp->size-1);
3663 tmp->area[write] = '\0';
3664 chunk_appendf(out, "%s\n", tmp->area);
3665
3666
3667 /* Next Update */
3668 chunk_appendf(out, "Next Update: ");
3669 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003670 if (BIO_reset(bio) == -1)
3671 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003672 if (ASN1_TIME_print(bio, X509_CRL_get0_nextUpdate(crl)) == 0)
3673 goto end;
3674 write = BIO_read(bio, tmp->area, tmp->size-1);
3675 tmp->area[write] = '\0';
3676 chunk_appendf(out, "%s\n", tmp->area);
3677
3678
3679 /* Revoked Certificates */
3680 rev = X509_CRL_get_REVOKED(crl);
3681 if (sk_X509_REVOKED_num(rev) > 0)
3682 chunk_appendf(out, "Revoked Certificates:\n");
3683 else
3684 chunk_appendf(out, "No Revoked Certificates.\n");
3685
3686 for (i = 0; i < sk_X509_REVOKED_num(rev); i++) {
3687 rev_entry = sk_X509_REVOKED_value(rev, i);
3688
3689 /* Serial Number and Revocation Date */
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003690 if (BIO_reset(bio) == -1)
3691 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003692 BIO_printf(bio , " Serial Number: ");
Remi Tricot-Le Breton18c7d832021-05-17 18:38:34 +02003693 i2a_ASN1_INTEGER(bio, (ASN1_INTEGER*)X509_REVOKED_get0_serialNumber(rev_entry));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003694 BIO_printf(bio, "\n Revocation Date: ");
Remi Tricot-Le Bretona6b27842021-05-18 10:06:00 +02003695 if (ASN1_TIME_print(bio, X509_REVOKED_get0_revocationDate(rev_entry)) == 0)
3696 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003697 BIO_printf(bio, "\n");
3698
3699 write = BIO_read(bio, tmp->area, tmp->size-1);
3700 tmp->area[write] = '\0';
3701 chunk_appendf(out, "%s", tmp->area);
3702 }
3703
3704end:
3705 free_trash_chunk(tmp);
3706 if (bio)
3707 BIO_free(bio);
3708
3709 return 0;
3710}
3711
Willy Tarreau821c3b02022-05-04 15:47:39 +02003712/* IO handler of details "show ssl crl-file <filename[:index]>".
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003713 * It uses show_crlfile_ctx and the global
3714 * crlfile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003715 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003716static int cli_io_handler_show_crlfile_detail(struct appctx *appctx)
3717{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003718 struct show_crlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003719 struct cafile_entry *cafile_entry = ctx->cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003720 struct buffer *out = alloc_trash_chunk();
3721 int i;
3722 X509_CRL *crl;
3723 STACK_OF(X509_OBJECT) *objs;
3724 int retval = 0;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003725 int index = ctx->index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003726
3727 if (!out)
3728 goto end_no_putchk;
3729
3730 chunk_appendf(out, "Filename: ");
3731 if (cafile_entry == crlfile_transaction.new_crlfile_entry)
3732 chunk_appendf(out, "*");
3733 chunk_appendf(out, "%s\n", cafile_entry->path);
3734
3735 chunk_appendf(out, "Status: ");
3736 if (!cafile_entry->ca_store)
3737 chunk_appendf(out, "Empty\n");
3738 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3739 chunk_appendf(out, "Unused\n");
3740 else
3741 chunk_appendf(out, "Used\n");
3742
3743 if (!cafile_entry->ca_store)
3744 goto end;
3745
3746 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3747 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
3748 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
3749 if (!crl)
3750 continue;
3751
3752 /* CRL indexes start at 1 on the CLI output. */
3753 if (index && index-1 != i)
3754 continue;
3755
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003756 chunk_appendf(out, " \nCertificate Revocation List #%d:\n", i+1);
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003757 retval = show_crl_detail(crl, out);
3758 if (retval < 0)
3759 goto end_no_putchk;
3760 else if (retval || index)
3761 goto end;
3762 }
3763
3764end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003765 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003766 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003767
3768end_no_putchk:
3769 free_trash_chunk(out);
3770 return 1;
3771yield:
3772 free_trash_chunk(out);
3773 return 0; /* should come back */
3774}
3775
Willy Tarreau821c3b02022-05-04 15:47:39 +02003776/* parsing function for 'show ssl crl-file [crlfile[:index]]'.
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003777 * It sets the context to a show_crlfile_ctx, and the global
Willy Tarreau821c3b02022-05-04 15:47:39 +02003778 * cafile_transaction.new_crlfile_entry under the ckch_lock.
3779 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003780static int cli_parse_show_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3781{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003782 struct show_crlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003783 struct cafile_entry *cafile_entry;
3784 long index = 0;
3785 char *colons;
3786 char *err = NULL;
3787
3788 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3789 return cli_err(appctx, "Can't allocate memory!\n");
3790
3791 /* The operations on the CKCH architecture are locked so we can
3792 * manipulate ckch_store and ckch_inst */
3793 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3794 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3795
3796 /* check if there is a certificate to lookup */
3797 if (*args[3]) {
3798
3799 /* Look for an optional index after the CRL file name */
3800 colons = strchr(args[3], ':');
3801 if (colons) {
3802 char *endptr;
3803
3804 index = strtol(colons + 1, &endptr, 10);
3805 /* Indexes start at 1 */
3806 if (colons + 1 == endptr || *endptr != '\0' || index <= 0) {
3807 memprintf(&err, "wrong CRL index after colons in '%s'!", args[3]);
3808 goto error;
3809 }
3810 *colons = '\0';
3811 }
3812
3813 if (*args[3] == '*') {
3814 if (!crlfile_transaction.new_crlfile_entry)
3815 goto error;
3816
3817 cafile_entry = crlfile_transaction.new_crlfile_entry;
3818
3819 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3820 goto error;
3821
3822 } else {
3823 /* Get the "original" cafile_entry and not the
3824 * uncommitted one if it exists. */
3825 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CRL)
3826 goto error;
3827 }
3828
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003829 ctx->cafile_entry = cafile_entry;
3830 ctx->index = index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003831 /* use the IO handler that shows details */
3832 appctx->io_handler = cli_io_handler_show_crlfile_detail;
3833 }
3834
3835 return 0;
3836
3837error:
3838 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3839 if (err)
3840 return cli_dynerr(appctx, err);
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003841 return cli_err(appctx, "Can't display the CRL file : Not found!\n");
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003842}
3843
3844/* IO handler of "show ssl crl-file". The command taking a specific CRL file name
3845 * is managed in cli_io_handler_show_crlfile_detail. */
3846static int cli_io_handler_show_crlfile(struct appctx *appctx)
3847{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003848 struct show_crlfile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003849 struct buffer *trash = alloc_trash_chunk();
3850 struct ebmb_node *node;
Christopher Faulet88041b32022-06-03 16:26:56 +02003851 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003852
3853 if (trash == NULL)
3854 return 1;
3855
Christopher Faulet9a99e542022-06-03 10:32:18 +02003856 if (!ctx->old_crlfile_entry && crlfile_transaction.old_crlfile_entry) {
3857 chunk_appendf(trash, "# transaction\n");
3858 chunk_appendf(trash, "*%s\n", crlfile_transaction.old_crlfile_entry->path);
3859 if (applet_putchk(appctx, trash) == -1)
3860 goto yield;
3861 ctx->old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003862 }
3863
3864 /* First time in this io_handler. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003865 if (!ctx->cafile_entry) {
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003866 chunk_appendf(trash, "# filename\n");
3867 node = ebmb_first(&cafile_tree);
3868 } else {
3869 /* We yielded during a previous call. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003870 node = &ctx->cafile_entry->node;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003871 }
3872
3873 while (node) {
3874 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3875 if (cafile_entry->type == CAFILE_CRL) {
3876 chunk_appendf(trash, "%s\n", cafile_entry->path);
3877 }
3878
3879 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003880 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003881 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003882 }
3883
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003884 ctx->cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003885 free_trash_chunk(trash);
3886 return 1;
3887yield:
3888
3889 free_trash_chunk(trash);
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003890 ctx->cafile_entry = cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003891 return 0; /* should come back */
3892}
3893
3894
3895/* release function of the 'show ssl crl-file' command */
3896static void cli_release_show_crlfile(struct appctx *appctx)
3897{
3898 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3899}
3900
3901
William Lallemandee8530c2020-06-23 18:19:42 +02003902void ckch_deinit()
3903{
3904 struct eb_node *node, *next;
3905 struct ckch_store *store;
William Lallemandb0c48272022-04-26 15:44:53 +02003906 struct ebmb_node *canode;
William Lallemandee8530c2020-06-23 18:19:42 +02003907
William Lallemandb0c48272022-04-26 15:44:53 +02003908 /* deinit the ckch stores */
William Lallemandee8530c2020-06-23 18:19:42 +02003909 node = eb_first(&ckchs_tree);
3910 while (node) {
3911 next = eb_next(node);
3912 store = ebmb_entry(node, struct ckch_store, node);
3913 ckch_store_free(store);
3914 node = next;
3915 }
William Lallemandb0c48272022-04-26 15:44:53 +02003916
3917 /* deinit the ca-file store */
3918 canode = ebmb_first(&cafile_tree);
3919 while (canode) {
3920 struct cafile_entry *entry = NULL;
3921
3922 entry = ebmb_entry(canode, struct cafile_entry, node);
3923 canode = ebmb_next(canode);
William Lallemand946580e2022-08-29 18:36:18 +02003924 ebmb_delete(&entry->node);
William Lallemandb0c48272022-04-26 15:44:53 +02003925 ssl_store_delete_cafile_entry(entry);
3926 }
William Lallemandee8530c2020-06-23 18:19:42 +02003927}
William Lallemandda8584c2020-05-14 10:14:37 +02003928
3929/* register cli keywords */
3930static struct cli_kw_list cli_kws = {{ },{
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003931 { { "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 },
3932 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
3933 { { "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 },
3934 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
3935 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
3936 { { "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 },
3937
Amaury Denoyelleb11ad9e2021-05-21 11:01:10 +02003938 { { "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 },
William Lallemand62c0b992022-07-29 17:50:58 +02003939 { { "add", "ssl", "ca-file", NULL }, "add ssl ca-file <cafile> <payload> : add a certificate into the CA file", cli_parse_set_cafile, NULL, NULL },
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003940 { { "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 +02003941 { { "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 +01003942 { { "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 +01003943 { { "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 +01003944 { { "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 +02003945
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003946 { { "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 +02003947 { { "set", "ssl", "crl-file", NULL }, "set ssl crl-file <crlfile> <payload> : replace a CRL file", cli_parse_set_crlfile, NULL, NULL },
3948 { { "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 +02003949 { { "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 +02003950 { { "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 +02003951 { { "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 +02003952 { { NULL }, NULL, NULL, NULL }
3953}};
3954
3955INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3956