blob: 61ffbc08f361ba35aa2301dabb6f8e7b1beb3d14 [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);
631 goto end;
632 }
633 }
634
635 ret = ERR_get_error();
636 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) {
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100754 HASSL_DH_up_ref(src->dh);
William Lallemand03c331c2020-05-13 10:10:01 +0200755 dst->dh = src->dh;
756 }
757
758 if (src->sctl) {
759 struct buffer *sctl;
760
761 sctl = calloc(1, sizeof(*sctl));
762 if (!chunk_dup(sctl, src->sctl)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100763 ha_free(&sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200764 goto error;
765 }
766 dst->sctl = sctl;
767 }
768
769 if (src->ocsp_response) {
770 struct buffer *ocsp_response;
771
772 ocsp_response = calloc(1, sizeof(*ocsp_response));
773 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100774 ha_free(&ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200775 goto error;
776 }
777 dst->ocsp_response = ocsp_response;
778 }
779
780 if (src->ocsp_issuer) {
781 X509_up_ref(src->ocsp_issuer);
782 dst->ocsp_issuer = src->ocsp_issuer;
783 }
784
785 return dst;
786
787error:
788
789 /* free everything */
790 ssl_sock_free_cert_key_and_chain_contents(dst);
791
792 return NULL;
793}
794
795/*
796 * return 0 on success or != 0 on failure
797 */
798int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
799{
800 int ret = 1;
801 BIO *in = NULL;
802 X509 *issuer;
803
804 if (buf) {
805 /* reading from a buffer */
806 in = BIO_new_mem_buf(buf, -1);
807 if (in == NULL) {
808 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
809 goto end;
810 }
811
812 } else {
813 /* reading from a file */
814 in = BIO_new(BIO_s_file());
815 if (in == NULL)
816 goto end;
817
818 if (BIO_read_filename(in, path) <= 0)
819 goto end;
820 }
821
822 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
823 if (!issuer) {
824 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
825 err && *err ? *err : "", path);
826 goto end;
827 }
828 /* no error, fill ckch with new context, old context must be free */
829 if (ckch->ocsp_issuer)
830 X509_free(ckch->ocsp_issuer);
831 ckch->ocsp_issuer = issuer;
832 ret = 0;
833
834end:
835
836 ERR_clear_error();
837 if (in)
838 BIO_free(in);
839
840 return ret;
841}
842
843/******************** ckch_store functions ***********************************
844 * The ckch_store is a structure used to cache and index the SSL files used in
845 * configuration
846 */
847
848/*
849 * Free a ckch_store, its ckch, its instances and remove it from the ebtree
850 */
851void ckch_store_free(struct ckch_store *store)
852{
853 struct ckch_inst *inst, *inst_s;
854
855 if (!store)
856 return;
857
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200858 ssl_sock_free_cert_key_and_chain_contents(store->ckch);
William Lallemand03c331c2020-05-13 10:10:01 +0200859
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100860 ha_free(&store->ckch);
William Lallemand03c331c2020-05-13 10:10:01 +0200861
862 list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) {
863 ckch_inst_free(inst);
864 }
865 ebmb_delete(&store->node);
866 free(store);
867}
868
869/*
870 * create and initialize a ckch_store
871 * <path> is the key name
872 * <nmemb> is the number of store->ckch objects to allocate
873 *
874 * Return a ckch_store or NULL upon failure.
875 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200876struct ckch_store *ckch_store_new(const char *filename)
William Lallemand03c331c2020-05-13 10:10:01 +0200877{
878 struct ckch_store *store;
879 int pathlen;
880
881 pathlen = strlen(filename);
882 store = calloc(1, sizeof(*store) + pathlen + 1);
883 if (!store)
884 return NULL;
885
William Lallemand03c331c2020-05-13 10:10:01 +0200886 memcpy(store->path, filename, pathlen + 1);
887
888 LIST_INIT(&store->ckch_inst);
889 LIST_INIT(&store->crtlist_entry);
890
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200891 store->ckch = calloc(1, sizeof(*store->ckch));
William Lallemand03c331c2020-05-13 10:10:01 +0200892 if (!store->ckch)
893 goto error;
894
895 return store;
896error:
897 ckch_store_free(store);
898 return NULL;
899}
900
901/* allocate and duplicate a ckch_store
902 * Return a new ckch_store or NULL */
903struct ckch_store *ckchs_dup(const struct ckch_store *src)
904{
905 struct ckch_store *dst;
906
William Lallemand6c096142021-02-23 14:45:45 +0100907 if (!src)
908 return NULL;
909
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200910 dst = ckch_store_new(src->path);
Eric Salama6ac61e32021-02-23 16:50:57 +0100911 if (!dst)
912 return NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200913
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200914 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
915 goto error;
William Lallemand03c331c2020-05-13 10:10:01 +0200916
917 return dst;
918
919error:
920 ckch_store_free(dst);
921
922 return NULL;
923}
924
925/*
926 * lookup a path into the ckchs tree.
927 */
928struct ckch_store *ckchs_lookup(char *path)
929{
930 struct ebmb_node *eb;
931
932 eb = ebst_lookup(&ckchs_tree, path);
933 if (!eb)
934 return NULL;
935
936 return ebmb_entry(eb, struct ckch_store, node);
937}
938
939/*
940 * This function allocate a ckch_store and populate it with certificates from files.
941 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200942struct ckch_store *ckchs_load_cert_file(char *path, char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200943{
944 struct ckch_store *ckchs;
945
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200946 ckchs = ckch_store_new(path);
William Lallemand03c331c2020-05-13 10:10:01 +0200947 if (!ckchs) {
948 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
949 goto end;
950 }
William Lallemand03c331c2020-05-13 10:10:01 +0200951
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200952 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
953 goto end;
William Lallemand03c331c2020-05-13 10:10:01 +0200954
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200955 /* insert into the ckchs tree */
956 memcpy(ckchs->path, path, strlen(path) + 1);
957 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand03c331c2020-05-13 10:10:01 +0200958 return ckchs;
959
960end:
961 ckch_store_free(ckchs);
962
963 return NULL;
964}
965
William Lallemandfa1d8b42020-05-13 15:46:10 +0200966
967/******************** ckch_inst functions ******************************/
968
969/* unlink a ckch_inst, free all SNIs, free the ckch_inst */
970/* The caller must use the lock of the bind_conf if used with inserted SNIs */
971void ckch_inst_free(struct ckch_inst *inst)
972{
973 struct sni_ctx *sni, *sni_s;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100974 struct ckch_inst_link_ref *link_ref, *link_ref_s;
William Lallemandfa1d8b42020-05-13 15:46:10 +0200975
976 if (inst == NULL)
977 return;
978
979 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
980 SSL_CTX_free(sni->ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +0200981 LIST_DELETE(&sni->by_ckch_inst);
William Lallemandfa1d8b42020-05-13 15:46:10 +0200982 ebmb_delete(&sni->name);
983 free(sni);
984 }
Remi Tricot-Le Bretonf3eedfe2021-01-25 17:19:44 +0100985 SSL_CTX_free(inst->ctx);
986 inst->ctx = NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +0200987 LIST_DELETE(&inst->by_ckchs);
988 LIST_DELETE(&inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100989
William Lallemande0fa91f2022-08-31 14:26:49 +0200990 /* Free the cafile_link_refs list */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100991 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
William Lallemande0fa91f2022-08-31 14:26:49 +0200992 if (link_ref->link && LIST_INLIST(&link_ref->link->list)) {
993 /* Try to detach and free the ckch_inst_link only if it
994 * was attached, this way it can be used to loop from
995 * the caller */
996 LIST_DEL_INIT(&link_ref->link->list);
997 ha_free(&link_ref->link);
998 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100999 LIST_DELETE(&link_ref->list);
1000 free(link_ref);
1001 }
1002
William Lallemandfa1d8b42020-05-13 15:46:10 +02001003 free(inst);
1004}
1005
1006/* Alloc and init a ckch_inst */
1007struct ckch_inst *ckch_inst_new()
1008{
1009 struct ckch_inst *ckch_inst;
1010
1011 ckch_inst = calloc(1, sizeof *ckch_inst);
1012 if (!ckch_inst)
1013 return NULL;
1014
1015 LIST_INIT(&ckch_inst->sni_ctx);
1016 LIST_INIT(&ckch_inst->by_ckchs);
1017 LIST_INIT(&ckch_inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001018 LIST_INIT(&ckch_inst->cafile_link_refs);
William Lallemandfa1d8b42020-05-13 15:46:10 +02001019
1020 return ckch_inst;
1021}
1022
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001023
1024/******************** ssl_store functions ******************************/
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001025struct eb_root cafile_tree = EB_ROOT;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001026
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001027/*
1028 * Returns the cafile_entry found in the cafile_tree indexed by the path 'path'.
1029 * If 'oldest_entry' is 1, returns the "original" cafile_entry (since
1030 * during a set cafile/commit cafile cycle there might be two entries for any
1031 * given path, the original one and the new one set via the CLI but not
1032 * committed yet).
1033 */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001034struct cafile_entry *ssl_store_get_cafile_entry(char *path, int oldest_entry)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001035{
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001036 struct cafile_entry *ca_e = NULL;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001037 struct ebmb_node *eb;
1038
1039 eb = ebst_lookup(&cafile_tree, path);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001040 while (eb) {
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001041 ca_e = ebmb_entry(eb, struct cafile_entry, node);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001042 /* The ebst_lookup in a tree that has duplicates returns the
1043 * oldest entry first. If we want the latest entry, we need to
1044 * iterate over all the duplicates until we find the last one
1045 * (in our case there should never be more than two entries for
1046 * any given path). */
1047 if (oldest_entry)
1048 return ca_e;
1049 eb = ebmb_next_dup(eb);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001050 }
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001051 return ca_e;
1052}
1053
Remi Tricot-Le Breton38c999b2021-02-23 16:28:43 +01001054int ssl_store_add_uncommitted_cafile_entry(struct cafile_entry *entry)
1055{
1056 return (ebst_insert(&cafile_tree, &entry->node) != &entry->node);
1057}
1058
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001059X509_STORE* ssl_store_get0_locations_file(char *path)
1060{
1061 struct cafile_entry *ca_e = ssl_store_get_cafile_entry(path, 0);
1062
1063 if (ca_e)
1064 return ca_e->ca_store;
1065
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001066 return NULL;
1067}
1068
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001069/* Create a cafile_entry object, without adding it to the cafile_tree. */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001070struct 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 +01001071{
1072 struct cafile_entry *ca_e;
1073 int pathlen;
1074
1075 pathlen = strlen(path);
1076
1077 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
1078 if (ca_e) {
1079 memcpy(ca_e->path, path, pathlen + 1);
1080 ca_e->ca_store = store;
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001081 ca_e->type = type;
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001082 LIST_INIT(&ca_e->ckch_inst_link);
1083 }
1084 return ca_e;
1085}
1086
William Lallemand62c0b992022-07-29 17:50:58 +02001087
1088/* Duplicate a cafile_entry
1089 * Allocate the X509_STORE and copy the X509 and CRL inside.
1090 *
1091 * Return the newly allocated cafile_entry or NULL.
1092 *
1093 */
1094struct cafile_entry *ssl_store_dup_cafile_entry(struct cafile_entry *src)
1095{
1096 struct cafile_entry *dst = NULL;
1097 X509_STORE *store = NULL;
1098 STACK_OF(X509_OBJECT) *objs;
1099 int i;
1100
1101 if (!src)
1102 return NULL;
1103
1104 if (src->ca_store) {
1105 /* if there was a store in the src, copy it */
1106 store = X509_STORE_new();
1107 if (!store)
1108 goto err;
1109
1110 objs = X509_STORE_get0_objects(src->ca_store);
1111 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
1112 X509 *cert;
1113 X509_CRL *crl;
1114
1115 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
1116 if (cert) {
1117 if (X509_STORE_add_cert(store, cert) == 0) {
1118 /* only exits on error if the error is not about duplicate certificates */
1119 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1120 goto err;
1121 }
1122 }
1123
1124 }
1125 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
1126 if (crl) {
1127 if (X509_STORE_add_crl(store, crl) == 0) {
1128 /* only exits on error if the error is not about duplicate certificates */
1129 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1130 goto err;
1131 }
1132 }
1133
1134 }
1135 }
1136 }
1137 dst = ssl_store_create_cafile_entry(src->path, store, src->type);
1138
1139 return dst;
1140
1141err:
1142 X509_STORE_free(store);
1143 ha_free(&dst);
1144
1145 return NULL;
1146}
1147
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001148/* Delete a cafile_entry. The caller is responsible from removing this entry
1149 * from the cafile_tree first if is was previously added into it. */
1150void ssl_store_delete_cafile_entry(struct cafile_entry *ca_e)
1151{
1152 struct ckch_inst_link *link, *link_s;
1153 if (!ca_e)
1154 return;
1155
1156 X509_STORE_free(ca_e->ca_store);
1157
1158 list_for_each_entry_safe(link, link_s, &ca_e->ckch_inst_link, list) {
1159 struct ckch_inst *inst = link->ckch_inst;
1160 struct ckch_inst_link_ref *link_ref, *link_ref_s;
1161 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
1162 if (link_ref->link == link) {
1163 LIST_DELETE(&link_ref->list);
1164 free(link_ref);
1165 break;
1166 }
1167 }
1168 LIST_DELETE(&link->list);
1169 free(link);
1170 }
1171
1172 free(ca_e);
1173}
1174
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001175/*
William Lallemandd4774d32022-07-29 17:08:02 +02001176 * Fill a cafile_entry <ca_e> X509_STORE ca_e->store out of a buffer <cert_buf>
1177 * instead of out of a file. The <append> field should be set to 1 if you want
1178 * to keep the existing X509_STORE and append data to it.
1179 *
1180 * This function is used when the "set ssl ca-file" cli command is used.
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001181 * It can parse CERTIFICATE sections as well as CRL ones.
1182 * Returns 0 in case of success, 1 otherwise.
William Lallemandd4774d32022-07-29 17:08:02 +02001183 *
1184 * /!\ Warning: If there was an error the X509_STORE could have been modified so it's
1185 * better to not use it after a return 1.
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001186 */
William Lallemandd4774d32022-07-29 17:08:02 +02001187int 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 +01001188{
William Lallemandd4774d32022-07-29 17:08:02 +02001189 BIO *bio = NULL;
1190 STACK_OF(X509_INFO) *infos;
1191 X509_INFO *info;
1192 int i;
1193 int retval = 1;
1194 int retcert = 0;
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001195
1196 if (!ca_e)
1197 return 1;
1198
William Lallemandd4774d32022-07-29 17:08:02 +02001199 if (!append) {
1200 X509_STORE_free(ca_e->ca_store);
1201 ca_e->ca_store = NULL;
1202 }
1203
1204 if (!ca_e->ca_store)
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001205 ca_e->ca_store = X509_STORE_new();
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001206
William Lallemandd4774d32022-07-29 17:08:02 +02001207 if (!ca_e->ca_store)
1208 goto end;
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001209
William Lallemandd4774d32022-07-29 17:08:02 +02001210 bio = BIO_new_mem_buf(cert_buf, strlen(cert_buf));
1211 if (!bio)
1212 goto end;
1213
1214 infos = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
1215 if (!infos)
1216 goto end;
1217
1218 for (i = 0; i < sk_X509_INFO_num(infos) && !retcert; i++) {
1219 info = sk_X509_INFO_value(infos, i);
1220
1221 /* X509_STORE_add_cert and X509_STORE_add_crl return 1 on success */
1222 if (info->x509)
1223 retcert = !X509_STORE_add_cert(ca_e->ca_store, info->x509);
1224 if (!retcert && info->crl)
1225 retcert = !X509_STORE_add_crl(ca_e->ca_store, info->crl);
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001226 }
1227
William Lallemandd4774d32022-07-29 17:08:02 +02001228 /* return an error if we didn't compute all the X509_INFO or if there was none
1229 * set to 0 if everything was right */
1230 if (!(retcert || (i != sk_X509_INFO_num(infos)) || (sk_X509_INFO_num(infos) == 0)))
1231 retval = 0;
1232
1233 /* Cleanup */
1234 sk_X509_INFO_pop_free(infos, X509_INFO_free);
1235
1236end:
1237 BIO_free(bio);
1238
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001239 return retval;
1240}
1241
William Lallemand0f17ab22022-07-19 18:03:16 +02001242/*
1243 * Try to load a ca-file from disk into the ca-file cache.
1244 *
1245 * Return 0 upon error
1246 */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001247int ssl_store_load_locations_file(char *path, int create_if_none, enum cafile_type type)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001248{
1249 X509_STORE *store = ssl_store_get0_locations_file(path);
1250
1251 /* If this function is called by the CLI, we should not call the
1252 * X509_STORE_load_locations function because it performs forbidden disk
1253 * accesses. */
1254 if (!store && create_if_none) {
William Lallemand87fd9942022-04-01 20:12:03 +02001255 STACK_OF(X509_OBJECT) *objs;
1256 int cert_count = 0;
1257 struct stat buf;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001258 struct cafile_entry *ca_e;
William Lallemandc6b17632022-04-01 23:39:37 +02001259 const char *file = NULL;
1260 const char *dir = NULL;
William Lallemand0f17ab22022-07-19 18:03:16 +02001261 unsigned long e;
William Lallemand87fd9942022-04-01 20:12:03 +02001262
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001263 store = X509_STORE_new();
William Lallemand0f17ab22022-07-19 18:03:16 +02001264 if (!store) {
1265 ha_alert("Cannot allocate memory!");
1266 goto err;
1267 }
William Lallemand87fd9942022-04-01 20:12:03 +02001268
William Lallemandc6b17632022-04-01 23:39:37 +02001269 if (strcmp(path, "@system-ca") == 0) {
1270 dir = X509_get_default_cert_dir();
William Lallemand0f17ab22022-07-19 18:03:16 +02001271 if (!dir) {
1272 ha_alert("Couldn't get the system CA directory from X509_get_default_cert_dir().");
1273 goto err;
1274 }
William Lallemand87fd9942022-04-01 20:12:03 +02001275
William Lallemandc6b17632022-04-01 23:39:37 +02001276 } else {
1277
William Lallemand0f17ab22022-07-19 18:03:16 +02001278 if (stat(path, &buf) == -1) {
1279 ha_alert("Couldn't open the ca-file '%s' (%s).", path, strerror(errno));
William Lallemandc6b17632022-04-01 23:39:37 +02001280 goto err;
William Lallemand0f17ab22022-07-19 18:03:16 +02001281 }
William Lallemandc6b17632022-04-01 23:39:37 +02001282
1283 if (S_ISDIR(buf.st_mode))
1284 dir = path;
1285 else
1286 file = path;
1287 }
William Lallemand87fd9942022-04-01 20:12:03 +02001288
1289 if (file) {
1290 if (!X509_STORE_load_locations(store, file, NULL)) {
William Lallemand0f17ab22022-07-19 18:03:16 +02001291 e = ERR_get_error();
1292 ha_alert("Couldn't open the ca-file '%s' (%s).", path, ERR_reason_error_string(e));
William Lallemand87fd9942022-04-01 20:12:03 +02001293 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001294 }
William Lallemand80296b42022-04-05 10:19:30 +02001295 } else if (dir) {
William Lallemand87fd9942022-04-01 20:12:03 +02001296 int n, i;
1297 struct dirent **de_list;
1298
1299 n = scandir(dir, &de_list, 0, alphasort);
1300 if (n < 0)
1301 goto err;
1302
1303 for (i= 0; i < n; i++) {
1304 char *end;
1305 struct dirent *de = de_list[i];
1306 BIO *in = NULL;
1307 X509 *ca = NULL;;
1308
William Lallemand43482322022-07-18 18:42:52 +02001309 ERR_clear_error();
1310
William Lallemand87fd9942022-04-01 20:12:03 +02001311 /* we try to load the files that would have
1312 * been loaded in an hashed directory loaded by
1313 * X509_LOOKUP_hash_dir, so according to "man 1
1314 * c_rehash", we should load ".pem", ".crt",
William Lallemande4b93eb2022-05-09 09:29:00 +02001315 * ".cer", or ".crl". Files starting with a dot
1316 * are ignored.
William Lallemand87fd9942022-04-01 20:12:03 +02001317 */
1318 end = strrchr(de->d_name, '.');
William Lallemande4b93eb2022-05-09 09:29:00 +02001319 if (!end || de->d_name[0] == '.' ||
1320 (strcmp(end, ".pem") != 0 &&
1321 strcmp(end, ".crt") != 0 &&
1322 strcmp(end, ".cer") != 0 &&
1323 strcmp(end, ".crl") != 0)) {
William Lallemand87fd9942022-04-01 20:12:03 +02001324 free(de);
1325 continue;
1326 }
1327 in = BIO_new(BIO_s_file());
1328 if (in == NULL)
1329 goto scandir_err;
1330
William Lallemandc6b17632022-04-01 23:39:37 +02001331 chunk_printf(&trash, "%s/%s", dir, de->d_name);
William Lallemand87fd9942022-04-01 20:12:03 +02001332
1333 if (BIO_read_filename(in, trash.area) == 0)
1334 goto scandir_err;
1335
1336 if (PEM_read_bio_X509_AUX(in, &ca, NULL, NULL) == NULL)
1337 goto scandir_err;
1338
William Lallemand43482322022-07-18 18:42:52 +02001339 if (X509_STORE_add_cert(store, ca) == 0) {
1340 /* only exits on error if the error is not about duplicate certificates */
1341 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1342 goto scandir_err;
1343 }
1344 }
William Lallemand87fd9942022-04-01 20:12:03 +02001345
William Lallemand4cfbf3c2022-04-26 15:57:33 +02001346 X509_free(ca);
William Lallemand87fd9942022-04-01 20:12:03 +02001347 BIO_free(in);
1348 free(de);
1349 continue;
1350
1351scandir_err:
William Lallemand0f17ab22022-07-19 18:03:16 +02001352 e = ERR_get_error();
William Lallemand4cfbf3c2022-04-26 15:57:33 +02001353 X509_free(ca);
William Lallemand87fd9942022-04-01 20:12:03 +02001354 BIO_free(in);
1355 free(de);
William Lallemand0f17ab22022-07-19 18:03:16 +02001356 /* warn if it can load one of the files, but don't abort */
1357 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 +02001358
1359 }
1360 free(de_list);
William Lallemand80296b42022-04-05 10:19:30 +02001361 } else {
1362 ha_alert("ca-file: couldn't load '%s'\n", path);
1363 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001364 }
William Lallemand87fd9942022-04-01 20:12:03 +02001365
1366 objs = X509_STORE_get0_objects(store);
1367 cert_count = sk_X509_OBJECT_num(objs);
William Lallemand0f17ab22022-07-19 18:03:16 +02001368 if (cert_count == 0) {
William Lallemand87fd9942022-04-01 20:12:03 +02001369 ha_warning("ca-file: 0 CA were loaded from '%s'\n", path);
William Lallemand0f17ab22022-07-19 18:03:16 +02001370 }
William Lallemand87fd9942022-04-01 20:12:03 +02001371 ca_e = ssl_store_create_cafile_entry(path, store, type);
William Lallemand0f17ab22022-07-19 18:03:16 +02001372 if (!ca_e) {
1373 ha_alert("Cannot allocate memory!\n");
William Lallemand87fd9942022-04-01 20:12:03 +02001374 goto err;
William Lallemand0f17ab22022-07-19 18:03:16 +02001375 }
William Lallemand87fd9942022-04-01 20:12:03 +02001376 ebst_insert(&cafile_tree, &ca_e->node);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001377 }
1378 return (store != NULL);
William Lallemand87fd9942022-04-01 20:12:03 +02001379
1380err:
1381 X509_STORE_free(store);
1382 store = NULL;
1383 return 0;
1384
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001385}
1386
1387
William Lallemandda8584c2020-05-14 10:14:37 +02001388/*************************** CLI commands ***********************/
1389
1390/* Type of SSL payloads that can be updated over the CLI */
1391
William Lallemandff8bf982022-03-29 10:44:23 +02001392struct cert_exts cert_exts[] = {
1393 { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand26654e72022-03-30 12:01:32 +02001394 { "crt", CERT_TYPE_CRT, &ssl_sock_load_pem_into_ckch },
William Lallemandff8bf982022-03-29 10:44:23 +02001395 { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemandda8584c2020-05-14 10:14:37 +02001396#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemandff8bf982022-03-29 10:44:23 +02001397 { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001398#endif
Ilya Shipitsinc47d6762021-02-13 11:45:33 +05001399#ifdef HAVE_SSL_SCTL
William Lallemandff8bf982022-03-29 10:44:23 +02001400 { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001401#endif
William Lallemandff8bf982022-03-29 10:44:23 +02001402 { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
1403 { NULL, CERT_TYPE_MAX, NULL },
William Lallemandda8584c2020-05-14 10:14:37 +02001404};
1405
1406
1407/* release function of the `show ssl cert' command */
1408static void cli_release_show_cert(struct appctx *appctx)
1409{
1410 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1411}
1412
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001413/* IO handler of "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001414 * It makes use of a show_cert_ctx context, and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001415 */
William Lallemandda8584c2020-05-14 10:14:37 +02001416static int cli_io_handler_show_cert(struct appctx *appctx)
1417{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001418 struct show_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001419 struct buffer *trash = alloc_trash_chunk();
1420 struct ebmb_node *node;
Christopher Fauletd1d2e4d2022-06-03 16:24:02 +02001421 struct ckch_store *ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001422
1423 if (trash == NULL)
1424 return 1;
1425
Christopher Faulet3e94f5d2022-06-03 10:46:40 +02001426 if (!ctx->old_ckchs && ckchs_transaction.old_ckchs) {
1427 ckchs = ckchs_transaction.old_ckchs;
1428 chunk_appendf(trash, "# transaction\n");
1429 chunk_appendf(trash, "*%s\n", ckchs->path);
1430 if (applet_putchk(appctx, trash) == -1)
1431 goto yield;
1432 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001433 }
1434
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001435 if (!ctx->cur_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02001436 chunk_appendf(trash, "# filename\n");
1437 node = ebmb_first(&ckchs_tree);
1438 } else {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001439 node = &ctx->cur_ckchs->node;
William Lallemandda8584c2020-05-14 10:14:37 +02001440 }
1441 while (node) {
1442 ckchs = ebmb_entry(node, struct ckch_store, node);
William Lallemand5685ccf2020-09-16 16:12:25 +02001443 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001444
1445 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001446 if (applet_putchk(appctx, trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001447 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001448 }
1449
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001450 ctx->cur_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001451 free_trash_chunk(trash);
1452 return 1;
1453yield:
1454
1455 free_trash_chunk(trash);
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001456 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001457 return 0; /* should come back */
1458}
1459
1460/*
1461 * Extract and format the DNS SAN extensions and copy result into a chuink
1462 * Return 0;
1463 */
1464#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1465static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
1466{
1467 int i;
1468 char *str;
1469 STACK_OF(GENERAL_NAME) *names = NULL;
1470
1471 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1472 if (names) {
1473 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
1474 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
1475 if (i > 0)
1476 chunk_appendf(out, ", ");
1477 if (name->type == GEN_DNS) {
1478 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
1479 chunk_appendf(out, "DNS:%s", str);
1480 OPENSSL_free(str);
1481 }
1482 }
1483 }
1484 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
1485 }
1486 return 0;
1487}
1488#endif
1489
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001490/*
1491 * Build the ckch_inst_link that will be chained in the CA file entry and the
1492 * corresponding ckch_inst_link_ref that will be chained in the ckch instance.
1493 * Return 0 in case of success.
1494 */
1495static int do_chain_inst_and_cafile(struct cafile_entry *cafile_entry, struct ckch_inst *ckch_inst)
1496{
1497 struct ckch_inst_link *new_link;
1498 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
1499 struct ckch_inst_link *link = LIST_ELEM(cafile_entry->ckch_inst_link.n,
1500 typeof(link), list);
1501 /* Do not add multiple references to the same
1502 * instance in a cafile_entry */
1503 if (link->ckch_inst == ckch_inst) {
1504 return 1;
1505 }
1506 }
1507
1508 new_link = calloc(1, sizeof(*new_link));
1509 if (new_link) {
1510 struct ckch_inst_link_ref *new_link_ref = calloc(1, sizeof(*new_link_ref));
1511 if (!new_link_ref) {
1512 free(new_link);
1513 return 1;
1514 }
1515
1516 new_link->ckch_inst = ckch_inst;
1517 new_link_ref->link = new_link;
1518 LIST_INIT(&new_link->list);
1519 LIST_INIT(&new_link_ref->list);
1520
1521 LIST_APPEND(&cafile_entry->ckch_inst_link, &new_link->list);
1522 LIST_APPEND(&ckch_inst->cafile_link_refs, &new_link_ref->list);
1523 }
1524
1525 return 0;
1526}
1527
1528
1529/*
1530 * Link a CA file tree entry to the ckch instance that uses it.
1531 * To determine if and which CA file tree entries need to be linked to the
1532 * instance, we follow the same logic performed in ssl_sock_prepare_ctx when
1533 * processing the verify option.
1534 * This function works for a frontend as well as for a backend, depending on the
1535 * configuration parameters given (bind_conf or server).
1536 */
1537void ckch_inst_add_cafile_link(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf,
1538 struct ssl_bind_conf *ssl_conf, const struct server *srv)
1539{
1540 int verify = SSL_VERIFY_NONE;
1541
1542 if (srv) {
1543
1544 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
1545 verify = SSL_VERIFY_PEER;
1546 switch (srv->ssl_ctx.verify) {
1547 case SSL_SOCK_VERIFY_NONE:
1548 verify = SSL_VERIFY_NONE;
1549 break;
1550 case SSL_SOCK_VERIFY_REQUIRED:
1551 verify = SSL_VERIFY_PEER;
1552 break;
1553 }
1554 }
1555 else {
1556 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
1557 case SSL_SOCK_VERIFY_NONE:
1558 verify = SSL_VERIFY_NONE;
1559 break;
1560 case SSL_SOCK_VERIFY_OPTIONAL:
1561 verify = SSL_VERIFY_PEER;
1562 break;
1563 case SSL_SOCK_VERIFY_REQUIRED:
1564 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1565 break;
1566 }
1567 }
1568
1569 if (verify & SSL_VERIFY_PEER) {
1570 struct cafile_entry *ca_file_entry = NULL;
1571 struct cafile_entry *ca_verify_file_entry = NULL;
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001572 struct cafile_entry *crl_file_entry = NULL;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001573 if (srv) {
1574 if (srv->ssl_ctx.ca_file) {
1575 ca_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.ca_file, 0);
1576
1577 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001578 if (srv->ssl_ctx.crl_file) {
1579 crl_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.crl_file, 0);
1580 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001581 }
1582 else {
1583 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
1584 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 +02001585 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 +01001586
1587 if (ca_file)
1588 ca_file_entry = ssl_store_get_cafile_entry(ca_file, 0);
1589 if (ca_verify_file)
1590 ca_verify_file_entry = ssl_store_get_cafile_entry(ca_verify_file, 0);
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001591 if (crl_file)
1592 crl_file_entry = ssl_store_get_cafile_entry(crl_file, 0);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001593 }
1594
1595 if (ca_file_entry) {
1596 /* If we have a ckch instance that is not already in the
1597 * cafile_entry's list, add it to it. */
1598 if (do_chain_inst_and_cafile(ca_file_entry, ckch_inst))
1599 return;
1600
1601 }
1602 if (ca_verify_file_entry && (ca_file_entry != ca_verify_file_entry)) {
1603 /* If we have a ckch instance that is not already in the
1604 * cafile_entry's list, add it to it. */
1605 if (do_chain_inst_and_cafile(ca_verify_file_entry, ckch_inst))
1606 return;
1607 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001608 if (crl_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(crl_file_entry, ckch_inst))
1612 return;
1613 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001614 }
1615}
1616
William Lallemandda8584c2020-05-14 10:14:37 +02001617
1618
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001619static int show_cert_detail(X509 *cert, STACK_OF(X509) *chain, struct buffer *out)
William Lallemandda8584c2020-05-14 10:14:37 +02001620{
William Lallemandda8584c2020-05-14 10:14:37 +02001621 BIO *bio = NULL;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001622 struct buffer *tmp = alloc_trash_chunk();
William Lallemandda8584c2020-05-14 10:14:37 +02001623 int i;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001624 int write = -1;
1625 unsigned int len = 0;
1626 X509_NAME *name = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001627
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001628 if (!tmp)
1629 return -1;
William Lallemandda8584c2020-05-14 10:14:37 +02001630
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001631 if (!cert)
William Lallemand5685ccf2020-09-16 16:12:25 +02001632 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02001633
William Lallemand5685ccf2020-09-16 16:12:25 +02001634 if (chain == NULL) {
1635 struct issuer_chain *issuer;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001636 issuer = ssl_get0_issuer_chain(cert);
William Lallemand5685ccf2020-09-16 16:12:25 +02001637 if (issuer) {
1638 chain = issuer->chain;
1639 chunk_appendf(out, "Chain Filename: ");
1640 chunk_appendf(out, "%s\n", issuer->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001641 }
William Lallemand5685ccf2020-09-16 16:12:25 +02001642 }
1643 chunk_appendf(out, "Serial: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001644 if (ssl_sock_get_serial(cert, tmp) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001645 goto end;
1646 dump_binary(out, tmp->area, tmp->data);
1647 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001648
William Lallemand5685ccf2020-09-16 16:12:25 +02001649 chunk_appendf(out, "notBefore: ");
1650 chunk_reset(tmp);
1651 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1652 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001653 if (ASN1_TIME_print(bio, X509_getm_notBefore(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001654 goto end;
1655 write = BIO_read(bio, tmp->area, tmp->size-1);
1656 tmp->area[write] = '\0';
1657 BIO_free(bio);
1658 bio = NULL;
1659 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001660
William Lallemand5685ccf2020-09-16 16:12:25 +02001661 chunk_appendf(out, "notAfter: ");
1662 chunk_reset(tmp);
1663 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1664 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001665 if (ASN1_TIME_print(bio, X509_getm_notAfter(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001666 goto end;
1667 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
1668 goto end;
1669 tmp->area[write] = '\0';
1670 BIO_free(bio);
1671 bio = NULL;
1672 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001673
1674#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand5685ccf2020-09-16 16:12:25 +02001675 chunk_appendf(out, "Subject Alternative Name: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001676 if (ssl_sock_get_san_oneline(cert, out) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001677 goto end;
1678 *(out->area + out->data) = '\0';
1679 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001680#endif
William Lallemand5685ccf2020-09-16 16:12:25 +02001681 chunk_reset(tmp);
1682 chunk_appendf(out, "Algorithm: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001683 if (cert_get_pkey_algo(cert, tmp) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001684 goto end;
1685 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001686
William Lallemand5685ccf2020-09-16 16:12:25 +02001687 chunk_reset(tmp);
1688 chunk_appendf(out, "SHA1 FingerPrint: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001689 if (X509_digest(cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001690 goto end;
1691 tmp->data = len;
1692 dump_binary(out, tmp->area, tmp->data);
1693 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001694
William Lallemand5685ccf2020-09-16 16:12:25 +02001695 chunk_appendf(out, "Subject: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001696 if ((name = X509_get_subject_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001697 goto end;
1698 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1699 goto end;
1700 *(tmp->area + tmp->data) = '\0';
1701 chunk_appendf(out, "%s\n", tmp->area);
1702
1703 chunk_appendf(out, "Issuer: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001704 if ((name = X509_get_issuer_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001705 goto end;
1706 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1707 goto end;
1708 *(tmp->area + tmp->data) = '\0';
1709 chunk_appendf(out, "%s\n", tmp->area);
1710
1711 /* Displays subject of each certificate in the chain */
1712 for (i = 0; i < sk_X509_num(chain); i++) {
1713 X509 *ca = sk_X509_value(chain, i);
1714
1715 chunk_appendf(out, "Chain Subject: ");
1716 if ((name = X509_get_subject_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001717 goto end;
1718 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1719 goto end;
1720 *(tmp->area + tmp->data) = '\0';
1721 chunk_appendf(out, "%s\n", tmp->area);
1722
William Lallemand5685ccf2020-09-16 16:12:25 +02001723 chunk_appendf(out, "Chain Issuer: ");
1724 if ((name = X509_get_issuer_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001725 goto end;
1726 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1727 goto end;
1728 *(tmp->area + tmp->data) = '\0';
1729 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001730 }
1731
1732end:
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001733 if (bio)
1734 BIO_free(bio);
1735 free_trash_chunk(tmp);
1736
1737 return 0;
1738}
1739
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001740#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 +02001741/*
1742 * Build the OCSP tree entry's key for a given ckch_store.
1743 * Returns a negative value in case of error.
1744 */
1745static int ckch_store_build_certid(struct ckch_store *ckch_store, unsigned char certid[128], unsigned int *key_length)
1746{
1747 OCSP_RESPONSE *resp;
1748 OCSP_BASICRESP *bs = NULL;
1749 OCSP_SINGLERESP *sr;
1750 OCSP_CERTID *id;
1751 unsigned char *p = NULL;
1752
1753 if (!key_length)
1754 return -1;
1755
1756 *key_length = 0;
1757
1758 if (!ckch_store->ckch->ocsp_response)
1759 return 0;
1760
1761 p = (unsigned char *) ckch_store->ckch->ocsp_response->area;
1762
1763 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
1764 ckch_store->ckch->ocsp_response->data);
1765 if (!resp) {
1766 goto end;
1767 }
1768
1769 bs = OCSP_response_get1_basic(resp);
1770 if (!bs) {
1771 goto end;
1772 }
1773
1774 sr = OCSP_resp_get0(bs, 0);
1775 if (!sr) {
1776 goto end;
1777 }
1778
1779 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
1780
1781 p = certid;
1782 *key_length = i2d_OCSP_CERTID(id, &p);
1783
1784end:
1785 return *key_length > 0;
1786}
1787#endif
1788
1789/*
1790 * Dump the OCSP certificate key (if it exists) of certificate <ckch> into
1791 * buffer <out>.
1792 * Returns 0 in case of success.
1793 */
1794static int ckch_store_show_ocsp_certid(struct ckch_store *ckch_store, struct buffer *out)
1795{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001796#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 +02001797 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1798 unsigned int key_length = 0;
1799 int i;
1800
1801 if (ckch_store_build_certid(ckch_store, (unsigned char*)key, &key_length) >= 0) {
1802 /* Dump the CERTID info */
1803 chunk_appendf(out, "OCSP Response Key: ");
1804 for (i = 0; i < key_length; ++i) {
1805 chunk_appendf(out, "%02x", key[i]);
1806 }
1807 chunk_appendf(out, "\n");
1808 }
1809#endif
1810
1811 return 0;
1812}
1813
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001814
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001815/* IO handler of the details "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001816 * It uses a struct show_cert_ctx and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001817 */
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001818static int cli_io_handler_show_cert_detail(struct appctx *appctx)
1819{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001820 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001821 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001822 struct buffer *out = alloc_trash_chunk();
1823 int retval = 0;
1824
1825 if (!out)
1826 goto end_no_putchk;
1827
1828 chunk_appendf(out, "Filename: ");
1829 if (ckchs == ckchs_transaction.new_ckchs)
1830 chunk_appendf(out, "*");
1831 chunk_appendf(out, "%s\n", ckchs->path);
1832
1833 chunk_appendf(out, "Status: ");
1834 if (ckchs->ckch->cert == NULL)
1835 chunk_appendf(out, "Empty\n");
1836 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
1837 chunk_appendf(out, "Unused\n");
1838 else
1839 chunk_appendf(out, "Used\n");
1840
1841 retval = show_cert_detail(ckchs->ckch->cert, ckchs->ckch->chain, out);
1842 if (retval < 0)
1843 goto end_no_putchk;
1844 else if (retval)
1845 goto end;
1846
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001847 ckch_store_show_ocsp_certid(ckchs, out);
1848
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001849end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001850 if (applet_putchk(appctx, out) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001851 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001852
1853end_no_putchk:
William Lallemandda8584c2020-05-14 10:14:37 +02001854 free_trash_chunk(out);
1855 return 1;
1856yield:
William Lallemandda8584c2020-05-14 10:14:37 +02001857 free_trash_chunk(out);
1858 return 0; /* should come back */
1859}
1860
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001861
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001862/* IO handler of the details "show ssl cert <filename.ocsp>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001863 * It uses a show_cert_ctx.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001864 */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001865static int cli_io_handler_show_cert_ocsp_detail(struct appctx *appctx)
1866{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001867#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL)
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001868 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001869 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001870 struct buffer *out = alloc_trash_chunk();
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001871 int from_transaction = ctx->transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001872
1873 if (!out)
1874 goto end_no_putchk;
1875
1876 /* If we try to display an ongoing transaction's OCSP response, we
1877 * need to dump the ckch's ocsp_response buffer directly.
1878 * Otherwise, we must rebuild the certificate's certid in order to
1879 * look for the current OCSP response in the tree. */
1880 if (from_transaction && ckchs->ckch->ocsp_response) {
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001881 if (ssl_ocsp_response_print(ckchs->ckch->ocsp_response, out))
1882 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001883 }
1884 else {
1885 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1886 unsigned int key_length = 0;
1887
1888 if (ckch_store_build_certid(ckchs, (unsigned char*)key, &key_length) < 0)
1889 goto end_no_putchk;
1890
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001891 if (ssl_get_ocspresponse_detail(key, out))
1892 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001893 }
1894
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001895 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001896 goto yield;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001897
1898end_no_putchk:
1899 free_trash_chunk(out);
1900 return 1;
1901yield:
1902 free_trash_chunk(out);
1903 return 0; /* should come back */
1904#else
1905 return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n");
1906#endif
1907}
1908
William Lallemandda8584c2020-05-14 10:14:37 +02001909/* parsing function for 'show ssl cert [certfile]' */
1910static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
1911{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001912 struct show_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02001913 struct ckch_store *ckchs;
1914
1915 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1916 return cli_err(appctx, "Can't allocate memory!\n");
1917
1918 /* The operations on the CKCH architecture are locked so we can
1919 * manipulate ckch_store and ckch_inst */
1920 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1921 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
1922
1923 /* check if there is a certificate to lookup */
1924 if (*args[3]) {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001925 int show_ocsp_detail = 0;
1926 int from_transaction = 0;
1927 char *end;
1928
1929 /* We manage the special case "certname.ocsp" through which we
1930 * can show the details of an OCSP response. */
1931 end = strrchr(args[3], '.');
1932 if (end && strcmp(end+1, "ocsp") == 0) {
1933 *end = '\0';
1934 show_ocsp_detail = 1;
1935 }
1936
William Lallemandda8584c2020-05-14 10:14:37 +02001937 if (*args[3] == '*') {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001938 from_transaction = 1;
William Lallemandda8584c2020-05-14 10:14:37 +02001939 if (!ckchs_transaction.new_ckchs)
1940 goto error;
1941
1942 ckchs = ckchs_transaction.new_ckchs;
1943
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001944 if (strcmp(args[3] + 1, ckchs->path) != 0)
William Lallemandda8584c2020-05-14 10:14:37 +02001945 goto error;
1946
1947 } else {
1948 if ((ckchs = ckchs_lookup(args[3])) == NULL)
1949 goto error;
1950
1951 }
1952
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001953 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001954 /* use the IO handler that shows details */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001955 if (show_ocsp_detail) {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001956 ctx->transaction = from_transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001957 appctx->io_handler = cli_io_handler_show_cert_ocsp_detail;
1958 }
1959 else
1960 appctx->io_handler = cli_io_handler_show_cert_detail;
William Lallemandda8584c2020-05-14 10:14:37 +02001961 }
1962
1963 return 0;
1964
1965error:
1966 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1967 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
1968}
1969
1970/* release function of the `set ssl cert' command, free things and unlock the spinlock */
1971static void cli_release_commit_cert(struct appctx *appctx)
1972{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02001973 struct commit_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001974
1975 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulet9d56e242022-05-31 16:37:01 +02001976 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
1977 if (ctx->new_ckchs)
1978 ckch_store_free(ctx->new_ckchs);
1979 ha_free(&ctx->err);
William Lallemandda8584c2020-05-14 10:14:37 +02001980}
1981
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001982
1983/*
1984 * Rebuild a new instance 'new_inst' based on an old instance 'ckchi' and a
1985 * specific ckch_store.
1986 * Returns 0 in case of success, 1 otherwise.
1987 */
William Lallemande60c7d62022-03-30 11:26:15 +02001988int ckch_inst_rebuild(struct ckch_store *ckch_store, struct ckch_inst *ckchi,
1989 struct ckch_inst **new_inst, char **err)
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001990{
1991 int retval = 0;
1992 int errcode = 0;
1993 struct sni_ctx *sc0, *sc0s;
1994 char **sni_filter = NULL;
1995 int fcount = 0;
1996
1997 if (ckchi->crtlist_entry) {
1998 sni_filter = ckchi->crtlist_entry->filters;
1999 fcount = ckchi->crtlist_entry->fcount;
2000 }
2001
2002 if (ckchi->is_server_instance)
2003 errcode |= ckch_inst_new_load_srv_store(ckch_store->path, ckch_store, new_inst, err);
2004 else
2005 errcode |= ckch_inst_new_load_store(ckch_store->path, ckch_store, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, new_inst, err);
2006
2007 if (errcode & ERR_CODE)
2008 return 1;
2009
2010 /* if the previous ckchi was used as the default */
2011 if (ckchi->is_default)
2012 (*new_inst)->is_default = 1;
2013
2014 (*new_inst)->is_server_instance = ckchi->is_server_instance;
2015 (*new_inst)->server = ckchi->server;
2016 /* Create a new SSL_CTX and link it to the new instance. */
2017 if ((*new_inst)->is_server_instance) {
2018 retval = ssl_sock_prep_srv_ctx_and_inst(ckchi->server, (*new_inst)->ctx, (*new_inst));
2019 if (retval)
2020 return 1;
2021 }
2022
2023 /* create the link to the crtlist_entry */
2024 (*new_inst)->crtlist_entry = ckchi->crtlist_entry;
2025
2026 /* we need to initialize the SSL_CTX generated */
2027 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
2028 list_for_each_entry_safe(sc0, sc0s, &(*new_inst)->sni_ctx, by_ckch_inst) {
2029 if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
2030 errcode |= ssl_sock_prep_ctx_and_inst(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, *new_inst, err);
2031 if (errcode & ERR_CODE)
2032 return 1;
2033 }
2034 }
2035
2036 return 0;
2037}
2038
2039/*
2040 * Load all the new SNIs of a newly built ckch instance in the trees, or replace
2041 * a server's main ckch instance.
2042 */
2043static void __ssl_sock_load_new_ckch_instance(struct ckch_inst *ckchi)
2044{
2045 /* The bind_conf will be null on server ckch_instances. */
2046 if (ckchi->is_server_instance) {
2047 int i;
2048 /* a lock is needed here since we have to free the SSL cache */
2049 HA_RWLOCK_WRLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
2050 /* free the server current SSL_CTX */
2051 SSL_CTX_free(ckchi->server->ssl_ctx.ctx);
2052 /* Actual ssl context update */
2053 SSL_CTX_up_ref(ckchi->ctx);
2054 ckchi->server->ssl_ctx.ctx = ckchi->ctx;
2055 ckchi->server->ssl_ctx.inst = ckchi;
2056
2057 /* flush the session cache of the server */
2058 for (i = 0; i < global.nbthread; i++) {
William Lallemandce990332021-11-23 15:15:09 +01002059 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].sni);
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002060 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].ptr);
2061 }
2062 HA_RWLOCK_WRUNLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
2063
2064 } else {
2065 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
2066 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
2067 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
2068 }
2069}
2070
2071/*
2072 * Delete a ckch instance that was replaced after a CLI command.
2073 */
2074static void __ckch_inst_free_locked(struct ckch_inst *ckchi)
2075{
2076 if (ckchi->is_server_instance) {
2077 /* no lock for servers */
2078 ckch_inst_free(ckchi);
2079 } else {
2080 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
2081
2082 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
2083 ckch_inst_free(ckchi);
2084 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
2085 }
2086}
2087
William Lallemand3b5a3a62022-03-29 14:29:31 +02002088/* Replace a ckch_store in the ckch tree and insert the whole dependencies,
2089* then free the previous dependencies and store.
2090* Used in the case of a certificate update.
2091*
2092* Every dependencies must allocated before using this function.
2093*
2094* This function can't fail as it only update pointers, and does not alloc anything.
2095*
2096* /!\ This function must be used under the ckch lock. /!\
2097*
2098* - Insert every dependencies (SNI, crtlist_entry, ckch_inst, etc)
2099* - Delete the old ckch_store from the tree
2100* - Insert the new ckch_store
2101* - Free the old dependencies and the old ckch_store
2102*/
2103void ckch_store_replace(struct ckch_store *old_ckchs, struct ckch_store *new_ckchs)
2104{
2105 struct crtlist_entry *entry;
2106 struct ckch_inst *ckchi, *ckchis;
2107
2108 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
2109 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
2110 ebpt_delete(&entry->node);
2111 /* change the ptr and reinsert the node */
2112 entry->node.key = new_ckchs;
2113 ebpt_insert(&entry->crtlist->entries, &entry->node);
2114 }
2115 /* insert the new ckch_insts in the crtlist_entry */
2116 list_for_each_entry(ckchi, &new_ckchs->ckch_inst, by_ckchs) {
2117 if (ckchi->crtlist_entry)
2118 LIST_INSERT(&ckchi->crtlist_entry->ckch_inst, &ckchi->by_crtlist_entry);
2119 }
2120 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2121 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
2122 __ssl_sock_load_new_ckch_instance(ckchi);
2123 }
2124 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
2125 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
2126 __ckch_inst_free_locked(ckchi);
2127 }
2128
2129 ckch_store_free(old_ckchs);
2130 ebst_insert(&ckchs_tree, &new_ckchs->node);
2131}
2132
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002133
William Lallemandda8584c2020-05-14 10:14:37 +02002134/*
2135 * This function tries to create the new ckch_inst and their SNIs
William Lallemand30fcca12022-03-30 12:03:12 +02002136 *
2137 * /!\ don't forget to update __hlua_ckch_commit() if you changes things there. /!\
William Lallemandda8584c2020-05-14 10:14:37 +02002138 */
2139static int cli_io_handler_commit_cert(struct appctx *appctx)
2140{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002141 struct commit_cert_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002142 struct stconn *sc = appctx_sc(appctx);
William Lallemandda8584c2020-05-14 10:14:37 +02002143 int y = 0;
William Lallemandda8584c2020-05-14 10:14:37 +02002144 struct ckch_store *old_ckchs, *new_ckchs = NULL;
William Lallemand3b5a3a62022-03-29 14:29:31 +02002145 struct ckch_inst *ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002146
Willy Tarreau475e4632022-05-27 10:26:46 +02002147 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Christopher Faulet9d56e242022-05-31 16:37:01 +02002148 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02002149
2150 while (1) {
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002151 switch (ctx->state) {
2152 case CERT_ST_INIT:
William Lallemandda8584c2020-05-14 10:14:37 +02002153 /* This state just print the update message */
Christopher Faulet9d56e242022-05-31 16:37:01 +02002154 chunk_printf(&trash, "Committing %s", ckchs_transaction.path);
2155 if (applet_putchk(appctx, &trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02002156 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002157
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002158 ctx->state = CERT_ST_GEN;
William Lallemandda8584c2020-05-14 10:14:37 +02002159 /* fallthrough */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002160 case CERT_ST_GEN:
William Lallemandda8584c2020-05-14 10:14:37 +02002161 /*
2162 * This state generates the ckch instances with their
2163 * sni_ctxs and SSL_CTX.
2164 *
2165 * Since the SSL_CTX generation can be CPU consumer, we
2166 * yield every 10 instances.
2167 */
2168
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002169 old_ckchs = ctx->old_ckchs;
2170 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002171
William Lallemandda8584c2020-05-14 10:14:37 +02002172 /* get the next ckchi to regenerate */
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002173 ckchi = ctx->next_ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002174 /* we didn't start yet, set it to the first elem */
2175 if (ckchi == NULL)
2176 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
2177
2178 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
2179 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
2180 struct ckch_inst *new_inst;
William Lallemandda8584c2020-05-14 10:14:37 +02002181
Christopher Faulet9d56e242022-05-31 16:37:01 +02002182 /* save the next ckchi to compute in case of yield */
2183 ctx->next_ckchi = ckchi;
2184
William Lallemandda8584c2020-05-14 10:14:37 +02002185 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2186 if (y >= 10) {
Christopher Faulet9d56e242022-05-31 16:37:01 +02002187 applet_have_more_data(appctx); /* let's come back later */
William Lallemandda8584c2020-05-14 10:14:37 +02002188 goto yield;
2189 }
2190
Christopher Faulet9d56e242022-05-31 16:37:01 +02002191 /* display one dot per new instance */
2192 if (applet_putstr(appctx, ".") == -1)
2193 goto yield;
2194
2195 ctx->err = NULL;
2196 if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &ctx->err)) {
2197 ctx->state = CERT_ST_ERROR;
William Lallemandda8584c2020-05-14 10:14:37 +02002198 goto error;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002199 }
William Lallemandda8584c2020-05-14 10:14:37 +02002200
William Lallemandda8584c2020-05-14 10:14:37 +02002201 /* link the new ckch_inst to the duplicate */
Willy Tarreau2b718102021-04-21 07:32:39 +02002202 LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002203 y++;
2204 }
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002205 ctx->state = CERT_ST_INSERT;
William Lallemandda8584c2020-05-14 10:14:37 +02002206 /* fallthrough */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002207 case CERT_ST_INSERT:
William Lallemandda8584c2020-05-14 10:14:37 +02002208 /* The generation is finished, we can insert everything */
2209
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002210 old_ckchs = ctx->old_ckchs;
2211 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002212
William Lallemand3b5a3a62022-03-29 14:29:31 +02002213 /* insert everything and remove the previous objects */
2214 ckch_store_replace(old_ckchs, new_ckchs);
Christopher Faulet9d56e242022-05-31 16:37:01 +02002215 ctx->new_ckchs = ctx->old_ckchs = NULL;
2216 ctx->state = CERT_ST_SUCCESS;
2217 /* fallthrough */
2218 case CERT_ST_SUCCESS:
2219 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2220 goto yield;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002221 ctx->state = CERT_ST_FIN;
William Lallemandda8584c2020-05-14 10:14:37 +02002222 /* fallthrough */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002223 case CERT_ST_FIN:
William Lallemandda8584c2020-05-14 10:14:37 +02002224 /* we achieved the transaction, we can set everything to NULL */
William Lallemandda8584c2020-05-14 10:14:37 +02002225 ckchs_transaction.new_ckchs = NULL;
2226 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002227 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002228 goto end;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002229
2230 case CERT_ST_ERROR:
2231 error:
2232 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2233 if (applet_putchk(appctx, &trash) == -1)
2234 goto yield;
2235 ctx->state = CERT_ST_FIN;
2236 break;
William Lallemandda8584c2020-05-14 10:14:37 +02002237 }
2238 }
2239end:
William Lallemandda8584c2020-05-14 10:14:37 +02002240 /* success: call the release function and don't come back */
2241 return 1;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002242
William Lallemandda8584c2020-05-14 10:14:37 +02002243yield:
William Lallemandda8584c2020-05-14 10:14:37 +02002244 return 0; /* should come back */
William Lallemandda8584c2020-05-14 10:14:37 +02002245}
2246
2247/*
2248 * Parsing function of 'commit ssl cert'
2249 */
2250static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
2251{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002252 struct commit_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02002253 char *err = NULL;
2254
2255 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2256 return 1;
2257
2258 if (!*args[3])
2259 return cli_err(appctx, "'commit ssl cert expects a filename\n");
2260
2261 /* The operations on the CKCH architecture are locked so we can
2262 * manipulate ckch_store and ckch_inst */
2263 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2264 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
2265
2266 if (!ckchs_transaction.path) {
2267 memprintf(&err, "No ongoing transaction! !\n");
2268 goto error;
2269 }
2270
2271 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2272 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
2273 goto error;
2274 }
2275
William Lallemand5685ccf2020-09-16 16:12:25 +02002276 /* if a certificate is here, a private key must be here too */
2277 if (ckchs_transaction.new_ckchs->ckch->cert && !ckchs_transaction.new_ckchs->ckch->key) {
2278 memprintf(&err, "The transaction must contain at least a certificate and a private key!\n");
2279 goto error;
2280 }
William Lallemanda9419522020-06-24 16:26:41 +02002281
William Lallemand5685ccf2020-09-16 16:12:25 +02002282 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
2283 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
2284 goto error;
William Lallemandda8584c2020-05-14 10:14:37 +02002285 }
2286
2287 /* init the appctx structure */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002288 ctx->state = CERT_ST_INIT;
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002289 ctx->next_ckchi = NULL;
2290 ctx->new_ckchs = ckchs_transaction.new_ckchs;
2291 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002292
2293 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
2294 return 0;
2295
2296error:
2297
2298 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2299 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2300
2301 return cli_dynerr(appctx, err);
2302}
2303
2304
2305
2306
2307/*
2308 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
Willy Tarreau329f4b42022-05-04 20:05:55 +02002309 * It uses a set_cert_ctx context, and ckchs_transaction under a lock.
William Lallemandda8584c2020-05-14 10:14:37 +02002310 */
2311static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
2312{
2313 struct ckch_store *new_ckchs = NULL;
2314 struct ckch_store *old_ckchs = NULL;
2315 char *err = NULL;
2316 int i;
William Lallemandda8584c2020-05-14 10:14:37 +02002317 int errcode = 0;
2318 char *end;
William Lallemandff8bf982022-03-29 10:44:23 +02002319 struct cert_exts *cert_ext = &cert_exts[0]; /* default one, PEM */
William Lallemandda8584c2020-05-14 10:14:37 +02002320 struct cert_key_and_chain *ckch;
2321 struct buffer *buf;
2322
2323 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2324 return 1;
2325
William Lallemandda8584c2020-05-14 10:14:37 +02002326 if (!*args[3] || !payload)
2327 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
2328
2329 /* The operations on the CKCH architecture are locked so we can
2330 * manipulate ckch_store and ckch_inst */
2331 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2332 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
2333
William Lallemand5ba80d62021-05-04 16:17:27 +02002334 if ((buf = alloc_trash_chunk()) == NULL) {
2335 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2336 errcode |= ERR_ALERT | ERR_FATAL;
2337 goto end;
2338 }
William Lallemande5ff4ad2020-06-08 09:40:37 +02002339
William Lallemandda8584c2020-05-14 10:14:37 +02002340 if (!chunk_strcpy(buf, args[3])) {
2341 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2342 errcode |= ERR_ALERT | ERR_FATAL;
2343 goto end;
2344 }
2345
2346 /* check which type of file we want to update */
William Lallemandff8bf982022-03-29 10:44:23 +02002347 for (i = 0; cert_exts[i].ext != NULL; i++) {
William Lallemandda8584c2020-05-14 10:14:37 +02002348 end = strrchr(buf->area, '.');
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002349 if (end && *cert_exts[i].ext && (strcmp(end + 1, cert_exts[i].ext) == 0)) {
William Lallemandda8584c2020-05-14 10:14:37 +02002350 *end = '\0';
William Lallemand089c1382020-10-23 17:35:12 +02002351 buf->data = strlen(buf->area);
William Lallemandff8bf982022-03-29 10:44:23 +02002352 cert_ext = &cert_exts[i];
William Lallemandda8584c2020-05-14 10:14:37 +02002353 break;
2354 }
2355 }
2356
William Lallemandda8584c2020-05-14 10:14:37 +02002357 /* if there is an ongoing transaction */
2358 if (ckchs_transaction.path) {
William Lallemandda8584c2020-05-14 10:14:37 +02002359 /* if there is an ongoing transaction, check if this is the same file */
2360 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
William Lallemand089c1382020-10-23 17:35:12 +02002361 /* we didn't find the transaction, must try more cases below */
2362
2363 /* 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 +02002364 if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
William Lallemand089c1382020-10-23 17:35:12 +02002365 if (!chunk_strcat(buf, ".crt")) {
2366 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2367 errcode |= ERR_ALERT | ERR_FATAL;
2368 goto end;
2369 }
2370
2371 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
2372 /* remove .crt of the error message */
2373 *(b_orig(buf) + b_data(buf) + strlen(".crt")) = '\0';
2374 b_sub(buf, strlen(".crt"));
2375
2376 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
2377 errcode |= ERR_ALERT | ERR_FATAL;
2378 goto end;
2379 }
2380 }
William Lallemandda8584c2020-05-14 10:14:37 +02002381 }
2382
Christopher Faulet24a20b92022-06-03 11:50:40 +02002383 old_ckchs = ckchs_transaction.new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002384
2385 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002386
William Lallemand95fefa12020-09-09 12:01:33 +02002387 /* lookup for the certificate in the tree */
Christopher Faulet24a20b92022-06-03 11:50:40 +02002388 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002389
Christopher Faulet24a20b92022-06-03 11:50:40 +02002390 if (!old_ckchs) {
William Lallemand089c1382020-10-23 17:35:12 +02002391 /* 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 +02002392 if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
William Lallemand089c1382020-10-23 17:35:12 +02002393 if (!chunk_strcat(buf, ".crt")) {
2394 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2395 errcode |= ERR_ALERT | ERR_FATAL;
2396 goto end;
2397 }
Christopher Faulet24a20b92022-06-03 11:50:40 +02002398 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002399 }
2400 }
William Lallemandda8584c2020-05-14 10:14:37 +02002401 }
2402
Christopher Faulet24a20b92022-06-03 11:50:40 +02002403 if (!old_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02002404 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
2405 err ? err : "");
2406 errcode |= ERR_ALERT | ERR_FATAL;
2407 goto end;
2408 }
2409
William Lallemandda8584c2020-05-14 10:14:37 +02002410 /* duplicate the ckch store */
2411 new_ckchs = ckchs_dup(old_ckchs);
2412 if (!new_ckchs) {
2413 memprintf(&err, "%sCannot allocate memory!\n",
2414 err ? err : "");
2415 errcode |= ERR_ALERT | ERR_FATAL;
2416 goto end;
2417 }
2418
William Lallemand95fefa12020-09-09 12:01:33 +02002419 ckch = new_ckchs->ckch;
William Lallemandda8584c2020-05-14 10:14:37 +02002420
2421 /* appply the change on the duplicate */
William Lallemandff8bf982022-03-29 10:44:23 +02002422 if (cert_ext->load(buf->area, payload, ckch, &err) != 0) {
William Lallemandda8584c2020-05-14 10:14:37 +02002423 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
2424 errcode |= ERR_ALERT | ERR_FATAL;
2425 goto end;
2426 }
2427
William Lallemandda8584c2020-05-14 10:14:37 +02002428 /* we succeed, we can save the ckchs in the transaction */
2429
2430 /* if there wasn't a transaction, update the old ckchs */
2431 if (!ckchs_transaction.old_ckchs) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002432 ckchs_transaction.old_ckchs = old_ckchs;
2433 ckchs_transaction.path = old_ckchs->path;
William Lallemandda8584c2020-05-14 10:14:37 +02002434 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
2435 } else {
2436 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
2437
2438 }
2439
2440 /* free the previous ckchs if there was a transaction */
2441 ckch_store_free(ckchs_transaction.new_ckchs);
2442
Christopher Faulet24a20b92022-06-03 11:50:40 +02002443 ckchs_transaction.new_ckchs = new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002444
2445
2446 /* creates the SNI ctxs later in the IO handler */
2447
2448end:
2449 free_trash_chunk(buf);
2450
2451 if (errcode & ERR_CODE) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002452 ckch_store_free(new_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002453 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2454 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2455 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002456 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2457 return cli_dynmsg(appctx, LOG_NOTICE, err);
2458 }
2459 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
2460}
2461
2462/* parsing function of 'abort ssl cert' */
2463static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
2464{
2465 char *err = NULL;
2466
2467 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2468 return 1;
2469
2470 if (!*args[3])
2471 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
2472
2473 /* The operations on the CKCH architecture are locked so we can
2474 * manipulate ckch_store and ckch_inst */
2475 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2476 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2477
2478 if (!ckchs_transaction.path) {
2479 memprintf(&err, "No ongoing transaction!\n");
2480 goto error;
2481 }
2482
2483 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2484 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
2485 goto error;
2486 }
2487
2488 /* Only free the ckchs there, because the SNI and instances were not generated yet */
2489 ckch_store_free(ckchs_transaction.new_ckchs);
2490 ckchs_transaction.new_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002491 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002492 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002493
2494 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2495
2496 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2497 return cli_dynmsg(appctx, LOG_NOTICE, err);
2498
2499error:
2500 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2501
2502 return cli_dynerr(appctx, err);
2503}
2504
2505/* parsing function of 'new ssl cert' */
2506static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
2507{
2508 struct ckch_store *store;
2509 char *err = NULL;
2510 char *path;
2511
2512 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2513 return 1;
2514
2515 if (!*args[3])
2516 return cli_err(appctx, "'new ssl cert' expects a filename\n");
2517
2518 path = args[3];
2519
2520 /* The operations on the CKCH architecture are locked so we can
2521 * manipulate ckch_store and ckch_inst */
2522 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2523 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
2524
2525 store = ckchs_lookup(path);
2526 if (store != NULL) {
2527 memprintf(&err, "Certificate '%s' already exists!\n", path);
2528 store = NULL; /* we don't want to free it */
2529 goto error;
2530 }
2531 /* we won't support multi-certificate bundle here */
William Lallemandbd8e6ed2020-09-16 16:08:08 +02002532 store = ckch_store_new(path);
William Lallemandda8584c2020-05-14 10:14:37 +02002533 if (!store) {
2534 memprintf(&err, "unable to allocate memory.\n");
2535 goto error;
2536 }
2537
2538 /* insert into the ckchs tree */
2539 ebst_insert(&ckchs_tree, &store->node);
2540 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
2541
2542 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2543 return cli_dynmsg(appctx, LOG_NOTICE, err);
2544error:
2545 free(store);
2546 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2547 return cli_dynerr(appctx, err);
2548}
2549
2550/* parsing function of 'del ssl cert' */
2551static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
2552{
2553 struct ckch_store *store;
2554 char *err = NULL;
2555 char *filename;
2556
2557 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2558 return 1;
2559
2560 if (!*args[3])
2561 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
2562
2563 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2564 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
2565
2566 filename = args[3];
2567
Christopher Faulet926fefc2022-05-31 18:04:25 +02002568 if (ckchs_transaction.path && strcmp(ckchs_transaction.path, filename) == 0) {
2569 memprintf(&err, "ongoing transaction for the certificate '%s'", filename);
2570 goto error;
2571 }
2572
William Lallemandda8584c2020-05-14 10:14:37 +02002573 store = ckchs_lookup(filename);
2574 if (store == NULL) {
2575 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
2576 goto error;
2577 }
2578 if (!LIST_ISEMPTY(&store->ckch_inst)) {
2579 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
2580 goto error;
2581 }
2582
2583 ebmb_delete(&store->node);
2584 ckch_store_free(store);
2585
2586 memprintf(&err, "Certificate '%s' deleted!\n", filename);
2587
2588 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2589 return cli_dynmsg(appctx, LOG_NOTICE, err);
2590
2591error:
2592 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
2593 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2594 return cli_dynerr(appctx, err);
2595}
2596
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002597
Remi Tricot-Le Breton9f40fe02021-03-16 16:21:27 +01002598
2599/* parsing function of 'new ssl ca-file' */
2600static int cli_parse_new_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2601{
2602 struct cafile_entry *cafile_entry;
2603 char *err = NULL;
2604 char *path;
2605
2606 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2607 return 1;
2608
2609 if (!*args[3])
2610 return cli_err(appctx, "'new ssl ca-file' expects a filename\n");
2611
2612 path = args[3];
2613
2614 /* The operations on the CKCH architecture are locked so we can
2615 * manipulate ckch_store and ckch_inst */
2616 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2617 return cli_err(appctx, "Can't create a CA file!\nOperations on certificates are currently locked!\n");
2618
2619 cafile_entry = ssl_store_get_cafile_entry(path, 0);
2620 if (cafile_entry) {
2621 memprintf(&err, "CA file '%s' already exists!\n", path);
2622 goto error;
2623 }
2624
2625 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CERT);
2626 if (!cafile_entry) {
2627 memprintf(&err, "%sCannot allocate memory!\n",
2628 err ? err : "");
2629 goto error;
2630 }
2631
2632 /* Add the newly created cafile_entry to the tree so that
2633 * any new ckch instance created from now can use it. */
2634 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
2635 goto error;
2636
2637 memprintf(&err, "New CA file created '%s'!\n", path);
2638
2639 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2640 return cli_dynmsg(appctx, LOG_NOTICE, err);
2641error:
2642 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2643 return cli_dynerr(appctx, err);
2644}
2645
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002646/*
2647 * Parsing function of `set ssl ca-file`
2648 */
2649static int cli_parse_set_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2650{
Christopher Faulet132c5952022-06-03 11:56:26 +02002651 struct cafile_entry *old_cafile_entry = NULL;
2652 struct cafile_entry *new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002653 char *err = NULL;
2654 int errcode = 0;
2655 struct buffer *buf;
William Lallemand62c0b992022-07-29 17:50:58 +02002656 int add_cmd = 0;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002657
2658 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2659 return 1;
2660
William Lallemand62c0b992022-07-29 17:50:58 +02002661 /* this is "add ssl ca-file" */
2662 if (*args[0] == 'a')
2663 add_cmd = 1;
2664
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002665 if (!*args[3] || !payload)
2666 return cli_err(appctx, "'set ssl ca-file expects a filename and CAs as a payload\n");
2667
2668 /* The operations on the CKCH architecture are locked so we can
2669 * manipulate ckch_store and ckch_inst */
2670 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2671 return cli_err(appctx, "Can't update the CA file!\nOperations on certificates are currently locked!\n");
2672
2673 if ((buf = alloc_trash_chunk()) == NULL) {
2674 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2675 errcode |= ERR_ALERT | ERR_FATAL;
2676 goto end;
2677 }
2678
2679 if (!chunk_strcpy(buf, args[3])) {
2680 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2681 errcode |= ERR_ALERT | ERR_FATAL;
2682 goto end;
2683 }
2684
Christopher Faulet132c5952022-06-03 11:56:26 +02002685 old_cafile_entry = NULL;
2686 new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002687
2688 /* if there is an ongoing transaction */
2689 if (cafile_transaction.path) {
2690 /* if there is an ongoing transaction, check if this is the same file */
2691 if (strcmp(cafile_transaction.path, buf->area) != 0) {
2692 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, buf->area);
2693 errcode |= ERR_ALERT | ERR_FATAL;
2694 goto end;
2695 }
Christopher Faulet132c5952022-06-03 11:56:26 +02002696 old_cafile_entry = cafile_transaction.old_cafile_entry;
William Lallemand62c0b992022-07-29 17:50:58 +02002697 } else {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002698 /* lookup for the certificate in the tree */
Christopher Faulet132c5952022-06-03 11:56:26 +02002699 old_cafile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002700 }
2701
Christopher Faulet132c5952022-06-03 11:56:26 +02002702 if (!old_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002703 memprintf(&err, "%sCan't replace a CA file which is not referenced by the configuration!\n",
2704 err ? err : "");
2705 errcode |= ERR_ALERT | ERR_FATAL;
2706 goto end;
2707 }
2708
William Lallemand62c0b992022-07-29 17:50:58 +02002709 /* if the transaction is new, duplicate the old_ca_file_entry, otherwise duplicate the cafile in the current transaction */
2710 if (cafile_transaction.new_cafile_entry)
2711 new_cafile_entry = ssl_store_dup_cafile_entry(cafile_transaction.new_cafile_entry);
2712 else
2713 new_cafile_entry = ssl_store_dup_cafile_entry(old_cafile_entry);
2714
Christopher Faulet132c5952022-06-03 11:56:26 +02002715 if (!new_cafile_entry) {
William Lallemand62c0b992022-07-29 17:50:58 +02002716 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002717 errcode |= ERR_ALERT | ERR_FATAL;
2718 goto end;
2719 }
2720
William Lallemand62c0b992022-07-29 17:50:58 +02002721 /* Fill the new entry with the new CAs. The add_cmd variable determine
2722 if we flush the X509_STORE or not */
2723 if (ssl_store_load_ca_from_buf(new_cafile_entry, payload, add_cmd)) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002724 memprintf(&err, "%sInvalid payload\n", err ? err : "");
2725 errcode |= ERR_ALERT | ERR_FATAL;
2726 goto end;
2727 }
2728
2729 /* we succeed, we can save the ca in the transaction */
2730
2731 /* if there wasn't a transaction, update the old CA */
2732 if (!cafile_transaction.old_cafile_entry) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002733 cafile_transaction.old_cafile_entry = old_cafile_entry;
2734 cafile_transaction.path = old_cafile_entry->path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002735 err = memprintf(&err, "transaction created for CA %s!\n", cafile_transaction.path);
2736 } else {
2737 err = memprintf(&err, "transaction updated for CA %s!\n", cafile_transaction.path);
2738 }
2739
2740 /* free the previous CA if there was a transaction */
2741 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2742
Christopher Faulet132c5952022-06-03 11:56:26 +02002743 cafile_transaction.new_cafile_entry = new_cafile_entry;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002744
2745 /* creates the SNI ctxs later in the IO handler */
2746
2747end:
2748 free_trash_chunk(buf);
2749
2750 if (errcode & ERR_CODE) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002751 ssl_store_delete_cafile_entry(new_cafile_entry);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002752 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2753 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2754 } else {
2755
2756 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2757 return cli_dynmsg(appctx, LOG_NOTICE, err);
2758 }
2759}
2760
2761
2762/*
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002763 * Parsing function of 'commit ssl ca-file'.
2764 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl crl-file".
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002765 */
2766static int cli_parse_commit_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2767{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002768 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002769 char *err = NULL;
2770
2771 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2772 return 1;
2773
2774 if (!*args[3])
2775 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
2776
2777 /* The operations on the CKCH architecture are locked so we can
2778 * manipulate ckch_store and ckch_inst */
2779 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2780 return cli_err(appctx, "Can't commit the CA file!\nOperations on certificates are currently locked!\n");
2781
2782 if (!cafile_transaction.path) {
2783 memprintf(&err, "No ongoing transaction! !\n");
2784 goto error;
2785 }
2786
2787 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2788 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, args[3]);
2789 goto error;
2790 }
2791 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002792 ctx->state = CACRL_ST_INIT;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002793 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002794 ctx->old_entry = cafile_transaction.old_cafile_entry;
2795 ctx->new_entry = cafile_transaction.new_cafile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002796 ctx->cafile_type = CAFILE_CERT;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002797
2798 return 0;
2799
2800error:
2801
2802 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2803 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2804
2805 return cli_dynerr(appctx, err);
2806}
2807
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002808/*
2809 * This function tries to create new ckch instances and their SNIs using a newly
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002810 * set certificate authority (CA file) or a newly set Certificate Revocation
2811 * List (CRL), depending on the command being called.
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002812 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002813static int cli_io_handler_commit_cafile_crlfile(struct appctx *appctx)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002814{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002815 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002816 struct stconn *sc = appctx_sc(appctx);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002817 int y = 0;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002818 struct cafile_entry *old_cafile_entry = ctx->old_entry;
2819 struct cafile_entry *new_cafile_entry = ctx->new_entry;
William Lallemand0bfa3e72022-08-30 17:32:38 +02002820 struct ckch_inst_link *ckchi_link;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002821 char *path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002822
Willy Tarreau475e4632022-05-27 10:26:46 +02002823 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002824 goto end;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002825
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002826 /* The ctx was already validated by the ca-file/crl-file parsing
2827 * function. Entries can only be NULL in CACRL_ST_SUCCESS or
2828 * CACRL_ST_FIN states
2829 */
2830 switch (ctx->cafile_type) {
2831 case CAFILE_CERT:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002832 path = cafile_transaction.path;
2833 break;
2834 case CAFILE_CRL:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002835 path = crlfile_transaction.path;
2836 break;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002837 default:
Willy Tarreaud543ae02022-06-22 05:40:25 +02002838 path = NULL;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002839 goto error;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002840 }
2841
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002842 while (1) {
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002843 switch (ctx->state) {
2844 case CACRL_ST_INIT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002845 /* This state just print the update message */
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002846 chunk_printf(&trash, "Committing %s", path);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002847 if (applet_putchk(appctx, &trash) == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002848 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002849
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002850 ctx->state = CACRL_ST_GEN;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002851 /* fallthrough */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002852 case CACRL_ST_GEN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002853 /*
2854 * This state generates the ckch instances with their
2855 * sni_ctxs and SSL_CTX.
2856 *
2857 * Since the SSL_CTX generation can be CPU consumer, we
2858 * yield every 10 instances.
2859 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002860
2861 /* get the next ckchi to regenerate */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002862 ckchi_link = ctx->next_ckchi_link;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002863
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002864 /* we didn't start yet, set it to the first elem */
2865 if (ckchi_link == NULL) {
2866 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2867 /* Add the newly created cafile_entry to the tree so that
2868 * any new ckch instance created from now can use it. */
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002869 if (ssl_store_add_uncommitted_cafile_entry(new_cafile_entry)) {
2870 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002871 goto error;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002872 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002873 }
2874
2875 list_for_each_entry_from(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002876 struct ckch_inst *new_inst;
2877
2878 /* save the next ckchi to compute */
2879 ctx->next_ckchi_link = ckchi_link;
2880
2881 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2882 if (y >= 10) {
2883 applet_have_more_data(appctx); /* let's come back later */
2884 goto yield;
2885 }
2886
2887 /* display one dot per new instance */
2888 if (applet_putstr(appctx, ".") == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002889 goto yield;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002890
2891 /* Rebuild a new ckch instance that uses the same ckch_store
2892 * than a reference ckchi instance but will use a new CA file. */
2893 ctx->err = NULL;
2894 if (ckch_inst_rebuild(ckchi_link->ckch_inst->ckch_store, ckchi_link->ckch_inst, &new_inst, &ctx->err)) {
2895 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002896 goto error;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002897 }
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002898
2899 y++;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002900 }
2901
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002902 ctx->state = CACRL_ST_INSERT;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002903 /* fallthrough */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002904 case CACRL_ST_INSERT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002905 /* The generation is finished, we can insert everything */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002906
2907 /* insert the new ckch_insts in the crtlist_entry */
2908 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2909 if (ckchi_link->ckch_inst->crtlist_entry)
2910 LIST_INSERT(&ckchi_link->ckch_inst->crtlist_entry->ckch_inst,
2911 &ckchi_link->ckch_inst->by_crtlist_entry);
2912 }
2913
2914 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2915 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2916 __ssl_sock_load_new_ckch_instance(ckchi_link->ckch_inst);
2917 }
2918
William Lallemande0fa91f2022-08-31 14:26:49 +02002919 /* delete the old sni_ctx, the old ckch_insts
2920 * and the ckch_store. ckch_inst_free() also
2921 * manipulates the list so it's cleaner to loop
2922 * until it's empty */
2923 while (!LIST_ISEMPTY(&old_cafile_entry->ckch_inst_link)) {
2924 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2925
2926 LIST_DEL_INIT(&ckchi_link->list); /* must reinit because ckch_inst checks the list */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002927 __ckch_inst_free_locked(ckchi_link->ckch_inst);
William Lallemande0fa91f2022-08-31 14:26:49 +02002928 free(ckchi_link);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002929 }
2930
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002931 /* Remove the old cafile entry from the tree */
2932 ebmb_delete(&old_cafile_entry->node);
2933 ssl_store_delete_cafile_entry(old_cafile_entry);
2934
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002935 ctx->old_entry = ctx->new_entry = NULL;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002936 ctx->state = CACRL_ST_SUCCESS;
2937 /* fallthrough */
2938 case CACRL_ST_SUCCESS:
2939 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2940 goto yield;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002941 ctx->state = CACRL_ST_FIN;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002942 /* fallthrough */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002943 case CACRL_ST_FIN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002944 /* we achieved the transaction, we can set everything to NULL */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002945 switch (ctx->cafile_type) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002946 case CAFILE_CERT:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002947 cafile_transaction.old_cafile_entry = NULL;
2948 cafile_transaction.new_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002949 cafile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002950 break;
2951 case CAFILE_CRL:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002952 crlfile_transaction.old_crlfile_entry = NULL;
2953 crlfile_transaction.new_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002954 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002955 break;
2956 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002957 goto end;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002958
2959 case CACRL_ST_ERROR:
2960 error:
2961 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2962 if (applet_putchk(appctx, &trash) == -1)
2963 goto yield;
2964 ctx->state = CACRL_ST_FIN;
2965 break;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002966 }
2967 }
2968end:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002969 /* success: call the release function and don't come back */
2970 return 1;
2971yield:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002972 return 0; /* should come back */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002973}
2974
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002975
2976/* parsing function of 'abort ssl ca-file' */
2977static int cli_parse_abort_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2978{
2979 char *err = NULL;
2980
2981 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2982 return 1;
2983
2984 if (!*args[3])
2985 return cli_err(appctx, "'abort ssl ca-file' expects a filename\n");
2986
2987 /* The operations on the CKCH architecture are locked so we can
2988 * manipulate ckch_store and ckch_inst */
2989 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2990 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2991
2992 if (!cafile_transaction.path) {
2993 memprintf(&err, "No ongoing transaction!\n");
2994 goto error;
2995 }
2996
2997 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2998 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", cafile_transaction.path, args[3]);
2999 goto error;
3000 }
3001
3002 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3003 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
3004 cafile_transaction.new_cafile_entry = NULL;
3005 cafile_transaction.old_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02003006 cafile_transaction.path = NULL;
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01003007
3008 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3009
3010 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3011 return cli_dynmsg(appctx, LOG_NOTICE, err);
3012
3013error:
3014 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3015
3016 return cli_dynerr(appctx, err);
3017}
3018
Willy Tarreau821c3b02022-05-04 15:47:39 +02003019/* release function of the `commit ssl ca-file' command, free things and unlock the spinlock.
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003020 * It uses a commit_cacrlfile_ctx context.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003021 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003022static void cli_release_commit_cafile(struct appctx *appctx)
3023{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003024 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003025 struct cafile_entry *new_cafile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003026
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003027 /* Remove the uncommitted cafile_entry from the tree. */
3028 if (new_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003029 ebmb_delete(&new_cafile_entry->node);
3030 ssl_store_delete_cafile_entry(new_cafile_entry);
3031 }
3032 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003033 ha_free(&ctx->err);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003034}
3035
3036
Willy Tarreau821c3b02022-05-04 15:47:39 +02003037/* IO handler of details "show ssl ca-file <filename[:index]>".
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003038 * It uses a show_cafile_ctx context, and the global
3039 * cafile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003040 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003041static int cli_io_handler_show_cafile_detail(struct appctx *appctx)
3042{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003043 struct show_cafile_ctx *ctx = appctx->svcctx;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003044 struct cafile_entry *cafile_entry = ctx->cur_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003045 struct buffer *out = alloc_trash_chunk();
William Lallemand03a32e52022-04-26 18:17:15 +02003046 int i = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003047 X509 *cert;
3048 STACK_OF(X509_OBJECT) *objs;
3049 int retval = 0;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003050 int ca_index = ctx->ca_index;
3051 int show_all = ctx->show_all;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003052
3053 if (!out)
3054 goto end_no_putchk;
3055
3056 chunk_appendf(out, "Filename: ");
3057 if (cafile_entry == cafile_transaction.new_cafile_entry)
3058 chunk_appendf(out, "*");
3059 chunk_appendf(out, "%s\n", cafile_entry->path);
3060
3061 chunk_appendf(out, "Status: ");
3062 if (!cafile_entry->ca_store)
3063 chunk_appendf(out, "Empty\n");
3064 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3065 chunk_appendf(out, "Unused\n");
3066 else
3067 chunk_appendf(out, "Used\n");
3068
3069 if (!cafile_entry->ca_store)
3070 goto end;
3071
3072 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
William Lallemand03a32e52022-04-26 18:17:15 +02003073 for (i = ca_index; i < sk_X509_OBJECT_num(objs); i++) {
3074
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003075 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
3076 if (!cert)
3077 continue;
3078
William Lallemand03a32e52022-04-26 18:17:15 +02003079 /* file starts at line 1 */
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003080 chunk_appendf(out, " \nCertificate #%d:\n", i+1);
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003081 retval = show_cert_detail(cert, NULL, out);
3082 if (retval < 0)
3083 goto end_no_putchk;
William Lallemand03a32e52022-04-26 18:17:15 +02003084 else if (retval)
3085 goto yield;
3086
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003087 if (applet_putchk(appctx, out) == -1)
William Lallemand03a32e52022-04-26 18:17:15 +02003088 goto yield;
William Lallemand03a32e52022-04-26 18:17:15 +02003089
3090 if (!show_all) /* only need to dump one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003091 goto end;
3092 }
3093
3094end:
William Lallemand03a32e52022-04-26 18:17:15 +02003095 free_trash_chunk(out);
3096 return 1; /* end, don't come back */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003097
3098end_no_putchk:
3099 free_trash_chunk(out);
3100 return 1;
3101yield:
William Lallemand03a32e52022-04-26 18:17:15 +02003102 /* save the current state */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003103 ctx->ca_index = i;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003104 free_trash_chunk(out);
3105 return 0; /* should come back */
3106}
3107
3108
Willy Tarreau06305792022-05-04 15:57:30 +02003109/* parsing function for 'show ssl ca-file [cafile[:index]]'.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003110 * It prepares a show_cafile_ctx context, and checks the global
3111 * cafile_transaction under the ckch_lock (read only).
Willy Tarreau06305792022-05-04 15:57:30 +02003112 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003113static int cli_parse_show_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3114{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003115 struct show_cafile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003116 struct cafile_entry *cafile_entry;
William Lallemand03a32e52022-04-26 18:17:15 +02003117 int ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003118 char *colons;
3119 char *err = NULL;
3120
3121 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3122 return cli_err(appctx, "Can't allocate memory!\n");
3123
3124 /* The operations on the CKCH architecture are locked so we can
3125 * manipulate ckch_store and ckch_inst */
3126 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3127 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3128
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003129 ctx->show_all = 1; /* show all certificates */
3130 ctx->ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003131 /* check if there is a certificate to lookup */
3132 if (*args[3]) {
3133
3134 /* Look for an optional CA index after the CA file name */
3135 colons = strchr(args[3], ':');
3136 if (colons) {
3137 char *endptr;
3138
3139 ca_index = strtol(colons + 1, &endptr, 10);
3140 /* Indexes start at 1 */
3141 if (colons + 1 == endptr || *endptr != '\0' || ca_index <= 0) {
3142 memprintf(&err, "wrong CA index after colons in '%s'!", args[3]);
3143 goto error;
3144 }
3145 *colons = '\0';
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003146 ctx->ca_index = ca_index - 1; /* we start counting at 0 in the ca_store, but at 1 on the CLI */
3147 ctx->show_all = 0; /* show only one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003148 }
3149
3150 if (*args[3] == '*') {
3151 if (!cafile_transaction.new_cafile_entry)
3152 goto error;
3153
3154 cafile_entry = cafile_transaction.new_cafile_entry;
3155
3156 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3157 goto error;
3158
3159 } else {
3160 /* Get the "original" cafile_entry and not the
3161 * uncommitted one if it exists. */
3162 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CERT)
3163 goto error;
3164 }
3165
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003166 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003167 /* use the IO handler that shows details */
3168 appctx->io_handler = cli_io_handler_show_cafile_detail;
3169 }
3170
3171 return 0;
3172
3173error:
3174 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3175 if (err)
3176 return cli_dynerr(appctx, err);
3177 return cli_err(appctx, "Can't display the CA file : Not found!\n");
3178}
3179
3180
3181/* release function of the 'show ssl ca-file' command */
3182static void cli_release_show_cafile(struct appctx *appctx)
3183{
3184 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3185}
3186
3187
3188/* This function returns the number of certificates in a cafile_entry. */
3189static int get_certificate_count(struct cafile_entry *cafile_entry)
3190{
3191 int cert_count = 0;
3192 STACK_OF(X509_OBJECT) *objs;
3193
3194 if (cafile_entry && cafile_entry->ca_store) {
3195 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3196 if (objs)
3197 cert_count = sk_X509_OBJECT_num(objs);
3198 }
3199 return cert_count;
3200}
3201
3202/* IO handler of "show ssl ca-file". The command taking a specific CA file name
Willy Tarreau821c3b02022-05-04 15:47:39 +02003203 * is managed in cli_io_handler_show_cafile_detail.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003204 * It uses a show_cafile_ctx and the global cafile_transaction.new_cafile_entry
3205 * in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003206 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003207static int cli_io_handler_show_cafile(struct appctx *appctx)
3208{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003209 struct show_cafile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003210 struct buffer *trash = alloc_trash_chunk();
3211 struct ebmb_node *node;
Christopher Faulet677cb4f2022-06-03 16:25:35 +02003212 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003213
3214 if (trash == NULL)
3215 return 1;
3216
Christopher Faulet5a2154b2022-06-03 10:42:48 +02003217 if (!ctx->old_cafile_entry && cafile_transaction.old_cafile_entry) {
3218 chunk_appendf(trash, "# transaction\n");
3219 chunk_appendf(trash, "*%s", cafile_transaction.old_cafile_entry->path);
3220 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_transaction.new_cafile_entry));
3221 if (applet_putchk(appctx, trash) == -1)
3222 goto yield;
3223 ctx->old_cafile_entry = cafile_transaction.new_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003224 }
3225
3226 /* First time in this io_handler. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003227 if (!ctx->cur_cafile_entry) {
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003228 chunk_appendf(trash, "# filename\n");
3229 node = ebmb_first(&cafile_tree);
3230 } else {
3231 /* We yielded during a previous call. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003232 node = &ctx->cur_cafile_entry->node;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003233 }
3234
3235 while (node) {
3236 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3237 if (cafile_entry->type == CAFILE_CERT) {
3238 chunk_appendf(trash, "%s", cafile_entry->path);
3239
3240 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_entry));
3241 }
3242
3243 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003244 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003245 goto yield;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003246 }
3247
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003248 ctx->cur_cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003249 free_trash_chunk(trash);
3250 return 1;
3251yield:
3252
3253 free_trash_chunk(trash);
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003254 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003255 return 0; /* should come back */
3256}
3257
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003258/* parsing function of 'del ssl ca-file' */
3259static int cli_parse_del_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3260{
3261 struct cafile_entry *cafile_entry;
3262 char *err = NULL;
3263 char *filename;
3264
3265 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3266 return 1;
3267
3268 if (!*args[3])
3269 return cli_err(appctx, "'del ssl ca-file' expects a CA file name\n");
3270
3271 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3272 return cli_err(appctx, "Can't delete the CA file!\nOperations on certificates are currently locked!\n");
3273
3274 filename = args[3];
3275
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003276 if (cafile_transaction.path && strcmp(cafile_transaction.path, filename) == 0) {
3277 memprintf(&err, "ongoing transaction for the CA file '%s'", filename);
3278 goto error;
3279 }
3280
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003281 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3282 if (!cafile_entry) {
3283 memprintf(&err, "CA file '%s' doesn't exist!\n", filename);
3284 goto error;
3285 }
3286
3287 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3288 memprintf(&err, "CA file '%s' in use, can't be deleted!\n", filename);
3289 goto error;
3290 }
3291
3292 /* Remove the cafile_entry from the tree */
3293 ebmb_delete(&cafile_entry->node);
3294 ssl_store_delete_cafile_entry(cafile_entry);
3295
3296 memprintf(&err, "CA file '%s' deleted!\n", filename);
3297
3298 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3299 return cli_dynmsg(appctx, LOG_NOTICE, err);
3300
3301error:
3302 memprintf(&err, "Can't remove the CA file: %s\n", err ? err : "");
3303 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3304 return cli_dynerr(appctx, err);
3305}
3306
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003307/* parsing function of 'new ssl crl-file' */
3308static int cli_parse_new_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3309{
3310 struct cafile_entry *cafile_entry;
3311 char *err = NULL;
3312 char *path;
3313
3314 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3315 return 1;
3316
3317 if (!*args[3])
3318 return cli_err(appctx, "'new ssl crl-file' expects a filename\n");
3319
3320 path = args[3];
3321
3322 /* The operations on the CKCH architecture are locked so we can
3323 * manipulate ckch_store and ckch_inst */
3324 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003325 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 +02003326
3327 cafile_entry = ssl_store_get_cafile_entry(path, 0);
3328 if (cafile_entry) {
3329 memprintf(&err, "CRL file '%s' already exists!\n", path);
3330 goto error;
3331 }
3332
3333 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CRL);
3334 if (!cafile_entry) {
3335 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3336 goto error;
3337 }
3338
3339 /* Add the newly created cafile_entry to the tree so that
3340 * any new ckch instance created from now can use it. */
3341 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
3342 goto error;
3343
3344 memprintf(&err, "New CRL file created '%s'!\n", path);
3345
3346 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3347 return cli_dynmsg(appctx, LOG_NOTICE, err);
3348error:
3349 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3350 return cli_dynerr(appctx, err);
3351}
3352
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003353/* Parsing function of `set ssl crl-file` */
3354static int cli_parse_set_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3355{
Christopher Faulet1f90f332022-06-03 16:34:30 +02003356 struct cafile_entry *old_crlfile_entry = NULL;
3357 struct cafile_entry *new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003358 char *err = NULL;
3359 int errcode = 0;
3360 struct buffer *buf;
3361
3362 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3363 return 1;
3364
3365 if (!*args[3] || !payload)
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003366 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 +02003367
3368 /* The operations on the CKCH architecture are locked so we can
3369 * manipulate ckch_store and ckch_inst */
3370 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3371 return cli_err(appctx, "Can't update the CRL file!\nOperations on certificates are currently locked!\n");
3372
3373 if ((buf = alloc_trash_chunk()) == NULL) {
3374 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3375 errcode |= ERR_ALERT | ERR_FATAL;
3376 goto end;
3377 }
3378
3379 if (!chunk_strcpy(buf, args[3])) {
3380 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3381 errcode |= ERR_ALERT | ERR_FATAL;
3382 goto end;
3383 }
3384
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003385 old_crlfile_entry = NULL;
3386 new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003387
3388 /* if there is an ongoing transaction */
3389 if (crlfile_transaction.path) {
3390 /* if there is an ongoing transaction, check if this is the same file */
3391 if (strcmp(crlfile_transaction.path, buf->area) != 0) {
3392 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, buf->area);
3393 errcode |= ERR_ALERT | ERR_FATAL;
3394 goto end;
3395 }
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003396 old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003397 }
3398 else {
3399 /* lookup for the certificate in the tree */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003400 old_crlfile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003401 }
3402
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003403 if (!old_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003404 memprintf(&err, "%sCan't replace a CRL file which is not referenced by the configuration!\n",
3405 err ? err : "");
3406 errcode |= ERR_ALERT | ERR_FATAL;
3407 goto end;
3408 }
3409
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003410 /* Create a new cafile_entry without adding it to the cafile tree. */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003411 new_crlfile_entry = ssl_store_create_cafile_entry(old_crlfile_entry->path, NULL, CAFILE_CRL);
3412 if (!new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003413 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3414 errcode |= ERR_ALERT | ERR_FATAL;
3415 goto end;
3416 }
3417
3418 /* Fill the new entry with the new CRL. */
William Lallemandd4774d32022-07-29 17:08:02 +02003419 if (ssl_store_load_ca_from_buf(new_crlfile_entry, payload, 0)) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003420 memprintf(&err, "%sInvalid payload\n", err ? err : "");
3421 errcode |= ERR_ALERT | ERR_FATAL;
3422 goto end;
3423 }
3424
3425 /* we succeed, we can save the crl in the transaction */
3426
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003427 /* if there wasn't a transaction, update the old CRL */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003428 if (!crlfile_transaction.old_crlfile_entry) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003429 crlfile_transaction.old_crlfile_entry = old_crlfile_entry;
3430 crlfile_transaction.path = old_crlfile_entry->path;
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003431 err = memprintf(&err, "transaction created for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003432 } else {
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003433 err = memprintf(&err, "transaction updated for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003434 }
3435
3436 /* free the previous CRL file if there was a transaction */
3437 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3438
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003439 crlfile_transaction.new_crlfile_entry = new_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003440
3441 /* creates the SNI ctxs later in the IO handler */
3442
3443end:
3444 free_trash_chunk(buf);
3445
3446 if (errcode & ERR_CODE) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003447 ssl_store_delete_cafile_entry(new_crlfile_entry);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003448 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3449 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
3450 } else {
3451
3452 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3453 return cli_dynmsg(appctx, LOG_NOTICE, err);
3454 }
3455}
3456
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003457/* Parsing function of 'commit ssl crl-file'.
3458 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl ca-file".
3459 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003460static int cli_parse_commit_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3461{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003462 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003463 char *err = NULL;
3464
3465 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3466 return 1;
3467
3468 if (!*args[3])
3469 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
3470
3471 /* The operations on the CKCH architecture are locked so we can
3472 * manipulate ckch_store and ckch_inst */
3473 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3474 return cli_err(appctx, "Can't commit the CRL file!\nOperations on certificates are currently locked!\n");
3475
3476 if (!crlfile_transaction.path) {
3477 memprintf(&err, "No ongoing transaction! !\n");
3478 goto error;
3479 }
3480
3481 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3482 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, args[3]);
3483 goto error;
3484 }
3485 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02003486 ctx->state = CACRL_ST_INIT;
Christopher Fauletf814c4a2022-06-03 11:32:05 +02003487 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003488 ctx->old_entry = crlfile_transaction.old_crlfile_entry;
3489 ctx->new_entry = crlfile_transaction.new_crlfile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003490 ctx->cafile_type = CAFILE_CRL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003491
3492 return 0;
3493
3494error:
3495
3496 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3497 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
3498
3499 return cli_dynerr(appctx, err);
3500}
3501
3502
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003503/* release function of the `commit ssl crl-file' command, free things and unlock the spinlock.
3504 * it uses a commit_cacrlfile_ctx that's the same as for "commit ssl ca-file".
3505 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003506static void cli_release_commit_crlfile(struct appctx *appctx)
3507{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003508 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003509 struct cafile_entry *new_crlfile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003510
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003511 /* Remove the uncommitted cafile_entry from the tree. */
3512 if (new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003513 ebmb_delete(&new_crlfile_entry->node);
3514 ssl_store_delete_cafile_entry(new_crlfile_entry);
3515 }
3516 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003517 ha_free(&ctx->err);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003518}
3519
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003520/* parsing function of 'del ssl crl-file' */
3521static int cli_parse_del_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3522{
3523 struct cafile_entry *cafile_entry;
3524 char *err = NULL;
3525 char *filename;
3526
3527 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3528 return 1;
3529
3530 if (!*args[3])
3531 return cli_err(appctx, "'del ssl crl-file' expects a CRL file name\n");
3532
3533 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3534 return cli_err(appctx, "Can't delete the CRL file!\nOperations on certificates are currently locked!\n");
3535
3536 filename = args[3];
3537
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003538 if (crlfile_transaction.path && strcmp(crlfile_transaction.path, filename) == 0) {
3539 memprintf(&err, "ongoing transaction for the CRL file '%s'", filename);
3540 goto error;
3541 }
3542
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003543 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3544 if (!cafile_entry) {
3545 memprintf(&err, "CRL file '%s' doesn't exist!\n", filename);
3546 goto error;
3547 }
3548 if (cafile_entry->type != CAFILE_CRL) {
3549 memprintf(&err, "'del ssl crl-file' does not work on CA files!\n");
3550 goto error;
3551 }
3552
3553 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3554 memprintf(&err, "CRL file '%s' in use, can't be deleted!\n", filename);
3555 goto error;
3556 }
3557
3558 /* Remove the cafile_entry from the tree */
3559 ebmb_delete(&cafile_entry->node);
3560 ssl_store_delete_cafile_entry(cafile_entry);
3561
3562 memprintf(&err, "CRL file '%s' deleted!\n", filename);
3563
3564 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3565 return cli_dynmsg(appctx, LOG_NOTICE, err);
3566
3567error:
3568 memprintf(&err, "Can't remove the CRL file: %s\n", err ? err : "");
3569 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3570 return cli_dynerr(appctx, err);
3571}
3572
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003573/* parsing function of 'abort ssl crl-file' */
3574static int cli_parse_abort_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3575{
3576 char *err = NULL;
3577
3578 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3579 return 1;
3580
3581 if (!*args[3])
3582 return cli_err(appctx, "'abort ssl crl-file' expects a filename\n");
3583
3584 /* The operations on the CKCH architecture are locked so we can
3585 * manipulate ckch_store and ckch_inst */
3586 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3587 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
3588
3589 if (!crlfile_transaction.path) {
3590 memprintf(&err, "No ongoing transaction!\n");
3591 goto error;
3592 }
3593
3594 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3595 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", crlfile_transaction.path, args[3]);
3596 goto error;
3597 }
3598
3599 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3600 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3601 crlfile_transaction.new_crlfile_entry = NULL;
3602 crlfile_transaction.old_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02003603 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003604
3605 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3606
3607 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3608 return cli_dynmsg(appctx, LOG_NOTICE, err);
3609
3610error:
3611 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3612
3613 return cli_dynerr(appctx, err);
3614}
3615
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003616
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003617/*
3618 * Display a Certificate Resignation List's information.
3619 * The information displayed is inspired by the output of 'openssl crl -in
3620 * crl.pem -text'.
3621 * Returns 0 in case of success.
3622 */
3623static int show_crl_detail(X509_CRL *crl, struct buffer *out)
3624{
3625 BIO *bio = NULL;
3626 struct buffer *tmp = alloc_trash_chunk();
3627 long version;
3628 X509_NAME *issuer;
3629 int write = -1;
3630 STACK_OF(X509_REVOKED) *rev = NULL;
3631 X509_REVOKED *rev_entry = NULL;
3632 int i;
3633
3634 if (!tmp)
3635 return -1;
3636
3637 if ((bio = BIO_new(BIO_s_mem())) == NULL)
3638 goto end;
3639
3640 /* Version (as displayed by 'openssl crl') */
3641 version = X509_CRL_get_version(crl);
3642 chunk_appendf(out, "Version %ld\n", version + 1);
3643
3644 /* Signature Algorithm */
3645 chunk_appendf(out, "Signature Algorithm: %s\n", OBJ_nid2ln(X509_CRL_get_signature_nid(crl)));
3646
3647 /* Issuer */
3648 chunk_appendf(out, "Issuer: ");
3649 if ((issuer = X509_CRL_get_issuer(crl)) == NULL)
3650 goto end;
3651 if ((ssl_sock_get_dn_oneline(issuer, tmp)) == -1)
3652 goto end;
3653 *(tmp->area + tmp->data) = '\0';
3654 chunk_appendf(out, "%s\n", tmp->area);
3655
3656 /* Last Update */
3657 chunk_appendf(out, "Last Update: ");
3658 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003659 if (BIO_reset(bio) == -1)
3660 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003661 if (ASN1_TIME_print(bio, X509_CRL_get0_lastUpdate(crl)) == 0)
3662 goto end;
3663 write = BIO_read(bio, tmp->area, tmp->size-1);
3664 tmp->area[write] = '\0';
3665 chunk_appendf(out, "%s\n", tmp->area);
3666
3667
3668 /* Next Update */
3669 chunk_appendf(out, "Next Update: ");
3670 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003671 if (BIO_reset(bio) == -1)
3672 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003673 if (ASN1_TIME_print(bio, X509_CRL_get0_nextUpdate(crl)) == 0)
3674 goto end;
3675 write = BIO_read(bio, tmp->area, tmp->size-1);
3676 tmp->area[write] = '\0';
3677 chunk_appendf(out, "%s\n", tmp->area);
3678
3679
3680 /* Revoked Certificates */
3681 rev = X509_CRL_get_REVOKED(crl);
3682 if (sk_X509_REVOKED_num(rev) > 0)
3683 chunk_appendf(out, "Revoked Certificates:\n");
3684 else
3685 chunk_appendf(out, "No Revoked Certificates.\n");
3686
3687 for (i = 0; i < sk_X509_REVOKED_num(rev); i++) {
3688 rev_entry = sk_X509_REVOKED_value(rev, i);
3689
3690 /* Serial Number and Revocation Date */
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003691 if (BIO_reset(bio) == -1)
3692 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003693 BIO_printf(bio , " Serial Number: ");
Remi Tricot-Le Breton18c7d832021-05-17 18:38:34 +02003694 i2a_ASN1_INTEGER(bio, (ASN1_INTEGER*)X509_REVOKED_get0_serialNumber(rev_entry));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003695 BIO_printf(bio, "\n Revocation Date: ");
Remi Tricot-Le Bretona6b27842021-05-18 10:06:00 +02003696 if (ASN1_TIME_print(bio, X509_REVOKED_get0_revocationDate(rev_entry)) == 0)
3697 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003698 BIO_printf(bio, "\n");
3699
3700 write = BIO_read(bio, tmp->area, tmp->size-1);
3701 tmp->area[write] = '\0';
3702 chunk_appendf(out, "%s", tmp->area);
3703 }
3704
3705end:
3706 free_trash_chunk(tmp);
3707 if (bio)
3708 BIO_free(bio);
3709
3710 return 0;
3711}
3712
Willy Tarreau821c3b02022-05-04 15:47:39 +02003713/* IO handler of details "show ssl crl-file <filename[:index]>".
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003714 * It uses show_crlfile_ctx and the global
3715 * crlfile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003716 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003717static int cli_io_handler_show_crlfile_detail(struct appctx *appctx)
3718{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003719 struct show_crlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003720 struct cafile_entry *cafile_entry = ctx->cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003721 struct buffer *out = alloc_trash_chunk();
3722 int i;
3723 X509_CRL *crl;
3724 STACK_OF(X509_OBJECT) *objs;
3725 int retval = 0;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003726 int index = ctx->index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003727
3728 if (!out)
3729 goto end_no_putchk;
3730
3731 chunk_appendf(out, "Filename: ");
3732 if (cafile_entry == crlfile_transaction.new_crlfile_entry)
3733 chunk_appendf(out, "*");
3734 chunk_appendf(out, "%s\n", cafile_entry->path);
3735
3736 chunk_appendf(out, "Status: ");
3737 if (!cafile_entry->ca_store)
3738 chunk_appendf(out, "Empty\n");
3739 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3740 chunk_appendf(out, "Unused\n");
3741 else
3742 chunk_appendf(out, "Used\n");
3743
3744 if (!cafile_entry->ca_store)
3745 goto end;
3746
3747 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3748 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
3749 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
3750 if (!crl)
3751 continue;
3752
3753 /* CRL indexes start at 1 on the CLI output. */
3754 if (index && index-1 != i)
3755 continue;
3756
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003757 chunk_appendf(out, " \nCertificate Revocation List #%d:\n", i+1);
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003758 retval = show_crl_detail(crl, out);
3759 if (retval < 0)
3760 goto end_no_putchk;
3761 else if (retval || index)
3762 goto end;
3763 }
3764
3765end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003766 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003767 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003768
3769end_no_putchk:
3770 free_trash_chunk(out);
3771 return 1;
3772yield:
3773 free_trash_chunk(out);
3774 return 0; /* should come back */
3775}
3776
Willy Tarreau821c3b02022-05-04 15:47:39 +02003777/* parsing function for 'show ssl crl-file [crlfile[:index]]'.
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003778 * It sets the context to a show_crlfile_ctx, and the global
Willy Tarreau821c3b02022-05-04 15:47:39 +02003779 * cafile_transaction.new_crlfile_entry under the ckch_lock.
3780 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003781static int cli_parse_show_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3782{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003783 struct show_crlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003784 struct cafile_entry *cafile_entry;
3785 long index = 0;
3786 char *colons;
3787 char *err = NULL;
3788
3789 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3790 return cli_err(appctx, "Can't allocate memory!\n");
3791
3792 /* The operations on the CKCH architecture are locked so we can
3793 * manipulate ckch_store and ckch_inst */
3794 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3795 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3796
3797 /* check if there is a certificate to lookup */
3798 if (*args[3]) {
3799
3800 /* Look for an optional index after the CRL file name */
3801 colons = strchr(args[3], ':');
3802 if (colons) {
3803 char *endptr;
3804
3805 index = strtol(colons + 1, &endptr, 10);
3806 /* Indexes start at 1 */
3807 if (colons + 1 == endptr || *endptr != '\0' || index <= 0) {
3808 memprintf(&err, "wrong CRL index after colons in '%s'!", args[3]);
3809 goto error;
3810 }
3811 *colons = '\0';
3812 }
3813
3814 if (*args[3] == '*') {
3815 if (!crlfile_transaction.new_crlfile_entry)
3816 goto error;
3817
3818 cafile_entry = crlfile_transaction.new_crlfile_entry;
3819
3820 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3821 goto error;
3822
3823 } else {
3824 /* Get the "original" cafile_entry and not the
3825 * uncommitted one if it exists. */
3826 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CRL)
3827 goto error;
3828 }
3829
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003830 ctx->cafile_entry = cafile_entry;
3831 ctx->index = index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003832 /* use the IO handler that shows details */
3833 appctx->io_handler = cli_io_handler_show_crlfile_detail;
3834 }
3835
3836 return 0;
3837
3838error:
3839 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3840 if (err)
3841 return cli_dynerr(appctx, err);
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003842 return cli_err(appctx, "Can't display the CRL file : Not found!\n");
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003843}
3844
3845/* IO handler of "show ssl crl-file". The command taking a specific CRL file name
3846 * is managed in cli_io_handler_show_crlfile_detail. */
3847static int cli_io_handler_show_crlfile(struct appctx *appctx)
3848{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003849 struct show_crlfile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003850 struct buffer *trash = alloc_trash_chunk();
3851 struct ebmb_node *node;
Christopher Faulet88041b32022-06-03 16:26:56 +02003852 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003853
3854 if (trash == NULL)
3855 return 1;
3856
Christopher Faulet9a99e542022-06-03 10:32:18 +02003857 if (!ctx->old_crlfile_entry && crlfile_transaction.old_crlfile_entry) {
3858 chunk_appendf(trash, "# transaction\n");
3859 chunk_appendf(trash, "*%s\n", crlfile_transaction.old_crlfile_entry->path);
3860 if (applet_putchk(appctx, trash) == -1)
3861 goto yield;
3862 ctx->old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003863 }
3864
3865 /* First time in this io_handler. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003866 if (!ctx->cafile_entry) {
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003867 chunk_appendf(trash, "# filename\n");
3868 node = ebmb_first(&cafile_tree);
3869 } else {
3870 /* We yielded during a previous call. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003871 node = &ctx->cafile_entry->node;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003872 }
3873
3874 while (node) {
3875 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3876 if (cafile_entry->type == CAFILE_CRL) {
3877 chunk_appendf(trash, "%s\n", cafile_entry->path);
3878 }
3879
3880 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003881 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003882 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003883 }
3884
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003885 ctx->cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003886 free_trash_chunk(trash);
3887 return 1;
3888yield:
3889
3890 free_trash_chunk(trash);
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003891 ctx->cafile_entry = cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003892 return 0; /* should come back */
3893}
3894
3895
3896/* release function of the 'show ssl crl-file' command */
3897static void cli_release_show_crlfile(struct appctx *appctx)
3898{
3899 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3900}
3901
3902
William Lallemandee8530c2020-06-23 18:19:42 +02003903void ckch_deinit()
3904{
3905 struct eb_node *node, *next;
3906 struct ckch_store *store;
William Lallemandb0c48272022-04-26 15:44:53 +02003907 struct ebmb_node *canode;
William Lallemandee8530c2020-06-23 18:19:42 +02003908
William Lallemandb0c48272022-04-26 15:44:53 +02003909 /* deinit the ckch stores */
William Lallemandee8530c2020-06-23 18:19:42 +02003910 node = eb_first(&ckchs_tree);
3911 while (node) {
3912 next = eb_next(node);
3913 store = ebmb_entry(node, struct ckch_store, node);
3914 ckch_store_free(store);
3915 node = next;
3916 }
William Lallemandb0c48272022-04-26 15:44:53 +02003917
3918 /* deinit the ca-file store */
3919 canode = ebmb_first(&cafile_tree);
3920 while (canode) {
3921 struct cafile_entry *entry = NULL;
3922
3923 entry = ebmb_entry(canode, struct cafile_entry, node);
3924 canode = ebmb_next(canode);
William Lallemand946580e2022-08-29 18:36:18 +02003925 ebmb_delete(&entry->node);
William Lallemandb0c48272022-04-26 15:44:53 +02003926 ssl_store_delete_cafile_entry(entry);
3927 }
William Lallemandee8530c2020-06-23 18:19:42 +02003928}
William Lallemandda8584c2020-05-14 10:14:37 +02003929
3930/* register cli keywords */
3931static struct cli_kw_list cli_kws = {{ },{
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003932 { { "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 },
3933 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
3934 { { "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 },
3935 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
3936 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
3937 { { "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 },
3938
Amaury Denoyelleb11ad9e2021-05-21 11:01:10 +02003939 { { "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 +02003940 { { "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 +01003941 { { "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 +02003942 { { "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 +01003943 { { "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 +01003944 { { "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 +01003945 { { "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 +02003946
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003947 { { "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 +02003948 { { "set", "ssl", "crl-file", NULL }, "set ssl crl-file <crlfile> <payload> : replace a CRL file", cli_parse_set_crlfile, NULL, NULL },
3949 { { "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 +02003950 { { "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 +02003951 { { "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 +02003952 { { "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 +02003953 { { NULL }, NULL, NULL, NULL }
3954}};
3955
3956INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3957