blob: 7a6168a0196a633ea718658a9238802e67e67728 [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 */
William Lallemand52ddd992022-11-22 11:51:53 +0100168int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct ckch_data *data, char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200169{
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 */
William Lallemand52ddd992022-11-22 11:51:53 +0100211 if (data->sctl) {
212 ha_free(&data->sctl->area);
213 free(data->sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200214 }
William Lallemand52ddd992022-11-22 11:51:53 +0100215 data->sctl = sctl;
William Lallemand03c331c2020-05-13 10:10:01 +0200216 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 */
William Lallemand52ddd992022-11-22 11:51:53 +0100231int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct ckch_data *data, char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200232{
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 }
William Lallemand52ddd992022-11-22 11:51:53 +0100290 /* no error, fill data with new context, old context must be free */
291 if (data->ocsp_response) {
292 ha_free(&data->ocsp_response->area);
293 free(data->ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200294 }
William Lallemand52ddd992022-11-22 11:51:53 +0100295 data->ocsp_response = ocsp_response;
William Lallemand03c331c2020-05-13 10:10:01 +0200296 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 */
William Lallemand52ddd992022-11-22 11:51:53 +0100320int ssl_sock_load_files_into_ckch(const char *path, struct ckch_data *data, char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200321{
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 */
William Lallemand52ddd992022-11-22 11:51:53 +0100327 if (ssl_sock_load_pem_into_ckch(path, NULL, data , err) != 0) {
William Lallemand03c331c2020-05-13 10:10:01 +0200328 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
William Lallemand52ddd992022-11-22 11:51:53 +0100359 if (data->key == NULL) {
Remi Tricot-Le Breton1bad7db2022-06-07 16:29:44 +0200360 /* 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) {
William Lallemand52ddd992022-11-22 11:51:53 +0100377 if (ssl_sock_load_key_into_ckch(fp->area, NULL, data, err)) {
Remi Tricot-Le Breton1bad7db2022-06-07 16:29:44 +0200378 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
William Lallemand52ddd992022-11-22 11:51:53 +0100384 if (data->key == NULL) {
Remi Tricot-Le Breton1bad7db2022-06-07 16:29:44 +0200385 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
William Lallemand52ddd992022-11-22 11:51:53 +0100394 if (!X509_check_private_key(data->cert, data->key)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200395 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) {
William Lallemand52ddd992022-11-22 11:51:53 +0100413 if (ssl_sock_load_sctl_from_file(fp->area, NULL, data, 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) {
William Lallemand52ddd992022-11-22 11:51:53 +0100438 if (ssl_sock_load_ocsp_response_from_file(fp->area, NULL, data, 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 */
William Lallemand52ddd992022-11-22 11:51:53 +0100449 if (data->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200450 /* if no issuer was found, try to load an issuer from the .issuer */
William Lallemand52ddd992022-11-22 11:51:53 +0100451 if (!data->ocsp_issuer) {
William Lallemand03c331c2020-05-13 10:10:01 +0200452 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) {
William Lallemand52ddd992022-11-22 11:51:53 +0100462 if (ssl_sock_load_issuer_file_into_ckch(fp->area, NULL, data, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200463 ret = 1;
464 goto end;
465 }
466
William Lallemand52ddd992022-11-22 11:51:53 +0100467 if (X509_check_issued(data->ocsp_issuer, data->cert) != X509_V_OK) {
William Lallemand03c331c2020-05-13 10:10:01 +0200468 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)
William Lallemand52ddd992022-11-22 11:51:53 +0100489 ssl_sock_free_cert_key_and_chain_contents(data);
William Lallemand03c331c2020-05-13 10:10:01 +0200490
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 */
William Lallemand52ddd992022-11-22 11:51:53 +0100503int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct ckch_data *data , char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200504{
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
William Lallemand52ddd992022-11-22 11:51:53 +0100537 SWAP(data->key, key);
William Lallemand03c331c2020-05-13 10:10:01 +0200538
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 */
William Lallemand52ddd992022-11-22 11:51:53 +0100559int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct ckch_data *data , char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200560{
561 BIO *in = NULL;
562 int ret = 1;
563 X509 *ca;
564 X509 *cert = NULL;
565 EVP_PKEY *key = NULL;
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100566 HASSL_DH *dh = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200567 STACK_OF(X509) *chain = NULL;
568
569 if (buf) {
570 /* reading from a buffer */
571 in = BIO_new_mem_buf(buf, -1);
572 if (in == NULL) {
573 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
574 goto end;
575 }
576
577 } else {
578 /* reading from a file */
579 in = BIO_new(BIO_s_file());
580 if (in == NULL) {
581 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
582 goto end;
583 }
584
585 if (BIO_read_filename(in, path) <= 0) {
586 memprintf(err, "%scannot open the file '%s'.\n",
587 err && *err ? *err : "", path);
588 goto end;
589 }
590 }
591
592 /* Read Private Key */
593 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
594 /* no need to check for errors here, because the private key could be loaded later */
595
596#ifndef OPENSSL_NO_DH
597 /* Seek back to beginning of file */
598 if (BIO_reset(in) == -1) {
599 memprintf(err, "%san error occurred while reading the file '%s'.\n",
600 err && *err ? *err : "", path);
601 goto end;
602 }
603
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100604 dh = ssl_sock_get_dh_from_bio(in);
605 ERR_clear_error();
William Lallemand03c331c2020-05-13 10:10:01 +0200606 /* no need to return an error there, dh is not mandatory */
607#endif
608
609 /* Seek back to beginning of file */
610 if (BIO_reset(in) == -1) {
611 memprintf(err, "%san error occurred while reading the file '%s'.\n",
612 err && *err ? *err : "", path);
613 goto end;
614 }
615
616 /* Read Certificate */
617 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
618 if (cert == NULL) {
William Lallemanda5384522022-10-25 15:53:01 +0200619 ret = ERR_get_error();
620 memprintf(err, "%sunable to load certificate from file '%s': %s.\n",
621 err && *err ? *err : "", path, ERR_reason_error_string(ret));
William Lallemand03c331c2020-05-13 10:10:01 +0200622 goto end;
623 }
624
625 /* Look for a Certificate Chain */
626 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
627 if (chain == NULL)
628 chain = sk_X509_new_null();
629 if (!sk_X509_push(chain, ca)) {
630 X509_free(ca);
William Lallemand432cd1a2022-10-25 15:55:13 +0200631 break;
William Lallemand03c331c2020-05-13 10:10:01 +0200632 }
633 }
634
635 ret = ERR_get_error();
William Lallemand78c7a062022-11-15 17:12:03 +0100636 if (ret && !(ERR_GET_LIB(ret) == ERR_LIB_PEM && ERR_GET_REASON(ret) == PEM_R_NO_START_LINE)) {
William Lallemandf784b902022-10-25 12:31:39 +0200637 memprintf(err, "%sunable to load certificate chain from file '%s': %s\n",
638 err && *err ? *err : "", path, ERR_reason_error_string(ret));
William Lallemand03c331c2020-05-13 10:10:01 +0200639 goto end;
640 }
641
William Lallemand52ddd992022-11-22 11:51:53 +0100642 /* once it loaded the PEM, it should remove everything else in the data */
643 if (data->ocsp_response) {
644 ha_free(&data->ocsp_response->area);
645 ha_free(&data->ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200646 }
647
William Lallemand52ddd992022-11-22 11:51:53 +0100648 if (data->sctl) {
649 ha_free(&data->sctl->area);
650 ha_free(&data->sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200651 }
652
William Lallemand52ddd992022-11-22 11:51:53 +0100653 if (data->ocsp_issuer) {
654 X509_free(data->ocsp_issuer);
655 data->ocsp_issuer = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200656 }
657
William Lallemand52ddd992022-11-22 11:51:53 +0100658 /* no error, fill data with new context, old context will be free at end: */
659 SWAP(data->key, key);
660 SWAP(data->dh, dh);
661 SWAP(data->cert, cert);
662 SWAP(data->chain, chain);
William Lallemand03c331c2020-05-13 10:10:01 +0200663
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 */
William Lallemand52ddd992022-11-22 11:51:53 +0100685void ssl_sock_free_cert_key_and_chain_contents(struct ckch_data *data)
William Lallemand03c331c2020-05-13 10:10:01 +0200686{
William Lallemand52ddd992022-11-22 11:51:53 +0100687 if (!data)
William Lallemand03c331c2020-05-13 10:10:01 +0200688 return;
689
690 /* Free the certificate and set pointer to NULL */
William Lallemand52ddd992022-11-22 11:51:53 +0100691 if (data->cert)
692 X509_free(data->cert);
693 data->cert = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200694
695 /* Free the key and set pointer to NULL */
William Lallemand52ddd992022-11-22 11:51:53 +0100696 if (data->key)
697 EVP_PKEY_free(data->key);
698 data->key = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200699
700 /* Free each certificate in the chain */
William Lallemand52ddd992022-11-22 11:51:53 +0100701 if (data->chain)
702 sk_X509_pop_free(data->chain, X509_free);
703 data->chain = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200704
William Lallemand52ddd992022-11-22 11:51:53 +0100705 if (data->dh)
706 HASSL_DH_free(data->dh);
707 data->dh = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200708
William Lallemand52ddd992022-11-22 11:51:53 +0100709 if (data->sctl) {
710 ha_free(&data->sctl->area);
711 ha_free(&data->sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200712 }
713
William Lallemand52ddd992022-11-22 11:51:53 +0100714 if (data->ocsp_response) {
715 ha_free(&data->ocsp_response->area);
716 ha_free(&data->ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200717 }
718
William Lallemand52ddd992022-11-22 11:51:53 +0100719 if (data->ocsp_issuer)
720 X509_free(data->ocsp_issuer);
721 data->ocsp_issuer = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200722}
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 */
William Lallemand52ddd992022-11-22 11:51:53 +0100733struct ckch_data *ssl_sock_copy_cert_key_and_chain(struct ckch_data *src,
734 struct ckch_data *dst)
William Lallemand03c331c2020-05-13 10:10:01 +0200735{
William Lallemand6c096142021-02-23 14:45:45 +0100736 if (!src || !dst)
737 return NULL;
738
William Lallemand03c331c2020-05-13 10:10:01 +0200739 if (src->cert) {
740 dst->cert = src->cert;
741 X509_up_ref(src->cert);
742 }
743
744 if (src->key) {
745 dst->key = src->key;
746 EVP_PKEY_up_ref(src->key);
747 }
748
749 if (src->chain) {
750 dst->chain = X509_chain_up_ref(src->chain);
751 }
752
753 if (src->dh) {
Uriah Pollock3cbf09e2022-11-23 16:41:25 +0100754#ifndef USE_OPENSSL_WOLFSSL
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100755 HASSL_DH_up_ref(src->dh);
William Lallemand03c331c2020-05-13 10:10:01 +0200756 dst->dh = src->dh;
Uriah Pollock3cbf09e2022-11-23 16:41:25 +0100757#else
758 dst->dh = wolfSSL_DH_dup(src->dh);
759 if (!dst->dh)
760 goto error;
761#endif
William Lallemand03c331c2020-05-13 10:10:01 +0200762 }
763
764 if (src->sctl) {
765 struct buffer *sctl;
766
767 sctl = calloc(1, sizeof(*sctl));
768 if (!chunk_dup(sctl, src->sctl)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100769 ha_free(&sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200770 goto error;
771 }
772 dst->sctl = sctl;
773 }
774
775 if (src->ocsp_response) {
776 struct buffer *ocsp_response;
777
778 ocsp_response = calloc(1, sizeof(*ocsp_response));
779 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100780 ha_free(&ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200781 goto error;
782 }
783 dst->ocsp_response = ocsp_response;
784 }
785
786 if (src->ocsp_issuer) {
787 X509_up_ref(src->ocsp_issuer);
788 dst->ocsp_issuer = src->ocsp_issuer;
789 }
790
791 return dst;
792
793error:
794
795 /* free everything */
796 ssl_sock_free_cert_key_and_chain_contents(dst);
797
798 return NULL;
799}
800
801/*
802 * return 0 on success or != 0 on failure
803 */
William Lallemand52ddd992022-11-22 11:51:53 +0100804int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct ckch_data *data, char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200805{
806 int ret = 1;
807 BIO *in = NULL;
808 X509 *issuer;
809
810 if (buf) {
811 /* reading from a buffer */
812 in = BIO_new_mem_buf(buf, -1);
813 if (in == NULL) {
814 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
815 goto end;
816 }
817
818 } else {
819 /* reading from a file */
820 in = BIO_new(BIO_s_file());
821 if (in == NULL)
822 goto end;
823
824 if (BIO_read_filename(in, path) <= 0)
825 goto end;
826 }
827
828 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
829 if (!issuer) {
830 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
831 err && *err ? *err : "", path);
832 goto end;
833 }
William Lallemand52ddd992022-11-22 11:51:53 +0100834 /* no error, fill data with new context, old context must be free */
835 if (data->ocsp_issuer)
836 X509_free(data->ocsp_issuer);
837 data->ocsp_issuer = issuer;
William Lallemand03c331c2020-05-13 10:10:01 +0200838 ret = 0;
839
840end:
841
842 ERR_clear_error();
843 if (in)
844 BIO_free(in);
845
846 return ret;
847}
848
849/******************** ckch_store functions ***********************************
850 * The ckch_store is a structure used to cache and index the SSL files used in
851 * configuration
852 */
853
854/*
855 * Free a ckch_store, its ckch, its instances and remove it from the ebtree
856 */
857void ckch_store_free(struct ckch_store *store)
858{
859 struct ckch_inst *inst, *inst_s;
860
861 if (!store)
862 return;
863
William Lallemand52ddd992022-11-22 11:51:53 +0100864 ssl_sock_free_cert_key_and_chain_contents(store->data);
William Lallemand03c331c2020-05-13 10:10:01 +0200865
William Lallemand52ddd992022-11-22 11:51:53 +0100866 ha_free(&store->data);
William Lallemand03c331c2020-05-13 10:10:01 +0200867
868 list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) {
869 ckch_inst_free(inst);
870 }
871 ebmb_delete(&store->node);
872 free(store);
873}
874
875/*
876 * create and initialize a ckch_store
877 * <path> is the key name
878 * <nmemb> is the number of store->ckch objects to allocate
879 *
880 * Return a ckch_store or NULL upon failure.
881 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200882struct ckch_store *ckch_store_new(const char *filename)
William Lallemand03c331c2020-05-13 10:10:01 +0200883{
884 struct ckch_store *store;
885 int pathlen;
886
887 pathlen = strlen(filename);
888 store = calloc(1, sizeof(*store) + pathlen + 1);
889 if (!store)
890 return NULL;
891
William Lallemand03c331c2020-05-13 10:10:01 +0200892 memcpy(store->path, filename, pathlen + 1);
893
894 LIST_INIT(&store->ckch_inst);
895 LIST_INIT(&store->crtlist_entry);
896
William Lallemand52ddd992022-11-22 11:51:53 +0100897 store->data = calloc(1, sizeof(*store->data));
898 if (!store->data)
William Lallemand03c331c2020-05-13 10:10:01 +0200899 goto error;
900
901 return store;
902error:
903 ckch_store_free(store);
904 return NULL;
905}
906
907/* allocate and duplicate a ckch_store
908 * Return a new ckch_store or NULL */
909struct ckch_store *ckchs_dup(const struct ckch_store *src)
910{
911 struct ckch_store *dst;
912
William Lallemand6c096142021-02-23 14:45:45 +0100913 if (!src)
914 return NULL;
915
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200916 dst = ckch_store_new(src->path);
Eric Salama6ac61e32021-02-23 16:50:57 +0100917 if (!dst)
918 return NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200919
William Lallemand52ddd992022-11-22 11:51:53 +0100920 if (!ssl_sock_copy_cert_key_and_chain(src->data, dst->data))
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200921 goto error;
William Lallemand03c331c2020-05-13 10:10:01 +0200922
923 return dst;
924
925error:
926 ckch_store_free(dst);
927
928 return NULL;
929}
930
931/*
932 * lookup a path into the ckchs tree.
933 */
934struct ckch_store *ckchs_lookup(char *path)
935{
936 struct ebmb_node *eb;
937
938 eb = ebst_lookup(&ckchs_tree, path);
939 if (!eb)
940 return NULL;
941
942 return ebmb_entry(eb, struct ckch_store, node);
943}
944
945/*
946 * This function allocate a ckch_store and populate it with certificates from files.
947 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200948struct ckch_store *ckchs_load_cert_file(char *path, char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200949{
950 struct ckch_store *ckchs;
951
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200952 ckchs = ckch_store_new(path);
William Lallemand03c331c2020-05-13 10:10:01 +0200953 if (!ckchs) {
954 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
955 goto end;
956 }
William Lallemand03c331c2020-05-13 10:10:01 +0200957
William Lallemand52ddd992022-11-22 11:51:53 +0100958 if (ssl_sock_load_files_into_ckch(path, ckchs->data, err) == 1)
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200959 goto end;
William Lallemand03c331c2020-05-13 10:10:01 +0200960
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200961 /* insert into the ckchs tree */
962 memcpy(ckchs->path, path, strlen(path) + 1);
963 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand03c331c2020-05-13 10:10:01 +0200964 return ckchs;
965
966end:
967 ckch_store_free(ckchs);
968
969 return NULL;
970}
971
William Lallemandfa1d8b42020-05-13 15:46:10 +0200972
973/******************** ckch_inst functions ******************************/
974
975/* unlink a ckch_inst, free all SNIs, free the ckch_inst */
976/* The caller must use the lock of the bind_conf if used with inserted SNIs */
977void ckch_inst_free(struct ckch_inst *inst)
978{
979 struct sni_ctx *sni, *sni_s;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100980 struct ckch_inst_link_ref *link_ref, *link_ref_s;
William Lallemandfa1d8b42020-05-13 15:46:10 +0200981
982 if (inst == NULL)
983 return;
984
985 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
986 SSL_CTX_free(sni->ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +0200987 LIST_DELETE(&sni->by_ckch_inst);
William Lallemandfa1d8b42020-05-13 15:46:10 +0200988 ebmb_delete(&sni->name);
989 free(sni);
990 }
Remi Tricot-Le Bretonf3eedfe2021-01-25 17:19:44 +0100991 SSL_CTX_free(inst->ctx);
992 inst->ctx = NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +0200993 LIST_DELETE(&inst->by_ckchs);
994 LIST_DELETE(&inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100995
William Lallemande0fa91f2022-08-31 14:26:49 +0200996 /* Free the cafile_link_refs list */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100997 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
William Lallemande0fa91f2022-08-31 14:26:49 +0200998 if (link_ref->link && LIST_INLIST(&link_ref->link->list)) {
999 /* Try to detach and free the ckch_inst_link only if it
1000 * was attached, this way it can be used to loop from
1001 * the caller */
1002 LIST_DEL_INIT(&link_ref->link->list);
1003 ha_free(&link_ref->link);
1004 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001005 LIST_DELETE(&link_ref->list);
1006 free(link_ref);
1007 }
1008
William Lallemandfa1d8b42020-05-13 15:46:10 +02001009 free(inst);
1010}
1011
1012/* Alloc and init a ckch_inst */
1013struct ckch_inst *ckch_inst_new()
1014{
1015 struct ckch_inst *ckch_inst;
1016
1017 ckch_inst = calloc(1, sizeof *ckch_inst);
1018 if (!ckch_inst)
1019 return NULL;
1020
1021 LIST_INIT(&ckch_inst->sni_ctx);
1022 LIST_INIT(&ckch_inst->by_ckchs);
1023 LIST_INIT(&ckch_inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001024 LIST_INIT(&ckch_inst->cafile_link_refs);
William Lallemandfa1d8b42020-05-13 15:46:10 +02001025
1026 return ckch_inst;
1027}
1028
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001029
1030/******************** ssl_store functions ******************************/
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001031struct eb_root cafile_tree = EB_ROOT;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001032
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001033/*
1034 * Returns the cafile_entry found in the cafile_tree indexed by the path 'path'.
1035 * If 'oldest_entry' is 1, returns the "original" cafile_entry (since
1036 * during a set cafile/commit cafile cycle there might be two entries for any
1037 * given path, the original one and the new one set via the CLI but not
1038 * committed yet).
1039 */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001040struct cafile_entry *ssl_store_get_cafile_entry(char *path, int oldest_entry)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001041{
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001042 struct cafile_entry *ca_e = NULL;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001043 struct ebmb_node *eb;
1044
1045 eb = ebst_lookup(&cafile_tree, path);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001046 while (eb) {
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001047 ca_e = ebmb_entry(eb, struct cafile_entry, node);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001048 /* The ebst_lookup in a tree that has duplicates returns the
1049 * oldest entry first. If we want the latest entry, we need to
1050 * iterate over all the duplicates until we find the last one
1051 * (in our case there should never be more than two entries for
1052 * any given path). */
1053 if (oldest_entry)
1054 return ca_e;
1055 eb = ebmb_next_dup(eb);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001056 }
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001057 return ca_e;
1058}
1059
Remi Tricot-Le Breton38c999b2021-02-23 16:28:43 +01001060int ssl_store_add_uncommitted_cafile_entry(struct cafile_entry *entry)
1061{
1062 return (ebst_insert(&cafile_tree, &entry->node) != &entry->node);
1063}
1064
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001065X509_STORE* ssl_store_get0_locations_file(char *path)
1066{
1067 struct cafile_entry *ca_e = ssl_store_get_cafile_entry(path, 0);
1068
1069 if (ca_e)
1070 return ca_e->ca_store;
1071
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001072 return NULL;
1073}
1074
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001075/* Create a cafile_entry object, without adding it to the cafile_tree. */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001076struct cafile_entry *ssl_store_create_cafile_entry(char *path, X509_STORE *store, enum cafile_type type)
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001077{
1078 struct cafile_entry *ca_e;
1079 int pathlen;
1080
1081 pathlen = strlen(path);
1082
1083 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
1084 if (ca_e) {
1085 memcpy(ca_e->path, path, pathlen + 1);
1086 ca_e->ca_store = store;
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001087 ca_e->type = type;
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001088 LIST_INIT(&ca_e->ckch_inst_link);
1089 }
1090 return ca_e;
1091}
1092
William Lallemand62c0b992022-07-29 17:50:58 +02001093
1094/* Duplicate a cafile_entry
1095 * Allocate the X509_STORE and copy the X509 and CRL inside.
1096 *
1097 * Return the newly allocated cafile_entry or NULL.
1098 *
1099 */
1100struct cafile_entry *ssl_store_dup_cafile_entry(struct cafile_entry *src)
1101{
1102 struct cafile_entry *dst = NULL;
1103 X509_STORE *store = NULL;
1104 STACK_OF(X509_OBJECT) *objs;
1105 int i;
1106
1107 if (!src)
1108 return NULL;
1109
1110 if (src->ca_store) {
1111 /* if there was a store in the src, copy it */
1112 store = X509_STORE_new();
1113 if (!store)
1114 goto err;
1115
1116 objs = X509_STORE_get0_objects(src->ca_store);
1117 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
1118 X509 *cert;
1119 X509_CRL *crl;
1120
1121 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
1122 if (cert) {
1123 if (X509_STORE_add_cert(store, cert) == 0) {
1124 /* only exits on error if the error is not about duplicate certificates */
1125 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1126 goto err;
1127 }
1128 }
1129
1130 }
1131 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
1132 if (crl) {
1133 if (X509_STORE_add_crl(store, crl) == 0) {
1134 /* only exits on error if the error is not about duplicate certificates */
1135 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1136 goto err;
1137 }
1138 }
1139
1140 }
1141 }
1142 }
1143 dst = ssl_store_create_cafile_entry(src->path, store, src->type);
1144
1145 return dst;
1146
1147err:
1148 X509_STORE_free(store);
1149 ha_free(&dst);
1150
1151 return NULL;
1152}
1153
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001154/* Delete a cafile_entry. The caller is responsible from removing this entry
1155 * from the cafile_tree first if is was previously added into it. */
1156void ssl_store_delete_cafile_entry(struct cafile_entry *ca_e)
1157{
1158 struct ckch_inst_link *link, *link_s;
1159 if (!ca_e)
1160 return;
1161
1162 X509_STORE_free(ca_e->ca_store);
1163
1164 list_for_each_entry_safe(link, link_s, &ca_e->ckch_inst_link, list) {
1165 struct ckch_inst *inst = link->ckch_inst;
1166 struct ckch_inst_link_ref *link_ref, *link_ref_s;
1167 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
1168 if (link_ref->link == link) {
1169 LIST_DELETE(&link_ref->list);
1170 free(link_ref);
1171 break;
1172 }
1173 }
1174 LIST_DELETE(&link->list);
1175 free(link);
1176 }
1177
1178 free(ca_e);
1179}
1180
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001181/*
William Lallemandd4774d32022-07-29 17:08:02 +02001182 * Fill a cafile_entry <ca_e> X509_STORE ca_e->store out of a buffer <cert_buf>
1183 * instead of out of a file. The <append> field should be set to 1 if you want
1184 * to keep the existing X509_STORE and append data to it.
1185 *
1186 * This function is used when the "set ssl ca-file" cli command is used.
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001187 * It can parse CERTIFICATE sections as well as CRL ones.
1188 * Returns 0 in case of success, 1 otherwise.
William Lallemandd4774d32022-07-29 17:08:02 +02001189 *
1190 * /!\ Warning: If there was an error the X509_STORE could have been modified so it's
1191 * better to not use it after a return 1.
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001192 */
William Lallemandd4774d32022-07-29 17:08:02 +02001193int ssl_store_load_ca_from_buf(struct cafile_entry *ca_e, char *cert_buf, int append)
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001194{
William Lallemandd4774d32022-07-29 17:08:02 +02001195 BIO *bio = NULL;
1196 STACK_OF(X509_INFO) *infos;
1197 X509_INFO *info;
1198 int i;
1199 int retval = 1;
1200 int retcert = 0;
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001201
1202 if (!ca_e)
1203 return 1;
1204
William Lallemandd4774d32022-07-29 17:08:02 +02001205 if (!append) {
1206 X509_STORE_free(ca_e->ca_store);
1207 ca_e->ca_store = NULL;
1208 }
1209
1210 if (!ca_e->ca_store)
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001211 ca_e->ca_store = X509_STORE_new();
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001212
William Lallemandd4774d32022-07-29 17:08:02 +02001213 if (!ca_e->ca_store)
1214 goto end;
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001215
William Lallemandd4774d32022-07-29 17:08:02 +02001216 bio = BIO_new_mem_buf(cert_buf, strlen(cert_buf));
1217 if (!bio)
1218 goto end;
1219
1220 infos = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
1221 if (!infos)
1222 goto end;
1223
1224 for (i = 0; i < sk_X509_INFO_num(infos) && !retcert; i++) {
1225 info = sk_X509_INFO_value(infos, i);
1226
1227 /* X509_STORE_add_cert and X509_STORE_add_crl return 1 on success */
1228 if (info->x509)
1229 retcert = !X509_STORE_add_cert(ca_e->ca_store, info->x509);
1230 if (!retcert && info->crl)
1231 retcert = !X509_STORE_add_crl(ca_e->ca_store, info->crl);
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001232 }
1233
William Lallemandd4774d32022-07-29 17:08:02 +02001234 /* return an error if we didn't compute all the X509_INFO or if there was none
1235 * set to 0 if everything was right */
1236 if (!(retcert || (i != sk_X509_INFO_num(infos)) || (sk_X509_INFO_num(infos) == 0)))
1237 retval = 0;
1238
1239 /* Cleanup */
1240 sk_X509_INFO_pop_free(infos, X509_INFO_free);
1241
1242end:
1243 BIO_free(bio);
1244
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001245 return retval;
1246}
1247
William Lallemand0f17ab22022-07-19 18:03:16 +02001248/*
1249 * Try to load a ca-file from disk into the ca-file cache.
William Lallemand0a2d6322022-11-24 19:14:19 +01001250 * <shuterror> allows you to to stop emitting the errors.
William Lallemand0f17ab22022-07-19 18:03:16 +02001251 * Return 0 upon error
1252 */
William Lallemand0a2d6322022-11-24 19:14:19 +01001253int __ssl_store_load_locations_file(char *path, int create_if_none, enum cafile_type type, int shuterror)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001254{
1255 X509_STORE *store = ssl_store_get0_locations_file(path);
1256
1257 /* If this function is called by the CLI, we should not call the
1258 * X509_STORE_load_locations function because it performs forbidden disk
1259 * accesses. */
1260 if (!store && create_if_none) {
William Lallemand87fd9942022-04-01 20:12:03 +02001261 STACK_OF(X509_OBJECT) *objs;
1262 int cert_count = 0;
1263 struct stat buf;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001264 struct cafile_entry *ca_e;
William Lallemandc6b17632022-04-01 23:39:37 +02001265 const char *file = NULL;
1266 const char *dir = NULL;
William Lallemand0f17ab22022-07-19 18:03:16 +02001267 unsigned long e;
William Lallemand87fd9942022-04-01 20:12:03 +02001268
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001269 store = X509_STORE_new();
William Lallemand0f17ab22022-07-19 18:03:16 +02001270 if (!store) {
William Lallemand0a2d6322022-11-24 19:14:19 +01001271 if (!shuterror)
1272 ha_alert("Cannot allocate memory!\n");
William Lallemand0f17ab22022-07-19 18:03:16 +02001273 goto err;
1274 }
William Lallemand87fd9942022-04-01 20:12:03 +02001275
William Lallemandc6b17632022-04-01 23:39:37 +02001276 if (strcmp(path, "@system-ca") == 0) {
1277 dir = X509_get_default_cert_dir();
William Lallemand0f17ab22022-07-19 18:03:16 +02001278 if (!dir) {
William Lallemand0a2d6322022-11-24 19:14:19 +01001279 if (!shuterror)
1280 ha_alert("Couldn't get the system CA directory from X509_get_default_cert_dir().\n");
William Lallemand0f17ab22022-07-19 18:03:16 +02001281 goto err;
1282 }
William Lallemand87fd9942022-04-01 20:12:03 +02001283
William Lallemandc6b17632022-04-01 23:39:37 +02001284 } else {
1285
William Lallemand0f17ab22022-07-19 18:03:16 +02001286 if (stat(path, &buf) == -1) {
William Lallemand0a2d6322022-11-24 19:14:19 +01001287 if (!shuterror)
1288 ha_alert("Couldn't open the ca-file '%s' (%s).\n", path, strerror(errno));
William Lallemandc6b17632022-04-01 23:39:37 +02001289 goto err;
William Lallemand0f17ab22022-07-19 18:03:16 +02001290 }
William Lallemandc6b17632022-04-01 23:39:37 +02001291
1292 if (S_ISDIR(buf.st_mode))
1293 dir = path;
1294 else
1295 file = path;
1296 }
William Lallemand87fd9942022-04-01 20:12:03 +02001297
1298 if (file) {
1299 if (!X509_STORE_load_locations(store, file, NULL)) {
William Lallemand0f17ab22022-07-19 18:03:16 +02001300 e = ERR_get_error();
William Lallemand0a2d6322022-11-24 19:14:19 +01001301 if (!shuterror)
1302 ha_alert("Couldn't open the ca-file '%s' (%s).\n", path, ERR_reason_error_string(e));
William Lallemand87fd9942022-04-01 20:12:03 +02001303 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001304 }
William Lallemand80296b42022-04-05 10:19:30 +02001305 } else if (dir) {
William Lallemand87fd9942022-04-01 20:12:03 +02001306 int n, i;
1307 struct dirent **de_list;
1308
1309 n = scandir(dir, &de_list, 0, alphasort);
1310 if (n < 0)
1311 goto err;
1312
1313 for (i= 0; i < n; i++) {
1314 char *end;
1315 struct dirent *de = de_list[i];
1316 BIO *in = NULL;
1317 X509 *ca = NULL;;
1318
William Lallemand43482322022-07-18 18:42:52 +02001319 ERR_clear_error();
1320
William Lallemand87fd9942022-04-01 20:12:03 +02001321 /* we try to load the files that would have
1322 * been loaded in an hashed directory loaded by
1323 * X509_LOOKUP_hash_dir, so according to "man 1
1324 * c_rehash", we should load ".pem", ".crt",
William Lallemande4b93eb2022-05-09 09:29:00 +02001325 * ".cer", or ".crl". Files starting with a dot
1326 * are ignored.
William Lallemand87fd9942022-04-01 20:12:03 +02001327 */
1328 end = strrchr(de->d_name, '.');
William Lallemande4b93eb2022-05-09 09:29:00 +02001329 if (!end || de->d_name[0] == '.' ||
1330 (strcmp(end, ".pem") != 0 &&
1331 strcmp(end, ".crt") != 0 &&
1332 strcmp(end, ".cer") != 0 &&
1333 strcmp(end, ".crl") != 0)) {
William Lallemand87fd9942022-04-01 20:12:03 +02001334 free(de);
1335 continue;
1336 }
1337 in = BIO_new(BIO_s_file());
1338 if (in == NULL)
1339 goto scandir_err;
1340
William Lallemandc6b17632022-04-01 23:39:37 +02001341 chunk_printf(&trash, "%s/%s", dir, de->d_name);
William Lallemand87fd9942022-04-01 20:12:03 +02001342
1343 if (BIO_read_filename(in, trash.area) == 0)
1344 goto scandir_err;
1345
1346 if (PEM_read_bio_X509_AUX(in, &ca, NULL, NULL) == NULL)
1347 goto scandir_err;
1348
William Lallemand43482322022-07-18 18:42:52 +02001349 if (X509_STORE_add_cert(store, ca) == 0) {
1350 /* only exits on error if the error is not about duplicate certificates */
1351 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1352 goto scandir_err;
1353 }
1354 }
William Lallemand87fd9942022-04-01 20:12:03 +02001355
William Lallemand4cfbf3c2022-04-26 15:57:33 +02001356 X509_free(ca);
William Lallemand87fd9942022-04-01 20:12:03 +02001357 BIO_free(in);
1358 free(de);
1359 continue;
1360
1361scandir_err:
William Lallemand0f17ab22022-07-19 18:03:16 +02001362 e = ERR_get_error();
William Lallemand4cfbf3c2022-04-26 15:57:33 +02001363 X509_free(ca);
William Lallemand87fd9942022-04-01 20:12:03 +02001364 BIO_free(in);
1365 free(de);
William Lallemand0f17ab22022-07-19 18:03:16 +02001366 /* warn if it can load one of the files, but don't abort */
William Lallemand0a2d6322022-11-24 19:14:19 +01001367 if (!shuterror)
1368 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 +02001369
1370 }
1371 free(de_list);
William Lallemand80296b42022-04-05 10:19:30 +02001372 } else {
William Lallemand0a2d6322022-11-24 19:14:19 +01001373 if (!shuterror)
1374 ha_alert("ca-file: couldn't load '%s'\n", path);
William Lallemand80296b42022-04-05 10:19:30 +02001375 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001376 }
William Lallemand87fd9942022-04-01 20:12:03 +02001377
1378 objs = X509_STORE_get0_objects(store);
1379 cert_count = sk_X509_OBJECT_num(objs);
William Lallemand0f17ab22022-07-19 18:03:16 +02001380 if (cert_count == 0) {
William Lallemand0a2d6322022-11-24 19:14:19 +01001381 if (!shuterror)
1382 ha_warning("ca-file: 0 CA were loaded from '%s'\n", path);
William Lallemand0f17ab22022-07-19 18:03:16 +02001383 }
William Lallemand87fd9942022-04-01 20:12:03 +02001384 ca_e = ssl_store_create_cafile_entry(path, store, type);
William Lallemand0f17ab22022-07-19 18:03:16 +02001385 if (!ca_e) {
William Lallemand0a2d6322022-11-24 19:14:19 +01001386 if (!shuterror)
1387 ha_alert("Cannot allocate memory!\n");
William Lallemand87fd9942022-04-01 20:12:03 +02001388 goto err;
William Lallemand0f17ab22022-07-19 18:03:16 +02001389 }
William Lallemand87fd9942022-04-01 20:12:03 +02001390 ebst_insert(&cafile_tree, &ca_e->node);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001391 }
1392 return (store != NULL);
William Lallemand87fd9942022-04-01 20:12:03 +02001393
1394err:
1395 X509_STORE_free(store);
1396 store = NULL;
1397 return 0;
1398
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001399}
1400
William Lallemand0a2d6322022-11-24 19:14:19 +01001401int ssl_store_load_locations_file(char *path, int create_if_none, enum cafile_type type)
1402{
1403 return __ssl_store_load_locations_file(path, create_if_none, type, 0);
1404}
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001405
William Lallemandda8584c2020-05-14 10:14:37 +02001406/*************************** CLI commands ***********************/
1407
1408/* Type of SSL payloads that can be updated over the CLI */
1409
William Lallemandff8bf982022-03-29 10:44:23 +02001410struct cert_exts cert_exts[] = {
1411 { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand26654e72022-03-30 12:01:32 +02001412 { "crt", CERT_TYPE_CRT, &ssl_sock_load_pem_into_ckch },
William Lallemandff8bf982022-03-29 10:44:23 +02001413 { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemandda8584c2020-05-14 10:14:37 +02001414#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemandff8bf982022-03-29 10:44:23 +02001415 { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001416#endif
Ilya Shipitsinc47d6762021-02-13 11:45:33 +05001417#ifdef HAVE_SSL_SCTL
William Lallemandff8bf982022-03-29 10:44:23 +02001418 { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001419#endif
William Lallemandff8bf982022-03-29 10:44:23 +02001420 { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
1421 { NULL, CERT_TYPE_MAX, NULL },
William Lallemandda8584c2020-05-14 10:14:37 +02001422};
1423
1424
1425/* release function of the `show ssl cert' command */
1426static void cli_release_show_cert(struct appctx *appctx)
1427{
1428 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1429}
1430
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001431/* IO handler of "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001432 * It makes use of a show_cert_ctx context, and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001433 */
William Lallemandda8584c2020-05-14 10:14:37 +02001434static int cli_io_handler_show_cert(struct appctx *appctx)
1435{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001436 struct show_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001437 struct buffer *trash = alloc_trash_chunk();
1438 struct ebmb_node *node;
Christopher Fauletd1d2e4d2022-06-03 16:24:02 +02001439 struct ckch_store *ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001440
1441 if (trash == NULL)
1442 return 1;
1443
Christopher Faulet3e94f5d2022-06-03 10:46:40 +02001444 if (!ctx->old_ckchs && ckchs_transaction.old_ckchs) {
1445 ckchs = ckchs_transaction.old_ckchs;
1446 chunk_appendf(trash, "# transaction\n");
1447 chunk_appendf(trash, "*%s\n", ckchs->path);
1448 if (applet_putchk(appctx, trash) == -1)
1449 goto yield;
1450 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001451 }
1452
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001453 if (!ctx->cur_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02001454 chunk_appendf(trash, "# filename\n");
1455 node = ebmb_first(&ckchs_tree);
1456 } else {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001457 node = &ctx->cur_ckchs->node;
William Lallemandda8584c2020-05-14 10:14:37 +02001458 }
1459 while (node) {
1460 ckchs = ebmb_entry(node, struct ckch_store, node);
William Lallemand5685ccf2020-09-16 16:12:25 +02001461 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001462
1463 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001464 if (applet_putchk(appctx, trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001465 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001466 }
1467
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001468 ctx->cur_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001469 free_trash_chunk(trash);
1470 return 1;
1471yield:
1472
1473 free_trash_chunk(trash);
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001474 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001475 return 0; /* should come back */
1476}
1477
1478/*
1479 * Extract and format the DNS SAN extensions and copy result into a chuink
1480 * Return 0;
1481 */
1482#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1483static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
1484{
1485 int i;
1486 char *str;
1487 STACK_OF(GENERAL_NAME) *names = NULL;
1488
1489 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1490 if (names) {
1491 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
1492 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
1493 if (i > 0)
1494 chunk_appendf(out, ", ");
1495 if (name->type == GEN_DNS) {
1496 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
1497 chunk_appendf(out, "DNS:%s", str);
1498 OPENSSL_free(str);
1499 }
1500 }
1501 }
1502 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
1503 }
1504 return 0;
1505}
1506#endif
1507
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001508/*
1509 * Build the ckch_inst_link that will be chained in the CA file entry and the
1510 * corresponding ckch_inst_link_ref that will be chained in the ckch instance.
1511 * Return 0 in case of success.
1512 */
1513static int do_chain_inst_and_cafile(struct cafile_entry *cafile_entry, struct ckch_inst *ckch_inst)
1514{
1515 struct ckch_inst_link *new_link;
1516 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
1517 struct ckch_inst_link *link = LIST_ELEM(cafile_entry->ckch_inst_link.n,
1518 typeof(link), list);
1519 /* Do not add multiple references to the same
1520 * instance in a cafile_entry */
1521 if (link->ckch_inst == ckch_inst) {
1522 return 1;
1523 }
1524 }
1525
1526 new_link = calloc(1, sizeof(*new_link));
1527 if (new_link) {
1528 struct ckch_inst_link_ref *new_link_ref = calloc(1, sizeof(*new_link_ref));
1529 if (!new_link_ref) {
1530 free(new_link);
1531 return 1;
1532 }
1533
1534 new_link->ckch_inst = ckch_inst;
1535 new_link_ref->link = new_link;
1536 LIST_INIT(&new_link->list);
1537 LIST_INIT(&new_link_ref->list);
1538
1539 LIST_APPEND(&cafile_entry->ckch_inst_link, &new_link->list);
1540 LIST_APPEND(&ckch_inst->cafile_link_refs, &new_link_ref->list);
1541 }
1542
1543 return 0;
1544}
1545
1546
1547/*
1548 * Link a CA file tree entry to the ckch instance that uses it.
1549 * To determine if and which CA file tree entries need to be linked to the
1550 * instance, we follow the same logic performed in ssl_sock_prepare_ctx when
1551 * processing the verify option.
1552 * This function works for a frontend as well as for a backend, depending on the
1553 * configuration parameters given (bind_conf or server).
1554 */
1555void ckch_inst_add_cafile_link(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf,
1556 struct ssl_bind_conf *ssl_conf, const struct server *srv)
1557{
1558 int verify = SSL_VERIFY_NONE;
1559
1560 if (srv) {
1561
1562 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
1563 verify = SSL_VERIFY_PEER;
1564 switch (srv->ssl_ctx.verify) {
1565 case SSL_SOCK_VERIFY_NONE:
1566 verify = SSL_VERIFY_NONE;
1567 break;
1568 case SSL_SOCK_VERIFY_REQUIRED:
1569 verify = SSL_VERIFY_PEER;
1570 break;
1571 }
1572 }
1573 else {
1574 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
1575 case SSL_SOCK_VERIFY_NONE:
1576 verify = SSL_VERIFY_NONE;
1577 break;
1578 case SSL_SOCK_VERIFY_OPTIONAL:
1579 verify = SSL_VERIFY_PEER;
1580 break;
1581 case SSL_SOCK_VERIFY_REQUIRED:
1582 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1583 break;
1584 }
1585 }
1586
1587 if (verify & SSL_VERIFY_PEER) {
1588 struct cafile_entry *ca_file_entry = NULL;
1589 struct cafile_entry *ca_verify_file_entry = NULL;
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001590 struct cafile_entry *crl_file_entry = NULL;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001591 if (srv) {
1592 if (srv->ssl_ctx.ca_file) {
1593 ca_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.ca_file, 0);
1594
1595 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001596 if (srv->ssl_ctx.crl_file) {
1597 crl_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.crl_file, 0);
1598 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001599 }
1600 else {
1601 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
1602 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 +02001603 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 +01001604
1605 if (ca_file)
1606 ca_file_entry = ssl_store_get_cafile_entry(ca_file, 0);
1607 if (ca_verify_file)
1608 ca_verify_file_entry = ssl_store_get_cafile_entry(ca_verify_file, 0);
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001609 if (crl_file)
1610 crl_file_entry = ssl_store_get_cafile_entry(crl_file, 0);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001611 }
1612
1613 if (ca_file_entry) {
1614 /* If we have a ckch instance that is not already in the
1615 * cafile_entry's list, add it to it. */
1616 if (do_chain_inst_and_cafile(ca_file_entry, ckch_inst))
1617 return;
1618
1619 }
1620 if (ca_verify_file_entry && (ca_file_entry != ca_verify_file_entry)) {
1621 /* If we have a ckch instance that is not already in the
1622 * cafile_entry's list, add it to it. */
1623 if (do_chain_inst_and_cafile(ca_verify_file_entry, ckch_inst))
1624 return;
1625 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001626 if (crl_file_entry) {
1627 /* If we have a ckch instance that is not already in the
1628 * cafile_entry's list, add it to it. */
1629 if (do_chain_inst_and_cafile(crl_file_entry, ckch_inst))
1630 return;
1631 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001632 }
1633}
1634
William Lallemandda8584c2020-05-14 10:14:37 +02001635
1636
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001637static int show_cert_detail(X509 *cert, STACK_OF(X509) *chain, struct buffer *out)
William Lallemandda8584c2020-05-14 10:14:37 +02001638{
William Lallemandda8584c2020-05-14 10:14:37 +02001639 BIO *bio = NULL;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001640 struct buffer *tmp = alloc_trash_chunk();
William Lallemandda8584c2020-05-14 10:14:37 +02001641 int i;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001642 int write = -1;
1643 unsigned int len = 0;
1644 X509_NAME *name = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001645
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001646 if (!tmp)
1647 return -1;
William Lallemandda8584c2020-05-14 10:14:37 +02001648
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001649 if (!cert)
William Lallemand5685ccf2020-09-16 16:12:25 +02001650 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02001651
William Lallemand5685ccf2020-09-16 16:12:25 +02001652 if (chain == NULL) {
1653 struct issuer_chain *issuer;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001654 issuer = ssl_get0_issuer_chain(cert);
William Lallemand5685ccf2020-09-16 16:12:25 +02001655 if (issuer) {
1656 chain = issuer->chain;
1657 chunk_appendf(out, "Chain Filename: ");
1658 chunk_appendf(out, "%s\n", issuer->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001659 }
William Lallemand5685ccf2020-09-16 16:12:25 +02001660 }
1661 chunk_appendf(out, "Serial: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001662 if (ssl_sock_get_serial(cert, tmp) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001663 goto end;
1664 dump_binary(out, tmp->area, tmp->data);
1665 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001666
William Lallemand5685ccf2020-09-16 16:12:25 +02001667 chunk_appendf(out, "notBefore: ");
1668 chunk_reset(tmp);
1669 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1670 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001671 if (ASN1_TIME_print(bio, X509_getm_notBefore(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001672 goto end;
1673 write = BIO_read(bio, tmp->area, tmp->size-1);
1674 tmp->area[write] = '\0';
1675 BIO_free(bio);
1676 bio = NULL;
1677 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001678
William Lallemand5685ccf2020-09-16 16:12:25 +02001679 chunk_appendf(out, "notAfter: ");
1680 chunk_reset(tmp);
1681 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1682 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001683 if (ASN1_TIME_print(bio, X509_getm_notAfter(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001684 goto end;
1685 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
1686 goto end;
1687 tmp->area[write] = '\0';
1688 BIO_free(bio);
1689 bio = NULL;
1690 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001691
1692#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand5685ccf2020-09-16 16:12:25 +02001693 chunk_appendf(out, "Subject Alternative Name: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001694 if (ssl_sock_get_san_oneline(cert, out) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001695 goto end;
1696 *(out->area + out->data) = '\0';
1697 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001698#endif
William Lallemand5685ccf2020-09-16 16:12:25 +02001699 chunk_reset(tmp);
1700 chunk_appendf(out, "Algorithm: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001701 if (cert_get_pkey_algo(cert, tmp) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001702 goto end;
1703 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001704
William Lallemand5685ccf2020-09-16 16:12:25 +02001705 chunk_reset(tmp);
1706 chunk_appendf(out, "SHA1 FingerPrint: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001707 if (X509_digest(cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001708 goto end;
1709 tmp->data = len;
1710 dump_binary(out, tmp->area, tmp->data);
1711 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001712
William Lallemand5685ccf2020-09-16 16:12:25 +02001713 chunk_appendf(out, "Subject: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001714 if ((name = X509_get_subject_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001715 goto end;
1716 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1717 goto end;
1718 *(tmp->area + tmp->data) = '\0';
1719 chunk_appendf(out, "%s\n", tmp->area);
1720
1721 chunk_appendf(out, "Issuer: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001722 if ((name = X509_get_issuer_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001723 goto end;
1724 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1725 goto end;
1726 *(tmp->area + tmp->data) = '\0';
1727 chunk_appendf(out, "%s\n", tmp->area);
1728
1729 /* Displays subject of each certificate in the chain */
1730 for (i = 0; i < sk_X509_num(chain); i++) {
1731 X509 *ca = sk_X509_value(chain, i);
1732
1733 chunk_appendf(out, "Chain Subject: ");
1734 if ((name = X509_get_subject_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001735 goto end;
1736 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1737 goto end;
1738 *(tmp->area + tmp->data) = '\0';
1739 chunk_appendf(out, "%s\n", tmp->area);
1740
William Lallemand5685ccf2020-09-16 16:12:25 +02001741 chunk_appendf(out, "Chain Issuer: ");
1742 if ((name = X509_get_issuer_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001743 goto end;
1744 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1745 goto end;
1746 *(tmp->area + tmp->data) = '\0';
1747 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001748 }
1749
1750end:
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001751 if (bio)
1752 BIO_free(bio);
1753 free_trash_chunk(tmp);
1754
1755 return 0;
1756}
1757
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001758#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 +02001759/*
1760 * Build the OCSP tree entry's key for a given ckch_store.
1761 * Returns a negative value in case of error.
1762 */
1763static int ckch_store_build_certid(struct ckch_store *ckch_store, unsigned char certid[128], unsigned int *key_length)
1764{
1765 OCSP_RESPONSE *resp;
1766 OCSP_BASICRESP *bs = NULL;
1767 OCSP_SINGLERESP *sr;
1768 OCSP_CERTID *id;
1769 unsigned char *p = NULL;
1770
1771 if (!key_length)
1772 return -1;
1773
1774 *key_length = 0;
1775
William Lallemand52ddd992022-11-22 11:51:53 +01001776 if (!ckch_store->data->ocsp_response)
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001777 return 0;
1778
William Lallemand52ddd992022-11-22 11:51:53 +01001779 p = (unsigned char *) ckch_store->data->ocsp_response->area;
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001780
1781 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
William Lallemand52ddd992022-11-22 11:51:53 +01001782 ckch_store->data->ocsp_response->data);
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001783 if (!resp) {
1784 goto end;
1785 }
1786
1787 bs = OCSP_response_get1_basic(resp);
1788 if (!bs) {
1789 goto end;
1790 }
1791
1792 sr = OCSP_resp_get0(bs, 0);
1793 if (!sr) {
1794 goto end;
1795 }
1796
1797 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
1798
1799 p = certid;
1800 *key_length = i2d_OCSP_CERTID(id, &p);
1801
1802end:
1803 return *key_length > 0;
1804}
1805#endif
1806
1807/*
1808 * Dump the OCSP certificate key (if it exists) of certificate <ckch> into
1809 * buffer <out>.
1810 * Returns 0 in case of success.
1811 */
1812static int ckch_store_show_ocsp_certid(struct ckch_store *ckch_store, struct buffer *out)
1813{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001814#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 +02001815 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1816 unsigned int key_length = 0;
1817 int i;
1818
1819 if (ckch_store_build_certid(ckch_store, (unsigned char*)key, &key_length) >= 0) {
1820 /* Dump the CERTID info */
1821 chunk_appendf(out, "OCSP Response Key: ");
1822 for (i = 0; i < key_length; ++i) {
1823 chunk_appendf(out, "%02x", key[i]);
1824 }
1825 chunk_appendf(out, "\n");
1826 }
1827#endif
1828
1829 return 0;
1830}
1831
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001832
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001833/* IO handler of the details "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001834 * It uses a struct show_cert_ctx and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001835 */
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001836static int cli_io_handler_show_cert_detail(struct appctx *appctx)
1837{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001838 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001839 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001840 struct buffer *out = alloc_trash_chunk();
1841 int retval = 0;
1842
1843 if (!out)
1844 goto end_no_putchk;
1845
1846 chunk_appendf(out, "Filename: ");
1847 if (ckchs == ckchs_transaction.new_ckchs)
1848 chunk_appendf(out, "*");
1849 chunk_appendf(out, "%s\n", ckchs->path);
1850
1851 chunk_appendf(out, "Status: ");
William Lallemand52ddd992022-11-22 11:51:53 +01001852 if (ckchs->data->cert == NULL)
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001853 chunk_appendf(out, "Empty\n");
1854 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
1855 chunk_appendf(out, "Unused\n");
1856 else
1857 chunk_appendf(out, "Used\n");
1858
William Lallemand52ddd992022-11-22 11:51:53 +01001859 retval = show_cert_detail(ckchs->data->cert, ckchs->data->chain, out);
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001860 if (retval < 0)
1861 goto end_no_putchk;
1862 else if (retval)
1863 goto end;
1864
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001865 ckch_store_show_ocsp_certid(ckchs, out);
1866
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001867end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001868 if (applet_putchk(appctx, out) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001869 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001870
1871end_no_putchk:
William Lallemandda8584c2020-05-14 10:14:37 +02001872 free_trash_chunk(out);
1873 return 1;
1874yield:
William Lallemandda8584c2020-05-14 10:14:37 +02001875 free_trash_chunk(out);
1876 return 0; /* should come back */
1877}
1878
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001879
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001880/* IO handler of the details "show ssl cert <filename.ocsp>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001881 * It uses a show_cert_ctx.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001882 */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001883static int cli_io_handler_show_cert_ocsp_detail(struct appctx *appctx)
1884{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001885#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL)
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001886 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001887 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001888 struct buffer *out = alloc_trash_chunk();
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001889 int from_transaction = ctx->transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001890
1891 if (!out)
1892 goto end_no_putchk;
1893
1894 /* If we try to display an ongoing transaction's OCSP response, we
1895 * need to dump the ckch's ocsp_response buffer directly.
1896 * Otherwise, we must rebuild the certificate's certid in order to
1897 * look for the current OCSP response in the tree. */
William Lallemand52ddd992022-11-22 11:51:53 +01001898 if (from_transaction && ckchs->data->ocsp_response) {
1899 if (ssl_ocsp_response_print(ckchs->data->ocsp_response, out))
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001900 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001901 }
1902 else {
1903 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1904 unsigned int key_length = 0;
1905
1906 if (ckch_store_build_certid(ckchs, (unsigned char*)key, &key_length) < 0)
1907 goto end_no_putchk;
1908
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001909 if (ssl_get_ocspresponse_detail(key, out))
1910 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001911 }
1912
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001913 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001914 goto yield;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001915
1916end_no_putchk:
1917 free_trash_chunk(out);
1918 return 1;
1919yield:
1920 free_trash_chunk(out);
1921 return 0; /* should come back */
1922#else
1923 return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n");
1924#endif
1925}
1926
William Lallemandda8584c2020-05-14 10:14:37 +02001927/* parsing function for 'show ssl cert [certfile]' */
1928static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
1929{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001930 struct show_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02001931 struct ckch_store *ckchs;
1932
1933 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1934 return cli_err(appctx, "Can't allocate memory!\n");
1935
1936 /* The operations on the CKCH architecture are locked so we can
1937 * manipulate ckch_store and ckch_inst */
1938 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1939 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
1940
1941 /* check if there is a certificate to lookup */
1942 if (*args[3]) {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001943 int show_ocsp_detail = 0;
1944 int from_transaction = 0;
1945 char *end;
1946
1947 /* We manage the special case "certname.ocsp" through which we
1948 * can show the details of an OCSP response. */
1949 end = strrchr(args[3], '.');
1950 if (end && strcmp(end+1, "ocsp") == 0) {
1951 *end = '\0';
1952 show_ocsp_detail = 1;
1953 }
1954
William Lallemandda8584c2020-05-14 10:14:37 +02001955 if (*args[3] == '*') {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001956 from_transaction = 1;
William Lallemandda8584c2020-05-14 10:14:37 +02001957 if (!ckchs_transaction.new_ckchs)
1958 goto error;
1959
1960 ckchs = ckchs_transaction.new_ckchs;
1961
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001962 if (strcmp(args[3] + 1, ckchs->path) != 0)
William Lallemandda8584c2020-05-14 10:14:37 +02001963 goto error;
1964
1965 } else {
1966 if ((ckchs = ckchs_lookup(args[3])) == NULL)
1967 goto error;
1968
1969 }
1970
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001971 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001972 /* use the IO handler that shows details */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001973 if (show_ocsp_detail) {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001974 ctx->transaction = from_transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001975 appctx->io_handler = cli_io_handler_show_cert_ocsp_detail;
1976 }
1977 else
1978 appctx->io_handler = cli_io_handler_show_cert_detail;
William Lallemandda8584c2020-05-14 10:14:37 +02001979 }
1980
1981 return 0;
1982
1983error:
1984 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1985 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
1986}
1987
1988/* release function of the `set ssl cert' command, free things and unlock the spinlock */
1989static void cli_release_commit_cert(struct appctx *appctx)
1990{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02001991 struct commit_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001992
1993 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulet9d56e242022-05-31 16:37:01 +02001994 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
1995 if (ctx->new_ckchs)
1996 ckch_store_free(ctx->new_ckchs);
1997 ha_free(&ctx->err);
William Lallemandda8584c2020-05-14 10:14:37 +02001998}
1999
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002000
2001/*
2002 * Rebuild a new instance 'new_inst' based on an old instance 'ckchi' and a
2003 * specific ckch_store.
2004 * Returns 0 in case of success, 1 otherwise.
2005 */
William Lallemande60c7d62022-03-30 11:26:15 +02002006int ckch_inst_rebuild(struct ckch_store *ckch_store, struct ckch_inst *ckchi,
2007 struct ckch_inst **new_inst, char **err)
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002008{
2009 int retval = 0;
2010 int errcode = 0;
2011 struct sni_ctx *sc0, *sc0s;
2012 char **sni_filter = NULL;
2013 int fcount = 0;
2014
2015 if (ckchi->crtlist_entry) {
2016 sni_filter = ckchi->crtlist_entry->filters;
2017 fcount = ckchi->crtlist_entry->fcount;
2018 }
2019
2020 if (ckchi->is_server_instance)
2021 errcode |= ckch_inst_new_load_srv_store(ckch_store->path, ckch_store, new_inst, err);
2022 else
2023 errcode |= ckch_inst_new_load_store(ckch_store->path, ckch_store, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, new_inst, err);
2024
2025 if (errcode & ERR_CODE)
2026 return 1;
2027
2028 /* if the previous ckchi was used as the default */
2029 if (ckchi->is_default)
2030 (*new_inst)->is_default = 1;
2031
2032 (*new_inst)->is_server_instance = ckchi->is_server_instance;
2033 (*new_inst)->server = ckchi->server;
2034 /* Create a new SSL_CTX and link it to the new instance. */
2035 if ((*new_inst)->is_server_instance) {
2036 retval = ssl_sock_prep_srv_ctx_and_inst(ckchi->server, (*new_inst)->ctx, (*new_inst));
2037 if (retval)
2038 return 1;
2039 }
2040
2041 /* create the link to the crtlist_entry */
2042 (*new_inst)->crtlist_entry = ckchi->crtlist_entry;
2043
2044 /* we need to initialize the SSL_CTX generated */
2045 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
2046 list_for_each_entry_safe(sc0, sc0s, &(*new_inst)->sni_ctx, by_ckch_inst) {
2047 if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
2048 errcode |= ssl_sock_prep_ctx_and_inst(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, *new_inst, err);
2049 if (errcode & ERR_CODE)
2050 return 1;
2051 }
2052 }
2053
2054 return 0;
2055}
2056
2057/*
2058 * Load all the new SNIs of a newly built ckch instance in the trees, or replace
2059 * a server's main ckch instance.
2060 */
2061static void __ssl_sock_load_new_ckch_instance(struct ckch_inst *ckchi)
2062{
2063 /* The bind_conf will be null on server ckch_instances. */
2064 if (ckchi->is_server_instance) {
2065 int i;
2066 /* a lock is needed here since we have to free the SSL cache */
2067 HA_RWLOCK_WRLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
2068 /* free the server current SSL_CTX */
2069 SSL_CTX_free(ckchi->server->ssl_ctx.ctx);
2070 /* Actual ssl context update */
2071 SSL_CTX_up_ref(ckchi->ctx);
2072 ckchi->server->ssl_ctx.ctx = ckchi->ctx;
2073 ckchi->server->ssl_ctx.inst = ckchi;
2074
2075 /* flush the session cache of the server */
2076 for (i = 0; i < global.nbthread; i++) {
William Lallemandce990332021-11-23 15:15:09 +01002077 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].sni);
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002078 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].ptr);
2079 }
2080 HA_RWLOCK_WRUNLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
2081
2082 } else {
2083 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
2084 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
2085 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
2086 }
2087}
2088
2089/*
2090 * Delete a ckch instance that was replaced after a CLI command.
2091 */
2092static void __ckch_inst_free_locked(struct ckch_inst *ckchi)
2093{
2094 if (ckchi->is_server_instance) {
2095 /* no lock for servers */
2096 ckch_inst_free(ckchi);
2097 } else {
2098 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
2099
2100 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
2101 ckch_inst_free(ckchi);
2102 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
2103 }
2104}
2105
William Lallemand3b5a3a62022-03-29 14:29:31 +02002106/* Replace a ckch_store in the ckch tree and insert the whole dependencies,
2107* then free the previous dependencies and store.
2108* Used in the case of a certificate update.
2109*
2110* Every dependencies must allocated before using this function.
2111*
2112* This function can't fail as it only update pointers, and does not alloc anything.
2113*
2114* /!\ This function must be used under the ckch lock. /!\
2115*
2116* - Insert every dependencies (SNI, crtlist_entry, ckch_inst, etc)
2117* - Delete the old ckch_store from the tree
2118* - Insert the new ckch_store
2119* - Free the old dependencies and the old ckch_store
2120*/
2121void ckch_store_replace(struct ckch_store *old_ckchs, struct ckch_store *new_ckchs)
2122{
2123 struct crtlist_entry *entry;
2124 struct ckch_inst *ckchi, *ckchis;
2125
2126 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
2127 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
2128 ebpt_delete(&entry->node);
2129 /* change the ptr and reinsert the node */
2130 entry->node.key = new_ckchs;
2131 ebpt_insert(&entry->crtlist->entries, &entry->node);
2132 }
2133 /* insert the new ckch_insts in the crtlist_entry */
2134 list_for_each_entry(ckchi, &new_ckchs->ckch_inst, by_ckchs) {
2135 if (ckchi->crtlist_entry)
2136 LIST_INSERT(&ckchi->crtlist_entry->ckch_inst, &ckchi->by_crtlist_entry);
2137 }
2138 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2139 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
2140 __ssl_sock_load_new_ckch_instance(ckchi);
2141 }
2142 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
2143 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
2144 __ckch_inst_free_locked(ckchi);
2145 }
2146
2147 ckch_store_free(old_ckchs);
2148 ebst_insert(&ckchs_tree, &new_ckchs->node);
2149}
2150
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002151
William Lallemandda8584c2020-05-14 10:14:37 +02002152/*
2153 * This function tries to create the new ckch_inst and their SNIs
William Lallemand30fcca12022-03-30 12:03:12 +02002154 *
2155 * /!\ don't forget to update __hlua_ckch_commit() if you changes things there. /!\
William Lallemandda8584c2020-05-14 10:14:37 +02002156 */
2157static int cli_io_handler_commit_cert(struct appctx *appctx)
2158{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002159 struct commit_cert_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002160 struct stconn *sc = appctx_sc(appctx);
William Lallemandda8584c2020-05-14 10:14:37 +02002161 int y = 0;
William Lallemandda8584c2020-05-14 10:14:37 +02002162 struct ckch_store *old_ckchs, *new_ckchs = NULL;
William Lallemand3b5a3a62022-03-29 14:29:31 +02002163 struct ckch_inst *ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002164
Willy Tarreau475e4632022-05-27 10:26:46 +02002165 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Christopher Faulet9d56e242022-05-31 16:37:01 +02002166 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02002167
2168 while (1) {
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002169 switch (ctx->state) {
2170 case CERT_ST_INIT:
William Lallemandda8584c2020-05-14 10:14:37 +02002171 /* This state just print the update message */
Christopher Faulet9d56e242022-05-31 16:37:01 +02002172 chunk_printf(&trash, "Committing %s", ckchs_transaction.path);
2173 if (applet_putchk(appctx, &trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02002174 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002175
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002176 ctx->state = CERT_ST_GEN;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002177 __fallthrough;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002178 case CERT_ST_GEN:
William Lallemandda8584c2020-05-14 10:14:37 +02002179 /*
2180 * This state generates the ckch instances with their
2181 * sni_ctxs and SSL_CTX.
2182 *
2183 * Since the SSL_CTX generation can be CPU consumer, we
2184 * yield every 10 instances.
2185 */
2186
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002187 old_ckchs = ctx->old_ckchs;
2188 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002189
William Lallemandda8584c2020-05-14 10:14:37 +02002190 /* get the next ckchi to regenerate */
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002191 ckchi = ctx->next_ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002192 /* we didn't start yet, set it to the first elem */
2193 if (ckchi == NULL)
2194 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
2195
2196 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
2197 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
2198 struct ckch_inst *new_inst;
William Lallemandda8584c2020-05-14 10:14:37 +02002199
Christopher Faulet9d56e242022-05-31 16:37:01 +02002200 /* save the next ckchi to compute in case of yield */
2201 ctx->next_ckchi = ckchi;
2202
William Lallemandda8584c2020-05-14 10:14:37 +02002203 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2204 if (y >= 10) {
Christopher Faulet9d56e242022-05-31 16:37:01 +02002205 applet_have_more_data(appctx); /* let's come back later */
William Lallemandda8584c2020-05-14 10:14:37 +02002206 goto yield;
2207 }
2208
Christopher Faulet9d56e242022-05-31 16:37:01 +02002209 /* display one dot per new instance */
2210 if (applet_putstr(appctx, ".") == -1)
2211 goto yield;
2212
2213 ctx->err = NULL;
2214 if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &ctx->err)) {
2215 ctx->state = CERT_ST_ERROR;
William Lallemandda8584c2020-05-14 10:14:37 +02002216 goto error;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002217 }
William Lallemandda8584c2020-05-14 10:14:37 +02002218
William Lallemandda8584c2020-05-14 10:14:37 +02002219 /* link the new ckch_inst to the duplicate */
Willy Tarreau2b718102021-04-21 07:32:39 +02002220 LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002221 y++;
2222 }
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002223 ctx->state = CERT_ST_INSERT;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002224 __fallthrough;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002225 case CERT_ST_INSERT:
William Lallemandda8584c2020-05-14 10:14:37 +02002226 /* The generation is finished, we can insert everything */
2227
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002228 old_ckchs = ctx->old_ckchs;
2229 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002230
William Lallemand3b5a3a62022-03-29 14:29:31 +02002231 /* insert everything and remove the previous objects */
2232 ckch_store_replace(old_ckchs, new_ckchs);
Christopher Faulet9d56e242022-05-31 16:37:01 +02002233 ctx->new_ckchs = ctx->old_ckchs = NULL;
2234 ctx->state = CERT_ST_SUCCESS;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002235 __fallthrough;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002236 case CERT_ST_SUCCESS:
2237 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2238 goto yield;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002239 ctx->state = CERT_ST_FIN;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002240 __fallthrough;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002241 case CERT_ST_FIN:
William Lallemandda8584c2020-05-14 10:14:37 +02002242 /* we achieved the transaction, we can set everything to NULL */
William Lallemandda8584c2020-05-14 10:14:37 +02002243 ckchs_transaction.new_ckchs = NULL;
2244 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002245 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002246 goto end;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002247
2248 case CERT_ST_ERROR:
2249 error:
2250 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2251 if (applet_putchk(appctx, &trash) == -1)
2252 goto yield;
2253 ctx->state = CERT_ST_FIN;
2254 break;
William Lallemandda8584c2020-05-14 10:14:37 +02002255 }
2256 }
2257end:
William Lallemandda8584c2020-05-14 10:14:37 +02002258 /* success: call the release function and don't come back */
2259 return 1;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002260
William Lallemandda8584c2020-05-14 10:14:37 +02002261yield:
William Lallemandda8584c2020-05-14 10:14:37 +02002262 return 0; /* should come back */
William Lallemandda8584c2020-05-14 10:14:37 +02002263}
2264
2265/*
2266 * Parsing function of 'commit ssl cert'
2267 */
2268static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
2269{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002270 struct commit_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02002271 char *err = NULL;
2272
2273 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2274 return 1;
2275
2276 if (!*args[3])
2277 return cli_err(appctx, "'commit ssl cert expects a filename\n");
2278
2279 /* The operations on the CKCH architecture are locked so we can
2280 * manipulate ckch_store and ckch_inst */
2281 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2282 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
2283
2284 if (!ckchs_transaction.path) {
2285 memprintf(&err, "No ongoing transaction! !\n");
2286 goto error;
2287 }
2288
2289 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2290 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
2291 goto error;
2292 }
2293
William Lallemand5685ccf2020-09-16 16:12:25 +02002294 /* if a certificate is here, a private key must be here too */
William Lallemand52ddd992022-11-22 11:51:53 +01002295 if (ckchs_transaction.new_ckchs->data->cert && !ckchs_transaction.new_ckchs->data->key) {
William Lallemand5685ccf2020-09-16 16:12:25 +02002296 memprintf(&err, "The transaction must contain at least a certificate and a private key!\n");
2297 goto error;
2298 }
William Lallemanda9419522020-06-24 16:26:41 +02002299
William Lallemand52ddd992022-11-22 11:51:53 +01002300 if (!X509_check_private_key(ckchs_transaction.new_ckchs->data->cert, ckchs_transaction.new_ckchs->data->key)) {
William Lallemand5685ccf2020-09-16 16:12:25 +02002301 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
2302 goto error;
William Lallemandda8584c2020-05-14 10:14:37 +02002303 }
2304
2305 /* init the appctx structure */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002306 ctx->state = CERT_ST_INIT;
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002307 ctx->next_ckchi = NULL;
2308 ctx->new_ckchs = ckchs_transaction.new_ckchs;
2309 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002310
2311 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
2312 return 0;
2313
2314error:
2315
2316 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2317 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2318
2319 return cli_dynerr(appctx, err);
2320}
2321
2322
2323
2324
2325/*
2326 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
Willy Tarreau329f4b42022-05-04 20:05:55 +02002327 * It uses a set_cert_ctx context, and ckchs_transaction under a lock.
William Lallemandda8584c2020-05-14 10:14:37 +02002328 */
2329static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
2330{
2331 struct ckch_store *new_ckchs = NULL;
2332 struct ckch_store *old_ckchs = NULL;
2333 char *err = NULL;
2334 int i;
William Lallemandda8584c2020-05-14 10:14:37 +02002335 int errcode = 0;
2336 char *end;
William Lallemandff8bf982022-03-29 10:44:23 +02002337 struct cert_exts *cert_ext = &cert_exts[0]; /* default one, PEM */
William Lallemand52ddd992022-11-22 11:51:53 +01002338 struct ckch_data *data;
William Lallemandda8584c2020-05-14 10:14:37 +02002339 struct buffer *buf;
2340
2341 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2342 return 1;
2343
William Lallemandda8584c2020-05-14 10:14:37 +02002344 if (!*args[3] || !payload)
2345 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
2346
2347 /* The operations on the CKCH architecture are locked so we can
2348 * manipulate ckch_store and ckch_inst */
2349 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2350 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
2351
William Lallemand5ba80d62021-05-04 16:17:27 +02002352 if ((buf = alloc_trash_chunk()) == NULL) {
2353 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2354 errcode |= ERR_ALERT | ERR_FATAL;
2355 goto end;
2356 }
William Lallemande5ff4ad2020-06-08 09:40:37 +02002357
William Lallemandda8584c2020-05-14 10:14:37 +02002358 if (!chunk_strcpy(buf, args[3])) {
2359 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2360 errcode |= ERR_ALERT | ERR_FATAL;
2361 goto end;
2362 }
2363
2364 /* check which type of file we want to update */
William Lallemandff8bf982022-03-29 10:44:23 +02002365 for (i = 0; cert_exts[i].ext != NULL; i++) {
William Lallemandda8584c2020-05-14 10:14:37 +02002366 end = strrchr(buf->area, '.');
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002367 if (end && *cert_exts[i].ext && (strcmp(end + 1, cert_exts[i].ext) == 0)) {
William Lallemandda8584c2020-05-14 10:14:37 +02002368 *end = '\0';
William Lallemand089c1382020-10-23 17:35:12 +02002369 buf->data = strlen(buf->area);
William Lallemandff8bf982022-03-29 10:44:23 +02002370 cert_ext = &cert_exts[i];
William Lallemandda8584c2020-05-14 10:14:37 +02002371 break;
2372 }
2373 }
2374
William Lallemandda8584c2020-05-14 10:14:37 +02002375 /* if there is an ongoing transaction */
2376 if (ckchs_transaction.path) {
William Lallemandda8584c2020-05-14 10:14:37 +02002377 /* if there is an ongoing transaction, check if this is the same file */
2378 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
William Lallemand089c1382020-10-23 17:35:12 +02002379 /* we didn't find the transaction, must try more cases below */
2380
2381 /* 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 +02002382 if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
William Lallemand089c1382020-10-23 17:35:12 +02002383 if (!chunk_strcat(buf, ".crt")) {
2384 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2385 errcode |= ERR_ALERT | ERR_FATAL;
2386 goto end;
2387 }
2388
2389 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
2390 /* remove .crt of the error message */
2391 *(b_orig(buf) + b_data(buf) + strlen(".crt")) = '\0';
2392 b_sub(buf, strlen(".crt"));
2393
2394 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
2395 errcode |= ERR_ALERT | ERR_FATAL;
2396 goto end;
2397 }
2398 }
William Lallemandda8584c2020-05-14 10:14:37 +02002399 }
2400
Christopher Faulet24a20b92022-06-03 11:50:40 +02002401 old_ckchs = ckchs_transaction.new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002402
2403 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002404
William Lallemand95fefa12020-09-09 12:01:33 +02002405 /* lookup for the certificate in the tree */
Christopher Faulet24a20b92022-06-03 11:50:40 +02002406 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002407
Christopher Faulet24a20b92022-06-03 11:50:40 +02002408 if (!old_ckchs) {
William Lallemand089c1382020-10-23 17:35:12 +02002409 /* 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 +02002410 if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
William Lallemand089c1382020-10-23 17:35:12 +02002411 if (!chunk_strcat(buf, ".crt")) {
2412 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2413 errcode |= ERR_ALERT | ERR_FATAL;
2414 goto end;
2415 }
Christopher Faulet24a20b92022-06-03 11:50:40 +02002416 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002417 }
2418 }
William Lallemandda8584c2020-05-14 10:14:37 +02002419 }
2420
Christopher Faulet24a20b92022-06-03 11:50:40 +02002421 if (!old_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02002422 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
2423 err ? err : "");
2424 errcode |= ERR_ALERT | ERR_FATAL;
2425 goto end;
2426 }
2427
William Lallemandda8584c2020-05-14 10:14:37 +02002428 /* duplicate the ckch store */
2429 new_ckchs = ckchs_dup(old_ckchs);
2430 if (!new_ckchs) {
2431 memprintf(&err, "%sCannot allocate memory!\n",
2432 err ? err : "");
2433 errcode |= ERR_ALERT | ERR_FATAL;
2434 goto end;
2435 }
2436
William Lallemand52ddd992022-11-22 11:51:53 +01002437 data = new_ckchs->data;
William Lallemandda8584c2020-05-14 10:14:37 +02002438
2439 /* appply the change on the duplicate */
William Lallemand52ddd992022-11-22 11:51:53 +01002440 if (cert_ext->load(buf->area, payload, data, &err) != 0) {
William Lallemandda8584c2020-05-14 10:14:37 +02002441 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
2442 errcode |= ERR_ALERT | ERR_FATAL;
2443 goto end;
2444 }
2445
William Lallemandda8584c2020-05-14 10:14:37 +02002446 /* we succeed, we can save the ckchs in the transaction */
2447
2448 /* if there wasn't a transaction, update the old ckchs */
2449 if (!ckchs_transaction.old_ckchs) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002450 ckchs_transaction.old_ckchs = old_ckchs;
2451 ckchs_transaction.path = old_ckchs->path;
William Lallemandda8584c2020-05-14 10:14:37 +02002452 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
2453 } else {
2454 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
2455
2456 }
2457
2458 /* free the previous ckchs if there was a transaction */
2459 ckch_store_free(ckchs_transaction.new_ckchs);
2460
Christopher Faulet24a20b92022-06-03 11:50:40 +02002461 ckchs_transaction.new_ckchs = new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002462
2463
2464 /* creates the SNI ctxs later in the IO handler */
2465
2466end:
2467 free_trash_chunk(buf);
2468
2469 if (errcode & ERR_CODE) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002470 ckch_store_free(new_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002471 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2472 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2473 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002474 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2475 return cli_dynmsg(appctx, LOG_NOTICE, err);
2476 }
2477 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
2478}
2479
2480/* parsing function of 'abort ssl cert' */
2481static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
2482{
2483 char *err = NULL;
2484
2485 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2486 return 1;
2487
2488 if (!*args[3])
2489 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
2490
2491 /* The operations on the CKCH architecture are locked so we can
2492 * manipulate ckch_store and ckch_inst */
2493 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2494 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2495
2496 if (!ckchs_transaction.path) {
2497 memprintf(&err, "No ongoing transaction!\n");
2498 goto error;
2499 }
2500
2501 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2502 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
2503 goto error;
2504 }
2505
2506 /* Only free the ckchs there, because the SNI and instances were not generated yet */
2507 ckch_store_free(ckchs_transaction.new_ckchs);
2508 ckchs_transaction.new_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002509 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002510 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002511
2512 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2513
2514 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2515 return cli_dynmsg(appctx, LOG_NOTICE, err);
2516
2517error:
2518 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2519
2520 return cli_dynerr(appctx, err);
2521}
2522
2523/* parsing function of 'new ssl cert' */
2524static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
2525{
2526 struct ckch_store *store;
2527 char *err = NULL;
2528 char *path;
2529
2530 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2531 return 1;
2532
2533 if (!*args[3])
2534 return cli_err(appctx, "'new ssl cert' expects a filename\n");
2535
2536 path = args[3];
2537
2538 /* The operations on the CKCH architecture are locked so we can
2539 * manipulate ckch_store and ckch_inst */
2540 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2541 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
2542
2543 store = ckchs_lookup(path);
2544 if (store != NULL) {
2545 memprintf(&err, "Certificate '%s' already exists!\n", path);
2546 store = NULL; /* we don't want to free it */
2547 goto error;
2548 }
2549 /* we won't support multi-certificate bundle here */
William Lallemandbd8e6ed2020-09-16 16:08:08 +02002550 store = ckch_store_new(path);
William Lallemandda8584c2020-05-14 10:14:37 +02002551 if (!store) {
2552 memprintf(&err, "unable to allocate memory.\n");
2553 goto error;
2554 }
2555
2556 /* insert into the ckchs tree */
2557 ebst_insert(&ckchs_tree, &store->node);
2558 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
2559
2560 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2561 return cli_dynmsg(appctx, LOG_NOTICE, err);
2562error:
2563 free(store);
2564 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2565 return cli_dynerr(appctx, err);
2566}
2567
2568/* parsing function of 'del ssl cert' */
2569static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
2570{
2571 struct ckch_store *store;
2572 char *err = NULL;
2573 char *filename;
2574
2575 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2576 return 1;
2577
2578 if (!*args[3])
2579 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
2580
2581 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2582 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
2583
2584 filename = args[3];
2585
Christopher Faulet926fefc2022-05-31 18:04:25 +02002586 if (ckchs_transaction.path && strcmp(ckchs_transaction.path, filename) == 0) {
2587 memprintf(&err, "ongoing transaction for the certificate '%s'", filename);
2588 goto error;
2589 }
2590
William Lallemandda8584c2020-05-14 10:14:37 +02002591 store = ckchs_lookup(filename);
2592 if (store == NULL) {
2593 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
2594 goto error;
2595 }
2596 if (!LIST_ISEMPTY(&store->ckch_inst)) {
2597 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
2598 goto error;
2599 }
2600
2601 ebmb_delete(&store->node);
2602 ckch_store_free(store);
2603
2604 memprintf(&err, "Certificate '%s' deleted!\n", filename);
2605
2606 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2607 return cli_dynmsg(appctx, LOG_NOTICE, err);
2608
2609error:
2610 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
2611 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2612 return cli_dynerr(appctx, err);
2613}
2614
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002615
Remi Tricot-Le Breton9f40fe02021-03-16 16:21:27 +01002616
2617/* parsing function of 'new ssl ca-file' */
2618static int cli_parse_new_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2619{
2620 struct cafile_entry *cafile_entry;
2621 char *err = NULL;
2622 char *path;
2623
2624 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2625 return 1;
2626
2627 if (!*args[3])
2628 return cli_err(appctx, "'new ssl ca-file' expects a filename\n");
2629
2630 path = args[3];
2631
2632 /* The operations on the CKCH architecture are locked so we can
2633 * manipulate ckch_store and ckch_inst */
2634 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2635 return cli_err(appctx, "Can't create a CA file!\nOperations on certificates are currently locked!\n");
2636
2637 cafile_entry = ssl_store_get_cafile_entry(path, 0);
2638 if (cafile_entry) {
2639 memprintf(&err, "CA file '%s' already exists!\n", path);
2640 goto error;
2641 }
2642
2643 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CERT);
2644 if (!cafile_entry) {
2645 memprintf(&err, "%sCannot allocate memory!\n",
2646 err ? err : "");
2647 goto error;
2648 }
2649
2650 /* Add the newly created cafile_entry to the tree so that
2651 * any new ckch instance created from now can use it. */
2652 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
2653 goto error;
2654
2655 memprintf(&err, "New CA file created '%s'!\n", path);
2656
2657 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2658 return cli_dynmsg(appctx, LOG_NOTICE, err);
2659error:
2660 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2661 return cli_dynerr(appctx, err);
2662}
2663
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002664/*
2665 * Parsing function of `set ssl ca-file`
2666 */
2667static int cli_parse_set_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2668{
Christopher Faulet132c5952022-06-03 11:56:26 +02002669 struct cafile_entry *old_cafile_entry = NULL;
2670 struct cafile_entry *new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002671 char *err = NULL;
2672 int errcode = 0;
2673 struct buffer *buf;
William Lallemand62c0b992022-07-29 17:50:58 +02002674 int add_cmd = 0;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002675
2676 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2677 return 1;
2678
William Lallemand62c0b992022-07-29 17:50:58 +02002679 /* this is "add ssl ca-file" */
2680 if (*args[0] == 'a')
2681 add_cmd = 1;
2682
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002683 if (!*args[3] || !payload)
2684 return cli_err(appctx, "'set ssl ca-file expects a filename and CAs as a payload\n");
2685
2686 /* The operations on the CKCH architecture are locked so we can
2687 * manipulate ckch_store and ckch_inst */
2688 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2689 return cli_err(appctx, "Can't update the CA file!\nOperations on certificates are currently locked!\n");
2690
2691 if ((buf = alloc_trash_chunk()) == NULL) {
2692 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2693 errcode |= ERR_ALERT | ERR_FATAL;
2694 goto end;
2695 }
2696
2697 if (!chunk_strcpy(buf, args[3])) {
2698 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2699 errcode |= ERR_ALERT | ERR_FATAL;
2700 goto end;
2701 }
2702
Christopher Faulet132c5952022-06-03 11:56:26 +02002703 old_cafile_entry = NULL;
2704 new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002705
2706 /* if there is an ongoing transaction */
2707 if (cafile_transaction.path) {
2708 /* if there is an ongoing transaction, check if this is the same file */
2709 if (strcmp(cafile_transaction.path, buf->area) != 0) {
2710 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, buf->area);
2711 errcode |= ERR_ALERT | ERR_FATAL;
2712 goto end;
2713 }
Christopher Faulet132c5952022-06-03 11:56:26 +02002714 old_cafile_entry = cafile_transaction.old_cafile_entry;
William Lallemand62c0b992022-07-29 17:50:58 +02002715 } else {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002716 /* lookup for the certificate in the tree */
Christopher Faulet132c5952022-06-03 11:56:26 +02002717 old_cafile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002718 }
2719
Christopher Faulet132c5952022-06-03 11:56:26 +02002720 if (!old_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002721 memprintf(&err, "%sCan't replace a CA file which is not referenced by the configuration!\n",
2722 err ? err : "");
2723 errcode |= ERR_ALERT | ERR_FATAL;
2724 goto end;
2725 }
2726
William Lallemand62c0b992022-07-29 17:50:58 +02002727 /* if the transaction is new, duplicate the old_ca_file_entry, otherwise duplicate the cafile in the current transaction */
2728 if (cafile_transaction.new_cafile_entry)
2729 new_cafile_entry = ssl_store_dup_cafile_entry(cafile_transaction.new_cafile_entry);
2730 else
2731 new_cafile_entry = ssl_store_dup_cafile_entry(old_cafile_entry);
2732
Christopher Faulet132c5952022-06-03 11:56:26 +02002733 if (!new_cafile_entry) {
William Lallemand62c0b992022-07-29 17:50:58 +02002734 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002735 errcode |= ERR_ALERT | ERR_FATAL;
2736 goto end;
2737 }
2738
William Lallemand62c0b992022-07-29 17:50:58 +02002739 /* Fill the new entry with the new CAs. The add_cmd variable determine
2740 if we flush the X509_STORE or not */
2741 if (ssl_store_load_ca_from_buf(new_cafile_entry, payload, add_cmd)) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002742 memprintf(&err, "%sInvalid payload\n", err ? err : "");
2743 errcode |= ERR_ALERT | ERR_FATAL;
2744 goto end;
2745 }
2746
2747 /* we succeed, we can save the ca in the transaction */
2748
2749 /* if there wasn't a transaction, update the old CA */
2750 if (!cafile_transaction.old_cafile_entry) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002751 cafile_transaction.old_cafile_entry = old_cafile_entry;
2752 cafile_transaction.path = old_cafile_entry->path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002753 err = memprintf(&err, "transaction created for CA %s!\n", cafile_transaction.path);
2754 } else {
2755 err = memprintf(&err, "transaction updated for CA %s!\n", cafile_transaction.path);
2756 }
2757
2758 /* free the previous CA if there was a transaction */
2759 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2760
Christopher Faulet132c5952022-06-03 11:56:26 +02002761 cafile_transaction.new_cafile_entry = new_cafile_entry;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002762
2763 /* creates the SNI ctxs later in the IO handler */
2764
2765end:
2766 free_trash_chunk(buf);
2767
2768 if (errcode & ERR_CODE) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002769 ssl_store_delete_cafile_entry(new_cafile_entry);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002770 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2771 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2772 } else {
2773
2774 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2775 return cli_dynmsg(appctx, LOG_NOTICE, err);
2776 }
2777}
2778
2779
2780/*
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002781 * Parsing function of 'commit ssl ca-file'.
2782 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl crl-file".
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002783 */
2784static int cli_parse_commit_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2785{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002786 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002787 char *err = NULL;
2788
2789 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2790 return 1;
2791
2792 if (!*args[3])
2793 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
2794
2795 /* The operations on the CKCH architecture are locked so we can
2796 * manipulate ckch_store and ckch_inst */
2797 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2798 return cli_err(appctx, "Can't commit the CA file!\nOperations on certificates are currently locked!\n");
2799
2800 if (!cafile_transaction.path) {
2801 memprintf(&err, "No ongoing transaction! !\n");
2802 goto error;
2803 }
2804
2805 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2806 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, args[3]);
2807 goto error;
2808 }
2809 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002810 ctx->state = CACRL_ST_INIT;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002811 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002812 ctx->old_entry = cafile_transaction.old_cafile_entry;
2813 ctx->new_entry = cafile_transaction.new_cafile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002814 ctx->cafile_type = CAFILE_CERT;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002815
2816 return 0;
2817
2818error:
2819
2820 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2821 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2822
2823 return cli_dynerr(appctx, err);
2824}
2825
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002826/*
2827 * This function tries to create new ckch instances and their SNIs using a newly
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002828 * set certificate authority (CA file) or a newly set Certificate Revocation
2829 * List (CRL), depending on the command being called.
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002830 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002831static int cli_io_handler_commit_cafile_crlfile(struct appctx *appctx)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002832{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002833 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002834 struct stconn *sc = appctx_sc(appctx);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002835 int y = 0;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002836 struct cafile_entry *old_cafile_entry = ctx->old_entry;
2837 struct cafile_entry *new_cafile_entry = ctx->new_entry;
William Lallemand0bfa3e72022-08-30 17:32:38 +02002838 struct ckch_inst_link *ckchi_link;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002839 char *path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002840
Willy Tarreau475e4632022-05-27 10:26:46 +02002841 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002842 goto end;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002843
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002844 /* The ctx was already validated by the ca-file/crl-file parsing
2845 * function. Entries can only be NULL in CACRL_ST_SUCCESS or
2846 * CACRL_ST_FIN states
2847 */
2848 switch (ctx->cafile_type) {
2849 case CAFILE_CERT:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002850 path = cafile_transaction.path;
2851 break;
2852 case CAFILE_CRL:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002853 path = crlfile_transaction.path;
2854 break;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002855 default:
Willy Tarreaud543ae02022-06-22 05:40:25 +02002856 path = NULL;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002857 goto error;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002858 }
2859
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002860 while (1) {
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002861 switch (ctx->state) {
2862 case CACRL_ST_INIT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002863 /* This state just print the update message */
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002864 chunk_printf(&trash, "Committing %s", path);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002865 if (applet_putchk(appctx, &trash) == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002866 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002867
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002868 ctx->state = CACRL_ST_GEN;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002869 __fallthrough;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002870 case CACRL_ST_GEN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002871 /*
2872 * This state generates the ckch instances with their
2873 * sni_ctxs and SSL_CTX.
2874 *
2875 * Since the SSL_CTX generation can be CPU consumer, we
2876 * yield every 10 instances.
2877 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002878
2879 /* get the next ckchi to regenerate */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002880 ckchi_link = ctx->next_ckchi_link;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002881
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002882 /* we didn't start yet, set it to the first elem */
2883 if (ckchi_link == NULL) {
2884 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2885 /* Add the newly created cafile_entry to the tree so that
2886 * any new ckch instance created from now can use it. */
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002887 if (ssl_store_add_uncommitted_cafile_entry(new_cafile_entry)) {
2888 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002889 goto error;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002890 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002891 }
2892
2893 list_for_each_entry_from(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002894 struct ckch_inst *new_inst;
2895
2896 /* save the next ckchi to compute */
2897 ctx->next_ckchi_link = ckchi_link;
2898
2899 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2900 if (y >= 10) {
2901 applet_have_more_data(appctx); /* let's come back later */
2902 goto yield;
2903 }
2904
2905 /* display one dot per new instance */
2906 if (applet_putstr(appctx, ".") == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002907 goto yield;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002908
2909 /* Rebuild a new ckch instance that uses the same ckch_store
2910 * than a reference ckchi instance but will use a new CA file. */
2911 ctx->err = NULL;
2912 if (ckch_inst_rebuild(ckchi_link->ckch_inst->ckch_store, ckchi_link->ckch_inst, &new_inst, &ctx->err)) {
2913 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002914 goto error;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002915 }
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002916
2917 y++;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002918 }
2919
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002920 ctx->state = CACRL_ST_INSERT;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002921 __fallthrough;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002922 case CACRL_ST_INSERT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002923 /* The generation is finished, we can insert everything */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002924
2925 /* insert the new ckch_insts in the crtlist_entry */
2926 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2927 if (ckchi_link->ckch_inst->crtlist_entry)
2928 LIST_INSERT(&ckchi_link->ckch_inst->crtlist_entry->ckch_inst,
2929 &ckchi_link->ckch_inst->by_crtlist_entry);
2930 }
2931
2932 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2933 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2934 __ssl_sock_load_new_ckch_instance(ckchi_link->ckch_inst);
2935 }
2936
William Lallemande0fa91f2022-08-31 14:26:49 +02002937 /* delete the old sni_ctx, the old ckch_insts
2938 * and the ckch_store. ckch_inst_free() also
2939 * manipulates the list so it's cleaner to loop
2940 * until it's empty */
2941 while (!LIST_ISEMPTY(&old_cafile_entry->ckch_inst_link)) {
2942 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2943
2944 LIST_DEL_INIT(&ckchi_link->list); /* must reinit because ckch_inst checks the list */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002945 __ckch_inst_free_locked(ckchi_link->ckch_inst);
William Lallemande0fa91f2022-08-31 14:26:49 +02002946 free(ckchi_link);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002947 }
2948
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002949 /* Remove the old cafile entry from the tree */
2950 ebmb_delete(&old_cafile_entry->node);
2951 ssl_store_delete_cafile_entry(old_cafile_entry);
2952
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002953 ctx->old_entry = ctx->new_entry = NULL;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002954 ctx->state = CACRL_ST_SUCCESS;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002955 __fallthrough;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002956 case CACRL_ST_SUCCESS:
2957 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2958 goto yield;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002959 ctx->state = CACRL_ST_FIN;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002960 __fallthrough;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002961 case CACRL_ST_FIN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002962 /* we achieved the transaction, we can set everything to NULL */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002963 switch (ctx->cafile_type) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002964 case CAFILE_CERT:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002965 cafile_transaction.old_cafile_entry = NULL;
2966 cafile_transaction.new_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002967 cafile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002968 break;
2969 case CAFILE_CRL:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002970 crlfile_transaction.old_crlfile_entry = NULL;
2971 crlfile_transaction.new_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002972 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002973 break;
2974 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002975 goto end;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002976
2977 case CACRL_ST_ERROR:
2978 error:
2979 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2980 if (applet_putchk(appctx, &trash) == -1)
2981 goto yield;
2982 ctx->state = CACRL_ST_FIN;
2983 break;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002984 }
2985 }
2986end:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002987 /* success: call the release function and don't come back */
2988 return 1;
2989yield:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002990 return 0; /* should come back */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002991}
2992
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002993
2994/* parsing function of 'abort ssl ca-file' */
2995static int cli_parse_abort_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2996{
2997 char *err = NULL;
2998
2999 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3000 return 1;
3001
3002 if (!*args[3])
3003 return cli_err(appctx, "'abort ssl ca-file' expects a filename\n");
3004
3005 /* The operations on the CKCH architecture are locked so we can
3006 * manipulate ckch_store and ckch_inst */
3007 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3008 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
3009
3010 if (!cafile_transaction.path) {
3011 memprintf(&err, "No ongoing transaction!\n");
3012 goto error;
3013 }
3014
3015 if (strcmp(cafile_transaction.path, args[3]) != 0) {
3016 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", cafile_transaction.path, args[3]);
3017 goto error;
3018 }
3019
3020 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3021 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
3022 cafile_transaction.new_cafile_entry = NULL;
3023 cafile_transaction.old_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02003024 cafile_transaction.path = NULL;
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01003025
3026 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3027
3028 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3029 return cli_dynmsg(appctx, LOG_NOTICE, err);
3030
3031error:
3032 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3033
3034 return cli_dynerr(appctx, err);
3035}
3036
Willy Tarreau821c3b02022-05-04 15:47:39 +02003037/* release function of the `commit ssl ca-file' command, free things and unlock the spinlock.
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003038 * It uses a commit_cacrlfile_ctx context.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003039 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003040static void cli_release_commit_cafile(struct appctx *appctx)
3041{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003042 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003043 struct cafile_entry *new_cafile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003044
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003045 /* Remove the uncommitted cafile_entry from the tree. */
3046 if (new_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003047 ebmb_delete(&new_cafile_entry->node);
3048 ssl_store_delete_cafile_entry(new_cafile_entry);
3049 }
3050 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003051 ha_free(&ctx->err);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003052}
3053
3054
Willy Tarreau821c3b02022-05-04 15:47:39 +02003055/* IO handler of details "show ssl ca-file <filename[:index]>".
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003056 * It uses a show_cafile_ctx context, and the global
3057 * cafile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003058 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003059static int cli_io_handler_show_cafile_detail(struct appctx *appctx)
3060{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003061 struct show_cafile_ctx *ctx = appctx->svcctx;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003062 struct cafile_entry *cafile_entry = ctx->cur_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003063 struct buffer *out = alloc_trash_chunk();
William Lallemand03a32e52022-04-26 18:17:15 +02003064 int i = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003065 X509 *cert;
3066 STACK_OF(X509_OBJECT) *objs;
3067 int retval = 0;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003068 int ca_index = ctx->ca_index;
3069 int show_all = ctx->show_all;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003070
3071 if (!out)
3072 goto end_no_putchk;
3073
3074 chunk_appendf(out, "Filename: ");
3075 if (cafile_entry == cafile_transaction.new_cafile_entry)
3076 chunk_appendf(out, "*");
3077 chunk_appendf(out, "%s\n", cafile_entry->path);
3078
3079 chunk_appendf(out, "Status: ");
3080 if (!cafile_entry->ca_store)
3081 chunk_appendf(out, "Empty\n");
3082 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3083 chunk_appendf(out, "Unused\n");
3084 else
3085 chunk_appendf(out, "Used\n");
3086
3087 if (!cafile_entry->ca_store)
3088 goto end;
3089
3090 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
William Lallemand03a32e52022-04-26 18:17:15 +02003091 for (i = ca_index; i < sk_X509_OBJECT_num(objs); i++) {
3092
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003093 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
3094 if (!cert)
3095 continue;
3096
William Lallemand03a32e52022-04-26 18:17:15 +02003097 /* file starts at line 1 */
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003098 chunk_appendf(out, " \nCertificate #%d:\n", i+1);
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003099 retval = show_cert_detail(cert, NULL, out);
3100 if (retval < 0)
3101 goto end_no_putchk;
William Lallemand03a32e52022-04-26 18:17:15 +02003102 else if (retval)
3103 goto yield;
3104
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003105 if (applet_putchk(appctx, out) == -1)
William Lallemand03a32e52022-04-26 18:17:15 +02003106 goto yield;
William Lallemand03a32e52022-04-26 18:17:15 +02003107
3108 if (!show_all) /* only need to dump one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003109 goto end;
3110 }
3111
3112end:
William Lallemand03a32e52022-04-26 18:17:15 +02003113 free_trash_chunk(out);
3114 return 1; /* end, don't come back */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003115
3116end_no_putchk:
3117 free_trash_chunk(out);
3118 return 1;
3119yield:
William Lallemand03a32e52022-04-26 18:17:15 +02003120 /* save the current state */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003121 ctx->ca_index = i;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003122 free_trash_chunk(out);
3123 return 0; /* should come back */
3124}
3125
3126
Willy Tarreau06305792022-05-04 15:57:30 +02003127/* parsing function for 'show ssl ca-file [cafile[:index]]'.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003128 * It prepares a show_cafile_ctx context, and checks the global
3129 * cafile_transaction under the ckch_lock (read only).
Willy Tarreau06305792022-05-04 15:57:30 +02003130 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003131static int cli_parse_show_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3132{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003133 struct show_cafile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003134 struct cafile_entry *cafile_entry;
William Lallemand03a32e52022-04-26 18:17:15 +02003135 int ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003136 char *colons;
3137 char *err = NULL;
3138
3139 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3140 return cli_err(appctx, "Can't allocate memory!\n");
3141
3142 /* The operations on the CKCH architecture are locked so we can
3143 * manipulate ckch_store and ckch_inst */
3144 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3145 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3146
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003147 ctx->show_all = 1; /* show all certificates */
3148 ctx->ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003149 /* check if there is a certificate to lookup */
3150 if (*args[3]) {
3151
3152 /* Look for an optional CA index after the CA file name */
3153 colons = strchr(args[3], ':');
3154 if (colons) {
3155 char *endptr;
3156
3157 ca_index = strtol(colons + 1, &endptr, 10);
3158 /* Indexes start at 1 */
3159 if (colons + 1 == endptr || *endptr != '\0' || ca_index <= 0) {
3160 memprintf(&err, "wrong CA index after colons in '%s'!", args[3]);
3161 goto error;
3162 }
3163 *colons = '\0';
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003164 ctx->ca_index = ca_index - 1; /* we start counting at 0 in the ca_store, but at 1 on the CLI */
3165 ctx->show_all = 0; /* show only one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003166 }
3167
3168 if (*args[3] == '*') {
3169 if (!cafile_transaction.new_cafile_entry)
3170 goto error;
3171
3172 cafile_entry = cafile_transaction.new_cafile_entry;
3173
3174 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3175 goto error;
3176
3177 } else {
3178 /* Get the "original" cafile_entry and not the
3179 * uncommitted one if it exists. */
3180 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CERT)
3181 goto error;
3182 }
3183
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003184 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003185 /* use the IO handler that shows details */
3186 appctx->io_handler = cli_io_handler_show_cafile_detail;
3187 }
3188
3189 return 0;
3190
3191error:
3192 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3193 if (err)
3194 return cli_dynerr(appctx, err);
3195 return cli_err(appctx, "Can't display the CA file : Not found!\n");
3196}
3197
3198
3199/* release function of the 'show ssl ca-file' command */
3200static void cli_release_show_cafile(struct appctx *appctx)
3201{
3202 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3203}
3204
3205
3206/* This function returns the number of certificates in a cafile_entry. */
3207static int get_certificate_count(struct cafile_entry *cafile_entry)
3208{
3209 int cert_count = 0;
3210 STACK_OF(X509_OBJECT) *objs;
3211
3212 if (cafile_entry && cafile_entry->ca_store) {
3213 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3214 if (objs)
3215 cert_count = sk_X509_OBJECT_num(objs);
3216 }
3217 return cert_count;
3218}
3219
3220/* IO handler of "show ssl ca-file". The command taking a specific CA file name
Willy Tarreau821c3b02022-05-04 15:47:39 +02003221 * is managed in cli_io_handler_show_cafile_detail.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003222 * It uses a show_cafile_ctx and the global cafile_transaction.new_cafile_entry
3223 * in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003224 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003225static int cli_io_handler_show_cafile(struct appctx *appctx)
3226{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003227 struct show_cafile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003228 struct buffer *trash = alloc_trash_chunk();
3229 struct ebmb_node *node;
Christopher Faulet677cb4f2022-06-03 16:25:35 +02003230 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003231
3232 if (trash == NULL)
3233 return 1;
3234
Christopher Faulet5a2154b2022-06-03 10:42:48 +02003235 if (!ctx->old_cafile_entry && cafile_transaction.old_cafile_entry) {
3236 chunk_appendf(trash, "# transaction\n");
3237 chunk_appendf(trash, "*%s", cafile_transaction.old_cafile_entry->path);
3238 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_transaction.new_cafile_entry));
3239 if (applet_putchk(appctx, trash) == -1)
3240 goto yield;
3241 ctx->old_cafile_entry = cafile_transaction.new_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003242 }
3243
3244 /* First time in this io_handler. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003245 if (!ctx->cur_cafile_entry) {
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003246 chunk_appendf(trash, "# filename\n");
3247 node = ebmb_first(&cafile_tree);
3248 } else {
3249 /* We yielded during a previous call. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003250 node = &ctx->cur_cafile_entry->node;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003251 }
3252
3253 while (node) {
3254 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3255 if (cafile_entry->type == CAFILE_CERT) {
3256 chunk_appendf(trash, "%s", cafile_entry->path);
3257
3258 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_entry));
3259 }
3260
3261 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003262 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003263 goto yield;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003264 }
3265
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003266 ctx->cur_cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003267 free_trash_chunk(trash);
3268 return 1;
3269yield:
3270
3271 free_trash_chunk(trash);
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003272 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003273 return 0; /* should come back */
3274}
3275
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003276/* parsing function of 'del ssl ca-file' */
3277static int cli_parse_del_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3278{
3279 struct cafile_entry *cafile_entry;
3280 char *err = NULL;
3281 char *filename;
3282
3283 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3284 return 1;
3285
3286 if (!*args[3])
3287 return cli_err(appctx, "'del ssl ca-file' expects a CA file name\n");
3288
3289 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3290 return cli_err(appctx, "Can't delete the CA file!\nOperations on certificates are currently locked!\n");
3291
3292 filename = args[3];
3293
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003294 if (cafile_transaction.path && strcmp(cafile_transaction.path, filename) == 0) {
3295 memprintf(&err, "ongoing transaction for the CA file '%s'", filename);
3296 goto error;
3297 }
3298
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003299 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3300 if (!cafile_entry) {
3301 memprintf(&err, "CA file '%s' doesn't exist!\n", filename);
3302 goto error;
3303 }
3304
3305 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3306 memprintf(&err, "CA file '%s' in use, can't be deleted!\n", filename);
3307 goto error;
3308 }
3309
3310 /* Remove the cafile_entry from the tree */
3311 ebmb_delete(&cafile_entry->node);
3312 ssl_store_delete_cafile_entry(cafile_entry);
3313
3314 memprintf(&err, "CA file '%s' deleted!\n", filename);
3315
3316 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3317 return cli_dynmsg(appctx, LOG_NOTICE, err);
3318
3319error:
3320 memprintf(&err, "Can't remove the CA file: %s\n", err ? err : "");
3321 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3322 return cli_dynerr(appctx, err);
3323}
3324
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003325/* parsing function of 'new ssl crl-file' */
3326static int cli_parse_new_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3327{
3328 struct cafile_entry *cafile_entry;
3329 char *err = NULL;
3330 char *path;
3331
3332 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3333 return 1;
3334
3335 if (!*args[3])
3336 return cli_err(appctx, "'new ssl crl-file' expects a filename\n");
3337
3338 path = args[3];
3339
3340 /* The operations on the CKCH architecture are locked so we can
3341 * manipulate ckch_store and ckch_inst */
3342 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003343 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 +02003344
3345 cafile_entry = ssl_store_get_cafile_entry(path, 0);
3346 if (cafile_entry) {
3347 memprintf(&err, "CRL file '%s' already exists!\n", path);
3348 goto error;
3349 }
3350
3351 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CRL);
3352 if (!cafile_entry) {
3353 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3354 goto error;
3355 }
3356
3357 /* Add the newly created cafile_entry to the tree so that
3358 * any new ckch instance created from now can use it. */
3359 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
3360 goto error;
3361
3362 memprintf(&err, "New CRL file created '%s'!\n", path);
3363
3364 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3365 return cli_dynmsg(appctx, LOG_NOTICE, err);
3366error:
3367 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3368 return cli_dynerr(appctx, err);
3369}
3370
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003371/* Parsing function of `set ssl crl-file` */
3372static int cli_parse_set_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3373{
Christopher Faulet1f90f332022-06-03 16:34:30 +02003374 struct cafile_entry *old_crlfile_entry = NULL;
3375 struct cafile_entry *new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003376 char *err = NULL;
3377 int errcode = 0;
3378 struct buffer *buf;
3379
3380 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3381 return 1;
3382
3383 if (!*args[3] || !payload)
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003384 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 +02003385
3386 /* The operations on the CKCH architecture are locked so we can
3387 * manipulate ckch_store and ckch_inst */
3388 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3389 return cli_err(appctx, "Can't update the CRL file!\nOperations on certificates are currently locked!\n");
3390
3391 if ((buf = alloc_trash_chunk()) == NULL) {
3392 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3393 errcode |= ERR_ALERT | ERR_FATAL;
3394 goto end;
3395 }
3396
3397 if (!chunk_strcpy(buf, args[3])) {
3398 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3399 errcode |= ERR_ALERT | ERR_FATAL;
3400 goto end;
3401 }
3402
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003403 old_crlfile_entry = NULL;
3404 new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003405
3406 /* if there is an ongoing transaction */
3407 if (crlfile_transaction.path) {
3408 /* if there is an ongoing transaction, check if this is the same file */
3409 if (strcmp(crlfile_transaction.path, buf->area) != 0) {
3410 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, buf->area);
3411 errcode |= ERR_ALERT | ERR_FATAL;
3412 goto end;
3413 }
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003414 old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003415 }
3416 else {
3417 /* lookup for the certificate in the tree */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003418 old_crlfile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003419 }
3420
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003421 if (!old_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003422 memprintf(&err, "%sCan't replace a CRL file which is not referenced by the configuration!\n",
3423 err ? err : "");
3424 errcode |= ERR_ALERT | ERR_FATAL;
3425 goto end;
3426 }
3427
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003428 /* Create a new cafile_entry without adding it to the cafile tree. */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003429 new_crlfile_entry = ssl_store_create_cafile_entry(old_crlfile_entry->path, NULL, CAFILE_CRL);
3430 if (!new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003431 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3432 errcode |= ERR_ALERT | ERR_FATAL;
3433 goto end;
3434 }
3435
3436 /* Fill the new entry with the new CRL. */
William Lallemandd4774d32022-07-29 17:08:02 +02003437 if (ssl_store_load_ca_from_buf(new_crlfile_entry, payload, 0)) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003438 memprintf(&err, "%sInvalid payload\n", err ? err : "");
3439 errcode |= ERR_ALERT | ERR_FATAL;
3440 goto end;
3441 }
3442
3443 /* we succeed, we can save the crl in the transaction */
3444
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003445 /* if there wasn't a transaction, update the old CRL */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003446 if (!crlfile_transaction.old_crlfile_entry) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003447 crlfile_transaction.old_crlfile_entry = old_crlfile_entry;
3448 crlfile_transaction.path = old_crlfile_entry->path;
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003449 err = memprintf(&err, "transaction created for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003450 } else {
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003451 err = memprintf(&err, "transaction updated for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003452 }
3453
3454 /* free the previous CRL file if there was a transaction */
3455 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3456
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003457 crlfile_transaction.new_crlfile_entry = new_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003458
3459 /* creates the SNI ctxs later in the IO handler */
3460
3461end:
3462 free_trash_chunk(buf);
3463
3464 if (errcode & ERR_CODE) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003465 ssl_store_delete_cafile_entry(new_crlfile_entry);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003466 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3467 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
3468 } else {
3469
3470 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3471 return cli_dynmsg(appctx, LOG_NOTICE, err);
3472 }
3473}
3474
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003475/* Parsing function of 'commit ssl crl-file'.
3476 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl ca-file".
3477 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003478static int cli_parse_commit_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3479{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003480 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003481 char *err = NULL;
3482
3483 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3484 return 1;
3485
3486 if (!*args[3])
3487 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
3488
3489 /* The operations on the CKCH architecture are locked so we can
3490 * manipulate ckch_store and ckch_inst */
3491 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3492 return cli_err(appctx, "Can't commit the CRL file!\nOperations on certificates are currently locked!\n");
3493
3494 if (!crlfile_transaction.path) {
3495 memprintf(&err, "No ongoing transaction! !\n");
3496 goto error;
3497 }
3498
3499 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3500 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, args[3]);
3501 goto error;
3502 }
3503 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02003504 ctx->state = CACRL_ST_INIT;
Christopher Fauletf814c4a2022-06-03 11:32:05 +02003505 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003506 ctx->old_entry = crlfile_transaction.old_crlfile_entry;
3507 ctx->new_entry = crlfile_transaction.new_crlfile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003508 ctx->cafile_type = CAFILE_CRL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003509
3510 return 0;
3511
3512error:
3513
3514 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3515 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
3516
3517 return cli_dynerr(appctx, err);
3518}
3519
3520
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003521/* release function of the `commit ssl crl-file' command, free things and unlock the spinlock.
3522 * it uses a commit_cacrlfile_ctx that's the same as for "commit ssl ca-file".
3523 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003524static void cli_release_commit_crlfile(struct appctx *appctx)
3525{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003526 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003527 struct cafile_entry *new_crlfile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003528
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003529 /* Remove the uncommitted cafile_entry from the tree. */
3530 if (new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003531 ebmb_delete(&new_crlfile_entry->node);
3532 ssl_store_delete_cafile_entry(new_crlfile_entry);
3533 }
3534 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003535 ha_free(&ctx->err);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003536}
3537
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003538/* parsing function of 'del ssl crl-file' */
3539static int cli_parse_del_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3540{
3541 struct cafile_entry *cafile_entry;
3542 char *err = NULL;
3543 char *filename;
3544
3545 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3546 return 1;
3547
3548 if (!*args[3])
3549 return cli_err(appctx, "'del ssl crl-file' expects a CRL file name\n");
3550
3551 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3552 return cli_err(appctx, "Can't delete the CRL file!\nOperations on certificates are currently locked!\n");
3553
3554 filename = args[3];
3555
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003556 if (crlfile_transaction.path && strcmp(crlfile_transaction.path, filename) == 0) {
3557 memprintf(&err, "ongoing transaction for the CRL file '%s'", filename);
3558 goto error;
3559 }
3560
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003561 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3562 if (!cafile_entry) {
3563 memprintf(&err, "CRL file '%s' doesn't exist!\n", filename);
3564 goto error;
3565 }
3566 if (cafile_entry->type != CAFILE_CRL) {
3567 memprintf(&err, "'del ssl crl-file' does not work on CA files!\n");
3568 goto error;
3569 }
3570
3571 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3572 memprintf(&err, "CRL file '%s' in use, can't be deleted!\n", filename);
3573 goto error;
3574 }
3575
3576 /* Remove the cafile_entry from the tree */
3577 ebmb_delete(&cafile_entry->node);
3578 ssl_store_delete_cafile_entry(cafile_entry);
3579
3580 memprintf(&err, "CRL file '%s' deleted!\n", filename);
3581
3582 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3583 return cli_dynmsg(appctx, LOG_NOTICE, err);
3584
3585error:
3586 memprintf(&err, "Can't remove the CRL file: %s\n", err ? err : "");
3587 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3588 return cli_dynerr(appctx, err);
3589}
3590
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003591/* parsing function of 'abort ssl crl-file' */
3592static int cli_parse_abort_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3593{
3594 char *err = NULL;
3595
3596 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3597 return 1;
3598
3599 if (!*args[3])
3600 return cli_err(appctx, "'abort ssl crl-file' expects a filename\n");
3601
3602 /* The operations on the CKCH architecture are locked so we can
3603 * manipulate ckch_store and ckch_inst */
3604 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3605 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
3606
3607 if (!crlfile_transaction.path) {
3608 memprintf(&err, "No ongoing transaction!\n");
3609 goto error;
3610 }
3611
3612 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3613 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", crlfile_transaction.path, args[3]);
3614 goto error;
3615 }
3616
3617 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3618 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3619 crlfile_transaction.new_crlfile_entry = NULL;
3620 crlfile_transaction.old_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02003621 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003622
3623 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3624
3625 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3626 return cli_dynmsg(appctx, LOG_NOTICE, err);
3627
3628error:
3629 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3630
3631 return cli_dynerr(appctx, err);
3632}
3633
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003634
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003635/*
3636 * Display a Certificate Resignation List's information.
3637 * The information displayed is inspired by the output of 'openssl crl -in
3638 * crl.pem -text'.
3639 * Returns 0 in case of success.
3640 */
3641static int show_crl_detail(X509_CRL *crl, struct buffer *out)
3642{
3643 BIO *bio = NULL;
3644 struct buffer *tmp = alloc_trash_chunk();
3645 long version;
3646 X509_NAME *issuer;
3647 int write = -1;
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003648#ifndef USE_OPENSSL_WOLFSSL
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003649 STACK_OF(X509_REVOKED) *rev = NULL;
3650 X509_REVOKED *rev_entry = NULL;
3651 int i;
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003652#endif
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003653
3654 if (!tmp)
3655 return -1;
3656
3657 if ((bio = BIO_new(BIO_s_mem())) == NULL)
3658 goto end;
3659
3660 /* Version (as displayed by 'openssl crl') */
3661 version = X509_CRL_get_version(crl);
3662 chunk_appendf(out, "Version %ld\n", version + 1);
3663
3664 /* Signature Algorithm */
3665 chunk_appendf(out, "Signature Algorithm: %s\n", OBJ_nid2ln(X509_CRL_get_signature_nid(crl)));
3666
3667 /* Issuer */
3668 chunk_appendf(out, "Issuer: ");
3669 if ((issuer = X509_CRL_get_issuer(crl)) == NULL)
3670 goto end;
3671 if ((ssl_sock_get_dn_oneline(issuer, tmp)) == -1)
3672 goto end;
3673 *(tmp->area + tmp->data) = '\0';
3674 chunk_appendf(out, "%s\n", tmp->area);
3675
3676 /* Last Update */
3677 chunk_appendf(out, "Last Update: ");
3678 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003679 if (BIO_reset(bio) == -1)
3680 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003681 if (ASN1_TIME_print(bio, X509_CRL_get0_lastUpdate(crl)) == 0)
3682 goto end;
3683 write = BIO_read(bio, tmp->area, tmp->size-1);
3684 tmp->area[write] = '\0';
3685 chunk_appendf(out, "%s\n", tmp->area);
3686
3687
3688 /* Next Update */
3689 chunk_appendf(out, "Next Update: ");
3690 chunk_reset(tmp);
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 if (ASN1_TIME_print(bio, X509_CRL_get0_nextUpdate(crl)) == 0)
3694 goto end;
3695 write = BIO_read(bio, tmp->area, tmp->size-1);
3696 tmp->area[write] = '\0';
3697 chunk_appendf(out, "%s\n", tmp->area);
3698
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003699#ifndef USE_OPENSSL_WOLFSSL
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003700 /* Revoked Certificates */
3701 rev = X509_CRL_get_REVOKED(crl);
3702 if (sk_X509_REVOKED_num(rev) > 0)
3703 chunk_appendf(out, "Revoked Certificates:\n");
3704 else
3705 chunk_appendf(out, "No Revoked Certificates.\n");
3706
3707 for (i = 0; i < sk_X509_REVOKED_num(rev); i++) {
3708 rev_entry = sk_X509_REVOKED_value(rev, i);
3709
3710 /* Serial Number and Revocation Date */
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003711 if (BIO_reset(bio) == -1)
3712 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003713 BIO_printf(bio , " Serial Number: ");
Remi Tricot-Le Breton18c7d832021-05-17 18:38:34 +02003714 i2a_ASN1_INTEGER(bio, (ASN1_INTEGER*)X509_REVOKED_get0_serialNumber(rev_entry));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003715 BIO_printf(bio, "\n Revocation Date: ");
Remi Tricot-Le Bretona6b27842021-05-18 10:06:00 +02003716 if (ASN1_TIME_print(bio, X509_REVOKED_get0_revocationDate(rev_entry)) == 0)
3717 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003718 BIO_printf(bio, "\n");
3719
3720 write = BIO_read(bio, tmp->area, tmp->size-1);
3721 tmp->area[write] = '\0';
3722 chunk_appendf(out, "%s", tmp->area);
3723 }
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003724#endif /* not USE_OPENSSL_WOLFSSL */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003725
3726end:
3727 free_trash_chunk(tmp);
3728 if (bio)
3729 BIO_free(bio);
3730
3731 return 0;
3732}
3733
Willy Tarreau821c3b02022-05-04 15:47:39 +02003734/* IO handler of details "show ssl crl-file <filename[:index]>".
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003735 * It uses show_crlfile_ctx and the global
3736 * crlfile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003737 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003738static int cli_io_handler_show_crlfile_detail(struct appctx *appctx)
3739{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003740 struct show_crlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003741 struct cafile_entry *cafile_entry = ctx->cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003742 struct buffer *out = alloc_trash_chunk();
3743 int i;
3744 X509_CRL *crl;
3745 STACK_OF(X509_OBJECT) *objs;
3746 int retval = 0;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003747 int index = ctx->index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003748
3749 if (!out)
3750 goto end_no_putchk;
3751
3752 chunk_appendf(out, "Filename: ");
3753 if (cafile_entry == crlfile_transaction.new_crlfile_entry)
3754 chunk_appendf(out, "*");
3755 chunk_appendf(out, "%s\n", cafile_entry->path);
3756
3757 chunk_appendf(out, "Status: ");
3758 if (!cafile_entry->ca_store)
3759 chunk_appendf(out, "Empty\n");
3760 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3761 chunk_appendf(out, "Unused\n");
3762 else
3763 chunk_appendf(out, "Used\n");
3764
3765 if (!cafile_entry->ca_store)
3766 goto end;
3767
3768 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3769 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
3770 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
3771 if (!crl)
3772 continue;
3773
3774 /* CRL indexes start at 1 on the CLI output. */
3775 if (index && index-1 != i)
3776 continue;
3777
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003778 chunk_appendf(out, " \nCertificate Revocation List #%d:\n", i+1);
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003779 retval = show_crl_detail(crl, out);
3780 if (retval < 0)
3781 goto end_no_putchk;
3782 else if (retval || index)
3783 goto end;
3784 }
3785
3786end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003787 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003788 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003789
3790end_no_putchk:
3791 free_trash_chunk(out);
3792 return 1;
3793yield:
3794 free_trash_chunk(out);
3795 return 0; /* should come back */
3796}
3797
Willy Tarreau821c3b02022-05-04 15:47:39 +02003798/* parsing function for 'show ssl crl-file [crlfile[:index]]'.
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003799 * It sets the context to a show_crlfile_ctx, and the global
Willy Tarreau821c3b02022-05-04 15:47:39 +02003800 * cafile_transaction.new_crlfile_entry under the ckch_lock.
3801 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003802static int cli_parse_show_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3803{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003804 struct show_crlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003805 struct cafile_entry *cafile_entry;
3806 long index = 0;
3807 char *colons;
3808 char *err = NULL;
3809
3810 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3811 return cli_err(appctx, "Can't allocate memory!\n");
3812
3813 /* The operations on the CKCH architecture are locked so we can
3814 * manipulate ckch_store and ckch_inst */
3815 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3816 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3817
3818 /* check if there is a certificate to lookup */
3819 if (*args[3]) {
3820
3821 /* Look for an optional index after the CRL file name */
3822 colons = strchr(args[3], ':');
3823 if (colons) {
3824 char *endptr;
3825
3826 index = strtol(colons + 1, &endptr, 10);
3827 /* Indexes start at 1 */
3828 if (colons + 1 == endptr || *endptr != '\0' || index <= 0) {
3829 memprintf(&err, "wrong CRL index after colons in '%s'!", args[3]);
3830 goto error;
3831 }
3832 *colons = '\0';
3833 }
3834
3835 if (*args[3] == '*') {
3836 if (!crlfile_transaction.new_crlfile_entry)
3837 goto error;
3838
3839 cafile_entry = crlfile_transaction.new_crlfile_entry;
3840
3841 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3842 goto error;
3843
3844 } else {
3845 /* Get the "original" cafile_entry and not the
3846 * uncommitted one if it exists. */
3847 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CRL)
3848 goto error;
3849 }
3850
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003851 ctx->cafile_entry = cafile_entry;
3852 ctx->index = index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003853 /* use the IO handler that shows details */
3854 appctx->io_handler = cli_io_handler_show_crlfile_detail;
3855 }
3856
3857 return 0;
3858
3859error:
3860 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3861 if (err)
3862 return cli_dynerr(appctx, err);
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003863 return cli_err(appctx, "Can't display the CRL file : Not found!\n");
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003864}
3865
3866/* IO handler of "show ssl crl-file". The command taking a specific CRL file name
3867 * is managed in cli_io_handler_show_crlfile_detail. */
3868static int cli_io_handler_show_crlfile(struct appctx *appctx)
3869{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003870 struct show_crlfile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003871 struct buffer *trash = alloc_trash_chunk();
3872 struct ebmb_node *node;
Christopher Faulet88041b32022-06-03 16:26:56 +02003873 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003874
3875 if (trash == NULL)
3876 return 1;
3877
Christopher Faulet9a99e542022-06-03 10:32:18 +02003878 if (!ctx->old_crlfile_entry && crlfile_transaction.old_crlfile_entry) {
3879 chunk_appendf(trash, "# transaction\n");
3880 chunk_appendf(trash, "*%s\n", crlfile_transaction.old_crlfile_entry->path);
3881 if (applet_putchk(appctx, trash) == -1)
3882 goto yield;
3883 ctx->old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003884 }
3885
3886 /* First time in this io_handler. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003887 if (!ctx->cafile_entry) {
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003888 chunk_appendf(trash, "# filename\n");
3889 node = ebmb_first(&cafile_tree);
3890 } else {
3891 /* We yielded during a previous call. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003892 node = &ctx->cafile_entry->node;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003893 }
3894
3895 while (node) {
3896 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3897 if (cafile_entry->type == CAFILE_CRL) {
3898 chunk_appendf(trash, "%s\n", cafile_entry->path);
3899 }
3900
3901 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003902 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003903 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003904 }
3905
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003906 ctx->cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003907 free_trash_chunk(trash);
3908 return 1;
3909yield:
3910
3911 free_trash_chunk(trash);
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003912 ctx->cafile_entry = cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003913 return 0; /* should come back */
3914}
3915
3916
3917/* release function of the 'show ssl crl-file' command */
3918static void cli_release_show_crlfile(struct appctx *appctx)
3919{
3920 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3921}
3922
3923
William Lallemandee8530c2020-06-23 18:19:42 +02003924void ckch_deinit()
3925{
3926 struct eb_node *node, *next;
3927 struct ckch_store *store;
William Lallemandb0c48272022-04-26 15:44:53 +02003928 struct ebmb_node *canode;
William Lallemandee8530c2020-06-23 18:19:42 +02003929
William Lallemandb0c48272022-04-26 15:44:53 +02003930 /* deinit the ckch stores */
William Lallemandee8530c2020-06-23 18:19:42 +02003931 node = eb_first(&ckchs_tree);
3932 while (node) {
3933 next = eb_next(node);
3934 store = ebmb_entry(node, struct ckch_store, node);
3935 ckch_store_free(store);
3936 node = next;
3937 }
William Lallemandb0c48272022-04-26 15:44:53 +02003938
3939 /* deinit the ca-file store */
3940 canode = ebmb_first(&cafile_tree);
3941 while (canode) {
3942 struct cafile_entry *entry = NULL;
3943
3944 entry = ebmb_entry(canode, struct cafile_entry, node);
3945 canode = ebmb_next(canode);
William Lallemand946580e2022-08-29 18:36:18 +02003946 ebmb_delete(&entry->node);
William Lallemandb0c48272022-04-26 15:44:53 +02003947 ssl_store_delete_cafile_entry(entry);
3948 }
William Lallemandee8530c2020-06-23 18:19:42 +02003949}
William Lallemandda8584c2020-05-14 10:14:37 +02003950
3951/* register cli keywords */
3952static struct cli_kw_list cli_kws = {{ },{
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003953 { { "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 },
3954 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
3955 { { "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 },
3956 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
3957 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
3958 { { "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 },
3959
Amaury Denoyelleb11ad9e2021-05-21 11:01:10 +02003960 { { "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 +02003961 { { "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 +01003962 { { "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 +02003963 { { "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 +01003964 { { "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 +01003965 { { "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 +01003966 { { "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 +02003967
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003968 { { "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 +02003969 { { "set", "ssl", "crl-file", NULL }, "set ssl crl-file <crlfile> <payload> : replace a CRL file", cli_parse_set_crlfile, NULL, NULL },
3970 { { "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 +02003971 { { "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 +02003972 { { "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 +02003973 { { "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 +02003974 { { NULL }, NULL, NULL, NULL }
3975}};
3976
3977INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3978