blob: f8e63edc33d4f9a16572ff4f0b885c88a1025e33 [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 */
William Lallemand26654e72022-03-30 12:01:32 +02001417 { "crt", CERT_TYPE_CRT, &ssl_sock_load_pem_into_ckch },
William Lallemandff8bf982022-03-29 10:44:23 +02001418 { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemandda8584c2020-05-14 10:14:37 +02001419#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemandff8bf982022-03-29 10:44:23 +02001420 { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001421#endif
Ilya Shipitsinc47d6762021-02-13 11:45:33 +05001422#ifdef HAVE_SSL_SCTL
William Lallemandff8bf982022-03-29 10:44:23 +02001423 { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemandda8584c2020-05-14 10:14:37 +02001424#endif
William Lallemandff8bf982022-03-29 10:44:23 +02001425 { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
1426 { NULL, CERT_TYPE_MAX, NULL },
William Lallemandda8584c2020-05-14 10:14:37 +02001427};
1428
1429
1430/* release function of the `show ssl cert' command */
1431static void cli_release_show_cert(struct appctx *appctx)
1432{
1433 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1434}
1435
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001436/* IO handler of "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001437 * It makes use of a show_cert_ctx context, and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001438 */
William Lallemandda8584c2020-05-14 10:14:37 +02001439static int cli_io_handler_show_cert(struct appctx *appctx)
1440{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001441 struct show_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001442 struct buffer *trash = alloc_trash_chunk();
1443 struct ebmb_node *node;
Christopher Fauletd1d2e4d2022-06-03 16:24:02 +02001444 struct ckch_store *ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001445
1446 if (trash == NULL)
1447 return 1;
1448
Christopher Faulet3e94f5d2022-06-03 10:46:40 +02001449 if (!ctx->old_ckchs && ckchs_transaction.old_ckchs) {
1450 ckchs = ckchs_transaction.old_ckchs;
1451 chunk_appendf(trash, "# transaction\n");
1452 chunk_appendf(trash, "*%s\n", ckchs->path);
1453 if (applet_putchk(appctx, trash) == -1)
1454 goto yield;
1455 ctx->old_ckchs = ckchs_transaction.old_ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001456 }
1457
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001458 if (!ctx->cur_ckchs) {
William Lallemandda8584c2020-05-14 10:14:37 +02001459 chunk_appendf(trash, "# filename\n");
1460 node = ebmb_first(&ckchs_tree);
1461 } else {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001462 node = &ctx->cur_ckchs->node;
William Lallemandda8584c2020-05-14 10:14:37 +02001463 }
1464 while (node) {
1465 ckchs = ebmb_entry(node, struct ckch_store, node);
William Lallemand5685ccf2020-09-16 16:12:25 +02001466 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001467
1468 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001469 if (applet_putchk(appctx, trash) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001470 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001471 }
1472
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001473 ctx->cur_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001474 free_trash_chunk(trash);
1475 return 1;
1476yield:
1477
1478 free_trash_chunk(trash);
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001479 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001480 return 0; /* should come back */
1481}
1482
1483/*
1484 * Extract and format the DNS SAN extensions and copy result into a chuink
1485 * Return 0;
1486 */
1487#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1488static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
1489{
1490 int i;
1491 char *str;
1492 STACK_OF(GENERAL_NAME) *names = NULL;
1493
1494 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1495 if (names) {
1496 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
1497 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
1498 if (i > 0)
1499 chunk_appendf(out, ", ");
1500 if (name->type == GEN_DNS) {
1501 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
1502 chunk_appendf(out, "DNS:%s", str);
1503 OPENSSL_free(str);
1504 }
1505 }
1506 }
1507 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
1508 }
1509 return 0;
1510}
1511#endif
1512
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001513/*
1514 * Build the ckch_inst_link that will be chained in the CA file entry and the
1515 * corresponding ckch_inst_link_ref that will be chained in the ckch instance.
1516 * Return 0 in case of success.
1517 */
1518static int do_chain_inst_and_cafile(struct cafile_entry *cafile_entry, struct ckch_inst *ckch_inst)
1519{
1520 struct ckch_inst_link *new_link;
1521 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
1522 struct ckch_inst_link *link = LIST_ELEM(cafile_entry->ckch_inst_link.n,
1523 typeof(link), list);
1524 /* Do not add multiple references to the same
1525 * instance in a cafile_entry */
1526 if (link->ckch_inst == ckch_inst) {
1527 return 1;
1528 }
1529 }
1530
1531 new_link = calloc(1, sizeof(*new_link));
1532 if (new_link) {
1533 struct ckch_inst_link_ref *new_link_ref = calloc(1, sizeof(*new_link_ref));
1534 if (!new_link_ref) {
1535 free(new_link);
1536 return 1;
1537 }
1538
1539 new_link->ckch_inst = ckch_inst;
1540 new_link_ref->link = new_link;
1541 LIST_INIT(&new_link->list);
1542 LIST_INIT(&new_link_ref->list);
1543
1544 LIST_APPEND(&cafile_entry->ckch_inst_link, &new_link->list);
1545 LIST_APPEND(&ckch_inst->cafile_link_refs, &new_link_ref->list);
1546 }
1547
1548 return 0;
1549}
1550
1551
1552/*
1553 * Link a CA file tree entry to the ckch instance that uses it.
1554 * To determine if and which CA file tree entries need to be linked to the
1555 * instance, we follow the same logic performed in ssl_sock_prepare_ctx when
1556 * processing the verify option.
1557 * This function works for a frontend as well as for a backend, depending on the
1558 * configuration parameters given (bind_conf or server).
1559 */
1560void ckch_inst_add_cafile_link(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf,
1561 struct ssl_bind_conf *ssl_conf, const struct server *srv)
1562{
1563 int verify = SSL_VERIFY_NONE;
1564
1565 if (srv) {
1566
1567 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
1568 verify = SSL_VERIFY_PEER;
1569 switch (srv->ssl_ctx.verify) {
1570 case SSL_SOCK_VERIFY_NONE:
1571 verify = SSL_VERIFY_NONE;
1572 break;
1573 case SSL_SOCK_VERIFY_REQUIRED:
1574 verify = SSL_VERIFY_PEER;
1575 break;
1576 }
1577 }
1578 else {
1579 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
1580 case SSL_SOCK_VERIFY_NONE:
1581 verify = SSL_VERIFY_NONE;
1582 break;
1583 case SSL_SOCK_VERIFY_OPTIONAL:
1584 verify = SSL_VERIFY_PEER;
1585 break;
1586 case SSL_SOCK_VERIFY_REQUIRED:
1587 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1588 break;
1589 }
1590 }
1591
1592 if (verify & SSL_VERIFY_PEER) {
1593 struct cafile_entry *ca_file_entry = NULL;
1594 struct cafile_entry *ca_verify_file_entry = NULL;
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001595 struct cafile_entry *crl_file_entry = NULL;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001596 if (srv) {
1597 if (srv->ssl_ctx.ca_file) {
1598 ca_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.ca_file, 0);
1599
1600 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001601 if (srv->ssl_ctx.crl_file) {
1602 crl_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.crl_file, 0);
1603 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001604 }
1605 else {
1606 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
1607 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 +02001608 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 +01001609
1610 if (ca_file)
1611 ca_file_entry = ssl_store_get_cafile_entry(ca_file, 0);
1612 if (ca_verify_file)
1613 ca_verify_file_entry = ssl_store_get_cafile_entry(ca_verify_file, 0);
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001614 if (crl_file)
1615 crl_file_entry = ssl_store_get_cafile_entry(crl_file, 0);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001616 }
1617
1618 if (ca_file_entry) {
1619 /* If we have a ckch instance that is not already in the
1620 * cafile_entry's list, add it to it. */
1621 if (do_chain_inst_and_cafile(ca_file_entry, ckch_inst))
1622 return;
1623
1624 }
1625 if (ca_verify_file_entry && (ca_file_entry != ca_verify_file_entry)) {
1626 /* If we have a ckch instance that is not already in the
1627 * cafile_entry's list, add it to it. */
1628 if (do_chain_inst_and_cafile(ca_verify_file_entry, ckch_inst))
1629 return;
1630 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001631 if (crl_file_entry) {
1632 /* If we have a ckch instance that is not already in the
1633 * cafile_entry's list, add it to it. */
1634 if (do_chain_inst_and_cafile(crl_file_entry, ckch_inst))
1635 return;
1636 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001637 }
1638}
1639
William Lallemandda8584c2020-05-14 10:14:37 +02001640
1641
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001642static int show_cert_detail(X509 *cert, STACK_OF(X509) *chain, struct buffer *out)
William Lallemandda8584c2020-05-14 10:14:37 +02001643{
William Lallemandda8584c2020-05-14 10:14:37 +02001644 BIO *bio = NULL;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001645 struct buffer *tmp = alloc_trash_chunk();
William Lallemandda8584c2020-05-14 10:14:37 +02001646 int i;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001647 int write = -1;
1648 unsigned int len = 0;
1649 X509_NAME *name = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001650
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001651 if (!tmp)
1652 return -1;
William Lallemandda8584c2020-05-14 10:14:37 +02001653
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001654 if (!cert)
William Lallemand5685ccf2020-09-16 16:12:25 +02001655 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02001656
William Lallemand5685ccf2020-09-16 16:12:25 +02001657 if (chain == NULL) {
1658 struct issuer_chain *issuer;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001659 issuer = ssl_get0_issuer_chain(cert);
William Lallemand5685ccf2020-09-16 16:12:25 +02001660 if (issuer) {
1661 chain = issuer->chain;
1662 chunk_appendf(out, "Chain Filename: ");
1663 chunk_appendf(out, "%s\n", issuer->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001664 }
William Lallemand5685ccf2020-09-16 16:12:25 +02001665 }
1666 chunk_appendf(out, "Serial: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001667 if (ssl_sock_get_serial(cert, tmp) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001668 goto end;
1669 dump_binary(out, tmp->area, tmp->data);
1670 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001671
William Lallemand5685ccf2020-09-16 16:12:25 +02001672 chunk_appendf(out, "notBefore: ");
1673 chunk_reset(tmp);
1674 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1675 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001676 if (ASN1_TIME_print(bio, X509_getm_notBefore(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001677 goto end;
1678 write = BIO_read(bio, tmp->area, tmp->size-1);
1679 tmp->area[write] = '\0';
1680 BIO_free(bio);
1681 bio = NULL;
1682 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001683
William Lallemand5685ccf2020-09-16 16:12:25 +02001684 chunk_appendf(out, "notAfter: ");
1685 chunk_reset(tmp);
1686 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1687 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001688 if (ASN1_TIME_print(bio, X509_getm_notAfter(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001689 goto end;
1690 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
1691 goto end;
1692 tmp->area[write] = '\0';
1693 BIO_free(bio);
1694 bio = NULL;
1695 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001696
1697#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand5685ccf2020-09-16 16:12:25 +02001698 chunk_appendf(out, "Subject Alternative Name: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001699 if (ssl_sock_get_san_oneline(cert, out) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001700 goto end;
1701 *(out->area + out->data) = '\0';
1702 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001703#endif
William Lallemand5685ccf2020-09-16 16:12:25 +02001704 chunk_reset(tmp);
1705 chunk_appendf(out, "Algorithm: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001706 if (cert_get_pkey_algo(cert, tmp) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001707 goto end;
1708 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001709
William Lallemand5685ccf2020-09-16 16:12:25 +02001710 chunk_reset(tmp);
1711 chunk_appendf(out, "SHA1 FingerPrint: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001712 if (X509_digest(cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001713 goto end;
1714 tmp->data = len;
1715 dump_binary(out, tmp->area, tmp->data);
1716 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001717
William Lallemand5685ccf2020-09-16 16:12:25 +02001718 chunk_appendf(out, "Subject: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001719 if ((name = X509_get_subject_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001720 goto end;
1721 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1722 goto end;
1723 *(tmp->area + tmp->data) = '\0';
1724 chunk_appendf(out, "%s\n", tmp->area);
1725
1726 chunk_appendf(out, "Issuer: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001727 if ((name = X509_get_issuer_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001728 goto end;
1729 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1730 goto end;
1731 *(tmp->area + tmp->data) = '\0';
1732 chunk_appendf(out, "%s\n", tmp->area);
1733
1734 /* Displays subject of each certificate in the chain */
1735 for (i = 0; i < sk_X509_num(chain); i++) {
1736 X509 *ca = sk_X509_value(chain, i);
1737
1738 chunk_appendf(out, "Chain Subject: ");
1739 if ((name = X509_get_subject_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001740 goto end;
1741 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1742 goto end;
1743 *(tmp->area + tmp->data) = '\0';
1744 chunk_appendf(out, "%s\n", tmp->area);
1745
William Lallemand5685ccf2020-09-16 16:12:25 +02001746 chunk_appendf(out, "Chain Issuer: ");
1747 if ((name = X509_get_issuer_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001748 goto end;
1749 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1750 goto end;
1751 *(tmp->area + tmp->data) = '\0';
1752 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001753 }
1754
1755end:
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001756 if (bio)
1757 BIO_free(bio);
1758 free_trash_chunk(tmp);
1759
1760 return 0;
1761}
1762
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001763#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 +02001764/*
1765 * Build the OCSP tree entry's key for a given ckch_store.
1766 * Returns a negative value in case of error.
1767 */
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +01001768static 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 +02001769{
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001770 unsigned char *p = NULL;
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +01001771 int i;
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001772
1773 if (!key_length)
1774 return -1;
1775
1776 *key_length = 0;
1777
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +01001778 if (!ckch_store->data->ocsp_cid)
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001779 return 0;
1780
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +01001781 i = i2d_OCSP_CERTID(ckch_store->data->ocsp_cid, NULL);
1782 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1783 return 0;
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001784
1785 p = certid;
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +01001786 *key_length = i2d_OCSP_CERTID(ckch_store->data->ocsp_cid, &p);
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001787
1788end:
1789 return *key_length > 0;
1790}
1791#endif
1792
1793/*
1794 * Dump the OCSP certificate key (if it exists) of certificate <ckch> into
1795 * buffer <out>.
1796 * Returns 0 in case of success.
1797 */
1798static int ckch_store_show_ocsp_certid(struct ckch_store *ckch_store, struct buffer *out)
1799{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001800#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 +02001801 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1802 unsigned int key_length = 0;
1803 int i;
1804
1805 if (ckch_store_build_certid(ckch_store, (unsigned char*)key, &key_length) >= 0) {
1806 /* Dump the CERTID info */
1807 chunk_appendf(out, "OCSP Response Key: ");
1808 for (i = 0; i < key_length; ++i) {
1809 chunk_appendf(out, "%02x", key[i]);
1810 }
1811 chunk_appendf(out, "\n");
1812 }
1813#endif
1814
1815 return 0;
1816}
1817
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001818
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001819/* IO handler of the details "show ssl cert <filename>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001820 * It uses a struct show_cert_ctx and ckchs_transaction in read-only.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001821 */
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001822static int cli_io_handler_show_cert_detail(struct appctx *appctx)
1823{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001824 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001825 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001826 struct buffer *out = alloc_trash_chunk();
1827 int retval = 0;
1828
1829 if (!out)
1830 goto end_no_putchk;
1831
1832 chunk_appendf(out, "Filename: ");
1833 if (ckchs == ckchs_transaction.new_ckchs)
1834 chunk_appendf(out, "*");
1835 chunk_appendf(out, "%s\n", ckchs->path);
1836
1837 chunk_appendf(out, "Status: ");
William Lallemand52ddd992022-11-22 11:51:53 +01001838 if (ckchs->data->cert == NULL)
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001839 chunk_appendf(out, "Empty\n");
1840 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
1841 chunk_appendf(out, "Unused\n");
1842 else
1843 chunk_appendf(out, "Used\n");
1844
William Lallemand52ddd992022-11-22 11:51:53 +01001845 retval = show_cert_detail(ckchs->data->cert, ckchs->data->chain, out);
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001846 if (retval < 0)
1847 goto end_no_putchk;
1848 else if (retval)
1849 goto end;
1850
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001851 ckch_store_show_ocsp_certid(ckchs, out);
1852
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001853end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001854 if (applet_putchk(appctx, out) == -1)
William Lallemandda8584c2020-05-14 10:14:37 +02001855 goto yield;
William Lallemandda8584c2020-05-14 10:14:37 +02001856
1857end_no_putchk:
William Lallemandda8584c2020-05-14 10:14:37 +02001858 free_trash_chunk(out);
1859 return 1;
1860yield:
William Lallemandda8584c2020-05-14 10:14:37 +02001861 free_trash_chunk(out);
1862 return 0; /* should come back */
1863}
1864
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001865
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001866/* IO handler of the details "show ssl cert <filename.ocsp>".
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001867 * It uses a show_cert_ctx.
Willy Tarreau4fd9b4d2022-05-04 16:11:50 +02001868 */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001869static int cli_io_handler_show_cert_ocsp_detail(struct appctx *appctx)
1870{
Remi Tricot-Le Breton3faf0cb2021-06-10 18:10:32 +02001871#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL)
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001872 struct show_cert_ctx *ctx = appctx->svcctx;
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001873 struct ckch_store *ckchs = ctx->cur_ckchs;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001874 struct buffer *out = alloc_trash_chunk();
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001875 int from_transaction = ctx->transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001876
1877 if (!out)
1878 goto end_no_putchk;
1879
1880 /* If we try to display an ongoing transaction's OCSP response, we
1881 * need to dump the ckch's ocsp_response buffer directly.
1882 * Otherwise, we must rebuild the certificate's certid in order to
1883 * look for the current OCSP response in the tree. */
William Lallemand52ddd992022-11-22 11:51:53 +01001884 if (from_transaction && ckchs->data->ocsp_response) {
1885 if (ssl_ocsp_response_print(ckchs->data->ocsp_response, out))
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001886 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001887 }
1888 else {
1889 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1890 unsigned int key_length = 0;
1891
1892 if (ckch_store_build_certid(ckchs, (unsigned char*)key, &key_length) < 0)
1893 goto end_no_putchk;
1894
Remi Tricot-Le Bretona9a591a2022-02-16 14:42:22 +01001895 if (ssl_get_ocspresponse_detail(key, out))
1896 goto end_no_putchk;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001897 }
1898
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001899 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001900 goto yield;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001901
1902end_no_putchk:
1903 free_trash_chunk(out);
1904 return 1;
1905yield:
1906 free_trash_chunk(out);
1907 return 0; /* should come back */
1908#else
1909 return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n");
1910#endif
1911}
1912
William Lallemandda8584c2020-05-14 10:14:37 +02001913/* parsing function for 'show ssl cert [certfile]' */
1914static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
1915{
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001916 struct show_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
William Lallemandda8584c2020-05-14 10:14:37 +02001917 struct ckch_store *ckchs;
1918
1919 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1920 return cli_err(appctx, "Can't allocate memory!\n");
1921
1922 /* The operations on the CKCH architecture are locked so we can
1923 * manipulate ckch_store and ckch_inst */
1924 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1925 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
1926
1927 /* check if there is a certificate to lookup */
1928 if (*args[3]) {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001929 int show_ocsp_detail = 0;
1930 int from_transaction = 0;
1931 char *end;
1932
1933 /* We manage the special case "certname.ocsp" through which we
1934 * can show the details of an OCSP response. */
1935 end = strrchr(args[3], '.');
1936 if (end && strcmp(end+1, "ocsp") == 0) {
1937 *end = '\0';
1938 show_ocsp_detail = 1;
1939 }
1940
William Lallemandda8584c2020-05-14 10:14:37 +02001941 if (*args[3] == '*') {
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001942 from_transaction = 1;
William Lallemandda8584c2020-05-14 10:14:37 +02001943 if (!ckchs_transaction.new_ckchs)
1944 goto error;
1945
1946 ckchs = ckchs_transaction.new_ckchs;
1947
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001948 if (strcmp(args[3] + 1, ckchs->path) != 0)
William Lallemandda8584c2020-05-14 10:14:37 +02001949 goto error;
1950
1951 } else {
1952 if ((ckchs = ckchs_lookup(args[3])) == NULL)
1953 goto error;
1954
1955 }
1956
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001957 ctx->cur_ckchs = ckchs;
William Lallemandda8584c2020-05-14 10:14:37 +02001958 /* use the IO handler that shows details */
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001959 if (show_ocsp_detail) {
Willy Tarreau96c9a6c2022-05-04 19:51:37 +02001960 ctx->transaction = from_transaction;
Remi Tricot-Le Breton6056e612021-06-10 13:51:15 +02001961 appctx->io_handler = cli_io_handler_show_cert_ocsp_detail;
1962 }
1963 else
1964 appctx->io_handler = cli_io_handler_show_cert_detail;
William Lallemandda8584c2020-05-14 10:14:37 +02001965 }
1966
1967 return 0;
1968
1969error:
1970 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1971 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
1972}
1973
1974/* release function of the `set ssl cert' command, free things and unlock the spinlock */
1975static void cli_release_commit_cert(struct appctx *appctx)
1976{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02001977 struct commit_cert_ctx *ctx = appctx->svcctx;
William Lallemandda8584c2020-05-14 10:14:37 +02001978
1979 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulet9d56e242022-05-31 16:37:01 +02001980 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
1981 if (ctx->new_ckchs)
1982 ckch_store_free(ctx->new_ckchs);
1983 ha_free(&ctx->err);
William Lallemandda8584c2020-05-14 10:14:37 +02001984}
1985
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001986
1987/*
1988 * Rebuild a new instance 'new_inst' based on an old instance 'ckchi' and a
1989 * specific ckch_store.
1990 * Returns 0 in case of success, 1 otherwise.
1991 */
William Lallemande60c7d62022-03-30 11:26:15 +02001992int ckch_inst_rebuild(struct ckch_store *ckch_store, struct ckch_inst *ckchi,
1993 struct ckch_inst **new_inst, char **err)
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001994{
1995 int retval = 0;
1996 int errcode = 0;
1997 struct sni_ctx *sc0, *sc0s;
1998 char **sni_filter = NULL;
1999 int fcount = 0;
2000
2001 if (ckchi->crtlist_entry) {
2002 sni_filter = ckchi->crtlist_entry->filters;
2003 fcount = ckchi->crtlist_entry->fcount;
2004 }
2005
2006 if (ckchi->is_server_instance)
2007 errcode |= ckch_inst_new_load_srv_store(ckch_store->path, ckch_store, new_inst, err);
2008 else
2009 errcode |= ckch_inst_new_load_store(ckch_store->path, ckch_store, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, new_inst, err);
2010
2011 if (errcode & ERR_CODE)
2012 return 1;
2013
2014 /* if the previous ckchi was used as the default */
2015 if (ckchi->is_default)
2016 (*new_inst)->is_default = 1;
2017
2018 (*new_inst)->is_server_instance = ckchi->is_server_instance;
2019 (*new_inst)->server = ckchi->server;
2020 /* Create a new SSL_CTX and link it to the new instance. */
2021 if ((*new_inst)->is_server_instance) {
2022 retval = ssl_sock_prep_srv_ctx_and_inst(ckchi->server, (*new_inst)->ctx, (*new_inst));
2023 if (retval)
2024 return 1;
2025 }
2026
2027 /* create the link to the crtlist_entry */
2028 (*new_inst)->crtlist_entry = ckchi->crtlist_entry;
2029
2030 /* we need to initialize the SSL_CTX generated */
2031 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
2032 list_for_each_entry_safe(sc0, sc0s, &(*new_inst)->sni_ctx, by_ckch_inst) {
2033 if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
2034 errcode |= ssl_sock_prep_ctx_and_inst(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, *new_inst, err);
2035 if (errcode & ERR_CODE)
2036 return 1;
2037 }
2038 }
2039
2040 return 0;
2041}
2042
2043/*
2044 * Load all the new SNIs of a newly built ckch instance in the trees, or replace
2045 * a server's main ckch instance.
2046 */
2047static void __ssl_sock_load_new_ckch_instance(struct ckch_inst *ckchi)
2048{
2049 /* The bind_conf will be null on server ckch_instances. */
2050 if (ckchi->is_server_instance) {
2051 int i;
2052 /* a lock is needed here since we have to free the SSL cache */
2053 HA_RWLOCK_WRLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
2054 /* free the server current SSL_CTX */
2055 SSL_CTX_free(ckchi->server->ssl_ctx.ctx);
2056 /* Actual ssl context update */
2057 SSL_CTX_up_ref(ckchi->ctx);
2058 ckchi->server->ssl_ctx.ctx = ckchi->ctx;
2059 ckchi->server->ssl_ctx.inst = ckchi;
2060
2061 /* flush the session cache of the server */
2062 for (i = 0; i < global.nbthread; i++) {
William Lallemandce990332021-11-23 15:15:09 +01002063 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].sni);
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002064 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].ptr);
2065 }
2066 HA_RWLOCK_WRUNLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
2067
2068 } else {
2069 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
2070 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
2071 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
2072 }
2073}
2074
2075/*
2076 * Delete a ckch instance that was replaced after a CLI command.
2077 */
2078static void __ckch_inst_free_locked(struct ckch_inst *ckchi)
2079{
2080 if (ckchi->is_server_instance) {
2081 /* no lock for servers */
2082 ckch_inst_free(ckchi);
2083 } else {
2084 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
2085
2086 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
2087 ckch_inst_free(ckchi);
2088 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
2089 }
2090}
2091
William Lallemand3b5a3a62022-03-29 14:29:31 +02002092/* Replace a ckch_store in the ckch tree and insert the whole dependencies,
2093* then free the previous dependencies and store.
2094* Used in the case of a certificate update.
2095*
2096* Every dependencies must allocated before using this function.
2097*
2098* This function can't fail as it only update pointers, and does not alloc anything.
2099*
2100* /!\ This function must be used under the ckch lock. /!\
2101*
2102* - Insert every dependencies (SNI, crtlist_entry, ckch_inst, etc)
2103* - Delete the old ckch_store from the tree
2104* - Insert the new ckch_store
2105* - Free the old dependencies and the old ckch_store
2106*/
2107void ckch_store_replace(struct ckch_store *old_ckchs, struct ckch_store *new_ckchs)
2108{
2109 struct crtlist_entry *entry;
2110 struct ckch_inst *ckchi, *ckchis;
2111
2112 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
2113 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
2114 ebpt_delete(&entry->node);
2115 /* change the ptr and reinsert the node */
2116 entry->node.key = new_ckchs;
2117 ebpt_insert(&entry->crtlist->entries, &entry->node);
2118 }
2119 /* insert the new ckch_insts in the crtlist_entry */
2120 list_for_each_entry(ckchi, &new_ckchs->ckch_inst, by_ckchs) {
2121 if (ckchi->crtlist_entry)
2122 LIST_INSERT(&ckchi->crtlist_entry->ckch_inst, &ckchi->by_crtlist_entry);
2123 }
2124 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2125 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
2126 __ssl_sock_load_new_ckch_instance(ckchi);
2127 }
2128 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
2129 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
2130 __ckch_inst_free_locked(ckchi);
2131 }
2132
2133 ckch_store_free(old_ckchs);
2134 ebst_insert(&ckchs_tree, &new_ckchs->node);
2135}
2136
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01002137
William Lallemandda8584c2020-05-14 10:14:37 +02002138/*
2139 * This function tries to create the new ckch_inst and their SNIs
William Lallemand30fcca12022-03-30 12:03:12 +02002140 *
2141 * /!\ don't forget to update __hlua_ckch_commit() if you changes things there. /!\
William Lallemandda8584c2020-05-14 10:14:37 +02002142 */
2143static int cli_io_handler_commit_cert(struct appctx *appctx)
2144{
Willy Tarreaua645b6a2022-05-04 19:58:00 +02002145 struct commit_cert_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002146 struct stconn *sc = appctx_sc(appctx);
William Lallemandda8584c2020-05-14 10:14:37 +02002147 int y = 0;
William Lallemandda8584c2020-05-14 10:14:37 +02002148 struct ckch_store *old_ckchs, *new_ckchs = NULL;
William Lallemand3b5a3a62022-03-29 14:29:31 +02002149 struct ckch_inst *ckchi;
William Lallemandda8584c2020-05-14 10:14:37 +02002150
Willy Tarreau475e4632022-05-27 10:26:46 +02002151 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
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
Willy Tarreau475e4632022-05-27 10:26:46 +02002827 if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002828 goto end;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002829
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002830 /* The ctx was already validated by the ca-file/crl-file parsing
2831 * function. Entries can only be NULL in CACRL_ST_SUCCESS or
2832 * CACRL_ST_FIN states
2833 */
2834 switch (ctx->cafile_type) {
2835 case CAFILE_CERT:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002836 path = cafile_transaction.path;
2837 break;
2838 case CAFILE_CRL:
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002839 path = crlfile_transaction.path;
2840 break;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002841 default:
Willy Tarreaud543ae02022-06-22 05:40:25 +02002842 path = NULL;
Christopher Fauletea2c8c62022-06-03 16:37:31 +02002843 goto error;
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002844 }
2845
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002846 while (1) {
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002847 switch (ctx->state) {
2848 case CACRL_ST_INIT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002849 /* This state just print the update message */
Christopher Fauletddc8e1c2022-06-03 09:00:09 +02002850 chunk_printf(&trash, "Committing %s", path);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002851 if (applet_putchk(appctx, &trash) == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002852 goto yield;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002853
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002854 ctx->state = CACRL_ST_GEN;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002855 __fallthrough;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002856 case CACRL_ST_GEN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002857 /*
2858 * This state generates the ckch instances with their
2859 * sni_ctxs and SSL_CTX.
2860 *
2861 * Since the SSL_CTX generation can be CPU consumer, we
2862 * yield every 10 instances.
2863 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002864
2865 /* get the next ckchi to regenerate */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002866 ckchi_link = ctx->next_ckchi_link;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002867
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002868 /* we didn't start yet, set it to the first elem */
2869 if (ckchi_link == NULL) {
2870 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2871 /* Add the newly created cafile_entry to the tree so that
2872 * any new ckch instance created from now can use it. */
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002873 if (ssl_store_add_uncommitted_cafile_entry(new_cafile_entry)) {
2874 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002875 goto error;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002876 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002877 }
2878
2879 list_for_each_entry_from(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002880 struct ckch_inst *new_inst;
2881
2882 /* save the next ckchi to compute */
2883 ctx->next_ckchi_link = ckchi_link;
2884
2885 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2886 if (y >= 10) {
2887 applet_have_more_data(appctx); /* let's come back later */
2888 goto yield;
2889 }
2890
2891 /* display one dot per new instance */
2892 if (applet_putstr(appctx, ".") == -1)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002893 goto yield;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002894
2895 /* Rebuild a new ckch instance that uses the same ckch_store
2896 * than a reference ckchi instance but will use a new CA file. */
2897 ctx->err = NULL;
2898 if (ckch_inst_rebuild(ckchi_link->ckch_inst->ckch_store, ckchi_link->ckch_inst, &new_inst, &ctx->err)) {
2899 ctx->state = CACRL_ST_ERROR;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002900 goto error;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002901 }
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002902
2903 y++;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002904 }
2905
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002906 ctx->state = CACRL_ST_INSERT;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002907 __fallthrough;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002908 case CACRL_ST_INSERT:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002909 /* The generation is finished, we can insert everything */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002910
2911 /* insert the new ckch_insts in the crtlist_entry */
2912 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2913 if (ckchi_link->ckch_inst->crtlist_entry)
2914 LIST_INSERT(&ckchi_link->ckch_inst->crtlist_entry->ckch_inst,
2915 &ckchi_link->ckch_inst->by_crtlist_entry);
2916 }
2917
2918 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2919 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2920 __ssl_sock_load_new_ckch_instance(ckchi_link->ckch_inst);
2921 }
2922
William Lallemande0fa91f2022-08-31 14:26:49 +02002923 /* delete the old sni_ctx, the old ckch_insts
2924 * and the ckch_store. ckch_inst_free() also
2925 * manipulates the list so it's cleaner to loop
2926 * until it's empty */
2927 while (!LIST_ISEMPTY(&old_cafile_entry->ckch_inst_link)) {
2928 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2929
2930 LIST_DEL_INIT(&ckchi_link->list); /* must reinit because ckch_inst checks the list */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002931 __ckch_inst_free_locked(ckchi_link->ckch_inst);
William Lallemande0fa91f2022-08-31 14:26:49 +02002932 free(ckchi_link);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002933 }
2934
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002935 /* Remove the old cafile entry from the tree */
2936 ebmb_delete(&old_cafile_entry->node);
2937 ssl_store_delete_cafile_entry(old_cafile_entry);
2938
Christopher Faulet6af2fc62022-06-03 11:42:38 +02002939 ctx->old_entry = ctx->new_entry = NULL;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002940 ctx->state = CACRL_ST_SUCCESS;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002941 __fallthrough;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002942 case CACRL_ST_SUCCESS:
2943 if (applet_putstr(appctx, "\nSuccess!\n") == -1)
2944 goto yield;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002945 ctx->state = CACRL_ST_FIN;
Willy Tarreau6fcc86b2022-11-14 07:05:31 +01002946 __fallthrough;
Willy Tarreau1d6dd802022-05-05 08:17:29 +02002947 case CACRL_ST_FIN:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002948 /* we achieved the transaction, we can set everything to NULL */
Willy Tarreaudec23dc2022-05-04 20:25:05 +02002949 switch (ctx->cafile_type) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002950 case CAFILE_CERT:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002951 cafile_transaction.old_cafile_entry = NULL;
2952 cafile_transaction.new_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002953 cafile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002954 break;
2955 case CAFILE_CRL:
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002956 crlfile_transaction.old_crlfile_entry = NULL;
2957 crlfile_transaction.new_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02002958 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002959 break;
2960 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002961 goto end;
Christopher Faulete9c3bd12022-05-31 17:51:06 +02002962
2963 case CACRL_ST_ERROR:
2964 error:
2965 chunk_printf(&trash, "\n%sFailed!\n", ctx->err);
2966 if (applet_putchk(appctx, &trash) == -1)
2967 goto yield;
2968 ctx->state = CACRL_ST_FIN;
2969 break;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002970 }
2971 }
2972end:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002973 /* success: call the release function and don't come back */
2974 return 1;
2975yield:
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002976 return 0; /* should come back */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002977}
2978
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002979
2980/* parsing function of 'abort ssl ca-file' */
2981static int cli_parse_abort_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2982{
2983 char *err = NULL;
2984
2985 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2986 return 1;
2987
2988 if (!*args[3])
2989 return cli_err(appctx, "'abort ssl ca-file' expects a filename\n");
2990
2991 /* The operations on the CKCH architecture are locked so we can
2992 * manipulate ckch_store and ckch_inst */
2993 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2994 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2995
2996 if (!cafile_transaction.path) {
2997 memprintf(&err, "No ongoing transaction!\n");
2998 goto error;
2999 }
3000
3001 if (strcmp(cafile_transaction.path, args[3]) != 0) {
3002 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", cafile_transaction.path, args[3]);
3003 goto error;
3004 }
3005
3006 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3007 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
3008 cafile_transaction.new_cafile_entry = NULL;
3009 cafile_transaction.old_cafile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02003010 cafile_transaction.path = NULL;
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01003011
3012 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3013
3014 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3015 return cli_dynmsg(appctx, LOG_NOTICE, err);
3016
3017error:
3018 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3019
3020 return cli_dynerr(appctx, err);
3021}
3022
Willy Tarreau821c3b02022-05-04 15:47:39 +02003023/* release function of the `commit ssl ca-file' command, free things and unlock the spinlock.
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003024 * It uses a commit_cacrlfile_ctx context.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003025 */
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003026static void cli_release_commit_cafile(struct appctx *appctx)
3027{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003028 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003029 struct cafile_entry *new_cafile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003030
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003031 /* Remove the uncommitted cafile_entry from the tree. */
3032 if (new_cafile_entry) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003033 ebmb_delete(&new_cafile_entry->node);
3034 ssl_store_delete_cafile_entry(new_cafile_entry);
3035 }
3036 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003037 ha_free(&ctx->err);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003038}
3039
3040
Willy Tarreau821c3b02022-05-04 15:47:39 +02003041/* IO handler of details "show ssl ca-file <filename[:index]>".
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003042 * It uses a show_cafile_ctx context, and the global
3043 * cafile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003044 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003045static int cli_io_handler_show_cafile_detail(struct appctx *appctx)
3046{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003047 struct show_cafile_ctx *ctx = appctx->svcctx;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003048 struct cafile_entry *cafile_entry = ctx->cur_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003049 struct buffer *out = alloc_trash_chunk();
William Lallemand03a32e52022-04-26 18:17:15 +02003050 int i = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003051 X509 *cert;
3052 STACK_OF(X509_OBJECT) *objs;
3053 int retval = 0;
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003054 int ca_index = ctx->ca_index;
3055 int show_all = ctx->show_all;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003056
3057 if (!out)
3058 goto end_no_putchk;
3059
3060 chunk_appendf(out, "Filename: ");
3061 if (cafile_entry == cafile_transaction.new_cafile_entry)
3062 chunk_appendf(out, "*");
3063 chunk_appendf(out, "%s\n", cafile_entry->path);
3064
3065 chunk_appendf(out, "Status: ");
3066 if (!cafile_entry->ca_store)
3067 chunk_appendf(out, "Empty\n");
3068 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3069 chunk_appendf(out, "Unused\n");
3070 else
3071 chunk_appendf(out, "Used\n");
3072
3073 if (!cafile_entry->ca_store)
3074 goto end;
3075
3076 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
William Lallemand03a32e52022-04-26 18:17:15 +02003077 for (i = ca_index; i < sk_X509_OBJECT_num(objs); i++) {
3078
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003079 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
3080 if (!cert)
3081 continue;
3082
William Lallemand03a32e52022-04-26 18:17:15 +02003083 /* file starts at line 1 */
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003084 chunk_appendf(out, " \nCertificate #%d:\n", i+1);
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003085 retval = show_cert_detail(cert, NULL, out);
3086 if (retval < 0)
3087 goto end_no_putchk;
William Lallemand03a32e52022-04-26 18:17:15 +02003088 else if (retval)
3089 goto yield;
3090
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003091 if (applet_putchk(appctx, out) == -1)
William Lallemand03a32e52022-04-26 18:17:15 +02003092 goto yield;
William Lallemand03a32e52022-04-26 18:17:15 +02003093
3094 if (!show_all) /* only need to dump one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003095 goto end;
3096 }
3097
3098end:
William Lallemand03a32e52022-04-26 18:17:15 +02003099 free_trash_chunk(out);
3100 return 1; /* end, don't come back */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003101
3102end_no_putchk:
3103 free_trash_chunk(out);
3104 return 1;
3105yield:
William Lallemand03a32e52022-04-26 18:17:15 +02003106 /* save the current state */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003107 ctx->ca_index = i;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003108 free_trash_chunk(out);
3109 return 0; /* should come back */
3110}
3111
3112
Willy Tarreau06305792022-05-04 15:57:30 +02003113/* parsing function for 'show ssl ca-file [cafile[:index]]'.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003114 * It prepares a show_cafile_ctx context, and checks the global
3115 * cafile_transaction under the ckch_lock (read only).
Willy Tarreau06305792022-05-04 15:57:30 +02003116 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003117static int cli_parse_show_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3118{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003119 struct show_cafile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003120 struct cafile_entry *cafile_entry;
William Lallemand03a32e52022-04-26 18:17:15 +02003121 int ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003122 char *colons;
3123 char *err = NULL;
3124
3125 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3126 return cli_err(appctx, "Can't allocate memory!\n");
3127
3128 /* The operations on the CKCH architecture are locked so we can
3129 * manipulate ckch_store and ckch_inst */
3130 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3131 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3132
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003133 ctx->show_all = 1; /* show all certificates */
3134 ctx->ca_index = 0;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003135 /* check if there is a certificate to lookup */
3136 if (*args[3]) {
3137
3138 /* Look for an optional CA index after the CA file name */
3139 colons = strchr(args[3], ':');
3140 if (colons) {
3141 char *endptr;
3142
3143 ca_index = strtol(colons + 1, &endptr, 10);
3144 /* Indexes start at 1 */
3145 if (colons + 1 == endptr || *endptr != '\0' || ca_index <= 0) {
3146 memprintf(&err, "wrong CA index after colons in '%s'!", args[3]);
3147 goto error;
3148 }
3149 *colons = '\0';
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003150 ctx->ca_index = ca_index - 1; /* we start counting at 0 in the ca_store, but at 1 on the CLI */
3151 ctx->show_all = 0; /* show only one certificate */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003152 }
3153
3154 if (*args[3] == '*') {
3155 if (!cafile_transaction.new_cafile_entry)
3156 goto error;
3157
3158 cafile_entry = cafile_transaction.new_cafile_entry;
3159
3160 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3161 goto error;
3162
3163 } else {
3164 /* Get the "original" cafile_entry and not the
3165 * uncommitted one if it exists. */
3166 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CERT)
3167 goto error;
3168 }
3169
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003170 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003171 /* use the IO handler that shows details */
3172 appctx->io_handler = cli_io_handler_show_cafile_detail;
3173 }
3174
3175 return 0;
3176
3177error:
3178 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3179 if (err)
3180 return cli_dynerr(appctx, err);
3181 return cli_err(appctx, "Can't display the CA file : Not found!\n");
3182}
3183
3184
3185/* release function of the 'show ssl ca-file' command */
3186static void cli_release_show_cafile(struct appctx *appctx)
3187{
3188 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3189}
3190
3191
3192/* This function returns the number of certificates in a cafile_entry. */
3193static int get_certificate_count(struct cafile_entry *cafile_entry)
3194{
3195 int cert_count = 0;
3196 STACK_OF(X509_OBJECT) *objs;
3197
3198 if (cafile_entry && cafile_entry->ca_store) {
3199 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3200 if (objs)
3201 cert_count = sk_X509_OBJECT_num(objs);
3202 }
3203 return cert_count;
3204}
3205
3206/* IO handler of "show ssl ca-file". The command taking a specific CA file name
Willy Tarreau821c3b02022-05-04 15:47:39 +02003207 * is managed in cli_io_handler_show_cafile_detail.
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003208 * It uses a show_cafile_ctx and the global cafile_transaction.new_cafile_entry
3209 * in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003210 */
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003211static int cli_io_handler_show_cafile(struct appctx *appctx)
3212{
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003213 struct show_cafile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003214 struct buffer *trash = alloc_trash_chunk();
3215 struct ebmb_node *node;
Christopher Faulet677cb4f2022-06-03 16:25:35 +02003216 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003217
3218 if (trash == NULL)
3219 return 1;
3220
Christopher Faulet5a2154b2022-06-03 10:42:48 +02003221 if (!ctx->old_cafile_entry && cafile_transaction.old_cafile_entry) {
3222 chunk_appendf(trash, "# transaction\n");
3223 chunk_appendf(trash, "*%s", cafile_transaction.old_cafile_entry->path);
3224 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_transaction.new_cafile_entry));
3225 if (applet_putchk(appctx, trash) == -1)
3226 goto yield;
3227 ctx->old_cafile_entry = cafile_transaction.new_cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003228 }
3229
3230 /* First time in this io_handler. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003231 if (!ctx->cur_cafile_entry) {
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003232 chunk_appendf(trash, "# filename\n");
3233 node = ebmb_first(&cafile_tree);
3234 } else {
3235 /* We yielded during a previous call. */
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003236 node = &ctx->cur_cafile_entry->node;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003237 }
3238
3239 while (node) {
3240 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3241 if (cafile_entry->type == CAFILE_CERT) {
3242 chunk_appendf(trash, "%s", cafile_entry->path);
3243
3244 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_entry));
3245 }
3246
3247 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003248 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003249 goto yield;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003250 }
3251
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003252 ctx->cur_cafile_entry = NULL;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003253 free_trash_chunk(trash);
3254 return 1;
3255yield:
3256
3257 free_trash_chunk(trash);
Willy Tarreau50c2f1e2022-05-04 19:26:59 +02003258 ctx->cur_cafile_entry = cafile_entry;
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003259 return 0; /* should come back */
3260}
3261
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003262/* parsing function of 'del ssl ca-file' */
3263static int cli_parse_del_cafile(char **args, char *payload, struct appctx *appctx, void *private)
3264{
3265 struct cafile_entry *cafile_entry;
3266 char *err = NULL;
3267 char *filename;
3268
3269 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3270 return 1;
3271
3272 if (!*args[3])
3273 return cli_err(appctx, "'del ssl ca-file' expects a CA file name\n");
3274
3275 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3276 return cli_err(appctx, "Can't delete the CA file!\nOperations on certificates are currently locked!\n");
3277
3278 filename = args[3];
3279
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003280 if (cafile_transaction.path && strcmp(cafile_transaction.path, filename) == 0) {
3281 memprintf(&err, "ongoing transaction for the CA file '%s'", filename);
3282 goto error;
3283 }
3284
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01003285 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3286 if (!cafile_entry) {
3287 memprintf(&err, "CA file '%s' doesn't exist!\n", filename);
3288 goto error;
3289 }
3290
3291 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3292 memprintf(&err, "CA file '%s' in use, can't be deleted!\n", filename);
3293 goto error;
3294 }
3295
3296 /* Remove the cafile_entry from the tree */
3297 ebmb_delete(&cafile_entry->node);
3298 ssl_store_delete_cafile_entry(cafile_entry);
3299
3300 memprintf(&err, "CA file '%s' deleted!\n", filename);
3301
3302 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3303 return cli_dynmsg(appctx, LOG_NOTICE, err);
3304
3305error:
3306 memprintf(&err, "Can't remove the CA file: %s\n", err ? err : "");
3307 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3308 return cli_dynerr(appctx, err);
3309}
3310
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003311/* parsing function of 'new ssl crl-file' */
3312static int cli_parse_new_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3313{
3314 struct cafile_entry *cafile_entry;
3315 char *err = NULL;
3316 char *path;
3317
3318 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3319 return 1;
3320
3321 if (!*args[3])
3322 return cli_err(appctx, "'new ssl crl-file' expects a filename\n");
3323
3324 path = args[3];
3325
3326 /* The operations on the CKCH architecture are locked so we can
3327 * manipulate ckch_store and ckch_inst */
3328 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003329 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 +02003330
3331 cafile_entry = ssl_store_get_cafile_entry(path, 0);
3332 if (cafile_entry) {
3333 memprintf(&err, "CRL file '%s' already exists!\n", path);
3334 goto error;
3335 }
3336
3337 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CRL);
3338 if (!cafile_entry) {
3339 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3340 goto error;
3341 }
3342
3343 /* Add the newly created cafile_entry to the tree so that
3344 * any new ckch instance created from now can use it. */
3345 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
3346 goto error;
3347
3348 memprintf(&err, "New CRL file created '%s'!\n", path);
3349
3350 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3351 return cli_dynmsg(appctx, LOG_NOTICE, err);
3352error:
3353 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3354 return cli_dynerr(appctx, err);
3355}
3356
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003357/* Parsing function of `set ssl crl-file` */
3358static int cli_parse_set_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3359{
Christopher Faulet1f90f332022-06-03 16:34:30 +02003360 struct cafile_entry *old_crlfile_entry = NULL;
3361 struct cafile_entry *new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003362 char *err = NULL;
3363 int errcode = 0;
3364 struct buffer *buf;
3365
3366 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3367 return 1;
3368
3369 if (!*args[3] || !payload)
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003370 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 +02003371
3372 /* The operations on the CKCH architecture are locked so we can
3373 * manipulate ckch_store and ckch_inst */
3374 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3375 return cli_err(appctx, "Can't update the CRL file!\nOperations on certificates are currently locked!\n");
3376
3377 if ((buf = alloc_trash_chunk()) == NULL) {
3378 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3379 errcode |= ERR_ALERT | ERR_FATAL;
3380 goto end;
3381 }
3382
3383 if (!chunk_strcpy(buf, args[3])) {
3384 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3385 errcode |= ERR_ALERT | ERR_FATAL;
3386 goto end;
3387 }
3388
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003389 old_crlfile_entry = NULL;
3390 new_crlfile_entry = NULL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003391
3392 /* if there is an ongoing transaction */
3393 if (crlfile_transaction.path) {
3394 /* if there is an ongoing transaction, check if this is the same file */
3395 if (strcmp(crlfile_transaction.path, buf->area) != 0) {
3396 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, buf->area);
3397 errcode |= ERR_ALERT | ERR_FATAL;
3398 goto end;
3399 }
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003400 old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003401 }
3402 else {
3403 /* lookup for the certificate in the tree */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003404 old_crlfile_entry = ssl_store_get_cafile_entry(buf->area, 0);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003405 }
3406
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003407 if (!old_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003408 memprintf(&err, "%sCan't replace a CRL file which is not referenced by the configuration!\n",
3409 err ? err : "");
3410 errcode |= ERR_ALERT | ERR_FATAL;
3411 goto end;
3412 }
3413
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003414 /* Create a new cafile_entry without adding it to the cafile tree. */
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003415 new_crlfile_entry = ssl_store_create_cafile_entry(old_crlfile_entry->path, NULL, CAFILE_CRL);
3416 if (!new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003417 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3418 errcode |= ERR_ALERT | ERR_FATAL;
3419 goto end;
3420 }
3421
3422 /* Fill the new entry with the new CRL. */
William Lallemandd4774d32022-07-29 17:08:02 +02003423 if (ssl_store_load_ca_from_buf(new_crlfile_entry, payload, 0)) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003424 memprintf(&err, "%sInvalid payload\n", err ? err : "");
3425 errcode |= ERR_ALERT | ERR_FATAL;
3426 goto end;
3427 }
3428
3429 /* we succeed, we can save the crl in the transaction */
3430
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003431 /* if there wasn't a transaction, update the old CRL */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003432 if (!crlfile_transaction.old_crlfile_entry) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003433 crlfile_transaction.old_crlfile_entry = old_crlfile_entry;
3434 crlfile_transaction.path = old_crlfile_entry->path;
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003435 err = memprintf(&err, "transaction created for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003436 } else {
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003437 err = memprintf(&err, "transaction updated for CRL %s!\n", crlfile_transaction.path);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003438 }
3439
3440 /* free the previous CRL file if there was a transaction */
3441 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3442
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003443 crlfile_transaction.new_crlfile_entry = new_crlfile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003444
3445 /* creates the SNI ctxs later in the IO handler */
3446
3447end:
3448 free_trash_chunk(buf);
3449
3450 if (errcode & ERR_CODE) {
Christopher Fauletd6c66f02022-06-03 11:59:10 +02003451 ssl_store_delete_cafile_entry(new_crlfile_entry);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003452 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3453 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
3454 } else {
3455
3456 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3457 return cli_dynmsg(appctx, LOG_NOTICE, err);
3458 }
3459}
3460
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003461/* Parsing function of 'commit ssl crl-file'.
3462 * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl ca-file".
3463 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003464static int cli_parse_commit_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3465{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003466 struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003467 char *err = NULL;
3468
3469 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3470 return 1;
3471
3472 if (!*args[3])
3473 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
3474
3475 /* The operations on the CKCH architecture are locked so we can
3476 * manipulate ckch_store and ckch_inst */
3477 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3478 return cli_err(appctx, "Can't commit the CRL file!\nOperations on certificates are currently locked!\n");
3479
3480 if (!crlfile_transaction.path) {
3481 memprintf(&err, "No ongoing transaction! !\n");
3482 goto error;
3483 }
3484
3485 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3486 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, args[3]);
3487 goto error;
3488 }
3489 /* init the appctx structure */
Willy Tarreau1d6dd802022-05-05 08:17:29 +02003490 ctx->state = CACRL_ST_INIT;
Christopher Fauletf814c4a2022-06-03 11:32:05 +02003491 ctx->next_ckchi_link = NULL;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003492 ctx->old_entry = crlfile_transaction.old_crlfile_entry;
3493 ctx->new_entry = crlfile_transaction.new_crlfile_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003494 ctx->cafile_type = CAFILE_CRL;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003495
3496 return 0;
3497
3498error:
3499
3500 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3501 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
3502
3503 return cli_dynerr(appctx, err);
3504}
3505
3506
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003507/* release function of the `commit ssl crl-file' command, free things and unlock the spinlock.
3508 * it uses a commit_cacrlfile_ctx that's the same as for "commit ssl ca-file".
3509 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003510static void cli_release_commit_crlfile(struct appctx *appctx)
3511{
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003512 struct commit_cacrlfile_ctx *ctx = appctx->svcctx;
Christopher Faulet6af2fc62022-06-03 11:42:38 +02003513 struct cafile_entry *new_crlfile_entry = ctx->new_entry;
Willy Tarreaudec23dc2022-05-04 20:25:05 +02003514
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003515 /* Remove the uncommitted cafile_entry from the tree. */
3516 if (new_crlfile_entry) {
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003517 ebmb_delete(&new_crlfile_entry->node);
3518 ssl_store_delete_cafile_entry(new_crlfile_entry);
3519 }
3520 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
Christopher Faulete9c3bd12022-05-31 17:51:06 +02003521 ha_free(&ctx->err);
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003522}
3523
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003524/* parsing function of 'del ssl crl-file' */
3525static int cli_parse_del_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3526{
3527 struct cafile_entry *cafile_entry;
3528 char *err = NULL;
3529 char *filename;
3530
3531 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3532 return 1;
3533
3534 if (!*args[3])
3535 return cli_err(appctx, "'del ssl crl-file' expects a CRL file name\n");
3536
3537 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3538 return cli_err(appctx, "Can't delete the CRL file!\nOperations on certificates are currently locked!\n");
3539
3540 filename = args[3];
3541
Christopher Faulet1f08fa42022-05-31 18:06:30 +02003542 if (crlfile_transaction.path && strcmp(crlfile_transaction.path, filename) == 0) {
3543 memprintf(&err, "ongoing transaction for the CRL file '%s'", filename);
3544 goto error;
3545 }
3546
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003547 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3548 if (!cafile_entry) {
3549 memprintf(&err, "CRL file '%s' doesn't exist!\n", filename);
3550 goto error;
3551 }
3552 if (cafile_entry->type != CAFILE_CRL) {
3553 memprintf(&err, "'del ssl crl-file' does not work on CA files!\n");
3554 goto error;
3555 }
3556
3557 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3558 memprintf(&err, "CRL file '%s' in use, can't be deleted!\n", filename);
3559 goto error;
3560 }
3561
3562 /* Remove the cafile_entry from the tree */
3563 ebmb_delete(&cafile_entry->node);
3564 ssl_store_delete_cafile_entry(cafile_entry);
3565
3566 memprintf(&err, "CRL file '%s' deleted!\n", filename);
3567
3568 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3569 return cli_dynmsg(appctx, LOG_NOTICE, err);
3570
3571error:
3572 memprintf(&err, "Can't remove the CRL file: %s\n", err ? err : "");
3573 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3574 return cli_dynerr(appctx, err);
3575}
3576
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003577/* parsing function of 'abort ssl crl-file' */
3578static int cli_parse_abort_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3579{
3580 char *err = NULL;
3581
3582 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3583 return 1;
3584
3585 if (!*args[3])
3586 return cli_err(appctx, "'abort ssl crl-file' expects a filename\n");
3587
3588 /* The operations on the CKCH architecture are locked so we can
3589 * manipulate ckch_store and ckch_inst */
3590 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3591 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
3592
3593 if (!crlfile_transaction.path) {
3594 memprintf(&err, "No ongoing transaction!\n");
3595 goto error;
3596 }
3597
3598 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3599 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", crlfile_transaction.path, args[3]);
3600 goto error;
3601 }
3602
3603 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3604 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3605 crlfile_transaction.new_crlfile_entry = NULL;
3606 crlfile_transaction.old_crlfile_entry = NULL;
Christopher Faulet1e00c7e2022-05-31 18:10:19 +02003607 crlfile_transaction.path = NULL;
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003608
3609 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3610
3611 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3612 return cli_dynmsg(appctx, LOG_NOTICE, err);
3613
3614error:
3615 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3616
3617 return cli_dynerr(appctx, err);
3618}
3619
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003620
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003621/*
3622 * Display a Certificate Resignation List's information.
3623 * The information displayed is inspired by the output of 'openssl crl -in
3624 * crl.pem -text'.
3625 * Returns 0 in case of success.
3626 */
3627static int show_crl_detail(X509_CRL *crl, struct buffer *out)
3628{
3629 BIO *bio = NULL;
3630 struct buffer *tmp = alloc_trash_chunk();
3631 long version;
3632 X509_NAME *issuer;
3633 int write = -1;
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003634#ifndef USE_OPENSSL_WOLFSSL
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003635 STACK_OF(X509_REVOKED) *rev = NULL;
3636 X509_REVOKED *rev_entry = NULL;
3637 int i;
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003638#endif
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003639
3640 if (!tmp)
3641 return -1;
3642
3643 if ((bio = BIO_new(BIO_s_mem())) == NULL)
3644 goto end;
3645
3646 /* Version (as displayed by 'openssl crl') */
3647 version = X509_CRL_get_version(crl);
3648 chunk_appendf(out, "Version %ld\n", version + 1);
3649
3650 /* Signature Algorithm */
3651 chunk_appendf(out, "Signature Algorithm: %s\n", OBJ_nid2ln(X509_CRL_get_signature_nid(crl)));
3652
3653 /* Issuer */
3654 chunk_appendf(out, "Issuer: ");
3655 if ((issuer = X509_CRL_get_issuer(crl)) == NULL)
3656 goto end;
3657 if ((ssl_sock_get_dn_oneline(issuer, tmp)) == -1)
3658 goto end;
3659 *(tmp->area + tmp->data) = '\0';
3660 chunk_appendf(out, "%s\n", tmp->area);
3661
3662 /* Last Update */
3663 chunk_appendf(out, "Last Update: ");
3664 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003665 if (BIO_reset(bio) == -1)
3666 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003667 if (ASN1_TIME_print(bio, X509_CRL_get0_lastUpdate(crl)) == 0)
3668 goto end;
3669 write = BIO_read(bio, tmp->area, tmp->size-1);
3670 tmp->area[write] = '\0';
3671 chunk_appendf(out, "%s\n", tmp->area);
3672
3673
3674 /* Next Update */
3675 chunk_appendf(out, "Next Update: ");
3676 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003677 if (BIO_reset(bio) == -1)
3678 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003679 if (ASN1_TIME_print(bio, X509_CRL_get0_nextUpdate(crl)) == 0)
3680 goto end;
3681 write = BIO_read(bio, tmp->area, tmp->size-1);
3682 tmp->area[write] = '\0';
3683 chunk_appendf(out, "%s\n", tmp->area);
3684
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003685#ifndef USE_OPENSSL_WOLFSSL
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003686 /* Revoked Certificates */
3687 rev = X509_CRL_get_REVOKED(crl);
3688 if (sk_X509_REVOKED_num(rev) > 0)
3689 chunk_appendf(out, "Revoked Certificates:\n");
3690 else
3691 chunk_appendf(out, "No Revoked Certificates.\n");
3692
3693 for (i = 0; i < sk_X509_REVOKED_num(rev); i++) {
3694 rev_entry = sk_X509_REVOKED_value(rev, i);
3695
3696 /* Serial Number and Revocation Date */
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003697 if (BIO_reset(bio) == -1)
3698 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003699 BIO_printf(bio , " Serial Number: ");
Remi Tricot-Le Breton18c7d832021-05-17 18:38:34 +02003700 i2a_ASN1_INTEGER(bio, (ASN1_INTEGER*)X509_REVOKED_get0_serialNumber(rev_entry));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003701 BIO_printf(bio, "\n Revocation Date: ");
Remi Tricot-Le Bretona6b27842021-05-18 10:06:00 +02003702 if (ASN1_TIME_print(bio, X509_REVOKED_get0_revocationDate(rev_entry)) == 0)
3703 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003704 BIO_printf(bio, "\n");
3705
3706 write = BIO_read(bio, tmp->area, tmp->size-1);
3707 tmp->area[write] = '\0';
3708 chunk_appendf(out, "%s", tmp->area);
3709 }
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01003710#endif /* not USE_OPENSSL_WOLFSSL */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003711
3712end:
3713 free_trash_chunk(tmp);
3714 if (bio)
3715 BIO_free(bio);
3716
3717 return 0;
3718}
3719
Willy Tarreau821c3b02022-05-04 15:47:39 +02003720/* IO handler of details "show ssl crl-file <filename[:index]>".
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003721 * It uses show_crlfile_ctx and the global
3722 * crlfile_transaction.new_cafile_entry in read-only.
Willy Tarreau821c3b02022-05-04 15:47:39 +02003723 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003724static int cli_io_handler_show_crlfile_detail(struct appctx *appctx)
3725{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003726 struct show_crlfile_ctx *ctx = appctx->svcctx;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003727 struct cafile_entry *cafile_entry = ctx->cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003728 struct buffer *out = alloc_trash_chunk();
3729 int i;
3730 X509_CRL *crl;
3731 STACK_OF(X509_OBJECT) *objs;
3732 int retval = 0;
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003733 int index = ctx->index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003734
3735 if (!out)
3736 goto end_no_putchk;
3737
3738 chunk_appendf(out, "Filename: ");
3739 if (cafile_entry == crlfile_transaction.new_crlfile_entry)
3740 chunk_appendf(out, "*");
3741 chunk_appendf(out, "%s\n", cafile_entry->path);
3742
3743 chunk_appendf(out, "Status: ");
3744 if (!cafile_entry->ca_store)
3745 chunk_appendf(out, "Empty\n");
3746 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3747 chunk_appendf(out, "Unused\n");
3748 else
3749 chunk_appendf(out, "Used\n");
3750
3751 if (!cafile_entry->ca_store)
3752 goto end;
3753
3754 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3755 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
3756 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
3757 if (!crl)
3758 continue;
3759
3760 /* CRL indexes start at 1 on the CLI output. */
3761 if (index && index-1 != i)
3762 continue;
3763
Remi Tricot-Le Bretone8041fe2022-04-05 16:44:21 +02003764 chunk_appendf(out, " \nCertificate Revocation List #%d:\n", i+1);
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003765 retval = show_crl_detail(crl, out);
3766 if (retval < 0)
3767 goto end_no_putchk;
3768 else if (retval || index)
3769 goto end;
3770 }
3771
3772end:
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003773 if (applet_putchk(appctx, out) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003774 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003775
3776end_no_putchk:
3777 free_trash_chunk(out);
3778 return 1;
3779yield:
3780 free_trash_chunk(out);
3781 return 0; /* should come back */
3782}
3783
Willy Tarreau821c3b02022-05-04 15:47:39 +02003784/* parsing function for 'show ssl crl-file [crlfile[:index]]'.
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003785 * It sets the context to a show_crlfile_ctx, and the global
Willy Tarreau821c3b02022-05-04 15:47:39 +02003786 * cafile_transaction.new_crlfile_entry under the ckch_lock.
3787 */
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003788static int cli_parse_show_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3789{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003790 struct show_crlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003791 struct cafile_entry *cafile_entry;
3792 long index = 0;
3793 char *colons;
3794 char *err = NULL;
3795
3796 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3797 return cli_err(appctx, "Can't allocate memory!\n");
3798
3799 /* The operations on the CKCH architecture are locked so we can
3800 * manipulate ckch_store and ckch_inst */
3801 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3802 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3803
3804 /* check if there is a certificate to lookup */
3805 if (*args[3]) {
3806
3807 /* Look for an optional index after the CRL file name */
3808 colons = strchr(args[3], ':');
3809 if (colons) {
3810 char *endptr;
3811
3812 index = strtol(colons + 1, &endptr, 10);
3813 /* Indexes start at 1 */
3814 if (colons + 1 == endptr || *endptr != '\0' || index <= 0) {
3815 memprintf(&err, "wrong CRL index after colons in '%s'!", args[3]);
3816 goto error;
3817 }
3818 *colons = '\0';
3819 }
3820
3821 if (*args[3] == '*') {
3822 if (!crlfile_transaction.new_crlfile_entry)
3823 goto error;
3824
3825 cafile_entry = crlfile_transaction.new_crlfile_entry;
3826
3827 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3828 goto error;
3829
3830 } else {
3831 /* Get the "original" cafile_entry and not the
3832 * uncommitted one if it exists. */
3833 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CRL)
3834 goto error;
3835 }
3836
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003837 ctx->cafile_entry = cafile_entry;
3838 ctx->index = index;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003839 /* use the IO handler that shows details */
3840 appctx->io_handler = cli_io_handler_show_crlfile_detail;
3841 }
3842
3843 return 0;
3844
3845error:
3846 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3847 if (err)
3848 return cli_dynerr(appctx, err);
Remi Tricot-Le Breton444d7022022-05-05 17:18:40 +02003849 return cli_err(appctx, "Can't display the CRL file : Not found!\n");
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003850}
3851
3852/* IO handler of "show ssl crl-file". The command taking a specific CRL file name
3853 * is managed in cli_io_handler_show_crlfile_detail. */
3854static int cli_io_handler_show_crlfile(struct appctx *appctx)
3855{
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003856 struct show_crlfile_ctx *ctx = appctx->svcctx;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003857 struct buffer *trash = alloc_trash_chunk();
3858 struct ebmb_node *node;
Christopher Faulet88041b32022-06-03 16:26:56 +02003859 struct cafile_entry *cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003860
3861 if (trash == NULL)
3862 return 1;
3863
Christopher Faulet9a99e542022-06-03 10:32:18 +02003864 if (!ctx->old_crlfile_entry && crlfile_transaction.old_crlfile_entry) {
3865 chunk_appendf(trash, "# transaction\n");
3866 chunk_appendf(trash, "*%s\n", crlfile_transaction.old_crlfile_entry->path);
3867 if (applet_putchk(appctx, trash) == -1)
3868 goto yield;
3869 ctx->old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003870 }
3871
3872 /* First time in this io_handler. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003873 if (!ctx->cafile_entry) {
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003874 chunk_appendf(trash, "# filename\n");
3875 node = ebmb_first(&cafile_tree);
3876 } else {
3877 /* We yielded during a previous call. */
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003878 node = &ctx->cafile_entry->node;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003879 }
3880
3881 while (node) {
3882 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3883 if (cafile_entry->type == CAFILE_CRL) {
3884 chunk_appendf(trash, "%s\n", cafile_entry->path);
3885 }
3886
3887 node = ebmb_next(node);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003888 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003889 goto yield;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003890 }
3891
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003892 ctx->cafile_entry = NULL;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003893 free_trash_chunk(trash);
3894 return 1;
3895yield:
3896
3897 free_trash_chunk(trash);
Willy Tarreauf3e8b3e2022-05-04 19:38:57 +02003898 ctx->cafile_entry = cafile_entry;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003899 return 0; /* should come back */
3900}
3901
3902
3903/* release function of the 'show ssl crl-file' command */
3904static void cli_release_show_crlfile(struct appctx *appctx)
3905{
3906 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3907}
3908
3909
William Lallemandee8530c2020-06-23 18:19:42 +02003910void ckch_deinit()
3911{
3912 struct eb_node *node, *next;
3913 struct ckch_store *store;
William Lallemandb0c48272022-04-26 15:44:53 +02003914 struct ebmb_node *canode;
William Lallemandee8530c2020-06-23 18:19:42 +02003915
William Lallemandb0c48272022-04-26 15:44:53 +02003916 /* deinit the ckch stores */
William Lallemandee8530c2020-06-23 18:19:42 +02003917 node = eb_first(&ckchs_tree);
3918 while (node) {
3919 next = eb_next(node);
3920 store = ebmb_entry(node, struct ckch_store, node);
3921 ckch_store_free(store);
3922 node = next;
3923 }
William Lallemandb0c48272022-04-26 15:44:53 +02003924
3925 /* deinit the ca-file store */
3926 canode = ebmb_first(&cafile_tree);
3927 while (canode) {
3928 struct cafile_entry *entry = NULL;
3929
3930 entry = ebmb_entry(canode, struct cafile_entry, node);
3931 canode = ebmb_next(canode);
William Lallemand946580e2022-08-29 18:36:18 +02003932 ebmb_delete(&entry->node);
William Lallemandb0c48272022-04-26 15:44:53 +02003933 ssl_store_delete_cafile_entry(entry);
3934 }
William Lallemandee8530c2020-06-23 18:19:42 +02003935}
William Lallemandda8584c2020-05-14 10:14:37 +02003936
3937/* register cli keywords */
3938static struct cli_kw_list cli_kws = {{ },{
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003939 { { "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 },
3940 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
3941 { { "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 },
3942 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
3943 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
3944 { { "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 },
3945
Amaury Denoyelleb11ad9e2021-05-21 11:01:10 +02003946 { { "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 +02003947 { { "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 +01003948 { { "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 +02003949 { { "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 +01003950 { { "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 +01003951 { { "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 +01003952 { { "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 +02003953
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003954 { { "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 +02003955 { { "set", "ssl", "crl-file", NULL }, "set ssl crl-file <crlfile> <payload> : replace a CRL file", cli_parse_set_crlfile, NULL, NULL },
3956 { { "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 +02003957 { { "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 +02003958 { { "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 +02003959 { { "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 +02003960 { { NULL }, NULL, NULL, NULL }
3961}};
3962
3963INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3964