blob: d531d39ae980557349fe7e5e0fbed3b1883d2150 [file] [log] [blame]
William Lallemand03c331c2020-05-13 10:10:01 +02001/*
2 *
3 * Copyright (C) 2020 HAProxy Technologies, William Lallemand <wlallemand@haproxy.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 */
11
12#define _GNU_SOURCE
13#include <ctype.h>
William Lallemand87fd9942022-04-01 20:12:03 +020014#include <dirent.h>
William Lallemand03c331c2020-05-13 10:10:01 +020015#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020020#include <syslog.h>
William Lallemand03c331c2020-05-13 10:10:01 +020021#include <unistd.h>
22
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreau74f24562021-10-06 17:54:12 +020026#include <import/ebpttree.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <import/ebsttree.h>
28
Willy Tarreau50c2f1e2022-05-04 19:26:59 +020029#include <haproxy/applet.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020030#include <haproxy/base64.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020031#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020032#include <haproxy/cli.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020033#include <haproxy/errors.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020034#include <haproxy/sc_strm.h>
Willy Tarreau47d7f902020-06-04 14:25:47 +020035#include <haproxy/ssl_ckch.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020036#include <haproxy/ssl_sock.h>
Willy Tarreaub2bd8652020-06-04 14:21:22 +020037#include <haproxy/ssl_utils.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020038#include <haproxy/stconn.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020039#include <haproxy/tools.h>
William Lallemand03c331c2020-05-13 10:10:01 +020040
William Lallemandda8584c2020-05-14 10:14:37 +020041/* Uncommitted CKCH transaction */
42
43static struct {
44 struct ckch_store *new_ckchs;
45 struct ckch_store *old_ckchs;
46 char *path;
47} ckchs_transaction;
48
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +010049/* Uncommitted CA file transaction */
50
51static struct {
52 struct cafile_entry *old_cafile_entry;
53 struct cafile_entry *new_cafile_entry;
54 char *path;
55} cafile_transaction;
William Lallemandda8584c2020-05-14 10:14:37 +020056
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +020057/* Uncommitted CRL file transaction */
58
59static struct {
60 struct cafile_entry *old_crlfile_entry;
61 struct cafile_entry *new_crlfile_entry;
62 char *path;
63} crlfile_transaction;
64
Willy Tarreau50c2f1e2022-05-04 19:26:59 +020065/* CLI context used by "show cafile" */
66struct show_cafile_ctx {
67 struct cafile_entry *cur_cafile_entry;
68 struct cafile_entry *old_cafile_entry;
69 int ca_index;
70 int show_all;
71};
William Lallemand03c331c2020-05-13 10:10:01 +020072
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +020073/* CLI context used by "show crlfile" */
74struct show_crlfile_ctx {
75 struct cafile_entry *cafile_entry;
Christopher Faulet51095ee2022-06-03 10:21:27 +020076 struct cafile_entry *old_crlfile_entry;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +020077 int index;
78};
79
Willy Tarreau96c9a6c2022-05-04 19:51:37 +020080/* CLI context used by "show cert" */
81struct show_cert_ctx {
82 struct ckch_store *old_ckchs;
83 struct ckch_store *cur_ckchs;
84 int transaction;
85};
86
Willy Tarreaua645b6a2022-05-04 19:58:00 +020087/* CLI context used by "commit cert" */
88struct commit_cert_ctx {
89 struct ckch_store *old_ckchs;
90 struct ckch_store *new_ckchs;
91 struct ckch_inst *next_ckchi;
Christopher Faulet9d56e242022-05-31 16:37:01 +020092 char *err;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +020093 enum {
94 CERT_ST_INIT = 0,
95 CERT_ST_GEN,
96 CERT_ST_INSERT,
Christopher Faulet9d56e242022-05-31 16:37:01 +020097 CERT_ST_SUCCESS,
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +020098 CERT_ST_FIN,
Christopher Faulet9d56e242022-05-31 16:37:01 +020099 CERT_ST_ERROR,
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +0200100 } state;
Willy Tarreaua645b6a2022-05-04 19:58:00 +0200101};
102
Willy Tarreaudec23dc2022-05-04 20:25:05 +0200103/* CLI context used by "commit cafile" and "commit crlfile" */
104struct commit_cacrlfile_ctx {
Christopher Faulet6af2fc62022-06-03 11:42:38 +0200105 struct cafile_entry *old_entry;
106 struct cafile_entry *new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +0200107 struct ckch_inst_link *next_ckchi_link;
Christopher Faulet14df9132022-06-03 09:17:09 +0200108 enum cafile_type cafile_type; /* either CA or CRL, depending on the current command */
Christopher Faulete9c3bd12022-05-31 17:51:06 +0200109 char *err;
Willy Tarreau1d6dd802022-05-05 08:17:29 +0200110 enum {
111 CACRL_ST_INIT = 0,
112 CACRL_ST_GEN,
113 CACRL_ST_INSERT,
Christopher Faulete9c3bd12022-05-31 17:51:06 +0200114 CACRL_ST_SUCCESS,
Willy Tarreau1d6dd802022-05-05 08:17:29 +0200115 CACRL_ST_FIN,
Christopher Faulete9c3bd12022-05-31 17:51:06 +0200116 CACRL_ST_ERROR,
Willy Tarreau1d6dd802022-05-05 08:17:29 +0200117 } state;
Willy Tarreaudec23dc2022-05-04 20:25:05 +0200118};
119
Willy Tarreaua37693f2022-05-04 20:12:55 +0200120
William Lallemand03c331c2020-05-13 10:10:01 +0200121/******************** cert_key_and_chain functions *************************
122 * These are the functions that fills a cert_key_and_chain structure. For the
123 * functions filling a SSL_CTX from a cert_key_and_chain, see ssl_sock.c
124 */
125
126/*
127 * Try to parse Signed Certificate Timestamp List structure. This function
128 * makes only basic test if the data seems like SCTL. No signature validation
129 * is performed.
130 */
131static int ssl_sock_parse_sctl(struct buffer *sctl)
132{
133 int ret = 1;
134 int len, pos, sct_len;
135 unsigned char *data;
136
137 if (sctl->data < 2)
138 goto out;
139
140 data = (unsigned char *) sctl->area;
141 len = (data[0] << 8) | data[1];
142
143 if (len + 2 != sctl->data)
144 goto out;
145
146 data = data + 2;
147 pos = 0;
148 while (pos < len) {
149 if (len - pos < 2)
150 goto out;
151
152 sct_len = (data[pos] << 8) | data[pos + 1];
153 if (pos + sct_len + 2 > len)
154 goto out;
155
156 pos += sct_len + 2;
157 }
158
159 ret = 0;
160
161out:
162 return ret;
163}
164
165/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
166 * It fills the ckch->sctl buffer
167 * return 0 on success or != 0 on failure */
168int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err)
169{
170 int fd = -1;
171 int r = 0;
172 int ret = 1;
173 struct buffer tmp;
174 struct buffer *src;
175 struct buffer *sctl;
176
177 if (buf) {
William Lallemand8d673942021-01-27 14:58:51 +0100178 chunk_initstr(&tmp, buf);
William Lallemand03c331c2020-05-13 10:10:01 +0200179 src = &tmp;
180 } else {
181 fd = open(sctl_path, O_RDONLY);
182 if (fd == -1)
183 goto end;
184
185 trash.data = 0;
186 while (trash.data < trash.size) {
187 r = read(fd, trash.area + trash.data, trash.size - trash.data);
188 if (r < 0) {
189 if (errno == EINTR)
190 continue;
191 goto end;
192 }
193 else if (r == 0) {
194 break;
195 }
196 trash.data += r;
197 }
198 src = &trash;
199 }
200
201 ret = ssl_sock_parse_sctl(src);
202 if (ret)
203 goto end;
204
205 sctl = calloc(1, sizeof(*sctl));
206 if (!chunk_dup(sctl, src)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100207 ha_free(&sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200208 goto end;
209 }
210 /* no error, fill ckch with new context, old context must be free */
211 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100212 ha_free(&ckch->sctl->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200213 free(ckch->sctl);
214 }
215 ckch->sctl = sctl;
216 ret = 0;
217end:
218 if (fd != -1)
219 close(fd);
220
221 return ret;
222}
223
224#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
225/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500226 * This function load the OCSP Response in DER format contained in file at
William Lallemand03c331c2020-05-13 10:10:01 +0200227 * path 'ocsp_path' or base64 in a buffer <buf>
228 *
229 * Returns 0 on success, 1 in error case.
230 */
231int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err)
232{
233 int fd = -1;
234 int r = 0;
235 int ret = 1;
236 struct buffer *ocsp_response;
237 struct buffer *src = NULL;
238
239 if (buf) {
240 int i, j;
241 /* if it's from a buffer it will be base64 */
242
243 /* remove \r and \n from the payload */
244 for (i = 0, j = 0; buf[i]; i++) {
245 if (buf[i] == '\r' || buf[i] == '\n')
246 continue;
247 buf[j++] = buf[i];
248 }
249 buf[j] = 0;
250
251 ret = base64dec(buf, j, trash.area, trash.size);
252 if (ret < 0) {
253 memprintf(err, "Error reading OCSP response in base64 format");
254 goto end;
255 }
256 trash.data = ret;
257 src = &trash;
258 } else {
259 fd = open(ocsp_path, O_RDONLY);
260 if (fd == -1) {
261 memprintf(err, "Error opening OCSP response file");
262 goto end;
263 }
264
265 trash.data = 0;
266 while (trash.data < trash.size) {
267 r = read(fd, trash.area + trash.data, trash.size - trash.data);
268 if (r < 0) {
269 if (errno == EINTR)
270 continue;
271
272 memprintf(err, "Error reading OCSP response from file");
273 goto end;
274 }
275 else if (r == 0) {
276 break;
277 }
278 trash.data += r;
279 }
280 close(fd);
281 fd = -1;
282 src = &trash;
283 }
284
285 ocsp_response = calloc(1, sizeof(*ocsp_response));
286 if (!chunk_dup(ocsp_response, src)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100287 ha_free(&ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200288 goto end;
289 }
290 /* no error, fill ckch with new context, old context must be free */
291 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100292 ha_free(&ckch->ocsp_response->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200293 free(ckch->ocsp_response);
294 }
295 ckch->ocsp_response = ocsp_response;
296 ret = 0;
297end:
298 if (fd != -1)
299 close(fd);
300
301 return ret;
302}
303#endif
304
305/*
306 * Try to load in a ckch every files related to a ckch.
307 * (PEM, sctl, ocsp, issuer etc.)
308 *
309 * This function is only used to load files during the configuration parsing,
310 * it is not used with the CLI.
311 *
312 * This allows us to carry the contents of the file without having to read the
313 * file multiple times. The caller must call
314 * ssl_sock_free_cert_key_and_chain_contents.
315 *
316 * returns:
317 * 0 on Success
318 * 1 on SSL Failure
319 */
320int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
321{
William Lallemand8e8581e2020-10-20 17:36:46 +0200322 struct buffer *fp = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200323 int ret = 1;
Remi Tricot-Le Breton9bf3a1f2022-05-09 11:07:13 +0200324 struct stat st;
William Lallemand03c331c2020-05-13 10:10:01 +0200325
326 /* try to load the PEM */
327 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
328 goto end;
329 }
330
William Lallemand8e8581e2020-10-20 17:36:46 +0200331 fp = alloc_trash_chunk();
332 if (!fp) {
333 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
334 goto end;
335 }
336
337 if (!chunk_strcpy(fp, path) || (b_data(fp) > MAXPATHLEN)) {
338 memprintf(err, "%s '%s' filename too long'.\n",
339 err && *err ? *err : "", fp->area);
340 ret = 1;
341 goto end;
342 }
343
William Lallemand089c1382020-10-23 17:35:12 +0200344 /* remove the ".crt" extension */
William Lallemand8e8581e2020-10-20 17:36:46 +0200345 if (global_ssl.extra_files_noext) {
346 char *ext;
347
348 /* look for the extension */
349 if ((ext = strrchr(fp->area, '.'))) {
William Lallemand8e8581e2020-10-20 17:36:46 +0200350
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100351 if (strcmp(ext, ".crt") == 0) {
William Lallemand8e8581e2020-10-20 17:36:46 +0200352 *ext = '\0';
William Lallemand089c1382020-10-23 17:35:12 +0200353 fp->data = strlen(fp->area);
354 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200355 }
356
357 }
358
Remi Tricot-Le Breton1bad7db2022-06-07 16:29:44 +0200359 if (ckch->key == NULL) {
360 /* If no private key was found yet and we cannot look for it in extra
361 * files, raise an error.
362 */
363 if (!(global_ssl.extra_files & SSL_GF_KEY)) {
364 memprintf(err, "%sNo Private Key found in '%s'.\n", err && *err ? *err : "", fp->area);
365 goto end;
366 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200367
Remi Tricot-Le Breton1bad7db2022-06-07 16:29:44 +0200368 /* try to load an external private key if it wasn't in the PEM */
369 if (!chunk_strcat(fp, ".key") || (b_data(fp) > MAXPATHLEN)) {
370 memprintf(err, "%s '%s' filename too long'.\n",
Remi Tricot-Le Breton9bf3a1f2022-05-09 11:07:13 +0200371 err && *err ? *err : "", fp->area);
Remi Tricot-Le Breton1bad7db2022-06-07 16:29:44 +0200372 ret = 1;
William Lallemand8e8581e2020-10-20 17:36:46 +0200373 goto end;
374 }
William Lallemand03c331c2020-05-13 10:10:01 +0200375
Remi Tricot-Le Breton1bad7db2022-06-07 16:29:44 +0200376 if (stat(fp->area, &st) == 0) {
377 if (ssl_sock_load_key_into_ckch(fp->area, NULL, ckch, err)) {
378 memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n",
379 err && *err ? *err : "", fp->area);
380 goto end;
381 }
382 }
383
384 if (ckch->key == NULL) {
385 memprintf(err, "%sNo Private Key found in '%s'.\n", err && *err ? *err : "", fp->area);
386 goto end;
387 }
388 /* remove the added extension */
389 *(fp->area + fp->data - strlen(".key")) = '\0';
390 b_sub(fp, strlen(".key"));
William Lallemand03c331c2020-05-13 10:10:01 +0200391 }
Remi Tricot-Le Breton9bf3a1f2022-05-09 11:07:13 +0200392
William Lallemand03c331c2020-05-13 10:10:01 +0200393
394 if (!X509_check_private_key(ckch->cert, ckch->key)) {
395 memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n",
396 err && *err ? *err : "", path);
397 goto end;
398 }
399
Ilya Shipitsinc47d6762021-02-13 11:45:33 +0500400#ifdef HAVE_SSL_SCTL
William Lallemand03c331c2020-05-13 10:10:01 +0200401 /* try to load the sctl file */
402 if (global_ssl.extra_files & SSL_GF_SCTL) {
William Lallemand03c331c2020-05-13 10:10:01 +0200403 struct stat st;
404
William Lallemand8e8581e2020-10-20 17:36:46 +0200405 if (!chunk_strcat(fp, ".sctl") || b_data(fp) > MAXPATHLEN) {
406 memprintf(err, "%s '%s' filename too long'.\n",
407 err && *err ? *err : "", fp->area);
408 ret = 1;
409 goto end;
410 }
411
412 if (stat(fp->area, &st) == 0) {
413 if (ssl_sock_load_sctl_from_file(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200414 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
William Lallemand8e8581e2020-10-20 17:36:46 +0200415 err && *err ? *err : "", fp->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200416 ret = 1;
417 goto end;
418 }
419 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200420 /* remove the added extension */
421 *(fp->area + fp->data - strlen(".sctl")) = '\0';
422 b_sub(fp, strlen(".sctl"));
William Lallemand03c331c2020-05-13 10:10:01 +0200423 }
424#endif
425
426 /* try to load an ocsp response file */
427 if (global_ssl.extra_files & SSL_GF_OCSP) {
William Lallemand03c331c2020-05-13 10:10:01 +0200428 struct stat st;
429
William Lallemand8e8581e2020-10-20 17:36:46 +0200430 if (!chunk_strcat(fp, ".ocsp") || b_data(fp) > MAXPATHLEN) {
431 memprintf(err, "%s '%s' filename too long'.\n",
432 err && *err ? *err : "", fp->area);
433 ret = 1;
434 goto end;
435 }
436
437 if (stat(fp->area, &st) == 0) {
438 if (ssl_sock_load_ocsp_response_from_file(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200439 ret = 1;
440 goto end;
441 }
442 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200443 /* remove the added extension */
444 *(fp->area + fp->data - strlen(".ocsp")) = '\0';
445 b_sub(fp, strlen(".ocsp"));
William Lallemand03c331c2020-05-13 10:10:01 +0200446 }
447
448#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
449 if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
450 /* if no issuer was found, try to load an issuer from the .issuer */
451 if (!ckch->ocsp_issuer) {
452 struct stat st;
William Lallemand8e8581e2020-10-20 17:36:46 +0200453
454 if (!chunk_strcat(fp, ".issuer") || b_data(fp) > MAXPATHLEN) {
455 memprintf(err, "%s '%s' filename too long'.\n",
456 err && *err ? *err : "", fp->area);
457 ret = 1;
458 goto end;
459 }
William Lallemand03c331c2020-05-13 10:10:01 +0200460
William Lallemand8e8581e2020-10-20 17:36:46 +0200461 if (stat(fp->area, &st) == 0) {
462 if (ssl_sock_load_issuer_file_into_ckch(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200463 ret = 1;
464 goto end;
465 }
466
467 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
468 memprintf(err, "%s '%s' is not an issuer'.\n",
William Lallemand8e8581e2020-10-20 17:36:46 +0200469 err && *err ? *err : "", fp->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200470 ret = 1;
471 goto end;
472 }
473 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200474 /* remove the added extension */
475 *(fp->area + fp->data - strlen(".issuer")) = '\0';
476 b_sub(fp, strlen(".issuer"));
William Lallemand03c331c2020-05-13 10:10:01 +0200477 }
478 }
479#endif
480
481 ret = 0;
482
483end:
484
485 ERR_clear_error();
486
487 /* Something went wrong in one of the reads */
488 if (ret != 0)
489 ssl_sock_free_cert_key_and_chain_contents(ckch);
490
William Lallemand8e8581e2020-10-20 17:36:46 +0200491 free_trash_chunk(fp);
492
William Lallemand03c331c2020-05-13 10:10:01 +0200493 return ret;
494}
495
496/*
497 * Try to load a private key file from a <path> or a buffer <buf>
498 *
499 * If it failed you should not attempt to use the ckch but free it.
500 *
501 * Return 0 on success or != 0 on failure
502 */
503int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
504{
505 BIO *in = NULL;
506 int ret = 1;
507 EVP_PKEY *key = NULL;
508
509 if (buf) {
510 /* reading from a buffer */
511 in = BIO_new_mem_buf(buf, -1);
512 if (in == NULL) {
513 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
514 goto end;
515 }
516
517 } else {
518 /* reading from a file */
519 in = BIO_new(BIO_s_file());
520 if (in == NULL)
521 goto end;
522
523 if (BIO_read_filename(in, path) <= 0)
524 goto end;
525 }
526
527 /* Read Private Key */
528 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
529 if (key == NULL) {
530 memprintf(err, "%sunable to load private key from file '%s'.\n",
531 err && *err ? *err : "", path);
532 goto end;
533 }
534
535 ret = 0;
536
537 SWAP(ckch->key, key);
538
539end:
540
541 ERR_clear_error();
542 if (in)
543 BIO_free(in);
544 if (key)
545 EVP_PKEY_free(key);
546
547 return ret;
548}
549
550/*
551 * Try to load a PEM file from a <path> or a buffer <buf>
552 * The PEM must contain at least a Certificate,
553 * It could contain a DH, a certificate chain and a PrivateKey.
554 *
555 * If it failed you should not attempt to use the ckch but free it.
556 *
557 * Return 0 on success or != 0 on failure
558 */
559int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
560{
561 BIO *in = NULL;
562 int ret = 1;
563 X509 *ca;
564 X509 *cert = NULL;
565 EVP_PKEY *key = NULL;
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100566 HASSL_DH *dh = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200567 STACK_OF(X509) *chain = NULL;
568
569 if (buf) {
570 /* reading from a buffer */
571 in = BIO_new_mem_buf(buf, -1);
572 if (in == NULL) {
573 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
574 goto end;
575 }
576
577 } else {
578 /* reading from a file */
579 in = BIO_new(BIO_s_file());
580 if (in == NULL) {
581 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
582 goto end;
583 }
584
585 if (BIO_read_filename(in, path) <= 0) {
586 memprintf(err, "%scannot open the file '%s'.\n",
587 err && *err ? *err : "", path);
588 goto end;
589 }
590 }
591
592 /* Read Private Key */
593 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
594 /* no need to check for errors here, because the private key could be loaded later */
595
596#ifndef OPENSSL_NO_DH
597 /* Seek back to beginning of file */
598 if (BIO_reset(in) == -1) {
599 memprintf(err, "%san error occurred while reading the file '%s'.\n",
600 err && *err ? *err : "", path);
601 goto end;
602 }
603
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100604 dh = ssl_sock_get_dh_from_bio(in);
605 ERR_clear_error();
William Lallemand03c331c2020-05-13 10:10:01 +0200606 /* no need to return an error there, dh is not mandatory */
607#endif
608
609 /* Seek back to beginning of file */
610 if (BIO_reset(in) == -1) {
611 memprintf(err, "%san error occurred while reading the file '%s'.\n",
612 err && *err ? *err : "", path);
613 goto end;
614 }
615
616 /* Read Certificate */
617 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
618 if (cert == NULL) {
619 memprintf(err, "%sunable to load certificate from file '%s'.\n",
620 err && *err ? *err : "", path);
621 goto end;
622 }
623
624 /* Look for a Certificate Chain */
625 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
626 if (chain == NULL)
627 chain = sk_X509_new_null();
628 if (!sk_X509_push(chain, ca)) {
629 X509_free(ca);
630 goto end;
631 }
632 }
633
634 ret = ERR_get_error();
635 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
636 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
637 err && *err ? *err : "", path);
638 goto end;
639 }
640
641 /* once it loaded the PEM, it should remove everything else in the ckch */
642 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100643 ha_free(&ckch->ocsp_response->area);
644 ha_free(&ckch->ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200645 }
646
647 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100648 ha_free(&ckch->sctl->area);
649 ha_free(&ckch->sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200650 }
651
652 if (ckch->ocsp_issuer) {
653 X509_free(ckch->ocsp_issuer);
654 ckch->ocsp_issuer = NULL;
655 }
656
657 /* no error, fill ckch with new context, old context will be free at end: */
658 SWAP(ckch->key, key);
659 SWAP(ckch->dh, dh);
660 SWAP(ckch->cert, cert);
661 SWAP(ckch->chain, chain);
662
663 ret = 0;
664
665end:
666
667 ERR_clear_error();
668 if (in)
669 BIO_free(in);
670 if (key)
671 EVP_PKEY_free(key);
672 if (dh)
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100673 HASSL_DH_free(dh);
William Lallemand03c331c2020-05-13 10:10:01 +0200674 if (cert)
675 X509_free(cert);
676 if (chain)
677 sk_X509_pop_free(chain, X509_free);
678
679 return ret;
680}
681
682/* Frees the contents of a cert_key_and_chain
683 */
684void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
685{
686 if (!ckch)
687 return;
688
689 /* Free the certificate and set pointer to NULL */
690 if (ckch->cert)
691 X509_free(ckch->cert);
692 ckch->cert = NULL;
693
694 /* Free the key and set pointer to NULL */
695 if (ckch->key)
696 EVP_PKEY_free(ckch->key);
697 ckch->key = NULL;
698
699 /* Free each certificate in the chain */
700 if (ckch->chain)
701 sk_X509_pop_free(ckch->chain, X509_free);
702 ckch->chain = NULL;
703
704 if (ckch->dh)
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100705 HASSL_DH_free(ckch->dh);
William Lallemand03c331c2020-05-13 10:10:01 +0200706 ckch->dh = NULL;
707
708 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100709 ha_free(&ckch->sctl->area);
710 ha_free(&ckch->sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200711 }
712
713 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100714 ha_free(&ckch->ocsp_response->area);
715 ha_free(&ckch->ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200716 }
717
718 if (ckch->ocsp_issuer)
719 X509_free(ckch->ocsp_issuer);
720 ckch->ocsp_issuer = NULL;
721}
722
723/*
724 *
725 * This function copy a cert_key_and_chain in memory
726 *
727 * It's used to try to apply changes on a ckch before committing them, because
728 * most of the time it's not possible to revert those changes
729 *
730 * Return a the dst or NULL
731 */
732struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
733 struct cert_key_and_chain *dst)
734{
William Lallemand6c096142021-02-23 14:45:45 +0100735 if (!src || !dst)
736 return NULL;
737
William Lallemand03c331c2020-05-13 10:10:01 +0200738 if (src->cert) {
739 dst->cert = src->cert;
740 X509_up_ref(src->cert);
741 }
742
743 if (src->key) {
744 dst->key = src->key;
745 EVP_PKEY_up_ref(src->key);
746 }
747
748 if (src->chain) {
749 dst->chain = X509_chain_up_ref(src->chain);
750 }
751
752 if (src->dh) {
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100753 HASSL_DH_up_ref(src->dh);
William Lallemand03c331c2020-05-13 10:10:01 +0200754 dst->dh = src->dh;
755 }
756
757 if (src->sctl) {
758 struct buffer *sctl;
759
760 sctl = calloc(1, sizeof(*sctl));
761 if (!chunk_dup(sctl, src->sctl)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100762 ha_free(&sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200763 goto error;
764 }
765 dst->sctl = sctl;
766 }
767
768 if (src->ocsp_response) {
769 struct buffer *ocsp_response;
770
771 ocsp_response = calloc(1, sizeof(*ocsp_response));
772 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100773 ha_free(&ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200774 goto error;
775 }
776 dst->ocsp_response = ocsp_response;
777 }
778
779 if (src->ocsp_issuer) {
780 X509_up_ref(src->ocsp_issuer);
781 dst->ocsp_issuer = src->ocsp_issuer;
782 }
783
784 return dst;
785
786error:
787
788 /* free everything */
789 ssl_sock_free_cert_key_and_chain_contents(dst);
790
791 return NULL;
792}
793
794/*
795 * return 0 on success or != 0 on failure
796 */
797int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
798{
799 int ret = 1;
800 BIO *in = NULL;
801 X509 *issuer;
802
803 if (buf) {
804 /* reading from a buffer */
805 in = BIO_new_mem_buf(buf, -1);
806 if (in == NULL) {
807 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
808 goto end;
809 }
810
811 } else {
812 /* reading from a file */
813 in = BIO_new(BIO_s_file());
814 if (in == NULL)
815 goto end;
816
817 if (BIO_read_filename(in, path) <= 0)
818 goto end;
819 }
820
821 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
822 if (!issuer) {
823 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
824 err && *err ? *err : "", path);
825 goto end;
826 }
827 /* no error, fill ckch with new context, old context must be free */
828 if (ckch->ocsp_issuer)
829 X509_free(ckch->ocsp_issuer);
830 ckch->ocsp_issuer = issuer;
831 ret = 0;
832
833end:
834
835 ERR_clear_error();
836 if (in)
837 BIO_free(in);
838
839 return ret;
840}
841
842/******************** ckch_store functions ***********************************
843 * The ckch_store is a structure used to cache and index the SSL files used in
844 * configuration
845 */
846
847/*
848 * Free a ckch_store, its ckch, its instances and remove it from the ebtree
849 */
850void ckch_store_free(struct ckch_store *store)
851{
852 struct ckch_inst *inst, *inst_s;
853
854 if (!store)
855 return;
856
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200857 ssl_sock_free_cert_key_and_chain_contents(store->ckch);
William Lallemand03c331c2020-05-13 10:10:01 +0200858
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100859 ha_free(&store->ckch);
William Lallemand03c331c2020-05-13 10:10:01 +0200860
861 list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) {
862 ckch_inst_free(inst);
863 }
864 ebmb_delete(&store->node);
865 free(store);
866}
867
868/*
869 * create and initialize a ckch_store
870 * <path> is the key name
871 * <nmemb> is the number of store->ckch objects to allocate
872 *
873 * Return a ckch_store or NULL upon failure.
874 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200875struct ckch_store *ckch_store_new(const char *filename)
William Lallemand03c331c2020-05-13 10:10:01 +0200876{
877 struct ckch_store *store;
878 int pathlen;
879
880 pathlen = strlen(filename);
881 store = calloc(1, sizeof(*store) + pathlen + 1);
882 if (!store)
883 return NULL;
884
William Lallemand03c331c2020-05-13 10:10:01 +0200885 memcpy(store->path, filename, pathlen + 1);
886
887 LIST_INIT(&store->ckch_inst);
888 LIST_INIT(&store->crtlist_entry);
889
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200890 store->ckch = calloc(1, sizeof(*store->ckch));
William Lallemand03c331c2020-05-13 10:10:01 +0200891 if (!store->ckch)
892 goto error;
893
894 return store;
895error:
896 ckch_store_free(store);
897 return NULL;
898}
899
900/* allocate and duplicate a ckch_store
901 * Return a new ckch_store or NULL */
902struct ckch_store *ckchs_dup(const struct ckch_store *src)
903{
904 struct ckch_store *dst;
905
William Lallemand6c096142021-02-23 14:45:45 +0100906 if (!src)
907 return NULL;
908
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200909 dst = ckch_store_new(src->path);
Eric Salama6ac61e32021-02-23 16:50:57 +0100910 if (!dst)
911 return NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200912
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200913 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
914 goto error;
William Lallemand03c331c2020-05-13 10:10:01 +0200915
916 return dst;
917
918error:
919 ckch_store_free(dst);
920
921 return NULL;
922}
923
924/*
925 * lookup a path into the ckchs tree.
926 */
927struct ckch_store *ckchs_lookup(char *path)
928{
929 struct ebmb_node *eb;
930
931 eb = ebst_lookup(&ckchs_tree, path);
932 if (!eb)
933 return NULL;
934
935 return ebmb_entry(eb, struct ckch_store, node);
936}
937
938/*
939 * This function allocate a ckch_store and populate it with certificates from files.
940 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200941struct ckch_store *ckchs_load_cert_file(char *path, char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200942{
943 struct ckch_store *ckchs;
944
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200945 ckchs = ckch_store_new(path);
William Lallemand03c331c2020-05-13 10:10:01 +0200946 if (!ckchs) {
947 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
948 goto end;
949 }
William Lallemand03c331c2020-05-13 10:10:01 +0200950
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200951 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
952 goto end;
William Lallemand03c331c2020-05-13 10:10:01 +0200953
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200954 /* insert into the ckchs tree */
955 memcpy(ckchs->path, path, strlen(path) + 1);
956 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand03c331c2020-05-13 10:10:01 +0200957 return ckchs;
958
959end:
960 ckch_store_free(ckchs);
961
962 return NULL;
963}
964
William Lallemandfa1d8b42020-05-13 15:46:10 +0200965
966/******************** ckch_inst functions ******************************/
967
968/* unlink a ckch_inst, free all SNIs, free the ckch_inst */
969/* The caller must use the lock of the bind_conf if used with inserted SNIs */
970void ckch_inst_free(struct ckch_inst *inst)
971{
972 struct sni_ctx *sni, *sni_s;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100973 struct ckch_inst_link_ref *link_ref, *link_ref_s;
William Lallemandfa1d8b42020-05-13 15:46:10 +0200974
975 if (inst == NULL)
976 return;
977
978 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
979 SSL_CTX_free(sni->ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +0200980 LIST_DELETE(&sni->by_ckch_inst);
William Lallemandfa1d8b42020-05-13 15:46:10 +0200981 ebmb_delete(&sni->name);
982 free(sni);
983 }
Remi Tricot-Le Bretonf3eedfe2021-01-25 17:19:44 +0100984 SSL_CTX_free(inst->ctx);
985 inst->ctx = NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +0200986 LIST_DELETE(&inst->by_ckchs);
987 LIST_DELETE(&inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100988
989 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
990 LIST_DELETE(&link_ref->link->list);
991 LIST_DELETE(&link_ref->list);
992 free(link_ref);
993 }
994
William Lallemandfa1d8b42020-05-13 15:46:10 +0200995 free(inst);
996}
997
998/* Alloc and init a ckch_inst */
999struct ckch_inst *ckch_inst_new()
1000{
1001 struct ckch_inst *ckch_inst;
1002
1003 ckch_inst = calloc(1, sizeof *ckch_inst);
1004 if (!ckch_inst)
1005 return NULL;
1006
1007 LIST_INIT(&ckch_inst->sni_ctx);
1008 LIST_INIT(&ckch_inst->by_ckchs);
1009 LIST_INIT(&ckch_inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001010 LIST_INIT(&ckch_inst->cafile_link_refs);
William Lallemandfa1d8b42020-05-13 15:46:10 +02001011
1012 return ckch_inst;
1013}
1014
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001015
1016/******************** ssl_store functions ******************************/
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001017struct eb_root cafile_tree = EB_ROOT;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001018
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001019/*
1020 * Returns the cafile_entry found in the cafile_tree indexed by the path 'path'.
1021 * If 'oldest_entry' is 1, returns the "original" cafile_entry (since
1022 * during a set cafile/commit cafile cycle there might be two entries for any
1023 * given path, the original one and the new one set via the CLI but not
1024 * committed yet).
1025 */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001026struct cafile_entry *ssl_store_get_cafile_entry(char *path, int oldest_entry)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001027{
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001028 struct cafile_entry *ca_e = NULL;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001029 struct ebmb_node *eb;
1030
1031 eb = ebst_lookup(&cafile_tree, path);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001032 while (eb) {
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001033 ca_e = ebmb_entry(eb, struct cafile_entry, node);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001034 /* The ebst_lookup in a tree that has duplicates returns the
1035 * oldest entry first. If we want the latest entry, we need to
1036 * iterate over all the duplicates until we find the last one
1037 * (in our case there should never be more than two entries for
1038 * any given path). */
1039 if (oldest_entry)
1040 return ca_e;
1041 eb = ebmb_next_dup(eb);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001042 }
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001043 return ca_e;
1044}
1045
Remi Tricot-Le Breton38c999b2021-02-23 16:28:43 +01001046int ssl_store_add_uncommitted_cafile_entry(struct cafile_entry *entry)
1047{
1048 return (ebst_insert(&cafile_tree, &entry->node) != &entry->node);
1049}
1050
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001051X509_STORE* ssl_store_get0_locations_file(char *path)
1052{
1053 struct cafile_entry *ca_e = ssl_store_get_cafile_entry(path, 0);
1054
1055 if (ca_e)
1056 return ca_e->ca_store;
1057
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001058 return NULL;
1059}
1060
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001061/* Create a cafile_entry object, without adding it to the cafile_tree. */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001062struct cafile_entry *ssl_store_create_cafile_entry(char *path, X509_STORE *store, enum cafile_type type)
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001063{
1064 struct cafile_entry *ca_e;
1065 int pathlen;
1066
1067 pathlen = strlen(path);
1068
1069 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
1070 if (ca_e) {
1071 memcpy(ca_e->path, path, pathlen + 1);
1072 ca_e->ca_store = store;
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001073 ca_e->type = type;
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001074 LIST_INIT(&ca_e->ckch_inst_link);
1075 }
1076 return ca_e;
1077}
1078
William Lallemand62c0b992022-07-29 17:50:58 +02001079
1080/* Duplicate a cafile_entry
1081 * Allocate the X509_STORE and copy the X509 and CRL inside.
1082 *
1083 * Return the newly allocated cafile_entry or NULL.
1084 *
1085 */
1086struct cafile_entry *ssl_store_dup_cafile_entry(struct cafile_entry *src)
1087{
1088 struct cafile_entry *dst = NULL;
1089 X509_STORE *store = NULL;
1090 STACK_OF(X509_OBJECT) *objs;
1091 int i;
1092
1093 if (!src)
1094 return NULL;
1095
1096 if (src->ca_store) {
1097 /* if there was a store in the src, copy it */
1098 store = X509_STORE_new();
1099 if (!store)
1100 goto err;
1101
1102 objs = X509_STORE_get0_objects(src->ca_store);
1103 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
1104 X509 *cert;
1105 X509_CRL *crl;
1106
1107 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
1108 if (cert) {
1109 if (X509_STORE_add_cert(store, cert) == 0) {
1110 /* only exits on error if the error is not about duplicate certificates */
1111 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1112 goto err;
1113 }
1114 }
1115
1116 }
1117 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
1118 if (crl) {
1119 if (X509_STORE_add_crl(store, crl) == 0) {
1120 /* only exits on error if the error is not about duplicate certificates */
1121 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1122 goto err;
1123 }
1124 }
1125
1126 }
1127 }
1128 }
1129 dst = ssl_store_create_cafile_entry(src->path, store, src->type);
1130
1131 return dst;
1132
1133err:
1134 X509_STORE_free(store);
1135 ha_free(&dst);
1136
1137 return NULL;
1138}
1139
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001140/* Delete a cafile_entry. The caller is responsible from removing this entry
1141 * from the cafile_tree first if is was previously added into it. */
1142void ssl_store_delete_cafile_entry(struct cafile_entry *ca_e)
1143{
1144 struct ckch_inst_link *link, *link_s;
1145 if (!ca_e)
1146 return;
1147
1148 X509_STORE_free(ca_e->ca_store);
1149
1150 list_for_each_entry_safe(link, link_s, &ca_e->ckch_inst_link, list) {
1151 struct ckch_inst *inst = link->ckch_inst;
1152 struct ckch_inst_link_ref *link_ref, *link_ref_s;
1153 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
1154 if (link_ref->link == link) {
1155 LIST_DELETE(&link_ref->list);
1156 free(link_ref);
1157 break;
1158 }
1159 }
1160 LIST_DELETE(&link->list);
1161 free(link);
1162 }
1163
1164 free(ca_e);
1165}
1166
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001167/*
William Lallemandd4774d32022-07-29 17:08:02 +02001168 * Fill a cafile_entry <ca_e> X509_STORE ca_e->store out of a buffer <cert_buf>
1169 * instead of out of a file. The <append> field should be set to 1 if you want
1170 * to keep the existing X509_STORE and append data to it.
1171 *
1172 * This function is used when the "set ssl ca-file" cli command is used.
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001173 * It can parse CERTIFICATE sections as well as CRL ones.
1174 * Returns 0 in case of success, 1 otherwise.
William Lallemandd4774d32022-07-29 17:08:02 +02001175 *
1176 * /!\ Warning: If there was an error the X509_STORE could have been modified so it's
1177 * better to not use it after a return 1.
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001178 */
William Lallemandd4774d32022-07-29 17:08:02 +02001179int 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 +01001180{
William Lallemandd4774d32022-07-29 17:08:02 +02001181 BIO *bio = NULL;
1182 STACK_OF(X509_INFO) *infos;
1183 X509_INFO *info;
1184 int i;
1185 int retval = 1;
1186 int retcert = 0;
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001187
1188 if (!ca_e)
1189 return 1;
1190
William Lallemandd4774d32022-07-29 17:08:02 +02001191 if (!append) {
1192 X509_STORE_free(ca_e->ca_store);
1193 ca_e->ca_store = NULL;
1194 }
1195
1196 if (!ca_e->ca_store)
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001197 ca_e->ca_store = X509_STORE_new();
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001198
William Lallemandd4774d32022-07-29 17:08:02 +02001199 if (!ca_e->ca_store)
1200 goto end;
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001201
William Lallemandd4774d32022-07-29 17:08:02 +02001202 bio = BIO_new_mem_buf(cert_buf, strlen(cert_buf));
1203 if (!bio)
1204 goto end;
1205
1206 infos = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
1207 if (!infos)
1208 goto end;
1209
1210 for (i = 0; i < sk_X509_INFO_num(infos) && !retcert; i++) {
1211 info = sk_X509_INFO_value(infos, i);
1212
1213 /* X509_STORE_add_cert and X509_STORE_add_crl return 1 on success */
1214 if (info->x509)
1215 retcert = !X509_STORE_add_cert(ca_e->ca_store, info->x509);
1216 if (!retcert && info->crl)
1217 retcert = !X509_STORE_add_crl(ca_e->ca_store, info->crl);
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001218 }
1219
William Lallemandd4774d32022-07-29 17:08:02 +02001220 /* return an error if we didn't compute all the X509_INFO or if there was none
1221 * set to 0 if everything was right */
1222 if (!(retcert || (i != sk_X509_INFO_num(infos)) || (sk_X509_INFO_num(infos) == 0)))
1223 retval = 0;
1224
1225 /* Cleanup */
1226 sk_X509_INFO_pop_free(infos, X509_INFO_free);
1227
1228end:
1229 BIO_free(bio);
1230
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001231 return retval;
1232}
1233
William Lallemand0f17ab22022-07-19 18:03:16 +02001234/*
1235 * Try to load a ca-file from disk into the ca-file cache.
1236 *
1237 * Return 0 upon error
1238 */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001239int ssl_store_load_locations_file(char *path, int create_if_none, enum cafile_type type)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001240{
1241 X509_STORE *store = ssl_store_get0_locations_file(path);
1242
1243 /* If this function is called by the CLI, we should not call the
1244 * X509_STORE_load_locations function because it performs forbidden disk
1245 * accesses. */
1246 if (!store && create_if_none) {
William Lallemand87fd9942022-04-01 20:12:03 +02001247 STACK_OF(X509_OBJECT) *objs;
1248 int cert_count = 0;
1249 struct stat buf;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001250 struct cafile_entry *ca_e;
William Lallemandc6b17632022-04-01 23:39:37 +02001251 const char *file = NULL;
1252 const char *dir = NULL;
William Lallemand0f17ab22022-07-19 18:03:16 +02001253 unsigned long e;
William Lallemand87fd9942022-04-01 20:12:03 +02001254
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001255 store = X509_STORE_new();
William Lallemand0f17ab22022-07-19 18:03:16 +02001256 if (!store) {
1257 ha_alert("Cannot allocate memory!");
1258 goto err;
1259 }
William Lallemand87fd9942022-04-01 20:12:03 +02001260
William Lallemandc6b17632022-04-01 23:39:37 +02001261 if (strcmp(path, "@system-ca") == 0) {
1262 dir = X509_get_default_cert_dir();
William Lallemand0f17ab22022-07-19 18:03:16 +02001263 if (!dir) {
1264 ha_alert("Couldn't get the system CA directory from X509_get_default_cert_dir().");
1265 goto err;
1266 }
William Lallemand87fd9942022-04-01 20:12:03 +02001267
William Lallemandc6b17632022-04-01 23:39:37 +02001268 } else {
1269
William Lallemand0f17ab22022-07-19 18:03:16 +02001270 if (stat(path, &buf) == -1) {
1271 ha_alert("Couldn't open the ca-file '%s' (%s).", path, strerror(errno));
William Lallemandc6b17632022-04-01 23:39:37 +02001272 goto err;
William Lallemand0f17ab22022-07-19 18:03:16 +02001273 }
William Lallemandc6b17632022-04-01 23:39:37 +02001274
1275 if (S_ISDIR(buf.st_mode))
1276 dir = path;
1277 else
1278 file = path;
1279 }
William Lallemand87fd9942022-04-01 20:12:03 +02001280
1281 if (file) {
1282 if (!X509_STORE_load_locations(store, file, NULL)) {
William Lallemand0f17ab22022-07-19 18:03:16 +02001283 e = ERR_get_error();
1284 ha_alert("Couldn't open the ca-file '%s' (%s).", path, ERR_reason_error_string(e));
William Lallemand87fd9942022-04-01 20:12:03 +02001285 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001286 }
William Lallemand80296b42022-04-05 10:19:30 +02001287 } else if (dir) {
William Lallemand87fd9942022-04-01 20:12:03 +02001288 int n, i;
1289 struct dirent **de_list;
1290
1291 n = scandir(dir, &de_list, 0, alphasort);
1292 if (n < 0)
1293 goto err;
1294
1295 for (i= 0; i < n; i++) {
1296 char *end;
1297 struct dirent *de = de_list[i];
1298 BIO *in = NULL;
1299 X509 *ca = NULL;;
1300
William Lallemand43482322022-07-18 18:42:52 +02001301 ERR_clear_error();
1302
William Lallemand87fd9942022-04-01 20:12:03 +02001303 /* we try to load the files that would have
1304 * been loaded in an hashed directory loaded by
1305 * X509_LOOKUP_hash_dir, so according to "man 1
1306 * c_rehash", we should load ".pem", ".crt",
William Lallemande4b93eb2022-05-09 09:29:00 +02001307 * ".cer", or ".crl". Files starting with a dot
1308 * are ignored.
William Lallemand87fd9942022-04-01 20:12:03 +02001309 */
1310 end = strrchr(de->d_name, '.');
William Lallemande4b93eb2022-05-09 09:29:00 +02001311 if (!end || de->d_name[0] == '.' ||
1312 (strcmp(end, ".pem") != 0 &&
1313 strcmp(end, ".crt") != 0 &&
1314 strcmp(end, ".cer") != 0 &&
1315 strcmp(end, ".crl") != 0)) {
William Lallemand87fd9942022-04-01 20:12:03 +02001316 free(de);
1317 continue;
1318 }
1319 in = BIO_new(BIO_s_file());
1320 if (in == NULL)
1321 goto scandir_err;
1322
William Lallemandc6b17632022-04-01 23:39:37 +02001323 chunk_printf(&trash, "%s/%s", dir, de->d_name);
William Lallemand87fd9942022-04-01 20:12:03 +02001324
1325 if (BIO_read_filename(in, trash.area) == 0)
1326 goto scandir_err;
1327
1328 if (PEM_read_bio_X509_AUX(in, &ca, NULL, NULL) == NULL)
1329 goto scandir_err;
1330
William Lallemand43482322022-07-18 18:42:52 +02001331 if (X509_STORE_add_cert(store, ca) == 0) {
1332 /* only exits on error if the error is not about duplicate certificates */
1333 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1334 goto scandir_err;
1335 }
1336 }
William Lallemand87fd9942022-04-01 20:12:03 +02001337
William Lallemand4cfbf3c2022-04-26 15:57:33 +02001338 X509_free(ca);
William Lallemand87fd9942022-04-01 20:12:03 +02001339 BIO_free(in);
1340 free(de);
1341 continue;
1342
1343scandir_err:
William Lallemand0f17ab22022-07-19 18:03:16 +02001344 e = ERR_get_error();
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);
William Lallemand0f17ab22022-07-19 18:03:16 +02001348 /* warn if it can load one of the files, but don't abort */
1349 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 +02001350
1351 }
1352 free(de_list);
William Lallemand80296b42022-04-05 10:19:30 +02001353 } else {
1354 ha_alert("ca-file: couldn't load '%s'\n", path);
1355 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001356 }
William Lallemand87fd9942022-04-01 20:12:03 +02001357
1358 objs = X509_STORE_get0_objects(store);
1359 cert_count = sk_X509_OBJECT_num(objs);
William Lallemand0f17ab22022-07-19 18:03:16 +02001360 if (cert_count == 0) {
William Lallemand87fd9942022-04-01 20:12:03 +02001361 ha_warning("ca-file: 0 CA were loaded from '%s'\n", path);
William Lallemand0f17ab22022-07-19 18:03:16 +02001362 }
William Lallemand87fd9942022-04-01 20:12:03 +02001363 ca_e = ssl_store_create_cafile_entry(path, store, type);
William Lallemand0f17ab22022-07-19 18:03:16 +02001364 if (!ca_e) {
1365 ha_alert("Cannot allocate memory!\n");
William Lallemand87fd9942022-04-01 20:12:03 +02001366 goto err;
William Lallemand0f17ab22022-07-19 18:03:16 +02001367 }
William Lallemand87fd9942022-04-01 20:12:03 +02001368 ebst_insert(&cafile_tree, &ca_e->node);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001369 }
1370 return (store != NULL);
William Lallemand87fd9942022-04-01 20:12:03 +02001371
1372err:
1373 X509_STORE_free(store);
1374 store = NULL;
1375 return 0;
1376
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001377}
1378
1379
William Lallemandda8584c2020-05-14 10:14:37 +02001380/*************************** CLI commands ***********************/
1381
1382/* Type of SSL payloads that can be updated over the CLI */
1383
William Lallemandff8bf982022-03-29 10:44:23 +02001384struct cert_exts cert_exts[] = {
1385 { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand26654e72022-03-30 12:01:32 +02001386 { "crt", CERT_TYPE_CRT, &ssl_sock_load_pem_into_ckch },
William Lallemandff8bf982022-03-29 10:44:23 +02001387 { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemandda8584c2020-05-14 10:14:37 +02001388#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemandff8bf982022-03-29 10:44:23 +02001389 { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001390#endif
Ilya Shipitsinc47d6762021-02-13 11:45:33 +05001391#ifdef HAVE_SSL_SCTL
William Lallemandff8bf982022-03-29 10:44:23 +02001392 { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001393#endif
William Lallemandff8bf982022-03-29 10:44:23 +02001394 { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
1395 { NULL, CERT_TYPE_MAX, NULL },
William Lallemandda8584c2020-05-14 10:14:37 +02001396};
1397
1398
1399/* release function of the `show ssl cert' command */
1400static void cli_release_show_cert(struct appctx *appctx)
1401{
1402 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1403}
1404
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001405/* IO handler of "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001406 * It makes use of a show_cert_ctx context, and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001407 */
William Lallemandda8584c2020-05-14 10:14:37 +02001408static int cli_io_handler_show_cert(struct appctx *appctx)
1409{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001410 struct show_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001411 struct buffer *trash = alloc_trash_chunk();
1412 struct ebmb_node *node;
Christopher Fauletd1d2e4d2022-06-03 16:24:02 +02001413 struct ckch_store *ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001414
1415 if (trash == NULL)
1416 return 1;
1417
Christopher Faulet3e94f5d2022-06-03 10:46:40 +02001418 if (!ctx->old_ckchs && ckchs_transaction.old_ckchs) {
1419 ckchs = ckchs_transaction.old_ckchs;
1420 chunk_appendf(trash, "# transaction\n");
1421 chunk_appendf(trash, "*%s\n", ckchs->path);
1422 if (applet_putchk(appctx, trash) == -1)
1423 goto yield;
1424 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001425 }
1426
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001427 if (!ctx->cur_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02001428 chunk_appendf(trash, "# filename\n");
1429 node = ebmb_first(&ckchs_tree);
1430 } else {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001431 node = &ctx->cur_ckchs->node;
William Lallemandda8584c2020-05-14 10:14:37 +02001432 }
1433 while (node) {
1434 ckchs = ebmb_entry(node, struct ckch_store, node);
William Lallemand5685ccf2020-09-16 16:12:25 +02001435 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001436
1437 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001438 if (applet_putchk(appctx, trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001439 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001440 }
1441
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001442 ctx->cur_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001443 free_trash_chunk(trash);
1444 return 1;
1445yield:
1446
1447 free_trash_chunk(trash);
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001448 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001449 return 0; /* should come back */
1450}
1451
1452/*
1453 * Extract and format the DNS SAN extensions and copy result into a chuink
1454 * Return 0;
1455 */
1456#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1457static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
1458{
1459 int i;
1460 char *str;
1461 STACK_OF(GENERAL_NAME) *names = NULL;
1462
1463 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1464 if (names) {
1465 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
1466 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
1467 if (i > 0)
1468 chunk_appendf(out, ", ");
1469 if (name->type == GEN_DNS) {
1470 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
1471 chunk_appendf(out, "DNS:%s", str);
1472 OPENSSL_free(str);
1473 }
1474 }
1475 }
1476 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
1477 }
1478 return 0;
1479}
1480#endif
1481
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001482/*
1483 * Build the ckch_inst_link that will be chained in the CA file entry and the
1484 * corresponding ckch_inst_link_ref that will be chained in the ckch instance.
1485 * Return 0 in case of success.
1486 */
1487static int do_chain_inst_and_cafile(struct cafile_entry *cafile_entry, struct ckch_inst *ckch_inst)
1488{
1489 struct ckch_inst_link *new_link;
1490 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
1491 struct ckch_inst_link *link = LIST_ELEM(cafile_entry->ckch_inst_link.n,
1492 typeof(link), list);
1493 /* Do not add multiple references to the same
1494 * instance in a cafile_entry */
1495 if (link->ckch_inst == ckch_inst) {
1496 return 1;
1497 }
1498 }
1499
1500 new_link = calloc(1, sizeof(*new_link));
1501 if (new_link) {
1502 struct ckch_inst_link_ref *new_link_ref = calloc(1, sizeof(*new_link_ref));
1503 if (!new_link_ref) {
1504 free(new_link);
1505 return 1;
1506 }
1507
1508 new_link->ckch_inst = ckch_inst;
1509 new_link_ref->link = new_link;
1510 LIST_INIT(&new_link->list);
1511 LIST_INIT(&new_link_ref->list);
1512
1513 LIST_APPEND(&cafile_entry->ckch_inst_link, &new_link->list);
1514 LIST_APPEND(&ckch_inst->cafile_link_refs, &new_link_ref->list);
1515 }
1516
1517 return 0;
1518}
1519
1520
1521/*
1522 * Link a CA file tree entry to the ckch instance that uses it.
1523 * To determine if and which CA file tree entries need to be linked to the
1524 * instance, we follow the same logic performed in ssl_sock_prepare_ctx when
1525 * processing the verify option.
1526 * This function works for a frontend as well as for a backend, depending on the
1527 * configuration parameters given (bind_conf or server).
1528 */
1529void ckch_inst_add_cafile_link(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf,
1530 struct ssl_bind_conf *ssl_conf, const struct server *srv)
1531{
1532 int verify = SSL_VERIFY_NONE;
1533
1534 if (srv) {
1535
1536 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
1537 verify = SSL_VERIFY_PEER;
1538 switch (srv->ssl_ctx.verify) {
1539 case SSL_SOCK_VERIFY_NONE:
1540 verify = SSL_VERIFY_NONE;
1541 break;
1542 case SSL_SOCK_VERIFY_REQUIRED:
1543 verify = SSL_VERIFY_PEER;
1544 break;
1545 }
1546 }
1547 else {
1548 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
1549 case SSL_SOCK_VERIFY_NONE:
1550 verify = SSL_VERIFY_NONE;
1551 break;
1552 case SSL_SOCK_VERIFY_OPTIONAL:
1553 verify = SSL_VERIFY_PEER;
1554 break;
1555 case SSL_SOCK_VERIFY_REQUIRED:
1556 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1557 break;
1558 }
1559 }
1560
1561 if (verify & SSL_VERIFY_PEER) {
1562 struct cafile_entry *ca_file_entry = NULL;
1563 struct cafile_entry *ca_verify_file_entry = NULL;
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001564 struct cafile_entry *crl_file_entry = NULL;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001565 if (srv) {
1566 if (srv->ssl_ctx.ca_file) {
1567 ca_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.ca_file, 0);
1568
1569 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001570 if (srv->ssl_ctx.crl_file) {
1571 crl_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.crl_file, 0);
1572 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001573 }
1574 else {
1575 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
1576 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 +02001577 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 +01001578
1579 if (ca_file)
1580 ca_file_entry = ssl_store_get_cafile_entry(ca_file, 0);
1581 if (ca_verify_file)
1582 ca_verify_file_entry = ssl_store_get_cafile_entry(ca_verify_file, 0);
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001583 if (crl_file)
1584 crl_file_entry = ssl_store_get_cafile_entry(crl_file, 0);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001585 }
1586
1587 if (ca_file_entry) {
1588 /* If we have a ckch instance that is not already in the
1589 * cafile_entry's list, add it to it. */
1590 if (do_chain_inst_and_cafile(ca_file_entry, ckch_inst))
1591 return;
1592
1593 }
1594 if (ca_verify_file_entry && (ca_file_entry != ca_verify_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_verify_file_entry, ckch_inst))
1598 return;
1599 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001600 if (crl_file_entry) {
1601 /* If we have a ckch instance that is not already in the
1602 * cafile_entry's list, add it to it. */
1603 if (do_chain_inst_and_cafile(crl_file_entry, ckch_inst))
1604 return;
1605 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001606 }
1607}
1608
William Lallemandda8584c2020-05-14 10:14:37 +02001609
1610
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001611static int show_cert_detail(X509 *cert, STACK_OF(X509) *chain, struct buffer *out)
William Lallemandda8584c2020-05-14 10:14:37 +02001612{
William Lallemandda8584c2020-05-14 10:14:37 +02001613 BIO *bio = NULL;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001614 struct buffer *tmp = alloc_trash_chunk();
William Lallemandda8584c2020-05-14 10:14:37 +02001615 int i;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001616 int write = -1;
1617 unsigned int len = 0;
1618 X509_NAME *name = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001619
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001620 if (!tmp)
1621 return -1;
William Lallemandda8584c2020-05-14 10:14:37 +02001622
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001623 if (!cert)
William Lallemand5685ccf2020-09-16 16:12:25 +02001624 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02001625
William Lallemand5685ccf2020-09-16 16:12:25 +02001626 if (chain == NULL) {
1627 struct issuer_chain *issuer;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001628 issuer = ssl_get0_issuer_chain(cert);
William Lallemand5685ccf2020-09-16 16:12:25 +02001629 if (issuer) {
1630 chain = issuer->chain;
1631 chunk_appendf(out, "Chain Filename: ");
1632 chunk_appendf(out, "%s\n", issuer->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001633 }
William Lallemand5685ccf2020-09-16 16:12:25 +02001634 }
1635 chunk_appendf(out, "Serial: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001636 if (ssl_sock_get_serial(cert, tmp) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001637 goto end;
1638 dump_binary(out, tmp->area, tmp->data);
1639 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001640
William Lallemand5685ccf2020-09-16 16:12:25 +02001641 chunk_appendf(out, "notBefore: ");
1642 chunk_reset(tmp);
1643 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1644 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001645 if (ASN1_TIME_print(bio, X509_getm_notBefore(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001646 goto end;
1647 write = BIO_read(bio, tmp->area, tmp->size-1);
1648 tmp->area[write] = '\0';
1649 BIO_free(bio);
1650 bio = NULL;
1651 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001652
William Lallemand5685ccf2020-09-16 16:12:25 +02001653 chunk_appendf(out, "notAfter: ");
1654 chunk_reset(tmp);
1655 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1656 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001657 if (ASN1_TIME_print(bio, X509_getm_notAfter(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001658 goto end;
1659 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
1660 goto end;
1661 tmp->area[write] = '\0';
1662 BIO_free(bio);
1663 bio = NULL;
1664 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001665
1666#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand5685ccf2020-09-16 16:12:25 +02001667 chunk_appendf(out, "Subject Alternative Name: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001668 if (ssl_sock_get_san_oneline(cert, out) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001669 goto end;
1670 *(out->area + out->data) = '\0';
1671 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001672#endif
William Lallemand5685ccf2020-09-16 16:12:25 +02001673 chunk_reset(tmp);
1674 chunk_appendf(out, "Algorithm: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001675 if (cert_get_pkey_algo(cert, tmp) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001676 goto end;
1677 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001678
William Lallemand5685ccf2020-09-16 16:12:25 +02001679 chunk_reset(tmp);
1680 chunk_appendf(out, "SHA1 FingerPrint: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001681 if (X509_digest(cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001682 goto end;
1683 tmp->data = len;
1684 dump_binary(out, tmp->area, tmp->data);
1685 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001686
William Lallemand5685ccf2020-09-16 16:12:25 +02001687 chunk_appendf(out, "Subject: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001688 if ((name = X509_get_subject_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001689 goto end;
1690 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1691 goto end;
1692 *(tmp->area + tmp->data) = '\0';
1693 chunk_appendf(out, "%s\n", tmp->area);
1694
1695 chunk_appendf(out, "Issuer: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001696 if ((name = X509_get_issuer_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001697 goto end;
1698 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1699 goto end;
1700 *(tmp->area + tmp->data) = '\0';
1701 chunk_appendf(out, "%s\n", tmp->area);
1702
1703 /* Displays subject of each certificate in the chain */
1704 for (i = 0; i < sk_X509_num(chain); i++) {
1705 X509 *ca = sk_X509_value(chain, i);
1706
1707 chunk_appendf(out, "Chain Subject: ");
1708 if ((name = X509_get_subject_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001709 goto end;
1710 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1711 goto end;
1712 *(tmp->area + tmp->data) = '\0';
1713 chunk_appendf(out, "%s\n", tmp->area);
1714
William Lallemand5685ccf2020-09-16 16:12:25 +02001715 chunk_appendf(out, "Chain Issuer: ");
1716 if ((name = X509_get_issuer_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001717 goto end;
1718 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1719 goto end;
1720 *(tmp->area + tmp->data) = '\0';
1721 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001722 }
1723
1724end:
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001725 if (bio)
1726 BIO_free(bio);
1727 free_trash_chunk(tmp);
1728
1729 return 0;
1730}
1731
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001732#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 +02001733/*
1734 * Build the OCSP tree entry's key for a given ckch_store.
1735 * Returns a negative value in case of error.
1736 */
1737static int ckch_store_build_certid(struct ckch_store *ckch_store, unsigned char certid[128], unsigned int *key_length)
1738{
1739 OCSP_RESPONSE *resp;
1740 OCSP_BASICRESP *bs = NULL;
1741 OCSP_SINGLERESP *sr;
1742 OCSP_CERTID *id;
1743 unsigned char *p = NULL;
1744
1745 if (!key_length)
1746 return -1;
1747
1748 *key_length = 0;
1749
1750 if (!ckch_store->ckch->ocsp_response)
1751 return 0;
1752
1753 p = (unsigned char *) ckch_store->ckch->ocsp_response->area;
1754
1755 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
1756 ckch_store->ckch->ocsp_response->data);
1757 if (!resp) {
1758 goto end;
1759 }
1760
1761 bs = OCSP_response_get1_basic(resp);
1762 if (!bs) {
1763 goto end;
1764 }
1765
1766 sr = OCSP_resp_get0(bs, 0);
1767 if (!sr) {
1768 goto end;
1769 }
1770
1771 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
1772
1773 p = certid;
1774 *key_length = i2d_OCSP_CERTID(id, &p);
1775
1776end:
1777 return *key_length > 0;
1778}
1779#endif
1780
1781/*
1782 * Dump the OCSP certificate key (if it exists) of certificate <ckch> into
1783 * buffer <out>.
1784 * Returns 0 in case of success.
1785 */
1786static int ckch_store_show_ocsp_certid(struct ckch_store *ckch_store, struct buffer *out)
1787{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001788#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 +02001789 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1790 unsigned int key_length = 0;
1791 int i;
1792
1793 if (ckch_store_build_certid(ckch_store, (unsigned char*)key, &key_length) >= 0) {
1794 /* Dump the CERTID info */
1795 chunk_appendf(out, "OCSP Response Key: ");
1796 for (i = 0; i < key_length; ++i) {
1797 chunk_appendf(out, "%02x", key[i]);
1798 }
1799 chunk_appendf(out, "\n");
1800 }
1801#endif
1802
1803 return 0;
1804}
1805
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001806
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001807/* IO handler of the details "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001808 * It uses a struct show_cert_ctx and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001809 */
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001810static int cli_io_handler_show_cert_detail(struct appctx *appctx)
1811{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001812 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001813 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001814 struct buffer *out = alloc_trash_chunk();
1815 int retval = 0;
1816
1817 if (!out)
1818 goto end_no_putchk;
1819
1820 chunk_appendf(out, "Filename: ");
1821 if (ckchs == ckchs_transaction.new_ckchs)
1822 chunk_appendf(out, "*");
1823 chunk_appendf(out, "%s\n", ckchs->path);
1824
1825 chunk_appendf(out, "Status: ");
1826 if (ckchs->ckch->cert == NULL)
1827 chunk_appendf(out, "Empty\n");
1828 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
1829 chunk_appendf(out, "Unused\n");
1830 else
1831 chunk_appendf(out, "Used\n");
1832
1833 retval = show_cert_detail(ckchs->ckch->cert, ckchs->ckch->chain, out);
1834 if (retval < 0)
1835 goto end_no_putchk;
1836 else if (retval)
1837 goto end;
1838
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001839 ckch_store_show_ocsp_certid(ckchs, out);
1840
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001841end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001842 if (applet_putchk(appctx, out) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001843 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001844
1845end_no_putchk:
William Lallemandda8584c2020-05-14 10:14:37 +02001846 free_trash_chunk(out);
1847 return 1;
1848yield:
William Lallemandda8584c2020-05-14 10:14:37 +02001849 free_trash_chunk(out);
1850 return 0; /* should come back */
1851}
1852
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001853
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001854/* IO handler of the details "show ssl cert <filename.ocsp>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001855 * It uses a show_cert_ctx.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001856 */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001857static int cli_io_handler_show_cert_ocsp_detail(struct appctx *appctx)
1858{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001859#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL)
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001860 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001861 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001862 struct buffer *out = alloc_trash_chunk();
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001863 int from_transaction = ctx->transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001864
1865 if (!out)
1866 goto end_no_putchk;
1867
1868 /* If we try to display an ongoing transaction's OCSP response, we
1869 * need to dump the ckch's ocsp_response buffer directly.
1870 * Otherwise, we must rebuild the certificate's certid in order to
1871 * look for the current OCSP response in the tree. */
1872 if (from_transaction && ckchs->ckch->ocsp_response) {
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001873 if (ssl_ocsp_response_print(ckchs->ckch->ocsp_response, out))
1874 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001875 }
1876 else {
1877 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1878 unsigned int key_length = 0;
1879
1880 if (ckch_store_build_certid(ckchs, (unsigned char*)key, &key_length) < 0)
1881 goto end_no_putchk;
1882
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001883 if (ssl_get_ocspresponse_detail(key, out))
1884 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001885 }
1886
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001887 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001888 goto yield;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001889
1890end_no_putchk:
1891 free_trash_chunk(out);
1892 return 1;
1893yield:
1894 free_trash_chunk(out);
1895 return 0; /* should come back */
1896#else
1897 return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n");
1898#endif
1899}
1900
William Lallemandda8584c2020-05-14 10:14:37 +02001901/* parsing function for 'show ssl cert [certfile]' */
1902static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
1903{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001904 struct show_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02001905 struct ckch_store *ckchs;
1906
1907 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1908 return cli_err(appctx, "Can't allocate memory!\n");
1909
1910 /* The operations on the CKCH architecture are locked so we can
1911 * manipulate ckch_store and ckch_inst */
1912 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1913 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
1914
1915 /* check if there is a certificate to lookup */
1916 if (*args[3]) {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001917 int show_ocsp_detail = 0;
1918 int from_transaction = 0;
1919 char *end;
1920
1921 /* We manage the special case "certname.ocsp" through which we
1922 * can show the details of an OCSP response. */
1923 end = strrchr(args[3], '.');
1924 if (end && strcmp(end+1, "ocsp") == 0) {
1925 *end = '\0';
1926 show_ocsp_detail = 1;
1927 }
1928
William Lallemandda8584c2020-05-14 10:14:37 +02001929 if (*args[3] == '*') {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001930 from_transaction = 1;
William Lallemandda8584c2020-05-14 10:14:37 +02001931 if (!ckchs_transaction.new_ckchs)
1932 goto error;
1933
1934 ckchs = ckchs_transaction.new_ckchs;
1935
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001936 if (strcmp(args[3] + 1, ckchs->path) != 0)
William Lallemandda8584c2020-05-14 10:14:37 +02001937 goto error;
1938
1939 } else {
1940 if ((ckchs = ckchs_lookup(args[3])) == NULL)
1941 goto error;
1942
1943 }
1944
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001945 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001946 /* use the IO handler that shows details */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001947 if (show_ocsp_detail) {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001948 ctx->transaction = from_transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001949 appctx->io_handler = cli_io_handler_show_cert_ocsp_detail;
1950 }
1951 else
1952 appctx->io_handler = cli_io_handler_show_cert_detail;
William Lallemandda8584c2020-05-14 10:14:37 +02001953 }
1954
1955 return 0;
1956
1957error:
1958 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1959 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
1960}
1961
1962/* release function of the `set ssl cert' command, free things and unlock the spinlock */
1963static void cli_release_commit_cert(struct appctx *appctx)
1964{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02001965 struct commit_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001966
1967 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulet9d56e242022-05-31 16:37:01 +02001968 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
1969 if (ctx->new_ckchs)
1970 ckch_store_free(ctx->new_ckchs);
1971 ha_free(&ctx->err);
William Lallemandda8584c2020-05-14 10:14:37 +02001972}
1973
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001974
1975/*
1976 * Rebuild a new instance 'new_inst' based on an old instance 'ckchi' and a
1977 * specific ckch_store.
1978 * Returns 0 in case of success, 1 otherwise.
1979 */
William Lallemande60c7d62022-03-30 11:26:15 +02001980int ckch_inst_rebuild(struct ckch_store *ckch_store, struct ckch_inst *ckchi,
1981 struct ckch_inst **new_inst, char **err)
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001982{
1983 int retval = 0;
1984 int errcode = 0;
1985 struct sni_ctx *sc0, *sc0s;
1986 char **sni_filter = NULL;
1987 int fcount = 0;
1988
1989 if (ckchi->crtlist_entry) {
1990 sni_filter = ckchi->crtlist_entry->filters;
1991 fcount = ckchi->crtlist_entry->fcount;
1992 }
1993
1994 if (ckchi->is_server_instance)
1995 errcode |= ckch_inst_new_load_srv_store(ckch_store->path, ckch_store, new_inst, err);
1996 else
1997 errcode |= ckch_inst_new_load_store(ckch_store->path, ckch_store, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, new_inst, err);
1998
1999 if (errcode & ERR_CODE)
2000 return 1;
2001
2002 /* if the previous ckchi was used as the default */
2003 if (ckchi->is_default)
2004 (*new_inst)->is_default = 1;
2005
2006 (*new_inst)->is_server_instance = ckchi->is_server_instance;
2007 (*new_inst)->server = ckchi->server;
2008 /* Create a new SSL_CTX and link it to the new instance. */
2009 if ((*new_inst)->is_server_instance) {
2010 retval = ssl_sock_prep_srv_ctx_and_inst(ckchi->server, (*new_inst)->ctx, (*new_inst));
2011 if (retval)
2012 return 1;
2013 }
2014
2015 /* create the link to the crtlist_entry */
2016 (*new_inst)->crtlist_entry = ckchi->crtlist_entry;
2017
2018 /* we need to initialize the SSL_CTX generated */
2019 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
2020 list_for_each_entry_safe(sc0, sc0s, &(*new_inst)->sni_ctx, by_ckch_inst) {
2021 if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
2022 errcode |= ssl_sock_prep_ctx_and_inst(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, *new_inst, err);
2023 if (errcode & ERR_CODE)
2024 return 1;
2025 }
2026 }
2027
2028 return 0;
2029}
2030
2031/*
2032 * Load all the new SNIs of a newly built ckch instance in the trees, or replace
2033 * a server's main ckch instance.
2034 */
2035static void __ssl_sock_load_new_ckch_instance(struct ckch_inst *ckchi)
2036{
2037 /* The bind_conf will be null on server ckch_instances. */
2038 if (ckchi->is_server_instance) {
2039 int i;
2040 /* a lock is needed here since we have to free the SSL cache */
2041 HA_RWLOCK_WRLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
2042 /* free the server current SSL_CTX */
2043 SSL_CTX_free(ckchi->server->ssl_ctx.ctx);
2044 /* Actual ssl context update */
2045 SSL_CTX_up_ref(ckchi->ctx);
2046 ckchi->server->ssl_ctx.ctx = ckchi->ctx;
2047 ckchi->server->ssl_ctx.inst = ckchi;
2048
2049 /* flush the session cache of the server */
2050 for (i = 0; i < global.nbthread; i++) {
William Lallemandce990332021-11-23 15:15:09 +01002051 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].sni);
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002052 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].ptr);
2053 }
2054 HA_RWLOCK_WRUNLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
2055
2056 } else {
2057 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
2058 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
2059 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
2060 }
2061}
2062
2063/*
2064 * Delete a ckch instance that was replaced after a CLI command.
2065 */
2066static void __ckch_inst_free_locked(struct ckch_inst *ckchi)
2067{
2068 if (ckchi->is_server_instance) {
2069 /* no lock for servers */
2070 ckch_inst_free(ckchi);
2071 } else {
2072 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
2073
2074 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
2075 ckch_inst_free(ckchi);
2076 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
2077 }
2078}
2079
William Lallemand3b5a3a62022-03-29 14:29:31 +02002080/* Replace a ckch_store in the ckch tree and insert the whole dependencies,
2081* then free the previous dependencies and store.
2082* Used in the case of a certificate update.
2083*
2084* Every dependencies must allocated before using this function.
2085*
2086* This function can't fail as it only update pointers, and does not alloc anything.
2087*
2088* /!\ This function must be used under the ckch lock. /!\
2089*
2090* - Insert every dependencies (SNI, crtlist_entry, ckch_inst, etc)
2091* - Delete the old ckch_store from the tree
2092* - Insert the new ckch_store
2093* - Free the old dependencies and the old ckch_store
2094*/
2095void ckch_store_replace(struct ckch_store *old_ckchs, struct ckch_store *new_ckchs)
2096{
2097 struct crtlist_entry *entry;
2098 struct ckch_inst *ckchi, *ckchis;
2099
2100 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
2101 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
2102 ebpt_delete(&entry->node);
2103 /* change the ptr and reinsert the node */
2104 entry->node.key = new_ckchs;
2105 ebpt_insert(&entry->crtlist->entries, &entry->node);
2106 }
2107 /* insert the new ckch_insts in the crtlist_entry */
2108 list_for_each_entry(ckchi, &new_ckchs->ckch_inst, by_ckchs) {
2109 if (ckchi->crtlist_entry)
2110 LIST_INSERT(&ckchi->crtlist_entry->ckch_inst, &ckchi->by_crtlist_entry);
2111 }
2112 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2113 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
2114 __ssl_sock_load_new_ckch_instance(ckchi);
2115 }
2116 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
2117 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
2118 __ckch_inst_free_locked(ckchi);
2119 }
2120
2121 ckch_store_free(old_ckchs);
2122 ebst_insert(&ckchs_tree, &new_ckchs->node);
2123}
2124
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002125
William Lallemandda8584c2020-05-14 10:14:37 +02002126/*
2127 * This function tries to create the new ckch_inst and their SNIs
William Lallemand30fcca12022-03-30 12:03:12 +02002128 *
2129 * /!\ don't forget to update __hlua_ckch_commit() if you changes things there. /!\
William Lallemandda8584c2020-05-14 10:14:37 +02002130 */
2131static int cli_io_handler_commit_cert(struct appctx *appctx)
2132{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002133 struct commit_cert_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002134 struct stconn *sc = appctx_sc(appctx);
William Lallemandda8584c2020-05-14 10:14:37 +02002135 int y = 0;
William Lallemandda8584c2020-05-14 10:14:37 +02002136 struct ckch_store *old_ckchs, *new_ckchs = NULL;
William Lallemand3b5a3a62022-03-29 14:29:31 +02002137 struct ckch_inst *ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002138
Willy Tarreau475e4632022-05-27 10:26:46 +02002139 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Christopher Faulet9d56e242022-05-31 16:37:01 +02002140 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02002141
2142 while (1) {
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002143 switch (ctx->state) {
2144 case CERT_ST_INIT:
William Lallemandda8584c2020-05-14 10:14:37 +02002145 /* This state just print the update message */
Christopher Faulet9d56e242022-05-31 16:37:01 +02002146 chunk_printf(&trash, "Committing %s", ckchs_transaction.path);
2147 if (applet_putchk(appctx, &trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02002148 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002149
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002150 ctx->state = CERT_ST_GEN;
William Lallemandda8584c2020-05-14 10:14:37 +02002151 /* fallthrough */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002152 case CERT_ST_GEN:
William Lallemandda8584c2020-05-14 10:14:37 +02002153 /*
2154 * This state generates the ckch instances with their
2155 * sni_ctxs and SSL_CTX.
2156 *
2157 * Since the SSL_CTX generation can be CPU consumer, we
2158 * yield every 10 instances.
2159 */
2160
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002161 old_ckchs = ctx->old_ckchs;
2162 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002163
William Lallemandda8584c2020-05-14 10:14:37 +02002164 /* get the next ckchi to regenerate */
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002165 ckchi = ctx->next_ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002166 /* we didn't start yet, set it to the first elem */
2167 if (ckchi == NULL)
2168 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
2169
2170 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
2171 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
2172 struct ckch_inst *new_inst;
William Lallemandda8584c2020-05-14 10:14:37 +02002173
Christopher Faulet9d56e242022-05-31 16:37:01 +02002174 /* save the next ckchi to compute in case of yield */
2175 ctx->next_ckchi = ckchi;
2176
William Lallemandda8584c2020-05-14 10:14:37 +02002177 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2178 if (y >= 10) {
Christopher Faulet9d56e242022-05-31 16:37:01 +02002179 applet_have_more_data(appctx); /* let's come back later */
William Lallemandda8584c2020-05-14 10:14:37 +02002180 goto yield;
2181 }
2182
Christopher Faulet9d56e242022-05-31 16:37:01 +02002183 /* display one dot per new instance */
2184 if (applet_putstr(appctx, ".") == -1)
2185 goto yield;
2186
2187 ctx->err = NULL;
2188 if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &ctx->err)) {
2189 ctx->state = CERT_ST_ERROR;
William Lallemandda8584c2020-05-14 10:14:37 +02002190 goto error;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002191 }
William Lallemandda8584c2020-05-14 10:14:37 +02002192
William Lallemandda8584c2020-05-14 10:14:37 +02002193 /* link the new ckch_inst to the duplicate */
Willy Tarreau2b718102021-04-21 07:32:39 +02002194 LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002195 y++;
2196 }
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002197 ctx->state = CERT_ST_INSERT;
William Lallemandda8584c2020-05-14 10:14:37 +02002198 /* fallthrough */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002199 case CERT_ST_INSERT:
William Lallemandda8584c2020-05-14 10:14:37 +02002200 /* The generation is finished, we can insert everything */
2201
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002202 old_ckchs = ctx->old_ckchs;
2203 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002204
William Lallemand3b5a3a62022-03-29 14:29:31 +02002205 /* insert everything and remove the previous objects */
2206 ckch_store_replace(old_ckchs, new_ckchs);
Christopher Faulet9d56e242022-05-31 16:37:01 +02002207 ctx->new_ckchs = ctx->old_ckchs = NULL;
2208 ctx->state = CERT_ST_SUCCESS;
2209 /* fallthrough */
2210 case CERT_ST_SUCCESS:
2211 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2212 goto yield;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002213 ctx->state = CERT_ST_FIN;
William Lallemandda8584c2020-05-14 10:14:37 +02002214 /* fallthrough */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002215 case CERT_ST_FIN:
William Lallemandda8584c2020-05-14 10:14:37 +02002216 /* we achieved the transaction, we can set everything to NULL */
William Lallemandda8584c2020-05-14 10:14:37 +02002217 ckchs_transaction.new_ckchs = NULL;
2218 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002219 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002220 goto end;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002221
2222 case CERT_ST_ERROR:
2223 error:
2224 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2225 if (applet_putchk(appctx, &trash) == -1)
2226 goto yield;
2227 ctx->state = CERT_ST_FIN;
2228 break;
William Lallemandda8584c2020-05-14 10:14:37 +02002229 }
2230 }
2231end:
William Lallemandda8584c2020-05-14 10:14:37 +02002232 /* success: call the release function and don't come back */
2233 return 1;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002234
William Lallemandda8584c2020-05-14 10:14:37 +02002235yield:
William Lallemandda8584c2020-05-14 10:14:37 +02002236 return 0; /* should come back */
William Lallemandda8584c2020-05-14 10:14:37 +02002237}
2238
2239/*
2240 * Parsing function of 'commit ssl cert'
2241 */
2242static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
2243{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002244 struct commit_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02002245 char *err = NULL;
2246
2247 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2248 return 1;
2249
2250 if (!*args[3])
2251 return cli_err(appctx, "'commit ssl cert expects a filename\n");
2252
2253 /* The operations on the CKCH architecture are locked so we can
2254 * manipulate ckch_store and ckch_inst */
2255 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2256 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
2257
2258 if (!ckchs_transaction.path) {
2259 memprintf(&err, "No ongoing transaction! !\n");
2260 goto error;
2261 }
2262
2263 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2264 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
2265 goto error;
2266 }
2267
William Lallemand5685ccf2020-09-16 16:12:25 +02002268 /* if a certificate is here, a private key must be here too */
2269 if (ckchs_transaction.new_ckchs->ckch->cert && !ckchs_transaction.new_ckchs->ckch->key) {
2270 memprintf(&err, "The transaction must contain at least a certificate and a private key!\n");
2271 goto error;
2272 }
William Lallemanda9419522020-06-24 16:26:41 +02002273
William Lallemand5685ccf2020-09-16 16:12:25 +02002274 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
2275 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
2276 goto error;
William Lallemandda8584c2020-05-14 10:14:37 +02002277 }
2278
2279 /* init the appctx structure */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002280 ctx->state = CERT_ST_INIT;
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002281 ctx->next_ckchi = NULL;
2282 ctx->new_ckchs = ckchs_transaction.new_ckchs;
2283 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002284
2285 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
2286 return 0;
2287
2288error:
2289
2290 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2291 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2292
2293 return cli_dynerr(appctx, err);
2294}
2295
2296
2297
2298
2299/*
2300 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
Willy Tarreau329f4b42022-05-04 20:05:55 +02002301 * It uses a set_cert_ctx context, and ckchs_transaction under a lock.
William Lallemandda8584c2020-05-14 10:14:37 +02002302 */
2303static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
2304{
2305 struct ckch_store *new_ckchs = NULL;
2306 struct ckch_store *old_ckchs = NULL;
2307 char *err = NULL;
2308 int i;
William Lallemandda8584c2020-05-14 10:14:37 +02002309 int errcode = 0;
2310 char *end;
William Lallemandff8bf982022-03-29 10:44:23 +02002311 struct cert_exts *cert_ext = &cert_exts[0]; /* default one, PEM */
William Lallemandda8584c2020-05-14 10:14:37 +02002312 struct cert_key_and_chain *ckch;
2313 struct buffer *buf;
2314
2315 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2316 return 1;
2317
William Lallemandda8584c2020-05-14 10:14:37 +02002318 if (!*args[3] || !payload)
2319 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
2320
2321 /* The operations on the CKCH architecture are locked so we can
2322 * manipulate ckch_store and ckch_inst */
2323 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2324 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
2325
William Lallemand5ba80d62021-05-04 16:17:27 +02002326 if ((buf = alloc_trash_chunk()) == NULL) {
2327 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2328 errcode |= ERR_ALERT | ERR_FATAL;
2329 goto end;
2330 }
William Lallemande5ff4ad2020-06-08 09:40:37 +02002331
William Lallemandda8584c2020-05-14 10:14:37 +02002332 if (!chunk_strcpy(buf, args[3])) {
2333 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2334 errcode |= ERR_ALERT | ERR_FATAL;
2335 goto end;
2336 }
2337
2338 /* check which type of file we want to update */
William Lallemandff8bf982022-03-29 10:44:23 +02002339 for (i = 0; cert_exts[i].ext != NULL; i++) {
William Lallemandda8584c2020-05-14 10:14:37 +02002340 end = strrchr(buf->area, '.');
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002341 if (end && *cert_exts[i].ext && (strcmp(end + 1, cert_exts[i].ext) == 0)) {
William Lallemandda8584c2020-05-14 10:14:37 +02002342 *end = '\0';
William Lallemand089c1382020-10-23 17:35:12 +02002343 buf->data = strlen(buf->area);
William Lallemandff8bf982022-03-29 10:44:23 +02002344 cert_ext = &cert_exts[i];
William Lallemandda8584c2020-05-14 10:14:37 +02002345 break;
2346 }
2347 }
2348
William Lallemandda8584c2020-05-14 10:14:37 +02002349 /* if there is an ongoing transaction */
2350 if (ckchs_transaction.path) {
William Lallemandda8584c2020-05-14 10:14:37 +02002351 /* if there is an ongoing transaction, check if this is the same file */
2352 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
William Lallemand089c1382020-10-23 17:35:12 +02002353 /* we didn't find the transaction, must try more cases below */
2354
2355 /* 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 +02002356 if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
William Lallemand089c1382020-10-23 17:35:12 +02002357 if (!chunk_strcat(buf, ".crt")) {
2358 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2359 errcode |= ERR_ALERT | ERR_FATAL;
2360 goto end;
2361 }
2362
2363 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
2364 /* remove .crt of the error message */
2365 *(b_orig(buf) + b_data(buf) + strlen(".crt")) = '\0';
2366 b_sub(buf, strlen(".crt"));
2367
2368 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
2369 errcode |= ERR_ALERT | ERR_FATAL;
2370 goto end;
2371 }
2372 }
William Lallemandda8584c2020-05-14 10:14:37 +02002373 }
2374
Christopher Faulet24a20b92022-06-03 11:50:40 +02002375 old_ckchs = ckchs_transaction.new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002376
2377 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002378
William Lallemand95fefa12020-09-09 12:01:33 +02002379 /* lookup for the certificate in the tree */
Christopher Faulet24a20b92022-06-03 11:50:40 +02002380 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002381
Christopher Faulet24a20b92022-06-03 11:50:40 +02002382 if (!old_ckchs) {
William Lallemand089c1382020-10-23 17:35:12 +02002383 /* 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 +02002384 if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
William Lallemand089c1382020-10-23 17:35:12 +02002385 if (!chunk_strcat(buf, ".crt")) {
2386 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2387 errcode |= ERR_ALERT | ERR_FATAL;
2388 goto end;
2389 }
Christopher Faulet24a20b92022-06-03 11:50:40 +02002390 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002391 }
2392 }
William Lallemandda8584c2020-05-14 10:14:37 +02002393 }
2394
Christopher Faulet24a20b92022-06-03 11:50:40 +02002395 if (!old_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02002396 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
2397 err ? err : "");
2398 errcode |= ERR_ALERT | ERR_FATAL;
2399 goto end;
2400 }
2401
William Lallemandda8584c2020-05-14 10:14:37 +02002402 /* duplicate the ckch store */
2403 new_ckchs = ckchs_dup(old_ckchs);
2404 if (!new_ckchs) {
2405 memprintf(&err, "%sCannot allocate memory!\n",
2406 err ? err : "");
2407 errcode |= ERR_ALERT | ERR_FATAL;
2408 goto end;
2409 }
2410
William Lallemand95fefa12020-09-09 12:01:33 +02002411 ckch = new_ckchs->ckch;
William Lallemandda8584c2020-05-14 10:14:37 +02002412
2413 /* appply the change on the duplicate */
William Lallemandff8bf982022-03-29 10:44:23 +02002414 if (cert_ext->load(buf->area, payload, ckch, &err) != 0) {
William Lallemandda8584c2020-05-14 10:14:37 +02002415 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
2416 errcode |= ERR_ALERT | ERR_FATAL;
2417 goto end;
2418 }
2419
William Lallemandda8584c2020-05-14 10:14:37 +02002420 /* we succeed, we can save the ckchs in the transaction */
2421
2422 /* if there wasn't a transaction, update the old ckchs */
2423 if (!ckchs_transaction.old_ckchs) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002424 ckchs_transaction.old_ckchs = old_ckchs;
2425 ckchs_transaction.path = old_ckchs->path;
William Lallemandda8584c2020-05-14 10:14:37 +02002426 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
2427 } else {
2428 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
2429
2430 }
2431
2432 /* free the previous ckchs if there was a transaction */
2433 ckch_store_free(ckchs_transaction.new_ckchs);
2434
Christopher Faulet24a20b92022-06-03 11:50:40 +02002435 ckchs_transaction.new_ckchs = new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002436
2437
2438 /* creates the SNI ctxs later in the IO handler */
2439
2440end:
2441 free_trash_chunk(buf);
2442
2443 if (errcode & ERR_CODE) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002444 ckch_store_free(new_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002445 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2446 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2447 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002448 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2449 return cli_dynmsg(appctx, LOG_NOTICE, err);
2450 }
2451 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
2452}
2453
2454/* parsing function of 'abort ssl cert' */
2455static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
2456{
2457 char *err = NULL;
2458
2459 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2460 return 1;
2461
2462 if (!*args[3])
2463 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
2464
2465 /* The operations on the CKCH architecture are locked so we can
2466 * manipulate ckch_store and ckch_inst */
2467 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2468 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2469
2470 if (!ckchs_transaction.path) {
2471 memprintf(&err, "No ongoing transaction!\n");
2472 goto error;
2473 }
2474
2475 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2476 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
2477 goto error;
2478 }
2479
2480 /* Only free the ckchs there, because the SNI and instances were not generated yet */
2481 ckch_store_free(ckchs_transaction.new_ckchs);
2482 ckchs_transaction.new_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002483 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002484 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002485
2486 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2487
2488 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2489 return cli_dynmsg(appctx, LOG_NOTICE, err);
2490
2491error:
2492 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2493
2494 return cli_dynerr(appctx, err);
2495}
2496
2497/* parsing function of 'new ssl cert' */
2498static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
2499{
2500 struct ckch_store *store;
2501 char *err = NULL;
2502 char *path;
2503
2504 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2505 return 1;
2506
2507 if (!*args[3])
2508 return cli_err(appctx, "'new ssl cert' expects a filename\n");
2509
2510 path = args[3];
2511
2512 /* The operations on the CKCH architecture are locked so we can
2513 * manipulate ckch_store and ckch_inst */
2514 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2515 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
2516
2517 store = ckchs_lookup(path);
2518 if (store != NULL) {
2519 memprintf(&err, "Certificate '%s' already exists!\n", path);
2520 store = NULL; /* we don't want to free it */
2521 goto error;
2522 }
2523 /* we won't support multi-certificate bundle here */
William Lallemandbd8e6ed2020-09-16 16:08:08 +02002524 store = ckch_store_new(path);
William Lallemandda8584c2020-05-14 10:14:37 +02002525 if (!store) {
2526 memprintf(&err, "unable to allocate memory.\n");
2527 goto error;
2528 }
2529
2530 /* insert into the ckchs tree */
2531 ebst_insert(&ckchs_tree, &store->node);
2532 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
2533
2534 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2535 return cli_dynmsg(appctx, LOG_NOTICE, err);
2536error:
2537 free(store);
2538 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2539 return cli_dynerr(appctx, err);
2540}
2541
2542/* parsing function of 'del ssl cert' */
2543static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
2544{
2545 struct ckch_store *store;
2546 char *err = NULL;
2547 char *filename;
2548
2549 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2550 return 1;
2551
2552 if (!*args[3])
2553 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
2554
2555 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2556 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
2557
2558 filename = args[3];
2559
Christopher Faulet926fefc2022-05-31 18:04:25 +02002560 if (ckchs_transaction.path && strcmp(ckchs_transaction.path, filename) == 0) {
2561 memprintf(&err, "ongoing transaction for the certificate '%s'", filename);
2562 goto error;
2563 }
2564
William Lallemandda8584c2020-05-14 10:14:37 +02002565 store = ckchs_lookup(filename);
2566 if (store == NULL) {
2567 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
2568 goto error;
2569 }
2570 if (!LIST_ISEMPTY(&store->ckch_inst)) {
2571 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
2572 goto error;
2573 }
2574
2575 ebmb_delete(&store->node);
2576 ckch_store_free(store);
2577
2578 memprintf(&err, "Certificate '%s' deleted!\n", filename);
2579
2580 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2581 return cli_dynmsg(appctx, LOG_NOTICE, err);
2582
2583error:
2584 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
2585 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2586 return cli_dynerr(appctx, err);
2587}
2588
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002589
Remi Tricot-Le Breton9f40fe02021-03-16 16:21:27 +01002590
2591/* parsing function of 'new ssl ca-file' */
2592static int cli_parse_new_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2593{
2594 struct cafile_entry *cafile_entry;
2595 char *err = NULL;
2596 char *path;
2597
2598 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2599 return 1;
2600
2601 if (!*args[3])
2602 return cli_err(appctx, "'new ssl ca-file' expects a filename\n");
2603
2604 path = args[3];
2605
2606 /* The operations on the CKCH architecture are locked so we can
2607 * manipulate ckch_store and ckch_inst */
2608 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2609 return cli_err(appctx, "Can't create a CA file!\nOperations on certificates are currently locked!\n");
2610
2611 cafile_entry = ssl_store_get_cafile_entry(path, 0);
2612 if (cafile_entry) {
2613 memprintf(&err, "CA file '%s' already exists!\n", path);
2614 goto error;
2615 }
2616
2617 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CERT);
2618 if (!cafile_entry) {
2619 memprintf(&err, "%sCannot allocate memory!\n",
2620 err ? err : "");
2621 goto error;
2622 }
2623
2624 /* Add the newly created cafile_entry to the tree so that
2625 * any new ckch instance created from now can use it. */
2626 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
2627 goto error;
2628
2629 memprintf(&err, "New CA file created '%s'!\n", path);
2630
2631 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2632 return cli_dynmsg(appctx, LOG_NOTICE, err);
2633error:
2634 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2635 return cli_dynerr(appctx, err);
2636}
2637
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002638/*
2639 * Parsing function of `set ssl ca-file`
2640 */
2641static int cli_parse_set_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2642{
Christopher Faulet132c5952022-06-03 11:56:26 +02002643 struct cafile_entry *old_cafile_entry = NULL;
2644 struct cafile_entry *new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002645 char *err = NULL;
2646 int errcode = 0;
2647 struct buffer *buf;
William Lallemand62c0b992022-07-29 17:50:58 +02002648 int add_cmd = 0;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002649
2650 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2651 return 1;
2652
William Lallemand62c0b992022-07-29 17:50:58 +02002653 /* this is "add ssl ca-file" */
2654 if (*args[0] == 'a')
2655 add_cmd = 1;
2656
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002657 if (!*args[3] || !payload)
2658 return cli_err(appctx, "'set ssl ca-file expects a filename and CAs as a payload\n");
2659
2660 /* The operations on the CKCH architecture are locked so we can
2661 * manipulate ckch_store and ckch_inst */
2662 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2663 return cli_err(appctx, "Can't update the CA file!\nOperations on certificates are currently locked!\n");
2664
2665 if ((buf = alloc_trash_chunk()) == NULL) {
2666 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2667 errcode |= ERR_ALERT | ERR_FATAL;
2668 goto end;
2669 }
2670
2671 if (!chunk_strcpy(buf, args[3])) {
2672 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2673 errcode |= ERR_ALERT | ERR_FATAL;
2674 goto end;
2675 }
2676
Christopher Faulet132c5952022-06-03 11:56:26 +02002677 old_cafile_entry = NULL;
2678 new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002679
2680 /* if there is an ongoing transaction */
2681 if (cafile_transaction.path) {
2682 /* if there is an ongoing transaction, check if this is the same file */
2683 if (strcmp(cafile_transaction.path, buf->area) != 0) {
2684 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, buf->area);
2685 errcode |= ERR_ALERT | ERR_FATAL;
2686 goto end;
2687 }
Christopher Faulet132c5952022-06-03 11:56:26 +02002688 old_cafile_entry = cafile_transaction.old_cafile_entry;
William Lallemand62c0b992022-07-29 17:50:58 +02002689 } else {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002690 /* lookup for the certificate in the tree */
Christopher Faulet132c5952022-06-03 11:56:26 +02002691 old_cafile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002692 }
2693
Christopher Faulet132c5952022-06-03 11:56:26 +02002694 if (!old_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002695 memprintf(&err, "%sCan't replace a CA file which is not referenced by the configuration!\n",
2696 err ? err : "");
2697 errcode |= ERR_ALERT | ERR_FATAL;
2698 goto end;
2699 }
2700
William Lallemand62c0b992022-07-29 17:50:58 +02002701 /* if the transaction is new, duplicate the old_ca_file_entry, otherwise duplicate the cafile in the current transaction */
2702 if (cafile_transaction.new_cafile_entry)
2703 new_cafile_entry = ssl_store_dup_cafile_entry(cafile_transaction.new_cafile_entry);
2704 else
2705 new_cafile_entry = ssl_store_dup_cafile_entry(old_cafile_entry);
2706
Christopher Faulet132c5952022-06-03 11:56:26 +02002707 if (!new_cafile_entry) {
William Lallemand62c0b992022-07-29 17:50:58 +02002708 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002709 errcode |= ERR_ALERT | ERR_FATAL;
2710 goto end;
2711 }
2712
William Lallemand62c0b992022-07-29 17:50:58 +02002713 /* Fill the new entry with the new CAs. The add_cmd variable determine
2714 if we flush the X509_STORE or not */
2715 if (ssl_store_load_ca_from_buf(new_cafile_entry, payload, add_cmd)) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002716 memprintf(&err, "%sInvalid payload\n", err ? err : "");
2717 errcode |= ERR_ALERT | ERR_FATAL;
2718 goto end;
2719 }
2720
2721 /* we succeed, we can save the ca in the transaction */
2722
2723 /* if there wasn't a transaction, update the old CA */
2724 if (!cafile_transaction.old_cafile_entry) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002725 cafile_transaction.old_cafile_entry = old_cafile_entry;
2726 cafile_transaction.path = old_cafile_entry->path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002727 err = memprintf(&err, "transaction created for CA %s!\n", cafile_transaction.path);
2728 } else {
2729 err = memprintf(&err, "transaction updated for CA %s!\n", cafile_transaction.path);
2730 }
2731
2732 /* free the previous CA if there was a transaction */
2733 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2734
Christopher Faulet132c5952022-06-03 11:56:26 +02002735 cafile_transaction.new_cafile_entry = new_cafile_entry;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002736
2737 /* creates the SNI ctxs later in the IO handler */
2738
2739end:
2740 free_trash_chunk(buf);
2741
2742 if (errcode & ERR_CODE) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002743 ssl_store_delete_cafile_entry(new_cafile_entry);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002744 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2745 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2746 } else {
2747
2748 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2749 return cli_dynmsg(appctx, LOG_NOTICE, err);
2750 }
2751}
2752
2753
2754/*
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002755 * Parsing function of 'commit ssl ca-file'.
2756 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl crl-file".
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002757 */
2758static int cli_parse_commit_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2759{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002760 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002761 char *err = NULL;
2762
2763 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2764 return 1;
2765
2766 if (!*args[3])
2767 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
2768
2769 /* The operations on the CKCH architecture are locked so we can
2770 * manipulate ckch_store and ckch_inst */
2771 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2772 return cli_err(appctx, "Can't commit the CA file!\nOperations on certificates are currently locked!\n");
2773
2774 if (!cafile_transaction.path) {
2775 memprintf(&err, "No ongoing transaction! !\n");
2776 goto error;
2777 }
2778
2779 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2780 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, args[3]);
2781 goto error;
2782 }
2783 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002784 ctx->state = CACRL_ST_INIT;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002785 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002786 ctx->old_entry = cafile_transaction.old_cafile_entry;
2787 ctx->new_entry = cafile_transaction.new_cafile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002788 ctx->cafile_type = CAFILE_CERT;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002789
2790 return 0;
2791
2792error:
2793
2794 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2795 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2796
2797 return cli_dynerr(appctx, err);
2798}
2799
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002800/*
2801 * This function tries to create new ckch instances and their SNIs using a newly
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002802 * set certificate authority (CA file) or a newly set Certificate Revocation
2803 * List (CRL), depending on the command being called.
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002804 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002805static int cli_io_handler_commit_cafile_crlfile(struct appctx *appctx)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002806{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002807 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002808 struct stconn *sc = appctx_sc(appctx);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002809 int y = 0;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002810 struct cafile_entry *old_cafile_entry = ctx->old_entry;
2811 struct cafile_entry *new_cafile_entry = ctx->new_entry;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002812 struct ckch_inst_link *ckchi_link;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002813 char *path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002814
Willy Tarreau475e4632022-05-27 10:26:46 +02002815 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002816 goto end;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002817
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002818 /* The ctx was already validated by the ca-file/crl-file parsing
2819 * function. Entries can only be NULL in CACRL_ST_SUCCESS or
2820 * CACRL_ST_FIN states
2821 */
2822 switch (ctx->cafile_type) {
2823 case CAFILE_CERT:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002824 path = cafile_transaction.path;
2825 break;
2826 case CAFILE_CRL:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002827 path = crlfile_transaction.path;
2828 break;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002829 default:
Willy Tarreaud543ae02022-06-22 05:40:25 +02002830 path = NULL;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002831 goto error;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002832 }
2833
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002834 while (1) {
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002835 switch (ctx->state) {
2836 case CACRL_ST_INIT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002837 /* This state just print the update message */
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002838 chunk_printf(&trash, "Committing %s", path);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002839 if (applet_putchk(appctx, &trash) == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002840 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002841
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002842 ctx->state = CACRL_ST_GEN;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002843 /* fallthrough */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002844 case CACRL_ST_GEN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002845 /*
2846 * This state generates the ckch instances with their
2847 * sni_ctxs and SSL_CTX.
2848 *
2849 * Since the SSL_CTX generation can be CPU consumer, we
2850 * yield every 10 instances.
2851 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002852
2853 /* get the next ckchi to regenerate */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002854 ckchi_link = ctx->next_ckchi_link;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002855
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002856 /* we didn't start yet, set it to the first elem */
2857 if (ckchi_link == NULL) {
2858 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2859 /* Add the newly created cafile_entry to the tree so that
2860 * any new ckch instance created from now can use it. */
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002861 if (ssl_store_add_uncommitted_cafile_entry(new_cafile_entry)) {
2862 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002863 goto error;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002864 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002865 }
2866
2867 list_for_each_entry_from(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002868 struct ckch_inst *new_inst;
2869
2870 /* save the next ckchi to compute */
2871 ctx->next_ckchi_link = ckchi_link;
2872
2873 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2874 if (y >= 10) {
2875 applet_have_more_data(appctx); /* let's come back later */
2876 goto yield;
2877 }
2878
2879 /* display one dot per new instance */
2880 if (applet_putstr(appctx, ".") == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002881 goto yield;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002882
2883 /* Rebuild a new ckch instance that uses the same ckch_store
2884 * than a reference ckchi instance but will use a new CA file. */
2885 ctx->err = NULL;
2886 if (ckch_inst_rebuild(ckchi_link->ckch_inst->ckch_store, ckchi_link->ckch_inst, &new_inst, &ctx->err)) {
2887 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002888 goto error;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002889 }
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002890
2891 y++;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002892 }
2893
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002894 ctx->state = CACRL_ST_INSERT;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002895 /* fallthrough */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002896 case CACRL_ST_INSERT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002897 /* The generation is finished, we can insert everything */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002898
2899 /* insert the new ckch_insts in the crtlist_entry */
2900 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2901 if (ckchi_link->ckch_inst->crtlist_entry)
2902 LIST_INSERT(&ckchi_link->ckch_inst->crtlist_entry->ckch_inst,
2903 &ckchi_link->ckch_inst->by_crtlist_entry);
2904 }
2905
2906 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2907 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2908 __ssl_sock_load_new_ckch_instance(ckchi_link->ckch_inst);
2909 }
2910
2911 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
2912 list_for_each_entry(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
2913 __ckch_inst_free_locked(ckchi_link->ckch_inst);
2914 }
2915
2916
2917 /* Remove the old cafile entry from the tree */
2918 ebmb_delete(&old_cafile_entry->node);
2919 ssl_store_delete_cafile_entry(old_cafile_entry);
2920
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002921 ctx->old_entry = ctx->new_entry = NULL;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002922 ctx->state = CACRL_ST_SUCCESS;
2923 /* fallthrough */
2924 case CACRL_ST_SUCCESS:
2925 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2926 goto yield;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002927 ctx->state = CACRL_ST_FIN;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002928 /* fallthrough */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002929 case CACRL_ST_FIN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002930 /* we achieved the transaction, we can set everything to NULL */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002931 switch (ctx->cafile_type) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002932 case CAFILE_CERT:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002933 cafile_transaction.old_cafile_entry = NULL;
2934 cafile_transaction.new_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002935 cafile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002936 break;
2937 case CAFILE_CRL:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002938 crlfile_transaction.old_crlfile_entry = NULL;
2939 crlfile_transaction.new_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002940 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002941 break;
2942 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002943 goto end;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002944
2945 case CACRL_ST_ERROR:
2946 error:
2947 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2948 if (applet_putchk(appctx, &trash) == -1)
2949 goto yield;
2950 ctx->state = CACRL_ST_FIN;
2951 break;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002952 }
2953 }
2954end:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002955 /* success: call the release function and don't come back */
2956 return 1;
2957yield:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002958 return 0; /* should come back */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002959}
2960
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002961
2962/* parsing function of 'abort ssl ca-file' */
2963static int cli_parse_abort_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2964{
2965 char *err = NULL;
2966
2967 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2968 return 1;
2969
2970 if (!*args[3])
2971 return cli_err(appctx, "'abort ssl ca-file' expects a filename\n");
2972
2973 /* The operations on the CKCH architecture are locked so we can
2974 * manipulate ckch_store and ckch_inst */
2975 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2976 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2977
2978 if (!cafile_transaction.path) {
2979 memprintf(&err, "No ongoing transaction!\n");
2980 goto error;
2981 }
2982
2983 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2984 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", cafile_transaction.path, args[3]);
2985 goto error;
2986 }
2987
2988 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
2989 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2990 cafile_transaction.new_cafile_entry = NULL;
2991 cafile_transaction.old_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002992 cafile_transaction.path = NULL;
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002993
2994 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2995
2996 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2997 return cli_dynmsg(appctx, LOG_NOTICE, err);
2998
2999error:
3000 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3001
3002 return cli_dynerr(appctx, err);
3003}
3004
Willy Tarreau821c3b02022-05-04 15:47:39 +02003005/* release function of the `commit ssl ca-file' command, free things and unlock the spinlock.
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003006 * It uses a commit_cacrlfile_ctx context.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003007 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003008static void cli_release_commit_cafile(struct appctx *appctx)
3009{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003010 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003011 struct cafile_entry *new_cafile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003012
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003013 /* Remove the uncommitted cafile_entry from the tree. */
3014 if (new_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003015 ebmb_delete(&new_cafile_entry->node);
3016 ssl_store_delete_cafile_entry(new_cafile_entry);
3017 }
3018 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003019 ha_free(&ctx->err);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003020}
3021
3022
Willy Tarreau821c3b02022-05-04 15:47:39 +02003023/* IO handler of details "show ssl ca-file <filename[:index]>".
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003024 * It uses a show_cafile_ctx context, and the global
3025 * cafile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003026 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003027static int cli_io_handler_show_cafile_detail(struct appctx *appctx)
3028{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003029 struct show_cafile_ctx *ctx = appctx->svcctx;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003030 struct cafile_entry *cafile_entry = ctx->cur_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003031 struct buffer *out = alloc_trash_chunk();
William Lallemand03a32e52022-04-26 18:17:15 +02003032 int i = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003033 X509 *cert;
3034 STACK_OF(X509_OBJECT) *objs;
3035 int retval = 0;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003036 int ca_index = ctx->ca_index;
3037 int show_all = ctx->show_all;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003038
3039 if (!out)
3040 goto end_no_putchk;
3041
3042 chunk_appendf(out, "Filename: ");
3043 if (cafile_entry == cafile_transaction.new_cafile_entry)
3044 chunk_appendf(out, "*");
3045 chunk_appendf(out, "%s\n", cafile_entry->path);
3046
3047 chunk_appendf(out, "Status: ");
3048 if (!cafile_entry->ca_store)
3049 chunk_appendf(out, "Empty\n");
3050 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3051 chunk_appendf(out, "Unused\n");
3052 else
3053 chunk_appendf(out, "Used\n");
3054
3055 if (!cafile_entry->ca_store)
3056 goto end;
3057
3058 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
William Lallemand03a32e52022-04-26 18:17:15 +02003059 for (i = ca_index; i < sk_X509_OBJECT_num(objs); i++) {
3060
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003061 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
3062 if (!cert)
3063 continue;
3064
William Lallemand03a32e52022-04-26 18:17:15 +02003065 /* file starts at line 1 */
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003066 chunk_appendf(out, " \nCertificate #%d:\n", i+1);
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003067 retval = show_cert_detail(cert, NULL, out);
3068 if (retval < 0)
3069 goto end_no_putchk;
William Lallemand03a32e52022-04-26 18:17:15 +02003070 else if (retval)
3071 goto yield;
3072
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003073 if (applet_putchk(appctx, out) == -1)
William Lallemand03a32e52022-04-26 18:17:15 +02003074 goto yield;
William Lallemand03a32e52022-04-26 18:17:15 +02003075
3076 if (!show_all) /* only need to dump one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003077 goto end;
3078 }
3079
3080end:
William Lallemand03a32e52022-04-26 18:17:15 +02003081 free_trash_chunk(out);
3082 return 1; /* end, don't come back */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003083
3084end_no_putchk:
3085 free_trash_chunk(out);
3086 return 1;
3087yield:
William Lallemand03a32e52022-04-26 18:17:15 +02003088 /* save the current state */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003089 ctx->ca_index = i;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003090 free_trash_chunk(out);
3091 return 0; /* should come back */
3092}
3093
3094
Willy Tarreau06305792022-05-04 15:57:30 +02003095/* parsing function for 'show ssl ca-file [cafile[:index]]'.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003096 * It prepares a show_cafile_ctx context, and checks the global
3097 * cafile_transaction under the ckch_lock (read only).
Willy Tarreau06305792022-05-04 15:57:30 +02003098 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003099static int cli_parse_show_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3100{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003101 struct show_cafile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003102 struct cafile_entry *cafile_entry;
William Lallemand03a32e52022-04-26 18:17:15 +02003103 int ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003104 char *colons;
3105 char *err = NULL;
3106
3107 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3108 return cli_err(appctx, "Can't allocate memory!\n");
3109
3110 /* The operations on the CKCH architecture are locked so we can
3111 * manipulate ckch_store and ckch_inst */
3112 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3113 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3114
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003115 ctx->show_all = 1; /* show all certificates */
3116 ctx->ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003117 /* check if there is a certificate to lookup */
3118 if (*args[3]) {
3119
3120 /* Look for an optional CA index after the CA file name */
3121 colons = strchr(args[3], ':');
3122 if (colons) {
3123 char *endptr;
3124
3125 ca_index = strtol(colons + 1, &endptr, 10);
3126 /* Indexes start at 1 */
3127 if (colons + 1 == endptr || *endptr != '\0' || ca_index <= 0) {
3128 memprintf(&err, "wrong CA index after colons in '%s'!", args[3]);
3129 goto error;
3130 }
3131 *colons = '\0';
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003132 ctx->ca_index = ca_index - 1; /* we start counting at 0 in the ca_store, but at 1 on the CLI */
3133 ctx->show_all = 0; /* show only one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003134 }
3135
3136 if (*args[3] == '*') {
3137 if (!cafile_transaction.new_cafile_entry)
3138 goto error;
3139
3140 cafile_entry = cafile_transaction.new_cafile_entry;
3141
3142 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3143 goto error;
3144
3145 } else {
3146 /* Get the "original" cafile_entry and not the
3147 * uncommitted one if it exists. */
3148 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CERT)
3149 goto error;
3150 }
3151
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003152 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003153 /* use the IO handler that shows details */
3154 appctx->io_handler = cli_io_handler_show_cafile_detail;
3155 }
3156
3157 return 0;
3158
3159error:
3160 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3161 if (err)
3162 return cli_dynerr(appctx, err);
3163 return cli_err(appctx, "Can't display the CA file : Not found!\n");
3164}
3165
3166
3167/* release function of the 'show ssl ca-file' command */
3168static void cli_release_show_cafile(struct appctx *appctx)
3169{
3170 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3171}
3172
3173
3174/* This function returns the number of certificates in a cafile_entry. */
3175static int get_certificate_count(struct cafile_entry *cafile_entry)
3176{
3177 int cert_count = 0;
3178 STACK_OF(X509_OBJECT) *objs;
3179
3180 if (cafile_entry && cafile_entry->ca_store) {
3181 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3182 if (objs)
3183 cert_count = sk_X509_OBJECT_num(objs);
3184 }
3185 return cert_count;
3186}
3187
3188/* IO handler of "show ssl ca-file". The command taking a specific CA file name
Willy Tarreau821c3b02022-05-04 15:47:39 +02003189 * is managed in cli_io_handler_show_cafile_detail.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003190 * It uses a show_cafile_ctx and the global cafile_transaction.new_cafile_entry
3191 * in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003192 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003193static int cli_io_handler_show_cafile(struct appctx *appctx)
3194{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003195 struct show_cafile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003196 struct buffer *trash = alloc_trash_chunk();
3197 struct ebmb_node *node;
Christopher Faulet677cb4f2022-06-03 16:25:35 +02003198 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003199
3200 if (trash == NULL)
3201 return 1;
3202
Christopher Faulet5a2154b2022-06-03 10:42:48 +02003203 if (!ctx->old_cafile_entry && cafile_transaction.old_cafile_entry) {
3204 chunk_appendf(trash, "# transaction\n");
3205 chunk_appendf(trash, "*%s", cafile_transaction.old_cafile_entry->path);
3206 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_transaction.new_cafile_entry));
3207 if (applet_putchk(appctx, trash) == -1)
3208 goto yield;
3209 ctx->old_cafile_entry = cafile_transaction.new_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003210 }
3211
3212 /* First time in this io_handler. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003213 if (!ctx->cur_cafile_entry) {
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003214 chunk_appendf(trash, "# filename\n");
3215 node = ebmb_first(&cafile_tree);
3216 } else {
3217 /* We yielded during a previous call. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003218 node = &ctx->cur_cafile_entry->node;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003219 }
3220
3221 while (node) {
3222 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3223 if (cafile_entry->type == CAFILE_CERT) {
3224 chunk_appendf(trash, "%s", cafile_entry->path);
3225
3226 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_entry));
3227 }
3228
3229 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003230 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003231 goto yield;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003232 }
3233
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003234 ctx->cur_cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003235 free_trash_chunk(trash);
3236 return 1;
3237yield:
3238
3239 free_trash_chunk(trash);
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003240 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003241 return 0; /* should come back */
3242}
3243
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003244/* parsing function of 'del ssl ca-file' */
3245static int cli_parse_del_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3246{
3247 struct cafile_entry *cafile_entry;
3248 char *err = NULL;
3249 char *filename;
3250
3251 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3252 return 1;
3253
3254 if (!*args[3])
3255 return cli_err(appctx, "'del ssl ca-file' expects a CA file name\n");
3256
3257 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3258 return cli_err(appctx, "Can't delete the CA file!\nOperations on certificates are currently locked!\n");
3259
3260 filename = args[3];
3261
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003262 if (cafile_transaction.path && strcmp(cafile_transaction.path, filename) == 0) {
3263 memprintf(&err, "ongoing transaction for the CA file '%s'", filename);
3264 goto error;
3265 }
3266
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003267 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3268 if (!cafile_entry) {
3269 memprintf(&err, "CA file '%s' doesn't exist!\n", filename);
3270 goto error;
3271 }
3272
3273 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3274 memprintf(&err, "CA file '%s' in use, can't be deleted!\n", filename);
3275 goto error;
3276 }
3277
3278 /* Remove the cafile_entry from the tree */
3279 ebmb_delete(&cafile_entry->node);
3280 ssl_store_delete_cafile_entry(cafile_entry);
3281
3282 memprintf(&err, "CA file '%s' deleted!\n", filename);
3283
3284 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3285 return cli_dynmsg(appctx, LOG_NOTICE, err);
3286
3287error:
3288 memprintf(&err, "Can't remove the CA file: %s\n", err ? err : "");
3289 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3290 return cli_dynerr(appctx, err);
3291}
3292
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003293/* parsing function of 'new ssl crl-file' */
3294static int cli_parse_new_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3295{
3296 struct cafile_entry *cafile_entry;
3297 char *err = NULL;
3298 char *path;
3299
3300 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3301 return 1;
3302
3303 if (!*args[3])
3304 return cli_err(appctx, "'new ssl crl-file' expects a filename\n");
3305
3306 path = args[3];
3307
3308 /* The operations on the CKCH architecture are locked so we can
3309 * manipulate ckch_store and ckch_inst */
3310 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003311 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 +02003312
3313 cafile_entry = ssl_store_get_cafile_entry(path, 0);
3314 if (cafile_entry) {
3315 memprintf(&err, "CRL file '%s' already exists!\n", path);
3316 goto error;
3317 }
3318
3319 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CRL);
3320 if (!cafile_entry) {
3321 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3322 goto error;
3323 }
3324
3325 /* Add the newly created cafile_entry to the tree so that
3326 * any new ckch instance created from now can use it. */
3327 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
3328 goto error;
3329
3330 memprintf(&err, "New CRL file created '%s'!\n", path);
3331
3332 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3333 return cli_dynmsg(appctx, LOG_NOTICE, err);
3334error:
3335 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3336 return cli_dynerr(appctx, err);
3337}
3338
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003339/* Parsing function of `set ssl crl-file` */
3340static int cli_parse_set_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3341{
Christopher Faulet1f90f332022-06-03 16:34:30 +02003342 struct cafile_entry *old_crlfile_entry = NULL;
3343 struct cafile_entry *new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003344 char *err = NULL;
3345 int errcode = 0;
3346 struct buffer *buf;
3347
3348 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3349 return 1;
3350
3351 if (!*args[3] || !payload)
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003352 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 +02003353
3354 /* The operations on the CKCH architecture are locked so we can
3355 * manipulate ckch_store and ckch_inst */
3356 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3357 return cli_err(appctx, "Can't update the CRL file!\nOperations on certificates are currently locked!\n");
3358
3359 if ((buf = alloc_trash_chunk()) == NULL) {
3360 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3361 errcode |= ERR_ALERT | ERR_FATAL;
3362 goto end;
3363 }
3364
3365 if (!chunk_strcpy(buf, args[3])) {
3366 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3367 errcode |= ERR_ALERT | ERR_FATAL;
3368 goto end;
3369 }
3370
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003371 old_crlfile_entry = NULL;
3372 new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003373
3374 /* if there is an ongoing transaction */
3375 if (crlfile_transaction.path) {
3376 /* if there is an ongoing transaction, check if this is the same file */
3377 if (strcmp(crlfile_transaction.path, buf->area) != 0) {
3378 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, buf->area);
3379 errcode |= ERR_ALERT | ERR_FATAL;
3380 goto end;
3381 }
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003382 old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003383 }
3384 else {
3385 /* lookup for the certificate in the tree */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003386 old_crlfile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003387 }
3388
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003389 if (!old_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003390 memprintf(&err, "%sCan't replace a CRL file which is not referenced by the configuration!\n",
3391 err ? err : "");
3392 errcode |= ERR_ALERT | ERR_FATAL;
3393 goto end;
3394 }
3395
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003396 /* Create a new cafile_entry without adding it to the cafile tree. */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003397 new_crlfile_entry = ssl_store_create_cafile_entry(old_crlfile_entry->path, NULL, CAFILE_CRL);
3398 if (!new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003399 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3400 errcode |= ERR_ALERT | ERR_FATAL;
3401 goto end;
3402 }
3403
3404 /* Fill the new entry with the new CRL. */
William Lallemandd4774d32022-07-29 17:08:02 +02003405 if (ssl_store_load_ca_from_buf(new_crlfile_entry, payload, 0)) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003406 memprintf(&err, "%sInvalid payload\n", err ? err : "");
3407 errcode |= ERR_ALERT | ERR_FATAL;
3408 goto end;
3409 }
3410
3411 /* we succeed, we can save the crl in the transaction */
3412
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003413 /* if there wasn't a transaction, update the old CRL */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003414 if (!crlfile_transaction.old_crlfile_entry) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003415 crlfile_transaction.old_crlfile_entry = old_crlfile_entry;
3416 crlfile_transaction.path = old_crlfile_entry->path;
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003417 err = memprintf(&err, "transaction created for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003418 } else {
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003419 err = memprintf(&err, "transaction updated for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003420 }
3421
3422 /* free the previous CRL file if there was a transaction */
3423 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3424
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003425 crlfile_transaction.new_crlfile_entry = new_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003426
3427 /* creates the SNI ctxs later in the IO handler */
3428
3429end:
3430 free_trash_chunk(buf);
3431
3432 if (errcode & ERR_CODE) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003433 ssl_store_delete_cafile_entry(new_crlfile_entry);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003434 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3435 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
3436 } else {
3437
3438 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3439 return cli_dynmsg(appctx, LOG_NOTICE, err);
3440 }
3441}
3442
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003443/* Parsing function of 'commit ssl crl-file'.
3444 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl ca-file".
3445 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003446static int cli_parse_commit_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3447{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003448 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003449 char *err = NULL;
3450
3451 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3452 return 1;
3453
3454 if (!*args[3])
3455 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
3456
3457 /* The operations on the CKCH architecture are locked so we can
3458 * manipulate ckch_store and ckch_inst */
3459 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3460 return cli_err(appctx, "Can't commit the CRL file!\nOperations on certificates are currently locked!\n");
3461
3462 if (!crlfile_transaction.path) {
3463 memprintf(&err, "No ongoing transaction! !\n");
3464 goto error;
3465 }
3466
3467 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3468 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, args[3]);
3469 goto error;
3470 }
3471 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02003472 ctx->state = CACRL_ST_INIT;
Christopher Fauletf814c4a2022-06-03 11:32:05 +02003473 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003474 ctx->old_entry = crlfile_transaction.old_crlfile_entry;
3475 ctx->new_entry = crlfile_transaction.new_crlfile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003476 ctx->cafile_type = CAFILE_CRL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003477
3478 return 0;
3479
3480error:
3481
3482 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3483 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
3484
3485 return cli_dynerr(appctx, err);
3486}
3487
3488
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003489/* release function of the `commit ssl crl-file' command, free things and unlock the spinlock.
3490 * it uses a commit_cacrlfile_ctx that's the same as for "commit ssl ca-file".
3491 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003492static void cli_release_commit_crlfile(struct appctx *appctx)
3493{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003494 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003495 struct cafile_entry *new_crlfile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003496
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003497 /* Remove the uncommitted cafile_entry from the tree. */
3498 if (new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003499 ebmb_delete(&new_crlfile_entry->node);
3500 ssl_store_delete_cafile_entry(new_crlfile_entry);
3501 }
3502 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003503 ha_free(&ctx->err);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003504}
3505
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003506/* parsing function of 'del ssl crl-file' */
3507static int cli_parse_del_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3508{
3509 struct cafile_entry *cafile_entry;
3510 char *err = NULL;
3511 char *filename;
3512
3513 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3514 return 1;
3515
3516 if (!*args[3])
3517 return cli_err(appctx, "'del ssl crl-file' expects a CRL file name\n");
3518
3519 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3520 return cli_err(appctx, "Can't delete the CRL file!\nOperations on certificates are currently locked!\n");
3521
3522 filename = args[3];
3523
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003524 if (crlfile_transaction.path && strcmp(crlfile_transaction.path, filename) == 0) {
3525 memprintf(&err, "ongoing transaction for the CRL file '%s'", filename);
3526 goto error;
3527 }
3528
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003529 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3530 if (!cafile_entry) {
3531 memprintf(&err, "CRL file '%s' doesn't exist!\n", filename);
3532 goto error;
3533 }
3534 if (cafile_entry->type != CAFILE_CRL) {
3535 memprintf(&err, "'del ssl crl-file' does not work on CA files!\n");
3536 goto error;
3537 }
3538
3539 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3540 memprintf(&err, "CRL file '%s' in use, can't be deleted!\n", filename);
3541 goto error;
3542 }
3543
3544 /* Remove the cafile_entry from the tree */
3545 ebmb_delete(&cafile_entry->node);
3546 ssl_store_delete_cafile_entry(cafile_entry);
3547
3548 memprintf(&err, "CRL file '%s' deleted!\n", filename);
3549
3550 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3551 return cli_dynmsg(appctx, LOG_NOTICE, err);
3552
3553error:
3554 memprintf(&err, "Can't remove the CRL file: %s\n", err ? err : "");
3555 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3556 return cli_dynerr(appctx, err);
3557}
3558
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003559/* parsing function of 'abort ssl crl-file' */
3560static int cli_parse_abort_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3561{
3562 char *err = NULL;
3563
3564 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3565 return 1;
3566
3567 if (!*args[3])
3568 return cli_err(appctx, "'abort ssl crl-file' expects a filename\n");
3569
3570 /* The operations on the CKCH architecture are locked so we can
3571 * manipulate ckch_store and ckch_inst */
3572 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3573 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
3574
3575 if (!crlfile_transaction.path) {
3576 memprintf(&err, "No ongoing transaction!\n");
3577 goto error;
3578 }
3579
3580 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3581 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", crlfile_transaction.path, args[3]);
3582 goto error;
3583 }
3584
3585 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3586 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3587 crlfile_transaction.new_crlfile_entry = NULL;
3588 crlfile_transaction.old_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02003589 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003590
3591 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3592
3593 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3594 return cli_dynmsg(appctx, LOG_NOTICE, err);
3595
3596error:
3597 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3598
3599 return cli_dynerr(appctx, err);
3600}
3601
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003602
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003603/*
3604 * Display a Certificate Resignation List's information.
3605 * The information displayed is inspired by the output of 'openssl crl -in
3606 * crl.pem -text'.
3607 * Returns 0 in case of success.
3608 */
3609static int show_crl_detail(X509_CRL *crl, struct buffer *out)
3610{
3611 BIO *bio = NULL;
3612 struct buffer *tmp = alloc_trash_chunk();
3613 long version;
3614 X509_NAME *issuer;
3615 int write = -1;
3616 STACK_OF(X509_REVOKED) *rev = NULL;
3617 X509_REVOKED *rev_entry = NULL;
3618 int i;
3619
3620 if (!tmp)
3621 return -1;
3622
3623 if ((bio = BIO_new(BIO_s_mem())) == NULL)
3624 goto end;
3625
3626 /* Version (as displayed by 'openssl crl') */
3627 version = X509_CRL_get_version(crl);
3628 chunk_appendf(out, "Version %ld\n", version + 1);
3629
3630 /* Signature Algorithm */
3631 chunk_appendf(out, "Signature Algorithm: %s\n", OBJ_nid2ln(X509_CRL_get_signature_nid(crl)));
3632
3633 /* Issuer */
3634 chunk_appendf(out, "Issuer: ");
3635 if ((issuer = X509_CRL_get_issuer(crl)) == NULL)
3636 goto end;
3637 if ((ssl_sock_get_dn_oneline(issuer, tmp)) == -1)
3638 goto end;
3639 *(tmp->area + tmp->data) = '\0';
3640 chunk_appendf(out, "%s\n", tmp->area);
3641
3642 /* Last Update */
3643 chunk_appendf(out, "Last Update: ");
3644 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003645 if (BIO_reset(bio) == -1)
3646 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003647 if (ASN1_TIME_print(bio, X509_CRL_get0_lastUpdate(crl)) == 0)
3648 goto end;
3649 write = BIO_read(bio, tmp->area, tmp->size-1);
3650 tmp->area[write] = '\0';
3651 chunk_appendf(out, "%s\n", tmp->area);
3652
3653
3654 /* Next Update */
3655 chunk_appendf(out, "Next Update: ");
3656 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003657 if (BIO_reset(bio) == -1)
3658 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003659 if (ASN1_TIME_print(bio, X509_CRL_get0_nextUpdate(crl)) == 0)
3660 goto end;
3661 write = BIO_read(bio, tmp->area, tmp->size-1);
3662 tmp->area[write] = '\0';
3663 chunk_appendf(out, "%s\n", tmp->area);
3664
3665
3666 /* Revoked Certificates */
3667 rev = X509_CRL_get_REVOKED(crl);
3668 if (sk_X509_REVOKED_num(rev) > 0)
3669 chunk_appendf(out, "Revoked Certificates:\n");
3670 else
3671 chunk_appendf(out, "No Revoked Certificates.\n");
3672
3673 for (i = 0; i < sk_X509_REVOKED_num(rev); i++) {
3674 rev_entry = sk_X509_REVOKED_value(rev, i);
3675
3676 /* Serial Number and Revocation Date */
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003677 if (BIO_reset(bio) == -1)
3678 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003679 BIO_printf(bio , " Serial Number: ");
Remi Tricot-Le Breton18c7d832021-05-17 18:38:34 +02003680 i2a_ASN1_INTEGER(bio, (ASN1_INTEGER*)X509_REVOKED_get0_serialNumber(rev_entry));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003681 BIO_printf(bio, "\n Revocation Date: ");
Remi Tricot-Le Bretona6b27842021-05-18 10:06:00 +02003682 if (ASN1_TIME_print(bio, X509_REVOKED_get0_revocationDate(rev_entry)) == 0)
3683 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003684 BIO_printf(bio, "\n");
3685
3686 write = BIO_read(bio, tmp->area, tmp->size-1);
3687 tmp->area[write] = '\0';
3688 chunk_appendf(out, "%s", tmp->area);
3689 }
3690
3691end:
3692 free_trash_chunk(tmp);
3693 if (bio)
3694 BIO_free(bio);
3695
3696 return 0;
3697}
3698
Willy Tarreau821c3b02022-05-04 15:47:39 +02003699/* IO handler of details "show ssl crl-file <filename[:index]>".
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003700 * It uses show_crlfile_ctx and the global
3701 * crlfile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003702 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003703static int cli_io_handler_show_crlfile_detail(struct appctx *appctx)
3704{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003705 struct show_crlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003706 struct cafile_entry *cafile_entry = ctx->cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003707 struct buffer *out = alloc_trash_chunk();
3708 int i;
3709 X509_CRL *crl;
3710 STACK_OF(X509_OBJECT) *objs;
3711 int retval = 0;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003712 int index = ctx->index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003713
3714 if (!out)
3715 goto end_no_putchk;
3716
3717 chunk_appendf(out, "Filename: ");
3718 if (cafile_entry == crlfile_transaction.new_crlfile_entry)
3719 chunk_appendf(out, "*");
3720 chunk_appendf(out, "%s\n", cafile_entry->path);
3721
3722 chunk_appendf(out, "Status: ");
3723 if (!cafile_entry->ca_store)
3724 chunk_appendf(out, "Empty\n");
3725 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3726 chunk_appendf(out, "Unused\n");
3727 else
3728 chunk_appendf(out, "Used\n");
3729
3730 if (!cafile_entry->ca_store)
3731 goto end;
3732
3733 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3734 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
3735 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
3736 if (!crl)
3737 continue;
3738
3739 /* CRL indexes start at 1 on the CLI output. */
3740 if (index && index-1 != i)
3741 continue;
3742
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003743 chunk_appendf(out, " \nCertificate Revocation List #%d:\n", i+1);
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003744 retval = show_crl_detail(crl, out);
3745 if (retval < 0)
3746 goto end_no_putchk;
3747 else if (retval || index)
3748 goto end;
3749 }
3750
3751end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003752 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003753 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003754
3755end_no_putchk:
3756 free_trash_chunk(out);
3757 return 1;
3758yield:
3759 free_trash_chunk(out);
3760 return 0; /* should come back */
3761}
3762
Willy Tarreau821c3b02022-05-04 15:47:39 +02003763/* parsing function for 'show ssl crl-file [crlfile[:index]]'.
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003764 * It sets the context to a show_crlfile_ctx, and the global
Willy Tarreau821c3b02022-05-04 15:47:39 +02003765 * cafile_transaction.new_crlfile_entry under the ckch_lock.
3766 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003767static int cli_parse_show_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3768{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003769 struct show_crlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003770 struct cafile_entry *cafile_entry;
3771 long index = 0;
3772 char *colons;
3773 char *err = NULL;
3774
3775 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3776 return cli_err(appctx, "Can't allocate memory!\n");
3777
3778 /* The operations on the CKCH architecture are locked so we can
3779 * manipulate ckch_store and ckch_inst */
3780 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3781 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3782
3783 /* check if there is a certificate to lookup */
3784 if (*args[3]) {
3785
3786 /* Look for an optional index after the CRL file name */
3787 colons = strchr(args[3], ':');
3788 if (colons) {
3789 char *endptr;
3790
3791 index = strtol(colons + 1, &endptr, 10);
3792 /* Indexes start at 1 */
3793 if (colons + 1 == endptr || *endptr != '\0' || index <= 0) {
3794 memprintf(&err, "wrong CRL index after colons in '%s'!", args[3]);
3795 goto error;
3796 }
3797 *colons = '\0';
3798 }
3799
3800 if (*args[3] == '*') {
3801 if (!crlfile_transaction.new_crlfile_entry)
3802 goto error;
3803
3804 cafile_entry = crlfile_transaction.new_crlfile_entry;
3805
3806 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3807 goto error;
3808
3809 } else {
3810 /* Get the "original" cafile_entry and not the
3811 * uncommitted one if it exists. */
3812 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CRL)
3813 goto error;
3814 }
3815
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003816 ctx->cafile_entry = cafile_entry;
3817 ctx->index = index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003818 /* use the IO handler that shows details */
3819 appctx->io_handler = cli_io_handler_show_crlfile_detail;
3820 }
3821
3822 return 0;
3823
3824error:
3825 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3826 if (err)
3827 return cli_dynerr(appctx, err);
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003828 return cli_err(appctx, "Can't display the CRL file : Not found!\n");
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003829}
3830
3831/* IO handler of "show ssl crl-file". The command taking a specific CRL file name
3832 * is managed in cli_io_handler_show_crlfile_detail. */
3833static int cli_io_handler_show_crlfile(struct appctx *appctx)
3834{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003835 struct show_crlfile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003836 struct buffer *trash = alloc_trash_chunk();
3837 struct ebmb_node *node;
Christopher Faulet88041b32022-06-03 16:26:56 +02003838 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003839
3840 if (trash == NULL)
3841 return 1;
3842
Christopher Faulet9a99e542022-06-03 10:32:18 +02003843 if (!ctx->old_crlfile_entry && crlfile_transaction.old_crlfile_entry) {
3844 chunk_appendf(trash, "# transaction\n");
3845 chunk_appendf(trash, "*%s\n", crlfile_transaction.old_crlfile_entry->path);
3846 if (applet_putchk(appctx, trash) == -1)
3847 goto yield;
3848 ctx->old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003849 }
3850
3851 /* First time in this io_handler. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003852 if (!ctx->cafile_entry) {
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003853 chunk_appendf(trash, "# filename\n");
3854 node = ebmb_first(&cafile_tree);
3855 } else {
3856 /* We yielded during a previous call. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003857 node = &ctx->cafile_entry->node;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003858 }
3859
3860 while (node) {
3861 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3862 if (cafile_entry->type == CAFILE_CRL) {
3863 chunk_appendf(trash, "%s\n", cafile_entry->path);
3864 }
3865
3866 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003867 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003868 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003869 }
3870
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003871 ctx->cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003872 free_trash_chunk(trash);
3873 return 1;
3874yield:
3875
3876 free_trash_chunk(trash);
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003877 ctx->cafile_entry = cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003878 return 0; /* should come back */
3879}
3880
3881
3882/* release function of the 'show ssl crl-file' command */
3883static void cli_release_show_crlfile(struct appctx *appctx)
3884{
3885 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3886}
3887
3888
William Lallemandee8530c2020-06-23 18:19:42 +02003889void ckch_deinit()
3890{
3891 struct eb_node *node, *next;
3892 struct ckch_store *store;
William Lallemandb0c48272022-04-26 15:44:53 +02003893 struct ebmb_node *canode;
William Lallemandee8530c2020-06-23 18:19:42 +02003894
William Lallemandb0c48272022-04-26 15:44:53 +02003895 /* deinit the ckch stores */
William Lallemandee8530c2020-06-23 18:19:42 +02003896 node = eb_first(&ckchs_tree);
3897 while (node) {
3898 next = eb_next(node);
3899 store = ebmb_entry(node, struct ckch_store, node);
3900 ckch_store_free(store);
3901 node = next;
3902 }
William Lallemandb0c48272022-04-26 15:44:53 +02003903
3904 /* deinit the ca-file store */
3905 canode = ebmb_first(&cafile_tree);
3906 while (canode) {
3907 struct cafile_entry *entry = NULL;
3908
3909 entry = ebmb_entry(canode, struct cafile_entry, node);
3910 canode = ebmb_next(canode);
3911 ssl_store_delete_cafile_entry(entry);
3912 }
William Lallemandee8530c2020-06-23 18:19:42 +02003913}
William Lallemandda8584c2020-05-14 10:14:37 +02003914
3915/* register cli keywords */
3916static struct cli_kw_list cli_kws = {{ },{
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003917 { { "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 },
3918 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
3919 { { "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 },
3920 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
3921 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
3922 { { "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 },
3923
Amaury Denoyelleb11ad9e2021-05-21 11:01:10 +02003924 { { "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 +02003925 { { "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 +01003926 { { "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 +02003927 { { "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 +01003928 { { "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 +01003929 { { "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 +01003930 { { "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 +02003931
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003932 { { "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 +02003933 { { "set", "ssl", "crl-file", NULL }, "set ssl crl-file <crlfile> <payload> : replace a CRL file", cli_parse_set_crlfile, NULL, NULL },
3934 { { "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 +02003935 { { "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 +02003936 { { "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 +02003937 { { "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 +02003938 { { NULL }, NULL, NULL, NULL }
3939}};
3940
3941INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3942