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 | |
Remi Tricot-Le Breton | 0bb4824 | 2021-04-16 17:59:23 +0200 | [diff] [blame] | 1155 | 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] | 1156 | { |
| 1157 | X509_STORE *store = ssl_store_get0_locations_file(path); |
| 1158 | |
| 1159 | /* If this function is called by the CLI, we should not call the |
| 1160 | * X509_STORE_load_locations function because it performs forbidden disk |
| 1161 | * accesses. */ |
| 1162 | if (!store && create_if_none) { |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1163 | STACK_OF(X509_OBJECT) *objs; |
| 1164 | int cert_count = 0; |
| 1165 | struct stat buf; |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1166 | struct cafile_entry *ca_e; |
William Lallemand | c6b1763 | 2022-04-01 23:39:37 +0200 | [diff] [blame] | 1167 | const char *file = NULL; |
| 1168 | const char *dir = NULL; |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1169 | |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1170 | store = X509_STORE_new(); |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1171 | |
William Lallemand | c6b1763 | 2022-04-01 23:39:37 +0200 | [diff] [blame] | 1172 | if (strcmp(path, "@system-ca") == 0) { |
| 1173 | dir = X509_get_default_cert_dir(); |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1174 | |
William Lallemand | c6b1763 | 2022-04-01 23:39:37 +0200 | [diff] [blame] | 1175 | } else { |
| 1176 | |
| 1177 | if (stat(path, &buf)) |
| 1178 | goto err; |
| 1179 | |
| 1180 | if (S_ISDIR(buf.st_mode)) |
| 1181 | dir = path; |
| 1182 | else |
| 1183 | file = path; |
| 1184 | } |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1185 | |
| 1186 | if (file) { |
| 1187 | if (!X509_STORE_load_locations(store, file, NULL)) { |
| 1188 | goto err; |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1189 | } |
William Lallemand | 80296b4 | 2022-04-05 10:19:30 +0200 | [diff] [blame] | 1190 | } else if (dir) { |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1191 | int n, i; |
| 1192 | struct dirent **de_list; |
| 1193 | |
| 1194 | n = scandir(dir, &de_list, 0, alphasort); |
| 1195 | if (n < 0) |
| 1196 | goto err; |
| 1197 | |
| 1198 | for (i= 0; i < n; i++) { |
| 1199 | char *end; |
| 1200 | struct dirent *de = de_list[i]; |
| 1201 | BIO *in = NULL; |
| 1202 | X509 *ca = NULL;; |
| 1203 | |
| 1204 | /* we try to load the files that would have |
| 1205 | * been loaded in an hashed directory loaded by |
| 1206 | * X509_LOOKUP_hash_dir, so according to "man 1 |
| 1207 | * c_rehash", we should load ".pem", ".crt", |
William Lallemand | e4b93eb | 2022-05-09 09:29:00 +0200 | [diff] [blame] | 1208 | * ".cer", or ".crl". Files starting with a dot |
| 1209 | * are ignored. |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1210 | */ |
| 1211 | end = strrchr(de->d_name, '.'); |
William Lallemand | e4b93eb | 2022-05-09 09:29:00 +0200 | [diff] [blame] | 1212 | if (!end || de->d_name[0] == '.' || |
| 1213 | (strcmp(end, ".pem") != 0 && |
| 1214 | strcmp(end, ".crt") != 0 && |
| 1215 | strcmp(end, ".cer") != 0 && |
| 1216 | strcmp(end, ".crl") != 0)) { |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1217 | free(de); |
| 1218 | continue; |
| 1219 | } |
| 1220 | in = BIO_new(BIO_s_file()); |
| 1221 | if (in == NULL) |
| 1222 | goto scandir_err; |
| 1223 | |
William Lallemand | c6b1763 | 2022-04-01 23:39:37 +0200 | [diff] [blame] | 1224 | chunk_printf(&trash, "%s/%s", dir, de->d_name); |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1225 | |
| 1226 | if (BIO_read_filename(in, trash.area) == 0) |
| 1227 | goto scandir_err; |
| 1228 | |
| 1229 | if (PEM_read_bio_X509_AUX(in, &ca, NULL, NULL) == NULL) |
| 1230 | goto scandir_err; |
| 1231 | |
| 1232 | if (X509_STORE_add_cert(store, ca) == 0) |
| 1233 | goto scandir_err; |
| 1234 | |
William Lallemand | 4cfbf3c | 2022-04-26 15:57:33 +0200 | [diff] [blame] | 1235 | X509_free(ca); |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1236 | BIO_free(in); |
| 1237 | free(de); |
| 1238 | continue; |
| 1239 | |
| 1240 | scandir_err: |
William Lallemand | 4cfbf3c | 2022-04-26 15:57:33 +0200 | [diff] [blame] | 1241 | X509_free(ca); |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1242 | BIO_free(in); |
| 1243 | free(de); |
| 1244 | ha_warning("ca-file: '%s' couldn't load '%s'\n", path, trash.area); |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1245 | |
| 1246 | } |
| 1247 | free(de_list); |
William Lallemand | 80296b4 | 2022-04-05 10:19:30 +0200 | [diff] [blame] | 1248 | } else { |
| 1249 | ha_alert("ca-file: couldn't load '%s'\n", path); |
| 1250 | goto err; |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1251 | } |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1252 | |
| 1253 | objs = X509_STORE_get0_objects(store); |
| 1254 | cert_count = sk_X509_OBJECT_num(objs); |
| 1255 | if (cert_count == 0) |
| 1256 | ha_warning("ca-file: 0 CA were loaded from '%s'\n", path); |
| 1257 | |
| 1258 | ca_e = ssl_store_create_cafile_entry(path, store, type); |
| 1259 | if (!ca_e) |
| 1260 | goto err; |
| 1261 | ebst_insert(&cafile_tree, &ca_e->node); |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1262 | } |
| 1263 | return (store != NULL); |
William Lallemand | 87fd994 | 2022-04-01 20:12:03 +0200 | [diff] [blame] | 1264 | |
| 1265 | err: |
| 1266 | X509_STORE_free(store); |
| 1267 | store = NULL; |
| 1268 | return 0; |
| 1269 | |
Remi Tricot-Le Breton | af8820a | 2021-04-13 10:10:37 +0200 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1273 | /*************************** CLI commands ***********************/ |
| 1274 | |
| 1275 | /* Type of SSL payloads that can be updated over the CLI */ |
| 1276 | |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 1277 | struct cert_exts cert_exts[] = { |
| 1278 | { "", 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] | 1279 | { "crt", CERT_TYPE_CRT, &ssl_sock_load_pem_into_ckch }, |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 1280 | { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch }, |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1281 | #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] | 1282 | { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file }, |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1283 | #endif |
Ilya Shipitsin | c47d676 | 2021-02-13 11:45:33 +0500 | [diff] [blame] | 1284 | #ifdef HAVE_SSL_SCTL |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 1285 | { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file }, |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1286 | #endif |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 1287 | { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch }, |
| 1288 | { NULL, CERT_TYPE_MAX, NULL }, |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1289 | }; |
| 1290 | |
| 1291 | |
| 1292 | /* release function of the `show ssl cert' command */ |
| 1293 | static void cli_release_show_cert(struct appctx *appctx) |
| 1294 | { |
| 1295 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1296 | } |
| 1297 | |
Willy Tarreau | 4fd9b4d | 2022-05-04 16:11:50 +0200 | [diff] [blame] | 1298 | /* IO handler of "show ssl cert <filename>". |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1299 | * 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] | 1300 | */ |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1301 | static int cli_io_handler_show_cert(struct appctx *appctx) |
| 1302 | { |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1303 | struct show_cert_ctx *ctx = appctx->svcctx; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1304 | struct buffer *trash = alloc_trash_chunk(); |
| 1305 | struct ebmb_node *node; |
Christopher Faulet | d1d2e4d | 2022-06-03 16:24:02 +0200 | [diff] [blame] | 1306 | struct ckch_store *ckchs = NULL; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1307 | |
| 1308 | if (trash == NULL) |
| 1309 | return 1; |
| 1310 | |
Christopher Faulet | 3e94f5d | 2022-06-03 10:46:40 +0200 | [diff] [blame] | 1311 | if (!ctx->old_ckchs && ckchs_transaction.old_ckchs) { |
| 1312 | ckchs = ckchs_transaction.old_ckchs; |
| 1313 | chunk_appendf(trash, "# transaction\n"); |
| 1314 | chunk_appendf(trash, "*%s\n", ckchs->path); |
| 1315 | if (applet_putchk(appctx, trash) == -1) |
| 1316 | goto yield; |
| 1317 | ctx->old_ckchs = ckchs_transaction.old_ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1318 | } |
| 1319 | |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1320 | if (!ctx->cur_ckchs) { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1321 | chunk_appendf(trash, "# filename\n"); |
| 1322 | node = ebmb_first(&ckchs_tree); |
| 1323 | } else { |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1324 | node = &ctx->cur_ckchs->node; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1325 | } |
| 1326 | while (node) { |
| 1327 | ckchs = ebmb_entry(node, struct ckch_store, node); |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1328 | chunk_appendf(trash, "%s\n", ckchs->path); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1329 | |
| 1330 | node = ebmb_next(node); |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 1331 | if (applet_putchk(appctx, trash) == -1) |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1332 | goto yield; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1333 | } |
| 1334 | |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1335 | ctx->cur_ckchs = NULL; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1336 | free_trash_chunk(trash); |
| 1337 | return 1; |
| 1338 | yield: |
| 1339 | |
| 1340 | free_trash_chunk(trash); |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1341 | ctx->cur_ckchs = ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1342 | return 0; /* should come back */ |
| 1343 | } |
| 1344 | |
| 1345 | /* |
| 1346 | * Extract and format the DNS SAN extensions and copy result into a chuink |
| 1347 | * Return 0; |
| 1348 | */ |
| 1349 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1350 | static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out) |
| 1351 | { |
| 1352 | int i; |
| 1353 | char *str; |
| 1354 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 1355 | |
| 1356 | names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 1357 | if (names) { |
| 1358 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 1359 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 1360 | if (i > 0) |
| 1361 | chunk_appendf(out, ", "); |
| 1362 | if (name->type == GEN_DNS) { |
| 1363 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 1364 | chunk_appendf(out, "DNS:%s", str); |
| 1365 | OPENSSL_free(str); |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 1370 | } |
| 1371 | return 0; |
| 1372 | } |
| 1373 | #endif |
| 1374 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 1375 | /* |
| 1376 | * Build the ckch_inst_link that will be chained in the CA file entry and the |
| 1377 | * corresponding ckch_inst_link_ref that will be chained in the ckch instance. |
| 1378 | * Return 0 in case of success. |
| 1379 | */ |
| 1380 | static int do_chain_inst_and_cafile(struct cafile_entry *cafile_entry, struct ckch_inst *ckch_inst) |
| 1381 | { |
| 1382 | struct ckch_inst_link *new_link; |
| 1383 | if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) { |
| 1384 | struct ckch_inst_link *link = LIST_ELEM(cafile_entry->ckch_inst_link.n, |
| 1385 | typeof(link), list); |
| 1386 | /* Do not add multiple references to the same |
| 1387 | * instance in a cafile_entry */ |
| 1388 | if (link->ckch_inst == ckch_inst) { |
| 1389 | return 1; |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | new_link = calloc(1, sizeof(*new_link)); |
| 1394 | if (new_link) { |
| 1395 | struct ckch_inst_link_ref *new_link_ref = calloc(1, sizeof(*new_link_ref)); |
| 1396 | if (!new_link_ref) { |
| 1397 | free(new_link); |
| 1398 | return 1; |
| 1399 | } |
| 1400 | |
| 1401 | new_link->ckch_inst = ckch_inst; |
| 1402 | new_link_ref->link = new_link; |
| 1403 | LIST_INIT(&new_link->list); |
| 1404 | LIST_INIT(&new_link_ref->list); |
| 1405 | |
| 1406 | LIST_APPEND(&cafile_entry->ckch_inst_link, &new_link->list); |
| 1407 | LIST_APPEND(&ckch_inst->cafile_link_refs, &new_link_ref->list); |
| 1408 | } |
| 1409 | |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
| 1413 | |
| 1414 | /* |
| 1415 | * Link a CA file tree entry to the ckch instance that uses it. |
| 1416 | * To determine if and which CA file tree entries need to be linked to the |
| 1417 | * instance, we follow the same logic performed in ssl_sock_prepare_ctx when |
| 1418 | * processing the verify option. |
| 1419 | * This function works for a frontend as well as for a backend, depending on the |
| 1420 | * configuration parameters given (bind_conf or server). |
| 1421 | */ |
| 1422 | void ckch_inst_add_cafile_link(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf, |
| 1423 | struct ssl_bind_conf *ssl_conf, const struct server *srv) |
| 1424 | { |
| 1425 | int verify = SSL_VERIFY_NONE; |
| 1426 | |
| 1427 | if (srv) { |
| 1428 | |
| 1429 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 1430 | verify = SSL_VERIFY_PEER; |
| 1431 | switch (srv->ssl_ctx.verify) { |
| 1432 | case SSL_SOCK_VERIFY_NONE: |
| 1433 | verify = SSL_VERIFY_NONE; |
| 1434 | break; |
| 1435 | case SSL_SOCK_VERIFY_REQUIRED: |
| 1436 | verify = SSL_VERIFY_PEER; |
| 1437 | break; |
| 1438 | } |
| 1439 | } |
| 1440 | else { |
| 1441 | switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) { |
| 1442 | case SSL_SOCK_VERIFY_NONE: |
| 1443 | verify = SSL_VERIFY_NONE; |
| 1444 | break; |
| 1445 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 1446 | verify = SSL_VERIFY_PEER; |
| 1447 | break; |
| 1448 | case SSL_SOCK_VERIFY_REQUIRED: |
| 1449 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 1450 | break; |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | if (verify & SSL_VERIFY_PEER) { |
| 1455 | struct cafile_entry *ca_file_entry = NULL; |
| 1456 | struct cafile_entry *ca_verify_file_entry = NULL; |
Remi Tricot-Le Breton | f81c70c | 2021-04-20 16:54:21 +0200 | [diff] [blame] | 1457 | struct cafile_entry *crl_file_entry = NULL; |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 1458 | if (srv) { |
| 1459 | if (srv->ssl_ctx.ca_file) { |
| 1460 | ca_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.ca_file, 0); |
| 1461 | |
| 1462 | } |
Remi Tricot-Le Breton | f81c70c | 2021-04-20 16:54:21 +0200 | [diff] [blame] | 1463 | if (srv->ssl_ctx.crl_file) { |
| 1464 | crl_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.crl_file, 0); |
| 1465 | } |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 1466 | } |
| 1467 | else { |
| 1468 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 1469 | 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] | 1470 | 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] | 1471 | |
| 1472 | if (ca_file) |
| 1473 | ca_file_entry = ssl_store_get_cafile_entry(ca_file, 0); |
| 1474 | if (ca_verify_file) |
| 1475 | 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] | 1476 | if (crl_file) |
| 1477 | 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] | 1478 | } |
| 1479 | |
| 1480 | if (ca_file_entry) { |
| 1481 | /* If we have a ckch instance that is not already in the |
| 1482 | * cafile_entry's list, add it to it. */ |
| 1483 | if (do_chain_inst_and_cafile(ca_file_entry, ckch_inst)) |
| 1484 | return; |
| 1485 | |
| 1486 | } |
| 1487 | if (ca_verify_file_entry && (ca_file_entry != ca_verify_file_entry)) { |
| 1488 | /* If we have a ckch instance that is not already in the |
| 1489 | * cafile_entry's list, add it to it. */ |
| 1490 | if (do_chain_inst_and_cafile(ca_verify_file_entry, ckch_inst)) |
| 1491 | return; |
| 1492 | } |
Remi Tricot-Le Breton | f81c70c | 2021-04-20 16:54:21 +0200 | [diff] [blame] | 1493 | if (crl_file_entry) { |
| 1494 | /* If we have a ckch instance that is not already in the |
| 1495 | * cafile_entry's list, add it to it. */ |
| 1496 | if (do_chain_inst_and_cafile(crl_file_entry, ckch_inst)) |
| 1497 | return; |
| 1498 | } |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 1499 | } |
| 1500 | } |
| 1501 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1502 | |
| 1503 | |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1504 | 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] | 1505 | { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1506 | BIO *bio = NULL; |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1507 | struct buffer *tmp = alloc_trash_chunk(); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1508 | int i; |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1509 | int write = -1; |
| 1510 | unsigned int len = 0; |
| 1511 | X509_NAME *name = NULL; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1512 | |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1513 | if (!tmp) |
| 1514 | return -1; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1515 | |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1516 | if (!cert) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1517 | goto end; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1518 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1519 | if (chain == NULL) { |
| 1520 | struct issuer_chain *issuer; |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1521 | issuer = ssl_get0_issuer_chain(cert); |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1522 | if (issuer) { |
| 1523 | chain = issuer->chain; |
| 1524 | chunk_appendf(out, "Chain Filename: "); |
| 1525 | chunk_appendf(out, "%s\n", issuer->path); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1526 | } |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1527 | } |
| 1528 | chunk_appendf(out, "Serial: "); |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1529 | if (ssl_sock_get_serial(cert, tmp) == -1) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1530 | goto end; |
| 1531 | dump_binary(out, tmp->area, tmp->data); |
| 1532 | chunk_appendf(out, "\n"); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1533 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1534 | chunk_appendf(out, "notBefore: "); |
| 1535 | chunk_reset(tmp); |
| 1536 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 1537 | goto end; |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1538 | if (ASN1_TIME_print(bio, X509_getm_notBefore(cert)) == 0) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1539 | goto end; |
| 1540 | write = BIO_read(bio, tmp->area, tmp->size-1); |
| 1541 | tmp->area[write] = '\0'; |
| 1542 | BIO_free(bio); |
| 1543 | bio = NULL; |
| 1544 | chunk_appendf(out, "%s\n", tmp->area); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1545 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1546 | chunk_appendf(out, "notAfter: "); |
| 1547 | chunk_reset(tmp); |
| 1548 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 1549 | goto end; |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1550 | if (ASN1_TIME_print(bio, X509_getm_notAfter(cert)) == 0) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1551 | goto end; |
| 1552 | if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0) |
| 1553 | goto end; |
| 1554 | tmp->area[write] = '\0'; |
| 1555 | BIO_free(bio); |
| 1556 | bio = NULL; |
| 1557 | chunk_appendf(out, "%s\n", tmp->area); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1558 | |
| 1559 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1560 | chunk_appendf(out, "Subject Alternative Name: "); |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1561 | if (ssl_sock_get_san_oneline(cert, out) == -1) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1562 | goto end; |
| 1563 | *(out->area + out->data) = '\0'; |
| 1564 | chunk_appendf(out, "\n"); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1565 | #endif |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1566 | chunk_reset(tmp); |
| 1567 | chunk_appendf(out, "Algorithm: "); |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1568 | if (cert_get_pkey_algo(cert, tmp) == 0) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1569 | goto end; |
| 1570 | chunk_appendf(out, "%s\n", tmp->area); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1571 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1572 | chunk_reset(tmp); |
| 1573 | chunk_appendf(out, "SHA1 FingerPrint: "); |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1574 | if (X509_digest(cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1575 | goto end; |
| 1576 | tmp->data = len; |
| 1577 | dump_binary(out, tmp->area, tmp->data); |
| 1578 | chunk_appendf(out, "\n"); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1579 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1580 | chunk_appendf(out, "Subject: "); |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1581 | if ((name = X509_get_subject_name(cert)) == NULL) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1582 | goto end; |
| 1583 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 1584 | goto end; |
| 1585 | *(tmp->area + tmp->data) = '\0'; |
| 1586 | chunk_appendf(out, "%s\n", tmp->area); |
| 1587 | |
| 1588 | chunk_appendf(out, "Issuer: "); |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1589 | if ((name = X509_get_issuer_name(cert)) == NULL) |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1590 | goto end; |
| 1591 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 1592 | goto end; |
| 1593 | *(tmp->area + tmp->data) = '\0'; |
| 1594 | chunk_appendf(out, "%s\n", tmp->area); |
| 1595 | |
| 1596 | /* Displays subject of each certificate in the chain */ |
| 1597 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1598 | X509 *ca = sk_X509_value(chain, i); |
| 1599 | |
| 1600 | chunk_appendf(out, "Chain Subject: "); |
| 1601 | if ((name = X509_get_subject_name(ca)) == NULL) |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1602 | goto end; |
| 1603 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 1604 | goto end; |
| 1605 | *(tmp->area + tmp->data) = '\0'; |
| 1606 | chunk_appendf(out, "%s\n", tmp->area); |
| 1607 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1608 | chunk_appendf(out, "Chain Issuer: "); |
| 1609 | if ((name = X509_get_issuer_name(ca)) == NULL) |
William Lallemand | da8584c | 2020-05-14 10:14:37 +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); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | end: |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1618 | if (bio) |
| 1619 | BIO_free(bio); |
| 1620 | free_trash_chunk(tmp); |
| 1621 | |
| 1622 | return 0; |
| 1623 | } |
| 1624 | |
Remi Tricot-Le Breton | 3faf0cb | 2021-06-10 18:10:32 +0200 | [diff] [blame] | 1625 | #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] | 1626 | /* |
| 1627 | * Build the OCSP tree entry's key for a given ckch_store. |
| 1628 | * Returns a negative value in case of error. |
| 1629 | */ |
| 1630 | static int ckch_store_build_certid(struct ckch_store *ckch_store, unsigned char certid[128], unsigned int *key_length) |
| 1631 | { |
| 1632 | OCSP_RESPONSE *resp; |
| 1633 | OCSP_BASICRESP *bs = NULL; |
| 1634 | OCSP_SINGLERESP *sr; |
| 1635 | OCSP_CERTID *id; |
| 1636 | unsigned char *p = NULL; |
| 1637 | |
| 1638 | if (!key_length) |
| 1639 | return -1; |
| 1640 | |
| 1641 | *key_length = 0; |
| 1642 | |
| 1643 | if (!ckch_store->ckch->ocsp_response) |
| 1644 | return 0; |
| 1645 | |
| 1646 | p = (unsigned char *) ckch_store->ckch->ocsp_response->area; |
| 1647 | |
| 1648 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 1649 | ckch_store->ckch->ocsp_response->data); |
| 1650 | if (!resp) { |
| 1651 | goto end; |
| 1652 | } |
| 1653 | |
| 1654 | bs = OCSP_response_get1_basic(resp); |
| 1655 | if (!bs) { |
| 1656 | goto end; |
| 1657 | } |
| 1658 | |
| 1659 | sr = OCSP_resp_get0(bs, 0); |
| 1660 | if (!sr) { |
| 1661 | goto end; |
| 1662 | } |
| 1663 | |
| 1664 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 1665 | |
| 1666 | p = certid; |
| 1667 | *key_length = i2d_OCSP_CERTID(id, &p); |
| 1668 | |
| 1669 | end: |
| 1670 | return *key_length > 0; |
| 1671 | } |
| 1672 | #endif |
| 1673 | |
| 1674 | /* |
| 1675 | * Dump the OCSP certificate key (if it exists) of certificate <ckch> into |
| 1676 | * buffer <out>. |
| 1677 | * Returns 0 in case of success. |
| 1678 | */ |
| 1679 | static int ckch_store_show_ocsp_certid(struct ckch_store *ckch_store, struct buffer *out) |
| 1680 | { |
Remi Tricot-Le Breton | 3faf0cb | 2021-06-10 18:10:32 +0200 | [diff] [blame] | 1681 | #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] | 1682 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {}; |
| 1683 | unsigned int key_length = 0; |
| 1684 | int i; |
| 1685 | |
| 1686 | if (ckch_store_build_certid(ckch_store, (unsigned char*)key, &key_length) >= 0) { |
| 1687 | /* Dump the CERTID info */ |
| 1688 | chunk_appendf(out, "OCSP Response Key: "); |
| 1689 | for (i = 0; i < key_length; ++i) { |
| 1690 | chunk_appendf(out, "%02x", key[i]); |
| 1691 | } |
| 1692 | chunk_appendf(out, "\n"); |
| 1693 | } |
| 1694 | #endif |
| 1695 | |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1699 | |
Willy Tarreau | 4fd9b4d | 2022-05-04 16:11:50 +0200 | [diff] [blame] | 1700 | /* IO handler of the details "show ssl cert <filename>". |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1701 | * 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] | 1702 | */ |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1703 | static int cli_io_handler_show_cert_detail(struct appctx *appctx) |
| 1704 | { |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1705 | struct show_cert_ctx *ctx = appctx->svcctx; |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1706 | struct ckch_store *ckchs = ctx->cur_ckchs; |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1707 | struct buffer *out = alloc_trash_chunk(); |
| 1708 | int retval = 0; |
| 1709 | |
| 1710 | if (!out) |
| 1711 | goto end_no_putchk; |
| 1712 | |
| 1713 | chunk_appendf(out, "Filename: "); |
| 1714 | if (ckchs == ckchs_transaction.new_ckchs) |
| 1715 | chunk_appendf(out, "*"); |
| 1716 | chunk_appendf(out, "%s\n", ckchs->path); |
| 1717 | |
| 1718 | chunk_appendf(out, "Status: "); |
| 1719 | if (ckchs->ckch->cert == NULL) |
| 1720 | chunk_appendf(out, "Empty\n"); |
| 1721 | else if (LIST_ISEMPTY(&ckchs->ckch_inst)) |
| 1722 | chunk_appendf(out, "Unused\n"); |
| 1723 | else |
| 1724 | chunk_appendf(out, "Used\n"); |
| 1725 | |
| 1726 | retval = show_cert_detail(ckchs->ckch->cert, ckchs->ckch->chain, out); |
| 1727 | if (retval < 0) |
| 1728 | goto end_no_putchk; |
| 1729 | else if (retval) |
| 1730 | goto end; |
| 1731 | |
Remi Tricot-Le Breton | da968f6 | 2021-06-10 13:51:14 +0200 | [diff] [blame] | 1732 | ckch_store_show_ocsp_certid(ckchs, out); |
| 1733 | |
Remi Tricot-Le Breton | 523f0e4 | 2021-03-16 10:11:44 +0100 | [diff] [blame] | 1734 | end: |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 1735 | if (applet_putchk(appctx, out) == -1) |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1736 | goto yield; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1737 | |
| 1738 | end_no_putchk: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1739 | free_trash_chunk(out); |
| 1740 | return 1; |
| 1741 | yield: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1742 | free_trash_chunk(out); |
| 1743 | return 0; /* should come back */ |
| 1744 | } |
| 1745 | |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1746 | |
Willy Tarreau | 4fd9b4d | 2022-05-04 16:11:50 +0200 | [diff] [blame] | 1747 | /* IO handler of the details "show ssl cert <filename.ocsp>". |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1748 | * It uses a show_cert_ctx. |
Willy Tarreau | 4fd9b4d | 2022-05-04 16:11:50 +0200 | [diff] [blame] | 1749 | */ |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1750 | static int cli_io_handler_show_cert_ocsp_detail(struct appctx *appctx) |
| 1751 | { |
Remi Tricot-Le Breton | 3faf0cb | 2021-06-10 18:10:32 +0200 | [diff] [blame] | 1752 | #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] | 1753 | struct show_cert_ctx *ctx = appctx->svcctx; |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1754 | struct ckch_store *ckchs = ctx->cur_ckchs; |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1755 | struct buffer *out = alloc_trash_chunk(); |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1756 | int from_transaction = ctx->transaction; |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1757 | |
| 1758 | if (!out) |
| 1759 | goto end_no_putchk; |
| 1760 | |
| 1761 | /* If we try to display an ongoing transaction's OCSP response, we |
| 1762 | * need to dump the ckch's ocsp_response buffer directly. |
| 1763 | * Otherwise, we must rebuild the certificate's certid in order to |
| 1764 | * look for the current OCSP response in the tree. */ |
| 1765 | if (from_transaction && ckchs->ckch->ocsp_response) { |
Remi Tricot-Le Breton | a9a591a | 2022-02-16 14:42:22 +0100 | [diff] [blame] | 1766 | if (ssl_ocsp_response_print(ckchs->ckch->ocsp_response, out)) |
| 1767 | goto end_no_putchk; |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1768 | } |
| 1769 | else { |
| 1770 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {}; |
| 1771 | unsigned int key_length = 0; |
| 1772 | |
| 1773 | if (ckch_store_build_certid(ckchs, (unsigned char*)key, &key_length) < 0) |
| 1774 | goto end_no_putchk; |
| 1775 | |
Remi Tricot-Le Breton | a9a591a | 2022-02-16 14:42:22 +0100 | [diff] [blame] | 1776 | if (ssl_get_ocspresponse_detail(key, out)) |
| 1777 | goto end_no_putchk; |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1778 | } |
| 1779 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 1780 | if (applet_putchk(appctx, out) == -1) |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1781 | goto yield; |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1782 | |
| 1783 | end_no_putchk: |
| 1784 | free_trash_chunk(out); |
| 1785 | return 1; |
| 1786 | yield: |
| 1787 | free_trash_chunk(out); |
| 1788 | return 0; /* should come back */ |
| 1789 | #else |
| 1790 | return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n"); |
| 1791 | #endif |
| 1792 | } |
| 1793 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1794 | /* parsing function for 'show ssl cert [certfile]' */ |
| 1795 | static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 1796 | { |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1797 | struct show_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1798 | struct ckch_store *ckchs; |
| 1799 | |
| 1800 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 1801 | return cli_err(appctx, "Can't allocate memory!\n"); |
| 1802 | |
| 1803 | /* The operations on the CKCH architecture are locked so we can |
| 1804 | * manipulate ckch_store and ckch_inst */ |
| 1805 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 1806 | return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n"); |
| 1807 | |
| 1808 | /* check if there is a certificate to lookup */ |
| 1809 | if (*args[3]) { |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1810 | int show_ocsp_detail = 0; |
| 1811 | int from_transaction = 0; |
| 1812 | char *end; |
| 1813 | |
| 1814 | /* We manage the special case "certname.ocsp" through which we |
| 1815 | * can show the details of an OCSP response. */ |
| 1816 | end = strrchr(args[3], '.'); |
| 1817 | if (end && strcmp(end+1, "ocsp") == 0) { |
| 1818 | *end = '\0'; |
| 1819 | show_ocsp_detail = 1; |
| 1820 | } |
| 1821 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1822 | if (*args[3] == '*') { |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1823 | from_transaction = 1; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1824 | if (!ckchs_transaction.new_ckchs) |
| 1825 | goto error; |
| 1826 | |
| 1827 | ckchs = ckchs_transaction.new_ckchs; |
| 1828 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1829 | if (strcmp(args[3] + 1, ckchs->path) != 0) |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1830 | goto error; |
| 1831 | |
| 1832 | } else { |
| 1833 | if ((ckchs = ckchs_lookup(args[3])) == NULL) |
| 1834 | goto error; |
| 1835 | |
| 1836 | } |
| 1837 | |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1838 | ctx->cur_ckchs = ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1839 | /* use the IO handler that shows details */ |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1840 | if (show_ocsp_detail) { |
Willy Tarreau | 96c9a6c | 2022-05-04 19:51:37 +0200 | [diff] [blame] | 1841 | ctx->transaction = from_transaction; |
Remi Tricot-Le Breton | 6056e61 | 2021-06-10 13:51:15 +0200 | [diff] [blame] | 1842 | appctx->io_handler = cli_io_handler_show_cert_ocsp_detail; |
| 1843 | } |
| 1844 | else |
| 1845 | appctx->io_handler = cli_io_handler_show_cert_detail; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | return 0; |
| 1849 | |
| 1850 | error: |
| 1851 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1852 | return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n"); |
| 1853 | } |
| 1854 | |
| 1855 | /* release function of the `set ssl cert' command, free things and unlock the spinlock */ |
| 1856 | static void cli_release_commit_cert(struct appctx *appctx) |
| 1857 | { |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 1858 | struct commit_cert_ctx *ctx = appctx->svcctx; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1859 | |
| 1860 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 1861 | /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */ |
| 1862 | if (ctx->new_ckchs) |
| 1863 | ckch_store_free(ctx->new_ckchs); |
| 1864 | ha_free(&ctx->err); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1865 | } |
| 1866 | |
Remi Tricot-Le Breton | bfadc02 | 2021-02-24 12:20:48 +0100 | [diff] [blame] | 1867 | |
| 1868 | /* |
| 1869 | * Rebuild a new instance 'new_inst' based on an old instance 'ckchi' and a |
| 1870 | * specific ckch_store. |
| 1871 | * Returns 0 in case of success, 1 otherwise. |
| 1872 | */ |
William Lallemand | e60c7d6 | 2022-03-30 11:26:15 +0200 | [diff] [blame] | 1873 | int ckch_inst_rebuild(struct ckch_store *ckch_store, struct ckch_inst *ckchi, |
| 1874 | struct ckch_inst **new_inst, char **err) |
Remi Tricot-Le Breton | bfadc02 | 2021-02-24 12:20:48 +0100 | [diff] [blame] | 1875 | { |
| 1876 | int retval = 0; |
| 1877 | int errcode = 0; |
| 1878 | struct sni_ctx *sc0, *sc0s; |
| 1879 | char **sni_filter = NULL; |
| 1880 | int fcount = 0; |
| 1881 | |
| 1882 | if (ckchi->crtlist_entry) { |
| 1883 | sni_filter = ckchi->crtlist_entry->filters; |
| 1884 | fcount = ckchi->crtlist_entry->fcount; |
| 1885 | } |
| 1886 | |
| 1887 | if (ckchi->is_server_instance) |
| 1888 | errcode |= ckch_inst_new_load_srv_store(ckch_store->path, ckch_store, new_inst, err); |
| 1889 | else |
| 1890 | errcode |= ckch_inst_new_load_store(ckch_store->path, ckch_store, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, new_inst, err); |
| 1891 | |
| 1892 | if (errcode & ERR_CODE) |
| 1893 | return 1; |
| 1894 | |
| 1895 | /* if the previous ckchi was used as the default */ |
| 1896 | if (ckchi->is_default) |
| 1897 | (*new_inst)->is_default = 1; |
| 1898 | |
| 1899 | (*new_inst)->is_server_instance = ckchi->is_server_instance; |
| 1900 | (*new_inst)->server = ckchi->server; |
| 1901 | /* Create a new SSL_CTX and link it to the new instance. */ |
| 1902 | if ((*new_inst)->is_server_instance) { |
| 1903 | retval = ssl_sock_prep_srv_ctx_and_inst(ckchi->server, (*new_inst)->ctx, (*new_inst)); |
| 1904 | if (retval) |
| 1905 | return 1; |
| 1906 | } |
| 1907 | |
| 1908 | /* create the link to the crtlist_entry */ |
| 1909 | (*new_inst)->crtlist_entry = ckchi->crtlist_entry; |
| 1910 | |
| 1911 | /* we need to initialize the SSL_CTX generated */ |
| 1912 | /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */ |
| 1913 | list_for_each_entry_safe(sc0, sc0s, &(*new_inst)->sni_ctx, by_ckch_inst) { |
| 1914 | if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */ |
| 1915 | errcode |= ssl_sock_prep_ctx_and_inst(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, *new_inst, err); |
| 1916 | if (errcode & ERR_CODE) |
| 1917 | return 1; |
| 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | return 0; |
| 1922 | } |
| 1923 | |
| 1924 | /* |
| 1925 | * Load all the new SNIs of a newly built ckch instance in the trees, or replace |
| 1926 | * a server's main ckch instance. |
| 1927 | */ |
| 1928 | static void __ssl_sock_load_new_ckch_instance(struct ckch_inst *ckchi) |
| 1929 | { |
| 1930 | /* The bind_conf will be null on server ckch_instances. */ |
| 1931 | if (ckchi->is_server_instance) { |
| 1932 | int i; |
| 1933 | /* a lock is needed here since we have to free the SSL cache */ |
| 1934 | HA_RWLOCK_WRLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock); |
| 1935 | /* free the server current SSL_CTX */ |
| 1936 | SSL_CTX_free(ckchi->server->ssl_ctx.ctx); |
| 1937 | /* Actual ssl context update */ |
| 1938 | SSL_CTX_up_ref(ckchi->ctx); |
| 1939 | ckchi->server->ssl_ctx.ctx = ckchi->ctx; |
| 1940 | ckchi->server->ssl_ctx.inst = ckchi; |
| 1941 | |
| 1942 | /* flush the session cache of the server */ |
| 1943 | for (i = 0; i < global.nbthread; i++) { |
William Lallemand | ce99033 | 2021-11-23 15:15:09 +0100 | [diff] [blame] | 1944 | ha_free(&ckchi->server->ssl_ctx.reused_sess[i].sni); |
Remi Tricot-Le Breton | bfadc02 | 2021-02-24 12:20:48 +0100 | [diff] [blame] | 1945 | ha_free(&ckchi->server->ssl_ctx.reused_sess[i].ptr); |
| 1946 | } |
| 1947 | HA_RWLOCK_WRUNLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock); |
| 1948 | |
| 1949 | } else { |
| 1950 | HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 1951 | ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf); |
| 1952 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | /* |
| 1957 | * Delete a ckch instance that was replaced after a CLI command. |
| 1958 | */ |
| 1959 | static void __ckch_inst_free_locked(struct ckch_inst *ckchi) |
| 1960 | { |
| 1961 | if (ckchi->is_server_instance) { |
| 1962 | /* no lock for servers */ |
| 1963 | ckch_inst_free(ckchi); |
| 1964 | } else { |
| 1965 | struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf; |
| 1966 | |
| 1967 | HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock); |
| 1968 | ckch_inst_free(ckchi); |
| 1969 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock); |
| 1970 | } |
| 1971 | } |
| 1972 | |
William Lallemand | 3b5a3a6 | 2022-03-29 14:29:31 +0200 | [diff] [blame] | 1973 | /* Replace a ckch_store in the ckch tree and insert the whole dependencies, |
| 1974 | * then free the previous dependencies and store. |
| 1975 | * Used in the case of a certificate update. |
| 1976 | * |
| 1977 | * Every dependencies must allocated before using this function. |
| 1978 | * |
| 1979 | * This function can't fail as it only update pointers, and does not alloc anything. |
| 1980 | * |
| 1981 | * /!\ This function must be used under the ckch lock. /!\ |
| 1982 | * |
| 1983 | * - Insert every dependencies (SNI, crtlist_entry, ckch_inst, etc) |
| 1984 | * - Delete the old ckch_store from the tree |
| 1985 | * - Insert the new ckch_store |
| 1986 | * - Free the old dependencies and the old ckch_store |
| 1987 | */ |
| 1988 | void ckch_store_replace(struct ckch_store *old_ckchs, struct ckch_store *new_ckchs) |
| 1989 | { |
| 1990 | struct crtlist_entry *entry; |
| 1991 | struct ckch_inst *ckchi, *ckchis; |
| 1992 | |
| 1993 | LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry); |
| 1994 | list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) { |
| 1995 | ebpt_delete(&entry->node); |
| 1996 | /* change the ptr and reinsert the node */ |
| 1997 | entry->node.key = new_ckchs; |
| 1998 | ebpt_insert(&entry->crtlist->entries, &entry->node); |
| 1999 | } |
| 2000 | /* insert the new ckch_insts in the crtlist_entry */ |
| 2001 | list_for_each_entry(ckchi, &new_ckchs->ckch_inst, by_ckchs) { |
| 2002 | if (ckchi->crtlist_entry) |
| 2003 | LIST_INSERT(&ckchi->crtlist_entry->ckch_inst, &ckchi->by_crtlist_entry); |
| 2004 | } |
| 2005 | /* First, we insert every new SNIs in the trees, also replace the default_ctx */ |
| 2006 | list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) { |
| 2007 | __ssl_sock_load_new_ckch_instance(ckchi); |
| 2008 | } |
| 2009 | /* delete the old sni_ctx, the old ckch_insts and the ckch_store */ |
| 2010 | list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) { |
| 2011 | __ckch_inst_free_locked(ckchi); |
| 2012 | } |
| 2013 | |
| 2014 | ckch_store_free(old_ckchs); |
| 2015 | ebst_insert(&ckchs_tree, &new_ckchs->node); |
| 2016 | } |
| 2017 | |
Remi Tricot-Le Breton | bfadc02 | 2021-02-24 12:20:48 +0100 | [diff] [blame] | 2018 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2019 | /* |
| 2020 | * This function tries to create the new ckch_inst and their SNIs |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 2021 | * |
| 2022 | * /!\ 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] | 2023 | */ |
| 2024 | static int cli_io_handler_commit_cert(struct appctx *appctx) |
| 2025 | { |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 2026 | struct commit_cert_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2027 | struct stconn *sc = appctx_sc(appctx); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2028 | int y = 0; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2029 | struct ckch_store *old_ckchs, *new_ckchs = NULL; |
William Lallemand | 3b5a3a6 | 2022-03-29 14:29:31 +0200 | [diff] [blame] | 2030 | struct ckch_inst *ckchi; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2031 | |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 2032 | if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2033 | goto end; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2034 | |
| 2035 | while (1) { |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2036 | switch (ctx->state) { |
| 2037 | case CERT_ST_INIT: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2038 | /* This state just print the update message */ |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2039 | chunk_printf(&trash, "Committing %s", ckchs_transaction.path); |
| 2040 | if (applet_putchk(appctx, &trash) == -1) |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2041 | goto yield; |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 2042 | |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2043 | ctx->state = CERT_ST_GEN; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2044 | /* fallthrough */ |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2045 | case CERT_ST_GEN: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2046 | /* |
| 2047 | * This state generates the ckch instances with their |
| 2048 | * sni_ctxs and SSL_CTX. |
| 2049 | * |
| 2050 | * Since the SSL_CTX generation can be CPU consumer, we |
| 2051 | * yield every 10 instances. |
| 2052 | */ |
| 2053 | |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 2054 | old_ckchs = ctx->old_ckchs; |
| 2055 | new_ckchs = ctx->new_ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2056 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2057 | /* get the next ckchi to regenerate */ |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 2058 | ckchi = ctx->next_ckchi; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2059 | /* we didn't start yet, set it to the first elem */ |
| 2060 | if (ckchi == NULL) |
| 2061 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 2062 | |
| 2063 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 2064 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 2065 | struct ckch_inst *new_inst; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2066 | |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2067 | /* save the next ckchi to compute in case of yield */ |
| 2068 | ctx->next_ckchi = ckchi; |
| 2069 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2070 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 2071 | if (y >= 10) { |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2072 | applet_have_more_data(appctx); /* let's come back later */ |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2073 | goto yield; |
| 2074 | } |
| 2075 | |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2076 | /* display one dot per new instance */ |
| 2077 | if (applet_putstr(appctx, ".") == -1) |
| 2078 | goto yield; |
| 2079 | |
| 2080 | ctx->err = NULL; |
| 2081 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &ctx->err)) { |
| 2082 | ctx->state = CERT_ST_ERROR; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2083 | goto error; |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2084 | } |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2085 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2086 | /* link the new ckch_inst to the duplicate */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 2087 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2088 | y++; |
| 2089 | } |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2090 | ctx->state = CERT_ST_INSERT; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2091 | /* fallthrough */ |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2092 | case CERT_ST_INSERT: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2093 | /* The generation is finished, we can insert everything */ |
| 2094 | |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 2095 | old_ckchs = ctx->old_ckchs; |
| 2096 | new_ckchs = ctx->new_ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2097 | |
William Lallemand | 3b5a3a6 | 2022-03-29 14:29:31 +0200 | [diff] [blame] | 2098 | /* insert everything and remove the previous objects */ |
| 2099 | ckch_store_replace(old_ckchs, new_ckchs); |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2100 | ctx->new_ckchs = ctx->old_ckchs = NULL; |
| 2101 | ctx->state = CERT_ST_SUCCESS; |
| 2102 | /* fallthrough */ |
| 2103 | case CERT_ST_SUCCESS: |
| 2104 | if (applet_putstr(appctx, "\nSuccess!\n") == -1) |
| 2105 | goto yield; |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2106 | ctx->state = CERT_ST_FIN; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2107 | /* fallthrough */ |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2108 | case CERT_ST_FIN: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2109 | /* we achieved the transaction, we can set everything to NULL */ |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2110 | ckchs_transaction.new_ckchs = NULL; |
| 2111 | ckchs_transaction.old_ckchs = NULL; |
Christopher Faulet | e2ef4dd | 2022-05-31 18:07:59 +0200 | [diff] [blame] | 2112 | ckchs_transaction.path = NULL; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2113 | goto end; |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2114 | |
| 2115 | case CERT_ST_ERROR: |
| 2116 | error: |
| 2117 | chunk_printf(&trash, "\n%sFailed!\n", ctx->err); |
| 2118 | if (applet_putchk(appctx, &trash) == -1) |
| 2119 | goto yield; |
| 2120 | ctx->state = CERT_ST_FIN; |
| 2121 | break; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2122 | } |
| 2123 | } |
| 2124 | end: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2125 | /* success: call the release function and don't come back */ |
| 2126 | return 1; |
Christopher Faulet | 9d56e24 | 2022-05-31 16:37:01 +0200 | [diff] [blame] | 2127 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2128 | yield: |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2129 | return 0; /* should come back */ |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | /* |
| 2133 | * Parsing function of 'commit ssl cert' |
| 2134 | */ |
| 2135 | static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 2136 | { |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 2137 | struct commit_cert_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2138 | char *err = NULL; |
| 2139 | |
| 2140 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2141 | return 1; |
| 2142 | |
| 2143 | if (!*args[3]) |
| 2144 | return cli_err(appctx, "'commit ssl cert expects a filename\n"); |
| 2145 | |
| 2146 | /* The operations on the CKCH architecture are locked so we can |
| 2147 | * manipulate ckch_store and ckch_inst */ |
| 2148 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2149 | return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n"); |
| 2150 | |
| 2151 | if (!ckchs_transaction.path) { |
| 2152 | memprintf(&err, "No ongoing transaction! !\n"); |
| 2153 | goto error; |
| 2154 | } |
| 2155 | |
| 2156 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 2157 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]); |
| 2158 | goto error; |
| 2159 | } |
| 2160 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 2161 | /* if a certificate is here, a private key must be here too */ |
| 2162 | if (ckchs_transaction.new_ckchs->ckch->cert && !ckchs_transaction.new_ckchs->ckch->key) { |
| 2163 | memprintf(&err, "The transaction must contain at least a certificate and a private key!\n"); |
| 2164 | goto error; |
| 2165 | } |
William Lallemand | a941952 | 2020-06-24 16:26:41 +0200 | [diff] [blame] | 2166 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 2167 | if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) { |
| 2168 | memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path); |
| 2169 | goto error; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | /* init the appctx structure */ |
Willy Tarreau | cb1b4ed | 2022-05-05 08:15:27 +0200 | [diff] [blame] | 2173 | ctx->state = CERT_ST_INIT; |
Willy Tarreau | a645b6a | 2022-05-04 19:58:00 +0200 | [diff] [blame] | 2174 | ctx->next_ckchi = NULL; |
| 2175 | ctx->new_ckchs = ckchs_transaction.new_ckchs; |
| 2176 | ctx->old_ckchs = ckchs_transaction.old_ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2177 | |
| 2178 | /* we don't unlock there, it will be unlock after the IO handler, in the release handler */ |
| 2179 | return 0; |
| 2180 | |
| 2181 | error: |
| 2182 | |
| 2183 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2184 | err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]); |
| 2185 | |
| 2186 | return cli_dynerr(appctx, err); |
| 2187 | } |
| 2188 | |
| 2189 | |
| 2190 | |
| 2191 | |
| 2192 | /* |
| 2193 | * 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] | 2194 | * 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] | 2195 | */ |
| 2196 | static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 2197 | { |
| 2198 | struct ckch_store *new_ckchs = NULL; |
| 2199 | struct ckch_store *old_ckchs = NULL; |
| 2200 | char *err = NULL; |
| 2201 | int i; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2202 | int errcode = 0; |
| 2203 | char *end; |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 2204 | struct cert_exts *cert_ext = &cert_exts[0]; /* default one, PEM */ |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2205 | struct cert_key_and_chain *ckch; |
| 2206 | struct buffer *buf; |
| 2207 | |
| 2208 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2209 | return 1; |
| 2210 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2211 | if (!*args[3] || !payload) |
| 2212 | return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n"); |
| 2213 | |
| 2214 | /* The operations on the CKCH architecture are locked so we can |
| 2215 | * manipulate ckch_store and ckch_inst */ |
| 2216 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2217 | return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n"); |
| 2218 | |
William Lallemand | 5ba80d6 | 2021-05-04 16:17:27 +0200 | [diff] [blame] | 2219 | if ((buf = alloc_trash_chunk()) == NULL) { |
| 2220 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 2221 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2222 | goto end; |
| 2223 | } |
William Lallemand | e5ff4ad | 2020-06-08 09:40:37 +0200 | [diff] [blame] | 2224 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2225 | if (!chunk_strcpy(buf, args[3])) { |
| 2226 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 2227 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2228 | goto end; |
| 2229 | } |
| 2230 | |
| 2231 | /* check which type of file we want to update */ |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 2232 | for (i = 0; cert_exts[i].ext != NULL; i++) { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2233 | end = strrchr(buf->area, '.'); |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 2234 | 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] | 2235 | *end = '\0'; |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 2236 | buf->data = strlen(buf->area); |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 2237 | cert_ext = &cert_exts[i]; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2238 | break; |
| 2239 | } |
| 2240 | } |
| 2241 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2242 | /* if there is an ongoing transaction */ |
| 2243 | if (ckchs_transaction.path) { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2244 | /* if there is an ongoing transaction, check if this is the same file */ |
| 2245 | if (strcmp(ckchs_transaction.path, buf->area) != 0) { |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 2246 | /* we didn't find the transaction, must try more cases below */ |
| 2247 | |
| 2248 | /* 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] | 2249 | if (cert_ext->type != CERT_TYPE_PEM && global_ssl.extra_files_noext) { |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 2250 | if (!chunk_strcat(buf, ".crt")) { |
| 2251 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 2252 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2253 | goto end; |
| 2254 | } |
| 2255 | |
| 2256 | if (strcmp(ckchs_transaction.path, buf->area) != 0) { |
| 2257 | /* remove .crt of the error message */ |
| 2258 | *(b_orig(buf) + b_data(buf) + strlen(".crt")) = '\0'; |
| 2259 | b_sub(buf, strlen(".crt")); |
| 2260 | |
| 2261 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area); |
| 2262 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2263 | goto end; |
| 2264 | } |
| 2265 | } |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2266 | } |
| 2267 | |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2268 | old_ckchs = ckchs_transaction.new_ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2269 | |
| 2270 | } else { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2271 | |
William Lallemand | 95fefa1 | 2020-09-09 12:01:33 +0200 | [diff] [blame] | 2272 | /* lookup for the certificate in the tree */ |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2273 | old_ckchs = ckchs_lookup(buf->area); |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 2274 | |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2275 | if (!old_ckchs) { |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 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 | } |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2283 | old_ckchs = ckchs_lookup(buf->area); |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 2284 | } |
| 2285 | } |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2286 | } |
| 2287 | |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2288 | if (!old_ckchs) { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2289 | memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n", |
| 2290 | err ? err : ""); |
| 2291 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2292 | goto end; |
| 2293 | } |
| 2294 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2295 | /* duplicate the ckch store */ |
| 2296 | new_ckchs = ckchs_dup(old_ckchs); |
| 2297 | if (!new_ckchs) { |
| 2298 | memprintf(&err, "%sCannot allocate memory!\n", |
| 2299 | err ? err : ""); |
| 2300 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2301 | goto end; |
| 2302 | } |
| 2303 | |
William Lallemand | 95fefa1 | 2020-09-09 12:01:33 +0200 | [diff] [blame] | 2304 | ckch = new_ckchs->ckch; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2305 | |
| 2306 | /* appply the change on the duplicate */ |
William Lallemand | ff8bf98 | 2022-03-29 10:44:23 +0200 | [diff] [blame] | 2307 | if (cert_ext->load(buf->area, payload, ckch, &err) != 0) { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2308 | memprintf(&err, "%sCan't load the payload\n", err ? err : ""); |
| 2309 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2310 | goto end; |
| 2311 | } |
| 2312 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2313 | /* we succeed, we can save the ckchs in the transaction */ |
| 2314 | |
| 2315 | /* if there wasn't a transaction, update the old ckchs */ |
| 2316 | if (!ckchs_transaction.old_ckchs) { |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2317 | ckchs_transaction.old_ckchs = old_ckchs; |
| 2318 | ckchs_transaction.path = old_ckchs->path; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2319 | err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path); |
| 2320 | } else { |
| 2321 | err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path); |
| 2322 | |
| 2323 | } |
| 2324 | |
| 2325 | /* free the previous ckchs if there was a transaction */ |
| 2326 | ckch_store_free(ckchs_transaction.new_ckchs); |
| 2327 | |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2328 | ckchs_transaction.new_ckchs = new_ckchs; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2329 | |
| 2330 | |
| 2331 | /* creates the SNI ctxs later in the IO handler */ |
| 2332 | |
| 2333 | end: |
| 2334 | free_trash_chunk(buf); |
| 2335 | |
| 2336 | if (errcode & ERR_CODE) { |
Christopher Faulet | 24a20b9 | 2022-06-03 11:50:40 +0200 | [diff] [blame] | 2337 | ckch_store_free(new_ckchs); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2338 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2339 | return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3])); |
| 2340 | } else { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2341 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2342 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 2343 | } |
| 2344 | /* TODO: handle the ERR_WARN which are not handled because of the io_handler */ |
| 2345 | } |
| 2346 | |
| 2347 | /* parsing function of 'abort ssl cert' */ |
| 2348 | static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 2349 | { |
| 2350 | char *err = NULL; |
| 2351 | |
| 2352 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2353 | return 1; |
| 2354 | |
| 2355 | if (!*args[3]) |
| 2356 | return cli_err(appctx, "'abort ssl cert' expects a filename\n"); |
| 2357 | |
| 2358 | /* The operations on the CKCH architecture are locked so we can |
| 2359 | * manipulate ckch_store and ckch_inst */ |
| 2360 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2361 | return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n"); |
| 2362 | |
| 2363 | if (!ckchs_transaction.path) { |
| 2364 | memprintf(&err, "No ongoing transaction!\n"); |
| 2365 | goto error; |
| 2366 | } |
| 2367 | |
| 2368 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 2369 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]); |
| 2370 | goto error; |
| 2371 | } |
| 2372 | |
| 2373 | /* Only free the ckchs there, because the SNI and instances were not generated yet */ |
| 2374 | ckch_store_free(ckchs_transaction.new_ckchs); |
| 2375 | ckchs_transaction.new_ckchs = NULL; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2376 | ckchs_transaction.old_ckchs = NULL; |
Christopher Faulet | e2ef4dd | 2022-05-31 18:07:59 +0200 | [diff] [blame] | 2377 | ckchs_transaction.path = NULL; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2378 | |
| 2379 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2380 | |
| 2381 | err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]); |
| 2382 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 2383 | |
| 2384 | error: |
| 2385 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2386 | |
| 2387 | return cli_dynerr(appctx, err); |
| 2388 | } |
| 2389 | |
| 2390 | /* parsing function of 'new ssl cert' */ |
| 2391 | static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 2392 | { |
| 2393 | struct ckch_store *store; |
| 2394 | char *err = NULL; |
| 2395 | char *path; |
| 2396 | |
| 2397 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2398 | return 1; |
| 2399 | |
| 2400 | if (!*args[3]) |
| 2401 | return cli_err(appctx, "'new ssl cert' expects a filename\n"); |
| 2402 | |
| 2403 | path = args[3]; |
| 2404 | |
| 2405 | /* The operations on the CKCH architecture are locked so we can |
| 2406 | * manipulate ckch_store and ckch_inst */ |
| 2407 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2408 | return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n"); |
| 2409 | |
| 2410 | store = ckchs_lookup(path); |
| 2411 | if (store != NULL) { |
| 2412 | memprintf(&err, "Certificate '%s' already exists!\n", path); |
| 2413 | store = NULL; /* we don't want to free it */ |
| 2414 | goto error; |
| 2415 | } |
| 2416 | /* we won't support multi-certificate bundle here */ |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 2417 | store = ckch_store_new(path); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2418 | if (!store) { |
| 2419 | memprintf(&err, "unable to allocate memory.\n"); |
| 2420 | goto error; |
| 2421 | } |
| 2422 | |
| 2423 | /* insert into the ckchs tree */ |
| 2424 | ebst_insert(&ckchs_tree, &store->node); |
| 2425 | memprintf(&err, "New empty certificate store '%s'!\n", args[3]); |
| 2426 | |
| 2427 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2428 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 2429 | error: |
| 2430 | free(store); |
| 2431 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2432 | return cli_dynerr(appctx, err); |
| 2433 | } |
| 2434 | |
| 2435 | /* parsing function of 'del ssl cert' */ |
| 2436 | static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 2437 | { |
| 2438 | struct ckch_store *store; |
| 2439 | char *err = NULL; |
| 2440 | char *filename; |
| 2441 | |
| 2442 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2443 | return 1; |
| 2444 | |
| 2445 | if (!*args[3]) |
| 2446 | return cli_err(appctx, "'del ssl cert' expects a certificate name\n"); |
| 2447 | |
| 2448 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2449 | return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n"); |
| 2450 | |
| 2451 | filename = args[3]; |
| 2452 | |
Christopher Faulet | 926fefc | 2022-05-31 18:04:25 +0200 | [diff] [blame] | 2453 | if (ckchs_transaction.path && strcmp(ckchs_transaction.path, filename) == 0) { |
| 2454 | memprintf(&err, "ongoing transaction for the certificate '%s'", filename); |
| 2455 | goto error; |
| 2456 | } |
| 2457 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 2458 | store = ckchs_lookup(filename); |
| 2459 | if (store == NULL) { |
| 2460 | memprintf(&err, "certificate '%s' doesn't exist!\n", filename); |
| 2461 | goto error; |
| 2462 | } |
| 2463 | if (!LIST_ISEMPTY(&store->ckch_inst)) { |
| 2464 | memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename); |
| 2465 | goto error; |
| 2466 | } |
| 2467 | |
| 2468 | ebmb_delete(&store->node); |
| 2469 | ckch_store_free(store); |
| 2470 | |
| 2471 | memprintf(&err, "Certificate '%s' deleted!\n", filename); |
| 2472 | |
| 2473 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2474 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 2475 | |
| 2476 | error: |
| 2477 | memprintf(&err, "Can't remove the certificate: %s\n", err ? err : ""); |
| 2478 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2479 | return cli_dynerr(appctx, err); |
| 2480 | } |
| 2481 | |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2482 | |
Remi Tricot-Le Breton | 9f40fe0 | 2021-03-16 16:21:27 +0100 | [diff] [blame] | 2483 | |
| 2484 | /* parsing function of 'new ssl ca-file' */ |
| 2485 | static int cli_parse_new_cafile(char **args, char *payload, struct appctx *appctx, void *private) |
| 2486 | { |
| 2487 | struct cafile_entry *cafile_entry; |
| 2488 | char *err = NULL; |
| 2489 | char *path; |
| 2490 | |
| 2491 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2492 | return 1; |
| 2493 | |
| 2494 | if (!*args[3]) |
| 2495 | return cli_err(appctx, "'new ssl ca-file' expects a filename\n"); |
| 2496 | |
| 2497 | path = args[3]; |
| 2498 | |
| 2499 | /* The operations on the CKCH architecture are locked so we can |
| 2500 | * manipulate ckch_store and ckch_inst */ |
| 2501 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2502 | return cli_err(appctx, "Can't create a CA file!\nOperations on certificates are currently locked!\n"); |
| 2503 | |
| 2504 | cafile_entry = ssl_store_get_cafile_entry(path, 0); |
| 2505 | if (cafile_entry) { |
| 2506 | memprintf(&err, "CA file '%s' already exists!\n", path); |
| 2507 | goto error; |
| 2508 | } |
| 2509 | |
| 2510 | cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CERT); |
| 2511 | if (!cafile_entry) { |
| 2512 | memprintf(&err, "%sCannot allocate memory!\n", |
| 2513 | err ? err : ""); |
| 2514 | goto error; |
| 2515 | } |
| 2516 | |
| 2517 | /* Add the newly created cafile_entry to the tree so that |
| 2518 | * any new ckch instance created from now can use it. */ |
| 2519 | if (ssl_store_add_uncommitted_cafile_entry(cafile_entry)) |
| 2520 | goto error; |
| 2521 | |
| 2522 | memprintf(&err, "New CA file created '%s'!\n", path); |
| 2523 | |
| 2524 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2525 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 2526 | error: |
| 2527 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2528 | return cli_dynerr(appctx, err); |
| 2529 | } |
| 2530 | |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2531 | /* |
| 2532 | * Parsing function of `set ssl ca-file` |
| 2533 | */ |
| 2534 | static int cli_parse_set_cafile(char **args, char *payload, struct appctx *appctx, void *private) |
| 2535 | { |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2536 | struct cafile_entry *old_cafile_entry = NULL; |
| 2537 | struct cafile_entry *new_cafile_entry = NULL; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2538 | char *err = NULL; |
| 2539 | int errcode = 0; |
| 2540 | struct buffer *buf; |
| 2541 | |
| 2542 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2543 | return 1; |
| 2544 | |
| 2545 | if (!*args[3] || !payload) |
| 2546 | return cli_err(appctx, "'set ssl ca-file expects a filename and CAs as a payload\n"); |
| 2547 | |
| 2548 | /* The operations on the CKCH architecture are locked so we can |
| 2549 | * manipulate ckch_store and ckch_inst */ |
| 2550 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2551 | return cli_err(appctx, "Can't update the CA file!\nOperations on certificates are currently locked!\n"); |
| 2552 | |
| 2553 | if ((buf = alloc_trash_chunk()) == NULL) { |
| 2554 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 2555 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2556 | goto end; |
| 2557 | } |
| 2558 | |
| 2559 | if (!chunk_strcpy(buf, args[3])) { |
| 2560 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 2561 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2562 | goto end; |
| 2563 | } |
| 2564 | |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2565 | old_cafile_entry = NULL; |
| 2566 | new_cafile_entry = NULL; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2567 | |
| 2568 | /* if there is an ongoing transaction */ |
| 2569 | if (cafile_transaction.path) { |
| 2570 | /* if there is an ongoing transaction, check if this is the same file */ |
| 2571 | if (strcmp(cafile_transaction.path, buf->area) != 0) { |
| 2572 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, buf->area); |
| 2573 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2574 | goto end; |
| 2575 | } |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2576 | old_cafile_entry = cafile_transaction.old_cafile_entry; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2577 | } |
| 2578 | else { |
| 2579 | /* lookup for the certificate in the tree */ |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2580 | 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] | 2581 | } |
| 2582 | |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2583 | if (!old_cafile_entry) { |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2584 | memprintf(&err, "%sCan't replace a CA file which is not referenced by the configuration!\n", |
| 2585 | err ? err : ""); |
| 2586 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2587 | goto end; |
| 2588 | } |
| 2589 | |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2590 | /* Create a new cafile_entry without adding it to the cafile tree. */ |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2591 | new_cafile_entry = ssl_store_create_cafile_entry(old_cafile_entry->path, NULL, CAFILE_CERT); |
| 2592 | if (!new_cafile_entry) { |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2593 | memprintf(&err, "%sCannot allocate memory!\n", |
| 2594 | err ? err : ""); |
| 2595 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2596 | goto end; |
| 2597 | } |
| 2598 | |
| 2599 | /* Fill the new entry with the new CAs. */ |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2600 | 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] | 2601 | memprintf(&err, "%sInvalid payload\n", err ? err : ""); |
| 2602 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2603 | goto end; |
| 2604 | } |
| 2605 | |
| 2606 | /* we succeed, we can save the ca in the transaction */ |
| 2607 | |
| 2608 | /* if there wasn't a transaction, update the old CA */ |
| 2609 | if (!cafile_transaction.old_cafile_entry) { |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2610 | cafile_transaction.old_cafile_entry = old_cafile_entry; |
| 2611 | cafile_transaction.path = old_cafile_entry->path; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2612 | err = memprintf(&err, "transaction created for CA %s!\n", cafile_transaction.path); |
| 2613 | } else { |
| 2614 | err = memprintf(&err, "transaction updated for CA %s!\n", cafile_transaction.path); |
| 2615 | } |
| 2616 | |
| 2617 | /* free the previous CA if there was a transaction */ |
| 2618 | ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry); |
| 2619 | |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2620 | cafile_transaction.new_cafile_entry = new_cafile_entry; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2621 | |
| 2622 | /* creates the SNI ctxs later in the IO handler */ |
| 2623 | |
| 2624 | end: |
| 2625 | free_trash_chunk(buf); |
| 2626 | |
| 2627 | if (errcode & ERR_CODE) { |
Christopher Faulet | 132c595 | 2022-06-03 11:56:26 +0200 | [diff] [blame] | 2628 | ssl_store_delete_cafile_entry(new_cafile_entry); |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2629 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2630 | return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3])); |
| 2631 | } else { |
| 2632 | |
| 2633 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2634 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | |
| 2639 | /* |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2640 | * Parsing function of 'commit ssl ca-file'. |
| 2641 | * 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] | 2642 | */ |
| 2643 | static int cli_parse_commit_cafile(char **args, char *payload, struct appctx *appctx, void *private) |
| 2644 | { |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2645 | 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] | 2646 | char *err = NULL; |
| 2647 | |
| 2648 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2649 | return 1; |
| 2650 | |
| 2651 | if (!*args[3]) |
| 2652 | return cli_err(appctx, "'commit ssl ca-file expects a filename\n"); |
| 2653 | |
| 2654 | /* The operations on the CKCH architecture are locked so we can |
| 2655 | * manipulate ckch_store and ckch_inst */ |
| 2656 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2657 | return cli_err(appctx, "Can't commit the CA file!\nOperations on certificates are currently locked!\n"); |
| 2658 | |
| 2659 | if (!cafile_transaction.path) { |
| 2660 | memprintf(&err, "No ongoing transaction! !\n"); |
| 2661 | goto error; |
| 2662 | } |
| 2663 | |
| 2664 | if (strcmp(cafile_transaction.path, args[3]) != 0) { |
| 2665 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, args[3]); |
| 2666 | goto error; |
| 2667 | } |
| 2668 | /* init the appctx structure */ |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2669 | ctx->state = CACRL_ST_INIT; |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2670 | ctx->next_ckchi_link = NULL; |
Christopher Faulet | 6af2fc6 | 2022-06-03 11:42:38 +0200 | [diff] [blame] | 2671 | ctx->old_entry = cafile_transaction.old_cafile_entry; |
| 2672 | ctx->new_entry = cafile_transaction.new_cafile_entry; |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2673 | ctx->cafile_type = CAFILE_CERT; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2674 | |
| 2675 | return 0; |
| 2676 | |
| 2677 | error: |
| 2678 | |
| 2679 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2680 | err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]); |
| 2681 | |
| 2682 | return cli_dynerr(appctx, err); |
| 2683 | } |
| 2684 | |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2685 | /* |
| 2686 | * 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] | 2687 | * set certificate authority (CA file) or a newly set Certificate Revocation |
| 2688 | * List (CRL), depending on the command being called. |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2689 | */ |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 2690 | 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] | 2691 | { |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2692 | struct commit_cacrlfile_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2693 | struct stconn *sc = appctx_sc(appctx); |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2694 | int y = 0; |
Christopher Faulet | 6af2fc6 | 2022-06-03 11:42:38 +0200 | [diff] [blame] | 2695 | struct cafile_entry *old_cafile_entry = ctx->old_entry; |
| 2696 | struct cafile_entry *new_cafile_entry = ctx->new_entry; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2697 | struct ckch_inst_link *ckchi_link; |
Christopher Faulet | ddc8e1c | 2022-06-03 09:00:09 +0200 | [diff] [blame] | 2698 | char *path; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2699 | |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 2700 | if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2701 | goto end; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2702 | |
Christopher Faulet | ddc8e1c | 2022-06-03 09:00:09 +0200 | [diff] [blame] | 2703 | /* The ctx was already validated by the ca-file/crl-file parsing |
| 2704 | * function. Entries can only be NULL in CACRL_ST_SUCCESS or |
| 2705 | * CACRL_ST_FIN states |
| 2706 | */ |
| 2707 | switch (ctx->cafile_type) { |
| 2708 | case CAFILE_CERT: |
Christopher Faulet | ddc8e1c | 2022-06-03 09:00:09 +0200 | [diff] [blame] | 2709 | path = cafile_transaction.path; |
| 2710 | break; |
| 2711 | case CAFILE_CRL: |
Christopher Faulet | ddc8e1c | 2022-06-03 09:00:09 +0200 | [diff] [blame] | 2712 | path = crlfile_transaction.path; |
| 2713 | break; |
Christopher Faulet | ea2c8c6 | 2022-06-03 16:37:31 +0200 | [diff] [blame] | 2714 | default: |
Willy Tarreau | d543ae0 | 2022-06-22 05:40:25 +0200 | [diff] [blame] | 2715 | path = NULL; |
Christopher Faulet | ea2c8c6 | 2022-06-03 16:37:31 +0200 | [diff] [blame] | 2716 | goto error; |
Christopher Faulet | ddc8e1c | 2022-06-03 09:00:09 +0200 | [diff] [blame] | 2717 | } |
| 2718 | |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2719 | while (1) { |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2720 | switch (ctx->state) { |
| 2721 | case CACRL_ST_INIT: |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2722 | /* This state just print the update message */ |
Christopher Faulet | ddc8e1c | 2022-06-03 09:00:09 +0200 | [diff] [blame] | 2723 | chunk_printf(&trash, "Committing %s", path); |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2724 | if (applet_putchk(appctx, &trash) == -1) |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2725 | goto yield; |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 2726 | |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2727 | ctx->state = CACRL_ST_GEN; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2728 | /* fallthrough */ |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2729 | case CACRL_ST_GEN: |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2730 | /* |
| 2731 | * This state generates the ckch instances with their |
| 2732 | * sni_ctxs and SSL_CTX. |
| 2733 | * |
| 2734 | * Since the SSL_CTX generation can be CPU consumer, we |
| 2735 | * yield every 10 instances. |
| 2736 | */ |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2737 | |
| 2738 | /* get the next ckchi to regenerate */ |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2739 | ckchi_link = ctx->next_ckchi_link; |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2740 | |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2741 | /* we didn't start yet, set it to the first elem */ |
| 2742 | if (ckchi_link == NULL) { |
| 2743 | ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list); |
| 2744 | /* Add the newly created cafile_entry to the tree so that |
| 2745 | * any new ckch instance created from now can use it. */ |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2746 | if (ssl_store_add_uncommitted_cafile_entry(new_cafile_entry)) { |
| 2747 | ctx->state = CACRL_ST_ERROR; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2748 | goto error; |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2749 | } |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2750 | } |
| 2751 | |
| 2752 | 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] | 2753 | struct ckch_inst *new_inst; |
| 2754 | |
| 2755 | /* save the next ckchi to compute */ |
| 2756 | ctx->next_ckchi_link = ckchi_link; |
| 2757 | |
| 2758 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 2759 | if (y >= 10) { |
| 2760 | applet_have_more_data(appctx); /* let's come back later */ |
| 2761 | goto yield; |
| 2762 | } |
| 2763 | |
| 2764 | /* display one dot per new instance */ |
| 2765 | if (applet_putstr(appctx, ".") == -1) |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2766 | goto yield; |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2767 | |
| 2768 | /* Rebuild a new ckch instance that uses the same ckch_store |
| 2769 | * than a reference ckchi instance but will use a new CA file. */ |
| 2770 | ctx->err = NULL; |
| 2771 | if (ckch_inst_rebuild(ckchi_link->ckch_inst->ckch_store, ckchi_link->ckch_inst, &new_inst, &ctx->err)) { |
| 2772 | ctx->state = CACRL_ST_ERROR; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2773 | goto error; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2774 | } |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2775 | |
| 2776 | y++; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2777 | } |
| 2778 | |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2779 | ctx->state = CACRL_ST_INSERT; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2780 | /* fallthrough */ |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2781 | case CACRL_ST_INSERT: |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2782 | /* The generation is finished, we can insert everything */ |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2783 | |
| 2784 | /* insert the new ckch_insts in the crtlist_entry */ |
| 2785 | list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) { |
| 2786 | if (ckchi_link->ckch_inst->crtlist_entry) |
| 2787 | LIST_INSERT(&ckchi_link->ckch_inst->crtlist_entry->ckch_inst, |
| 2788 | &ckchi_link->ckch_inst->by_crtlist_entry); |
| 2789 | } |
| 2790 | |
| 2791 | /* First, we insert every new SNIs in the trees, also replace the default_ctx */ |
| 2792 | list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) { |
| 2793 | __ssl_sock_load_new_ckch_instance(ckchi_link->ckch_inst); |
| 2794 | } |
| 2795 | |
| 2796 | /* delete the old sni_ctx, the old ckch_insts and the ckch_store */ |
| 2797 | list_for_each_entry(ckchi_link, &old_cafile_entry->ckch_inst_link, list) { |
| 2798 | __ckch_inst_free_locked(ckchi_link->ckch_inst); |
| 2799 | } |
| 2800 | |
| 2801 | |
| 2802 | /* Remove the old cafile entry from the tree */ |
| 2803 | ebmb_delete(&old_cafile_entry->node); |
| 2804 | ssl_store_delete_cafile_entry(old_cafile_entry); |
| 2805 | |
Christopher Faulet | 6af2fc6 | 2022-06-03 11:42:38 +0200 | [diff] [blame] | 2806 | ctx->old_entry = ctx->new_entry = NULL; |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2807 | ctx->state = CACRL_ST_SUCCESS; |
| 2808 | /* fallthrough */ |
| 2809 | case CACRL_ST_SUCCESS: |
| 2810 | if (applet_putstr(appctx, "\nSuccess!\n") == -1) |
| 2811 | goto yield; |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2812 | ctx->state = CACRL_ST_FIN; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2813 | /* fallthrough */ |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 2814 | case CACRL_ST_FIN: |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2815 | /* we achieved the transaction, we can set everything to NULL */ |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2816 | switch (ctx->cafile_type) { |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 2817 | case CAFILE_CERT: |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 2818 | cafile_transaction.old_cafile_entry = NULL; |
| 2819 | cafile_transaction.new_cafile_entry = NULL; |
Christopher Faulet | 1e00c7e | 2022-05-31 18:10:19 +0200 | [diff] [blame] | 2820 | cafile_transaction.path = NULL; |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 2821 | break; |
| 2822 | case CAFILE_CRL: |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 2823 | crlfile_transaction.old_crlfile_entry = NULL; |
| 2824 | crlfile_transaction.new_crlfile_entry = NULL; |
Christopher Faulet | 1e00c7e | 2022-05-31 18:10:19 +0200 | [diff] [blame] | 2825 | crlfile_transaction.path = NULL; |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 2826 | break; |
| 2827 | } |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2828 | goto end; |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2829 | |
| 2830 | case CACRL_ST_ERROR: |
| 2831 | error: |
| 2832 | chunk_printf(&trash, "\n%sFailed!\n", ctx->err); |
| 2833 | if (applet_putchk(appctx, &trash) == -1) |
| 2834 | goto yield; |
| 2835 | ctx->state = CACRL_ST_FIN; |
| 2836 | break; |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2837 | } |
| 2838 | } |
| 2839 | end: |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2840 | /* success: call the release function and don't come back */ |
| 2841 | return 1; |
| 2842 | yield: |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2843 | return 0; /* should come back */ |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2844 | } |
| 2845 | |
Remi Tricot-Le Breton | d5fd09d | 2021-03-11 10:22:52 +0100 | [diff] [blame] | 2846 | |
| 2847 | /* parsing function of 'abort ssl ca-file' */ |
| 2848 | static int cli_parse_abort_cafile(char **args, char *payload, struct appctx *appctx, void *private) |
| 2849 | { |
| 2850 | char *err = NULL; |
| 2851 | |
| 2852 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 2853 | return 1; |
| 2854 | |
| 2855 | if (!*args[3]) |
| 2856 | return cli_err(appctx, "'abort ssl ca-file' expects a filename\n"); |
| 2857 | |
| 2858 | /* The operations on the CKCH architecture are locked so we can |
| 2859 | * manipulate ckch_store and ckch_inst */ |
| 2860 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2861 | return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n"); |
| 2862 | |
| 2863 | if (!cafile_transaction.path) { |
| 2864 | memprintf(&err, "No ongoing transaction!\n"); |
| 2865 | goto error; |
| 2866 | } |
| 2867 | |
| 2868 | if (strcmp(cafile_transaction.path, args[3]) != 0) { |
| 2869 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", cafile_transaction.path, args[3]); |
| 2870 | goto error; |
| 2871 | } |
| 2872 | |
| 2873 | /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */ |
| 2874 | ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry); |
| 2875 | cafile_transaction.new_cafile_entry = NULL; |
| 2876 | cafile_transaction.old_cafile_entry = NULL; |
Christopher Faulet | 1e00c7e | 2022-05-31 18:10:19 +0200 | [diff] [blame] | 2877 | cafile_transaction.path = NULL; |
Remi Tricot-Le Breton | d5fd09d | 2021-03-11 10:22:52 +0100 | [diff] [blame] | 2878 | |
| 2879 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2880 | |
| 2881 | err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]); |
| 2882 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 2883 | |
| 2884 | error: |
| 2885 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 2886 | |
| 2887 | return cli_dynerr(appctx, err); |
| 2888 | } |
| 2889 | |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 2890 | /* 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] | 2891 | * It uses a commit_cacrlfile_ctx context. |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 2892 | */ |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2893 | static void cli_release_commit_cafile(struct appctx *appctx) |
| 2894 | { |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2895 | struct commit_cacrlfile_ctx *ctx = appctx->svcctx; |
Christopher Faulet | 6af2fc6 | 2022-06-03 11:42:38 +0200 | [diff] [blame] | 2896 | struct cafile_entry *new_cafile_entry = ctx->new_entry; |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 2897 | |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2898 | /* Remove the uncommitted cafile_entry from the tree. */ |
| 2899 | if (new_cafile_entry) { |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2900 | ebmb_delete(&new_cafile_entry->node); |
| 2901 | ssl_store_delete_cafile_entry(new_cafile_entry); |
| 2902 | } |
| 2903 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 2904 | ha_free(&ctx->err); |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 2908 | /* IO handler of details "show ssl ca-file <filename[:index]>". |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 2909 | * It uses a show_cafile_ctx context, and the global |
| 2910 | * cafile_transaction.new_cafile_entry in read-only. |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 2911 | */ |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2912 | static int cli_io_handler_show_cafile_detail(struct appctx *appctx) |
| 2913 | { |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 2914 | struct show_cafile_ctx *ctx = appctx->svcctx; |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 2915 | struct cafile_entry *cafile_entry = ctx->cur_cafile_entry; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2916 | struct buffer *out = alloc_trash_chunk(); |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2917 | int i = 0; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2918 | X509 *cert; |
| 2919 | STACK_OF(X509_OBJECT) *objs; |
| 2920 | int retval = 0; |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 2921 | int ca_index = ctx->ca_index; |
| 2922 | int show_all = ctx->show_all; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2923 | |
| 2924 | if (!out) |
| 2925 | goto end_no_putchk; |
| 2926 | |
| 2927 | chunk_appendf(out, "Filename: "); |
| 2928 | if (cafile_entry == cafile_transaction.new_cafile_entry) |
| 2929 | chunk_appendf(out, "*"); |
| 2930 | chunk_appendf(out, "%s\n", cafile_entry->path); |
| 2931 | |
| 2932 | chunk_appendf(out, "Status: "); |
| 2933 | if (!cafile_entry->ca_store) |
| 2934 | chunk_appendf(out, "Empty\n"); |
| 2935 | else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) |
| 2936 | chunk_appendf(out, "Unused\n"); |
| 2937 | else |
| 2938 | chunk_appendf(out, "Used\n"); |
| 2939 | |
| 2940 | if (!cafile_entry->ca_store) |
| 2941 | goto end; |
| 2942 | |
| 2943 | objs = X509_STORE_get0_objects(cafile_entry->ca_store); |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2944 | for (i = ca_index; i < sk_X509_OBJECT_num(objs); i++) { |
| 2945 | |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2946 | cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i)); |
| 2947 | if (!cert) |
| 2948 | continue; |
| 2949 | |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2950 | /* file starts at line 1 */ |
Remi Tricot-Le Breton | e8041fe | 2022-04-05 16:44:21 +0200 | [diff] [blame] | 2951 | chunk_appendf(out, " \nCertificate #%d:\n", i+1); |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2952 | retval = show_cert_detail(cert, NULL, out); |
| 2953 | if (retval < 0) |
| 2954 | goto end_no_putchk; |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2955 | else if (retval) |
| 2956 | goto yield; |
| 2957 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 2958 | if (applet_putchk(appctx, out) == -1) |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2959 | goto yield; |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2960 | |
| 2961 | if (!show_all) /* only need to dump one certificate */ |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2962 | goto end; |
| 2963 | } |
| 2964 | |
| 2965 | end: |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2966 | free_trash_chunk(out); |
| 2967 | return 1; /* end, don't come back */ |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2968 | |
| 2969 | end_no_putchk: |
| 2970 | free_trash_chunk(out); |
| 2971 | return 1; |
| 2972 | yield: |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2973 | /* save the current state */ |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 2974 | ctx->ca_index = i; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2975 | free_trash_chunk(out); |
| 2976 | return 0; /* should come back */ |
| 2977 | } |
| 2978 | |
| 2979 | |
Willy Tarreau | 0630579 | 2022-05-04 15:57:30 +0200 | [diff] [blame] | 2980 | /* parsing function for 'show ssl ca-file [cafile[:index]]'. |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 2981 | * It prepares a show_cafile_ctx context, and checks the global |
| 2982 | * cafile_transaction under the ckch_lock (read only). |
Willy Tarreau | 0630579 | 2022-05-04 15:57:30 +0200 | [diff] [blame] | 2983 | */ |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2984 | static int cli_parse_show_cafile(char **args, char *payload, struct appctx *appctx, void *private) |
| 2985 | { |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 2986 | 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] | 2987 | struct cafile_entry *cafile_entry; |
William Lallemand | 03a32e5 | 2022-04-26 18:17:15 +0200 | [diff] [blame] | 2988 | int ca_index = 0; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 2989 | char *colons; |
| 2990 | char *err = NULL; |
| 2991 | |
| 2992 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 2993 | return cli_err(appctx, "Can't allocate memory!\n"); |
| 2994 | |
| 2995 | /* The operations on the CKCH architecture are locked so we can |
| 2996 | * manipulate ckch_store and ckch_inst */ |
| 2997 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 2998 | return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n"); |
| 2999 | |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3000 | ctx->show_all = 1; /* show all certificates */ |
| 3001 | ctx->ca_index = 0; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3002 | /* check if there is a certificate to lookup */ |
| 3003 | if (*args[3]) { |
| 3004 | |
| 3005 | /* Look for an optional CA index after the CA file name */ |
| 3006 | colons = strchr(args[3], ':'); |
| 3007 | if (colons) { |
| 3008 | char *endptr; |
| 3009 | |
| 3010 | ca_index = strtol(colons + 1, &endptr, 10); |
| 3011 | /* Indexes start at 1 */ |
| 3012 | if (colons + 1 == endptr || *endptr != '\0' || ca_index <= 0) { |
| 3013 | memprintf(&err, "wrong CA index after colons in '%s'!", args[3]); |
| 3014 | goto error; |
| 3015 | } |
| 3016 | *colons = '\0'; |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3017 | ctx->ca_index = ca_index - 1; /* we start counting at 0 in the ca_store, but at 1 on the CLI */ |
| 3018 | ctx->show_all = 0; /* show only one certificate */ |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | if (*args[3] == '*') { |
| 3022 | if (!cafile_transaction.new_cafile_entry) |
| 3023 | goto error; |
| 3024 | |
| 3025 | cafile_entry = cafile_transaction.new_cafile_entry; |
| 3026 | |
| 3027 | if (strcmp(args[3] + 1, cafile_entry->path) != 0) |
| 3028 | goto error; |
| 3029 | |
| 3030 | } else { |
| 3031 | /* Get the "original" cafile_entry and not the |
| 3032 | * uncommitted one if it exists. */ |
| 3033 | if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CERT) |
| 3034 | goto error; |
| 3035 | } |
| 3036 | |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3037 | ctx->cur_cafile_entry = cafile_entry; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3038 | /* use the IO handler that shows details */ |
| 3039 | appctx->io_handler = cli_io_handler_show_cafile_detail; |
| 3040 | } |
| 3041 | |
| 3042 | return 0; |
| 3043 | |
| 3044 | error: |
| 3045 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3046 | if (err) |
| 3047 | return cli_dynerr(appctx, err); |
| 3048 | return cli_err(appctx, "Can't display the CA file : Not found!\n"); |
| 3049 | } |
| 3050 | |
| 3051 | |
| 3052 | /* release function of the 'show ssl ca-file' command */ |
| 3053 | static void cli_release_show_cafile(struct appctx *appctx) |
| 3054 | { |
| 3055 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3056 | } |
| 3057 | |
| 3058 | |
| 3059 | /* This function returns the number of certificates in a cafile_entry. */ |
| 3060 | static int get_certificate_count(struct cafile_entry *cafile_entry) |
| 3061 | { |
| 3062 | int cert_count = 0; |
| 3063 | STACK_OF(X509_OBJECT) *objs; |
| 3064 | |
| 3065 | if (cafile_entry && cafile_entry->ca_store) { |
| 3066 | objs = X509_STORE_get0_objects(cafile_entry->ca_store); |
| 3067 | if (objs) |
| 3068 | cert_count = sk_X509_OBJECT_num(objs); |
| 3069 | } |
| 3070 | return cert_count; |
| 3071 | } |
| 3072 | |
| 3073 | /* 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] | 3074 | * is managed in cli_io_handler_show_cafile_detail. |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3075 | * It uses a show_cafile_ctx and the global cafile_transaction.new_cafile_entry |
| 3076 | * in read-only. |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 3077 | */ |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3078 | static int cli_io_handler_show_cafile(struct appctx *appctx) |
| 3079 | { |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3080 | struct show_cafile_ctx *ctx = appctx->svcctx; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3081 | struct buffer *trash = alloc_trash_chunk(); |
| 3082 | struct ebmb_node *node; |
Christopher Faulet | 677cb4f | 2022-06-03 16:25:35 +0200 | [diff] [blame] | 3083 | struct cafile_entry *cafile_entry = NULL; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3084 | |
| 3085 | if (trash == NULL) |
| 3086 | return 1; |
| 3087 | |
Christopher Faulet | 5a2154b | 2022-06-03 10:42:48 +0200 | [diff] [blame] | 3088 | if (!ctx->old_cafile_entry && cafile_transaction.old_cafile_entry) { |
| 3089 | chunk_appendf(trash, "# transaction\n"); |
| 3090 | chunk_appendf(trash, "*%s", cafile_transaction.old_cafile_entry->path); |
| 3091 | chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_transaction.new_cafile_entry)); |
| 3092 | if (applet_putchk(appctx, trash) == -1) |
| 3093 | goto yield; |
| 3094 | ctx->old_cafile_entry = cafile_transaction.new_cafile_entry; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3095 | } |
| 3096 | |
| 3097 | /* First time in this io_handler. */ |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3098 | if (!ctx->cur_cafile_entry) { |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3099 | chunk_appendf(trash, "# filename\n"); |
| 3100 | node = ebmb_first(&cafile_tree); |
| 3101 | } else { |
| 3102 | /* We yielded during a previous call. */ |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3103 | node = &ctx->cur_cafile_entry->node; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | while (node) { |
| 3107 | cafile_entry = ebmb_entry(node, struct cafile_entry, node); |
| 3108 | if (cafile_entry->type == CAFILE_CERT) { |
| 3109 | chunk_appendf(trash, "%s", cafile_entry->path); |
| 3110 | |
| 3111 | chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_entry)); |
| 3112 | } |
| 3113 | |
| 3114 | node = ebmb_next(node); |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 3115 | if (applet_putchk(appctx, trash) == -1) |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3116 | goto yield; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3117 | } |
| 3118 | |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3119 | ctx->cur_cafile_entry = NULL; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3120 | free_trash_chunk(trash); |
| 3121 | return 1; |
| 3122 | yield: |
| 3123 | |
| 3124 | free_trash_chunk(trash); |
Willy Tarreau | 50c2f1e | 2022-05-04 19:26:59 +0200 | [diff] [blame] | 3125 | ctx->cur_cafile_entry = cafile_entry; |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3126 | return 0; /* should come back */ |
| 3127 | } |
| 3128 | |
Remi Tricot-Le Breton | c3a8477 | 2021-03-25 18:13:57 +0100 | [diff] [blame] | 3129 | /* parsing function of 'del ssl ca-file' */ |
| 3130 | static int cli_parse_del_cafile(char **args, char *payload, struct appctx *appctx, void *private) |
| 3131 | { |
| 3132 | struct cafile_entry *cafile_entry; |
| 3133 | char *err = NULL; |
| 3134 | char *filename; |
| 3135 | |
| 3136 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 3137 | return 1; |
| 3138 | |
| 3139 | if (!*args[3]) |
| 3140 | return cli_err(appctx, "'del ssl ca-file' expects a CA file name\n"); |
| 3141 | |
| 3142 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 3143 | return cli_err(appctx, "Can't delete the CA file!\nOperations on certificates are currently locked!\n"); |
| 3144 | |
| 3145 | filename = args[3]; |
| 3146 | |
Christopher Faulet | 1f08fa4 | 2022-05-31 18:06:30 +0200 | [diff] [blame] | 3147 | if (cafile_transaction.path && strcmp(cafile_transaction.path, filename) == 0) { |
| 3148 | memprintf(&err, "ongoing transaction for the CA file '%s'", filename); |
| 3149 | goto error; |
| 3150 | } |
| 3151 | |
Remi Tricot-Le Breton | c3a8477 | 2021-03-25 18:13:57 +0100 | [diff] [blame] | 3152 | cafile_entry = ssl_store_get_cafile_entry(filename, 0); |
| 3153 | if (!cafile_entry) { |
| 3154 | memprintf(&err, "CA file '%s' doesn't exist!\n", filename); |
| 3155 | goto error; |
| 3156 | } |
| 3157 | |
| 3158 | if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) { |
| 3159 | memprintf(&err, "CA file '%s' in use, can't be deleted!\n", filename); |
| 3160 | goto error; |
| 3161 | } |
| 3162 | |
| 3163 | /* Remove the cafile_entry from the tree */ |
| 3164 | ebmb_delete(&cafile_entry->node); |
| 3165 | ssl_store_delete_cafile_entry(cafile_entry); |
| 3166 | |
| 3167 | memprintf(&err, "CA file '%s' deleted!\n", filename); |
| 3168 | |
| 3169 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3170 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 3171 | |
| 3172 | error: |
| 3173 | memprintf(&err, "Can't remove the CA file: %s\n", err ? err : ""); |
| 3174 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3175 | return cli_dynerr(appctx, err); |
| 3176 | } |
| 3177 | |
Remi Tricot-Le Breton | 720e3b9 | 2021-04-26 11:00:42 +0200 | [diff] [blame] | 3178 | /* parsing function of 'new ssl crl-file' */ |
| 3179 | static int cli_parse_new_crlfile(char **args, char *payload, struct appctx *appctx, void *private) |
| 3180 | { |
| 3181 | struct cafile_entry *cafile_entry; |
| 3182 | char *err = NULL; |
| 3183 | char *path; |
| 3184 | |
| 3185 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 3186 | return 1; |
| 3187 | |
| 3188 | if (!*args[3]) |
| 3189 | return cli_err(appctx, "'new ssl crl-file' expects a filename\n"); |
| 3190 | |
| 3191 | path = args[3]; |
| 3192 | |
| 3193 | /* The operations on the CKCH architecture are locked so we can |
| 3194 | * manipulate ckch_store and ckch_inst */ |
| 3195 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
Remi Tricot-Le Breton | 444d702 | 2022-05-05 17:18:40 +0200 | [diff] [blame] | 3196 | 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] | 3197 | |
| 3198 | cafile_entry = ssl_store_get_cafile_entry(path, 0); |
| 3199 | if (cafile_entry) { |
| 3200 | memprintf(&err, "CRL file '%s' already exists!\n", path); |
| 3201 | goto error; |
| 3202 | } |
| 3203 | |
| 3204 | cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CRL); |
| 3205 | if (!cafile_entry) { |
| 3206 | memprintf(&err, "%sCannot allocate memory!\n", err ? err : ""); |
| 3207 | goto error; |
| 3208 | } |
| 3209 | |
| 3210 | /* Add the newly created cafile_entry to the tree so that |
| 3211 | * any new ckch instance created from now can use it. */ |
| 3212 | if (ssl_store_add_uncommitted_cafile_entry(cafile_entry)) |
| 3213 | goto error; |
| 3214 | |
| 3215 | memprintf(&err, "New CRL file created '%s'!\n", path); |
| 3216 | |
| 3217 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3218 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 3219 | error: |
| 3220 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3221 | return cli_dynerr(appctx, err); |
| 3222 | } |
| 3223 | |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3224 | /* Parsing function of `set ssl crl-file` */ |
| 3225 | static int cli_parse_set_crlfile(char **args, char *payload, struct appctx *appctx, void *private) |
| 3226 | { |
Christopher Faulet | 1f90f33 | 2022-06-03 16:34:30 +0200 | [diff] [blame] | 3227 | struct cafile_entry *old_crlfile_entry = NULL; |
| 3228 | struct cafile_entry *new_crlfile_entry = NULL; |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3229 | char *err = NULL; |
| 3230 | int errcode = 0; |
| 3231 | struct buffer *buf; |
| 3232 | |
| 3233 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 3234 | return 1; |
| 3235 | |
| 3236 | if (!*args[3] || !payload) |
Remi Tricot-Le Breton | 444d702 | 2022-05-05 17:18:40 +0200 | [diff] [blame] | 3237 | 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] | 3238 | |
| 3239 | /* The operations on the CKCH architecture are locked so we can |
| 3240 | * manipulate ckch_store and ckch_inst */ |
| 3241 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 3242 | return cli_err(appctx, "Can't update the CRL file!\nOperations on certificates are currently locked!\n"); |
| 3243 | |
| 3244 | if ((buf = alloc_trash_chunk()) == NULL) { |
| 3245 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 3246 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3247 | goto end; |
| 3248 | } |
| 3249 | |
| 3250 | if (!chunk_strcpy(buf, args[3])) { |
| 3251 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 3252 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3253 | goto end; |
| 3254 | } |
| 3255 | |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3256 | old_crlfile_entry = NULL; |
| 3257 | new_crlfile_entry = NULL; |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3258 | |
| 3259 | /* if there is an ongoing transaction */ |
| 3260 | if (crlfile_transaction.path) { |
| 3261 | /* if there is an ongoing transaction, check if this is the same file */ |
| 3262 | if (strcmp(crlfile_transaction.path, buf->area) != 0) { |
| 3263 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, buf->area); |
| 3264 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3265 | goto end; |
| 3266 | } |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3267 | old_crlfile_entry = crlfile_transaction.old_crlfile_entry; |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3268 | } |
| 3269 | else { |
| 3270 | /* lookup for the certificate in the tree */ |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3271 | 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] | 3272 | } |
| 3273 | |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3274 | if (!old_crlfile_entry) { |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3275 | memprintf(&err, "%sCan't replace a CRL file which is not referenced by the configuration!\n", |
| 3276 | err ? err : ""); |
| 3277 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3278 | goto end; |
| 3279 | } |
| 3280 | |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3281 | /* Create a new cafile_entry without adding it to the cafile tree. */ |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3282 | new_crlfile_entry = ssl_store_create_cafile_entry(old_crlfile_entry->path, NULL, CAFILE_CRL); |
| 3283 | if (!new_crlfile_entry) { |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3284 | memprintf(&err, "%sCannot allocate memory!\n", err ? err : ""); |
| 3285 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3286 | goto end; |
| 3287 | } |
| 3288 | |
| 3289 | /* Fill the new entry with the new CRL. */ |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3290 | 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] | 3291 | memprintf(&err, "%sInvalid payload\n", err ? err : ""); |
| 3292 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3293 | goto end; |
| 3294 | } |
| 3295 | |
| 3296 | /* we succeed, we can save the crl in the transaction */ |
| 3297 | |
Remi Tricot-Le Breton | 444d702 | 2022-05-05 17:18:40 +0200 | [diff] [blame] | 3298 | /* if there wasn't a transaction, update the old CRL */ |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3299 | if (!crlfile_transaction.old_crlfile_entry) { |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3300 | crlfile_transaction.old_crlfile_entry = old_crlfile_entry; |
| 3301 | crlfile_transaction.path = old_crlfile_entry->path; |
Remi Tricot-Le Breton | 444d702 | 2022-05-05 17:18:40 +0200 | [diff] [blame] | 3302 | 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] | 3303 | } else { |
Remi Tricot-Le Breton | 444d702 | 2022-05-05 17:18:40 +0200 | [diff] [blame] | 3304 | 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] | 3305 | } |
| 3306 | |
| 3307 | /* free the previous CRL file if there was a transaction */ |
| 3308 | ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry); |
| 3309 | |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3310 | crlfile_transaction.new_crlfile_entry = new_crlfile_entry; |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3311 | |
| 3312 | /* creates the SNI ctxs later in the IO handler */ |
| 3313 | |
| 3314 | end: |
| 3315 | free_trash_chunk(buf); |
| 3316 | |
| 3317 | if (errcode & ERR_CODE) { |
Christopher Faulet | d6c66f0 | 2022-06-03 11:59:10 +0200 | [diff] [blame] | 3318 | ssl_store_delete_cafile_entry(new_crlfile_entry); |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3319 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3320 | return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3])); |
| 3321 | } else { |
| 3322 | |
| 3323 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3324 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 3325 | } |
| 3326 | } |
| 3327 | |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 3328 | /* Parsing function of 'commit ssl crl-file'. |
| 3329 | * It uses a commit_cacrlfile_ctx that's also shared with "commit ssl ca-file". |
| 3330 | */ |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3331 | static int cli_parse_commit_crlfile(char **args, char *payload, struct appctx *appctx, void *private) |
| 3332 | { |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 3333 | 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] | 3334 | char *err = NULL; |
| 3335 | |
| 3336 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 3337 | return 1; |
| 3338 | |
| 3339 | if (!*args[3]) |
| 3340 | return cli_err(appctx, "'commit ssl ca-file expects a filename\n"); |
| 3341 | |
| 3342 | /* The operations on the CKCH architecture are locked so we can |
| 3343 | * manipulate ckch_store and ckch_inst */ |
| 3344 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 3345 | return cli_err(appctx, "Can't commit the CRL file!\nOperations on certificates are currently locked!\n"); |
| 3346 | |
| 3347 | if (!crlfile_transaction.path) { |
| 3348 | memprintf(&err, "No ongoing transaction! !\n"); |
| 3349 | goto error; |
| 3350 | } |
| 3351 | |
| 3352 | if (strcmp(crlfile_transaction.path, args[3]) != 0) { |
| 3353 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, args[3]); |
| 3354 | goto error; |
| 3355 | } |
| 3356 | /* init the appctx structure */ |
Willy Tarreau | 1d6dd80 | 2022-05-05 08:17:29 +0200 | [diff] [blame] | 3357 | ctx->state = CACRL_ST_INIT; |
Christopher Faulet | f814c4a | 2022-06-03 11:32:05 +0200 | [diff] [blame] | 3358 | ctx->next_ckchi_link = NULL; |
Christopher Faulet | 6af2fc6 | 2022-06-03 11:42:38 +0200 | [diff] [blame] | 3359 | ctx->old_entry = crlfile_transaction.old_crlfile_entry; |
| 3360 | ctx->new_entry = crlfile_transaction.new_crlfile_entry; |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 3361 | ctx->cafile_type = CAFILE_CRL; |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3362 | |
| 3363 | return 0; |
| 3364 | |
| 3365 | error: |
| 3366 | |
| 3367 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3368 | err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]); |
| 3369 | |
| 3370 | return cli_dynerr(appctx, err); |
| 3371 | } |
| 3372 | |
| 3373 | |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 3374 | /* release function of the `commit ssl crl-file' command, free things and unlock the spinlock. |
| 3375 | * it uses a commit_cacrlfile_ctx that's the same as for "commit ssl ca-file". |
| 3376 | */ |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3377 | static void cli_release_commit_crlfile(struct appctx *appctx) |
| 3378 | { |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 3379 | struct commit_cacrlfile_ctx *ctx = appctx->svcctx; |
Christopher Faulet | 6af2fc6 | 2022-06-03 11:42:38 +0200 | [diff] [blame] | 3380 | struct cafile_entry *new_crlfile_entry = ctx->new_entry; |
Willy Tarreau | dec23dc | 2022-05-04 20:25:05 +0200 | [diff] [blame] | 3381 | |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 3382 | /* Remove the uncommitted cafile_entry from the tree. */ |
| 3383 | if (new_crlfile_entry) { |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3384 | ebmb_delete(&new_crlfile_entry->node); |
| 3385 | ssl_store_delete_cafile_entry(new_crlfile_entry); |
| 3386 | } |
| 3387 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
Christopher Faulet | e9c3bd1 | 2022-05-31 17:51:06 +0200 | [diff] [blame] | 3388 | ha_free(&ctx->err); |
Remi Tricot-Le Breton | a51b339 | 2021-04-20 17:38:14 +0200 | [diff] [blame] | 3389 | } |
| 3390 | |
Remi Tricot-Le Breton | 720e3b9 | 2021-04-26 11:00:42 +0200 | [diff] [blame] | 3391 | /* parsing function of 'del ssl crl-file' */ |
| 3392 | static int cli_parse_del_crlfile(char **args, char *payload, struct appctx *appctx, void *private) |
| 3393 | { |
| 3394 | struct cafile_entry *cafile_entry; |
| 3395 | char *err = NULL; |
| 3396 | char *filename; |
| 3397 | |
| 3398 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 3399 | return 1; |
| 3400 | |
| 3401 | if (!*args[3]) |
| 3402 | return cli_err(appctx, "'del ssl crl-file' expects a CRL file name\n"); |
| 3403 | |
| 3404 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 3405 | return cli_err(appctx, "Can't delete the CRL file!\nOperations on certificates are currently locked!\n"); |
| 3406 | |
| 3407 | filename = args[3]; |
| 3408 | |
Christopher Faulet | 1f08fa4 | 2022-05-31 18:06:30 +0200 | [diff] [blame] | 3409 | if (crlfile_transaction.path && strcmp(crlfile_transaction.path, filename) == 0) { |
| 3410 | memprintf(&err, "ongoing transaction for the CRL file '%s'", filename); |
| 3411 | goto error; |
| 3412 | } |
| 3413 | |
Remi Tricot-Le Breton | 720e3b9 | 2021-04-26 11:00:42 +0200 | [diff] [blame] | 3414 | cafile_entry = ssl_store_get_cafile_entry(filename, 0); |
| 3415 | if (!cafile_entry) { |
| 3416 | memprintf(&err, "CRL file '%s' doesn't exist!\n", filename); |
| 3417 | goto error; |
| 3418 | } |
| 3419 | if (cafile_entry->type != CAFILE_CRL) { |
| 3420 | memprintf(&err, "'del ssl crl-file' does not work on CA files!\n"); |
| 3421 | goto error; |
| 3422 | } |
| 3423 | |
| 3424 | if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) { |
| 3425 | memprintf(&err, "CRL file '%s' in use, can't be deleted!\n", filename); |
| 3426 | goto error; |
| 3427 | } |
| 3428 | |
| 3429 | /* Remove the cafile_entry from the tree */ |
| 3430 | ebmb_delete(&cafile_entry->node); |
| 3431 | ssl_store_delete_cafile_entry(cafile_entry); |
| 3432 | |
| 3433 | memprintf(&err, "CRL file '%s' deleted!\n", filename); |
| 3434 | |
| 3435 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3436 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 3437 | |
| 3438 | error: |
| 3439 | memprintf(&err, "Can't remove the CRL file: %s\n", err ? err : ""); |
| 3440 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3441 | return cli_dynerr(appctx, err); |
| 3442 | } |
| 3443 | |
Remi Tricot-Le Breton | eef8e7b | 2021-04-20 17:42:02 +0200 | [diff] [blame] | 3444 | /* parsing function of 'abort ssl crl-file' */ |
| 3445 | static int cli_parse_abort_crlfile(char **args, char *payload, struct appctx *appctx, void *private) |
| 3446 | { |
| 3447 | char *err = NULL; |
| 3448 | |
| 3449 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 3450 | return 1; |
| 3451 | |
| 3452 | if (!*args[3]) |
| 3453 | return cli_err(appctx, "'abort ssl crl-file' expects a filename\n"); |
| 3454 | |
| 3455 | /* The operations on the CKCH architecture are locked so we can |
| 3456 | * manipulate ckch_store and ckch_inst */ |
| 3457 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 3458 | return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n"); |
| 3459 | |
| 3460 | if (!crlfile_transaction.path) { |
| 3461 | memprintf(&err, "No ongoing transaction!\n"); |
| 3462 | goto error; |
| 3463 | } |
| 3464 | |
| 3465 | if (strcmp(crlfile_transaction.path, args[3]) != 0) { |
| 3466 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", crlfile_transaction.path, args[3]); |
| 3467 | goto error; |
| 3468 | } |
| 3469 | |
| 3470 | /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */ |
| 3471 | ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry); |
| 3472 | crlfile_transaction.new_crlfile_entry = NULL; |
| 3473 | crlfile_transaction.old_crlfile_entry = NULL; |
Christopher Faulet | 1e00c7e | 2022-05-31 18:10:19 +0200 | [diff] [blame] | 3474 | crlfile_transaction.path = NULL; |
Remi Tricot-Le Breton | eef8e7b | 2021-04-20 17:42:02 +0200 | [diff] [blame] | 3475 | |
| 3476 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3477 | |
| 3478 | err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]); |
| 3479 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 3480 | |
| 3481 | error: |
| 3482 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3483 | |
| 3484 | return cli_dynerr(appctx, err); |
| 3485 | } |
| 3486 | |
Remi Tricot-Le Breton | 2a22e16 | 2021-03-16 11:19:33 +0100 | [diff] [blame] | 3487 | |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3488 | /* |
| 3489 | * Display a Certificate Resignation List's information. |
| 3490 | * The information displayed is inspired by the output of 'openssl crl -in |
| 3491 | * crl.pem -text'. |
| 3492 | * Returns 0 in case of success. |
| 3493 | */ |
| 3494 | static int show_crl_detail(X509_CRL *crl, struct buffer *out) |
| 3495 | { |
| 3496 | BIO *bio = NULL; |
| 3497 | struct buffer *tmp = alloc_trash_chunk(); |
| 3498 | long version; |
| 3499 | X509_NAME *issuer; |
| 3500 | int write = -1; |
| 3501 | STACK_OF(X509_REVOKED) *rev = NULL; |
| 3502 | X509_REVOKED *rev_entry = NULL; |
| 3503 | int i; |
| 3504 | |
| 3505 | if (!tmp) |
| 3506 | return -1; |
| 3507 | |
| 3508 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 3509 | goto end; |
| 3510 | |
| 3511 | /* Version (as displayed by 'openssl crl') */ |
| 3512 | version = X509_CRL_get_version(crl); |
| 3513 | chunk_appendf(out, "Version %ld\n", version + 1); |
| 3514 | |
| 3515 | /* Signature Algorithm */ |
| 3516 | chunk_appendf(out, "Signature Algorithm: %s\n", OBJ_nid2ln(X509_CRL_get_signature_nid(crl))); |
| 3517 | |
| 3518 | /* Issuer */ |
| 3519 | chunk_appendf(out, "Issuer: "); |
| 3520 | if ((issuer = X509_CRL_get_issuer(crl)) == NULL) |
| 3521 | goto end; |
| 3522 | if ((ssl_sock_get_dn_oneline(issuer, tmp)) == -1) |
| 3523 | goto end; |
| 3524 | *(tmp->area + tmp->data) = '\0'; |
| 3525 | chunk_appendf(out, "%s\n", tmp->area); |
| 3526 | |
| 3527 | /* Last Update */ |
| 3528 | chunk_appendf(out, "Last Update: "); |
| 3529 | chunk_reset(tmp); |
Remi Tricot-Le Breton | d75b99e | 2021-05-17 11:45:55 +0200 | [diff] [blame] | 3530 | if (BIO_reset(bio) == -1) |
| 3531 | goto end; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3532 | if (ASN1_TIME_print(bio, X509_CRL_get0_lastUpdate(crl)) == 0) |
| 3533 | goto end; |
| 3534 | write = BIO_read(bio, tmp->area, tmp->size-1); |
| 3535 | tmp->area[write] = '\0'; |
| 3536 | chunk_appendf(out, "%s\n", tmp->area); |
| 3537 | |
| 3538 | |
| 3539 | /* Next Update */ |
| 3540 | chunk_appendf(out, "Next Update: "); |
| 3541 | chunk_reset(tmp); |
Remi Tricot-Le Breton | d75b99e | 2021-05-17 11:45:55 +0200 | [diff] [blame] | 3542 | if (BIO_reset(bio) == -1) |
| 3543 | goto end; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3544 | if (ASN1_TIME_print(bio, X509_CRL_get0_nextUpdate(crl)) == 0) |
| 3545 | goto end; |
| 3546 | write = BIO_read(bio, tmp->area, tmp->size-1); |
| 3547 | tmp->area[write] = '\0'; |
| 3548 | chunk_appendf(out, "%s\n", tmp->area); |
| 3549 | |
| 3550 | |
| 3551 | /* Revoked Certificates */ |
| 3552 | rev = X509_CRL_get_REVOKED(crl); |
| 3553 | if (sk_X509_REVOKED_num(rev) > 0) |
| 3554 | chunk_appendf(out, "Revoked Certificates:\n"); |
| 3555 | else |
| 3556 | chunk_appendf(out, "No Revoked Certificates.\n"); |
| 3557 | |
| 3558 | for (i = 0; i < sk_X509_REVOKED_num(rev); i++) { |
| 3559 | rev_entry = sk_X509_REVOKED_value(rev, i); |
| 3560 | |
| 3561 | /* Serial Number and Revocation Date */ |
Remi Tricot-Le Breton | d75b99e | 2021-05-17 11:45:55 +0200 | [diff] [blame] | 3562 | if (BIO_reset(bio) == -1) |
| 3563 | goto end; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3564 | BIO_printf(bio , " Serial Number: "); |
Remi Tricot-Le Breton | 18c7d83 | 2021-05-17 18:38:34 +0200 | [diff] [blame] | 3565 | 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] | 3566 | BIO_printf(bio, "\n Revocation Date: "); |
Remi Tricot-Le Breton | a6b2784 | 2021-05-18 10:06:00 +0200 | [diff] [blame] | 3567 | if (ASN1_TIME_print(bio, X509_REVOKED_get0_revocationDate(rev_entry)) == 0) |
| 3568 | goto end; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3569 | BIO_printf(bio, "\n"); |
| 3570 | |
| 3571 | write = BIO_read(bio, tmp->area, tmp->size-1); |
| 3572 | tmp->area[write] = '\0'; |
| 3573 | chunk_appendf(out, "%s", tmp->area); |
| 3574 | } |
| 3575 | |
| 3576 | end: |
| 3577 | free_trash_chunk(tmp); |
| 3578 | if (bio) |
| 3579 | BIO_free(bio); |
| 3580 | |
| 3581 | return 0; |
| 3582 | } |
| 3583 | |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 3584 | /* IO handler of details "show ssl crl-file <filename[:index]>". |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3585 | * It uses show_crlfile_ctx and the global |
| 3586 | * crlfile_transaction.new_cafile_entry in read-only. |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 3587 | */ |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3588 | static int cli_io_handler_show_crlfile_detail(struct appctx *appctx) |
| 3589 | { |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3590 | struct show_crlfile_ctx *ctx = appctx->svcctx; |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3591 | struct cafile_entry *cafile_entry = ctx->cafile_entry; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3592 | struct buffer *out = alloc_trash_chunk(); |
| 3593 | int i; |
| 3594 | X509_CRL *crl; |
| 3595 | STACK_OF(X509_OBJECT) *objs; |
| 3596 | int retval = 0; |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3597 | int index = ctx->index; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3598 | |
| 3599 | if (!out) |
| 3600 | goto end_no_putchk; |
| 3601 | |
| 3602 | chunk_appendf(out, "Filename: "); |
| 3603 | if (cafile_entry == crlfile_transaction.new_crlfile_entry) |
| 3604 | chunk_appendf(out, "*"); |
| 3605 | chunk_appendf(out, "%s\n", cafile_entry->path); |
| 3606 | |
| 3607 | chunk_appendf(out, "Status: "); |
| 3608 | if (!cafile_entry->ca_store) |
| 3609 | chunk_appendf(out, "Empty\n"); |
| 3610 | else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) |
| 3611 | chunk_appendf(out, "Unused\n"); |
| 3612 | else |
| 3613 | chunk_appendf(out, "Used\n"); |
| 3614 | |
| 3615 | if (!cafile_entry->ca_store) |
| 3616 | goto end; |
| 3617 | |
| 3618 | objs = X509_STORE_get0_objects(cafile_entry->ca_store); |
| 3619 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 3620 | crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i)); |
| 3621 | if (!crl) |
| 3622 | continue; |
| 3623 | |
| 3624 | /* CRL indexes start at 1 on the CLI output. */ |
| 3625 | if (index && index-1 != i) |
| 3626 | continue; |
| 3627 | |
Remi Tricot-Le Breton | e8041fe | 2022-04-05 16:44:21 +0200 | [diff] [blame] | 3628 | chunk_appendf(out, " \nCertificate Revocation List #%d:\n", i+1); |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3629 | retval = show_crl_detail(crl, out); |
| 3630 | if (retval < 0) |
| 3631 | goto end_no_putchk; |
| 3632 | else if (retval || index) |
| 3633 | goto end; |
| 3634 | } |
| 3635 | |
| 3636 | end: |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 3637 | if (applet_putchk(appctx, out) == -1) |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3638 | goto yield; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3639 | |
| 3640 | end_no_putchk: |
| 3641 | free_trash_chunk(out); |
| 3642 | return 1; |
| 3643 | yield: |
| 3644 | free_trash_chunk(out); |
| 3645 | return 0; /* should come back */ |
| 3646 | } |
| 3647 | |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 3648 | /* parsing function for 'show ssl crl-file [crlfile[:index]]'. |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3649 | * It sets the context to a show_crlfile_ctx, and the global |
Willy Tarreau | 821c3b0 | 2022-05-04 15:47:39 +0200 | [diff] [blame] | 3650 | * cafile_transaction.new_crlfile_entry under the ckch_lock. |
| 3651 | */ |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3652 | static int cli_parse_show_crlfile(char **args, char *payload, struct appctx *appctx, void *private) |
| 3653 | { |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3654 | 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] | 3655 | struct cafile_entry *cafile_entry; |
| 3656 | long index = 0; |
| 3657 | char *colons; |
| 3658 | char *err = NULL; |
| 3659 | |
| 3660 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 3661 | return cli_err(appctx, "Can't allocate memory!\n"); |
| 3662 | |
| 3663 | /* The operations on the CKCH architecture are locked so we can |
| 3664 | * manipulate ckch_store and ckch_inst */ |
| 3665 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 3666 | return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n"); |
| 3667 | |
| 3668 | /* check if there is a certificate to lookup */ |
| 3669 | if (*args[3]) { |
| 3670 | |
| 3671 | /* Look for an optional index after the CRL file name */ |
| 3672 | colons = strchr(args[3], ':'); |
| 3673 | if (colons) { |
| 3674 | char *endptr; |
| 3675 | |
| 3676 | index = strtol(colons + 1, &endptr, 10); |
| 3677 | /* Indexes start at 1 */ |
| 3678 | if (colons + 1 == endptr || *endptr != '\0' || index <= 0) { |
| 3679 | memprintf(&err, "wrong CRL index after colons in '%s'!", args[3]); |
| 3680 | goto error; |
| 3681 | } |
| 3682 | *colons = '\0'; |
| 3683 | } |
| 3684 | |
| 3685 | if (*args[3] == '*') { |
| 3686 | if (!crlfile_transaction.new_crlfile_entry) |
| 3687 | goto error; |
| 3688 | |
| 3689 | cafile_entry = crlfile_transaction.new_crlfile_entry; |
| 3690 | |
| 3691 | if (strcmp(args[3] + 1, cafile_entry->path) != 0) |
| 3692 | goto error; |
| 3693 | |
| 3694 | } else { |
| 3695 | /* Get the "original" cafile_entry and not the |
| 3696 | * uncommitted one if it exists. */ |
| 3697 | if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CRL) |
| 3698 | goto error; |
| 3699 | } |
| 3700 | |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3701 | ctx->cafile_entry = cafile_entry; |
| 3702 | ctx->index = index; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3703 | /* use the IO handler that shows details */ |
| 3704 | appctx->io_handler = cli_io_handler_show_crlfile_detail; |
| 3705 | } |
| 3706 | |
| 3707 | return 0; |
| 3708 | |
| 3709 | error: |
| 3710 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3711 | if (err) |
| 3712 | return cli_dynerr(appctx, err); |
Remi Tricot-Le Breton | 444d702 | 2022-05-05 17:18:40 +0200 | [diff] [blame] | 3713 | 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] | 3714 | } |
| 3715 | |
| 3716 | /* IO handler of "show ssl crl-file". The command taking a specific CRL file name |
| 3717 | * is managed in cli_io_handler_show_crlfile_detail. */ |
| 3718 | static int cli_io_handler_show_crlfile(struct appctx *appctx) |
| 3719 | { |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3720 | struct show_crlfile_ctx *ctx = appctx->svcctx; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3721 | struct buffer *trash = alloc_trash_chunk(); |
| 3722 | struct ebmb_node *node; |
Christopher Faulet | 88041b3 | 2022-06-03 16:26:56 +0200 | [diff] [blame] | 3723 | struct cafile_entry *cafile_entry = NULL; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3724 | |
| 3725 | if (trash == NULL) |
| 3726 | return 1; |
| 3727 | |
Christopher Faulet | 9a99e54 | 2022-06-03 10:32:18 +0200 | [diff] [blame] | 3728 | if (!ctx->old_crlfile_entry && crlfile_transaction.old_crlfile_entry) { |
| 3729 | chunk_appendf(trash, "# transaction\n"); |
| 3730 | chunk_appendf(trash, "*%s\n", crlfile_transaction.old_crlfile_entry->path); |
| 3731 | if (applet_putchk(appctx, trash) == -1) |
| 3732 | goto yield; |
| 3733 | ctx->old_crlfile_entry = crlfile_transaction.old_crlfile_entry; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3734 | } |
| 3735 | |
| 3736 | /* First time in this io_handler. */ |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3737 | if (!ctx->cafile_entry) { |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3738 | chunk_appendf(trash, "# filename\n"); |
| 3739 | node = ebmb_first(&cafile_tree); |
| 3740 | } else { |
| 3741 | /* We yielded during a previous call. */ |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3742 | node = &ctx->cafile_entry->node; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3743 | } |
| 3744 | |
| 3745 | while (node) { |
| 3746 | cafile_entry = ebmb_entry(node, struct cafile_entry, node); |
| 3747 | if (cafile_entry->type == CAFILE_CRL) { |
| 3748 | chunk_appendf(trash, "%s\n", cafile_entry->path); |
| 3749 | } |
| 3750 | |
| 3751 | node = ebmb_next(node); |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 3752 | if (applet_putchk(appctx, trash) == -1) |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3753 | goto yield; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3754 | } |
| 3755 | |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3756 | ctx->cafile_entry = NULL; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3757 | free_trash_chunk(trash); |
| 3758 | return 1; |
| 3759 | yield: |
| 3760 | |
| 3761 | free_trash_chunk(trash); |
Willy Tarreau | f3e8b3e | 2022-05-04 19:38:57 +0200 | [diff] [blame] | 3762 | ctx->cafile_entry = cafile_entry; |
Remi Tricot-Le Breton | 51e28b6 | 2021-04-20 17:58:01 +0200 | [diff] [blame] | 3763 | return 0; /* should come back */ |
| 3764 | } |
| 3765 | |
| 3766 | |
| 3767 | /* release function of the 'show ssl crl-file' command */ |
| 3768 | static void cli_release_show_crlfile(struct appctx *appctx) |
| 3769 | { |
| 3770 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 3771 | } |
| 3772 | |
| 3773 | |
William Lallemand | ee8530c | 2020-06-23 18:19:42 +0200 | [diff] [blame] | 3774 | void ckch_deinit() |
| 3775 | { |
| 3776 | struct eb_node *node, *next; |
| 3777 | struct ckch_store *store; |
William Lallemand | b0c4827 | 2022-04-26 15:44:53 +0200 | [diff] [blame] | 3778 | struct ebmb_node *canode; |
William Lallemand | ee8530c | 2020-06-23 18:19:42 +0200 | [diff] [blame] | 3779 | |
William Lallemand | b0c4827 | 2022-04-26 15:44:53 +0200 | [diff] [blame] | 3780 | /* deinit the ckch stores */ |
William Lallemand | ee8530c | 2020-06-23 18:19:42 +0200 | [diff] [blame] | 3781 | node = eb_first(&ckchs_tree); |
| 3782 | while (node) { |
| 3783 | next = eb_next(node); |
| 3784 | store = ebmb_entry(node, struct ckch_store, node); |
| 3785 | ckch_store_free(store); |
| 3786 | node = next; |
| 3787 | } |
William Lallemand | b0c4827 | 2022-04-26 15:44:53 +0200 | [diff] [blame] | 3788 | |
| 3789 | /* deinit the ca-file store */ |
| 3790 | canode = ebmb_first(&cafile_tree); |
| 3791 | while (canode) { |
| 3792 | struct cafile_entry *entry = NULL; |
| 3793 | |
| 3794 | entry = ebmb_entry(canode, struct cafile_entry, node); |
| 3795 | canode = ebmb_next(canode); |
| 3796 | ssl_store_delete_cafile_entry(entry); |
| 3797 | } |
William Lallemand | ee8530c | 2020-06-23 18:19:42 +0200 | [diff] [blame] | 3798 | } |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 3799 | |
| 3800 | /* register cli keywords */ |
| 3801 | static struct cli_kw_list cli_kws = {{ },{ |
Remi Tricot-Le Breton | a32a68b | 2021-02-24 17:35:43 +0100 | [diff] [blame] | 3802 | { { "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 }, |
| 3803 | { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL }, |
| 3804 | { { "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 }, |
| 3805 | { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL }, |
| 3806 | { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL }, |
| 3807 | { { "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 }, |
| 3808 | |
Amaury Denoyelle | b11ad9e | 2021-05-21 11:01:10 +0200 | [diff] [blame] | 3809 | { { "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] | 3810 | { { "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] | 3811 | { { "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] | 3812 | { { "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] | 3813 | { { "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] | 3814 | { { "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] | 3815 | |
Remi Tricot-Le Breton | 720e3b9 | 2021-04-26 11:00:42 +0200 | [diff] [blame] | 3816 | { { "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] | 3817 | { { "set", "ssl", "crl-file", NULL }, "set ssl crl-file <crlfile> <payload> : replace a CRL file", cli_parse_set_crlfile, NULL, NULL }, |
| 3818 | { { "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] | 3819 | { { "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] | 3820 | { { "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] | 3821 | { { "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] | 3822 | { { NULL }, NULL, NULL, NULL } |
| 3823 | }}; |
| 3824 | |
| 3825 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 3826 | |