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