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