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