blob: 341531d174caf27b4dc58cc6bc6abe3b703498ed [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;
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +0100722
723 OCSP_CERTID_free(data->ocsp_cid);
724 data->ocsp_cid = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200725}
726
727/*
728 *
729 * This function copy a cert_key_and_chain in memory
730 *
731 * It's used to try to apply changes on a ckch before committing them, because
732 * most of the time it's not possible to revert those changes
733 *
734 * Return a the dst or NULL
735 */
William Lallemand52ddd992022-11-22 11:51:53 +0100736struct ckch_data *ssl_sock_copy_cert_key_and_chain(struct ckch_data *src,
737 struct ckch_data *dst)
William Lallemand03c331c2020-05-13 10:10:01 +0200738{
William Lallemand6c096142021-02-23 14:45:45 +0100739 if (!src || !dst)
740 return NULL;
741
William Lallemand03c331c2020-05-13 10:10:01 +0200742 if (src->cert) {
743 dst->cert = src->cert;
744 X509_up_ref(src->cert);
745 }
746
747 if (src->key) {
748 dst->key = src->key;
749 EVP_PKEY_up_ref(src->key);
750 }
751
752 if (src->chain) {
753 dst->chain = X509_chain_up_ref(src->chain);
754 }
755
756 if (src->dh) {
Uriah Pollock3cbf09e2022-11-23 16:41:25 +0100757#ifndef USE_OPENSSL_WOLFSSL
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100758 HASSL_DH_up_ref(src->dh);
William Lallemand03c331c2020-05-13 10:10:01 +0200759 dst->dh = src->dh;
Uriah Pollock3cbf09e2022-11-23 16:41:25 +0100760#else
761 dst->dh = wolfSSL_DH_dup(src->dh);
762 if (!dst->dh)
763 goto error;
764#endif
William Lallemand03c331c2020-05-13 10:10:01 +0200765 }
766
767 if (src->sctl) {
768 struct buffer *sctl;
769
770 sctl = calloc(1, sizeof(*sctl));
771 if (!chunk_dup(sctl, src->sctl)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100772 ha_free(&sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200773 goto error;
774 }
775 dst->sctl = sctl;
776 }
777
778 if (src->ocsp_response) {
779 struct buffer *ocsp_response;
780
781 ocsp_response = calloc(1, sizeof(*ocsp_response));
782 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100783 ha_free(&ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200784 goto error;
785 }
786 dst->ocsp_response = ocsp_response;
787 }
788
789 if (src->ocsp_issuer) {
790 X509_up_ref(src->ocsp_issuer);
791 dst->ocsp_issuer = src->ocsp_issuer;
792 }
793
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +0100794 dst->ocsp_cid = OCSP_CERTID_dup(src->ocsp_cid);
795
William Lallemand03c331c2020-05-13 10:10:01 +0200796 return dst;
797
798error:
799
800 /* free everything */
801 ssl_sock_free_cert_key_and_chain_contents(dst);
802
803 return NULL;
804}
805
806/*
807 * return 0 on success or != 0 on failure
808 */
William Lallemand52ddd992022-11-22 11:51:53 +0100809int 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 +0200810{
811 int ret = 1;
812 BIO *in = NULL;
813 X509 *issuer;
814
815 if (buf) {
816 /* reading from a buffer */
817 in = BIO_new_mem_buf(buf, -1);
818 if (in == NULL) {
819 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
820 goto end;
821 }
822
823 } else {
824 /* reading from a file */
825 in = BIO_new(BIO_s_file());
826 if (in == NULL)
827 goto end;
828
829 if (BIO_read_filename(in, path) <= 0)
830 goto end;
831 }
832
833 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
834 if (!issuer) {
835 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
836 err && *err ? *err : "", path);
837 goto end;
838 }
William Lallemand52ddd992022-11-22 11:51:53 +0100839 /* no error, fill data with new context, old context must be free */
840 if (data->ocsp_issuer)
841 X509_free(data->ocsp_issuer);
842 data->ocsp_issuer = issuer;
William Lallemand03c331c2020-05-13 10:10:01 +0200843 ret = 0;
844
845end:
846
847 ERR_clear_error();
848 if (in)
849 BIO_free(in);
850
851 return ret;
852}
853
854/******************** ckch_store functions ***********************************
855 * The ckch_store is a structure used to cache and index the SSL files used in
856 * configuration
857 */
858
859/*
860 * Free a ckch_store, its ckch, its instances and remove it from the ebtree
861 */
862void ckch_store_free(struct ckch_store *store)
863{
864 struct ckch_inst *inst, *inst_s;
865
866 if (!store)
867 return;
868
William Lallemand52ddd992022-11-22 11:51:53 +0100869 ssl_sock_free_cert_key_and_chain_contents(store->data);
William Lallemand03c331c2020-05-13 10:10:01 +0200870
William Lallemand52ddd992022-11-22 11:51:53 +0100871 ha_free(&store->data);
William Lallemand03c331c2020-05-13 10:10:01 +0200872
873 list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) {
874 ckch_inst_free(inst);
875 }
876 ebmb_delete(&store->node);
877 free(store);
878}
879
880/*
881 * create and initialize a ckch_store
882 * <path> is the key name
883 * <nmemb> is the number of store->ckch objects to allocate
884 *
885 * Return a ckch_store or NULL upon failure.
886 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200887struct ckch_store *ckch_store_new(const char *filename)
William Lallemand03c331c2020-05-13 10:10:01 +0200888{
889 struct ckch_store *store;
890 int pathlen;
891
892 pathlen = strlen(filename);
893 store = calloc(1, sizeof(*store) + pathlen + 1);
894 if (!store)
895 return NULL;
896
William Lallemand03c331c2020-05-13 10:10:01 +0200897 memcpy(store->path, filename, pathlen + 1);
898
899 LIST_INIT(&store->ckch_inst);
900 LIST_INIT(&store->crtlist_entry);
901
William Lallemand52ddd992022-11-22 11:51:53 +0100902 store->data = calloc(1, sizeof(*store->data));
903 if (!store->data)
William Lallemand03c331c2020-05-13 10:10:01 +0200904 goto error;
905
906 return store;
907error:
908 ckch_store_free(store);
909 return NULL;
910}
911
912/* allocate and duplicate a ckch_store
913 * Return a new ckch_store or NULL */
914struct ckch_store *ckchs_dup(const struct ckch_store *src)
915{
916 struct ckch_store *dst;
917
William Lallemand6c096142021-02-23 14:45:45 +0100918 if (!src)
919 return NULL;
920
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200921 dst = ckch_store_new(src->path);
Eric Salama6ac61e32021-02-23 16:50:57 +0100922 if (!dst)
923 return NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200924
William Lallemand52ddd992022-11-22 11:51:53 +0100925 if (!ssl_sock_copy_cert_key_and_chain(src->data, dst->data))
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200926 goto error;
William Lallemand03c331c2020-05-13 10:10:01 +0200927
928 return dst;
929
930error:
931 ckch_store_free(dst);
932
933 return NULL;
934}
935
936/*
937 * lookup a path into the ckchs tree.
938 */
939struct ckch_store *ckchs_lookup(char *path)
940{
941 struct ebmb_node *eb;
942
943 eb = ebst_lookup(&ckchs_tree, path);
944 if (!eb)
945 return NULL;
946
947 return ebmb_entry(eb, struct ckch_store, node);
948}
949
950/*
951 * This function allocate a ckch_store and populate it with certificates from files.
952 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200953struct ckch_store *ckchs_load_cert_file(char *path, char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200954{
955 struct ckch_store *ckchs;
956
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200957 ckchs = ckch_store_new(path);
William Lallemand03c331c2020-05-13 10:10:01 +0200958 if (!ckchs) {
959 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
960 goto end;
961 }
William Lallemand03c331c2020-05-13 10:10:01 +0200962
William Lallemand52ddd992022-11-22 11:51:53 +0100963 if (ssl_sock_load_files_into_ckch(path, ckchs->data, err) == 1)
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200964 goto end;
William Lallemand03c331c2020-05-13 10:10:01 +0200965
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200966 /* insert into the ckchs tree */
967 memcpy(ckchs->path, path, strlen(path) + 1);
968 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand03c331c2020-05-13 10:10:01 +0200969 return ckchs;
970
971end:
972 ckch_store_free(ckchs);
973
974 return NULL;
975}
976
William Lallemandfa1d8b42020-05-13 15:46:10 +0200977
978/******************** ckch_inst functions ******************************/
979
980/* unlink a ckch_inst, free all SNIs, free the ckch_inst */
981/* The caller must use the lock of the bind_conf if used with inserted SNIs */
982void ckch_inst_free(struct ckch_inst *inst)
983{
984 struct sni_ctx *sni, *sni_s;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100985 struct ckch_inst_link_ref *link_ref, *link_ref_s;
William Lallemandfa1d8b42020-05-13 15:46:10 +0200986
987 if (inst == NULL)
988 return;
989
990 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
991 SSL_CTX_free(sni->ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +0200992 LIST_DELETE(&sni->by_ckch_inst);
William Lallemandfa1d8b42020-05-13 15:46:10 +0200993 ebmb_delete(&sni->name);
994 free(sni);
995 }
Remi Tricot-Le Bretonf3eedfe2021-01-25 17:19:44 +0100996 SSL_CTX_free(inst->ctx);
997 inst->ctx = NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +0200998 LIST_DELETE(&inst->by_ckchs);
999 LIST_DELETE(&inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001000
William Lallemande0fa91f2022-08-31 14:26:49 +02001001 /* Free the cafile_link_refs list */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001002 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
William Lallemande0fa91f2022-08-31 14:26:49 +02001003 if (link_ref->link && LIST_INLIST(&link_ref->link->list)) {
1004 /* Try to detach and free the ckch_inst_link only if it
1005 * was attached, this way it can be used to loop from
1006 * the caller */
1007 LIST_DEL_INIT(&link_ref->link->list);
1008 ha_free(&link_ref->link);
1009 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001010 LIST_DELETE(&link_ref->list);
1011 free(link_ref);
1012 }
1013
William Lallemandfa1d8b42020-05-13 15:46:10 +02001014 free(inst);
1015}
1016
1017/* Alloc and init a ckch_inst */
1018struct ckch_inst *ckch_inst_new()
1019{
1020 struct ckch_inst *ckch_inst;
1021
1022 ckch_inst = calloc(1, sizeof *ckch_inst);
1023 if (!ckch_inst)
1024 return NULL;
1025
1026 LIST_INIT(&ckch_inst->sni_ctx);
1027 LIST_INIT(&ckch_inst->by_ckchs);
1028 LIST_INIT(&ckch_inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001029 LIST_INIT(&ckch_inst->cafile_link_refs);
William Lallemandfa1d8b42020-05-13 15:46:10 +02001030
1031 return ckch_inst;
1032}
1033
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001034
1035/******************** ssl_store functions ******************************/
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001036struct eb_root cafile_tree = EB_ROOT;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001037
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001038/*
1039 * Returns the cafile_entry found in the cafile_tree indexed by the path 'path'.
1040 * If 'oldest_entry' is 1, returns the "original" cafile_entry (since
1041 * during a set cafile/commit cafile cycle there might be two entries for any
1042 * given path, the original one and the new one set via the CLI but not
1043 * committed yet).
1044 */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001045struct cafile_entry *ssl_store_get_cafile_entry(char *path, int oldest_entry)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001046{
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001047 struct cafile_entry *ca_e = NULL;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001048 struct ebmb_node *eb;
1049
1050 eb = ebst_lookup(&cafile_tree, path);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001051 while (eb) {
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001052 ca_e = ebmb_entry(eb, struct cafile_entry, node);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001053 /* The ebst_lookup in a tree that has duplicates returns the
1054 * oldest entry first. If we want the latest entry, we need to
1055 * iterate over all the duplicates until we find the last one
1056 * (in our case there should never be more than two entries for
1057 * any given path). */
1058 if (oldest_entry)
1059 return ca_e;
1060 eb = ebmb_next_dup(eb);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001061 }
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001062 return ca_e;
1063}
1064
Remi Tricot-Le Breton38c999b2021-02-23 16:28:43 +01001065int ssl_store_add_uncommitted_cafile_entry(struct cafile_entry *entry)
1066{
1067 return (ebst_insert(&cafile_tree, &entry->node) != &entry->node);
1068}
1069
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +01001070X509_STORE* ssl_store_get0_locations_file(char *path)
1071{
1072 struct cafile_entry *ca_e = ssl_store_get_cafile_entry(path, 0);
1073
1074 if (ca_e)
1075 return ca_e->ca_store;
1076
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001077 return NULL;
1078}
1079
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001080/* Create a cafile_entry object, without adding it to the cafile_tree. */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001081struct 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 +01001082{
1083 struct cafile_entry *ca_e;
1084 int pathlen;
1085
1086 pathlen = strlen(path);
1087
1088 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
1089 if (ca_e) {
1090 memcpy(ca_e->path, path, pathlen + 1);
1091 ca_e->ca_store = store;
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001092 ca_e->type = type;
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001093 LIST_INIT(&ca_e->ckch_inst_link);
1094 }
1095 return ca_e;
1096}
1097
William Lallemand62c0b992022-07-29 17:50:58 +02001098
1099/* Duplicate a cafile_entry
1100 * Allocate the X509_STORE and copy the X509 and CRL inside.
1101 *
1102 * Return the newly allocated cafile_entry or NULL.
1103 *
1104 */
1105struct cafile_entry *ssl_store_dup_cafile_entry(struct cafile_entry *src)
1106{
1107 struct cafile_entry *dst = NULL;
1108 X509_STORE *store = NULL;
1109 STACK_OF(X509_OBJECT) *objs;
1110 int i;
1111
1112 if (!src)
1113 return NULL;
1114
1115 if (src->ca_store) {
1116 /* if there was a store in the src, copy it */
1117 store = X509_STORE_new();
1118 if (!store)
1119 goto err;
1120
1121 objs = X509_STORE_get0_objects(src->ca_store);
1122 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
1123 X509 *cert;
1124 X509_CRL *crl;
1125
1126 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
1127 if (cert) {
1128 if (X509_STORE_add_cert(store, cert) == 0) {
1129 /* only exits on error if the error is not about duplicate certificates */
1130 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1131 goto err;
1132 }
1133 }
1134
1135 }
1136 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
1137 if (crl) {
1138 if (X509_STORE_add_crl(store, crl) == 0) {
1139 /* only exits on error if the error is not about duplicate certificates */
1140 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1141 goto err;
1142 }
1143 }
1144
1145 }
1146 }
1147 }
1148 dst = ssl_store_create_cafile_entry(src->path, store, src->type);
1149
1150 return dst;
1151
1152err:
1153 X509_STORE_free(store);
1154 ha_free(&dst);
1155
1156 return NULL;
1157}
1158
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001159/* Delete a cafile_entry. The caller is responsible from removing this entry
1160 * from the cafile_tree first if is was previously added into it. */
1161void ssl_store_delete_cafile_entry(struct cafile_entry *ca_e)
1162{
1163 struct ckch_inst_link *link, *link_s;
1164 if (!ca_e)
1165 return;
1166
1167 X509_STORE_free(ca_e->ca_store);
1168
1169 list_for_each_entry_safe(link, link_s, &ca_e->ckch_inst_link, list) {
1170 struct ckch_inst *inst = link->ckch_inst;
1171 struct ckch_inst_link_ref *link_ref, *link_ref_s;
1172 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
1173 if (link_ref->link == link) {
1174 LIST_DELETE(&link_ref->list);
1175 free(link_ref);
1176 break;
1177 }
1178 }
1179 LIST_DELETE(&link->list);
1180 free(link);
1181 }
1182
1183 free(ca_e);
1184}
1185
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001186/*
William Lallemandd4774d32022-07-29 17:08:02 +02001187 * Fill a cafile_entry <ca_e> X509_STORE ca_e->store out of a buffer <cert_buf>
1188 * instead of out of a file. The <append> field should be set to 1 if you want
1189 * to keep the existing X509_STORE and append data to it.
1190 *
1191 * This function is used when the "set ssl ca-file" cli command is used.
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001192 * It can parse CERTIFICATE sections as well as CRL ones.
1193 * Returns 0 in case of success, 1 otherwise.
William Lallemandd4774d32022-07-29 17:08:02 +02001194 *
1195 * /!\ Warning: If there was an error the X509_STORE could have been modified so it's
1196 * better to not use it after a return 1.
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001197 */
William Lallemandd4774d32022-07-29 17:08:02 +02001198int 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 +01001199{
William Lallemandd4774d32022-07-29 17:08:02 +02001200 BIO *bio = NULL;
1201 STACK_OF(X509_INFO) *infos;
1202 X509_INFO *info;
1203 int i;
1204 int retval = 1;
1205 int retcert = 0;
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001206
1207 if (!ca_e)
1208 return 1;
1209
William Lallemandd4774d32022-07-29 17:08:02 +02001210 if (!append) {
1211 X509_STORE_free(ca_e->ca_store);
1212 ca_e->ca_store = NULL;
1213 }
1214
1215 if (!ca_e->ca_store)
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001216 ca_e->ca_store = X509_STORE_new();
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001217
William Lallemandd4774d32022-07-29 17:08:02 +02001218 if (!ca_e->ca_store)
1219 goto end;
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001220
William Lallemandd4774d32022-07-29 17:08:02 +02001221 bio = BIO_new_mem_buf(cert_buf, strlen(cert_buf));
1222 if (!bio)
1223 goto end;
1224
1225 infos = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
1226 if (!infos)
1227 goto end;
1228
1229 for (i = 0; i < sk_X509_INFO_num(infos) && !retcert; i++) {
1230 info = sk_X509_INFO_value(infos, i);
1231
1232 /* X509_STORE_add_cert and X509_STORE_add_crl return 1 on success */
1233 if (info->x509)
1234 retcert = !X509_STORE_add_cert(ca_e->ca_store, info->x509);
1235 if (!retcert && info->crl)
1236 retcert = !X509_STORE_add_crl(ca_e->ca_store, info->crl);
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001237 }
1238
William Lallemandd4774d32022-07-29 17:08:02 +02001239 /* return an error if we didn't compute all the X509_INFO or if there was none
1240 * set to 0 if everything was right */
1241 if (!(retcert || (i != sk_X509_INFO_num(infos)) || (sk_X509_INFO_num(infos) == 0)))
1242 retval = 0;
1243
1244 /* Cleanup */
1245 sk_X509_INFO_pop_free(infos, X509_INFO_free);
1246
1247end:
1248 BIO_free(bio);
1249
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001250 return retval;
1251}
1252
William Lallemand0f17ab22022-07-19 18:03:16 +02001253/*
1254 * Try to load a ca-file from disk into the ca-file cache.
William Lallemand0a2d6322022-11-24 19:14:19 +01001255 * <shuterror> allows you to to stop emitting the errors.
William Lallemand0f17ab22022-07-19 18:03:16 +02001256 * Return 0 upon error
1257 */
William Lallemand0a2d6322022-11-24 19:14:19 +01001258int __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 +02001259{
1260 X509_STORE *store = ssl_store_get0_locations_file(path);
1261
1262 /* If this function is called by the CLI, we should not call the
1263 * X509_STORE_load_locations function because it performs forbidden disk
1264 * accesses. */
1265 if (!store && create_if_none) {
William Lallemand87fd9942022-04-01 20:12:03 +02001266 STACK_OF(X509_OBJECT) *objs;
1267 int cert_count = 0;
1268 struct stat buf;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001269 struct cafile_entry *ca_e;
William Lallemandc6b17632022-04-01 23:39:37 +02001270 const char *file = NULL;
1271 const char *dir = NULL;
William Lallemand0f17ab22022-07-19 18:03:16 +02001272 unsigned long e;
William Lallemand87fd9942022-04-01 20:12:03 +02001273
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001274 store = X509_STORE_new();
William Lallemand0f17ab22022-07-19 18:03:16 +02001275 if (!store) {
William Lallemand0a2d6322022-11-24 19:14:19 +01001276 if (!shuterror)
1277 ha_alert("Cannot allocate memory!\n");
William Lallemand0f17ab22022-07-19 18:03:16 +02001278 goto err;
1279 }
William Lallemand87fd9942022-04-01 20:12:03 +02001280
William Lallemandc6b17632022-04-01 23:39:37 +02001281 if (strcmp(path, "@system-ca") == 0) {
1282 dir = X509_get_default_cert_dir();
William Lallemand0f17ab22022-07-19 18:03:16 +02001283 if (!dir) {
William Lallemand0a2d6322022-11-24 19:14:19 +01001284 if (!shuterror)
1285 ha_alert("Couldn't get the system CA directory from X509_get_default_cert_dir().\n");
William Lallemand0f17ab22022-07-19 18:03:16 +02001286 goto err;
1287 }
William Lallemand87fd9942022-04-01 20:12:03 +02001288
William Lallemandc6b17632022-04-01 23:39:37 +02001289 } else {
1290
William Lallemand0f17ab22022-07-19 18:03:16 +02001291 if (stat(path, &buf) == -1) {
William Lallemand0a2d6322022-11-24 19:14:19 +01001292 if (!shuterror)
1293 ha_alert("Couldn't open the ca-file '%s' (%s).\n", path, strerror(errno));
William Lallemandc6b17632022-04-01 23:39:37 +02001294 goto err;
William Lallemand0f17ab22022-07-19 18:03:16 +02001295 }
William Lallemandc6b17632022-04-01 23:39:37 +02001296
1297 if (S_ISDIR(buf.st_mode))
1298 dir = path;
1299 else
1300 file = path;
1301 }
William Lallemand87fd9942022-04-01 20:12:03 +02001302
1303 if (file) {
1304 if (!X509_STORE_load_locations(store, file, NULL)) {
William Lallemand0f17ab22022-07-19 18:03:16 +02001305 e = ERR_get_error();
William Lallemand0a2d6322022-11-24 19:14:19 +01001306 if (!shuterror)
1307 ha_alert("Couldn't open the ca-file '%s' (%s).\n", path, ERR_reason_error_string(e));
William Lallemand87fd9942022-04-01 20:12:03 +02001308 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001309 }
William Lallemand80296b42022-04-05 10:19:30 +02001310 } else if (dir) {
William Lallemand87fd9942022-04-01 20:12:03 +02001311 int n, i;
1312 struct dirent **de_list;
1313
1314 n = scandir(dir, &de_list, 0, alphasort);
1315 if (n < 0)
1316 goto err;
1317
1318 for (i= 0; i < n; i++) {
1319 char *end;
1320 struct dirent *de = de_list[i];
1321 BIO *in = NULL;
1322 X509 *ca = NULL;;
1323
William Lallemand43482322022-07-18 18:42:52 +02001324 ERR_clear_error();
1325
William Lallemand87fd9942022-04-01 20:12:03 +02001326 /* we try to load the files that would have
1327 * been loaded in an hashed directory loaded by
1328 * X509_LOOKUP_hash_dir, so according to "man 1
1329 * c_rehash", we should load ".pem", ".crt",
William Lallemande4b93eb2022-05-09 09:29:00 +02001330 * ".cer", or ".crl". Files starting with a dot
1331 * are ignored.
William Lallemand87fd9942022-04-01 20:12:03 +02001332 */
1333 end = strrchr(de->d_name, '.');
William Lallemande4b93eb2022-05-09 09:29:00 +02001334 if (!end || de->d_name[0] == '.' ||
1335 (strcmp(end, ".pem") != 0 &&
1336 strcmp(end, ".crt") != 0 &&
1337 strcmp(end, ".cer") != 0 &&
1338 strcmp(end, ".crl") != 0)) {
William Lallemand87fd9942022-04-01 20:12:03 +02001339 free(de);
1340 continue;
1341 }
1342 in = BIO_new(BIO_s_file());
1343 if (in == NULL)
1344 goto scandir_err;
1345
William Lallemandc6b17632022-04-01 23:39:37 +02001346 chunk_printf(&trash, "%s/%s", dir, de->d_name);
William Lallemand87fd9942022-04-01 20:12:03 +02001347
1348 if (BIO_read_filename(in, trash.area) == 0)
1349 goto scandir_err;
1350
1351 if (PEM_read_bio_X509_AUX(in, &ca, NULL, NULL) == NULL)
1352 goto scandir_err;
1353
William Lallemand43482322022-07-18 18:42:52 +02001354 if (X509_STORE_add_cert(store, ca) == 0) {
1355 /* only exits on error if the error is not about duplicate certificates */
1356 if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) {
1357 goto scandir_err;
1358 }
1359 }
William Lallemand87fd9942022-04-01 20:12:03 +02001360
William Lallemand4cfbf3c2022-04-26 15:57:33 +02001361 X509_free(ca);
William Lallemand87fd9942022-04-01 20:12:03 +02001362 BIO_free(in);
1363 free(de);
1364 continue;
1365
1366scandir_err:
William Lallemand0f17ab22022-07-19 18:03:16 +02001367 e = ERR_get_error();
William Lallemand4cfbf3c2022-04-26 15:57:33 +02001368 X509_free(ca);
William Lallemand87fd9942022-04-01 20:12:03 +02001369 BIO_free(in);
1370 free(de);
William Lallemand0f17ab22022-07-19 18:03:16 +02001371 /* warn if it can load one of the files, but don't abort */
William Lallemand0a2d6322022-11-24 19:14:19 +01001372 if (!shuterror)
1373 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 +02001374
1375 }
1376 free(de_list);
William Lallemand80296b42022-04-05 10:19:30 +02001377 } else {
William Lallemand0a2d6322022-11-24 19:14:19 +01001378 if (!shuterror)
1379 ha_alert("ca-file: couldn't load '%s'\n", path);
William Lallemand80296b42022-04-05 10:19:30 +02001380 goto err;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001381 }
William Lallemand87fd9942022-04-01 20:12:03 +02001382
1383 objs = X509_STORE_get0_objects(store);
1384 cert_count = sk_X509_OBJECT_num(objs);
William Lallemand0f17ab22022-07-19 18:03:16 +02001385 if (cert_count == 0) {
William Lallemand0a2d6322022-11-24 19:14:19 +01001386 if (!shuterror)
1387 ha_warning("ca-file: 0 CA were loaded from '%s'\n", path);
William Lallemand0f17ab22022-07-19 18:03:16 +02001388 }
William Lallemand87fd9942022-04-01 20:12:03 +02001389 ca_e = ssl_store_create_cafile_entry(path, store, type);
William Lallemand0f17ab22022-07-19 18:03:16 +02001390 if (!ca_e) {
William Lallemand0a2d6322022-11-24 19:14:19 +01001391 if (!shuterror)
1392 ha_alert("Cannot allocate memory!\n");
William Lallemand87fd9942022-04-01 20:12:03 +02001393 goto err;
William Lallemand0f17ab22022-07-19 18:03:16 +02001394 }
William Lallemand87fd9942022-04-01 20:12:03 +02001395 ebst_insert(&cafile_tree, &ca_e->node);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001396 }
1397 return (store != NULL);
William Lallemand87fd9942022-04-01 20:12:03 +02001398
1399err:
1400 X509_STORE_free(store);
1401 store = NULL;
1402 return 0;
1403
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001404}
1405
William Lallemand0a2d6322022-11-24 19:14:19 +01001406int ssl_store_load_locations_file(char *path, int create_if_none, enum cafile_type type)
1407{
1408 return __ssl_store_load_locations_file(path, create_if_none, type, 0);
1409}
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001410
William Lallemandda8584c2020-05-14 10:14:37 +02001411/*************************** CLI commands ***********************/
1412
1413/* Type of SSL payloads that can be updated over the CLI */
1414
William Lallemandff8bf982022-03-29 10:44:23 +02001415struct cert_exts cert_exts[] = {
1416 { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
1417 { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemandda8584c2020-05-14 10:14:37 +02001418#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemandff8bf982022-03-29 10:44:23 +02001419 { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001420#endif
Ilya Shipitsinc47d6762021-02-13 11:45:33 +05001421#ifdef HAVE_SSL_SCTL
William Lallemandff8bf982022-03-29 10:44:23 +02001422 { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001423#endif
William Lallemandff8bf982022-03-29 10:44:23 +02001424 { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
1425 { NULL, CERT_TYPE_MAX, NULL },
William Lallemandda8584c2020-05-14 10:14:37 +02001426};
1427
1428
1429/* release function of the `show ssl cert' command */
1430static void cli_release_show_cert(struct appctx *appctx)
1431{
1432 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1433}
1434
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001435/* IO handler of "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001436 * It makes use of a show_cert_ctx context, and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001437 */
William Lallemandda8584c2020-05-14 10:14:37 +02001438static int cli_io_handler_show_cert(struct appctx *appctx)
1439{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001440 struct show_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001441 struct buffer *trash = alloc_trash_chunk();
1442 struct ebmb_node *node;
Christopher Fauletd1d2e4d2022-06-03 16:24:02 +02001443 struct ckch_store *ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001444
1445 if (trash == NULL)
1446 return 1;
1447
Christopher Faulet3e94f5d2022-06-03 10:46:40 +02001448 if (!ctx->old_ckchs && ckchs_transaction.old_ckchs) {
1449 ckchs = ckchs_transaction.old_ckchs;
1450 chunk_appendf(trash, "# transaction\n");
1451 chunk_appendf(trash, "*%s\n", ckchs->path);
1452 if (applet_putchk(appctx, trash) == -1)
1453 goto yield;
1454 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001455 }
1456
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001457 if (!ctx->cur_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02001458 chunk_appendf(trash, "# filename\n");
1459 node = ebmb_first(&ckchs_tree);
1460 } else {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001461 node = &ctx->cur_ckchs->node;
William Lallemandda8584c2020-05-14 10:14:37 +02001462 }
1463 while (node) {
1464 ckchs = ebmb_entry(node, struct ckch_store, node);
William Lallemand5685ccf2020-09-16 16:12:25 +02001465 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001466
1467 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001468 if (applet_putchk(appctx, trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001469 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001470 }
1471
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001472 ctx->cur_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001473 free_trash_chunk(trash);
1474 return 1;
1475yield:
1476
1477 free_trash_chunk(trash);
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001478 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001479 return 0; /* should come back */
1480}
1481
1482/*
1483 * Extract and format the DNS SAN extensions and copy result into a chuink
1484 * Return 0;
1485 */
1486#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1487static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
1488{
1489 int i;
1490 char *str;
1491 STACK_OF(GENERAL_NAME) *names = NULL;
1492
1493 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1494 if (names) {
1495 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
1496 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
1497 if (i > 0)
1498 chunk_appendf(out, ", ");
1499 if (name->type == GEN_DNS) {
1500 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
1501 chunk_appendf(out, "DNS:%s", str);
1502 OPENSSL_free(str);
1503 }
1504 }
1505 }
1506 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
1507 }
1508 return 0;
1509}
1510#endif
1511
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001512/*
1513 * Build the ckch_inst_link that will be chained in the CA file entry and the
1514 * corresponding ckch_inst_link_ref that will be chained in the ckch instance.
1515 * Return 0 in case of success.
1516 */
1517static int do_chain_inst_and_cafile(struct cafile_entry *cafile_entry, struct ckch_inst *ckch_inst)
1518{
1519 struct ckch_inst_link *new_link;
1520 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
1521 struct ckch_inst_link *link = LIST_ELEM(cafile_entry->ckch_inst_link.n,
1522 typeof(link), list);
1523 /* Do not add multiple references to the same
1524 * instance in a cafile_entry */
1525 if (link->ckch_inst == ckch_inst) {
1526 return 1;
1527 }
1528 }
1529
1530 new_link = calloc(1, sizeof(*new_link));
1531 if (new_link) {
1532 struct ckch_inst_link_ref *new_link_ref = calloc(1, sizeof(*new_link_ref));
1533 if (!new_link_ref) {
1534 free(new_link);
1535 return 1;
1536 }
1537
1538 new_link->ckch_inst = ckch_inst;
1539 new_link_ref->link = new_link;
1540 LIST_INIT(&new_link->list);
1541 LIST_INIT(&new_link_ref->list);
1542
1543 LIST_APPEND(&cafile_entry->ckch_inst_link, &new_link->list);
1544 LIST_APPEND(&ckch_inst->cafile_link_refs, &new_link_ref->list);
1545 }
1546
1547 return 0;
1548}
1549
1550
1551/*
1552 * Link a CA file tree entry to the ckch instance that uses it.
1553 * To determine if and which CA file tree entries need to be linked to the
1554 * instance, we follow the same logic performed in ssl_sock_prepare_ctx when
1555 * processing the verify option.
1556 * This function works for a frontend as well as for a backend, depending on the
1557 * configuration parameters given (bind_conf or server).
1558 */
1559void ckch_inst_add_cafile_link(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf,
1560 struct ssl_bind_conf *ssl_conf, const struct server *srv)
1561{
1562 int verify = SSL_VERIFY_NONE;
1563
1564 if (srv) {
1565
1566 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
1567 verify = SSL_VERIFY_PEER;
1568 switch (srv->ssl_ctx.verify) {
1569 case SSL_SOCK_VERIFY_NONE:
1570 verify = SSL_VERIFY_NONE;
1571 break;
1572 case SSL_SOCK_VERIFY_REQUIRED:
1573 verify = SSL_VERIFY_PEER;
1574 break;
1575 }
1576 }
1577 else {
1578 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
1579 case SSL_SOCK_VERIFY_NONE:
1580 verify = SSL_VERIFY_NONE;
1581 break;
1582 case SSL_SOCK_VERIFY_OPTIONAL:
1583 verify = SSL_VERIFY_PEER;
1584 break;
1585 case SSL_SOCK_VERIFY_REQUIRED:
1586 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1587 break;
1588 }
1589 }
1590
1591 if (verify & SSL_VERIFY_PEER) {
1592 struct cafile_entry *ca_file_entry = NULL;
1593 struct cafile_entry *ca_verify_file_entry = NULL;
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001594 struct cafile_entry *crl_file_entry = NULL;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001595 if (srv) {
1596 if (srv->ssl_ctx.ca_file) {
1597 ca_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.ca_file, 0);
1598
1599 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001600 if (srv->ssl_ctx.crl_file) {
1601 crl_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.crl_file, 0);
1602 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001603 }
1604 else {
1605 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
1606 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 +02001607 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 +01001608
1609 if (ca_file)
1610 ca_file_entry = ssl_store_get_cafile_entry(ca_file, 0);
1611 if (ca_verify_file)
1612 ca_verify_file_entry = ssl_store_get_cafile_entry(ca_verify_file, 0);
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001613 if (crl_file)
1614 crl_file_entry = ssl_store_get_cafile_entry(crl_file, 0);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001615 }
1616
1617 if (ca_file_entry) {
1618 /* If we have a ckch instance that is not already in the
1619 * cafile_entry's list, add it to it. */
1620 if (do_chain_inst_and_cafile(ca_file_entry, ckch_inst))
1621 return;
1622
1623 }
1624 if (ca_verify_file_entry && (ca_file_entry != ca_verify_file_entry)) {
1625 /* If we have a ckch instance that is not already in the
1626 * cafile_entry's list, add it to it. */
1627 if (do_chain_inst_and_cafile(ca_verify_file_entry, ckch_inst))
1628 return;
1629 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001630 if (crl_file_entry) {
1631 /* If we have a ckch instance that is not already in the
1632 * cafile_entry's list, add it to it. */
1633 if (do_chain_inst_and_cafile(crl_file_entry, ckch_inst))
1634 return;
1635 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001636 }
1637}
1638
William Lallemandda8584c2020-05-14 10:14:37 +02001639
1640
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001641static int show_cert_detail(X509 *cert, STACK_OF(X509) *chain, struct buffer *out)
William Lallemandda8584c2020-05-14 10:14:37 +02001642{
William Lallemandda8584c2020-05-14 10:14:37 +02001643 BIO *bio = NULL;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001644 struct buffer *tmp = alloc_trash_chunk();
William Lallemandda8584c2020-05-14 10:14:37 +02001645 int i;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001646 int write = -1;
1647 unsigned int len = 0;
1648 X509_NAME *name = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001649
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001650 if (!tmp)
1651 return -1;
William Lallemandda8584c2020-05-14 10:14:37 +02001652
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001653 if (!cert)
William Lallemand5685ccf2020-09-16 16:12:25 +02001654 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02001655
William Lallemand5685ccf2020-09-16 16:12:25 +02001656 if (chain == NULL) {
1657 struct issuer_chain *issuer;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001658 issuer = ssl_get0_issuer_chain(cert);
William Lallemand5685ccf2020-09-16 16:12:25 +02001659 if (issuer) {
1660 chain = issuer->chain;
1661 chunk_appendf(out, "Chain Filename: ");
1662 chunk_appendf(out, "%s\n", issuer->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001663 }
William Lallemand5685ccf2020-09-16 16:12:25 +02001664 }
1665 chunk_appendf(out, "Serial: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001666 if (ssl_sock_get_serial(cert, tmp) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001667 goto end;
1668 dump_binary(out, tmp->area, tmp->data);
1669 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001670
William Lallemand5685ccf2020-09-16 16:12:25 +02001671 chunk_appendf(out, "notBefore: ");
1672 chunk_reset(tmp);
1673 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1674 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001675 if (ASN1_TIME_print(bio, X509_getm_notBefore(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001676 goto end;
1677 write = BIO_read(bio, tmp->area, tmp->size-1);
1678 tmp->area[write] = '\0';
1679 BIO_free(bio);
1680 bio = NULL;
1681 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001682
William Lallemand5685ccf2020-09-16 16:12:25 +02001683 chunk_appendf(out, "notAfter: ");
1684 chunk_reset(tmp);
1685 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1686 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001687 if (ASN1_TIME_print(bio, X509_getm_notAfter(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001688 goto end;
1689 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
1690 goto end;
1691 tmp->area[write] = '\0';
1692 BIO_free(bio);
1693 bio = NULL;
1694 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001695
1696#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand5685ccf2020-09-16 16:12:25 +02001697 chunk_appendf(out, "Subject Alternative Name: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001698 if (ssl_sock_get_san_oneline(cert, out) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001699 goto end;
1700 *(out->area + out->data) = '\0';
1701 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001702#endif
William Lallemand5685ccf2020-09-16 16:12:25 +02001703 chunk_reset(tmp);
1704 chunk_appendf(out, "Algorithm: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001705 if (cert_get_pkey_algo(cert, tmp) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001706 goto end;
1707 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001708
William Lallemand5685ccf2020-09-16 16:12:25 +02001709 chunk_reset(tmp);
1710 chunk_appendf(out, "SHA1 FingerPrint: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001711 if (X509_digest(cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001712 goto end;
1713 tmp->data = len;
1714 dump_binary(out, tmp->area, tmp->data);
1715 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001716
William Lallemand5685ccf2020-09-16 16:12:25 +02001717 chunk_appendf(out, "Subject: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001718 if ((name = X509_get_subject_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001719 goto end;
1720 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1721 goto end;
1722 *(tmp->area + tmp->data) = '\0';
1723 chunk_appendf(out, "%s\n", tmp->area);
1724
1725 chunk_appendf(out, "Issuer: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001726 if ((name = X509_get_issuer_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001727 goto end;
1728 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1729 goto end;
1730 *(tmp->area + tmp->data) = '\0';
1731 chunk_appendf(out, "%s\n", tmp->area);
1732
1733 /* Displays subject of each certificate in the chain */
1734 for (i = 0; i < sk_X509_num(chain); i++) {
1735 X509 *ca = sk_X509_value(chain, i);
1736
1737 chunk_appendf(out, "Chain Subject: ");
1738 if ((name = X509_get_subject_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001739 goto end;
1740 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1741 goto end;
1742 *(tmp->area + tmp->data) = '\0';
1743 chunk_appendf(out, "%s\n", tmp->area);
1744
William Lallemand5685ccf2020-09-16 16:12:25 +02001745 chunk_appendf(out, "Chain Issuer: ");
1746 if ((name = X509_get_issuer_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001747 goto end;
1748 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1749 goto end;
1750 *(tmp->area + tmp->data) = '\0';
1751 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001752 }
1753
1754end:
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001755 if (bio)
1756 BIO_free(bio);
1757 free_trash_chunk(tmp);
1758
1759 return 0;
1760}
1761
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001762#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 +02001763/*
1764 * Build the OCSP tree entry's key for a given ckch_store.
1765 * Returns a negative value in case of error.
1766 */
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +01001767static int ckch_store_build_certid(struct ckch_store *ckch_store, unsigned char certid[OCSP_MAX_CERTID_ASN1_LENGTH], unsigned int *key_length)
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001768{
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001769 unsigned char *p = NULL;
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +01001770 int i;
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001771
1772 if (!key_length)
1773 return -1;
1774
1775 *key_length = 0;
1776
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +01001777 if (!ckch_store->data->ocsp_cid)
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001778 return 0;
1779
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +01001780 i = i2d_OCSP_CERTID(ckch_store->data->ocsp_cid, NULL);
1781 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1782 return 0;
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001783
1784 p = certid;
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +01001785 *key_length = i2d_OCSP_CERTID(ckch_store->data->ocsp_cid, &p);
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001786
1787end:
1788 return *key_length > 0;
1789}
1790#endif
1791
1792/*
1793 * Dump the OCSP certificate key (if it exists) of certificate <ckch> into
1794 * buffer <out>.
1795 * Returns 0 in case of success.
1796 */
1797static int ckch_store_show_ocsp_certid(struct ckch_store *ckch_store, struct buffer *out)
1798{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001799#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 +02001800 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1801 unsigned int key_length = 0;
1802 int i;
1803
1804 if (ckch_store_build_certid(ckch_store, (unsigned char*)key, &key_length) >= 0) {
1805 /* Dump the CERTID info */
1806 chunk_appendf(out, "OCSP Response Key: ");
1807 for (i = 0; i < key_length; ++i) {
1808 chunk_appendf(out, "%02x", key[i]);
1809 }
1810 chunk_appendf(out, "\n");
1811 }
1812#endif
1813
1814 return 0;
1815}
1816
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001817
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001818/* IO handler of the details "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001819 * It uses a struct show_cert_ctx and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001820 */
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001821static int cli_io_handler_show_cert_detail(struct appctx *appctx)
1822{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001823 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001824 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001825 struct buffer *out = alloc_trash_chunk();
1826 int retval = 0;
1827
1828 if (!out)
1829 goto end_no_putchk;
1830
1831 chunk_appendf(out, "Filename: ");
1832 if (ckchs == ckchs_transaction.new_ckchs)
1833 chunk_appendf(out, "*");
1834 chunk_appendf(out, "%s\n", ckchs->path);
1835
1836 chunk_appendf(out, "Status: ");
William Lallemand52ddd992022-11-22 11:51:53 +01001837 if (ckchs->data->cert == NULL)
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001838 chunk_appendf(out, "Empty\n");
1839 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
1840 chunk_appendf(out, "Unused\n");
1841 else
1842 chunk_appendf(out, "Used\n");
1843
William Lallemand52ddd992022-11-22 11:51:53 +01001844 retval = show_cert_detail(ckchs->data->cert, ckchs->data->chain, out);
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001845 if (retval < 0)
1846 goto end_no_putchk;
1847 else if (retval)
1848 goto end;
1849
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001850 ckch_store_show_ocsp_certid(ckchs, out);
1851
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001852end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001853 if (applet_putchk(appctx, out) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001854 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001855
1856end_no_putchk:
William Lallemandda8584c2020-05-14 10:14:37 +02001857 free_trash_chunk(out);
1858 return 1;
1859yield:
William Lallemandda8584c2020-05-14 10:14:37 +02001860 free_trash_chunk(out);
1861 return 0; /* should come back */
1862}
1863
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001864
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001865/* IO handler of the details "show ssl cert <filename.ocsp>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001866 * It uses a show_cert_ctx.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001867 */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001868static int cli_io_handler_show_cert_ocsp_detail(struct appctx *appctx)
1869{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001870#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL)
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001871 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001872 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001873 struct buffer *out = alloc_trash_chunk();
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001874 int from_transaction = ctx->transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001875
1876 if (!out)
1877 goto end_no_putchk;
1878
1879 /* If we try to display an ongoing transaction's OCSP response, we
1880 * need to dump the ckch's ocsp_response buffer directly.
1881 * Otherwise, we must rebuild the certificate's certid in order to
1882 * look for the current OCSP response in the tree. */
William Lallemand52ddd992022-11-22 11:51:53 +01001883 if (from_transaction && ckchs->data->ocsp_response) {
1884 if (ssl_ocsp_response_print(ckchs->data->ocsp_response, out))
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001885 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001886 }
1887 else {
1888 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1889 unsigned int key_length = 0;
1890
1891 if (ckch_store_build_certid(ckchs, (unsigned char*)key, &key_length) < 0)
1892 goto end_no_putchk;
1893
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001894 if (ssl_get_ocspresponse_detail(key, out))
1895 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001896 }
1897
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001898 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001899 goto yield;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001900
1901end_no_putchk:
1902 free_trash_chunk(out);
1903 return 1;
1904yield:
1905 free_trash_chunk(out);
1906 return 0; /* should come back */
1907#else
1908 return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n");
1909#endif
1910}
1911
William Lallemandda8584c2020-05-14 10:14:37 +02001912/* parsing function for 'show ssl cert [certfile]' */
1913static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
1914{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001915 struct show_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02001916 struct ckch_store *ckchs;
1917
1918 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1919 return cli_err(appctx, "Can't allocate memory!\n");
1920
1921 /* The operations on the CKCH architecture are locked so we can
1922 * manipulate ckch_store and ckch_inst */
1923 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1924 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
1925
1926 /* check if there is a certificate to lookup */
1927 if (*args[3]) {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001928 int show_ocsp_detail = 0;
1929 int from_transaction = 0;
1930 char *end;
1931
1932 /* We manage the special case "certname.ocsp" through which we
1933 * can show the details of an OCSP response. */
1934 end = strrchr(args[3], '.');
1935 if (end && strcmp(end+1, "ocsp") == 0) {
1936 *end = '\0';
1937 show_ocsp_detail = 1;
1938 }
1939
William Lallemandda8584c2020-05-14 10:14:37 +02001940 if (*args[3] == '*') {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001941 from_transaction = 1;
William Lallemandda8584c2020-05-14 10:14:37 +02001942 if (!ckchs_transaction.new_ckchs)
1943 goto error;
1944
1945 ckchs = ckchs_transaction.new_ckchs;
1946
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001947 if (strcmp(args[3] + 1, ckchs->path) != 0)
William Lallemandda8584c2020-05-14 10:14:37 +02001948 goto error;
1949
1950 } else {
1951 if ((ckchs = ckchs_lookup(args[3])) == NULL)
1952 goto error;
1953
1954 }
1955
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001956 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001957 /* use the IO handler that shows details */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001958 if (show_ocsp_detail) {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001959 ctx->transaction = from_transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001960 appctx->io_handler = cli_io_handler_show_cert_ocsp_detail;
1961 }
1962 else
1963 appctx->io_handler = cli_io_handler_show_cert_detail;
William Lallemandda8584c2020-05-14 10:14:37 +02001964 }
1965
1966 return 0;
1967
1968error:
1969 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1970 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
1971}
1972
1973/* release function of the `set ssl cert' command, free things and unlock the spinlock */
1974static void cli_release_commit_cert(struct appctx *appctx)
1975{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02001976 struct commit_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001977
1978 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulet9d56e242022-05-31 16:37:01 +02001979 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
1980 if (ctx->new_ckchs)
1981 ckch_store_free(ctx->new_ckchs);
1982 ha_free(&ctx->err);
William Lallemandda8584c2020-05-14 10:14:37 +02001983}
1984
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001985
1986/*
1987 * Rebuild a new instance 'new_inst' based on an old instance 'ckchi' and a
1988 * specific ckch_store.
1989 * Returns 0 in case of success, 1 otherwise.
1990 */
William Lallemande60c7d62022-03-30 11:26:15 +02001991int ckch_inst_rebuild(struct ckch_store *ckch_store, struct ckch_inst *ckchi,
1992 struct ckch_inst **new_inst, char **err)
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001993{
1994 int retval = 0;
1995 int errcode = 0;
1996 struct sni_ctx *sc0, *sc0s;
1997 char **sni_filter = NULL;
1998 int fcount = 0;
1999
2000 if (ckchi->crtlist_entry) {
2001 sni_filter = ckchi->crtlist_entry->filters;
2002 fcount = ckchi->crtlist_entry->fcount;
2003 }
2004
2005 if (ckchi->is_server_instance)
2006 errcode |= ckch_inst_new_load_srv_store(ckch_store->path, ckch_store, new_inst, err);
2007 else
2008 errcode |= ckch_inst_new_load_store(ckch_store->path, ckch_store, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, new_inst, err);
2009
2010 if (errcode & ERR_CODE)
2011 return 1;
2012
2013 /* if the previous ckchi was used as the default */
2014 if (ckchi->is_default)
2015 (*new_inst)->is_default = 1;
2016
2017 (*new_inst)->is_server_instance = ckchi->is_server_instance;
2018 (*new_inst)->server = ckchi->server;
2019 /* Create a new SSL_CTX and link it to the new instance. */
2020 if ((*new_inst)->is_server_instance) {
2021 retval = ssl_sock_prep_srv_ctx_and_inst(ckchi->server, (*new_inst)->ctx, (*new_inst));
2022 if (retval)
2023 return 1;
2024 }
2025
2026 /* create the link to the crtlist_entry */
2027 (*new_inst)->crtlist_entry = ckchi->crtlist_entry;
2028
2029 /* we need to initialize the SSL_CTX generated */
2030 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
2031 list_for_each_entry_safe(sc0, sc0s, &(*new_inst)->sni_ctx, by_ckch_inst) {
2032 if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
2033 errcode |= ssl_sock_prep_ctx_and_inst(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, *new_inst, err);
2034 if (errcode & ERR_CODE)
2035 return 1;
2036 }
2037 }
2038
2039 return 0;
2040}
2041
2042/*
2043 * Load all the new SNIs of a newly built ckch instance in the trees, or replace
2044 * a server's main ckch instance.
2045 */
2046static void __ssl_sock_load_new_ckch_instance(struct ckch_inst *ckchi)
2047{
2048 /* The bind_conf will be null on server ckch_instances. */
2049 if (ckchi->is_server_instance) {
2050 int i;
2051 /* a lock is needed here since we have to free the SSL cache */
2052 HA_RWLOCK_WRLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
2053 /* free the server current SSL_CTX */
2054 SSL_CTX_free(ckchi->server->ssl_ctx.ctx);
2055 /* Actual ssl context update */
2056 SSL_CTX_up_ref(ckchi->ctx);
2057 ckchi->server->ssl_ctx.ctx = ckchi->ctx;
2058 ckchi->server->ssl_ctx.inst = ckchi;
2059
2060 /* flush the session cache of the server */
2061 for (i = 0; i < global.nbthread; i++) {
William Lallemandce990332021-11-23 15:15:09 +01002062 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].sni);
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002063 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].ptr);
2064 }
2065 HA_RWLOCK_WRUNLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
2066
2067 } else {
2068 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
2069 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
2070 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
2071 }
2072}
2073
2074/*
2075 * Delete a ckch instance that was replaced after a CLI command.
2076 */
2077static void __ckch_inst_free_locked(struct ckch_inst *ckchi)
2078{
2079 if (ckchi->is_server_instance) {
2080 /* no lock for servers */
2081 ckch_inst_free(ckchi);
2082 } else {
2083 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
2084
2085 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
2086 ckch_inst_free(ckchi);
2087 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
2088 }
2089}
2090
William Lallemand3b5a3a62022-03-29 14:29:31 +02002091/* Replace a ckch_store in the ckch tree and insert the whole dependencies,
2092* then free the previous dependencies and store.
2093* Used in the case of a certificate update.
2094*
2095* Every dependencies must allocated before using this function.
2096*
2097* This function can't fail as it only update pointers, and does not alloc anything.
2098*
2099* /!\ This function must be used under the ckch lock. /!\
2100*
2101* - Insert every dependencies (SNI, crtlist_entry, ckch_inst, etc)
2102* - Delete the old ckch_store from the tree
2103* - Insert the new ckch_store
2104* - Free the old dependencies and the old ckch_store
2105*/
2106void ckch_store_replace(struct ckch_store *old_ckchs, struct ckch_store *new_ckchs)
2107{
2108 struct crtlist_entry *entry;
2109 struct ckch_inst *ckchi, *ckchis;
2110
2111 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
2112 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
2113 ebpt_delete(&entry->node);
2114 /* change the ptr and reinsert the node */
2115 entry->node.key = new_ckchs;
2116 ebpt_insert(&entry->crtlist->entries, &entry->node);
2117 }
2118 /* insert the new ckch_insts in the crtlist_entry */
2119 list_for_each_entry(ckchi, &new_ckchs->ckch_inst, by_ckchs) {
2120 if (ckchi->crtlist_entry)
2121 LIST_INSERT(&ckchi->crtlist_entry->ckch_inst, &ckchi->by_crtlist_entry);
2122 }
2123 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2124 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
2125 __ssl_sock_load_new_ckch_instance(ckchi);
2126 }
2127 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
2128 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
2129 __ckch_inst_free_locked(ckchi);
2130 }
2131
2132 ckch_store_free(old_ckchs);
2133 ebst_insert(&ckchs_tree, &new_ckchs->node);
2134}
2135
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002136
William Lallemandda8584c2020-05-14 10:14:37 +02002137/*
2138 * This function tries to create the new ckch_inst and their SNIs
William Lallemand30fcca12022-03-30 12:03:12 +02002139 *
2140 * /!\ don't forget to update __hlua_ckch_commit() if you changes things there. /!\
William Lallemandda8584c2020-05-14 10:14:37 +02002141 */
2142static int cli_io_handler_commit_cert(struct appctx *appctx)
2143{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002144 struct commit_cert_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002145 struct stconn *sc = appctx_sc(appctx);
William Lallemandda8584c2020-05-14 10:14:37 +02002146 int y = 0;
William Lallemandda8584c2020-05-14 10:14:37 +02002147 struct ckch_store *old_ckchs, *new_ckchs = NULL;
William Lallemand3b5a3a62022-03-29 14:29:31 +02002148 struct ckch_inst *ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002149
Christopher Faulet87633c32023-04-03 18:32:50 +02002150 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02002151 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
Christopher Faulet9d56e242022-05-31 16:37:01 +02002152 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02002153
2154 while (1) {
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002155 switch (ctx->state) {
2156 case CERT_ST_INIT:
William Lallemandda8584c2020-05-14 10:14:37 +02002157 /* This state just print the update message */
Christopher Faulet9d56e242022-05-31 16:37:01 +02002158 chunk_printf(&trash, "Committing %s", ckchs_transaction.path);
2159 if (applet_putchk(appctx, &trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02002160 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002161
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002162 ctx->state = CERT_ST_GEN;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002163 __fallthrough;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002164 case CERT_ST_GEN:
William Lallemandda8584c2020-05-14 10:14:37 +02002165 /*
2166 * This state generates the ckch instances with their
2167 * sni_ctxs and SSL_CTX.
2168 *
2169 * Since the SSL_CTX generation can be CPU consumer, we
2170 * yield every 10 instances.
2171 */
2172
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002173 old_ckchs = ctx->old_ckchs;
2174 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002175
William Lallemandda8584c2020-05-14 10:14:37 +02002176 /* get the next ckchi to regenerate */
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002177 ckchi = ctx->next_ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002178 /* we didn't start yet, set it to the first elem */
2179 if (ckchi == NULL)
2180 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
2181
2182 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
2183 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
2184 struct ckch_inst *new_inst;
William Lallemandda8584c2020-05-14 10:14:37 +02002185
Christopher Faulet9d56e242022-05-31 16:37:01 +02002186 /* save the next ckchi to compute in case of yield */
2187 ctx->next_ckchi = ckchi;
2188
William Lallemandda8584c2020-05-14 10:14:37 +02002189 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2190 if (y >= 10) {
Christopher Faulet9d56e242022-05-31 16:37:01 +02002191 applet_have_more_data(appctx); /* let's come back later */
William Lallemandda8584c2020-05-14 10:14:37 +02002192 goto yield;
2193 }
2194
Christopher Faulet9d56e242022-05-31 16:37:01 +02002195 /* display one dot per new instance */
2196 if (applet_putstr(appctx, ".") == -1)
2197 goto yield;
2198
2199 ctx->err = NULL;
2200 if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &ctx->err)) {
2201 ctx->state = CERT_ST_ERROR;
William Lallemandda8584c2020-05-14 10:14:37 +02002202 goto error;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002203 }
William Lallemandda8584c2020-05-14 10:14:37 +02002204
William Lallemandda8584c2020-05-14 10:14:37 +02002205 /* link the new ckch_inst to the duplicate */
Willy Tarreau2b718102021-04-21 07:32:39 +02002206 LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002207 y++;
2208 }
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002209 ctx->state = CERT_ST_INSERT;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002210 __fallthrough;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002211 case CERT_ST_INSERT:
William Lallemandda8584c2020-05-14 10:14:37 +02002212 /* The generation is finished, we can insert everything */
2213
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002214 old_ckchs = ctx->old_ckchs;
2215 new_ckchs = ctx->new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002216
William Lallemand3b5a3a62022-03-29 14:29:31 +02002217 /* insert everything and remove the previous objects */
2218 ckch_store_replace(old_ckchs, new_ckchs);
Christopher Faulet9d56e242022-05-31 16:37:01 +02002219 ctx->new_ckchs = ctx->old_ckchs = NULL;
2220 ctx->state = CERT_ST_SUCCESS;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002221 __fallthrough;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002222 case CERT_ST_SUCCESS:
2223 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2224 goto yield;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002225 ctx->state = CERT_ST_FIN;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002226 __fallthrough;
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002227 case CERT_ST_FIN:
William Lallemandda8584c2020-05-14 10:14:37 +02002228 /* we achieved the transaction, we can set everything to NULL */
William Lallemandda8584c2020-05-14 10:14:37 +02002229 ckchs_transaction.new_ckchs = NULL;
2230 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002231 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002232 goto end;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002233
2234 case CERT_ST_ERROR:
2235 error:
2236 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2237 if (applet_putchk(appctx, &trash) == -1)
2238 goto yield;
2239 ctx->state = CERT_ST_FIN;
2240 break;
William Lallemandda8584c2020-05-14 10:14:37 +02002241 }
2242 }
2243end:
William Lallemandda8584c2020-05-14 10:14:37 +02002244 /* success: call the release function and don't come back */
2245 return 1;
Christopher Faulet9d56e242022-05-31 16:37:01 +02002246
William Lallemandda8584c2020-05-14 10:14:37 +02002247yield:
William Lallemandda8584c2020-05-14 10:14:37 +02002248 return 0; /* should come back */
William Lallemandda8584c2020-05-14 10:14:37 +02002249}
2250
2251/*
2252 * Parsing function of 'commit ssl cert'
2253 */
2254static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
2255{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002256 struct commit_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02002257 char *err = NULL;
2258
2259 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2260 return 1;
2261
2262 if (!*args[3])
2263 return cli_err(appctx, "'commit ssl cert expects a filename\n");
2264
2265 /* The operations on the CKCH architecture are locked so we can
2266 * manipulate ckch_store and ckch_inst */
2267 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2268 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
2269
2270 if (!ckchs_transaction.path) {
2271 memprintf(&err, "No ongoing transaction! !\n");
2272 goto error;
2273 }
2274
2275 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2276 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
2277 goto error;
2278 }
2279
William Lallemand5685ccf2020-09-16 16:12:25 +02002280 /* if a certificate is here, a private key must be here too */
William Lallemand52ddd992022-11-22 11:51:53 +01002281 if (ckchs_transaction.new_ckchs->data->cert && !ckchs_transaction.new_ckchs->data->key) {
William Lallemand5685ccf2020-09-16 16:12:25 +02002282 memprintf(&err, "The transaction must contain at least a certificate and a private key!\n");
2283 goto error;
2284 }
William Lallemanda9419522020-06-24 16:26:41 +02002285
William Lallemand52ddd992022-11-22 11:51:53 +01002286 if (!X509_check_private_key(ckchs_transaction.new_ckchs->data->cert, ckchs_transaction.new_ckchs->data->key)) {
William Lallemand5685ccf2020-09-16 16:12:25 +02002287 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
2288 goto error;
William Lallemandda8584c2020-05-14 10:14:37 +02002289 }
2290
2291 /* init the appctx structure */
Willy Tarreaucb1b4ed2022-05-05 08:15:27 +02002292 ctx->state = CERT_ST_INIT;
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002293 ctx->next_ckchi = NULL;
2294 ctx->new_ckchs = ckchs_transaction.new_ckchs;
2295 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002296
2297 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
2298 return 0;
2299
2300error:
2301
2302 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2303 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2304
2305 return cli_dynerr(appctx, err);
2306}
2307
2308
2309
2310
2311/*
2312 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
Willy Tarreau329f4b42022-05-04 20:05:55 +02002313 * It uses a set_cert_ctx context, and ckchs_transaction under a lock.
William Lallemandda8584c2020-05-14 10:14:37 +02002314 */
2315static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
2316{
2317 struct ckch_store *new_ckchs = NULL;
2318 struct ckch_store *old_ckchs = NULL;
2319 char *err = NULL;
2320 int i;
William Lallemandda8584c2020-05-14 10:14:37 +02002321 int errcode = 0;
2322 char *end;
William Lallemandff8bf982022-03-29 10:44:23 +02002323 struct cert_exts *cert_ext = &cert_exts[0]; /* default one, PEM */
William Lallemand52ddd992022-11-22 11:51:53 +01002324 struct ckch_data *data;
William Lallemandda8584c2020-05-14 10:14:37 +02002325 struct buffer *buf;
2326
2327 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2328 return 1;
2329
William Lallemandda8584c2020-05-14 10:14:37 +02002330 if (!*args[3] || !payload)
2331 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
2332
2333 /* The operations on the CKCH architecture are locked so we can
2334 * manipulate ckch_store and ckch_inst */
2335 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2336 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
2337
William Lallemand5ba80d62021-05-04 16:17:27 +02002338 if ((buf = alloc_trash_chunk()) == NULL) {
2339 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2340 errcode |= ERR_ALERT | ERR_FATAL;
2341 goto end;
2342 }
William Lallemande5ff4ad2020-06-08 09:40:37 +02002343
William Lallemandda8584c2020-05-14 10:14:37 +02002344 if (!chunk_strcpy(buf, args[3])) {
2345 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2346 errcode |= ERR_ALERT | ERR_FATAL;
2347 goto end;
2348 }
2349
2350 /* check which type of file we want to update */
William Lallemandff8bf982022-03-29 10:44:23 +02002351 for (i = 0; cert_exts[i].ext != NULL; i++) {
William Lallemandda8584c2020-05-14 10:14:37 +02002352 end = strrchr(buf->area, '.');
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002353 if (end && *cert_exts[i].ext && (strcmp(end + 1, cert_exts[i].ext) == 0)) {
William Lallemandda8584c2020-05-14 10:14:37 +02002354 *end = '\0';
William Lallemand089c1382020-10-23 17:35:12 +02002355 buf->data = strlen(buf->area);
William Lallemandff8bf982022-03-29 10:44:23 +02002356 cert_ext = &cert_exts[i];
William Lallemandda8584c2020-05-14 10:14:37 +02002357 break;
2358 }
2359 }
2360
William Lallemandda8584c2020-05-14 10:14:37 +02002361 /* if there is an ongoing transaction */
2362 if (ckchs_transaction.path) {
William Lallemandda8584c2020-05-14 10:14:37 +02002363 /* if there is an ongoing transaction, check if this is the same file */
2364 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
William Lallemand089c1382020-10-23 17:35:12 +02002365 /* we didn't find the transaction, must try more cases below */
2366
2367 /* 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 +02002368 if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
William Lallemand089c1382020-10-23 17:35:12 +02002369 if (!chunk_strcat(buf, ".crt")) {
2370 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2371 errcode |= ERR_ALERT | ERR_FATAL;
2372 goto end;
2373 }
2374
2375 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
2376 /* remove .crt of the error message */
2377 *(b_orig(buf) + b_data(buf) + strlen(".crt")) = '\0';
2378 b_sub(buf, strlen(".crt"));
2379
2380 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
2381 errcode |= ERR_ALERT | ERR_FATAL;
2382 goto end;
2383 }
2384 }
William Lallemandda8584c2020-05-14 10:14:37 +02002385 }
2386
Christopher Faulet24a20b92022-06-03 11:50:40 +02002387 old_ckchs = ckchs_transaction.new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002388
2389 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002390
William Lallemand95fefa12020-09-09 12:01:33 +02002391 /* lookup for the certificate in the tree */
Christopher Faulet24a20b92022-06-03 11:50:40 +02002392 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002393
Christopher Faulet24a20b92022-06-03 11:50:40 +02002394 if (!old_ckchs) {
William Lallemand089c1382020-10-23 17:35:12 +02002395 /* 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 +02002396 if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
William Lallemand089c1382020-10-23 17:35:12 +02002397 if (!chunk_strcat(buf, ".crt")) {
2398 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2399 errcode |= ERR_ALERT | ERR_FATAL;
2400 goto end;
2401 }
Christopher Faulet24a20b92022-06-03 11:50:40 +02002402 old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002403 }
2404 }
William Lallemandda8584c2020-05-14 10:14:37 +02002405 }
2406
Christopher Faulet24a20b92022-06-03 11:50:40 +02002407 if (!old_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02002408 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
2409 err ? err : "");
2410 errcode |= ERR_ALERT | ERR_FATAL;
2411 goto end;
2412 }
2413
William Lallemandda8584c2020-05-14 10:14:37 +02002414 /* duplicate the ckch store */
2415 new_ckchs = ckchs_dup(old_ckchs);
2416 if (!new_ckchs) {
2417 memprintf(&err, "%sCannot allocate memory!\n",
2418 err ? err : "");
2419 errcode |= ERR_ALERT | ERR_FATAL;
2420 goto end;
2421 }
2422
William Lallemand52ddd992022-11-22 11:51:53 +01002423 data = new_ckchs->data;
William Lallemandda8584c2020-05-14 10:14:37 +02002424
2425 /* appply the change on the duplicate */
William Lallemand52ddd992022-11-22 11:51:53 +01002426 if (cert_ext->load(buf->area, payload, data, &err) != 0) {
William Lallemandda8584c2020-05-14 10:14:37 +02002427 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
2428 errcode |= ERR_ALERT | ERR_FATAL;
2429 goto end;
2430 }
2431
William Lallemandda8584c2020-05-14 10:14:37 +02002432 /* we succeed, we can save the ckchs in the transaction */
2433
2434 /* if there wasn't a transaction, update the old ckchs */
2435 if (!ckchs_transaction.old_ckchs) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002436 ckchs_transaction.old_ckchs = old_ckchs;
2437 ckchs_transaction.path = old_ckchs->path;
William Lallemandda8584c2020-05-14 10:14:37 +02002438 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
2439 } else {
2440 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
2441
2442 }
2443
2444 /* free the previous ckchs if there was a transaction */
2445 ckch_store_free(ckchs_transaction.new_ckchs);
2446
Christopher Faulet24a20b92022-06-03 11:50:40 +02002447 ckchs_transaction.new_ckchs = new_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02002448
2449
2450 /* creates the SNI ctxs later in the IO handler */
2451
2452end:
2453 free_trash_chunk(buf);
2454
2455 if (errcode & ERR_CODE) {
Christopher Faulet24a20b92022-06-03 11:50:40 +02002456 ckch_store_free(new_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02002457 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2458 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2459 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002460 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2461 return cli_dynmsg(appctx, LOG_NOTICE, err);
2462 }
2463 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
2464}
2465
2466/* parsing function of 'abort ssl cert' */
2467static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
2468{
2469 char *err = NULL;
2470
2471 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2472 return 1;
2473
2474 if (!*args[3])
2475 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
2476
2477 /* The operations on the CKCH architecture are locked so we can
2478 * manipulate ckch_store and ckch_inst */
2479 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2480 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2481
2482 if (!ckchs_transaction.path) {
2483 memprintf(&err, "No ongoing transaction!\n");
2484 goto error;
2485 }
2486
2487 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2488 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
2489 goto error;
2490 }
2491
2492 /* Only free the ckchs there, because the SNI and instances were not generated yet */
2493 ckch_store_free(ckchs_transaction.new_ckchs);
2494 ckchs_transaction.new_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002495 ckchs_transaction.old_ckchs = NULL;
Christopher Faulete2ef4dd2022-05-31 18:07:59 +02002496 ckchs_transaction.path = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002497
2498 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2499
2500 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2501 return cli_dynmsg(appctx, LOG_NOTICE, err);
2502
2503error:
2504 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2505
2506 return cli_dynerr(appctx, err);
2507}
2508
2509/* parsing function of 'new ssl cert' */
2510static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
2511{
2512 struct ckch_store *store;
2513 char *err = NULL;
2514 char *path;
2515
2516 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2517 return 1;
2518
2519 if (!*args[3])
2520 return cli_err(appctx, "'new ssl cert' expects a filename\n");
2521
2522 path = args[3];
2523
2524 /* The operations on the CKCH architecture are locked so we can
2525 * manipulate ckch_store and ckch_inst */
2526 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2527 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
2528
2529 store = ckchs_lookup(path);
2530 if (store != NULL) {
2531 memprintf(&err, "Certificate '%s' already exists!\n", path);
2532 store = NULL; /* we don't want to free it */
2533 goto error;
2534 }
2535 /* we won't support multi-certificate bundle here */
William Lallemandbd8e6ed2020-09-16 16:08:08 +02002536 store = ckch_store_new(path);
William Lallemandda8584c2020-05-14 10:14:37 +02002537 if (!store) {
2538 memprintf(&err, "unable to allocate memory.\n");
2539 goto error;
2540 }
2541
2542 /* insert into the ckchs tree */
2543 ebst_insert(&ckchs_tree, &store->node);
2544 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
2545
2546 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2547 return cli_dynmsg(appctx, LOG_NOTICE, err);
2548error:
2549 free(store);
2550 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2551 return cli_dynerr(appctx, err);
2552}
2553
2554/* parsing function of 'del ssl cert' */
2555static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
2556{
2557 struct ckch_store *store;
2558 char *err = NULL;
2559 char *filename;
2560
2561 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2562 return 1;
2563
2564 if (!*args[3])
2565 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
2566
2567 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2568 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
2569
2570 filename = args[3];
2571
Christopher Faulet926fefc2022-05-31 18:04:25 +02002572 if (ckchs_transaction.path && strcmp(ckchs_transaction.path, filename) == 0) {
2573 memprintf(&err, "ongoing transaction for the certificate '%s'", filename);
2574 goto error;
2575 }
2576
William Lallemandda8584c2020-05-14 10:14:37 +02002577 store = ckchs_lookup(filename);
2578 if (store == NULL) {
2579 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
2580 goto error;
2581 }
2582 if (!LIST_ISEMPTY(&store->ckch_inst)) {
2583 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
2584 goto error;
2585 }
2586
2587 ebmb_delete(&store->node);
2588 ckch_store_free(store);
2589
2590 memprintf(&err, "Certificate '%s' deleted!\n", filename);
2591
2592 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2593 return cli_dynmsg(appctx, LOG_NOTICE, err);
2594
2595error:
2596 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
2597 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2598 return cli_dynerr(appctx, err);
2599}
2600
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002601
Remi Tricot-Le Breton9f40fe02021-03-16 16:21:27 +01002602
2603/* parsing function of 'new ssl ca-file' */
2604static int cli_parse_new_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2605{
2606 struct cafile_entry *cafile_entry;
2607 char *err = NULL;
2608 char *path;
2609
2610 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2611 return 1;
2612
2613 if (!*args[3])
2614 return cli_err(appctx, "'new ssl ca-file' expects a filename\n");
2615
2616 path = args[3];
2617
2618 /* The operations on the CKCH architecture are locked so we can
2619 * manipulate ckch_store and ckch_inst */
2620 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2621 return cli_err(appctx, "Can't create a CA file!\nOperations on certificates are currently locked!\n");
2622
2623 cafile_entry = ssl_store_get_cafile_entry(path, 0);
2624 if (cafile_entry) {
2625 memprintf(&err, "CA file '%s' already exists!\n", path);
2626 goto error;
2627 }
2628
2629 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CERT);
2630 if (!cafile_entry) {
2631 memprintf(&err, "%sCannot allocate memory!\n",
2632 err ? err : "");
2633 goto error;
2634 }
2635
2636 /* Add the newly created cafile_entry to the tree so that
2637 * any new ckch instance created from now can use it. */
2638 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
2639 goto error;
2640
2641 memprintf(&err, "New CA file created '%s'!\n", path);
2642
2643 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2644 return cli_dynmsg(appctx, LOG_NOTICE, err);
2645error:
2646 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2647 return cli_dynerr(appctx, err);
2648}
2649
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002650/*
2651 * Parsing function of `set ssl ca-file`
2652 */
2653static int cli_parse_set_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2654{
Christopher Faulet132c5952022-06-03 11:56:26 +02002655 struct cafile_entry *old_cafile_entry = NULL;
2656 struct cafile_entry *new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002657 char *err = NULL;
2658 int errcode = 0;
2659 struct buffer *buf;
William Lallemand62c0b992022-07-29 17:50:58 +02002660 int add_cmd = 0;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002661
2662 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2663 return 1;
2664
William Lallemand62c0b992022-07-29 17:50:58 +02002665 /* this is "add ssl ca-file" */
2666 if (*args[0] == 'a')
2667 add_cmd = 1;
2668
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002669 if (!*args[3] || !payload)
2670 return cli_err(appctx, "'set ssl ca-file expects a filename and CAs as a payload\n");
2671
2672 /* The operations on the CKCH architecture are locked so we can
2673 * manipulate ckch_store and ckch_inst */
2674 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2675 return cli_err(appctx, "Can't update the CA file!\nOperations on certificates are currently locked!\n");
2676
2677 if ((buf = alloc_trash_chunk()) == NULL) {
2678 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2679 errcode |= ERR_ALERT | ERR_FATAL;
2680 goto end;
2681 }
2682
2683 if (!chunk_strcpy(buf, args[3])) {
2684 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2685 errcode |= ERR_ALERT | ERR_FATAL;
2686 goto end;
2687 }
2688
Christopher Faulet132c5952022-06-03 11:56:26 +02002689 old_cafile_entry = NULL;
2690 new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002691
2692 /* if there is an ongoing transaction */
2693 if (cafile_transaction.path) {
2694 /* if there is an ongoing transaction, check if this is the same file */
2695 if (strcmp(cafile_transaction.path, buf->area) != 0) {
2696 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, buf->area);
2697 errcode |= ERR_ALERT | ERR_FATAL;
2698 goto end;
2699 }
Christopher Faulet132c5952022-06-03 11:56:26 +02002700 old_cafile_entry = cafile_transaction.old_cafile_entry;
William Lallemand62c0b992022-07-29 17:50:58 +02002701 } else {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002702 /* lookup for the certificate in the tree */
Christopher Faulet132c5952022-06-03 11:56:26 +02002703 old_cafile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002704 }
2705
Christopher Faulet132c5952022-06-03 11:56:26 +02002706 if (!old_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002707 memprintf(&err, "%sCan't replace a CA file which is not referenced by the configuration!\n",
2708 err ? err : "");
2709 errcode |= ERR_ALERT | ERR_FATAL;
2710 goto end;
2711 }
2712
William Lallemand62c0b992022-07-29 17:50:58 +02002713 /* if the transaction is new, duplicate the old_ca_file_entry, otherwise duplicate the cafile in the current transaction */
2714 if (cafile_transaction.new_cafile_entry)
2715 new_cafile_entry = ssl_store_dup_cafile_entry(cafile_transaction.new_cafile_entry);
2716 else
2717 new_cafile_entry = ssl_store_dup_cafile_entry(old_cafile_entry);
2718
Christopher Faulet132c5952022-06-03 11:56:26 +02002719 if (!new_cafile_entry) {
William Lallemand62c0b992022-07-29 17:50:58 +02002720 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002721 errcode |= ERR_ALERT | ERR_FATAL;
2722 goto end;
2723 }
2724
William Lallemand62c0b992022-07-29 17:50:58 +02002725 /* Fill the new entry with the new CAs. The add_cmd variable determine
2726 if we flush the X509_STORE or not */
2727 if (ssl_store_load_ca_from_buf(new_cafile_entry, payload, add_cmd)) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002728 memprintf(&err, "%sInvalid payload\n", err ? err : "");
2729 errcode |= ERR_ALERT | ERR_FATAL;
2730 goto end;
2731 }
2732
2733 /* we succeed, we can save the ca in the transaction */
2734
2735 /* if there wasn't a transaction, update the old CA */
2736 if (!cafile_transaction.old_cafile_entry) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002737 cafile_transaction.old_cafile_entry = old_cafile_entry;
2738 cafile_transaction.path = old_cafile_entry->path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002739 err = memprintf(&err, "transaction created for CA %s!\n", cafile_transaction.path);
2740 } else {
2741 err = memprintf(&err, "transaction updated for CA %s!\n", cafile_transaction.path);
2742 }
2743
2744 /* free the previous CA if there was a transaction */
2745 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2746
Christopher Faulet132c5952022-06-03 11:56:26 +02002747 cafile_transaction.new_cafile_entry = new_cafile_entry;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002748
2749 /* creates the SNI ctxs later in the IO handler */
2750
2751end:
2752 free_trash_chunk(buf);
2753
2754 if (errcode & ERR_CODE) {
Christopher Faulet132c5952022-06-03 11:56:26 +02002755 ssl_store_delete_cafile_entry(new_cafile_entry);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002756 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2757 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2758 } else {
2759
2760 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2761 return cli_dynmsg(appctx, LOG_NOTICE, err);
2762 }
2763}
2764
2765
2766/*
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002767 * Parsing function of 'commit ssl ca-file'.
2768 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl crl-file".
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002769 */
2770static int cli_parse_commit_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2771{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002772 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002773 char *err = NULL;
2774
2775 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2776 return 1;
2777
2778 if (!*args[3])
2779 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
2780
2781 /* The operations on the CKCH architecture are locked so we can
2782 * manipulate ckch_store and ckch_inst */
2783 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2784 return cli_err(appctx, "Can't commit the CA file!\nOperations on certificates are currently locked!\n");
2785
2786 if (!cafile_transaction.path) {
2787 memprintf(&err, "No ongoing transaction! !\n");
2788 goto error;
2789 }
2790
2791 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2792 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, args[3]);
2793 goto error;
2794 }
2795 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002796 ctx->state = CACRL_ST_INIT;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002797 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002798 ctx->old_entry = cafile_transaction.old_cafile_entry;
2799 ctx->new_entry = cafile_transaction.new_cafile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002800 ctx->cafile_type = CAFILE_CERT;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002801
2802 return 0;
2803
2804error:
2805
2806 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2807 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2808
2809 return cli_dynerr(appctx, err);
2810}
2811
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002812/*
2813 * This function tries to create new ckch instances and their SNIs using a newly
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002814 * set certificate authority (CA file) or a newly set Certificate Revocation
2815 * List (CRL), depending on the command being called.
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002816 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002817static int cli_io_handler_commit_cafile_crlfile(struct appctx *appctx)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002818{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002819 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002820 struct stconn *sc = appctx_sc(appctx);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002821 int y = 0;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002822 struct cafile_entry *old_cafile_entry = ctx->old_entry;
2823 struct cafile_entry *new_cafile_entry = ctx->new_entry;
William Lallemand0bfa3e72022-08-30 17:32:38 +02002824 struct ckch_inst_link *ckchi_link;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002825 char *path;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002826
Christopher Faulet87633c32023-04-03 18:32:50 +02002827 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02002828 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002829 goto end;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002830
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002831 /* The ctx was already validated by the ca-file/crl-file parsing
2832 * function. Entries can only be NULL in CACRL_ST_SUCCESS or
2833 * CACRL_ST_FIN states
2834 */
2835 switch (ctx->cafile_type) {
2836 case CAFILE_CERT:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002837 path = cafile_transaction.path;
2838 break;
2839 case CAFILE_CRL:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002840 path = crlfile_transaction.path;
2841 break;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002842 default:
Willy Tarreaud543ae02022-06-22 05:40:25 +02002843 path = NULL;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002844 goto error;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002845 }
2846
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002847 while (1) {
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002848 switch (ctx->state) {
2849 case CACRL_ST_INIT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002850 /* This state just print the update message */
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002851 chunk_printf(&trash, "Committing %s", path);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002852 if (applet_putchk(appctx, &trash) == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002853 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002854
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002855 ctx->state = CACRL_ST_GEN;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002856 __fallthrough;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002857 case CACRL_ST_GEN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002858 /*
2859 * This state generates the ckch instances with their
2860 * sni_ctxs and SSL_CTX.
2861 *
2862 * Since the SSL_CTX generation can be CPU consumer, we
2863 * yield every 10 instances.
2864 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002865
2866 /* get the next ckchi to regenerate */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002867 ckchi_link = ctx->next_ckchi_link;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002868
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002869 /* we didn't start yet, set it to the first elem */
2870 if (ckchi_link == NULL) {
2871 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2872 /* Add the newly created cafile_entry to the tree so that
2873 * any new ckch instance created from now can use it. */
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002874 if (ssl_store_add_uncommitted_cafile_entry(new_cafile_entry)) {
2875 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002876 goto error;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002877 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002878 }
2879
2880 list_for_each_entry_from(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002881 struct ckch_inst *new_inst;
2882
2883 /* save the next ckchi to compute */
2884 ctx->next_ckchi_link = ckchi_link;
2885
2886 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2887 if (y >= 10) {
2888 applet_have_more_data(appctx); /* let's come back later */
2889 goto yield;
2890 }
2891
2892 /* display one dot per new instance */
2893 if (applet_putstr(appctx, ".") == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002894 goto yield;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002895
2896 /* Rebuild a new ckch instance that uses the same ckch_store
2897 * than a reference ckchi instance but will use a new CA file. */
2898 ctx->err = NULL;
2899 if (ckch_inst_rebuild(ckchi_link->ckch_inst->ckch_store, ckchi_link->ckch_inst, &new_inst, &ctx->err)) {
2900 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002901 goto error;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002902 }
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002903
2904 y++;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002905 }
2906
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002907 ctx->state = CACRL_ST_INSERT;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002908 __fallthrough;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002909 case CACRL_ST_INSERT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002910 /* The generation is finished, we can insert everything */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002911
2912 /* insert the new ckch_insts in the crtlist_entry */
2913 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2914 if (ckchi_link->ckch_inst->crtlist_entry)
2915 LIST_INSERT(&ckchi_link->ckch_inst->crtlist_entry->ckch_inst,
2916 &ckchi_link->ckch_inst->by_crtlist_entry);
2917 }
2918
2919 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2920 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2921 __ssl_sock_load_new_ckch_instance(ckchi_link->ckch_inst);
2922 }
2923
William Lallemande0fa91f2022-08-31 14:26:49 +02002924 /* delete the old sni_ctx, the old ckch_insts
2925 * and the ckch_store. ckch_inst_free() also
2926 * manipulates the list so it's cleaner to loop
2927 * until it's empty */
2928 while (!LIST_ISEMPTY(&old_cafile_entry->ckch_inst_link)) {
2929 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2930
2931 LIST_DEL_INIT(&ckchi_link->list); /* must reinit because ckch_inst checks the list */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002932 __ckch_inst_free_locked(ckchi_link->ckch_inst);
William Lallemande0fa91f2022-08-31 14:26:49 +02002933 free(ckchi_link);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002934 }
2935
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002936 /* Remove the old cafile entry from the tree */
2937 ebmb_delete(&old_cafile_entry->node);
2938 ssl_store_delete_cafile_entry(old_cafile_entry);
2939
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002940 ctx->old_entry = ctx->new_entry = NULL;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002941 ctx->state = CACRL_ST_SUCCESS;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002942 __fallthrough;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002943 case CACRL_ST_SUCCESS:
2944 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2945 goto yield;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002946 ctx->state = CACRL_ST_FIN;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002947 __fallthrough;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002948 case CACRL_ST_FIN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002949 /* we achieved the transaction, we can set everything to NULL */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002950 switch (ctx->cafile_type) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002951 case CAFILE_CERT:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002952 cafile_transaction.old_cafile_entry = NULL;
2953 cafile_transaction.new_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002954 cafile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002955 break;
2956 case CAFILE_CRL:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002957 crlfile_transaction.old_crlfile_entry = NULL;
2958 crlfile_transaction.new_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002959 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002960 break;
2961 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002962 goto end;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002963
2964 case CACRL_ST_ERROR:
2965 error:
2966 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2967 if (applet_putchk(appctx, &trash) == -1)
2968 goto yield;
2969 ctx->state = CACRL_ST_FIN;
2970 break;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002971 }
2972 }
2973end:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002974 /* success: call the release function and don't come back */
2975 return 1;
2976yield:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002977 return 0; /* should come back */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002978}
2979
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002980
2981/* parsing function of 'abort ssl ca-file' */
2982static int cli_parse_abort_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2983{
2984 char *err = NULL;
2985
2986 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2987 return 1;
2988
2989 if (!*args[3])
2990 return cli_err(appctx, "'abort ssl ca-file' expects a filename\n");
2991
2992 /* The operations on the CKCH architecture are locked so we can
2993 * manipulate ckch_store and ckch_inst */
2994 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2995 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2996
2997 if (!cafile_transaction.path) {
2998 memprintf(&err, "No ongoing transaction!\n");
2999 goto error;
3000 }
3001
3002 if (strcmp(cafile_transaction.path, args[3]) != 0) {
3003 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", cafile_transaction.path, args[3]);
3004 goto error;
3005 }
3006
3007 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3008 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
3009 cafile_transaction.new_cafile_entry = NULL;
3010 cafile_transaction.old_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02003011 cafile_transaction.path = NULL;
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01003012
3013 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3014
3015 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3016 return cli_dynmsg(appctx, LOG_NOTICE, err);
3017
3018error:
3019 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3020
3021 return cli_dynerr(appctx, err);
3022}
3023
Willy Tarreau821c3b02022-05-04 15:47:39 +02003024/* release function of the `commit ssl ca-file' command, free things and unlock the spinlock.
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003025 * It uses a commit_cacrlfile_ctx context.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003026 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003027static void cli_release_commit_cafile(struct appctx *appctx)
3028{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003029 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003030 struct cafile_entry *new_cafile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003031
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003032 /* Remove the uncommitted cafile_entry from the tree. */
3033 if (new_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003034 ebmb_delete(&new_cafile_entry->node);
3035 ssl_store_delete_cafile_entry(new_cafile_entry);
3036 }
3037 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003038 ha_free(&ctx->err);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003039}
3040
3041
Willy Tarreau821c3b02022-05-04 15:47:39 +02003042/* IO handler of details "show ssl ca-file <filename[:index]>".
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003043 * It uses a show_cafile_ctx context, and the global
3044 * cafile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003045 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003046static int cli_io_handler_show_cafile_detail(struct appctx *appctx)
3047{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003048 struct show_cafile_ctx *ctx = appctx->svcctx;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003049 struct cafile_entry *cafile_entry = ctx->cur_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003050 struct buffer *out = alloc_trash_chunk();
William Lallemand03a32e52022-04-26 18:17:15 +02003051 int i = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003052 X509 *cert;
3053 STACK_OF(X509_OBJECT) *objs;
3054 int retval = 0;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003055 int ca_index = ctx->ca_index;
3056 int show_all = ctx->show_all;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003057
3058 if (!out)
3059 goto end_no_putchk;
3060
3061 chunk_appendf(out, "Filename: ");
3062 if (cafile_entry == cafile_transaction.new_cafile_entry)
3063 chunk_appendf(out, "*");
3064 chunk_appendf(out, "%s\n", cafile_entry->path);
3065
3066 chunk_appendf(out, "Status: ");
3067 if (!cafile_entry->ca_store)
3068 chunk_appendf(out, "Empty\n");
3069 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3070 chunk_appendf(out, "Unused\n");
3071 else
3072 chunk_appendf(out, "Used\n");
3073
3074 if (!cafile_entry->ca_store)
3075 goto end;
3076
3077 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
William Lallemand03a32e52022-04-26 18:17:15 +02003078 for (i = ca_index; i < sk_X509_OBJECT_num(objs); i++) {
3079
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003080 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
3081 if (!cert)
3082 continue;
3083
William Lallemand03a32e52022-04-26 18:17:15 +02003084 /* file starts at line 1 */
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003085 chunk_appendf(out, " \nCertificate #%d:\n", i+1);
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003086 retval = show_cert_detail(cert, NULL, out);
3087 if (retval < 0)
3088 goto end_no_putchk;
William Lallemand03a32e52022-04-26 18:17:15 +02003089 else if (retval)
3090 goto yield;
3091
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003092 if (applet_putchk(appctx, out) == -1)
William Lallemand03a32e52022-04-26 18:17:15 +02003093 goto yield;
William Lallemand03a32e52022-04-26 18:17:15 +02003094
3095 if (!show_all) /* only need to dump one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003096 goto end;
3097 }
3098
3099end:
William Lallemand03a32e52022-04-26 18:17:15 +02003100 free_trash_chunk(out);
3101 return 1; /* end, don't come back */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003102
3103end_no_putchk:
3104 free_trash_chunk(out);
3105 return 1;
3106yield:
William Lallemand03a32e52022-04-26 18:17:15 +02003107 /* save the current state */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003108 ctx->ca_index = i;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003109 free_trash_chunk(out);
3110 return 0; /* should come back */
3111}
3112
3113
Willy Tarreau06305792022-05-04 15:57:30 +02003114/* parsing function for 'show ssl ca-file [cafile[:index]]'.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003115 * It prepares a show_cafile_ctx context, and checks the global
3116 * cafile_transaction under the ckch_lock (read only).
Willy Tarreau06305792022-05-04 15:57:30 +02003117 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003118static int cli_parse_show_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3119{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003120 struct show_cafile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003121 struct cafile_entry *cafile_entry;
William Lallemand03a32e52022-04-26 18:17:15 +02003122 int ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003123 char *colons;
3124 char *err = NULL;
3125
3126 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3127 return cli_err(appctx, "Can't allocate memory!\n");
3128
3129 /* The operations on the CKCH architecture are locked so we can
3130 * manipulate ckch_store and ckch_inst */
3131 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3132 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3133
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003134 ctx->show_all = 1; /* show all certificates */
3135 ctx->ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003136 /* check if there is a certificate to lookup */
3137 if (*args[3]) {
3138
3139 /* Look for an optional CA index after the CA file name */
3140 colons = strchr(args[3], ':');
3141 if (colons) {
3142 char *endptr;
3143
3144 ca_index = strtol(colons + 1, &endptr, 10);
3145 /* Indexes start at 1 */
3146 if (colons + 1 == endptr || *endptr != '\0' || ca_index <= 0) {
3147 memprintf(&err, "wrong CA index after colons in '%s'!", args[3]);
3148 goto error;
3149 }
3150 *colons = '\0';
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003151 ctx->ca_index = ca_index - 1; /* we start counting at 0 in the ca_store, but at 1 on the CLI */
3152 ctx->show_all = 0; /* show only one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003153 }
3154
3155 if (*args[3] == '*') {
3156 if (!cafile_transaction.new_cafile_entry)
3157 goto error;
3158
3159 cafile_entry = cafile_transaction.new_cafile_entry;
3160
3161 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3162 goto error;
3163
3164 } else {
3165 /* Get the "original" cafile_entry and not the
3166 * uncommitted one if it exists. */
3167 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CERT)
3168 goto error;
3169 }
3170
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003171 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003172 /* use the IO handler that shows details */
3173 appctx->io_handler = cli_io_handler_show_cafile_detail;
3174 }
3175
3176 return 0;
3177
3178error:
3179 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3180 if (err)
3181 return cli_dynerr(appctx, err);
3182 return cli_err(appctx, "Can't display the CA file : Not found!\n");
3183}
3184
3185
3186/* release function of the 'show ssl ca-file' command */
3187static void cli_release_show_cafile(struct appctx *appctx)
3188{
3189 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3190}
3191
3192
3193/* This function returns the number of certificates in a cafile_entry. */
3194static int get_certificate_count(struct cafile_entry *cafile_entry)
3195{
3196 int cert_count = 0;
3197 STACK_OF(X509_OBJECT) *objs;
3198
3199 if (cafile_entry && cafile_entry->ca_store) {
3200 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3201 if (objs)
3202 cert_count = sk_X509_OBJECT_num(objs);
3203 }
3204 return cert_count;
3205}
3206
3207/* IO handler of "show ssl ca-file". The command taking a specific CA file name
Willy Tarreau821c3b02022-05-04 15:47:39 +02003208 * is managed in cli_io_handler_show_cafile_detail.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003209 * It uses a show_cafile_ctx and the global cafile_transaction.new_cafile_entry
3210 * in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003211 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003212static int cli_io_handler_show_cafile(struct appctx *appctx)
3213{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003214 struct show_cafile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003215 struct buffer *trash = alloc_trash_chunk();
3216 struct ebmb_node *node;
Christopher Faulet677cb4f2022-06-03 16:25:35 +02003217 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003218
3219 if (trash == NULL)
3220 return 1;
3221
Christopher Faulet5a2154b2022-06-03 10:42:48 +02003222 if (!ctx->old_cafile_entry && cafile_transaction.old_cafile_entry) {
3223 chunk_appendf(trash, "# transaction\n");
3224 chunk_appendf(trash, "*%s", cafile_transaction.old_cafile_entry->path);
3225 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_transaction.new_cafile_entry));
3226 if (applet_putchk(appctx, trash) == -1)
3227 goto yield;
3228 ctx->old_cafile_entry = cafile_transaction.new_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003229 }
3230
3231 /* First time in this io_handler. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003232 if (!ctx->cur_cafile_entry) {
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003233 chunk_appendf(trash, "# filename\n");
3234 node = ebmb_first(&cafile_tree);
3235 } else {
3236 /* We yielded during a previous call. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003237 node = &ctx->cur_cafile_entry->node;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003238 }
3239
3240 while (node) {
3241 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3242 if (cafile_entry->type == CAFILE_CERT) {
3243 chunk_appendf(trash, "%s", cafile_entry->path);
3244
3245 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_entry));
3246 }
3247
3248 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003249 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003250 goto yield;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003251 }
3252
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003253 ctx->cur_cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003254 free_trash_chunk(trash);
3255 return 1;
3256yield:
3257
3258 free_trash_chunk(trash);
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003259 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003260 return 0; /* should come back */
3261}
3262
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003263/* parsing function of 'del ssl ca-file' */
3264static int cli_parse_del_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3265{
3266 struct cafile_entry *cafile_entry;
3267 char *err = NULL;
3268 char *filename;
3269
3270 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3271 return 1;
3272
3273 if (!*args[3])
3274 return cli_err(appctx, "'del ssl ca-file' expects a CA file name\n");
3275
3276 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3277 return cli_err(appctx, "Can't delete the CA file!\nOperations on certificates are currently locked!\n");
3278
3279 filename = args[3];
3280
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003281 if (cafile_transaction.path && strcmp(cafile_transaction.path, filename) == 0) {
3282 memprintf(&err, "ongoing transaction for the CA file '%s'", filename);
3283 goto error;
3284 }
3285
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003286 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3287 if (!cafile_entry) {
3288 memprintf(&err, "CA file '%s' doesn't exist!\n", filename);
3289 goto error;
3290 }
3291
3292 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3293 memprintf(&err, "CA file '%s' in use, can't be deleted!\n", filename);
3294 goto error;
3295 }
3296
3297 /* Remove the cafile_entry from the tree */
3298 ebmb_delete(&cafile_entry->node);
3299 ssl_store_delete_cafile_entry(cafile_entry);
3300
3301 memprintf(&err, "CA file '%s' deleted!\n", filename);
3302
3303 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3304 return cli_dynmsg(appctx, LOG_NOTICE, err);
3305
3306error:
3307 memprintf(&err, "Can't remove the CA file: %s\n", err ? err : "");
3308 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3309 return cli_dynerr(appctx, err);
3310}
3311
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003312/* parsing function of 'new ssl crl-file' */
3313static int cli_parse_new_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3314{
3315 struct cafile_entry *cafile_entry;
3316 char *err = NULL;
3317 char *path;
3318
3319 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3320 return 1;
3321
3322 if (!*args[3])
3323 return cli_err(appctx, "'new ssl crl-file' expects a filename\n");
3324
3325 path = args[3];
3326
3327 /* The operations on the CKCH architecture are locked so we can
3328 * manipulate ckch_store and ckch_inst */
3329 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003330 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 +02003331
3332 cafile_entry = ssl_store_get_cafile_entry(path, 0);
3333 if (cafile_entry) {
3334 memprintf(&err, "CRL file '%s' already exists!\n", path);
3335 goto error;
3336 }
3337
3338 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CRL);
3339 if (!cafile_entry) {
3340 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3341 goto error;
3342 }
3343
3344 /* Add the newly created cafile_entry to the tree so that
3345 * any new ckch instance created from now can use it. */
3346 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
3347 goto error;
3348
3349 memprintf(&err, "New CRL file created '%s'!\n", path);
3350
3351 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3352 return cli_dynmsg(appctx, LOG_NOTICE, err);
3353error:
3354 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3355 return cli_dynerr(appctx, err);
3356}
3357
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003358/* Parsing function of `set ssl crl-file` */
3359static int cli_parse_set_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3360{
Christopher Faulet1f90f332022-06-03 16:34:30 +02003361 struct cafile_entry *old_crlfile_entry = NULL;
3362 struct cafile_entry *new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003363 char *err = NULL;
3364 int errcode = 0;
3365 struct buffer *buf;
3366
3367 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3368 return 1;
3369
3370 if (!*args[3] || !payload)
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003371 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 +02003372
3373 /* The operations on the CKCH architecture are locked so we can
3374 * manipulate ckch_store and ckch_inst */
3375 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3376 return cli_err(appctx, "Can't update the CRL file!\nOperations on certificates are currently locked!\n");
3377
3378 if ((buf = alloc_trash_chunk()) == NULL) {
3379 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3380 errcode |= ERR_ALERT | ERR_FATAL;
3381 goto end;
3382 }
3383
3384 if (!chunk_strcpy(buf, args[3])) {
3385 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3386 errcode |= ERR_ALERT | ERR_FATAL;
3387 goto end;
3388 }
3389
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003390 old_crlfile_entry = NULL;
3391 new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003392
3393 /* if there is an ongoing transaction */
3394 if (crlfile_transaction.path) {
3395 /* if there is an ongoing transaction, check if this is the same file */
3396 if (strcmp(crlfile_transaction.path, buf->area) != 0) {
3397 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, buf->area);
3398 errcode |= ERR_ALERT | ERR_FATAL;
3399 goto end;
3400 }
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003401 old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003402 }
3403 else {
3404 /* lookup for the certificate in the tree */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003405 old_crlfile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003406 }
3407
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003408 if (!old_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003409 memprintf(&err, "%sCan't replace a CRL file which is not referenced by the configuration!\n",
3410 err ? err : "");
3411 errcode |= ERR_ALERT | ERR_FATAL;
3412 goto end;
3413 }
3414
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003415 /* Create a new cafile_entry without adding it to the cafile tree. */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003416 new_crlfile_entry = ssl_store_create_cafile_entry(old_crlfile_entry->path, NULL, CAFILE_CRL);
3417 if (!new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003418 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3419 errcode |= ERR_ALERT | ERR_FATAL;
3420 goto end;
3421 }
3422
3423 /* Fill the new entry with the new CRL. */
William Lallemandd4774d32022-07-29 17:08:02 +02003424 if (ssl_store_load_ca_from_buf(new_crlfile_entry, payload, 0)) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003425 memprintf(&err, "%sInvalid payload\n", err ? err : "");
3426 errcode |= ERR_ALERT | ERR_FATAL;
3427 goto end;
3428 }
3429
3430 /* we succeed, we can save the crl in the transaction */
3431
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003432 /* if there wasn't a transaction, update the old CRL */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003433 if (!crlfile_transaction.old_crlfile_entry) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003434 crlfile_transaction.old_crlfile_entry = old_crlfile_entry;
3435 crlfile_transaction.path = old_crlfile_entry->path;
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003436 err = memprintf(&err, "transaction created for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003437 } else {
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003438 err = memprintf(&err, "transaction updated for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003439 }
3440
3441 /* free the previous CRL file if there was a transaction */
3442 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3443
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003444 crlfile_transaction.new_crlfile_entry = new_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003445
3446 /* creates the SNI ctxs later in the IO handler */
3447
3448end:
3449 free_trash_chunk(buf);
3450
3451 if (errcode & ERR_CODE) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003452 ssl_store_delete_cafile_entry(new_crlfile_entry);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003453 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3454 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
3455 } else {
3456
3457 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3458 return cli_dynmsg(appctx, LOG_NOTICE, err);
3459 }
3460}
3461
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003462/* Parsing function of 'commit ssl crl-file'.
3463 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl ca-file".
3464 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003465static int cli_parse_commit_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3466{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003467 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003468 char *err = NULL;
3469
3470 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3471 return 1;
3472
3473 if (!*args[3])
3474 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
3475
3476 /* The operations on the CKCH architecture are locked so we can
3477 * manipulate ckch_store and ckch_inst */
3478 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3479 return cli_err(appctx, "Can't commit the CRL file!\nOperations on certificates are currently locked!\n");
3480
3481 if (!crlfile_transaction.path) {
3482 memprintf(&err, "No ongoing transaction! !\n");
3483 goto error;
3484 }
3485
3486 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3487 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, args[3]);
3488 goto error;
3489 }
3490 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02003491 ctx->state = CACRL_ST_INIT;
Christopher Fauletf814c4a2022-06-03 11:32:05 +02003492 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003493 ctx->old_entry = crlfile_transaction.old_crlfile_entry;
3494 ctx->new_entry = crlfile_transaction.new_crlfile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003495 ctx->cafile_type = CAFILE_CRL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003496
3497 return 0;
3498
3499error:
3500
3501 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3502 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
3503
3504 return cli_dynerr(appctx, err);
3505}
3506
3507
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003508/* release function of the `commit ssl crl-file' command, free things and unlock the spinlock.
3509 * it uses a commit_cacrlfile_ctx that's the same as for "commit ssl ca-file".
3510 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003511static void cli_release_commit_crlfile(struct appctx *appctx)
3512{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003513 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003514 struct cafile_entry *new_crlfile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003515
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003516 /* Remove the uncommitted cafile_entry from the tree. */
3517 if (new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003518 ebmb_delete(&new_crlfile_entry->node);
3519 ssl_store_delete_cafile_entry(new_crlfile_entry);
3520 }
3521 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003522 ha_free(&ctx->err);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003523}
3524
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003525/* parsing function of 'del ssl crl-file' */
3526static int cli_parse_del_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3527{
3528 struct cafile_entry *cafile_entry;
3529 char *err = NULL;
3530 char *filename;
3531
3532 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3533 return 1;
3534
3535 if (!*args[3])
3536 return cli_err(appctx, "'del ssl crl-file' expects a CRL file name\n");
3537
3538 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3539 return cli_err(appctx, "Can't delete the CRL file!\nOperations on certificates are currently locked!\n");
3540
3541 filename = args[3];
3542
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003543 if (crlfile_transaction.path && strcmp(crlfile_transaction.path, filename) == 0) {
3544 memprintf(&err, "ongoing transaction for the CRL file '%s'", filename);
3545 goto error;
3546 }
3547
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003548 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3549 if (!cafile_entry) {
3550 memprintf(&err, "CRL file '%s' doesn't exist!\n", filename);
3551 goto error;
3552 }
3553 if (cafile_entry->type != CAFILE_CRL) {
3554 memprintf(&err, "'del ssl crl-file' does not work on CA files!\n");
3555 goto error;
3556 }
3557
3558 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3559 memprintf(&err, "CRL file '%s' in use, can't be deleted!\n", filename);
3560 goto error;
3561 }
3562
3563 /* Remove the cafile_entry from the tree */
3564 ebmb_delete(&cafile_entry->node);
3565 ssl_store_delete_cafile_entry(cafile_entry);
3566
3567 memprintf(&err, "CRL file '%s' deleted!\n", filename);
3568
3569 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3570 return cli_dynmsg(appctx, LOG_NOTICE, err);
3571
3572error:
3573 memprintf(&err, "Can't remove the CRL file: %s\n", err ? err : "");
3574 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3575 return cli_dynerr(appctx, err);
3576}
3577
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003578/* parsing function of 'abort ssl crl-file' */
3579static int cli_parse_abort_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3580{
3581 char *err = NULL;
3582
3583 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3584 return 1;
3585
3586 if (!*args[3])
3587 return cli_err(appctx, "'abort ssl crl-file' expects a filename\n");
3588
3589 /* The operations on the CKCH architecture are locked so we can
3590 * manipulate ckch_store and ckch_inst */
3591 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3592 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
3593
3594 if (!crlfile_transaction.path) {
3595 memprintf(&err, "No ongoing transaction!\n");
3596 goto error;
3597 }
3598
3599 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3600 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", crlfile_transaction.path, args[3]);
3601 goto error;
3602 }
3603
3604 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3605 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3606 crlfile_transaction.new_crlfile_entry = NULL;
3607 crlfile_transaction.old_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02003608 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003609
3610 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3611
3612 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3613 return cli_dynmsg(appctx, LOG_NOTICE, err);
3614
3615error:
3616 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3617
3618 return cli_dynerr(appctx, err);
3619}
3620
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003621
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003622/*
3623 * Display a Certificate Resignation List's information.
3624 * The information displayed is inspired by the output of 'openssl crl -in
3625 * crl.pem -text'.
3626 * Returns 0 in case of success.
3627 */
3628static int show_crl_detail(X509_CRL *crl, struct buffer *out)
3629{
3630 BIO *bio = NULL;
3631 struct buffer *tmp = alloc_trash_chunk();
3632 long version;
3633 X509_NAME *issuer;
3634 int write = -1;
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003635#ifndef USE_OPENSSL_WOLFSSL
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003636 STACK_OF(X509_REVOKED) *rev = NULL;
3637 X509_REVOKED *rev_entry = NULL;
3638 int i;
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003639#endif
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003640
3641 if (!tmp)
3642 return -1;
3643
3644 if ((bio = BIO_new(BIO_s_mem())) == NULL)
3645 goto end;
3646
3647 /* Version (as displayed by 'openssl crl') */
3648 version = X509_CRL_get_version(crl);
3649 chunk_appendf(out, "Version %ld\n", version + 1);
3650
3651 /* Signature Algorithm */
3652 chunk_appendf(out, "Signature Algorithm: %s\n", OBJ_nid2ln(X509_CRL_get_signature_nid(crl)));
3653
3654 /* Issuer */
3655 chunk_appendf(out, "Issuer: ");
3656 if ((issuer = X509_CRL_get_issuer(crl)) == NULL)
3657 goto end;
3658 if ((ssl_sock_get_dn_oneline(issuer, tmp)) == -1)
3659 goto end;
3660 *(tmp->area + tmp->data) = '\0';
3661 chunk_appendf(out, "%s\n", tmp->area);
3662
3663 /* Last Update */
3664 chunk_appendf(out, "Last Update: ");
3665 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003666 if (BIO_reset(bio) == -1)
3667 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003668 if (ASN1_TIME_print(bio, X509_CRL_get0_lastUpdate(crl)) == 0)
3669 goto end;
3670 write = BIO_read(bio, tmp->area, tmp->size-1);
3671 tmp->area[write] = '\0';
3672 chunk_appendf(out, "%s\n", tmp->area);
3673
3674
3675 /* Next Update */
3676 chunk_appendf(out, "Next Update: ");
3677 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003678 if (BIO_reset(bio) == -1)
3679 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003680 if (ASN1_TIME_print(bio, X509_CRL_get0_nextUpdate(crl)) == 0)
3681 goto end;
3682 write = BIO_read(bio, tmp->area, tmp->size-1);
3683 tmp->area[write] = '\0';
3684 chunk_appendf(out, "%s\n", tmp->area);
3685
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003686#ifndef USE_OPENSSL_WOLFSSL
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003687 /* Revoked Certificates */
3688 rev = X509_CRL_get_REVOKED(crl);
3689 if (sk_X509_REVOKED_num(rev) > 0)
3690 chunk_appendf(out, "Revoked Certificates:\n");
3691 else
3692 chunk_appendf(out, "No Revoked Certificates.\n");
3693
3694 for (i = 0; i < sk_X509_REVOKED_num(rev); i++) {
3695 rev_entry = sk_X509_REVOKED_value(rev, i);
3696
3697 /* Serial Number and Revocation Date */
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003698 if (BIO_reset(bio) == -1)
3699 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003700 BIO_printf(bio , " Serial Number: ");
Remi Tricot-Le Breton18c7d832021-05-17 18:38:34 +02003701 i2a_ASN1_INTEGER(bio, (ASN1_INTEGER*)X509_REVOKED_get0_serialNumber(rev_entry));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003702 BIO_printf(bio, "\n Revocation Date: ");
Remi Tricot-Le Bretona6b27842021-05-18 10:06:00 +02003703 if (ASN1_TIME_print(bio, X509_REVOKED_get0_revocationDate(rev_entry)) == 0)
3704 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003705 BIO_printf(bio, "\n");
3706
3707 write = BIO_read(bio, tmp->area, tmp->size-1);
3708 tmp->area[write] = '\0';
3709 chunk_appendf(out, "%s", tmp->area);
3710 }
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003711#endif /* not USE_OPENSSL_WOLFSSL */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003712
3713end:
3714 free_trash_chunk(tmp);
3715 if (bio)
3716 BIO_free(bio);
3717
3718 return 0;
3719}
3720
Willy Tarreau821c3b02022-05-04 15:47:39 +02003721/* IO handler of details "show ssl crl-file <filename[:index]>".
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003722 * It uses show_crlfile_ctx and the global
3723 * crlfile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003724 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003725static int cli_io_handler_show_crlfile_detail(struct appctx *appctx)
3726{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003727 struct show_crlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003728 struct cafile_entry *cafile_entry = ctx->cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003729 struct buffer *out = alloc_trash_chunk();
3730 int i;
3731 X509_CRL *crl;
3732 STACK_OF(X509_OBJECT) *objs;
3733 int retval = 0;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003734 int index = ctx->index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003735
3736 if (!out)
3737 goto end_no_putchk;
3738
3739 chunk_appendf(out, "Filename: ");
3740 if (cafile_entry == crlfile_transaction.new_crlfile_entry)
3741 chunk_appendf(out, "*");
3742 chunk_appendf(out, "%s\n", cafile_entry->path);
3743
3744 chunk_appendf(out, "Status: ");
3745 if (!cafile_entry->ca_store)
3746 chunk_appendf(out, "Empty\n");
3747 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3748 chunk_appendf(out, "Unused\n");
3749 else
3750 chunk_appendf(out, "Used\n");
3751
3752 if (!cafile_entry->ca_store)
3753 goto end;
3754
3755 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3756 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
3757 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
3758 if (!crl)
3759 continue;
3760
3761 /* CRL indexes start at 1 on the CLI output. */
3762 if (index && index-1 != i)
3763 continue;
3764
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003765 chunk_appendf(out, " \nCertificate Revocation List #%d:\n", i+1);
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003766 retval = show_crl_detail(crl, out);
3767 if (retval < 0)
3768 goto end_no_putchk;
3769 else if (retval || index)
3770 goto end;
3771 }
3772
3773end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003774 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003775 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003776
3777end_no_putchk:
3778 free_trash_chunk(out);
3779 return 1;
3780yield:
3781 free_trash_chunk(out);
3782 return 0; /* should come back */
3783}
3784
Willy Tarreau821c3b02022-05-04 15:47:39 +02003785/* parsing function for 'show ssl crl-file [crlfile[:index]]'.
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003786 * It sets the context to a show_crlfile_ctx, and the global
Willy Tarreau821c3b02022-05-04 15:47:39 +02003787 * cafile_transaction.new_crlfile_entry under the ckch_lock.
3788 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003789static int cli_parse_show_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3790{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003791 struct show_crlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003792 struct cafile_entry *cafile_entry;
3793 long index = 0;
3794 char *colons;
3795 char *err = NULL;
3796
3797 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3798 return cli_err(appctx, "Can't allocate memory!\n");
3799
3800 /* The operations on the CKCH architecture are locked so we can
3801 * manipulate ckch_store and ckch_inst */
3802 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3803 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3804
3805 /* check if there is a certificate to lookup */
3806 if (*args[3]) {
3807
3808 /* Look for an optional index after the CRL file name */
3809 colons = strchr(args[3], ':');
3810 if (colons) {
3811 char *endptr;
3812
3813 index = strtol(colons + 1, &endptr, 10);
3814 /* Indexes start at 1 */
3815 if (colons + 1 == endptr || *endptr != '\0' || index <= 0) {
3816 memprintf(&err, "wrong CRL index after colons in '%s'!", args[3]);
3817 goto error;
3818 }
3819 *colons = '\0';
3820 }
3821
3822 if (*args[3] == '*') {
3823 if (!crlfile_transaction.new_crlfile_entry)
3824 goto error;
3825
3826 cafile_entry = crlfile_transaction.new_crlfile_entry;
3827
3828 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3829 goto error;
3830
3831 } else {
3832 /* Get the "original" cafile_entry and not the
3833 * uncommitted one if it exists. */
3834 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CRL)
3835 goto error;
3836 }
3837
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003838 ctx->cafile_entry = cafile_entry;
3839 ctx->index = index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003840 /* use the IO handler that shows details */
3841 appctx->io_handler = cli_io_handler_show_crlfile_detail;
3842 }
3843
3844 return 0;
3845
3846error:
3847 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3848 if (err)
3849 return cli_dynerr(appctx, err);
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003850 return cli_err(appctx, "Can't display the CRL file : Not found!\n");
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003851}
3852
3853/* IO handler of "show ssl crl-file". The command taking a specific CRL file name
3854 * is managed in cli_io_handler_show_crlfile_detail. */
3855static int cli_io_handler_show_crlfile(struct appctx *appctx)
3856{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003857 struct show_crlfile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003858 struct buffer *trash = alloc_trash_chunk();
3859 struct ebmb_node *node;
Christopher Faulet88041b32022-06-03 16:26:56 +02003860 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003861
3862 if (trash == NULL)
3863 return 1;
3864
Christopher Faulet9a99e542022-06-03 10:32:18 +02003865 if (!ctx->old_crlfile_entry && crlfile_transaction.old_crlfile_entry) {
3866 chunk_appendf(trash, "# transaction\n");
3867 chunk_appendf(trash, "*%s\n", crlfile_transaction.old_crlfile_entry->path);
3868 if (applet_putchk(appctx, trash) == -1)
3869 goto yield;
3870 ctx->old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003871 }
3872
3873 /* First time in this io_handler. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003874 if (!ctx->cafile_entry) {
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003875 chunk_appendf(trash, "# filename\n");
3876 node = ebmb_first(&cafile_tree);
3877 } else {
3878 /* We yielded during a previous call. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003879 node = &ctx->cafile_entry->node;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003880 }
3881
3882 while (node) {
3883 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3884 if (cafile_entry->type == CAFILE_CRL) {
3885 chunk_appendf(trash, "%s\n", cafile_entry->path);
3886 }
3887
3888 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003889 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003890 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003891 }
3892
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003893 ctx->cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003894 free_trash_chunk(trash);
3895 return 1;
3896yield:
3897
3898 free_trash_chunk(trash);
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003899 ctx->cafile_entry = cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003900 return 0; /* should come back */
3901}
3902
3903
3904/* release function of the 'show ssl crl-file' command */
3905static void cli_release_show_crlfile(struct appctx *appctx)
3906{
3907 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3908}
3909
3910
William Lallemandee8530c2020-06-23 18:19:42 +02003911void ckch_deinit()
3912{
3913 struct eb_node *node, *next;
3914 struct ckch_store *store;
William Lallemandb0c48272022-04-26 15:44:53 +02003915 struct ebmb_node *canode;
William Lallemandee8530c2020-06-23 18:19:42 +02003916
William Lallemandb0c48272022-04-26 15:44:53 +02003917 /* deinit the ckch stores */
William Lallemandee8530c2020-06-23 18:19:42 +02003918 node = eb_first(&ckchs_tree);
3919 while (node) {
3920 next = eb_next(node);
3921 store = ebmb_entry(node, struct ckch_store, node);
3922 ckch_store_free(store);
3923 node = next;
3924 }
William Lallemandb0c48272022-04-26 15:44:53 +02003925
3926 /* deinit the ca-file store */
3927 canode = ebmb_first(&cafile_tree);
3928 while (canode) {
3929 struct cafile_entry *entry = NULL;
3930
3931 entry = ebmb_entry(canode, struct cafile_entry, node);
3932 canode = ebmb_next(canode);
William Lallemand946580e2022-08-29 18:36:18 +02003933 ebmb_delete(&entry->node);
William Lallemandb0c48272022-04-26 15:44:53 +02003934 ssl_store_delete_cafile_entry(entry);
3935 }
William Lallemandee8530c2020-06-23 18:19:42 +02003936}
William Lallemandda8584c2020-05-14 10:14:37 +02003937
3938/* register cli keywords */
3939static struct cli_kw_list cli_kws = {{ },{
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003940 { { "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 },
3941 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
3942 { { "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 },
3943 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
3944 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
3945 { { "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 },
3946
Amaury Denoyelleb11ad9e2021-05-21 11:01:10 +02003947 { { "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 +02003948 { { "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 +01003949 { { "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 +02003950 { { "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 +01003951 { { "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 +01003952 { { "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 +01003953 { { "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 +02003954
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003955 { { "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 +02003956 { { "set", "ssl", "crl-file", NULL }, "set ssl crl-file <crlfile> <payload> : replace a CRL file", cli_parse_set_crlfile, NULL, NULL },
3957 { { "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 +02003958 { { "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 +02003959 { { "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 +02003960 { { "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 +02003961 { { NULL }, NULL, NULL, NULL }
3962}};
3963
3964INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3965