blob: 674513e013d462e48e52b8884717543b485bc480 [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) {
William Lallemanda5384522022-10-25 15:53:01 +0200619 ret = ERR_get_error();
620 memprintf(err, "%sunable to load certificate from file '%s': %s.\n",
621 err && *err ? *err : "", path, ERR_reason_error_string(ret));
William Lallemand03c331c2020-05-13 10:10:01 +0200622 goto end;
623 }
624
625 /* Look for a Certificate Chain */
626 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
627 if (chain == NULL)
628 chain = sk_X509_new_null();
629 if (!sk_X509_push(chain, ca)) {
630 X509_free(ca);
William Lallemand432cd1a2022-10-25 15:55:13 +0200631 break;
William Lallemand03c331c2020-05-13 10:10:01 +0200632 }
633 }
634
635 ret = ERR_get_error();
William Lallemand78c7a062022-11-15 17:12:03 +0100636 if (ret && !(ERR_GET_LIB(ret) == ERR_LIB_PEM && ERR_GET_REASON(ret) == PEM_R_NO_START_LINE)) {
William Lallemandf784b902022-10-25 12:31:39 +0200637 memprintf(err, "%sunable to load certificate chain from file '%s': %s\n",
638 err && *err ? *err : "", path, ERR_reason_error_string(ret));
William Lallemand03c331c2020-05-13 10:10:01 +0200639 goto end;
640 }
641
642 /* once it loaded the PEM, it should remove everything else in the ckch */
643 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100644 ha_free(&ckch->ocsp_response->area);
645 ha_free(&ckch->ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200646 }
647
648 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100649 ha_free(&ckch->sctl->area);
650 ha_free(&ckch->sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200651 }
652
653 if (ckch->ocsp_issuer) {
654 X509_free(ckch->ocsp_issuer);
655 ckch->ocsp_issuer = NULL;
656 }
657
658 /* no error, fill ckch with new context, old context will be free at end: */
659 SWAP(ckch->key, key);
660 SWAP(ckch->dh, dh);
661 SWAP(ckch->cert, cert);
662 SWAP(ckch->chain, chain);
663
664 ret = 0;
665
666end:
667
668 ERR_clear_error();
669 if (in)
670 BIO_free(in);
671 if (key)
672 EVP_PKEY_free(key);
673 if (dh)
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100674 HASSL_DH_free(dh);
William Lallemand03c331c2020-05-13 10:10:01 +0200675 if (cert)
676 X509_free(cert);
677 if (chain)
678 sk_X509_pop_free(chain, X509_free);
679
680 return ret;
681}
682
683/* Frees the contents of a cert_key_and_chain
684 */
685void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
686{
687 if (!ckch)
688 return;
689
690 /* Free the certificate and set pointer to NULL */
691 if (ckch->cert)
692 X509_free(ckch->cert);
693 ckch->cert = NULL;
694
695 /* Free the key and set pointer to NULL */
696 if (ckch->key)
697 EVP_PKEY_free(ckch->key);
698 ckch->key = NULL;
699
700 /* Free each certificate in the chain */
701 if (ckch->chain)
702 sk_X509_pop_free(ckch->chain, X509_free);
703 ckch->chain = NULL;
704
705 if (ckch->dh)
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100706 HASSL_DH_free(ckch->dh);
William Lallemand03c331c2020-05-13 10:10:01 +0200707 ckch->dh = NULL;
708
709 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100710 ha_free(&ckch->sctl->area);
711 ha_free(&ckch->sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200712 }
713
714 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100715 ha_free(&ckch->ocsp_response->area);
716 ha_free(&ckch->ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200717 }
718
719 if (ckch->ocsp_issuer)
720 X509_free(ckch->ocsp_issuer);
721 ckch->ocsp_issuer = NULL;
722}
723
724/*
725 *
726 * This function copy a cert_key_and_chain in memory
727 *
728 * It's used to try to apply changes on a ckch before committing them, because
729 * most of the time it's not possible to revert those changes
730 *
731 * Return a the dst or NULL
732 */
733struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
734 struct cert_key_and_chain *dst)
735{
William Lallemand6c096142021-02-23 14:45:45 +0100736 if (!src || !dst)
737 return NULL;
738
William Lallemand03c331c2020-05-13 10:10:01 +0200739 if (src->cert) {
740 dst->cert = src->cert;
741 X509_up_ref(src->cert);
742 }
743
744 if (src->key) {
745 dst->key = src->key;
746 EVP_PKEY_up_ref(src->key);
747 }
748
749 if (src->chain) {
750 dst->chain = X509_chain_up_ref(src->chain);
751 }
752
753 if (src->dh) {
Uriah Pollock3cbf09e2022-11-23 16:41:25 +0100754#ifndef USE_OPENSSL_WOLFSSL
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100755 HASSL_DH_up_ref(src->dh);
William Lallemand03c331c2020-05-13 10:10:01 +0200756 dst->dh = src->dh;
Uriah Pollock3cbf09e2022-11-23 16:41:25 +0100757#else
758 dst->dh = wolfSSL_DH_dup(src->dh);
759 if (!dst->dh)
760 goto error;
761#endif
William Lallemand03c331c2020-05-13 10:10:01 +0200762 }
763
764 if (src->sctl) {
765 struct buffer *sctl;
766
767 sctl = calloc(1, sizeof(*sctl));
768 if (!chunk_dup(sctl, src->sctl)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100769 ha_free(&sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200770 goto error;
771 }
772 dst->sctl = sctl;
773 }
774
775 if (src->ocsp_response) {
776 struct buffer *ocsp_response;
777
778 ocsp_response = calloc(1, sizeof(*ocsp_response));
779 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100780 ha_free(&ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200781 goto error;
782 }
783 dst->ocsp_response = ocsp_response;
784 }
785
786 if (src->ocsp_issuer) {
787 X509_up_ref(src->ocsp_issuer);
788 dst->ocsp_issuer = src->ocsp_issuer;
789 }
790
791 return dst;
792
793error:
794
795 /* free everything */
796 ssl_sock_free_cert_key_and_chain_contents(dst);
797
798 return NULL;
799}
800
801/*
802 * return 0 on success or != 0 on failure
803 */
804int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
805{
806 int ret = 1;
807 BIO *in = NULL;
808 X509 *issuer;
809
810 if (buf) {
811 /* reading from a buffer */
812 in = BIO_new_mem_buf(buf, -1);
813 if (in == NULL) {
814 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
815 goto end;
816 }
817
818 } else {
819 /* reading from a file */
820 in = BIO_new(BIO_s_file());
821 if (in == NULL)
822 goto end;
823
824 if (BIO_read_filename(in, path) <= 0)
825 goto end;
826 }
827
828 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
829 if (!issuer) {
830 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
831 err && *err ? *err : "", path);
832 goto end;
833 }
834 /* no error, fill ckch with new context, old context must be free */
835 if (ckch->ocsp_issuer)
836 X509_free(ckch->ocsp_issuer);
837 ckch->ocsp_issuer = issuer;
838 ret = 0;
839
840end:
841
842 ERR_clear_error();
843 if (in)
844 BIO_free(in);
845
846 return ret;
847}
848
849/******************** ckch_store functions ***********************************
850 * The ckch_store is a structure used to cache and index the SSL files used in
851 * configuration
852 */
853
854/*
855 * Free a ckch_store, its ckch, its instances and remove it from the ebtree
856 */
857void ckch_store_free(struct ckch_store *store)
858{
859 struct ckch_inst *inst, *inst_s;
860
861 if (!store)
862 return;
863
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200864 ssl_sock_free_cert_key_and_chain_contents(store->ckch);
William Lallemand03c331c2020-05-13 10:10:01 +0200865
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100866 ha_free(&store->ckch);
William Lallemand03c331c2020-05-13 10:10:01 +0200867
868 list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) {
869 ckch_inst_free(inst);
870 }
871 ebmb_delete(&store->node);
872 free(store);
873}
874
875/*
876 * create and initialize a ckch_store
877 * <path> is the key name
878 * <nmemb> is the number of store->ckch objects to allocate
879 *
880 * Return a ckch_store or NULL upon failure.
881 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200882struct ckch_store *ckch_store_new(const char *filename)
William Lallemand03c331c2020-05-13 10:10:01 +0200883{
884 struct ckch_store *store;
885 int pathlen;
886
887 pathlen = strlen(filename);
888 store = calloc(1, sizeof(*store) + pathlen + 1);
889 if (!store)
890 return NULL;
891
William Lallemand03c331c2020-05-13 10:10:01 +0200892 memcpy(store->path, filename, pathlen + 1);
893
894 LIST_INIT(&store->ckch_inst);
895 LIST_INIT(&store->crtlist_entry);
896
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200897 store->ckch = calloc(1, sizeof(*store->ckch));
William Lallemand03c331c2020-05-13 10:10:01 +0200898 if (!store->ckch)
899 goto error;
900
901 return store;
902error:
903 ckch_store_free(store);
904 return NULL;
905}
906
907/* allocate and duplicate a ckch_store
908 * Return a new ckch_store or NULL */
909struct ckch_store *ckchs_dup(const struct ckch_store *src)
910{
911 struct ckch_store *dst;
912
William Lallemand6c096142021-02-23 14:45:45 +0100913 if (!src)
914 return NULL;
915
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200916 dst = ckch_store_new(src->path);
Eric Salama6ac61e32021-02-23 16:50:57 +0100917 if (!dst)
918 return NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200919
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200920 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
921 goto error;
William Lallemand03c331c2020-05-13 10:10:01 +0200922
923 return dst;
924
925error:
926 ckch_store_free(dst);
927
928 return NULL;
929}
930
931/*
932 * lookup a path into the ckchs tree.
933 */
934struct ckch_store *ckchs_lookup(char *path)
935{
936 struct ebmb_node *eb;
937
938 eb = ebst_lookup(&ckchs_tree, path);
939 if (!eb)
940 return NULL;
941
942 return ebmb_entry(eb, struct ckch_store, node);
943}
944
945/*
946 * This function allocate a ckch_store and populate it with certificates from files.
947 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200948struct ckch_store *ckchs_load_cert_file(char *path, char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200949{
950 struct ckch_store *ckchs;
951
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200952 ckchs = ckch_store_new(path);
William Lallemand03c331c2020-05-13 10:10:01 +0200953 if (!ckchs) {
954 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
955 goto end;
956 }
William Lallemand03c331c2020-05-13 10:10:01 +0200957
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200958 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
959 goto end;
William Lallemand03c331c2020-05-13 10:10:01 +0200960
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200961 /* insert into the ckchs tree */
962 memcpy(ckchs->path, path, strlen(path) + 1);
963 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand03c331c2020-05-13 10:10:01 +0200964 return ckchs;
965
966end:
967 ckch_store_free(ckchs);
968
969 return NULL;
970}
971
William Lallemandfa1d8b42020-05-13 15:46:10 +0200972
973/******************** ckch_inst functions ******************************/
974
975/* unlink a ckch_inst, free all SNIs, free the ckch_inst */
976/* The caller must use the lock of the bind_conf if used with inserted SNIs */
977void ckch_inst_free(struct ckch_inst *inst)
978{
979 struct sni_ctx *sni, *sni_s;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100980 struct ckch_inst_link_ref *link_ref, *link_ref_s;
William Lallemandfa1d8b42020-05-13 15:46:10 +0200981
982 if (inst == NULL)
983 return;
984
985 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
986 SSL_CTX_free(sni->ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +0200987 LIST_DELETE(&sni->by_ckch_inst);
William Lallemandfa1d8b42020-05-13 15:46:10 +0200988 ebmb_delete(&sni->name);
989 free(sni);
990 }
Remi Tricot-Le Bretonf3eedfe2021-01-25 17:19:44 +0100991 SSL_CTX_free(inst->ctx);
992 inst->ctx = NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +0200993 LIST_DELETE(&inst->by_ckchs);
994 LIST_DELETE(&inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100995
William Lallemande0fa91f2022-08-31 14:26:49 +0200996 /* Free the cafile_link_refs list */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100997 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
William Lallemande0fa91f2022-08-31 14:26:49 +0200998 if (link_ref->link && LIST_INLIST(&link_ref->link->list)) {
999 /* Try to detach and free the ckch_inst_link only if it
1000 * was attached, this way it can be used to loop from
1001 * the caller */
1002 LIST_DEL_INIT(&link_ref->link->list);
1003 ha_free(&link_ref->link);
1004 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001005 LIST_DELETE(&link_ref->list);
1006 free(link_ref);
1007 }
1008
William Lallemandfa1d8b42020-05-13 15:46:10 +02001009 free(inst);
1010}
1011
1012/* Alloc and init a ckch_inst */
1013struct ckch_inst *ckch_inst_new()
1014{
1015 struct ckch_inst *ckch_inst;
1016
1017 ckch_inst = calloc(1, sizeof *ckch_inst);
1018 if (!ckch_inst)
1019 return NULL;
1020
1021 LIST_INIT(&ckch_inst->sni_ctx);
1022 LIST_INIT(&ckch_inst->by_ckchs);
1023 LIST_INIT(&ckch_inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001024 LIST_INIT(&ckch_inst->cafile_link_refs);
William Lallemandfa1d8b42020-05-13 15:46:10 +02001025
1026 return ckch_inst;
1027}
1028
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001029
1030/******************** ssl_store functions ******************************/
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001031struct eb_root cafile_tree = EB_ROOT;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001032
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001033/*
1034 * Returns the cafile_entry found in the cafile_tree indexed by the path 'path'.
1035 * If 'oldest_entry' is 1, returns the "original" cafile_entry (since
1036 * during a set cafile/commit cafile cycle there might be two entries for any
1037 * given path, the original one and the new one set via the CLI but not
1038 * committed yet).
1039 */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001040struct cafile_entry *ssl_store_get_cafile_entry(char *path, int oldest_entry)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001041{
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001042 struct cafile_entry *ca_e = NULL;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001043 struct ebmb_node *eb;
1044
1045 eb = ebst_lookup(&cafile_tree, path);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001046 while (eb) {
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001047 ca_e = ebmb_entry(eb, struct cafile_entry, node);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001048 /* The ebst_lookup in a tree that has duplicates returns the
1049 * oldest entry first. If we want the latest entry, we need to
1050 * iterate over all the duplicates until we find the last one
1051 * (in our case there should never be more than two entries for
1052 * any given path). */
1053 if (oldest_entry)
1054 return ca_e;
1055 eb = ebmb_next_dup(eb);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001056 }
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001057 return ca_e;
1058}
1059
Remi Tricot-Le Breton38c999b2021-02-23 16:28:43 +01001060int ssl_store_add_uncommitted_cafile_entry(struct cafile_entry *entry)
1061{
1062 return (ebst_insert(&cafile_tree, &entry->node) != &entry->node);
1063}
1064
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001065X509_STORE* ssl_store_get0_locations_file(char *path)
1066{
1067 struct cafile_entry *ca_e = ssl_store_get_cafile_entry(path, 0);
1068
1069 if (ca_e)
1070 return ca_e->ca_store;
1071
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001072 return NULL;
1073}
1074
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001075/* Create a cafile_entry object, without adding it to the cafile_tree. */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001076struct 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 +01001077{
1078 struct cafile_entry *ca_e;
1079 int pathlen;
1080
1081 pathlen = strlen(path);
1082
1083 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
1084 if (ca_e) {
1085 memcpy(ca_e->path, path, pathlen + 1);
1086 ca_e->ca_store = store;
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001087 ca_e->type = type;
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001088 LIST_INIT(&ca_e->ckch_inst_link);
1089 }
1090 return ca_e;
1091}
1092
William Lallemand62c0b992022-07-29 17:50:58 +02001093
1094/* Duplicate a cafile_entry
1095 * Allocate the X509_STORE and copy the X509 and CRL inside.
1096 *
1097 * Return the newly allocated cafile_entry or NULL.
1098 *
1099 */
1100struct cafile_entry *ssl_store_dup_cafile_entry(struct cafile_entry *src)
1101{
1102 struct cafile_entry *dst = NULL;
1103 X509_STORE *store = NULL;
1104 STACK_OF(X509_OBJECT) *objs;
1105 int i;
1106
1107 if (!src)
1108 return NULL;
1109
1110 if (src->ca_store) {
1111 /* if there was a store in the src, copy it */
1112 store = X509_STORE_new();
1113 if (!store)
1114 goto err;
1115
1116 objs = X509_STORE_get0_objects(src->ca_store);
1117 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
1118 X509 *cert;
1119 X509_CRL *crl;
1120
1121 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
1122 if (cert) {
1123 if (X509_STORE_add_cert(store, cert) == 0) {
1124 /* only exits on error if the error is not about duplicate certificates */
1125 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1126 goto err;
1127 }
1128 }
1129
1130 }
1131 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
1132 if (crl) {
1133 if (X509_STORE_add_crl(store, crl) == 0) {
1134 /* only exits on error if the error is not about duplicate certificates */
1135 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1136 goto err;
1137 }
1138 }
1139
1140 }
1141 }
1142 }
1143 dst = ssl_store_create_cafile_entry(src->path, store, src->type);
1144
1145 return dst;
1146
1147err:
1148 X509_STORE_free(store);
1149 ha_free(&dst);
1150
1151 return NULL;
1152}
1153
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001154/* Delete a cafile_entry. The caller is responsible from removing this entry
1155 * from the cafile_tree first if is was previously added into it. */
1156void ssl_store_delete_cafile_entry(struct cafile_entry *ca_e)
1157{
1158 struct ckch_inst_link *link, *link_s;
1159 if (!ca_e)
1160 return;
1161
1162 X509_STORE_free(ca_e->ca_store);
1163
1164 list_for_each_entry_safe(link, link_s, &ca_e->ckch_inst_link, list) {
1165 struct ckch_inst *inst = link->ckch_inst;
1166 struct ckch_inst_link_ref *link_ref, *link_ref_s;
1167 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
1168 if (link_ref->link == link) {
1169 LIST_DELETE(&link_ref->list);
1170 free(link_ref);
1171 break;
1172 }
1173 }
1174 LIST_DELETE(&link->list);
1175 free(link);
1176 }
1177
1178 free(ca_e);
1179}
1180
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001181/*
William Lallemandd4774d32022-07-29 17:08:02 +02001182 * Fill a cafile_entry <ca_e> X509_STORE ca_e->store out of a buffer <cert_buf>
1183 * instead of out of a file. The <append> field should be set to 1 if you want
1184 * to keep the existing X509_STORE and append data to it.
1185 *
1186 * This function is used when the "set ssl ca-file" cli command is used.
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001187 * It can parse CERTIFICATE sections as well as CRL ones.
1188 * Returns 0 in case of success, 1 otherwise.
William Lallemandd4774d32022-07-29 17:08:02 +02001189 *
1190 * /!\ Warning: If there was an error the X509_STORE could have been modified so it's
1191 * better to not use it after a return 1.
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001192 */
William Lallemandd4774d32022-07-29 17:08:02 +02001193int 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 +01001194{
William Lallemandd4774d32022-07-29 17:08:02 +02001195 BIO *bio = NULL;
1196 STACK_OF(X509_INFO) *infos;
1197 X509_INFO *info;
1198 int i;
1199 int retval = 1;
1200 int retcert = 0;
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001201
1202 if (!ca_e)
1203 return 1;
1204
William Lallemandd4774d32022-07-29 17:08:02 +02001205 if (!append) {
1206 X509_STORE_free(ca_e->ca_store);
1207 ca_e->ca_store = NULL;
1208 }
1209
1210 if (!ca_e->ca_store)
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001211 ca_e->ca_store = X509_STORE_new();
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001212
William Lallemandd4774d32022-07-29 17:08:02 +02001213 if (!ca_e->ca_store)
1214 goto end;
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001215
William Lallemandd4774d32022-07-29 17:08:02 +02001216 bio = BIO_new_mem_buf(cert_buf, strlen(cert_buf));
1217 if (!bio)
1218 goto end;
1219
1220 infos = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
1221 if (!infos)
1222 goto end;
1223
1224 for (i = 0; i < sk_X509_INFO_num(infos) && !retcert; i++) {
1225 info = sk_X509_INFO_value(infos, i);
1226
1227 /* X509_STORE_add_cert and X509_STORE_add_crl return 1 on success */
1228 if (info->x509)
1229 retcert = !X509_STORE_add_cert(ca_e->ca_store, info->x509);
1230 if (!retcert && info->crl)
1231 retcert = !X509_STORE_add_crl(ca_e->ca_store, info->crl);
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001232 }
1233
William Lallemandd4774d32022-07-29 17:08:02 +02001234 /* return an error if we didn't compute all the X509_INFO or if there was none
1235 * set to 0 if everything was right */
1236 if (!(retcert || (i != sk_X509_INFO_num(infos)) || (sk_X509_INFO_num(infos) == 0)))
1237 retval = 0;
1238
1239 /* Cleanup */
1240 sk_X509_INFO_pop_free(infos, X509_INFO_free);
1241
1242end:
1243 BIO_free(bio);
1244
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001245 return retval;
1246}
1247
William Lallemand0f17ab22022-07-19 18:03:16 +02001248/*
1249 * Try to load a ca-file from disk into the ca-file cache.
1250 *
1251 * Return 0 upon error
1252 */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001253int ssl_store_load_locations_file(char *path, int create_if_none, enum cafile_type type)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001254{
1255 X509_STORE *store = ssl_store_get0_locations_file(path);
1256
1257 /* If this function is called by the CLI, we should not call the
1258 * X509_STORE_load_locations function because it performs forbidden disk
1259 * accesses. */
1260 if (!store && create_if_none) {
William Lallemand87fd9942022-04-01 20:12:03 +02001261 STACK_OF(X509_OBJECT) *objs;
1262 int cert_count = 0;
1263 struct stat buf;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001264 struct cafile_entry *ca_e;
William Lallemandc6b17632022-04-01 23:39:37 +02001265 const char *file = NULL;
1266 const char *dir = NULL;
William Lallemand0f17ab22022-07-19 18:03:16 +02001267 unsigned long e;
William Lallemand87fd9942022-04-01 20:12:03 +02001268
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001269 store = X509_STORE_new();
William Lallemand0f17ab22022-07-19 18:03:16 +02001270 if (!store) {
1271 ha_alert("Cannot allocate memory!");
1272 goto err;
1273 }
William Lallemand87fd9942022-04-01 20:12:03 +02001274
William Lallemandc6b17632022-04-01 23:39:37 +02001275 if (strcmp(path, "@system-ca") == 0) {
1276 dir = X509_get_default_cert_dir();
William Lallemand0f17ab22022-07-19 18:03:16 +02001277 if (!dir) {
1278 ha_alert("Couldn't get the system CA directory from X509_get_default_cert_dir().");
1279 goto err;
1280 }
William Lallemand87fd9942022-04-01 20:12:03 +02001281
William Lallemandc6b17632022-04-01 23:39:37 +02001282 } else {
1283
William Lallemand0f17ab22022-07-19 18:03:16 +02001284 if (stat(path, &buf) == -1) {
1285 ha_alert("Couldn't open the ca-file '%s' (%s).", path, strerror(errno));
William Lallemandc6b17632022-04-01 23:39:37 +02001286 goto err;
William Lallemand0f17ab22022-07-19 18:03:16 +02001287 }
William Lallemandc6b17632022-04-01 23:39:37 +02001288
1289 if (S_ISDIR(buf.st_mode))
1290 dir = path;
1291 else
1292 file = path;
1293 }
William Lallemand87fd9942022-04-01 20:12:03 +02001294
1295 if (file) {
1296 if (!X509_STORE_load_locations(store, file, NULL)) {
William Lallemand0f17ab22022-07-19 18:03:16 +02001297 e = ERR_get_error();
1298 ha_alert("Couldn't open the ca-file '%s' (%s).", path, ERR_reason_error_string(e));
William Lallemand87fd9942022-04-01 20:12:03 +02001299 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001300 }
William Lallemand80296b42022-04-05 10:19:30 +02001301 } else if (dir) {
William Lallemand87fd9942022-04-01 20:12:03 +02001302 int n, i;
1303 struct dirent **de_list;
1304
1305 n = scandir(dir, &de_list, 0, alphasort);
1306 if (n < 0)
1307 goto err;
1308
1309 for (i= 0; i < n; i++) {
1310 char *end;
1311 struct dirent *de = de_list[i];
1312 BIO *in = NULL;
1313 X509 *ca = NULL;;
1314
William Lallemand43482322022-07-18 18:42:52 +02001315 ERR_clear_error();
1316
William Lallemand87fd9942022-04-01 20:12:03 +02001317 /* we try to load the files that would have
1318 * been loaded in an hashed directory loaded by
1319 * X509_LOOKUP_hash_dir, so according to "man 1
1320 * c_rehash", we should load ".pem", ".crt",
William Lallemande4b93eb2022-05-09 09:29:00 +02001321 * ".cer", or ".crl". Files starting with a dot
1322 * are ignored.
William Lallemand87fd9942022-04-01 20:12:03 +02001323 */
1324 end = strrchr(de->d_name, '.');
William Lallemande4b93eb2022-05-09 09:29:00 +02001325 if (!end || de->d_name[0] == '.' ||
1326 (strcmp(end, ".pem") != 0 &&
1327 strcmp(end, ".crt") != 0 &&
1328 strcmp(end, ".cer") != 0 &&
1329 strcmp(end, ".crl") != 0)) {
William Lallemand87fd9942022-04-01 20:12:03 +02001330 free(de);
1331 continue;
1332 }
1333 in = BIO_new(BIO_s_file());
1334 if (in == NULL)
1335 goto scandir_err;
1336
William Lallemandc6b17632022-04-01 23:39:37 +02001337 chunk_printf(&trash, "%s/%s", dir, de->d_name);
William Lallemand87fd9942022-04-01 20:12:03 +02001338
1339 if (BIO_read_filename(in, trash.area) == 0)
1340 goto scandir_err;
1341
1342 if (PEM_read_bio_X509_AUX(in, &ca, NULL, NULL) == NULL)
1343 goto scandir_err;
1344
William Lallemand43482322022-07-18 18:42:52 +02001345 if (X509_STORE_add_cert(store, ca) == 0) {
1346 /* only exits on error if the error is not about duplicate certificates */
1347 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1348 goto scandir_err;
1349 }
1350 }
William Lallemand87fd9942022-04-01 20:12:03 +02001351
William Lallemand4cfbf3c2022-04-26 15:57:33 +02001352 X509_free(ca);
William Lallemand87fd9942022-04-01 20:12:03 +02001353 BIO_free(in);
1354 free(de);
1355 continue;
1356
1357scandir_err:
William Lallemand0f17ab22022-07-19 18:03:16 +02001358 e = ERR_get_error();
William Lallemand4cfbf3c2022-04-26 15:57:33 +02001359 X509_free(ca);
William Lallemand87fd9942022-04-01 20:12:03 +02001360 BIO_free(in);
1361 free(de);
William Lallemand0f17ab22022-07-19 18:03:16 +02001362 /* warn if it can load one of the files, but don't abort */
1363 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 +02001364
1365 }
1366 free(de_list);
William Lallemand80296b42022-04-05 10:19:30 +02001367 } else {
1368 ha_alert("ca-file: couldn't load '%s'\n", path);
1369 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001370 }
William Lallemand87fd9942022-04-01 20:12:03 +02001371
1372 objs = X509_STORE_get0_objects(store);
1373 cert_count = sk_X509_OBJECT_num(objs);
William Lallemand0f17ab22022-07-19 18:03:16 +02001374 if (cert_count == 0) {
William Lallemand87fd9942022-04-01 20:12:03 +02001375 ha_warning("ca-file: 0 CA were loaded from '%s'\n", path);
William Lallemand0f17ab22022-07-19 18:03:16 +02001376 }
William Lallemand87fd9942022-04-01 20:12:03 +02001377 ca_e = ssl_store_create_cafile_entry(path, store, type);
William Lallemand0f17ab22022-07-19 18:03:16 +02001378 if (!ca_e) {
1379 ha_alert("Cannot allocate memory!\n");
William Lallemand87fd9942022-04-01 20:12:03 +02001380 goto err;
William Lallemand0f17ab22022-07-19 18:03:16 +02001381 }
William Lallemand87fd9942022-04-01 20:12:03 +02001382 ebst_insert(&cafile_tree, &ca_e->node);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001383 }
1384 return (store != NULL);
William Lallemand87fd9942022-04-01 20:12:03 +02001385
1386err:
1387 X509_STORE_free(store);
1388 store = NULL;
1389 return 0;
1390
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001391}
1392
1393
William Lallemandda8584c2020-05-14 10:14:37 +02001394/*************************** CLI commands ***********************/
1395
1396/* Type of SSL payloads that can be updated over the CLI */
1397
William Lallemandff8bf982022-03-29 10:44:23 +02001398struct cert_exts cert_exts[] = {
1399 { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand26654e72022-03-30 12:01:32 +02001400 { "crt", CERT_TYPE_CRT, &ssl_sock_load_pem_into_ckch },
William Lallemandff8bf982022-03-29 10:44:23 +02001401 { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemandda8584c2020-05-14 10:14:37 +02001402#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemandff8bf982022-03-29 10:44:23 +02001403 { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001404#endif
Ilya Shipitsinc47d6762021-02-13 11:45:33 +05001405#ifdef HAVE_SSL_SCTL
William Lallemandff8bf982022-03-29 10:44:23 +02001406 { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001407#endif
William Lallemandff8bf982022-03-29 10:44:23 +02001408 { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
1409 { NULL, CERT_TYPE_MAX, NULL },
William Lallemandda8584c2020-05-14 10:14:37 +02001410};
1411
1412
1413/* release function of the `show ssl cert' command */
1414static void cli_release_show_cert(struct appctx *appctx)
1415{
1416 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1417}
1418
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001419/* IO handler of "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001420 * It makes use of a show_cert_ctx context, and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001421 */
William Lallemandda8584c2020-05-14 10:14:37 +02001422static int cli_io_handler_show_cert(struct appctx *appctx)
1423{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001424 struct show_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001425 struct buffer *trash = alloc_trash_chunk();
1426 struct ebmb_node *node;
Christopher Fauletd1d2e4d2022-06-03 16:24:02 +02001427 struct ckch_store *ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001428
1429 if (trash == NULL)
1430 return 1;
1431
Christopher Faulet3e94f5d2022-06-03 10:46:40 +02001432 if (!ctx->old_ckchs && ckchs_transaction.old_ckchs) {
1433 ckchs = ckchs_transaction.old_ckchs;
1434 chunk_appendf(trash, "# transaction\n");
1435 chunk_appendf(trash, "*%s\n", ckchs->path);
1436 if (applet_putchk(appctx, trash) == -1)
1437 goto yield;
1438 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001439 }
1440
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001441 if (!ctx->cur_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02001442 chunk_appendf(trash, "# filename\n");
1443 node = ebmb_first(&ckchs_tree);
1444 } else {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001445 node = &ctx->cur_ckchs->node;
William Lallemandda8584c2020-05-14 10:14:37 +02001446 }
1447 while (node) {
1448 ckchs = ebmb_entry(node, struct ckch_store, node);
William Lallemand5685ccf2020-09-16 16:12:25 +02001449 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001450
1451 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001452 if (applet_putchk(appctx, trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001453 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001454 }
1455
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001456 ctx->cur_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001457 free_trash_chunk(trash);
1458 return 1;
1459yield:
1460
1461 free_trash_chunk(trash);
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001462 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001463 return 0; /* should come back */
1464}
1465
1466/*
1467 * Extract and format the DNS SAN extensions and copy result into a chuink
1468 * Return 0;
1469 */
1470#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1471static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
1472{
1473 int i;
1474 char *str;
1475 STACK_OF(GENERAL_NAME) *names = NULL;
1476
1477 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1478 if (names) {
1479 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
1480 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
1481 if (i > 0)
1482 chunk_appendf(out, ", ");
1483 if (name->type == GEN_DNS) {
1484 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
1485 chunk_appendf(out, "DNS:%s", str);
1486 OPENSSL_free(str);
1487 }
1488 }
1489 }
1490 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
1491 }
1492 return 0;
1493}
1494#endif
1495
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001496/*
1497 * Build the ckch_inst_link that will be chained in the CA file entry and the
1498 * corresponding ckch_inst_link_ref that will be chained in the ckch instance.
1499 * Return 0 in case of success.
1500 */
1501static int do_chain_inst_and_cafile(struct cafile_entry *cafile_entry, struct ckch_inst *ckch_inst)
1502{
1503 struct ckch_inst_link *new_link;
1504 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
1505 struct ckch_inst_link *link = LIST_ELEM(cafile_entry->ckch_inst_link.n,
1506 typeof(link), list);
1507 /* Do not add multiple references to the same
1508 * instance in a cafile_entry */
1509 if (link->ckch_inst == ckch_inst) {
1510 return 1;
1511 }
1512 }
1513
1514 new_link = calloc(1, sizeof(*new_link));
1515 if (new_link) {
1516 struct ckch_inst_link_ref *new_link_ref = calloc(1, sizeof(*new_link_ref));
1517 if (!new_link_ref) {
1518 free(new_link);
1519 return 1;
1520 }
1521
1522 new_link->ckch_inst = ckch_inst;
1523 new_link_ref->link = new_link;
1524 LIST_INIT(&new_link->list);
1525 LIST_INIT(&new_link_ref->list);
1526
1527 LIST_APPEND(&cafile_entry->ckch_inst_link, &new_link->list);
1528 LIST_APPEND(&ckch_inst->cafile_link_refs, &new_link_ref->list);
1529 }
1530
1531 return 0;
1532}
1533
1534
1535/*
1536 * Link a CA file tree entry to the ckch instance that uses it.
1537 * To determine if and which CA file tree entries need to be linked to the
1538 * instance, we follow the same logic performed in ssl_sock_prepare_ctx when
1539 * processing the verify option.
1540 * This function works for a frontend as well as for a backend, depending on the
1541 * configuration parameters given (bind_conf or server).
1542 */
1543void ckch_inst_add_cafile_link(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf,
1544 struct ssl_bind_conf *ssl_conf, const struct server *srv)
1545{
1546 int verify = SSL_VERIFY_NONE;
1547
1548 if (srv) {
1549
1550 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
1551 verify = SSL_VERIFY_PEER;
1552 switch (srv->ssl_ctx.verify) {
1553 case SSL_SOCK_VERIFY_NONE:
1554 verify = SSL_VERIFY_NONE;
1555 break;
1556 case SSL_SOCK_VERIFY_REQUIRED:
1557 verify = SSL_VERIFY_PEER;
1558 break;
1559 }
1560 }
1561 else {
1562 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
1563 case SSL_SOCK_VERIFY_NONE:
1564 verify = SSL_VERIFY_NONE;
1565 break;
1566 case SSL_SOCK_VERIFY_OPTIONAL:
1567 verify = SSL_VERIFY_PEER;
1568 break;
1569 case SSL_SOCK_VERIFY_REQUIRED:
1570 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1571 break;
1572 }
1573 }
1574
1575 if (verify & SSL_VERIFY_PEER) {
1576 struct cafile_entry *ca_file_entry = NULL;
1577 struct cafile_entry *ca_verify_file_entry = NULL;
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001578 struct cafile_entry *crl_file_entry = NULL;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001579 if (srv) {
1580 if (srv->ssl_ctx.ca_file) {
1581 ca_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.ca_file, 0);
1582
1583 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001584 if (srv->ssl_ctx.crl_file) {
1585 crl_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.crl_file, 0);
1586 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001587 }
1588 else {
1589 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
1590 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 +02001591 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 +01001592
1593 if (ca_file)
1594 ca_file_entry = ssl_store_get_cafile_entry(ca_file, 0);
1595 if (ca_verify_file)
1596 ca_verify_file_entry = ssl_store_get_cafile_entry(ca_verify_file, 0);
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001597 if (crl_file)
1598 crl_file_entry = ssl_store_get_cafile_entry(crl_file, 0);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001599 }
1600
1601 if (ca_file_entry) {
1602 /* If we have a ckch instance that is not already in the
1603 * cafile_entry's list, add it to it. */
1604 if (do_chain_inst_and_cafile(ca_file_entry, ckch_inst))
1605 return;
1606
1607 }
1608 if (ca_verify_file_entry && (ca_file_entry != ca_verify_file_entry)) {
1609 /* If we have a ckch instance that is not already in the
1610 * cafile_entry's list, add it to it. */
1611 if (do_chain_inst_and_cafile(ca_verify_file_entry, ckch_inst))
1612 return;
1613 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001614 if (crl_file_entry) {
1615 /* If we have a ckch instance that is not already in the
1616 * cafile_entry's list, add it to it. */
1617 if (do_chain_inst_and_cafile(crl_file_entry, ckch_inst))
1618 return;
1619 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001620 }
1621}
1622
William Lallemandda8584c2020-05-14 10:14:37 +02001623
1624
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001625static int show_cert_detail(X509 *cert, STACK_OF(X509) *chain, struct buffer *out)
William Lallemandda8584c2020-05-14 10:14:37 +02001626{
William Lallemandda8584c2020-05-14 10:14:37 +02001627 BIO *bio = NULL;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001628 struct buffer *tmp = alloc_trash_chunk();
William Lallemandda8584c2020-05-14 10:14:37 +02001629 int i;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001630 int write = -1;
1631 unsigned int len = 0;
1632 X509_NAME *name = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001633
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001634 if (!tmp)
1635 return -1;
William Lallemandda8584c2020-05-14 10:14:37 +02001636
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001637 if (!cert)
William Lallemand5685ccf2020-09-16 16:12:25 +02001638 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02001639
William Lallemand5685ccf2020-09-16 16:12:25 +02001640 if (chain == NULL) {
1641 struct issuer_chain *issuer;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001642 issuer = ssl_get0_issuer_chain(cert);
William Lallemand5685ccf2020-09-16 16:12:25 +02001643 if (issuer) {
1644 chain = issuer->chain;
1645 chunk_appendf(out, "Chain Filename: ");
1646 chunk_appendf(out, "%s\n", issuer->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001647 }
William Lallemand5685ccf2020-09-16 16:12:25 +02001648 }
1649 chunk_appendf(out, "Serial: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001650 if (ssl_sock_get_serial(cert, tmp) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001651 goto end;
1652 dump_binary(out, tmp->area, tmp->data);
1653 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001654
William Lallemand5685ccf2020-09-16 16:12:25 +02001655 chunk_appendf(out, "notBefore: ");
1656 chunk_reset(tmp);
1657 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1658 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001659 if (ASN1_TIME_print(bio, X509_getm_notBefore(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001660 goto end;
1661 write = BIO_read(bio, tmp->area, tmp->size-1);
1662 tmp->area[write] = '\0';
1663 BIO_free(bio);
1664 bio = NULL;
1665 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001666
William Lallemand5685ccf2020-09-16 16:12:25 +02001667 chunk_appendf(out, "notAfter: ");
1668 chunk_reset(tmp);
1669 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1670 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001671 if (ASN1_TIME_print(bio, X509_getm_notAfter(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001672 goto end;
1673 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
1674 goto end;
1675 tmp->area[write] = '\0';
1676 BIO_free(bio);
1677 bio = NULL;
1678 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001679
1680#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand5685ccf2020-09-16 16:12:25 +02001681 chunk_appendf(out, "Subject Alternative Name: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001682 if (ssl_sock_get_san_oneline(cert, out) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001683 goto end;
1684 *(out->area + out->data) = '\0';
1685 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001686#endif
William Lallemand5685ccf2020-09-16 16:12:25 +02001687 chunk_reset(tmp);
1688 chunk_appendf(out, "Algorithm: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001689 if (cert_get_pkey_algo(cert, tmp) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001690 goto end;
1691 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001692
William Lallemand5685ccf2020-09-16 16:12:25 +02001693 chunk_reset(tmp);
1694 chunk_appendf(out, "SHA1 FingerPrint: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001695 if (X509_digest(cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001696 goto end;
1697 tmp->data = len;
1698 dump_binary(out, tmp->area, tmp->data);
1699 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001700
William Lallemand5685ccf2020-09-16 16:12:25 +02001701 chunk_appendf(out, "Subject: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001702 if ((name = X509_get_subject_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001703 goto end;
1704 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1705 goto end;
1706 *(tmp->area + tmp->data) = '\0';
1707 chunk_appendf(out, "%s\n", tmp->area);
1708
1709 chunk_appendf(out, "Issuer: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001710 if ((name = X509_get_issuer_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001711 goto end;
1712 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1713 goto end;
1714 *(tmp->area + tmp->data) = '\0';
1715 chunk_appendf(out, "%s\n", tmp->area);
1716
1717 /* Displays subject of each certificate in the chain */
1718 for (i = 0; i < sk_X509_num(chain); i++) {
1719 X509 *ca = sk_X509_value(chain, i);
1720
1721 chunk_appendf(out, "Chain Subject: ");
1722 if ((name = X509_get_subject_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001723 goto end;
1724 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1725 goto end;
1726 *(tmp->area + tmp->data) = '\0';
1727 chunk_appendf(out, "%s\n", tmp->area);
1728
William Lallemand5685ccf2020-09-16 16:12:25 +02001729 chunk_appendf(out, "Chain Issuer: ");
1730 if ((name = X509_get_issuer_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001731 goto end;
1732 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1733 goto end;
1734 *(tmp->area + tmp->data) = '\0';
1735 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001736 }
1737
1738end:
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001739 if (bio)
1740 BIO_free(bio);
1741 free_trash_chunk(tmp);
1742
1743 return 0;
1744}
1745
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001746#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 +02001747/*
1748 * Build the OCSP tree entry's key for a given ckch_store.
1749 * Returns a negative value in case of error.
1750 */
1751static int ckch_store_build_certid(struct ckch_store *ckch_store, unsigned char certid[128], unsigned int *key_length)
1752{
1753 OCSP_RESPONSE *resp;
1754 OCSP_BASICRESP *bs = NULL;
1755 OCSP_SINGLERESP *sr;
1756 OCSP_CERTID *id;
1757 unsigned char *p = NULL;
1758
1759 if (!key_length)
1760 return -1;
1761
1762 *key_length = 0;
1763
1764 if (!ckch_store->ckch->ocsp_response)
1765 return 0;
1766
1767 p = (unsigned char *) ckch_store->ckch->ocsp_response->area;
1768
1769 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
1770 ckch_store->ckch->ocsp_response->data);
1771 if (!resp) {
1772 goto end;
1773 }
1774
1775 bs = OCSP_response_get1_basic(resp);
1776 if (!bs) {
1777 goto end;
1778 }
1779
1780 sr = OCSP_resp_get0(bs, 0);
1781 if (!sr) {
1782 goto end;
1783 }
1784
1785 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
1786
1787 p = certid;
1788 *key_length = i2d_OCSP_CERTID(id, &p);
1789
1790end:
1791 return *key_length > 0;
1792}
1793#endif
1794
1795/*
1796 * Dump the OCSP certificate key (if it exists) of certificate <ckch> into
1797 * buffer <out>.
1798 * Returns 0 in case of success.
1799 */
1800static int ckch_store_show_ocsp_certid(struct ckch_store *ckch_store, struct buffer *out)
1801{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001802#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 +02001803 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1804 unsigned int key_length = 0;
1805 int i;
1806
1807 if (ckch_store_build_certid(ckch_store, (unsigned char*)key, &key_length) >= 0) {
1808 /* Dump the CERTID info */
1809 chunk_appendf(out, "OCSP Response Key: ");
1810 for (i = 0; i < key_length; ++i) {
1811 chunk_appendf(out, "%02x", key[i]);
1812 }
1813 chunk_appendf(out, "\n");
1814 }
1815#endif
1816
1817 return 0;
1818}
1819
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001820
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001821/* IO handler of the details "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001822 * It uses a struct show_cert_ctx and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001823 */
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001824static int cli_io_handler_show_cert_detail(struct appctx *appctx)
1825{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001826 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001827 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001828 struct buffer *out = alloc_trash_chunk();
1829 int retval = 0;
1830
1831 if (!out)
1832 goto end_no_putchk;
1833
1834 chunk_appendf(out, "Filename: ");
1835 if (ckchs == ckchs_transaction.new_ckchs)
1836 chunk_appendf(out, "*");
1837 chunk_appendf(out, "%s\n", ckchs->path);
1838
1839 chunk_appendf(out, "Status: ");
1840 if (ckchs->ckch->cert == NULL)
1841 chunk_appendf(out, "Empty\n");
1842 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
1843 chunk_appendf(out, "Unused\n");
1844 else
1845 chunk_appendf(out, "Used\n");
1846
1847 retval = show_cert_detail(ckchs->ckch->cert, ckchs->ckch->chain, out);
1848 if (retval < 0)
1849 goto end_no_putchk;
1850 else if (retval)
1851 goto end;
1852
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001853 ckch_store_show_ocsp_certid(ckchs, out);
1854
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001855end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001856 if (applet_putchk(appctx, out) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001857 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001858
1859end_no_putchk:
William Lallemandda8584c2020-05-14 10:14:37 +02001860 free_trash_chunk(out);
1861 return 1;
1862yield:
William Lallemandda8584c2020-05-14 10:14:37 +02001863 free_trash_chunk(out);
1864 return 0; /* should come back */
1865}
1866
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001867
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001868/* IO handler of the details "show ssl cert <filename.ocsp>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001869 * It uses a show_cert_ctx.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001870 */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001871static int cli_io_handler_show_cert_ocsp_detail(struct appctx *appctx)
1872{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001873#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL)
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001874 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001875 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001876 struct buffer *out = alloc_trash_chunk();
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001877 int from_transaction = ctx->transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001878
1879 if (!out)
1880 goto end_no_putchk;
1881
1882 /* If we try to display an ongoing transaction's OCSP response, we
1883 * need to dump the ckch's ocsp_response buffer directly.
1884 * Otherwise, we must rebuild the certificate's certid in order to
1885 * look for the current OCSP response in the tree. */
1886 if (from_transaction && ckchs->ckch->ocsp_response) {
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001887 if (ssl_ocsp_response_print(ckchs->ckch->ocsp_response, out))
1888 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001889 }
1890 else {
1891 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1892 unsigned int key_length = 0;
1893
1894 if (ckch_store_build_certid(ckchs, (unsigned char*)key, &key_length) < 0)
1895 goto end_no_putchk;
1896
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001897 if (ssl_get_ocspresponse_detail(key, out))
1898 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001899 }
1900
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001901 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001902 goto yield;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001903
1904end_no_putchk:
1905 free_trash_chunk(out);
1906 return 1;
1907yield:
1908 free_trash_chunk(out);
1909 return 0; /* should come back */
1910#else
1911 return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n");
1912#endif
1913}
1914
William Lallemandda8584c2020-05-14 10:14:37 +02001915/* parsing function for 'show ssl cert [certfile]' */
1916static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
1917{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001918 struct show_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02001919 struct ckch_store *ckchs;
1920
1921 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1922 return cli_err(appctx, "Can't allocate memory!\n");
1923
1924 /* The operations on the CKCH architecture are locked so we can
1925 * manipulate ckch_store and ckch_inst */
1926 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1927 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
1928
1929 /* check if there is a certificate to lookup */
1930 if (*args[3]) {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001931 int show_ocsp_detail = 0;
1932 int from_transaction = 0;
1933 char *end;
1934
1935 /* We manage the special case "certname.ocsp" through which we
1936 * can show the details of an OCSP response. */
1937 end = strrchr(args[3], '.');
1938 if (end && strcmp(end+1, "ocsp") == 0) {
1939 *end = '\0';
1940 show_ocsp_detail = 1;
1941 }
1942
William Lallemandda8584c2020-05-14 10:14:37 +02001943 if (*args[3] == '*') {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001944 from_transaction = 1;
William Lallemandda8584c2020-05-14 10:14:37 +02001945 if (!ckchs_transaction.new_ckchs)
1946 goto error;
1947
1948 ckchs = ckchs_transaction.new_ckchs;
1949
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001950 if (strcmp(args[3] + 1, ckchs->path) != 0)
William Lallemandda8584c2020-05-14 10:14:37 +02001951 goto error;
1952
1953 } else {
1954 if ((ckchs = ckchs_lookup(args[3])) == NULL)
1955 goto error;
1956
1957 }
1958
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001959 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001960 /* use the IO handler that shows details */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001961 if (show_ocsp_detail) {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001962 ctx->transaction = from_transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001963 appctx->io_handler = cli_io_handler_show_cert_ocsp_detail;
1964 }
1965 else
1966 appctx->io_handler = cli_io_handler_show_cert_detail;
William Lallemandda8584c2020-05-14 10:14:37 +02001967 }
1968
1969 return 0;
1970
1971error:
1972 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1973 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
1974}
1975
1976/* release function of the `set ssl cert' command, free things and unlock the spinlock */
1977static void cli_release_commit_cert(struct appctx *appctx)
1978{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02001979 struct commit_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001980
1981 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulet9d56e242022-05-31 16:37:01 +02001982 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
1983 if (ctx->new_ckchs)
1984 ckch_store_free(ctx->new_ckchs);
1985 ha_free(&ctx->err);
William Lallemandda8584c2020-05-14 10:14:37 +02001986}
1987
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001988
1989/*
1990 * Rebuild a new instance 'new_inst' based on an old instance 'ckchi' and a
1991 * specific ckch_store.
1992 * Returns 0 in case of success, 1 otherwise.
1993 */
William Lallemande60c7d62022-03-30 11:26:15 +02001994int ckch_inst_rebuild(struct ckch_store *ckch_store, struct ckch_inst *ckchi,
1995 struct ckch_inst **new_inst, char **err)
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001996{
1997 int retval = 0;
1998 int errcode = 0;
1999 struct sni_ctx *sc0, *sc0s;
2000 char **sni_filter = NULL;
2001 int fcount = 0;
2002
2003 if (ckchi->crtlist_entry) {
2004 sni_filter = ckchi->crtlist_entry->filters;
2005 fcount = ckchi->crtlist_entry->fcount;
2006 }
2007
2008 if (ckchi->is_server_instance)
2009 errcode |= ckch_inst_new_load_srv_store(ckch_store->path, ckch_store, new_inst, err);
2010 else
2011 errcode |= ckch_inst_new_load_store(ckch_store->path, ckch_store, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, new_inst, err);
2012
2013 if (errcode & ERR_CODE)
2014 return 1;
2015
2016 /* if the previous ckchi was used as the default */
2017 if (ckchi->is_default)
2018 (*new_inst)->is_default = 1;
2019
2020 (*new_inst)->is_server_instance = ckchi->is_server_instance;
2021 (*new_inst)->server = ckchi->server;
2022 /* Create a new SSL_CTX and link it to the new instance. */
2023 if ((*new_inst)->is_server_instance) {
2024 retval = ssl_sock_prep_srv_ctx_and_inst(ckchi->server, (*new_inst)->ctx, (*new_inst));
2025 if (retval)
2026 return 1;
2027 }
2028
2029 /* create the link to the crtlist_entry */
2030 (*new_inst)->crtlist_entry = ckchi->crtlist_entry;
2031
2032 /* we need to initialize the SSL_CTX generated */
2033 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
2034 list_for_each_entry_safe(sc0, sc0s, &(*new_inst)->sni_ctx, by_ckch_inst) {
2035 if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
2036 errcode |= ssl_sock_prep_ctx_and_inst(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, *new_inst, err);
2037 if (errcode & ERR_CODE)
2038 return 1;
2039 }
2040 }
2041
2042 return 0;
2043}
2044
2045/*
2046 * Load all the new SNIs of a newly built ckch instance in the trees, or replace
2047 * a server's main ckch instance.
2048 */
2049static void __ssl_sock_load_new_ckch_instance(struct ckch_inst *ckchi)
2050{
2051 /* The bind_conf will be null on server ckch_instances. */
2052 if (ckchi->is_server_instance) {
2053 int i;
2054 /* a lock is needed here since we have to free the SSL cache */
2055 HA_RWLOCK_WRLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
2056 /* free the server current SSL_CTX */
2057 SSL_CTX_free(ckchi->server->ssl_ctx.ctx);
2058 /* Actual ssl context update */
2059 SSL_CTX_up_ref(ckchi->ctx);
2060 ckchi->server->ssl_ctx.ctx = ckchi->ctx;
2061 ckchi->server->ssl_ctx.inst = ckchi;
2062
2063 /* flush the session cache of the server */
2064 for (i = 0; i < global.nbthread; i++) {
William Lallemandce990332021-11-23 15:15:09 +01002065 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].sni);
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002066 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].ptr);
2067 }
2068 HA_RWLOCK_WRUNLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
2069
2070 } else {
2071 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
2072 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
2073 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
2074 }
2075}
2076
2077/*
2078 * Delete a ckch instance that was replaced after a CLI command.
2079 */
2080static void __ckch_inst_free_locked(struct ckch_inst *ckchi)
2081{
2082 if (ckchi->is_server_instance) {
2083 /* no lock for servers */
2084 ckch_inst_free(ckchi);
2085 } else {
2086 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
2087
2088 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
2089 ckch_inst_free(ckchi);
2090 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
2091 }
2092}
2093
William Lallemand3b5a3a62022-03-29 14:29:31 +02002094/* Replace a ckch_store in the ckch tree and insert the whole dependencies,
2095* then free the previous dependencies and store.
2096* Used in the case of a certificate update.
2097*
2098* Every dependencies must allocated before using this function.
2099*
2100* This function can't fail as it only update pointers, and does not alloc anything.
2101*
2102* /!\ This function must be used under the ckch lock. /!\
2103*
2104* - Insert every dependencies (SNI, crtlist_entry, ckch_inst, etc)
2105* - Delete the old ckch_store from the tree
2106* - Insert the new ckch_store
2107* - Free the old dependencies and the old ckch_store
2108*/
2109void ckch_store_replace(struct ckch_store *old_ckchs, struct ckch_store *new_ckchs)
2110{
2111 struct crtlist_entry *entry;
2112 struct ckch_inst *ckchi, *ckchis;
2113
2114 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
2115 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
2116 ebpt_delete(&entry->node);
2117 /* change the ptr and reinsert the node */
2118 entry->node.key = new_ckchs;
2119 ebpt_insert(&entry->crtlist->entries, &entry->node);
2120 }
2121 /* insert the new ckch_insts in the crtlist_entry */
2122 list_for_each_entry(ckchi, &new_ckchs->ckch_inst, by_ckchs) {
2123 if (ckchi->crtlist_entry)
2124 LIST_INSERT(&ckchi->crtlist_entry->ckch_inst, &ckchi->by_crtlist_entry);
2125 }
2126 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2127 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
2128 __ssl_sock_load_new_ckch_instance(ckchi);
2129 }
2130 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
2131 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
2132 __ckch_inst_free_locked(ckchi);
2133 }
2134
2135 ckch_store_free(old_ckchs);
2136 ebst_insert(&ckchs_tree, &new_ckchs->node);
2137}
2138
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002139
William Lallemandda8584c2020-05-14 10:14:37 +02002140/*
2141 * This function tries to create the new ckch_inst and their SNIs
William Lallemand30fcca12022-03-30 12:03:12 +02002142 *
2143 * /!\ don't forget to update __hlua_ckch_commit() if you changes things there. /!\
William Lallemandda8584c2020-05-14 10:14:37 +02002144 */
2145static int cli_io_handler_commit_cert(struct appctx *appctx)
2146{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002147 struct commit_cert_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002148 struct stconn *sc = appctx_sc(appctx);
William Lallemandda8584c2020-05-14 10:14:37 +02002149 int y = 0;
William Lallemandda8584c2020-05-14 10:14:37 +02002150 struct ckch_store *old_ckchs, *new_ckchs = NULL;
William Lallemand3b5a3a62022-03-29 14:29:31 +02002151 struct ckch_inst *ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002152
Willy Tarreau475e4632022-05-27 10:26:46 +02002153 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Christopher Faulet9d56e242022-05-31 16:37:01 +02002154 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02002155
2156 while (1) {
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002157 switch (ctx->state) {
2158 case CERT_ST_INIT:
William Lallemandda8584c2020-05-14 10:14:37 +02002159 /* This state just print the update message */
Christopher Faulet9d56e242022-05-31 16:37:01 +02002160 chunk_printf(&trash, "Committing %s", ckchs_transaction.path);
2161 if (applet_putchk(appctx, &trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02002162 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002163
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002164 ctx->state = CERT_ST_GEN;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002165 __fallthrough;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002166 case CERT_ST_GEN:
William Lallemandda8584c2020-05-14 10:14:37 +02002167 /*
2168 * This state generates the ckch instances with their
2169 * sni_ctxs and SSL_CTX.
2170 *
2171 * Since the SSL_CTX generation can be CPU consumer, we
2172 * yield every 10 instances.
2173 */
2174
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002175 old_ckchs = ctx->old_ckchs;
2176 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002177
William Lallemandda8584c2020-05-14 10:14:37 +02002178 /* get the next ckchi to regenerate */
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002179 ckchi = ctx->next_ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002180 /* we didn't start yet, set it to the first elem */
2181 if (ckchi == NULL)
2182 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
2183
2184 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
2185 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
2186 struct ckch_inst *new_inst;
William Lallemandda8584c2020-05-14 10:14:37 +02002187
Christopher Faulet9d56e242022-05-31 16:37:01 +02002188 /* save the next ckchi to compute in case of yield */
2189 ctx->next_ckchi = ckchi;
2190
William Lallemandda8584c2020-05-14 10:14:37 +02002191 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2192 if (y >= 10) {
Christopher Faulet9d56e242022-05-31 16:37:01 +02002193 applet_have_more_data(appctx); /* let's come back later */
William Lallemandda8584c2020-05-14 10:14:37 +02002194 goto yield;
2195 }
2196
Christopher Faulet9d56e242022-05-31 16:37:01 +02002197 /* display one dot per new instance */
2198 if (applet_putstr(appctx, ".") == -1)
2199 goto yield;
2200
2201 ctx->err = NULL;
2202 if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &ctx->err)) {
2203 ctx->state = CERT_ST_ERROR;
William Lallemandda8584c2020-05-14 10:14:37 +02002204 goto error;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002205 }
William Lallemandda8584c2020-05-14 10:14:37 +02002206
William Lallemandda8584c2020-05-14 10:14:37 +02002207 /* link the new ckch_inst to the duplicate */
Willy Tarreau2b718102021-04-21 07:32:39 +02002208 LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002209 y++;
2210 }
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002211 ctx->state = CERT_ST_INSERT;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002212 __fallthrough;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002213 case CERT_ST_INSERT:
William Lallemandda8584c2020-05-14 10:14:37 +02002214 /* The generation is finished, we can insert everything */
2215
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002216 old_ckchs = ctx->old_ckchs;
2217 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002218
William Lallemand3b5a3a62022-03-29 14:29:31 +02002219 /* insert everything and remove the previous objects */
2220 ckch_store_replace(old_ckchs, new_ckchs);
Christopher Faulet9d56e242022-05-31 16:37:01 +02002221 ctx->new_ckchs = ctx->old_ckchs = NULL;
2222 ctx->state = CERT_ST_SUCCESS;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002223 __fallthrough;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002224 case CERT_ST_SUCCESS:
2225 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2226 goto yield;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002227 ctx->state = CERT_ST_FIN;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002228 __fallthrough;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002229 case CERT_ST_FIN:
William Lallemandda8584c2020-05-14 10:14:37 +02002230 /* we achieved the transaction, we can set everything to NULL */
William Lallemandda8584c2020-05-14 10:14:37 +02002231 ckchs_transaction.new_ckchs = NULL;
2232 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002233 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002234 goto end;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002235
2236 case CERT_ST_ERROR:
2237 error:
2238 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2239 if (applet_putchk(appctx, &trash) == -1)
2240 goto yield;
2241 ctx->state = CERT_ST_FIN;
2242 break;
William Lallemandda8584c2020-05-14 10:14:37 +02002243 }
2244 }
2245end:
William Lallemandda8584c2020-05-14 10:14:37 +02002246 /* success: call the release function and don't come back */
2247 return 1;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002248
William Lallemandda8584c2020-05-14 10:14:37 +02002249yield:
William Lallemandda8584c2020-05-14 10:14:37 +02002250 return 0; /* should come back */
William Lallemandda8584c2020-05-14 10:14:37 +02002251}
2252
2253/*
2254 * Parsing function of 'commit ssl cert'
2255 */
2256static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
2257{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002258 struct commit_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02002259 char *err = NULL;
2260
2261 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2262 return 1;
2263
2264 if (!*args[3])
2265 return cli_err(appctx, "'commit ssl cert expects a filename\n");
2266
2267 /* The operations on the CKCH architecture are locked so we can
2268 * manipulate ckch_store and ckch_inst */
2269 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2270 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
2271
2272 if (!ckchs_transaction.path) {
2273 memprintf(&err, "No ongoing transaction! !\n");
2274 goto error;
2275 }
2276
2277 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2278 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
2279 goto error;
2280 }
2281
William Lallemand5685ccf2020-09-16 16:12:25 +02002282 /* if a certificate is here, a private key must be here too */
2283 if (ckchs_transaction.new_ckchs->ckch->cert && !ckchs_transaction.new_ckchs->ckch->key) {
2284 memprintf(&err, "The transaction must contain at least a certificate and a private key!\n");
2285 goto error;
2286 }
William Lallemanda9419522020-06-24 16:26:41 +02002287
William Lallemand5685ccf2020-09-16 16:12:25 +02002288 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
2289 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
2290 goto error;
William Lallemandda8584c2020-05-14 10:14:37 +02002291 }
2292
2293 /* init the appctx structure */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002294 ctx->state = CERT_ST_INIT;
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002295 ctx->next_ckchi = NULL;
2296 ctx->new_ckchs = ckchs_transaction.new_ckchs;
2297 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002298
2299 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
2300 return 0;
2301
2302error:
2303
2304 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2305 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2306
2307 return cli_dynerr(appctx, err);
2308}
2309
2310
2311
2312
2313/*
2314 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
Willy Tarreau329f4b42022-05-04 20:05:55 +02002315 * It uses a set_cert_ctx context, and ckchs_transaction under a lock.
William Lallemandda8584c2020-05-14 10:14:37 +02002316 */
2317static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
2318{
2319 struct ckch_store *new_ckchs = NULL;
2320 struct ckch_store *old_ckchs = NULL;
2321 char *err = NULL;
2322 int i;
William Lallemandda8584c2020-05-14 10:14:37 +02002323 int errcode = 0;
2324 char *end;
William Lallemandff8bf982022-03-29 10:44:23 +02002325 struct cert_exts *cert_ext = &cert_exts[0]; /* default one, PEM */
William Lallemandda8584c2020-05-14 10:14:37 +02002326 struct cert_key_and_chain *ckch;
2327 struct buffer *buf;
2328
2329 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2330 return 1;
2331
William Lallemandda8584c2020-05-14 10:14:37 +02002332 if (!*args[3] || !payload)
2333 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
2334
2335 /* The operations on the CKCH architecture are locked so we can
2336 * manipulate ckch_store and ckch_inst */
2337 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2338 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
2339
William Lallemand5ba80d62021-05-04 16:17:27 +02002340 if ((buf = alloc_trash_chunk()) == NULL) {
2341 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2342 errcode |= ERR_ALERT | ERR_FATAL;
2343 goto end;
2344 }
William Lallemande5ff4ad2020-06-08 09:40:37 +02002345
William Lallemandda8584c2020-05-14 10:14:37 +02002346 if (!chunk_strcpy(buf, args[3])) {
2347 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2348 errcode |= ERR_ALERT | ERR_FATAL;
2349 goto end;
2350 }
2351
2352 /* check which type of file we want to update */
William Lallemandff8bf982022-03-29 10:44:23 +02002353 for (i = 0; cert_exts[i].ext != NULL; i++) {
William Lallemandda8584c2020-05-14 10:14:37 +02002354 end = strrchr(buf->area, '.');
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002355 if (end && *cert_exts[i].ext && (strcmp(end + 1, cert_exts[i].ext) == 0)) {
William Lallemandda8584c2020-05-14 10:14:37 +02002356 *end = '\0';
William Lallemand089c1382020-10-23 17:35:12 +02002357 buf->data = strlen(buf->area);
William Lallemandff8bf982022-03-29 10:44:23 +02002358 cert_ext = &cert_exts[i];
William Lallemandda8584c2020-05-14 10:14:37 +02002359 break;
2360 }
2361 }
2362
William Lallemandda8584c2020-05-14 10:14:37 +02002363 /* if there is an ongoing transaction */
2364 if (ckchs_transaction.path) {
William Lallemandda8584c2020-05-14 10:14:37 +02002365 /* if there is an ongoing transaction, check if this is the same file */
2366 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
William Lallemand089c1382020-10-23 17:35:12 +02002367 /* we didn't find the transaction, must try more cases below */
2368
2369 /* 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 +02002370 if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
William Lallemand089c1382020-10-23 17:35:12 +02002371 if (!chunk_strcat(buf, ".crt")) {
2372 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2373 errcode |= ERR_ALERT | ERR_FATAL;
2374 goto end;
2375 }
2376
2377 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
2378 /* remove .crt of the error message */
2379 *(b_orig(buf) + b_data(buf) + strlen(".crt")) = '\0';
2380 b_sub(buf, strlen(".crt"));
2381
2382 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
2383 errcode |= ERR_ALERT | ERR_FATAL;
2384 goto end;
2385 }
2386 }
William Lallemandda8584c2020-05-14 10:14:37 +02002387 }
2388
Christopher Faulet24a20b92022-06-03 11:50:40 +02002389 old_ckchs = ckchs_transaction.new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002390
2391 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002392
William Lallemand95fefa12020-09-09 12:01:33 +02002393 /* lookup for the certificate in the tree */
Christopher Faulet24a20b92022-06-03 11:50:40 +02002394 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002395
Christopher Faulet24a20b92022-06-03 11:50:40 +02002396 if (!old_ckchs) {
William Lallemand089c1382020-10-23 17:35:12 +02002397 /* 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 +02002398 if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
William Lallemand089c1382020-10-23 17:35:12 +02002399 if (!chunk_strcat(buf, ".crt")) {
2400 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2401 errcode |= ERR_ALERT | ERR_FATAL;
2402 goto end;
2403 }
Christopher Faulet24a20b92022-06-03 11:50:40 +02002404 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002405 }
2406 }
William Lallemandda8584c2020-05-14 10:14:37 +02002407 }
2408
Christopher Faulet24a20b92022-06-03 11:50:40 +02002409 if (!old_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02002410 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
2411 err ? err : "");
2412 errcode |= ERR_ALERT | ERR_FATAL;
2413 goto end;
2414 }
2415
William Lallemandda8584c2020-05-14 10:14:37 +02002416 /* duplicate the ckch store */
2417 new_ckchs = ckchs_dup(old_ckchs);
2418 if (!new_ckchs) {
2419 memprintf(&err, "%sCannot allocate memory!\n",
2420 err ? err : "");
2421 errcode |= ERR_ALERT | ERR_FATAL;
2422 goto end;
2423 }
2424
William Lallemand95fefa12020-09-09 12:01:33 +02002425 ckch = new_ckchs->ckch;
William Lallemandda8584c2020-05-14 10:14:37 +02002426
2427 /* appply the change on the duplicate */
William Lallemandff8bf982022-03-29 10:44:23 +02002428 if (cert_ext->load(buf->area, payload, ckch, &err) != 0) {
William Lallemandda8584c2020-05-14 10:14:37 +02002429 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
2430 errcode |= ERR_ALERT | ERR_FATAL;
2431 goto end;
2432 }
2433
William Lallemandda8584c2020-05-14 10:14:37 +02002434 /* we succeed, we can save the ckchs in the transaction */
2435
2436 /* if there wasn't a transaction, update the old ckchs */
2437 if (!ckchs_transaction.old_ckchs) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002438 ckchs_transaction.old_ckchs = old_ckchs;
2439 ckchs_transaction.path = old_ckchs->path;
William Lallemandda8584c2020-05-14 10:14:37 +02002440 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
2441 } else {
2442 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
2443
2444 }
2445
2446 /* free the previous ckchs if there was a transaction */
2447 ckch_store_free(ckchs_transaction.new_ckchs);
2448
Christopher Faulet24a20b92022-06-03 11:50:40 +02002449 ckchs_transaction.new_ckchs = new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002450
2451
2452 /* creates the SNI ctxs later in the IO handler */
2453
2454end:
2455 free_trash_chunk(buf);
2456
2457 if (errcode & ERR_CODE) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002458 ckch_store_free(new_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002459 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2460 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2461 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002462 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2463 return cli_dynmsg(appctx, LOG_NOTICE, err);
2464 }
2465 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
2466}
2467
2468/* parsing function of 'abort ssl cert' */
2469static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
2470{
2471 char *err = NULL;
2472
2473 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2474 return 1;
2475
2476 if (!*args[3])
2477 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
2478
2479 /* The operations on the CKCH architecture are locked so we can
2480 * manipulate ckch_store and ckch_inst */
2481 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2482 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2483
2484 if (!ckchs_transaction.path) {
2485 memprintf(&err, "No ongoing transaction!\n");
2486 goto error;
2487 }
2488
2489 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2490 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
2491 goto error;
2492 }
2493
2494 /* Only free the ckchs there, because the SNI and instances were not generated yet */
2495 ckch_store_free(ckchs_transaction.new_ckchs);
2496 ckchs_transaction.new_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002497 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002498 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002499
2500 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2501
2502 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2503 return cli_dynmsg(appctx, LOG_NOTICE, err);
2504
2505error:
2506 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2507
2508 return cli_dynerr(appctx, err);
2509}
2510
2511/* parsing function of 'new ssl cert' */
2512static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
2513{
2514 struct ckch_store *store;
2515 char *err = NULL;
2516 char *path;
2517
2518 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2519 return 1;
2520
2521 if (!*args[3])
2522 return cli_err(appctx, "'new ssl cert' expects a filename\n");
2523
2524 path = args[3];
2525
2526 /* The operations on the CKCH architecture are locked so we can
2527 * manipulate ckch_store and ckch_inst */
2528 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2529 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
2530
2531 store = ckchs_lookup(path);
2532 if (store != NULL) {
2533 memprintf(&err, "Certificate '%s' already exists!\n", path);
2534 store = NULL; /* we don't want to free it */
2535 goto error;
2536 }
2537 /* we won't support multi-certificate bundle here */
William Lallemandbd8e6ed2020-09-16 16:08:08 +02002538 store = ckch_store_new(path);
William Lallemandda8584c2020-05-14 10:14:37 +02002539 if (!store) {
2540 memprintf(&err, "unable to allocate memory.\n");
2541 goto error;
2542 }
2543
2544 /* insert into the ckchs tree */
2545 ebst_insert(&ckchs_tree, &store->node);
2546 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
2547
2548 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2549 return cli_dynmsg(appctx, LOG_NOTICE, err);
2550error:
2551 free(store);
2552 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2553 return cli_dynerr(appctx, err);
2554}
2555
2556/* parsing function of 'del ssl cert' */
2557static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
2558{
2559 struct ckch_store *store;
2560 char *err = NULL;
2561 char *filename;
2562
2563 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2564 return 1;
2565
2566 if (!*args[3])
2567 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
2568
2569 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2570 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
2571
2572 filename = args[3];
2573
Christopher Faulet926fefc2022-05-31 18:04:25 +02002574 if (ckchs_transaction.path && strcmp(ckchs_transaction.path, filename) == 0) {
2575 memprintf(&err, "ongoing transaction for the certificate '%s'", filename);
2576 goto error;
2577 }
2578
William Lallemandda8584c2020-05-14 10:14:37 +02002579 store = ckchs_lookup(filename);
2580 if (store == NULL) {
2581 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
2582 goto error;
2583 }
2584 if (!LIST_ISEMPTY(&store->ckch_inst)) {
2585 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
2586 goto error;
2587 }
2588
2589 ebmb_delete(&store->node);
2590 ckch_store_free(store);
2591
2592 memprintf(&err, "Certificate '%s' deleted!\n", filename);
2593
2594 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2595 return cli_dynmsg(appctx, LOG_NOTICE, err);
2596
2597error:
2598 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
2599 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2600 return cli_dynerr(appctx, err);
2601}
2602
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002603
Remi Tricot-Le Breton9f40fe02021-03-16 16:21:27 +01002604
2605/* parsing function of 'new ssl ca-file' */
2606static int cli_parse_new_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2607{
2608 struct cafile_entry *cafile_entry;
2609 char *err = NULL;
2610 char *path;
2611
2612 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2613 return 1;
2614
2615 if (!*args[3])
2616 return cli_err(appctx, "'new ssl ca-file' expects a filename\n");
2617
2618 path = args[3];
2619
2620 /* The operations on the CKCH architecture are locked so we can
2621 * manipulate ckch_store and ckch_inst */
2622 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2623 return cli_err(appctx, "Can't create a CA file!\nOperations on certificates are currently locked!\n");
2624
2625 cafile_entry = ssl_store_get_cafile_entry(path, 0);
2626 if (cafile_entry) {
2627 memprintf(&err, "CA file '%s' already exists!\n", path);
2628 goto error;
2629 }
2630
2631 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CERT);
2632 if (!cafile_entry) {
2633 memprintf(&err, "%sCannot allocate memory!\n",
2634 err ? err : "");
2635 goto error;
2636 }
2637
2638 /* Add the newly created cafile_entry to the tree so that
2639 * any new ckch instance created from now can use it. */
2640 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
2641 goto error;
2642
2643 memprintf(&err, "New CA file created '%s'!\n", path);
2644
2645 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2646 return cli_dynmsg(appctx, LOG_NOTICE, err);
2647error:
2648 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2649 return cli_dynerr(appctx, err);
2650}
2651
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002652/*
2653 * Parsing function of `set ssl ca-file`
2654 */
2655static int cli_parse_set_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2656{
Christopher Faulet132c5952022-06-03 11:56:26 +02002657 struct cafile_entry *old_cafile_entry = NULL;
2658 struct cafile_entry *new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002659 char *err = NULL;
2660 int errcode = 0;
2661 struct buffer *buf;
William Lallemand62c0b992022-07-29 17:50:58 +02002662 int add_cmd = 0;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002663
2664 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2665 return 1;
2666
William Lallemand62c0b992022-07-29 17:50:58 +02002667 /* this is "add ssl ca-file" */
2668 if (*args[0] == 'a')
2669 add_cmd = 1;
2670
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002671 if (!*args[3] || !payload)
2672 return cli_err(appctx, "'set ssl ca-file expects a filename and CAs as a payload\n");
2673
2674 /* The operations on the CKCH architecture are locked so we can
2675 * manipulate ckch_store and ckch_inst */
2676 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2677 return cli_err(appctx, "Can't update the CA file!\nOperations on certificates are currently locked!\n");
2678
2679 if ((buf = alloc_trash_chunk()) == NULL) {
2680 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2681 errcode |= ERR_ALERT | ERR_FATAL;
2682 goto end;
2683 }
2684
2685 if (!chunk_strcpy(buf, args[3])) {
2686 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2687 errcode |= ERR_ALERT | ERR_FATAL;
2688 goto end;
2689 }
2690
Christopher Faulet132c5952022-06-03 11:56:26 +02002691 old_cafile_entry = NULL;
2692 new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002693
2694 /* if there is an ongoing transaction */
2695 if (cafile_transaction.path) {
2696 /* if there is an ongoing transaction, check if this is the same file */
2697 if (strcmp(cafile_transaction.path, buf->area) != 0) {
2698 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, buf->area);
2699 errcode |= ERR_ALERT | ERR_FATAL;
2700 goto end;
2701 }
Christopher Faulet132c5952022-06-03 11:56:26 +02002702 old_cafile_entry = cafile_transaction.old_cafile_entry;
William Lallemand62c0b992022-07-29 17:50:58 +02002703 } else {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002704 /* lookup for the certificate in the tree */
Christopher Faulet132c5952022-06-03 11:56:26 +02002705 old_cafile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002706 }
2707
Christopher Faulet132c5952022-06-03 11:56:26 +02002708 if (!old_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002709 memprintf(&err, "%sCan't replace a CA file which is not referenced by the configuration!\n",
2710 err ? err : "");
2711 errcode |= ERR_ALERT | ERR_FATAL;
2712 goto end;
2713 }
2714
William Lallemand62c0b992022-07-29 17:50:58 +02002715 /* if the transaction is new, duplicate the old_ca_file_entry, otherwise duplicate the cafile in the current transaction */
2716 if (cafile_transaction.new_cafile_entry)
2717 new_cafile_entry = ssl_store_dup_cafile_entry(cafile_transaction.new_cafile_entry);
2718 else
2719 new_cafile_entry = ssl_store_dup_cafile_entry(old_cafile_entry);
2720
Christopher Faulet132c5952022-06-03 11:56:26 +02002721 if (!new_cafile_entry) {
William Lallemand62c0b992022-07-29 17:50:58 +02002722 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002723 errcode |= ERR_ALERT | ERR_FATAL;
2724 goto end;
2725 }
2726
William Lallemand62c0b992022-07-29 17:50:58 +02002727 /* Fill the new entry with the new CAs. The add_cmd variable determine
2728 if we flush the X509_STORE or not */
2729 if (ssl_store_load_ca_from_buf(new_cafile_entry, payload, add_cmd)) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002730 memprintf(&err, "%sInvalid payload\n", err ? err : "");
2731 errcode |= ERR_ALERT | ERR_FATAL;
2732 goto end;
2733 }
2734
2735 /* we succeed, we can save the ca in the transaction */
2736
2737 /* if there wasn't a transaction, update the old CA */
2738 if (!cafile_transaction.old_cafile_entry) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002739 cafile_transaction.old_cafile_entry = old_cafile_entry;
2740 cafile_transaction.path = old_cafile_entry->path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002741 err = memprintf(&err, "transaction created for CA %s!\n", cafile_transaction.path);
2742 } else {
2743 err = memprintf(&err, "transaction updated for CA %s!\n", cafile_transaction.path);
2744 }
2745
2746 /* free the previous CA if there was a transaction */
2747 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2748
Christopher Faulet132c5952022-06-03 11:56:26 +02002749 cafile_transaction.new_cafile_entry = new_cafile_entry;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002750
2751 /* creates the SNI ctxs later in the IO handler */
2752
2753end:
2754 free_trash_chunk(buf);
2755
2756 if (errcode & ERR_CODE) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002757 ssl_store_delete_cafile_entry(new_cafile_entry);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002758 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2759 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2760 } else {
2761
2762 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2763 return cli_dynmsg(appctx, LOG_NOTICE, err);
2764 }
2765}
2766
2767
2768/*
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002769 * Parsing function of 'commit ssl ca-file'.
2770 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl crl-file".
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002771 */
2772static int cli_parse_commit_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2773{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002774 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002775 char *err = NULL;
2776
2777 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2778 return 1;
2779
2780 if (!*args[3])
2781 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
2782
2783 /* The operations on the CKCH architecture are locked so we can
2784 * manipulate ckch_store and ckch_inst */
2785 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2786 return cli_err(appctx, "Can't commit the CA file!\nOperations on certificates are currently locked!\n");
2787
2788 if (!cafile_transaction.path) {
2789 memprintf(&err, "No ongoing transaction! !\n");
2790 goto error;
2791 }
2792
2793 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2794 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, args[3]);
2795 goto error;
2796 }
2797 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002798 ctx->state = CACRL_ST_INIT;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002799 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002800 ctx->old_entry = cafile_transaction.old_cafile_entry;
2801 ctx->new_entry = cafile_transaction.new_cafile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002802 ctx->cafile_type = CAFILE_CERT;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002803
2804 return 0;
2805
2806error:
2807
2808 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2809 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2810
2811 return cli_dynerr(appctx, err);
2812}
2813
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002814/*
2815 * This function tries to create new ckch instances and their SNIs using a newly
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002816 * set certificate authority (CA file) or a newly set Certificate Revocation
2817 * List (CRL), depending on the command being called.
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002818 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002819static int cli_io_handler_commit_cafile_crlfile(struct appctx *appctx)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002820{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002821 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002822 struct stconn *sc = appctx_sc(appctx);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002823 int y = 0;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002824 struct cafile_entry *old_cafile_entry = ctx->old_entry;
2825 struct cafile_entry *new_cafile_entry = ctx->new_entry;
William Lallemand0bfa3e72022-08-30 17:32:38 +02002826 struct ckch_inst_link *ckchi_link;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002827 char *path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002828
Willy Tarreau475e4632022-05-27 10:26:46 +02002829 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002830 goto end;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002831
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002832 /* The ctx was already validated by the ca-file/crl-file parsing
2833 * function. Entries can only be NULL in CACRL_ST_SUCCESS or
2834 * CACRL_ST_FIN states
2835 */
2836 switch (ctx->cafile_type) {
2837 case CAFILE_CERT:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002838 path = cafile_transaction.path;
2839 break;
2840 case CAFILE_CRL:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002841 path = crlfile_transaction.path;
2842 break;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002843 default:
Willy Tarreaud543ae02022-06-22 05:40:25 +02002844 path = NULL;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002845 goto error;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002846 }
2847
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002848 while (1) {
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002849 switch (ctx->state) {
2850 case CACRL_ST_INIT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002851 /* This state just print the update message */
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002852 chunk_printf(&trash, "Committing %s", path);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002853 if (applet_putchk(appctx, &trash) == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002854 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002855
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002856 ctx->state = CACRL_ST_GEN;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002857 __fallthrough;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002858 case CACRL_ST_GEN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002859 /*
2860 * This state generates the ckch instances with their
2861 * sni_ctxs and SSL_CTX.
2862 *
2863 * Since the SSL_CTX generation can be CPU consumer, we
2864 * yield every 10 instances.
2865 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002866
2867 /* get the next ckchi to regenerate */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002868 ckchi_link = ctx->next_ckchi_link;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002869
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002870 /* we didn't start yet, set it to the first elem */
2871 if (ckchi_link == NULL) {
2872 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2873 /* Add the newly created cafile_entry to the tree so that
2874 * any new ckch instance created from now can use it. */
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002875 if (ssl_store_add_uncommitted_cafile_entry(new_cafile_entry)) {
2876 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002877 goto error;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002878 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002879 }
2880
2881 list_for_each_entry_from(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002882 struct ckch_inst *new_inst;
2883
2884 /* save the next ckchi to compute */
2885 ctx->next_ckchi_link = ckchi_link;
2886
2887 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2888 if (y >= 10) {
2889 applet_have_more_data(appctx); /* let's come back later */
2890 goto yield;
2891 }
2892
2893 /* display one dot per new instance */
2894 if (applet_putstr(appctx, ".") == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002895 goto yield;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002896
2897 /* Rebuild a new ckch instance that uses the same ckch_store
2898 * than a reference ckchi instance but will use a new CA file. */
2899 ctx->err = NULL;
2900 if (ckch_inst_rebuild(ckchi_link->ckch_inst->ckch_store, ckchi_link->ckch_inst, &new_inst, &ctx->err)) {
2901 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002902 goto error;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002903 }
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002904
2905 y++;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002906 }
2907
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002908 ctx->state = CACRL_ST_INSERT;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002909 __fallthrough;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002910 case CACRL_ST_INSERT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002911 /* The generation is finished, we can insert everything */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002912
2913 /* insert the new ckch_insts in the crtlist_entry */
2914 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2915 if (ckchi_link->ckch_inst->crtlist_entry)
2916 LIST_INSERT(&ckchi_link->ckch_inst->crtlist_entry->ckch_inst,
2917 &ckchi_link->ckch_inst->by_crtlist_entry);
2918 }
2919
2920 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2921 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2922 __ssl_sock_load_new_ckch_instance(ckchi_link->ckch_inst);
2923 }
2924
William Lallemande0fa91f2022-08-31 14:26:49 +02002925 /* delete the old sni_ctx, the old ckch_insts
2926 * and the ckch_store. ckch_inst_free() also
2927 * manipulates the list so it's cleaner to loop
2928 * until it's empty */
2929 while (!LIST_ISEMPTY(&old_cafile_entry->ckch_inst_link)) {
2930 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2931
2932 LIST_DEL_INIT(&ckchi_link->list); /* must reinit because ckch_inst checks the list */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002933 __ckch_inst_free_locked(ckchi_link->ckch_inst);
William Lallemande0fa91f2022-08-31 14:26:49 +02002934 free(ckchi_link);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002935 }
2936
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002937 /* Remove the old cafile entry from the tree */
2938 ebmb_delete(&old_cafile_entry->node);
2939 ssl_store_delete_cafile_entry(old_cafile_entry);
2940
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002941 ctx->old_entry = ctx->new_entry = NULL;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002942 ctx->state = CACRL_ST_SUCCESS;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002943 __fallthrough;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002944 case CACRL_ST_SUCCESS:
2945 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2946 goto yield;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002947 ctx->state = CACRL_ST_FIN;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002948 __fallthrough;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002949 case CACRL_ST_FIN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002950 /* we achieved the transaction, we can set everything to NULL */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002951 switch (ctx->cafile_type) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002952 case CAFILE_CERT:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002953 cafile_transaction.old_cafile_entry = NULL;
2954 cafile_transaction.new_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002955 cafile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002956 break;
2957 case CAFILE_CRL:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002958 crlfile_transaction.old_crlfile_entry = NULL;
2959 crlfile_transaction.new_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002960 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002961 break;
2962 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002963 goto end;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002964
2965 case CACRL_ST_ERROR:
2966 error:
2967 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2968 if (applet_putchk(appctx, &trash) == -1)
2969 goto yield;
2970 ctx->state = CACRL_ST_FIN;
2971 break;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002972 }
2973 }
2974end:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002975 /* success: call the release function and don't come back */
2976 return 1;
2977yield:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002978 return 0; /* should come back */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002979}
2980
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002981
2982/* parsing function of 'abort ssl ca-file' */
2983static int cli_parse_abort_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2984{
2985 char *err = NULL;
2986
2987 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2988 return 1;
2989
2990 if (!*args[3])
2991 return cli_err(appctx, "'abort ssl ca-file' expects a filename\n");
2992
2993 /* The operations on the CKCH architecture are locked so we can
2994 * manipulate ckch_store and ckch_inst */
2995 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2996 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2997
2998 if (!cafile_transaction.path) {
2999 memprintf(&err, "No ongoing transaction!\n");
3000 goto error;
3001 }
3002
3003 if (strcmp(cafile_transaction.path, args[3]) != 0) {
3004 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", cafile_transaction.path, args[3]);
3005 goto error;
3006 }
3007
3008 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3009 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
3010 cafile_transaction.new_cafile_entry = NULL;
3011 cafile_transaction.old_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02003012 cafile_transaction.path = NULL;
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01003013
3014 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3015
3016 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3017 return cli_dynmsg(appctx, LOG_NOTICE, err);
3018
3019error:
3020 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3021
3022 return cli_dynerr(appctx, err);
3023}
3024
Willy Tarreau821c3b02022-05-04 15:47:39 +02003025/* release function of the `commit ssl ca-file' command, free things and unlock the spinlock.
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003026 * It uses a commit_cacrlfile_ctx context.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003027 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003028static void cli_release_commit_cafile(struct appctx *appctx)
3029{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003030 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003031 struct cafile_entry *new_cafile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003032
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003033 /* Remove the uncommitted cafile_entry from the tree. */
3034 if (new_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003035 ebmb_delete(&new_cafile_entry->node);
3036 ssl_store_delete_cafile_entry(new_cafile_entry);
3037 }
3038 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003039 ha_free(&ctx->err);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003040}
3041
3042
Willy Tarreau821c3b02022-05-04 15:47:39 +02003043/* IO handler of details "show ssl ca-file <filename[:index]>".
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003044 * It uses a show_cafile_ctx context, and the global
3045 * cafile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003046 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003047static int cli_io_handler_show_cafile_detail(struct appctx *appctx)
3048{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003049 struct show_cafile_ctx *ctx = appctx->svcctx;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003050 struct cafile_entry *cafile_entry = ctx->cur_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003051 struct buffer *out = alloc_trash_chunk();
William Lallemand03a32e52022-04-26 18:17:15 +02003052 int i = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003053 X509 *cert;
3054 STACK_OF(X509_OBJECT) *objs;
3055 int retval = 0;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003056 int ca_index = ctx->ca_index;
3057 int show_all = ctx->show_all;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003058
3059 if (!out)
3060 goto end_no_putchk;
3061
3062 chunk_appendf(out, "Filename: ");
3063 if (cafile_entry == cafile_transaction.new_cafile_entry)
3064 chunk_appendf(out, "*");
3065 chunk_appendf(out, "%s\n", cafile_entry->path);
3066
3067 chunk_appendf(out, "Status: ");
3068 if (!cafile_entry->ca_store)
3069 chunk_appendf(out, "Empty\n");
3070 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3071 chunk_appendf(out, "Unused\n");
3072 else
3073 chunk_appendf(out, "Used\n");
3074
3075 if (!cafile_entry->ca_store)
3076 goto end;
3077
3078 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
William Lallemand03a32e52022-04-26 18:17:15 +02003079 for (i = ca_index; i < sk_X509_OBJECT_num(objs); i++) {
3080
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003081 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
3082 if (!cert)
3083 continue;
3084
William Lallemand03a32e52022-04-26 18:17:15 +02003085 /* file starts at line 1 */
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003086 chunk_appendf(out, " \nCertificate #%d:\n", i+1);
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003087 retval = show_cert_detail(cert, NULL, out);
3088 if (retval < 0)
3089 goto end_no_putchk;
William Lallemand03a32e52022-04-26 18:17:15 +02003090 else if (retval)
3091 goto yield;
3092
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003093 if (applet_putchk(appctx, out) == -1)
William Lallemand03a32e52022-04-26 18:17:15 +02003094 goto yield;
William Lallemand03a32e52022-04-26 18:17:15 +02003095
3096 if (!show_all) /* only need to dump one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003097 goto end;
3098 }
3099
3100end:
William Lallemand03a32e52022-04-26 18:17:15 +02003101 free_trash_chunk(out);
3102 return 1; /* end, don't come back */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003103
3104end_no_putchk:
3105 free_trash_chunk(out);
3106 return 1;
3107yield:
William Lallemand03a32e52022-04-26 18:17:15 +02003108 /* save the current state */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003109 ctx->ca_index = i;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003110 free_trash_chunk(out);
3111 return 0; /* should come back */
3112}
3113
3114
Willy Tarreau06305792022-05-04 15:57:30 +02003115/* parsing function for 'show ssl ca-file [cafile[:index]]'.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003116 * It prepares a show_cafile_ctx context, and checks the global
3117 * cafile_transaction under the ckch_lock (read only).
Willy Tarreau06305792022-05-04 15:57:30 +02003118 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003119static int cli_parse_show_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3120{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003121 struct show_cafile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003122 struct cafile_entry *cafile_entry;
William Lallemand03a32e52022-04-26 18:17:15 +02003123 int ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003124 char *colons;
3125 char *err = NULL;
3126
3127 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3128 return cli_err(appctx, "Can't allocate memory!\n");
3129
3130 /* The operations on the CKCH architecture are locked so we can
3131 * manipulate ckch_store and ckch_inst */
3132 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3133 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3134
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003135 ctx->show_all = 1; /* show all certificates */
3136 ctx->ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003137 /* check if there is a certificate to lookup */
3138 if (*args[3]) {
3139
3140 /* Look for an optional CA index after the CA file name */
3141 colons = strchr(args[3], ':');
3142 if (colons) {
3143 char *endptr;
3144
3145 ca_index = strtol(colons + 1, &endptr, 10);
3146 /* Indexes start at 1 */
3147 if (colons + 1 == endptr || *endptr != '\0' || ca_index <= 0) {
3148 memprintf(&err, "wrong CA index after colons in '%s'!", args[3]);
3149 goto error;
3150 }
3151 *colons = '\0';
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003152 ctx->ca_index = ca_index - 1; /* we start counting at 0 in the ca_store, but at 1 on the CLI */
3153 ctx->show_all = 0; /* show only one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003154 }
3155
3156 if (*args[3] == '*') {
3157 if (!cafile_transaction.new_cafile_entry)
3158 goto error;
3159
3160 cafile_entry = cafile_transaction.new_cafile_entry;
3161
3162 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3163 goto error;
3164
3165 } else {
3166 /* Get the "original" cafile_entry and not the
3167 * uncommitted one if it exists. */
3168 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CERT)
3169 goto error;
3170 }
3171
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003172 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003173 /* use the IO handler that shows details */
3174 appctx->io_handler = cli_io_handler_show_cafile_detail;
3175 }
3176
3177 return 0;
3178
3179error:
3180 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3181 if (err)
3182 return cli_dynerr(appctx, err);
3183 return cli_err(appctx, "Can't display the CA file : Not found!\n");
3184}
3185
3186
3187/* release function of the 'show ssl ca-file' command */
3188static void cli_release_show_cafile(struct appctx *appctx)
3189{
3190 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3191}
3192
3193
3194/* This function returns the number of certificates in a cafile_entry. */
3195static int get_certificate_count(struct cafile_entry *cafile_entry)
3196{
3197 int cert_count = 0;
3198 STACK_OF(X509_OBJECT) *objs;
3199
3200 if (cafile_entry && cafile_entry->ca_store) {
3201 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3202 if (objs)
3203 cert_count = sk_X509_OBJECT_num(objs);
3204 }
3205 return cert_count;
3206}
3207
3208/* IO handler of "show ssl ca-file". The command taking a specific CA file name
Willy Tarreau821c3b02022-05-04 15:47:39 +02003209 * is managed in cli_io_handler_show_cafile_detail.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003210 * It uses a show_cafile_ctx and the global cafile_transaction.new_cafile_entry
3211 * in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003212 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003213static int cli_io_handler_show_cafile(struct appctx *appctx)
3214{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003215 struct show_cafile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003216 struct buffer *trash = alloc_trash_chunk();
3217 struct ebmb_node *node;
Christopher Faulet677cb4f2022-06-03 16:25:35 +02003218 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003219
3220 if (trash == NULL)
3221 return 1;
3222
Christopher Faulet5a2154b2022-06-03 10:42:48 +02003223 if (!ctx->old_cafile_entry && cafile_transaction.old_cafile_entry) {
3224 chunk_appendf(trash, "# transaction\n");
3225 chunk_appendf(trash, "*%s", cafile_transaction.old_cafile_entry->path);
3226 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_transaction.new_cafile_entry));
3227 if (applet_putchk(appctx, trash) == -1)
3228 goto yield;
3229 ctx->old_cafile_entry = cafile_transaction.new_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003230 }
3231
3232 /* First time in this io_handler. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003233 if (!ctx->cur_cafile_entry) {
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003234 chunk_appendf(trash, "# filename\n");
3235 node = ebmb_first(&cafile_tree);
3236 } else {
3237 /* We yielded during a previous call. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003238 node = &ctx->cur_cafile_entry->node;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003239 }
3240
3241 while (node) {
3242 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3243 if (cafile_entry->type == CAFILE_CERT) {
3244 chunk_appendf(trash, "%s", cafile_entry->path);
3245
3246 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_entry));
3247 }
3248
3249 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003250 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003251 goto yield;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003252 }
3253
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003254 ctx->cur_cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003255 free_trash_chunk(trash);
3256 return 1;
3257yield:
3258
3259 free_trash_chunk(trash);
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003260 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003261 return 0; /* should come back */
3262}
3263
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003264/* parsing function of 'del ssl ca-file' */
3265static int cli_parse_del_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3266{
3267 struct cafile_entry *cafile_entry;
3268 char *err = NULL;
3269 char *filename;
3270
3271 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3272 return 1;
3273
3274 if (!*args[3])
3275 return cli_err(appctx, "'del ssl ca-file' expects a CA file name\n");
3276
3277 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3278 return cli_err(appctx, "Can't delete the CA file!\nOperations on certificates are currently locked!\n");
3279
3280 filename = args[3];
3281
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003282 if (cafile_transaction.path && strcmp(cafile_transaction.path, filename) == 0) {
3283 memprintf(&err, "ongoing transaction for the CA file '%s'", filename);
3284 goto error;
3285 }
3286
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003287 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3288 if (!cafile_entry) {
3289 memprintf(&err, "CA file '%s' doesn't exist!\n", filename);
3290 goto error;
3291 }
3292
3293 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3294 memprintf(&err, "CA file '%s' in use, can't be deleted!\n", filename);
3295 goto error;
3296 }
3297
3298 /* Remove the cafile_entry from the tree */
3299 ebmb_delete(&cafile_entry->node);
3300 ssl_store_delete_cafile_entry(cafile_entry);
3301
3302 memprintf(&err, "CA file '%s' deleted!\n", filename);
3303
3304 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3305 return cli_dynmsg(appctx, LOG_NOTICE, err);
3306
3307error:
3308 memprintf(&err, "Can't remove the CA file: %s\n", err ? err : "");
3309 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3310 return cli_dynerr(appctx, err);
3311}
3312
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003313/* parsing function of 'new ssl crl-file' */
3314static int cli_parse_new_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3315{
3316 struct cafile_entry *cafile_entry;
3317 char *err = NULL;
3318 char *path;
3319
3320 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3321 return 1;
3322
3323 if (!*args[3])
3324 return cli_err(appctx, "'new ssl crl-file' expects a filename\n");
3325
3326 path = args[3];
3327
3328 /* The operations on the CKCH architecture are locked so we can
3329 * manipulate ckch_store and ckch_inst */
3330 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003331 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 +02003332
3333 cafile_entry = ssl_store_get_cafile_entry(path, 0);
3334 if (cafile_entry) {
3335 memprintf(&err, "CRL file '%s' already exists!\n", path);
3336 goto error;
3337 }
3338
3339 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CRL);
3340 if (!cafile_entry) {
3341 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3342 goto error;
3343 }
3344
3345 /* Add the newly created cafile_entry to the tree so that
3346 * any new ckch instance created from now can use it. */
3347 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
3348 goto error;
3349
3350 memprintf(&err, "New CRL file created '%s'!\n", path);
3351
3352 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3353 return cli_dynmsg(appctx, LOG_NOTICE, err);
3354error:
3355 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3356 return cli_dynerr(appctx, err);
3357}
3358
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003359/* Parsing function of `set ssl crl-file` */
3360static int cli_parse_set_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3361{
Christopher Faulet1f90f332022-06-03 16:34:30 +02003362 struct cafile_entry *old_crlfile_entry = NULL;
3363 struct cafile_entry *new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003364 char *err = NULL;
3365 int errcode = 0;
3366 struct buffer *buf;
3367
3368 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3369 return 1;
3370
3371 if (!*args[3] || !payload)
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003372 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 +02003373
3374 /* The operations on the CKCH architecture are locked so we can
3375 * manipulate ckch_store and ckch_inst */
3376 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3377 return cli_err(appctx, "Can't update the CRL file!\nOperations on certificates are currently locked!\n");
3378
3379 if ((buf = alloc_trash_chunk()) == NULL) {
3380 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3381 errcode |= ERR_ALERT | ERR_FATAL;
3382 goto end;
3383 }
3384
3385 if (!chunk_strcpy(buf, args[3])) {
3386 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3387 errcode |= ERR_ALERT | ERR_FATAL;
3388 goto end;
3389 }
3390
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003391 old_crlfile_entry = NULL;
3392 new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003393
3394 /* if there is an ongoing transaction */
3395 if (crlfile_transaction.path) {
3396 /* if there is an ongoing transaction, check if this is the same file */
3397 if (strcmp(crlfile_transaction.path, buf->area) != 0) {
3398 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, buf->area);
3399 errcode |= ERR_ALERT | ERR_FATAL;
3400 goto end;
3401 }
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003402 old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003403 }
3404 else {
3405 /* lookup for the certificate in the tree */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003406 old_crlfile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003407 }
3408
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003409 if (!old_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003410 memprintf(&err, "%sCan't replace a CRL file which is not referenced by the configuration!\n",
3411 err ? err : "");
3412 errcode |= ERR_ALERT | ERR_FATAL;
3413 goto end;
3414 }
3415
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003416 /* Create a new cafile_entry without adding it to the cafile tree. */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003417 new_crlfile_entry = ssl_store_create_cafile_entry(old_crlfile_entry->path, NULL, CAFILE_CRL);
3418 if (!new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003419 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3420 errcode |= ERR_ALERT | ERR_FATAL;
3421 goto end;
3422 }
3423
3424 /* Fill the new entry with the new CRL. */
William Lallemandd4774d32022-07-29 17:08:02 +02003425 if (ssl_store_load_ca_from_buf(new_crlfile_entry, payload, 0)) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003426 memprintf(&err, "%sInvalid payload\n", err ? err : "");
3427 errcode |= ERR_ALERT | ERR_FATAL;
3428 goto end;
3429 }
3430
3431 /* we succeed, we can save the crl in the transaction */
3432
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003433 /* if there wasn't a transaction, update the old CRL */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003434 if (!crlfile_transaction.old_crlfile_entry) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003435 crlfile_transaction.old_crlfile_entry = old_crlfile_entry;
3436 crlfile_transaction.path = old_crlfile_entry->path;
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003437 err = memprintf(&err, "transaction created for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003438 } else {
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003439 err = memprintf(&err, "transaction updated for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003440 }
3441
3442 /* free the previous CRL file if there was a transaction */
3443 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3444
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003445 crlfile_transaction.new_crlfile_entry = new_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003446
3447 /* creates the SNI ctxs later in the IO handler */
3448
3449end:
3450 free_trash_chunk(buf);
3451
3452 if (errcode & ERR_CODE) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003453 ssl_store_delete_cafile_entry(new_crlfile_entry);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003454 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3455 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
3456 } else {
3457
3458 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3459 return cli_dynmsg(appctx, LOG_NOTICE, err);
3460 }
3461}
3462
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003463/* Parsing function of 'commit ssl crl-file'.
3464 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl ca-file".
3465 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003466static int cli_parse_commit_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3467{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003468 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003469 char *err = NULL;
3470
3471 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3472 return 1;
3473
3474 if (!*args[3])
3475 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
3476
3477 /* The operations on the CKCH architecture are locked so we can
3478 * manipulate ckch_store and ckch_inst */
3479 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3480 return cli_err(appctx, "Can't commit the CRL file!\nOperations on certificates are currently locked!\n");
3481
3482 if (!crlfile_transaction.path) {
3483 memprintf(&err, "No ongoing transaction! !\n");
3484 goto error;
3485 }
3486
3487 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3488 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, args[3]);
3489 goto error;
3490 }
3491 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02003492 ctx->state = CACRL_ST_INIT;
Christopher Fauletf814c4a2022-06-03 11:32:05 +02003493 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003494 ctx->old_entry = crlfile_transaction.old_crlfile_entry;
3495 ctx->new_entry = crlfile_transaction.new_crlfile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003496 ctx->cafile_type = CAFILE_CRL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003497
3498 return 0;
3499
3500error:
3501
3502 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3503 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
3504
3505 return cli_dynerr(appctx, err);
3506}
3507
3508
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003509/* release function of the `commit ssl crl-file' command, free things and unlock the spinlock.
3510 * it uses a commit_cacrlfile_ctx that's the same as for "commit ssl ca-file".
3511 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003512static void cli_release_commit_crlfile(struct appctx *appctx)
3513{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003514 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003515 struct cafile_entry *new_crlfile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003516
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003517 /* Remove the uncommitted cafile_entry from the tree. */
3518 if (new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003519 ebmb_delete(&new_crlfile_entry->node);
3520 ssl_store_delete_cafile_entry(new_crlfile_entry);
3521 }
3522 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003523 ha_free(&ctx->err);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003524}
3525
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003526/* parsing function of 'del ssl crl-file' */
3527static int cli_parse_del_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3528{
3529 struct cafile_entry *cafile_entry;
3530 char *err = NULL;
3531 char *filename;
3532
3533 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3534 return 1;
3535
3536 if (!*args[3])
3537 return cli_err(appctx, "'del ssl crl-file' expects a CRL file name\n");
3538
3539 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3540 return cli_err(appctx, "Can't delete the CRL file!\nOperations on certificates are currently locked!\n");
3541
3542 filename = args[3];
3543
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003544 if (crlfile_transaction.path && strcmp(crlfile_transaction.path, filename) == 0) {
3545 memprintf(&err, "ongoing transaction for the CRL file '%s'", filename);
3546 goto error;
3547 }
3548
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003549 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3550 if (!cafile_entry) {
3551 memprintf(&err, "CRL file '%s' doesn't exist!\n", filename);
3552 goto error;
3553 }
3554 if (cafile_entry->type != CAFILE_CRL) {
3555 memprintf(&err, "'del ssl crl-file' does not work on CA files!\n");
3556 goto error;
3557 }
3558
3559 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3560 memprintf(&err, "CRL file '%s' in use, can't be deleted!\n", filename);
3561 goto error;
3562 }
3563
3564 /* Remove the cafile_entry from the tree */
3565 ebmb_delete(&cafile_entry->node);
3566 ssl_store_delete_cafile_entry(cafile_entry);
3567
3568 memprintf(&err, "CRL file '%s' deleted!\n", filename);
3569
3570 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3571 return cli_dynmsg(appctx, LOG_NOTICE, err);
3572
3573error:
3574 memprintf(&err, "Can't remove the CRL file: %s\n", err ? err : "");
3575 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3576 return cli_dynerr(appctx, err);
3577}
3578
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003579/* parsing function of 'abort ssl crl-file' */
3580static int cli_parse_abort_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3581{
3582 char *err = NULL;
3583
3584 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3585 return 1;
3586
3587 if (!*args[3])
3588 return cli_err(appctx, "'abort ssl crl-file' expects a filename\n");
3589
3590 /* The operations on the CKCH architecture are locked so we can
3591 * manipulate ckch_store and ckch_inst */
3592 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3593 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
3594
3595 if (!crlfile_transaction.path) {
3596 memprintf(&err, "No ongoing transaction!\n");
3597 goto error;
3598 }
3599
3600 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3601 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", crlfile_transaction.path, args[3]);
3602 goto error;
3603 }
3604
3605 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3606 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3607 crlfile_transaction.new_crlfile_entry = NULL;
3608 crlfile_transaction.old_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02003609 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003610
3611 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3612
3613 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3614 return cli_dynmsg(appctx, LOG_NOTICE, err);
3615
3616error:
3617 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3618
3619 return cli_dynerr(appctx, err);
3620}
3621
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003622
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003623/*
3624 * Display a Certificate Resignation List's information.
3625 * The information displayed is inspired by the output of 'openssl crl -in
3626 * crl.pem -text'.
3627 * Returns 0 in case of success.
3628 */
3629static int show_crl_detail(X509_CRL *crl, struct buffer *out)
3630{
3631 BIO *bio = NULL;
3632 struct buffer *tmp = alloc_trash_chunk();
3633 long version;
3634 X509_NAME *issuer;
3635 int write = -1;
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003636#ifndef USE_OPENSSL_WOLFSSL
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003637 STACK_OF(X509_REVOKED) *rev = NULL;
3638 X509_REVOKED *rev_entry = NULL;
3639 int i;
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003640#endif
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003641
3642 if (!tmp)
3643 return -1;
3644
3645 if ((bio = BIO_new(BIO_s_mem())) == NULL)
3646 goto end;
3647
3648 /* Version (as displayed by 'openssl crl') */
3649 version = X509_CRL_get_version(crl);
3650 chunk_appendf(out, "Version %ld\n", version + 1);
3651
3652 /* Signature Algorithm */
3653 chunk_appendf(out, "Signature Algorithm: %s\n", OBJ_nid2ln(X509_CRL_get_signature_nid(crl)));
3654
3655 /* Issuer */
3656 chunk_appendf(out, "Issuer: ");
3657 if ((issuer = X509_CRL_get_issuer(crl)) == NULL)
3658 goto end;
3659 if ((ssl_sock_get_dn_oneline(issuer, tmp)) == -1)
3660 goto end;
3661 *(tmp->area + tmp->data) = '\0';
3662 chunk_appendf(out, "%s\n", tmp->area);
3663
3664 /* Last Update */
3665 chunk_appendf(out, "Last Update: ");
3666 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003667 if (BIO_reset(bio) == -1)
3668 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003669 if (ASN1_TIME_print(bio, X509_CRL_get0_lastUpdate(crl)) == 0)
3670 goto end;
3671 write = BIO_read(bio, tmp->area, tmp->size-1);
3672 tmp->area[write] = '\0';
3673 chunk_appendf(out, "%s\n", tmp->area);
3674
3675
3676 /* Next Update */
3677 chunk_appendf(out, "Next Update: ");
3678 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003679 if (BIO_reset(bio) == -1)
3680 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003681 if (ASN1_TIME_print(bio, X509_CRL_get0_nextUpdate(crl)) == 0)
3682 goto end;
3683 write = BIO_read(bio, tmp->area, tmp->size-1);
3684 tmp->area[write] = '\0';
3685 chunk_appendf(out, "%s\n", tmp->area);
3686
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003687#ifndef USE_OPENSSL_WOLFSSL
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003688 /* Revoked Certificates */
3689 rev = X509_CRL_get_REVOKED(crl);
3690 if (sk_X509_REVOKED_num(rev) > 0)
3691 chunk_appendf(out, "Revoked Certificates:\n");
3692 else
3693 chunk_appendf(out, "No Revoked Certificates.\n");
3694
3695 for (i = 0; i < sk_X509_REVOKED_num(rev); i++) {
3696 rev_entry = sk_X509_REVOKED_value(rev, i);
3697
3698 /* Serial Number and Revocation Date */
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003699 if (BIO_reset(bio) == -1)
3700 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003701 BIO_printf(bio , " Serial Number: ");
Remi Tricot-Le Breton18c7d832021-05-17 18:38:34 +02003702 i2a_ASN1_INTEGER(bio, (ASN1_INTEGER*)X509_REVOKED_get0_serialNumber(rev_entry));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003703 BIO_printf(bio, "\n Revocation Date: ");
Remi Tricot-Le Bretona6b27842021-05-18 10:06:00 +02003704 if (ASN1_TIME_print(bio, X509_REVOKED_get0_revocationDate(rev_entry)) == 0)
3705 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003706 BIO_printf(bio, "\n");
3707
3708 write = BIO_read(bio, tmp->area, tmp->size-1);
3709 tmp->area[write] = '\0';
3710 chunk_appendf(out, "%s", tmp->area);
3711 }
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003712#endif /* not USE_OPENSSL_WOLFSSL */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003713
3714end:
3715 free_trash_chunk(tmp);
3716 if (bio)
3717 BIO_free(bio);
3718
3719 return 0;
3720}
3721
Willy Tarreau821c3b02022-05-04 15:47:39 +02003722/* IO handler of details "show ssl crl-file <filename[:index]>".
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003723 * It uses show_crlfile_ctx and the global
3724 * crlfile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003725 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003726static int cli_io_handler_show_crlfile_detail(struct appctx *appctx)
3727{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003728 struct show_crlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003729 struct cafile_entry *cafile_entry = ctx->cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003730 struct buffer *out = alloc_trash_chunk();
3731 int i;
3732 X509_CRL *crl;
3733 STACK_OF(X509_OBJECT) *objs;
3734 int retval = 0;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003735 int index = ctx->index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003736
3737 if (!out)
3738 goto end_no_putchk;
3739
3740 chunk_appendf(out, "Filename: ");
3741 if (cafile_entry == crlfile_transaction.new_crlfile_entry)
3742 chunk_appendf(out, "*");
3743 chunk_appendf(out, "%s\n", cafile_entry->path);
3744
3745 chunk_appendf(out, "Status: ");
3746 if (!cafile_entry->ca_store)
3747 chunk_appendf(out, "Empty\n");
3748 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3749 chunk_appendf(out, "Unused\n");
3750 else
3751 chunk_appendf(out, "Used\n");
3752
3753 if (!cafile_entry->ca_store)
3754 goto end;
3755
3756 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3757 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
3758 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
3759 if (!crl)
3760 continue;
3761
3762 /* CRL indexes start at 1 on the CLI output. */
3763 if (index && index-1 != i)
3764 continue;
3765
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003766 chunk_appendf(out, " \nCertificate Revocation List #%d:\n", i+1);
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003767 retval = show_crl_detail(crl, out);
3768 if (retval < 0)
3769 goto end_no_putchk;
3770 else if (retval || index)
3771 goto end;
3772 }
3773
3774end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003775 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003776 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003777
3778end_no_putchk:
3779 free_trash_chunk(out);
3780 return 1;
3781yield:
3782 free_trash_chunk(out);
3783 return 0; /* should come back */
3784}
3785
Willy Tarreau821c3b02022-05-04 15:47:39 +02003786/* parsing function for 'show ssl crl-file [crlfile[:index]]'.
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003787 * It sets the context to a show_crlfile_ctx, and the global
Willy Tarreau821c3b02022-05-04 15:47:39 +02003788 * cafile_transaction.new_crlfile_entry under the ckch_lock.
3789 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003790static int cli_parse_show_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3791{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003792 struct show_crlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003793 struct cafile_entry *cafile_entry;
3794 long index = 0;
3795 char *colons;
3796 char *err = NULL;
3797
3798 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3799 return cli_err(appctx, "Can't allocate memory!\n");
3800
3801 /* The operations on the CKCH architecture are locked so we can
3802 * manipulate ckch_store and ckch_inst */
3803 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3804 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3805
3806 /* check if there is a certificate to lookup */
3807 if (*args[3]) {
3808
3809 /* Look for an optional index after the CRL file name */
3810 colons = strchr(args[3], ':');
3811 if (colons) {
3812 char *endptr;
3813
3814 index = strtol(colons + 1, &endptr, 10);
3815 /* Indexes start at 1 */
3816 if (colons + 1 == endptr || *endptr != '\0' || index <= 0) {
3817 memprintf(&err, "wrong CRL index after colons in '%s'!", args[3]);
3818 goto error;
3819 }
3820 *colons = '\0';
3821 }
3822
3823 if (*args[3] == '*') {
3824 if (!crlfile_transaction.new_crlfile_entry)
3825 goto error;
3826
3827 cafile_entry = crlfile_transaction.new_crlfile_entry;
3828
3829 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3830 goto error;
3831
3832 } else {
3833 /* Get the "original" cafile_entry and not the
3834 * uncommitted one if it exists. */
3835 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CRL)
3836 goto error;
3837 }
3838
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003839 ctx->cafile_entry = cafile_entry;
3840 ctx->index = index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003841 /* use the IO handler that shows details */
3842 appctx->io_handler = cli_io_handler_show_crlfile_detail;
3843 }
3844
3845 return 0;
3846
3847error:
3848 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3849 if (err)
3850 return cli_dynerr(appctx, err);
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003851 return cli_err(appctx, "Can't display the CRL file : Not found!\n");
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003852}
3853
3854/* IO handler of "show ssl crl-file". The command taking a specific CRL file name
3855 * is managed in cli_io_handler_show_crlfile_detail. */
3856static int cli_io_handler_show_crlfile(struct appctx *appctx)
3857{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003858 struct show_crlfile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003859 struct buffer *trash = alloc_trash_chunk();
3860 struct ebmb_node *node;
Christopher Faulet88041b32022-06-03 16:26:56 +02003861 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003862
3863 if (trash == NULL)
3864 return 1;
3865
Christopher Faulet9a99e542022-06-03 10:32:18 +02003866 if (!ctx->old_crlfile_entry && crlfile_transaction.old_crlfile_entry) {
3867 chunk_appendf(trash, "# transaction\n");
3868 chunk_appendf(trash, "*%s\n", crlfile_transaction.old_crlfile_entry->path);
3869 if (applet_putchk(appctx, trash) == -1)
3870 goto yield;
3871 ctx->old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003872 }
3873
3874 /* First time in this io_handler. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003875 if (!ctx->cafile_entry) {
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003876 chunk_appendf(trash, "# filename\n");
3877 node = ebmb_first(&cafile_tree);
3878 } else {
3879 /* We yielded during a previous call. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003880 node = &ctx->cafile_entry->node;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003881 }
3882
3883 while (node) {
3884 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3885 if (cafile_entry->type == CAFILE_CRL) {
3886 chunk_appendf(trash, "%s\n", cafile_entry->path);
3887 }
3888
3889 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003890 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003891 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003892 }
3893
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003894 ctx->cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003895 free_trash_chunk(trash);
3896 return 1;
3897yield:
3898
3899 free_trash_chunk(trash);
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003900 ctx->cafile_entry = cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003901 return 0; /* should come back */
3902}
3903
3904
3905/* release function of the 'show ssl crl-file' command */
3906static void cli_release_show_crlfile(struct appctx *appctx)
3907{
3908 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3909}
3910
3911
William Lallemandee8530c2020-06-23 18:19:42 +02003912void ckch_deinit()
3913{
3914 struct eb_node *node, *next;
3915 struct ckch_store *store;
William Lallemandb0c48272022-04-26 15:44:53 +02003916 struct ebmb_node *canode;
William Lallemandee8530c2020-06-23 18:19:42 +02003917
William Lallemandb0c48272022-04-26 15:44:53 +02003918 /* deinit the ckch stores */
William Lallemandee8530c2020-06-23 18:19:42 +02003919 node = eb_first(&ckchs_tree);
3920 while (node) {
3921 next = eb_next(node);
3922 store = ebmb_entry(node, struct ckch_store, node);
3923 ckch_store_free(store);
3924 node = next;
3925 }
William Lallemandb0c48272022-04-26 15:44:53 +02003926
3927 /* deinit the ca-file store */
3928 canode = ebmb_first(&cafile_tree);
3929 while (canode) {
3930 struct cafile_entry *entry = NULL;
3931
3932 entry = ebmb_entry(canode, struct cafile_entry, node);
3933 canode = ebmb_next(canode);
William Lallemand946580e2022-08-29 18:36:18 +02003934 ebmb_delete(&entry->node);
William Lallemandb0c48272022-04-26 15:44:53 +02003935 ssl_store_delete_cafile_entry(entry);
3936 }
William Lallemandee8530c2020-06-23 18:19:42 +02003937}
William Lallemandda8584c2020-05-14 10:14:37 +02003938
3939/* register cli keywords */
3940static struct cli_kw_list cli_kws = {{ },{
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003941 { { "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 },
3942 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
3943 { { "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 },
3944 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
3945 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
3946 { { "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 },
3947
Amaury Denoyelleb11ad9e2021-05-21 11:01:10 +02003948 { { "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 +02003949 { { "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 +01003950 { { "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 +02003951 { { "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 +01003952 { { "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 +01003953 { { "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 +01003954 { { "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 +02003955
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003956 { { "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 +02003957 { { "set", "ssl", "crl-file", NULL }, "set ssl crl-file <crlfile> <payload> : replace a CRL file", cli_parse_set_crlfile, NULL, NULL },
3958 { { "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 +02003959 { { "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 +02003960 { { "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 +02003961 { { "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 +02003962 { { NULL }, NULL, NULL, NULL }
3963}};
3964
3965INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3966