William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 1 | /* |
| 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 Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 14 | #include <dirent.h> |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 15 | #include <errno.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 20 | #include <syslog.h> |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | 74f2456 | 2021-10-06 17:54:12 +0200 | [diff] [blame] | 26 | #include <import/ebpttree.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 27 | #include <import/ebsttree.h> |
| 28 | |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 29 | #include <haproxy/applet.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 30 | #include <haproxy/base64.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 31 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 32 | #include <haproxy/cli.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 33 | #include <haproxy/errors.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 34 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 47d7f90 | 2020-06-04 14:25:47 +0200 | [diff] [blame] | 35 | #include <haproxy/ssl_ckch.h> |
Willy Tarreau | 209108d | 2020-06-04 20:30:20 +0200 | [diff] [blame] | 36 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | b2bd865 | 2020-06-04 14:21:22 +0200 | [diff] [blame] | 37 | #include <haproxy/ssl_utils.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 38 | #include <haproxy/stconn.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 39 | #include <haproxy/tools.h> |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 40 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 41 | /* Uncommitted CKCH transaction */ |
| 42 | |
| 43 | static struct { |
| 44 | struct ckch_store *new_ckchs; |
| 45 | struct ckch_store *old_ckchs; |
| 46 | char *path; |
| 47 | } ckchs_transaction; |
| 48 | |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 49 | /* Uncommitted CA file transaction */ |
| 50 | |
| 51 | static struct { |
| 52 | struct cafile_entry *old_cafile_entry; |
| 53 | struct cafile_entry *new_cafile_entry; |
| 54 | char *path; |
| 55 | } cafile_transaction; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 56 | |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 57 | /* Uncommitted CRL file transaction */ |
| 58 | |
| 59 | static struct { |
| 60 | struct cafile_entry *old_crlfile_entry; |
| 61 | struct cafile_entry *new_crlfile_entry; |
| 62 | char *path; |
| 63 | } crlfile_transaction; |
| 64 | |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 65 | /* CLI context used by "show cafile" */ |
| 66 | struct 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 Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 72 | |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 73 | /* CLI context used by "show crlfile" */ |
| 74 | struct show_crlfile_ctx { |
| 75 | struct cafile_entry *cafile_entry; |
Christopher Faulet | 51095ee | 2022-06-03 10:21:27 +0200 | [diff] [blame] | 76 | struct cafile_entry *old_crlfile_entry; |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 77 | int index; |
| 78 | }; |
| 79 | |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 80 | /* CLI context used by "show cert" */ |
| 81 | struct show_cert_ctx { |
| 82 | struct ckch_store *old_ckchs; |
| 83 | struct ckch_store *cur_ckchs; |
| 84 | int transaction; |
| 85 | }; |
| 86 | |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 87 | /* CLI context used by "commit cert" */ |
| 88 | struct commit_cert_ctx { |
| 89 | struct ckch_store *old_ckchs; |
| 90 | struct ckch_store *new_ckchs; |
| 91 | struct ckch_inst *next_ckchi; |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 92 | char *err; |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 93 | enum { |
| 94 | CERT_ST_INIT = 0, |
| 95 | CERT_ST_GEN, |
| 96 | CERT_ST_INSERT, |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 97 | CERT_ST_SUCCESS, |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 98 | CERT_ST_FIN, |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 99 | CERT_ST_ERROR, |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 100 | } state; |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 103 | /* CLI context used by "commit cafile" and "commit crlfile" */ |
| 104 | struct commit_cacrlfile_ctx { |
Christopher Faulet | 6af2fc6 | 2022-06-03 11:42:38 +0200 | [diff] [blame] | 105 | struct cafile_entry *old_entry; |
| 106 | struct cafile_entry *new_entry; |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 107 | struct ckch_inst_link *next_ckchi_link; |
Christopher Faulet | 14df913 | 2022-06-03 09:17:09 +0200 | [diff] [blame] | 108 | enum cafile_type cafile_type; /* either CA or CRL, depending on the current command */ |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 109 | char *err; |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 110 | enum { |
| 111 | CACRL_ST_INIT = 0, |
| 112 | CACRL_ST_GEN, |
| 113 | CACRL_ST_INSERT, |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 114 | CACRL_ST_SUCCESS, |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 115 | CACRL_ST_FIN, |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 116 | CACRL_ST_ERROR, |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 117 | } state; |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 118 | }; |
| 119 | |
Willy Tarreau | a37693f | 2022-05-04 20:12:55 +0200 | [diff] [blame] | 120 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 121 | /******************** 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 | */ |
| 131 | static 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 | |
| 161 | out: |
| 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 */ |
| 168 | int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err) |
| 169 | { |
| 170 | int fd = -1; |
| 171 | int r = 0; |
| 172 | int ret = 1; |
| 173 | struct buffer tmp; |
| 174 | struct buffer *src; |
| 175 | struct buffer *sctl; |
| 176 | |
| 177 | if (buf) { |
William Lallemand | 8d67394 | 2021-01-27 14:58:51 +0100 | [diff] [blame] | 178 | chunk_initstr(&tmp, buf); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 179 | 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 Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 207 | ha_free(&sctl); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 208 | goto end; |
| 209 | } |
| 210 | /* no error, fill ckch with new context, old context must be free */ |
| 211 | if (ckch->sctl) { |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 212 | ha_free(&ckch->sctl->area); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 213 | free(ckch->sctl); |
| 214 | } |
| 215 | ckch->sctl = sctl; |
| 216 | ret = 0; |
| 217 | end: |
| 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 Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 226 | * This function load the OCSP Response in DER format contained in file at |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 227 | * path 'ocsp_path' or base64 in a buffer <buf> |
| 228 | * |
| 229 | * Returns 0 on success, 1 in error case. |
| 230 | */ |
| 231 | int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err) |
| 232 | { |
| 233 | int fd = -1; |
| 234 | int r = 0; |
| 235 | int ret = 1; |
| 236 | struct buffer *ocsp_response; |
| 237 | struct buffer *src = NULL; |
| 238 | |
| 239 | if (buf) { |
| 240 | int i, j; |
| 241 | /* if it's from a buffer it will be base64 */ |
| 242 | |
| 243 | /* remove \r and \n from the payload */ |
| 244 | for (i = 0, j = 0; buf[i]; i++) { |
| 245 | if (buf[i] == '\r' || buf[i] == '\n') |
| 246 | continue; |
| 247 | buf[j++] = buf[i]; |
| 248 | } |
| 249 | buf[j] = 0; |
| 250 | |
| 251 | ret = base64dec(buf, j, trash.area, trash.size); |
| 252 | if (ret < 0) { |
| 253 | memprintf(err, "Error reading OCSP response in base64 format"); |
| 254 | goto end; |
| 255 | } |
| 256 | trash.data = ret; |
| 257 | src = &trash; |
| 258 | } else { |
| 259 | fd = open(ocsp_path, O_RDONLY); |
| 260 | if (fd == -1) { |
| 261 | memprintf(err, "Error opening OCSP response file"); |
| 262 | goto end; |
| 263 | } |
| 264 | |
| 265 | trash.data = 0; |
| 266 | while (trash.data < trash.size) { |
| 267 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
| 268 | if (r < 0) { |
| 269 | if (errno == EINTR) |
| 270 | continue; |
| 271 | |
| 272 | memprintf(err, "Error reading OCSP response from file"); |
| 273 | goto end; |
| 274 | } |
| 275 | else if (r == 0) { |
| 276 | break; |
| 277 | } |
| 278 | trash.data += r; |
| 279 | } |
| 280 | close(fd); |
| 281 | fd = -1; |
| 282 | src = &trash; |
| 283 | } |
| 284 | |
| 285 | ocsp_response = calloc(1, sizeof(*ocsp_response)); |
| 286 | if (!chunk_dup(ocsp_response, src)) { |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 287 | ha_free(&ocsp_response); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 288 | goto end; |
| 289 | } |
| 290 | /* no error, fill ckch with new context, old context must be free */ |
| 291 | if (ckch->ocsp_response) { |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 292 | ha_free(&ckch->ocsp_response->area); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 293 | free(ckch->ocsp_response); |
| 294 | } |
| 295 | ckch->ocsp_response = ocsp_response; |
| 296 | ret = 0; |
| 297 | end: |
| 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 | */ |
| 320 | int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 321 | { |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 322 | struct buffer *fp = NULL; |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 323 | int ret = 1; |
Remi Tricot-Le Breton | 9bf3a1f | 2022-05-09 11:07:13 +0200 | [diff] [blame] | 324 | struct stat st; |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 325 | |
| 326 | /* try to load the PEM */ |
| 327 | if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) { |
| 328 | goto end; |
| 329 | } |
| 330 | |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 331 | 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 Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 344 | /* remove the ".crt" extension */ |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 345 | if (global_ssl.extra_files_noext) { |
| 346 | char *ext; |
| 347 | |
| 348 | /* look for the extension */ |
| 349 | if ((ext = strrchr(fp->area, '.'))) { |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 350 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 351 | if (strcmp(ext, ".crt") == 0) { |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 352 | *ext = '\0'; |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 353 | fp->data = strlen(fp->area); |
| 354 | } |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | } |
| 358 | |
Remi Tricot-Le Breton | 1bad7db | 2022-06-07 16:29:44 +0200 | [diff] [blame] | 359 | if (ckch->key == NULL) { |
| 360 | /* If no private key was found yet and we cannot look for it in extra |
| 361 | * files, raise an error. |
| 362 | */ |
| 363 | if (!(global_ssl.extra_files & SSL_GF_KEY)) { |
| 364 | memprintf(err, "%sNo Private Key found in '%s'.\n", err && *err ? *err : "", fp->area); |
| 365 | goto end; |
| 366 | } |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 367 | |
Remi Tricot-Le Breton | 1bad7db | 2022-06-07 16:29:44 +0200 | [diff] [blame] | 368 | /* 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 Breton | 9bf3a1f | 2022-05-09 11:07:13 +0200 | [diff] [blame] | 371 | err && *err ? *err : "", fp->area); |
Remi Tricot-Le Breton | 1bad7db | 2022-06-07 16:29:44 +0200 | [diff] [blame] | 372 | ret = 1; |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 373 | goto end; |
| 374 | } |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 375 | |
Remi Tricot-Le Breton | 1bad7db | 2022-06-07 16:29:44 +0200 | [diff] [blame] | 376 | if (stat(fp->area, &st) == 0) { |
| 377 | if (ssl_sock_load_key_into_ckch(fp->area, NULL, ckch, err)) { |
| 378 | memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n", |
| 379 | err && *err ? *err : "", fp->area); |
| 380 | goto end; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | if (ckch->key == NULL) { |
| 385 | memprintf(err, "%sNo Private Key found in '%s'.\n", err && *err ? *err : "", fp->area); |
| 386 | goto end; |
| 387 | } |
| 388 | /* remove the added extension */ |
| 389 | *(fp->area + fp->data - strlen(".key")) = '\0'; |
| 390 | b_sub(fp, strlen(".key")); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 391 | } |
Remi Tricot-Le Breton | 9bf3a1f | 2022-05-09 11:07:13 +0200 | [diff] [blame] | 392 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 393 | |
| 394 | if (!X509_check_private_key(ckch->cert, ckch->key)) { |
| 395 | memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n", |
| 396 | err && *err ? *err : "", path); |
| 397 | goto end; |
| 398 | } |
| 399 | |
Ilya Shipitsin | c47d676 | 2021-02-13 11:45:33 +0500 | [diff] [blame] | 400 | #ifdef HAVE_SSL_SCTL |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 401 | /* try to load the sctl file */ |
| 402 | if (global_ssl.extra_files & SSL_GF_SCTL) { |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 403 | struct stat st; |
| 404 | |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 405 | if (!chunk_strcat(fp, ".sctl") || b_data(fp) > MAXPATHLEN) { |
| 406 | memprintf(err, "%s '%s' filename too long'.\n", |
| 407 | err && *err ? *err : "", fp->area); |
| 408 | ret = 1; |
| 409 | goto end; |
| 410 | } |
| 411 | |
| 412 | if (stat(fp->area, &st) == 0) { |
| 413 | if (ssl_sock_load_sctl_from_file(fp->area, NULL, ckch, err)) { |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 414 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 415 | err && *err ? *err : "", fp->area); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 416 | ret = 1; |
| 417 | goto end; |
| 418 | } |
| 419 | } |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 420 | /* remove the added extension */ |
| 421 | *(fp->area + fp->data - strlen(".sctl")) = '\0'; |
| 422 | b_sub(fp, strlen(".sctl")); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 423 | } |
| 424 | #endif |
| 425 | |
| 426 | /* try to load an ocsp response file */ |
| 427 | if (global_ssl.extra_files & SSL_GF_OCSP) { |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 428 | struct stat st; |
| 429 | |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 430 | if (!chunk_strcat(fp, ".ocsp") || b_data(fp) > MAXPATHLEN) { |
| 431 | memprintf(err, "%s '%s' filename too long'.\n", |
| 432 | err && *err ? *err : "", fp->area); |
| 433 | ret = 1; |
| 434 | goto end; |
| 435 | } |
| 436 | |
| 437 | if (stat(fp->area, &st) == 0) { |
| 438 | if (ssl_sock_load_ocsp_response_from_file(fp->area, NULL, ckch, err)) { |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 439 | ret = 1; |
| 440 | goto end; |
| 441 | } |
| 442 | } |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 443 | /* remove the added extension */ |
| 444 | *(fp->area + fp->data - strlen(".ocsp")) = '\0'; |
| 445 | b_sub(fp, strlen(".ocsp")); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | #ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */ |
| 449 | if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) { |
| 450 | /* if no issuer was found, try to load an issuer from the .issuer */ |
| 451 | if (!ckch->ocsp_issuer) { |
| 452 | struct stat st; |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 453 | |
| 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 Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 460 | |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 461 | if (stat(fp->area, &st) == 0) { |
| 462 | if (ssl_sock_load_issuer_file_into_ckch(fp->area, NULL, ckch, err)) { |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 463 | ret = 1; |
| 464 | goto end; |
| 465 | } |
| 466 | |
| 467 | if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) { |
| 468 | memprintf(err, "%s '%s' is not an issuer'.\n", |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 469 | err && *err ? *err : "", fp->area); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 470 | ret = 1; |
| 471 | goto end; |
| 472 | } |
| 473 | } |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 474 | /* remove the added extension */ |
| 475 | *(fp->area + fp->data - strlen(".issuer")) = '\0'; |
| 476 | b_sub(fp, strlen(".issuer")); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | #endif |
| 480 | |
| 481 | ret = 0; |
| 482 | |
| 483 | end: |
| 484 | |
| 485 | ERR_clear_error(); |
| 486 | |
| 487 | /* Something went wrong in one of the reads */ |
| 488 | if (ret != 0) |
| 489 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 490 | |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 491 | free_trash_chunk(fp); |
| 492 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 493 | 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 | */ |
| 503 | int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err) |
| 504 | { |
| 505 | BIO *in = NULL; |
| 506 | int ret = 1; |
| 507 | EVP_PKEY *key = NULL; |
| 508 | |
| 509 | if (buf) { |
| 510 | /* reading from a buffer */ |
| 511 | in = BIO_new_mem_buf(buf, -1); |
| 512 | if (in == NULL) { |
| 513 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 514 | goto end; |
| 515 | } |
| 516 | |
| 517 | } else { |
| 518 | /* reading from a file */ |
| 519 | in = BIO_new(BIO_s_file()); |
| 520 | if (in == NULL) |
| 521 | goto end; |
| 522 | |
| 523 | if (BIO_read_filename(in, path) <= 0) |
| 524 | goto end; |
| 525 | } |
| 526 | |
| 527 | /* Read Private Key */ |
| 528 | key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 529 | if (key == NULL) { |
| 530 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 531 | err && *err ? *err : "", path); |
| 532 | goto end; |
| 533 | } |
| 534 | |
| 535 | ret = 0; |
| 536 | |
| 537 | SWAP(ckch->key, key); |
| 538 | |
| 539 | end: |
| 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 | */ |
| 559 | int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err) |
| 560 | { |
| 561 | BIO *in = NULL; |
| 562 | int ret = 1; |
| 563 | X509 *ca; |
| 564 | X509 *cert = NULL; |
| 565 | EVP_PKEY *key = NULL; |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 566 | HASSL_DH *dh = NULL; |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 567 | 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 Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 604 | dh = ssl_sock_get_dh_from_bio(in); |
| 605 | ERR_clear_error(); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 606 | /* 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) { |
| 619 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
| 620 | err && *err ? *err : "", path); |
| 621 | goto end; |
| 622 | } |
| 623 | |
| 624 | /* Look for a Certificate Chain */ |
| 625 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 626 | if (chain == NULL) |
| 627 | chain = sk_X509_new_null(); |
| 628 | if (!sk_X509_push(chain, ca)) { |
| 629 | X509_free(ca); |
| 630 | goto end; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | ret = ERR_get_error(); |
| 635 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 636 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 637 | err && *err ? *err : "", path); |
| 638 | goto end; |
| 639 | } |
| 640 | |
| 641 | /* once it loaded the PEM, it should remove everything else in the ckch */ |
| 642 | if (ckch->ocsp_response) { |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 643 | ha_free(&ckch->ocsp_response->area); |
| 644 | ha_free(&ckch->ocsp_response); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | if (ckch->sctl) { |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 648 | ha_free(&ckch->sctl->area); |
| 649 | ha_free(&ckch->sctl); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | if (ckch->ocsp_issuer) { |
| 653 | X509_free(ckch->ocsp_issuer); |
| 654 | ckch->ocsp_issuer = NULL; |
| 655 | } |
| 656 | |
| 657 | /* no error, fill ckch with new context, old context will be free at end: */ |
| 658 | SWAP(ckch->key, key); |
| 659 | SWAP(ckch->dh, dh); |
| 660 | SWAP(ckch->cert, cert); |
| 661 | SWAP(ckch->chain, chain); |
| 662 | |
| 663 | ret = 0; |
| 664 | |
| 665 | end: |
| 666 | |
| 667 | ERR_clear_error(); |
| 668 | if (in) |
| 669 | BIO_free(in); |
| 670 | if (key) |
| 671 | EVP_PKEY_free(key); |
| 672 | if (dh) |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 673 | HASSL_DH_free(dh); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 674 | if (cert) |
| 675 | X509_free(cert); |
| 676 | if (chain) |
| 677 | sk_X509_pop_free(chain, X509_free); |
| 678 | |
| 679 | return ret; |
| 680 | } |
| 681 | |
| 682 | /* Frees the contents of a cert_key_and_chain |
| 683 | */ |
| 684 | void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 685 | { |
| 686 | if (!ckch) |
| 687 | return; |
| 688 | |
| 689 | /* Free the certificate and set pointer to NULL */ |
| 690 | if (ckch->cert) |
| 691 | X509_free(ckch->cert); |
| 692 | ckch->cert = NULL; |
| 693 | |
| 694 | /* Free the key and set pointer to NULL */ |
| 695 | if (ckch->key) |
| 696 | EVP_PKEY_free(ckch->key); |
| 697 | ckch->key = NULL; |
| 698 | |
| 699 | /* Free each certificate in the chain */ |
| 700 | if (ckch->chain) |
| 701 | sk_X509_pop_free(ckch->chain, X509_free); |
| 702 | ckch->chain = NULL; |
| 703 | |
| 704 | if (ckch->dh) |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 705 | HASSL_DH_free(ckch->dh); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 706 | ckch->dh = NULL; |
| 707 | |
| 708 | if (ckch->sctl) { |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 709 | ha_free(&ckch->sctl->area); |
| 710 | ha_free(&ckch->sctl); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | if (ckch->ocsp_response) { |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 714 | ha_free(&ckch->ocsp_response->area); |
| 715 | ha_free(&ckch->ocsp_response); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | if (ckch->ocsp_issuer) |
| 719 | X509_free(ckch->ocsp_issuer); |
| 720 | ckch->ocsp_issuer = NULL; |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | * |
| 725 | * This function copy a cert_key_and_chain in memory |
| 726 | * |
| 727 | * It's used to try to apply changes on a ckch before committing them, because |
| 728 | * most of the time it's not possible to revert those changes |
| 729 | * |
| 730 | * Return a the dst or NULL |
| 731 | */ |
| 732 | struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src, |
| 733 | struct cert_key_and_chain *dst) |
| 734 | { |
William Lallemand | 6c09614 | 2021-02-23 14:45:45 +0100 | [diff] [blame] | 735 | if (!src || !dst) |
| 736 | return NULL; |
| 737 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 738 | if (src->cert) { |
| 739 | dst->cert = src->cert; |
| 740 | X509_up_ref(src->cert); |
| 741 | } |
| 742 | |
| 743 | if (src->key) { |
| 744 | dst->key = src->key; |
| 745 | EVP_PKEY_up_ref(src->key); |
| 746 | } |
| 747 | |
| 748 | if (src->chain) { |
| 749 | dst->chain = X509_chain_up_ref(src->chain); |
| 750 | } |
| 751 | |
| 752 | if (src->dh) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 753 | HASSL_DH_up_ref(src->dh); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 754 | dst->dh = src->dh; |
| 755 | } |
| 756 | |
| 757 | if (src->sctl) { |
| 758 | struct buffer *sctl; |
| 759 | |
| 760 | sctl = calloc(1, sizeof(*sctl)); |
| 761 | if (!chunk_dup(sctl, src->sctl)) { |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 762 | ha_free(&sctl); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 763 | goto error; |
| 764 | } |
| 765 | dst->sctl = sctl; |
| 766 | } |
| 767 | |
| 768 | if (src->ocsp_response) { |
| 769 | struct buffer *ocsp_response; |
| 770 | |
| 771 | ocsp_response = calloc(1, sizeof(*ocsp_response)); |
| 772 | if (!chunk_dup(ocsp_response, src->ocsp_response)) { |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 773 | ha_free(&ocsp_response); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 774 | goto error; |
| 775 | } |
| 776 | dst->ocsp_response = ocsp_response; |
| 777 | } |
| 778 | |
| 779 | if (src->ocsp_issuer) { |
| 780 | X509_up_ref(src->ocsp_issuer); |
| 781 | dst->ocsp_issuer = src->ocsp_issuer; |
| 782 | } |
| 783 | |
| 784 | return dst; |
| 785 | |
| 786 | error: |
| 787 | |
| 788 | /* free everything */ |
| 789 | ssl_sock_free_cert_key_and_chain_contents(dst); |
| 790 | |
| 791 | return NULL; |
| 792 | } |
| 793 | |
| 794 | /* |
| 795 | * return 0 on success or != 0 on failure |
| 796 | */ |
| 797 | int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err) |
| 798 | { |
| 799 | int ret = 1; |
| 800 | BIO *in = NULL; |
| 801 | X509 *issuer; |
| 802 | |
| 803 | if (buf) { |
| 804 | /* reading from a buffer */ |
| 805 | in = BIO_new_mem_buf(buf, -1); |
| 806 | if (in == NULL) { |
| 807 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 808 | goto end; |
| 809 | } |
| 810 | |
| 811 | } else { |
| 812 | /* reading from a file */ |
| 813 | in = BIO_new(BIO_s_file()); |
| 814 | if (in == NULL) |
| 815 | goto end; |
| 816 | |
| 817 | if (BIO_read_filename(in, path) <= 0) |
| 818 | goto end; |
| 819 | } |
| 820 | |
| 821 | issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 822 | if (!issuer) { |
| 823 | memprintf(err, "%s'%s' cannot be read or parsed'.\n", |
| 824 | err && *err ? *err : "", path); |
| 825 | goto end; |
| 826 | } |
| 827 | /* no error, fill ckch with new context, old context must be free */ |
| 828 | if (ckch->ocsp_issuer) |
| 829 | X509_free(ckch->ocsp_issuer); |
| 830 | ckch->ocsp_issuer = issuer; |
| 831 | ret = 0; |
| 832 | |
| 833 | end: |
| 834 | |
| 835 | ERR_clear_error(); |
| 836 | if (in) |
| 837 | BIO_free(in); |
| 838 | |
| 839 | return ret; |
| 840 | } |
| 841 | |
| 842 | /******************** ckch_store functions *********************************** |
| 843 | * The ckch_store is a structure used to cache and index the SSL files used in |
| 844 | * configuration |
| 845 | */ |
| 846 | |
| 847 | /* |
| 848 | * Free a ckch_store, its ckch, its instances and remove it from the ebtree |
| 849 | */ |
| 850 | void ckch_store_free(struct ckch_store *store) |
| 851 | { |
| 852 | struct ckch_inst *inst, *inst_s; |
| 853 | |
| 854 | if (!store) |
| 855 | return; |
| 856 | |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 857 | ssl_sock_free_cert_key_and_chain_contents(store->ckch); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 858 | |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 859 | ha_free(&store->ckch); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 860 | |
| 861 | list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) { |
| 862 | ckch_inst_free(inst); |
| 863 | } |
| 864 | ebmb_delete(&store->node); |
| 865 | free(store); |
| 866 | } |
| 867 | |
| 868 | /* |
| 869 | * create and initialize a ckch_store |
| 870 | * <path> is the key name |
| 871 | * <nmemb> is the number of store->ckch objects to allocate |
| 872 | * |
| 873 | * Return a ckch_store or NULL upon failure. |
| 874 | */ |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 875 | struct ckch_store *ckch_store_new(const char *filename) |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 876 | { |
| 877 | struct ckch_store *store; |
| 878 | int pathlen; |
| 879 | |
| 880 | pathlen = strlen(filename); |
| 881 | store = calloc(1, sizeof(*store) + pathlen + 1); |
| 882 | if (!store) |
| 883 | return NULL; |
| 884 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 885 | memcpy(store->path, filename, pathlen + 1); |
| 886 | |
| 887 | LIST_INIT(&store->ckch_inst); |
| 888 | LIST_INIT(&store->crtlist_entry); |
| 889 | |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 890 | store->ckch = calloc(1, sizeof(*store->ckch)); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 891 | if (!store->ckch) |
| 892 | goto error; |
| 893 | |
| 894 | return store; |
| 895 | error: |
| 896 | ckch_store_free(store); |
| 897 | return NULL; |
| 898 | } |
| 899 | |
| 900 | /* allocate and duplicate a ckch_store |
| 901 | * Return a new ckch_store or NULL */ |
| 902 | struct ckch_store *ckchs_dup(const struct ckch_store *src) |
| 903 | { |
| 904 | struct ckch_store *dst; |
| 905 | |
William Lallemand | 6c09614 | 2021-02-23 14:45:45 +0100 | [diff] [blame] | 906 | if (!src) |
| 907 | return NULL; |
| 908 | |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 909 | dst = ckch_store_new(src->path); |
Eric Salama | 6ac61e3 | 2021-02-23 16:50:57 +0100 | [diff] [blame] | 910 | if (!dst) |
| 911 | return NULL; |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 912 | |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 913 | if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch)) |
| 914 | goto error; |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 915 | |
| 916 | return dst; |
| 917 | |
| 918 | error: |
| 919 | ckch_store_free(dst); |
| 920 | |
| 921 | return NULL; |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * lookup a path into the ckchs tree. |
| 926 | */ |
| 927 | struct ckch_store *ckchs_lookup(char *path) |
| 928 | { |
| 929 | struct ebmb_node *eb; |
| 930 | |
| 931 | eb = ebst_lookup(&ckchs_tree, path); |
| 932 | if (!eb) |
| 933 | return NULL; |
| 934 | |
| 935 | return ebmb_entry(eb, struct ckch_store, node); |
| 936 | } |
| 937 | |
| 938 | /* |
| 939 | * This function allocate a ckch_store and populate it with certificates from files. |
| 940 | */ |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 941 | struct ckch_store *ckchs_load_cert_file(char *path, char **err) |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 942 | { |
| 943 | struct ckch_store *ckchs; |
| 944 | |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 945 | ckchs = ckch_store_new(path); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 946 | if (!ckchs) { |
| 947 | memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : ""); |
| 948 | goto end; |
| 949 | } |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 950 | |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 951 | if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1) |
| 952 | goto end; |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 953 | |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 954 | /* insert into the ckchs tree */ |
| 955 | memcpy(ckchs->path, path, strlen(path) + 1); |
| 956 | ebst_insert(&ckchs_tree, &ckchs->node); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 957 | return ckchs; |
| 958 | |
| 959 | end: |
| 960 | ckch_store_free(ckchs); |
| 961 | |
| 962 | return NULL; |
| 963 | } |
| 964 | |
William Lallemand | fa1d8b4 | 2020-05-13 15:46:10 +0200 | [diff] [blame] | 965 | |
| 966 | /******************** ckch_inst functions ******************************/ |
| 967 | |
| 968 | /* unlink a ckch_inst, free all SNIs, free the ckch_inst */ |
| 969 | /* The caller must use the lock of the bind_conf if used with inserted SNIs */ |
| 970 | void ckch_inst_free(struct ckch_inst *inst) |
| 971 | { |
| 972 | struct sni_ctx *sni, *sni_s; |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 973 | struct ckch_inst_link_ref *link_ref, *link_ref_s; |
William Lallemand | fa1d8b4 | 2020-05-13 15:46:10 +0200 | [diff] [blame] | 974 | |
| 975 | if (inst == NULL) |
| 976 | return; |
| 977 | |
| 978 | list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) { |
| 979 | SSL_CTX_free(sni->ctx); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 980 | LIST_DELETE(&sni->by_ckch_inst); |
William Lallemand | fa1d8b4 | 2020-05-13 15:46:10 +0200 | [diff] [blame] | 981 | ebmb_delete(&sni->name); |
| 982 | free(sni); |
| 983 | } |
Remi Tricot-Le Breton | f3eedfe | 2021-01-25 17:19:44 +0100 | [diff] [blame] | 984 | SSL_CTX_free(inst->ctx); |
| 985 | inst->ctx = NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 986 | LIST_DELETE(&inst->by_ckchs); |
| 987 | LIST_DELETE(&inst->by_crtlist_entry); |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 988 | |
| 989 | list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) { |
| 990 | LIST_DELETE(&link_ref->link->list); |
| 991 | LIST_DELETE(&link_ref->list); |
| 992 | free(link_ref); |
| 993 | } |
| 994 | |
William Lallemand | fa1d8b4 | 2020-05-13 15:46:10 +0200 | [diff] [blame] | 995 | free(inst); |
| 996 | } |
| 997 | |
| 998 | /* Alloc and init a ckch_inst */ |
| 999 | struct ckch_inst *ckch_inst_new() |
| 1000 | { |
| 1001 | struct ckch_inst *ckch_inst; |
| 1002 | |
| 1003 | ckch_inst = calloc(1, sizeof *ckch_inst); |
| 1004 | if (!ckch_inst) |
| 1005 | return NULL; |
| 1006 | |
| 1007 | LIST_INIT(&ckch_inst->sni_ctx); |
| 1008 | LIST_INIT(&ckch_inst->by_ckchs); |
| 1009 | LIST_INIT(&ckch_inst->by_crtlist_entry); |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 1010 | LIST_INIT(&ckch_inst->cafile_link_refs); |
William Lallemand | fa1d8b4 | 2020-05-13 15:46:10 +0200 | [diff] [blame] | 1011 | |
| 1012 | return ckch_inst; |
| 1013 | } |
| 1014 | |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1015 | |
| 1016 | /******************** ssl_store functions ******************************/ |
Remi Tricot-Le Breton | 9f0c936 | 2021-02-19 15:06:28 +0100 | [diff] [blame] | 1017 | struct eb_root cafile_tree = EB_ROOT; |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1018 | |
Remi Tricot-Le Breton | 9f0c936 | 2021-02-19 15:06:28 +0100 | [diff] [blame] | 1019 | /* |
| 1020 | * Returns the cafile_entry found in the cafile_tree indexed by the path 'path'. |
| 1021 | * If 'oldest_entry' is 1, returns the "original" cafile_entry (since |
| 1022 | * during a set cafile/commit cafile cycle there might be two entries for any |
| 1023 | * given path, the original one and the new one set via the CLI but not |
| 1024 | * committed yet). |
| 1025 | */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 1026 | struct cafile_entry *ssl_store_get_cafile_entry(char *path, int oldest_entry) |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1027 | { |
Remi Tricot-Le Breton | 9f0c936 | 2021-02-19 15:06:28 +0100 | [diff] [blame] | 1028 | struct cafile_entry *ca_e = NULL; |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1029 | struct ebmb_node *eb; |
| 1030 | |
| 1031 | eb = ebst_lookup(&cafile_tree, path); |
Remi Tricot-Le Breton | 9f0c936 | 2021-02-19 15:06:28 +0100 | [diff] [blame] | 1032 | while (eb) { |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1033 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
Remi Tricot-Le Breton | 9f0c936 | 2021-02-19 15:06:28 +0100 | [diff] [blame] | 1034 | /* The ebst_lookup in a tree that has duplicates returns the |
| 1035 | * oldest entry first. If we want the latest entry, we need to |
| 1036 | * iterate over all the duplicates until we find the last one |
| 1037 | * (in our case there should never be more than two entries for |
| 1038 | * any given path). */ |
| 1039 | if (oldest_entry) |
| 1040 | return ca_e; |
| 1041 | eb = ebmb_next_dup(eb); |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1042 | } |
Remi Tricot-Le Breton | 9f0c936 | 2021-02-19 15:06:28 +0100 | [diff] [blame] | 1043 | return ca_e; |
| 1044 | } |
| 1045 | |
Remi Tricot-Le Breton | 38c999b | 2021-02-23 16:28:43 +0100 | [diff] [blame] | 1046 | int ssl_store_add_uncommitted_cafile_entry(struct cafile_entry *entry) |
| 1047 | { |
| 1048 | return (ebst_insert(&cafile_tree, &entry->node) != &entry->node); |
| 1049 | } |
| 1050 | |
Remi Tricot-Le Breton | 9f0c936 | 2021-02-19 15:06:28 +0100 | [diff] [blame] | 1051 | X509_STORE* ssl_store_get0_locations_file(char *path) |
| 1052 | { |
| 1053 | struct cafile_entry *ca_e = ssl_store_get_cafile_entry(path, 0); |
| 1054 | |
| 1055 | if (ca_e) |
| 1056 | return ca_e->ca_store; |
| 1057 | |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1058 | return NULL; |
| 1059 | } |
| 1060 | |
Remi Tricot-Le Breton | 5daff3c | 2021-02-22 15:54:55 +0100 | [diff] [blame] | 1061 | /* Create a cafile_entry object, without adding it to the cafile_tree. */ |
Remi Tricot-Le Breton | 0bb4824 | 2021-04-16 17:59:23 +0200 | [diff] [blame] | 1062 | struct cafile_entry *ssl_store_create_cafile_entry(char *path, X509_STORE *store, enum cafile_type type) |
Remi Tricot-Le Breton | 5daff3c | 2021-02-22 15:54:55 +0100 | [diff] [blame] | 1063 | { |
| 1064 | struct cafile_entry *ca_e; |
| 1065 | int pathlen; |
| 1066 | |
| 1067 | pathlen = strlen(path); |
| 1068 | |
| 1069 | ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1); |
| 1070 | if (ca_e) { |
| 1071 | memcpy(ca_e->path, path, pathlen + 1); |
| 1072 | ca_e->ca_store = store; |
Remi Tricot-Le Breton | 0bb4824 | 2021-04-16 17:59:23 +0200 | [diff] [blame] | 1073 | ca_e->type = type; |
Remi Tricot-Le Breton | 5daff3c | 2021-02-22 15:54:55 +0100 | [diff] [blame] | 1074 | LIST_INIT(&ca_e->ckch_inst_link); |
| 1075 | } |
| 1076 | return ca_e; |
| 1077 | } |
| 1078 | |
| 1079 | /* Delete a cafile_entry. The caller is responsible from removing this entry |
| 1080 | * from the cafile_tree first if is was previously added into it. */ |
| 1081 | void ssl_store_delete_cafile_entry(struct cafile_entry *ca_e) |
| 1082 | { |
| 1083 | struct ckch_inst_link *link, *link_s; |
| 1084 | if (!ca_e) |
| 1085 | return; |
| 1086 | |
| 1087 | X509_STORE_free(ca_e->ca_store); |
| 1088 | |
| 1089 | list_for_each_entry_safe(link, link_s, &ca_e->ckch_inst_link, list) { |
| 1090 | struct ckch_inst *inst = link->ckch_inst; |
| 1091 | struct ckch_inst_link_ref *link_ref, *link_ref_s; |
| 1092 | list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) { |
| 1093 | if (link_ref->link == link) { |
| 1094 | LIST_DELETE(&link_ref->list); |
| 1095 | free(link_ref); |
| 1096 | break; |
| 1097 | } |
| 1098 | } |
| 1099 | LIST_DELETE(&link->list); |
| 1100 | free(link); |
| 1101 | } |
| 1102 | |
| 1103 | free(ca_e); |
| 1104 | } |
| 1105 | |
Remi Tricot-Le Breton | 383fb14 | 2021-02-22 18:26:14 +0100 | [diff] [blame] | 1106 | /* |
| 1107 | * Build a cafile_entry out of a buffer instead of out of a file. |
| 1108 | * This function is used when the "commit ssl ca-file" cli command is used. |
| 1109 | * It can parse CERTIFICATE sections as well as CRL ones. |
| 1110 | * Returns 0 in case of success, 1 otherwise. |
| 1111 | */ |
| 1112 | int ssl_store_load_ca_from_buf(struct cafile_entry *ca_e, char *cert_buf) |
| 1113 | { |
| 1114 | int retval = 0; |
| 1115 | |
| 1116 | if (!ca_e) |
| 1117 | return 1; |
| 1118 | |
| 1119 | if (!ca_e->ca_store) { |
| 1120 | ca_e->ca_store = X509_STORE_new(); |
| 1121 | if (ca_e->ca_store) { |
| 1122 | BIO *bio = BIO_new_mem_buf(cert_buf, strlen(cert_buf)); |
| 1123 | if (bio) { |
| 1124 | X509_INFO *info; |
| 1125 | int i; |
| 1126 | STACK_OF(X509_INFO) *infos = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL); |
| 1127 | if (!infos) |
| 1128 | { |
| 1129 | BIO_free(bio); |
| 1130 | return 1; |
| 1131 | } |
| 1132 | |
| 1133 | for (i = 0; i < sk_X509_INFO_num(infos) && !retval; i++) { |
| 1134 | info = sk_X509_INFO_value(infos, i); |
| 1135 | /* X509_STORE_add_cert and X509_STORE_add_crl return 1 on success */ |
| 1136 | if (info->x509) { |
| 1137 | retval = !X509_STORE_add_cert(ca_e->ca_store, info->x509); |
| 1138 | } |
| 1139 | if (!retval && info->crl) { |
| 1140 | retval = !X509_STORE_add_crl(ca_e->ca_store, info->crl); |
| 1141 | } |
| 1142 | } |
| 1143 | retval = retval || (i != sk_X509_INFO_num(infos)); |
| 1144 | |
| 1145 | /* Cleanup */ |
| 1146 | sk_X509_INFO_pop_free(infos, X509_INFO_free); |
| 1147 | BIO_free(bio); |
| 1148 | } |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | return retval; |
| 1153 | } |
| 1154 | |
William Lallemand | 0f17ab2 | 2022-07-19 18:03:16 +0200 | [diff] [blame^] | 1155 | /* |
| 1156 | * Try to load a ca-file from disk into the ca-file cache. |
| 1157 | * |
| 1158 | * Return 0 upon error |
| 1159 | */ |
Remi Tricot-Le Breton | 0bb4824 | 2021-04-16 17:59:23 +0200 | [diff] [blame] | 1160 | int ssl_store_load_locations_file(char *path, int create_if_none, enum cafile_type type) |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1161 | { |
| 1162 | X509_STORE *store = ssl_store_get0_locations_file(path); |
| 1163 | |
| 1164 | /* If this function is called by the CLI, we should not call the |
| 1165 | * X509_STORE_load_locations function because it performs forbidden disk |
| 1166 | * accesses. */ |
| 1167 | if (!store && create_if_none) { |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1168 | STACK_OF(X509_OBJECT) *objs; |
| 1169 | int cert_count = 0; |
| 1170 | struct stat buf; |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1171 | struct cafile_entry *ca_e; |
William Lallemand | c6b1763 | 2022-04-01 23:39:37 +0200 | [diff] [blame] | 1172 | const char *file = NULL; |
| 1173 | const char *dir = NULL; |
William Lallemand | 0f17ab2 | 2022-07-19 18:03:16 +0200 | [diff] [blame^] | 1174 | unsigned long e; |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1175 | |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1176 | store = X509_STORE_new(); |
William Lallemand | 0f17ab2 | 2022-07-19 18:03:16 +0200 | [diff] [blame^] | 1177 | if (!store) { |
| 1178 | ha_alert("Cannot allocate memory!"); |
| 1179 | goto err; |
| 1180 | } |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1181 | |
William Lallemand | c6b1763 | 2022-04-01 23:39:37 +0200 | [diff] [blame] | 1182 | if (strcmp(path, "@system-ca") == 0) { |
| 1183 | dir = X509_get_default_cert_dir(); |
William Lallemand | 0f17ab2 | 2022-07-19 18:03:16 +0200 | [diff] [blame^] | 1184 | if (!dir) { |
| 1185 | ha_alert("Couldn't get the system CA directory from X509_get_default_cert_dir()."); |
| 1186 | goto err; |
| 1187 | } |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1188 | |
William Lallemand | c6b1763 | 2022-04-01 23:39:37 +0200 | [diff] [blame] | 1189 | } else { |
| 1190 | |
William Lallemand | 0f17ab2 | 2022-07-19 18:03:16 +0200 | [diff] [blame^] | 1191 | if (stat(path, &buf) == -1) { |
| 1192 | ha_alert("Couldn't open the ca-file '%s' (%s).", path, strerror(errno)); |
William Lallemand | c6b1763 | 2022-04-01 23:39:37 +0200 | [diff] [blame] | 1193 | goto err; |
William Lallemand | 0f17ab2 | 2022-07-19 18:03:16 +0200 | [diff] [blame^] | 1194 | } |
William Lallemand | c6b1763 | 2022-04-01 23:39:37 +0200 | [diff] [blame] | 1195 | |
| 1196 | if (S_ISDIR(buf.st_mode)) |
| 1197 | dir = path; |
| 1198 | else |
| 1199 | file = path; |
| 1200 | } |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1201 | |
| 1202 | if (file) { |
| 1203 | if (!X509_STORE_load_locations(store, file, NULL)) { |
William Lallemand | 0f17ab2 | 2022-07-19 18:03:16 +0200 | [diff] [blame^] | 1204 | e = ERR_get_error(); |
| 1205 | ha_alert("Couldn't open the ca-file '%s' (%s).", path, ERR_reason_error_string(e)); |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1206 | goto err; |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1207 | } |
William Lallemand | 80296b4 | 2022-04-05 10:19:30 +0200 | [diff] [blame] | 1208 | } else if (dir) { |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1209 | int n, i; |
| 1210 | struct dirent **de_list; |
| 1211 | |
| 1212 | n = scandir(dir, &de_list, 0, alphasort); |
| 1213 | if (n < 0) |
| 1214 | goto err; |
| 1215 | |
| 1216 | for (i= 0; i < n; i++) { |
| 1217 | char *end; |
| 1218 | struct dirent *de = de_list[i]; |
| 1219 | BIO *in = NULL; |
| 1220 | X509 *ca = NULL;; |
| 1221 | |
William Lallemand | 4348232 | 2022-07-18 18:42:52 +0200 | [diff] [blame] | 1222 | ERR_clear_error(); |
| 1223 | |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1224 | /* we try to load the files that would have |
| 1225 | * been loaded in an hashed directory loaded by |
| 1226 | * X509_LOOKUP_hash_dir, so according to "man 1 |
| 1227 | * c_rehash", we should load ".pem", ".crt", |
William Lallemand | e4b93eb | 2022-05-09 09:29:00 +0200 | [diff] [blame] | 1228 | * ".cer", or ".crl". Files starting with a dot |
| 1229 | * are ignored. |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1230 | */ |
| 1231 | end = strrchr(de->d_name, '.'); |
William Lallemand | e4b93eb | 2022-05-09 09:29:00 +0200 | [diff] [blame] | 1232 | if (!end || de->d_name[0] == '.' || |
| 1233 | (strcmp(end, ".pem") != 0 && |
| 1234 | strcmp(end, ".crt") != 0 && |
| 1235 | strcmp(end, ".cer") != 0 && |
| 1236 | strcmp(end, ".crl") != 0)) { |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1237 | free(de); |
| 1238 | continue; |
| 1239 | } |
| 1240 | in = BIO_new(BIO_s_file()); |
| 1241 | if (in == NULL) |
| 1242 | goto scandir_err; |
| 1243 | |
William Lallemand | c6b1763 | 2022-04-01 23:39:37 +0200 | [diff] [blame] | 1244 | chunk_printf(&trash, "%s/%s", dir, de->d_name); |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1245 | |
| 1246 | if (BIO_read_filename(in, trash.area) == 0) |
| 1247 | goto scandir_err; |
| 1248 | |
| 1249 | if (PEM_read_bio_X509_AUX(in, &ca, NULL, NULL) == NULL) |
| 1250 | goto scandir_err; |
| 1251 | |
William Lallemand | 4348232 | 2022-07-18 18:42:52 +0200 | [diff] [blame] | 1252 | if (X509_STORE_add_cert(store, ca) == 0) { |
| 1253 | /* only exits on error if the error is not about duplicate certificates */ |
| 1254 | if (!(ERR_GET_REASON(ERR_get_error()) == X509_R_CERT_ALREADY_IN_HASH_TABLE)) { |
| 1255 | goto scandir_err; |
| 1256 | } |
| 1257 | } |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1258 | |
William Lallemand | 4cfbf3c | 2022-04-26 15:57:33 +0200 | [diff] [blame] | 1259 | X509_free(ca); |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1260 | BIO_free(in); |
| 1261 | free(de); |
| 1262 | continue; |
| 1263 | |
| 1264 | scandir_err: |
William Lallemand | 0f17ab2 | 2022-07-19 18:03:16 +0200 | [diff] [blame^] | 1265 | e = ERR_get_error(); |
William Lallemand | 4cfbf3c | 2022-04-26 15:57:33 +0200 | [diff] [blame] | 1266 | X509_free(ca); |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1267 | BIO_free(in); |
| 1268 | free(de); |
William Lallemand | 0f17ab2 | 2022-07-19 18:03:16 +0200 | [diff] [blame^] | 1269 | /* warn if it can load one of the files, but don't abort */ |
| 1270 | ha_warning("ca-file: '%s' couldn't load '%s' (%s)\n", path, trash.area, ERR_reason_error_string(e)); |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1271 | |
| 1272 | } |
| 1273 | free(de_list); |
William Lallemand | 80296b4 | 2022-04-05 10:19:30 +0200 | [diff] [blame] | 1274 | } else { |
| 1275 | ha_alert("ca-file: couldn't load '%s'\n", path); |
| 1276 | goto err; |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1277 | } |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1278 | |
| 1279 | objs = X509_STORE_get0_objects(store); |
| 1280 | cert_count = sk_X509_OBJECT_num(objs); |
William Lallemand | 0f17ab2 | 2022-07-19 18:03:16 +0200 | [diff] [blame^] | 1281 | if (cert_count == 0) { |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1282 | ha_warning("ca-file: 0 CA were loaded from '%s'\n", path); |
William Lallemand | 0f17ab2 | 2022-07-19 18:03:16 +0200 | [diff] [blame^] | 1283 | } |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1284 | ca_e = ssl_store_create_cafile_entry(path, store, type); |
William Lallemand | 0f17ab2 | 2022-07-19 18:03:16 +0200 | [diff] [blame^] | 1285 | if (!ca_e) { |
| 1286 | ha_alert("Cannot allocate memory!\n"); |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1287 | goto err; |
William Lallemand | 0f17ab2 | 2022-07-19 18:03:16 +0200 | [diff] [blame^] | 1288 | } |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1289 | ebst_insert(&cafile_tree, &ca_e->node); |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1290 | } |
| 1291 | return (store != NULL); |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1292 | |
| 1293 | err: |
| 1294 | X509_STORE_free(store); |
| 1295 | store = NULL; |
| 1296 | return 0; |
| 1297 | |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1301 | /*************************** CLI commands ***********************/ |
| 1302 | |
| 1303 | /* Type of SSL payloads that can be updated over the CLI */ |
| 1304 | |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 1305 | struct cert_exts cert_exts[] = { |
| 1306 | { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */ |
William Lallemand | 26654e7 | 2022-03-30 12:01:32 +0200 | [diff] [blame] | 1307 | { "crt", CERT_TYPE_CRT, &ssl_sock_load_pem_into_ckch }, |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 1308 | { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch }, |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1309 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 1310 | { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file }, |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1311 | #endif |
Ilya Shipitsin | c47d676 | 2021-02-13 11:45:33 +0500 | [diff] [blame] | 1312 | #ifdef HAVE_SSL_SCTL |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 1313 | { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file }, |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1314 | #endif |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 1315 | { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch }, |
| 1316 | { NULL, CERT_TYPE_MAX, NULL }, |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1317 | }; |
| 1318 | |
| 1319 | |
| 1320 | /* release function of the `show ssl cert' command */ |
| 1321 | static void cli_release_show_cert(struct appctx *appctx) |
| 1322 | { |
| 1323 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1324 | } |
| 1325 | |
Willy Tarreau | 4fd9b4d | 2022-05-04 16:11:50 +0200 | [diff] [blame] | 1326 | /* IO handler of "show ssl cert <filename>". |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1327 | * It makes use of a show_cert_ctx context, and ckchs_transaction in read-only. |
Willy Tarreau | 4fd9b4d | 2022-05-04 16:11:50 +0200 | [diff] [blame] | 1328 | */ |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1329 | static int cli_io_handler_show_cert(struct appctx *appctx) |
| 1330 | { |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1331 | struct show_cert_ctx *ctx = appctx->svcctx; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1332 | struct buffer *trash = alloc_trash_chunk(); |
| 1333 | struct ebmb_node *node; |
Christopher Faulet | d1d2e4d | 2022-06-03 16:24:02 +0200 | [diff] [blame] | 1334 | struct ckch_store *ckchs = NULL; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1335 | |
| 1336 | if (trash == NULL) |
| 1337 | return 1; |
| 1338 | |
Christopher Faulet | 3e94f5d | 2022-06-03 10:46:40 +0200 | [diff] [blame] | 1339 | if (!ctx->old_ckchs && ckchs_transaction.old_ckchs) { |
| 1340 | ckchs = ckchs_transaction.old_ckchs; |
| 1341 | chunk_appendf(trash, "# transaction\n"); |
| 1342 | chunk_appendf(trash, "*%s\n", ckchs->path); |
| 1343 | if (applet_putchk(appctx, trash) == -1) |
| 1344 | goto yield; |
| 1345 | ctx->old_ckchs = ckchs_transaction.old_ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1346 | } |
| 1347 | |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1348 | if (!ctx->cur_ckchs) { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1349 | chunk_appendf(trash, "# filename\n"); |
| 1350 | node = ebmb_first(&ckchs_tree); |
| 1351 | } else { |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1352 | node = &ctx->cur_ckchs->node; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1353 | } |
| 1354 | while (node) { |
| 1355 | ckchs = ebmb_entry(node, struct ckch_store, node); |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1356 | chunk_appendf(trash, "%s\n", ckchs->path); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1357 | |
| 1358 | node = ebmb_next(node); |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 1359 | if (applet_putchk(appctx, trash) == -1) |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1360 | goto yield; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1361 | } |
| 1362 | |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1363 | ctx->cur_ckchs = NULL; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1364 | free_trash_chunk(trash); |
| 1365 | return 1; |
| 1366 | yield: |
| 1367 | |
| 1368 | free_trash_chunk(trash); |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1369 | ctx->cur_ckchs = ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1370 | return 0; /* should come back */ |
| 1371 | } |
| 1372 | |
| 1373 | /* |
| 1374 | * Extract and format the DNS SAN extensions and copy result into a chuink |
| 1375 | * Return 0; |
| 1376 | */ |
| 1377 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1378 | static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out) |
| 1379 | { |
| 1380 | int i; |
| 1381 | char *str; |
| 1382 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 1383 | |
| 1384 | names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 1385 | if (names) { |
| 1386 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 1387 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 1388 | if (i > 0) |
| 1389 | chunk_appendf(out, ", "); |
| 1390 | if (name->type == GEN_DNS) { |
| 1391 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 1392 | chunk_appendf(out, "DNS:%s", str); |
| 1393 | OPENSSL_free(str); |
| 1394 | } |
| 1395 | } |
| 1396 | } |
| 1397 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 1398 | } |
| 1399 | return 0; |
| 1400 | } |
| 1401 | #endif |
| 1402 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 1403 | /* |
| 1404 | * Build the ckch_inst_link that will be chained in the CA file entry and the |
| 1405 | * corresponding ckch_inst_link_ref that will be chained in the ckch instance. |
| 1406 | * Return 0 in case of success. |
| 1407 | */ |
| 1408 | static int do_chain_inst_and_cafile(struct cafile_entry *cafile_entry, struct ckch_inst *ckch_inst) |
| 1409 | { |
| 1410 | struct ckch_inst_link *new_link; |
| 1411 | if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) { |
| 1412 | struct ckch_inst_link *link = LIST_ELEM(cafile_entry->ckch_inst_link.n, |
| 1413 | typeof(link), list); |
| 1414 | /* Do not add multiple references to the same |
| 1415 | * instance in a cafile_entry */ |
| 1416 | if (link->ckch_inst == ckch_inst) { |
| 1417 | return 1; |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | new_link = calloc(1, sizeof(*new_link)); |
| 1422 | if (new_link) { |
| 1423 | struct ckch_inst_link_ref *new_link_ref = calloc(1, sizeof(*new_link_ref)); |
| 1424 | if (!new_link_ref) { |
| 1425 | free(new_link); |
| 1426 | return 1; |
| 1427 | } |
| 1428 | |
| 1429 | new_link->ckch_inst = ckch_inst; |
| 1430 | new_link_ref->link = new_link; |
| 1431 | LIST_INIT(&new_link->list); |
| 1432 | LIST_INIT(&new_link_ref->list); |
| 1433 | |
| 1434 | LIST_APPEND(&cafile_entry->ckch_inst_link, &new_link->list); |
| 1435 | LIST_APPEND(&ckch_inst->cafile_link_refs, &new_link_ref->list); |
| 1436 | } |
| 1437 | |
| 1438 | return 0; |
| 1439 | } |
| 1440 | |
| 1441 | |
| 1442 | /* |
| 1443 | * Link a CA file tree entry to the ckch instance that uses it. |
| 1444 | * To determine if and which CA file tree entries need to be linked to the |
| 1445 | * instance, we follow the same logic performed in ssl_sock_prepare_ctx when |
| 1446 | * processing the verify option. |
| 1447 | * This function works for a frontend as well as for a backend, depending on the |
| 1448 | * configuration parameters given (bind_conf or server). |
| 1449 | */ |
| 1450 | void ckch_inst_add_cafile_link(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf, |
| 1451 | struct ssl_bind_conf *ssl_conf, const struct server *srv) |
| 1452 | { |
| 1453 | int verify = SSL_VERIFY_NONE; |
| 1454 | |
| 1455 | if (srv) { |
| 1456 | |
| 1457 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 1458 | verify = SSL_VERIFY_PEER; |
| 1459 | switch (srv->ssl_ctx.verify) { |
| 1460 | case SSL_SOCK_VERIFY_NONE: |
| 1461 | verify = SSL_VERIFY_NONE; |
| 1462 | break; |
| 1463 | case SSL_SOCK_VERIFY_REQUIRED: |
| 1464 | verify = SSL_VERIFY_PEER; |
| 1465 | break; |
| 1466 | } |
| 1467 | } |
| 1468 | else { |
| 1469 | switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) { |
| 1470 | case SSL_SOCK_VERIFY_NONE: |
| 1471 | verify = SSL_VERIFY_NONE; |
| 1472 | break; |
| 1473 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 1474 | verify = SSL_VERIFY_PEER; |
| 1475 | break; |
| 1476 | case SSL_SOCK_VERIFY_REQUIRED: |
| 1477 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 1478 | break; |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | if (verify & SSL_VERIFY_PEER) { |
| 1483 | struct cafile_entry *ca_file_entry = NULL; |
| 1484 | struct cafile_entry *ca_verify_file_entry = NULL; |
Remi Tricot-Le Breton | f81c70c | 2021-04-20 16:54:21 +0200 | [diff] [blame] | 1485 | struct cafile_entry *crl_file_entry = NULL; |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 1486 | if (srv) { |
| 1487 | if (srv->ssl_ctx.ca_file) { |
| 1488 | ca_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.ca_file, 0); |
| 1489 | |
| 1490 | } |
Remi Tricot-Le Breton | f81c70c | 2021-04-20 16:54:21 +0200 | [diff] [blame] | 1491 | if (srv->ssl_ctx.crl_file) { |
| 1492 | crl_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.crl_file, 0); |
| 1493 | } |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 1494 | } |
| 1495 | else { |
| 1496 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 1497 | 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 Breton | f81c70c | 2021-04-20 16:54:21 +0200 | [diff] [blame] | 1498 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 1499 | |
| 1500 | if (ca_file) |
| 1501 | ca_file_entry = ssl_store_get_cafile_entry(ca_file, 0); |
| 1502 | if (ca_verify_file) |
| 1503 | ca_verify_file_entry = ssl_store_get_cafile_entry(ca_verify_file, 0); |
Remi Tricot-Le Breton | f81c70c | 2021-04-20 16:54:21 +0200 | [diff] [blame] | 1504 | if (crl_file) |
| 1505 | crl_file_entry = ssl_store_get_cafile_entry(crl_file, 0); |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | if (ca_file_entry) { |
| 1509 | /* If we have a ckch instance that is not already in the |
| 1510 | * cafile_entry's list, add it to it. */ |
| 1511 | if (do_chain_inst_and_cafile(ca_file_entry, ckch_inst)) |
| 1512 | return; |
| 1513 | |
| 1514 | } |
| 1515 | if (ca_verify_file_entry && (ca_file_entry != ca_verify_file_entry)) { |
| 1516 | /* If we have a ckch instance that is not already in the |
| 1517 | * cafile_entry's list, add it to it. */ |
| 1518 | if (do_chain_inst_and_cafile(ca_verify_file_entry, ckch_inst)) |
| 1519 | return; |
| 1520 | } |
Remi Tricot-Le Breton | f81c70c | 2021-04-20 16:54:21 +0200 | [diff] [blame] | 1521 | if (crl_file_entry) { |
| 1522 | /* If we have a ckch instance that is not already in the |
| 1523 | * cafile_entry's list, add it to it. */ |
| 1524 | if (do_chain_inst_and_cafile(crl_file_entry, ckch_inst)) |
| 1525 | return; |
| 1526 | } |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 1527 | } |
| 1528 | } |
| 1529 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1530 | |
| 1531 | |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1532 | static int show_cert_detail(X509 *cert, STACK_OF(X509) *chain, struct buffer *out) |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1533 | { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1534 | BIO *bio = NULL; |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1535 | struct buffer *tmp = alloc_trash_chunk(); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1536 | int i; |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1537 | int write = -1; |
| 1538 | unsigned int len = 0; |
| 1539 | X509_NAME *name = NULL; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1540 | |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1541 | if (!tmp) |
| 1542 | return -1; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1543 | |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1544 | if (!cert) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1545 | goto end; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1546 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1547 | if (chain == NULL) { |
| 1548 | struct issuer_chain *issuer; |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1549 | issuer = ssl_get0_issuer_chain(cert); |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1550 | if (issuer) { |
| 1551 | chain = issuer->chain; |
| 1552 | chunk_appendf(out, "Chain Filename: "); |
| 1553 | chunk_appendf(out, "%s\n", issuer->path); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1554 | } |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1555 | } |
| 1556 | chunk_appendf(out, "Serial: "); |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1557 | if (ssl_sock_get_serial(cert, tmp) == -1) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1558 | goto end; |
| 1559 | dump_binary(out, tmp->area, tmp->data); |
| 1560 | chunk_appendf(out, "\n"); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1561 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1562 | chunk_appendf(out, "notBefore: "); |
| 1563 | chunk_reset(tmp); |
| 1564 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 1565 | goto end; |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1566 | if (ASN1_TIME_print(bio, X509_getm_notBefore(cert)) == 0) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1567 | goto end; |
| 1568 | write = BIO_read(bio, tmp->area, tmp->size-1); |
| 1569 | tmp->area[write] = '\0'; |
| 1570 | BIO_free(bio); |
| 1571 | bio = NULL; |
| 1572 | chunk_appendf(out, "%s\n", tmp->area); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1573 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1574 | chunk_appendf(out, "notAfter: "); |
| 1575 | chunk_reset(tmp); |
| 1576 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 1577 | goto end; |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1578 | if (ASN1_TIME_print(bio, X509_getm_notAfter(cert)) == 0) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1579 | goto end; |
| 1580 | if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0) |
| 1581 | goto end; |
| 1582 | tmp->area[write] = '\0'; |
| 1583 | BIO_free(bio); |
| 1584 | bio = NULL; |
| 1585 | chunk_appendf(out, "%s\n", tmp->area); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1586 | |
| 1587 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1588 | chunk_appendf(out, "Subject Alternative Name: "); |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1589 | if (ssl_sock_get_san_oneline(cert, out) == -1) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1590 | goto end; |
| 1591 | *(out->area + out->data) = '\0'; |
| 1592 | chunk_appendf(out, "\n"); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1593 | #endif |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1594 | chunk_reset(tmp); |
| 1595 | chunk_appendf(out, "Algorithm: "); |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1596 | if (cert_get_pkey_algo(cert, tmp) == 0) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1597 | goto end; |
| 1598 | chunk_appendf(out, "%s\n", tmp->area); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1599 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1600 | chunk_reset(tmp); |
| 1601 | chunk_appendf(out, "SHA1 FingerPrint: "); |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1602 | if (X509_digest(cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1603 | goto end; |
| 1604 | tmp->data = len; |
| 1605 | dump_binary(out, tmp->area, tmp->data); |
| 1606 | chunk_appendf(out, "\n"); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1607 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1608 | chunk_appendf(out, "Subject: "); |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1609 | if ((name = X509_get_subject_name(cert)) == NULL) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1610 | goto end; |
| 1611 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 1612 | goto end; |
| 1613 | *(tmp->area + tmp->data) = '\0'; |
| 1614 | chunk_appendf(out, "%s\n", tmp->area); |
| 1615 | |
| 1616 | chunk_appendf(out, "Issuer: "); |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1617 | if ((name = X509_get_issuer_name(cert)) == NULL) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1618 | goto end; |
| 1619 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 1620 | goto end; |
| 1621 | *(tmp->area + tmp->data) = '\0'; |
| 1622 | chunk_appendf(out, "%s\n", tmp->area); |
| 1623 | |
| 1624 | /* Displays subject of each certificate in the chain */ |
| 1625 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1626 | X509 *ca = sk_X509_value(chain, i); |
| 1627 | |
| 1628 | chunk_appendf(out, "Chain Subject: "); |
| 1629 | if ((name = X509_get_subject_name(ca)) == NULL) |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1630 | goto end; |
| 1631 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 1632 | goto end; |
| 1633 | *(tmp->area + tmp->data) = '\0'; |
| 1634 | chunk_appendf(out, "%s\n", tmp->area); |
| 1635 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1636 | chunk_appendf(out, "Chain Issuer: "); |
| 1637 | if ((name = X509_get_issuer_name(ca)) == NULL) |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1638 | goto end; |
| 1639 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 1640 | goto end; |
| 1641 | *(tmp->area + tmp->data) = '\0'; |
| 1642 | chunk_appendf(out, "%s\n", tmp->area); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | end: |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1646 | if (bio) |
| 1647 | BIO_free(bio); |
| 1648 | free_trash_chunk(tmp); |
| 1649 | |
| 1650 | return 0; |
| 1651 | } |
| 1652 | |
Remi Tricot-Le Breton | 3faf0cb | 2021-06-10 18:10:32 +0200 | [diff] [blame] | 1653 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
Remi Tricot-Le Breton | da968f6 | 2021-06-10 13:51:14 +0200 | [diff] [blame] | 1654 | /* |
| 1655 | * Build the OCSP tree entry's key for a given ckch_store. |
| 1656 | * Returns a negative value in case of error. |
| 1657 | */ |
| 1658 | static int ckch_store_build_certid(struct ckch_store *ckch_store, unsigned char certid[128], unsigned int *key_length) |
| 1659 | { |
| 1660 | OCSP_RESPONSE *resp; |
| 1661 | OCSP_BASICRESP *bs = NULL; |
| 1662 | OCSP_SINGLERESP *sr; |
| 1663 | OCSP_CERTID *id; |
| 1664 | unsigned char *p = NULL; |
| 1665 | |
| 1666 | if (!key_length) |
| 1667 | return -1; |
| 1668 | |
| 1669 | *key_length = 0; |
| 1670 | |
| 1671 | if (!ckch_store->ckch->ocsp_response) |
| 1672 | return 0; |
| 1673 | |
| 1674 | p = (unsigned char *) ckch_store->ckch->ocsp_response->area; |
| 1675 | |
| 1676 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 1677 | ckch_store->ckch->ocsp_response->data); |
| 1678 | if (!resp) { |
| 1679 | goto end; |
| 1680 | } |
| 1681 | |
| 1682 | bs = OCSP_response_get1_basic(resp); |
| 1683 | if (!bs) { |
| 1684 | goto end; |
| 1685 | } |
| 1686 | |
| 1687 | sr = OCSP_resp_get0(bs, 0); |
| 1688 | if (!sr) { |
| 1689 | goto end; |
| 1690 | } |
| 1691 | |
| 1692 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 1693 | |
| 1694 | p = certid; |
| 1695 | *key_length = i2d_OCSP_CERTID(id, &p); |
| 1696 | |
| 1697 | end: |
| 1698 | return *key_length > 0; |
| 1699 | } |
| 1700 | #endif |
| 1701 | |
| 1702 | /* |
| 1703 | * Dump the OCSP certificate key (if it exists) of certificate <ckch> into |
| 1704 | * buffer <out>. |
| 1705 | * Returns 0 in case of success. |
| 1706 | */ |
| 1707 | static int ckch_store_show_ocsp_certid(struct ckch_store *ckch_store, struct buffer *out) |
| 1708 | { |
Remi Tricot-Le Breton | 3faf0cb | 2021-06-10 18:10:32 +0200 | [diff] [blame] | 1709 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
Remi Tricot-Le Breton | da968f6 | 2021-06-10 13:51:14 +0200 | [diff] [blame] | 1710 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {}; |
| 1711 | unsigned int key_length = 0; |
| 1712 | int i; |
| 1713 | |
| 1714 | if (ckch_store_build_certid(ckch_store, (unsigned char*)key, &key_length) >= 0) { |
| 1715 | /* Dump the CERTID info */ |
| 1716 | chunk_appendf(out, "OCSP Response Key: "); |
| 1717 | for (i = 0; i < key_length; ++i) { |
| 1718 | chunk_appendf(out, "%02x", key[i]); |
| 1719 | } |
| 1720 | chunk_appendf(out, "\n"); |
| 1721 | } |
| 1722 | #endif |
| 1723 | |
| 1724 | return 0; |
| 1725 | } |
| 1726 | |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1727 | |
Willy Tarreau | 4fd9b4d | 2022-05-04 16:11:50 +0200 | [diff] [blame] | 1728 | /* IO handler of the details "show ssl cert <filename>". |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1729 | * It uses a struct show_cert_ctx and ckchs_transaction in read-only. |
Willy Tarreau | 4fd9b4d | 2022-05-04 16:11:50 +0200 | [diff] [blame] | 1730 | */ |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1731 | static int cli_io_handler_show_cert_detail(struct appctx *appctx) |
| 1732 | { |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1733 | struct show_cert_ctx *ctx = appctx->svcctx; |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1734 | struct ckch_store *ckchs = ctx->cur_ckchs; |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1735 | struct buffer *out = alloc_trash_chunk(); |
| 1736 | int retval = 0; |
| 1737 | |
| 1738 | if (!out) |
| 1739 | goto end_no_putchk; |
| 1740 | |
| 1741 | chunk_appendf(out, "Filename: "); |
| 1742 | if (ckchs == ckchs_transaction.new_ckchs) |
| 1743 | chunk_appendf(out, "*"); |
| 1744 | chunk_appendf(out, "%s\n", ckchs->path); |
| 1745 | |
| 1746 | chunk_appendf(out, "Status: "); |
| 1747 | if (ckchs->ckch->cert == NULL) |
| 1748 | chunk_appendf(out, "Empty\n"); |
| 1749 | else if (LIST_ISEMPTY(&ckchs->ckch_inst)) |
| 1750 | chunk_appendf(out, "Unused\n"); |
| 1751 | else |
| 1752 | chunk_appendf(out, "Used\n"); |
| 1753 | |
| 1754 | retval = show_cert_detail(ckchs->ckch->cert, ckchs->ckch->chain, out); |
| 1755 | if (retval < 0) |
| 1756 | goto end_no_putchk; |
| 1757 | else if (retval) |
| 1758 | goto end; |
| 1759 | |
Remi Tricot-Le Breton | da968f6 | 2021-06-10 13:51:14 +0200 | [diff] [blame] | 1760 | ckch_store_show_ocsp_certid(ckchs, out); |
| 1761 | |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1762 | end: |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 1763 | if (applet_putchk(appctx, out) == -1) |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1764 | goto yield; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1765 | |
| 1766 | end_no_putchk: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1767 | free_trash_chunk(out); |
| 1768 | return 1; |
| 1769 | yield: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1770 | free_trash_chunk(out); |
| 1771 | return 0; /* should come back */ |
| 1772 | } |
| 1773 | |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1774 | |
Willy Tarreau | 4fd9b4d | 2022-05-04 16:11:50 +0200 | [diff] [blame] | 1775 | /* IO handler of the details "show ssl cert <filename.ocsp>". |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1776 | * It uses a show_cert_ctx. |
Willy Tarreau | 4fd9b4d | 2022-05-04 16:11:50 +0200 | [diff] [blame] | 1777 | */ |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1778 | static int cli_io_handler_show_cert_ocsp_detail(struct appctx *appctx) |
| 1779 | { |
Remi Tricot-Le Breton | 3faf0cb | 2021-06-10 18:10:32 +0200 | [diff] [blame] | 1780 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1781 | struct show_cert_ctx *ctx = appctx->svcctx; |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1782 | struct ckch_store *ckchs = ctx->cur_ckchs; |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1783 | struct buffer *out = alloc_trash_chunk(); |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1784 | int from_transaction = ctx->transaction; |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1785 | |
| 1786 | if (!out) |
| 1787 | goto end_no_putchk; |
| 1788 | |
| 1789 | /* If we try to display an ongoing transaction's OCSP response, we |
| 1790 | * need to dump the ckch's ocsp_response buffer directly. |
| 1791 | * Otherwise, we must rebuild the certificate's certid in order to |
| 1792 | * look for the current OCSP response in the tree. */ |
| 1793 | if (from_transaction && ckchs->ckch->ocsp_response) { |
Remi Tricot-Le Breton | a9a591a | 2022-02-16 14:42:22 +0100 | [diff] [blame] | 1794 | if (ssl_ocsp_response_print(ckchs->ckch->ocsp_response, out)) |
| 1795 | goto end_no_putchk; |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1796 | } |
| 1797 | else { |
| 1798 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {}; |
| 1799 | unsigned int key_length = 0; |
| 1800 | |
| 1801 | if (ckch_store_build_certid(ckchs, (unsigned char*)key, &key_length) < 0) |
| 1802 | goto end_no_putchk; |
| 1803 | |
Remi Tricot-Le Breton | a9a591a | 2022-02-16 14:42:22 +0100 | [diff] [blame] | 1804 | if (ssl_get_ocspresponse_detail(key, out)) |
| 1805 | goto end_no_putchk; |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1806 | } |
| 1807 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 1808 | if (applet_putchk(appctx, out) == -1) |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1809 | goto yield; |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1810 | |
| 1811 | end_no_putchk: |
| 1812 | free_trash_chunk(out); |
| 1813 | return 1; |
| 1814 | yield: |
| 1815 | free_trash_chunk(out); |
| 1816 | return 0; /* should come back */ |
| 1817 | #else |
| 1818 | return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n"); |
| 1819 | #endif |
| 1820 | } |
| 1821 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1822 | /* parsing function for 'show ssl cert [certfile]' */ |
| 1823 | static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 1824 | { |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1825 | struct show_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1826 | struct ckch_store *ckchs; |
| 1827 | |
| 1828 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 1829 | return cli_err(appctx, "Can't allocate memory!\n"); |
| 1830 | |
| 1831 | /* The operations on the CKCH architecture are locked so we can |
| 1832 | * manipulate ckch_store and ckch_inst */ |
| 1833 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 1834 | return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n"); |
| 1835 | |
| 1836 | /* check if there is a certificate to lookup */ |
| 1837 | if (*args[3]) { |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1838 | int show_ocsp_detail = 0; |
| 1839 | int from_transaction = 0; |
| 1840 | char *end; |
| 1841 | |
| 1842 | /* We manage the special case "certname.ocsp" through which we |
| 1843 | * can show the details of an OCSP response. */ |
| 1844 | end = strrchr(args[3], '.'); |
| 1845 | if (end && strcmp(end+1, "ocsp") == 0) { |
| 1846 | *end = '\0'; |
| 1847 | show_ocsp_detail = 1; |
| 1848 | } |
| 1849 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1850 | if (*args[3] == '*') { |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1851 | from_transaction = 1; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1852 | if (!ckchs_transaction.new_ckchs) |
| 1853 | goto error; |
| 1854 | |
| 1855 | ckchs = ckchs_transaction.new_ckchs; |
| 1856 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1857 | if (strcmp(args[3] + 1, ckchs->path) != 0) |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1858 | goto error; |
| 1859 | |
| 1860 | } else { |
| 1861 | if ((ckchs = ckchs_lookup(args[3])) == NULL) |
| 1862 | goto error; |
| 1863 | |
| 1864 | } |
| 1865 | |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1866 | ctx->cur_ckchs = ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1867 | /* use the IO handler that shows details */ |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1868 | if (show_ocsp_detail) { |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1869 | ctx->transaction = from_transaction; |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1870 | appctx->io_handler = cli_io_handler_show_cert_ocsp_detail; |
| 1871 | } |
| 1872 | else |
| 1873 | appctx->io_handler = cli_io_handler_show_cert_detail; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | return 0; |
| 1877 | |
| 1878 | error: |
| 1879 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1880 | return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n"); |
| 1881 | } |
| 1882 | |
| 1883 | /* release function of the `set ssl cert' command, free things and unlock the spinlock */ |
| 1884 | static void cli_release_commit_cert(struct appctx *appctx) |
| 1885 | { |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 1886 | struct commit_cert_ctx *ctx = appctx->svcctx; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1887 | |
| 1888 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 1889 | /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */ |
| 1890 | if (ctx->new_ckchs) |
| 1891 | ckch_store_free(ctx->new_ckchs); |
| 1892 | ha_free(&ctx->err); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1893 | } |
| 1894 | |
Remi Tricot-Le Breton | bfadc02 | 2021-02-24 12:20:48 +0100 | [diff] [blame] | 1895 | |
| 1896 | /* |
| 1897 | * Rebuild a new instance 'new_inst' based on an old instance 'ckchi' and a |
| 1898 | * specific ckch_store. |
| 1899 | * Returns 0 in case of success, 1 otherwise. |
| 1900 | */ |
William Lallemand | e60c7d6 | 2022-03-30 11:26:15 +0200 | [diff] [blame] | 1901 | int ckch_inst_rebuild(struct ckch_store *ckch_store, struct ckch_inst *ckchi, |
| 1902 | struct ckch_inst **new_inst, char **err) |
Remi Tricot-Le Breton | bfadc02 | 2021-02-24 12:20:48 +0100 | [diff] [blame] | 1903 | { |
| 1904 | int retval = 0; |
| 1905 | int errcode = 0; |
| 1906 | struct sni_ctx *sc0, *sc0s; |
| 1907 | char **sni_filter = NULL; |
| 1908 | int fcount = 0; |
| 1909 | |
| 1910 | if (ckchi->crtlist_entry) { |
| 1911 | sni_filter = ckchi->crtlist_entry->filters; |
| 1912 | fcount = ckchi->crtlist_entry->fcount; |
| 1913 | } |
| 1914 | |
| 1915 | if (ckchi->is_server_instance) |
| 1916 | errcode |= ckch_inst_new_load_srv_store(ckch_store->path, ckch_store, new_inst, err); |
| 1917 | else |
| 1918 | errcode |= ckch_inst_new_load_store(ckch_store->path, ckch_store, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, new_inst, err); |
| 1919 | |
| 1920 | if (errcode & ERR_CODE) |
| 1921 | return 1; |
| 1922 | |
| 1923 | /* if the previous ckchi was used as the default */ |
| 1924 | if (ckchi->is_default) |
| 1925 | (*new_inst)->is_default = 1; |
| 1926 | |
| 1927 | (*new_inst)->is_server_instance = ckchi->is_server_instance; |
| 1928 | (*new_inst)->server = ckchi->server; |
| 1929 | /* Create a new SSL_CTX and link it to the new instance. */ |
| 1930 | if ((*new_inst)->is_server_instance) { |
| 1931 | retval = ssl_sock_prep_srv_ctx_and_inst(ckchi->server, (*new_inst)->ctx, (*new_inst)); |
| 1932 | if (retval) |
| 1933 | return 1; |
| 1934 | } |
| 1935 | |
| 1936 | /* create the link to the crtlist_entry */ |
| 1937 | (*new_inst)->crtlist_entry = ckchi->crtlist_entry; |
| 1938 | |
| 1939 | /* we need to initialize the SSL_CTX generated */ |
| 1940 | /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */ |
| 1941 | list_for_each_entry_safe(sc0, sc0s, &(*new_inst)->sni_ctx, by_ckch_inst) { |
| 1942 | if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */ |
| 1943 | errcode |= ssl_sock_prep_ctx_and_inst(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, *new_inst, err); |
| 1944 | if (errcode & ERR_CODE) |
| 1945 | return 1; |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | return 0; |
| 1950 | } |
| 1951 | |
| 1952 | /* |
| 1953 | * Load all the new SNIs of a newly built ckch instance in the trees, or replace |
| 1954 | * a server's main ckch instance. |
| 1955 | */ |
| 1956 | static void __ssl_sock_load_new_ckch_instance(struct ckch_inst *ckchi) |
| 1957 | { |
| 1958 | /* The bind_conf will be null on server ckch_instances. */ |
| 1959 | if (ckchi->is_server_instance) { |
| 1960 | int i; |
| 1961 | /* a lock is needed here since we have to free the SSL cache */ |
| 1962 | HA_RWLOCK_WRLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock); |
| 1963 | /* free the server current SSL_CTX */ |
| 1964 | SSL_CTX_free(ckchi->server->ssl_ctx.ctx); |
| 1965 | /* Actual ssl context update */ |
| 1966 | SSL_CTX_up_ref(ckchi->ctx); |
| 1967 | ckchi->server->ssl_ctx.ctx = ckchi->ctx; |
| 1968 | ckchi->server->ssl_ctx.inst = ckchi; |
| 1969 | |
| 1970 | /* flush the session cache of the server */ |
| 1971 | for (i = 0; i < global.nbthread; i++) { |
William Lallemand | ce99033 | 2021-11-23 15:15:09 +0100 | [diff] [blame] | 1972 | ha_free(&ckchi->server->ssl_ctx.reused_sess[i].sni); |
Remi Tricot-Le Breton | bfadc02 | 2021-02-24 12:20:48 +0100 | [diff] [blame] | 1973 | ha_free(&ckchi->server->ssl_ctx.reused_sess[i].ptr); |
| 1974 | } |
| 1975 | HA_RWLOCK_WRUNLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock); |
| 1976 | |
| 1977 | } else { |
| 1978 | HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 1979 | ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf); |
| 1980 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | /* |
| 1985 | * Delete a ckch instance that was replaced after a CLI command. |
| 1986 | */ |
| 1987 | static void __ckch_inst_free_locked(struct ckch_inst *ckchi) |
| 1988 | { |
| 1989 | if (ckchi->is_server_instance) { |
| 1990 | /* no lock for servers */ |
| 1991 | ckch_inst_free(ckchi); |
| 1992 | } else { |
| 1993 | struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf; |
| 1994 | |
| 1995 | HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock); |
| 1996 | ckch_inst_free(ckchi); |
| 1997 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock); |
| 1998 | } |
| 1999 | } |
| 2000 | |
William Lallemand | 3b5a3a6 | 2022-03-29 14:29:31 +0200 | [diff] [blame] | 2001 | /* Replace a ckch_store in the ckch tree and insert the whole dependencies, |
| 2002 | * then free the previous dependencies and store. |
| 2003 | * Used in the case of a certificate update. |
| 2004 | * |
| 2005 | * Every dependencies must allocated before using this function. |
| 2006 | * |
| 2007 | * This function can't fail as it only update pointers, and does not alloc anything. |
| 2008 | * |
| 2009 | * /!\ This function must be used under the ckch lock. /!\ |
| 2010 | * |
| 2011 | * - Insert every dependencies (SNI, crtlist_entry, ckch_inst, etc) |
| 2012 | * - Delete the old ckch_store from the tree |
| 2013 | * - Insert the new ckch_store |
| 2014 | * - Free the old dependencies and the old ckch_store |
| 2015 | */ |
| 2016 | void ckch_store_replace(struct ckch_store *old_ckchs, struct ckch_store *new_ckchs) |
| 2017 | { |
| 2018 | struct crtlist_entry *entry; |
| 2019 | struct ckch_inst *ckchi, *ckchis; |
| 2020 | |
| 2021 | LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry); |
| 2022 | list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) { |
| 2023 | ebpt_delete(&entry->node); |
| 2024 | /* change the ptr and reinsert the node */ |
| 2025 | entry->node.key = new_ckchs; |
| 2026 | ebpt_insert(&entry->crtlist->entries, &entry->node); |
| 2027 | } |
| 2028 | /* insert the new ckch_insts in the crtlist_entry */ |
| 2029 | list_for_each_entry(ckchi, &new_ckchs->ckch_inst, by_ckchs) { |
| 2030 | if (ckchi->crtlist_entry) |
| 2031 | LIST_INSERT(&ckchi->crtlist_entry->ckch_inst, &ckchi->by_crtlist_entry); |
| 2032 | } |
| 2033 | /* First, we insert every new SNIs in the trees, also replace the default_ctx */ |
| 2034 | list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) { |
| 2035 | __ssl_sock_load_new_ckch_instance(ckchi); |
| 2036 | } |
| 2037 | /* delete the old sni_ctx, the old ckch_insts and the ckch_store */ |
| 2038 | list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) { |
| 2039 | __ckch_inst_free_locked(ckchi); |
| 2040 | } |
| 2041 | |
| 2042 | ckch_store_free(old_ckchs); |
| 2043 | ebst_insert(&ckchs_tree, &new_ckchs->node); |
| 2044 | } |
| 2045 | |
Remi Tricot-Le Breton | bfadc02 | 2021-02-24 12:20:48 +0100 | [diff] [blame] | 2046 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2047 | /* |
| 2048 | * This function tries to create the new ckch_inst and their SNIs |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 2049 | * |
| 2050 | * /!\ don't forget to update __hlua_ckch_commit() if you changes things there. /!\ |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2051 | */ |
| 2052 | static int cli_io_handler_commit_cert(struct appctx *appctx) |
| 2053 | { |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 2054 | struct commit_cert_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2055 | struct stconn *sc = appctx_sc(appctx); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2056 | int y = 0; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2057 | struct ckch_store *old_ckchs, *new_ckchs = NULL; |
William Lallemand | 3b5a3a6 | 2022-03-29 14:29:31 +0200 | [diff] [blame] | 2058 | struct ckch_inst *ckchi; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2059 | |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 2060 | if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2061 | goto end; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2062 | |
| 2063 | while (1) { |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2064 | switch (ctx->state) { |
| 2065 | case CERT_ST_INIT: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2066 | /* This state just print the update message */ |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2067 | chunk_printf(&trash, "Committing %s", ckchs_transaction.path); |
| 2068 | if (applet_putchk(appctx, &trash) == -1) |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2069 | goto yield; |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 2070 | |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2071 | ctx->state = CERT_ST_GEN; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2072 | /* fallthrough */ |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2073 | case CERT_ST_GEN: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2074 | /* |
| 2075 | * This state generates the ckch instances with their |
| 2076 | * sni_ctxs and SSL_CTX. |
| 2077 | * |
| 2078 | * Since the SSL_CTX generation can be CPU consumer, we |
| 2079 | * yield every 10 instances. |
| 2080 | */ |
| 2081 | |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 2082 | old_ckchs = ctx->old_ckchs; |
| 2083 | new_ckchs = ctx->new_ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2084 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2085 | /* get the next ckchi to regenerate */ |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 2086 | ckchi = ctx->next_ckchi; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2087 | /* we didn't start yet, set it to the first elem */ |
| 2088 | if (ckchi == NULL) |
| 2089 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 2090 | |
| 2091 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 2092 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 2093 | struct ckch_inst *new_inst; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2094 | |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2095 | /* save the next ckchi to compute in case of yield */ |
| 2096 | ctx->next_ckchi = ckchi; |
| 2097 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2098 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 2099 | if (y >= 10) { |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2100 | applet_have_more_data(appctx); /* let's come back later */ |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2101 | goto yield; |
| 2102 | } |
| 2103 | |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2104 | /* display one dot per new instance */ |
| 2105 | if (applet_putstr(appctx, ".") == -1) |
| 2106 | goto yield; |
| 2107 | |
| 2108 | ctx->err = NULL; |
| 2109 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &ctx->err)) { |
| 2110 | ctx->state = CERT_ST_ERROR; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2111 | goto error; |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2112 | } |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2113 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2114 | /* link the new ckch_inst to the duplicate */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2115 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2116 | y++; |
| 2117 | } |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2118 | ctx->state = CERT_ST_INSERT; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2119 | /* fallthrough */ |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2120 | case CERT_ST_INSERT: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2121 | /* The generation is finished, we can insert everything */ |
| 2122 | |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 2123 | old_ckchs = ctx->old_ckchs; |
| 2124 | new_ckchs = ctx->new_ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2125 | |
William Lallemand | 3b5a3a6 | 2022-03-29 14:29:31 +0200 | [diff] [blame] | 2126 | /* insert everything and remove the previous objects */ |
| 2127 | ckch_store_replace(old_ckchs, new_ckchs); |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2128 | ctx->new_ckchs = ctx->old_ckchs = NULL; |
| 2129 | ctx->state = CERT_ST_SUCCESS; |
| 2130 | /* fallthrough */ |
| 2131 | case CERT_ST_SUCCESS: |
| 2132 | if (applet_putstr(appctx, "\nSuccess!\n") == -1) |
| 2133 | goto yield; |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2134 | ctx->state = CERT_ST_FIN; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2135 | /* fallthrough */ |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2136 | case CERT_ST_FIN: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2137 | /* we achieved the transaction, we can set everything to NULL */ |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2138 | ckchs_transaction.new_ckchs = NULL; |
| 2139 | ckchs_transaction.old_ckchs = NULL; |
Christopher Faulet | e2ef4dd | 2022-05-31 18:07:59 +0200 | [diff] [blame] | 2140 | ckchs_transaction.path = NULL; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2141 | goto end; |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2142 | |
| 2143 | case CERT_ST_ERROR: |
| 2144 | error: |
| 2145 | chunk_printf(&trash, "\n%sFailed!\n", ctx->err); |
| 2146 | if (applet_putchk(appctx, &trash) == -1) |
| 2147 | goto yield; |
| 2148 | ctx->state = CERT_ST_FIN; |
| 2149 | break; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2150 | } |
| 2151 | } |
| 2152 | end: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2153 | /* success: call the release function and don't come back */ |
| 2154 | return 1; |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2155 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2156 | yield: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2157 | return 0; /* should come back */ |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | /* |
| 2161 | * Parsing function of 'commit ssl cert' |
| 2162 | */ |
| 2163 | static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 2164 | { |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 2165 | struct commit_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2166 | char *err = NULL; |
| 2167 | |
| 2168 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2169 | return 1; |
| 2170 | |
| 2171 | if (!*args[3]) |
| 2172 | return cli_err(appctx, "'commit ssl cert expects a filename\n"); |
| 2173 | |
| 2174 | /* The operations on the CKCH architecture are locked so we can |
| 2175 | * manipulate ckch_store and ckch_inst */ |
| 2176 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2177 | return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n"); |
| 2178 | |
| 2179 | if (!ckchs_transaction.path) { |
| 2180 | memprintf(&err, "No ongoing transaction! !\n"); |
| 2181 | goto error; |
| 2182 | } |
| 2183 | |
| 2184 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 2185 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]); |
| 2186 | goto error; |
| 2187 | } |
| 2188 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 2189 | /* if a certificate is here, a private key must be here too */ |
| 2190 | if (ckchs_transaction.new_ckchs->ckch->cert && !ckchs_transaction.new_ckchs->ckch->key) { |
| 2191 | memprintf(&err, "The transaction must contain at least a certificate and a private key!\n"); |
| 2192 | goto error; |
| 2193 | } |
William Lallemand | a941952 | 2020-06-24 16:26:41 +0200 | [diff] [blame] | 2194 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 2195 | if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) { |
| 2196 | memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path); |
| 2197 | goto error; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | /* init the appctx structure */ |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2201 | ctx->state = CERT_ST_INIT; |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 2202 | ctx->next_ckchi = NULL; |
| 2203 | ctx->new_ckchs = ckchs_transaction.new_ckchs; |
| 2204 | ctx->old_ckchs = ckchs_transaction.old_ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2205 | |
| 2206 | /* we don't unlock there, it will be unlock after the IO handler, in the release handler */ |
| 2207 | return 0; |
| 2208 | |
| 2209 | error: |
| 2210 | |
| 2211 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2212 | err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]); |
| 2213 | |
| 2214 | return cli_dynerr(appctx, err); |
| 2215 | } |
| 2216 | |
| 2217 | |
| 2218 | |
| 2219 | |
| 2220 | /* |
| 2221 | * Parsing function of `set ssl cert`, it updates or creates a temporary ckch. |
Willy Tarreau | 329f4b4 | 2022-05-04 20:05:55 +0200 | [diff] [blame] | 2222 | * It uses a set_cert_ctx context, and ckchs_transaction under a lock. |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2223 | */ |
| 2224 | static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 2225 | { |
| 2226 | struct ckch_store *new_ckchs = NULL; |
| 2227 | struct ckch_store *old_ckchs = NULL; |
| 2228 | char *err = NULL; |
| 2229 | int i; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2230 | int errcode = 0; |
| 2231 | char *end; |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 2232 | struct cert_exts *cert_ext = &cert_exts[0]; /* default one, PEM */ |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2233 | struct cert_key_and_chain *ckch; |
| 2234 | struct buffer *buf; |
| 2235 | |
| 2236 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2237 | return 1; |
| 2238 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2239 | if (!*args[3] || !payload) |
| 2240 | return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n"); |
| 2241 | |
| 2242 | /* The operations on the CKCH architecture are locked so we can |
| 2243 | * manipulate ckch_store and ckch_inst */ |
| 2244 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2245 | return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n"); |
| 2246 | |
William Lallemand | 5ba80d6 | 2021-05-04 16:17:27 +0200 | [diff] [blame] | 2247 | if ((buf = alloc_trash_chunk()) == NULL) { |
| 2248 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 2249 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2250 | goto end; |
| 2251 | } |
William Lallemand | e5ff4ad | 2020-06-08 09:40:37 +0200 | [diff] [blame] | 2252 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2253 | if (!chunk_strcpy(buf, args[3])) { |
| 2254 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 2255 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2256 | goto end; |
| 2257 | } |
| 2258 | |
| 2259 | /* check which type of file we want to update */ |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 2260 | for (i = 0; cert_exts[i].ext != NULL; i++) { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2261 | end = strrchr(buf->area, '.'); |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 2262 | if (end && *cert_exts[i].ext && (strcmp(end + 1, cert_exts[i].ext) == 0)) { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2263 | *end = '\0'; |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 2264 | buf->data = strlen(buf->area); |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 2265 | cert_ext = &cert_exts[i]; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2266 | break; |
| 2267 | } |
| 2268 | } |
| 2269 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2270 | /* if there is an ongoing transaction */ |
| 2271 | if (ckchs_transaction.path) { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2272 | /* if there is an ongoing transaction, check if this is the same file */ |
| 2273 | if (strcmp(ckchs_transaction.path, buf->area) != 0) { |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 2274 | /* we didn't find the transaction, must try more cases below */ |
| 2275 | |
| 2276 | /* if the del-ext option is activated we should try to take a look at a ".crt" too. */ |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 2277 | if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) { |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 2278 | if (!chunk_strcat(buf, ".crt")) { |
| 2279 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 2280 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2281 | goto end; |
| 2282 | } |
| 2283 | |
| 2284 | if (strcmp(ckchs_transaction.path, buf->area) != 0) { |
| 2285 | /* remove .crt of the error message */ |
| 2286 | *(b_orig(buf) + b_data(buf) + strlen(".crt")) = '\0'; |
| 2287 | b_sub(buf, strlen(".crt")); |
| 2288 | |
| 2289 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area); |
| 2290 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2291 | goto end; |
| 2292 | } |
| 2293 | } |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2294 | } |
| 2295 | |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2296 | old_ckchs = ckchs_transaction.new_ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2297 | |
| 2298 | } else { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2299 | |
William Lallemand | 95fefa1 | 2020-09-09 12:01:33 +0200 | [diff] [blame] | 2300 | /* lookup for the certificate in the tree */ |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2301 | old_ckchs = ckchs_lookup(buf->area); |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 2302 | |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2303 | if (!old_ckchs) { |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 2304 | /* if the del-ext option is activated we should try to take a look at a ".crt" too. */ |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 2305 | if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) { |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 2306 | if (!chunk_strcat(buf, ".crt")) { |
| 2307 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 2308 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2309 | goto end; |
| 2310 | } |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2311 | old_ckchs = ckchs_lookup(buf->area); |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 2312 | } |
| 2313 | } |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2314 | } |
| 2315 | |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2316 | if (!old_ckchs) { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2317 | memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n", |
| 2318 | err ? err : ""); |
| 2319 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2320 | goto end; |
| 2321 | } |
| 2322 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2323 | /* duplicate the ckch store */ |
| 2324 | new_ckchs = ckchs_dup(old_ckchs); |
| 2325 | if (!new_ckchs) { |
| 2326 | memprintf(&err, "%sCannot allocate memory!\n", |
| 2327 | err ? err : ""); |
| 2328 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2329 | goto end; |
| 2330 | } |
| 2331 | |
William Lallemand | 95fefa1 | 2020-09-09 12:01:33 +0200 | [diff] [blame] | 2332 | ckch = new_ckchs->ckch; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2333 | |
| 2334 | /* appply the change on the duplicate */ |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 2335 | if (cert_ext->load(buf->area, payload, ckch, &err) != 0) { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2336 | memprintf(&err, "%sCan't load the payload\n", err ? err : ""); |
| 2337 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2338 | goto end; |
| 2339 | } |
| 2340 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2341 | /* we succeed, we can save the ckchs in the transaction */ |
| 2342 | |
| 2343 | /* if there wasn't a transaction, update the old ckchs */ |
| 2344 | if (!ckchs_transaction.old_ckchs) { |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2345 | ckchs_transaction.old_ckchs = old_ckchs; |
| 2346 | ckchs_transaction.path = old_ckchs->path; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2347 | err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path); |
| 2348 | } else { |
| 2349 | err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path); |
| 2350 | |
| 2351 | } |
| 2352 | |
| 2353 | /* free the previous ckchs if there was a transaction */ |
| 2354 | ckch_store_free(ckchs_transaction.new_ckchs); |
| 2355 | |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2356 | ckchs_transaction.new_ckchs = new_ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2357 | |
| 2358 | |
| 2359 | /* creates the SNI ctxs later in the IO handler */ |
| 2360 | |
| 2361 | end: |
| 2362 | free_trash_chunk(buf); |
| 2363 | |
| 2364 | if (errcode & ERR_CODE) { |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2365 | ckch_store_free(new_ckchs); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2366 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2367 | return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3])); |
| 2368 | } else { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2369 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2370 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 2371 | } |
| 2372 | /* TODO: handle the ERR_WARN which are not handled because of the io_handler */ |
| 2373 | } |
| 2374 | |
| 2375 | /* parsing function of 'abort ssl cert' */ |
| 2376 | static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 2377 | { |
| 2378 | char *err = NULL; |
| 2379 | |
| 2380 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2381 | return 1; |
| 2382 | |
| 2383 | if (!*args[3]) |
| 2384 | return cli_err(appctx, "'abort ssl cert' expects a filename\n"); |
| 2385 | |
| 2386 | /* The operations on the CKCH architecture are locked so we can |
| 2387 | * manipulate ckch_store and ckch_inst */ |
| 2388 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2389 | return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n"); |
| 2390 | |
| 2391 | if (!ckchs_transaction.path) { |
| 2392 | memprintf(&err, "No ongoing transaction!\n"); |
| 2393 | goto error; |
| 2394 | } |
| 2395 | |
| 2396 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 2397 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]); |
| 2398 | goto error; |
| 2399 | } |
| 2400 | |
| 2401 | /* Only free the ckchs there, because the SNI and instances were not generated yet */ |
| 2402 | ckch_store_free(ckchs_transaction.new_ckchs); |
| 2403 | ckchs_transaction.new_ckchs = NULL; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2404 | ckchs_transaction.old_ckchs = NULL; |
Christopher Faulet | e2ef4dd | 2022-05-31 18:07:59 +0200 | [diff] [blame] | 2405 | ckchs_transaction.path = NULL; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2406 | |
| 2407 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2408 | |
| 2409 | err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]); |
| 2410 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 2411 | |
| 2412 | error: |
| 2413 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2414 | |
| 2415 | return cli_dynerr(appctx, err); |
| 2416 | } |
| 2417 | |
| 2418 | /* parsing function of 'new ssl cert' */ |
| 2419 | static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 2420 | { |
| 2421 | struct ckch_store *store; |
| 2422 | char *err = NULL; |
| 2423 | char *path; |
| 2424 | |
| 2425 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2426 | return 1; |
| 2427 | |
| 2428 | if (!*args[3]) |
| 2429 | return cli_err(appctx, "'new ssl cert' expects a filename\n"); |
| 2430 | |
| 2431 | path = args[3]; |
| 2432 | |
| 2433 | /* The operations on the CKCH architecture are locked so we can |
| 2434 | * manipulate ckch_store and ckch_inst */ |
| 2435 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2436 | return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n"); |
| 2437 | |
| 2438 | store = ckchs_lookup(path); |
| 2439 | if (store != NULL) { |
| 2440 | memprintf(&err, "Certificate '%s' already exists!\n", path); |
| 2441 | store = NULL; /* we don't want to free it */ |
| 2442 | goto error; |
| 2443 | } |
| 2444 | /* we won't support multi-certificate bundle here */ |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 2445 | store = ckch_store_new(path); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2446 | if (!store) { |
| 2447 | memprintf(&err, "unable to allocate memory.\n"); |
| 2448 | goto error; |
| 2449 | } |
| 2450 | |
| 2451 | /* insert into the ckchs tree */ |
| 2452 | ebst_insert(&ckchs_tree, &store->node); |
| 2453 | memprintf(&err, "New empty certificate store '%s'!\n", args[3]); |
| 2454 | |
| 2455 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2456 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 2457 | error: |
| 2458 | free(store); |
| 2459 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2460 | return cli_dynerr(appctx, err); |
| 2461 | } |
| 2462 | |
| 2463 | /* parsing function of 'del ssl cert' */ |
| 2464 | static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 2465 | { |
| 2466 | struct ckch_store *store; |
| 2467 | char *err = NULL; |
| 2468 | char *filename; |
| 2469 | |
| 2470 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2471 | return 1; |
| 2472 | |
| 2473 | if (!*args[3]) |
| 2474 | return cli_err(appctx, "'del ssl cert' expects a certificate name\n"); |
| 2475 | |
| 2476 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2477 | return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n"); |
| 2478 | |
| 2479 | filename = args[3]; |
| 2480 | |
Christopher Faulet | 926fefc | 2022-05-31 18:04:25 +0200 | [diff] [blame] | 2481 | if (ckchs_transaction.path && strcmp(ckchs_transaction.path, filename) == 0) { |
| 2482 | memprintf(&err, "ongoing transaction for the certificate '%s'", filename); |
| 2483 | goto error; |
| 2484 | } |
| 2485 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2486 | store = ckchs_lookup(filename); |
| 2487 | if (store == NULL) { |
| 2488 | memprintf(&err, "certificate '%s' doesn't exist!\n", filename); |
| 2489 | goto error; |
| 2490 | } |
| 2491 | if (!LIST_ISEMPTY(&store->ckch_inst)) { |
| 2492 | memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename); |
| 2493 | goto error; |
| 2494 | } |
| 2495 | |
| 2496 | ebmb_delete(&store->node); |
| 2497 | ckch_store_free(store); |
| 2498 | |
| 2499 | memprintf(&err, "Certificate '%s' deleted!\n", filename); |
| 2500 | |
| 2501 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2502 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 2503 | |
| 2504 | error: |
| 2505 | memprintf(&err, "Can't remove the certificate: %s\n", err ? err : ""); |
| 2506 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2507 | return cli_dynerr(appctx, err); |
| 2508 | } |
| 2509 | |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2510 | |
Remi Tricot-Le Breton | 9f40fe0 | 2021-03-16 16:21:27 +0100 | [diff] [blame] | 2511 | |
| 2512 | /* parsing function of 'new ssl ca-file' */ |
| 2513 | static int cli_parse_new_cafile(char **args, char *payload, struct appctx *appctx, void *private) |
| 2514 | { |
| 2515 | struct cafile_entry *cafile_entry; |
| 2516 | char *err = NULL; |
| 2517 | char *path; |
| 2518 | |
| 2519 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2520 | return 1; |
| 2521 | |
| 2522 | if (!*args[3]) |
| 2523 | return cli_err(appctx, "'new ssl ca-file' expects a filename\n"); |
| 2524 | |
| 2525 | path = args[3]; |
| 2526 | |
| 2527 | /* The operations on the CKCH architecture are locked so we can |
| 2528 | * manipulate ckch_store and ckch_inst */ |
| 2529 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2530 | return cli_err(appctx, "Can't create a CA file!\nOperations on certificates are currently locked!\n"); |
| 2531 | |
| 2532 | cafile_entry = ssl_store_get_cafile_entry(path, 0); |
| 2533 | if (cafile_entry) { |
| 2534 | memprintf(&err, "CA file '%s' already exists!\n", path); |
| 2535 | goto error; |
| 2536 | } |
| 2537 | |
| 2538 | cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CERT); |
| 2539 | if (!cafile_entry) { |
| 2540 | memprintf(&err, "%sCannot allocate memory!\n", |
| 2541 | err ? err : ""); |
| 2542 | goto error; |
| 2543 | } |
| 2544 | |
| 2545 | /* Add the newly created cafile_entry to the tree so that |
| 2546 | * any new ckch instance created from now can use it. */ |
| 2547 | if (ssl_store_add_uncommitted_cafile_entry(cafile_entry)) |
| 2548 | goto error; |
| 2549 | |
| 2550 | memprintf(&err, "New CA file created '%s'!\n", path); |
| 2551 | |
| 2552 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2553 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 2554 | error: |
| 2555 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2556 | return cli_dynerr(appctx, err); |
| 2557 | } |
| 2558 | |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2559 | /* |
| 2560 | * Parsing function of `set ssl ca-file` |
| 2561 | */ |
| 2562 | static int cli_parse_set_cafile(char **args, char *payload, struct appctx *appctx, void *private) |
| 2563 | { |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2564 | struct cafile_entry *old_cafile_entry = NULL; |
| 2565 | struct cafile_entry *new_cafile_entry = NULL; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2566 | char *err = NULL; |
| 2567 | int errcode = 0; |
| 2568 | struct buffer *buf; |
| 2569 | |
| 2570 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2571 | return 1; |
| 2572 | |
| 2573 | if (!*args[3] || !payload) |
| 2574 | return cli_err(appctx, "'set ssl ca-file expects a filename and CAs as a payload\n"); |
| 2575 | |
| 2576 | /* The operations on the CKCH architecture are locked so we can |
| 2577 | * manipulate ckch_store and ckch_inst */ |
| 2578 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2579 | return cli_err(appctx, "Can't update the CA file!\nOperations on certificates are currently locked!\n"); |
| 2580 | |
| 2581 | if ((buf = alloc_trash_chunk()) == NULL) { |
| 2582 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 2583 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2584 | goto end; |
| 2585 | } |
| 2586 | |
| 2587 | if (!chunk_strcpy(buf, args[3])) { |
| 2588 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 2589 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2590 | goto end; |
| 2591 | } |
| 2592 | |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2593 | old_cafile_entry = NULL; |
| 2594 | new_cafile_entry = NULL; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2595 | |
| 2596 | /* if there is an ongoing transaction */ |
| 2597 | if (cafile_transaction.path) { |
| 2598 | /* if there is an ongoing transaction, check if this is the same file */ |
| 2599 | if (strcmp(cafile_transaction.path, buf->area) != 0) { |
| 2600 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, buf->area); |
| 2601 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2602 | goto end; |
| 2603 | } |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2604 | old_cafile_entry = cafile_transaction.old_cafile_entry; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2605 | } |
| 2606 | else { |
| 2607 | /* lookup for the certificate in the tree */ |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2608 | old_cafile_entry = ssl_store_get_cafile_entry(buf->area, 0); |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2609 | } |
| 2610 | |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2611 | if (!old_cafile_entry) { |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2612 | memprintf(&err, "%sCan't replace a CA file which is not referenced by the configuration!\n", |
| 2613 | err ? err : ""); |
| 2614 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2615 | goto end; |
| 2616 | } |
| 2617 | |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2618 | /* Create a new cafile_entry without adding it to the cafile tree. */ |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2619 | new_cafile_entry = ssl_store_create_cafile_entry(old_cafile_entry->path, NULL, CAFILE_CERT); |
| 2620 | if (!new_cafile_entry) { |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2621 | memprintf(&err, "%sCannot allocate memory!\n", |
| 2622 | err ? err : ""); |
| 2623 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2624 | goto end; |
| 2625 | } |
| 2626 | |
| 2627 | /* Fill the new entry with the new CAs. */ |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2628 | if (ssl_store_load_ca_from_buf(new_cafile_entry, payload)) { |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2629 | memprintf(&err, "%sInvalid payload\n", err ? err : ""); |
| 2630 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2631 | goto end; |
| 2632 | } |
| 2633 | |
| 2634 | /* we succeed, we can save the ca in the transaction */ |
| 2635 | |
| 2636 | /* if there wasn't a transaction, update the old CA */ |
| 2637 | if (!cafile_transaction.old_cafile_entry) { |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2638 | cafile_transaction.old_cafile_entry = old_cafile_entry; |
| 2639 | cafile_transaction.path = old_cafile_entry->path; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2640 | err = memprintf(&err, "transaction created for CA %s!\n", cafile_transaction.path); |
| 2641 | } else { |
| 2642 | err = memprintf(&err, "transaction updated for CA %s!\n", cafile_transaction.path); |
| 2643 | } |
| 2644 | |
| 2645 | /* free the previous CA if there was a transaction */ |
| 2646 | ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry); |
| 2647 | |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2648 | cafile_transaction.new_cafile_entry = new_cafile_entry; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2649 | |
| 2650 | /* creates the SNI ctxs later in the IO handler */ |
| 2651 | |
| 2652 | end: |
| 2653 | free_trash_chunk(buf); |
| 2654 | |
| 2655 | if (errcode & ERR_CODE) { |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2656 | ssl_store_delete_cafile_entry(new_cafile_entry); |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2657 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2658 | return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3])); |
| 2659 | } else { |
| 2660 | |
| 2661 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2662 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 2663 | } |
| 2664 | } |
| 2665 | |
| 2666 | |
| 2667 | /* |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2668 | * Parsing function of 'commit ssl ca-file'. |
| 2669 | * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl crl-file". |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2670 | */ |
| 2671 | static int cli_parse_commit_cafile(char **args, char *payload, struct appctx *appctx, void *private) |
| 2672 | { |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2673 | struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2674 | char *err = NULL; |
| 2675 | |
| 2676 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2677 | return 1; |
| 2678 | |
| 2679 | if (!*args[3]) |
| 2680 | return cli_err(appctx, "'commit ssl ca-file expects a filename\n"); |
| 2681 | |
| 2682 | /* The operations on the CKCH architecture are locked so we can |
| 2683 | * manipulate ckch_store and ckch_inst */ |
| 2684 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2685 | return cli_err(appctx, "Can't commit the CA file!\nOperations on certificates are currently locked!\n"); |
| 2686 | |
| 2687 | if (!cafile_transaction.path) { |
| 2688 | memprintf(&err, "No ongoing transaction! !\n"); |
| 2689 | goto error; |
| 2690 | } |
| 2691 | |
| 2692 | if (strcmp(cafile_transaction.path, args[3]) != 0) { |
| 2693 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, args[3]); |
| 2694 | goto error; |
| 2695 | } |
| 2696 | /* init the appctx structure */ |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2697 | ctx->state = CACRL_ST_INIT; |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2698 | ctx->next_ckchi_link = NULL; |
Christopher Faulet | 6af2fc6 | 2022-06-03 11:42:38 +0200 | [diff] [blame] | 2699 | ctx->old_entry = cafile_transaction.old_cafile_entry; |
| 2700 | ctx->new_entry = cafile_transaction.new_cafile_entry; |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2701 | ctx->cafile_type = CAFILE_CERT; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2702 | |
| 2703 | return 0; |
| 2704 | |
| 2705 | error: |
| 2706 | |
| 2707 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2708 | err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]); |
| 2709 | |
| 2710 | return cli_dynerr(appctx, err); |
| 2711 | } |
| 2712 | |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2713 | /* |
| 2714 | * This function tries to create new ckch instances and their SNIs using a newly |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 2715 | * set certificate authority (CA file) or a newly set Certificate Revocation |
| 2716 | * List (CRL), depending on the command being called. |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2717 | */ |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 2718 | static int cli_io_handler_commit_cafile_crlfile(struct appctx *appctx) |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2719 | { |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2720 | struct commit_cacrlfile_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2721 | struct stconn *sc = appctx_sc(appctx); |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2722 | int y = 0; |
Christopher Faulet | 6af2fc6 | 2022-06-03 11:42:38 +0200 | [diff] [blame] | 2723 | struct cafile_entry *old_cafile_entry = ctx->old_entry; |
| 2724 | struct cafile_entry *new_cafile_entry = ctx->new_entry; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2725 | struct ckch_inst_link *ckchi_link; |
Christopher Faulet | ddc8e1c | 2022-06-03 09:00:09 +0200 | [diff] [blame] | 2726 | char *path; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2727 | |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 2728 | if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2729 | goto end; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2730 | |
Christopher Faulet | ddc8e1c | 2022-06-03 09:00:09 +0200 | [diff] [blame] | 2731 | /* The ctx was already validated by the ca-file/crl-file parsing |
| 2732 | * function. Entries can only be NULL in CACRL_ST_SUCCESS or |
| 2733 | * CACRL_ST_FIN states |
| 2734 | */ |
| 2735 | switch (ctx->cafile_type) { |
| 2736 | case CAFILE_CERT: |
Christopher Faulet | ddc8e1c | 2022-06-03 09:00:09 +0200 | [diff] [blame] | 2737 | path = cafile_transaction.path; |
| 2738 | break; |
| 2739 | case CAFILE_CRL: |
Christopher Faulet | ddc8e1c | 2022-06-03 09:00:09 +0200 | [diff] [blame] | 2740 | path = crlfile_transaction.path; |
| 2741 | break; |
Christopher Faulet | ea2c8c6 | 2022-06-03 16:37:31 +0200 | [diff] [blame] | 2742 | default: |
Willy Tarreau | d543ae0 | 2022-06-22 05:40:25 +0200 | [diff] [blame] | 2743 | path = NULL; |
Christopher Faulet | ea2c8c6 | 2022-06-03 16:37:31 +0200 | [diff] [blame] | 2744 | goto error; |
Christopher Faulet | ddc8e1c | 2022-06-03 09:00:09 +0200 | [diff] [blame] | 2745 | } |
| 2746 | |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2747 | while (1) { |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2748 | switch (ctx->state) { |
| 2749 | case CACRL_ST_INIT: |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2750 | /* This state just print the update message */ |
Christopher Faulet | ddc8e1c | 2022-06-03 09:00:09 +0200 | [diff] [blame] | 2751 | chunk_printf(&trash, "Committing %s", path); |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2752 | if (applet_putchk(appctx, &trash) == -1) |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2753 | goto yield; |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 2754 | |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2755 | ctx->state = CACRL_ST_GEN; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2756 | /* fallthrough */ |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2757 | case CACRL_ST_GEN: |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2758 | /* |
| 2759 | * This state generates the ckch instances with their |
| 2760 | * sni_ctxs and SSL_CTX. |
| 2761 | * |
| 2762 | * Since the SSL_CTX generation can be CPU consumer, we |
| 2763 | * yield every 10 instances. |
| 2764 | */ |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2765 | |
| 2766 | /* get the next ckchi to regenerate */ |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2767 | ckchi_link = ctx->next_ckchi_link; |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2768 | |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2769 | /* we didn't start yet, set it to the first elem */ |
| 2770 | if (ckchi_link == NULL) { |
| 2771 | ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list); |
| 2772 | /* Add the newly created cafile_entry to the tree so that |
| 2773 | * any new ckch instance created from now can use it. */ |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2774 | if (ssl_store_add_uncommitted_cafile_entry(new_cafile_entry)) { |
| 2775 | ctx->state = CACRL_ST_ERROR; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2776 | goto error; |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2777 | } |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | list_for_each_entry_from(ckchi_link, &old_cafile_entry->ckch_inst_link, list) { |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2781 | struct ckch_inst *new_inst; |
| 2782 | |
| 2783 | /* save the next ckchi to compute */ |
| 2784 | ctx->next_ckchi_link = ckchi_link; |
| 2785 | |
| 2786 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 2787 | if (y >= 10) { |
| 2788 | applet_have_more_data(appctx); /* let's come back later */ |
| 2789 | goto yield; |
| 2790 | } |
| 2791 | |
| 2792 | /* display one dot per new instance */ |
| 2793 | if (applet_putstr(appctx, ".") == -1) |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2794 | goto yield; |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2795 | |
| 2796 | /* Rebuild a new ckch instance that uses the same ckch_store |
| 2797 | * than a reference ckchi instance but will use a new CA file. */ |
| 2798 | ctx->err = NULL; |
| 2799 | if (ckch_inst_rebuild(ckchi_link->ckch_inst->ckch_store, ckchi_link->ckch_inst, &new_inst, &ctx->err)) { |
| 2800 | ctx->state = CACRL_ST_ERROR; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2801 | goto error; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2802 | } |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2803 | |
| 2804 | y++; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2805 | } |
| 2806 | |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2807 | ctx->state = CACRL_ST_INSERT; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2808 | /* fallthrough */ |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2809 | case CACRL_ST_INSERT: |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2810 | /* The generation is finished, we can insert everything */ |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2811 | |
| 2812 | /* insert the new ckch_insts in the crtlist_entry */ |
| 2813 | list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) { |
| 2814 | if (ckchi_link->ckch_inst->crtlist_entry) |
| 2815 | LIST_INSERT(&ckchi_link->ckch_inst->crtlist_entry->ckch_inst, |
| 2816 | &ckchi_link->ckch_inst->by_crtlist_entry); |
| 2817 | } |
| 2818 | |
| 2819 | /* First, we insert every new SNIs in the trees, also replace the default_ctx */ |
| 2820 | list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) { |
| 2821 | __ssl_sock_load_new_ckch_instance(ckchi_link->ckch_inst); |
| 2822 | } |
| 2823 | |
| 2824 | /* delete the old sni_ctx, the old ckch_insts and the ckch_store */ |
| 2825 | list_for_each_entry(ckchi_link, &old_cafile_entry->ckch_inst_link, list) { |
| 2826 | __ckch_inst_free_locked(ckchi_link->ckch_inst); |
| 2827 | } |
| 2828 | |
| 2829 | |
| 2830 | /* Remove the old cafile entry from the tree */ |
| 2831 | ebmb_delete(&old_cafile_entry->node); |
| 2832 | ssl_store_delete_cafile_entry(old_cafile_entry); |
| 2833 | |
Christopher Faulet | 6af2fc6 | 2022-06-03 11:42:38 +0200 | [diff] [blame] | 2834 | ctx->old_entry = ctx->new_entry = NULL; |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2835 | ctx->state = CACRL_ST_SUCCESS; |
| 2836 | /* fallthrough */ |
| 2837 | case CACRL_ST_SUCCESS: |
| 2838 | if (applet_putstr(appctx, "\nSuccess!\n") == -1) |
| 2839 | goto yield; |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2840 | ctx->state = CACRL_ST_FIN; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2841 | /* fallthrough */ |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2842 | case CACRL_ST_FIN: |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2843 | /* we achieved the transaction, we can set everything to NULL */ |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2844 | switch (ctx->cafile_type) { |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 2845 | case CAFILE_CERT: |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 2846 | cafile_transaction.old_cafile_entry = NULL; |
| 2847 | cafile_transaction.new_cafile_entry = NULL; |
Christopher Faulet | 1e00c7e | 2022-05-31 18:10:19 +0200 | [diff] [blame] | 2848 | cafile_transaction.path = NULL; |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 2849 | break; |
| 2850 | case CAFILE_CRL: |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 2851 | crlfile_transaction.old_crlfile_entry = NULL; |
| 2852 | crlfile_transaction.new_crlfile_entry = NULL; |
Christopher Faulet | 1e00c7e | 2022-05-31 18:10:19 +0200 | [diff] [blame] | 2853 | crlfile_transaction.path = NULL; |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 2854 | break; |
| 2855 | } |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2856 | goto end; |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2857 | |
| 2858 | case CACRL_ST_ERROR: |
| 2859 | error: |
| 2860 | chunk_printf(&trash, "\n%sFailed!\n", ctx->err); |
| 2861 | if (applet_putchk(appctx, &trash) == -1) |
| 2862 | goto yield; |
| 2863 | ctx->state = CACRL_ST_FIN; |
| 2864 | break; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2865 | } |
| 2866 | } |
| 2867 | end: |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2868 | /* success: call the release function and don't come back */ |
| 2869 | return 1; |
| 2870 | yield: |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2871 | return 0; /* should come back */ |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2872 | } |
| 2873 | |
Remi Tricot-Le Breton | d5fd09d | 2021-03-11 10:22:52 +0100 | [diff] [blame] | 2874 | |
| 2875 | /* parsing function of 'abort ssl ca-file' */ |
| 2876 | static int cli_parse_abort_cafile(char **args, char *payload, struct appctx *appctx, void *private) |
| 2877 | { |
| 2878 | char *err = NULL; |
| 2879 | |
| 2880 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2881 | return 1; |
| 2882 | |
| 2883 | if (!*args[3]) |
| 2884 | return cli_err(appctx, "'abort ssl ca-file' expects a filename\n"); |
| 2885 | |
| 2886 | /* The operations on the CKCH architecture are locked so we can |
| 2887 | * manipulate ckch_store and ckch_inst */ |
| 2888 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2889 | return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n"); |
| 2890 | |
| 2891 | if (!cafile_transaction.path) { |
| 2892 | memprintf(&err, "No ongoing transaction!\n"); |
| 2893 | goto error; |
| 2894 | } |
| 2895 | |
| 2896 | if (strcmp(cafile_transaction.path, args[3]) != 0) { |
| 2897 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", cafile_transaction.path, args[3]); |
| 2898 | goto error; |
| 2899 | } |
| 2900 | |
| 2901 | /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */ |
| 2902 | ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry); |
| 2903 | cafile_transaction.new_cafile_entry = NULL; |
| 2904 | cafile_transaction.old_cafile_entry = NULL; |
Christopher Faulet | 1e00c7e | 2022-05-31 18:10:19 +0200 | [diff] [blame] | 2905 | cafile_transaction.path = NULL; |
Remi Tricot-Le Breton | d5fd09d | 2021-03-11 10:22:52 +0100 | [diff] [blame] | 2906 | |
| 2907 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2908 | |
| 2909 | err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]); |
| 2910 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 2911 | |
| 2912 | error: |
| 2913 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2914 | |
| 2915 | return cli_dynerr(appctx, err); |
| 2916 | } |
| 2917 | |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 2918 | /* release function of the `commit ssl ca-file' command, free things and unlock the spinlock. |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2919 | * It uses a commit_cacrlfile_ctx context. |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 2920 | */ |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2921 | static void cli_release_commit_cafile(struct appctx *appctx) |
| 2922 | { |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2923 | struct commit_cacrlfile_ctx *ctx = appctx->svcctx; |
Christopher Faulet | 6af2fc6 | 2022-06-03 11:42:38 +0200 | [diff] [blame] | 2924 | struct cafile_entry *new_cafile_entry = ctx->new_entry; |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2925 | |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2926 | /* Remove the uncommitted cafile_entry from the tree. */ |
| 2927 | if (new_cafile_entry) { |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2928 | ebmb_delete(&new_cafile_entry->node); |
| 2929 | ssl_store_delete_cafile_entry(new_cafile_entry); |
| 2930 | } |
| 2931 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2932 | ha_free(&ctx->err); |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 2936 | /* IO handler of details "show ssl ca-file <filename[:index]>". |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 2937 | * It uses a show_cafile_ctx context, and the global |
| 2938 | * cafile_transaction.new_cafile_entry in read-only. |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 2939 | */ |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2940 | static int cli_io_handler_show_cafile_detail(struct appctx *appctx) |
| 2941 | { |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 2942 | struct show_cafile_ctx *ctx = appctx->svcctx; |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 2943 | struct cafile_entry *cafile_entry = ctx->cur_cafile_entry; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2944 | struct buffer *out = alloc_trash_chunk(); |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2945 | int i = 0; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2946 | X509 *cert; |
| 2947 | STACK_OF(X509_OBJECT) *objs; |
| 2948 | int retval = 0; |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 2949 | int ca_index = ctx->ca_index; |
| 2950 | int show_all = ctx->show_all; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2951 | |
| 2952 | if (!out) |
| 2953 | goto end_no_putchk; |
| 2954 | |
| 2955 | chunk_appendf(out, "Filename: "); |
| 2956 | if (cafile_entry == cafile_transaction.new_cafile_entry) |
| 2957 | chunk_appendf(out, "*"); |
| 2958 | chunk_appendf(out, "%s\n", cafile_entry->path); |
| 2959 | |
| 2960 | chunk_appendf(out, "Status: "); |
| 2961 | if (!cafile_entry->ca_store) |
| 2962 | chunk_appendf(out, "Empty\n"); |
| 2963 | else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) |
| 2964 | chunk_appendf(out, "Unused\n"); |
| 2965 | else |
| 2966 | chunk_appendf(out, "Used\n"); |
| 2967 | |
| 2968 | if (!cafile_entry->ca_store) |
| 2969 | goto end; |
| 2970 | |
| 2971 | objs = X509_STORE_get0_objects(cafile_entry->ca_store); |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2972 | for (i = ca_index; i < sk_X509_OBJECT_num(objs); i++) { |
| 2973 | |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2974 | cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i)); |
| 2975 | if (!cert) |
| 2976 | continue; |
| 2977 | |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2978 | /* file starts at line 1 */ |
Remi Tricot-Le Breton | e8041fe | 2022-04-05 16:44:21 +0200 | [diff] [blame] | 2979 | chunk_appendf(out, " \nCertificate #%d:\n", i+1); |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2980 | retval = show_cert_detail(cert, NULL, out); |
| 2981 | if (retval < 0) |
| 2982 | goto end_no_putchk; |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2983 | else if (retval) |
| 2984 | goto yield; |
| 2985 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 2986 | if (applet_putchk(appctx, out) == -1) |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2987 | goto yield; |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2988 | |
| 2989 | if (!show_all) /* only need to dump one certificate */ |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2990 | goto end; |
| 2991 | } |
| 2992 | |
| 2993 | end: |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2994 | free_trash_chunk(out); |
| 2995 | return 1; /* end, don't come back */ |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2996 | |
| 2997 | end_no_putchk: |
| 2998 | free_trash_chunk(out); |
| 2999 | return 1; |
| 3000 | yield: |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 3001 | /* save the current state */ |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3002 | ctx->ca_index = i; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3003 | free_trash_chunk(out); |
| 3004 | return 0; /* should come back */ |
| 3005 | } |
| 3006 | |
| 3007 | |
Willy Tarreau | 0630579 | 2022-05-04 15:57:30 +0200 | [diff] [blame] | 3008 | /* parsing function for 'show ssl ca-file [cafile[:index]]'. |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3009 | * It prepares a show_cafile_ctx context, and checks the global |
| 3010 | * cafile_transaction under the ckch_lock (read only). |
Willy Tarreau | 0630579 | 2022-05-04 15:57:30 +0200 | [diff] [blame] | 3011 | */ |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3012 | static int cli_parse_show_cafile(char **args, char *payload, struct appctx *appctx, void *private) |
| 3013 | { |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3014 | struct show_cafile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3015 | struct cafile_entry *cafile_entry; |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 3016 | int ca_index = 0; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3017 | char *colons; |
| 3018 | char *err = NULL; |
| 3019 | |
| 3020 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 3021 | return cli_err(appctx, "Can't allocate memory!\n"); |
| 3022 | |
| 3023 | /* The operations on the CKCH architecture are locked so we can |
| 3024 | * manipulate ckch_store and ckch_inst */ |
| 3025 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 3026 | return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n"); |
| 3027 | |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3028 | ctx->show_all = 1; /* show all certificates */ |
| 3029 | ctx->ca_index = 0; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3030 | /* check if there is a certificate to lookup */ |
| 3031 | if (*args[3]) { |
| 3032 | |
| 3033 | /* Look for an optional CA index after the CA file name */ |
| 3034 | colons = strchr(args[3], ':'); |
| 3035 | if (colons) { |
| 3036 | char *endptr; |
| 3037 | |
| 3038 | ca_index = strtol(colons + 1, &endptr, 10); |
| 3039 | /* Indexes start at 1 */ |
| 3040 | if (colons + 1 == endptr || *endptr != '\0' || ca_index <= 0) { |
| 3041 | memprintf(&err, "wrong CA index after colons in '%s'!", args[3]); |
| 3042 | goto error; |
| 3043 | } |
| 3044 | *colons = '\0'; |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3045 | ctx->ca_index = ca_index - 1; /* we start counting at 0 in the ca_store, but at 1 on the CLI */ |
| 3046 | ctx->show_all = 0; /* show only one certificate */ |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | if (*args[3] == '*') { |
| 3050 | if (!cafile_transaction.new_cafile_entry) |
| 3051 | goto error; |
| 3052 | |
| 3053 | cafile_entry = cafile_transaction.new_cafile_entry; |
| 3054 | |
| 3055 | if (strcmp(args[3] + 1, cafile_entry->path) != 0) |
| 3056 | goto error; |
| 3057 | |
| 3058 | } else { |
| 3059 | /* Get the "original" cafile_entry and not the |
| 3060 | * uncommitted one if it exists. */ |
| 3061 | if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CERT) |
| 3062 | goto error; |
| 3063 | } |
| 3064 | |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3065 | ctx->cur_cafile_entry = cafile_entry; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3066 | /* use the IO handler that shows details */ |
| 3067 | appctx->io_handler = cli_io_handler_show_cafile_detail; |
| 3068 | } |
| 3069 | |
| 3070 | return 0; |
| 3071 | |
| 3072 | error: |
| 3073 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3074 | if (err) |
| 3075 | return cli_dynerr(appctx, err); |
| 3076 | return cli_err(appctx, "Can't display the CA file : Not found!\n"); |
| 3077 | } |
| 3078 | |
| 3079 | |
| 3080 | /* release function of the 'show ssl ca-file' command */ |
| 3081 | static void cli_release_show_cafile(struct appctx *appctx) |
| 3082 | { |
| 3083 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3084 | } |
| 3085 | |
| 3086 | |
| 3087 | /* This function returns the number of certificates in a cafile_entry. */ |
| 3088 | static int get_certificate_count(struct cafile_entry *cafile_entry) |
| 3089 | { |
| 3090 | int cert_count = 0; |
| 3091 | STACK_OF(X509_OBJECT) *objs; |
| 3092 | |
| 3093 | if (cafile_entry && cafile_entry->ca_store) { |
| 3094 | objs = X509_STORE_get0_objects(cafile_entry->ca_store); |
| 3095 | if (objs) |
| 3096 | cert_count = sk_X509_OBJECT_num(objs); |
| 3097 | } |
| 3098 | return cert_count; |
| 3099 | } |
| 3100 | |
| 3101 | /* IO handler of "show ssl ca-file". The command taking a specific CA file name |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 3102 | * is managed in cli_io_handler_show_cafile_detail. |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3103 | * It uses a show_cafile_ctx and the global cafile_transaction.new_cafile_entry |
| 3104 | * in read-only. |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 3105 | */ |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3106 | static int cli_io_handler_show_cafile(struct appctx *appctx) |
| 3107 | { |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3108 | struct show_cafile_ctx *ctx = appctx->svcctx; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3109 | struct buffer *trash = alloc_trash_chunk(); |
| 3110 | struct ebmb_node *node; |
Christopher Faulet | 677cb4f | 2022-06-03 16:25:35 +0200 | [diff] [blame] | 3111 | struct cafile_entry *cafile_entry = NULL; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3112 | |
| 3113 | if (trash == NULL) |
| 3114 | return 1; |
| 3115 | |
Christopher Faulet | 5a2154b | 2022-06-03 10:42:48 +0200 | [diff] [blame] | 3116 | if (!ctx->old_cafile_entry && cafile_transaction.old_cafile_entry) { |
| 3117 | chunk_appendf(trash, "# transaction\n"); |
| 3118 | chunk_appendf(trash, "*%s", cafile_transaction.old_cafile_entry->path); |
| 3119 | chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_transaction.new_cafile_entry)); |
| 3120 | if (applet_putchk(appctx, trash) == -1) |
| 3121 | goto yield; |
| 3122 | ctx->old_cafile_entry = cafile_transaction.new_cafile_entry; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | /* First time in this io_handler. */ |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3126 | if (!ctx->cur_cafile_entry) { |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3127 | chunk_appendf(trash, "# filename\n"); |
| 3128 | node = ebmb_first(&cafile_tree); |
| 3129 | } else { |
| 3130 | /* We yielded during a previous call. */ |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3131 | node = &ctx->cur_cafile_entry->node; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3132 | } |
| 3133 | |
| 3134 | while (node) { |
| 3135 | cafile_entry = ebmb_entry(node, struct cafile_entry, node); |
| 3136 | if (cafile_entry->type == CAFILE_CERT) { |
| 3137 | chunk_appendf(trash, "%s", cafile_entry->path); |
| 3138 | |
| 3139 | chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_entry)); |
| 3140 | } |
| 3141 | |
| 3142 | node = ebmb_next(node); |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 3143 | if (applet_putchk(appctx, trash) == -1) |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3144 | goto yield; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3145 | } |
| 3146 | |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3147 | ctx->cur_cafile_entry = NULL; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3148 | free_trash_chunk(trash); |
| 3149 | return 1; |
| 3150 | yield: |
| 3151 | |
| 3152 | free_trash_chunk(trash); |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3153 | ctx->cur_cafile_entry = cafile_entry; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3154 | return 0; /* should come back */ |
| 3155 | } |
| 3156 | |
Remi Tricot-Le Breton | c3a8477 | 2021-03-25 18:13:57 +0100 | [diff] [blame] | 3157 | /* parsing function of 'del ssl ca-file' */ |
| 3158 | static int cli_parse_del_cafile(char **args, char *payload, struct appctx *appctx, void *private) |
| 3159 | { |
| 3160 | struct cafile_entry *cafile_entry; |
| 3161 | char *err = NULL; |
| 3162 | char *filename; |
| 3163 | |
| 3164 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 3165 | return 1; |
| 3166 | |
| 3167 | if (!*args[3]) |
| 3168 | return cli_err(appctx, "'del ssl ca-file' expects a CA file name\n"); |
| 3169 | |
| 3170 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 3171 | return cli_err(appctx, "Can't delete the CA file!\nOperations on certificates are currently locked!\n"); |
| 3172 | |
| 3173 | filename = args[3]; |
| 3174 | |
Christopher Faulet | 1f08fa4 | 2022-05-31 18:06:30 +0200 | [diff] [blame] | 3175 | if (cafile_transaction.path && strcmp(cafile_transaction.path, filename) == 0) { |
| 3176 | memprintf(&err, "ongoing transaction for the CA file '%s'", filename); |
| 3177 | goto error; |
| 3178 | } |
| 3179 | |
Remi Tricot-Le Breton | c3a8477 | 2021-03-25 18:13:57 +0100 | [diff] [blame] | 3180 | cafile_entry = ssl_store_get_cafile_entry(filename, 0); |
| 3181 | if (!cafile_entry) { |
| 3182 | memprintf(&err, "CA file '%s' doesn't exist!\n", filename); |
| 3183 | goto error; |
| 3184 | } |
| 3185 | |
| 3186 | if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) { |
| 3187 | memprintf(&err, "CA file '%s' in use, can't be deleted!\n", filename); |
| 3188 | goto error; |
| 3189 | } |
| 3190 | |
| 3191 | /* Remove the cafile_entry from the tree */ |
| 3192 | ebmb_delete(&cafile_entry->node); |
| 3193 | ssl_store_delete_cafile_entry(cafile_entry); |
| 3194 | |
| 3195 | memprintf(&err, "CA file '%s' deleted!\n", filename); |
| 3196 | |
| 3197 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3198 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 3199 | |
| 3200 | error: |
| 3201 | memprintf(&err, "Can't remove the CA file: %s\n", err ? err : ""); |
| 3202 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3203 | return cli_dynerr(appctx, err); |
| 3204 | } |
| 3205 | |
Remi Tricot-Le Breton | 720e3b9 | 2021-04-26 11:00:42 +0200 | [diff] [blame] | 3206 | /* parsing function of 'new ssl crl-file' */ |
| 3207 | static int cli_parse_new_crlfile(char **args, char *payload, struct appctx *appctx, void *private) |
| 3208 | { |
| 3209 | struct cafile_entry *cafile_entry; |
| 3210 | char *err = NULL; |
| 3211 | char *path; |
| 3212 | |
| 3213 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 3214 | return 1; |
| 3215 | |
| 3216 | if (!*args[3]) |
| 3217 | return cli_err(appctx, "'new ssl crl-file' expects a filename\n"); |
| 3218 | |
| 3219 | path = args[3]; |
| 3220 | |
| 3221 | /* The operations on the CKCH architecture are locked so we can |
| 3222 | * manipulate ckch_store and ckch_inst */ |
| 3223 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
Remi Tricot-Le Breton | 444d702 | 2022-05-05 17:18:40 +0200 | [diff] [blame] | 3224 | return cli_err(appctx, "Can't create a CRL file!\nOperations on certificates are currently locked!\n"); |
Remi Tricot-Le Breton | 720e3b9 | 2021-04-26 11:00:42 +0200 | [diff] [blame] | 3225 | |
| 3226 | cafile_entry = ssl_store_get_cafile_entry(path, 0); |
| 3227 | if (cafile_entry) { |
| 3228 | memprintf(&err, "CRL file '%s' already exists!\n", path); |
| 3229 | goto error; |
| 3230 | } |
| 3231 | |
| 3232 | cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CRL); |
| 3233 | if (!cafile_entry) { |
| 3234 | memprintf(&err, "%sCannot allocate memory!\n", err ? err : ""); |
| 3235 | goto error; |
| 3236 | } |
| 3237 | |
| 3238 | /* Add the newly created cafile_entry to the tree so that |
| 3239 | * any new ckch instance created from now can use it. */ |
| 3240 | if (ssl_store_add_uncommitted_cafile_entry(cafile_entry)) |
| 3241 | goto error; |
| 3242 | |
| 3243 | memprintf(&err, "New CRL file created '%s'!\n", path); |
| 3244 | |
| 3245 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3246 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 3247 | error: |
| 3248 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3249 | return cli_dynerr(appctx, err); |
| 3250 | } |
| 3251 | |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3252 | /* Parsing function of `set ssl crl-file` */ |
| 3253 | static int cli_parse_set_crlfile(char **args, char *payload, struct appctx *appctx, void *private) |
| 3254 | { |
Christopher Faulet | 1f90f33 | 2022-06-03 16:34:30 +0200 | [diff] [blame] | 3255 | struct cafile_entry *old_crlfile_entry = NULL; |
| 3256 | struct cafile_entry *new_crlfile_entry = NULL; |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3257 | char *err = NULL; |
| 3258 | int errcode = 0; |
| 3259 | struct buffer *buf; |
| 3260 | |
| 3261 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 3262 | return 1; |
| 3263 | |
| 3264 | if (!*args[3] || !payload) |
Remi Tricot-Le Breton | 444d702 | 2022-05-05 17:18:40 +0200 | [diff] [blame] | 3265 | return cli_err(appctx, "'set ssl crl-file expects a filename and CRLs as a payload\n"); |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3266 | |
| 3267 | /* The operations on the CKCH architecture are locked so we can |
| 3268 | * manipulate ckch_store and ckch_inst */ |
| 3269 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 3270 | return cli_err(appctx, "Can't update the CRL file!\nOperations on certificates are currently locked!\n"); |
| 3271 | |
| 3272 | if ((buf = alloc_trash_chunk()) == NULL) { |
| 3273 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 3274 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3275 | goto end; |
| 3276 | } |
| 3277 | |
| 3278 | if (!chunk_strcpy(buf, args[3])) { |
| 3279 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 3280 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3281 | goto end; |
| 3282 | } |
| 3283 | |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3284 | old_crlfile_entry = NULL; |
| 3285 | new_crlfile_entry = NULL; |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3286 | |
| 3287 | /* if there is an ongoing transaction */ |
| 3288 | if (crlfile_transaction.path) { |
| 3289 | /* if there is an ongoing transaction, check if this is the same file */ |
| 3290 | if (strcmp(crlfile_transaction.path, buf->area) != 0) { |
| 3291 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, buf->area); |
| 3292 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3293 | goto end; |
| 3294 | } |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3295 | old_crlfile_entry = crlfile_transaction.old_crlfile_entry; |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3296 | } |
| 3297 | else { |
| 3298 | /* lookup for the certificate in the tree */ |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3299 | old_crlfile_entry = ssl_store_get_cafile_entry(buf->area, 0); |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3300 | } |
| 3301 | |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3302 | if (!old_crlfile_entry) { |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3303 | memprintf(&err, "%sCan't replace a CRL file which is not referenced by the configuration!\n", |
| 3304 | err ? err : ""); |
| 3305 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3306 | goto end; |
| 3307 | } |
| 3308 | |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3309 | /* Create a new cafile_entry without adding it to the cafile tree. */ |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3310 | new_crlfile_entry = ssl_store_create_cafile_entry(old_crlfile_entry->path, NULL, CAFILE_CRL); |
| 3311 | if (!new_crlfile_entry) { |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3312 | memprintf(&err, "%sCannot allocate memory!\n", err ? err : ""); |
| 3313 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3314 | goto end; |
| 3315 | } |
| 3316 | |
| 3317 | /* Fill the new entry with the new CRL. */ |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3318 | if (ssl_store_load_ca_from_buf(new_crlfile_entry, payload)) { |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3319 | memprintf(&err, "%sInvalid payload\n", err ? err : ""); |
| 3320 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3321 | goto end; |
| 3322 | } |
| 3323 | |
| 3324 | /* we succeed, we can save the crl in the transaction */ |
| 3325 | |
Remi Tricot-Le Breton | 444d702 | 2022-05-05 17:18:40 +0200 | [diff] [blame] | 3326 | /* if there wasn't a transaction, update the old CRL */ |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3327 | if (!crlfile_transaction.old_crlfile_entry) { |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3328 | crlfile_transaction.old_crlfile_entry = old_crlfile_entry; |
| 3329 | crlfile_transaction.path = old_crlfile_entry->path; |
Remi Tricot-Le Breton | 444d702 | 2022-05-05 17:18:40 +0200 | [diff] [blame] | 3330 | err = memprintf(&err, "transaction created for CRL %s!\n", crlfile_transaction.path); |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3331 | } else { |
Remi Tricot-Le Breton | 444d702 | 2022-05-05 17:18:40 +0200 | [diff] [blame] | 3332 | err = memprintf(&err, "transaction updated for CRL %s!\n", crlfile_transaction.path); |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | /* free the previous CRL file if there was a transaction */ |
| 3336 | ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry); |
| 3337 | |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3338 | crlfile_transaction.new_crlfile_entry = new_crlfile_entry; |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3339 | |
| 3340 | /* creates the SNI ctxs later in the IO handler */ |
| 3341 | |
| 3342 | end: |
| 3343 | free_trash_chunk(buf); |
| 3344 | |
| 3345 | if (errcode & ERR_CODE) { |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3346 | ssl_store_delete_cafile_entry(new_crlfile_entry); |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3347 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3348 | return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3])); |
| 3349 | } else { |
| 3350 | |
| 3351 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3352 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 3353 | } |
| 3354 | } |
| 3355 | |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 3356 | /* Parsing function of 'commit ssl crl-file'. |
| 3357 | * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl ca-file". |
| 3358 | */ |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3359 | static int cli_parse_commit_crlfile(char **args, char *payload, struct appctx *appctx, void *private) |
| 3360 | { |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 3361 | struct commit_cacrlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3362 | char *err = NULL; |
| 3363 | |
| 3364 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 3365 | return 1; |
| 3366 | |
| 3367 | if (!*args[3]) |
| 3368 | return cli_err(appctx, "'commit ssl ca-file expects a filename\n"); |
| 3369 | |
| 3370 | /* The operations on the CKCH architecture are locked so we can |
| 3371 | * manipulate ckch_store and ckch_inst */ |
| 3372 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 3373 | return cli_err(appctx, "Can't commit the CRL file!\nOperations on certificates are currently locked!\n"); |
| 3374 | |
| 3375 | if (!crlfile_transaction.path) { |
| 3376 | memprintf(&err, "No ongoing transaction! !\n"); |
| 3377 | goto error; |
| 3378 | } |
| 3379 | |
| 3380 | if (strcmp(crlfile_transaction.path, args[3]) != 0) { |
| 3381 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, args[3]); |
| 3382 | goto error; |
| 3383 | } |
| 3384 | /* init the appctx structure */ |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 3385 | ctx->state = CACRL_ST_INIT; |
Christopher Faulet | f814c4a | 2022-06-03 11:32:05 +0200 | [diff] [blame] | 3386 | ctx->next_ckchi_link = NULL; |
Christopher Faulet | 6af2fc6 | 2022-06-03 11:42:38 +0200 | [diff] [blame] | 3387 | ctx->old_entry = crlfile_transaction.old_crlfile_entry; |
| 3388 | ctx->new_entry = crlfile_transaction.new_crlfile_entry; |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 3389 | ctx->cafile_type = CAFILE_CRL; |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3390 | |
| 3391 | return 0; |
| 3392 | |
| 3393 | error: |
| 3394 | |
| 3395 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3396 | err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]); |
| 3397 | |
| 3398 | return cli_dynerr(appctx, err); |
| 3399 | } |
| 3400 | |
| 3401 | |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 3402 | /* release function of the `commit ssl crl-file' command, free things and unlock the spinlock. |
| 3403 | * it uses a commit_cacrlfile_ctx that's the same as for "commit ssl ca-file". |
| 3404 | */ |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3405 | static void cli_release_commit_crlfile(struct appctx *appctx) |
| 3406 | { |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 3407 | struct commit_cacrlfile_ctx *ctx = appctx->svcctx; |
Christopher Faulet | 6af2fc6 | 2022-06-03 11:42:38 +0200 | [diff] [blame] | 3408 | struct cafile_entry *new_crlfile_entry = ctx->new_entry; |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 3409 | |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 3410 | /* Remove the uncommitted cafile_entry from the tree. */ |
| 3411 | if (new_crlfile_entry) { |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3412 | ebmb_delete(&new_crlfile_entry->node); |
| 3413 | ssl_store_delete_cafile_entry(new_crlfile_entry); |
| 3414 | } |
| 3415 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 3416 | ha_free(&ctx->err); |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3417 | } |
| 3418 | |
Remi Tricot-Le Breton | 720e3b9 | 2021-04-26 11:00:42 +0200 | [diff] [blame] | 3419 | /* parsing function of 'del ssl crl-file' */ |
| 3420 | static int cli_parse_del_crlfile(char **args, char *payload, struct appctx *appctx, void *private) |
| 3421 | { |
| 3422 | struct cafile_entry *cafile_entry; |
| 3423 | char *err = NULL; |
| 3424 | char *filename; |
| 3425 | |
| 3426 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 3427 | return 1; |
| 3428 | |
| 3429 | if (!*args[3]) |
| 3430 | return cli_err(appctx, "'del ssl crl-file' expects a CRL file name\n"); |
| 3431 | |
| 3432 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 3433 | return cli_err(appctx, "Can't delete the CRL file!\nOperations on certificates are currently locked!\n"); |
| 3434 | |
| 3435 | filename = args[3]; |
| 3436 | |
Christopher Faulet | 1f08fa4 | 2022-05-31 18:06:30 +0200 | [diff] [blame] | 3437 | if (crlfile_transaction.path && strcmp(crlfile_transaction.path, filename) == 0) { |
| 3438 | memprintf(&err, "ongoing transaction for the CRL file '%s'", filename); |
| 3439 | goto error; |
| 3440 | } |
| 3441 | |
Remi Tricot-Le Breton | 720e3b9 | 2021-04-26 11:00:42 +0200 | [diff] [blame] | 3442 | cafile_entry = ssl_store_get_cafile_entry(filename, 0); |
| 3443 | if (!cafile_entry) { |
| 3444 | memprintf(&err, "CRL file '%s' doesn't exist!\n", filename); |
| 3445 | goto error; |
| 3446 | } |
| 3447 | if (cafile_entry->type != CAFILE_CRL) { |
| 3448 | memprintf(&err, "'del ssl crl-file' does not work on CA files!\n"); |
| 3449 | goto error; |
| 3450 | } |
| 3451 | |
| 3452 | if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) { |
| 3453 | memprintf(&err, "CRL file '%s' in use, can't be deleted!\n", filename); |
| 3454 | goto error; |
| 3455 | } |
| 3456 | |
| 3457 | /* Remove the cafile_entry from the tree */ |
| 3458 | ebmb_delete(&cafile_entry->node); |
| 3459 | ssl_store_delete_cafile_entry(cafile_entry); |
| 3460 | |
| 3461 | memprintf(&err, "CRL file '%s' deleted!\n", filename); |
| 3462 | |
| 3463 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3464 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 3465 | |
| 3466 | error: |
| 3467 | memprintf(&err, "Can't remove the CRL file: %s\n", err ? err : ""); |
| 3468 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3469 | return cli_dynerr(appctx, err); |
| 3470 | } |
| 3471 | |
Remi Tricot-Le Breton | eef8e7b | 2021-04-20 17:42:02 +0200 | [diff] [blame] | 3472 | /* parsing function of 'abort ssl crl-file' */ |
| 3473 | static int cli_parse_abort_crlfile(char **args, char *payload, struct appctx *appctx, void *private) |
| 3474 | { |
| 3475 | char *err = NULL; |
| 3476 | |
| 3477 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 3478 | return 1; |
| 3479 | |
| 3480 | if (!*args[3]) |
| 3481 | return cli_err(appctx, "'abort ssl crl-file' expects a filename\n"); |
| 3482 | |
| 3483 | /* The operations on the CKCH architecture are locked so we can |
| 3484 | * manipulate ckch_store and ckch_inst */ |
| 3485 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 3486 | return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n"); |
| 3487 | |
| 3488 | if (!crlfile_transaction.path) { |
| 3489 | memprintf(&err, "No ongoing transaction!\n"); |
| 3490 | goto error; |
| 3491 | } |
| 3492 | |
| 3493 | if (strcmp(crlfile_transaction.path, args[3]) != 0) { |
| 3494 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", crlfile_transaction.path, args[3]); |
| 3495 | goto error; |
| 3496 | } |
| 3497 | |
| 3498 | /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */ |
| 3499 | ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry); |
| 3500 | crlfile_transaction.new_crlfile_entry = NULL; |
| 3501 | crlfile_transaction.old_crlfile_entry = NULL; |
Christopher Faulet | 1e00c7e | 2022-05-31 18:10:19 +0200 | [diff] [blame] | 3502 | crlfile_transaction.path = NULL; |
Remi Tricot-Le Breton | eef8e7b | 2021-04-20 17:42:02 +0200 | [diff] [blame] | 3503 | |
| 3504 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3505 | |
| 3506 | err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]); |
| 3507 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 3508 | |
| 3509 | error: |
| 3510 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3511 | |
| 3512 | return cli_dynerr(appctx, err); |
| 3513 | } |
| 3514 | |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3515 | |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3516 | /* |
| 3517 | * Display a Certificate Resignation List's information. |
| 3518 | * The information displayed is inspired by the output of 'openssl crl -in |
| 3519 | * crl.pem -text'. |
| 3520 | * Returns 0 in case of success. |
| 3521 | */ |
| 3522 | static int show_crl_detail(X509_CRL *crl, struct buffer *out) |
| 3523 | { |
| 3524 | BIO *bio = NULL; |
| 3525 | struct buffer *tmp = alloc_trash_chunk(); |
| 3526 | long version; |
| 3527 | X509_NAME *issuer; |
| 3528 | int write = -1; |
| 3529 | STACK_OF(X509_REVOKED) *rev = NULL; |
| 3530 | X509_REVOKED *rev_entry = NULL; |
| 3531 | int i; |
| 3532 | |
| 3533 | if (!tmp) |
| 3534 | return -1; |
| 3535 | |
| 3536 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 3537 | goto end; |
| 3538 | |
| 3539 | /* Version (as displayed by 'openssl crl') */ |
| 3540 | version = X509_CRL_get_version(crl); |
| 3541 | chunk_appendf(out, "Version %ld\n", version + 1); |
| 3542 | |
| 3543 | /* Signature Algorithm */ |
| 3544 | chunk_appendf(out, "Signature Algorithm: %s\n", OBJ_nid2ln(X509_CRL_get_signature_nid(crl))); |
| 3545 | |
| 3546 | /* Issuer */ |
| 3547 | chunk_appendf(out, "Issuer: "); |
| 3548 | if ((issuer = X509_CRL_get_issuer(crl)) == NULL) |
| 3549 | goto end; |
| 3550 | if ((ssl_sock_get_dn_oneline(issuer, tmp)) == -1) |
| 3551 | goto end; |
| 3552 | *(tmp->area + tmp->data) = '\0'; |
| 3553 | chunk_appendf(out, "%s\n", tmp->area); |
| 3554 | |
| 3555 | /* Last Update */ |
| 3556 | chunk_appendf(out, "Last Update: "); |
| 3557 | chunk_reset(tmp); |
Remi Tricot-Le Breton | d75b99e | 2021-05-17 11:45:55 +0200 | [diff] [blame] | 3558 | if (BIO_reset(bio) == -1) |
| 3559 | goto end; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3560 | if (ASN1_TIME_print(bio, X509_CRL_get0_lastUpdate(crl)) == 0) |
| 3561 | goto end; |
| 3562 | write = BIO_read(bio, tmp->area, tmp->size-1); |
| 3563 | tmp->area[write] = '\0'; |
| 3564 | chunk_appendf(out, "%s\n", tmp->area); |
| 3565 | |
| 3566 | |
| 3567 | /* Next Update */ |
| 3568 | chunk_appendf(out, "Next Update: "); |
| 3569 | chunk_reset(tmp); |
Remi Tricot-Le Breton | d75b99e | 2021-05-17 11:45:55 +0200 | [diff] [blame] | 3570 | if (BIO_reset(bio) == -1) |
| 3571 | goto end; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3572 | if (ASN1_TIME_print(bio, X509_CRL_get0_nextUpdate(crl)) == 0) |
| 3573 | goto end; |
| 3574 | write = BIO_read(bio, tmp->area, tmp->size-1); |
| 3575 | tmp->area[write] = '\0'; |
| 3576 | chunk_appendf(out, "%s\n", tmp->area); |
| 3577 | |
| 3578 | |
| 3579 | /* Revoked Certificates */ |
| 3580 | rev = X509_CRL_get_REVOKED(crl); |
| 3581 | if (sk_X509_REVOKED_num(rev) > 0) |
| 3582 | chunk_appendf(out, "Revoked Certificates:\n"); |
| 3583 | else |
| 3584 | chunk_appendf(out, "No Revoked Certificates.\n"); |
| 3585 | |
| 3586 | for (i = 0; i < sk_X509_REVOKED_num(rev); i++) { |
| 3587 | rev_entry = sk_X509_REVOKED_value(rev, i); |
| 3588 | |
| 3589 | /* Serial Number and Revocation Date */ |
Remi Tricot-Le Breton | d75b99e | 2021-05-17 11:45:55 +0200 | [diff] [blame] | 3590 | if (BIO_reset(bio) == -1) |
| 3591 | goto end; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3592 | BIO_printf(bio , " Serial Number: "); |
Remi Tricot-Le Breton | 18c7d83 | 2021-05-17 18:38:34 +0200 | [diff] [blame] | 3593 | i2a_ASN1_INTEGER(bio, (ASN1_INTEGER*)X509_REVOKED_get0_serialNumber(rev_entry)); |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3594 | BIO_printf(bio, "\n Revocation Date: "); |
Remi Tricot-Le Breton | a6b2784 | 2021-05-18 10:06:00 +0200 | [diff] [blame] | 3595 | if (ASN1_TIME_print(bio, X509_REVOKED_get0_revocationDate(rev_entry)) == 0) |
| 3596 | goto end; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3597 | BIO_printf(bio, "\n"); |
| 3598 | |
| 3599 | write = BIO_read(bio, tmp->area, tmp->size-1); |
| 3600 | tmp->area[write] = '\0'; |
| 3601 | chunk_appendf(out, "%s", tmp->area); |
| 3602 | } |
| 3603 | |
| 3604 | end: |
| 3605 | free_trash_chunk(tmp); |
| 3606 | if (bio) |
| 3607 | BIO_free(bio); |
| 3608 | |
| 3609 | return 0; |
| 3610 | } |
| 3611 | |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 3612 | /* IO handler of details "show ssl crl-file <filename[:index]>". |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3613 | * It uses show_crlfile_ctx and the global |
| 3614 | * crlfile_transaction.new_cafile_entry in read-only. |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 3615 | */ |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3616 | static int cli_io_handler_show_crlfile_detail(struct appctx *appctx) |
| 3617 | { |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3618 | struct show_crlfile_ctx *ctx = appctx->svcctx; |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3619 | struct cafile_entry *cafile_entry = ctx->cafile_entry; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3620 | struct buffer *out = alloc_trash_chunk(); |
| 3621 | int i; |
| 3622 | X509_CRL *crl; |
| 3623 | STACK_OF(X509_OBJECT) *objs; |
| 3624 | int retval = 0; |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3625 | int index = ctx->index; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3626 | |
| 3627 | if (!out) |
| 3628 | goto end_no_putchk; |
| 3629 | |
| 3630 | chunk_appendf(out, "Filename: "); |
| 3631 | if (cafile_entry == crlfile_transaction.new_crlfile_entry) |
| 3632 | chunk_appendf(out, "*"); |
| 3633 | chunk_appendf(out, "%s\n", cafile_entry->path); |
| 3634 | |
| 3635 | chunk_appendf(out, "Status: "); |
| 3636 | if (!cafile_entry->ca_store) |
| 3637 | chunk_appendf(out, "Empty\n"); |
| 3638 | else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) |
| 3639 | chunk_appendf(out, "Unused\n"); |
| 3640 | else |
| 3641 | chunk_appendf(out, "Used\n"); |
| 3642 | |
| 3643 | if (!cafile_entry->ca_store) |
| 3644 | goto end; |
| 3645 | |
| 3646 | objs = X509_STORE_get0_objects(cafile_entry->ca_store); |
| 3647 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 3648 | crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i)); |
| 3649 | if (!crl) |
| 3650 | continue; |
| 3651 | |
| 3652 | /* CRL indexes start at 1 on the CLI output. */ |
| 3653 | if (index && index-1 != i) |
| 3654 | continue; |
| 3655 | |
Remi Tricot-Le Breton | e8041fe | 2022-04-05 16:44:21 +0200 | [diff] [blame] | 3656 | chunk_appendf(out, " \nCertificate Revocation List #%d:\n", i+1); |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3657 | retval = show_crl_detail(crl, out); |
| 3658 | if (retval < 0) |
| 3659 | goto end_no_putchk; |
| 3660 | else if (retval || index) |
| 3661 | goto end; |
| 3662 | } |
| 3663 | |
| 3664 | end: |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 3665 | if (applet_putchk(appctx, out) == -1) |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3666 | goto yield; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3667 | |
| 3668 | end_no_putchk: |
| 3669 | free_trash_chunk(out); |
| 3670 | return 1; |
| 3671 | yield: |
| 3672 | free_trash_chunk(out); |
| 3673 | return 0; /* should come back */ |
| 3674 | } |
| 3675 | |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 3676 | /* parsing function for 'show ssl crl-file [crlfile[:index]]'. |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3677 | * It sets the context to a show_crlfile_ctx, and the global |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 3678 | * cafile_transaction.new_crlfile_entry under the ckch_lock. |
| 3679 | */ |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3680 | static int cli_parse_show_crlfile(char **args, char *payload, struct appctx *appctx, void *private) |
| 3681 | { |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3682 | struct show_crlfile_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3683 | struct cafile_entry *cafile_entry; |
| 3684 | long index = 0; |
| 3685 | char *colons; |
| 3686 | char *err = NULL; |
| 3687 | |
| 3688 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 3689 | return cli_err(appctx, "Can't allocate memory!\n"); |
| 3690 | |
| 3691 | /* The operations on the CKCH architecture are locked so we can |
| 3692 | * manipulate ckch_store and ckch_inst */ |
| 3693 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 3694 | return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n"); |
| 3695 | |
| 3696 | /* check if there is a certificate to lookup */ |
| 3697 | if (*args[3]) { |
| 3698 | |
| 3699 | /* Look for an optional index after the CRL file name */ |
| 3700 | colons = strchr(args[3], ':'); |
| 3701 | if (colons) { |
| 3702 | char *endptr; |
| 3703 | |
| 3704 | index = strtol(colons + 1, &endptr, 10); |
| 3705 | /* Indexes start at 1 */ |
| 3706 | if (colons + 1 == endptr || *endptr != '\0' || index <= 0) { |
| 3707 | memprintf(&err, "wrong CRL index after colons in '%s'!", args[3]); |
| 3708 | goto error; |
| 3709 | } |
| 3710 | *colons = '\0'; |
| 3711 | } |
| 3712 | |
| 3713 | if (*args[3] == '*') { |
| 3714 | if (!crlfile_transaction.new_crlfile_entry) |
| 3715 | goto error; |
| 3716 | |
| 3717 | cafile_entry = crlfile_transaction.new_crlfile_entry; |
| 3718 | |
| 3719 | if (strcmp(args[3] + 1, cafile_entry->path) != 0) |
| 3720 | goto error; |
| 3721 | |
| 3722 | } else { |
| 3723 | /* Get the "original" cafile_entry and not the |
| 3724 | * uncommitted one if it exists. */ |
| 3725 | if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CRL) |
| 3726 | goto error; |
| 3727 | } |
| 3728 | |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3729 | ctx->cafile_entry = cafile_entry; |
| 3730 | ctx->index = index; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3731 | /* use the IO handler that shows details */ |
| 3732 | appctx->io_handler = cli_io_handler_show_crlfile_detail; |
| 3733 | } |
| 3734 | |
| 3735 | return 0; |
| 3736 | |
| 3737 | error: |
| 3738 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3739 | if (err) |
| 3740 | return cli_dynerr(appctx, err); |
Remi Tricot-Le Breton | 444d702 | 2022-05-05 17:18:40 +0200 | [diff] [blame] | 3741 | return cli_err(appctx, "Can't display the CRL file : Not found!\n"); |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3742 | } |
| 3743 | |
| 3744 | /* IO handler of "show ssl crl-file". The command taking a specific CRL file name |
| 3745 | * is managed in cli_io_handler_show_crlfile_detail. */ |
| 3746 | static int cli_io_handler_show_crlfile(struct appctx *appctx) |
| 3747 | { |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3748 | struct show_crlfile_ctx *ctx = appctx->svcctx; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3749 | struct buffer *trash = alloc_trash_chunk(); |
| 3750 | struct ebmb_node *node; |
Christopher Faulet | 88041b3 | 2022-06-03 16:26:56 +0200 | [diff] [blame] | 3751 | struct cafile_entry *cafile_entry = NULL; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3752 | |
| 3753 | if (trash == NULL) |
| 3754 | return 1; |
| 3755 | |
Christopher Faulet | 9a99e54 | 2022-06-03 10:32:18 +0200 | [diff] [blame] | 3756 | if (!ctx->old_crlfile_entry && crlfile_transaction.old_crlfile_entry) { |
| 3757 | chunk_appendf(trash, "# transaction\n"); |
| 3758 | chunk_appendf(trash, "*%s\n", crlfile_transaction.old_crlfile_entry->path); |
| 3759 | if (applet_putchk(appctx, trash) == -1) |
| 3760 | goto yield; |
| 3761 | ctx->old_crlfile_entry = crlfile_transaction.old_crlfile_entry; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | /* First time in this io_handler. */ |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3765 | if (!ctx->cafile_entry) { |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3766 | chunk_appendf(trash, "# filename\n"); |
| 3767 | node = ebmb_first(&cafile_tree); |
| 3768 | } else { |
| 3769 | /* We yielded during a previous call. */ |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3770 | node = &ctx->cafile_entry->node; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3771 | } |
| 3772 | |
| 3773 | while (node) { |
| 3774 | cafile_entry = ebmb_entry(node, struct cafile_entry, node); |
| 3775 | if (cafile_entry->type == CAFILE_CRL) { |
| 3776 | chunk_appendf(trash, "%s\n", cafile_entry->path); |
| 3777 | } |
| 3778 | |
| 3779 | node = ebmb_next(node); |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 3780 | if (applet_putchk(appctx, trash) == -1) |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3781 | goto yield; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3782 | } |
| 3783 | |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3784 | ctx->cafile_entry = NULL; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3785 | free_trash_chunk(trash); |
| 3786 | return 1; |
| 3787 | yield: |
| 3788 | |
| 3789 | free_trash_chunk(trash); |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3790 | ctx->cafile_entry = cafile_entry; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3791 | return 0; /* should come back */ |
| 3792 | } |
| 3793 | |
| 3794 | |
| 3795 | /* release function of the 'show ssl crl-file' command */ |
| 3796 | static void cli_release_show_crlfile(struct appctx *appctx) |
| 3797 | { |
| 3798 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3799 | } |
| 3800 | |
| 3801 | |
William Lallemand | ee8530c | 2020-06-23 18:19:42 +0200 | [diff] [blame] | 3802 | void ckch_deinit() |
| 3803 | { |
| 3804 | struct eb_node *node, *next; |
| 3805 | struct ckch_store *store; |
William Lallemand | b0c4827 | 2022-04-26 15:44:53 +0200 | [diff] [blame] | 3806 | struct ebmb_node *canode; |
William Lallemand | ee8530c | 2020-06-23 18:19:42 +0200 | [diff] [blame] | 3807 | |
William Lallemand | b0c4827 | 2022-04-26 15:44:53 +0200 | [diff] [blame] | 3808 | /* deinit the ckch stores */ |
William Lallemand | ee8530c | 2020-06-23 18:19:42 +0200 | [diff] [blame] | 3809 | node = eb_first(&ckchs_tree); |
| 3810 | while (node) { |
| 3811 | next = eb_next(node); |
| 3812 | store = ebmb_entry(node, struct ckch_store, node); |
| 3813 | ckch_store_free(store); |
| 3814 | node = next; |
| 3815 | } |
William Lallemand | b0c4827 | 2022-04-26 15:44:53 +0200 | [diff] [blame] | 3816 | |
| 3817 | /* deinit the ca-file store */ |
| 3818 | canode = ebmb_first(&cafile_tree); |
| 3819 | while (canode) { |
| 3820 | struct cafile_entry *entry = NULL; |
| 3821 | |
| 3822 | entry = ebmb_entry(canode, struct cafile_entry, node); |
| 3823 | canode = ebmb_next(canode); |
| 3824 | ssl_store_delete_cafile_entry(entry); |
| 3825 | } |
William Lallemand | ee8530c | 2020-06-23 18:19:42 +0200 | [diff] [blame] | 3826 | } |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 3827 | |
| 3828 | /* register cli keywords */ |
| 3829 | static struct cli_kw_list cli_kws = {{ },{ |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 3830 | { { "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 }, |
| 3831 | { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL }, |
| 3832 | { { "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 }, |
| 3833 | { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL }, |
| 3834 | { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL }, |
| 3835 | { { "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 }, |
| 3836 | |
Amaury Denoyelle | b11ad9e | 2021-05-21 11:01:10 +0200 | [diff] [blame] | 3837 | { { "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 }, |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 3838 | { { "set", "ssl", "ca-file", NULL }, "set ssl ca-file <cafile> <payload> : replace a CA file", cli_parse_set_cafile, NULL, NULL }, |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3839 | { { "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 Breton | d5fd09d | 2021-03-11 10:22:52 +0100 | [diff] [blame] | 3840 | { { "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 Breton | c3a8477 | 2021-03-25 18:13:57 +0100 | [diff] [blame] | 3841 | { { "del", "ssl", "ca-file", NULL }, "del ssl ca-file <cafile> : delete an unused CA file", cli_parse_del_cafile, NULL, NULL }, |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3842 | { { "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 Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3843 | |
Remi Tricot-Le Breton | 720e3b9 | 2021-04-26 11:00:42 +0200 | [diff] [blame] | 3844 | { { "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 Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3845 | { { "set", "ssl", "crl-file", NULL }, "set ssl crl-file <crlfile> <payload> : replace a CRL file", cli_parse_set_crlfile, NULL, NULL }, |
| 3846 | { { "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 Breton | eef8e7b | 2021-04-20 17:42:02 +0200 | [diff] [blame] | 3847 | { { "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 Breton | 720e3b9 | 2021-04-26 11:00:42 +0200 | [diff] [blame] | 3848 | { { "del", "ssl", "crl-file", NULL }, "del ssl crl-file <crlfile> : delete an unused CRL file", cli_parse_del_crlfile, NULL, NULL }, |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3849 | { { "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 Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 3850 | { { NULL }, NULL, NULL, NULL } |
| 3851 | }}; |
| 3852 | |
| 3853 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 3854 | |